panel.c 40.7 KB
Newer Older
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
1 2 3
/**************************************************************************
*
* Copyright (C) 2008 Pål Staurland (staura@gmail.com)
4
* Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**************************************************************************/

19
#include <glib.h>
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
20
#include <stdio.h>
21
#include <stdlib.h>
22
#include <string.h>
23 24
#include <sys/types.h>
#include <unistd.h>
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
25 26 27 28 29 30 31 32
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <pango/pangocairo.h>

#include "server.h"
33
#include "config.h"
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
34 35 36
#include "window.h"
#include "task.h"
#include "panel.h"
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
37
#include "tooltip.h"
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
38

39 40
void panel_clear_background(void *obj);

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
MouseAction mouse_left;
MouseAction mouse_middle;
MouseAction mouse_right;
MouseAction mouse_scroll_up;
MouseAction mouse_scroll_down;
MouseAction mouse_tilt_left;
MouseAction mouse_tilt_right;

TaskbarMode taskbar_mode;
gboolean wm_menu;
gboolean panel_dock;
Layer panel_layer;
PanelPosition panel_position;
gboolean panel_horizontal;
gboolean panel_refresh;
gboolean task_dragged;
57
char *panel_window_name = NULL;
58
gboolean debug_geometry;
o9000's avatar
o9000 committed
59
gboolean debug_gradients;
60
gboolean startup_notifications;
61

62
gboolean panel_autohide;
63 64 65
int panel_autohide_show_timeout;
int panel_autohide_hide_timeout;
int panel_autohide_height;
66
gboolean panel_shrink;
67
Strut panel_strut_policy;
68
char *panel_items_order;
Andreas.Fink85's avatar
Andreas.Fink85 committed
69

70
int max_tick_urgent;
71

72 73 74
// panel's initial config
Panel panel_config;
// panels (one panel per monitor)
75 76
Panel *panels;
int num_panels;
77

78
GArray *backgrounds;
o9000's avatar
o9000 committed
79
GArray *gradients;
80

81
Imlib_Image default_icon;
82
char *default_font = NULL;
83

84 85
void default_panel()
{
o9000's avatar
o9000 committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    panels = NULL;
    num_panels = 0;
    default_icon = NULL;
    task_dragged = FALSE;
    panel_horizontal = TRUE;
    panel_position = CENTER;
    panel_items_order = NULL;
    panel_autohide = FALSE;
    panel_autohide_show_timeout = 0;
    panel_autohide_hide_timeout = 0;
    panel_autohide_height = 5; // for vertical panels this is of course the width
    panel_shrink = FALSE;
    panel_strut_policy = STRUT_FOLLOW_SIZE;
    panel_dock = FALSE;         // default not in the dock
    panel_layer = BOTTOM_LAYER; // default is bottom layer
    panel_window_name = strdup("tint2");
    wm_menu = FALSE;
    max_tick_urgent = 14;
    mouse_left = TOGGLE_ICONIFY;
    backgrounds = g_array_new(0, 0, sizeof(Background));
    gradients = g_array_new(0, 0, sizeof(GradientClass));

    memset(&panel_config, 0, sizeof(Panel));
    snprintf(panel_config.area.name, sizeof(panel_config.area.name), "Panel");
    panel_config.mouse_over_alpha = 100;
    panel_config.mouse_over_saturation = 0;
    panel_config.mouse_over_brightness = 10;
    panel_config.mouse_pressed_alpha = 100;
    panel_config.mouse_pressed_saturation = 0;
    panel_config.mouse_pressed_brightness = 0;
    panel_config.mouse_effects = 1;

    // First background is always fully transparent
    Background transparent_bg;
    init_background(&transparent_bg);
    g_array_append_val(backgrounds, transparent_bg);
    GradientClass transparent_gradient;
    init_gradient(&transparent_gradient, GRADIENT_VERTICAL);
    g_array_append_val(gradients, transparent_gradient);
125 126 127 128
}

void cleanup_panel()
{
o9000's avatar
o9000 committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    if (!panels)
        return;

    cleanup_taskbar();

    for (int i = 0; i < num_panels; i++) {
        Panel *p = &panels[i];

        free_area(&p->area);
        if (p->temp_pmap)
            XFreePixmap(server.display, p->temp_pmap);
        p->temp_pmap = 0;
        if (p->hidden_pixmap)
            XFreePixmap(server.display, p->hidden_pixmap);
        p->hidden_pixmap = 0;
        if (p->main_win)
            XDestroyWindow(server.display, p->main_win);
        p->main_win = 0;
        stop_timeout(p->autohide_timeout);
        cleanup_freespace(p);
    }

151 152
    free_icon_themes();

o9000's avatar
o9000 committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    free(panel_items_order);
    panel_items_order = NULL;
    free(panel_window_name);
    panel_window_name = NULL;
    free(panels);
    panels = NULL;

    free_area(&panel_config.area);

    g_array_free(backgrounds, TRUE);
    backgrounds = NULL;
    if (gradients) {
        for (guint i = 0; i < gradients->len; i++)
            cleanup_gradient(&g_array_index(gradients, GradientClass, i));
        g_array_free(gradients, TRUE);
    }
    gradients = NULL;
    pango_font_description_free(panel_config.g_task.font_desc);
    panel_config.g_task.font_desc = NULL;
    pango_font_description_free(panel_config.taskbarname_font_desc);
    panel_config.taskbarname_font_desc = NULL;
174 175
}

