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