panel.c 28.9 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>
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
23 24 25 26 27 28 29 30
#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"
31
#include "config.h"
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
32 33 34
#include "window.h"
#include "task.h"
#include "panel.h"
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
35
#include "tooltip.h"
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
36

37 38
void panel_clear_background(void *obj);

39
int signal_pending;
40 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;
59

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

67
int max_tick_urgent;
68

69 70 71
// panel's initial config
Panel panel_config;
// panels (one panel per monitor)
72 73
Panel *panels;
int num_panels;
74

75
GArray *backgrounds;
76

77
Imlib_Image default_icon;
78
char *default_font = NULL;
79

80 81
void default_panel()
{
82 83
	panels = NULL;
	num_panels = 0;
84
	default_icon = NULL;
85 86
	task_dragged = FALSE;
	panel_horizontal = TRUE;
87
	panel_position = CENTER;
88
	panel_items_order = NULL;
89
	panel_autohide = FALSE;
90 91
	panel_autohide_show_timeout = 0;
	panel_autohide_hide_timeout = 0;
92
	panel_autohide_height = 5; // for vertical panels this is of course the width
93
	panel_strut_policy = STRUT_FOLLOW_SIZE;
94 95
	panel_dock = FALSE;         // default not in the dock
	panel_layer = BOTTOM_LAYER; // default is bottom layer
96
	panel_window_name = strdup("tint2");
97
	wm_menu = FALSE;
98
	max_tick_urgent = 14;
99
	mouse_left = TOGGLE_ICONIFY;
100 101
	backgrounds = g_array_new(0, 0, sizeof(Background));

thilor77's avatar
thilor77 committed
102
	memset(&panel_config, 0, sizeof(Panel));
103
	snprintf(panel_config.area.name, sizeof(panel_config.area.name), "Panel");
104 105 106 107 108
	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;
o9000's avatar
o9000 committed
109
	panel_config.mouse_pressed_brightness = 0;
110
	panel_config.mouse_effects = 1;
thilor77's avatar
thilor77 committed
111

112
	// First background is always fully transparent
113
	Background transparent_bg;
o9000's avatar
o9000 committed
114
	init_background(&transparent_bg);
115 116 117 118 119
	g_array_append_val(backgrounds, transparent_bg);
}

void cleanup_panel()
{
120
	if (!panels)
121
		return;
122 123 124

	cleanup_taskbar();

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

		free_area(&p->area);
129
		if (p->temp_pmap)
o9000's avatar
o9000 committed
130
			XFreePixmap(server.display, p->temp_pmap);
131 132
		p->temp_pmap = 0;
		if (p->hidden_pixmap)
o9000's avatar
o9000 committed
133
			XFreePixmap(server.display, p->hidden_pixmap);
134 135
		p->hidden_pixmap = 0;
		if (p->main_win)
o9000's avatar
o9000 committed
136
			XDestroyWindow(server.display, p->main_win);
137
		p->main_win = 0;
138
		stop_timeout(p->autohide_timeout);
139 140
	}

141 142
	free(panel_items_order);
	panel_items_order = NULL;
143
	free(panel_window_name);
144
	panel_window_name = NULL;
145 146
	free(panels);
	panels = NULL;
thilor77's avatar
thilor77 committed
147 148
	if (backgrounds)
		g_array_free(backgrounds, 1);
149 150 151
	backgrounds = NULL;
	pango_font_description_free(panel_config.g_task.font_desc);
	panel_config.g_task.font_desc = NULL;
152 153
	pango_font_description_free(panel_config.taskbarname_font_desc);
	panel_config.taskbarname_font_desc = NULL;
154 155
}

