2003-09-17 07:44:49 +00:00
|
|
|
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
|
2003-09-17 07:32:52 +00:00
|
|
|
|
|
|
|
grab.c for the Openbox window manager
|
2006-08-22 16:44:18 +00:00
|
|
|
Copyright (c) 2006 Mikael Magnusson
|
2007-04-23 17:56:35 +00:00
|
|
|
Copyright (c) 2003-2007 Dana Jansens
|
2003-09-17 07:32:52 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
See the COPYING file for a copy of the GNU General Public License.
|
|
|
|
*/
|
|
|
|
|
2003-09-04 00:02:53 +00:00
|
|
|
#include "grab.h"
|
2007-05-04 01:10:03 +00:00
|
|
|
#include "modkeys.h"
|
2003-03-18 19:51:56 +00:00
|
|
|
#include "openbox.h"
|
2003-03-19 18:55:10 +00:00
|
|
|
#include "event.h"
|
2003-03-29 00:48:32 +00:00
|
|
|
#include "xerror.h"
|
2003-09-16 07:48:55 +00:00
|
|
|
#include "screen.h"
|
2007-04-23 17:56:35 +00:00
|
|
|
#include "debug.h"
|
2003-03-19 18:55:10 +00:00
|
|
|
|
2003-03-18 19:51:56 +00:00
|
|
|
#include <glib.h>
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
|
2007-04-24 01:16:33 +00:00
|
|
|
#define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
|
2003-04-17 07:00:13 +00:00
|
|
|
#define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
|
2003-03-18 19:51:56 +00:00
|
|
|
|
2003-03-19 18:55:10 +00:00
|
|
|
#define MASK_LIST_SIZE 8
|
|
|
|
|
|
|
|
/*! A list of all possible combinations of keyboard lock masks */
|
2003-10-15 03:59:35 +00:00
|
|
|
static guint mask_list[MASK_LIST_SIZE];
|
2003-09-21 17:17:19 +00:00
|
|
|
static guint kgrabs = 0;
|
|
|
|
static guint pgrabs = 0;
|
2007-03-28 01:11:40 +00:00
|
|
|
/*! The time at which the last grab was made */
|
|
|
|
static Time grab_time = CurrentTime;
|
2007-06-12 15:53:14 +00:00
|
|
|
static gint passive_count = 0;
|
2003-09-21 17:17:19 +00:00
|
|
|
|
2007-09-04 04:21:22 +00:00
|
|
|
static Time ungrab_time(void)
|
2007-03-28 01:52:06 +00:00
|
|
|
{
|
|
|
|
Time t = event_curtime;
|
2007-05-05 16:12:36 +00:00
|
|
|
if (grab_time == CurrentTime ||
|
|
|
|
!(t == CurrentTime || event_time_after(t, grab_time)))
|
2007-03-28 19:41:41 +00:00
|
|
|
/* When the time moves backward on the server, then we can't use
|
|
|
|
the grab time because that will be in the future. So instead we
|
|
|
|
have to use CurrentTime.
|
|
|
|
|
|
|
|
"XUngrabPointer does not release the pointer if the specified time
|
|
|
|
is earlier than the last-pointer-grab time or is later than the
|
|
|
|
current X server time."
|
|
|
|
*/
|
|
|
|
t = CurrentTime; /*grab_time;*/
|
2007-03-28 01:52:06 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2007-09-04 04:21:22 +00:00
|
|
|
gboolean grab_on_keyboard(void)
|
2003-09-21 17:17:19 +00:00
|
|
|
{
|
|
|
|
return kgrabs > 0;
|
|
|
|
}
|
|
|
|
|
2007-09-04 04:21:22 +00:00
|
|
|
gboolean grab_on_pointer(void)
|
2003-09-21 17:17:19 +00:00
|
|
|
{
|
|
|
|
return pgrabs > 0;
|
|
|
|
}
|
2003-03-19 18:55:10 +00:00
|
|
|
|
2007-05-13 23:30:52 +00:00
|
|
|
gboolean grab_keyboard_full(gboolean grab)
|
2003-03-18 19:51:56 +00:00
|
|
|
{
|
2003-06-26 07:58:25 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2003-03-18 19:51:56 +00:00
|
|
|
if (grab) {
|
2003-09-28 09:24:22 +00:00
|
|
|
if (kgrabs++ == 0) {
|
2003-07-10 19:01:41 +00:00
|
|
|
ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
|
2007-04-22 02:12:32 +00:00
|
|
|
False, GrabModeAsync, GrabModeAsync,
|
2007-03-02 02:23:00 +00:00
|
|
|
event_curtime) == Success;
|
2003-09-28 09:24:22 +00:00
|
|
|
if (!ret)
|
|
|
|
--kgrabs;
|
2007-06-12 15:53:14 +00:00
|
|
|
else {
|
|
|
|
passive_count = 0;
|
2007-03-28 01:11:40 +00:00
|
|
|
grab_time = event_curtime;
|
2007-06-12 15:53:14 +00:00
|
|
|
}
|
2003-09-28 09:24:22 +00:00
|
|
|
} else
|
2003-06-26 23:31:20 +00:00
|
|
|
ret = TRUE;
|
2003-03-18 19:51:56 +00:00
|
|
|
} else if (kgrabs > 0) {
|
2007-03-28 01:11:40 +00:00
|
|
|
if (--kgrabs == 0) {
|
2007-06-03 17:07:26 +00:00
|
|
|
XUngrabKeyboard(ob_display, ungrab_time());
|
2007-03-28 01:11:40 +00:00
|
|
|
}
|
2003-06-26 23:33:29 +00:00
|
|
|
ret = TRUE;
|
2003-03-18 19:51:56 +00:00
|
|
|
}
|
2003-08-30 07:20:16 +00:00
|
|
|
|
2003-06-26 07:58:25 +00:00
|
|
|
return ret;
|
2003-03-18 19:51:56 +00:00
|
|
|
}
|
|
|
|
|
2007-05-13 23:30:52 +00:00
|
|
|
gboolean grab_pointer_full(gboolean grab, gboolean owner_events,
|
|
|
|
gboolean confine, ObCursor cur)
|
2003-03-18 19:51:56 +00:00
|
|
|
{
|
2003-06-26 07:58:25 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
2003-03-18 19:51:56 +00:00
|
|
|
if (grab) {
|
2003-09-28 09:24:22 +00:00
|
|
|
if (pgrabs++ == 0) {
|
2007-04-22 14:07:29 +00:00
|
|
|
ret = XGrabPointer(ob_display, screen_support_win, owner_events,
|
|
|
|
GRAB_PTR_MASK,
|
2007-05-13 23:30:52 +00:00
|
|
|
GrabModeAsync, GrabModeAsync,
|
|
|
|
(confine ? RootWindow(ob_display, ob_screen) :
|
|
|
|
None),
|
2007-03-02 02:23:00 +00:00
|
|
|
ob_cursor(cur), event_curtime) == Success;
|
2003-09-28 09:24:22 +00:00
|
|
|
if (!ret)
|
|
|
|
--pgrabs;
|
2007-03-28 01:11:40 +00:00
|
|
|
else
|
|
|
|
grab_time = event_curtime;
|
2003-09-28 09:24:22 +00:00
|
|
|
} else
|
2003-06-26 23:31:20 +00:00
|
|
|
ret = TRUE;
|
2003-03-18 19:51:56 +00:00
|
|
|
} else if (pgrabs > 0) {
|
2003-09-21 18:09:48 +00:00
|
|
|
if (--pgrabs == 0) {
|
2007-06-03 17:07:26 +00:00
|
|
|
XUngrabPointer(ob_display, ungrab_time());
|
2003-09-21 18:09:48 +00:00
|
|
|
}
|
2003-06-26 23:33:29 +00:00
|
|
|
ret = TRUE;
|
2003-03-18 19:51:56 +00:00
|
|
|
}
|
2003-06-26 07:58:25 +00:00
|
|
|
return ret;
|
2003-03-18 19:51:56 +00:00
|
|
|
}
|
|
|
|
|
2003-09-04 00:02:53 +00:00
|
|
|
gint grab_server(gboolean grab)
|
2003-04-16 18:09:11 +00:00
|
|
|
{
|
|
|
|
static guint sgrabs = 0;
|
2003-03-18 20:00:44 +00:00
|
|
|
if (grab) {
|
|
|
|
if (sgrabs++ == 0) {
|
|
|
|
XGrabServer(ob_display);
|
|
|
|
XSync(ob_display, FALSE);
|
|
|
|
}
|
|
|
|
} else if (sgrabs > 0) {
|
|
|
|
if (--sgrabs == 0) {
|
|
|
|
XUngrabServer(ob_display);
|
|
|
|
XFlush(ob_display);
|
|
|
|
}
|
|
|
|
}
|
2003-04-16 18:09:11 +00:00
|
|
|
return sgrabs;
|
2003-03-18 20:00:44 +00:00
|
|
|
}
|
|
|
|
|
2003-09-04 00:02:53 +00:00
|
|
|
void grab_startup(gboolean reconfig)
|
2003-03-18 19:51:56 +00:00
|
|
|
{
|
2003-03-19 18:55:10 +00:00
|
|
|
guint i = 0;
|
2007-05-04 01:10:03 +00:00
|
|
|
guint num, caps, scroll;
|
2003-03-19 18:55:10 +00:00
|
|
|
|
2007-05-04 01:10:03 +00:00
|
|
|
num = modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
|
|
|
|
caps = modkeys_key_to_mask(OB_MODKEY_KEY_CAPSLOCK);
|
|
|
|
scroll = modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
|
2003-09-04 00:02:53 +00:00
|
|
|
|
2003-03-19 18:55:10 +00:00
|
|
|
mask_list[i++] = 0;
|
2007-05-04 01:10:03 +00:00
|
|
|
mask_list[i++] = num;
|
|
|
|
mask_list[i++] = caps;
|
|
|
|
mask_list[i++] = scroll;
|
|
|
|
mask_list[i++] = num | caps;
|
|
|
|
mask_list[i++] = num | scroll;
|
|
|
|
mask_list[i++] = caps | scroll;
|
|
|
|
mask_list[i++] = num | caps | scroll;
|
2003-03-19 18:55:10 +00:00
|
|
|
g_assert(i == MASK_LIST_SIZE);
|
2003-03-18 19:51:56 +00:00
|
|
|
}
|
|
|
|
|
2003-09-04 00:02:53 +00:00
|
|
|
void grab_shutdown(gboolean reconfig)
|
2003-03-18 19:51:56 +00:00
|
|
|
{
|
2003-09-04 00:02:53 +00:00
|
|
|
if (reconfig) return;
|
|
|
|
|
2007-05-13 23:30:52 +00:00
|
|
|
while (ungrab_keyboard());
|
|
|
|
while (ungrab_pointer());
|
2003-04-16 18:09:11 +00:00
|
|
|
while (grab_server(FALSE));
|
2003-03-18 19:51:56 +00:00
|
|
|
}
|
2003-03-19 18:55:10 +00:00
|
|
|
|
2003-05-11 05:15:43 +00:00
|
|
|
void grab_button_full(guint button, guint state, Window win, guint mask,
|
2003-10-15 03:59:35 +00:00
|
|
|
gint pointer_mode, ObCursor cur)
|
2003-03-19 18:55:10 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2007-04-23 17:56:35 +00:00
|
|
|
xerror_set_ignore(TRUE); /* can get BadAccess from these */
|
2003-08-01 19:34:52 +00:00
|
|
|
xerror_occured = FALSE;
|
2003-03-19 18:55:10 +00:00
|
|
|
for (i = 0; i < MASK_LIST_SIZE; ++i)
|
2007-04-25 14:13:32 +00:00
|
|
|
XGrabButton(ob_display, button, state | mask_list[i], win, False, mask,
|
|
|
|
pointer_mode, GrabModeAsync, None, ob_cursor(cur));
|
2003-08-01 19:34:52 +00:00
|
|
|
xerror_set_ignore(FALSE);
|
|
|
|
if (xerror_occured)
|
2007-04-23 17:56:35 +00:00
|
|
|
ob_debug("Failed to grab button %d modifiers %d", button, state);
|
2003-05-11 05:15:43 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 18:55:10 +00:00
|
|
|
void ungrab_button(guint button, guint state, Window win)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
for (i = 0; i < MASK_LIST_SIZE; ++i)
|
|
|
|
XUngrabButton(ob_display, button, state | mask_list[i], win);
|
|
|
|
}
|
|
|
|
|
2003-10-15 03:59:35 +00:00
|
|
|
void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
|
2003-03-19 18:55:10 +00:00
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
2003-03-29 00:48:32 +00:00
|
|
|
xerror_set_ignore(TRUE); /* can get BadAccess' from these */
|
|
|
|
xerror_occured = FALSE;
|
2003-03-19 18:55:10 +00:00
|
|
|
for (i = 0; i < MASK_LIST_SIZE; ++i)
|
2003-06-04 22:45:54 +00:00
|
|
|
XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
|
2003-08-31 17:32:28 +00:00
|
|
|
GrabModeAsync, keyboard_mode);
|
2003-03-29 00:48:32 +00:00
|
|
|
xerror_set_ignore(FALSE);
|
|
|
|
if (xerror_occured)
|
2007-04-23 17:56:35 +00:00
|
|
|
ob_debug("Failed to grab keycode %d modifiers %d", keycode, state);
|
2003-03-19 18:55:10 +00:00
|
|
|
}
|
|
|
|
|
2003-06-04 22:45:54 +00:00
|
|
|
void ungrab_all_keys(Window win)
|
2003-03-19 18:55:10 +00:00
|
|
|
{
|
2003-06-04 22:45:54 +00:00
|
|
|
XUngrabKey(ob_display, AnyKey, AnyModifier, win);
|
2003-03-19 18:55:10 +00:00
|
|
|
}
|
2007-06-12 15:53:14 +00:00
|
|
|
|
|
|
|
void grab_key_passive_count(int change)
|
|
|
|
{
|
|
|
|
if (grab_on_keyboard()) return;
|
|
|
|
passive_count += change;
|
|
|
|
if (passive_count < 0) passive_count = 0;
|
|
|
|
}
|
|
|
|
|
2007-09-04 04:21:22 +00:00
|
|
|
void ungrab_passive_key(void)
|
2007-06-12 15:53:14 +00:00
|
|
|
{
|
2007-06-13 15:14:20 +00:00
|
|
|
/*ob_debug("ungrabbing %d passive grabs\n", passive_count);*/
|
2007-06-12 15:53:14 +00:00
|
|
|
if (passive_count) {
|
|
|
|
/* kill out passive grab */
|
|
|
|
XUngrabKeyboard(ob_display, event_curtime);
|
|
|
|
passive_count = 0;
|
|
|
|
}
|
|
|
|
}
|