summaryrefslogtreecommitdiffstats
path: root/src/lassi-grab.c
blob: 9c7a685d627fe93beb080c8e97c2e5e148c9a6f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

#include <string.h>
#include <stdlib.h>

#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>

#include <gdk/gdk.h>
#include <gdk/gdkx.h>

#include "lassi-server.h"
#include "lassi-grab.h"

#define TRIGGER_WIDTH 1

static int local2global(LassiGrabInfo *i, int y) {
    g_assert(i);
    g_assert(y >= 0 && y <= gdk_screen_get_height(i->screen)-1);

    /* Convert local screen coordinates (0 .. height) into global ones (0 . 65535) */
    return (y * 0xFFFF) / (gdk_screen_get_height(i->screen)-1);
}

static int global2local(LassiGrabInfo *i, int y) {
    g_assert(i);
    g_assert(y >= 0 && y <= 0xFFFF);

    /* Convert global screen coordinates (0 . 65535) into local ones (0 .. height) */
    return (y * (gdk_screen_get_height(i->screen)-1)) / 0xFFFF;
}

static void move_pointer(LassiGrabInfo *i, int x, int y) {
    g_assert(i);

    /* Move the pointer ... */
    gdk_display_warp_pointer(i->display, i->screen, x, y);

    i->last_x = x;
    i->last_y = y;
}

static void drop_motion_events(LassiGrabInfo *i) {
    XEvent txe;

    g_assert(i);

    /* Drop all queued motion events */
    while (XCheckTypedEvent(GDK_DISPLAY_XDISPLAY(i->display), MotionNotify, &txe))
        ;
}

static int grab_input(LassiGrabInfo *i, GdkWindow *w) {
    g_assert(i);
    g_assert(w);

    if (gdk_pointer_grab(w, TRUE,
                         GDK_POINTER_MOTION_MASK|
                         GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK,
                         NULL, i->empty_cursor, GDK_CURRENT_TIME) != GDK_GRAB_SUCCESS) {
        g_debug("pointer grab failed");
        return -1;
    }


    if (gdk_keyboard_grab(w, TRUE, GDK_CURRENT_TIME) != GDK_GRAB_SUCCESS) {
        gdk_pointer_ungrab(GDK_CURRENT_TIME);
        g_debug("keyboard grab failed");
        return -1;
    }

    XTestGrabControl(GDK_DISPLAY_XDISPLAY(i->display), False);

    if (i->grab_window != w) {
        /* Now, rebase the pointer, so that we can easily calculate
         * relative movements */
        move_pointer(i, i->base_x, i->base_y);

        i->grab_window = w;

        i->left_shift = i->right_shift = i->double_shift = FALSE;

        g_debug("Input now grabbed");
    }

    return 0;
}

int lassi_grab_start(LassiGrabInfo *i, gboolean to_left) {
    g_assert(i);

    return grab_input(i, to_left ? i->left_window : i->right_window);
}

void lassi_grab_stop(LassiGrabInfo *i, int y) {
    int x;

    g_assert(i);

    if (!i->grab_window)
        return;

    /* Move the pointer back into our screen */
    if (y >= 0 && y < 0xFFFF) {

        /* We received a valid y coordinate, so let's use it */
        y = global2local(i, y);

        if (i->grab_window == i->left_window)
            x = TRIGGER_WIDTH;
        else
            x = gdk_screen_get_width(i->screen)-TRIGGER_WIDTH-1;

    } else {

        /* We received an invlid y coordinate, so let's center the
         * pointer */
        x = i->base_x;
        y = i->base_y;
    }

    move_pointer(i, x, y);

    gdk_keyboard_ungrab(GDK_CURRENT_TIME);
    gdk_pointer_ungrab(GDK_CURRENT_TIME);

    drop_motion_events(i);

    i->grab_window = NULL;

    g_debug("Input now ungrabbed");

    XTestGrabControl(GDK_DISPLAY_XDISPLAY(i->display), True);
}

