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