176
void init_panel()
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
177
{
o9000's avatar
o9000 committed
178 179 180 181 182
    if (panel_config.monitor > (server.num_monitors - 1)) {
        // server.num_monitors minimum value is 1 (see get_monitors())
        fprintf(stderr, "warning : monitor not found. tint2 default to all monitors.\n");
        panel_config.monitor = 0;
    }
183

o9000's avatar
o9000 committed
184
    fprintf(stderr, "panel items: %s\n", panel_items_order);
185

o9000's avatar
o9000 committed
186
    icon_theme_wrapper = NULL;
o9000's avatar
o9000 committed
187

o9000's avatar
o9000 committed
188 189 190 191
    init_tooltip();
    init_systray();
    init_launcher();
    init_clock();
192
#ifdef ENABLE_BATTERY
o9000's avatar
o9000 committed
193
    init_battery();
194
#endif
o9000's avatar
o9000 committed
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    init_taskbar();
    init_separator();
    init_execp();
    init_button();

    // number of panels (one monitor or 'all' monitors)
    if (panel_config.monitor >= 0)
        num_panels = 1;
    else
        num_panels = server.num_monitors;

    panels = calloc(num_panels, sizeof(Panel));
    for (int i = 0; i < num_panels; i++) {
        memcpy(&panels[i], &panel_config, sizeof(Panel));
    }

    fprintf(stderr,
212
            "tint2 : nb monitors %d, nb monitors used %d, nb desktops %d\n",
o9000's avatar
o9000 committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
            server.num_monitors,
            num_panels,
            server.num_desktops);
    for (int i = 0; i < num_panels; i++) {
        Panel *p = &panels[i];

        if (panel_config.monitor < 0)
            p->monitor = i;
        if (!p->area.bg)
            p->area.bg = &g_array_index(backgrounds, Background, 0);
        p->area.parent = p;
        p->area.panel = p;
        snprintf(p->area.name, sizeof(p->area.name), "Panel %d", i);
        p->area.on_screen = TRUE;
        p->area.resize_needed = 1;
        p->area.size_mode = LAYOUT_DYNAMIC;
        p->area._resize = resize_panel;
        p->area._clear = panel_clear_background;
        p->separator_list = NULL;
        init_panel_size_and_position(p);
        instantiate_area_gradients(&p->area);
        // add children according to panel_items
        for (int k = 0; k < strlen(panel_items_order); k++) {
            if (panel_items_order[k] == 'L')
                init_launcher_panel(p);
            if (panel_items_order[k] == 'T')
                init_taskbar_panel(p);
240
#ifdef ENABLE_BATTERY
o9000's avatar
o9000 committed
241 242
            if (panel_items_order[k] == 'B')
                init_battery_panel(p);
243
#endif
o9000's avatar
o9000 committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
            if (panel_items_order[k] == 'S' && systray_on_monitor(i, num_panels)) {
                init_systray_panel(p);
                refresh_systray = 1;
            }
            if (panel_items_order[k] == 'C')
                init_clock_panel(p);
            if (panel_items_order[k] == 'F' && !strstr(panel_items_order, "T"))
                init_freespace_panel(p);
            if (panel_items_order[k] == ':')
                init_separator_panel(p);
            if (panel_items_order[k] == 'E')
                init_execp_panel(p);
            if (panel_items_order[k] == 'P')
                init_button_panel(p);
        }
        set_panel_items_order(p);

        // catch some events
        XSetWindowAttributes att = {.colormap = server.colormap, .background_pixel = 0, .border_pixel = 0};
        unsigned long mask = CWEventMask | CWColormap | CWBackPixel | CWBorderPixel;
        p->main_win = XCreateWindow(server.display,
                                    server.root_win,
                                    p->posx,
                                    p->posy,
                                    p->area.width,
                                    p->area.height,
                                    0,
                                    server.depth,
                                    InputOutput,
                                    server.visual,
                                    mask,
                                    &att);

        long event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PropertyChangeMask;
        if (p->mouse_effects || p->g_task.tooltip_enabled || p->clock.area._get_tooltip_text ||
            (launcher_enabled && launcher_tooltip_enabled))
            event_mask |= PointerMotionMask | LeaveWindowMask;
        if (panel_autohide)
            event_mask |= LeaveWindowMask | EnterWindowMask;
        XChangeWindowAttributes(server.display,
                                p->main_win,
                                CWEventMask,
                                &(XSetWindowAttributes){.event_mask = event_mask});

        if (!server.gc) {
            XGCValues gcv;
            server.gc = XCreateGC(server.display, p->main_win, 0, &gcv);
        }
292
        // fprintf(stderr, "panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
o9000's avatar
o9000 committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306
        set_panel_properties(p);
        set_panel_background(p);
        if (!snapshot_path) {
            // if we are not in 'snapshot' mode then map new panel
            XMapWindow(server.display, p->main_win);
        }

        if (panel_autohide)
            autohide_trigger_hide(p);
    }

    taskbar_refresh_tasklist();
    reset_active_task();
    update_all_taskbars_visibility();
307 308
}