156
void init_panel()
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
157
{
158 159
	if (panel_config.monitor > (server.num_monitors - 1)) {
		// server.num_monitors minimum value is 1 (see get_monitors())
160 161 162 163
		fprintf(stderr, "warning : monitor not found. tint2 default to all monitors.\n");
		panel_config.monitor = 0;
	}

164 165
	fprintf(stderr, "panel items: %s\n", panel_items_order);

166 167
	init_tooltip();
	init_systray();
thilor77's avatar
thilor77 committed
168
	init_launcher();
169 170 171 172
	init_clock();
#ifdef ENABLE_BATTERY
	init_battery();
#endif
173
	init_taskbar();
174
	init_execp();
175

thilor77's avatar
thilor77 committed
176
	// number of panels (one monitor or 'all' monitors)
177
	if (panel_config.monitor >= 0)
178
		num_panels = 1;
179
	else
180
		num_panels = server.num_monitors;
181

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

187
	fprintf(stderr,
188 189 190 191
	        "tint2 : nb monitor %d, nb monitor used %d, nb desktop %d\n",
	        server.num_monitors,
	        num_panels,
	        server.num_desktops);
192 193
	for (int i = 0; i < num_panels; i++) {
		Panel *p = &panels[i];
194

thilor77's avatar
thilor77 committed
195 196
		if (panel_config.monitor < 0)
			p->monitor = i;
197
		if (!p->area.bg)
198
			p->area.bg = &g_array_index(backgrounds, Background, 0);
199
		p->area.parent = p;
200
		p->area.panel = p;
201
		snprintf(p->area.name, sizeof(p->area.name), "Panel %d", i);
202
		p->area.on_screen = TRUE;
o9000's avatar
o9000 committed
203 204
		p->area.resize_needed = 1;
		p->area.size_mode = LAYOUT_DYNAMIC;
205
		p->area._resize = resize_panel;
206
		p->area._clear = panel_clear_background;
207
		init_panel_size_and_position(p);
o9000's avatar
o9000 committed
208
		// add children according to panel_items
209 210
		for (int k = 0; k < strlen(panel_items_order); k++) {
			if (panel_items_order[k] == 'L')
211 212 213 214 215 216 217
				init_launcher_panel(p);
			if (panel_items_order[k] == 'T')
				init_taskbar_panel(p);
#ifdef ENABLE_BATTERY
			if (panel_items_order[k] == 'B')
				init_battery_panel(p);
#endif
218
			if (panel_items_order[k] == 'S' && systray_on_monitor(i, num_panels)) {
219 220 221 222 223
				init_systray_panel(p);
				refresh_systray = 1;
			}
			if (panel_items_order[k] == 'C')
				init_clock_panel(p);
224
			if (panel_items_order[k] == 'F' && !strstr(panel_items_order, "T"))
o9000's avatar
o9000 committed
225
				init_freespace_panel(p);
226 227
			if (panel_items_order[k] == 'E')
				init_execp_panel(p);
228 229
		}
		set_panel_items_order(p);
230

thilor77's avatar
thilor77 committed
231
		// catch some events
232 233
		XSetWindowAttributes att = {.colormap = server.colormap, .background_pixel = 0, .border_pixel = 0};
		unsigned long mask = CWEventMask | CWColormap | CWBackPixel | CWBorderPixel;
o9000's avatar
o9000 committed
234
		p->main_win = XCreateWindow(server.display,
235 236 237 238 239 240 241 242 243 244 245
		                            server.root_win,
		                            p->posx,
		                            p->posy,
		                            p->area.width,
		                            p->area.height,
		                            0,
		                            server.depth,
		                            InputOutput,
		                            server.visual,
		                            mask,
		                            &att);
246

247
		long event_mask = ExposureMask | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | PropertyChangeMask;
248
		if (p->mouse_effects || p->g_task.tooltip_enabled || p->clock.area._get_tooltip_text ||
249
		    (launcher_enabled && launcher_tooltip_enabled))
250
			event_mask |= PointerMotionMask | LeaveWindowMask;
251
		if (panel_autohide)
252
			event_mask |= LeaveWindowMask | EnterWindowMask;
o9000's avatar
o9000 committed
253
		XChangeWindowAttributes(server.display,
254 255 256
		                        p->main_win,
		                        CWEventMask,
		                        &(XSetWindowAttributes){.event_mask = event_mask});
257

258
		if (!server.gc) {
259
			XGCValues gcv;
o9000's avatar
o9000 committed
260
			server.gc = XCreateGC(server.display, p->main_win, 0, &gcv);
261
		}
262
		// printf("panel %d : %d, %d, %d, %d\n", i, p->posx, p->posy, p->area.width, p->area.height);
263 264
		set_panel_properties(p);
		set_panel_background(p);
265
		if (!snapshot_path) {
266
			// if we are not in 'snapshot' mode then map new panel
o9000's avatar
o9000 committed
267
			XMapWindow(server.display, p->main_win);
268
		}
Andreas.Fink85's avatar
Andreas.Fink85 committed
269 270

		if (panel_autohide)
271
			autohide_trigger_hide(p);
272
	}
273

o9000's avatar
o9000 committed
274
	taskbar_refresh_tasklist();
275
	reset_active_task();
276
	update_all_taskbars_visibility();
277 278
}

