]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
fixed bug with "task on all desktop" (issue 39), fixed memory corruption
[chaz/tint2] / src / taskbar / task.c
1 /**************************************************************************
2 *
3 * Tint2 : task
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
28 #include <Imlib2.h>
29
30 #include "window.h"
31 #include "task.h"
32 #include "server.h"
33 #include "panel.h"
34
35
36
37 void add_task (Window win)
38 {
39 Task *new_tsk;
40 int desktop, monitor;
41
42 if (!win) return;
43 if (window_is_hidden (win) || win == window.main_win) return;
44
45 new_tsk = malloc(sizeof(Task));
46 new_tsk->win = win;
47 new_tsk->title = 0;
48 new_tsk->icon_data = 0;
49
50 get_icon(new_tsk);
51 get_title(new_tsk);
52 memcpy(&new_tsk->area, &g_task.area, sizeof(Area));
53 desktop = window_get_desktop (new_tsk->win);
54 monitor = window_get_monitor (new_tsk->win);
55
56 //if (panel.mode == MULTI_MONITOR) monitor = window_get_monitor (new_tsk->win);
57 //else monitor = 0;
58 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
59 XSelectInput (server.dsp, new_tsk->win, PropertyChangeMask|StructureNotifyMask);
60
61 Taskbar *tskbar;
62 if (desktop == 0xFFFFFFFF) {
63 tskbar = &panel.taskbar[index(0, monitor)];
64 new_tsk->all_desktop = 1;
65 }
66 else {
67 tskbar = &panel.taskbar[index(desktop, monitor)];
68 new_tsk->all_desktop = 0;
69 }
70
71 //printf("add_task %d %s\n", index(desktop, monitor), new_tsk->title);
72 new_tsk->area.parent = tskbar;
73 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk);
74
75 if (resize_tasks (tskbar))
76 set_redraw (&tskbar->area);
77
78 if (desktop == 0xFFFFFFFF) {
79 // task on all desktop
80 int i;
81 Task *new_tsk2;
82 for (i = 1 ; i < server.nb_desktop ; i++) {
83 new_tsk2 = malloc(sizeof(Task));
84 memcpy(new_tsk2, new_tsk, sizeof(Task));
85
86 new_tsk2->title = 0;
87 new_tsk2->icon_data = 0;
88 get_icon(new_tsk2);
89 get_title(new_tsk2);
90
91 tskbar = &panel.taskbar[index(i, monitor)];
92 new_tsk2->area.parent = tskbar;
93 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk2);
94
95 if (resize_tasks (tskbar))
96 set_redraw (&tskbar->area);
97 }
98 }
99 }
100
101
102 void remove_task (Task *tsk)
103 {
104 if (!tsk) return;
105
106 Taskbar *tskbar;
107 tskbar = (Taskbar*)tsk->area.parent;
108 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk);
109 resize_tasks (tskbar);
110 set_redraw (&tskbar->area);
111 //printf("remove_task %d %s\n", index(tskbar->desktop, tskbar->monitor), tsk->title);
112
113 if (tsk == panel.task_active)
114 panel.task_active = 0;
115 if (tsk == panel.task_drag)
116 panel.task_drag = 0;
117
118 if (tsk->title) {
119 free (tsk->title);
120 tsk->title = 0;
121 }
122 if (tsk->icon_data) {
123 free (tsk->icon_data);
124 tsk->icon_data = 0;
125 }
126
127 XFreePixmap (server.dsp, tsk->area.pix.pmap);
128 XFreePixmap (server.dsp, tsk->area.pix_active.pmap);
129 free(tsk);
130 }
131
132
133 void get_title(Task *tsk)
134 {
135 if (!g_task.text) return;
136
137 char *title, *name;
138
139 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
140 if (!name || !strlen(name)) {
141 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
142 if (!name || !strlen(name)) {
143 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
144 if (!name || !strlen(name)) {
145 name = malloc(10);
146 strcpy(name, "Untitled");
147 }
148 }
149 }
150
151 // add space before title
152 title = malloc(strlen(name)+2);
153 if (g_task.icon) strcpy(title, " ");
154 else title[0] = 0;
155 strcat(title, name);
156 if (name) XFree (name);
157
158 if (tsk->title)
159 free(tsk->title);
160 tsk->title = title;
161 }
162
163
164 void get_icon (Task *tsk)
165 {
166 if (!g_task.icon) return;
167
168 long *data;
169 int num;
170
171 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &num);
172 if (data) {
173 printf("get_icon plein\n");
174 // ARGB
175 int w, h;
176 long *tmp_data;
177 tmp_data = get_best_icon (data, get_icon_count (data, num), num, &w, &h, g_task.icon_size1);
178
179 tsk->icon_width = w;
180 tsk->icon_height = h;
181 tsk->icon_data = malloc (w * h * sizeof (long));
182 memcpy (tsk->icon_data, tmp_data, w * h * sizeof (long));
183
184 XFree (data);
185 }
186 else {
187 printf("get_icon vide\n");
188 //XWMHints *hints;
189 //hints = XGetWMHints(server.dsp, tkwin);
190 //if (hints != NULL) {
191 // XFree(hints);
192 //}
193 // XChangeProperty (display, windowH, XInternAtom (display, "_NET_WM_ICON", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char*) data, dataSize);
194 return;
195 }
196 }
197
198
199 void draw_task_icon (Task *tsk, int text_width, int active)
200 {
201 if (tsk->icon_data == 0) get_icon (tsk);
202 if (tsk->icon_data == 0) return;
203
204 Pixmap *pmap = (active == 0) ? (&tsk->area.pix.pmap) : (&tsk->area.pix_active.pmap);
205
206 /* Find pos */
207 int pos_x;
208 if (g_task.centered) {
209 if (g_task.text)
210 pos_x = (tsk->area.width - text_width - g_task.icon_size1) / 2;
211 else
212 pos_x = (tsk->area.width - g_task.icon_size1) / 2;
213 }
214 else pos_x = g_task.area.paddingx + g_task.area.pix.border.width;
215
216 /* Render */
217 Imlib_Image icon;
218 Imlib_Color_Modifier cmod;
219 DATA8 red[256], green[256], blue[256], alpha[256];
220
221 // TODO: cpu improvement : compute only when icon changed
222 DATA32 *data;
223 /* do we have 64bit? => long = 8bit */
224 if (sizeof(long) != 4) {
225 int length = tsk->icon_width * tsk->icon_height;
226 data = malloc(sizeof(DATA32) * length);
227 int i;
228 for (i = 0; i < length; ++i)
229 data[i] = tsk->icon_data[i];
230 }
231 else data = (DATA32 *) tsk->icon_data;
232
233 icon = imlib_create_image_using_data (tsk->icon_width, tsk->icon_height, data);
234 imlib_context_set_image (icon);
235 imlib_context_set_drawable (*pmap);
236
237 cmod = imlib_create_color_modifier ();
238 imlib_context_set_color_modifier (cmod);
239 imlib_image_set_has_alpha (1);
240 imlib_get_color_modifier_tables (red, green, blue, alpha);
241
242 int i, opacity;
243 opacity = (active == 0) ? (255*g_task.font.alpha) : (255*g_task.font_active.alpha);
244 for (i = 127; i < 256; i++) alpha[i] = opacity;
245
246 imlib_set_color_modifier_tables (red, green, blue, alpha);
247
248 //imlib_render_image_on_drawable (pos_x, pos_y);
249 imlib_render_image_on_drawable_at_size (pos_x, g_task.icon_posy, g_task.icon_size1, g_task.icon_size1);
250
251 imlib_free_color_modifier ();
252 imlib_free_image ();
253 if (sizeof(long) != 4) free(data);
254 }
255
256
257 void draw_foreground_task (void *obj, cairo_t *c, int active)
258 {
259 Task *tsk = obj;
260 PangoLayout *layout;
261 config_color *config_text;
262 int width, height;
263
264 if (g_task.text) {
265 /* Layout */
266 layout = pango_cairo_create_layout (c);
267 pango_layout_set_font_description (layout, g_task.font_desc);
268 pango_layout_set_text (layout, tsk->title, -1);
269
270 /* Drawing width and Cut text */
271 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
272 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
273
274 /* Center text */
275 if (g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
276 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
277
278 pango_layout_get_pixel_size (layout, &width, &height);
279
280 if (active) config_text = &g_task.font_active;
281 else config_text = &g_task.font;
282
283 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
284
285 pango_cairo_update_layout (c, layout);
286 cairo_move_to (c, g_task.text_posx, g_task.text_posy);
287 pango_cairo_show_layout (c, layout);
288
289 if (g_task.font_shadow) {
290 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
291 pango_cairo_update_layout (c, layout);
292 cairo_move_to (c, g_task.text_posx + 1, g_task.text_posy + 1);
293 pango_cairo_show_layout (c, layout);
294 }
295 g_object_unref (layout);
296 }
297
298 if (g_task.icon) {
299 // icon use same opacity as text
300 draw_task_icon (tsk, width, active);
301 }
302 }
303
This page took 0.048251 seconds and 5 git commands to generate.