Improve dockapp recognition
[wmaker-crm.git] / src / wmspec.c
bloba926d54b5276d84ac349fbc267cc3a81ba189a1b
1 /* wmspec.c-- support for the wm-spec Hints
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 * TODO
25 * ----
27 * This file needs to be checked for all calls to XGetWindowProperty() and
28 * proper checks need to be made on the returned values. Only checking for
29 * return to be Success is not enough. -Dan
32 #include "wconfig.h"
34 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xarch.h>
37 #include <string.h>
39 #include "WindowMaker.h"
40 #include "window.h"
41 #include "screen.h"
42 #include "workspace.h"
43 #include "framewin.h"
44 #include "actions.h"
45 #include "client.h"
46 #include "wmspec.h"
47 #include "icon.h"
48 #include "stacking.h"
49 #include "xinerama.h"
50 #include "properties.h"
52 #ifdef DEBUG_WMSPEC
53 #include <stdio.h>
54 #endif
56 /* Global variables */
57 extern Atom _XA_WM_DELETE_WINDOW;
58 extern Time LastTimestamp;
59 extern WPreferences wPreferences;
61 /* Root Window Properties */
62 static Atom net_supported;
63 static Atom net_client_list;
64 static Atom net_client_list_stacking;
65 static Atom net_number_of_desktops;
66 static Atom net_desktop_geometry;
67 static Atom net_desktop_viewport;
68 static Atom net_current_desktop;
69 static Atom net_desktop_names;
70 static Atom net_active_window;
71 static Atom net_workarea; /* XXX: not xinerama compatible */
72 static Atom net_supporting_wm_check;
73 static Atom net_virtual_roots; /* N/A */
74 static Atom net_desktop_layout; /* XXX */
75 static Atom net_showing_desktop;
77 /* Other Root Window Messages */
78 static Atom net_close_window;
79 static Atom net_moveresize_window; /* TODO */
80 static Atom net_wm_moveresize; /* TODO */
82 /* Application Window Properties */
83 static Atom net_wm_name;
84 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
85 static Atom net_wm_icon_name;
86 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
87 static Atom net_wm_desktop;
88 static Atom net_wm_window_type;
89 static Atom net_wm_window_type_desktop;
90 static Atom net_wm_window_type_dock;
91 static Atom net_wm_window_type_toolbar;
92 static Atom net_wm_window_type_menu;
93 static Atom net_wm_window_type_utility;
94 static Atom net_wm_window_type_splash;
95 static Atom net_wm_window_type_dialog;
96 static Atom net_wm_window_type_normal;
97 static Atom net_wm_state;
98 static Atom net_wm_state_modal; /* XXX: what is this?!? */
99 static Atom net_wm_state_sticky;
100 static Atom net_wm_state_maximized_vert;
101 static Atom net_wm_state_maximized_horz;
102 static Atom net_wm_state_shaded;
103 static Atom net_wm_state_skip_taskbar;
104 static Atom net_wm_state_skip_pager;
105 static Atom net_wm_state_hidden;
106 static Atom net_wm_state_fullscreen;
107 static Atom net_wm_state_above;
108 static Atom net_wm_state_below;
109 static Atom net_wm_allowed_actions;
110 static Atom net_wm_action_move;
111 static Atom net_wm_action_resize;
112 static Atom net_wm_action_minimize;
113 static Atom net_wm_action_shade;
114 static Atom net_wm_action_stick;
115 static Atom net_wm_action_maximize_horz;
116 static Atom net_wm_action_maximize_vert;
117 static Atom net_wm_action_fullscreen;
118 static Atom net_wm_action_change_desktop;
119 static Atom net_wm_action_close;
120 static Atom net_wm_strut; /* XXX: see net_workarea */
121 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
122 static Atom net_wm_icon_geometry; /* FIXME: should work together with net_wm_handled_icons, gnome-panel-2.2.0.1 doesn't use _NET_WM_HANDLED_ICONS, thus present situation. */
123 static Atom net_wm_icon;
124 static Atom net_wm_pid; /* TODO */
125 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
127 /* Window Manager Protocols */
128 static Atom net_wm_ping; /* TODO */
130 static Atom utf8_string;
132 typedef struct {
133 char *name;
134 Atom *atom;
135 } atomitem_t;
137 static atomitem_t atomNames[] = {
138 {"_NET_SUPPORTED", &net_supported},
139 {"_NET_CLIENT_LIST", &net_client_list},
140 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
141 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
142 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
143 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
144 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
145 {"_NET_DESKTOP_NAMES", &net_desktop_names},
146 {"_NET_ACTIVE_WINDOW", &net_active_window},
147 {"_NET_WORKAREA", &net_workarea},
148 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
149 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
150 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
151 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
153 {"_NET_CLOSE_WINDOW", &net_close_window},
154 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
155 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
157 {"_NET_WM_NAME", &net_wm_name},
158 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
159 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
160 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
161 {"_NET_WM_DESKTOP", &net_wm_desktop},
162 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
163 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
164 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
165 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
166 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
167 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
168 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
169 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
170 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
171 {"_NET_WM_STATE", &net_wm_state},
172 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
173 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
174 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
175 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
176 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
177 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
178 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
179 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
180 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
181 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
182 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
183 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
184 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
185 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
186 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
187 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
188 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
189 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
190 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
191 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
192 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
193 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
194 {"_NET_WM_STRUT", &net_wm_strut},
195 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
196 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
197 {"_NET_WM_ICON", &net_wm_icon},
198 {"_NET_WM_PID", &net_wm_pid},
199 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
201 {"_NET_WM_PING", &net_wm_ping},
203 {"UTF8_STRING", &utf8_string},
206 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
208 #define _NET_WM_STATE_REMOVE 0
209 #define _NET_WM_STATE_ADD 1
210 #define _NET_WM_STATE_TOGGLE 2
212 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
213 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
214 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
215 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
216 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
217 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
218 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
219 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
220 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
221 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
222 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
224 static void observer(void *self, WMNotification * notif);
225 static void wsobserver(void *self, WMNotification * notif);
227 static void updateClientList(WScreen * scr);
228 static void updateClientListStacking(WScreen * scr, WWindow *);
230 static void updateWorkspaceNames(WScreen * scr);
231 static void updateCurrentWorkspace(WScreen * scr);
232 static void updateWorkspaceCount(WScreen * scr);
234 typedef struct NetData {
235 WScreen *scr;
236 WReservedArea *strut;
237 WWindow **show_desktop;
238 } NetData;
240 static void setSupportedHints(WScreen * scr)
242 Atom atom[atomNr];
243 int i = 0;
245 /* set supported hints list */
246 /* XXX: extend this !!! */
248 atom[i++] = net_client_list;
249 atom[i++] = net_client_list_stacking;
250 atom[i++] = net_number_of_desktops;
251 atom[i++] = net_desktop_geometry;
252 atom[i++] = net_desktop_viewport;
253 atom[i++] = net_current_desktop;
254 atom[i++] = net_desktop_names;
255 atom[i++] = net_active_window;
256 atom[i++] = net_workarea;
257 atom[i++] = net_supporting_wm_check;
258 atom[i++] = net_showing_desktop;
259 #if 0
260 atom[i++] = net_wm_moveresize;
261 #endif
262 atom[i++] = net_wm_desktop;
263 atom[i++] = net_wm_window_type;
264 atom[i++] = net_wm_window_type_desktop;
265 atom[i++] = net_wm_window_type_dock;
266 atom[i++] = net_wm_window_type_toolbar;
267 atom[i++] = net_wm_window_type_menu;
268 atom[i++] = net_wm_window_type_utility;
269 atom[i++] = net_wm_window_type_splash;
270 atom[i++] = net_wm_window_type_dialog;
271 atom[i++] = net_wm_window_type_normal;
273 atom[i++] = net_wm_state;
274 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
275 atom[i++] = net_wm_state_sticky;
276 atom[i++] = net_wm_state_shaded;
277 atom[i++] = net_wm_state_maximized_horz;
278 atom[i++] = net_wm_state_maximized_vert;
279 atom[i++] = net_wm_state_skip_taskbar;
280 atom[i++] = net_wm_state_skip_pager;
281 atom[i++] = net_wm_state_hidden;
282 atom[i++] = net_wm_state_fullscreen;
283 atom[i++] = net_wm_state_above;
284 atom[i++] = net_wm_state_below;
286 atom[i++] = net_wm_allowed_actions;
287 atom[i++] = net_wm_action_move;
288 atom[i++] = net_wm_action_resize;
289 atom[i++] = net_wm_action_minimize;
290 atom[i++] = net_wm_action_shade;
291 atom[i++] = net_wm_action_stick;
292 atom[i++] = net_wm_action_maximize_horz;
293 atom[i++] = net_wm_action_maximize_vert;
294 atom[i++] = net_wm_action_fullscreen;
295 atom[i++] = net_wm_action_change_desktop;
296 atom[i++] = net_wm_action_close;
298 atom[i++] = net_wm_strut;
299 atom[i++] = net_wm_icon_geometry;
300 atom[i++] = net_wm_icon;
301 atom[i++] = net_wm_handled_icons;
303 atom[i++] = net_wm_name;
304 atom[i++] = net_wm_icon_name;
306 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
308 /* set supporting wm hint */
309 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
310 PropModeReplace, (unsigned char *)&scr->info_window, 1);
312 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
313 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
316 void wNETWMUpdateDesktop(WScreen * scr)
318 long *views, sizes[2];
319 int count, i;
321 if (scr->workspace_count == 0)
322 return;
324 count = scr->workspace_count * 2;
325 views = wmalloc(sizeof(long) * count);
326 /*memset(views, 0, sizeof(long) * count); */
327 sizes[0] = scr->scr_width;
328 sizes[1] = scr->scr_height;
330 for (i = 0; i < scr->workspace_count; i++) {
331 views[2 * i + 0] = 0;
332 views[2 * i + 1] = 0;
335 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
336 PropModeReplace, (unsigned char *)sizes, 2);
338 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
339 PropModeReplace, (unsigned char *)views, count);
341 wfree(views);
344 int wNETWMGetCurrentDesktopFromHint(WScreen * scr)
346 int count;
347 unsigned char *prop;
349 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
350 if (prop) {
351 int desktop = *(long *)prop;
352 XFree(prop);
353 return desktop;
355 return -1;
359 * Find the best icon to be used by Window Maker for appicon/miniwindows.
360 * Currently the algorithm is to take the image with the size closest
361 * to icon_size x icon_size, but never bigger than that.
363 * This algorithm is very poorly implemented and needs to be redone (it can
364 * easily select images with very large widths and very small heights over
365 * square images, if the area of the former is closer to the desired one).
367 * The logic can also be changed to accept bigger images and scale them down.
369 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
371 int size, wanted, d;
372 unsigned long i, distance;
373 unsigned long *icon;
375 /* better use only 75% of icon_size. For 64x64 this means 48x48
376 * This leaves room around the icon for the miniwindow title and
377 * results in better overall aesthetics -Dan */
378 wanted = wPreferences.icon_size * wPreferences.icon_size;
380 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
381 size = data[i] * data[i + 1];
382 if (size == 0)
383 break;
384 d = wanted - size;
385 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
386 distance = d;
387 icon = &data[i];
389 i += size + 2;
392 return icon;
395 static RImage *makeRImageFromARGBData(unsigned long *data)
397 int size, width, height, i;
398 RImage *image;
399 unsigned char *imgdata;
400 unsigned long pixel;
402 width = data[0];
403 height = data[1];
404 size = width * height;
406 if (size == 0)
407 return NULL;
409 image = RCreateImage(width, height, True);
411 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
412 pixel = data[i];
413 #if BYTE_ORDER == BIG_ENDIAN
414 imgdata[2] = (pixel >> 24) & 0xff; /* A */
415 imgdata[1] = (pixel >> 16) & 0xff; /* R */
416 imgdata[0] = (pixel >> 8) & 0xff; /* G */
417 imgdata[3] = (pixel >> 0) & 0xff; /* B */
418 #else /* Little endian */
419 imgdata[3] = (pixel >> 24) & 0xff; /* A */
420 imgdata[0] = (pixel >> 16) & 0xff; /* R */
421 imgdata[1] = (pixel >> 8) & 0xff; /* G */
422 imgdata[2] = (pixel >> 0) & 0xff; /* B */
423 #endif /* endianness */
427 return image;
430 static void updateIconImage(WScreen * scr, WWindow * wwin)
432 unsigned long *property, *data;
433 unsigned long items, rest;
434 Atom type;
435 int format;
436 RImage *image;
438 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
439 False, XA_CARDINAL, &type, &format, &items, &rest,
440 (unsigned char **)&property) != Success || !property) {
441 return;
444 if (type != XA_CARDINAL || format != 32 || items < 2) {
445 XFree(property);
446 return;
449 data = findBestIcon(property, items);
450 if (!data) {
451 XFree(property);
452 return;
455 image = makeRImageFromARGBData(data);
457 if (image) {
458 if (wwin->net_icon_image)
459 RReleaseImage(wwin->net_icon_image);
460 wwin->net_icon_image = image;
463 XFree(property);
466 static void updateShowDesktop(WScreen * scr, Bool show)
468 long foo;
470 foo = (show == True);
471 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
472 PropModeReplace, (unsigned char *)&foo, 1);
475 void wNETWMShowingDesktop(WScreen * scr, Bool show)
477 if (show && scr->netdata->show_desktop == NULL) {
478 WWindow *tmp, **wins;
479 int i = 0;
481 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
483 tmp = scr->focused_window;
484 while (tmp) {
485 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
487 wins[i++] = tmp;
488 tmp->flags.skip_next_animation = 1;
489 tmp->flags.net_show_desktop = 1;
490 wIconifyWindow(tmp);
493 tmp = tmp->prev;
495 wins[i++] = NULL;
497 scr->netdata->show_desktop = wins;
498 updateShowDesktop(scr, True);
499 } else if (scr->netdata->show_desktop != NULL) {
500 /* FIXME: get rid of workspace flashing ! */
501 int ws = scr->current_workspace;
502 WWindow **tmp;
503 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
504 wDeiconifyWindow(*tmp);
505 (*tmp)->flags.net_show_desktop = 0;
507 if (ws != scr->current_workspace)
508 wWorkspaceChange(scr, ws);
509 wfree(scr->netdata->show_desktop);
510 scr->netdata->show_desktop = NULL;
511 updateShowDesktop(scr, False);
515 void wNETWMInitStuff(WScreen * scr)
517 NetData *data;
518 int i;
520 #ifdef DEBUG_WMSPEC
521 printf("wNETWMInitStuff\n");
522 #endif
524 #ifdef HAVE_XINTERNATOMS
526 Atom atoms[atomNr];
527 char *names[atomNr];
529 for (i = 0; i < atomNr; ++i) {
530 names[i] = atomNames[i].name;
532 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
533 for (i = 0; i < atomNr; ++i) {
534 *atomNames[i].atom = atoms[i];
537 #else
538 for (i = 0; i < atomNr; i++) {
539 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
541 #endif
543 data = wmalloc(sizeof(NetData));
544 data->scr = scr;
545 data->strut = NULL;
546 data->show_desktop = NULL;
548 scr->netdata = data;
550 setSupportedHints(scr);
552 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
553 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
554 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
555 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
556 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
557 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
558 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
560 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
561 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
562 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
563 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
565 updateClientList(scr);
566 updateClientListStacking(scr, NULL);
567 updateWorkspaceCount(scr);
568 updateWorkspaceNames(scr);
569 updateShowDesktop(scr, False);
571 wScreenUpdateUsableArea(scr);
574 void wNETWMCleanup(WScreen * scr)
576 int i;
578 for (i = 0; i < atomNr; i++)
579 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
582 void wNETWMUpdateActions(WWindow * wwin, Bool del)
584 Atom action[10]; /* nr of actions atoms defined */
585 int i = 0;
587 if (del) {
588 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
589 return;
592 if (IS_MOVABLE(wwin))
593 action[i++] = net_wm_action_move;
595 if (IS_RESIZABLE(wwin))
596 action[i++] = net_wm_action_resize;
598 if (!WFLAGP(wwin, no_miniaturizable))
599 action[i++] = net_wm_action_minimize;
601 if (!WFLAGP(wwin, no_shadeable))
602 action[i++] = net_wm_action_shade;
604 /* if (!WFLAGP(wwin, no_stickable)) */
605 action[i++] = net_wm_action_stick;
607 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
608 if (IS_RESIZABLE(wwin))
609 action[i++] = net_wm_action_maximize_horz;
611 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
612 if (IS_RESIZABLE(wwin))
613 action[i++] = net_wm_action_maximize_vert;
615 /* if (!WFLAGP(wwin, no_fullscreen)) */
616 action[i++] = net_wm_action_fullscreen;
618 /* if (!WFLAGP(wwin, no_change_desktop)) */
619 action[i++] = net_wm_action_change_desktop;
621 if (!WFLAGP(wwin, no_closable))
622 action[i++] = net_wm_action_close;
624 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
625 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
628 void wNETWMUpdateWorkarea(WScreen * scr, WArea usableArea)
630 long *area;
631 int count, i;
633 /* XXX: not Xinerama compatible,
634 xinerama gets the largest available */
636 if (!scr->netdata || scr->workspace_count == 0)
637 return;
639 count = scr->workspace_count * 4;
640 area = wmalloc(sizeof(long) * count);
641 for (i = 0; i < scr->workspace_count; i++) {
642 area[4 * i + 0] = usableArea.x1;
643 area[4 * i + 1] = usableArea.y1;
644 area[4 * i + 2] = usableArea.x2 - usableArea.x1;
645 area[4 * i + 3] = usableArea.y2 - usableArea.y1;
648 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
649 PropModeReplace, (unsigned char *)area, count);
651 wfree(area);
654 Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
656 WReservedArea *cur;
657 WMRect rect;
659 if (!scr->netdata || !scr->netdata->strut)
660 return False;
662 area->x1 = area->y1 = area->x2 = area->y2 = 0;
664 for (cur = scr->netdata->strut; cur; cur = cur->next) {
665 WWindow *wwin = wWindowFor(cur->window);
666 if (wWindowTouchesHead(wwin, head)) {
667 if (cur->area.x1 > area->x1)
668 area->x1 = cur->area.x1;
669 if (cur->area.y1 > area->y1)
670 area->y1 = cur->area.y1;
671 if (cur->area.x2 > area->x2)
672 area->x2 = cur->area.x2;
673 if (cur->area.y2 > area->y2)
674 area->y2 = cur->area.y2;
678 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
679 return False;
681 rect = wGetRectForHead(scr, head);
683 area->x1 = rect.pos.x + area->x1;
684 area->x2 = rect.pos.x + rect.size.width - area->x2;
685 area->y1 = rect.pos.y + area->y1;
686 area->y2 = rect.pos.y + rect.size.height - area->y2;
688 return True;
691 static void updateClientList(WScreen * scr)
693 WWindow *wwin;
694 Window *windows;
695 int count;
697 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
699 count = 0;
700 wwin = scr->focused_window;
701 while (wwin) {
702 windows[count++] = wwin->client_win;
703 wwin = wwin->prev;
705 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
706 PropModeReplace, (unsigned char *)windows, count);
708 wfree(windows);
709 XFlush(dpy);
712 static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
714 WWindow *wwin;
715 Window *client_list;
716 Window *client_list_reverse;
717 int client_count;
718 WCoreWindow *tmp;
719 WMBagIterator iter;
720 int i;
722 /* update client list */
723 i = scr->window_count + 1;
724 client_list = (Window *) wmalloc(sizeof(Window) * i);
725 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
727 client_count = 0;
728 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
729 while (tmp) {
730 wwin = wWindowFor(tmp->window);
731 /* wwin_excl is a window to exclude from the list
732 (e.g. it's now unmanaged) */
733 if (wwin && (wwin != wwin_excl))
734 client_list[client_count++] = wwin->client_win;
735 tmp = tmp->stacking->under;
739 for (i = 0; i < client_count; i++) {
740 Window w = client_list[client_count - i - 1];
741 client_list_reverse[i] = w;
744 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
745 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
747 wfree(client_list);
748 wfree(client_list_reverse);
750 XFlush(dpy);
753 static void updateWorkspaceCount(WScreen * scr)
754 { /* changeable */
755 long count;
757 count = scr->workspace_count;
759 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
760 32, PropModeReplace, (unsigned char *)&count, 1);
763 static void updateCurrentWorkspace(WScreen * scr)
764 { /* changeable */
765 long count;
767 count = scr->current_workspace;
769 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
770 PropModeReplace, (unsigned char *)&count, 1);
773 static void updateWorkspaceNames(WScreen * scr)
775 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
776 unsigned int i, len, curr_size;
778 pos = buf;
779 len = 0;
780 for (i = 0; i < scr->workspace_count; i++) {
781 curr_size = strlen(scr->workspaces[i]->name);
782 strcpy(pos, scr->workspaces[i]->name);
783 pos += (curr_size + 1);
784 len += (curr_size + 1);
787 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
788 PropModeReplace, (unsigned char *)buf, len);
791 static void updateFocusHint(WScreen * scr, WWindow * wwin)
792 { /* changeable */
793 Window window;
795 if (!scr->focused_window || !scr->focused_window->flags.focused)
796 window = None;
797 else
798 window = scr->focused_window->client_win;
800 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
801 PropModeReplace, (unsigned char *)&window, 1);
804 static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
806 long l;
808 if (del) {
809 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
810 } else {
811 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
812 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
813 32, PropModeReplace, (unsigned char *)&l, 1);
817 static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
818 { /* changeable */
819 if (del) {
820 if (!wwin->flags.net_state_from_client) {
821 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
823 } else {
824 Atom state[15]; /* nr of defined state atoms */
825 int i = 0;
827 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
828 updateWorkspaceHint(wwin, False, False);
830 if (IS_OMNIPRESENT(wwin))
831 state[i++] = net_wm_state_sticky;
832 if (wwin->flags.shaded)
833 state[i++] = net_wm_state_shaded;
834 if (wwin->flags.maximized & MAX_HORIZONTAL)
835 state[i++] = net_wm_state_maximized_horz;
836 if (wwin->flags.maximized & MAX_VERTICAL)
837 state[i++] = net_wm_state_maximized_vert;
838 if (WFLAGP(wwin, skip_window_list))
839 state[i++] = net_wm_state_skip_taskbar;
840 if (wwin->flags.net_skip_pager)
841 state[i++] = net_wm_state_skip_pager;
842 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
843 state[i++] = net_wm_state_hidden;
844 state[i++] = net_wm_state_skip_pager;
846 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
847 if (!IS_OMNIPRESENT(wwin))
848 updateWorkspaceHint(wwin, True, False);
849 state[i++] = net_wm_state_sticky;
852 if (WFLAGP(wwin, sunken))
853 state[i++] = net_wm_state_below;
854 if (WFLAGP(wwin, floating))
855 state[i++] = net_wm_state_above;
856 if (wwin->flags.fullscreen)
857 state[i++] = net_wm_state_fullscreen;
859 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
860 PropModeReplace, (unsigned char *)state, i);
864 static Bool updateStrut(WWindow * wwin, Bool adding)
866 WScreen *scr;
867 WReservedArea *area;
868 Bool hasState = False;
870 scr = wwin->screen_ptr;
872 if (adding) {
873 Atom type_ret;
874 int fmt_ret;
875 unsigned long nitems_ret;
876 unsigned long bytes_after_ret;
877 long *data = NULL;
879 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
880 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
881 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
883 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
884 area->area.x1 = data[0];
885 area->area.x2 = data[1];
886 area->area.y1 = data[2];
887 area->area.y2 = data[3];
889 area->window = wwin->client_win;
891 area->next = scr->netdata->strut;
892 scr->netdata->strut = area;
894 XFree(data);
895 hasState = True;
898 } else {
899 /* deleting */
900 area = scr->netdata->strut;
902 if (area) {
903 if (area->window == wwin->client_win) {
904 scr->netdata->strut = area->next;
905 wfree(area);
906 hasState = True;
907 } else {
908 while (area->next && area->next->window != wwin->client_win)
909 area = area->next;
911 if (area->next) {
912 WReservedArea *next;
914 next = area->next->next;
915 wfree(area->next);
916 area->next = next;
918 hasState = True;
924 return hasState;
927 static int getWindowLayer(WWindow * wwin)
929 int layer = WMNormalLevel;
931 if (wwin->type == net_wm_window_type_desktop) {
932 layer = WMDesktopLevel;
933 } else if (wwin->type == net_wm_window_type_dock) {
934 layer = WMDockLevel;
935 } else if (wwin->type == net_wm_window_type_toolbar) {
936 layer = WMMainMenuLevel;
937 } else if (wwin->type == net_wm_window_type_menu) {
938 layer = WMSubmenuLevel;
939 } else if (wwin->type == net_wm_window_type_utility) {
940 } else if (wwin->type == net_wm_window_type_splash) {
941 } else if (wwin->type == net_wm_window_type_dialog) {
942 if (wwin->transient_for) {
943 WWindow *parent = wWindowFor(wwin->transient_for);
944 if (parent && parent->flags.fullscreen) {
945 layer = WMFullscreenLevel;
948 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
949 } else if (wwin->type == net_wm_window_type_normal) {
952 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
953 layer = WMSunkenLevel;
954 if (wwin->client_flags.floating && WMFloatingLevel > layer)
955 layer = WMFloatingLevel;
957 return layer;
960 static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
963 if (state == net_wm_state_sticky) {
964 if (set == _NET_WM_STATE_TOGGLE) {
965 set = !IS_OMNIPRESENT(wwin);
967 if (set != wwin->flags.omnipresent) {
968 wWindowSetOmnipresent(wwin, set);
970 } else if (state == net_wm_state_shaded) {
971 if (set == _NET_WM_STATE_TOGGLE) {
972 set = !wwin->flags.shaded;
974 if (init) {
975 wwin->flags.shaded = set;
976 } else {
977 if (set) {
978 wShadeWindow(wwin);
979 } else {
980 wUnshadeWindow(wwin);
983 } else if (state == net_wm_state_skip_taskbar) {
984 if (set == _NET_WM_STATE_TOGGLE) {
985 set = !wwin->client_flags.skip_window_list;
987 wwin->client_flags.skip_window_list = set;
988 } else if (state == net_wm_state_skip_pager) {
989 if (set == _NET_WM_STATE_TOGGLE) {
990 set = !wwin->flags.net_skip_pager;
992 wwin->flags.net_skip_pager = set;
993 } else if (state == net_wm_state_maximized_vert) {
994 if (set == _NET_WM_STATE_TOGGLE) {
995 set = !(wwin->flags.maximized & MAX_VERTICAL);
997 if (init) {
998 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
999 } else {
1000 if (set) {
1001 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1002 } else {
1003 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1006 } else if (state == net_wm_state_maximized_horz) {
1007 if (set == _NET_WM_STATE_TOGGLE) {
1008 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1010 if (init) {
1011 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1012 } else {
1013 if (set) {
1014 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1015 } else {
1016 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1019 } else if (state == net_wm_state_hidden) {
1020 if (set == _NET_WM_STATE_TOGGLE) {
1021 set = !(wwin->flags.miniaturized);
1023 if (init) {
1024 wwin->flags.miniaturized = set;
1025 } else {
1026 if (set) {
1027 wIconifyWindow(wwin);
1028 } else {
1029 wDeiconifyWindow(wwin);
1032 } else if (state == net_wm_state_fullscreen) {
1033 if (set == _NET_WM_STATE_TOGGLE) {
1034 set = !(wwin->flags.fullscreen);
1036 if (init) {
1037 wwin->flags.fullscreen = set;
1038 } else {
1039 if (set) {
1040 wFullscreenWindow(wwin);
1041 } else {
1042 wUnfullscreenWindow(wwin);
1045 } else if (state == net_wm_state_above) {
1046 if (set == _NET_WM_STATE_TOGGLE) {
1047 set = !(wwin->client_flags.floating);
1049 if (init) {
1050 wwin->client_flags.floating = set;
1051 } else {
1052 wwin->client_flags.floating = set;
1053 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1056 } else if (state == net_wm_state_below) {
1057 if (set == _NET_WM_STATE_TOGGLE) {
1058 set = !(wwin->client_flags.sunken);
1060 if (init) {
1061 wwin->client_flags.sunken = set;
1062 } else {
1063 wwin->client_flags.sunken = set;
1064 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1067 } else {
1068 #ifdef DEBUG_WMSPEC
1069 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1070 #endif
1074 static void removeIcon(WWindow * wwin)
1076 if (wwin->icon == NULL)
1077 return;
1078 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1079 XUnmapWindow(dpy, wwin->icon->core->window);
1080 RemoveFromStackList(wwin->icon->core);
1081 wIconDestroy(wwin->icon);
1082 wwin->icon = NULL;
1086 static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
1088 Bool ret = True;
1090 if (type == net_wm_window_type_desktop) {
1091 wwin->client_flags.no_titlebar = 1;
1092 wwin->client_flags.no_resizable = 1;
1093 wwin->client_flags.no_miniaturizable = 1;
1094 wwin->client_flags.no_border = 1;
1095 wwin->client_flags.no_resizebar = 1;
1096 wwin->client_flags.no_shadeable = 1;
1097 wwin->client_flags.no_movable = 1;
1098 wwin->client_flags.omnipresent = 1;
1099 wwin->client_flags.skip_window_list = 1;
1100 wwin->client_flags.dont_move_off = 1;
1101 wwin->client_flags.no_appicon = 1;
1102 wwin->flags.net_skip_pager = 1;
1103 wwin->frame_x = 0;
1104 wwin->frame_y = 0;
1105 } else if (type == net_wm_window_type_dock) {
1106 wwin->client_flags.no_titlebar = 1;
1107 wwin->client_flags.no_resizable = 1;
1108 wwin->client_flags.no_miniaturizable = 1;
1109 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1110 wwin->client_flags.no_resizebar = 1;
1111 wwin->client_flags.no_shadeable = 1;
1112 wwin->client_flags.no_movable = 1;
1113 wwin->client_flags.omnipresent = 1;
1114 wwin->client_flags.skip_window_list = 1;
1115 wwin->client_flags.dont_move_off = 1;
1116 wwin->flags.net_skip_pager = 1;
1117 } else if (type == net_wm_window_type_toolbar) {
1118 wwin->client_flags.no_titlebar = 1;
1119 wwin->client_flags.no_resizable = 1;
1120 wwin->client_flags.no_miniaturizable = 1;
1121 wwin->client_flags.no_resizebar = 1;
1122 wwin->client_flags.no_shadeable = 1;
1123 wwin->client_flags.skip_window_list = 1;
1124 wwin->client_flags.dont_move_off = 1;
1125 wwin->client_flags.no_appicon = 1;
1126 } else if (type == net_wm_window_type_menu) {
1127 wwin->client_flags.no_titlebar = 1;
1128 wwin->client_flags.no_resizable = 1;
1129 wwin->client_flags.no_miniaturizable = 1;
1130 wwin->client_flags.no_resizebar = 1;
1131 wwin->client_flags.no_shadeable = 1;
1132 wwin->client_flags.skip_window_list = 1;
1133 wwin->client_flags.dont_move_off = 1;
1134 wwin->client_flags.no_appicon = 1;
1135 } else if (type == net_wm_window_type_utility) {
1136 wwin->client_flags.no_appicon = 1;
1137 } else if (type == net_wm_window_type_splash) {
1138 wwin->client_flags.no_titlebar = 1;
1139 wwin->client_flags.no_resizable = 1;
1140 wwin->client_flags.no_miniaturizable = 1;
1141 wwin->client_flags.no_resizebar = 1;
1142 wwin->client_flags.no_shadeable = 1;
1143 wwin->client_flags.no_movable = 1;
1144 wwin->client_flags.skip_window_list = 1;
1145 wwin->client_flags.dont_move_off = 1;
1146 wwin->client_flags.no_appicon = 1;
1147 wwin->flags.net_skip_pager = 1;
1148 } else if (type == net_wm_window_type_dialog) {
1149 /* These also seem a bad idea in our context -Dan
1150 // wwin->client_flags.skip_window_list = 1;
1151 // wwin->client_flags.no_appicon = 1;
1153 } else if (type == net_wm_window_type_normal) {
1154 } else {
1155 ret = False;
1158 wwin->type = type;
1159 *layer = getWindowLayer(wwin);
1161 return ret;
1164 void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
1166 if (wwin->type == net_wm_window_type_splash) {
1167 WScreen *scr = wwin->screen_ptr;
1168 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1169 *x = rect.pos.x + (rect.size.width - width) / 2;
1170 *y = rect.pos.y + (rect.size.height - height) / 2;
1174 static void updateWindowType(WWindow * wwin)
1176 Atom type_ret;
1177 int fmt_ret;
1178 unsigned long nitems_ret;
1179 unsigned long bytes_after_ret;
1180 long *data = NULL;
1181 int layer;
1182 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1183 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1184 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1186 int i;
1187 Atom *type = (Atom *) data;
1188 for (i = 0; i < nitems_ret; ++i) {
1189 if (handleWindowType(wwin, type[i], &layer))
1190 break;
1192 XFree(data);
1195 if (wwin->frame != NULL) {
1196 ChangeStackingLevel(wwin->frame->core, layer);
1197 wwin->frame->flags.need_texture_change = 1;
1198 wWindowConfigureBorders(wwin);
1199 wFrameWindowPaint(wwin->frame);
1200 wNETWMUpdateActions(wwin, False);
1204 Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
1206 Atom type_ret;
1207 int fmt_ret;
1208 unsigned long nitems_ret;
1209 unsigned long bytes_after_ret;
1210 long *data = NULL;
1211 Bool hasState = False;
1212 int i;
1214 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1215 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1216 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1218 long desktop = *data;
1219 XFree(data);
1221 if (desktop == -1)
1222 wwin->client_flags.omnipresent = 1;
1223 else
1224 *workspace = desktop;
1226 hasState = True;
1229 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1230 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1231 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1233 Atom *state = (Atom *) data;
1234 for (i = 0; i < nitems_ret; ++i) {
1235 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1237 XFree(data);
1238 hasState = True;
1241 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1242 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1243 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1245 Atom *type = (Atom *) data;
1246 for (i = 0; i < nitems_ret; ++i) {
1247 if (handleWindowType(wwin, type[i], layer))
1248 break;
1250 XFree(data);
1251 hasState = True;
1254 wNETWMUpdateActions(wwin, False);
1255 updateStrut(wwin, False);
1256 if (updateStrut(wwin, True)) {
1257 hasState = True;
1259 wScreenUpdateUsableArea(wwin->screen_ptr);
1261 return hasState;
1264 static Bool updateNetIconInfo(WWindow * wwin)
1267 Atom type_ret;
1268 int fmt_ret;
1269 unsigned long nitems_ret;
1270 unsigned long bytes_after_ret;
1271 long *data = NULL;
1272 Bool hasState = False;
1273 Bool old_state = wwin->flags.net_handle_icon;
1275 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1276 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1277 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1278 long handled = *data;
1279 wwin->flags.net_handle_icon = (handled != 0);
1280 XFree(data);
1281 hasState = True;
1283 } else
1284 wwin->flags.net_handle_icon = False;
1286 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1287 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1288 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1290 #ifdef NETWM_PROPER
1291 if (wwin->flags.net_handle_icon)
1292 #else
1293 wwin->flags.net_handle_icon = True;
1294 #endif
1296 wwin->icon_x = data[0];
1297 wwin->icon_y = data[1];
1298 wwin->icon_w = data[2];
1299 wwin->icon_h = data[3];
1302 XFree(data);
1303 hasState = True;
1305 } else
1306 wwin->flags.net_handle_icon = False;
1308 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1309 if (wwin->flags.net_handle_icon) {
1310 removeIcon(wwin);
1311 } else {
1312 wwin->flags.miniaturized = False;
1313 wwin->flags.skip_next_animation = True;
1314 wIconifyWindow(wwin);
1318 return hasState;
1321 Bool wNETWMCheckInitialClientState(WWindow * wwin)
1323 Bool hasState = False;
1325 #ifdef DEBUG_WMSPEC
1326 printf("CheckInitialClientState\n");
1327 #endif
1329 wNETWMShowingDesktop(wwin->screen_ptr, False);
1331 hasState |= updateNetIconInfo(wwin);
1333 updateIconImage(wwin->screen_ptr, wwin);
1335 return hasState;
1338 static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
1340 unsigned long nitems_ret, bytes_after_ret;
1341 char *data, *names[32];
1342 int fmt_ret, i, n;
1343 Atom type_ret;
1345 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1346 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1347 &bytes_after_ret, (unsigned char **)&data) != Success) {
1348 return;
1351 if (data == NULL)
1352 return;
1354 if (type_ret != utf8_string || fmt_ret != 8)
1355 return;
1357 n = 0;
1358 names[n] = data;
1359 for (i = 0; i < nitems_ret; i++) {
1360 if (data[i] == 0) {
1361 n++;
1362 names[n] = &data[i];
1363 } else if (*names[n] == 0) {
1364 names[n] = &data[i];
1365 wWorkspaceRename(scr, n, names[n]);
1370 Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
1372 WScreen *scr;
1373 WWindow *wwin;
1374 Bool done = True;
1376 #ifdef DEBUG_WMSPEC
1377 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1378 #endif
1380 scr = wScreenForWindow(event->window);
1381 if (scr) {
1382 /* generic client messages */
1383 if (event->message_type == net_current_desktop) {
1384 wWorkspaceChange(scr, event->data.l[0]);
1385 } else if (event->message_type == net_number_of_desktops) {
1386 long value;
1388 value = event->data.l[0];
1389 if (value > scr->workspace_count) {
1390 wWorkspaceMake(scr, value - scr->workspace_count);
1391 } else if (value < scr->workspace_count) {
1392 int i;
1393 Bool rebuild = False;
1395 for (i = scr->workspace_count - 1; i >= value; i--) {
1396 if (!wWorkspaceDelete(scr, i)) {
1397 rebuild = True;
1398 break;
1402 if (rebuild) {
1403 updateWorkspaceCount(scr);
1406 } else if (event->message_type == net_showing_desktop) {
1407 wNETWMShowingDesktop(scr, event->data.l[0]);
1408 } else if (event->message_type == net_desktop_names) {
1409 handleDesktopNames(event, scr);
1410 } else {
1411 done = False;
1414 if (done)
1415 return True;
1418 /* window specific client messages */
1420 wwin = wWindowFor(event->window);
1421 if (!wwin)
1422 return False;
1424 if (event->message_type == net_active_window) {
1426 * Firefox sends aditional 'net_active_window' signals on startup if
1427 * multiple tabs are open. That causes unnecessary workspace switching if
1428 * those signals come from other workspaces. Therefore we also check if
1429 * we should ignore these spurious focus across workspaces requests (but
1430 * allow the switching if it comes from a pager).
1432 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace
1433 || !WFLAGP(wwin, dont_focus_across_wksp) || event->data.l[0] == 2) {
1434 wNETWMShowingDesktop(scr, False);
1435 wMakeWindowVisible(wwin);
1437 } else if (event->message_type == net_close_window) {
1438 if (!WFLAGP(wwin, no_closable)) {
1439 if (wwin->protocols.DELETE_WINDOW)
1440 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1442 } else if (event->message_type == net_wm_state) {
1443 int maximized = wwin->flags.maximized;
1444 long set = event->data.l[0];
1446 #ifdef DEBUG_WMSPEC
1447 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1448 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1449 #endif
1451 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1452 if (event->data.l[2])
1453 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1455 if (wwin->flags.maximized != maximized) {
1456 if (!wwin->flags.maximized) {
1457 wwin->flags.maximized = maximized;
1458 wUnmaximizeWindow(wwin);
1459 } else {
1460 wMaximizeWindow(wwin, wwin->flags.maximized);
1463 updateStateHint(wwin, False, False);
1464 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1465 updateNetIconInfo(wwin);
1466 } else if (event->message_type == net_wm_desktop) {
1467 long desktop = event->data.l[0];
1468 if (desktop == -1) {
1469 wWindowSetOmnipresent(wwin, True);
1470 } else {
1471 if (IS_OMNIPRESENT(wwin))
1472 wWindowSetOmnipresent(wwin, False);
1473 wWindowChangeWorkspace(wwin, desktop);
1475 } else if (event->message_type == net_wm_icon) {
1476 updateIconImage(scr, wwin);
1477 } else {
1478 done = False;
1481 return done;
1484 Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
1486 Bool ret = True;
1488 if (event->atom == net_wm_strut) {
1489 updateStrut(wwin, False);
1490 updateStrut(wwin, True);
1491 wScreenUpdateUsableArea(wwin->screen_ptr);
1492 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1493 updateNetIconInfo(wwin);
1494 } else if (event->atom == net_wm_window_type) {
1495 updateWindowType(wwin);
1496 } else if (event->atom == net_wm_name) {
1497 char *name = wNETWMGetWindowName(wwin->client_win);
1498 wWindowUpdateName(wwin, name);
1499 if (name)
1500 wfree(name);
1501 } else if (event->atom == net_wm_icon_name) {
1502 if (wwin->icon) {
1503 char *name = wNETWMGetIconName(wwin->client_win);
1504 wIconChangeTitle(wwin->icon, name);
1506 } else {
1507 ret = False;
1510 return ret;
1513 int wNETWMGetPidForWindow(Window window)
1515 Atom type_ret;
1516 int fmt_ret;
1517 unsigned long nitems_ret;
1518 unsigned long bytes_after_ret;
1519 long *data = NULL;
1520 int pid;
1522 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1523 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1524 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1526 pid = *data;
1527 XFree(data);
1528 } else {
1529 pid = 0;
1532 return pid;
1535 char *wNETWMGetWindowName(Window window)
1537 char *name;
1538 char *ret;
1539 int size;
1541 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1542 if (name) {
1543 ret = wstrndup(name, size);
1544 XFree(name);
1545 } else
1546 ret = NULL;
1547 return ret;
1550 char *wNETWMGetIconName(Window window)
1552 char *name;
1553 char *ret;
1554 int size;
1556 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1557 if (name) {
1558 ret = wstrndup(name, size);
1559 XFree(name);
1560 } else
1561 ret = NULL;
1562 return ret;
1565 static void observer(void *self, WMNotification * notif)
1567 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1568 const char *name = WMGetNotificationName(notif);
1569 void *data = WMGetNotificationClientData(notif);
1570 NetData *ndata = (NetData *) self;
1572 if (strcmp(name, WMNManaged) == 0 && wwin) {
1573 updateClientList(wwin->screen_ptr);
1574 updateClientListStacking(wwin->screen_ptr, NULL);
1575 updateStateHint(wwin, True, False);
1577 updateStrut(wwin, False);
1578 updateStrut(wwin, True);
1579 wScreenUpdateUsableArea(wwin->screen_ptr);
1580 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1581 updateClientList(wwin->screen_ptr);
1582 updateClientListStacking(wwin->screen_ptr, wwin);
1583 updateWorkspaceHint(wwin, False, True);
1584 updateStateHint(wwin, False, True);
1585 wNETWMUpdateActions(wwin, True);
1587 updateStrut(wwin, False);
1588 wScreenUpdateUsableArea(wwin->screen_ptr);
1589 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1590 updateClientListStacking(wwin->screen_ptr, NULL);
1591 updateStateHint(wwin, False, False);
1592 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1593 updateClientListStacking(wwin->screen_ptr, NULL);
1594 updateStateHint(wwin, False, False);
1595 } else if (strcmp(name, WMNChangedFocus) == 0) {
1596 updateFocusHint(ndata->scr, wwin);
1597 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1598 updateWorkspaceHint(wwin, False, False);
1599 updateStateHint(wwin, True, False);
1600 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1601 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1605 static void wsobserver(void *self, WMNotification * notif)
1607 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1608 const char *name = WMGetNotificationName(notif);
1610 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1611 updateWorkspaceCount(scr);
1612 updateWorkspaceNames(scr);
1613 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1614 updateWorkspaceCount(scr);
1615 updateWorkspaceNames(scr);
1616 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1617 updateCurrentWorkspace(scr);
1618 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1619 updateWorkspaceNames(scr);