gdktestutils-x11.c 8.6 KB
Newer Older
1
/* Gtk+ testing utilities
2
3
 * Copyright (C) 2007 Imendio AB
 * Authors: Tim Janik
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
20
21
22
23
24
25
26

#include "config.h"

#include "gdktestutils.h"

#include "gdkkeysyms.h"
#include "gdkx.h"
27
28
29
30

#include <X11/Xlib.h>

/**
Matthias Clasen's avatar
Matthias Clasen committed
31
32
 * gdk_test_render_sync:
 * @window: a mapped #GdkWindow
33
 *
Matthias Clasen's avatar
Matthias Clasen committed
34
 * This function retrieves a pixel from @window to force the windowing
35
36
37
 * system to carry out any pending rendering commands.
 * This function is intended to be used to syncronize with rendering
 * pipelines, to benchmark windowing system rendering operations.
Matthias Clasen's avatar
Matthias Clasen committed
38
39
 *
 * Since: 2.14
40
41
42
43
 **/
void
gdk_test_render_sync (GdkWindow *window)
{
44
45
46
  Display *display = gdk_x11_drawable_get_xdisplay (window);
  XImage *ximage;

47
48
49
  /* syncronize to X drawing queue, see:
   * http://mail.gnome.org/archives/gtk-devel-list/2006-October/msg00103.html
   */
50
51
52
53
  ximage = XGetImage (display, DefaultRootWindow (display),
	     	      0, 0, 1, 1, AllPlanes, ZPixmap);
  if (ximage != NULL)
    XDestroyImage (ximage);
54
55
56
57
}

/**
 * gdk_test_simulate_key
Matthias Clasen's avatar
Matthias Clasen committed
58
 * @window: a #GdkWindow to simulate a key event for.
59
60
 * @x:      x coordinate within @window for the key event.
 * @y:      y coordinate within @window for the key event.
Matthias Clasen's avatar
Matthias Clasen committed
61
 * @keyval: A GDK keyboard value.
62
63
64
 * @modifiers: Keyboard modifiers the event is setup with.
 * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
 *
Matthias Clasen's avatar
Matthias Clasen committed
65
 * This function is intended to be used in GTK+ test programs.
66
67
68
 * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
 * the given (@x,@y) corrdinates within @window and simulate a
 * key press or release event.
Matthias Clasen's avatar
Matthias Clasen committed
69
 *
70
71
72
73
74
75
 * When the mouse pointer is warped to the target location, use
 * of this function outside of test programs that run in their
 * own virtual windowing system (e.g. Xvfb) is not recommended.
 * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
 * be warped and @window origin will be used as mouse pointer
 * location for the event.
Matthias Clasen's avatar
Matthias Clasen committed
76
 *
77
78
79
80
81
 * Also, gtk_test_simulate_key() is a fairly low level function,
 * for most testing purposes, gtk_test_widget_send_key() is the
 * right function to call which will generate a key press event
 * followed by its accompanying key release event.
 *
82
 * Returns: whether all actions neccessary for a key event simulation 
Matthias Clasen's avatar
Matthias Clasen committed
83
84
85
 *     were carried out successfully.
 *
 * Since: 2.14
86
87
88
89
90
91
92
93
94
95
96
 **/
gboolean
gdk_test_simulate_key (GdkWindow      *window,
                       gint            x,
                       gint            y,
                       guint           keyval,
                       GdkModifierType modifiers,
                       GdkEventType    key_pressrelease)
{
  GdkScreen *screen;
  GdkKeymapKey *keys = NULL;
97
  GdkWindowObject *priv;
98
99
100
101
102
103
104
105
106
107
108
  gboolean success;
  gint n_keys = 0;
  XKeyEvent xev = {
    0,  /* type */
    0,  /* serial */
    1,  /* send_event */
  };
  g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS || key_pressrelease == GDK_KEY_RELEASE, FALSE);
  g_return_val_if_fail (window != NULL, FALSE);
  if (!GDK_WINDOW_IS_MAPPED (window))
    return FALSE;
109

110
  screen = gdk_window_get_screen (window);
111
112
  priv = (GdkWindowObject *)window;

113
114
  if (x < 0 && y < 0)
    {
115
116
      x = priv->width / 2;
      y = priv->height / 2;
117
    }
118
119
120
121
122

  /* Convert to impl coordinates */
  x = x + priv->abs_x;
  y = y + priv->abs_y;

123
124
125
126
127
128
129
130
131
132
133
134
  xev.type = key_pressrelease == GDK_KEY_PRESS ? KeyPress : KeyRelease;
  xev.display = GDK_DRAWABLE_XDISPLAY (window);
  xev.window = GDK_WINDOW_XID (window);
  xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
  xev.subwindow = 0;
  xev.time = 0;
  xev.x = MAX (x, 0);
  xev.y = MAX (y, 0);
  xev.x_root = 0;
  xev.y_root = 0;
  xev.state = modifiers;
  xev.keycode = 0;