309
void panel_compute_size(Panel *panel)
310
{
o9000's avatar
o9000 committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324
    if (panel_horizontal) {
        if (panel->area.width == 0) {
            panel->fractional_width = TRUE;
            panel->area.width = 100;
        }
        if (panel->area.height == 0) {
            panel->fractional_height = FALSE;
            panel->area.height = 32;
        }
        if (panel->fractional_width)
            panel->area.width = (server.monitors[panel->monitor].width - panel->marginx) * panel->area.width / 100;
        if (panel->fractional_height)
            panel->area.height = (server.monitors[panel->monitor].height - panel->marginy) * panel->area.height / 100;
        if (panel->area.bg->border.radius > panel->area.height / 2) {
325
            fprintf(stderr, "panel_background_id rounded is too big... please fix your tint2rc\n");
o9000's avatar
o9000 committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
            g_array_append_val(backgrounds, *panel->area.bg);
            panel->area.bg = &g_array_index(backgrounds, Background, backgrounds->len - 1);
            panel->area.bg->border.radius = panel->area.height / 2;
        }
    } else {
        if (panel->area.height == 0) {
            panel->fractional_height = TRUE;
            panel->area.height = 100;
        }
        if (panel->area.width == 0) {
            panel->fractional_width = FALSE;
            panel->area.width = 140;
        }
        int old_panel_height = panel->area.height;
        if (panel->fractional_width)
            panel->area.height = (server.monitors[panel->monitor].height - panel->marginy) * panel->area.width / 100;
        else
            panel->area.height = panel->area.width;

        if (panel->fractional_height)
            panel->area.width = (server.monitors[panel->monitor].width - panel->marginx) * old_panel_height / 100;
        else
            panel->area.width = old_panel_height;

        if (panel->area.bg->border.radius > panel->area.width / 2) {
351
            fprintf(stderr, "panel_background_id rounded is too big... please fix your tint2rc\n");
o9000's avatar
o9000 committed
352 353 354 355 356 357 358 359 360 361 362 363
            g_array_append_val(backgrounds, *panel->area.bg);
            panel->area.bg = &g_array_index(backgrounds, Background, backgrounds->len - 1);
            panel->area.bg->border.radius = panel->area.width / 2;
        }
    }

    if (panel->area.width + panel->marginx > server.monitors[panel->monitor].width)
        panel->area.width = server.monitors[panel->monitor].width - panel->marginx;
    if (panel->area.height + panel->marginy > server.monitors[panel->monitor].height)
        panel->area.height = server.monitors[panel->monitor].height - panel->marginy;

    panel->max_size = panel_horizontal ? panel->area.width : panel->area.height;
364 365 366 367
}

void panel_compute_position(Panel *panel)
{
o9000's avatar
o9000 committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    // panel position determined here
    if (panel_position & LEFT) {
        panel->posx = server.monitors[panel->monitor].x + panel->marginx;
    } else {
        if (panel_position & RIGHT) {
            panel->posx = server.monitors[panel->monitor].x + server.monitors[panel->monitor].width -
                          panel->area.width - panel->marginx;
        } else {
            if (panel_horizontal)
                panel->posx = server.monitors[panel->monitor].x +
                              ((server.monitors[panel->monitor].width - panel->area.width) / 2);
            else
                panel->posx = server.monitors[panel->monitor].x + panel->marginx;
        }
    }
    if (panel_position & TOP) {
        panel->posy = server.monitors[panel->monitor].y + panel->marginy;
    } else {
        if (panel_position & BOTTOM) {
            panel->posy = server.monitors[panel->monitor].y + server.monitors[panel->monitor].height -
                          panel->area.height - panel->marginy;
        } else {
            panel->posy =
                server.monitors[panel->monitor].y + ((server.monitors[panel->monitor].height - panel->area.height) / 2);
        }
    }

    // autohide or strut_policy=minimum
    int diff = (panel_horizontal ? panel->area.height : panel->area.width) - panel_autohide_height;
    if (panel_horizontal) {
        panel->hidden_width = panel->area.width;
        panel->hidden_height = panel->area.height - diff;
    } else {
        panel->hidden_width = panel->area.width - diff;
        panel->hidden_height = panel->area.height;
    }
404
    // fprintf(stderr, "panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width,
o9000's avatar
o9000 committed
405
    // panel->area.height);
406 407
}

408 409
void init_panel_size_and_position(Panel *panel)
{
o9000's avatar
o9000 committed
410 411
    panel_compute_size(panel);
    panel_compute_position(panel);
412 413
}