279 280 281 282
void init_panel_size_and_position(Panel *panel)
{
	// detect panel size
	if (panel_horizontal) {
283 284 285 286 287 288 289 290
		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;
		}
291
		if (panel->fractional_width)
292
			panel->area.width = (server.monitors[panel->monitor].width - panel->marginx) * panel->area.width / 100;
293
		if (panel->fractional_height)
294
			panel->area.height = (server.monitors[panel->monitor].height - panel->marginy) * panel->area.height / 100;
295
		if (panel->area.bg->border.radius > panel->area.height / 2) {
296 297
			printf("panel_background_id rounded is too big... please fix your tint2rc\n");
			g_array_append_val(backgrounds, *panel->area.bg);
298 299
			panel->area.bg = &g_array_index(backgrounds, Background, backgrounds->len - 1);
			panel->area.bg->border.radius = panel->area.height / 2;
300
		}
301
	} else {
302 303 304 305 306 307 308 309
		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;
		}
310
		int old_panel_height = panel->area.height;
311
		if (panel->fractional_width)
312
			panel->area.height = (server.monitors[panel->monitor].height - panel->marginy) * panel->area.width / 100;
313
		else
314
			panel->area.height = panel->area.width;
315 316

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

		if (panel->area.bg->border.radius > panel->area.width / 2) {
322 323
			printf("panel_background_id rounded is too big... please fix your tint2rc\n");
			g_array_append_val(backgrounds, *panel->area.bg);
324 325
			panel->area.bg = &g_array_index(backgrounds, Background, backgrounds->len - 1);
			panel->area.bg->border.radius = panel->area.width / 2;
326
		}
327 328
	}

329 330 331 332 333
	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;

334 335
	// panel position determined here
	if (panel_position & LEFT) {
o9000's avatar
o9000 committed
336
		panel->posx = server.monitors[panel->monitor].x + panel->marginx;
337
	} else {
338
		if (panel_position & RIGHT) {
o9000's avatar
o9000 committed
339
			panel->posx = server.monitors[panel->monitor].x + server.monitors[panel->monitor].width -
340
			              panel->area.width - panel->marginx;
341
		} else {
342
			if (panel_horizontal)
o9000's avatar
o9000 committed
343
				panel->posx = server.monitors[panel->monitor].x +
344
				              ((server.monitors[panel->monitor].width - panel->area.width) / 2);
345
			else
o9000's avatar
o9000 committed
346
				panel->posx = server.monitors[panel->monitor].x + panel->marginx;
347 348 349
		}
	}
	if (panel_position & TOP) {
o9000's avatar
o9000 committed
350
		panel->posy = server.monitors[panel->monitor].y + panel->marginy;
351
	} else {
352
		if (panel_position & BOTTOM) {
o9000's avatar
o9000 committed
353
			panel->posy = server.monitors[panel->monitor].y + server.monitors[panel->monitor].height -
354
			              panel->area.height - panel->marginy;
355 356
		} else {
			panel->posy =
357
			    server.monitors[panel->monitor].y + ((server.monitors[panel->monitor].height - panel->area.height) / 2);
358 359
		}
	}
Andreas.Fink85's avatar
Andreas.Fink85 committed
360

361 362 363 364 365
	// 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;
366
	} else {
367 368
		panel->hidden_width = panel->area.width - diff;
		panel->hidden_height = panel->area.height;
Andreas.Fink85's avatar
Andreas.Fink85 committed
369
	}
370 371
	// printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width,
	// panel->area.height);
372 373
}

374
gboolean resize_panel(void *obj)
Andreas.Fink85's avatar
Andreas.Fink85 committed
375
{
376
	Panel *panel = (Panel *)obj;
o9000's avatar
o9000 committed
377
	relayout_with_constraint(&panel->area, 0);
378 379 380

	// printf("resize_panel\n");
	if (taskbar_mode != MULTI_DESKTOP && taskbar_enabled) {
381
		// propagate width/height on hidden taskbar
382 383 384
		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++) {
385 386
			panel->taskbar[i].area.width = width;
			panel->taskbar[i].area.height = height;
o9000's avatar
o9000 committed
387
			panel->taskbar[i].area.resize_needed = 1;
388 389
		}
	}
