]> Dogcows Code - chaz/tint2/blob - src/taskbar/taskbar.c
*fix* minor bugfixes
[chaz/tint2] / src / taskbar / taskbar.c
1 /**************************************************************************
2 *
3 * Tint2 : taskbar
4 *
5 * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 #include <X11/Xatom.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
27 #include <Imlib2.h>
28
29 #include "task.h"
30 #include "taskbar.h"
31 #include "server.h"
32 #include "window.h"
33 #include "panel.h"
34
35
36 GHashTable* win_to_task_table = 0;
37
38 guint win_hash(gconstpointer key) { return (guint)*((Window*)key); }
39 gboolean win_compare(gconstpointer a, gconstpointer b) { return (*((Window*)a) == *((Window*)b)); }
40 void free_ptr_array(gpointer* data) { g_ptr_array_free(data, 1); }
41
42 void init_taskbar()
43 {
44 Panel *panel;
45 int i, j;
46
47 if (win_to_task_table == 0)
48 win_to_task_table = g_hash_table_new_full(win_hash, win_compare, free, free_ptr_array);
49
50 for (i=0 ; i < nb_panel ; i++) {
51 panel = &panel1[i];
52
53 if (panel->taskbar) {
54 free(panel->taskbar);
55 panel->taskbar = 0;
56 }
57
58 // taskbar
59 panel->g_taskbar.area._resize = resize_taskbar;
60 panel->g_taskbar.area.redraw = 1;
61 panel->g_taskbar.area.on_screen = 1;
62 if (panel_horizontal) {
63 panel->g_taskbar.area.posy = panel->area.bg->border.width + panel->area.paddingy;
64 panel->g_taskbar.area.height = panel->area.height - (2 * panel->g_taskbar.area.posy);
65 }
66 else {
67 panel->g_taskbar.area.posx = panel->area.bg->border.width + panel->area.paddingy;
68 panel->g_taskbar.area.width = panel->area.width - (2 * panel->g_taskbar.area.posx);
69 }
70
71 // task
72 panel->g_task.area._draw_foreground = draw_task;
73 panel->g_task.area.redraw = 1;
74 panel->g_task.area.on_screen = 1;
75 if ((panel->g_task.config_asb_mask & (1<<TASK_ACTIVE)) == 0) {
76 panel->g_task.alpha[TASK_ACTIVE] = panel->g_task.alpha[TASK_NORMAL];
77 panel->g_task.saturation[TASK_ACTIVE] = panel->g_task.saturation[TASK_NORMAL];
78 panel->g_task.brightness[TASK_ACTIVE] = panel->g_task.brightness[TASK_NORMAL];
79 }
80 if ((panel->g_task.config_asb_mask & (1<<TASK_ICONIFIED)) == 0) {
81 panel->g_task.alpha[TASK_ICONIFIED] = panel->g_task.alpha[TASK_NORMAL];
82 panel->g_task.saturation[TASK_ICONIFIED] = panel->g_task.saturation[TASK_NORMAL];
83 panel->g_task.brightness[TASK_ICONIFIED] = panel->g_task.brightness[TASK_NORMAL];
84 }
85 if ((panel->g_task.config_asb_mask & (1<<TASK_URGENT)) == 0) {
86 panel->g_task.alpha[TASK_URGENT] = panel->g_task.alpha[TASK_ACTIVE];
87 panel->g_task.saturation[TASK_URGENT] = panel->g_task.saturation[TASK_ACTIVE];
88 panel->g_task.brightness[TASK_URGENT] = panel->g_task.brightness[TASK_ACTIVE];
89 }
90 if ((panel->g_task.config_font_mask & (1<<TASK_ACTIVE)) == 0) panel->g_task.font[TASK_ACTIVE] = panel->g_task.font[TASK_NORMAL];
91 if ((panel->g_task.config_font_mask & (1<<TASK_ICONIFIED)) == 0) panel->g_task.font[TASK_ICONIFIED] = panel->g_task.font[TASK_NORMAL];
92 if ((panel->g_task.config_font_mask & (1<<TASK_URGENT)) == 0) panel->g_task.font[TASK_URGENT] = panel->g_task.font[TASK_ACTIVE];
93 if ((panel->g_task.config_background_mask & (1<<TASK_ACTIVE)) == 0) panel->g_task.background[TASK_ACTIVE] = panel->g_task.background[TASK_NORMAL];
94 if ((panel->g_task.config_background_mask & (1<<TASK_ICONIFIED)) == 0) panel->g_task.background[TASK_ICONIFIED] = panel->g_task.background[TASK_NORMAL];
95 if ((panel->g_task.config_background_mask & (1<<TASK_URGENT)) == 0) panel->g_task.background[TASK_URGENT] = panel->g_task.background[TASK_ACTIVE];
96
97 if (panel_horizontal) {
98 panel->g_task.area.posy = panel->g_taskbar.area.posy + panel->g_taskbar.bg->border.width + panel->g_taskbar.area.paddingy;
99 panel->g_task.area.height = panel->area.height - (2 * panel->g_task.area.posy);
100 }
101 else {
102 panel->g_task.area.posx = panel->g_taskbar.area.posx + panel->g_taskbar.bg->border.width + panel->g_taskbar.area.paddingy;
103 panel->g_task.area.width = panel->area.width - (2 * panel->g_task.area.posx);
104 panel->g_task.area.height = panel->g_task.maximum_height;
105 }
106
107 int k;
108 for (k=0; k<TASK_STATE_COUNT; ++k) {
109 if (panel->g_task.background[k]->border.rounded > panel->g_task.area.height/2) {
110 printf("task%sbackground_id is too big. Please fix your tint2rc\n", k==0 ? "_" : k==1 ? "_active_" : k==2 ? "_iconified_" : "_urgent_");
111 g_array_append_val(backgrounds, *panel->g_task.background[k]);
112 panel->g_task.background[k] = &g_array_index(backgrounds, Background, backgrounds->len-1);
113 panel->g_task.background[k]->border.rounded = panel->g_task.area.height/2;
114 }
115 }
116
117 // compute vertical position : text and icon
118 int height_ink, height;
119 get_text_size(panel->g_task.font_desc, &height_ink, &height, panel->area.height, "TAjpg", 5);
120
121 if (!panel->g_task.maximum_width && panel_horizontal)
122 panel->g_task.maximum_width = server.monitor[panel->monitor].width;
123
124 panel->g_task.text_posx = panel->g_task.background[0]->border.width + panel->g_task.area.paddingxlr;
125 panel->g_task.text_posy = (panel->g_task.area.height - height) / 2.0;
126 if (panel->g_task.icon) {
127 panel->g_task.icon_size1 = panel->g_task.area.height - (2 * panel->g_task.area.paddingy);
128 panel->g_task.text_posx += panel->g_task.icon_size1;
129 panel->g_task.icon_posy = (panel->g_task.area.height - panel->g_task.icon_size1) / 2;
130 }
131 //printf("monitor %d, task_maximum_width %d\n", panel->monitor, panel->g_task.maximum_width);
132
133 Taskbar *tskbar;
134 panel->nb_desktop = server.nb_desktop;
135 panel->taskbar = calloc(panel->nb_desktop, sizeof(Taskbar));
136 for (j=0 ; j < panel->nb_desktop ; j++) {
137 tskbar = &panel->taskbar[j];
138 memcpy(&tskbar->area, &panel->g_taskbar, sizeof(Area));
139 tskbar->desktop = j;
140 if (j == server.desktop && panel->g_taskbar.use_active)
141 tskbar->area.bg = panel->g_taskbar.bg_active;
142
143 // add taskbar to the panel
144 panel->area.list = g_slist_append(panel->area.list, tskbar);
145 }
146 }
147 }
148
149 void taskbar_remove_task(gpointer key, gpointer value, gpointer user_data) {remove_task(task_get_task(*(Window*)key)); }
150 void cleanup_taskbar()
151 {
152 Panel *panel;
153 Taskbar *tskbar;
154 int i, j;
155
156 if (win_to_task_table) g_hash_table_foreach(win_to_task_table, taskbar_remove_task, 0);
157 for (i=0 ; i < nb_panel ; i++) {
158 panel = &panel1[i];
159 for (j=0 ; j < panel->nb_desktop ; j++) {
160 tskbar = &panel->taskbar[j];
161 free_area (&tskbar->area);
162 // remove taskbar from the panel
163 panel->area.list = g_slist_remove(panel->area.list, tskbar);
164 }
165 if (panel->taskbar) {
166 free(panel->taskbar);
167 panel->taskbar = 0;
168 }
169 }
170
171 if (win_to_task_table) {
172 g_hash_table_destroy(win_to_task_table);
173 win_to_task_table = 0;
174 }
175 }
176
177
178 Task *task_get_task (Window win)
179 {
180 GPtrArray* task_group = task_get_tasks(win);
181 if (task_group)
182 return g_ptr_array_index(task_group, 0);
183 else
184 return 0;
185 }
186
187
188 GPtrArray* task_get_tasks(Window win)
189 {
190 return g_hash_table_lookup(win_to_task_table, &win);
191 }
192
193
194 void task_refresh_tasklist ()
195 {
196 Window *win;
197 int num_results, i;
198
199 win = server_get_property (server.root_win, server.atom._NET_CLIENT_LIST, XA_WINDOW, &num_results);
200 if (!win) return;
201
202 // Remove any old and set active win
203 // remark from Andreas: This seems unneccessary...
204 // active_task();
205
206 GList* win_list = g_hash_table_get_keys(win_to_task_table);
207 GList* it;
208 for (it=win_list; it; it=it->next) {
209 for (i = 0; i < num_results; i++)
210 if (*((Window*)it->data) == win[i])
211 break;
212 if (i == num_results)
213 taskbar_remove_task(it->data, 0, 0);
214 }
215 g_list_free(win_list);
216
217 // Add any new
218 for (i = 0; i < num_results; i++)
219 if (!task_get_task (win[i]))
220 add_task (win[i]);
221
222 XFree (win);
223 }
224
225
226 void resize_taskbar(void *obj)
227 {
228 Taskbar *taskbar = (Taskbar*)obj;
229 Panel *panel = (Panel*)taskbar->area.panel;
230 Task *tsk;
231 GSList *l;
232 int task_count, border_width;
233
234 //printf("resize_taskbar : posx et width des taches\n");
235 taskbar->area.redraw = 1;
236
237 border_width = taskbar->area.bg->border.width;
238
239 if (panel_horizontal) {
240 int pixel_width, modulo_width=0;
241 int x, taskbar_width;
242
243 // new task width for 'desktop'
244 task_count = g_slist_length(taskbar->area.list);
245 if (!task_count) pixel_width = panel->g_task.maximum_width;
246 else {
247 taskbar_width = taskbar->area.width - (2 * border_width) - (2 * panel->g_taskbar.area.paddingxlr);
248 if (task_count>1) taskbar_width -= ((task_count-1) * panel->g_taskbar.area.paddingx);
249
250 pixel_width = taskbar_width / task_count;
251 if (pixel_width > panel->g_task.maximum_width)
252 pixel_width = panel->g_task.maximum_width;
253 else
254 modulo_width = taskbar_width % task_count;
255 }
256
257 taskbar->task_width = pixel_width;
258 taskbar->task_modulo = modulo_width;
259 taskbar->text_width = pixel_width - panel->g_task.text_posx - panel->g_task.area.bg->border.width - panel->g_task.area.paddingx;
260
261 // change pos_x and width for all tasks
262 x = taskbar->area.posx + border_width + taskbar->area.paddingxlr;
263 for (l = taskbar->area.list; l ; l = l->next) {
264 tsk = l->data;
265 if (!tsk->area.on_screen) continue;
266 tsk->area.posx = x;
267 if (tsk->area.width != pixel_width) set_task_redraw(tsk);
268 tsk->area.width = pixel_width;
269 if (modulo_width) {
270 tsk->area.width++;
271 modulo_width--;
272 }
273
274 x += tsk->area.width + panel->g_taskbar.area.paddingx;
275 }
276 }
277 else {
278 int pixel_height, modulo_height=0;
279 int y, taskbar_height;
280
281 // new task width for 'desktop'
282 task_count = g_slist_length(taskbar->area.list);
283 if (!task_count) pixel_height = panel->g_task.maximum_height;
284 else {
285 taskbar_height = taskbar->area.height - (2 * border_width) - (2 * panel->g_taskbar.area.paddingxlr);
286 if (task_count>1) taskbar_height -= ((task_count-1) * panel->g_taskbar.area.paddingx);
287
288 pixel_height = taskbar_height / task_count;
289 if (pixel_height > panel->g_task.maximum_height)
290 pixel_height = panel->g_task.maximum_height;
291 else
292 modulo_height = taskbar_height % task_count;
293 }
294
295 taskbar->task_width = pixel_height;
296 taskbar->task_modulo = modulo_height;
297 taskbar->text_width = taskbar->area.width - (2 * panel->g_taskbar.area.paddingy) - panel->g_task.text_posx - panel->g_task.area.bg->border.width - panel->g_task.area.paddingx;
298
299 // change pos_y and height for all tasks
300 y = taskbar->area.posy + border_width + taskbar->area.paddingxlr;
301 for (l = taskbar->area.list; l ; l = l->next) {
302 tsk = l->data;
303 if (!tsk->area.on_screen) continue;
304 tsk->area.posy = y;
305 if (tsk->area.height != pixel_height) set_task_redraw(tsk);
306 tsk->area.height = pixel_height;
307 if (modulo_height) {
308 tsk->area.height++;
309 modulo_height--;
310 }
311
312 y += tsk->area.height + panel->g_taskbar.area.paddingx;
313 }
314 }
315 }
This page took 0.0468499999999999 seconds and 5 git commands to generate.