414
gboolean resize_panel(void *obj)
Andreas.Fink85's avatar
Andreas.Fink85 committed
415
{
o9000's avatar
o9000 committed
416 417 418
    Panel *panel = (Panel *)obj;
    relayout_with_constraint(&panel->area, 0);

419
    // fprintf(stderr, "resize_panel\n");
o9000's avatar
o9000 committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
    if (taskbar_mode != MULTI_DESKTOP && taskbar_enabled) {
        // propagate width/height on hidden taskbar
        int width = panel->taskbar[server.desktop].area.width;
        int height = panel->taskbar[server.desktop].area.height;
        for (int i = 0; i < panel->num_desktops; i++) {
            panel->taskbar[i].area.resize_needed =
                panel->taskbar[i].area.width != width || panel->taskbar[i].area.height != height;
            panel->taskbar[i].area.width = width;
            panel->taskbar[i].area.height = height;
        }
    } else if (taskbar_mode == MULTI_DESKTOP && taskbar_enabled && taskbar_distribute_size) {
        for (int i = 0; i < panel->num_desktops; i++) {
            Taskbar *taskbar = &panel->taskbar[i];
            taskbar->area.old_width = taskbar->area.width;
            taskbar->area.old_height = taskbar->area.height;
        }

        // The total available size
        int total_size = 0;
        for (int i = 0; i < panel->num_desktops; i++) {
            Taskbar *taskbar = &panel->taskbar[i];
            if (!taskbar->area.on_screen)
                continue;
            total_size += panel_horizontal ? taskbar->area.width : taskbar->area.height;
        }

        // Reserve size for padding, taskbarname and spacings
        for (int i = 0; i < panel->num_desktops; i++) {
            Taskbar *taskbar = &panel->taskbar[i];
            if (!taskbar->area.on_screen)
                continue;
            if (panel_horizontal)
                taskbar->area.width = 2 * taskbar->area.paddingxlr;
            else
                taskbar->area.height = 2 * taskbar->area.paddingxlr;
            if (taskbarname_enabled && taskbar->area.children) {
                Area *name = (Area *)taskbar->area.children->data;
                if (name->on_screen) {
                    if (panel_horizontal)
                        taskbar->area.width += name->width;
                    else
                        taskbar->area.height += name->height;
                }
            }
            gboolean first_child = TRUE;
            for (GList *l = taskbar->area.children; l; l = l->next) {
                Area *child = (Area *)l->data;
                if (!child->on_screen)
                    continue;
                if (!first_child) {
                    if (panel_horizontal)
                        taskbar->area.width += taskbar->area.paddingx;
                    else
                        taskbar->area.height += taskbar->area.paddingy;
                }
                first_child = FALSE;
            }
            total_size -= panel_horizontal ? taskbar->area.width : taskbar->area.height;
        }

        // Compute the total number of tasks
        int num_tasks = 0;
        for (int i = 0; i < panel->num_desktops; i++) {
            Taskbar *taskbar = &panel->taskbar[i];
            if (!taskbar->area.on_screen)
                continue;
            for (GList *l = taskbar->area.children; l; l = l->next) {
                Area *child = (Area *)l->data;
                if (!child->on_screen)
                    continue;
                if (taskbarname_enabled && l == taskbar->area.children)
                    continue;
                num_tasks++;
            }
        }

        // Distribute the remaining size between tasks
        if (num_tasks > 0) {
            int task_size = total_size / num_tasks;
            for (int i = 0; i < panel->num_desktops; i++) {
                Taskbar *taskbar = &panel->taskbar[i];
                if (!taskbar->area.on_screen)
                    continue;
                for (GList *l = taskbar->area.children; l; l = l->next) {
                    Area *child = (Area *)l->data;
                    if (!child->on_screen)
                        continue;
                    if (taskbarname_enabled && l == taskbar->area.children)
                        continue;
                    if (panel_horizontal)
                        taskbar->area.width += task_size;
                    else
                        taskbar->area.height += task_size;
                }
            }
        } else {
            // No tasks => expand the first visible taskbar
            for (int i = 0; i < panel->num_desktops; i++) {
                Taskbar *taskbar = &panel->taskbar[i];
                if (!taskbar->area.on_screen)
                    continue;
                if (panel_horizontal)
                    taskbar->area.width += total_size;
                else
                    taskbar->area.height += total_size;
                break;
            }
        }
        for (int i = 0; i < panel->num_desktops; i++) {
            Taskbar *taskbar = &panel->taskbar[i];
            taskbar->area.resize_needed =
                taskbar->area.old_width != taskbar->area.width || taskbar->area.old_height != taskbar->area.height;
        }
    }
    for (GList *l = panel->freespace_list; l; l = g_list_next(l))
        resize_freespace(l->data);
    return FALSE;
537 538
}

539
void update_strut(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
540
{
o9000's avatar
o9000 committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
    if (panel_strut_policy == STRUT_NONE) {
        XDeleteProperty(server.display, p->main_win, server.atom._NET_WM_STRUT);
        XDeleteProperty(server.display, p->main_win, server.atom._NET_WM_STRUT_PARTIAL);
        return;
    }

    // Reserved space
    unsigned int d1, screen_width, screen_height;
    Window d2;
    int d3;
    XGetGeometry(server.display, server.root_win, &d2, &d3, &d3, &screen_width, &screen_height, &d1, &d1);
    Monitor monitor = server.monitors[p->monitor];
    long struts[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    if (panel_horizontal) {
        int height = p->area.height + p->marginy;
556
        if (panel_strut_policy == STRUT_MINIMUM || (panel_strut_policy == STRUT_FOLLOW_SIZE && panel_autohide && p->is_hidden))
o9000's avatar
o9000 committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570
            height = p->hidden_height;
        if (panel_position & TOP) {
            struts[2] = height + monitor.y;
            struts[8] = p->posx;
            // p->area.width - 1 allowed full screen on monitor 2
            struts[9] = p->posx + p->area.width - 1;
        } else {
            struts[3] = height + screen_height - monitor.y - monitor.height;
            struts[10] = p->posx;
            // p->area.width - 1 allowed full screen on monitor 2
            struts[11] = p->posx + p->area.width - 1;
        }
    } else {
        int width = p->area.width + p->marginx;
571
        if (panel_strut_policy == STRUT_MINIMUM || (panel_strut_policy == STRUT_FOLLOW_SIZE && panel_autohide && p->is_hidden))
o9000's avatar
o9000 committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
            width = p->hidden_width;
        if (panel_position & LEFT) {
            struts[0] = width + monitor.x;
            struts[4] = p->posy;
            // p->area.width - 1 allowed full screen on monitor 2
            struts[5] = p->posy + p->area.height - 1;
        } else {
            struts[1] = width + screen_width - monitor.x - monitor.width;
            struts[6] = p->posy;
            // p->area.width - 1 allowed full screen on monitor 2
            struts[7] = p->posy + p->area.height - 1;
        }
    }
    // Old specification : fluxbox need _NET_WM_STRUT.
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._NET_WM_STRUT,
                    XA_CARDINAL,
                    32,
                    PropModeReplace,
                    (unsigned char *)&struts,
                    4);
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._NET_WM_STRUT_PARTIAL,
                    XA_CARDINAL,
                    32,
                    PropModeReplace,
                    (unsigned char *)&struts,
                    12);
Andreas.Fink85's avatar
Andreas.Fink85 committed
602 603
}

