all repos — tint2 @ 0cf39bb1beb9ea0fb1cec010636a4ba36cc0d4b9

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

src/launcher/launcher.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
/**************************************************************************
* Tint2 : launcher
*
* Copyright (C) 2010       (mrovi@interfete-web-club.com)
*
* 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 <string.h>
#include <stdio.h>
#include <cairo.h>
#include <cairo-xlib.h>
#include <pango/pangocairo.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>

#include "window.h"
#include "server.h"
#include "area.h"
#include "panel.h"
#include "taskbar.h"
#include "launcher.h"

int launcher_enabled;
int launcher_max_icon_size;


void default_launcher()
{
	launcher_enabled = 0;
	launcher_max_icon_size = 0;
}


void init_launcher()
{
}


void init_launcher_panel(void *p)
{
	Panel *panel =(Panel*)p;
	Launcher *launcher = &panel->launcher;

	launcher->area.parent = p;
	launcher->area.panel = p;
	launcher->area._draw_foreground = draw_launcher;
	launcher->area._resize = resize_launcher;
	launcher->area.resize = 1;
	launcher->area.redraw = 1;
	launcher->area.on_screen = 1;

	if (panel_horizontal) {
		// panel horizonal => fixed height and posy
		launcher->area.posy = panel->area.bg->border.width + panel->area.paddingy;
		launcher->area.height = panel->area.height - (2 * launcher->area.posy);
	}
	else {
		// panel vertical => fixed width, height, posy and posx
		launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
		launcher->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
	}

	// Load icons
	{
		GSList* path = launcher->list_icon_paths;
		GSList* cmd = launcher->list_cmds;
		while (path != NULL && cmd != NULL)
		{
			LauncherIcon *launcherIcon = malloc(sizeof(LauncherIcon));
			launcherIcon->icon = imlib_load_image(path->data);
			launcherIcon->cmd = strdup(cmd->data);
			launcher->list_icons = g_slist_append(launcher->list_icons, launcherIcon);
			path = g_slist_next(path);
			cmd = g_slist_next(cmd);
		}
	}
}


void cleanup_launcher()
{
	int i;

	for (i = 0 ; i < nb_panel ; i++) {
		Panel *panel = &panel1[i];
		Launcher *launcher = &panel->launcher;
		free_area(&launcher->area);
		GSList *l;
		for (l = launcher->list_icons; l ; l = l->next) {
			LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
			if (launcherIcon && launcherIcon->icon) {
				imlib_context_set_image (launcherIcon->icon);
				imlib_free_image();
			}
			free(launcherIcon->cmd);
			free(launcherIcon);
		}
		g_slist_free(launcher->list_icons);
		for (l = launcher->list_icon_paths; l ; l = l->next) {
			free(l->data);
		}
		g_slist_free(launcher->list_icon_paths);
		for (l = launcher->list_cmds; l ; l = l->next) {
			free(l->data);
		}
		g_slist_free(launcher->list_cmds);
	}
	launcher_enabled = 0;
}

void resize_launcher(void *obj)
{
	Launcher *launcher = obj;
	Panel *panel = launcher->area.panel;
	GSList *l;
	int count, icon_size;
	int icons_per_column=1, icons_per_row=1, marging=0;

	if (panel_horizontal)
		icon_size = launcher->area.height;
	else
		icon_size = launcher->area.width;
	icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
	if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
		icon_size = launcher_max_icon_size;
	
	count = 0;
	for (l = launcher->list_icons; l ; l = l->next) {
		count++;
	}

	if (panel_horizontal) {
		if (!count) launcher->area.width = 0;
		else {
			int height = launcher->area.height - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
			// here icons_per_column always higher than 0
			icons_per_column = (height+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
			marging = height - (icons_per_column-1)*(icon_size+launcher->area.paddingx) - icon_size;
			icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
			launcher->area.width = (2 * launcher->area.bg->border.width) + (2 * launcher->area.paddingxlr) + (icon_size * icons_per_row) + ((icons_per_row-1) * launcher->area.paddingx);
		}

		launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
		launcher->area.posy = panel->area.bg->border.width;
	}
	else {
		if (!count) launcher->area.height = 0;
		else {
			int width = launcher->area.width - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
			// here icons_per_row always higher than 0
			icons_per_row = (width+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
			marging = width - (icons_per_row-1)*(icon_size+launcher->area.paddingx) - icon_size;
			icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
			launcher->area.height = (2 * launcher->area.bg->border.width) + (2 * launcher->area.paddingxlr) + (icon_size * icons_per_column) + ((icons_per_column-1) * launcher->area.paddingx);
		}

		launcher->area.posx = panel->area.bg->border.width;
		launcher->area.posy = panel->area.height - panel->area.bg->border.width - panel->area.paddingxlr - launcher->area.height;
	}

	int i, posx, posy;
	int start = launcher->area.bg->border.width + launcher->area.paddingy;// +marging/2;
	if (panel_horizontal) {
		posy = start;
		posx = launcher->area.bg->border.width + launcher->area.paddingxlr;
	}
	else {
		posx = start;
		posy = launcher->area.bg->border.width + launcher->area.paddingxlr;
	}

	for (i=1, l = launcher->list_icons; l ; i++, l = l->next) {
		LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
		
		launcherIcon->y = posy;
		launcherIcon->x = posx;
		launcherIcon->width = icon_size;
		launcherIcon->height = icon_size;
		if (panel_horizontal) {
			if (i % icons_per_column)
				posy += icon_size + launcher->area.paddingx;
			else {
				posy = start;
				posx += (icon_size + launcher->area.paddingx);
			}
		}
		else {
			if (i % icons_per_row)
				posx += icon_size + launcher->area.paddingx;
			else {
				posx = start;
				posy += (icon_size + launcher->area.paddingx);
			}
		}
	}
	
	// resize force the redraw
	launcher->area.redraw = 1;
}


void draw_launcher(void *obj, cairo_t *c)
{
	Launcher *launcher = obj;
	GSList *l;
	if (launcher->list_icons == 0) return;

	for (l = launcher->list_icons; l ; l = l->next) {
		LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
		int pos_x = launcherIcon->x;
		int pos_y = launcherIcon->y;
		int width = launcherIcon->width;
		int height = launcherIcon->height;
		Imlib_Image icon_scaled;
		if (launcherIcon->icon) {
			imlib_context_set_image (launcherIcon->icon);
			icon_scaled = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), width, height);
		} else {
			icon_scaled = imlib_create_image(width, height);
			imlib_context_set_image (icon_scaled);
			imlib_context_set_color(255, 255, 255, 255);
			imlib_image_fill_rectangle(0, 0, width, height);
		}

		// Render
		imlib_context_set_image (icon_scaled);
		if (server.real_transparency) {
			render_image(launcher->area.pix, pos_x, pos_y, imlib_image_get_width(), imlib_image_get_height() );
		}
		else {
			imlib_context_set_drawable(launcher->area.pix);
			imlib_render_image_on_drawable (pos_x, pos_y);
		}
		imlib_free_image();
	}
}

void launcher_action(LauncherIcon *icon)
{
	char *cmd = malloc(strlen(icon->cmd) + 10);
	sprintf(cmd, "(%s&)", icon->cmd);
	if (cmd) {
		pid_t pid;
		pid = fork();
		if (pid == 0) {
			// change for the fork the signal mask
			//			sigset_t sigset;
			//			sigprocmask(SIG_SETMASK, &sigset, 0);
			//			sigprocmask(SIG_UNBLOCK, &sigset, 0);
			// exec
			execl("/bin/sh", "/bin/sh", "-c", cmd, NULL);
			_exit(0);
		}
	}
}