390
	if (taskbar_mode == MULTI_DESKTOP && taskbar_enabled && taskbar_distribute_size) {
391 392 393 394 395 396
		// Distribute the available space between taskbars

		// Compute the total available size, and the total size requested by the taskbars
		int total_size = 0;
		int total_name_size = 0;
		int total_items = 0;
397
		int visible_taskbars = 0;
398
		for (int i = 0; i < panel->num_desktops; i++) {
399 400 401
			if (!panel->taskbar[i].area.on_screen)
				continue;
			visible_taskbars++;
402 403 404 405 406 407 408
			if (panel_horizontal) {
				total_size += panel->taskbar[i].area.width;
			} else {
				total_size += panel->taskbar[i].area.height;
			}

			Taskbar *taskbar = &panel->taskbar[i];
409
			GList *l;
o9000's avatar
o9000 committed
410
			for (l = taskbar->area.children; l; l = l->next) {
411
				Area *child = (Area *)l->data;
412 413 414 415 416
				if (!child->on_screen)
					continue;
				total_items++;
			}
			if (taskbarname_enabled) {
o9000's avatar
o9000 committed
417
				if (taskbar->area.children) {
418
					total_items--;
419
					Area *name = (Area *)taskbar->area.children->data;
420 421 422 423 424 425 426 427 428 429 430 431 432
					if (panel_horizontal) {
						total_name_size += name->width;
					} else {
						total_name_size += name->height;
					}
				}
			}
		}
		// Distribute the space proportionally to the requested size (that is, to the
		// number of tasks in each taskbar)
		if (total_items) {
			int actual_name_size;
			if (total_name_size <= total_size) {
433
				actual_name_size = total_name_size / visible_taskbars;
434
			} else {
435
				actual_name_size = total_size / visible_taskbars;
436 437 438
			}
			total_size -= total_name_size;

439
			for (int i = 0; i < panel->num_desktops; i++) {
440
				Taskbar *taskbar = &panel->taskbar[i];
441 442
				if (!taskbar->area.on_screen)
					continue;
443

444 445 446
				int requested_size = (panel_horizontal ? left_right_border_width(&taskbar->area)
				                                       : top_bottom_border_width(&taskbar->area)) +
									 2 * taskbar->area.paddingxlr;
447
				int items = 0;
o9000's avatar
o9000 committed
448
				GList *l = taskbar->area.children;
449 450 451
				if (taskbarname_enabled)
					l = l->next;
				for (; l; l = l->next) {
452
					Area *child = (Area *)l->data;
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
					if (!child->on_screen)
						continue;
					items++;
					if (panel_horizontal) {
						requested_size += child->width + taskbar->area.paddingy;
					} else {
						requested_size += child->height + taskbar->area.paddingx;
					}
				}
				if (panel_horizontal) {
					requested_size -= taskbar->area.paddingy;
				} else {
					requested_size -= taskbar->area.paddingx;
				}

				if (panel_horizontal) {
					taskbar->area.width = actual_name_size + items / (float)total_items * total_size;
				} else {
					taskbar->area.height = actual_name_size + items / (float)total_items * total_size;
				}
o9000's avatar
o9000 committed
473
				taskbar->area.resize_needed = 1;
474 475 476
			}
		}
	}
o9000's avatar
o9000 committed
477
	if (panel->freespace.area.on_screen)
478
		resize_freespace(&panel->freespace);
479
	return FALSE;
480 481
}

