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