summaryrefslogtreecommitdiff
path: root/src/gs-grab-x11.c
blob: 83b04e0d10ed9286a224b1bc0b9523e7d3c772c2 (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2004-2006 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
# include <X11/extensions/xf86misc.h>
#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */

#include "gs-window.h"
#include "gs-grab.h"
#include "gs-debug.h"

static void     gs_grab_class_init (GSGrabClass *klass);
static void     gs_grab_init       (GSGrab      *grab);
static void     gs_grab_finalize   (GObject        *object);

#define GS_GRAB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_GRAB, GSGrabPrivate))

G_DEFINE_TYPE (GSGrab, gs_grab, G_TYPE_OBJECT)

static gpointer grab_object = NULL;

struct GSGrabPrivate
{
	guint      mouse_hide_cursor : 1;
	GdkWindow *mouse_grab_window;
	GdkWindow *keyboard_grab_window;
	GdkScreen *mouse_grab_screen;
	GdkScreen *keyboard_grab_screen;

	GtkWidget *invisible;
};

static const char *
grab_string (int status)
{
	switch (status)
	{
	case GDK_GRAB_SUCCESS:
		return "GrabSuccess";
	case GDK_GRAB_ALREADY_GRABBED:
		return "AlreadyGrabbed";
	case GDK_GRAB_INVALID_TIME:
		return "GrabInvalidTime";
	case GDK_GRAB_NOT_VIEWABLE:
		return "GrabNotViewable";
	case GDK_GRAB_FROZEN:
		return "GrabFrozen";
	default:
	{
		static char foo [255];
		sprintf (foo, "unknown status: %d", status);
		return foo;
	}
	}
}

#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
/* This function enables and disables the Ctrl-Alt-KP_star and
   Ctrl-Alt-KP_slash hot-keys, which (in XFree86 4.2) break any
   grabs and/or kill the grabbing client.  That would effectively
   unlock the screen, so we don't like that.

   The Ctrl-Alt-KP_star and Ctrl-Alt-KP_slash hot-keys only exist
   if AllowDeactivateGrabs and/or AllowClosedownGrabs are turned on
   in XF86Config.  I believe they are disabled by default.

   This does not affect any other keys (specifically Ctrl-Alt-BS or
   Ctrl-Alt-F1) but I wish it did.  Maybe it will someday.
 */
static void
xorg_lock_smasher_set_active (GSGrab  *grab,
                              gboolean active)
{
	int status, event, error;

	if (!XF86MiscQueryExtension (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &event, &error))
	{
		gs_debug ("No XFree86-Misc extension present");
		return;
	}

	if (active)
	{
		gs_debug ("Enabling the x.org grab smasher");
	}
	else
	{
		gs_debug ("Disabling the x.org grab smasher");
	}

	gdk_error_trap_push ();

	status = XF86MiscSetGrabKeysState (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), active);

	gdk_display_sync (gdk_display_get_default ());
	gdk_error_trap_pop ();

	if (active && status == MiscExtGrabStateAlready)
	{
		/* shut up, consider this success */
		status = MiscExtGrabStateSuccess;
	}

	gs_debug ("XF86MiscSetGrabKeysState(%s) returned %s\n",
	          active ? "on" : "off",
	          (status == MiscExtGrabStateSuccess ? "MiscExtGrabStateSuccess" :
	           status == MiscExtGrabStateLocked  ? "MiscExtGrabStateLocked"  :
	           status == MiscExtGrabStateAlready ? "MiscExtGrabStateAlready" :
	           "unknown value"));
}
#else
static void
xorg_lock_smasher_set_active (GSGrab  *grab,
                              gboolean active)
{
}
#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */

static int
gs_grab_get_keyboard (GSGrab    *grab,
                      GdkWindow *window,
                      GdkScreen *screen)
{
	GdkGrabStatus status;

	g_return_val_if_fail (window != NULL, FALSE);
	g_return_val_if_fail (screen != NULL, FALSE);

	gs_debug ("Grabbing keyboard widget=%X", (guint32) GDK_WINDOW_XID (window));
	status = gdk_keyboard_grab (window, FALSE, GDK_CURRENT_TIME);

	if (status == GDK_GRAB_SUCCESS)
	{
		if (grab->priv->keyboard_grab_window != NULL)
		{
			g_object_remove_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
			                              (gpointer *) &grab->priv->keyboard_grab_window);
		}
		grab->priv->keyboard_grab_window = window;

		g_object_add_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
		                           (gpointer *) &grab->priv->keyboard_grab_window);

		grab->priv->keyboard_grab_screen = screen;
	}
	else
	{
		gs_debug ("Couldn't grab keyboard!  (%s)", grab_string (status));
	}

	return status;
}

