FreeRDP
Loading...
Searching...
No Matches
xf_event.c
1
21#include <freerdp/config.h>
22
23#include <X11/Xlib.h>
24#include <X11/Xutil.h>
25
26#include <string.h>
27#include <math.h>
28
29#include <winpr/assert.h>
30#include <winpr/path.h>
31
32#include <freerdp/log.h>
33#include <freerdp/locale/keyboard.h>
34
35#include "xf_rail.h"
36#include "xf_window.h"
37#include "xf_cliprdr.h"
38#include "xf_disp.h"
39#include "xf_input.h"
40#include "xf_gfx.h"
41#include "xf_graphics.h"
42#include "xf_utils.h"
43
44#include "xf_debug.h"
45#include "xf_event.h"
46
47#define CLAMP_COORDINATES(x, y) \
48 do \
49 { \
50 if ((x) < 0) \
51 (x) = 0; \
52 if ((y) < 0) \
53 (y) = 0; \
54 } while (0)
55
56static const DWORD mouseLogLevel = WLOG_TRACE;
57
58const char* x11_event_string(int event)
59{
60 switch (event)
61 {
62 case KeyPress:
63 return "KeyPress";
64
65 case KeyRelease:
66 return "KeyRelease";
67
68 case ButtonPress:
69 return "ButtonPress";
70
71 case ButtonRelease:
72 return "ButtonRelease";
73
74 case MotionNotify:
75 return "MotionNotify";
76
77 case EnterNotify:
78 return "EnterNotify";
79
80 case LeaveNotify:
81 return "LeaveNotify";
82
83 case FocusIn:
84 return "FocusIn";
85
86 case FocusOut:
87 return "FocusOut";
88
89 case KeymapNotify:
90 return "KeymapNotify";
91
92 case Expose:
93 return "Expose";
94
95 case GraphicsExpose:
96 return "GraphicsExpose";
97
98 case NoExpose:
99 return "NoExpose";
100
101 case VisibilityNotify:
102 return "VisibilityNotify";
103
104 case CreateNotify:
105 return "CreateNotify";
106
107 case DestroyNotify:
108 return "DestroyNotify";
109
110 case UnmapNotify:
111 return "UnmapNotify";
112
113 case MapNotify:
114 return "MapNotify";
115
116 case MapRequest:
117 return "MapRequest";
118
119 case ReparentNotify:
120 return "ReparentNotify";
121
122 case ConfigureNotify:
123 return "ConfigureNotify";
124
125 case ConfigureRequest:
126 return "ConfigureRequest";
127
128 case GravityNotify:
129 return "GravityNotify";
130
131 case ResizeRequest:
132 return "ResizeRequest";
133
134 case CirculateNotify:
135 return "CirculateNotify";
136
137 case CirculateRequest:
138 return "CirculateRequest";
139
140 case PropertyNotify:
141 return "PropertyNotify";
142
143 case SelectionClear:
144 return "SelectionClear";
145
146 case SelectionRequest:
147 return "SelectionRequest";
148
149 case SelectionNotify:
150 return "SelectionNotify";
151
152 case ColormapNotify:
153 return "ColormapNotify";
154
155 case ClientMessage:
156 return "ClientMessage";
157
158 case MappingNotify:
159 return "MappingNotify";
160
161 case GenericEvent:
162 return "GenericEvent";
163
164 default:
165 return "UNKNOWN";
166 }
167}
168
169static BOOL xf_action_script_append(xfContext* xfc, const char* buffer, size_t size,
170 WINPR_ATTR_UNUSED void* user, const char* what, const char* arg)
171{
172 WINPR_ASSERT(xfc);
173 WINPR_UNUSED(what);
174 WINPR_UNUSED(arg);
175
176 if (buffer || (size == 0))
177 return TRUE;
178
179 if (!ArrayList_Append(xfc->xevents, buffer))
180 {
181 ArrayList_Clear(xfc->xevents);
182 return FALSE;
183 }
184 return TRUE;
185}
186
187BOOL xf_event_action_script_init(xfContext* xfc)
188{
189 WINPR_ASSERT(xfc);
190
191 xf_event_action_script_free(xfc);
192
193 char* val = getConfigOption(TRUE, "isActionScriptAllowed");
194
195 /* We default to enabled if there is no global config file. */
196 xfc->isActionScriptAllowed = !val || (_stricmp(val, "true") == 0);
197 free(val);
198
199 if (!xfc->isActionScriptAllowed)
200 return TRUE;
201
202 xfc->xevents = ArrayList_New(TRUE);
203
204 if (!xfc->xevents)
205 return FALSE;
206
207 wObject* obj = ArrayList_Object(xfc->xevents);
208 WINPR_ASSERT(obj);
209 obj->fnObjectNew = winpr_ObjectStringClone;
210 obj->fnObjectFree = winpr_ObjectStringFree;
211
212 return run_action_script(xfc, "xevent", nullptr, xf_action_script_append, nullptr);
213}
214
215void xf_event_action_script_free(xfContext* xfc)
216{
217 if (xfc->xevents)
218 {
219 ArrayList_Free(xfc->xevents);
220 xfc->xevents = nullptr;
221 }
222}
223
224static BOOL action_script_run(xfContext* xfc, const char* buffer, size_t size, void* user,
225 const char* what, const char* arg)
226{
227 WINPR_UNUSED(xfc);
228 if (!xfc->isActionScriptAllowed)
229 return TRUE;
230
231 WINPR_UNUSED(what);
232 WINPR_UNUSED(arg);
233 WINPR_ASSERT(user);
234 int* pstatus = user;
235
236 if (size == 0)
237 {
238 WLog_Print(xfc->log, WLOG_WARN, "ActionScript xevent: script did not return data");
239 return FALSE;
240 }
241
242 if (winpr_PathFileExists(buffer))
243 {
244 char* cmd = nullptr;
245 size_t cmdlen = 0;
246 winpr_asprintf(&cmd, &cmdlen, "%s %s %s", buffer, what, arg);
247 if (!cmd)
248 return FALSE;
249
250 // NOLINTNEXTLINE(bugprone-command-processor)
251 FILE* fp = popen(cmd, "w");
252 free(cmd);
253 if (!fp)
254 {
255 WLog_Print(xfc->log, WLOG_ERROR, "Failed to execute '%s'", buffer);
256 return FALSE;
257 }
258
259 *pstatus = pclose(fp);
260 if (*pstatus < 0)
261 {
262 WLog_Print(xfc->log, WLOG_ERROR, "Command '%s' returned %d", buffer, *pstatus);
263 return FALSE;
264 }
265 }
266 else
267 {
268 WLog_Print(xfc->log, WLOG_WARN, "ActionScript xevent: No such file '%s'", buffer);
269 return FALSE;
270 }
271
272 return TRUE;
273}
274
275static BOOL xf_event_execute_action_script(xfContext* xfc, const XEvent* event)
276{
277 size_t count = 0;
278 char* name = nullptr;
279 BOOL match = FALSE;
280 const char* xeventName = nullptr;
281
282 if (!xfc->actionScriptExists || !xfc->xevents || !xfc->window)
283 return FALSE;
284
285 if (event->type > LASTEvent)
286 return FALSE;
287
288 xeventName = x11_event_string(event->type);
289 count = ArrayList_Count(xfc->xevents);
290
291 for (size_t index = 0; index < count; index++)
292 {
293 name = (char*)ArrayList_GetItem(xfc->xevents, index);
294
295 if (_stricmp(name, xeventName) == 0)
296 {
297 match = TRUE;
298 break;
299 }
300 }
301
302 if (!match)
303 return FALSE;
304
305 char command[2048] = WINPR_C_ARRAY_INIT;
306 char arg[2048] = WINPR_C_ARRAY_INIT;
307 (void)_snprintf(command, sizeof(command), "xevent %s", xeventName);
308 (void)_snprintf(arg, sizeof(arg), "%lu", (unsigned long)xfc->window->handle);
309 return run_action_script(xfc, command, arg, action_script_run, nullptr);
310}
311
312void xf_adjust_coordinates_to_screen(xfContext* xfc, UINT32* x, UINT32* y)
313{
314 if (!xfc || !xfc->common.context.settings || !y || !x)
315 return;
316
317 rdpSettings* settings = xfc->common.context.settings;
318 INT64 tx = *x;
319 INT64 ty = *y;
320 if (!xfc->remote_app)
321 {
322#ifdef WITH_XRENDER
323
324 if (xf_picture_transform_required(xfc))
325 {
326 const double dw = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth);
327 const double dh = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight);
328 double xScalingFactor = xfc->scaledWidth / dw;
329 double yScalingFactor = xfc->scaledHeight / dh;
330 tx = (INT64)lround((1.0 * (*x) + xfc->offset_x) * xScalingFactor);
331 ty = (INT64)lround((1.0 * (*y) + xfc->offset_y) * yScalingFactor);
332 }
333
334#endif
335 }
336
337 CLAMP_COORDINATES(tx, ty);
338 *x = (UINT32)tx;
339 *y = (UINT32)ty;
340}
341
342void xf_event_adjust_coordinates(xfContext* xfc, int* x, int* y)
343{
344 if (!xfc || !xfc->common.context.settings || !y || !x)
345 return;
346
347 if (!xfc->remote_app)
348 {
349#ifdef WITH_XRENDER
350 rdpSettings* settings = xfc->common.context.settings;
351 if (xf_picture_transform_required(xfc))
352 {
353 double xScalingFactor = freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth) /
354 (double)xfc->scaledWidth;
355 double yScalingFactor = freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight) /
356 (double)xfc->scaledHeight;
357 *x = (int)((*x - xfc->offset_x) * xScalingFactor);
358 *y = (int)((*y - xfc->offset_y) * yScalingFactor);
359 }
360
361#endif
362 }
363
364 CLAMP_COORDINATES(*x, *y);
365}
366
367static BOOL xf_event_Expose(xfContext* xfc, const XExposeEvent* event, BOOL app)
368{
369 WINPR_ASSERT(xfc);
370 WINPR_ASSERT(event);
371
372 rdpSettings* settings = xfc->common.context.settings;
373 WINPR_ASSERT(settings);
374
375 if (!app && (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
376 freerdp_settings_get_bool(settings, FreeRDP_MultiTouchGestures)))
377 {
378 xfc->exposedArea.x = 0;
379 xfc->exposedArea.y = 0;
380 xfc->exposedArea.w = WINPR_ASSERTING_INT_CAST(
381 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
382 xfc->exposedArea.h = WINPR_ASSERTING_INT_CAST(
383 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
384 }
385 else
386 {
387 xfc->exposedArea.x = event->x;
388 xfc->exposedArea.y = event->y;
389 xfc->exposedArea.w = event->width;
390 xfc->exposedArea.h = event->height;
391 }
392
393 xfc->exposedWindow = event->window;
394 xfc->exposeRequested = true;
395
396 return TRUE;
397}
398
399static BOOL xf_event_VisibilityNotify(xfContext* xfc, const XVisibilityEvent* event, BOOL app)
400{
401 WINPR_UNUSED(app);
402 xfc->unobscured = event->state == VisibilityUnobscured;
403 return TRUE;
404}
405
406BOOL xf_generic_MotionNotify_(xfContext* xfc, int x, int y, Window window, BOOL app,
407 const char* file, const char* fkt, size_t line)
408{
409 Window childWindow = None;
410
411 if (WLog_IsLevelActive(xfc->log, mouseLogLevel))
412 WLog_PrintTextMessage(xfc->log, mouseLogLevel, line, file, fkt,
413 "%s: x=%d, y=%d, window=0x%08lx, app=%d", __func__, x, y, window,
414 app);
415
416 WINPR_ASSERT(xfc);
417 WINPR_ASSERT(xfc->common.context.settings);
418
419 if (app)
420 {
421 /* make sure window exists */
422 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, window);
423 xf_rail_return_window(appWindow, FALSE);
424 if (!appWindow)
425 return TRUE;
426
427 /* Translate to desktop coordinates */
428 XTranslateCoordinates(xfc->display, window, RootWindowOfScreen(xfc->screen), x, y, &x, &y,
429 &childWindow);
430 }
431
432 xf_event_adjust_coordinates(xfc, &x, &y);
433 freerdp_client_send_button_event(&xfc->common, FALSE, PTR_FLAGS_MOVE, x, y);
434
435 if (xfc->fullscreen && !app)
436 {
437 if (xfc->window)
438 XSetInputFocus(xfc->display, xfc->window->handle, RevertToPointerRoot, CurrentTime);
439 }
440
441 return TRUE;
442}
443
444BOOL xf_generic_RawMotionNotify_(xfContext* xfc, int x, int y, WINPR_ATTR_UNUSED Window window,
445 BOOL app, const char* file, const char* fkt, size_t line)
446{
447 WINPR_ASSERT(xfc);
448
449 if (WLog_IsLevelActive(xfc->log, mouseLogLevel))
450 WLog_PrintTextMessage(xfc->log, mouseLogLevel, line, file, fkt,
451 "%s: x=%d, y=%d, window=0x%08lx, app=%d", __func__, x, y, window,
452 app);
453
454 if (app)
455 {
456 WLog_Print(xfc->log, WLOG_ERROR,
457 "Relative mouse input is not supported with remoate app mode!");
458 return FALSE;
459 }
460
461 return freerdp_client_send_button_event(&xfc->common, TRUE, PTR_FLAGS_MOVE, x, y);
462}
463
464static BOOL xf_event_MotionNotify(xfContext* xfc, const XMotionEvent* event, BOOL app)
465{
466 WINPR_ASSERT(xfc);
467
468 if (xfc->window)
469 xf_floatbar_set_root_y(xfc->window->floatbar, event->y);
470
471 if (xfc->xi_event || xfc->xi_rawevent || (xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc)))
472 return TRUE;
473
474 return xf_generic_MotionNotify(xfc, event->x, event->y, event->window, app);
475}
476
477BOOL xf_generic_ButtonEvent_(xfContext* xfc, int x, int y, int button, Window window, BOOL app,
478 BOOL down, const char* file, const char* fkt, size_t line)
479{
480 UINT16 flags = 0;
481 Window childWindow = None;
482
483 if (WLog_IsLevelActive(xfc->log, mouseLogLevel))
484 WLog_PrintTextMessage(xfc->log, mouseLogLevel, line, file, fkt,
485 "%s: x=%d, y=%d, button=%d, window=0x%08lx, app=%d, down=%d",
486 __func__, x, y, button, window, app, down);
487
488 WINPR_ASSERT(xfc);
489 if (button < 0)
490 return FALSE;
491
492 for (size_t i = 0; i < ARRAYSIZE(xfc->button_map); i++)
493 {
494 const button_map* cur = &xfc->button_map[i];
495
496 if (cur->button == (UINT32)button)
497 {
498 flags = cur->flags;
499 break;
500 }
501 }
502
503 if (flags != 0)
504 {
505 if (flags & (PTR_FLAGS_WHEEL | PTR_FLAGS_HWHEEL))
506 {
507 if (down)
508 freerdp_client_send_wheel_event(&xfc->common, flags);
509 }
510 else
511 {
512 BOOL extended = FALSE;
513
514 if (flags & (PTR_XFLAGS_BUTTON1 | PTR_XFLAGS_BUTTON2))
515 {
516 extended = TRUE;
517
518 if (down)
519 flags |= PTR_XFLAGS_DOWN;
520 }
521 else if (flags & (PTR_FLAGS_BUTTON1 | PTR_FLAGS_BUTTON2 | PTR_FLAGS_BUTTON3))
522 {
523 if (down)
524 flags |= PTR_FLAGS_DOWN;
525 }
526
527 if (app)
528 {
529 /* make sure window exists */
530 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, window);
531 xf_rail_return_window(appWindow, FALSE);
532 if (!appWindow)
533 return TRUE;
534
535 /* Translate to desktop coordinates */
536 XTranslateCoordinates(xfc->display, window, RootWindowOfScreen(xfc->screen), x, y,
537 &x, &y, &childWindow);
538 }
539
540 xf_event_adjust_coordinates(xfc, &x, &y);
541
542 if (extended)
543 freerdp_client_send_extended_button_event(&xfc->common, FALSE, flags, x, y);
544 else
545 freerdp_client_send_button_event(&xfc->common, FALSE, flags, x, y);
546 }
547 }
548
549 return TRUE;
550}
551
552static BOOL xf_grab_mouse(xfContext* xfc)
553{
554 WINPR_ASSERT(xfc);
555
556 if (!xfc->window)
557 return FALSE;
558
559 if (freerdp_settings_get_bool(xfc->common.context.settings, FreeRDP_GrabMouse))
560 {
561 XGrabPointer(xfc->display, xfc->window->handle, False,
562 ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask |
563 EnterWindowMask | LeaveWindowMask,
564 GrabModeAsync, GrabModeAsync, xfc->window->handle, None, CurrentTime);
565 xfc->common.mouse_grabbed = TRUE;
566 }
567 return TRUE;
568}
569
570static BOOL xf_grab_kbd(xfContext* xfc)
571{
572 WINPR_ASSERT(xfc);
573
574 if (!xfc->window)
575 return FALSE;
576
577 XGrabKeyboard(xfc->display, xfc->window->handle, TRUE, GrabModeAsync, GrabModeAsync,
578 CurrentTime);
579 return TRUE;
580}
581
582static BOOL xf_event_ButtonPress(xfContext* xfc, const XButtonEvent* event, BOOL app)
583{
584 xf_grab_mouse(xfc);
585
586 if (xfc->xi_event || xfc->xi_rawevent || (xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc)))
587 return TRUE;
588 if (!app && xfc_is_floatbar_window(xfc, event->window))
589 return TRUE;
590 return xf_generic_ButtonEvent(xfc, event->x, event->y,
591 WINPR_ASSERTING_INT_CAST(int, event->button), event->window, app,
592 TRUE);
593}
594
595static BOOL xf_event_ButtonRelease(xfContext* xfc, const XButtonEvent* event, BOOL app)
596{
597 xf_grab_mouse(xfc);
598
599 if (xfc->xi_event || xfc->xi_rawevent || (xfc->common.mouse_grabbed && xf_use_rel_mouse(xfc)))
600 return TRUE;
601 return xf_generic_ButtonEvent(xfc, event->x, event->y,
602 WINPR_ASSERTING_INT_CAST(int, event->button), event->window, app,
603 FALSE);
604}
605
606static BOOL xf_event_KeyPress(xfContext* xfc, const XKeyEvent* event, BOOL app)
607{
608 KeySym keysym = 0;
609 char str[256] = WINPR_C_ARRAY_INIT;
610 union
611 {
612 const XKeyEvent* cev;
613 XKeyEvent* ev;
614 } cnv;
615 cnv.cev = event;
616 WINPR_UNUSED(app);
617 XLookupString(cnv.ev, str, sizeof(str), &keysym, nullptr);
618 xf_keyboard_key_press(xfc, event, keysym);
619 return TRUE;
620}
621
622static BOOL xf_event_KeyRelease(xfContext* xfc, const XKeyEvent* event, BOOL app)
623{
624 KeySym keysym = 0;
625 char str[256] = WINPR_C_ARRAY_INIT;
626 union
627 {
628 const XKeyEvent* cev;
629 XKeyEvent* ev;
630 } cnv;
631 cnv.cev = event;
632
633 WINPR_UNUSED(app);
634 XLookupString(cnv.ev, str, sizeof(str), &keysym, nullptr);
635 xf_keyboard_key_release(xfc, event, keysym);
636 return TRUE;
637}
638
639/* Release a key, but ignore the event in case of autorepeat.
640 */
641static BOOL xf_event_KeyReleaseOrIgnore(xfContext* xfc, const XKeyEvent* event, BOOL app)
642{
643 WINPR_ASSERT(xfc);
644 WINPR_ASSERT(event);
645
646 if ((event->type == KeyRelease) && XEventsQueued(xfc->display, QueuedAfterReading))
647 {
648 XEvent nev = WINPR_C_ARRAY_INIT;
649 XPeekEvent(xfc->display, &nev);
650
651 if ((nev.type == KeyPress) && (nev.xkey.time == event->time) &&
652 (nev.xkey.keycode == event->keycode))
653 {
654 /* Key wasn’t actually released */
655 return TRUE;
656 }
657 }
658
659 return xf_event_KeyRelease(xfc, event, app);
660}
661
662static BOOL xf_event_FocusIn(xfContext* xfc, const XFocusInEvent* event, BOOL app)
663{
664 if (event->mode == NotifyGrab)
665 return TRUE;
666
667 xfc->focused = TRUE;
668
669 if (xfc->mouse_active && !app)
670 {
671 xf_grab_mouse(xfc);
672 if (!xf_grab_kbd(xfc))
673 return FALSE;
674 }
675
676 /* Release all keys, should already be done at FocusOut but might be missed
677 * if the WM decided to use an alternate event order */
678 if (!app)
679 xf_keyboard_release_all_keypress(xfc);
680 else
681 {
682 if (!xf_rail_send_activate(xfc, event->window, TRUE))
683 return FALSE;
684 }
685
686 xf_pointer_update_scale(xfc);
687
688 if (app)
689 {
690 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->window);
691
692 /* Update the server with any window changes that occurred while the window was not focused.
693 */
694 if (appWindow)
695 xf_rail_adjust_position(xfc, appWindow);
696 xf_rail_return_window(appWindow, FALSE);
697 }
698
699 xf_keyboard_focus_in(xfc);
700 return TRUE;
701}
702
703static BOOL xf_event_FocusOut(xfContext* xfc, const XFocusOutEvent* event, BOOL app)
704{
705 if (event->mode == NotifyUngrab)
706 return TRUE;
707
708 xfc->focused = FALSE;
709
710 if (event->mode == NotifyWhileGrabbed)
711 XUngrabKeyboard(xfc->display, CurrentTime);
712
713 xf_keyboard_release_all_keypress(xfc);
714 if (app)
715 return xf_rail_send_activate(xfc, event->window, FALSE);
716
717 return TRUE;
718}
719
720static BOOL xf_event_MappingNotify(xfContext* xfc, const XMappingEvent* event, BOOL app)
721{
722 WINPR_UNUSED(app);
723
724 switch (event->request)
725 {
726 case MappingModifier:
727 return xf_keyboard_update_modifier_map(xfc);
728 case MappingKeyboard:
729 WLog_Print(xfc->log, WLOG_TRACE, "[%d] MappingKeyboard", event->request);
730 return xf_keyboard_init(xfc);
731 case MappingPointer:
732 WLog_Print(xfc->log, WLOG_TRACE, "[%d] MappingPointer", event->request);
733 xf_button_map_init(xfc);
734 return TRUE;
735 default:
736 WLog_Print(xfc->log, WLOG_WARN,
737 "[%d] Unsupported MappingNotify::request, must be one "
738 "of[MappingModifier(%d), MappingKeyboard(%d), MappingPointer(%d)]",
739 event->request, MappingModifier, MappingKeyboard, MappingPointer);
740 return FALSE;
741 }
742}
743
744static BOOL xf_event_ClientMessage(xfContext* xfc, const XClientMessageEvent* event, BOOL app)
745{
746 if ((event->message_type == xfc->WM_PROTOCOLS) &&
747 ((Atom)event->data.l[0] == xfc->WM_DELETE_WINDOW))
748 {
749 if (app)
750 {
751 BOOL rc = TRUE;
752 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->window);
753
754 if (appWindow)
755 rc = xf_rail_send_client_system_command(xfc, appWindow->windowId, SC_CLOSE);
756 xf_rail_return_window(appWindow, FALSE);
757 return rc;
758 }
759 else
760 {
761 WLog_Print(xfc->log, WLOG_TRACE, "Main window closed");
762 return FALSE;
763 }
764 }
765
766 return TRUE;
767}
768
769static BOOL xf_event_EnterNotify(xfContext* xfc, const XEnterWindowEvent* event, BOOL app)
770{
771 if (!app)
772 {
773 if (!xfc->window)
774 return FALSE;
775
776 xfc->mouse_active = TRUE;
777
778 if (xfc->fullscreen)
779 XSetInputFocus(xfc->display, xfc->window->handle, RevertToPointerRoot, CurrentTime);
780
781 if (xfc->focused)
782 xf_grab_kbd(xfc);
783 }
784 else
785 {
786 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->window);
787
788 /* keep track of which window has focus so that we can apply pointer updates */
789 xfc->appWindow = appWindow;
790 xf_rail_return_window(appWindow, FALSE);
791 }
792
793 return TRUE;
794}
795
796static BOOL xf_event_LeaveNotify(xfContext* xfc, const XLeaveWindowEvent* event, BOOL app)
797{
798 if (event->mode == NotifyGrab || event->mode == NotifyUngrab)
799 return TRUE;
800 if (!app)
801 {
802 xfc->mouse_active = FALSE;
803 XUngrabKeyboard(xfc->display, CurrentTime);
804 }
805 else
806 {
807 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->window);
808
809 /* keep track of which window has focus so that we can apply pointer updates */
810 if (xfc->appWindow == appWindow)
811 xfc->appWindow = nullptr;
812 xf_rail_return_window(appWindow, FALSE);
813 }
814 return TRUE;
815}
816
817static BOOL xf_event_ConfigureNotify(xfContext* xfc, const XConfigureEvent* event, BOOL app)
818{
819 Window childWindow = None;
820 xfAppWindow* appWindow = nullptr;
821
822 WINPR_ASSERT(xfc);
823 WINPR_ASSERT(event);
824
825 const rdpSettings* settings = xfc->common.context.settings;
826 WINPR_ASSERT(settings);
827
828 WLog_Print(xfc->log, WLOG_DEBUG, "x=%" PRId32 ", y=%" PRId32 ", w=%" PRId32 ", h=%" PRId32,
829 event->x, event->y, event->width, event->height);
830
831 if (!app)
832 {
833 if (!xfc->window)
834 return FALSE;
835
836 if (xfc->window->left != event->x)
837 xfc->window->left = event->x;
838
839 if (xfc->window->top != event->y)
840 xfc->window->top = event->y;
841
842 if (xfc->window->width != event->width || xfc->window->height != event->height)
843 {
844 xfc->window->width = event->width;
845 xfc->window->height = event->height;
846#ifdef WITH_XRENDER
847 xfc->offset_x = 0;
848 xfc->offset_y = 0;
849
850 if (freerdp_settings_get_bool(settings, FreeRDP_SmartSizing) ||
851 freerdp_settings_get_bool(settings, FreeRDP_MultiTouchGestures))
852 {
853 xfc->scaledWidth = xfc->window->width;
854 xfc->scaledHeight = xfc->window->height;
855 xf_draw_screen(
856 xfc, 0, 0,
857 WINPR_ASSERTING_INT_CAST(
858 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth)),
859 WINPR_ASSERTING_INT_CAST(
860 int32_t, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight)));
861 }
862 else
863 {
864 xfc->scaledWidth = WINPR_ASSERTING_INT_CAST(
865 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopWidth));
866 xfc->scaledHeight = WINPR_ASSERTING_INT_CAST(
867 int, freerdp_settings_get_uint32(settings, FreeRDP_DesktopHeight));
868 }
869
870#endif
871 }
872
873 if (freerdp_settings_get_bool(settings, FreeRDP_DynamicResolutionUpdate))
874 {
875 const int alignedWidth = (xfc->window->width / 2) * 2;
876 const int alignedHeight = (xfc->window->height / 2) * 2;
877 /* ask the server to resize using the display channel */
878 xf_disp_handle_configureNotify(xfc, alignedWidth, alignedHeight);
879 }
880 }
881 else
882 {
883 appWindow = xf_AppWindowFromX11Window(xfc, event->window);
884
885 if (appWindow)
886 {
887 /*
888 * ConfigureNotify coordinates are expressed relative to the window parent.
889 * Translate these to root window coordinates.
890 */
891 XTranslateCoordinates(xfc->display, appWindow->handle, RootWindowOfScreen(xfc->screen),
892 0, 0, &appWindow->x, &appWindow->y, &childWindow);
893 appWindow->width = event->width;
894 appWindow->height = event->height;
895
896 xf_AppWindowResize(xfc, appWindow);
897
898 /*
899 * Additional checks for not in a local move and not ignoring configure to send
900 * position update to server, also should the window not be focused then do not
901 * send to server yet (i.e. resizing using window decoration).
902 * The server will be updated when the window gets refocused.
903 */
904 if (appWindow->decorations)
905 {
906 /* moving resizing using window decoration */
907 xf_rail_adjust_position(xfc, appWindow);
908 }
909 else
910 {
911 if ((!event->send_event || appWindow->local_move.state == LMS_NOT_ACTIVE) &&
912 !appWindow->rail_ignore_configure && xfc->focused)
913 xf_rail_adjust_position(xfc, appWindow);
914 }
915 }
916 xf_rail_return_window(appWindow, FALSE);
917 }
918 return xf_pointer_update_scale(xfc);
919}
920
921static BOOL xf_event_MapNotify(xfContext* xfc, const XMapEvent* event, BOOL app)
922{
923 WINPR_ASSERT(xfc);
924 if (!app)
925 {
926 if (!gdi_send_suppress_output(xfc->common.context.gdi, FALSE))
927 return FALSE;
928 }
929 else
930 {
931 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->window);
932
933 if (appWindow)
934 {
935 /* local restore event */
936 /* This is now handled as part of the PropertyNotify
937 * Doing this here would inhibit the ability to restore a maximized window
938 * that is minimized back to the maximized state
939 */
940 // xf_rail_send_client_system_command(xfc, appWindow->windowId, SC_RESTORE);
941 appWindow->is_mapped = TRUE;
942 }
943 xf_rail_return_window(appWindow, FALSE);
944 }
945
946 return TRUE;
947}
948
949static BOOL xf_event_UnmapNotify(xfContext* xfc, const XUnmapEvent* event, BOOL app)
950{
951 WINPR_ASSERT(xfc);
952 WINPR_ASSERT(event);
953
954 if (!app)
955 xf_keyboard_release_all_keypress(xfc);
956
957 if (!app)
958 return gdi_send_suppress_output(xfc->common.context.gdi, TRUE);
959
960 {
961 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->window);
962
963 if (appWindow)
964 appWindow->is_mapped = FALSE;
965 xf_rail_return_window(appWindow, FALSE);
966 }
967
968 return TRUE;
969}
970
971static BOOL xf_event_PropertyNotify(xfContext* xfc, const XPropertyEvent* event, BOOL app)
972{
973 BOOL rc = TRUE;
974 WINPR_ASSERT(xfc);
975 WINPR_ASSERT(event);
976
977 /*
978 * This section handles sending the appropriate commands to the rail server
979 * when the window has been minimized, maximized, restored locally
980 * ie. not using the buttons on the rail window itself
981 */
982 if (((event->atom == xfc->NET_WM_STATE) && (event->state != PropertyDelete)) ||
983 ((event->atom == xfc->WM_STATE) && (event->state != PropertyDelete)))
984 {
985 BOOL status = FALSE;
986 BOOL minimized = FALSE;
987 BOOL minimizedChanged = FALSE;
988 unsigned long nitems = 0;
989 unsigned long bytes = 0;
990 unsigned char* prop = nullptr;
991 xfAppWindow* appWindow = nullptr;
992
993 if (app)
994 {
995 appWindow = xf_AppWindowFromX11Window(xfc, event->window);
996
997 if (!appWindow)
998 goto fail;
999 }
1000
1001 if (event->atom == xfc->NET_WM_STATE)
1002 {
1003 status = xf_GetWindowProperty(xfc, event->window, xfc->NET_WM_STATE, 12, &nitems,
1004 &bytes, &prop);
1005
1006 if (status)
1007 {
1008 if (appWindow)
1009 {
1010 appWindow->maxVert = FALSE;
1011 appWindow->maxHorz = FALSE;
1012 }
1013 for (unsigned long i = 0; i < nitems; i++)
1014 {
1015 if ((Atom)((UINT16**)prop)[i] ==
1016 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_MAXIMIZED_VERT",
1017 False))
1018 {
1019 if (appWindow)
1020 appWindow->maxVert = TRUE;
1021 }
1022
1023 if ((Atom)((UINT16**)prop)[i] ==
1024 Logging_XInternAtom(xfc->log, xfc->display, "_NET_WM_STATE_MAXIMIZED_HORZ",
1025 False))
1026 {
1027 if (appWindow)
1028 appWindow->maxHorz = TRUE;
1029 }
1030 }
1031
1032 XFree(prop);
1033 }
1034 }
1035
1036 if (event->atom == xfc->WM_STATE)
1037 {
1038 status =
1039 xf_GetWindowProperty(xfc, event->window, xfc->WM_STATE, 1, &nitems, &bytes, &prop);
1040
1041 if (status)
1042 {
1043 /* If the window is in the iconic state */
1044 if (((UINT32)*prop == 3) && !IsGnome())
1045 {
1046 minimized = TRUE;
1047 if (appWindow)
1048 appWindow->minimized = TRUE;
1049 }
1050 else
1051 {
1052 minimized = FALSE;
1053 if (appWindow)
1054 appWindow->minimized = FALSE;
1055 }
1056
1057 minimizedChanged = TRUE;
1058 XFree(prop);
1059 }
1060 }
1061
1062 if (app)
1063 {
1064 WINPR_ASSERT(appWindow);
1065 if (appWindow->maxVert && appWindow->maxHorz && !appWindow->minimized)
1066 {
1067 if (appWindow->rail_state != WINDOW_SHOW_MAXIMIZED)
1068 {
1069 appWindow->rail_state = WINDOW_SHOW_MAXIMIZED;
1070 rc = xf_rail_send_client_system_command(xfc, appWindow->windowId, SC_MAXIMIZE);
1071 }
1072 }
1073 else if (appWindow->minimized)
1074 {
1075 if (appWindow->rail_state != WINDOW_SHOW_MINIMIZED)
1076 {
1077 appWindow->rail_state = WINDOW_SHOW_MINIMIZED;
1078 rc = xf_rail_send_client_system_command(xfc, appWindow->windowId, SC_MINIMIZE);
1079 }
1080 }
1081 else
1082 {
1083 if (appWindow->rail_state != WINDOW_SHOW && appWindow->rail_state != WINDOW_HIDE)
1084 {
1085 appWindow->rail_state = WINDOW_SHOW;
1086 rc = xf_rail_send_client_system_command(xfc, appWindow->windowId, SC_RESTORE);
1087 }
1088 }
1089 }
1090 else if (minimizedChanged)
1091 rc = gdi_send_suppress_output(xfc->common.context.gdi, minimized);
1092
1093 fail:
1094 xf_rail_return_window(appWindow, FALSE);
1095 }
1096
1097 return rc;
1098}
1099
1100static BOOL xf_event_suppress_events(xfContext* xfc, xfAppWindow* appWindow, const XEvent* event)
1101{
1102 if (!xfc->remote_app)
1103 return FALSE;
1104
1105 switch (appWindow->local_move.state)
1106 {
1107 case LMS_NOT_ACTIVE:
1108
1109 /* No local move in progress, nothing to do */
1110
1111 /* Prevent Configure from happening during indeterminant state of Horz or Vert Max only
1112 */
1113 if ((event->type == ConfigureNotify) && appWindow->rail_ignore_configure)
1114 {
1115 appWindow->rail_ignore_configure = FALSE;
1116 return TRUE;
1117 }
1118
1119 break;
1120
1121 case LMS_STARTING:
1122
1123 /* Local move initiated by RDP server, but we have not yet seen any updates from the X
1124 * server */
1125 switch (event->type)
1126 {
1127 case ConfigureNotify:
1128 /* Starting to see move events from the X server. Local move is now in progress.
1129 */
1130 appWindow->local_move.state = LMS_ACTIVE;
1131 /* Allow these events to be processed during move to keep our state up to date.
1132 */
1133 break;
1134
1135 case ButtonPress:
1136 case ButtonRelease:
1137 case KeyPress:
1138 case KeyRelease:
1139 case UnmapNotify:
1140 /*
1141 * A button release event means the X window server did not grab the
1142 * mouse before the user released it. In this case we must cancel the
1143 * local move. The event will be processed below as normal, below.
1144 */
1145 break;
1146
1147 case VisibilityNotify:
1148 case PropertyNotify:
1149 case Expose:
1150 /* Allow these events to pass */
1151 break;
1152
1153 default:
1154 /* Eat any other events */
1155 return TRUE;
1156 }
1157
1158 break;
1159
1160 case LMS_ACTIVE:
1161
1162 /* Local move is in progress */
1163 switch (event->type)
1164 {
1165 case ConfigureNotify:
1166 case VisibilityNotify:
1167 case PropertyNotify:
1168 case Expose:
1169 case GravityNotify:
1170 /* Keep us up to date on position */
1171 break;
1172
1173 default:
1174 /* Any other event terminates move */
1175 xf_rail_end_local_move(xfc, appWindow);
1176 break;
1177 }
1178
1179 break;
1180
1181 case LMS_TERMINATING:
1182 /* Already sent RDP end move to server. Allow events to pass. */
1183 break;
1184 default:
1185 break;
1186 }
1187
1188 return FALSE;
1189}
1190
1191BOOL xf_event_process(freerdp* instance, const XEvent* event)
1192{
1193 BOOL status = TRUE;
1194
1195 WINPR_ASSERT(instance);
1196 WINPR_ASSERT(event);
1197
1198 xfContext* xfc = (xfContext*)instance->context;
1199 WINPR_ASSERT(xfc);
1200
1201 rdpSettings* settings = xfc->common.context.settings;
1202 WINPR_ASSERT(settings);
1203
1204 if (xfc->remote_app)
1205 {
1206 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, event->xany.window);
1207
1208 if (appWindow)
1209 {
1210 /* Update "current" window for cursor change orders */
1211 xfc->appWindow = appWindow;
1212
1213 const BOOL rc = xf_event_suppress_events(xfc, appWindow, event);
1214 xf_rail_return_window(appWindow, FALSE);
1215 if (rc)
1216 return TRUE;
1217 }
1218 }
1219
1220 if (xfc->window)
1221 {
1222 xfFloatbar* floatbar = xfc->window->floatbar;
1223 if (xf_floatbar_check_event(floatbar, event))
1224 {
1225 xf_floatbar_event_process(floatbar, event);
1226 return TRUE;
1227 }
1228
1229 if (xf_floatbar_is_locked(floatbar))
1230 {
1231 /* Filter input events, floatbar is locked do not forward anything to the session */
1232 switch (event->type)
1233 {
1234 case MotionNotify:
1235 case ButtonPress:
1236 case ButtonRelease:
1237 case KeyPress:
1238 case KeyRelease:
1239 case FocusIn:
1240 case FocusOut:
1241 case EnterNotify:
1242 case LeaveNotify:
1243 return TRUE;
1244 default:
1245 break;
1246 }
1247 }
1248 }
1249
1250 xf_event_execute_action_script(xfc, event);
1251
1252 if (event->type != MotionNotify)
1253 {
1254 WLog_Print(xfc->log, WLOG_TRACE, "%s Event(%d): wnd=0x%08lX", x11_event_string(event->type),
1255 event->type, (unsigned long)event->xany.window);
1256 }
1257
1258 switch (event->type)
1259 {
1260 case Expose:
1261 status = xf_event_Expose(xfc, &event->xexpose, xfc->remote_app);
1262 break;
1263
1264 case VisibilityNotify:
1265 status = xf_event_VisibilityNotify(xfc, &event->xvisibility, xfc->remote_app);
1266 break;
1267
1268 case MotionNotify:
1269 status = xf_event_MotionNotify(xfc, &event->xmotion, xfc->remote_app);
1270 break;
1271
1272 case ButtonPress:
1273 status = xf_event_ButtonPress(xfc, &event->xbutton, xfc->remote_app);
1274 break;
1275
1276 case ButtonRelease:
1277 status = xf_event_ButtonRelease(xfc, &event->xbutton, xfc->remote_app);
1278 break;
1279
1280 case KeyPress:
1281 status = xf_event_KeyPress(xfc, &event->xkey, xfc->remote_app);
1282 break;
1283
1284 case KeyRelease:
1285 status = xf_event_KeyReleaseOrIgnore(xfc, &event->xkey, xfc->remote_app);
1286 break;
1287
1288 case FocusIn:
1289 status = xf_event_FocusIn(xfc, &event->xfocus, xfc->remote_app);
1290 break;
1291
1292 case FocusOut:
1293 status = xf_event_FocusOut(xfc, &event->xfocus, xfc->remote_app);
1294 break;
1295
1296 case EnterNotify:
1297 status = xf_event_EnterNotify(xfc, &event->xcrossing, xfc->remote_app);
1298 break;
1299
1300 case LeaveNotify:
1301 status = xf_event_LeaveNotify(xfc, &event->xcrossing, xfc->remote_app);
1302 break;
1303
1304 case NoExpose:
1305 break;
1306
1307 case GraphicsExpose:
1308 break;
1309
1310 case ConfigureNotify:
1311 status = xf_event_ConfigureNotify(xfc, &event->xconfigure, xfc->remote_app);
1312 break;
1313
1314 case MapNotify:
1315 status = xf_event_MapNotify(xfc, &event->xmap, xfc->remote_app);
1316 break;
1317
1318 case UnmapNotify:
1319 status = xf_event_UnmapNotify(xfc, &event->xunmap, xfc->remote_app);
1320 break;
1321
1322 case ReparentNotify:
1323 break;
1324
1325 case MappingNotify:
1326 status = xf_event_MappingNotify(xfc, &event->xmapping, xfc->remote_app);
1327 break;
1328
1329 case ClientMessage:
1330 status = xf_event_ClientMessage(xfc, &event->xclient, xfc->remote_app);
1331 break;
1332
1333 case PropertyNotify:
1334 status = xf_event_PropertyNotify(xfc, &event->xproperty, xfc->remote_app);
1335 break;
1336
1337 default:
1338 if (freerdp_settings_get_bool(settings, FreeRDP_SupportDisplayControl))
1339 xf_disp_handle_xevent(xfc, event);
1340
1341 break;
1342 }
1343
1344 xfWindow* window = xfc->window;
1345 xfFloatbar* floatbar = nullptr;
1346 if (window)
1347 floatbar = window->floatbar;
1348
1349 xf_cliprdr_handle_xevent(xfc, event);
1350 if (!xf_floatbar_check_event(floatbar, event) && !xf_floatbar_is_locked(floatbar))
1351 {
1352 if (xf_input_handle_event(xfc, event) < 0)
1353 return FALSE;
1354 }
1355
1356 LogDynAndXSync(xfc->log, xfc->display, FALSE);
1357 return status;
1358}
1359
1360BOOL xf_generic_RawButtonEvent_(xfContext* xfc, int button, BOOL app, BOOL down, const char* file,
1361 const char* fkt, size_t line)
1362{
1363 UINT16 flags = 0;
1364
1365 if (WLog_IsLevelActive(xfc->log, mouseLogLevel))
1366 WLog_PrintTextMessage(xfc->log, mouseLogLevel, line, file, fkt,
1367 "%s: button=%d, app=%d, down=%d", __func__, button, app, down);
1368
1369 if (app || (button < 0))
1370 return FALSE;
1371
1372 for (size_t i = 0; i < ARRAYSIZE(xfc->button_map); i++)
1373 {
1374 const button_map* cur = &xfc->button_map[i];
1375
1376 if (cur->button == (UINT32)button)
1377 {
1378 flags = cur->flags;
1379 break;
1380 }
1381 }
1382
1383 if (flags != 0)
1384 {
1385 if (flags & (PTR_FLAGS_WHEEL | PTR_FLAGS_HWHEEL))
1386 {
1387 if (down)
1388 freerdp_client_send_wheel_event(&xfc->common, flags);
1389 }
1390 else
1391 {
1392 BOOL extended = FALSE;
1393
1394 if (flags & (PTR_XFLAGS_BUTTON1 | PTR_XFLAGS_BUTTON2))
1395 {
1396 extended = TRUE;
1397
1398 if (down)
1399 flags |= PTR_XFLAGS_DOWN;
1400 }
1401 else if (flags & (PTR_FLAGS_BUTTON1 | PTR_FLAGS_BUTTON2 | PTR_FLAGS_BUTTON3))
1402 {
1403 if (down)
1404 flags |= PTR_FLAGS_DOWN;
1405 }
1406
1407 if (extended)
1408 freerdp_client_send_extended_button_event(&xfc->common, TRUE, flags, 0, 0);
1409 else
1410 freerdp_client_send_button_event(&xfc->common, TRUE, flags, 0, 0);
1411 }
1412 }
1413
1414 return TRUE;
1415}
1416
1417BOOL xf_event_update_screen(freerdp* instance)
1418{
1419 WINPR_ASSERT(instance);
1420
1421 xfContext* xfc = (xfContext*)instance->context;
1422 WINPR_ASSERT(xfc);
1423
1424 if (!xfc->exposeRequested)
1425 return TRUE;
1426 xfc->exposeRequested = false;
1427
1428 if (!xfc->remote_app)
1429 {
1430 if (xfc->common.context.gdi->gfx)
1431 {
1432 xf_OutputExpose(xfc, WINPR_ASSERTING_INT_CAST(uint32_t, xfc->exposedArea.x),
1433 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->exposedArea.y),
1434 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->exposedArea.w),
1435 WINPR_ASSERTING_INT_CAST(uint32_t, xfc->exposedArea.h));
1436 return TRUE;
1437 }
1438 xf_draw_screen(xfc, xfc->exposedArea.x, xfc->exposedArea.y, xfc->exposedArea.w,
1439 xfc->exposedArea.h);
1440 }
1441 else
1442 {
1443 xfAppWindow* appWindow = xf_AppWindowFromX11Window(xfc, xfc->exposedWindow);
1444 if (appWindow)
1445 xf_UpdateWindowArea(xfc, appWindow, xfc->exposedArea.x, xfc->exposedArea.y,
1446 xfc->exposedArea.w, xfc->exposedArea.h);
1447 xf_rail_return_window(appWindow, FALSE);
1448 }
1449 return TRUE;
1450}
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
This struct contains function pointer to initialize/free objects.
Definition collections.h:52
OBJECT_FREE_FN fnObjectFree
Definition collections.h:59
WINPR_ATTR_NODISCARD OBJECT_NEW_FN fnObjectNew
Definition collections.h:54