Debian Codegraph @

gdk/win32/gdkdevice-winpointer.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2020 the GTK team
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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "config.h"
19
20 #include <gdk/gdkwindow.h>
21
22 #include <windows.h>
23
24 #include "gdkwin32.h"
25 #include "gdkdevice-winpointer.h"
26
27 G_DEFINE_TYPE (GdkDeviceWinpointer, gdk_device_winpointer, GDK_TYPE_DEVICE)
28
29 static GdkModifierType
30 get_keyboard_mask (void)
31 {
32 GdkModifierType mask;
33 BYTE kbd[256];
34
35 GetKeyboardState (kbd);
36 mask = 0;
37 if (kbd[VK_SHIFT] & 0x80)
38 mask |= GDK_SHIFT_MASK;
39 if (kbd[VK_CAPITAL] & 0x80)
40 mask |= GDK_LOCK_MASK;
41 if (kbd[VK_CONTROL] & 0x80)
42 mask |= GDK_CONTROL_MASK;
43 if (kbd[VK_MENU] & 0x80)
44 mask |= GDK_MOD1_MASK;
45
46 return mask;
47 }
48
49 static gboolean
50 gdk_device_winpointer_get_history (GdkDevice *device,
51 GdkWindow *window,
52 guint32 start,
53 guint32 stop,
54 GdkTimeCoord ***events,
55 gint *n_events)
56 {
57 return FALSE;
58 }
59
60 static void
61 gdk_device_winpointer_get_state (GdkDevice *device,
62 GdkWindow *window,
63 gdouble *axes,
64 GdkModifierType *mask)
65 {
66 GdkDeviceWinpointer *device_winpointer = GDK_DEVICE_WINPOINTER (device);
67
68 if (mask)
69 {
70 *mask = get_keyboard_mask ();
71 *mask |= device_winpointer->last_button_mask;
72 }
73
74 if (axes)
75 {
76 gsize size = sizeof (double) * device_winpointer->num_axes;
77 memcpy (axes, device_winpointer->last_axis_data, size);
78 }
79 }
80
81 static void
82 gdk_device_winpointer_set_window_cursor (GdkDevice *device,
83 GdkWindow *window,
84 GdkCursor *cursor)
85 {
86 }
87
88 static void
89 gdk_device_winpointer_warp (GdkDevice *device,
90 GdkScreen *screen,
91 gdouble x,
92 gdouble y)
93 {
94 }
95
96 static void
97 gdk_device_winpointer_query_state (GdkDevice *device,
98 GdkWindow *window,
99 GdkWindow **root_window,
100 GdkWindow **child_window,
101 gdouble *root_x,
102 gdouble *root_y,
103 gdouble *win_x,
104 gdouble *win_y,
105 GdkModifierType *mask)
106 {
107 GdkDeviceWinpointer *device_winpointer = GDK_DEVICE_WINPOINTER (device);
108 GdkWindowImplWin32 *impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
109 GdkScreen *screen = gdk_window_get_screen (window);
110 HWND hwnd = GDK_WINDOW_HWND (window);
111 HWND hwndc = NULL;
112 POINT point;
113
114 _gdk_win32_get_cursor_pos (&point);
115
116 if (root_x)
117 *root_x = (point.x + _gdk_offset_x) / impl->window_scale;
118
119 if (root_y)
120 *root_y = (point.y + _gdk_offset_y) / impl->window_scale;
121
122 if (window == gdk_screen_get_root_window (screen))
123 {
124 if (win_x)
125 *win_x = (point.x + _gdk_offset_x) / impl->window_scale;
126
127 if (win_y)
128 *win_y = (point.y + _gdk_offset_y) / impl->window_scale;
129 }
130 else
131 {
132 ScreenToClient (hwnd, &point);
133
134 if (win_x)
135 *win_x = point.x / impl->window_scale;
136
137 if (win_y)
138 *win_y = point.y / impl->window_scale;
139 }
140
141 if (child_window)
142 {
143 if (window == gdk_screen_get_root_window (screen))
144 {
145 /* Always use WindowFromPoint when searching from the root window.
146 * Only WindowFromPoint is able to look through transparent
147 * layered windows.
148 */
149 hwndc = GetAncestor (WindowFromPoint (point), GA_ROOT);
150 }
151 else
152 {
153 hwndc = ChildWindowFromPoint (hwnd, point);
154 }
155
156 if (hwndc && hwndc != hwnd)
157 *child_window = gdk_win32_handle_table_lookup (hwndc);
158 else
159 *child_window = NULL; /* Direct child unknown to gdk */
160 }
161
162 if (root_window)
163 *root_window = gdk_screen_get_root_window (screen);
164
165 if (mask)
166 {
167 *mask = get_keyboard_mask ();
168 *mask |= device_winpointer->last_button_mask;
169 }
170 }
171
172 static GdkGrabStatus
173 gdk_device_winpointer_grab (GdkDevice *device,
174 GdkWindow *window,
175 gboolean owner_events,
176 GdkEventMask event_mask,
177 GdkWindow *confine_to,
178 GdkCursor *cursor,
179 guint32 time_)
180 {
181 return GDK_GRAB_SUCCESS;
182 }
183
184 static void
185 gdk_device_winpointer_ungrab (GdkDevice *device,
186 guint32 time_)
187 {
188 }
189
190 static void
191 screen_to_client (HWND hwnd, POINT screen_pt, POINT *client_pt)
192 {
193 *client_pt = screen_pt;
194 ScreenToClient (hwnd, client_pt);
195 }
196
197 GdkWindow *
198 _gdk_device_winpointer_window_at_position (GdkDevice *device,
199 gdouble *win_x,
200 gdouble *win_y,
201 GdkModifierType *mask,
202 gboolean get_toplevel)
203 {
204 GdkDeviceWinpointer *device_winpointer = GDK_DEVICE_WINPOINTER (device);
205 GdkWindow *window = NULL;
206 GdkWindowImplWin32 *impl = NULL;
207 POINT screen_pt, client_pt;
208 HWND hwnd;
209 RECT rect;
210
211 if (!_gdk_win32_get_cursor_pos (&screen_pt))
212 return NULL;
213
214 hwnd = WindowFromPoint (screen_pt);
215
216 if (get_toplevel)
217 {
218 /* Use WindowFromPoint instead of ChildWindowFromPoint(Ex).
219 * Only WindowFromPoint is able to look through transparent
220 * layered windows.
221 */
222 hwnd = GetAncestor (hwnd, GA_ROOT);
223 }
224
225 /* Verify that we're really inside the client area of the window */
226 GetClientRect (hwnd, &rect);
227 screen_to_client (hwnd, screen_pt, &client_pt);
228 if (!PtInRect (&rect, client_pt))
229 hwnd = NULL;
230
231 if (!get_toplevel && hwnd == NULL)
232 {
233 /* If we didn't hit any window, return the root window */
234 /* note that the root window ain't a toplevel window */
235 window = gdk_get_default_root_window ();
236 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
237
238 if (win_x)
239 *win_x = (screen_pt.x + _gdk_offset_x) / impl->window_scale;
240 if (win_y)
241 *win_y = (screen_pt.y + _gdk_offset_y) / impl->window_scale;
242
243 return window;
244 }
245
246 window = gdk_win32_handle_table_lookup (hwnd);
247
248 if (window && (win_x || win_y))
249 {
250 impl = GDK_WINDOW_IMPL_WIN32 (window->impl);
251
252 if (win_x)
253 *win_x = client_pt.x / impl->window_scale;
254 if (win_y)
255 *win_y = client_pt.y / impl->window_scale;
256 }
257
258 if (mask)
259 {
260 *mask = get_keyboard_mask ();
261 *mask |= device_winpointer->last_button_mask;
262 }
263
264 return window;
265 }
266
267 static void
268 gdk_device_winpointer_select_window_events (GdkDevice *device,
269 GdkWindow *window,
270 GdkEventMask event_mask)
271 {
272 }
273
274 static void
275 gdk_device_winpointer_init (GdkDeviceWinpointer *device_winpointer)
276 {
277 device_winpointer->device_handle = NULL;
278 device_winpointer->start_cursor_id = 0;
279 device_winpointer->end_cursor_id = 0;
280
281 device_winpointer->origin_x = 0;
282 device_winpointer->origin_y = 0;
283 device_winpointer->scale_x = 0.0;
284 device_winpointer->scale_y = 0.0;
285
286 device_winpointer->last_axis_data = NULL;
287 device_winpointer->num_axes = 0;
288 device_winpointer->last_button_mask = 0;
289 }
290
291 static void
292 gdk_device_winpointer_finalize (GObject *object)
293 {
294 GdkDeviceWinpointer *device_winpointer = GDK_DEVICE_WINPOINTER (object);
295
296 g_free (device_winpointer->last_axis_data);
297
298 G_OBJECT_CLASS (gdk_device_winpointer_parent_class)->finalize (object);
299 }
300
301 static void
302 gdk_device_winpointer_class_init (GdkDeviceWinpointerClass *klass)
303 {
304 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
305 GObjectClass *object_class = G_OBJECT_CLASS (klass);
306
307 object_class->finalize = gdk_device_winpointer_finalize;
308 device_class->get_history = gdk_device_winpointer_get_history;
309 device_class->get_state = gdk_device_winpointer_get_state;
310 device_class->set_window_cursor = gdk_device_winpointer_set_window_cursor;
311 device_class->warp = gdk_device_winpointer_warp;
312 device_class->query_state = gdk_device_winpointer_query_state;
313 device_class->grab = gdk_device_winpointer_grab;
314 device_class->ungrab = gdk_device_winpointer_ungrab;
315 device_class->window_at_position = _gdk_device_winpointer_window_at_position;
316 device_class->select_window_events = gdk_device_winpointer_select_window_events;
317 }