static int
gs_grab_get_mouse (GSGrab    *grab,
                   GdkWindow *window,
                   GdkScreen *screen,
                   gboolean   hide_cursor)
{
	GdkGrabStatus status;
	GdkCursor    *cursor;

	g_return_val_if_fail (window != NULL, FALSE);
	g_return_val_if_fail (screen != NULL, FALSE);

	cursor = gdk_cursor_new (GDK_BLANK_CURSOR);

	gs_debug ("Grabbing mouse widget=%X", (guint32) GDK_WINDOW_XID (window));
	status = gdk_pointer_grab (window, TRUE, 0, NULL,
	                           (hide_cursor ? cursor : NULL),
	                           GDK_CURRENT_TIME);

	if (status == GDK_GRAB_SUCCESS)
	{
		if (grab->priv->mouse_grab_window != NULL)
		{
			g_object_remove_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
			                              (gpointer *) &grab->priv->mouse_grab_window);
		}
		grab->priv->mouse_grab_window = window;

		g_object_add_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
		                           (gpointer *) &grab->priv->mouse_grab_window);

		grab->priv->mouse_grab_screen = screen;
		grab->priv->mouse_hide_cursor = hide_cursor;
	}

	gdk_cursor_unref (cursor);

	return status;
}

void
gs_grab_keyboard_reset (GSGrab *grab)
{
	if (grab->priv->keyboard_grab_window != NULL)
	{
		g_object_remove_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
		                              (gpointer *) &grab->priv->keyboard_grab_window);
	}
	grab->priv->keyboard_grab_window = NULL;
	grab->priv->keyboard_grab_screen = NULL;
}

static gboolean
gs_grab_release_keyboard (GSGrab *grab)
{
	gs_debug ("Ungrabbing keyboard");
	gdk_keyboard_ungrab (GDK_CURRENT_TIME);

	gs_grab_keyboard_reset (grab);

	return TRUE;
}

void
gs_grab_mouse_reset (GSGrab *grab)
{
	if (grab->priv->mouse_grab_window != NULL)
	{
		g_object_remove_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
		                              (gpointer *) &grab->priv->mouse_grab_window);
	}

	grab->priv->mouse_grab_window = NULL;
	grab->priv->mouse_grab_screen = NULL;
}

gboolean
gs_grab_release_mouse (GSGrab *grab)
{
	gs_debug ("Ungrabbing pointer");
	gdk_pointer_ungrab (GDK_CURRENT_TIME);

	gs_grab_mouse_reset (grab);

	return TRUE;
}

static gboolean
gs_grab_move_mouse (GSGrab    *grab,
                    GdkWindow *window,
                    GdkScreen *screen,
                    gboolean   hide_cursor)
{
	gboolean   result;
	GdkWindow *old_window;
	GdkScreen *old_screen;
	gboolean   old_hide_cursor;

	/* if the pointer is not grabbed and we have a
	   mouse_grab_window defined then we lost the grab */
	if (! gdk_pointer_is_grabbed ())
	{
		gs_grab_mouse_reset (grab);
	}

	if (grab->priv->mouse_grab_window == window)
	{
		gs_debug ("Window %X is already grabbed, skipping",
		          (guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window));
		return TRUE;
	}

#if 0
	gs_debug ("Intentionally skipping move pointer grabs");
	/* FIXME: GTK doesn't like having the pointer grabbed */
	return TRUE;
#else
	if (grab->priv->mouse_grab_window)
	{
		gs_debug ("Moving pointer grab from %X to %X",
		          (guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window),
		          (guint32) GDK_WINDOW_XID (window));
	}
	else
	{
		gs_debug ("Getting pointer grab on %X",
		          (guint32) GDK_WINDOW_XID (window));
	}
#endif

	gs_debug ("*** doing X server grab");
	gdk_x11_grab_server ();

	old_window = grab->priv->mouse_grab_window;
	old_screen = grab->priv->mouse_grab_screen;
	old_hide_cursor = grab->priv->mouse_hide_cursor;

	if (old_window)
	{
		gs_grab_release_mouse (grab);
	}

	result = gs_grab_get_mouse (grab, window, screen, hide_cursor);

	if (result != GDK_GRAB_SUCCESS)
	{
		sleep (1);
		result = gs_grab_get_mouse (grab, window, screen, hide_cursor);
	}

	if ((result != GDK_GRAB_SUCCESS) && old_window)
	{
		gs_debug ("Could not grab mouse for new window.  Resuming previous grab.");
		gs_grab_get_mouse (grab, old_window, old_screen, old_hide_cursor);
	}

	gs_debug ("*** releasing X server grab");
	gdk_x11_ungrab_server ();
	gdk_flush ();

	return (result == GDK_GRAB_SUCCESS);
}

