Don't save app settings proplist when nothing changed
[wmaker-crm.git] / src / wmspec.c
blob1d50246148fc610813cbd1c5135e57578001683f
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * TODO
24 * ----
26 * This file needs to be checked for all calls to XGetWindowProperty() and
27 * proper checks need to be made on the returned values. Only checking for
28 * return to be Success is not enough. -Dan
31 #include "wconfig.h"
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xarch.h>
36 #include <string.h>
38 #include "WindowMaker.h"
39 #include "window.h"
40 #include "screen.h"
41 #include "workspace.h"
42 #include "framewin.h"
43 #include "actions.h"
44 #include "client.h"
45 #include "appicon.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(WWindow * wwin)
432 unsigned long *property, *data;
433 unsigned long items, rest;
434 Atom type;
435 int format;
437 if (wwin->net_icon_image)
438 RReleaseImage(wwin->net_icon_image);
439 wwin->net_icon_image = NULL;
441 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
442 False, XA_CARDINAL, &type, &format, &items, &rest,
443 (unsigned char **)&property) != Success || !property) {
444 return;
447 if (type != XA_CARDINAL || format != 32 || items < 2) {
448 XFree(property);
449 return;
452 data = findBestIcon(property, items);
453 if (!data) {
454 XFree(property);
455 return;
458 wwin->net_icon_image = makeRImageFromARGBData(data);
460 XFree(property);
462 if (wwin->icon) wIconUpdate(wwin->icon);
463 WApplication *app = wApplicationOf(wwin->main_window);
464 if (app && app->app_icon){
465 wIconUpdate(app->app_icon->icon);
466 wAppIconPaint(app->app_icon);
470 static void updateShowDesktop(WScreen * scr, Bool show)
472 long foo;
474 foo = (show == True);
475 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
476 PropModeReplace, (unsigned char *)&foo, 1);
479 void wNETWMShowingDesktop(WScreen * scr, Bool show)
481 if (show && scr->netdata->show_desktop == NULL) {
482 WWindow *tmp, **wins;
483 int i = 0;
485 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
487 tmp = scr->focused_window;
488 while (tmp) {
489 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
491 wins[i++] = tmp;
492 tmp->flags.skip_next_animation = 1;
493 tmp->flags.net_show_desktop = 1;
494 wIconifyWindow(tmp);
497 tmp = tmp->prev;
499 wins[i++] = NULL;
501 scr->netdata->show_desktop = wins;
502 updateShowDesktop(scr, True);
503 } else if (scr->netdata->show_desktop != NULL) {
504 /* FIXME: get rid of workspace flashing ! */
505 int ws = scr->current_workspace;
506 WWindow **tmp;
507 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
508 wDeiconifyWindow(*tmp);
509 (*tmp)->flags.net_show_desktop = 0;
511 if (ws != scr->current_workspace)
512 wWorkspaceChange(scr, ws);
513 wfree(scr->netdata->show_desktop);
514 scr->netdata->show_desktop = NULL;
515 updateShowDesktop(scr, False);
519 void wNETWMInitStuff(WScreen * scr)
521 NetData *data;
522 int i;
524 #ifdef DEBUG_WMSPEC
525 printf("wNETWMInitStuff\n");
526 #endif
528 #ifdef HAVE_XINTERNATOMS
530 Atom atoms[atomNr];
531 char *names[atomNr];
533 for (i = 0; i < atomNr; ++i) {
534 names[i] = atomNames[i].name;
536 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
537 for (i = 0; i < atomNr; ++i) {
538 *atomNames[i].atom = atoms[i];
541 #else
542 for (i = 0; i < atomNr; i++) {
543 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
545 #endif
547 data = wmalloc(sizeof(NetData));
548 data->scr = scr;
549 data->strut = NULL;
550 data->show_desktop = NULL;
552 scr->netdata = data;
554 setSupportedHints(scr);
556 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
557 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
558 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
559 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
560 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
561 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
562 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
564 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
565 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
566 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
567 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
569 updateClientList(scr);
570 updateClientListStacking(scr, NULL);
571 updateWorkspaceCount(scr);
572 updateWorkspaceNames(scr);
573 updateShowDesktop(scr, False);
575 wScreenUpdateUsableArea(scr);
578 void wNETWMCleanup(WScreen * scr)
580 int i;
582 for (i = 0; i < atomNr; i++)
583 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
586 void wNETWMUpdateActions(WWindow * wwin, Bool del)
588 Atom action[10]; /* nr of actions atoms defined */
589 int i = 0;
591 if (del) {
592 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
593 return;
596 if (IS_MOVABLE(wwin))
597 action[i++] = net_wm_action_move;
599 if (IS_RESIZABLE(wwin))
600 action[i++] = net_wm_action_resize;
602 if (!WFLAGP(wwin, no_miniaturizable))
603 action[i++] = net_wm_action_minimize;
605 if (!WFLAGP(wwin, no_shadeable))
606 action[i++] = net_wm_action_shade;
608 /* if (!WFLAGP(wwin, no_stickable)) */
609 action[i++] = net_wm_action_stick;
611 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
612 if (IS_RESIZABLE(wwin))
613 action[i++] = net_wm_action_maximize_horz;
615 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
616 if (IS_RESIZABLE(wwin))
617 action[i++] = net_wm_action_maximize_vert;
619 /* if (!WFLAGP(wwin, no_fullscreen)) */
620 action[i++] = net_wm_action_fullscreen;
622 /* if (!WFLAGP(wwin, no_change_desktop)) */
623 action[i++] = net_wm_action_change_desktop;
625 if (!WFLAGP(wwin, no_closable))
626 action[i++] = net_wm_action_close;
628 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
629 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
632 void wNETWMUpdateWorkarea(WScreen * scr, WArea usableArea)
634 long *area;
635 int count, i;
637 /* XXX: not Xinerama compatible,
638 xinerama gets the largest available */
640 if (!scr->netdata || scr->workspace_count == 0)
641 return;
643 count = scr->workspace_count * 4;
644 area = wmalloc(sizeof(long) * count);
645 for (i = 0; i < scr->workspace_count; i++) {
646 area[4 * i + 0] = usableArea.x1;
647 area[4 * i + 1] = usableArea.y1;
648 area[4 * i + 2] = usableArea.x2 - usableArea.x1;
649 area[4 * i + 3] = usableArea.y2 - usableArea.y1;
652 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
653 PropModeReplace, (unsigned char *)area, count);
655 wfree(area);
658 Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
660 WReservedArea *cur;
661 WMRect rect;
663 if (!scr->netdata || !scr->netdata->strut)
664 return False;
666 area->x1 = area->y1 = area->x2 = area->y2 = 0;
668 for (cur = scr->netdata->strut; cur; cur = cur->next) {
669 WWindow *wwin = wWindowFor(cur->window);
670 if (wWindowTouchesHead(wwin, head)) {
671 if (cur->area.x1 > area->x1)
672 area->x1 = cur->area.x1;
673 if (cur->area.y1 > area->y1)
674 area->y1 = cur->area.y1;
675 if (cur->area.x2 > area->x2)
676 area->x2 = cur->area.x2;
677 if (cur->area.y2 > area->y2)
678 area->y2 = cur->area.y2;
682 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
683 return False;
685 rect = wGetRectForHead(scr, head);
687 area->x1 = rect.pos.x + area->x1;
688 area->x2 = rect.pos.x + rect.size.width - area->x2;
689 area->y1 = rect.pos.y + area->y1;
690 area->y2 = rect.pos.y + rect.size.height - area->y2;
692 return True;
695 static void updateClientList(WScreen * scr)
697 WWindow *wwin;
698 Window *windows;
699 int count;
701 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
703 count = 0;
704 wwin = scr->focused_window;
705 while (wwin) {
706 windows[count++] = wwin->client_win;
707 wwin = wwin->prev;
709 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
710 PropModeReplace, (unsigned char *)windows, count);
712 wfree(windows);
713 XFlush(dpy);
716 static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
718 WWindow *wwin;
719 Window *client_list;
720 Window *client_list_reverse;
721 int client_count;
722 WCoreWindow *tmp;
723 WMBagIterator iter;
724 int i;
726 /* update client list */
727 i = scr->window_count + 1;
728 client_list = (Window *) wmalloc(sizeof(Window) * i);
729 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
731 client_count = 0;
732 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
733 while (tmp) {
734 wwin = wWindowFor(tmp->window);
735 /* wwin_excl is a window to exclude from the list
736 (e.g. it's now unmanaged) */
737 if (wwin && (wwin != wwin_excl))
738 client_list[client_count++] = wwin->client_win;
739 tmp = tmp->stacking->under;
743 for (i = 0; i < client_count; i++) {
744 Window w = client_list[client_count - i - 1];
745 client_list_reverse[i] = w;
748 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
749 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
751 wfree(client_list);
752 wfree(client_list_reverse);
754 XFlush(dpy);
757 static void updateWorkspaceCount(WScreen * scr)
758 { /* changeable */
759 long count;
761 count = scr->workspace_count;
763 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
764 32, PropModeReplace, (unsigned char *)&count, 1);
767 static void updateCurrentWorkspace(WScreen * scr)
768 { /* changeable */
769 long count;
771 count = scr->current_workspace;
773 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
774 PropModeReplace, (unsigned char *)&count, 1);
777 static void updateWorkspaceNames(WScreen * scr)
779 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
780 unsigned int i, len, curr_size;
782 pos = buf;
783 len = 0;
784 for (i = 0; i < scr->workspace_count; i++) {
785 curr_size = strlen(scr->workspaces[i]->name);
786 strcpy(pos, scr->workspaces[i]->name);
787 pos += (curr_size + 1);
788 len += (curr_size + 1);
791 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
792 PropModeReplace, (unsigned char *)buf, len);
795 static void updateFocusHint(WScreen * scr, WWindow * wwin)
796 { /* changeable */
797 Window window;
799 if (!scr->focused_window || !scr->focused_window->flags.focused)
800 window = None;
801 else
802 window = scr->focused_window->client_win;
804 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
805 PropModeReplace, (unsigned char *)&window, 1);
808 static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
810 long l;
812 if (del) {
813 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
814 } else {
815 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
816 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
817 32, PropModeReplace, (unsigned char *)&l, 1);
821 static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
822 { /* changeable */
823 if (del) {
824 if (!wwin->flags.net_state_from_client) {
825 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
827 } else {
828 Atom state[15]; /* nr of defined state atoms */
829 int i = 0;
831 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
832 updateWorkspaceHint(wwin, False, False);
834 if (IS_OMNIPRESENT(wwin))
835 state[i++] = net_wm_state_sticky;
836 if (wwin->flags.shaded)
837 state[i++] = net_wm_state_shaded;
838 if (wwin->flags.maximized & MAX_HORIZONTAL)
839 state[i++] = net_wm_state_maximized_horz;
840 if (wwin->flags.maximized & MAX_VERTICAL)
841 state[i++] = net_wm_state_maximized_vert;
842 if (WFLAGP(wwin, skip_window_list))
843 state[i++] = net_wm_state_skip_taskbar;
844 if (wwin->flags.net_skip_pager)
845 state[i++] = net_wm_state_skip_pager;
846 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
847 state[i++] = net_wm_state_hidden;
848 state[i++] = net_wm_state_skip_pager;
850 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
851 if (!IS_OMNIPRESENT(wwin))
852 updateWorkspaceHint(wwin, True, False);
853 state[i++] = net_wm_state_sticky;
856 if (WFLAGP(wwin, sunken))
857 state[i++] = net_wm_state_below;
858 if (WFLAGP(wwin, floating))
859 state[i++] = net_wm_state_above;
860 if (wwin->flags.fullscreen)
861 state[i++] = net_wm_state_fullscreen;
863 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
864 PropModeReplace, (unsigned char *)state, i);
868 static Bool updateStrut(WWindow * wwin, Bool adding)
870 WScreen *scr;
871 WReservedArea *area;
872 Bool hasState = False;
874 scr = wwin->screen_ptr;
876 if (adding) {
877 Atom type_ret;
878 int fmt_ret;
879 unsigned long nitems_ret;
880 unsigned long bytes_after_ret;
881 long *data = NULL;
883 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
884 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
885 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
887 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
888 area->area.x1 = data[0];
889 area->area.x2 = data[1];
890 area->area.y1 = data[2];
891 area->area.y2 = data[3];
893 area->window = wwin->client_win;
895 area->next = scr->netdata->strut;
896 scr->netdata->strut = area;
898 XFree(data);
899 hasState = True;
902 } else {
903 /* deleting */
904 area = scr->netdata->strut;
906 if (area) {
907 if (area->window == wwin->client_win) {
908 scr->netdata->strut = area->next;
909 wfree(area);
910 hasState = True;
911 } else {
912 while (area->next && area->next->window != wwin->client_win)
913 area = area->next;
915 if (area->next) {
916 WReservedArea *next;
918 next = area->next->next;
919 wfree(area->next);
920 area->next = next;
922 hasState = True;
928 return hasState;
931 static int getWindowLayer(WWindow * wwin)
933 int layer = WMNormalLevel;
935 if (wwin->type == net_wm_window_type_desktop) {
936 layer = WMDesktopLevel;
937 } else if (wwin->type == net_wm_window_type_dock) {
938 layer = WMDockLevel;
939 } else if (wwin->type == net_wm_window_type_toolbar) {
940 layer = WMMainMenuLevel;
941 } else if (wwin->type == net_wm_window_type_menu) {
942 layer = WMSubmenuLevel;
943 } else if (wwin->type == net_wm_window_type_utility) {
944 } else if (wwin->type == net_wm_window_type_splash) {
945 } else if (wwin->type == net_wm_window_type_dialog) {
946 if (wwin->transient_for) {
947 WWindow *parent = wWindowFor(wwin->transient_for);
948 if (parent && parent->flags.fullscreen) {
949 layer = WMFullscreenLevel;
952 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
953 } else if (wwin->type == net_wm_window_type_normal) {
956 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
957 layer = WMSunkenLevel;
958 if (wwin->client_flags.floating && WMFloatingLevel > layer)
959 layer = WMFloatingLevel;
961 return layer;
964 static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
967 if (state == net_wm_state_sticky) {
968 if (set == _NET_WM_STATE_TOGGLE) {
969 set = !IS_OMNIPRESENT(wwin);
971 if (set != wwin->flags.omnipresent) {
972 wWindowSetOmnipresent(wwin, set);
974 } else if (state == net_wm_state_shaded) {
975 if (set == _NET_WM_STATE_TOGGLE) {
976 set = !wwin->flags.shaded;
978 if (init) {
979 wwin->flags.shaded = set;
980 } else {
981 if (set) {
982 wShadeWindow(wwin);
983 } else {
984 wUnshadeWindow(wwin);
987 } else if (state == net_wm_state_skip_taskbar) {
988 if (set == _NET_WM_STATE_TOGGLE) {
989 set = !wwin->client_flags.skip_window_list;
991 wwin->client_flags.skip_window_list = set;
992 } else if (state == net_wm_state_skip_pager) {
993 if (set == _NET_WM_STATE_TOGGLE) {
994 set = !wwin->flags.net_skip_pager;
996 wwin->flags.net_skip_pager = set;
997 } else if (state == net_wm_state_maximized_vert) {
998 if (set == _NET_WM_STATE_TOGGLE) {
999 set = !(wwin->flags.maximized & MAX_VERTICAL);
1001 if (init) {
1002 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1003 } else {
1004 if (set) {
1005 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1006 } else {
1007 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1010 } else if (state == net_wm_state_maximized_horz) {
1011 if (set == _NET_WM_STATE_TOGGLE) {
1012 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1014 if (init) {
1015 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1016 } else {
1017 if (set) {
1018 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1019 } else {
1020 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1023 } else if (state == net_wm_state_hidden) {
1024 if (set == _NET_WM_STATE_TOGGLE) {
1025 set = !(wwin->flags.miniaturized);
1027 if (init) {
1028 wwin->flags.miniaturized = set;
1029 } else {
1030 if (set) {
1031 wIconifyWindow(wwin);
1032 } else {
1033 wDeiconifyWindow(wwin);
1036 } else if (state == net_wm_state_fullscreen) {
1037 if (set == _NET_WM_STATE_TOGGLE) {
1038 set = !(wwin->flags.fullscreen);
1040 if (init) {
1041 wwin->flags.fullscreen = set;
1042 } else {
1043 if (set) {
1044 wFullscreenWindow(wwin);
1045 } else {
1046 wUnfullscreenWindow(wwin);
1049 } else if (state == net_wm_state_above) {
1050 if (set == _NET_WM_STATE_TOGGLE) {
1051 set = !(wwin->client_flags.floating);
1053 if (init) {
1054 wwin->client_flags.floating = set;
1055 } else {
1056 wwin->client_flags.floating = set;
1057 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1060 } else if (state == net_wm_state_below) {
1061 if (set == _NET_WM_STATE_TOGGLE) {
1062 set = !(wwin->client_flags.sunken);
1064 if (init) {
1065 wwin->client_flags.sunken = set;
1066 } else {
1067 wwin->client_flags.sunken = set;
1068 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1071 } else {
1072 #ifdef DEBUG_WMSPEC
1073 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1074 #endif
1078 static void removeIcon(WWindow * wwin)
1080 if (wwin->icon == NULL)
1081 return;
1082 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1083 XUnmapWindow(dpy, wwin->icon->core->window);
1084 RemoveFromStackList(wwin->icon->core);
1085 wIconDestroy(wwin->icon);
1086 wwin->icon = NULL;
1090 static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
1092 Bool ret = True;
1094 if (type == net_wm_window_type_desktop) {
1095 wwin->client_flags.no_titlebar = 1;
1096 wwin->client_flags.no_resizable = 1;
1097 wwin->client_flags.no_miniaturizable = 1;
1098 wwin->client_flags.no_border = 1;
1099 wwin->client_flags.no_resizebar = 1;
1100 wwin->client_flags.no_shadeable = 1;
1101 wwin->client_flags.no_movable = 1;
1102 wwin->client_flags.omnipresent = 1;
1103 wwin->client_flags.skip_window_list = 1;
1104 wwin->client_flags.dont_move_off = 1;
1105 wwin->client_flags.no_appicon = 1;
1106 wwin->flags.net_skip_pager = 1;
1107 wwin->frame_x = 0;
1108 wwin->frame_y = 0;
1109 } else if (type == net_wm_window_type_dock) {
1110 wwin->client_flags.no_titlebar = 1;
1111 wwin->client_flags.no_resizable = 1;
1112 wwin->client_flags.no_miniaturizable = 1;
1113 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1114 wwin->client_flags.no_resizebar = 1;
1115 wwin->client_flags.no_shadeable = 1;
1116 wwin->client_flags.no_movable = 1;
1117 wwin->client_flags.omnipresent = 1;
1118 wwin->client_flags.skip_window_list = 1;
1119 wwin->client_flags.dont_move_off = 1;
1120 wwin->flags.net_skip_pager = 1;
1121 } else if (type == net_wm_window_type_toolbar) {
1122 wwin->client_flags.no_titlebar = 1;
1123 wwin->client_flags.no_resizable = 1;
1124 wwin->client_flags.no_miniaturizable = 1;
1125 wwin->client_flags.no_resizebar = 1;
1126 wwin->client_flags.no_shadeable = 1;
1127 wwin->client_flags.skip_window_list = 1;
1128 wwin->client_flags.dont_move_off = 1;
1129 wwin->client_flags.no_appicon = 1;
1130 } else if (type == net_wm_window_type_menu) {
1131 wwin->client_flags.no_titlebar = 1;
1132 wwin->client_flags.no_resizable = 1;
1133 wwin->client_flags.no_miniaturizable = 1;
1134 wwin->client_flags.no_resizebar = 1;
1135 wwin->client_flags.no_shadeable = 1;
1136 wwin->client_flags.skip_window_list = 1;
1137 wwin->client_flags.dont_move_off = 1;
1138 wwin->client_flags.no_appicon = 1;
1139 } else if (type == net_wm_window_type_utility) {
1140 wwin->client_flags.no_appicon = 1;
1141 } else if (type == net_wm_window_type_splash) {
1142 wwin->client_flags.no_titlebar = 1;
1143 wwin->client_flags.no_resizable = 1;
1144 wwin->client_flags.no_miniaturizable = 1;
1145 wwin->client_flags.no_resizebar = 1;
1146 wwin->client_flags.no_shadeable = 1;
1147 wwin->client_flags.no_movable = 1;
1148 wwin->client_flags.skip_window_list = 1;
1149 wwin->client_flags.dont_move_off = 1;
1150 wwin->client_flags.no_appicon = 1;
1151 wwin->flags.net_skip_pager = 1;
1152 } else if (type == net_wm_window_type_dialog) {
1153 /* These also seem a bad idea in our context -Dan
1154 // wwin->client_flags.skip_window_list = 1;
1155 // wwin->client_flags.no_appicon = 1;
1157 } else if (type == net_wm_window_type_normal) {
1158 } else {
1159 ret = False;
1162 wwin->type = type;
1163 *layer = getWindowLayer(wwin);
1165 return ret;
1168 void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
1170 if (wwin->type == net_wm_window_type_splash) {
1171 WScreen *scr = wwin->screen_ptr;
1172 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1173 *x = rect.pos.x + (rect.size.width - width) / 2;
1174 *y = rect.pos.y + (rect.size.height - height) / 2;
1178 static void updateWindowType(WWindow * wwin)
1180 Atom type_ret;
1181 int fmt_ret;
1182 unsigned long nitems_ret;
1183 unsigned long bytes_after_ret;
1184 long *data = NULL;
1185 int layer;
1186 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1187 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1188 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1190 int i;
1191 Atom *type = (Atom *) data;
1192 for (i = 0; i < nitems_ret; ++i) {
1193 if (handleWindowType(wwin, type[i], &layer))
1194 break;
1196 XFree(data);
1199 if (wwin->frame != NULL) {
1200 ChangeStackingLevel(wwin->frame->core, layer);
1201 wwin->frame->flags.need_texture_change = 1;
1202 wWindowConfigureBorders(wwin);
1203 wFrameWindowPaint(wwin->frame);
1204 wNETWMUpdateActions(wwin, False);
1208 Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
1210 Atom type_ret;
1211 int fmt_ret;
1212 unsigned long nitems_ret;
1213 unsigned long bytes_after_ret;
1214 long *data = NULL;
1215 Bool hasState = False;
1216 int i;
1218 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1219 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1220 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1222 long desktop = *data;
1223 XFree(data);
1225 if (desktop == -1)
1226 wwin->client_flags.omnipresent = 1;
1227 else
1228 *workspace = desktop;
1230 hasState = True;
1233 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1234 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1235 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1237 Atom *state = (Atom *) data;
1238 for (i = 0; i < nitems_ret; ++i) {
1239 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1241 XFree(data);
1242 hasState = True;
1245 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1246 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1247 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1249 Atom *type = (Atom *) data;
1250 for (i = 0; i < nitems_ret; ++i) {
1251 if (handleWindowType(wwin, type[i], layer))
1252 break;
1254 XFree(data);
1255 hasState = True;
1258 wNETWMUpdateActions(wwin, False);
1259 updateStrut(wwin, False);
1260 if (updateStrut(wwin, True)) {
1261 hasState = True;
1263 wScreenUpdateUsableArea(wwin->screen_ptr);
1265 return hasState;
1268 static Bool updateNetIconInfo(WWindow * wwin)
1271 Atom type_ret;
1272 int fmt_ret;
1273 unsigned long nitems_ret;
1274 unsigned long bytes_after_ret;
1275 long *data = NULL;
1276 Bool hasState = False;
1277 Bool old_state = wwin->flags.net_handle_icon;
1279 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1280 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1281 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1282 long handled = *data;
1283 wwin->flags.net_handle_icon = (handled != 0);
1284 XFree(data);
1285 hasState = True;
1287 } else
1288 wwin->flags.net_handle_icon = False;
1290 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1291 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1292 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1294 #ifdef NETWM_PROPER
1295 if (wwin->flags.net_handle_icon)
1296 #else
1297 wwin->flags.net_handle_icon = True;
1298 #endif
1300 wwin->icon_x = data[0];
1301 wwin->icon_y = data[1];
1302 wwin->icon_w = data[2];
1303 wwin->icon_h = data[3];
1306 XFree(data);
1307 hasState = True;
1309 } else
1310 wwin->flags.net_handle_icon = False;
1312 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1313 if (wwin->flags.net_handle_icon) {
1314 removeIcon(wwin);
1315 } else {
1316 wwin->flags.miniaturized = False;
1317 wwin->flags.skip_next_animation = True;
1318 wIconifyWindow(wwin);
1322 return hasState;
1325 Bool wNETWMCheckInitialClientState(WWindow * wwin)
1327 Bool hasState = False;
1329 #ifdef DEBUG_WMSPEC
1330 printf("CheckInitialClientState\n");
1331 #endif
1333 wNETWMShowingDesktop(wwin->screen_ptr, False);
1335 hasState |= updateNetIconInfo(wwin);
1337 updateIconImage(wwin);
1339 return hasState;
1342 static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
1344 unsigned long nitems_ret, bytes_after_ret;
1345 char *data, *names[32];
1346 int fmt_ret, i, n;
1347 Atom type_ret;
1349 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1350 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1351 &bytes_after_ret, (unsigned char **)&data) != Success) {
1352 return;
1355 if (data == NULL)
1356 return;
1358 if (type_ret != utf8_string || fmt_ret != 8)
1359 return;
1361 n = 0;
1362 names[n] = data;
1363 for (i = 0; i < nitems_ret; i++) {
1364 if (data[i] == 0) {
1365 n++;
1366 names[n] = &data[i];
1367 } else if (*names[n] == 0) {
1368 names[n] = &data[i];
1369 wWorkspaceRename(scr, n, names[n]);
1374 Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
1376 WScreen *scr;
1377 WWindow *wwin;
1378 Bool done = True;
1380 #ifdef DEBUG_WMSPEC
1381 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1382 #endif
1384 scr = wScreenForWindow(event->window);
1385 if (scr) {
1386 /* generic client messages */
1387 if (event->message_type == net_current_desktop) {
1388 wWorkspaceChange(scr, event->data.l[0]);
1389 } else if (event->message_type == net_number_of_desktops) {
1390 long value;
1392 value = event->data.l[0];
1393 if (value > scr->workspace_count) {
1394 wWorkspaceMake(scr, value - scr->workspace_count);
1395 } else if (value < scr->workspace_count) {
1396 int i;
1397 Bool rebuild = False;
1399 for (i = scr->workspace_count - 1; i >= value; i--) {
1400 if (!wWorkspaceDelete(scr, i)) {
1401 rebuild = True;
1402 break;
1406 if (rebuild) {
1407 updateWorkspaceCount(scr);
1410 } else if (event->message_type == net_showing_desktop) {
1411 wNETWMShowingDesktop(scr, event->data.l[0]);
1412 } else if (event->message_type == net_desktop_names) {
1413 handleDesktopNames(event, scr);
1414 } else {
1415 done = False;
1418 if (done)
1419 return True;
1422 /* window specific client messages */
1424 wwin = wWindowFor(event->window);
1425 if (!wwin)
1426 return False;
1428 if (event->message_type == net_active_window) {
1430 * Firefox sends aditional 'net_active_window' signals on startup if
1431 * multiple tabs are open. That causes unnecessary workspace switching if
1432 * those signals come from other workspaces. Therefore we also check if
1433 * we should ignore these spurious focus across workspaces requests (but
1434 * allow the switching if it comes from a pager).
1436 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace
1437 || !WFLAGP(wwin, dont_focus_across_wksp) || event->data.l[0] == 2) {
1438 wNETWMShowingDesktop(scr, False);
1439 wMakeWindowVisible(wwin);
1441 } else if (event->message_type == net_close_window) {
1442 if (!WFLAGP(wwin, no_closable)) {
1443 if (wwin->protocols.DELETE_WINDOW)
1444 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1446 } else if (event->message_type == net_wm_state) {
1447 int maximized = wwin->flags.maximized;
1448 long set = event->data.l[0];
1450 #ifdef DEBUG_WMSPEC
1451 printf("net_wm_state set %ld a1 %s a2 %s\n", set,
1452 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1453 #endif
1455 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1456 if (event->data.l[2])
1457 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1459 if (wwin->flags.maximized != maximized) {
1460 if (!wwin->flags.maximized) {
1461 wwin->flags.maximized = maximized;
1462 wUnmaximizeWindow(wwin);
1463 } else {
1464 wMaximizeWindow(wwin, wwin->flags.maximized);
1467 updateStateHint(wwin, False, False);
1468 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1469 updateNetIconInfo(wwin);
1470 } else if (event->message_type == net_wm_desktop) {
1471 long desktop = event->data.l[0];
1472 if (desktop == -1) {
1473 wWindowSetOmnipresent(wwin, True);
1474 } else {
1475 if (IS_OMNIPRESENT(wwin))
1476 wWindowSetOmnipresent(wwin, False);
1477 wWindowChangeWorkspace(wwin, desktop);
1479 } else {
1480 done = False;
1483 return done;
1486 Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
1488 Bool ret = True;
1490 #ifdef DEBUG_WMSPEC
1491 printf("clientHintChange type %s\n", XGetAtomName(dpy, event->atom));
1492 #endif
1494 if (event->atom == net_wm_strut) {
1495 updateStrut(wwin, False);
1496 updateStrut(wwin, True);
1497 wScreenUpdateUsableArea(wwin->screen_ptr);
1498 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1499 updateNetIconInfo(wwin);
1500 } else if (event->atom == net_wm_window_type) {
1501 updateWindowType(wwin);
1502 } else if (event->atom == net_wm_name) {
1503 char *name = wNETWMGetWindowName(wwin->client_win);
1504 wWindowUpdateName(wwin, name);
1505 if (name)
1506 wfree(name);
1507 } else if (event->atom == net_wm_icon_name) {
1508 if (wwin->icon) {
1509 char *name = wNETWMGetIconName(wwin->client_win);
1510 wIconChangeTitle(wwin->icon, name);
1512 } else if (event->atom == net_wm_icon) {
1513 updateIconImage(wwin);
1514 } else {
1515 ret = False;
1518 return ret;
1521 int wNETWMGetPidForWindow(Window window)
1523 Atom type_ret;
1524 int fmt_ret;
1525 unsigned long nitems_ret;
1526 unsigned long bytes_after_ret;
1527 long *data = NULL;
1528 int pid;
1530 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1531 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1532 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1534 pid = *data;
1535 XFree(data);
1536 } else {
1537 pid = 0;
1540 return pid;
1543 char *wNETWMGetWindowName(Window window)
1545 char *name;
1546 char *ret;
1547 int size;
1549 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1550 if (name) {
1551 ret = wstrndup(name, size);
1552 XFree(name);
1553 } else
1554 ret = NULL;
1555 return ret;
1558 char *wNETWMGetIconName(Window window)
1560 char *name;
1561 char *ret;
1562 int size;
1564 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1565 if (name) {
1566 ret = wstrndup(name, size);
1567 XFree(name);
1568 } else
1569 ret = NULL;
1570 return ret;
1573 static void observer(void *self, WMNotification * notif)
1575 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1576 const char *name = WMGetNotificationName(notif);
1577 void *data = WMGetNotificationClientData(notif);
1578 NetData *ndata = (NetData *) self;
1580 if (strcmp(name, WMNManaged) == 0 && wwin) {
1581 updateClientList(wwin->screen_ptr);
1582 updateClientListStacking(wwin->screen_ptr, NULL);
1583 updateStateHint(wwin, True, False);
1585 updateStrut(wwin, False);
1586 updateStrut(wwin, True);
1587 wScreenUpdateUsableArea(wwin->screen_ptr);
1588 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1589 updateClientList(wwin->screen_ptr);
1590 updateClientListStacking(wwin->screen_ptr, wwin);
1591 updateWorkspaceHint(wwin, False, True);
1592 updateStateHint(wwin, False, True);
1593 wNETWMUpdateActions(wwin, True);
1595 updateStrut(wwin, False);
1596 wScreenUpdateUsableArea(wwin->screen_ptr);
1597 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1598 updateClientListStacking(wwin->screen_ptr, NULL);
1599 updateStateHint(wwin, False, False);
1600 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1601 updateClientListStacking(wwin->screen_ptr, NULL);
1602 updateStateHint(wwin, False, False);
1603 } else if (strcmp(name, WMNChangedFocus) == 0) {
1604 updateFocusHint(ndata->scr, wwin);
1605 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1606 updateWorkspaceHint(wwin, False, False);
1607 updateStateHint(wwin, True, False);
1608 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1609 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1613 static void wsobserver(void *self, WMNotification * notif)
1615 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1616 const char *name = WMGetNotificationName(notif);
1618 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1619 updateWorkspaceCount(scr);
1620 updateWorkspaceNames(scr);
1621 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1622 updateWorkspaceCount(scr);
1623 updateWorkspaceNames(scr);
1624 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1625 updateCurrentWorkspace(scr);
1626 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1627 updateWorkspaceNames(scr);