]> Pileus Git - ~andy/gtk/blob - gdk/x11/gdktestutils-x11.c
Moved Gdk test functions from Gtk+ to Gdk test utils.
[~andy/gtk] / gdk / x11 / gdktestutils-x11.c
1 /* Gtk+ testing utilities
2  * Copyright (C) 2007 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <gdk/gdktestutils.h>
20 #include <gdk/gdkkeysyms.h>
21 #include <x11/gdkx.h>
22 #include "gdkalias.h"
23
24 #include <X11/Xlib.h>
25
26 /**
27  * gdk_test_render_sync
28  * @window: a mapped GdkWindow
29  *
30  * This function retrives 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 void
36 gdk_test_render_sync (GdkWindow *window)
37 {
38   static GdkImage *p1image = NULL;
39   /* syncronize to X drawing queue, see:
40    * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
41    */
42   p1image = gdk_drawable_copy_to_image (window, p1image, 0, 0, 0, 0, 1, 1);
43 }
44
45 /**
46  * gdk_test_simulate_key
47  * @window: Gdk window to simulate a key event for.
48  * @x:      x coordinate within @window for the key event.
49  * @y:      y coordinate within @window for the key event.
50  * @keyval: A Gdk keyboard value.
51  * @modifiers: Keyboard modifiers the event is setup with.
52  * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
53  *
54  * This function is intended to be used in Gtk+ test programs.
55  * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
56  * the given (@x,@y) corrdinates within @window and simulate a
57  * key press or release event.
58  * When the mouse pointer is warped to the target location, use
59  * of this function outside of test programs that run in their
60  * own virtual windowing system (e.g. Xvfb) is not recommended.
61  * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
62  * be warped and @window origin will be used as mouse pointer
63  * location for the event.
64  * Also, gtk_test_simulate_key() is a fairly low level function,
65  * for most testing purposes, gtk_test_widget_send_key() is the
66  * right function to call which will generate a key press event
67  * followed by its accompanying key release event.
68  *
69  * Returns: wether all actions neccessary for a key event simulation were carried out successfully.
70  **/
71 gboolean
72 gdk_test_simulate_key (GdkWindow      *window,
73                        gint            x,
74                        gint            y,
75                        guint           keyval,
76                        GdkModifierType modifiers,
77                        GdkEventType    key_pressrelease)
78 {
79   GdkScreen *screen;
80   GdkKeymapKey *keys = NULL;
81   gboolean success;
82   gint n_keys = 0;
83   XKeyEvent xev = {
84     0,  /* type */
85     0,  /* serial */
86     1,  /* send_event */
87   };
88   g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
89   g_return_val_if_fail (window != NULL, FALSE);
90   if (!GDK_WINDOW_IS_MAPPED (window))
91     return FALSE;
92   screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
93   if (x < 0 && y < 0)
94     {
95       gdk_drawable_get_size (window, &x, &y);
96       x /= 2;
97       y /= 2;
98     }
99   xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
100   xev.display = GDK_DRAWABLE_XDISPLAY (window);
101   xev.window = GDK_WINDOW_XID (window);
102   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
103   xev.subwindow = 0;
104   xev.time = 0;
105   xev.x = MAX (x, 0);
106   xev.y = MAX (y, 0);
107   xev.x_root = 0;
108   xev.y_root = 0;
109   xev.state = modifiers;
110   xev.keycode = 0;
111   success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_drawable_get_display (window)), keyval, &keys, &n_keys);
112   success &= n_keys > 0;
113   if (success)
114     {
115       gint i;
116       for (i = 0; i < n_keys; i++)
117         if (keys[i].group == 0 && keys[i].level == 0)
118           {
119             xev.keycode = keys[i].keycode;
120             break;
121           }
122       if (i >= n_keys) /* no match for group==0 and level==0 */
123         xev.keycode = keys[0].keycode;
124     }
125   g_free (keys);
126   if (!success)
127     return FALSE;
128   gdk_error_trap_push ();
129   xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
130                                            xev.x, xev.y, &xev.x_root, &xev.y_root,
131                                            &xev.subwindow);
132   if (!xev.subwindow)
133     xev.subwindow = xev.window;
134   success &= xev.same_screen;
135   if (x >= 0 && y >= 0)
136     success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
137   success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
138   XSync (xev.display, False);
139   success &= 0 == gdk_error_trap_pop();
140   return success;
141 }
142
143 /**
144  * gdk_test_simulate_button
145  * @window: Gdk window to simulate a button event for.
146  * @x:      x coordinate within @window for the button event.
147  * @y:      y coordinate within @window for the button event.
148  * @button: Number of the pointer button for the event, usually 1, 2 or 3.
149  * @modifiers: Keyboard modifiers the event is setup with.
150  * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
151  *
152  * This function is intended to be used in Gtk+ test programs.
153  * It will warp the mouse pointer to the given (@x,@y) corrdinates
154  * within @window and simulate a button press or release event.
155  * Because the mouse pointer needs to be warped to the target
156  * location, use of this function outside of test programs that
157  * run in their own virtual windowing system (e.g. Xvfb) is not
158  * recommended.
159  * Also, gtk_test_simulate_button() is a fairly low level function,
160  * for most testing purposes, gtk_test_widget_click() is the right
161  * function to call which will generate a button press event followed
162  * by its accompanying button release event.
163  *
164  * Returns: wether all actions neccessary for a button event simulation were carried out successfully.
165  **/
166 gboolean
167 gdk_test_simulate_button (GdkWindow      *window,
168                           gint            x,
169                           gint            y,
170                           guint           button, /*1..3*/
171                           GdkModifierType modifiers,
172                           GdkEventType    button_pressrelease)
173 {
174   GdkScreen *screen;
175   XButtonEvent xev = {
176     0,  /* type */
177     0,  /* serial */
178     1,  /* send_event */
179   };
180   g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
181   g_return_val_if_fail (window != NULL, FALSE);
182   if (!GDK_WINDOW_IS_MAPPED (window))
183     return FALSE;
184   screen = gdk_colormap_get_screen (gdk_drawable_get_colormap (window));
185   if (x < 0 && y < 0)
186     {
187       gdk_drawable_get_size (window, &x, &y);
188       x /= 2;
189       y /= 2;
190     }
191   xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
192   xev.display = GDK_DRAWABLE_XDISPLAY (window);
193   xev.window = GDK_WINDOW_XID (window);
194   xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
195   xev.subwindow = 0;
196   xev.time = 0;
197   xev.x = x;
198   xev.y = y;
199   xev.x_root = 0;
200   xev.y_root = 0;
201   xev.state = modifiers;
202   xev.button = button;
203   gdk_error_trap_push ();
204   xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
205                                            xev.x, xev.y, &xev.x_root, &xev.y_root,
206                                            &xev.subwindow);
207   if (!xev.subwindow)
208     xev.subwindow = xev.window;
209   gboolean success = xev.same_screen;
210   success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
211   success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
212   XSync (xev.display, False);
213   success &= 0 == gdk_error_trap_pop();
214   return success;
215 }
216
217 #define __GDK_TEST_UTILS_X11_C__
218 #include "gdkaliasdef.c"