Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ConfineMouse option
  • Loading branch information
CyberShadow committed Oct 2, 2017
1 parent 62fca4e commit 54a07fb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -58,6 +58,7 @@ Name | Values | Description
`FilterFocus` | `0`/`1` | Boolean - Filter out `FocusOut` events, making games think they always have focus.
`NoMouseGrab` | `0`/`1` | Boolean - Filter out `GrabPointer` requests, preventing games from exclusively grabbing the mouse pointer.
`NoKeyboardGrab` | `0`/`1` | Boolean - Filter out `GrabKeyboard` requests, preventing games from exclusively grabbing the keyboard, and thus disabling global hotkeys.
`ConfineMouse` | `0`/`1` | Boolean - Confine the mouse to the program's window while it is focused. Focus must then be changed with the keyboard. Experimental.
`MainX`/`Y` | Integer | The X11 coordinates of your primary monitor (or left-top-most monitor to be used for games)
`MainW`/`H` | Integer | The resolution of your primary monitor (or total resolution of monitors to be used for games)
`DesktopW`/`H` | Integer | The resolution of your desktop (all monitors combined)
Expand Down
65 changes: 62 additions & 3 deletions lib.c
Expand Up @@ -84,6 +84,7 @@ struct Config
char noMouseGrab;
char noKeyboardGrab;
char dumb; // undocumented - act as a dumb pipe, nothing more
char confineMouse;
};

static struct Config config = {};
Expand Down Expand Up @@ -161,6 +162,7 @@ static void readConfig(const char* fn)
PARSE_INT(noMouseGrab)
PARSE_INT(noKeyboardGrab)
PARSE_INT(dumb)
PARSE_INT(confineMouse)

/* else */
log_error("Unknown option: %s\n", buf);
Expand Down Expand Up @@ -599,12 +601,14 @@ typedef struct
unsigned char opcode_NV_GLX;
CARD16 serial, serialLast, serialDelta;
unsigned char skip[1<<16];
Window focus;
} X11ConnData;

enum
{
Note_None,
Note_X_GetGeometry,
Note_X_GetInputFocus,
Note_X_InternAtom_Other,
Note_X_QueryExtension_XFree86_VidModeExtension,
Note_X_QueryExtension_RANDR,
Expand All @@ -630,6 +634,38 @@ static void injectRequest(X11ConnData *data, struct Connection* conn, void* buf,
log_debug2("[%d][%d] Injected request %d (%s) with data %d, length %d\n", data->index, sequenceNumber, req->reqType, requestNames[req->reqType], req->data, size);
}

static void grabMouse(X11ConnData *data, bool grab, Window window)
{
struct Connection conn = {};
conn.recvfd = data->client;
conn.sendfd = data->server;
conn.dir = '<';

if (grab)
{
xGrabPointerReq req;
req.reqType = X_GrabPointer;
req.ownerEvents = true; // ?
req.length = sizeof(req)/4;
req.grabWindow = window;
req.eventMask = ~0xFFFF8003;
req.pointerMode = 1 /* Asynchronous */;
req.keyboardMode = 1 /* Asynchronous */;
req.confineTo = window;
req.cursor = None;
req.time = CurrentTime;
injectRequest(data, &conn, &req, sizeof(req));
}
else
{
xResourceReq req;
req.reqType = X_UngrabPointer;
req.length = sizeof(req)/4;
req.id = CurrentTime;
injectRequest(data, &conn, &req, sizeof(req));
}
}

static void* x11connThreadReadProc(void* dataPtr)
{
X11ConnData* data = (X11ConnData*)dataPtr;
Expand Down Expand Up @@ -749,6 +785,12 @@ static void* x11connThreadReadProc(void* dataPtr)
break;
}

case X_GetInputFocus:
{
data->notes[sequenceNumber] = Note_X_GetInputFocus;
break;
}

case X_InternAtom:
{
xInternAtomReq* req = (xInternAtomReq*)buf;
Expand Down Expand Up @@ -964,6 +1006,14 @@ static void* x11connThreadWriteProc(void* dataPtr)
break;
}

case Note_X_GetInputFocus:
{
xGetInputFocusReply* reply = (xGetInputFocusReply*)buf;
log_debug2(" XGetInputFocus(0x%x)\n", reply->focus);
data->focus = reply->focus;
break;
}

case Note_X_InternAtom_Other:
{
xInternAtomReply* reply = (xInternAtomReply*)buf;
Expand Down Expand Up @@ -1124,10 +1174,19 @@ static void* x11connThreadWriteProc(void* dataPtr)
if (config.debug >= 2 && config.actualX && config.actualY && memmem(buf, ofs, &config.actualX, 2) && memmem(buf, ofs, &config.actualY, 2))
log_debug2(" Found actualW/H in output! ----------------------------------------------------------------------------------------------\n");

if (reply->generic.type == FocusOut && config.filterFocus)
if (reply->generic.type == FocusIn || reply->generic.type == FocusOut)
{
log_debug("Filtering out FocusOut event\n");
continue;
//XFocusChangeEvent* event = (XFocusChangeEvent*)reply;
if (config.confineMouse)
{
log_debug("%s mouse grab\n", reply->generic.type == FocusIn ? "Acquiring" : "Releasing");
grabMouse(data, reply->generic.type == FocusIn, /* event->window */ data->focus);
}
if (reply->generic.type == FocusOut && config.filterFocus)
{
log_debug("Filtering out FocusOut event\n");
continue;
}
}

while (data->serialLast != reply->generic.sequenceNumber)
Expand Down

0 comments on commit 54a07fb

Please sign in to comment.