482
void update_strut(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
483
{
484
	if (panel_strut_policy == STRUT_NONE) {
o9000's avatar
o9000 committed
485 486
		XDeleteProperty(server.display, p->main_win, server.atom._NET_WM_STRUT);
		XDeleteProperty(server.display, p->main_win, server.atom._NET_WM_STRUT_PARTIAL);
487 488 489
		return;
	}

Andreas.Fink85's avatar
Andreas.Fink85 committed
490 491 492 493
	// Reserved space
	unsigned int d1, screen_width, screen_height;
	Window d2;
	int d3;
o9000's avatar
o9000 committed
494
	XGetGeometry(server.display, server.root_win, &d2, &d3, &d3, &screen_width, &screen_height, &d1, &d1);
o9000's avatar
o9000 committed
495
	Monitor monitor = server.monitors[p->monitor];
496
	long struts[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Andreas.Fink85's avatar
Andreas.Fink85 committed
497 498
	if (panel_horizontal) {
		int height = p->area.height + p->marginy;
499
		if (panel_strut_policy == STRUT_MINIMUM || (panel_strut_policy == STRUT_FOLLOW_SIZE && p->is_hidden))
Andreas.Fink85's avatar
Andreas.Fink85 committed
500 501 502 503 504 505
			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;
506
		} else {
Andreas.Fink85's avatar
Andreas.Fink85 committed
507 508 509 510 511
			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;
		}
512
	} else {
Andreas.Fink85's avatar
Andreas.Fink85 committed
513
		int width = p->area.width + p->marginx;
514
		if (panel_strut_policy == STRUT_MINIMUM || (panel_strut_policy == STRUT_FOLLOW_SIZE && p->is_hidden))
Andreas.Fink85's avatar
Andreas.Fink85 committed
515 516 517 518 519 520
			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;
521
		} else {
Andreas.Fink85's avatar
Andreas.Fink85 committed
522 523 524 525 526 527 528
			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.
o9000's avatar
o9000 committed
529
	XChangeProperty(server.display,
530 531 532 533 534 535 536
	                p->main_win,
	                server.atom._NET_WM_STRUT,
	                XA_CARDINAL,
	                32,
	                PropModeReplace,
	                (unsigned char *)&struts,
	                4);
o9000's avatar
o9000 committed
537
	XChangeProperty(server.display,
538 539 540 541 542 543 544
	                p->main_win,
	                server.atom._NET_WM_STRUT_PARTIAL,
	                XA_CARDINAL,
	                32,
	                PropModeReplace,
	                (unsigned char *)&struts,
	                12);
Andreas.Fink85's avatar
Andreas.Fink85 committed
545 546
}

547
void set_panel_items_order(Panel *p)
548
{
o9000's avatar
o9000 committed
549 550 551
	if (p->area.children) {
		g_list_free(p->area.children);
		p->area.children = 0;
552
	}
553

554
	int i_execp = 0;
555
	for (int k = 0; k < strlen(panel_items_order); k++) {
556
		if (panel_items_order[k] == 'L') {
o9000's avatar
o9000 committed
557
			p->area.children = g_list_append(p->area.children, &p->launcher);
o9000's avatar
o9000 committed
558
			p->launcher.area.resize_needed = 1;
559
		}
560
		if (panel_items_order[k] == 'T') {
561
			for (int j = 0; j < p->num_desktops; j++)
o9000's avatar
o9000 committed
562
				p->area.children = g_list_append(p->area.children, &p->taskbar[j]);
563 564
		}
#ifdef ENABLE_BATTERY
565
		if (panel_items_order[k] == 'B')
o9000's avatar
o9000 committed
566
			p->area.children = g_list_append(p->area.children, &p->battery);
567
#endif
568 569
		int i = p - panels;
		if (panel_items_order[k] == 'S' && systray_on_monitor(i, num_panels)) {
o9000's avatar
o9000 committed
570
			p->area.children = g_list_append(p->area.children, &systray);
571
		}
572
		if (panel_items_order[k] == 'C')
o9000's avatar
o9000 committed
573
			p->area.children = g_list_append(p->area.children, &p->clock);
o9000's avatar
o9000 committed
574
		if (panel_items_order[k] == 'F')
o9000's avatar
o9000 committed
575
			p->area.children = g_list_append(p->area.children, &p->freespace);
576 577 578 579
		if (panel_items_order[k] == 'E') {
			GList *item = g_list_nth(p->execp_list, i_execp);
			i_execp++;
			if (item)
o9000's avatar
o9000 committed
580
				p->area.children = g_list_append(p->area.children, (Area *)item->data);
581
		}
582
	}
o9000's avatar
o9000 committed
583
	initialize_positions(&p->area, 0);
584 585
}

586 587 588 589
void place_panel_all_desktops(Panel *p)
{
	long val = ALL_DESKTOPS;
	XChangeProperty(server.display,
590 591 592 593 594 595 596
	                p->main_win,
	                server.atom._NET_WM_DESKTOP,
	                XA_CARDINAL,
	                32,
	                PropModeReplace,
	                (unsigned char *)&val,
	                1);
597 598 599 600 601 602 603 604

	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] = panel_layer == BOTTOM_LAYER ? server.atom._NET_WM_STATE_BELOW : server.atom._NET_WM_STATE_ABOVE;
	int num_atoms = panel_layer == NORMAL_LAYER ? 3 : 4;
	XChangeProperty(server.display,
605 606 607 608 609 610 611
	                p->main_win,
	                server.atom._NET_WM_STATE,
	                XA_ATOM,
	                32,
	                PropModeReplace,
	                (unsigned char *)state,
	                num_atoms);
612 613
}

o9000's avatar
o9000 committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
void replace_panel_all_desktops(Panel *p)
{
	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);
}

