all repos — tint2 @ 8193f7f4942edb041789e352c27786540dfbbc81

fork of the tint2 desktop panel for my custom setup - only minimized windows across all desktops for the taskbar

src/panel.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
240
241
242
243
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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
/**************************************************************************
*
* Copyright (C) 2008 Pål Staurland (staura@gmail.com)
* Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
*
* 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.
**************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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"
#include "window.h"
#include "task.h"
#include "panel.h"
#include "tooltip.h"


int signal_pending;
// --------------------------------------------------
// mouse events
int mouse_middle;
int mouse_right;
int mouse_scroll_up;
int mouse_scroll_down;
int mouse_tilt_left;
int mouse_tilt_right;

int panel_mode;
int wm_menu;
int panel_dock=0;  // default not in the dock
int panel_position;
int panel_horizontal;
int panel_refresh;

Task *task_active;
Task *task_drag;
Task *task_urgent;
int  tick_urgent;
int  max_tick_urgent;

// panel's initial config
Panel panel_config;
// panels (one panel per monitor)
Panel *panel1 = NULL;
int  nb_panel = 0;

Imlib_Image default_icon = NULL;



void init_panel()
{
	int i, old_nb_panel;
	Panel *new_panel, *p;

	cleanup_taskbar();
	for (i=0 ; i < nb_panel ; i++) {
		free_area(&panel1[i].area);
	}

	// alloc panels (one monitor or all monitors)
	old_nb_panel = nb_panel;
	if (panel_config.monitor >= 0)
		nb_panel = 1;
	else
		nb_panel = server.nb_monitor;
	fprintf(stderr, "tint2 : nb monitor %d, nb desktop %d\n", nb_panel, server.nb_desktop);

/*	if (nb_panel < old_nb_panel) {
		// freed old panels
		for (i=nb_panel ; i < old_nb_panel ; i++) {
		}
	}*/

	if (nb_panel != old_nb_panel)
		new_panel = realloc(panel1, nb_panel * sizeof(Panel));
	else
		new_panel = panel1;

	for (i=0 ; i < nb_panel ; i++) {
		p = &new_panel[i];

		if (i >= old_nb_panel) {
			// new panel
			memcpy(p, &panel_config, sizeof(Panel));
		}
		p->monitor = i;
		p->area.parent = p;
		p->area.panel = p;
		p->area.on_screen = 1;
		p->area.resize = 1;
		p->area._resize = resize_panel;
		p->g_taskbar.parent = p;
		p->g_taskbar.panel = p;
		p->g_task.area.panel = p;
		init_panel_size_and_position(p);

		// add childs
		if (p->clock.area.on_screen) {
			init_clock_panel(p);
			p->area.list = g_slist_append(p->area.list, &p->clock);
		}
#ifdef ENABLE_BATTERY
		if (p->battery.area.on_screen) {
			init_battery_panel(p);
			p->area.list = g_slist_append(p->area.list, &p->battery);
		}
#endif
		// systray only on first panel
		if (systray.area.on_screen && i == 0) {
			init_systray_panel(p);
			p->area.list = g_slist_append(p->area.list, &systray);
		}

		// full width mode
		if (!p->initial_width) {
			p->initial_width = 100;
			p->pourcentx = 1;
		}

		if (i >= old_nb_panel) {
			// new panel
			//printf("new window\n");

			// Catch some events
			long event_mask = ExposureMask|ButtonPressMask|ButtonReleaseMask;
			if (g_tooltip.enabled)
				event_mask |= PointerMotionMask|LeaveWindowMask;
			XSetWindowAttributes att = { ParentRelative, 0L, 0, 0L, 0, 0, Always, 0L, 0L, False, event_mask, NoEventMask, False, 0, 0 };
			if (p->main_win) XDestroyWindow(server.dsp, p->main_win);
			p->main_win = XCreateWindow(server.dsp, server.root_win, p->posx, p->posy, p->area.width, p->area.height, 0, server.depth, InputOutput, CopyFromParent, CWEventMask, &att);

			set_panel_properties(p);
			set_panel_background(p);
			XMapWindow (server.dsp, p->main_win);
		}
		else {
			// old panel
			//printf("move old window\n");
			XMoveResizeWindow(server.dsp, p->main_win, p->posx, p->posy, p->area.width, p->area.height);
			set_panel_background(p);
		}
	}

	panel1 = new_panel;
	panel_refresh = 1;
	init_taskbar();
	visible_object();
	task_refresh_tasklist();
}