604
void set_panel_items_order(Panel *p)
605
{
o9000's avatar
o9000 committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
    if (p->area.children) {
        g_list_free(p->area.children);
        p->area.children = 0;
    }

    int i_execp = 0;
    int i_separator = 0;
    int i_freespace = 0;
    int i_button = 0;
    for (int k = 0; k < strlen(panel_items_order); k++) {
        if (panel_items_order[k] == 'L') {
            p->area.children = g_list_append(p->area.children, &p->launcher);
            p->launcher.area.resize_needed = 1;
        }
        if (panel_items_order[k] == 'T') {
            for (int j = 0; j < p->num_desktops; j++)
                p->area.children = g_list_append(p->area.children, &p->taskbar[j]);
        }
624
#ifdef ENABLE_BATTERY
o9000's avatar
o9000 committed
625 626
        if (panel_items_order[k] == 'B')
            p->area.children = g_list_append(p->area.children, &p->battery);
627
#endif
o9000's avatar
o9000 committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
        int i = p - panels;
        if (panel_items_order[k] == 'S' && systray_on_monitor(i, num_panels)) {
            p->area.children = g_list_append(p->area.children, &systray);
        }
        if (panel_items_order[k] == 'C')
            p->area.children = g_list_append(p->area.children, &p->clock);
        if (panel_items_order[k] == 'F') {
            GList *item = g_list_nth(p->freespace_list, i_freespace);
            i_freespace++;
            if (item)
                p->area.children = g_list_append(p->area.children, (Area *)item->data);
        }
        if (panel_items_order[k] == ':') {
            GList *item = g_list_nth(p->separator_list, i_separator);
            i_separator++;
            if (item)
                p->area.children = g_list_append(p->area.children, (Area *)item->data);
        }
        if (panel_items_order[k] == 'E') {
            GList *item = g_list_nth(p->execp_list, i_execp);
            i_execp++;
            if (item)
                p->area.children = g_list_append(p->area.children, (Area *)item->data);
        }
        if (panel_items_order[k] == 'P') {
            GList *item = g_list_nth(p->button_list, i_button);
            i_button++;
            if (item)
                p->area.children = g_list_append(p->area.children, (Area *)item->data);
        }
    }
    initialize_positions(&p->area, 0);
660 661
}

662 663
void place_panel_all_desktops(Panel *p)
{
o9000's avatar
o9000 committed
664 665 666 667 668 669 670 671 672
    long val = ALL_DESKTOPS;
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._NET_WM_DESKTOP,
                    XA_CARDINAL,
                    32,
                    PropModeReplace,
                    (unsigned char *)&val,
                    1);
673
}
674

675 676
void set_panel_layer(Panel *p, Layer layer)
{
o9000's avatar
o9000 committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690
    Atom state[4];
    state[0] = server.atom._NET_WM_STATE_SKIP_PAGER;
    state[1] = server.atom._NET_WM_STATE_SKIP_TASKBAR;
    state[2] = server.atom._NET_WM_STATE_STICKY;
    state[3] = layer == BOTTOM_LAYER ? server.atom._NET_WM_STATE_BELOW : server.atom._NET_WM_STATE_ABOVE;
    int num_atoms = layer == NORMAL_LAYER ? 3 : 4;
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._NET_WM_STATE,
                    XA_ATOM,
                    32,
                    PropModeReplace,
                    (unsigned char *)state,
                    num_atoms);
691 692
}

o9000's avatar
o9000 committed
693 694
void replace_panel_all_desktops(Panel *p)
{
o9000's avatar
o9000 committed
695 696 697 698 699 700 701 702 703 704 705
    XClientMessageEvent m;
    memset(&m, 0, sizeof(m));
    m.type = ClientMessage;
    m.send_event = True;
    m.display = server.display;
    m.window = p->main_win;
    m.message_type = server.atom._NET_WM_DESKTOP;
    m.format = 32;
    m.data.l[0] = ALL_DESKTOPS;
    XSendEvent(server.display, server.root_win, False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m);
    XSync(server.display, False);
o9000's avatar
o9000 committed
706 707
}

708 709
void set_panel_window_geometry(Panel *panel)
{
o9000's avatar
o9000 committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    update_strut(panel);

    // Fixed position and non-resizable window
    // Allow panel move and resize when tint2 reload config file
    int minwidth = panel_autohide ? panel->hidden_width : panel->area.width;
    int minheight = panel_autohide ? panel->hidden_height : panel->area.height;
    XSizeHints size_hints;
    size_hints.flags = PPosition | PMinSize | PMaxSize;
    size_hints.min_width = minwidth;
    size_hints.max_width = panel->area.width;
    size_hints.min_height = minheight;
    size_hints.max_height = panel->area.height;
    XSetWMNormalHints(server.display, panel->main_win, &size_hints);

    if (!panel->is_hidden) {
        if (panel_horizontal) {
            if (panel_position & TOP)
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy,
                                  panel->area.width,
                                  panel->area.height);
            else
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy,
                                  panel->area.width,
                                  panel->area.height);
        } else {
            if (panel_position & LEFT)
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy,
                                  panel->area.width,
                                  panel->area.height);
            else
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy,
                                  panel->area.width,
                                  panel->area.height);
        }
    } else {
        int diff = (panel_horizontal ? panel->area.height : panel->area.width) - panel_autohide_height;
        if (panel_horizontal) {
            if (panel_position & TOP)
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy,
                                  panel->hidden_width,
                                  panel->hidden_height);
            else
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy + diff,
                                  panel->hidden_width,
                                  panel->hidden_height);
        } else {
            if (panel_position & LEFT)
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx,
                                  panel->posy,
                                  panel->hidden_width,
                                  panel->hidden_height);
            else
                XMoveResizeWindow(server.display,
                                  panel->main_win,
                                  panel->posx + diff,
                                  panel->posy,
                                  panel->hidden_width,
                                  panel->hidden_height);
        }
    }
