FreeRDP
Loading...
Searching...
No Matches
xf_floatbar.c
1
17
18#include <X11/Xlib.h>
19#include <X11/Xutil.h>
20#include <X11/Xatom.h>
21#include <X11/extensions/shape.h>
22#include <X11/cursorfont.h>
23
24#include <string.h>
25
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28#include <winpr/sysinfo.h>
29
30#include "xf_floatbar.h"
31#include "xf_utils.h"
32#include "resource/close.xbm"
33#include "resource/lock.xbm"
34#include "resource/unlock.xbm"
35#include "resource/minimize.xbm"
36#include "resource/restore.xbm"
37
38#include <freerdp/log.h>
39#define TAG CLIENT_TAG("x11")
40
41#define FLOATBAR_HEIGHT 26
42#define FLOATBAR_DEFAULT_WIDTH 576
43#define FLOATBAR_MIN_WIDTH 200
44#define FLOATBAR_BORDER 24
45#define FLOATBAR_BUTTON_WIDTH 24
46#define FLOATBAR_COLOR_BACKGROUND "RGB:31/6c/a9"
47#define FLOATBAR_COLOR_BORDER "RGB:75/9a/c8"
48#define FLOATBAR_COLOR_FOREGROUND "RGB:FF/FF/FF"
49
50/* Pointer position (relative to the main window) at or above which the bar is shown */
51#define FLOATBAR_SHOW_TRIGGER 10
52/* Pointer position (relative to the main window) below which hiding is armed.
53 * The gap between the two thresholds is hysteresis: the bar keeps its state there. */
54#define FLOATBAR_HIDE_THRESHOLD (FLOATBAR_HEIGHT + 14)
55/* The pointer has to stay below FLOATBAR_HIDE_THRESHOLD this long before the bar hides */
56#define FLOATBAR_HIDE_DELAY_MS 300
57/* Duration of a complete show or hide slide */
58#define FLOATBAR_ANIM_DURATION_MS 130
59/* Frame interval used while a slide is running */
60#define FLOATBAR_FRAME_MS 16
61
62#define XF_FLOATBAR_FLAG_POSITION_MASK 0x00C0
63#define XF_FLOATBAR_FLAG_POSITION_LEFT 0x0040
64#define XF_FLOATBAR_FLAG_POSITION_RIGHT 0x0080
65
66#define XF_FLOATBAR_MODE_NONE 0
67#define XF_FLOATBAR_MODE_DRAGGING 1
68#define XF_FLOATBAR_MODE_RESIZE_LEFT 2
69#define XF_FLOATBAR_MODE_RESIZE_RIGHT 3
70
71#define XF_FLOATBAR_BUTTON_CLOSE 1
72#define XF_FLOATBAR_BUTTON_RESTORE 2
73#define XF_FLOATBAR_BUTTON_MINIMIZE 3
74#define XF_FLOATBAR_BUTTON_LOCKED 4
75
76typedef BOOL (*OnClick)(xfFloatbar*);
77
78typedef struct
79{
80 int x;
81 int y;
82 int type;
83 bool focus;
84 bool clicked;
85 OnClick onclick;
86 Window handle;
87 /* cached icon bitmap, rebuilt only when the icon changes (lock/unlock) */
88 Pixmap pattern;
89 const unsigned char* pattern_bits;
90} xfFloatbarButton;
91
92/* colors already allocated by xf_floatbar_get_color(), keyed by their rgb string */
93typedef struct
94{
95 const char* rgb;
96 unsigned long pixel;
97} xfFloatbarColor;
98
99struct xf_floatbar
100{
101 int x;
102 int y;
103 int width;
104 int height;
105 int mode;
106 int last_motion_x_root;
107 int last_motion_y_root;
108 BOOL locked;
109 xfFloatbarButton* buttons[4];
110 Window handle;
111 BOOL hasCursor;
112 xfContext* xfc;
113 DWORD flags;
114 BOOL created;
115 Window root_window;
116 char* title;
117 XFontSet fontSet;
118 BOOL entered;
119 /* slide animation state, positions are in window y coordinates */
120 int target_y;
121 BOOL animating;
122 int anim_from_y;
123 int anim_to_y;
124 UINT64 anim_start_ms;
125 /* time since the pointer went below FLOATBAR_HIDE_THRESHOLD, 0 if not armed */
126 UINT64 hide_armed_ms;
127 /* width the buttons and the background were last drawn for */
128 int drawn_width;
129 /* one entry per floatbar color: background, border, foreground */
130 xfFloatbarColor colors[3];
131 /* cursors created once in xf_floatbar_init_resources() */
132 Cursor cursor_arrow;
133 Cursor cursor_resize;
134};
135
136static void xf_floatbar_button_free(xfContext* xfc, xfFloatbarButton* button);
137static void xf_floatbar_init_resources(xfFloatbar* floatbar);
138
139WINPR_ATTR_MALLOC(xf_floatbar_button_free, 1)
140static xfFloatbarButton* xf_floatbar_new_button(xfFloatbar* floatbar, int type);
141
142WINPR_ATTR_NODISCARD
143static BOOL xf_floatbar_button_onclick_close(xfFloatbar* floatbar)
144{
145 if (!floatbar)
146 return FALSE;
147
148 return freerdp_abort_connect_context(&floatbar->xfc->common.context);
149}
150
151WINPR_ATTR_NODISCARD
152static BOOL xf_floatbar_button_onclick_minimize(xfFloatbar* floatbar)
153{
154 xfContext* xfc = nullptr;
155
156 if (!floatbar || !floatbar->xfc)
157 return FALSE;
158
159 xfc = floatbar->xfc;
160 xf_SetWindowMinimized(xfc, xfc->window);
161 return TRUE;
162}
163
164WINPR_ATTR_NODISCARD
165static BOOL xf_floatbar_button_onclick_restore(xfFloatbar* floatbar)
166{
167 if (!floatbar)
168 return FALSE;
169
170 xf_toggle_fullscreen(floatbar->xfc);
171 return TRUE;
172}
173
174WINPR_ATTR_NODISCARD
175static BOOL xf_floatbar_button_onclick_locked(xfFloatbar* floatbar)
176{
177 if (!floatbar)
178 return FALSE;
179
180 floatbar->locked = !((floatbar->locked));
181 return xf_floatbar_hide_and_show(floatbar);
182}
183
184BOOL xf_floatbar_set_root_y(xfFloatbar* floatbar, int y)
185{
186 if (!floatbar)
187 return FALSE;
188
189 floatbar->last_motion_y_root = y;
190 return TRUE;
191}
192
193static UINT64 xf_floatbar_now_ms(void)
194{
195 return winpr_GetTickCount64NS() / 1000000ULL;
196}
197
198/* Set the bar to a position immediately and cancel any running slide */
199static void xf_floatbar_snap(xfFloatbar* floatbar, int y)
200{
201 WINPR_ASSERT(floatbar);
202 floatbar->y = y;
203 floatbar->target_y = y;
204 floatbar->animating = FALSE;
205 floatbar->hide_armed_ms = 0;
206}
207
208static int xf_floatbar_hidden_y(void)
209{
210 return -FLOATBAR_HEIGHT + 1;
211}
212
213/* Decide whether the bar should be shown or hidden from the last known pointer position.
214 * Showing is immediate, hiding waits until the pointer stayed away for FLOATBAR_HIDE_DELAY_MS.
215 */
216static void xf_floatbar_update_target(xfFloatbar* floatbar, UINT64 now)
217{
218 WINPR_ASSERT(floatbar);
219
220 if (floatbar->last_motion_y_root <= FLOATBAR_SHOW_TRIGGER)
221 {
222 floatbar->target_y = 0;
223 floatbar->hide_armed_ms = 0;
224 }
225 else if (floatbar->last_motion_y_root > FLOATBAR_HIDE_THRESHOLD)
226 {
227 if (floatbar->hide_armed_ms == 0)
228 floatbar->hide_armed_ms = now;
229
230 if (now - floatbar->hide_armed_ms >= FLOATBAR_HIDE_DELAY_MS)
231 floatbar->target_y = xf_floatbar_hidden_y();
232 }
233 else
234 {
235 /* between the thresholds: keep the current state */
236 floatbar->hide_armed_ms = 0;
237 }
238}
239
240/* Advance the slide to the position for time now. Uses time based interpolation with
241 * ease-out when showing and ease-in when hiding, so the speed does not depend on how
242 * often this is called.
243 */
244static void xf_floatbar_step_animation(xfFloatbar* floatbar, UINT64 now)
245{
246 int y = 0;
247 xfContext* xfc = nullptr;
248
249 WINPR_ASSERT(floatbar);
250 xfc = floatbar->xfc;
251 WINPR_ASSERT(xfc);
252 WINPR_ASSERT(xfc->display);
253
254 if (!floatbar->animating)
255 {
256 if (floatbar->y == floatbar->target_y)
257 return;
258
259 floatbar->animating = TRUE;
260 floatbar->anim_from_y = floatbar->y;
261 floatbar->anim_to_y = floatbar->target_y;
262 floatbar->anim_start_ms = now;
263 }
264 else if (floatbar->anim_to_y != floatbar->target_y)
265 {
266 /* target changed while sliding, restart from the current position */
267 floatbar->anim_from_y = floatbar->y;
268 floatbar->anim_to_y = floatbar->target_y;
269 floatbar->anim_start_ms = now;
270 }
271
272 const UINT64 elapsed = now - floatbar->anim_start_ms;
273
274 if (elapsed >= FLOATBAR_ANIM_DURATION_MS)
275 {
276 y = floatbar->anim_to_y;
277 floatbar->animating = FALSE;
278 }
279 else
280 {
281 const double t = (double)elapsed / FLOATBAR_ANIM_DURATION_MS;
282 double progress = 0.0;
283
284 if (floatbar->anim_to_y > floatbar->anim_from_y)
285 {
286 /* showing: ease-out quartic, starts at full speed and settles softly */
287 const double inv = 1.0 - t;
288 const double inv2 = inv * inv;
289 progress = 1.0 - inv2 * inv2;
290 }
291 else
292 {
293 /* hiding: ease-in cubic */
294 progress = t * t * t;
295 }
296
297 const int delta = floatbar->anim_to_y - floatbar->anim_from_y;
298 y = floatbar->anim_from_y + (int)((double)delta * progress);
299 }
300
301 if (y != floatbar->y)
302 {
303 floatbar->y = y;
304 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, floatbar->x, floatbar->y);
305 /* the caller waits for events, make sure the move reaches the server now */
306 LogDynAndXFlush(xfc->log, xfc->display);
307 }
308}
309
310BOOL xf_floatbar_hide_and_show(xfFloatbar* floatbar)
311{
312 if (!floatbar || !floatbar->xfc)
313 return FALSE;
314
315 if (!floatbar->created)
316 return TRUE;
317
318 if (floatbar->locked || (floatbar->mode != XF_FLOATBAR_MODE_NONE))
319 {
320 /* Locked or dragged bars do not move, stop any running slide */
321 floatbar->animating = FALSE;
322 floatbar->hide_armed_ms = 0;
323 return TRUE;
324 }
325
326 const UINT64 now = xf_floatbar_now_ms();
327 xf_floatbar_update_target(floatbar, now);
328 xf_floatbar_step_animation(floatbar, now);
329 return TRUE;
330}
331
332DWORD xf_floatbar_get_wait_timeout(xfFloatbar* floatbar)
333{
334 if (!floatbar || !floatbar->created)
335 return INFINITE;
336
337 if (floatbar->locked || (floatbar->mode != XF_FLOATBAR_MODE_NONE))
338 return INFINITE;
339
340 if (floatbar->animating)
341 return FLOATBAR_FRAME_MS;
342
343 /* a pending hide needs a wakeup even if no pointer event arrives */
344 if ((floatbar->hide_armed_ms != 0) && (floatbar->target_y != xf_floatbar_hidden_y()))
345 {
346 const UINT64 now = xf_floatbar_now_ms();
347 const UINT64 armed = now - floatbar->hide_armed_ms;
348
349 if (armed >= FLOATBAR_HIDE_DELAY_MS)
350 return FLOATBAR_FRAME_MS;
351
352 return WINPR_ASSERTING_INT_CAST(DWORD, FLOATBAR_HIDE_DELAY_MS - armed);
353 }
354
355 return INFINITE;
356}
357
358WINPR_ATTR_NODISCARD
359static BOOL create_floatbar(xfFloatbar* floatbar)
360{
361 xfContext* xfc = nullptr;
362 Status status = 0;
363 XWindowAttributes attr = WINPR_C_ARRAY_INIT;
364
365 WINPR_ASSERT(floatbar);
366 if (floatbar->created)
367 return TRUE;
368
369 xfc = floatbar->xfc;
370 WINPR_ASSERT(xfc);
371 WINPR_ASSERT(xfc->display);
372
373 status = LogDynAndXGetWindowAttributes(xfc->log, xfc->display, floatbar->root_window, &attr);
374 if (status != 1)
375 return FALSE;
376
377 switch (floatbar->flags & XF_FLOATBAR_FLAG_POSITION_MASK)
378 {
379 case XF_FLOATBAR_FLAG_POSITION_LEFT:
380 floatbar->x = 0;
381 break;
382
383 case XF_FLOATBAR_FLAG_POSITION_RIGHT:
384 floatbar->x = attr.width - FLOATBAR_DEFAULT_WIDTH;
385 break;
386
387 default:
388 floatbar->x = attr.width / 2 - FLOATBAR_DEFAULT_WIDTH / 2;
389 break;
390 }
391
392 /* never start off screen, e.g. if the window is narrower than the floatbar */
393 if (floatbar->x < 0)
394 floatbar->x = 0;
395
396 {
397 int y = 0;
398
399 if (((floatbar->flags & 0x0004) == 0) && !floatbar->locked)
400 y = xf_floatbar_hidden_y();
401
402 xf_floatbar_snap(floatbar, y);
403 }
404
405 /* create the window at its logical position, not at 0 and then move it */
406 floatbar->handle =
407 LogDynAndXCreateWindow(xfc->log, xfc->display, floatbar->root_window, floatbar->x,
408 floatbar->y, FLOATBAR_DEFAULT_WIDTH, FLOATBAR_HEIGHT, 0,
409 CopyFromParent, InputOutput, CopyFromParent, 0, nullptr);
410 floatbar->width = FLOATBAR_DEFAULT_WIDTH;
411 floatbar->height = FLOATBAR_HEIGHT;
412 floatbar->drawn_width = 0;
413 floatbar->mode = XF_FLOATBAR_MODE_NONE;
414 floatbar->buttons[0] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_CLOSE);
415 floatbar->buttons[1] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_RESTORE);
416 floatbar->buttons[2] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_MINIMIZE);
417 floatbar->buttons[3] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_LOCKED);
418 LogDynAndXSelectInput(xfc->log, xfc->display, floatbar->handle,
419 ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
420 FocusChangeMask | LeaveWindowMask | EnterWindowMask |
421 StructureNotifyMask | PropertyChangeMask);
422 xf_floatbar_init_resources(floatbar);
423 floatbar->created = TRUE;
424 return TRUE;
425}
426
427BOOL xf_floatbar_toggle_fullscreen(xfFloatbar* floatbar, bool fullscreen)
428{
429 int size = 0;
430 bool visible = False;
431 xfContext* xfc = nullptr;
432
433 if (!floatbar || !floatbar->xfc)
434 return FALSE;
435
436 xfc = floatbar->xfc;
437 WINPR_ASSERT(xfc->display);
438
439 /* Only visible if enabled */
440 if (floatbar->flags & 0x0001)
441 {
442 /* Visible if fullscreen and flag visible in fullscreen mode */
443 visible |= ((floatbar->flags & 0x0010) != 0) && fullscreen;
444 /* Visible if window and flag visible in window mode */
445 visible |= ((floatbar->flags & 0x0020) != 0) && !fullscreen;
446 }
447
448 if (visible)
449 {
450 if (!create_floatbar(floatbar))
451 return FALSE;
452
453 LogDynAndXMapWindow(xfc->log, xfc->display, floatbar->handle);
454 size = ARRAYSIZE(floatbar->buttons);
455
456 for (int i = 0; i < size; i++)
457 {
458 xfFloatbarButton* button = floatbar->buttons[i];
459 LogDynAndXMapWindow(xfc->log, xfc->display, button->handle);
460 }
461
462 /* If default is hidden (and not sticky) don't show on fullscreen state changes */
463 if (((floatbar->flags & 0x0004) == 0) && !floatbar->locked)
464 xf_floatbar_snap(floatbar, xf_floatbar_hidden_y());
465
466 xf_floatbar_hide_and_show(floatbar);
467 }
468 else if (floatbar->created)
469 {
470 XUnmapSubwindows(xfc->display, floatbar->handle);
471 LogDynAndXUnmapWindow(xfc->log, xfc->display, floatbar->handle);
472 }
473
474 return TRUE;
475}
476
477xfFloatbarButton* xf_floatbar_new_button(xfFloatbar* floatbar, int type)
478{
479 xfFloatbarButton* button = nullptr;
480
481 WINPR_ASSERT(floatbar);
482 WINPR_ASSERT(floatbar->xfc);
483 WINPR_ASSERT(floatbar->xfc->display);
484 WINPR_ASSERT(floatbar->handle);
485
486 button = (xfFloatbarButton*)calloc(1, sizeof(xfFloatbarButton));
487 button->type = type;
488
489 switch (type)
490 {
491 case XF_FLOATBAR_BUTTON_CLOSE:
492 button->x = floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * type;
493 button->onclick = xf_floatbar_button_onclick_close;
494 break;
495
496 case XF_FLOATBAR_BUTTON_RESTORE:
497 button->x = floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * type;
498 button->onclick = xf_floatbar_button_onclick_restore;
499 break;
500
501 case XF_FLOATBAR_BUTTON_MINIMIZE:
502 button->x = floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * type;
503 button->onclick = xf_floatbar_button_onclick_minimize;
504 break;
505
506 case XF_FLOATBAR_BUTTON_LOCKED:
507 button->x = FLOATBAR_BORDER;
508 button->onclick = xf_floatbar_button_onclick_locked;
509 break;
510
511 default:
512 break;
513 }
514
515 button->y = 0;
516 button->focus = FALSE;
517 button->handle =
518 LogDynAndXCreateWindow(floatbar->xfc->log, floatbar->xfc->display, floatbar->handle,
519 button->x, 0, FLOATBAR_BUTTON_WIDTH, FLOATBAR_BUTTON_WIDTH, 0,
520 CopyFromParent, InputOutput, CopyFromParent, 0, nullptr);
521 LogDynAndXSelectInput(floatbar->xfc->log, floatbar->xfc->display, button->handle,
522 ExposureMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
523 LeaveWindowMask | EnterWindowMask | StructureNotifyMask);
524 return button;
525}
526
527xfFloatbar* xf_floatbar_new(xfContext* xfc, Window window, const char* title, DWORD flags)
528{
529 WINPR_ASSERT(xfc);
530 WINPR_ASSERT(xfc->display);
531 WINPR_ASSERT(title);
532
533 /* Floatbar not enabled */
534 if ((flags & 0x0001) == 0)
535 return nullptr;
536
537 if (!xfc)
538 return nullptr;
539
540 /* Force disable with remote app */
541 if (xfc->remote_app)
542 return nullptr;
543
544 xfFloatbar* floatbar = (xfFloatbar*)calloc(1, sizeof(xfFloatbar));
545
546 if (!floatbar)
547 return nullptr;
548
549 floatbar->title = _strdup(title);
550
551 if (!floatbar->title)
552 goto fail;
553
554 floatbar->root_window = window;
555 floatbar->flags = flags;
556 floatbar->xfc = xfc;
557 floatbar->locked = (flags & 0x0002) != 0;
558 xf_floatbar_toggle_fullscreen(floatbar, FALSE);
559
560 {
561 char** missingList = nullptr;
562 int missingCount = 0;
563 char* defString = nullptr;
564 floatbar->fontSet = XCreateFontSet(floatbar->xfc->display, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
565 &missingList, &missingCount, &defString);
566
567 if (floatbar->fontSet == nullptr)
568 {
569 WLog_ERR(TAG, "Failed to create fontset");
570 }
571 XFreeStringList(missingList);
572 }
573 return floatbar;
574fail:
575 WINPR_PRAGMA_DIAG_PUSH
576 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
577 xf_floatbar_free(floatbar);
578 WINPR_PRAGMA_DIAG_POP
579 return nullptr;
580}
581
582WINPR_ATTR_NODISCARD
583static unsigned long xf_floatbar_get_color(xfFloatbar* floatbar, char* rgb_value)
584{
585 XColor color;
586
587 WINPR_ASSERT(floatbar);
588 WINPR_ASSERT(floatbar->xfc);
589 WINPR_ASSERT(rgb_value);
590
591 /* allocate each color once, painting then needs no round trip to the X server */
592 for (size_t i = 0; i < ARRAYSIZE(floatbar->colors); i++)
593 {
594 const xfFloatbarColor* entry = &floatbar->colors[i];
595
596 if (entry->rgb && (strcmp(entry->rgb, rgb_value) == 0))
597 return entry->pixel;
598 }
599
600 Display* display = floatbar->xfc->display;
601 WINPR_ASSERT(display);
602
603 Colormap cmap = DefaultColormap(display, XDefaultScreen(display));
604 XParseColor(display, cmap, rgb_value, &color);
605 XAllocColor(display, cmap, &color);
606
607 for (size_t i = 0; i < ARRAYSIZE(floatbar->colors); i++)
608 {
609 xfFloatbarColor* entry = &floatbar->colors[i];
610
611 if (!entry->rgb)
612 {
613 entry->rgb = rgb_value;
614 entry->pixel = color.pixel;
615 break;
616 }
617 }
618
619 return color.pixel;
620}
621
622/* Create the cursors once, so pointer motion does not create and free one each time */
623static void xf_floatbar_init_resources(xfFloatbar* floatbar)
624{
625 WINPR_ASSERT(floatbar);
626 WINPR_ASSERT(floatbar->xfc);
627 WINPR_ASSERT(floatbar->xfc->display);
628
629 floatbar->cursor_arrow = XCreateFontCursor(floatbar->xfc->display, XC_arrow);
630 floatbar->cursor_resize = XCreateFontCursor(floatbar->xfc->display, XC_sb_h_double_arrow);
631}
632
633static void xf_floatbar_event_expose(xfFloatbar* floatbar)
634{
635 GC gc = nullptr;
636 GC shape_gc = nullptr;
637 Pixmap pmap = 0;
638 Pixmap buffer = 0;
639 XPoint shape[5] = WINPR_C_ARRAY_INIT;
640 XPoint border[5] = WINPR_C_ARRAY_INIT;
641
642 WINPR_ASSERT(floatbar);
643 WINPR_ASSERT(floatbar->xfc);
644
645 Display* display = floatbar->xfc->display;
646 WINPR_ASSERT(display);
647
648 /* create the pixmap that we'll use for shaping the window */
649 pmap = LogDynAndXCreatePixmap(floatbar->xfc->log, display, floatbar->handle,
650 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->width),
651 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height), 1);
652 gc = LogDynAndXCreateGC(floatbar->xfc->log, display, floatbar->handle, 0, 0);
653 shape_gc = LogDynAndXCreateGC(floatbar->xfc->log, display, pmap, 0, 0);
654 /* draw into an off-screen buffer and copy it once, avoids flicker */
655 buffer = LogDynAndXCreatePixmap(floatbar->xfc->log, display, floatbar->handle,
656 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->width),
657 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height),
658 (unsigned int)DefaultDepth(display, XDefaultScreen(display)));
659 /* points for drawing the floatbar */
660 shape[0].x = 0;
661 shape[0].y = 0;
662 shape[1].x = WINPR_ASSERTING_INT_CAST(short, floatbar->width);
663 shape[1].y = 0;
664 shape[2].x = WINPR_ASSERTING_INT_CAST(short, shape[1].x - FLOATBAR_BORDER);
665 shape[2].y = FLOATBAR_HEIGHT;
666 shape[3].x = WINPR_ASSERTING_INT_CAST(short, shape[0].x + FLOATBAR_BORDER);
667 shape[3].y = FLOATBAR_HEIGHT;
668 shape[4].x = shape[0].x;
669 shape[4].y = shape[0].y;
670 /* points for drawing the border of the floatbar */
671 border[0].x = shape[0].x;
672 border[0].y = WINPR_ASSERTING_INT_CAST(short, shape[0].y - 1);
673 border[1].x = WINPR_ASSERTING_INT_CAST(short, shape[1].x - 1);
674 border[1].y = WINPR_ASSERTING_INT_CAST(short, shape[1].y - 1);
675 border[2].x = shape[2].x;
676 border[2].y = WINPR_ASSERTING_INT_CAST(short, shape[2].y - 1);
677 border[3].x = WINPR_ASSERTING_INT_CAST(short, shape[3].x - 1);
678 border[3].y = WINPR_ASSERTING_INT_CAST(short, shape[3].y - 1);
679 border[4].x = border[0].x;
680 border[4].y = border[0].y;
681 /* Fill all pixels with 0 */
682 LogDynAndXSetForeground(floatbar->xfc->log, display, shape_gc, 0);
683 LogDynAndXFillRectangle(floatbar->xfc->log, display, pmap, shape_gc, 0, 0,
684 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->width),
685 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height));
686 /* Fill all pixels which should be shown with 1 */
687 LogDynAndXSetForeground(floatbar->xfc->log, display, shape_gc, 1);
688 XFillPolygon(display, pmap, shape_gc, shape, 5, 0, CoordModeOrigin);
689 XShapeCombineMask(display, floatbar->handle, ShapeBounding, 0, 0, pmap, ShapeSet);
690 /* draw the float bar */
691 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
692 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BACKGROUND));
693 XFillPolygon(display, buffer, gc, shape, 4, 0, CoordModeOrigin);
694 /* draw an border for the floatbar */
695 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
696 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
697 XDrawLines(display, buffer, gc, border, 5, CoordModeOrigin);
698 /* draw the host name connected to (limit to maximum file name) */
699 const size_t len = strnlen(floatbar->title, MAX_PATH);
700 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
701 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_FOREGROUND));
702
703 WINPR_ASSERT(len <= INT32_MAX / 2);
704 const int fx = floatbar->width / 2 - (int)len * 2;
705 if (floatbar->fontSet != nullptr)
706 {
707 XmbDrawString(display, buffer, floatbar->fontSet, gc, fx, 15, floatbar->title, (int)len);
708 }
709 else
710 {
711 XDrawString(display, buffer, gc, fx, 15, floatbar->title, (int)len);
712 }
713
714 LogDynAndXCopyArea(floatbar->xfc->log, display, buffer, floatbar->handle, gc, 0, 0,
715 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->width),
716 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height), 0, 0);
717 LogDynAndXFreePixmap(floatbar->xfc->log, display, buffer);
718 LogDynAndXFreeGC(floatbar->xfc->log, display, gc);
719 LogDynAndXFreeGC(floatbar->xfc->log, display, shape_gc);
720}
721
722WINPR_ATTR_NODISCARD
723static xfFloatbarButton* xf_floatbar_get_button(xfFloatbar* floatbar, Window window)
724{
725 WINPR_ASSERT(floatbar);
726 const size_t size = ARRAYSIZE(floatbar->buttons);
727
728 for (size_t i = 0; i < size; i++)
729 {
730 xfFloatbarButton* button = floatbar->buttons[i];
731 if (button->handle == window)
732 {
733 return button;
734 }
735 }
736
737 return nullptr;
738}
739
740static void xf_floatbar_button_update_positon(xfFloatbar* floatbar)
741{
742 xfFloatbarButton* button = nullptr;
743 WINPR_ASSERT(floatbar);
744 xfContext* xfc = floatbar->xfc;
745 const size_t size = ARRAYSIZE(floatbar->buttons);
746
747 for (size_t i = 0; i < size; i++)
748 {
749 button = floatbar->buttons[i];
750
751 switch (button->type)
752 {
753 case XF_FLOATBAR_BUTTON_CLOSE:
754 button->x =
755 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
756 break;
757
758 case XF_FLOATBAR_BUTTON_RESTORE:
759 button->x =
760 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
761 break;
762
763 case XF_FLOATBAR_BUTTON_MINIMIZE:
764 button->x =
765 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
766 break;
767
768 default:
769 break;
770 }
771
772 WINPR_ASSERT(xfc);
773 WINPR_ASSERT(xfc->display);
774 LogDynAndXMoveWindow(xfc->log, xfc->display, button->handle, button->x, button->y);
775 }
776
777 floatbar->drawn_width = floatbar->width;
778 xf_floatbar_event_expose(floatbar);
779}
780
781static void xf_floatbar_button_event_expose(xfFloatbar* floatbar, Window window)
782{
783 xfFloatbarButton* button = xf_floatbar_get_button(floatbar, window);
784 static unsigned char* bits;
785 GC gc = nullptr;
786 Pixmap pattern = 0;
787 xfContext* xfc = floatbar->xfc;
788
789 if (!button)
790 return;
791
792 WINPR_ASSERT(xfc);
793 WINPR_ASSERT(xfc->display);
794 WINPR_ASSERT(xfc->window);
795
796 gc = LogDynAndXCreateGC(xfc->log, xfc->display, button->handle, 0, 0);
797 floatbar = xfc->window->floatbar;
798 WINPR_ASSERT(floatbar);
799
800 switch (button->type)
801 {
802 case XF_FLOATBAR_BUTTON_CLOSE:
803 bits = close_bits;
804 break;
805
806 case XF_FLOATBAR_BUTTON_RESTORE:
807 bits = restore_bits;
808 break;
809
810 case XF_FLOATBAR_BUTTON_MINIMIZE:
811 bits = minimize_bits;
812 break;
813
814 case XF_FLOATBAR_BUTTON_LOCKED:
815 if (floatbar->locked)
816 bits = lock_bits;
817 else
818 bits = unlock_bits;
819
820 break;
821
822 default:
823 break;
824 }
825
826 /* rebuild the icon bitmap only when the icon itself changed */
827 if (button->pattern_bits != bits)
828 {
829 if (button->pattern)
830 LogDynAndXFreePixmap(xfc->log, xfc->display, button->pattern);
831
832 button->pattern = XCreateBitmapFromData(xfc->display, button->handle, (const char*)bits,
833 FLOATBAR_BUTTON_WIDTH, FLOATBAR_BUTTON_WIDTH);
834 button->pattern_bits = bits;
835 }
836
837 pattern = button->pattern;
838
839 if (!(button->focus))
840 LogDynAndXSetForeground(floatbar->xfc->log, xfc->display, gc,
841 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BACKGROUND));
842 else
843 LogDynAndXSetForeground(floatbar->xfc->log, xfc->display, gc,
844 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
845
846 LogDynAndXSetBackground(xfc->log, xfc->display, gc,
847 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_FOREGROUND));
848 XCopyPlane(xfc->display, pattern, button->handle, gc, 0, 0, FLOATBAR_BUTTON_WIDTH,
849 FLOATBAR_BUTTON_WIDTH, 0, 0, 1);
850 LogDynAndXFreeGC(xfc->log, xfc->display, gc);
851}
852
853static void xf_floatbar_button_event_buttonpress(xfFloatbar* floatbar, const XButtonEvent* event)
854{
855 WINPR_ASSERT(event);
856 xfFloatbarButton* button = xf_floatbar_get_button(floatbar, event->window);
857
858 if (button)
859 button->clicked = TRUE;
860}
861
862static void xf_floatbar_button_event_buttonrelease(xfFloatbar* floatbar, const XButtonEvent* event)
863{
864 xfFloatbarButton* button = nullptr;
865
866 WINPR_ASSERT(floatbar);
867 WINPR_ASSERT(event);
868
869 button = xf_floatbar_get_button(floatbar, event->window);
870
871 if (button)
872 {
873 if (button->clicked)
874 button->onclick(floatbar);
875 button->clicked = FALSE;
876 }
877}
878
879static void xf_floatbar_event_buttonpress(xfFloatbar* floatbar, const XButtonEvent* event)
880{
881 WINPR_ASSERT(floatbar);
882 WINPR_ASSERT(event);
883
884 switch (event->button)
885 {
886 case Button1:
887 if (event->x <= FLOATBAR_BORDER)
888 floatbar->mode = XF_FLOATBAR_MODE_RESIZE_LEFT;
889 else if (event->x >= (floatbar->width - FLOATBAR_BORDER))
890 floatbar->mode = XF_FLOATBAR_MODE_RESIZE_RIGHT;
891 else
892 floatbar->mode = XF_FLOATBAR_MODE_DRAGGING;
893
894 break;
895
896 default:
897 break;
898 }
899}
900
901static void xf_floatbar_event_buttonrelease(xfFloatbar* floatbar, const XButtonEvent* event)
902{
903 WINPR_ASSERT(floatbar);
904 WINPR_ASSERT(event);
905
906 switch (event->button)
907 {
908 case Button1:
909 floatbar->mode = XF_FLOATBAR_MODE_NONE;
910 break;
911
912 default:
913 break;
914 }
915}
916
917static void xf_floatbar_resize(xfFloatbar* floatbar, const XMotionEvent* event)
918{
919 int x = 0;
920 int width = 0;
921 int movement = 0;
922
923 WINPR_ASSERT(floatbar);
924 WINPR_ASSERT(event);
925
926 xfContext* xfc = floatbar->xfc;
927 WINPR_ASSERT(xfc);
928 WINPR_ASSERT(xfc->display);
929
930 /* calculate movement which happened on the root window */
931 movement = event->x_root - floatbar->last_motion_x_root;
932
933 /* set x and width depending if movement happens on the left or right */
934 if (floatbar->mode == XF_FLOATBAR_MODE_RESIZE_LEFT)
935 {
936 x = floatbar->x + movement;
937 width = floatbar->width + movement * -1;
938 }
939 else
940 {
941 x = floatbar->x;
942 width = floatbar->width + movement;
943 }
944
945 /* only resize and move window if still above minimum width */
946 if (FLOATBAR_MIN_WIDTH < width)
947 {
948 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, floatbar->handle, x, 0,
949 WINPR_ASSERTING_INT_CAST(uint32_t, width),
950 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height));
951 floatbar->x = x;
952 floatbar->width = width;
953 }
954}
955
956static void xf_floatbar_dragging(xfFloatbar* floatbar, const XMotionEvent* event)
957{
958 int x = 0;
959 int movement = 0;
960
961 WINPR_ASSERT(floatbar);
962 WINPR_ASSERT(event);
963 xfContext* xfc = floatbar->xfc;
964 WINPR_ASSERT(xfc);
965 WINPR_ASSERT(xfc->window);
966 WINPR_ASSERT(xfc->display);
967
968 /* calculate movement and new x position */
969 movement = event->x_root - floatbar->last_motion_x_root;
970 x = floatbar->x + movement;
971
972 /* do nothing if floatbar would be moved out of the window */
973 if (x < 0 || (x + floatbar->width) > xfc->window->width)
974 return;
975
976 /* move window to new x position */
977 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, x, 0);
978 /* update struct values for the next event */
979 floatbar->last_motion_x_root = floatbar->last_motion_x_root + movement;
980 floatbar->x = x;
981}
982
983static void xf_floatbar_event_motionnotify(xfFloatbar* floatbar, const XMotionEvent* event)
984{
985 int mode = 0;
986 Cursor cursor = 0;
987
988 WINPR_ASSERT(floatbar);
989 WINPR_ASSERT(event);
990
991 xfContext* xfc = floatbar->xfc;
992 WINPR_ASSERT(xfc);
993 WINPR_ASSERT(xfc->display);
994
995 mode = floatbar->mode;
996 cursor = floatbar->cursor_arrow;
997
998 if ((event->state & Button1Mask) && (mode > XF_FLOATBAR_MODE_DRAGGING))
999 {
1000 xf_floatbar_resize(floatbar, event);
1001 }
1002 else if ((event->state & Button1Mask) && (mode == XF_FLOATBAR_MODE_DRAGGING))
1003 {
1004 xf_floatbar_dragging(floatbar, event);
1005 }
1006 else
1007 {
1008 if (event->x <= FLOATBAR_BORDER || event->x >= floatbar->width - FLOATBAR_BORDER)
1009 cursor = floatbar->cursor_resize;
1010 }
1011
1012 XDefineCursor(xfc->display, xfc->window->handle, cursor);
1013 floatbar->last_motion_x_root = event->x_root;
1014}
1015
1016static void xf_floatbar_button_event_focusin(xfFloatbar* floatbar, const XAnyEvent* event)
1017{
1018 xfFloatbarButton* button = nullptr;
1019
1020 WINPR_ASSERT(floatbar);
1021 WINPR_ASSERT(event);
1022
1023 button = xf_floatbar_get_button(floatbar, event->window);
1024
1025 if (button)
1026 {
1027 button->focus = TRUE;
1028 xf_floatbar_button_event_expose(floatbar, event->window);
1029 }
1030}
1031
1032static void xf_floatbar_button_event_focusout(xfFloatbar* floatbar, const XAnyEvent* event)
1033{
1034 xfFloatbarButton* button = nullptr;
1035
1036 WINPR_ASSERT(floatbar);
1037 WINPR_ASSERT(event);
1038
1039 button = xf_floatbar_get_button(floatbar, event->window);
1040
1041 if (button)
1042 {
1043 button->focus = FALSE;
1044 xf_floatbar_button_event_expose(floatbar, event->window);
1045 }
1046}
1047
1048static void xf_floatbar_event_focusout(xfFloatbar* floatbar)
1049{
1050 WINPR_ASSERT(floatbar);
1051 xfContext* xfc = floatbar->xfc;
1052 WINPR_ASSERT(xfc);
1053
1054 if (xfc->pointer)
1055 {
1056 WINPR_ASSERT(xfc->window);
1057 WINPR_ASSERT(xfc->pointer);
1058 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
1059 }
1060}
1061
1062/* y is relative to the floatbar window, convert it to the main window */
1063static void xf_floatbar_set_pointer_y(xfFloatbar* floatbar, int y)
1064{
1065 WINPR_ASSERT(floatbar);
1066 floatbar->last_motion_y_root = y + floatbar->y;
1067}
1068
1069BOOL xf_floatbar_check_event(xfFloatbar* floatbar, const XEvent* event)
1070{
1071 if (!floatbar || !floatbar->xfc || !event)
1072 return FALSE;
1073
1074 if (!floatbar->created)
1075 return FALSE;
1076
1077 if (event->xany.window == floatbar->handle)
1078 return TRUE;
1079
1080 size_t size = ARRAYSIZE(floatbar->buttons);
1081
1082 for (size_t i = 0; i < size; i++)
1083 {
1084 const xfFloatbarButton* button = floatbar->buttons[i];
1085
1086 if (event->xany.window == button->handle)
1087 return TRUE;
1088 }
1089
1090 return FALSE;
1091}
1092
1093BOOL xf_floatbar_event_process(xfFloatbar* floatbar, const XEvent* event)
1094{
1095 if (!floatbar || !floatbar->xfc || !event)
1096 return FALSE;
1097
1098 if (!floatbar->created)
1099 return FALSE;
1100
1101 switch (event->type)
1102 {
1103 case Expose:
1104 if (event->xexpose.window == floatbar->handle)
1105 xf_floatbar_event_expose(floatbar);
1106 else
1107 xf_floatbar_button_event_expose(floatbar, event->xexpose.window);
1108
1109 break;
1110
1111 case MotionNotify:
1112 if (event->xany.window == floatbar->handle)
1113 xf_floatbar_set_pointer_y(floatbar, event->xmotion.y);
1114
1115 xf_floatbar_event_motionnotify(floatbar, &event->xmotion);
1116 break;
1117
1118 case ButtonPress:
1119 if (event->xany.window == floatbar->handle)
1120 xf_floatbar_event_buttonpress(floatbar, &event->xbutton);
1121 else
1122 xf_floatbar_button_event_buttonpress(floatbar, &event->xbutton);
1123
1124 break;
1125
1126 case ButtonRelease:
1127 if (event->xany.window == floatbar->handle)
1128 xf_floatbar_event_buttonrelease(floatbar, &event->xbutton);
1129 else
1130 xf_floatbar_button_event_buttonrelease(floatbar, &event->xbutton);
1131
1132 break;
1133
1134 case EnterNotify:
1135 floatbar->entered = TRUE;
1136 if (event->xany.window == floatbar->handle)
1137 xf_floatbar_set_pointer_y(floatbar, event->xcrossing.y);
1138
1139 WINPR_FALLTHROUGH
1140 case FocusIn:
1141 if (event->xany.window != floatbar->handle)
1142 xf_floatbar_button_event_focusin(floatbar, &event->xany);
1143
1144 break;
1145
1146 case LeaveNotify:
1147 floatbar->entered = FALSE;
1148 WINPR_FALLTHROUGH
1149 case FocusOut:
1150 if (event->xany.window == floatbar->handle)
1151 xf_floatbar_event_focusout(floatbar);
1152 else
1153 xf_floatbar_button_event_focusout(floatbar, &event->xany);
1154
1155 break;
1156
1157 case ConfigureNotify:
1158 /* a plain move (the slide) does not change the layout, only a resize does */
1159 if ((event->xany.window == floatbar->handle) &&
1160 (event->xconfigure.width != floatbar->drawn_width))
1161 xf_floatbar_button_update_positon(floatbar);
1162
1163 break;
1164
1165 case PropertyNotify:
1166 if (event->xany.window == floatbar->handle)
1167 xf_floatbar_button_update_positon(floatbar);
1168
1169 break;
1170
1171 default:
1172 break;
1173 }
1174
1175 return floatbar->handle == event->xany.window;
1176}
1177
1178void xf_floatbar_button_free(xfContext* xfc, xfFloatbarButton* button)
1179{
1180 if (!button)
1181 return;
1182
1183 if (button->handle)
1184 {
1185 WINPR_ASSERT(xfc);
1186 WINPR_ASSERT(xfc->display);
1187 LogDynAndXUnmapWindow(xfc->log, xfc->display, button->handle);
1188 LogDynAndXDestroyWindow(xfc->log, xfc->display, button->handle);
1189 }
1190
1191 if (button->pattern)
1192 LogDynAndXFreePixmap(xfc->log, xfc->display, button->pattern);
1193
1194 free(button);
1195}
1196
1197void xf_floatbar_free(xfFloatbar* floatbar)
1198{
1199 size_t size = 0;
1200 xfContext* xfc = nullptr;
1201
1202 if (!floatbar)
1203 return;
1204
1205 free(floatbar->title);
1206 xfc = floatbar->xfc;
1207 WINPR_ASSERT(xfc);
1208
1209 size = ARRAYSIZE(floatbar->buttons);
1210
1211 for (size_t i = 0; i < size; i++)
1212 {
1213 xf_floatbar_button_free(xfc, floatbar->buttons[i]);
1214 floatbar->buttons[i] = nullptr;
1215 }
1216
1217 if (floatbar->handle)
1218 {
1219 WINPR_ASSERT(xfc->display);
1220 LogDynAndXUnmapWindow(xfc->log, xfc->display, floatbar->handle);
1221 LogDynAndXDestroyWindow(xfc->log, xfc->display, floatbar->handle);
1222 }
1223
1224 if (floatbar->created)
1225 {
1226 XFreeCursor(xfc->display, floatbar->cursor_arrow);
1227 XFreeCursor(xfc->display, floatbar->cursor_resize);
1228 }
1229
1230 free(floatbar);
1231}
1232
1233BOOL xf_floatbar_is_locked(xfFloatbar* floatbar)
1234{
1235 if (!floatbar)
1236 return FALSE;
1237 return floatbar->mode != XF_FLOATBAR_MODE_NONE;
1238}
1239
1240BOOL xf_floatbar_is_window(xfFloatbar* floatbar, Window window)
1241{
1242 if (!floatbar)
1243 return FALSE;
1244 if (floatbar->handle == window)
1245 return TRUE;
1246 return floatbar->entered;
1247}
1248
1249BOOL xfc_is_floatbar_window(xfContext* xfc, Window window)
1250{
1251 if (!xfc || !xfc->window)
1252 return FALSE;
1253 return xf_floatbar_is_window(xfc->window->floatbar, window);
1254}