void init_panel_size_and_position(Panel *panel)
{
	// detect panel size
	if (panel_horizontal) {
		if (panel->pourcentx)
			panel->area.width = (float)server.monitor[panel->monitor].width * panel->initial_width / 100;
		else
			panel->area.width = panel->initial_width;
		if (panel->pourcenty)
			panel->area.height = (float)server.monitor[panel->monitor].height * panel->initial_height / 100;
		else
			panel->area.height = panel->initial_height;
		if (panel->area.pix.border.rounded > panel->area.height/2)
			panel->area.pix.border.rounded = panel->area.height/2;
	}
	else {
		if (panel->pourcentx)
			panel->area.height = (float)server.monitor[panel->monitor].height * panel->initial_width / 100;
		else
			panel->area.height = panel->initial_width;
		if (panel->pourcenty)
			panel->area.width = (float)server.monitor[panel->monitor].width * panel->initial_height / 100;
		else
			panel->area.width = panel->initial_height;
		if (panel->area.pix.border.rounded > panel->area.width/2)
			panel->area.pix.border.rounded = panel->area.width/2;
	}

	// panel position determined here
	if (panel_position & LEFT) {
		panel->posx = server.monitor[panel->monitor].x + panel->marginx;
	}
	else {
		if (panel_position & RIGHT) {
			panel->posx = server.monitor[panel->monitor].x + server.monitor[panel->monitor].width - panel->area.width - panel->marginx;
		}
		else {
			if (panel_horizontal)
				panel->posx = server.monitor[panel->monitor].x + ((server.monitor[panel->monitor].width - panel->area.width) / 2);
			else
				panel->posx = server.monitor[panel->monitor].x + panel->marginx;
		}
	}
	if (panel_position & TOP) {
		panel->posy = server.monitor[panel->monitor].y + panel->marginy;
	}
	else {
		if (panel_position & BOTTOM) {
			panel->posy = server.monitor[panel->monitor].y + server.monitor[panel->monitor].height - panel->area.height - panel->marginy;
		}
		else {
			panel->posy = server.monitor[panel->monitor].y + ((server.monitor[panel->monitor].height - panel->area.height) / 2);
		}
	}
	// printf("panel : posx %d, posy %d, width %d, height %d\n", panel->posx, panel->posy, panel->area.width, panel->area.height);
}


void cleanup_panel()
{
	if (!panel1) return;

	task_active = 0;
	task_drag = 0;
	task_urgent = 0;
	cleanup_taskbar();

	// font allocated once
	if (panel1[0].g_task.font_desc) {
		pango_font_description_free(panel1[0].g_task.font_desc);
		panel1[0].g_task.font_desc = 0;
	}

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

		free_area(&p->area);

		if (p->temp_pmap) {
			XFreePixmap(server.dsp, p->temp_pmap);
			p->temp_pmap = 0;
		}
		if (p->main_win) {
			XDestroyWindow(server.dsp, p->main_win);
			p->main_win = 0;
		}
	}

	if (panel1) free(panel1);
	panel1 = 0;
}


void resize_panel(void *obj)
{
	Panel *panel = (Panel*)obj;

	if (panel_horizontal) {
		int taskbar_width, modulo_width = 0;

		taskbar_width = panel->area.width - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
		if (panel->clock.area.on_screen && panel->clock.area.width)
			taskbar_width -= (panel->clock.area.width + panel->area.paddingx);
	#ifdef ENABLE_BATTERY
		if (panel->battery.area.on_screen && panel->battery.area.width)
			taskbar_width -= (panel->battery.area.width + panel->area.paddingx);
	#endif
		// TODO : systray only on first panel. search better implementation !
		if (systray.area.on_screen && systray.area.width && panel == &panel1[0])
			taskbar_width -= (systray.area.width + panel->area.paddingx);

		if (panel_mode == MULTI_DESKTOP) {
			int width = taskbar_width - ((panel->nb_desktop-1) * panel->area.paddingx);
			taskbar_width = width / panel->nb_desktop;
			modulo_width = width % panel->nb_desktop;
		}

		// change posx and width for all taskbar
		int i, posx;
		posx = panel->area.pix.border.width + panel->area.paddingxlr;
		for (i=0 ; i < panel->nb_desktop ; i++) {
			panel->taskbar[i].area.posx = posx;
			panel->taskbar[i].area.width = taskbar_width;
			panel->taskbar[i].area.resize = 1;
			if (modulo_width) {
				panel->taskbar[i].area.width++;
				modulo_width--;
			}
			//printf("taskbar %d : posx %d, width, %d, posy %d\n", i, posx, panel->taskbar[i].area.width, posx + panel->taskbar[i].area.width);
			if (panel_mode == MULTI_DESKTOP)
				posx += panel->taskbar[i].area.width + panel->area.paddingx;
		}
	}
	else {
		int taskbar_height, modulo_height = 0;
		int i, posy;

		taskbar_height = panel->area.height - (2 * panel->area.paddingxlr) - (2 * panel->area.pix.border.width);
		if (panel->clock.area.on_screen && panel->clock.area.height)
			taskbar_height -= (panel->clock.area.height + panel->area.paddingx);
	#ifdef ENABLE_BATTERY
		if (panel->battery.area.on_screen && panel->battery.area.height)
			taskbar_height -= (panel->battery.area.height + panel->area.paddingx);
	#endif
		// TODO : systray only on first panel. search better implementation !
		if (systray.area.on_screen && systray.area.height && panel == &panel1[0])
			taskbar_height -= (systray.area.height + panel->area.paddingx);

		posy = panel->area.height - panel->area.pix.border.width - panel->area.paddingxlr - taskbar_height;
		if (panel_mode == MULTI_DESKTOP) {
			int height = taskbar_height - ((panel->nb_desktop-1) * panel->area.paddingx);
			taskbar_height = height / panel->nb_desktop;
			modulo_height = height % panel->nb_desktop;
		}

		// change posy and height for all taskbar
		for (i=0 ; i < panel->nb_desktop ; i++) {
			panel->taskbar[i].area.posy = posy;
			panel->taskbar[i].area.height = taskbar_height;
			panel->taskbar[i].area.resize = 1;
			if (modulo_height) {
				panel->taskbar[i].area.height++;
				modulo_height--;
			}
			if (panel_mode == MULTI_DESKTOP)
				posy += panel->taskbar[i].area.height + panel->area.paddingx;
		}
	}
}


