]> Dogcows Code - chaz/tint2/blob - src/taskbar/task.c
improve multi_monitor mode
[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 = XGetWMHints(server.dsp, tsk->win);
192 if (hints) {
193 if (hints->flags & IconPixmapHint) {
194 }
195 XFree(hints);
196 }
197 }
198 }
199
200
201 void draw_task_icon (Task *tsk, int text_width, int active)
202 {
203 if (tsk->icon_data == 0) get_icon (tsk);
204 if (tsk->icon_data == 0) return;
205
206 Pixmap *pmap = (active == 0) ? (&tsk->area.pix.pmap) : (&tsk->area.pix_active.pmap);
207
208 /* Find pos */
209 int pos_x;
210 if (g_task.centered) {
211 if (g_task.text)
212 pos_x = (tsk->area.width - text_width - g_task.icon_size1) / 2;
213 else
214 pos_x = (tsk->area.width - g_task.icon_size1) / 2;
215 }
216 else pos_x = g_task.area.paddingxlr + g_task.area.pix.border.width;
217
218 /* Render */
219 Imlib_Image icon;
220 Imlib_Color_Modifier cmod;
221 DATA8 red[256], green[256], blue[256], alpha[256];
222
223 // TODO: cpu improvement : compute only when icon changed
224 DATA32 *data;
225 /* do we have 64bit? => long = 8bit */
226 if (sizeof(long) != 4) {
227 int length = tsk->icon_width * tsk->icon_height;
228 data = malloc(sizeof(DATA32) * length);
229 int i;
230 for (i = 0; i < length; ++i)
231 data[i] = tsk->icon_data[i];
232 }
233 else data = (DATA32 *) tsk->icon_data;
234
235 icon = imlib_create_image_using_data (tsk->icon_width, tsk->icon_height, data);
236 imlib_context_set_image (icon);
237 imlib_context_set_drawable (*pmap);
238
239 cmod = imlib_create_color_modifier ();
240 imlib_context_set_color_modifier (cmod);
241 imlib_image_set_has_alpha (1);
242 imlib_get_color_modifier_tables (red, green, blue, alpha);
243
244 int i, opacity;
245 opacity = (active == 0) ? (255*g_task.font.alpha) : (255*g_task.font_active.alpha);
246 for (i = 127; i < 256; i++) alpha[i] = opacity;
247
248 imlib_set_color_modifier_tables (red, green, blue, alpha);
249
250 //imlib_render_image_on_drawable (pos_x, pos_y);
251 imlib_render_image_on_drawable_at_size (pos_x, g_task.icon_posy, g_task.icon_size1, g_task.icon_size1);
252
253 imlib_free_color_modifier ();
254 imlib_free_image ();
255 if (sizeof(long) != 4) free(data);
256 }
257
258
259 void draw_foreground_task (void *obj, cairo_t *c, int active)
260 {
261 Task *tsk = obj;
262 PangoLayout *layout;
263 config_color *config_text;
264 int width, height;
265
266 if (g_task.text) {
267 /* Layout */
268 layout = pango_cairo_create_layout (c);
269 pango_layout_set_font_description (layout, g_task.font_desc);
270 pango_layout_set_text (layout, tsk->title, -1);
271
272 /* Drawing width and Cut text */
273 pango_layout_set_width (layout, ((Taskbar*)tsk->area.parent)->text_width * PANGO_SCALE);
274 pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_END);
275
276 /* Center text */
277 if (g_task.centered) pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
278 else pango_layout_set_alignment (layout, PANGO_ALIGN_LEFT);
279
280 pango_layout_get_pixel_size (layout, &width, &height);
281
282 if (active) config_text = &g_task.font_active;
283 else config_text = &g_task.font;
284
285 cairo_set_source_rgba (c, config_text->color[0], config_text->color[1], config_text->color[2], config_text->alpha);
286
287 pango_cairo_update_layout (c, layout);
288 cairo_move_to (c, g_task.text_posx, g_task.text_posy);
289 pango_cairo_show_layout (c, layout);
290
291 if (g_task.font_shadow) {
292 cairo_set_source_rgba (c, 0.0, 0.0, 0.0, 0.5);
293 pango_cairo_update_layout (c, layout);
294 cairo_move_to (c, g_task.text_posx + 1, g_task.text_posy + 1);
295 pango_cairo_show_layout (c, layout);
296 }
297 g_object_unref (layout);
298 }
299
300 if (g_task.icon) {
301 // icon use same opacity as text
302 draw_task_icon (tsk, width, active);
303 }
304 }
305
This page took 0.047316 seconds and 5 git commands to generate.