static gboolean
gs_grab_move_keyboard (GSGrab    *grab,
                       GdkWindow *window,
                       GdkScreen *screen)
{
	gboolean   result;
	GdkWindow *old_window;
	GdkScreen *old_screen;

	if (grab->priv->keyboard_grab_window == window)
	{
		gs_debug ("Window %X is already grabbed, skipping",
		          (guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window));
		return TRUE;
	}

	if (grab->priv->keyboard_grab_window != NULL)
	{
		gs_debug ("Moving keyboard grab from %X to %X",
		          (guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window),
		          (guint32) GDK_WINDOW_XID (window));
	}
	else
	{
		gs_debug ("Getting keyboard grab on %X",
		          (guint32) GDK_WINDOW_XID (window));

	}

	gs_debug ("*** doing X server grab");
	gdk_x11_grab_server ();

	old_window = grab->priv->keyboard_grab_window;
	old_screen = grab->priv->keyboard_grab_screen;

	if (old_window)
	{
		gs_grab_release_keyboard (grab);
	}

	result = gs_grab_get_keyboard (grab, window, screen);

	if (result != GDK_GRAB_SUCCESS)
	{
		sleep (1);
		result = gs_grab_get_keyboard (grab, window, screen);
	}

	if ((result != GDK_GRAB_SUCCESS) && old_window)
	{
		gs_debug ("Could not grab keyboard for new window.  Resuming previous grab.");
		gs_grab_get_keyboard (grab, old_window, old_screen);
	}

	gs_debug ("*** releasing X server grab");
	gdk_x11_ungrab_server ();
	gdk_flush ();

	return (result == GDK_GRAB_SUCCESS);
}

static void
gs_grab_nuke_focus (void)
{
	Window focus = 0;
	int    rev = 0;

	gs_debug ("Nuking focus");

	gdk_error_trap_push ();

	XGetInputFocus (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), &focus, &rev);
	XSetInputFocus (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), None, RevertToNone, CurrentTime);

	gdk_display_sync (gdk_display_get_default ());
	gdk_error_trap_pop ();
}

void
gs_grab_release (GSGrab *grab)
{
	gs_debug ("Releasing all grabs");

	gs_grab_release_mouse (grab);
	gs_grab_release_keyboard (grab);

	/* FIXME: is it right to enable this ? */
	xorg_lock_smasher_set_active (grab, TRUE);

	gdk_display_sync (gdk_display_get_default ());
	gdk_flush ();
}

