| 1 |
/* Gtk+ testing utilities |
| 2 |
* Copyright (C) 2007 Imendio AB |
| 3 |
* Authors: Tim Janik |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU Lesser General Public |
| 16 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "config.h" |
| 20 |
|
| 21 |
#include "gdktestutils.h" |
| 22 |
|
| 23 |
#include "gdkkeysyms.h" |
| 24 |
#include "gdkprivate-x11.h" |
| 25 |
|
| 26 |
#include <X11/Xlib.h> |
| 27 |
|
| 28 |
void |
| 29 |
_gdk_x11_window_sync_rendering (GdkWindow *window) |
| 30 |
{ |
| 31 |
Display *display = GDK_WINDOW_XDISPLAY (window); |
| 32 |
XImage *ximage; |
| 33 |
|
| 34 |
/* syncronize to X drawing queue, see: |
| 35 |
* http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html |
| 36 |
*/ |
| 37 |
ximage = XGetImage (display, DefaultRootWindow (display), |
| 38 |
0, 0, 1, 1, AllPlanes, ZPixmap); |
| 39 |
if (ximage != NULL) |
| 40 |
XDestroyImage (ximage); |
| 41 |
} |
| 42 |
|
| 43 |
gboolean |
| 44 |
_gdk_x11_window_simulate_key (GdkWindow *window, |
| 45 |
gint x, |
| 46 |
gint y, |
| 47 |
guint keyval, |
| 48 |
GdkModifierType modifiers, |
| 49 |
GdkEventType key_pressrelease) |
| 50 |
{ |
| 51 |
GdkScreen *screen; |
| 52 |
GdkKeymapKey *keys = NULL; |
| 53 |
gboolean success; |
| 54 |
gint n_keys = 0; |
| 55 |
XKeyEvent xev = { |
| 56 |
0, /* type */ |
| 57 |
0, /* serial */ |
| 58 |
1, /* send_event */ |
| 59 |
}; |
| 60 |
g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE); |
| 61 |
g_return_val_if_fail (window != NULL, FALSE); |
| 62 |
if (!GDK_WINDOW_IS_MAPPED (window)) |
| 63 |
return FALSE; |
| 64 |
|
| 65 |
screen = gdk_window_get_screen (window); |
| 66 |
|
| 67 |
if (x < 0 && y < 0) |
| 68 |
{ |
| 69 |
x = window->width / 2; |
| 70 |
y = window->height / 2; |
| 71 |
} |
| 72 |
|
| 73 |
/* Convert to impl coordinates */ |
| 74 |
x = x + window->abs_x; |
| 75 |
y = y + window->abs_y; |
| 76 |
|
| 77 |
xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease; |
| 78 |
xev.display = GDK_WINDOW_XDISPLAY (window); |
| 79 |
xev.window = GDK_WINDOW_XID (window); |
| 80 |
xev.root = RootWindow (xev.display, GDK_X11_SCREEN (screen)->screen_num); |
| 81 |
xev.subwindow = 0; |
| 82 |
xev.time = 0; |
| 83 |
xev.x = MAX (x, 0); |
| 84 |
xev.y = MAX (y, 0); |
| 85 |
xev.x_root = 0; |
| 86 |
xev.y_root = 0; |
| 87 |
xev.state = modifiers; |
| 88 |
xev.keycode = 0; |
| 89 |
success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_window_get_display (window)), keyval, &keys, &n_keys); |
| 90 |
success &= n_keys > 0; |
| 91 |
if (success) |
| 92 |
{ |
| 93 |
gint i; |
| 94 |
for (i = 0; i < n_keys; i++) |
| 95 |
if (keys[i].group == 0 && (keys[i].level == 0 || keys[i].level == 1)) |
| 96 |
{ |
| 97 |
xev.keycode = keys[i].keycode; |
| 98 |
if (keys[i].level == 1) |
| 99 |
{ |
| 100 |
/* Assume shift takes us to level 1 */ |
| 101 |
xev.state |= GDK_SHIFT_MASK; |
| 102 |
} |
| 103 |
break; |
| 104 |
} |
| 105 |
if (i >= n_keys) /* no match for group==0 and level==0 or 1 */ |
| 106 |
xev.keycode = keys[0].keycode; |
| 107 |
} |
| 108 |
g_free (keys); |
| 109 |
if (!success) |
| 110 |
return FALSE; |
| 111 |
gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window)); |
| 112 |
xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root, |
| 113 |
xev.x, xev.y, &xev.x_root, &xev.y_root, |
| 114 |
&xev.subwindow); |
| 115 |
if (!xev.subwindow) |
| 116 |
xev.subwindow = xev.window; |
| 117 |
success &= xev.same_screen; |
| 118 |
if (x >= 0 && y >= 0) |
| 119 |
success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y); |
| 120 |
success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev); |
| 121 |
XSync (xev.display, False); |
| 122 |
success &= 0 == gdk_x11_display_error_trap_pop (GDK_WINDOW_DISPLAY (window)); |
| 123 |
return success; |
| 124 |
} |
| 125 |
|
| 126 |
gboolean |
| 127 |
_gdk_x11_window_simulate_button (GdkWindow *window, |
| 128 |
gint x, |
| 129 |
gint y, |
| 130 |
guint button, /*1..3*/ |
| 131 |
GdkModifierType modifiers, |
| 132 |
GdkEventType button_pressrelease) |
| 133 |
{ |
| 134 |
GdkScreen *screen; |
| 135 |
XButtonEvent xev = { |
| 136 |
0, /* type */ |
| 137 |
0, /* serial */ |
| 138 |
1, /* send_event */ |
| 139 |
}; |
| 140 |
gboolean success; |
| 141 |
|
| 142 |
g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE); |
| 143 |
g_return_val_if_fail (window != NULL, FALSE); |
| 144 |
|
| 145 |
if (!GDK_WINDOW_IS_MAPPED (window)) |
| 146 |
return FALSE; |
| 147 |
|
| 148 |
screen = gdk_window_get_screen (window); |
| 149 |
|
| 150 |
if (x < 0 && y < 0) |
| 151 |
{ |
| 152 |
x = window->width / 2; |
| 153 |
y = window->height / 2; |
| 154 |
} |
| 155 |
|
| 156 |
/* Convert to impl coordinates */ |
| 157 |
x = x + window->abs_x; |
| 158 |
y = y + window->abs_y; |
| 159 |
|
| 160 |
xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease; |
| 161 |
xev.display = GDK_WINDOW_XDISPLAY (window); |
| 162 |
xev.window = GDK_WINDOW_XID (window); |
| 163 |
xev.root = RootWindow (xev.display, GDK_X11_SCREEN (screen)->screen_num); |
| 164 |
xev.subwindow = 0; |
| 165 |
xev.time = 0; |
| 166 |
xev.x = x; |
| 167 |
xev.y = y; |
| 168 |
xev.x_root = 0; |
| 169 |
xev.y_root = 0; |
| 170 |
xev.state = modifiers; |
| 171 |
xev.button = button; |
| 172 |
gdk_x11_display_error_trap_push (GDK_WINDOW_DISPLAY (window)); |
| 173 |
xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root, |
| 174 |
xev.x, xev.y, &xev.x_root, &xev.y_root, |
| 175 |
&xev.subwindow); |
| 176 |
if (!xev.subwindow) |
| 177 |
xev.subwindow = xev.window; |
| 178 |
success = xev.same_screen; |
| 179 |
success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y); |
| 180 |
success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev); |
| 181 |
XSync (xev.display, False); |
| 182 |
success &= 0 == gdk_x11_display_error_trap_pop(GDK_WINDOW_DISPLAY (window)); |
| 183 |
return success; |
| 184 |
} |