135
  success = gdk_keymap_get_entries_for_keyval (gdk_keymap_get_for_display (gdk_window_get_display (window)), keyval, &keys, &n_keys);
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  success &= n_keys > 0;
  if (success)
    {
      gint i;
      for (i = 0; i < n_keys; i++)
        if (keys[i].group == 0 && keys[i].level == 0)
          {
            xev.keycode = keys[i].keycode;
            break;
          }
      if (i >= n_keys) /* no match for group==0 and level==0 */
        xev.keycode = keys[0].keycode;
    }
  g_free (keys);
  if (!success)
    return FALSE;
  gdk_error_trap_push ();
  xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
                                           xev.x, xev.y, &xev.x_root, &xev.y_root,
                                           &xev.subwindow);
  if (!xev.subwindow)
    xev.subwindow = xev.window;
  success &= xev.same_screen;
  if (x >= 0 && y >= 0)
    success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
  success &= 0 != XSendEvent (xev.display, xev.window, True, key_pressrelease == GDK_KEY_PRESS ? KeyPressMask : KeyReleaseMask, (XEvent*) &xev);
  XSync (xev.display, False);
  success &= 0 == gdk_error_trap_pop();
  return success;
}

/**
 * gdk_test_simulate_button
Matthias Clasen's avatar
Matthias Clasen committed
169
 * @window: a #GdkWindow to simulate a button event for.
170
171
172
173
174
175
 * @x:      x coordinate within @window for the button event.
 * @y:      y coordinate within @window for the button event.
 * @button: Number of the pointer button for the event, usually 1, 2 or 3.
 * @modifiers: Keyboard modifiers the event is setup with.
 * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
 *
Matthias Clasen's avatar
Matthias Clasen committed
176
 * This function is intended to be used in GTK+ test programs.
177
178
179
180
181
182
 * It will warp the mouse pointer to the given (@x,@y) corrdinates
 * within @window and simulate a button press or release event.
 * Because the mouse pointer needs to be warped to the target
 * location, use of this function outside of test programs that
 * run in their own virtual windowing system (e.g. Xvfb) is not
 * recommended.
Matthias Clasen's avatar
Matthias Clasen committed
183
 *
184
185
186
187
188
 * Also, gtk_test_simulate_button() is a fairly low level function,
 * for most testing purposes, gtk_test_widget_click() is the right
 * function to call which will generate a button press event followed
 * by its accompanying button release event.
 *
189
 * Returns: whether all actions neccessary for a button event simulation 
Matthias Clasen's avatar
Matthias Clasen committed
190
191
192
 *     were carried out successfully.
 *
 * Since: 2.14
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
 **/
gboolean
gdk_test_simulate_button (GdkWindow      *window,
                          gint            x,
                          gint            y,
                          guint           button, /*1..3*/
                          GdkModifierType modifiers,
                          GdkEventType    button_pressrelease)
{
  GdkScreen *screen;
  XButtonEvent xev = {
    0,  /* type */
    0,  /* serial */
    1,  /* send_event */
  };
208
  gboolean success;
209
  GdkWindowObject *priv;
210

211
212
  g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS || button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
  g_return_val_if_fail (window != NULL, FALSE);
213

214
215
  if (!GDK_WINDOW_IS_MAPPED (window))
    return FALSE;
216

217
  screen = gdk_window_get_screen (window);
218
219
  priv = (GdkWindowObject *)window;

220
221
  if (x < 0 && y < 0)
    {
222
223
      x = priv->width / 2;
      y = priv->height / 2;
224
    }
225
226
227
228
229

  /* Convert to impl coordinates */
  x = x + priv->abs_x;
  y = y + priv->abs_y;

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
  xev.type = button_pressrelease == GDK_BUTTON_PRESS ? ButtonPress : ButtonRelease;
  xev.display = GDK_DRAWABLE_XDISPLAY (window);
  xev.window = GDK_WINDOW_XID (window);
  xev.root = RootWindow (xev.display, GDK_SCREEN_XNUMBER (screen));
  xev.subwindow = 0;
  xev.time = 0;
  xev.x = x;
  xev.y = y;
  xev.x_root = 0;
  xev.y_root = 0;
  xev.state = modifiers;
  xev.button = button;
  gdk_error_trap_push ();
  xev.same_screen = XTranslateCoordinates (xev.display, xev.window, xev.root,
                                           xev.x, xev.y, &xev.x_root, &xev.y_root,
                                           &xev.subwindow);
  if (!xev.subwindow)
    xev.subwindow = xev.window;
248
  success = xev.same_screen;
249
250
251
252
253
254
  success &= 0 != XWarpPointer (xev.display, None, xev.window, 0, 0, 0, 0, xev.x, xev.y);
  success &= 0 != XSendEvent (xev.display, xev.window, True, button_pressrelease == GDK_BUTTON_PRESS ? ButtonPressMask : ButtonReleaseMask, (XEvent*) &xev);
  XSync (xev.display, False);
  success &= 0 == gdk_error_trap_pop();
  return success;
}