static void handle_motion(LassiGrabInfo *i, int x, int y) {
    int dx, dy;
    int r;
    int w, h;

    dx = x - i->last_x;
    dy = y - i->last_y;

    i->last_x = x;
    i->last_y = y;

/*     g_debug("rel motion %i %i", dx, dy); */

    w = gdk_screen_get_width(i->screen);
    h = gdk_screen_get_height(i->screen);

    if (x <= w/10 || y <= h/10 ||
        x >= (w*9)/10 || y >= (h*9)/10) {

        XEvent txe;

        /* Pointer is too near to the edges, move cursor
         * back to center, so that further movements are
         * not clipped */

        g_debug("centering");

        /* First, make sure there is no further motion event in the queue */
        while (XCheckTypedEvent(GDK_DISPLAY_XDISPLAY(i->display), MotionNotify, &txe)) {
            dx += txe.xmotion.x - i->last_x;
            dy += txe.xmotion.y - i->last_y;

            i->last_x = txe.xmotion.x;
            i->last_y = txe.xmotion.y;
        }

        move_pointer(i, i->base_x, i->base_y);
    }

    /* Filter out non-existant or too large motions */
    if ((dx != 0 || dy != 0) &&
        ((abs(dx) <= (w*9)/20) && (abs(dy) <= (h*9)/20))) {

/*         g_debug("sending motion"); */

        /* Send the event */
        r = lassi_server_motion_event(i->server, dx, dy);
        g_assert(r >= 0);
    }
}

static GdkFilterReturn filter_func(GdkXEvent *gxe, GdkEvent *event, gpointer data) {
    LassiGrabInfo *i = data;
    XEvent *xe = (XEvent*) gxe;
    GdkWindow *w = ((GdkEventAny*) event)->window;

    g_assert(i);
    g_assert(xe);
    g_assert(event);

    switch (xe->type){

        case EnterNotify: {
            XEnterWindowEvent *ewe = (XEnterWindowEvent*) xe;

            if (ewe->mode == NotifyNormal && ewe->state == 0 && !i->grab_window) {
/*                 g_debug("enter %u %u", ewe->x_root, ewe->y_root); */

                /* Only honour this when no button/key is pressed */

                if (lassi_server_change_grab(i->server, w == i->left_window, local2global(i, ewe->y_root)) >= 0)
                    grab_input(i, w);

            } else if (i->grab_window)
                handle_motion(i, ewe->x_root, ewe->y_root);

            break;
        }

        case MotionNotify:

            if (i->grab_window) {
                XMotionEvent *me = (XMotionEvent*) xe;

/*                 g_debug("motion %u %u", me->x_root, me->y_root); */
                handle_motion(i, me->x_root, me->y_root);
            }

            break;

        case ButtonPress:
        case ButtonRelease:

            if (i->grab_window) {
                int r;
                XButtonEvent *be = (XButtonEvent*) xe;

/*                 g_debug("button press/release"); */
                handle_motion(i,  be->x_root, be->y_root);

                /* Send the event */
                r = lassi_server_button_event(i->server, be->button, xe->type == ButtonPress);
                g_assert(r >= 0);
            }
            break;

        case KeyPress:
        case KeyRelease:

/*             g_debug("raw key"); */

            if (i->grab_window) {
                int r;
                XKeyEvent *ke = (XKeyEvent *) xe;
                KeySym keysym;

                keysym = XKeycodeToKeysym(GDK_DISPLAY_XDISPLAY(i->display), ke->keycode, 0);

                if (keysym == XK_Shift_L)
                    i->left_shift = ke->type == KeyPress;
                if (keysym == XK_Shift_R)
                    i->right_shift = xe->type == KeyPress;

                if (i->left_shift && i->right_shift)
                    i->double_shift = TRUE;

/*                 g_debug("left_shift=%i right_shift=%i 0x04%x", i->left_shift, i->right_shift, (unsigned) keysym); */

/*                 g_debug("key press/release"); */
                handle_motion(i,  ke->x_root, ke->y_root);

                /* Send the event */
                r = lassi_server_key_event(i->server, keysym, xe->type == KeyPress);
                g_assert(r >= 0);

                if (!i->left_shift && !i->right_shift && i->double_shift) {
/*                     g_debug("Got double shift"); */
                    lassi_server_acquire_grab(i->server);
                    lassi_grab_stop(i, -1);
                }
            }
            break;
    }

    return GDK_FILTER_CONTINUE;
}