790 791
}

792
void set_panel_properties(Panel *p)
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
793
{
o9000's avatar
o9000 committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
    XStoreName(server.display, p->main_win, panel_window_name);
    XSetIconName(server.display, p->main_win, panel_window_name);

    gsize len;
    gchar *name = g_locale_to_utf8(panel_window_name, -1, NULL, &len, NULL);
    if (name != NULL) {
        XChangeProperty(server.display,
                        p->main_win,
                        server.atom._NET_WM_NAME,
                        server.atom.UTF8_STRING,
                        8,
                        PropModeReplace,
                        (unsigned char *)name,
                        (int)len);
        XChangeProperty(server.display,
                        p->main_win,
                        server.atom._NET_WM_ICON_NAME,
                        server.atom.UTF8_STRING,
                        8,
                        PropModeReplace,
                        (unsigned char *)name,
                        (int)len);
        g_free(name);
    }

819 820 821 822 823 824 825 826 827 828
    long pid = getpid();
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._NET_WM_PID,
                    XA_CARDINAL,
                    32,
                    PropModeReplace,
                    (unsigned char *)&pid,
                    1);

o9000's avatar
o9000 committed
829 830 831 832 833 834 835
    // Dock
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._NET_WM_WINDOW_TYPE,
                    XA_ATOM,
                    32,
                    PropModeReplace,
836
                    (unsigned char *)&server.atom._NET_WM_WINDOW_TYPE_DOCK,
o9000's avatar
o9000 committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
                    1);

    place_panel_all_desktops(p);
    set_panel_layer(p, panel_layer);

    XWMHints wmhints;
    memset(&wmhints, 0, sizeof(wmhints));
    if (panel_dock) {
        // Necessary for placing the panel into the dock on Openbox and Fluxbox.
        // See https://gitlab.com/o9000/tint2/issues/465
        wmhints.flags = IconWindowHint | WindowGroupHint | StateHint;
        wmhints.icon_window = wmhints.window_group = p->main_win;
        wmhints.initial_state = WithdrawnState;
    }
    // We do not need keyboard input focus.
    wmhints.flags |= InputHint;
    wmhints.input = False;
    XSetWMHints(server.display, p->main_win, &wmhints);

    // Undecorated
    long prop[5] = {2, 0, 0, 0, 0};
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom._MOTIF_WM_HINTS,
                    server.atom._MOTIF_WM_HINTS,
                    32,
                    PropModeReplace,
                    (unsigned char *)prop,
                    5);

    // XdndAware - Register for Xdnd events
    Atom version = 4;
    XChangeProperty(server.display,
                    p->main_win,
                    server.atom.XdndAware,
                    XA_ATOM,
                    32,
                    PropModeReplace,
                    (unsigned char *)&version,
                    1);

    // Set WM_CLASS
    XClassHint *classhint = XAllocClassHint();
    classhint->res_name = (char *)"tint2";
    classhint->res_class = (char *)"Tint2";
    XSetClassHint(server.display, p->main_win, classhint);
    XFree(classhint);

    set_panel_window_geometry(p);
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
886 887
}

888
void panel_clear_background(void *obj)
889
{
o9000's avatar
o9000 committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    Panel *p = obj;
    clear_pixmap(p->area.pix, 0, 0, p->area.width, p->area.height);
    if (!server.real_transparency) {
        get_root_pixmap();
        // copy background (server.root_pmap) in panel.area.pix
        Window dummy;
        int x, y;
        XTranslateCoordinates(server.display, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);

        if (panel_autohide && p->is_hidden) {
            int xoff = 0, yoff = 0;
            if (panel_horizontal && panel_position & BOTTOM)
                yoff = p->area.height - p->hidden_height;
            else if (!panel_horizontal && panel_position & RIGHT)
                xoff = p->area.width - p->hidden_width;
            x -= xoff;
            y -= yoff;
        }

        XSetTSOrigin(server.display, server.gc, -x, -y);
        XFillRectangle(server.display, p->area.pix, server.gc, 0, 0, p->area.width, p->area.height);
    }
912
}
913

914 915
void set_panel_background(Panel *p)
{
o9000's avatar
o9000 committed
916 917
    panel_clear_background(p);
    schedule_redraw(&p->area);
918

o9000's avatar
o9000 committed
919 920 921 922
    if (p->hidden_pixmap) {
        XFreePixmap(server.display, p->hidden_pixmap);
        p->hidden_pixmap = 0;
    }
923 924
}

925 926
Panel *get_panel(Window win)
{
o9000's avatar
o9000 committed
927 928 929 930 931 932
    for (int i = 0; i < num_panels; i++) {
        if (panels[i].main_win == win) {
            return &panels[i];
        }
    }
    return 0;
933
}
934

