src/taskbar/taskbar.c (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
/************************************************************************** * * Tint2 : taskbar * * Copyright (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr) from Omega distribution * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **************************************************************************/ #include <X11/Xlib.h> #include <X11/Xutil.h> #include <X11/Xatom.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <glib.h> #include <Imlib2.h> #include "task.h" #include "taskbar.h" #include "server.h" #include "window.h" #include "panel.h" #include "strnatcmp.h" /* win_to_task holds for every Window an array of tasks. Usually the array contains only one element. However for omnipresent windows (windows which are visible in every taskbar) the array contains to every Task* on each panel a pointer (i.e. GPtrArray.len == server.num_desktops) */ GHashTable *win_to_task; Task *active_task; Task *task_drag; gboolean taskbar_enabled; gboolean taskbar_distribute_size; gboolean hide_inactive_tasks; gboolean hide_task_diff_monitor; TaskbarSortMethod taskbar_sort_method; Alignment taskbar_alignment; guint win_hash(gconstpointer key) { return *((const Window *)key); } gboolean win_compare(gconstpointer a, gconstpointer b) { return (*((const Window *)a) == *((const Window *)b)); } void free_ptr_array(gpointer data) { g_ptr_array_free(data, 1); } void default_taskbar() { win_to_task = NULL; urgent_timeout = NULL; urgent_list = NULL; taskbar_enabled = 0; taskbar_distribute_size = 0; hide_inactive_tasks = 0; hide_task_diff_monitor = 0; taskbar_sort_method = TASKBAR_NOSORT; taskbar_alignment = ALIGN_LEFT; default_taskbarname(); } void cleanup_taskbar() { cleanup_taskbarname(); if (win_to_task) { while (g_hash_table_size(win_to_task)) { GHashTableIter iter; gpointer key, value; g_hash_table_iter_init(&iter, win_to_task); if (g_hash_table_iter_next(&iter, &key, &value)) { taskbar_remove_task(key, 0, 0); } } g_hash_table_destroy(win_to_task); win_to_task = NULL; } for (int i = 0; i < num_panels; i++) { Panel *panel = &panels[i]; for (int j = 0; j < panel->num_desktops; j++) { Taskbar *taskbar = &panel->taskbar[j]; for (int k = 0; k < TASKBAR_STATE_COUNT; ++k) { if (taskbar->state_pix[k]) XFreePixmap(server.dsp, taskbar->state_pix[k]); taskbar->state_pix[k] = 0; } free_area(&taskbar->area); // remove taskbar from the panel remove_area((Area *)taskbar); } if (panel->taskbar) { free(panel->taskbar); panel->taskbar = NULL; } } g_slist_free(urgent_list); urgent_list = NULL; stop_timeout(urgent_timeout); } void init_taskbar() { if (!win_to_task) win_to_task = g_hash_table_new_full(win_hash, win_compare, free, free_ptr_array); active_task = 0; task_drag = 0; } void init_taskbar_panel(void *p) { Panel *panel = (Panel *)p; if (!panel->g_taskbar.background[TASKBAR_NORMAL]) { panel->g_taskbar.background[TASKBAR_NORMAL] = &g_array_index(backgrounds, Background, 0); panel->g_taskbar.background[TASKBAR_ACTIVE] = &g_array_index(backgrounds, Background, 0); } if (!panel->g_taskbar.background_name[TASKBAR_NORMAL]) { panel->g_taskbar.background_name[TASKBAR_NORMAL] = &g_array_index(backgrounds, Background, 0); panel->g_taskbar.background_name[TASKBAR_ACTIVE] = &g_array_index(backgrounds, Background, 0); } if (!panel->g_task.font_desc) panel->g_task.font_desc = pango_font_description_from_string(DEFAULT_FONT); if (!panel->g_task.area.bg) panel->g_task.area.bg = &g_array_index(backgrounds, Background, 0); // taskbar name panel->g_taskbar.area_name.panel = panel; panel->g_taskbar.area_name.size_mode = LAYOUT_FIXED; panel->g_taskbar.area_name._resize = resize_taskbarname; panel->g_taskbar.area_name._draw_foreground = draw_taskbarname; panel->g_taskbar.area_name._on_change_layout = 0; panel->g_taskbar.area_name.resize_needed = 1; panel->g_taskbar.area_name.on_screen = TRUE; // taskbar panel->g_taskbar.area.parent = panel; panel->g_taskbar.area.panel = panel; panel->g_taskbar.area.size_mode = LAYOUT_DYNAMIC; panel->g_taskbar.area.alignment = taskbar_alignment; panel->g_taskbar.area._resize = resize_taskbar; panel->g_taskbar.area._draw_foreground = draw_taskbar; panel->g_taskbar.area._on_change_layout = on_change_taskbar; panel->g_taskbar.area.resize_needed = 1; panel->g_taskbar.area.on_screen = TRUE; if (panel_horizontal) { panel->g_taskbar.area.posy = panel->area.bg->border.width + panel->area.paddingy; panel->g_taskbar.area.height = panel->area.height - (2 * panel->g_taskbar.area.posy); panel->g_taskbar.area_name.posy = panel->g_taskbar.area.posy; panel->g_taskbar.area_name.height = panel->g_taskbar.area.height; } else { panel->g_taskbar.area.posx = panel->area.bg->border.width + panel->area.paddingy; panel->g_taskbar.area.width = panel->area.width - (2 * panel->g_taskbar.area.posx); panel->g_taskbar.area_name.posx = panel->g_taskbar.area.posx; panel->g_taskbar.area_name.width = panel->g_taskbar.area.width; } // task panel->g_task.area.panel = panel; panel->g_task.area.size_mode = LAYOUT_DYNAMIC; panel->g_task.area._draw_foreground = draw_task; panel->g_task.area._on_change_layout = on_change_task; panel->g_task.area.resize_needed = 1; panel->g_task.area.on_screen = TRUE; if ((panel->g_task.config_asb_mask & (1 << TASK_NORMAL)) == 0) { panel->g_task.alpha[TASK_NORMAL] = 100; panel->g_task.saturation[TASK_NORMAL] = 0; panel->g_task.brightness[TASK_NORMAL] = 0; } if ((panel->g_task.config_asb_mask & (1 << TASK_ACTIVE)) == 0) { panel->g_task.alpha[TASK_ACTIVE] = panel->g_task.alpha[TASK_NORMAL]; panel->g_task.saturation[TASK_ACTIVE] = panel->g_task.saturation[TASK_NORMAL]; panel->g_task.brightness[TASK_ACTIVE] = panel->g_task.brightness[TASK_NORMAL]; } if ((panel->g_task.config_asb_mask & (1 << TASK_ICONIFIED)) == 0) { panel->g_task.alpha[TASK_ICONIFIED] = panel->g_task.alpha[TASK_NORMAL]; panel->g_task.saturation[TASK_ICONIFIED] = panel->g_task.saturation[TASK_NORMAL]; panel->g_task.brightness[TASK_ICONIFIED] = panel->g_task.brightness[TASK_NORMAL]; } if ((panel->g_task.config_asb_mask & (1 << TASK_URGENT)) == 0) { panel->g_task.alpha[TASK_URGENT] = panel->g_task.alpha[TASK_ACTIVE]; panel->g_task.saturation[TASK_URGENT] = panel->g_task.saturation[TASK_ACTIVE]; panel->g_task.brightness[TASK_URGENT] = panel->g_task.brightness[TASK_ACTIVE]; } if ((panel->g_task.config_font_mask & (1 << TASK_NORMAL)) == 0) panel->g_task.font[TASK_NORMAL] = (Color){{0, 0, 0}, 0}; if ((panel->g_task.config_font_mask & (1 << TASK_ACTIVE)) == 0) panel->g_task.font[TASK_ACTIVE] = panel->g_task.font[TASK_NORMAL]; if ((panel->g_task.config_font_mask & (1 << TASK_ICONIFIED)) == 0) panel->g_task.font[TASK_ICONIFIED] = panel->g_task.font[TASK_NORMAL]; if ((panel->g_task.config_font_mask & (1 << TASK_URGENT)) == 0) panel->g_task.font[TASK_URGENT] = panel->g_task.font[TASK_ACTIVE]; if ((panel->g_task.config_background_mask & (1 << TASK_NORMAL)) == 0) panel->g_task.background[TASK_NORMAL] = &g_array_index(backgrounds, Background, 0); if ((panel->g_task.config_background_mask & (1 << TASK_ACTIVE)) == 0) panel->g_task.background[TASK_ACTIVE] = panel->g_task.background[TASK_NORMAL]; if ((panel->g_task.config_background_mask & (1 << TASK_ICONIFIED)) == 0) panel->g_task.background[TASK_ICONIFIED] = panel->g_task.background[TASK_NORMAL]; if ((panel->g_task.config_background_mask & (1 << TASK_URGENT)) == 0) panel->g_task.background[TASK_URGENT] = panel->g_task.background[TASK_ACTIVE]; if (panel_horizontal) { panel->g_task.area.posy = panel->g_taskbar.area.posy + panel->g_taskbar.background[TASKBAR_NORMAL]->border.width + panel->g_taskbar.area.paddingy; panel->g_task.area.height = panel->area.height - (2 * panel->g_task.area.posy); } else { panel->g_task.area.posx = panel->g_taskbar.area.posx + panel->g_taskbar.background[TASKBAR_NORMAL]->border.width + panel->g_taskbar.area.paddingy; panel->g_task.area.width = panel->area.width - (2 * panel->g_task.area.posx); panel->g_task.area.height = panel->g_task.maximum_height; } for (int j = 0; j < TASK_STATE_COUNT; ++j) { if (!panel->g_task.background[j]) panel->g_task.background[j] = &g_array_index(backgrounds, Background, 0); if (panel->g_task.background[j]->border.radius > panel->g_task.area.height / 2) { printf("task%sbackground_id has a too large rounded value. Please fix your tint2rc\n", j == 0 ? "_" : j == 1 ? "_active_" : j == 2 ? "_iconified_" : "_urgent_"); g_array_append_val(backgrounds, *panel->g_task.background[j]); panel->g_task.background[j] = &g_array_index(backgrounds, Background, backgrounds->len - 1); panel->g_task.background[j]->border.radius = panel->g_task.area.height / 2; } } // compute vertical position : text and icon int height_ink, height, width; get_text_size2(panel->g_task.font_desc, &height_ink, &height, &width, panel->area.height, panel->area.width, "TAjpg", 5, PANGO_WRAP_WORD_CHAR, PANGO_ELLIPSIZE_END); if (!panel->g_task.maximum_width && panel_horizontal) panel->g_task.maximum_width = server.monitor[panel->monitor].width; panel->g_task.text_posx = panel->g_task.background[0]->border.width + panel->g_task.area.paddingxlr; panel->g_task.text_height = panel->g_task.area.height - (2 * panel->g_task.area.paddingy); if (panel->g_task.icon) { panel->g_task.icon_size1 = panel->g_task.area.height - (2 * panel->g_task.area.paddingy); panel->g_task.text_posx += panel->g_task.icon_size1 + panel->g_task.area.paddingx; panel->g_task.icon_posy = (panel->g_task.area.height - panel->g_task.icon_size1) / 2; } // printf("monitor %d, task_maximum_width %d\n", panel->monitor, panel->g_task.maximum_width); Taskbar *taskbar; panel->num_desktops = server.num_desktops; panel->taskbar = calloc(server.num_desktops, sizeof(Taskbar)); for (int j = 0; j < panel->num_desktops; j++) { taskbar = &panel->taskbar[j]; memcpy(&taskbar->area, &panel->g_taskbar.area, sizeof(Area)); taskbar->desktop = j; if (j == server.desktop) taskbar->area.bg = panel->g_taskbar.background[TASKBAR_ACTIVE]; else taskbar->area.bg = panel->g_taskbar.background[TASKBAR_NORMAL]; } init_taskbarname_panel(panel); } void taskbar_remove_task(gpointer key, gpointer value, gpointer user_data) { remove_task(task_get_task(*(Window *)key)); } Task *task_get_task(Window win) { GPtrArray *task_group = task_get_tasks(win); if (task_group) return g_ptr_array_index(task_group, 0); return NULL; } GPtrArray *task_get_tasks(Window win) { if (win_to_task && taskbar_enabled) return g_hash_table_lookup(win_to_task, &win); return NULL; } void task_refresh_tasklist() { if (!taskbar_enabled) return; int num_results; Window *win = server_get_property(server.root_win, server.atom._NET_CLIENT_LIST, XA_WINDOW, &num_results); if (!win) return; GList *win_list = g_hash_table_get_keys(win_to_task); GList *it; for (it = win_list; it; it = it->next) { int i; for (i = 0; i < num_results; i++) if (*((Window *)it->data) == win[i]) break; if (i == num_results) taskbar_remove_task(it->data, 0, 0); } g_list_free(win_list); // Add any new for (int i = 0; i < num_results; i++) if (!task_get_task(win[i])) add_task(win[i]); XFree(win); } void draw_taskbar(void *obj, cairo_t *c) { Taskbar *taskbar = (Taskbar *)obj; int state = (taskbar->desktop == server.desktop) ? TASKBAR_ACTIVE : TASKBAR_NORMAL; taskbar->state_pix[state] = taskbar->area.pix; } gboolean resize_taskbar(void *obj) { Taskbar *taskbar = (Taskbar *)obj; Panel *panel = (Panel *)taskbar->area.panel; // printf("resize_taskbar %d %d\n", taskbar->area.posx, taskbar->area.posy); if (panel_horizontal) { relayout_with_constraint(&taskbar->area, panel->g_task.maximum_width); int text_width = panel->g_task.maximum_width; GList *l = taskbar->area.children; if (taskbarname_enabled) l = l->next; for (; l != NULL; l = l->next) { if (((Task *)l->data)->area.on_screen) { text_width = ((Task *)l->data)->area.width; break; } } taskbar->text_width = text_width - panel->g_task.text_posx - panel->g_task.area.bg->border.width - panel->g_task.area.paddingxlr; } else { relayout_with_constraint(&taskbar->area, panel->g_task.maximum_height); 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.paddingxlr; } return FALSE; } void on_change_taskbar(void *obj) { Taskbar *taskbar = (Taskbar *)obj; // reset Pixmap when position/size changed for (int k = 0; k < TASKBAR_STATE_COUNT; ++k) { if (taskbar->state_pix[k]) XFreePixmap(server.dsp, taskbar->state_pix[k]); taskbar->state_pix[k] = 0; } taskbar->area.pix = 0; taskbar->area.redraw_needed = TRUE; } void set_taskbar_state(Taskbar *taskbar, TaskbarState state) { taskbar->area.bg = panels[0].g_taskbar.background[state]; taskbar->area.pix = taskbar->state_pix[state]; if (taskbarname_enabled) { taskbar->bar_name.area.bg = panels[0].g_taskbar.background_name[state]; if (!panel_config.mouse_effects) { taskbar->bar_name.area.pix = taskbar->bar_name.state_pix[state]; } } if (taskbar_mode != MULTI_DESKTOP) { if (state == TASKBAR_NORMAL) taskbar->area.on_screen = FALSE; else taskbar->area.on_screen = TRUE; } if (taskbar->area.on_screen) { if (!taskbar->state_pix[state]) taskbar->area.redraw_needed = TRUE; if (taskbarname_enabled) { if (!panel_config.mouse_effects) { if (!taskbar->bar_name.state_pix[state]) taskbar->bar_name.area.redraw_needed = TRUE; } else { taskbar->bar_name.area.redraw_needed = TRUE; } } if (taskbar_mode == MULTI_DESKTOP && panels[0].g_taskbar.background[TASKBAR_NORMAL] != panels[0].g_taskbar.background[TASKBAR_ACTIVE]) { GList *l = taskbar->area.children; if (taskbarname_enabled) l = l->next; for (; l; l = l->next) set_task_redraw((Task *)l->data); } } panel_refresh = TRUE; } void visible_taskbar(void *p) { Panel *panel = (Panel *)p; Taskbar *taskbar; for (int j = 0; j < panel->num_desktops; j++) { taskbar = &panel->taskbar[j]; if (taskbar_mode != MULTI_DESKTOP && taskbar->desktop != server.desktop) { // SINGLE_DESKTOP and not current desktop taskbar->area.on_screen = FALSE; } else { taskbar->area.on_screen = TRUE; } } panel_refresh = TRUE; } #define NONTRIVIAL 2 gint compare_tasks_trivial(Task *a, Task *b, Taskbar *taskbar) { if (a == b) return 0; if (taskbarname_enabled) { if (a == taskbar->area.children->data) return -1; if (b == taskbar->area.children->data) return 1; } return NONTRIVIAL; } gint contained_within(Task *a, Task *b) { if ((a->win_x <= b->win_x) && (a->win_y <= b->win_y) && (a->win_x + a->win_w >= b->win_x + b->win_w) && (a->win_y + a->win_h >= b->win_y + b->win_h)) { return 1; } return 0; } gint compare_task_centers(Task *a, Task *b, Taskbar *taskbar) { int trivial = compare_tasks_trivial(a, b, taskbar); if (trivial != NONTRIVIAL) return trivial; // If a window has the same coordinates and size as the other, // they are considered to be equal in the comparison. if ((a->win_x == b->win_x) && (a->win_y == b->win_y) && (a->win_w == b->win_w) && (a->win_h == b->win_h)) { return 0; } // If a window is completely contained in another, // then it is considered to come after (to the right/bottom) of the other. if (contained_within(a, b)) return -1; if (contained_within(b, a)) return 1; // Compare centers int a_horiz_c = a->win_x + a->win_w / 2; int b_horiz_c = b->win_x + b->win_w / 2; int a_vert_c = a->win_y + a->win_h / 2; int b_vert_c = b->win_y + b->win_h / 2; if (panel_horizontal) { if (a_horiz_c != b_horiz_c) { return a_horiz_c - b_horiz_c; } return a_vert_c - b_vert_c; } else { if (a_vert_c != b_vert_c) { return a_vert_c - b_vert_c; } return a_horiz_c - b_horiz_c; } } gint compare_task_titles(Task *a, Task *b, Taskbar *taskbar) { int trivial = compare_tasks_trivial(a, b, taskbar); if (trivial != NONTRIVIAL) return trivial; return strnatcasecmp(a->title ? a->title : "", b->title ? b->title : ""); } gint compare_tasks(Task *a, Task *b, Taskbar *taskbar) { int trivial = compare_tasks_trivial(a, b, taskbar); if (trivial != NONTRIVIAL) return trivial; if (taskbar_sort_method == TASKBAR_NOSORT) { return 0; } else if (taskbar_sort_method == TASKBAR_SORT_CENTER) { return compare_task_centers(a, b, taskbar); } else if (taskbar_sort_method == TASKBAR_SORT_TITLE) { return compare_task_titles(a, b, taskbar); } return 0; } gboolean taskbar_needs_sort(Taskbar *taskbar) { if (taskbar_sort_method == TASKBAR_NOSORT) return FALSE; for (GList *i = taskbar->area.children, *j = i ? i->next : NULL; i && j; i = i->next, j = j->next) { if (compare_tasks(i->data, j->data, taskbar) > 0) { return TRUE; } } return FALSE; } void sort_tasks(Taskbar *taskbar) { if (!taskbar) return; if (!taskbar_needs_sort(taskbar)) return; taskbar->area.children = g_list_sort_with_data(taskbar->area.children, (GCompareDataFunc)compare_tasks, taskbar); taskbar->area.resize_needed = TRUE; panel_refresh = TRUE; ((Panel *)taskbar->area.panel)->area.resize_needed = TRUE; } void sort_taskbar_for_win(Window win) { if (taskbar_sort_method == TASKBAR_NOSORT) return; GPtrArray *task_group = task_get_tasks(win); if (task_group) { Task *task0 = g_ptr_array_index(task_group, 0); if (task0) { get_window_coordinates(win, &task0->win_x, &task0->win_y, &task0->win_w, &task0->win_h); } for (int i = 0; i < task_group->len; ++i) { Task *task = g_ptr_array_index(task_group, i); task->win_x = task0->win_x; task->win_y = task0->win_y; task->win_w = task0->win_w; task->win_h = task0->win_h; sort_tasks(task->area.parent); } } } |