]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
patch from Robert Escriva and fixed MULTI_MONITOR mode with task_on_all_desktop
[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, all_desktop;
41
42 if (!win) return;
43 if (window_is_hidden (win) || win == window.main_win) return;
44
45 desktop = window_get_desktop (win);
46 if (desktop == 0xFFFFFFFF) {
47 desktop = 0;
48 all_desktop = 1;
49 }
50 else
51 all_desktop = 0;
52
53 if (panel.mode == MULTI_MONITOR) monitor = window_get_monitor (win);
54 else monitor = 0;
55
56 deb:
57 new_tsk = malloc(sizeof(Task));
58 new_tsk->win = win;
59 new_tsk->all_desktop = all_desktop;
60 new_tsk->title = 0;
61 new_tsk->icon_data = 0;
62
63 get_icon(new_tsk);
64 get_title(new_tsk);
65 memcpy(&new_tsk->area, &g_task.area, sizeof(Area));
66
67 //printf("task %s : desktop %d, monitor %d\n", new_tsk->title, desktop, monitor);
68 XSelectInput (server.dsp, new_tsk->win, PropertyChangeMask|StructureNotifyMask);
69
70 Taskbar *tskbar = &panel.taskbar[index(desktop, monitor)];
71 new_tsk->area.parent = tskbar;
72 tskbar->area.list = g_slist_append(tskbar->area.list, new_tsk);
73
74 if (resize_tasks (tskbar))
75 set_redraw (&tskbar->area);
76
77 if (all_desktop) {
78 desktop++;
79 if (desktop < server.nb_desktop)
80 goto deb;
81 }
82 }
83
84
85 void remove_task (Task *tsk)
86 {
87 if (!tsk) return;
88
89 Task *tsk2 = tsk;
90 Taskbar *tskbar;
91 Window win = tsk->win;
92 int desktop = 0, all_desktop = tsk->all_desktop;
93 int monitor = ((Taskbar*)tsk->area.parent)->monitor;
94
95 deb:
96 if (all_desktop) {
97 tskbar = &panel.taskbar[index(desktop, monitor)];
98 GSList *l0;
99 for (l0 = tskbar->area.list; l0 ; ) {
100 tsk2 = l0->data;
101 l0 = l0->next;
102 if (win == tsk2->win)
103 break;
104 }
105 }
106 else
107 tskbar = (Taskbar*)tsk->area.parent;
108
109 tskbar->area.list = g_slist_remove(tskbar->area.list, tsk2);
110 resize_tasks (tskbar);
111 set_redraw (&tskbar->area);
112 //printf("remove_task %d %s\n", index(tskbar->desktop, tskbar->monitor), tsk->title);
113
114 if (tsk2 == panel.task_active)
115 panel.task_active = 0;
116 if (tsk2 == panel.task_drag)
117 panel.task_drag = 0;
118
119 if (tsk2->title)
120 free (tsk2->title);
121 if (tsk2->icon_data)
122 free (tsk2->icon_data);
123
124 XFreePixmap (server.dsp, tsk2->area.pix.pmap);
125 XFreePixmap (server.dsp, tsk2->area.pix_active.pmap);
126 free(tsk2);
127
128 if (all_desktop) {
129 desktop++;
130 if (desktop < server.nb_desktop)
131 goto deb;
132 }
133 }
134
135
136 void get_title(Task *tsk)
137 {
138 if (!g_task.text) return;
139
140 char *title, *name;
141
142 name = server_get_property (tsk->win, server.atom._NET_WM_VISIBLE_NAME, server.atom.UTF8_STRING, 0);
143 if (!name || !strlen(name)) {
144 name = server_get_property (tsk->win, server.atom._NET_WM_NAME, server.atom.UTF8_STRING, 0);
145 if (!name || !strlen(name)) {
146 name = server_get_property (tsk->win, server.atom.WM_NAME, XA_STRING, 0);
147 if (!name || !strlen(name)) {
148 name = malloc(10);
149 strcpy(name, "Untitled");
150 }
151 }
152 }
153
154 // add space before title
155 title = malloc(strlen(name)+2);
156 if (g_task.icon) strcpy(title, " ");
157 else title[0] = 0;
158 strcat(title, name);
159 if (name) XFree (name);
160
161 if (tsk->title)
162 free(tsk->title);
163 tsk->title = title;
164 }
165
166
167 void get_icon (Task *tsk)
168 {
169 if (!g_task.icon) return;
170
171 long *data;
172 int num;
173
174 data = server_get_property (tsk->win, server.atom._NET_WM_ICON, XA_CARDINAL, &num);
175 if (data) {
176 printf("get_icon plein\n");
177 // ARGB
178 int w, h;
179 long *tmp_data;
180 tmp_data = get_best_icon (data, get_icon_count (data, num), num, &w, &h, g_task.icon_size1);
181
182 tsk->icon_width = w;
183 tsk->icon_height = h;
184 tsk->icon_data = malloc (w * h * sizeof (long));
185 memcpy (tsk->icon_data, tmp_data, w * h * sizeof (long));
186
187 XFree (data);
188 }
189 else {
190 printf("get_icon vide\n");
191 //XWMHints *hints;
192 //hints = XGetWMHints(server.dsp, tkwin);
193 //if (hints != NULL) {
194 // XFree(hints);
195 //}
196 // XChangeProperty (display, windowH, XInternAtom (display, "_NET_WM_ICON", False), XA_CARDINAL, 32, PropModeReplace, (unsigned char*) data, dataSize);
197 return;
198 }
199 }
200
201
202 void draw_task_icon (Task *tsk, int text_width, int active)
203 {
204 if (tsk->icon_data == 0) get_icon (tsk);
205 if (tsk->icon_data == 0) return;
206
207 Pixmap *pmap = (active == 0) ? (&tsk->area.pix.pmap) : (&tsk->area.pix_active.pmap);
208
209 /* Find pos */
210 int pos_x;
211 if (g_task.centered) {
212 if (g_task.text)
213 pos_x = (tsk->area.width - text_width - g_task.icon_size1) / 2;
214 else
215 pos_x = (tsk->area.width - g_task.icon_size1) / 2;
216 }
217 else pos_x = g_task.area.paddingx + g_task.area.pix.border.width;
218
219 /* Render */
220 Imlib_Image icon;
221 Imlib_Color_Modifier cmod;
222 DATA8 red[256], green[256], blue[256], alpha[256];
223
224 // TODO: cpu improvement : compute only when icon changed
225 DATA32 *data;
226 /* do we have 64bit? => long = 8bit */
227 if (sizeof(long) != 4) {
228 int length = tsk->icon_width * tsk->icon_height;
229 data = malloc(sizeof(DATA32) * length);
230 int i;
231 for (i = 0; i < length; ++i)
232 data[i] = tsk->icon_data[i];
233 }
234 else data = (DATA32 *) tsk->icon_data;
235
236 icon = imlib_create_image_using_data (tsk->icon_width, tsk->icon_height, data);
237 imlib_context_set_image (icon);
238 imlib_context_set_drawable (*pmap);
239
240 cmod = imlib_create_color_modifier ();
241 imlib_context_set_color_modifier (cmod);
242 imlib_image_set_has_alpha (1);
243 imlib_get_color_modifier_tables (red, green, blue, alpha);
244
245 int i, opacity;
246 opacity = (active == 0) ? (255*g_task.font.alpha) : (255*g_task.font_active.alpha);
247 for (i = 127; i < 256; i++) alpha[i] = opacity;
248
249 imlib_set_color_modifier_tables (red, green, blue, alpha);
250
251 //imlib_render_image_on_drawable (pos_x, pos_y);
252 imlib_render_image_on_drawable_at_size (pos_x, g_task.icon_posy, g_task.icon_size1, g_task.icon_size1);
253
254 imlib_free_color_modifier ();
255 imlib_free_image ();
256 if (sizeof(long) != 4) free(data);
257 }
258
259
260 void draw_foreground_task (void *obj, cairo_t *c, int active)
261 {
262 Task *tsk = obj;
263 PangoLayout *layout;
264 config_color *config_text;
265 int width, height;
266
267 if (g_task.text) {
268 /* Layout */
269 layout = pango_cairo_create_layout (c);
270 pango_layout_set_font_description (layout, g_task.font_desc);
271 pango_layout_set_text (layout, tsk->title, -1);
272
273 /* Drawing width and Cut text */
274 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
275 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
276
277 /* Center text */
278 if (g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
279 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
280
281 pango_layout_get_pixel_size (layout, &width, &height);
282
283 if (active) config_text = &g_task.font_active;
284 else config_text = &g_task.font;
285
286 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
287
288 pango_cairo_update_layout (c, layout);
289 cairo_move_to (c, g_task.text_posx, g_task.text_posy);
290 pango_cairo_show_layout (c, layout);
291
292 if (g_task.font_shadow) {
293 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
294 pango_cairo_update_layout (c, layout);
295 cairo_move_to (c, g_task.text_posx + 1, g_task.text_posy + 1);
296 pango_cairo_show_layout (c, layout);
297 }
298 g_object_unref (layout);
299 }
300
301 if (g_task.icon) {
302 // icon use same opacity as text
303 draw_task_icon (tsk, width, active);
304 }
305 }
306
This page took 0.044813 seconds and 5 git commands to generate.