935
Taskbar *click_taskbar(Panel *panel, int x, int y)
936
{
o9000's avatar
o9000 committed
937 938 939 940 941 942
    for (int i = 0; i < panel->num_desktops; i++) {
        Taskbar *taskbar = &panel->taskbar[i];
        if (area_is_under_mouse(taskbar, x, y))
            return taskbar;
    }
    return NULL;
943 944
}

945
Task *click_task(Panel *panel, int x, int y)
946
{
o9000's avatar
o9000 committed
947 948 949 950 951 952 953 954 955 956 957 958 959
    Taskbar *taskbar = click_taskbar(panel, x, y);
    if (taskbar) {
        GList *l = taskbar->area.children;
        if (taskbarname_enabled)
            l = l->next;
        for (; l; l = l->next) {
            Task *task = (Task *)l->data;
            if (area_is_under_mouse(task, x, y)) {
                return task;
            }
        }
    }
    return NULL;
960 961
}

962
Launcher *click_launcher(Panel *panel, int x, int y)
thilor77's avatar
thilor77 committed
963
{
o9000's avatar
o9000 committed
964
    Launcher *launcher = &panel->launcher;
965

o9000's avatar
o9000 committed
966 967
    if (area_is_under_mouse(launcher, x, y))
        return launcher;
968

o9000's avatar
o9000 committed
969
    return NULL;
thilor77's avatar
thilor77 committed
970 971
}

972
LauncherIcon *click_launcher_icon(Panel *panel, int x, int y)
thilor77's avatar
thilor77 committed
973
{
o9000's avatar
o9000 committed
974 975 976 977 978 979 980 981 982
    Launcher *launcher = click_launcher(panel, x, y);
    if (launcher) {
        for (GSList *l = launcher->list_icons; l; l = l->next) {
            LauncherIcon *icon = (LauncherIcon *)l->data;
            if (area_is_under_mouse(icon, x, y))
                return icon;
        }
    }
    return NULL;
thilor77's avatar
thilor77 committed
983 984
}

985
Clock *click_clock(Panel *panel, int x, int y)
986
{
o9000's avatar
o9000 committed
987 988 989 990
    Clock *clock = &panel->clock;
    if (area_is_under_mouse(clock, x, y))
        return clock;
    return NULL;
991 992
}

993
#ifdef ENABLE_BATTERY
994
Battery *click_battery(Panel *panel, int x, int y)
995
{
o9000's avatar
o9000 committed
996 997 998 999
    Battery *bat = &panel->battery;
    if (area_is_under_mouse(bat, x, y))
        return bat;
    return NULL;
1000 1001 1002
}
#endif

1003 1004
Execp *click_execp(Panel *panel, int x, int y)
{
o9000's avatar
o9000 committed
1005 1006 1007 1008 1009 1010
    for (GList *l = panel->execp_list; l; l = l->next) {
        Execp *execp = (Execp *)l->data;
        if (area_is_under_mouse(execp, x, y))
            return execp;
    }
    return NULL;
1011 1012
}

o9000's avatar
o9000 committed
1013 1014
Button *click_button(Panel *panel, int x, int y)
{
o9000's avatar
o9000 committed
1015 1016 1017 1018 1019 1020
    for (GList *l = panel->button_list; l; l = l->next) {
        Button *button = (Button *)l->data;
        if (area_is_under_mouse(button, x, y))
            return button;
    }
    return NULL;
o9000's avatar
o9000 committed
1021 1022
}

