obsolete.computer/geekery/

Quick GameMode Hacks

GameMode is a daemon that sits waiting for you to play a game on your Linux PC, and when you do it springs to action and modifies a bunch of system parameters to make the game run better. A couple of things that I always wanted it to do: to pause Redshift, and to pause XMRig. Turns out the solutions weren't terribly hard, but especially for the latter I figured I'd save someone a few minutes and show you how I accomplished both of these.

Pausing Redshift

I was hoping for a more elegant solution, but the answer was evident on the Redshift page in the Archlinux Wiki:

The color adjustments done by Redshift can be temporarily toggled on and off by sending it the USR1 signal:

$ pkill -USR1 redshift

So I simply added the appropriate entries to the end of my /etc/gamemode.ini:

[custom]
; Custom scripts (executed using the shell) when gamemode starts and ends
start=notify-send "GameMode started"
     /usr/bin/pkill -USR1 redshift-gtk

end=notify-send "GameMode ended"
     /usr/bin/pkill -USR1 redshift-gtk

Ideally, I wanted to specify on and off, rather than a toggle, because I could see the possibility of creating an incorrect state. But this seems to work well enough.

Pausing XMRig

This one took a bit more digging. But the solution was apparent: I had to communicate with the miner via its json API. First, I had to enable the API in my config.json:

{
    "api": {
        "id": 1,
        "worker-id": null
    },
    "http": {
        "enabled": true,
        "host": "127.0.0.1",
        "port": 9080,
        "access-token": "1234", //pick your own access token here
        "restricted": false
    },

    ...

That was easy enough. The tricky part was crafting the right curl command to talk to it:

[custom]
; Custom scripts (executed using the shell) when gamemode starts and ends
start=notify-send "GameMode started"
     /usr/bin/curl --json '{"method":"pause","id":1}' -H "Authorization: Bearer 1234" http://127.0.0.1:9080/json_rpc

end=notify-send "GameMode ended"
     /usr/bin/curl --json '{"method":"resume","id":1}' -H "Authorization: Bearer 1234" http://127.0.0.1:9080/json_rpc

This ultimately worked for me but you may need to use a different port if you're doing local web development. Also I recommend coming up with a different 'access token' though I'm not really sure what the real risk is since it's just listening locally... I guess another app could mess with your miner but if that's happening you've got other security issues.

So that's it... now when I fire up a game from Steam that supports game mode, it automatically pauses Redshift and XMRig, along with all the other usual system tweaks. Makes for a hassle-free gaming experience. Now all I need is some time to play games.