keybindings: Use input event helper functions

This commit is contained in:
Carlos Garnacho 2011-06-11 18:24:44 +02:00
parent 3b0aa9f881
commit 36e237cf1e

View File

@ -34,6 +34,7 @@
#include "ui.h"
#include "frame.h"
#include "place.h"
#include "input-events.h"
#include <meta/prefs.h>
#include <meta/util.h>
@ -1256,17 +1257,23 @@ process_overlay_key (MetaDisplay *display,
XEvent *event,
KeySym keysym)
{
if (event->xkey.keycode != display->overlay_key_combo.keycode)
guint evtype, keycode;
if (!meta_input_event_get_type (display, event, &evtype) ||
!meta_input_event_get_keycode (display, event, &keycode))
return FALSE;
if (keycode != display->overlay_key_combo.keycode)
{
display->overlay_key_only_pressed = FALSE;
return FALSE;
}
if (event->xkey.type == KeyPress)
if (evtype == KeyPress)
{
display->overlay_key_only_pressed = TRUE;
}
else if (event->xkey.type == KeyRelease && display->overlay_key_only_pressed)
else if (evtype == KeyRelease && display->overlay_key_only_pressed)
{
display->overlay_key_only_pressed = FALSE;
meta_display_overlay_key_activate (display);
@ -1325,10 +1332,16 @@ process_event (MetaKeyBinding *bindings,
KeySym keysym,
gboolean on_window)
{
guint evtype, keycode, state;
int i;
/* we used to have release-based bindings but no longer. */
if (event->type == KeyRelease)
if (!meta_input_event_get_type (display, event, &evtype) ||
!meta_input_event_get_keycode (display, event, &keycode) ||
!meta_input_event_get_state (display, event, &state))
return FALSE;
if (evtype == KeyRelease)
return FALSE;
/*
@ -1340,12 +1353,12 @@ process_event (MetaKeyBinding *bindings,
MetaKeyHandler *handler = bindings[i].handler;
if ((!on_window && handler->flags & BINDING_PER_WINDOW) ||
event->type != KeyPress ||
bindings[i].keycode != event->xkey.keycode ||
((event->xkey.state & 0xff & ~(display->ignored_modifier_mask)) !=
evtype != KeyPress ||
bindings[i].keycode != keycode ||
((state & 0xff & ~(display->ignored_modifier_mask)) !=
bindings[i].mask))
continue;
/*
* window must be non-NULL for on_window to be true,
* and so also window must be non-NULL if we get here and
@ -1355,7 +1368,7 @@ process_event (MetaKeyBinding *bindings,
meta_topic (META_DEBUG_KEYBINDINGS,
"Binding keycode 0x%x mask 0x%x matches event 0x%x state 0x%x\n",
bindings[i].keycode, bindings[i].mask,
event->xkey.keycode, event->xkey.state);
keycode, state);
if (handler == NULL)
meta_bug ("Binding %s has no handler\n", bindings[i].name);
@ -1363,7 +1376,7 @@ process_event (MetaKeyBinding *bindings,
meta_topic (META_DEBUG_KEYBINDINGS,
"Running handler for %s\n",
bindings[i].name);
/* Global keybindings count as a let-the-terminal-lose-focus
* due to new window mapping until the user starts
* interacting with the terminal again.
@ -1405,39 +1418,49 @@ meta_display_process_key_event (MetaDisplay *display,
gboolean handled;
const char *str;
MetaScreen *screen;
guint evtype, keycode, state;
Window xwindow;
Time evtime;
if (!meta_input_event_get_type (display, event, &evtype) ||
!meta_input_event_get_keycode (display, event, &keycode) ||
!meta_input_event_get_state (display, event, &state))
return FALSE;
evtime = meta_input_event_get_time (display, event);
XAllowEvents (display->xdisplay,
all_bindings_disabled ? ReplayKeyboard : AsyncKeyboard,
event->xkey.time);
evtime);
if (all_bindings_disabled)
return FALSE;
xwindow = meta_input_event_get_window (display, event);
/* if key event was on root window, we have a shortcut */
screen = meta_display_screen_for_root (display, event->xkey.window);
screen = meta_display_screen_for_root (display, xwindow);
/* else round-trip to server */
if (screen == NULL)
screen = meta_display_screen_for_xwindow (display,
event->xany.window);
screen = meta_display_screen_for_xwindow (display, xwindow);
if (screen == NULL)
return FALSE; /* event window is destroyed */
/* ignore key events on popup menus and such. */
if (meta_ui_window_is_widget (screen->ui, event->xany.window))
if (meta_ui_window_is_widget (screen->ui, xwindow))
return FALSE;
/* window may be NULL */
keysym = XKeycodeToKeysym (display->xdisplay, event->xkey.keycode, 0);
keysym = XKeycodeToKeysym (display->xdisplay, keycode, 0);
str = XKeysymToString (keysym);
/* was topic */
meta_topic (META_DEBUG_KEYBINDINGS,
"Processing key %s event, keysym: %s state: 0x%x window: %s\n",
event->type == KeyPress ? "press" : "release",
str ? str : "none", event->xkey.state,
evtype == KeyPress ? "press" : "release",
str ? str : "none", state,
window ? window->desc : "(no window)");
keep_grab = TRUE;
@ -1521,7 +1544,8 @@ meta_display_process_key_event (MetaDisplay *display,
meta_topic (META_DEBUG_KEYBINDINGS,
"Ending grab op %u on key event sym %s\n",
display->grab_op, XKeysymToString (keysym));
meta_display_end_grab_op (display, event->xkey.time);
meta_display_end_grab_op (display,
meta_input_event_get_time (display, event));
}
return TRUE;
@ -1546,7 +1570,7 @@ process_mouse_move_resize_grab (MetaDisplay *display,
KeySym keysym)
{
/* don't care about releases, but eat them, don't end grab */
if (event->type == KeyRelease)
if (meta_input_event_is_type (display, event, KeyRelease))
return TRUE;
if (keysym == XK_Escape)
@ -1595,27 +1619,31 @@ process_keyboard_move_grab (MetaDisplay *display,
int x, y;
int incr;
gboolean smart_snap;
guint keycode, state;
handled = FALSE;
/* don't care about releases, but eat them, don't end grab */
if (event->type == KeyRelease)
if (meta_input_event_is_type (display, event, KeyRelease))
return TRUE;
meta_input_event_get_keycode (display, event, &keycode);
meta_input_event_get_state (display, event, &state);
/* don't end grab on modifier key presses */
if (is_modifier (display, event->xkey.keycode))
if (is_modifier (display, keycode))
return TRUE;
meta_window_get_position (window, &x, &y);
smart_snap = (event->xkey.state & ShiftMask) != 0;
smart_snap = (state & ShiftMask) != 0;
#define SMALL_INCREMENT 1
#define NORMAL_INCREMENT 10
if (smart_snap)
incr = 1;
else if (event->xkey.state & ControlMask)
else if (state & ControlMask)
incr = SMALL_INCREMENT;
else
incr = NORMAL_INCREMENT;
@ -1841,15 +1869,19 @@ process_keyboard_resize_grab (MetaDisplay *display,
int width, height;
gboolean smart_snap;
int gravity;
guint keycode, state;
handled = FALSE;
/* don't care about releases, but eat them, don't end grab */
if (event->type == KeyRelease)
if (meta_input_event_is_type (display, event, KeyRelease))
return TRUE;
meta_input_event_get_keycode (display, event, &keycode);
meta_input_event_get_state (display, event, &state);
/* don't end grab on modifier key presses */
if (is_modifier (display, event->xkey.keycode))
if (is_modifier (display, keycode))
return TRUE;
if (keysym == XK_Escape)
@ -1874,7 +1906,7 @@ process_keyboard_resize_grab (MetaDisplay *display,
gravity = meta_resize_gravity_from_grab_op (display->grab_op);
smart_snap = (event->xkey.state & ShiftMask) != 0;
smart_snap = (state & ShiftMask) != 0;
#define SMALL_INCREMENT 1
#define NORMAL_INCREMENT 10
@ -1884,7 +1916,7 @@ process_keyboard_resize_grab (MetaDisplay *display,
height_inc = 1;
width_inc = 1;
}
else if (event->xkey.state & ControlMask)
else if (state & ControlMask)
{
width_inc = SMALL_INCREMENT;
height_inc = SMALL_INCREMENT;
@ -2103,13 +2135,18 @@ process_tab_grab (MetaDisplay *display,
gboolean backward;
gboolean key_used;
MetaWindow *prev_window;
guint evtype, keycode;
if (screen != display->grab_screen)
return FALSE;
if (!meta_input_event_get_type (display, event, &evtype) ||
!meta_input_event_get_keycode (display, event, &keycode))
return FALSE;
binding = display_get_keybinding (display,
keysym,
event->xkey.keycode,
keycode,
display->grab_mask);
if (binding)
action = meta_prefs_get_keybinding_action (binding->name);
@ -2123,9 +2160,9 @@ process_tab_grab (MetaDisplay *display,
*/
if (!screen->tab_popup)
{
if (event->type == KeyRelease)
if (evtype == KeyRelease)
{
if (end_keyboard_grab (display, event->xkey.keycode))
if (end_keyboard_grab (display, keycode))
{
invoke_handler_by_name (display, screen, "tab_popup_select", NULL, event);
@ -2174,7 +2211,7 @@ process_tab_grab (MetaDisplay *display,
* If this is simply user pressing the Shift key, we do not want
* to cancel the grab.
*/
if (is_modifier (display, event->xkey.keycode))
if (is_modifier (display, keycode))
return TRUE;
}
@ -2187,8 +2224,8 @@ process_tab_grab (MetaDisplay *display,
return FALSE;
}
if (event->type == KeyRelease &&
end_keyboard_grab (display, event->xkey.keycode))
if (evtype == KeyRelease &&
end_keyboard_grab (display, keycode))
{
/* We're done, move to the new window. */
MetaWindow *target_window;
@ -2200,6 +2237,9 @@ process_tab_grab (MetaDisplay *display,
if (target_window)
{
Time evtime;
evtime = meta_input_event_get_time (display, event);
target_window->tab_unminimized = FALSE;
meta_topic (META_DEBUG_KEYBINDINGS,
@ -2209,11 +2249,11 @@ process_tab_grab (MetaDisplay *display,
"selection and turning mouse_mode off\n",
target_window->desc);
display->mouse_mode = FALSE;
meta_window_activate (target_window, event->xkey.time);
meta_window_activate (target_window, evtime);
meta_topic (META_DEBUG_KEYBINDINGS,
"Ending grab early so we can focus the target window\n");
meta_display_end_grab_op (display, event->xkey.time);
meta_display_end_grab_op (display, evtime);
return TRUE; /* we already ended the grab */
}
@ -2222,11 +2262,11 @@ process_tab_grab (MetaDisplay *display,
}
/* don't care about other releases, but eat them, don't end grab */
if (event->type == KeyRelease)
if (evtype == KeyRelease)
return TRUE;
/* don't end grab on modifier key presses */
if (is_modifier (display, event->xkey.keycode))
if (is_modifier (display, keycode))
return TRUE;
prev_window = meta_screen_tab_popup_get_selected (screen);
@ -2342,10 +2382,14 @@ process_tab_grab (MetaDisplay *display,
if (key_used)
{
guint state;
meta_topic (META_DEBUG_KEYBINDINGS,
"Key pressed, moving tab focus in popup\n");
if (event->xkey.state & ShiftMask)
meta_input_event_get_state (display, event, &state);
if (state & ShiftMask)
backward = !backward;
if (backward)
@ -2432,7 +2476,8 @@ handle_switch_to_workspace (MetaDisplay *display,
if (workspace)
{
meta_workspace_activate (workspace, event->xkey.time);
meta_workspace_activate (workspace,
meta_input_event_get_time (display, event));
}
else
{
@ -2544,8 +2589,10 @@ handle_run_command (MetaDisplay *display,
gint which = binding->handler->data;
const char *command;
GError *err;
Time evtime;
command = meta_prefs_get_command (which);
evtime = meta_input_event_get_time (display, event);
if (command == NULL)
{
@ -2557,7 +2604,7 @@ handle_run_command (MetaDisplay *display,
s = g_strdup_printf (_("No command %d has been defined.\n"),
which + 1);
error_on_command (which, NULL, s, screen, event->xkey.time);
error_on_command (which, NULL, s, screen, evtime);
g_free (s);
return;
@ -2566,8 +2613,8 @@ handle_run_command (MetaDisplay *display,
err = NULL;
if (!meta_spawn_command_line_async_on_screen (command, screen, &err))
{
error_on_command (which, command, err->message, screen, event->xkey.time);
error_on_command (which, command, err->message, screen, evtime);
g_error_free (err);
}
}
@ -2783,12 +2830,20 @@ process_workspace_switch_grab (MetaDisplay *display,
KeySym keysym)
{
MetaWorkspace *workspace;
guint evtype, keycode;
Time evtime;
if (screen != display->grab_screen || !screen->ws_popup)
return FALSE;
if (event->type == KeyRelease &&
end_keyboard_grab (display, event->xkey.keycode))
if (!meta_input_event_get_type (display, event, &evtype) ||
!meta_input_event_get_keycode (display, event, &keycode))
return FALSE;
evtime = meta_input_event_get_time (display, event);
if (evtype == KeyRelease &&
end_keyboard_grab (display, keycode))
{
/* We're done, move to the new workspace. */
MetaWorkspace *target_workspace;
@ -2802,14 +2857,14 @@ process_workspace_switch_grab (MetaDisplay *display,
{
meta_topic (META_DEBUG_KEYBINDINGS,
"Ending grab so we can focus on the target workspace\n");
meta_display_end_grab_op (display, event->xkey.time);
meta_display_end_grab_op (display, evtime);
meta_topic (META_DEBUG_KEYBINDINGS,
"Focusing default window on target workspace\n");
meta_workspace_focus_default_window (target_workspace,
NULL,
event->xkey.time);
evtime);
return TRUE; /* we already ended the grab */
}
@ -2821,11 +2876,11 @@ process_workspace_switch_grab (MetaDisplay *display,
}
/* don't care about other releases, but eat them, don't end grab */
if (event->type == KeyRelease)
if (evtype == KeyRelease)
return TRUE;
/* don't end grab on modifier key presses */
if (is_modifier (display, event->xkey.keycode))
if (is_modifier (display, keycode))
return TRUE;
/* select the next workspace in the popup */
@ -2837,7 +2892,7 @@ process_workspace_switch_grab (MetaDisplay *display,
MetaKeyBindingAction action;
action = meta_display_get_keybinding_action (display,
event->xkey.keycode,
keycode,
display->grab_mask);
switch (action)
@ -2876,7 +2931,7 @@ process_workspace_switch_grab (MetaDisplay *display,
meta_topic (META_DEBUG_KEYBINDINGS,
"Activating target workspace\n");
meta_workspace_activate (target_workspace, event->xkey.time);
meta_workspace_activate (target_workspace, evtime);
return TRUE; /* we already ended the grab */
}
@ -2886,7 +2941,7 @@ process_workspace_switch_grab (MetaDisplay *display,
meta_topic (META_DEBUG_KEYBINDINGS,
"Ending workspace tabbing & focusing default window; uninteresting key pressed\n");
workspace = meta_screen_workspace_popup_get_selected (screen);
meta_workspace_focus_default_window (workspace, NULL, event->xkey.time);
meta_workspace_focus_default_window (workspace, NULL, evtime);
return FALSE;
}
@ -2898,15 +2953,18 @@ handle_show_desktop (MetaDisplay *display,
MetaKeyBinding *binding,
gpointer dummy)
{
Time evtime;
evtime = meta_input_event_get_time (display, event);
if (screen->active_workspace->showing_desktop)
{
meta_screen_unshow_desktop (screen);
meta_workspace_focus_default_window (screen->active_workspace,
NULL,
event->xkey.time);
meta_workspace_focus_default_window (screen->active_workspace,
NULL, evtime);
}
else
meta_screen_show_desktop (screen, event->xkey.time);
meta_screen_show_desktop (screen, evtime);
}
static void
@ -2920,6 +2978,7 @@ handle_panel (MetaDisplay *display,
MetaKeyBindingAction action = binding->handler->data;
Atom action_atom;
XClientMessageEvent ev;
Time evtime;
action_atom = None;
switch (action)
@ -2934,23 +2993,25 @@ handle_panel (MetaDisplay *display,
default:
return;
}
evtime = meta_input_event_get_time (display, event);
ev.type = ClientMessage;
ev.window = screen->xroot;
ev.message_type = display->atom__GNOME_PANEL_ACTION;
ev.format = 32;
ev.data.l[0] = action_atom;
ev.data.l[1] = event->xkey.time;
ev.data.l[1] = evtime;
meta_topic (META_DEBUG_KEYBINDINGS,
"Sending panel message with timestamp %lu, and turning mouse_mode "
"off due to keybinding press\n", event->xkey.time);
"off due to keybinding press\n", evtime);
display->mouse_mode = FALSE;
meta_error_trap_push (display);
/* Release the grab for the panel before sending the event */
XUngrabKeyboard (display->xdisplay, event->xkey.time);
XUngrabKeyboard (display->xdisplay, evtime);
XSendEvent (display->xdisplay,
screen->xroot,
@ -2982,6 +3043,7 @@ handle_activate_window_menu (MetaDisplay *display,
{
if (display->focus_window)
{
Time evtime;
int x, y;
meta_window_get_position (display->focus_window,
@ -2990,10 +3052,12 @@ handle_activate_window_menu (MetaDisplay *display,
if (meta_ui_get_direction() == META_UI_DIRECTION_RTL)
x += display->focus_window->rect.width;
evtime = meta_input_event_get_time (display, event);
meta_window_show_menu (display->focus_window,
x, y,
0,
event->xkey.time);
evtime);
}
}
@ -3044,14 +3108,21 @@ do_choose_window (MetaDisplay *display,
{
MetaTabList type = binding->handler->data;
MetaWindow *initial_selection;
guint state;
Time evtime;
meta_topic (META_DEBUG_KEYBINDINGS,
"Tab list = %u show_popup = %d\n", type, show_popup);
if (!meta_input_event_get_state (display, event, &state))
return;
/* reverse direction if shift is down */
if (event->xkey.state & ShiftMask)
if (state & ShiftMask)
backward = !backward;
evtime = meta_input_event_get_time (display, event);
initial_selection = meta_display_get_tab_next (display,
type,
screen,
@ -3082,7 +3153,7 @@ do_choose_window (MetaDisplay *display,
"switch/cycle windows with no modifiers\n",
initial_selection->desc);
display->mouse_mode = FALSE;
meta_window_activate (initial_selection, event->xkey.time);
meta_window_activate (initial_selection, evtime);
return;
}
@ -3102,7 +3173,7 @@ do_choose_window (MetaDisplay *display,
FALSE,
0,
binding->mask,
event->xkey.time,
evtime,
0, 0))
return;
@ -3117,9 +3188,10 @@ do_choose_window (MetaDisplay *display,
"mouse_mode due to switch/cycle windows where "
"modifier was released prior to grab\n",
initial_selection->desc);
meta_display_end_grab_op (display, event->xkey.time);
meta_display_end_grab_op (display, evtime);
display->mouse_mode = FALSE;
meta_window_activate (initial_selection, event->xkey.time);
meta_window_activate (initial_selection, evtime);
return;
}
@ -3269,10 +3341,14 @@ handle_toggle_shaded (MetaDisplay *display,
MetaKeyBinding *binding,
gpointer dummy)
{
Time evtime;
evtime = meta_input_event_get_time (display, event);
if (window->shaded)
meta_window_unshade (window, event->xkey.time);
meta_window_unshade (window, evtime);
else if (window->has_shade_func)
meta_window_shade (window, event->xkey.time);
meta_window_shade (window, evtime);
}
static void
@ -3284,7 +3360,9 @@ handle_close (MetaDisplay *display,
gpointer dummy)
{
if (window->has_close_func)
meta_window_delete (window, event->xkey.time);
meta_window_delete (window,
meta_input_event_get_time (display,
event));
}
static void
@ -3312,7 +3390,7 @@ handle_begin_move (MetaDisplay *display,
meta_window_begin_grab_op (window,
META_GRAB_OP_KEYBOARD_MOVING,
FALSE,
event->xkey.time);
meta_input_event_get_time (display, event));
}
}
@ -3329,7 +3407,7 @@ handle_begin_resize (MetaDisplay *display,
meta_window_begin_grab_op (window,
META_GRAB_OP_KEYBOARD_RESIZING_UNKNOWN,
FALSE,
event->xkey.time);
meta_input_event_get_time (display, event));
}
}
@ -3387,13 +3465,13 @@ handle_move_to_workspace (MetaDisplay *display,
meta_window_change_workspace (window, workspace);
if (flip)
{
Time evtime = meta_input_event_get_time (display, event);
meta_topic (META_DEBUG_FOCUS,
"Resetting mouse_mode to FALSE due to "
"handle_move_to_workspace() call with flip set.\n");
workspace->screen->display->mouse_mode = FALSE;
meta_workspace_activate_with_focus (workspace,
window,
event->xkey.time);
meta_workspace_activate_with_focus (workspace, window, evtime);
}
}
else
@ -3483,15 +3561,21 @@ handle_workspace_switch (MetaDisplay *display,
unsigned int grab_mask;
MetaWorkspace *next;
gboolean grabbed_before_release;
guint state;
Time evtime;
g_assert (motion < 0);
meta_topic (META_DEBUG_KEYBINDINGS,
"Starting tab between workspaces, showing popup\n");
if (!meta_input_event_get_state (display, event, &state))
return;
/* FIXME should we use binding->mask ? */
grab_mask = event->xkey.state & ~(display->ignored_modifier_mask);
grab_mask = state & ~(display->ignored_modifier_mask);
evtime = meta_input_event_get_time (display, event);
if (!meta_display_begin_grab_op (display,
screen,
NULL,
@ -3500,7 +3584,7 @@ handle_workspace_switch (MetaDisplay *display,
FALSE,
0,
grab_mask,
event->xkey.time,
evtime,
0, 0))
return;
@ -3518,10 +3602,10 @@ handle_workspace_switch (MetaDisplay *display,
* release event. Must end grab before we can switch
* spaces.
*/
meta_display_end_grab_op (display, event->xkey.time);
meta_display_end_grab_op (display, evtime);
}
meta_workspace_activate (next, event->xkey.time);
meta_workspace_activate (next, evtime);
if (grabbed_before_release && !meta_prefs_get_no_tab_popup ())
meta_screen_workspace_popup_create (screen, next);
@ -3555,8 +3639,10 @@ handle_run_terminal (MetaDisplay *display,
gpointer dummy)
{
const char *command;
Time evtime;
GError *err;
evtime = meta_input_event_get_time (display, event);
command = meta_prefs_get_terminal_command ();
if (command == NULL)
@ -3566,11 +3652,11 @@ handle_run_terminal (MetaDisplay *display,
meta_topic (META_DEBUG_KEYBINDINGS,
"No terminal command to run in response to "
"keybinding press\n");
s = g_strdup_printf (_("No terminal command has been defined.\n"));
error_on_command (-1, NULL, s, screen, event->xkey.time);
error_on_command (-1, NULL, s, screen, evtime);
g_free (s);
return;
}
@ -3578,8 +3664,8 @@ handle_run_terminal (MetaDisplay *display,
if (!meta_spawn_command_line_async_on_screen (command, screen, &err))
{
error_on_command (-1, command, err->message, screen,
event->xkey.time);
evtime);
g_error_free (err);
}
}