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