FreeRDP
Loading...
Searching...
No Matches
xf_floatbar.c
1
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 <winpr/assert.h>
25#include <winpr/cast.h>
26
27#include "xf_floatbar.h"
28#include "xf_utils.h"
29#include "resource/close.xbm"
30#include "resource/lock.xbm"
31#include "resource/unlock.xbm"
32#include "resource/minimize.xbm"
33#include "resource/restore.xbm"
34
35#include <freerdp/log.h>
36#define TAG CLIENT_TAG("x11")
37
38#define FLOATBAR_HEIGHT 26
39#define FLOATBAR_DEFAULT_WIDTH 576
40#define FLOATBAR_MIN_WIDTH 200
41#define FLOATBAR_BORDER 24
42#define FLOATBAR_BUTTON_WIDTH 24
43#define FLOATBAR_COLOR_BACKGROUND "RGB:31/6c/a9"
44#define FLOATBAR_COLOR_BORDER "RGB:75/9a/c8"
45#define FLOATBAR_COLOR_FOREGROUND "RGB:FF/FF/FF"
46
47#define XF_FLOATBAR_MODE_NONE 0
48#define XF_FLOATBAR_MODE_DRAGGING 1
49#define XF_FLOATBAR_MODE_RESIZE_LEFT 2
50#define XF_FLOATBAR_MODE_RESIZE_RIGHT 3
51
52#define XF_FLOATBAR_BUTTON_CLOSE 1
53#define XF_FLOATBAR_BUTTON_RESTORE 2
54#define XF_FLOATBAR_BUTTON_MINIMIZE 3
55#define XF_FLOATBAR_BUTTON_LOCKED 4
56
57typedef BOOL (*OnClick)(xfFloatbar*);
58
59typedef struct
60{
61 int x;
62 int y;
63 int type;
64 bool focus;
65 bool clicked;
66 OnClick onclick;
67 Window handle;
68} xfFloatbarButton;
69
70struct xf_floatbar
71{
72 int x;
73 int y;
74 int width;
75 int height;
76 int mode;
77 int last_motion_x_root;
78 int last_motion_y_root;
79 BOOL locked;
80 xfFloatbarButton* buttons[4];
81 Window handle;
82 BOOL hasCursor;
83 xfContext* xfc;
84 DWORD flags;
85 BOOL created;
86 Window root_window;
87 char* title;
88 XFontSet fontSet;
89};
90
91static xfFloatbarButton* xf_floatbar_new_button(xfFloatbar* floatbar, int type);
92
93static BOOL xf_floatbar_button_onclick_close(xfFloatbar* floatbar)
94{
95 if (!floatbar)
96 return FALSE;
97
98 return freerdp_abort_connect_context(&floatbar->xfc->common.context);
99}
100
101static BOOL xf_floatbar_button_onclick_minimize(xfFloatbar* floatbar)
102{
103 xfContext* xfc = NULL;
104
105 if (!floatbar || !floatbar->xfc)
106 return FALSE;
107
108 xfc = floatbar->xfc;
109 xf_SetWindowMinimized(xfc, xfc->window);
110 return TRUE;
111}
112
113static BOOL xf_floatbar_button_onclick_restore(xfFloatbar* floatbar)
114{
115 if (!floatbar)
116 return FALSE;
117
118 xf_toggle_fullscreen(floatbar->xfc);
119 return TRUE;
120}
121
122static BOOL xf_floatbar_button_onclick_locked(xfFloatbar* floatbar)
123{
124 if (!floatbar)
125 return FALSE;
126
127 floatbar->locked = (floatbar->locked) ? FALSE : TRUE;
128 return xf_floatbar_hide_and_show(floatbar);
129}
130
131BOOL xf_floatbar_set_root_y(xfFloatbar* floatbar, int y)
132{
133 if (!floatbar)
134 return FALSE;
135
136 floatbar->last_motion_y_root = y;
137 return TRUE;
138}
139
140BOOL xf_floatbar_hide_and_show(xfFloatbar* floatbar)
141{
142 xfContext* xfc = NULL;
143
144 if (!floatbar || !floatbar->xfc)
145 return FALSE;
146
147 if (!floatbar->created)
148 return TRUE;
149
150 xfc = floatbar->xfc;
151 WINPR_ASSERT(xfc);
152 WINPR_ASSERT(xfc->display);
153
154 if (!floatbar->locked)
155 {
156 if ((floatbar->mode == XF_FLOATBAR_MODE_NONE) && (floatbar->last_motion_y_root > 10) &&
157 (floatbar->y > (FLOATBAR_HEIGHT * -1)))
158 {
159 floatbar->y = floatbar->y - 1;
160 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, floatbar->x,
161 floatbar->y);
162 }
163 else if (floatbar->y < 0 && (floatbar->last_motion_y_root < 10))
164 {
165 floatbar->y = floatbar->y + 1;
166 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, floatbar->x,
167 floatbar->y);
168 }
169 }
170
171 return TRUE;
172}
173
174static BOOL create_floatbar(xfFloatbar* floatbar)
175{
176 xfContext* xfc = NULL;
177 Status status = 0;
178 XWindowAttributes attr = { 0 };
179
180 WINPR_ASSERT(floatbar);
181 if (floatbar->created)
182 return TRUE;
183
184 xfc = floatbar->xfc;
185 WINPR_ASSERT(xfc);
186 WINPR_ASSERT(xfc->display);
187
188 status = XGetWindowAttributes(xfc->display, floatbar->root_window, &attr);
189 if (status == 0)
190 {
191 WLog_WARN(TAG, "XGetWindowAttributes failed");
192 return FALSE;
193 }
194 floatbar->x = attr.x + attr.width / 2 - FLOATBAR_DEFAULT_WIDTH / 2;
195 floatbar->y = 0;
196
197 if (((floatbar->flags & 0x0004) == 0) && !floatbar->locked)
198 floatbar->y = -FLOATBAR_HEIGHT + 1;
199
200 floatbar->handle = LogDynAndXCreateWindow(
201 xfc->log, xfc->display, floatbar->root_window, floatbar->x, 0, FLOATBAR_DEFAULT_WIDTH,
202 FLOATBAR_HEIGHT, 0, CopyFromParent, InputOutput, CopyFromParent, 0, NULL);
203 floatbar->width = FLOATBAR_DEFAULT_WIDTH;
204 floatbar->height = FLOATBAR_HEIGHT;
205 floatbar->mode = XF_FLOATBAR_MODE_NONE;
206 floatbar->buttons[0] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_CLOSE);
207 floatbar->buttons[1] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_RESTORE);
208 floatbar->buttons[2] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_MINIMIZE);
209 floatbar->buttons[3] = xf_floatbar_new_button(floatbar, XF_FLOATBAR_BUTTON_LOCKED);
210 XSelectInput(xfc->display, floatbar->handle,
211 ExposureMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
212 FocusChangeMask | LeaveWindowMask | EnterWindowMask | StructureNotifyMask |
213 PropertyChangeMask);
214 floatbar->created = TRUE;
215 return TRUE;
216}
217
218BOOL xf_floatbar_toggle_fullscreen(xfFloatbar* floatbar, bool fullscreen)
219{
220 int size = 0;
221 bool visible = False;
222 xfContext* xfc = NULL;
223
224 if (!floatbar || !floatbar->xfc)
225 return FALSE;
226
227 xfc = floatbar->xfc;
228 WINPR_ASSERT(xfc->display);
229
230 /* Only visible if enabled */
231 if (floatbar->flags & 0x0001)
232 {
233 /* Visible if fullscreen and flag visible in fullscreen mode */
234 visible |= ((floatbar->flags & 0x0010) != 0) && fullscreen;
235 /* Visible if window and flag visible in window mode */
236 visible |= ((floatbar->flags & 0x0020) != 0) && !fullscreen;
237 }
238
239 if (visible)
240 {
241 if (!create_floatbar(floatbar))
242 return FALSE;
243
244 LogDynAndXMapWindow(xfc->log, xfc->display, floatbar->handle);
245 size = ARRAYSIZE(floatbar->buttons);
246
247 for (int i = 0; i < size; i++)
248 {
249 xfFloatbarButton* button = floatbar->buttons[i];
250 LogDynAndXMapWindow(xfc->log, xfc->display, button->handle);
251 }
252
253 /* If default is hidden (and not sticky) don't show on fullscreen state changes */
254 if (((floatbar->flags & 0x0004) == 0) && !floatbar->locked)
255 floatbar->y = -FLOATBAR_HEIGHT + 1;
256
257 xf_floatbar_hide_and_show(floatbar);
258 }
259 else if (floatbar->created)
260 {
261 XUnmapSubwindows(xfc->display, floatbar->handle);
262 LogDynAndXUnmapWindow(xfc->log, xfc->display, floatbar->handle);
263 }
264
265 return TRUE;
266}
267
268xfFloatbarButton* xf_floatbar_new_button(xfFloatbar* floatbar, int type)
269{
270 xfFloatbarButton* button = NULL;
271
272 WINPR_ASSERT(floatbar);
273 WINPR_ASSERT(floatbar->xfc);
274 WINPR_ASSERT(floatbar->xfc->display);
275 WINPR_ASSERT(floatbar->handle);
276
277 button = (xfFloatbarButton*)calloc(1, sizeof(xfFloatbarButton));
278 button->type = type;
279
280 switch (type)
281 {
282 case XF_FLOATBAR_BUTTON_CLOSE:
283 button->x = floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * type;
284 button->onclick = xf_floatbar_button_onclick_close;
285 break;
286
287 case XF_FLOATBAR_BUTTON_RESTORE:
288 button->x = floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * type;
289 button->onclick = xf_floatbar_button_onclick_restore;
290 break;
291
292 case XF_FLOATBAR_BUTTON_MINIMIZE:
293 button->x = floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * type;
294 button->onclick = xf_floatbar_button_onclick_minimize;
295 break;
296
297 case XF_FLOATBAR_BUTTON_LOCKED:
298 button->x = FLOATBAR_BORDER;
299 button->onclick = xf_floatbar_button_onclick_locked;
300 break;
301
302 default:
303 break;
304 }
305
306 button->y = 0;
307 button->focus = FALSE;
308 button->handle =
309 LogDynAndXCreateWindow(floatbar->xfc->log, floatbar->xfc->display, floatbar->handle,
310 button->x, 0, FLOATBAR_BUTTON_WIDTH, FLOATBAR_BUTTON_WIDTH, 0,
311 CopyFromParent, InputOutput, CopyFromParent, 0, NULL);
312 XSelectInput(floatbar->xfc->display, button->handle,
313 ExposureMask | ButtonPressMask | ButtonReleaseMask | FocusChangeMask |
314 LeaveWindowMask | EnterWindowMask | StructureNotifyMask);
315 return button;
316}
317
318xfFloatbar* xf_floatbar_new(xfContext* xfc, Window window, const char* name, DWORD flags)
319{
320 WINPR_ASSERT(xfc);
321 WINPR_ASSERT(xfc->display);
322 WINPR_ASSERT(name);
323
324 /* Floatbar not enabled */
325 if ((flags & 0x0001) == 0)
326 return NULL;
327
328 if (!xfc)
329 return NULL;
330
331 /* Force disable with remote app */
332 if (xfc->remote_app)
333 return NULL;
334
335 xfFloatbar* floatbar = (xfFloatbar*)calloc(1, sizeof(xfFloatbar));
336
337 if (!floatbar)
338 return NULL;
339
340 floatbar->title = _strdup(name);
341
342 if (!floatbar->title)
343 goto fail;
344
345 floatbar->root_window = window;
346 floatbar->flags = flags;
347 floatbar->xfc = xfc;
348 floatbar->locked = (flags & 0x0002) != 0 ? TRUE : FALSE;
349 xf_floatbar_toggle_fullscreen(floatbar, FALSE);
350 char** missingList = NULL;
351 int missingCount = 0;
352 char* defString = NULL;
353 floatbar->fontSet = XCreateFontSet(floatbar->xfc->display, "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
354 &missingList, &missingCount, &defString);
355 if (floatbar->fontSet == NULL)
356 {
357 WLog_ERR(TAG, "Failed to create fontset");
358 }
359 XFreeStringList(missingList);
360 return floatbar;
361fail:
362 WINPR_PRAGMA_DIAG_PUSH
363 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
364 xf_floatbar_free(floatbar);
365 WINPR_PRAGMA_DIAG_POP
366 return NULL;
367}
368
369static unsigned long xf_floatbar_get_color(xfFloatbar* floatbar, char* rgb_value)
370{
371 XColor color;
372
373 WINPR_ASSERT(floatbar);
374 WINPR_ASSERT(floatbar->xfc);
375
376 Display* display = floatbar->xfc->display;
377 WINPR_ASSERT(display);
378
379 Colormap cmap = DefaultColormap(display, XDefaultScreen(display));
380 XParseColor(display, cmap, rgb_value, &color);
381 XAllocColor(display, cmap, &color);
382 return color.pixel;
383}
384
385static void xf_floatbar_event_expose(xfFloatbar* floatbar)
386{
387 GC gc = NULL;
388 GC shape_gc = NULL;
389 Pixmap pmap = 0;
390 XPoint shape[5] = { 0 };
391 XPoint border[5] = { 0 };
392
393 WINPR_ASSERT(floatbar);
394 WINPR_ASSERT(floatbar->xfc);
395
396 Display* display = floatbar->xfc->display;
397 WINPR_ASSERT(display);
398
399 /* create the pixmap that we'll use for shaping the window */
400 pmap = LogDynAndXCreatePixmap(floatbar->xfc->log, display, floatbar->handle,
401 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->width),
402 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height), 1);
403 gc = LogDynAndXCreateGC(floatbar->xfc->log, display, floatbar->handle, 0, 0);
404 shape_gc = LogDynAndXCreateGC(floatbar->xfc->log, display, pmap, 0, 0);
405 /* points for drawing the floatbar */
406 shape[0].x = 0;
407 shape[0].y = 0;
408 shape[1].x = WINPR_ASSERTING_INT_CAST(short, floatbar->width);
409 shape[1].y = 0;
410 shape[2].x = WINPR_ASSERTING_INT_CAST(short, shape[1].x - FLOATBAR_BORDER);
411 shape[2].y = FLOATBAR_HEIGHT;
412 shape[3].x = WINPR_ASSERTING_INT_CAST(short, shape[0].x + FLOATBAR_BORDER);
413 shape[3].y = FLOATBAR_HEIGHT;
414 shape[4].x = shape[0].x;
415 shape[4].y = shape[0].y;
416 /* points for drawing the border of the floatbar */
417 border[0].x = shape[0].x;
418 border[0].y = WINPR_ASSERTING_INT_CAST(short, shape[0].y - 1);
419 border[1].x = WINPR_ASSERTING_INT_CAST(short, shape[1].x - 1);
420 border[1].y = WINPR_ASSERTING_INT_CAST(short, shape[1].y - 1);
421 border[2].x = shape[2].x;
422 border[2].y = WINPR_ASSERTING_INT_CAST(short, shape[2].y - 1);
423 border[3].x = WINPR_ASSERTING_INT_CAST(short, shape[3].x - 1);
424 border[3].y = WINPR_ASSERTING_INT_CAST(short, shape[3].y - 1);
425 border[4].x = border[0].x;
426 border[4].y = border[0].y;
427 /* Fill all pixels with 0 */
428 LogDynAndXSetForeground(floatbar->xfc->log, display, shape_gc, 0);
429 LogDynAndXFillRectangle(floatbar->xfc->log, display, pmap, shape_gc, 0, 0,
430 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->width),
431 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height));
432 /* Fill all pixels which should be shown with 1 */
433 LogDynAndXSetForeground(floatbar->xfc->log, display, shape_gc, 1);
434 XFillPolygon(display, pmap, shape_gc, shape, 5, 0, CoordModeOrigin);
435 XShapeCombineMask(display, floatbar->handle, ShapeBounding, 0, 0, pmap, ShapeSet);
436 /* draw the float bar */
437 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
438 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BACKGROUND));
439 XFillPolygon(display, floatbar->handle, gc, shape, 4, 0, CoordModeOrigin);
440 /* draw an border for the floatbar */
441 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
442 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
443 XDrawLines(display, floatbar->handle, gc, border, 5, CoordModeOrigin);
444 /* draw the host name connected to (limit to maximum file name) */
445 const size_t len = strnlen(floatbar->title, MAX_PATH);
446 LogDynAndXSetForeground(floatbar->xfc->log, display, gc,
447 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_FOREGROUND));
448
449 WINPR_ASSERT(len <= INT32_MAX / 2);
450 const int fx = floatbar->width / 2 - (int)len * 2;
451 if (floatbar->fontSet != NULL)
452 {
453 XmbDrawString(display, floatbar->handle, floatbar->fontSet, gc, fx, 15, floatbar->title,
454 (int)len);
455 }
456 else
457 {
458 XDrawString(display, floatbar->handle, gc, fx, 15, floatbar->title, (int)len);
459 }
460 LogDynAndXFreeGC(floatbar->xfc->log, display, gc);
461 LogDynAndXFreeGC(floatbar->xfc->log, display, shape_gc);
462}
463
464static xfFloatbarButton* xf_floatbar_get_button(xfFloatbar* floatbar, Window window)
465{
466 WINPR_ASSERT(floatbar);
467 const size_t size = ARRAYSIZE(floatbar->buttons);
468
469 for (size_t i = 0; i < size; i++)
470 {
471 xfFloatbarButton* button = floatbar->buttons[i];
472 if (button->handle == window)
473 {
474 return button;
475 }
476 }
477
478 return NULL;
479}
480
481static void xf_floatbar_button_update_positon(xfFloatbar* floatbar)
482{
483 xfFloatbarButton* button = NULL;
484 WINPR_ASSERT(floatbar);
485 xfContext* xfc = floatbar->xfc;
486 const size_t size = ARRAYSIZE(floatbar->buttons);
487
488 for (size_t i = 0; i < size; i++)
489 {
490 button = floatbar->buttons[i];
491
492 switch (button->type)
493 {
494 case XF_FLOATBAR_BUTTON_CLOSE:
495 button->x =
496 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
497 break;
498
499 case XF_FLOATBAR_BUTTON_RESTORE:
500 button->x =
501 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
502 break;
503
504 case XF_FLOATBAR_BUTTON_MINIMIZE:
505 button->x =
506 floatbar->width - FLOATBAR_BORDER - FLOATBAR_BUTTON_WIDTH * button->type;
507 break;
508
509 default:
510 break;
511 }
512
513 WINPR_ASSERT(xfc);
514 WINPR_ASSERT(xfc->display);
515 LogDynAndXMoveWindow(xfc->log, xfc->display, button->handle, button->x, button->y);
516 xf_floatbar_event_expose(floatbar);
517 }
518}
519
520static void xf_floatbar_button_event_expose(xfFloatbar* floatbar, Window window)
521{
522 xfFloatbarButton* button = xf_floatbar_get_button(floatbar, window);
523 static unsigned char* bits;
524 GC gc = NULL;
525 Pixmap pattern = 0;
526 xfContext* xfc = floatbar->xfc;
527
528 if (!button)
529 return;
530
531 WINPR_ASSERT(xfc);
532 WINPR_ASSERT(xfc->display);
533 WINPR_ASSERT(xfc->window);
534
535 gc = LogDynAndXCreateGC(xfc->log, xfc->display, button->handle, 0, 0);
536 floatbar = xfc->window->floatbar;
537 WINPR_ASSERT(floatbar);
538
539 switch (button->type)
540 {
541 case XF_FLOATBAR_BUTTON_CLOSE:
542 bits = close_bits;
543 break;
544
545 case XF_FLOATBAR_BUTTON_RESTORE:
546 bits = restore_bits;
547 break;
548
549 case XF_FLOATBAR_BUTTON_MINIMIZE:
550 bits = minimize_bits;
551 break;
552
553 case XF_FLOATBAR_BUTTON_LOCKED:
554 if (floatbar->locked)
555 bits = lock_bits;
556 else
557 bits = unlock_bits;
558
559 break;
560
561 default:
562 break;
563 }
564
565 pattern = XCreateBitmapFromData(xfc->display, button->handle, (const char*)bits,
566 FLOATBAR_BUTTON_WIDTH, FLOATBAR_BUTTON_WIDTH);
567
568 if (!(button->focus))
569 LogDynAndXSetForeground(floatbar->xfc->log, xfc->display, gc,
570 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BACKGROUND));
571 else
572 LogDynAndXSetForeground(floatbar->xfc->log, xfc->display, gc,
573 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_BORDER));
574
575 LogDynAndXSetBackground(xfc->log, xfc->display, gc,
576 xf_floatbar_get_color(floatbar, FLOATBAR_COLOR_FOREGROUND));
577 XCopyPlane(xfc->display, pattern, button->handle, gc, 0, 0, FLOATBAR_BUTTON_WIDTH,
578 FLOATBAR_BUTTON_WIDTH, 0, 0, 1);
579 LogDynAndXFreePixmap(xfc->log, xfc->display, pattern);
580 LogDynAndXFreeGC(xfc->log, xfc->display, gc);
581}
582
583static void xf_floatbar_button_event_buttonpress(xfFloatbar* floatbar, const XButtonEvent* event)
584{
585 WINPR_ASSERT(event);
586 xfFloatbarButton* button = xf_floatbar_get_button(floatbar, event->window);
587
588 if (button)
589 button->clicked = TRUE;
590}
591
592static void xf_floatbar_button_event_buttonrelease(xfFloatbar* floatbar, const XButtonEvent* event)
593{
594 xfFloatbarButton* button = NULL;
595
596 WINPR_ASSERT(floatbar);
597 WINPR_ASSERT(event);
598
599 button = xf_floatbar_get_button(floatbar, event->window);
600
601 if (button)
602 {
603 if (button->clicked)
604 button->onclick(floatbar);
605 button->clicked = FALSE;
606 }
607}
608
609static void xf_floatbar_event_buttonpress(xfFloatbar* floatbar, const XButtonEvent* event)
610{
611 WINPR_ASSERT(floatbar);
612 WINPR_ASSERT(event);
613
614 switch (event->button)
615 {
616 case Button1:
617 if (event->x <= FLOATBAR_BORDER)
618 floatbar->mode = XF_FLOATBAR_MODE_RESIZE_LEFT;
619 else if (event->x >= (floatbar->width - FLOATBAR_BORDER))
620 floatbar->mode = XF_FLOATBAR_MODE_RESIZE_RIGHT;
621 else
622 floatbar->mode = XF_FLOATBAR_MODE_DRAGGING;
623
624 break;
625
626 default:
627 break;
628 }
629}
630
631static void xf_floatbar_event_buttonrelease(xfFloatbar* floatbar, const XButtonEvent* event)
632{
633 WINPR_ASSERT(floatbar);
634 WINPR_ASSERT(event);
635
636 switch (event->button)
637 {
638 case Button1:
639 floatbar->mode = XF_FLOATBAR_MODE_NONE;
640 break;
641
642 default:
643 break;
644 }
645}
646
647static void xf_floatbar_resize(xfFloatbar* floatbar, const XMotionEvent* event)
648{
649 int x = 0;
650 int width = 0;
651 int movement = 0;
652
653 WINPR_ASSERT(floatbar);
654 WINPR_ASSERT(event);
655
656 xfContext* xfc = floatbar->xfc;
657 WINPR_ASSERT(xfc);
658 WINPR_ASSERT(xfc->display);
659
660 /* calculate movement which happened on the root window */
661 movement = event->x_root - floatbar->last_motion_x_root;
662
663 /* set x and width depending if movement happens on the left or right */
664 if (floatbar->mode == XF_FLOATBAR_MODE_RESIZE_LEFT)
665 {
666 x = floatbar->x + movement;
667 width = floatbar->width + movement * -1;
668 }
669 else
670 {
671 x = floatbar->x;
672 width = floatbar->width + movement;
673 }
674
675 /* only resize and move window if still above minimum width */
676 if (FLOATBAR_MIN_WIDTH < width)
677 {
678 LogDynAndXMoveResizeWindow(xfc->log, xfc->display, floatbar->handle, x, 0,
679 WINPR_ASSERTING_INT_CAST(uint32_t, width),
680 WINPR_ASSERTING_INT_CAST(uint32_t, floatbar->height));
681 floatbar->x = x;
682 floatbar->width = width;
683 }
684}
685
686static void xf_floatbar_dragging(xfFloatbar* floatbar, const XMotionEvent* event)
687{
688 int x = 0;
689 int movement = 0;
690
691 WINPR_ASSERT(floatbar);
692 WINPR_ASSERT(event);
693 xfContext* xfc = floatbar->xfc;
694 WINPR_ASSERT(xfc);
695 WINPR_ASSERT(xfc->window);
696 WINPR_ASSERT(xfc->display);
697
698 /* calculate movement and new x position */
699 movement = event->x_root - floatbar->last_motion_x_root;
700 x = floatbar->x + movement;
701
702 /* do nothing if floatbar would be moved out of the window */
703 if (x < 0 || (x + floatbar->width) > xfc->window->width)
704 return;
705
706 /* move window to new x position */
707 LogDynAndXMoveWindow(xfc->log, xfc->display, floatbar->handle, x, 0);
708 /* update struct values for the next event */
709 floatbar->last_motion_x_root = floatbar->last_motion_x_root + movement;
710 floatbar->x = x;
711}
712
713static void xf_floatbar_event_motionnotify(xfFloatbar* floatbar, const XMotionEvent* event)
714{
715 int mode = 0;
716 Cursor cursor = 0;
717
718 WINPR_ASSERT(floatbar);
719 WINPR_ASSERT(event);
720
721 xfContext* xfc = floatbar->xfc;
722 WINPR_ASSERT(xfc);
723 WINPR_ASSERT(xfc->display);
724
725 mode = floatbar->mode;
726 cursor = XCreateFontCursor(xfc->display, XC_arrow);
727
728 if ((event->state & Button1Mask) && (mode > XF_FLOATBAR_MODE_DRAGGING))
729 {
730 xf_floatbar_resize(floatbar, event);
731 }
732 else if ((event->state & Button1Mask) && (mode == XF_FLOATBAR_MODE_DRAGGING))
733 {
734 xf_floatbar_dragging(floatbar, event);
735 }
736 else
737 {
738 if (event->x <= FLOATBAR_BORDER || event->x >= floatbar->width - FLOATBAR_BORDER)
739 cursor = XCreateFontCursor(xfc->display, XC_sb_h_double_arrow);
740 }
741
742 XDefineCursor(xfc->display, xfc->window->handle, cursor);
743 XFreeCursor(xfc->display, cursor);
744 floatbar->last_motion_x_root = event->x_root;
745}
746
747static void xf_floatbar_button_event_focusin(xfFloatbar* floatbar, const XAnyEvent* event)
748{
749 xfFloatbarButton* button = NULL;
750
751 WINPR_ASSERT(floatbar);
752 WINPR_ASSERT(event);
753
754 button = xf_floatbar_get_button(floatbar, event->window);
755
756 if (button)
757 {
758 button->focus = TRUE;
759 xf_floatbar_button_event_expose(floatbar, event->window);
760 }
761}
762
763static void xf_floatbar_button_event_focusout(xfFloatbar* floatbar, const XAnyEvent* event)
764{
765 xfFloatbarButton* button = NULL;
766
767 WINPR_ASSERT(floatbar);
768 WINPR_ASSERT(event);
769
770 button = xf_floatbar_get_button(floatbar, event->window);
771
772 if (button)
773 {
774 button->focus = FALSE;
775 xf_floatbar_button_event_expose(floatbar, event->window);
776 }
777}
778
779static void xf_floatbar_event_focusout(xfFloatbar* floatbar)
780{
781 WINPR_ASSERT(floatbar);
782 xfContext* xfc = floatbar->xfc;
783 WINPR_ASSERT(xfc);
784
785 if (xfc->pointer)
786 {
787 WINPR_ASSERT(xfc->window);
788 WINPR_ASSERT(xfc->pointer);
789 XDefineCursor(xfc->display, xfc->window->handle, xfc->pointer->cursor);
790 }
791}
792
793BOOL xf_floatbar_check_event(xfFloatbar* floatbar, const XEvent* event)
794{
795 if (!floatbar || !floatbar->xfc || !event)
796 return FALSE;
797
798 if (!floatbar->created)
799 return FALSE;
800
801 if (event->xany.window == floatbar->handle)
802 return TRUE;
803
804 size_t size = ARRAYSIZE(floatbar->buttons);
805
806 for (size_t i = 0; i < size; i++)
807 {
808 const xfFloatbarButton* button = floatbar->buttons[i];
809
810 if (event->xany.window == button->handle)
811 return TRUE;
812 }
813
814 return FALSE;
815}
816
817BOOL xf_floatbar_event_process(xfFloatbar* floatbar, const XEvent* event)
818{
819 if (!floatbar || !floatbar->xfc || !event)
820 return FALSE;
821
822 if (!floatbar->created)
823 return FALSE;
824
825 switch (event->type)
826 {
827 case Expose:
828 if (event->xexpose.window == floatbar->handle)
829 xf_floatbar_event_expose(floatbar);
830 else
831 xf_floatbar_button_event_expose(floatbar, event->xexpose.window);
832
833 break;
834
835 case MotionNotify:
836 xf_floatbar_event_motionnotify(floatbar, &event->xmotion);
837 break;
838
839 case ButtonPress:
840 if (event->xany.window == floatbar->handle)
841 xf_floatbar_event_buttonpress(floatbar, &event->xbutton);
842 else
843 xf_floatbar_button_event_buttonpress(floatbar, &event->xbutton);
844
845 break;
846
847 case ButtonRelease:
848 if (event->xany.window == floatbar->handle)
849 xf_floatbar_event_buttonrelease(floatbar, &event->xbutton);
850 else
851 xf_floatbar_button_event_buttonrelease(floatbar, &event->xbutton);
852
853 break;
854
855 case EnterNotify:
856 case FocusIn:
857 if (event->xany.window != floatbar->handle)
858 xf_floatbar_button_event_focusin(floatbar, &event->xany);
859
860 break;
861
862 case LeaveNotify:
863 case FocusOut:
864 if (event->xany.window == floatbar->handle)
865 xf_floatbar_event_focusout(floatbar);
866 else
867 xf_floatbar_button_event_focusout(floatbar, &event->xany);
868
869 break;
870
871 case ConfigureNotify:
872 if (event->xany.window == floatbar->handle)
873 xf_floatbar_button_update_positon(floatbar);
874
875 break;
876
877 case PropertyNotify:
878 if (event->xany.window == floatbar->handle)
879 xf_floatbar_button_update_positon(floatbar);
880
881 break;
882
883 default:
884 break;
885 }
886
887 return floatbar->handle == event->xany.window;
888}
889
890static void xf_floatbar_button_free(xfContext* xfc, xfFloatbarButton* button)
891{
892 if (!button)
893 return;
894
895 if (button->handle)
896 {
897 WINPR_ASSERT(xfc);
898 WINPR_ASSERT(xfc->display);
899 LogDynAndXUnmapWindow(xfc->log, xfc->display, button->handle);
900 LogDynAndXDestroyWindow(xfc->log, xfc->display, button->handle);
901 }
902
903 free(button);
904}
905
906void xf_floatbar_free(xfFloatbar* floatbar)
907{
908 size_t size = 0;
909 xfContext* xfc = NULL;
910
911 if (!floatbar)
912 return;
913
914 free(floatbar->title);
915 xfc = floatbar->xfc;
916 WINPR_ASSERT(xfc);
917
918 size = ARRAYSIZE(floatbar->buttons);
919
920 for (size_t i = 0; i < size; i++)
921 {
922 xf_floatbar_button_free(xfc, floatbar->buttons[i]);
923 floatbar->buttons[i] = NULL;
924 }
925
926 if (floatbar->handle)
927 {
928 WINPR_ASSERT(xfc->display);
929 LogDynAndXUnmapWindow(xfc->log, xfc->display, floatbar->handle);
930 LogDynAndXDestroyWindow(xfc->log, xfc->display, floatbar->handle);
931 }
932
933 free(floatbar);
934}
935
936BOOL xf_floatbar_is_locked(xfFloatbar* floatbar)
937{
938 if (!floatbar)
939 return FALSE;
940 return floatbar->mode != XF_FLOATBAR_MODE_NONE;
941}