gboolean
gs_grab_grab_window (GSGrab    *grab,
                     GdkWindow *window,
                     GdkScreen *screen,
                     gboolean   hide_cursor)
{
	gboolean mstatus = FALSE;
	gboolean kstatus = FALSE;
	int      i;
	int      retries = 4;
	gboolean focus_fuckus = FALSE;

AGAIN:

	for (i = 0; i < retries; i++)
	{
		kstatus = gs_grab_get_keyboard (grab, window, screen);
		if (kstatus == GDK_GRAB_SUCCESS)
		{
			break;
		}

		/* else, wait a second and try to grab again. */
		sleep (1);
	}

	if (kstatus != GDK_GRAB_SUCCESS)
	{
		if (!focus_fuckus)
		{
			focus_fuckus = TRUE;
			gs_grab_nuke_focus ();
			goto AGAIN;
		}
	}

	for (i = 0; i < retries; i++)
	{
		mstatus = gs_grab_get_mouse (grab, window, screen, hide_cursor);
		if (mstatus == GDK_GRAB_SUCCESS)
		{
			break;
		}

		/* else, wait a second and try to grab again. */
		sleep (1);
	}

	if (mstatus != GDK_GRAB_SUCCESS)
	{
		gs_debug ("Couldn't grab pointer!  (%s)",
		          grab_string (mstatus));
	}

#if 0
	/* FIXME: release the pointer grab so GTK will work */
	gs_grab_release_mouse (grab);
#endif

	/* When should we allow blanking to proceed?  The current theory
	   is that both a keyboard grab and a mouse grab are mandatory

	   - If we don't have a keyboard grab, then we won't be able to
	   read a password to unlock, so the kbd grab is manditory.

	   - If we don't have a mouse grab, then we might not see mouse
	   clicks as a signal to unblank, on-screen widgets won't work ideally,
	   and gs_grab_move_to_window() will spin forever when it gets called.
	*/

	if (kstatus != GDK_GRAB_SUCCESS || mstatus != GDK_GRAB_SUCCESS)
	{
		/* Do not blank without a keyboard and mouse grabs. */

		/* Release keyboard or mouse which was grabbed. */
		if (kstatus == GDK_GRAB_SUCCESS)
		{
			gs_grab_release_keyboard (grab);
		}
		if (mstatus == GDK_GRAB_SUCCESS)
		{
			gs_grab_release_mouse (grab);
		}

		return FALSE;
	}

	/* Grab is good, go ahead and blank.  */
	return TRUE;
}

/* this is used to grab the keyboard and mouse to the root */
gboolean
gs_grab_grab_root (GSGrab  *grab,
                   gboolean hide_cursor)
{
	GdkDisplay *display;
	GdkWindow  *root;
	GdkScreen  *screen;
	gboolean    res;

	gs_debug ("Grabbing the root window");

	display = gdk_display_get_default ();
	gdk_display_get_pointer (display, &screen, NULL, NULL, NULL);
	root = gdk_screen_get_root_window (screen);

	res = gs_grab_grab_window (grab, root, screen, hide_cursor);

	return res;
}

/* this is used to grab the keyboard and mouse to an offscreen window */
gboolean
gs_grab_grab_offscreen (GSGrab *grab,
                        gboolean hide_cursor)
{
	GdkScreen *screen;
	gboolean   res;

	gs_debug ("Grabbing an offscreen window");

	screen = gtk_invisible_get_screen (GTK_INVISIBLE (grab->priv->invisible));
	res = gs_grab_grab_window (grab, gtk_widget_get_window (GTK_WIDGET (grab->priv->invisible)), screen, hide_cursor);

	return res;
}

/* This is similar to gs_grab_grab_window but doesn't fail */
void
gs_grab_move_to_window (GSGrab    *grab,
                        GdkWindow *window,
                        GdkScreen *screen,
                        gboolean   hide_cursor)
{
	gboolean result = FALSE;

	g_return_if_fail (GS_IS_GRAB (grab));

	xorg_lock_smasher_set_active (grab, FALSE);

	do
	{
		result = gs_grab_move_keyboard (grab, window, screen);
		gdk_flush ();
	}
	while (!result);

	do
	{
		result = gs_grab_move_mouse (grab, window, screen, hide_cursor);
		gdk_flush ();
	}
	while (!result);
}

static void
gs_grab_class_init (GSGrabClass *klass)
{
	GObjectClass   *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = gs_grab_finalize;

	g_type_class_add_private (klass, sizeof (GSGrabPrivate));
}

static void
gs_grab_init (GSGrab *grab)
{
	grab->priv = GS_GRAB_GET_PRIVATE (grab);

	grab->priv->mouse_hide_cursor = FALSE;
	grab->priv->invisible = gtk_invisible_new ();
	gtk_widget_show (grab->priv->invisible);
}

static void
gs_grab_finalize (GObject *object)
{
	GSGrab *grab;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GS_IS_GRAB (object));

	grab = GS_GRAB (object);

	g_return_if_fail (grab->priv != NULL);

	gtk_widget_destroy (grab->priv->invisible);

	G_OBJECT_CLASS (gs_grab_parent_class)->finalize (object);
}

GSGrab *
gs_grab_new (void)
{
	if (grab_object)
	{
		g_object_ref (grab_object);
	}
	else
	{
		grab_object = g_object_new (GS_TYPE_GRAB, NULL);
		g_object_add_weak_pointer (grab_object,
		                           (gpointer *) &grab_object);
	}

	return GS_GRAB (grab_object);
}