]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdktestutils-x11.c
gdk: Rename GdkWindowObject to GdkWindow
[~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_WINDOW_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   gboolean success;
98   gint n_keys = 0;
99   XKeyEvent xev = {
100     0,  /* type */
101     0,  /* serial */
102     1,  /* send_event */
103   };
104   g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
105   g_return_val_if_fail (window != NULL, FALSE);
106   if (!GDK_WINDOW_IS_MAPPED (window))
107     return FALSE;
108
109   screen = gdk_window_get_screen (window);
110
111   if (x < 0 && y < 0)
112     {
113       x = window->width / 2;
114       y = window->height / 2;
115     }
116
117   /* Convert to impl coordinates */
118   x = x + window->abs_x;
119   y = y + window->abs_y;
120
121   xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
122   xev.display = GDK_WINDOW_XDISPLAY (window);
123   xev.window = GDK_WINDOW_XID (window);
124   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
125   xev.subwindow = 0;
126   xev.time = 0;
127   xev.x = MAX (x, 0);
128   xev.y = MAX (y, 0);
129   xev.x_root = 0;
130   xev.y_root = 0;
131   xev.state = modifiers;
132   xev.keycode = 0;
133   success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_window_get_display (window)), keyval, &keys, &n_keys);
134   success &= n_keys > 0;
135   if (success)
136     {
137       gint i;
138       for (i = 0; i < n_keys; i++)
139         if (keys[i].group == 0 && keys[i].level == 0)
140           {
141             xev.keycode = keys[i].keycode;
142             break;
143           }
144       if (i >= n_keys) /* no match for group==0 and level==0 */
145         xev.keycode = keys[0].keycode;
146     }
147   g_free (keys);
148   if (!success)
149     return FALSE;
150   gdk_error_trap_push ();
151   xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
152                                            xev.x, xev.y, &xev.x_root, &xev.y_root,
153                                            &xev.subwindow);
154   if (!xev.subwindow)
155     xev.subwindow = xev.window;
156   success &= xev.same_screen;
157   if (x >= 0 && y >= 0)
158     success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
159   success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
160   XSync (xev.display, False);
161   success &= 0 == gdk_error_trap_pop();
162   return success;
163 }
164
165 /**
166  * gdk_test_simulate_button
167  * @window: a #GdkWindow to simulate a button event for.
168  * @x:      x coordinate within @window for the button event.
169  * @y:      y coordinate within @window for the button event.
170  * @button: Number of the pointer button for the event, usually 1, 2 or 3.
171  * @modifiers: Keyboard modifiers the event is setup with.
172  * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
173  *
174  * This function is intended to be used in GTK+ test programs.
175  * It will warp the mouse pointer to the given (@x,@y) corrdinates
176  * within @window and simulate a button press or release event.
177  * Because the mouse pointer needs to be warped to the target
178  * location, use of this function outside of test programs that
179  * run in their own virtual windowing system (e.g. Xvfb) is not
180  * recommended.
181  *
182  * Also, gtk_test_simulate_button() is a fairly low level function,
183  * for most testing purposes, gtk_test_widget_click() is the right
184  * function to call which will generate a button press event followed
185  * by its accompanying button release event.
186  *
187  * Returns: whether all actions neccessary for a button event simulation 
188  *     were carried out successfully.
189  *
190  * Since: 2.14
191  **/
192 gboolean
193 gdk_test_simulate_button (GdkWindow      *window,
194                           gint            x,
195                           gint            y,
196                           guint           button, /*1..3*/
197                           GdkModifierType modifiers,
198                           GdkEventType    button_pressrelease)
199 {
200   GdkScreen *screen;
201   XButtonEvent xev = {
202     0,  /* type */
203     0,  /* serial */
204     1,  /* send_event */
205   };
206   gboolean success;
207
208   g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
209   g_return_val_if_fail (window != NULL, FALSE);
210
211   if (!GDK_WINDOW_IS_MAPPED (window))
212     return FALSE;
213
214   screen = gdk_window_get_screen (window);
215
216   if (x < 0 && y < 0)
217     {
218       x = window->width / 2;
219       y = window->height / 2;
220     }
221
222   /* Convert to impl coordinates */
223   x = x + window->abs_x;
224   y = y + window->abs_y;
225
226   xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
227   xev.display = GDK_WINDOW_XDISPLAY (window);
228   xev.window = GDK_WINDOW_XID (window);
229   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
230   xev.subwindow = 0;
231   xev.time = 0;
232   xev.x = x;
233   xev.y = y;
234   xev.x_root = 0;
235   xev.y_root = 0;
236   xev.state = modifiers;
237   xev.button = button;
238   gdk_error_trap_push ();
239   xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
240                                            xev.x, xev.y, &xev.x_root, &xev.y_root,
241                                            &xev.subwindow);
242   if (!xev.subwindow)
243     xev.subwindow = xev.window;
244   success = xev.same_screen;
245   success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
246   success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
247   XSync (xev.display, False);
248   success &= 0 == gdk_error_trap_pop();
249   return success;
250 }