629
void set_panel_properties(Panel *p)
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
630
{
o9000's avatar
o9000 committed
631 632
	XStoreName(server.display, p->main_win, panel_window_name);
	XSetIconName(server.display, p->main_win, panel_window_name);
633 634

	gsize len;
635
	gchar *name = g_locale_to_utf8(panel_window_name, -1, NULL, &len, NULL);
636
	if (name != NULL) {
o9000's avatar
o9000 committed
637
		XChangeProperty(server.display,
638 639 640 641 642 643 644
		                p->main_win,
		                server.atom._NET_WM_NAME,
		                server.atom.UTF8_STRING,
		                8,
		                PropModeReplace,
		                (unsigned char *)name,
		                (int)len);
o9000's avatar
o9000 committed
645
		XChangeProperty(server.display,
646 647 648 649 650 651 652
		                p->main_win,
		                server.atom._NET_WM_ICON_NAME,
		                server.atom.UTF8_STRING,
		                8,
		                PropModeReplace,
		                (unsigned char *)name,
		                (int)len);
653 654 655 656
		g_free(name);
	}

	// Dock
657
	long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
o9000's avatar
o9000 committed
658
	XChangeProperty(server.display,
659 660 661 662 663 664 665
	                p->main_win,
	                server.atom._NET_WM_WINDOW_TYPE,
	                XA_ATOM,
	                32,
	                PropModeReplace,
	                (unsigned char *)&val,
	                1);
666

667
	place_panel_all_desktops(p);
668 669

	XWMHints wmhints;
o9000's avatar
o9000 committed
670
	memset(&wmhints, 0, sizeof(wmhints));
671 672
	if (panel_dock) {
		// Necessary for placing the panel into the dock on Openbox and Fluxbox.
673
		// See https://gitlab.com/o9000/tint2/issues/465
o9000's avatar
o9000 committed
674
		wmhints.flags = IconWindowHint | WindowGroupHint | StateHint;
675 676 677
		wmhints.icon_window = wmhints.window_group = p->main_win;
		wmhints.initial_state = WithdrawnState;
	}
o9000's avatar
o9000 committed
678
	// We do not need keyboard input focus.
679
	wmhints.flags |= InputHint;
680
	wmhints.input = False;
o9000's avatar
o9000 committed
681
	XSetWMHints(server.display, p->main_win, &wmhints);
682 683

	// Undecorated
684
	long prop[5] = {2, 0, 0, 0, 0};
o9000's avatar
o9000 committed
685
	XChangeProperty(server.display,
686 687 688 689 690 691 692
	                p->main_win,
	                server.atom._MOTIF_WM_HINTS,
	                server.atom._MOTIF_WM_HINTS,
	                32,
	                PropModeReplace,
	                (unsigned char *)prop,
	                5);
693 694

	// XdndAware - Register for Xdnd events
695
	Atom version = 4;
o9000's avatar
o9000 committed
696
	XChangeProperty(server.display,
697 698 699 700 701 702 703
	                p->main_win,
	                server.atom.XdndAware,
	                XA_ATOM,
	                32,
	                PropModeReplace,
	                (unsigned char *)&version,
	                1);
704

Andreas.Fink85's avatar
Andreas.Fink85 committed
705
	update_strut(p);
706

707
	// Fixed position and non-resizable window
708
	// Allow panel move and resize when tint2 reload config file
Andreas.Fink85's avatar
Andreas.Fink85 committed
709 710
	int minwidth = panel_autohide ? p->hidden_width : p->area.width;
	int minheight = panel_autohide ? p->hidden_height : p->area.height;
711
	XSizeHints size_hints;
712
	size_hints.flags = PPosition | PMinSize | PMaxSize;
Andreas.Fink85's avatar
Andreas.Fink85 committed
713 714 715 716
	size_hints.min_width = minwidth;
	size_hints.max_width = p->area.width;
	size_hints.min_height = minheight;
	size_hints.max_height = p->area.height;
o9000's avatar
o9000 committed
717
	XSetWMNormalHints(server.display, p->main_win, &size_hints);
Andreas.Fink85's avatar
Andreas.Fink85 committed
718 719

	// Set WM_CLASS
720 721 722
	XClassHint *classhint = XAllocClassHint();
	classhint->res_name = (char *)"tint2";
	classhint->res_class = (char *)"Tint2";
o9000's avatar
o9000 committed
723
	XSetClassHint(server.display, p->main_win, classhint);
Andreas.Fink85's avatar
Andreas.Fink85 committed
724
	XFree(classhint);
lorthiois@bbsoft.fr's avatar
lorthiois@bbsoft.fr committed
725 726
}

