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