int lassi_grab_init(LassiGrabInfo *i, LassiServer *s) {
    GdkWindowAttr wa;
    GdkColor black = { 0, 0, 0, 0 };
    const gchar cursor_data[1] = { 0 };
    GdkBitmap *bitmap;
    int xtest_event_base, xtest_error_base;
    int major_version, minor_version;

    memset(i, 0, sizeof(*i));
    i->server = s;

    i->screen = gdk_screen_get_default();
    i->display = gdk_screen_get_display(i->screen);
    i->root = gdk_screen_get_root_window(i->screen);

    if (!XTestQueryExtension(GDK_DISPLAY_XDISPLAY(i->display), &xtest_event_base, &xtest_error_base, &major_version, &minor_version)) {
        g_warning("XTest extension not supported.");
        return -1;
    }

    g_debug("XTest %u.%u supported.", major_version, minor_version);

    /* Create empty cursor */
    bitmap = gdk_bitmap_create_from_data(NULL, cursor_data, 1, 1);
    i->empty_cursor = gdk_cursor_new_from_pixmap(bitmap, bitmap, &black, &black, 0, 0);
    gdk_pixmap_unref(bitmap);

    /* Create trigger windows */
    memset(&wa, 0, sizeof(wa));

    wa.title = (char*) "Mango Lassi Left";
    wa.event_mask = GDK_POINTER_MOTION_MASK|GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK|GDK_KEY_PRESS_MASK|GDK_KEY_RELEASE_MASK|GDK_ENTER_NOTIFY_MASK;
    wa.x = 0;
    wa.y = gdk_screen_get_height(i->screen)/20;
    wa.width = TRIGGER_WIDTH;
    wa.height = (gdk_screen_get_height(i->screen)*18)/20;
    wa.wclass = GDK_INPUT_ONLY;
    wa.window_type = GDK_WINDOW_FOREIGN;
    wa.override_redirect = TRUE;
    wa.type_hint = GDK_WINDOW_TYPE_HINT_DOCK;
    wa.cursor = i->empty_cursor;

    i->left_window = gdk_window_new(i->root, &wa, GDK_WA_TITLE|GDK_WA_X|GDK_WA_Y|GDK_WA_NOREDIR|GDK_WA_TYPE_HINT|GDK_WA_CURSOR);
    gdk_window_set_keep_above(i->left_window, TRUE);
    gdk_window_add_filter(i->left_window, filter_func, i);

    wa.title = (char*) "Mango Lassi Right";
    wa.x = gdk_screen_get_width(i->screen) - TRIGGER_WIDTH;

    i->right_window = gdk_window_new(i->root, &wa, GDK_WA_TITLE|GDK_WA_X|GDK_WA_Y|GDK_WA_NOREDIR|GDK_WA_TYPE_HINT);
    gdk_window_set_keep_above(i->right_window, TRUE);
    gdk_window_add_filter(i->right_window, filter_func, i);

    i->base_x = gdk_screen_get_width(i->screen)/2;
    i->base_y = gdk_screen_get_height(i->screen)/2;

    XTestGrabControl(GDK_DISPLAY_XDISPLAY(i->display), True);

    return 0;
}

void lassi_grab_done(LassiGrabInfo *i) {
    g_assert(i);

    lassi_grab_stop(i, -1);

    if (i->left_window)
        gdk_window_destroy(i->left_window);

    if (i->right_window)
        gdk_window_destroy(i->right_window);

    if (i->empty_cursor)
        gdk_cursor_unref(i->empty_cursor);
}

void lassi_grab_enable_triggers(LassiGrabInfo *i, gboolean left, gboolean right) {
    g_assert(i);

    g_debug("Showing windows: left=%s, right=%s", left ? "yes" : "no", right ? "yes" : "no");

    if (left)
        gdk_window_show(i->left_window);
    else
        gdk_window_hide(i->left_window);

    if (right)
        gdk_window_show(i->right_window);
    else
        gdk_window_hide(i->right_window);
}

int lassi_grab_move_pointer_relative(LassiGrabInfo *i, int dx, int dy) {
    g_assert(i);

    if (i->grab_window)
        return -1;

    XTestFakeRelativeMotionEvent(GDK_DISPLAY_XDISPLAY(i->display), dx, dy, 0);
    XSync(GDK_DISPLAY_XDISPLAY(i->display), False);

    return 0;
}

int lassi_grab_press_button(LassiGrabInfo *i, unsigned button, gboolean is_press) {
    g_assert(i);

    if (i->grab_window)
        return -1;

    XTestFakeButtonEvent(GDK_DISPLAY_XDISPLAY(i->display), button, is_press, 0);
    XSync(GDK_DISPLAY_XDISPLAY(i->display), False);

    return 0;
}

int lassi_grab_press_key(LassiGrabInfo *i, unsigned key, gboolean is_press) {
    g_assert(i);
    if (i->grab_window)
        return -1;

    XTestFakeKeyEvent(GDK_DISPLAY_XDISPLAY(i->display), XKeysymToKeycode(GDK_DISPLAY_XDISPLAY(i->display), key), is_press, 0);
    XSync(GDK_DISPLAY_XDISPLAY(i->display), False);

    return 0;
}