]> Pileus Git - ~andy/gtk/blob - gdk/directfb/gdktestutils-directfb.c
container: suggest parentheses around assignment used as truth value
[~andy/gtk] / gdk / directfb / gdktestutils-directfb.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
21 /*
22  * GTK+ DirectFB backend
23  * Copyright (C) 2001-2002  convergence integrated media GmbH
24  * Copyright (C) 2002-2004  convergence GmbH 
25  * Written by Denis Oliver Kropp <dok@convergence.de> and
26  *            Sven Neumann <sven@convergence.de>
27  */
28 #include "config.h"
29
30 #include <unistd.h>
31
32 #include "gdk.h"
33 #include "gdkdirectfb.h"
34 #include "gdkprivate-directfb.h"
35
36 #include <gdk/gdktestutils.h>
37 #include <gdk/gdkkeysyms.h>
38
39
40 static DFBInputDeviceKeySymbol
41 _gdk_keyval_to_directfb (guint keyval)
42 {
43   switch (keyval) {
44     case 0 ... 127:
45       return DFB_KEY( UNICODE, keyval );
46     case GDK_F1 ... GDK_F12:
47       return keyval - GDK_F1 + DIKS_F1;
48     case GDK_BackSpace:
49       return DIKS_BACKSPACE;
50     case GDK_Tab:
51       return DIKS_TAB;
52     case GDK_Return:
53       return DIKS_RETURN;
54     case GDK_Escape:
55       return DIKS_ESCAPE;
56     case GDK_Delete:
57       return DIKS_DELETE; 
58     case GDK_Left:
59       return DIKS_CURSOR_LEFT;
60     case GDK_Up:
61       return DIKS_CURSOR_UP;
62     case GDK_Right:
63       return DIKS_CURSOR_RIGHT;
64     case GDK_Down:
65       return DIKS_CURSOR_DOWN;
66     case GDK_Insert:
67       return DIKS_INSERT;
68     case GDK_Home:
69       return DIKS_HOME;
70     case GDK_End:
71       return DIKS_END;
72     case GDK_Page_Up:
73       return DIKS_PAGE_UP;
74     case GDK_Page_Down:
75       return DIKS_PAGE_DOWN;
76     case GDK_Print:
77       return DIKS_PRINT;
78     case GDK_Pause:
79       return DIKS_PAUSE;
80     case GDK_Clear:
81       return DIKS_CLEAR;
82     case GDK_Cancel:
83       return DIKS_CANCEL;
84       /* TODO: handle them all */
85     default:
86       break;
87   }
88
89   return DIKS_NULL;
90 }
91
92 static DFBInputDeviceModifierMask
93 _gdk_modifiers_to_directfb (GdkModifierType modifiers)
94 {
95   DFBInputDeviceModifierMask dfb_modifiers = 0;
96   
97   if (modifiers & GDK_MOD1_MASK)
98     dfb_modifiers |= DIMM_ALT;
99   if (modifiers & GDK_MOD2_MASK)
100     dfb_modifiers |= DIMM_ALTGR;
101   if (modifiers & GDK_CONTROL_MASK)
102     dfb_modifiers |= DIMM_CONTROL;
103   if (modifiers & GDK_SHIFT_MASK)
104     dfb_modifiers |= DIMM_SHIFT;
105
106   return dfb_modifiers;
107 }
108
109 /**
110  * gdk_test_render_sync
111  * @window: a mapped GdkWindow
112  *
113  * This function retrives a pixel from @window to force the windowing
114  * system to carry out any pending rendering commands.
115  * This function is intended to be used to syncronize with rendering
116  * pipelines, to benchmark windowing system rendering operations.
117  **/
118 void
119 gdk_test_render_sync (GdkWindow *window)
120 {
121   _gdk_display->directfb->WaitIdle (_gdk_display->directfb);    
122 }
123
124 /**
125  * gdk_test_simulate_key
126  * @window: Gdk window to simulate a key event for.
127  * @x:      x coordinate within @window for the key event.
128  * @y:      y coordinate within @window for the key event.
129  * @keyval: A Gdk keyboard value.
130  * @modifiers: Keyboard modifiers the event is setup with.
131  * @key_pressrelease: either %GDK_KEY_PRESS or %GDK_KEY_RELEASE
132  *
133  * This function is intended to be used in Gtk+ test programs.
134  * If (@x,@y) are > (-1,-1), it will warp the mouse pointer to
135  * the given (@x,@y) corrdinates within @window and simulate a
136  * key press or release event.
137  * When the mouse pointer is warped to the target location, use
138  * of this function outside of test programs that run in their
139  * own virtual windowing system (e.g. Xvfb) is not recommended.
140  * If (@x,@y) are passed as (-1,-1), the mouse pointer will not
141  * be warped and @window origin will be used as mouse pointer
142  * location for the event.
143  * Also, gtk_test_simulate_key() is a fairly low level function,
144  * for most testing purposes, gtk_test_widget_send_key() is the
145  * right function to call which will generate a key press event
146  * followed by its accompanying key release event.
147  *
148  * Returns: wether all actions neccessary for a key event simulation were carried out successfully.
149  **/
150 gboolean
151 gdk_test_simulate_key (GdkWindow      *window,
152                        gint            x,
153                        gint            y,
154                        guint           keyval,
155                        GdkModifierType modifiers,
156                        GdkEventType    key_pressrelease)
157 {
158   GdkWindowObject       *private;
159   GdkWindowImplDirectFB *impl;
160   DFBWindowEvent         evt; 
161
162   g_return_val_if_fail (GDK_IS_WINDOW(window), FALSE);
163   g_return_val_if_fail (key_pressrelease == GDK_KEY_PRESS ||
164                         key_pressrelease == GDK_KEY_RELEASE, FALSE);
165   
166   private = GDK_WINDOW_OBJECT (window);
167   impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);
168
169   if (x >= 0 && y >= 0) {
170     int win_x, win_y;
171     impl->window->GetPosition (impl->window, &win_x, &win_y);
172     if (_gdk_display->layer->WarpCursor (_gdk_display->layer, win_x+x, win_y+y))
173       return FALSE;
174   }
175  
176   evt.clazz      = DFEC_WINDOW;
177   evt.type       = (key_pressrelease == GDK_KEY_PRESS) ? DWET_KEYDOWN : DWET_KEYUP;
178 #if ((DIRECTFB_MAJOR_VERSION > 1) || (DIRECTFB_MINOR_VERSION >= 2))
179   evt.flags      = DWEF_NONE;
180 #endif
181   evt.window_id  = impl->dfb_id;
182   evt.x          = MAX(x, 0);
183   evt.y          = MAX(y, 0);
184   _gdk_display->layer->GetCursorPosition (_gdk_display->layer, &evt.cx, &evt.cy);
185   evt.key_code   = -1;
186   evt.key_symbol = _gdk_keyval_to_directfb (keyval);
187   evt.modifiers  = _gdk_modifiers_to_directfb (modifiers);
188   evt.locks      = (modifiers & GDK_LOCK_MASK) ? DILS_CAPS : 0;
189   gettimeofday (&evt.timestamp, NULL);
190
191   _gdk_display->buffer->PostEvent (_gdk_display->buffer, DFB_EVENT(&evt));
192
193   return TRUE;
194 }
195
196 /**
197  * gdk_test_simulate_button
198  * @window: Gdk window to simulate a button event for.
199  * @x:      x coordinate within @window for the button event.
200  * @y:      y coordinate within @window for the button event.
201  * @button: Number of the pointer button for the event, usually 1, 2 or 3.
202  * @modifiers: Keyboard modifiers the event is setup with.
203  * @button_pressrelease: either %GDK_BUTTON_PRESS or %GDK_BUTTON_RELEASE
204  *
205  * This function is intended to be used in Gtk+ test programs.
206  * It will warp the mouse pointer to the given (@x,@y) corrdinates
207  * within @window and simulate a button press or release event.
208  * Because the mouse pointer needs to be warped to the target
209  * location, use of this function outside of test programs that
210  * run in their own virtual windowing system (e.g. Xvfb) is not
211  * recommended.
212  * Also, gtk_test_simulate_button() is a fairly low level function,
213  * for most testing purposes, gtk_test_widget_click() is the right
214  * function to call which will generate a button press event followed
215  * by its accompanying button release event.
216  *
217  * Returns: wether all actions neccessary for a button event simulation were carried out successfully.
218  **/
219 gboolean
220 gdk_test_simulate_button (GdkWindow      *window,
221                           gint            x,
222                           gint            y,
223                           guint           button, /*1..3*/
224                           GdkModifierType modifiers,
225                           GdkEventType    button_pressrelease)
226 {
227   GdkWindowObject       *private;
228   GdkWindowImplDirectFB *impl;
229   DFBWindowEvent         evt;  
230   
231   g_return_val_if_fail (GDK_IS_WINDOW(window), FALSE);
232   g_return_val_if_fail (button_pressrelease == GDK_BUTTON_PRESS ||
233                         button_pressrelease == GDK_BUTTON_RELEASE, FALSE);
234   
235   private = GDK_WINDOW_OBJECT (window);
236   impl = GDK_WINDOW_IMPL_DIRECTFB (private->impl);
237
238   if (x >= 0 && y >= 0) {
239     int win_x, win_y;
240     impl->window->GetPosition (impl->window, &win_x, &win_y);
241     if (_gdk_display->layer->WarpCursor (_gdk_display->layer, win_x+x, win_y+y))
242       return FALSE;
243   }
244
245   evt.clazz      = DFEC_WINDOW;
246   evt.type       = (button_pressrelease == GDK_BUTTON_PRESS) ? DWET_BUTTONDOWN : DWET_BUTTONUP;
247 #if ((DIRECTFB_MAJOR_VERSION > 1) || (DIRECTFB_MINOR_VERSION >= 2))
248   evt.flags      = DWEF_NONE;
249 #endif
250   evt.window_id  = impl->dfb_id;
251   evt.x          = MAX(x, 0);
252   evt.y          = MAX(y, 0); 
253   _gdk_display->layer->GetCursorPosition (_gdk_display->layer, &evt.cx, &evt.cy);
254   evt.modifiers  = _gdk_modifiers_to_directfb (modifiers);
255   evt.locks      = (modifiers & GDK_LOCK_MASK) ? DILS_CAPS : 0;
256   evt.button     = button;
257   evt.buttons    = 0;
258   gettimeofday (&evt.timestamp, NULL);
259
260   _gdk_display->buffer->PostEvent (_gdk_display->buffer, DFB_EVENT(&evt));
261
262   return TRUE;
263 }