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