727
void panel_clear_background(void *obj)
728
{
729 730 731
	Panel *p = obj;
	clear_pixmap(p->area.pix, 0, 0, p->area.width, p->area.height);
	if (!server.real_transparency) {
732
		get_root_pixmap();
733
		// copy background (server.root_pmap) in panel.area.pix
734
		Window dummy;
735
		int x, y;
o9000's avatar
o9000 committed
736
		XTranslateCoordinates(server.display, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
o9000's avatar
o9000 committed
737

738
		if (panel_autohide && p->is_hidden) {
o9000's avatar
o9000 committed
739 740 741 742 743
			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;
744 745 746
			x -= xoff;
			y -= yoff;
		}
o9000's avatar
o9000 committed
747

o9000's avatar
o9000 committed
748 749
		XSetTSOrigin(server.display, server.gc, -x, -y);
		XFillRectangle(server.display, p->area.pix, server.gc, 0, 0, p->area.width, p->area.height);
750
	}
751
}
752

753 754 755 756
void set_panel_background(Panel *p)
{
	panel_clear_background(p);
	schedule_redraw(&p->area);
757

758 759 760
	if (p->hidden_pixmap) {
		XFreePixmap(server.display, p->hidden_pixmap);
		p->hidden_pixmap = 0;
761
	}
762 763
}

764 765
Panel *get_panel(Window win)
{
766 767 768
	for (int i = 0; i < num_panels; i++) {
		if (panels[i].main_win == win) {
			return &panels[i];
769 770 771 772
		}
	}
	return 0;
}
773

774
Taskbar *click_taskbar(Panel *panel, int x, int y)
775
{
776 777 778 779
	for (int i = 0; i < panel->num_desktops; i++) {
		Taskbar *taskbar = &panel->taskbar[i];
		if (area_is_under_mouse(taskbar, x, y))
			return taskbar;
780 781 782 783
	}
	return NULL;
}

784
Task *click_task(Panel *panel, int x, int y)
785
{
786 787
	Taskbar *taskbar = click_taskbar(panel, x, y);
	if (taskbar) {
788 789 790 791 792 793 794
		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;
795 796 797 798 799 800
			}
		}
	}
	return NULL;
}

801
Launcher *click_launcher(Panel *panel, int x, int y)
thilor77's avatar
thilor77 committed
802 803
{
	Launcher *launcher = &panel->launcher;
804

805 806 807
	if (area_is_under_mouse(launcher, x, y))
		return launcher;

thilor77's avatar
thilor77 committed
808 809 810
	return NULL;
}

811
LauncherIcon *click_launcher_icon(Panel *panel, int x, int y)
thilor77's avatar
thilor77 committed
812
{
813 814 815 816
	Launcher *launcher = click_launcher(panel, x, y);
	if (launcher) {
		for (GSList *l = launcher->list_icons; l; l = l->next) {
			LauncherIcon *icon = (LauncherIcon *)l->data;
817
			if (area_is_under_mouse(icon, x, y))
thilor77's avatar
thilor77 committed
818 819 820 821 822 823
				return icon;
		}
	}
	return NULL;
}

824
Clock *click_clock(Panel *panel, int x, int y)
825
{
826
	Clock *clock = &panel->clock;
827 828 829
	if (area_is_under_mouse(clock, x, y))
		return clock;
	return NULL;
830 831
}

832
#ifdef ENABLE_BATTERY
833
Battery *click_battery(Panel *panel, int x, int y)
834
{
835
	Battery *bat = &panel->battery;
836 837 838
	if (area_is_under_mouse(bat, x, y))
		return bat;
	return NULL;
839 840 841
}
#endif

842 843
Execp *click_execp(Panel *panel, int x, int y)
{
844
	for (GList *l = panel->execp_list; l; l = l->next) {
845
		Execp *execp = (Execp *)l->data;
846 847
		if (area_is_under_mouse(execp, x, y))
			return execp;
848 849 850 851
	}
	return NULL;
}

