]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdktestutils-x11.c
x11: Get rid of gdk_drawable_get_size() usage
[~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   Display *display = gdk_x11_drawable_get_xdisplay (window);
41   XImage *ximage;
42
43   /* syncronize to X drawing queue, see:
44    * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
45    */
46   ximage = XGetImage (display, DefaultRootWindow (display),
47                       0, 0, 1, 1, AllPlanes, ZPixmap);
48   if (ximage != NULL)
49     XDestroyImage (ximage);
50 }
51
52 /**
53  * gdk_test_simulate_key
54  * @window: a #GdkWindow to simulate a key event for.
55  * @x:      x coordinate within @window for the key event.
56  * @y:      y coordinate within @window for the key event.
57  * @keyval: A GDK keyboard value.
58  * @modifiers: Keyboard modifiers the event is setup with.
59  * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
60  *
61  * This function is intended to be used in GTK+ test programs.
62  * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
63  * the given (@x,@y) corrdinates within @window and simulate a
64  * key press or release event.
65  *
66  * When the mouse pointer is warped to the target location, use
67  * of this function outside of test programs that run in their
68  * own virtual windowing system (e.g. Xvfb) is not recommended.
69  * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
70  * be warped and @window origin will be used as mouse pointer
71  * location for the event.
72  *
73  * Also, gtk_test_simulate_key() is a fairly low level function,
74  * for most testing purposes, gtk_test_widget_send_key() is the
75  * right function to call which will generate a key press event
76  * followed by its accompanying key release event.
77  *
78  * Returns: whether all actions neccessary for a key event simulation 
79  *     were carried out successfully.
80  *
81  * Since: 2.14
82  **/
83 gboolean
84 gdk_test_simulate_key (GdkWindow      *window,
85                        gint            x,
86                        gint            y,
87                        guint           keyval,
88                        GdkModifierType modifiers,
89                        GdkEventType    key_pressrelease)
90 {
91   GdkScreen *screen;
92   GdkKeymapKey *keys = NULL;
93   GdkWindowObject *priv;
94   gboolean success;
95   gint n_keys = 0;
96   XKeyEvent xev = {
97     0,  /* type */
98     0,  /* serial */
99     1,  /* send_event */
100   };
101   g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
102   g_return_val_if_fail (window != NULL, FALSE);
103   if (!GDK_WINDOW_IS_MAPPED (window))
104     return FALSE;
105
106   screen = gdk_window_get_screen (window);
107   priv = (GdkWindowObject *)window;
108
109   if (x < 0 && y < 0)
110     {
111       x = priv->width / 2;
112       y = priv->height / 2;
113     }
114
115   /* Convert to impl coordinates */
116   x = x + priv->abs_x;
117   y = y + priv->abs_y;
118
119   xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
120   xev.display = GDK_DRAWABLE_XDISPLAY (window);
121   xev.window = GDK_WINDOW_XID (window);
122   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
123   xev.subwindow = 0;
124   xev.time = 0;
125   xev.x = MAX (x, 0);
126   xev.y = MAX (y, 0);
127   xev.x_root = 0;
128   xev.y_root = 0;
129   xev.state = modifiers;
130   xev.keycode = 0;
131   success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_window_get_display (window)), keyval, &keys, &n_keys);
132   success &= n_keys > 0;
133   if (success)
134     {
135       gint i;
136       for (i = 0; i < n_keys; i++)
137         if (keys[i].group == 0 && keys[i].level == 0)
138           {
139             xev.keycode = keys[i].keycode;
140             break;
141           }
142       if (i >= n_keys) /* no match for group==0 and level==0 */
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
213   screen = gdk_window_get_screen (window);
214   priv = (GdkWindowObject *)window;
215
216   if (x < 0 && y < 0)
217     {
218       x = priv->width / 2;
219       y = priv->height / 2;
220     }
221
222   /* Convert to impl coordinates */
223   x = x + priv->abs_x;
224   y = y + priv->abs_y;
225
226   xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
227   xev.display = GDK_DRAWABLE_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 }