void visible_object()
{
	Panel *panel;
	int i, j;

	for (i=0 ; i < nb_panel ; i++) {
		panel = &panel1[i];

		Taskbar *taskbar;
		for (j=0 ; j < panel->nb_desktop ; j++) {
			taskbar = &panel->taskbar[j];
			if (panel_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) {
				// SINGLE_DESKTOP and not current desktop
				taskbar->area.on_screen = 0;
			}
			else {
				taskbar->area.on_screen = 1;
			}
		}
	}
	panel_refresh = 1;
}


void set_panel_properties(Panel *p)
{
	XStoreName (server.dsp, p->main_win, "tint2");

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

	// Dock
	long val = server.atom._NET_WM_WINDOW_TYPE_DOCK;
	XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char *) &val, 1);

	// Reserved space
	long   struts [12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
	if (panel_horizontal) {
		if (panel_position & TOP) {
			struts[2] = p->area.height + p->marginy;
			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] = p->area.height + p->marginy;
			struts[10] = p->posx;
			// p->area.width - 1 allowed full screen on monitor 2
			struts[11] = p->posx + p->area.width - 1;
		}
	}
	else {
		if (panel_position & LEFT) {
			struts[0] = p->area.width + p->marginx;
			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] = p->area.width + p->marginx;
			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.dsp, p->main_win, server.atom._NET_WM_STRUT, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 4);
	XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STRUT_PARTIAL, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &struts, 12);

	// Sticky and below other window
	val = 0xFFFFFFFF;
	XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_DESKTOP, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &val, 1);
	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] = server.atom._NET_WM_STATE_BELOW;
	XChangeProperty (server.dsp, p->main_win, server.atom._NET_WM_STATE, XA_ATOM, 32, PropModeReplace, (unsigned char *) state, 4);

	// Fixed position and non-resizable window
	XSizeHints size_hints;
	size_hints.flags = PPosition|PMinSize|PMaxSize;
	size_hints.min_width = size_hints.max_width = p->area.width;
	size_hints.min_height = size_hints.max_height = p->area.height;
	XSetWMNormalHints(server.dsp, p->main_win, &size_hints);

	// Unfocusable
	XWMHints wmhints;
	if (panel_dock) {
		// TODO: Xdnd feature cannot be used in withdrawn state at the moment (at least GTK apps fail, qt seems to work)
		wmhints.icon_window = wmhints.window_group = p->main_win;
		wmhints.flags = StateHint | IconWindowHint;
		wmhints.initial_state = WithdrawnState;
	}
	else {
		wmhints.flags = InputHint;
		wmhints.input = False;
	}
	XSetWMHints(server.dsp, p->main_win, &wmhints);

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

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


void set_panel_background(Panel *p)
{
	get_root_pixmap();

	if (p->area.pix.pmap) XFreePixmap (server.dsp, p->area.pix.pmap);
	p->area.pix.pmap = XCreatePixmap (server.dsp, server.root_win, p->area.width, p->area.height, server.depth);

	// copy background (server.root_pmap) in panel.area.pix.pmap
	Window dummy;
	int  x, y;
	XTranslateCoordinates(server.dsp, p->main_win, server.root_win, 0, 0, &x, &y, &dummy);
	XSetTSOrigin(server.dsp, server.gc, -x, -y) ;
	XFillRectangle(server.dsp, p->area.pix.pmap, server.gc, 0, 0, p->area.width, p->area.height);

	// draw background panel
	cairo_surface_t *cs;
	cairo_t *c;
	cs = cairo_xlib_surface_create (server.dsp, p->area.pix.pmap, server.visual, p->area.width, p->area.height);
	c = cairo_create (cs);

	draw_background(&p->area, c, 0);

	cairo_destroy (c);
	cairo_surface_destroy (cs);

	// redraw panel's object
	GSList *l0;
	Area *a;
	for (l0 = p->area.list; l0 ; l0 = l0->next) {
		a = l0->data;
		set_redraw(a);
	}
}


Panel *get_panel(Window win)
{
	int i;
	for (i=0 ; i < nb_panel ; i++) {
		if (panel1[i].main_win == win) {
			return &panel1[i];
		}
	}
	return 0;
}