852
void stop_autohide_timeout(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
853
{
854
	stop_timeout(p->autohide_timeout);
Andreas.Fink85's avatar
Andreas.Fink85 committed
855 856
}

857
void autohide_show(void *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
858
{
859
	Panel *panel = (Panel *)p;
Andreas.Fink85's avatar
Andreas.Fink85 committed
860 861 862
	stop_autohide_timeout(panel);
	panel->is_hidden = 0;

o9000's avatar
o9000 committed
863
	XMapSubwindows(server.display, panel->main_win); // systray windows
Andreas.Fink85's avatar
Andreas.Fink85 committed
864 865
	if (panel_horizontal) {
		if (panel_position & TOP)
o9000's avatar
o9000 committed
866
			XResizeWindow(server.display, panel->main_win, panel->area.width, panel->area.height);
Andreas.Fink85's avatar
Andreas.Fink85 committed
867
		else
o9000's avatar
o9000 committed
868
			XMoveResizeWindow(server.display,
869 870 871 872 873
			                  panel->main_win,
			                  panel->posx,
			                  panel->posy,
			                  panel->area.width,
			                  panel->area.height);
874
	} else {
Andreas.Fink85's avatar
Andreas.Fink85 committed
875
		if (panel_position & LEFT)
o9000's avatar
o9000 committed
876
			XResizeWindow(server.display, panel->main_win, panel->area.width, panel->area.height);
Andreas.Fink85's avatar
Andreas.Fink85 committed
877
		else
o9000's avatar
o9000 committed
878
			XMoveResizeWindow(server.display,
879 880 881 882 883
			                  panel->main_win,
			                  panel->posx,
			                  panel->posy,
			                  panel->area.width,
			                  panel->area.height);
Andreas.Fink85's avatar
Andreas.Fink85 committed
884
	}
885
	if (panel_strut_policy == STRUT_FOLLOW_SIZE)
886 887 888
		update_strut(panel);
	refresh_systray = TRUE; // ugly hack, because we actually only need to call XSetBackgroundPixmap
	panel_refresh = TRUE;
Andreas.Fink85's avatar
Andreas.Fink85 committed
889 890
}

891
void autohide_hide(void *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
892
{
893
	Panel *panel = (Panel *)p;
Andreas.Fink85's avatar
Andreas.Fink85 committed
894
	stop_autohide_timeout(panel);
895
	panel->is_hidden = TRUE;
Andreas.Fink85's avatar
Andreas.Fink85 committed
896
	if (panel_strut_policy == STRUT_FOLLOW_SIZE)
897
		update_strut(panel);
Andreas.Fink85's avatar
Andreas.Fink85 committed
898

o9000's avatar
o9000 committed
899
	XUnmapSubwindows(server.display, panel->main_win); // systray windows
Andreas.Fink85's avatar
Andreas.Fink85 committed
900
	int diff = (panel_horizontal ? panel->area.height : panel->area.width) - panel_autohide_height;
901
	// printf("autohide_hide : diff %d, w %d, h %d\n", diff, panel->hidden_width, panel->hidden_height);
Andreas.Fink85's avatar
Andreas.Fink85 committed
902 903
	if (panel_horizontal) {
		if (panel_position & TOP)
o9000's avatar
o9000 committed
904
			XResizeWindow(server.display, panel->main_win, panel->hidden_width, panel->hidden_height);
Andreas.Fink85's avatar
Andreas.Fink85 committed
905
		else
o9000's avatar
o9000 committed
906
			XMoveResizeWindow(server.display,
907 908 909 910 911
			                  panel->main_win,
			                  panel->posx,
			                  panel->posy + diff,
			                  panel->hidden_width,
			                  panel->hidden_height);
912
	} else {
Andreas.Fink85's avatar
Andreas.Fink85 committed
913
		if (panel_position & LEFT)
o9000's avatar
o9000 committed
914
			XResizeWindow(server.display, panel->main_win, panel->hidden_width, panel->hidden_height);
Andreas.Fink85's avatar
Andreas.Fink85 committed
915
		else
o9000's avatar
o9000 committed
916
			XMoveResizeWindow(server.display,
917 918 919 920 921
			                  panel->main_win,
			                  panel->posx + diff,
			                  panel->posy,
			                  panel->hidden_width,
			                  panel->hidden_height);
922 923
	}
	panel_refresh = TRUE;
Andreas.Fink85's avatar
Andreas.Fink85 committed
924 925
}

926
void autohide_trigger_show(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
927 928 929
{
	if (!p)
		return;
930
	change_timeout(&p->autohide_timeout, panel_autohide_show_timeout, 0, autohide_show, p);
Andreas.Fink85's avatar
Andreas.Fink85 committed
931 932
}

933
void autohide_trigger_hide(Panel *p)
Andreas.Fink85's avatar
Andreas.Fink85 committed
934 935 936 937 938 939 940
{
	if (!p)
		return;

	Window root, child;
	int xr, yr, xw, yw;
	unsigned int mask;
o9000's avatar
o9000 committed
941
	if (XQueryPointer(server.display, p->main_win, &root, &child, &xr, &yr, &xw, &yw, &mask))
942 943
		if (child)
			return; // mouse over one of the system tray icons
Andreas.Fink85's avatar
Andreas.Fink85 committed
944

945
	change_timeout(&p->autohide_timeout, panel_autohide_hide_timeout, 0, autohide_hide, p);
Andreas.Fink85's avatar
Andreas.Fink85 committed
946
}
o9000's avatar
o9000 committed
947 948 949 950

void render_panel(Panel *panel)
{
	relayout(&panel->area);
951 952
	if (debug_geometry)
		area_dump_geometry(&panel->area, 0);
o9000's avatar
o9000 committed
953 954
	draw_tree(&panel->area);
}
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

const char *get_default_font()
{
	if (default_font)
		return default_font;
	return DEFAULT_FONT;
}

void default_icon_theme_changed()
{
	launcher_default_icon_theme_changed();
}

void default_font_changed()
{
970
#ifdef ENABLE_BATTERY
971
	battery_default_font_changed();
972
#endif
973 974 975 976 977 978
	clock_default_font_changed();
	execp_default_font_changed();
	taskbar_default_font_changed();
	taskbarname_default_font_changed();
	tooltip_default_font_changed();
}