1023
void stop_autohide_timeout(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
1024
{
o9000's avatar
o9000 committed
1025
    stop_timeout(p->autohide_timeout);
Andreas.Fink85's avatar
Andreas.Fink85 committed
1026 1027
}

1028
void autohide_show(void *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
1029
{
o9000's avatar
o9000 committed
1030 1031 1032 1033 1034 1035 1036 1037
    Panel *panel = (Panel *)p;
    stop_autohide_timeout(panel);
    panel->is_hidden = 0;
    XMapSubwindows(server.display, panel->main_win); // systray windows
    set_panel_window_geometry(panel);
    set_panel_layer(panel, TOP_LAYER);
    refresh_systray = TRUE; // ugly hack, because we actually only need to call XSetBackgroundPixmap
    schedule_panel_redraw();
Andreas.Fink85's avatar
Andreas.Fink85 committed
1038 1039
}

1040
void autohide_hide(void *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
1041
{
o9000's avatar
o9000 committed
1042 1043 1044 1045 1046 1047 1048
    Panel *panel = (Panel *)p;
    stop_autohide_timeout(panel);
    set_panel_layer(panel, panel_layer);
    panel->is_hidden = TRUE;
    XUnmapSubwindows(server.display, panel->main_win); // systray windows
    set_panel_window_geometry(panel);
    schedule_panel_redraw();
Andreas.Fink85's avatar
Andreas.Fink85 committed
1049 1050
}

1051
void autohide_trigger_show(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
1052
{
o9000's avatar
o9000 committed
1053 1054 1055
    if (!p)
        return;
    change_timeout(&p->autohide_timeout, panel_autohide_show_timeout, 0, autohide_show, p);
Andreas.Fink85's avatar
Andreas.Fink85 committed
1056 1057
}

1058
void autohide_trigger_hide(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
1059
{
o9000's avatar
o9000 committed
1060 1061
    if (!p)
        return;
Andreas.Fink85's avatar
Andreas.Fink85 committed
1062

o9000's avatar
o9000 committed
1063 1064 1065 1066 1067 1068
    Window root, child;
    int xr, yr, xw, yw;
    unsigned int mask;
    if (XQueryPointer(server.display, p->main_win, &root, &child, &xr, &yr, &xw, &yw, &mask))
        if (child)
            return; // mouse over one of the system tray icons
Andreas.Fink85's avatar
Andreas.Fink85 committed
1069

o9000's avatar
o9000 committed
1070
    change_timeout(&p->autohide_timeout, panel_autohide_hide_timeout, 0, autohide_hide, p);
Andreas.Fink85's avatar
Andreas.Fink85 committed
1071
}
o9000's avatar
o9000 committed
1072

1073 1074
void shrink_panel(Panel *panel)
{
o9000's avatar
o9000 committed
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
    if (!panel_shrink)
        return;
    int size = MIN(compute_desired_size(&panel->area), panel->max_size);
    gboolean update = FALSE;
    if (panel_horizontal) {
        if (panel->area.width != size) {
            panel->area.width = size;
            update = TRUE;
        }
    } else {
        if (panel->area.height != size) {
            panel->area.height = size;
            update = TRUE;
        }
    }
    if (update) {
        panel_compute_position(panel);
        set_panel_window_geometry(panel);
        set_panel_background(panel);
        panel->area.resize_needed = TRUE;
        systray.area.resize_needed = TRUE;
        schedule_redraw(&systray.area);
        refresh_systray = TRUE;
        update_minimized_icon_positions(panel);
    }
1100 1101
}

o9000's avatar
o9000 committed
1102 1103
void render_panel(Panel *panel)
{
o9000's avatar
o9000 committed
1104 1105 1106 1107 1108
    relayout(&panel->area);
    if (debug_geometry)
        area_dump_geometry(&panel->area, 0);
    update_dependent_gradients(&panel->area);
    draw_tree(&panel->area);
o9000's avatar
o9000 committed
1109
}
1110 1111 1112

const char *get_default_font()
{
o9000's avatar
o9000 committed
1113 1114 1115
    if (default_font)
        return default_font;
    return DEFAULT_FONT;
1116 1117 1118 1119
}

void default_icon_theme_changed()
{
o9000's avatar
o9000 committed
1120 1121 1122 1123
    if (!launcher_enabled && !panel_config.button_list)
        return;
    if (launcher_icon_theme_override && icon_theme_name_config)
        return;
o9000's avatar
o9000 committed
1124

o9000's avatar
o9000 committed
1125 1126
    free_icon_themes();
    load_icon_themes();
o9000's avatar
o9000 committed
1127

o9000's avatar
o9000 committed
1128 1129
    launcher_default_icon_theme_changed();
    button_default_icon_theme_changed();
1130 1131 1132 1133
}

void default_font_changed()
{
1134
#ifdef ENABLE_BATTERY
o9000's avatar
o9000 committed
1135
    battery_default_font_changed();
1136
#endif
o9000's avatar
o9000 committed
1137 1138 1139 1140 1141 1142
    clock_default_font_changed();
    execp_default_font_changed();
    button_default_font_changed();
    taskbar_default_font_changed();
    taskbarname_default_font_changed();
    tooltip_default_font_changed();
1143
}
1144 1145 1146

void _schedule_panel_redraw(const char *file, const char *function, const int line)
{
o9000's avatar
o9000 committed
1147 1148 1149 1150
    panel_refresh = TRUE;
    if (debug_fps) {
        fprintf(stderr, YELLOW "%s %s %d: triggering panel redraw" RESET "\n", file, function, line);
    }
1151
}
o9000's avatar
o9000 committed
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

void save_panel_screenshot(const Panel *panel, const char *path)
{
    imlib_context_set_drawable(panel->temp_pmap);
    Imlib_Image img = imlib_create_image_from_drawable(0, 0, 0, panel->area.width, panel->area.height, 1);

    if (!img) {
        XImage *ximg =
            XGetImage(server.display, panel->temp_pmap, 0, 0, panel->area.width, panel->area.height, AllPlanes, ZPixmap);

        if (ximg) {
            DATA32 *pixels = (DATA32 *)calloc(panel->area.width * panel->area.height, sizeof(DATA32));
            for (int x = 0; x < panel->area.width; x++) {
                for (int y = 0; y < panel->area.height; y++) {
                    DATA32 xpixel = XGetPixel(ximg, x, y);

                    DATA32 r = (xpixel >> 16) & 0xff;
                    DATA32 g = (xpixel >> 8) & 0xff;
                    DATA32 b = (xpixel >> 0) & 0xff;
                    DATA32 a = 0x0;

                    DATA32 argb = (a << 24) | (r << 16) | (g << 8) | b;
                    pixels[y * panel->area.width + x] = argb;
                }
            }
            XDestroyImage(ximg);
            img = imlib_create_image_using_data(panel->area.width, panel->area.height, pixels);
        }
    }

    if (img) {
        imlib_context_set_image(img);
        if (!panel_horizontal) {
            // rotate 90° vertical panel
            imlib_image_flip_horizontal();
            imlib_image_flip_diagonal();
        }
        imlib_save_image(path);
        imlib_free_image();
    }
}

void save_screenshot(const char *path)
{
    Panel *panel = &panels[0];

    if (panel->area.width > server.monitors[0].width)
        panel->area.width = server.monitors[0].width;

    panel->temp_pmap =
        XCreatePixmap(server.display, server.root_win, panel->area.width, panel->area.height, server.depth);
    render_panel(panel);

    XSync(server.display, False);

    save_panel_screenshot(panel, path);
}