| 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, write to the |
| 17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 |
* Boston, MA 02111-1307, USA. |
| 19 |
*/ |
| 20 |
#include <gdk/gdktestutils.h> |
| 21 |
#include <gdk/gdkkeysyms.h> |
| 22 |
#include <x11/gdkx.h> |
| 23 |
#include "gdkalias.h" |
| 24 |
|
| 25 |
#include <X11/Xlib.h> |
| 26 |
|
| 27 |
/** |
| 28 |
* gdk_test_render_sync: |
| 29 |
* @window: a mapped #GdkWindow |
| 30 |
* |
| 31 |
* This function retrieves a pixel from @window to force the windowing |
| 32 |
* system to carry out any pending rendering commands. |
| 33 |
* This function is intended to be used to syncronize with rendering |
| 34 |
* pipelines, to benchmark windowing system rendering operations. |
| 35 |
* |
| 36 |
* Since: 2.14 |
| 37 |
**/ |
| 38 |
void |
| 39 |
gdk_test_render_sync (GdkWindow *window) |
| 40 |
{ |
| 41 |
static GdkImage *p1image = NULL; |
| 42 |
/* syncronize to X drawing queue, see: |
| 43 |
* http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html |
| 44 |
*/ |
| 45 |
p1image = gdk_drawable_copy_to_image (window, p1image, 0, 0, 0, 0, 1, 1); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* gdk_test_simulate_key |
| 50 |
* @window: a #GdkWindow to simulate a key event for. |
| 51 |
* @x: x coordinate within @window for the key event. |
| 52 |
* @y: y coordinate within @window for the key event. |
| 53 |
* @keyval: A GDK keyboard value. |
| 54 |
* @modifiers: Keyboard modifiers the event is setup with. |
| 55 |
* @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE |
| 56 |
* |
| 57 |
* This function is intended to be used in GTK+ test programs. |
| 58 |
* If (@x,@y) are > (-1,-1), it will warp the mouse pointer to |
| 59 |
* the given (@x,@y) corrdinates within @window and simulate a |
| 60 |
* key press or release event. |
| 61 |
* |
| 62 |
* When the mouse pointer is warped to the target location, use |
| 63 |
* of this function outside of test programs that run in their |
| 64 |
* own virtual windowing system (e.g. Xvfb) is not recommended. |
| 65 |
* If (@x,@y) are passed as (-1,-1), the mouse pointer will not |
| 66 |
* be warped and @window origin will be used as mouse pointer |
| 67 |
* location for the event. |
| 68 |
* |
| 69 |
* Also, gtk_test_simulate_key() is a fairly low level function, |
| 70 |
* for most testing purposes, gtk_test_widget_send_key() is the |
| 71 |
* right function to call which will generate a key press event |
| 72 |
* followed by its accompanying key release event. |
| 73 |
* |
| 74 |
* Returns: whether all actions neccessary for a key event simulation |
| 75 |
* were carried out successfully. |
| 76 |
* |
| 77 |
* Since: 2.14 |
| 78 |
**/ |
| 79 |
gboolean |
| 80 |
gdk_test_simulate_key (GdkWindow *window, |
| 81 |
gint x, |
| 82 |
gint y, |
| 83 |
guint keyval, |
| 84 |
GdkModifierType modifiers, |
| 85 |
GdkEventType key_pressrelease) |
| 86 |
{ |
| 87 |
GdkScreen *screen; |
| 88 |
GdkKeymapKey *keys = NULL; |
| 89 |
GdkWindowObject *priv; |
| 90 |
gboolean success; |
| 91 |
gint n_keys = 0; |
| 92 |
XKeyEvent xev = { |
| 93 |
0, /* type */ |
| 94 |
0, /* serial */ |
| 95 |
1, /* send_event */ |
| 96 |
}; |
| 97 |
g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE); |
| 98 |
g_return_val_if_fail (window != NULL, FALSE); |
| 99 |
if (!GDK_WINDOW_IS_MAPPED (window)) |
| 100 |
return FALSE; |
| 101 |
screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window)); |
| 102 |
if (x < 0 && y < 0) |
| 103 |
{ |
| 104 |
gdk_drawable_get_size (window, &x, &y); |
| 105 |
x /= 2; |
| 106 |
y /= 2; |
| 107 |
} |
| 108 |
|
| 109 |
priv = (GdkWindowObject *)window; |
| 110 |
/* Convert to impl coordinates */ |
| 111 |
x = x + priv->abs_x; |
| 112 |
y = y + priv->abs_y; |
| 113 |
|
| 114 |
xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease; |
| 115 |
xev.display = GDK_DRAWABLE_XDISPLAY (window); |
| 116 |
xev.window = GDK_WINDOW_XID (window); |
| 117 |
xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen)); |
| 118 |
xev.subwindow = 0; |
| 119 |
xev.time = 0; |
| 120 |
xev.x = MAX (x, 0); |
| 121 |
xev.y = MAX (y, 0); |
| 122 |
xev.x_root = 0; |
| 123 |
xev.y_root = 0; |
| 124 |
xev.state = modifiers; |
| 125 |
xev.keycode = 0; |
| 126 |
success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_drawable_get_display (window)), keyval, &keys, &n_keys); |
| 127 |
success &= n_keys > 0; |
| 128 |
if (success) |
| 129 |
{ |
| 130 |
gint i; |
| 131 |
for (i = 0; i < n_keys; i++) |
| 132 |
if (keys[i].group == 0 && (keys[i].level == 0 || keys[i].level == 1)) |
| 133 |
{ |
| 134 |
xev.keycode = keys[i].keycode; |
| 135 |
if (keys[i].level == 1) |
| 136 |
{ |
| 137 |
/* Assume shift takes us to level 1 */ |
| 138 |
xev.state |= GDK_SHIFT_MASK; |
| 139 |
} |
| 140 |
break; |
| 141 |
} |
| 142 |
if (i >= n_keys) /* no match for group==0 and level==0 or 1 */ |
| 143 |
xev.keycode = keys[0].keycode; |
| 144 |
} |
| 145 |
g_free (keys); |
| 146 |
if (!success) |
| 147 |
return FALSE; |
| 148 |
gdk_error_trap_push (); |
| 149 |
xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root, |
| 150 |
xev.x, xev.y, &xev.x_root, &xev.y_root, |
| 151 |
&xev.subwindow); |
| 152 |
if (!xev.subwindow) |
| 153 |
xev.subwindow = xev.window; |
| 154 |
success &= xev.same_screen; |
| 155 |
if (x >= 0 && y >= 0) |
| 156 |
success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y); |
| 157 |
success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev); |
| 158 |
XSync (xev.display, False); |
| 159 |
success &= 0 == gdk_error_trap_pop(); |
| 160 |
return success; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* gdk_test_simulate_button |
| 165 |
* @window: a #GdkWindow to simulate a button event for. |
| 166 |
* @x: x coordinate within @window for the button event. |
| 167 |
* @y: y coordinate within @window for the button event. |
| 168 |
* @button: Number of the pointer button for the event, usually 1, 2 or 3. |
| 169 |
* @modifiers: Keyboard modifiers the event is setup with. |
| 170 |
* @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE |
| 171 |
* |
| 172 |
* This function is intended to be used in GTK+ test programs. |
| 173 |
* It will warp the mouse pointer to the given (@x,@y) corrdinates |
| 174 |
* within @window and simulate a button press or release event. |
| 175 |
* Because the mouse pointer needs to be warped to the target |
| 176 |
* location, use of this function outside of test programs that |
| 177 |
* run in their own virtual windowing system (e.g. Xvfb) is not |
| 178 |
* recommended. |
| 179 |
* |
| 180 |
* Also, gtk_test_simulate_button() is a fairly low level function, |
| 181 |
* for most testing purposes, gtk_test_widget_click() is the right |
| 182 |
* function to call which will generate a button press event followed |
| 183 |
* by its accompanying button release event. |
| 184 |
* |
| 185 |
* Returns: whether all actions neccessary for a button event simulation |
| 186 |
* were carried out successfully. |
| 187 |
* |
| 188 |
* Since: 2.14 |
| 189 |
**/ |
| 190 |
gboolean |
| 191 |
gdk_test_simulate_button (GdkWindow *window, |
| 192 |
gint x, |
| 193 |
gint y, |
| 194 |
guint button, /*1..3*/ |
| 195 |
GdkModifierType modifiers, |
| 196 |
GdkEventType button_pressrelease) |
| 197 |
{ |
| 198 |
GdkScreen *screen; |
| 199 |
XButtonEvent xev = { |
| 200 |
0, /* type */ |
| 201 |
0, /* serial */ |
| 202 |
1, /* send_event */ |
| 203 |
}; |
| 204 |
gboolean success; |
| 205 |
GdkWindowObject *priv; |
| 206 |
|
| 207 |
g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE); |
| 208 |
g_return_val_if_fail (window != NULL, FALSE); |
| 209 |
|
| 210 |
if (!GDK_WINDOW_IS_MAPPED (window)) |
| 211 |
return FALSE; |
| 212 |
screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window)); |
| 213 |
if (x < 0 && y < 0) |
| 214 |
{ |
| 215 |
gdk_drawable_get_size (window, &x, &y); |
| 216 |
x /= 2; |
| 217 |
y /= 2; |
| 218 |
} |
| 219 |
|
| 220 |
priv = (GdkWindowObject *)window; |
| 221 |
/* Convert to impl coordinates */ |
| 222 |
x = x + priv->abs_x; |
| 223 |
y = y + priv->abs_y; |
| 224 |
|
| 225 |
xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease; |
| 226 |
xev.display = GDK_DRAWABLE_XDISPLAY (window); |
| 227 |
xev.window = GDK_WINDOW_XID (window); |
| 228 |
xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen)); |
| 229 |
xev.subwindow = 0; |
| 230 |
xev.time = 0; |
| 231 |
xev.x = x; |
| 232 |
xev.y = y; |
| 233 |
xev.x_root = 0; |
| 234 |
xev.y_root = 0; |
| 235 |
xev.state = modifiers; |
| 236 |
xev.button = button; |
| 237 |
gdk_error_trap_push (); |
| 238 |
xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root, |
| 239 |
xev.x, xev.y, &xev.x_root, &xev.y_root, |
| 240 |
&xev.subwindow); |
| 241 |
if (!xev.subwindow) |
| 242 |
xev.subwindow = xev.window; |
| 243 |
success = xev.same_screen; |
| 244 |
success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y); |
| 245 |
success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev); |
| 246 |
XSync (xev.display, False); |
| 247 |
success &= 0 == gdk_error_trap_pop(); |
| 248 |
return success; |
| 249 |
} |
| 250 |
|
| 251 |
#define __GDK_TEST_UTILS_X11_C__ |
| 252 |
#include "gdkaliasdef.c" |