21#include <X11/extensions/shape.h>
22#include <X11/cursorfont.h>
26#include <winpr/assert.h>
27#include <winpr/cast.h>
28#include <winpr/sysinfo.h>
30#include "xf_floatbar.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"
38#include <freerdp/log.h>
39#define TAG CLIENT_TAG("x11")
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"
51#define FLOATBAR_SHOW_TRIGGER 10
54#define FLOATBAR_HIDE_THRESHOLD (FLOATBAR_HEIGHT + 14)
56#define FLOATBAR_HIDE_DELAY_MS 300
58#define FLOATBAR_ANIM_DURATION_MS 130
60#define FLOATBAR_FRAME_MS 16
62#define XF_FLOATBAR_FLAG_POSITION_MASK 0x00C0
63#define XF_FLOATBAR_FLAG_POSITION_LEFT 0x0040
64#define XF_FLOATBAR_FLAG_POSITION_RIGHT 0x0080
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
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
76typedef BOOL (*OnClick)(xfFloatbar*);
89 const unsigned char* pattern_bits;
106 int last_motion_x_root;
107 int last_motion_y_root;
109 xfFloatbarButton* buttons[4];
124 UINT64 anim_start_ms;
126 UINT64 hide_armed_ms;
130 xfFloatbarColor colors[3];
133 Cursor cursor_resize;
136static void xf_floatbar_button_free(xfContext* xfc, xfFloatbarButton* button);
137static void xf_floatbar_init_resources(xfFloatbar* floatbar);
139WINPR_ATTR_MALLOC(xf_floatbar_button_free, 1)
140static xfFloatbarButton* xf_floatbar_new_button(xfFloatbar* floatbar,
int type);
143static BOOL xf_floatbar_button_onclick_close(xfFloatbar* floatbar)
148 return freerdp_abort_connect_context(&floatbar->xfc->common.context);
152static BOOL xf_floatbar_button_onclick_minimize(xfFloatbar* floatbar)
154 xfContext* xfc =
nullptr;
156 if (!floatbar || !floatbar->xfc)
160 xf_SetWindowMinimized(xfc, xfc->window);
165static BOOL xf_floatbar_button_onclick_restore(xfFloatbar* floatbar)
170 xf_toggle_fullscreen(floatbar->xfc);
175static BOOL xf_floatbar_button_onclick_locked(xfFloatbar* floatbar)
180 floatbar->locked = !((floatbar->locked));
181 return xf_floatbar_hide_and_show(floatbar);
184BOOL xf_floatbar_set_root_y(xfFloatbar* floatbar,
int y)
189 floatbar->last_motion_y_root = y;
193static UINT64 xf_floatbar_now_ms(
void)
195 return winpr_GetTickCount64NS() / 1000000ULL;
199static void xf_floatbar_snap(xfFloatbar* floatbar,
int y)
201 WINPR_ASSERT(floatbar);
203 floatbar->target_y = y;
204 floatbar->animating = FALSE;
205 floatbar->hide_armed_ms = 0;
208static int xf_floatbar_hidden_y(
void)
210 return -FLOATBAR_HEIGHT + 1;
216static void xf_floatbar_update_target(xfFloatbar* floatbar, UINT64 now)
218 WINPR_ASSERT(floatbar);
220 if (floatbar->last_motion_y_root <= FLOATBAR_SHOW_TRIGGER)
222 floatbar->target_y = 0;
223 floatbar->hide_armed_ms = 0;
225 else if (floatbar->last_motion_y_root > FLOATBAR_HIDE_THRESHOLD)
227 if (floatbar->hide_armed_ms == 0)
228 floatbar->hide_armed_ms = now;
230 if (now - floatbar->hide_armed_ms >= FLOATBAR_HIDE_DELAY_MS)
231 floatbar->target_y = xf_floatbar_hidden_y();
236 floatbar->hide_armed_ms = 0;
244static void xf_floatbar_step_animation(xfFloatbar* floatbar, UINT64 now)
247 xfContext* xfc =
nullptr;
249 WINPR_ASSERT(floatbar);
252 WINPR_ASSERT(xfc->display);
254 if (!floatbar->animating)
256 if (floatbar->y == floatbar->target_y)
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;
264 else if (floatbar->anim_to_y != floatbar->target_y)
267 floatbar->anim_from_y = floatbar->y;
268 floatbar->anim_to_y = floatbar->target_y;
269 floatbar->anim_start_ms = now;
272 const UINT64 elapsed = now - floatbar->anim_start_ms;
274 if (elapsed >= FLOATBAR_ANIM_DURATION_MS)
276 y = floatbar->anim_to_y;
277 floatbar->animating = FALSE;
281 const double t = (double)elapsed / FLOATBAR_ANIM_DURATION_MS;
282 double progress = 0.0;
284 if (floatbar->anim_to_y > floatbar->anim_from_y)
287 const double inv = 1.0 - t;
288 const double inv2 = inv * inv;
289 progress = 1.0 - inv2 * inv2;
294 progress = t * t * t;
297 const int delta = floatbar->anim_to_y - floatbar->anim_from_y;
298 y = floatbar->anim_from_y + (int)((
double)delta * progress);
301 if (y != floatbar->y)
304 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, floatbar->x, floatbar->y);
306 LogDynAndXFlush(xfc->log, xfc->display);
310BOOL xf_floatbar_hide_and_show(xfFloatbar* floatbar)
312 if (!floatbar || !floatbar->xfc)
315 if (!floatbar->created)
318 if (floatbar->locked || (floatbar->mode != XF_FLOATBAR_MODE_NONE))
321 floatbar->animating = FALSE;
322 floatbar->hide_armed_ms = 0;
326 const UINT64 now = xf_floatbar_now_ms();
327 xf_floatbar_update_target(floatbar, now);
328 xf_floatbar_step_animation(floatbar, now);
332DWORD xf_floatbar_get_wait_timeout(xfFloatbar* floatbar)
334 if (!floatbar || !floatbar->created)
337 if (floatbar->locked || (floatbar->mode != XF_FLOATBAR_MODE_NONE))
340 if (floatbar->animating)
341 return FLOATBAR_FRAME_MS;
344 if ((floatbar->hide_armed_ms != 0) && (floatbar->target_y != xf_floatbar_hidden_y()))
346 const UINT64 now = xf_floatbar_now_ms();
347 const UINT64 armed = now - floatbar->hide_armed_ms;
349 if (armed >= FLOATBAR_HIDE_DELAY_MS)
350 return FLOATBAR_FRAME_MS;
352 return WINPR_ASSERTING_INT_CAST(DWORD, FLOATBAR_HIDE_DELAY_MS - armed);
359static BOOL create_floatbar(xfFloatbar* floatbar)
361 xfContext* xfc =
nullptr;
363 XWindowAttributes attr = WINPR_C_ARRAY_INIT;
365 WINPR_ASSERT(floatbar);
366 if (floatbar->created)
371 WINPR_ASSERT(xfc->display);
373 status = LogDynAndXGetWindowAttributes(xfc->log, xfc->display, floatbar->root_window, &attr);
377 switch (floatbar->flags & XF_FLOATBAR_FLAG_POSITION_MASK)
379 case XF_FLOATBAR_FLAG_POSITION_LEFT:
383 case XF_FLOATBAR_FLAG_POSITION_RIGHT:
384 floatbar->x = attr.width - FLOATBAR_DEFAULT_WIDTH;
388 floatbar->x = attr.width / 2 - FLOATBAR_DEFAULT_WIDTH / 2;
399 if (((floatbar->flags & 0x0004) == 0) && !floatbar->locked)
400 y = xf_floatbar_hidden_y();
402 xf_floatbar_snap(floatbar, y);
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;
427BOOL xf_floatbar_toggle_fullscreen(xfFloatbar* floatbar,
bool fullscreen)
430 bool visible = False;
431 xfContext* xfc =
nullptr;
433 if (!floatbar || !floatbar->xfc)
437 WINPR_ASSERT(xfc->display);
440 if (floatbar->flags & 0x0001)
443 visible |= ((floatbar->flags & 0x0010) != 0) && fullscreen;
445 visible |= ((floatbar->flags & 0x0020) != 0) && !fullscreen;
450 if (!create_floatbar(floatbar))
453 LogDynAndXMapWindow(xfc->log, xfc->display, floatbar->handle);
454 size = ARRAYSIZE(floatbar->buttons);
456 for (
int i = 0; i < size; i++)
458 xfFloatbarButton* button = floatbar->buttons[i];
459 LogDynAndXMapWindow(xfc->log, xfc->display, button->handle);
463 if (((floatbar->flags & 0x0004) == 0) && !floatbar->locked)
464 xf_floatbar_snap(floatbar, xf_floatbar_hidden_y());
466 xf_floatbar_hide_and_show(floatbar);
468 else if (floatbar->created)
470 XUnmapSubwindows(xfc->display, floatbar->handle);
471 LogDynAndXUnmapWindow(xfc->log, xfc->display, floatbar->handle);
477xfFloatbarButton* xf_floatbar_new_button(xfFloatbar* floatbar,
int type)
479 xfFloatbarButton* button =
nullptr;
481 WINPR_ASSERT(floatbar);
482 WINPR_ASSERT(floatbar->xfc);
483 WINPR_ASSERT(floatbar->xfc->display);
484 WINPR_ASSERT(floatbar->handle);
486 button = (xfFloatbarButton*)calloc(1,
sizeof(xfFloatbarButton));
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;
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;
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;
506 case XF_FLOATBAR_BUTTON_LOCKED:
507 button->x = FLOATBAR_BORDER;
508 button->onclick = xf_floatbar_button_onclick_locked;
516 button->focus = FALSE;
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);
527xfFloatbar* xf_floatbar_new(xfContext* xfc, Window window,
const char* title, DWORD flags)
530 WINPR_ASSERT(xfc->display);
534 if ((flags & 0x0001) == 0)
544 xfFloatbar* floatbar = (xfFloatbar*)calloc(1,
sizeof(xfFloatbar));
549 floatbar->title = _strdup(title);
551 if (!floatbar->title)
554 floatbar->root_window = window;
555 floatbar->flags = flags;
557 floatbar->locked = (flags & 0x0002) != 0;
558 xf_floatbar_toggle_fullscreen(floatbar, FALSE);
561 char** missingList =
nullptr;
562 int missingCount = 0;
563 char* defString =
nullptr;
564 floatbar->fontSet = XCreateFontSet(floatbar->xfc->display,
"-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
565 &missingList, &missingCount, &defString);
567 if (floatbar->fontSet ==
nullptr)
569 WLog_ERR(TAG,
"Failed to create fontset");
571 XFreeStringList(missingList);
575 WINPR_PRAGMA_DIAG_PUSH
576 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
577 xf_floatbar_free(floatbar);
578 WINPR_PRAGMA_DIAG_POP
583static unsigned long xf_floatbar_get_color(xfFloatbar* floatbar,
char* rgb_value)
587 WINPR_ASSERT(floatbar);
588 WINPR_ASSERT(floatbar->xfc);
589 WINPR_ASSERT(rgb_value);
592 for (
size_t i = 0; i < ARRAYSIZE(floatbar->colors); i++)
594 const xfFloatbarColor* entry = &floatbar->colors[i];
596 if (entry->rgb && (strcmp(entry->rgb, rgb_value) == 0))
600 Display* display = floatbar->xfc->display;
601 WINPR_ASSERT(display);
603 Colormap cmap = DefaultColormap(display, XDefaultScreen(display));
604 XParseColor(display, cmap, rgb_value, &color);
605 XAllocColor(display, cmap, &color);
607 for (
size_t i = 0; i < ARRAYSIZE(floatbar->colors); i++)
609 xfFloatbarColor* entry = &floatbar->colors[i];
613 entry->rgb = rgb_value;
614 entry->pixel = color.pixel;
623static void xf_floatbar_init_resources(xfFloatbar* floatbar)
625 WINPR_ASSERT(floatbar);
626 WINPR_ASSERT(floatbar->xfc);
627 WINPR_ASSERT(floatbar->xfc->display);
629 floatbar->cursor_arrow = XCreateFontCursor(floatbar->xfc->display, XC_arrow);
630 floatbar->cursor_resize = XCreateFontCursor(floatbar->xfc->display, XC_sb_h_double_arrow);
633static void xf_floatbar_event_expose(xfFloatbar* floatbar)
636 GC shape_gc =
nullptr;
639 XPoint shape[5] = WINPR_C_ARRAY_INIT;
640 XPoint border[5] = WINPR_C_ARRAY_INIT;
642 WINPR_ASSERT(floatbar);
643 WINPR_ASSERT(floatbar->xfc);
645 Display* display = floatbar->xfc->display;
646 WINPR_ASSERT(display);
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);
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)));
662 shape[1].x = WINPR_ASSERTING_INT_CAST(
short, floatbar->width);
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;
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;
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));
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);
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);
695 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
696 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
697 XDrawLines(display, buffer, gc, border, 5, CoordModeOrigin);
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));
703 WINPR_ASSERT(len <= INT32_MAX / 2);
704 const int fx = floatbar->width / 2 - (int)len * 2;
705 if (floatbar->fontSet !=
nullptr)
707 XmbDrawString(display, buffer, floatbar->fontSet, gc, fx, 15, floatbar->title, (
int)len);
711 XDrawString(display, buffer, gc, fx, 15, floatbar->title, (
int)len);
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);
723static xfFloatbarButton* xf_floatbar_get_button(xfFloatbar* floatbar, Window window)
725 WINPR_ASSERT(floatbar);
726 const size_t size = ARRAYSIZE(floatbar->buttons);
728 for (
size_t i = 0; i < size; i++)
730 xfFloatbarButton* button = floatbar->buttons[i];
731 if (button->handle == window)
740static void xf_floatbar_button_update_positon(xfFloatbar* floatbar)
742 xfFloatbarButton* button =
nullptr;
743 WINPR_ASSERT(floatbar);
744 xfContext* xfc = floatbar->xfc;
745 const size_t size = ARRAYSIZE(floatbar->buttons);
747 for (
size_t i = 0; i < size; i++)
749 button = floatbar->buttons[i];
751 switch (button->type)
753 case XF_FLOATBAR_BUTTON_CLOSE:
755 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
758 case XF_FLOATBAR_BUTTON_RESTORE:
760 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
763 case XF_FLOATBAR_BUTTON_MINIMIZE:
765 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
773 WINPR_ASSERT(xfc->display);
774 LogDynAndXMoveWindow(xfc->log, xfc->display, button->handle, button->x, button->y);
777 floatbar->drawn_width = floatbar->width;
778 xf_floatbar_event_expose(floatbar);
781static void xf_floatbar_button_event_expose(xfFloatbar* floatbar, Window window)
783 xfFloatbarButton* button = xf_floatbar_get_button(floatbar, window);
784 static unsigned char* bits;
787 xfContext* xfc = floatbar->xfc;
793 WINPR_ASSERT(xfc->display);
794 WINPR_ASSERT(xfc->window);
796 gc = LogDynAndXCreateGC(xfc->log, xfc->display, button->handle, 0, 0);
797 floatbar = xfc->window->floatbar;
798 WINPR_ASSERT(floatbar);
800 switch (button->type)
802 case XF_FLOATBAR_BUTTON_CLOSE:
806 case XF_FLOATBAR_BUTTON_RESTORE:
810 case XF_FLOATBAR_BUTTON_MINIMIZE:
811 bits = minimize_bits;
814 case XF_FLOATBAR_BUTTON_LOCKED:
815 if (floatbar->locked)
827 if (button->pattern_bits != bits)
830 LogDynAndXFreePixmap(xfc->log, xfc->display, button->pattern);
832 button->pattern = XCreateBitmapFromData(xfc->display, button->handle, (
const char*)bits,
833 FLOATBAR_BUTTON_WIDTH, FLOATBAR_BUTTON_WIDTH);
834 button->pattern_bits = bits;
837 pattern = button->pattern;
839 if (!(button->focus))
840 LogDynAndXSetForeground(floatbar->xfc->log, xfc->display, gc,
841 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BACKGROUND));
843 LogDynAndXSetForeground(floatbar->xfc->log, xfc->display, gc,
844 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
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);
853static void xf_floatbar_button_event_buttonpress(xfFloatbar* floatbar,
const XButtonEvent* event)
856 xfFloatbarButton* button = xf_floatbar_get_button(floatbar, event->window);
859 button->clicked = TRUE;
862static void xf_floatbar_button_event_buttonrelease(xfFloatbar* floatbar,
const XButtonEvent* event)
864 xfFloatbarButton* button =
nullptr;
866 WINPR_ASSERT(floatbar);
869 button = xf_floatbar_get_button(floatbar, event->window);
874 button->onclick(floatbar);
875 button->clicked = FALSE;
879static void xf_floatbar_event_buttonpress(xfFloatbar* floatbar,
const XButtonEvent* event)
881 WINPR_ASSERT(floatbar);
884 switch (event->button)
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;
892 floatbar->mode = XF_FLOATBAR_MODE_DRAGGING;
901static void xf_floatbar_event_buttonrelease(xfFloatbar* floatbar,
const XButtonEvent* event)
903 WINPR_ASSERT(floatbar);
906 switch (event->button)
909 floatbar->mode = XF_FLOATBAR_MODE_NONE;
917static void xf_floatbar_resize(xfFloatbar* floatbar,
const XMotionEvent* event)
923 WINPR_ASSERT(floatbar);
926 xfContext* xfc = floatbar->xfc;
928 WINPR_ASSERT(xfc->display);
931 movement =
event->x_root - floatbar->last_motion_x_root;
934 if (floatbar->mode == XF_FLOATBAR_MODE_RESIZE_LEFT)
936 x = floatbar->x + movement;
937 width = floatbar->width + movement * -1;
942 width = floatbar->width + movement;
946 if (FLOATBAR_MIN_WIDTH < width)
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));
952 floatbar->width = width;
956static void xf_floatbar_dragging(xfFloatbar* floatbar,
const XMotionEvent* event)
961 WINPR_ASSERT(floatbar);
963 xfContext* xfc = floatbar->xfc;
965 WINPR_ASSERT(xfc->window);
966 WINPR_ASSERT(xfc->display);
969 movement =
event->x_root - floatbar->last_motion_x_root;
970 x = floatbar->x + movement;
973 if (x < 0 || (x + floatbar->width) > xfc->window->width)
977 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, x, 0);
979 floatbar->last_motion_x_root = floatbar->last_motion_x_root + movement;
983static void xf_floatbar_event_motionnotify(xfFloatbar* floatbar,
const XMotionEvent* event)
988 WINPR_ASSERT(floatbar);
991 xfContext* xfc = floatbar->xfc;
993 WINPR_ASSERT(xfc->display);
995 mode = floatbar->mode;
996 cursor = floatbar->cursor_arrow;
998 if ((event->state & Button1Mask) && (mode > XF_FLOATBAR_MODE_DRAGGING))
1000 xf_floatbar_resize(floatbar, event);
1002 else if ((event->state & Button1Mask) && (mode == XF_FLOATBAR_MODE_DRAGGING))
1004 xf_floatbar_dragging(floatbar, event);
1008 if (event->x <= FLOATBAR_BORDER || event->x >= floatbar->width - FLOATBAR_BORDER)
1009 cursor = floatbar->cursor_resize;
1012 XDefineCursor(xfc->display, xfc->window->handle, cursor);
1013 floatbar->last_motion_x_root =
event->x_root;
1016static void xf_floatbar_button_event_focusin(xfFloatbar* floatbar,
const XAnyEvent* event)
1018 xfFloatbarButton* button =
nullptr;
1020 WINPR_ASSERT(floatbar);
1021 WINPR_ASSERT(event);
1023 button = xf_floatbar_get_button(floatbar, event->window);
1027 button->focus = TRUE;
1028 xf_floatbar_button_event_expose(floatbar, event->window);
1032static void xf_floatbar_button_event_focusout(xfFloatbar* floatbar,
const XAnyEvent* event)
1034 xfFloatbarButton* button =
nullptr;
1036 WINPR_ASSERT(floatbar);
1037 WINPR_ASSERT(event);
1039 button = xf_floatbar_get_button(floatbar, event->window);
1043 button->focus = FALSE;
1044 xf_floatbar_button_event_expose(floatbar, event->window);
1048static void xf_floatbar_event_focusout(xfFloatbar* floatbar)
1050 WINPR_ASSERT(floatbar);
1051 xfContext* xfc = floatbar->xfc;
1056 WINPR_ASSERT(xfc->window);
1057 WINPR_ASSERT(xfc->pointer);
1058 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
1063static void xf_floatbar_set_pointer_y(xfFloatbar* floatbar,
int y)
1065 WINPR_ASSERT(floatbar);
1066 floatbar->last_motion_y_root = y + floatbar->y;
1069BOOL xf_floatbar_check_event(xfFloatbar* floatbar,
const XEvent* event)
1071 if (!floatbar || !floatbar->xfc || !event)
1074 if (!floatbar->created)
1077 if (event->xany.window == floatbar->handle)
1080 size_t size = ARRAYSIZE(floatbar->buttons);
1082 for (
size_t i = 0; i < size; i++)
1084 const xfFloatbarButton* button = floatbar->buttons[i];
1086 if (event->xany.window == button->handle)
1093BOOL xf_floatbar_event_process(xfFloatbar* floatbar,
const XEvent* event)
1095 if (!floatbar || !floatbar->xfc || !event)
1098 if (!floatbar->created)
1101 switch (event->type)
1104 if (event->xexpose.window == floatbar->handle)
1105 xf_floatbar_event_expose(floatbar);
1107 xf_floatbar_button_event_expose(floatbar, event->xexpose.window);
1112 if (event->xany.window == floatbar->handle)
1113 xf_floatbar_set_pointer_y(floatbar, event->xmotion.y);
1115 xf_floatbar_event_motionnotify(floatbar, &event->xmotion);
1119 if (event->xany.window == floatbar->handle)
1120 xf_floatbar_event_buttonpress(floatbar, &event->xbutton);
1122 xf_floatbar_button_event_buttonpress(floatbar, &event->xbutton);
1127 if (event->xany.window == floatbar->handle)
1128 xf_floatbar_event_buttonrelease(floatbar, &event->xbutton);
1130 xf_floatbar_button_event_buttonrelease(floatbar, &event->xbutton);
1135 floatbar->entered = TRUE;
1136 if (event->xany.window == floatbar->handle)
1137 xf_floatbar_set_pointer_y(floatbar, event->xcrossing.y);
1141 if (event->xany.window != floatbar->handle)
1142 xf_floatbar_button_event_focusin(floatbar, &event->xany);
1147 floatbar->entered = FALSE;
1150 if (event->xany.window == floatbar->handle)
1151 xf_floatbar_event_focusout(floatbar);
1153 xf_floatbar_button_event_focusout(floatbar, &event->xany);
1157 case ConfigureNotify:
1159 if ((event->xany.window == floatbar->handle) &&
1160 (event->xconfigure.width != floatbar->drawn_width))
1161 xf_floatbar_button_update_positon(floatbar);
1165 case PropertyNotify:
1166 if (event->xany.window == floatbar->handle)
1167 xf_floatbar_button_update_positon(floatbar);
1175 return floatbar->handle ==
event->xany.window;
1178void xf_floatbar_button_free(xfContext* xfc, xfFloatbarButton* button)
1186 WINPR_ASSERT(xfc->display);
1187 LogDynAndXUnmapWindow(xfc->log, xfc->display, button->handle);
1188 LogDynAndXDestroyWindow(xfc->log, xfc->display, button->handle);
1191 if (button->pattern)
1192 LogDynAndXFreePixmap(xfc->log, xfc->display, button->pattern);
1197void xf_floatbar_free(xfFloatbar* floatbar)
1200 xfContext* xfc =
nullptr;
1205 free(floatbar->title);
1206 xfc = floatbar->xfc;
1209 size = ARRAYSIZE(floatbar->buttons);
1211 for (
size_t i = 0; i < size; i++)
1213 xf_floatbar_button_free(xfc, floatbar->buttons[i]);
1214 floatbar->buttons[i] =
nullptr;
1217 if (floatbar->handle)
1219 WINPR_ASSERT(xfc->display);
1220 LogDynAndXUnmapWindow(xfc->log, xfc->display, floatbar->handle);
1221 LogDynAndXDestroyWindow(xfc->log, xfc->display, floatbar->handle);
1224 if (floatbar->created)
1226 XFreeCursor(xfc->display, floatbar->cursor_arrow);
1227 XFreeCursor(xfc->display, floatbar->cursor_resize);
1233BOOL xf_floatbar_is_locked(xfFloatbar* floatbar)
1237 return floatbar->mode != XF_FLOATBAR_MODE_NONE;
1240BOOL xf_floatbar_is_window(xfFloatbar* floatbar, Window window)
1244 if (floatbar->handle == window)
1246 return floatbar->entered;
1249BOOL xfc_is_floatbar_window(xfContext* xfc, Window window)
1251 if (!xfc || !xfc->window)
1253 return xf_floatbar_is_window(xfc->window->floatbar, window);