]> Dogcows Code - chaz/tint2/blob - src/launcher/launcher.c
Added .desktop file parsing (but not for UTF8...) and skeleton for loading icons...
[chaz/tint2] / src / launcher / launcher.c
1 /**************************************************************************
2 * Tint2 : launcher
3 *
4 * Copyright (C) 2010 (mrovi@interfete-web-club.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 **************************************************************************/
18
19 #include <string.h>
20 #include <stdio.h>
21 #include <cairo.h>
22 #include <cairo-xlib.h>
23 #include <pango/pangocairo.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <stdlib.h>
27
28 #include "window.h"
29 #include "server.h"
30 #include "area.h"
31 #include "panel.h"
32 #include "taskbar.h"
33 #include "launcher.h"
34
35 int launcher_enabled;
36 int launcher_max_icon_size;
37 GSList *icon_themes = 0;
38
39 void default_launcher()
40 {
41 launcher_enabled = 0;
42 launcher_max_icon_size = 0;
43 }
44
45
46 void init_launcher()
47 {
48 }
49
50
51 void init_launcher_panel(void *p)
52 {
53 Panel *panel =(Panel*)p;
54 Launcher *launcher = &panel->launcher;
55
56 launcher->area.parent = p;
57 launcher->area.panel = p;
58 launcher->area._draw_foreground = draw_launcher;
59 launcher->area._resize = resize_launcher;
60 launcher->area.resize = 1;
61 launcher->area.redraw = 1;
62 launcher->area.on_screen = 1;
63
64 if (panel_horizontal) {
65 // panel horizonal => fixed height and posy
66 launcher->area.posy = panel->area.bg->border.width + panel->area.paddingy;
67 launcher->area.height = panel->area.height - (2 * launcher->area.posy);
68 }
69 else {
70 // panel vertical => fixed width, height, posy and posx
71 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
72 launcher->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
73 }
74
75 // Load icons
76 {
77 GSList* path = launcher->list_icon_paths;
78 GSList* cmd = launcher->list_cmds;
79 while (path != NULL && cmd != NULL)
80 {
81 LauncherIcon *launcherIcon = malloc(sizeof(LauncherIcon));
82 launcherIcon->icon = imlib_load_image(path->data);
83 launcherIcon->cmd = strdup(cmd->data);
84 launcher->list_icons = g_slist_append(launcher->list_icons, launcherIcon);
85 path = g_slist_next(path);
86 cmd = g_slist_next(cmd);
87 }
88 }
89 }
90
91
92 void cleanup_launcher()
93 {
94 int i;
95
96 for (i = 0 ; i < nb_panel ; i++) {
97 Panel *panel = &panel1[i];
98 Launcher *launcher = &panel->launcher;
99 free_area(&launcher->area);
100 GSList *l;
101 for (l = launcher->list_icons; l ; l = l->next) {
102 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
103 if (launcherIcon && launcherIcon->icon) {
104 imlib_context_set_image (launcherIcon->icon);
105 imlib_free_image();
106 }
107 free(launcherIcon->cmd);
108 free(launcherIcon);
109 }
110 g_slist_free(launcher->list_icons);
111 for (l = launcher->list_icon_paths; l ; l = l->next) {
112 free(l->data);
113 }
114 g_slist_free(launcher->list_icon_paths);
115 for (l = launcher->list_cmds; l ; l = l->next) {
116 free(l->data);
117 }
118 g_slist_free(launcher->list_cmds);
119 }
120 launcher_enabled = 0;
121 }
122
123 void resize_launcher(void *obj)
124 {
125 Launcher *launcher = obj;
126 Panel *panel = launcher->area.panel;
127 GSList *l;
128 int count, icon_size;
129 int icons_per_column=1, icons_per_row=1, marging=0;
130
131 if (panel_horizontal)
132 icon_size = launcher->area.height;
133 else
134 icon_size = launcher->area.width;
135 icon_size = icon_size - (2 * launcher->area.bg->border.width) - (2 * launcher->area.paddingy);
136 if (launcher_max_icon_size > 0 && icon_size > launcher_max_icon_size)
137 icon_size = launcher_max_icon_size;
138
139 count = 0;
140 for (l = launcher->list_icons; l ; l = l->next) {
141 count++;
142 }
143
144 if (panel_horizontal) {
145 if (!count) launcher->area.width = 0;
146 else {
147 int height = launcher->area.height - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
148 // here icons_per_column always higher than 0
149 icons_per_column = (height+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
150 marging = height - (icons_per_column-1)*(icon_size+launcher->area.paddingx) - icon_size;
151 icons_per_row = count / icons_per_column + (count%icons_per_column != 0);
152 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);
153 }
154
155 launcher->area.posx = panel->area.bg->border.width + panel->area.paddingxlr;
156 launcher->area.posy = panel->area.bg->border.width;
157 }
158 else {
159 if (!count) launcher->area.height = 0;
160 else {
161 int width = launcher->area.width - 2*launcher->area.bg->border.width - 2*launcher->area.paddingy;
162 // here icons_per_row always higher than 0
163 icons_per_row = (width+launcher->area.paddingx) / (icon_size+launcher->area.paddingx);
164 marging = width - (icons_per_row-1)*(icon_size+launcher->area.paddingx) - icon_size;
165 icons_per_column = count / icons_per_row+ (count%icons_per_row != 0);
166 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);
167 }
168
169 launcher->area.posx = panel->area.bg->border.width;
170 launcher->area.posy = panel->area.height - panel->area.bg->border.width - panel->area.paddingxlr - launcher->area.height;
171 }
172
173 int i, posx, posy;
174 int start = launcher->area.bg->border.width + launcher->area.paddingy;// +marging/2;
175 if (panel_horizontal) {
176 posy = start;
177 posx = launcher->area.bg->border.width + launcher->area.paddingxlr;
178 }
179 else {
180 posx = start;
181 posy = launcher->area.bg->border.width + launcher->area.paddingxlr;
182 }
183
184 for (i=1, l = launcher->list_icons; l ; i++, l = l->next) {
185 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
186
187 launcherIcon->y = posy;
188 launcherIcon->x = posx;
189 launcherIcon->width = icon_size;
190 launcherIcon->height = icon_size;
191 if (panel_horizontal) {
192 if (i % icons_per_column)
193 posy += icon_size + launcher->area.paddingx;
194 else {
195 posy = start;
196 posx += (icon_size + launcher->area.paddingx);
197 }
198 }
199 else {
200 if (i % icons_per_row)
201 posx += icon_size + launcher->area.paddingx;
202 else {
203 posx = start;
204 posy += (icon_size + launcher->area.paddingx);
205 }
206 }
207 }
208
209 // resize force the redraw
210 launcher->area.redraw = 1;
211 }
212
213
214 void draw_launcher(void *obj, cairo_t *c)
215 {
216 Launcher *launcher = obj;
217 GSList *l;
218 if (launcher->list_icons == 0) return;
219
220 for (l = launcher->list_icons; l ; l = l->next) {
221 LauncherIcon *launcherIcon = (LauncherIcon*)l->data;
222 int pos_x = launcherIcon->x;
223 int pos_y = launcherIcon->y;
224 int width = launcherIcon->width;
225 int height = launcherIcon->height;
226 Imlib_Image icon_scaled;
227 if (launcherIcon->icon) {
228 imlib_context_set_image (launcherIcon->icon);
229 icon_scaled = imlib_create_cropped_scaled_image(0, 0, imlib_image_get_width(), imlib_image_get_height(), width, height);
230 } else {
231 icon_scaled = imlib_create_image(width, height);
232 imlib_context_set_image (icon_scaled);
233 imlib_context_set_color(255, 255, 255, 255);
234 imlib_image_fill_rectangle(0, 0, width, height);
235 }
236
237 // Render
238 imlib_context_set_image (icon_scaled);
239 if (server.real_transparency) {
240 render_image(launcher->area.pix, pos_x, pos_y, imlib_image_get_width(), imlib_image_get_height() );
241 }
242 else {
243 imlib_context_set_drawable(launcher->area.pix);
244 imlib_render_image_on_drawable (pos_x, pos_y);
245 }
246 imlib_free_image();
247 }
248 }
249
250 void launcher_action(LauncherIcon *icon)
251 {
252 char *cmd = malloc(strlen(icon->cmd) + 10);
253 sprintf(cmd, "(%s&)", icon->cmd);
254 tint_exec(cmd);
255 free(cmd);
256 }
257
258 /***************** Freedesktop app.desktop and icon theme handling *********************/
259 /* http://standards.freedesktop.org/desktop-entry-spec/ */
260 /* http://standards.freedesktop.org/icon-theme-spec/ */
261
262 int parse_dektop_line(char *line, char **key, char **value)
263 {
264 char *p;
265 int found = 0;
266 *key = line;
267 for (p = line; *p; p++) {
268 if (*p == '=') {
269 *value = p + 1;
270 *p = 0;
271 found = 1;
272 break;
273 }
274 }
275 if (!found)
276 return 0;
277 if (found && (strlen(*key) == 0 || strlen(*value) == 0))
278 return 0;
279 return 1;
280 }
281
282 void expand_exec(DesktopEntry *entry, const char *path)
283 {
284 // Expand % in exec
285 // %i -> --icon Icon
286 // %c -> Name
287 // %k -> path
288 if (entry->exec) {
289 char *exec2 = malloc(strlen(entry->exec) + strlen(entry->name) + strlen(entry->icon) + 100);
290 char *p, *q;
291 // p will never point to an escaped char
292 for (p = entry->exec, q = exec2; *p; p++, q++) {
293 *q = *p; // Copy
294 if (*p == '\\') {
295 p++, q++;
296 // Copy the escaped char
297 if (*p == '%') // For % we delete the backslash, i.e. write % over it
298 q--;
299 *q = *p;
300 if (!*p) break;
301 continue;
302 }
303 if (*p == '%') {
304 p++;
305 if (!*p) break;
306 if (*p == 'i' && entry->icon != NULL) {
307 sprintf(q, "--icon '%s'", entry->icon);
308 q += strlen("--icon ''");
309 q += strlen(entry->icon);
310 q--; // To balance the q++ in the for
311 } else if (*p == 'c' && entry->name != NULL) {
312 sprintf(q, "'%s'", entry->name);
313 q += strlen("''");
314 q += strlen(entry->name);
315 q--; // To balance the q++ in the for
316 } else if (*p == 'c') {
317 sprintf(q, "'%s'", path);
318 q += strlen("''");
319 q += strlen(path);
320 q--; // To balance the q++ in the for
321 } else {
322 // We don't care about other expansions
323 q--; // Delete the last % from q
324 }
325 continue;
326 }
327 }
328 *q = '\0';
329 free(entry->exec);
330 entry->exec = exec2;
331 }
332 }
333
334 int launcher_read_desktop_file(const char *path, DesktopEntry *entry)
335 {
336 FILE *fp;
337 char line[4096];
338 char *buffer;
339 char *key, *value;
340
341 entry->name = entry->icon = entry->exec = NULL;
342
343 if ((fp = fopen(path, "r")) == NULL) {
344 fprintf(stderr, "Could not open file %s\n", path);
345 return 0;
346 }
347
348 buffer = line;
349 while (fgets(buffer, sizeof(line) - (buffer - line), fp) != NULL) {
350 //TODO use UTF8 capable strlen
351 int len = strlen(buffer);
352 int total_len = strlen(line);
353 if (!g_utf8_validate(buffer, total_len, NULL)) {
354 // Not a real newline, read more
355 buffer += len;
356 if (sizeof(line) - (buffer - line) < 2) {
357 fprintf(stderr, "%s %d: line too long (%s)\n", __FILE__, __LINE__, path);
358 break;
359 } else {
360 continue;
361 }
362 }
363 // We have a valid line
364 buffer[len-1] = '\0';
365 buffer = line;
366 if (parse_dektop_line(line, &key, &value)) {
367 if (strcmp(key, "Name") == 0) {
368 //TODO use UTF-8 capable strdup
369 entry->name = strdup(value);
370 } else if (strcmp(key, "Exec") == 0) {
371 entry->exec = strdup(value);
372 } else if (strcmp(key, "Icon") == 0) {
373 //TODO use UTF-8 capable strdup
374 entry->icon = strdup(value);
375 }
376 }
377 }
378 fclose (fp);
379 // From this point:
380 // entry->name, entry->icon, entry->exec will never be empty strings (can be NULL though)
381
382 expand_exec(entry, path);
383
384 return 1;
385 }
386
387 void test_launcher_read_desktop_file()
388 {
389 DesktopEntry entry;
390 launcher_read_desktop_file("/usr/share/applications/firefox.desktop", &entry);
391 printf("Name:%s Icon:%s Exec:%s\n", entry.name, entry.icon, entry.exec);
392 }
393
394 IconTheme *load_theme(char *name)
395 {
396 //TODO
397 // Look for name/index.theme in $HOME/.icons, /usr/share/icons, /usr/share/pixmaps (stop at the first found)
398 // Parse index.theme -> list of IconThemeDir with attributes
399 // Return IconTheme struct
400 return NULL;
401 }
402
403 // Populates the icon_themes list
404 void launcher_load_themes()
405 {
406 //TODO load the user theme, all the inherited themes recursively (DFS), and the hicolor theme
407 // avoid inheritance loops
408 }
409
410 // Returns the full path to an icon file (or NULL) given the icon name
411 char *icon_path(const char *name)
412 {
413 //TODO
414 /*
415
416 // Stage 1: exact size match
417 LookupIcon (iconname, size):
418 for each subdir in $(theme subdir list) { // <---- each subdir in each theme
419 for each directory in $(basename list) {
420 for extension in ("png", "svg", "xpm") {
421 if DirectoryMatchesSize(subdir, size) {
422 filename = directory/$(themename)/subdirectory/iconname.extension
423 if exist filename
424 return filename
425 }
426 }
427 }
428 }
429
430 // Stage 2: best size match
431 minimal_size = MAXINT
432 for each subdir in $(theme subdir list) { // <---- each subdir in each theme
433 for each directory in $(basename list) {
434 for extension in ("png", "svg", "xpm") {
435 filename = directory/$(themename)/subdirectory/iconname.extension
436 if exist filename and DirectorySizeDistance(subdir, size) < minimal_size
437 closest_filename = filename
438 minimal_size = DirectorySizeDistance(subdir, size)
439 }
440 }
441 }
442 if closest_filename set
443 return closest_filename
444
445 // Stage 3: look in unthemed icons
446 for each directory in $(basename list) { // <---- $HOME/.icons, /usr/share/icons, /usr/share/pixmaps
447 for extension in ("png", "svg", "xpm") {
448 if exists directory/iconname.extension
449 return directory/iconname.extension
450 }
451 }
452
453 return failed icon lookup
454
455 // With the following helper functions:
456
457 DirectoryMatchesSize(subdir, iconsize):
458 read Type and size data from subdir
459 if Type is Fixed
460 return Size == iconsize
461 if Type is Scaled
462 return MinSize <= iconsize <= MaxSize
463 if Type is Threshold
464 return Size - Threshold <= iconsize <= Size + Threshold
465
466 DirectorySizeDistance(subdir, size):
467 read Type and size data from subdir
468 if Type is Fixed
469 return abs(Size - iconsize)
470 if Type is Scaled
471 if iconsize < MinSize
472 return MinSize - iconsize
473 if iconsize > MaxSize
474 return iconsize - MaxSize
475 return 0
476 if Type is Threshold
477 if iconsize < Size - Threshold
478 return MinSize - iconsize
479 if iconsize > Size + Threshold
480 return iconsize - MaxSize
481 return 0
482 */
483 }
This page took 0.056821 seconds and 5 git commands to generate.