Fix some warnings
[wmaker-crm.git] / src / wmspec.c
blobf4a2ceebaafe1fbd193cf8774fcee827d875ad09
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 "wmspec.h"
46 #include "icon.h"
47 #include "stacking.h"
48 #include "xinerama.h"
49 #include "properties.h"
51 #ifdef DEBUG_WMSPEC
52 #include <stdio.h>
53 #endif
55 /* Global variables */
56 extern Atom _XA_WM_DELETE_WINDOW;
57 extern Time LastTimestamp;
58 extern WPreferences wPreferences;
60 /* Root Window Properties */
61 static Atom net_supported;
62 static Atom net_client_list;
63 static Atom net_client_list_stacking;
64 static Atom net_number_of_desktops;
65 static Atom net_desktop_geometry;
66 static Atom net_desktop_viewport;
67 static Atom net_current_desktop;
68 static Atom net_desktop_names;
69 static Atom net_active_window;
70 static Atom net_workarea; /* XXX: not xinerama compatible */
71 static Atom net_supporting_wm_check;
72 static Atom net_virtual_roots; /* N/A */
73 static Atom net_desktop_layout; /* XXX */
74 static Atom net_showing_desktop;
76 /* Other Root Window Messages */
77 static Atom net_close_window;
78 static Atom net_moveresize_window; /* TODO */
79 static Atom net_wm_moveresize; /* TODO */
81 /* Application Window Properties */
82 static Atom net_wm_name;
83 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
84 static Atom net_wm_icon_name;
85 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
86 static Atom net_wm_desktop;
87 static Atom net_wm_window_type;
88 static Atom net_wm_window_type_desktop;
89 static Atom net_wm_window_type_dock;
90 static Atom net_wm_window_type_toolbar;
91 static Atom net_wm_window_type_menu;
92 static Atom net_wm_window_type_utility;
93 static Atom net_wm_window_type_splash;
94 static Atom net_wm_window_type_dialog;
95 static Atom net_wm_window_type_normal;
96 static Atom net_wm_state;
97 static Atom net_wm_state_modal; /* XXX: what is this?!? */
98 static Atom net_wm_state_sticky;
99 static Atom net_wm_state_maximized_vert;
100 static Atom net_wm_state_maximized_horz;
101 static Atom net_wm_state_shaded;
102 static Atom net_wm_state_skip_taskbar;
103 static Atom net_wm_state_skip_pager;
104 static Atom net_wm_state_hidden;
105 static Atom net_wm_state_fullscreen;
106 static Atom net_wm_state_above;
107 static Atom net_wm_state_below;
108 static Atom net_wm_allowed_actions;
109 static Atom net_wm_action_move;
110 static Atom net_wm_action_resize;
111 static Atom net_wm_action_minimize;
112 static Atom net_wm_action_shade;
113 static Atom net_wm_action_stick;
114 static Atom net_wm_action_maximize_horz;
115 static Atom net_wm_action_maximize_vert;
116 static Atom net_wm_action_fullscreen;
117 static Atom net_wm_action_change_desktop;
118 static Atom net_wm_action_close;
119 static Atom net_wm_strut; /* XXX: see net_workarea */
120 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
121 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. */
122 static Atom net_wm_icon;
123 static Atom net_wm_pid; /* TODO */
124 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
126 /* Window Manager Protocols */
127 static Atom net_wm_ping; /* TODO */
129 static Atom utf8_string;
131 typedef struct {
132 char *name;
133 Atom *atom;
134 } atomitem_t;
136 static atomitem_t atomNames[] = {
137 {"_NET_SUPPORTED", &net_supported},
138 {"_NET_CLIENT_LIST", &net_client_list},
139 {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
140 {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
141 {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
142 {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
143 {"_NET_CURRENT_DESKTOP", &net_current_desktop},
144 {"_NET_DESKTOP_NAMES", &net_desktop_names},
145 {"_NET_ACTIVE_WINDOW", &net_active_window},
146 {"_NET_WORKAREA", &net_workarea},
147 {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
148 {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
149 {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
150 {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
152 {"_NET_CLOSE_WINDOW", &net_close_window},
153 {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
154 {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
156 {"_NET_WM_NAME", &net_wm_name},
157 {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
158 {"_NET_WM_ICON_NAME", &net_wm_icon_name},
159 {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
160 {"_NET_WM_DESKTOP", &net_wm_desktop},
161 {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
162 {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
163 {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
164 {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
165 {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
166 {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
167 {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
168 {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
169 {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
170 {"_NET_WM_STATE", &net_wm_state},
171 {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
172 {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
173 {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
174 {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
175 {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
176 {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
177 {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
178 {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
179 {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
180 {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
181 {"_NET_WM_STATE_BELOW", &net_wm_state_below},
182 {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
183 {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
184 {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
185 {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
186 {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
187 {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
188 {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
189 {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
190 {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
191 {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
192 {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
193 {"_NET_WM_STRUT", &net_wm_strut},
194 {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
195 {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
196 {"_NET_WM_ICON", &net_wm_icon},
197 {"_NET_WM_PID", &net_wm_pid},
198 {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
200 {"_NET_WM_PING", &net_wm_ping},
202 {"UTF8_STRING", &utf8_string},
205 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
207 #define _NET_WM_STATE_REMOVE 0
208 #define _NET_WM_STATE_ADD 1
209 #define _NET_WM_STATE_TOGGLE 2
211 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
212 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
213 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
214 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
215 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
216 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
217 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
218 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
219 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
220 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
221 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
223 static void observer(void *self, WMNotification * notif);
224 static void wsobserver(void *self, WMNotification * notif);
226 static void updateClientList(WScreen * scr);
227 static void updateClientListStacking(WScreen * scr, WWindow *);
229 static void updateWorkspaceNames(WScreen * scr);
230 static void updateCurrentWorkspace(WScreen * scr);
231 static void updateWorkspaceCount(WScreen * scr);
233 typedef struct NetData {
234 WScreen *scr;
235 WReservedArea *strut;
236 WWindow **show_desktop;
237 } NetData;
239 static void setSupportedHints(WScreen * scr)
241 Atom atom[atomNr];
242 int i = 0;
244 /* set supported hints list */
245 /* XXX: extend this !!! */
247 atom[i++] = net_client_list;
248 atom[i++] = net_client_list_stacking;
249 atom[i++] = net_number_of_desktops;
250 atom[i++] = net_desktop_geometry;
251 atom[i++] = net_desktop_viewport;
252 atom[i++] = net_current_desktop;
253 atom[i++] = net_desktop_names;
254 atom[i++] = net_active_window;
255 atom[i++] = net_workarea;
256 atom[i++] = net_supporting_wm_check;
257 atom[i++] = net_showing_desktop;
258 #if 0
259 atom[i++] = net_wm_moveresize;
260 #endif
261 atom[i++] = net_wm_desktop;
262 atom[i++] = net_wm_window_type;
263 atom[i++] = net_wm_window_type_desktop;
264 atom[i++] = net_wm_window_type_dock;
265 atom[i++] = net_wm_window_type_toolbar;
266 atom[i++] = net_wm_window_type_menu;
267 atom[i++] = net_wm_window_type_utility;
268 atom[i++] = net_wm_window_type_splash;
269 atom[i++] = net_wm_window_type_dialog;
270 atom[i++] = net_wm_window_type_normal;
272 atom[i++] = net_wm_state;
273 /* atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
274 atom[i++] = net_wm_state_sticky;
275 atom[i++] = net_wm_state_shaded;
276 atom[i++] = net_wm_state_maximized_horz;
277 atom[i++] = net_wm_state_maximized_vert;
278 atom[i++] = net_wm_state_skip_taskbar;
279 atom[i++] = net_wm_state_skip_pager;
280 atom[i++] = net_wm_state_hidden;
281 atom[i++] = net_wm_state_fullscreen;
282 atom[i++] = net_wm_state_above;
283 atom[i++] = net_wm_state_below;
285 atom[i++] = net_wm_allowed_actions;
286 atom[i++] = net_wm_action_move;
287 atom[i++] = net_wm_action_resize;
288 atom[i++] = net_wm_action_minimize;
289 atom[i++] = net_wm_action_shade;
290 atom[i++] = net_wm_action_stick;
291 atom[i++] = net_wm_action_maximize_horz;
292 atom[i++] = net_wm_action_maximize_vert;
293 atom[i++] = net_wm_action_fullscreen;
294 atom[i++] = net_wm_action_change_desktop;
295 atom[i++] = net_wm_action_close;
297 atom[i++] = net_wm_strut;
298 atom[i++] = net_wm_icon_geometry;
299 atom[i++] = net_wm_icon;
300 atom[i++] = net_wm_handled_icons;
302 atom[i++] = net_wm_name;
303 atom[i++] = net_wm_icon_name;
305 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
307 /* set supporting wm hint */
308 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
309 PropModeReplace, (unsigned char *)&scr->info_window, 1);
311 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
312 32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
315 void wNETWMUpdateDesktop(WScreen * scr)
317 long *views, sizes[2];
318 int count, i;
320 if (scr->workspace_count == 0)
321 return;
323 count = scr->workspace_count * 2;
324 views = wmalloc(sizeof(long) * count);
325 /*memset(views, 0, sizeof(long) * count); */
326 sizes[0] = scr->scr_width;
327 sizes[1] = scr->scr_height;
329 for (i = 0; i < scr->workspace_count; i++) {
330 views[2 * i + 0] = 0;
331 views[2 * i + 1] = 0;
334 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
335 PropModeReplace, (unsigned char *)sizes, 2);
337 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
338 PropModeReplace, (unsigned char *)views, count);
340 wfree(views);
343 int wNETWMGetCurrentDesktopFromHint(WScreen * scr)
345 int count;
346 unsigned char *prop;
348 prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
349 if (prop) {
350 int desktop = *(long *)prop;
351 XFree(prop);
352 return desktop;
354 return -1;
358 * Find the best icon to be used by Window Maker for appicon/miniwindows.
359 * Currently the algorithm is to take the image with the size closest
360 * to icon_size x icon_size, but never bigger than that.
362 * This algorithm is very poorly implemented and needs to be redone (it can
363 * easily select images with very large widths and very small heights over
364 * square images, if the area of the former is closer to the desired one).
366 * The logic can also be changed to accept bigger images and scale them down.
368 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
370 int size, wanted, d;
371 unsigned long i, distance;
372 unsigned long *icon;
374 /* better use only 75% of icon_size. For 64x64 this means 48x48
375 * This leaves room around the icon for the miniwindow title and
376 * results in better overall aesthetics -Dan */
377 wanted = wPreferences.icon_size * wPreferences.icon_size;
379 for (icon = NULL, distance = wanted, i = 0L; i < items - 1;) {
380 size = data[i] * data[i + 1];
381 if (size == 0)
382 break;
383 d = wanted - size;
384 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
385 distance = d;
386 icon = &data[i];
388 i += size + 2;
391 return icon;
394 static RImage *makeRImageFromARGBData(unsigned long *data)
396 int size, width, height, i;
397 RImage *image;
398 unsigned char *imgdata;
399 unsigned long pixel;
401 width = data[0];
402 height = data[1];
403 size = width * height;
405 if (size == 0)
406 return NULL;
408 image = RCreateImage(width, height, True);
410 for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
411 pixel = data[i];
412 #if BYTE_ORDER == BIG_ENDIAN
413 imgdata[2] = (pixel >> 24) & 0xff; /* A */
414 imgdata[1] = (pixel >> 16) & 0xff; /* R */
415 imgdata[0] = (pixel >> 8) & 0xff; /* G */
416 imgdata[3] = (pixel >> 0) & 0xff; /* B */
417 #else /* Little endian */
418 imgdata[3] = (pixel >> 24) & 0xff; /* A */
419 imgdata[0] = (pixel >> 16) & 0xff; /* R */
420 imgdata[1] = (pixel >> 8) & 0xff; /* G */
421 imgdata[2] = (pixel >> 0) & 0xff; /* B */
422 #endif /* endianness */
426 return image;
429 static void updateIconImage(WScreen * scr, WWindow * wwin)
431 unsigned long *property, *data;
432 unsigned long items, rest;
433 Atom type;
434 int format;
435 RImage *image;
437 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
438 False, XA_CARDINAL, &type, &format, &items, &rest,
439 (unsigned char **)&property) != Success || !property) {
440 return;
443 if (type != XA_CARDINAL || format != 32 || items < 2) {
444 XFree(property);
445 return;
448 data = findBestIcon(property, items);
449 if (!data) {
450 XFree(property);
451 return;
454 image = makeRImageFromARGBData(data);
456 if (image) {
457 if (wwin->net_icon_image)
458 RReleaseImage(wwin->net_icon_image);
459 wwin->net_icon_image = image;
462 XFree(property);
465 static void updateShowDesktop(WScreen * scr, Bool show)
467 long foo;
469 foo = (show == True);
470 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
471 PropModeReplace, (unsigned char *)&foo, 1);
474 void wNETWMShowingDesktop(WScreen * scr, Bool show)
476 if (show && scr->netdata->show_desktop == NULL) {
477 WWindow *tmp, **wins;
478 int i = 0;
480 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
482 tmp = scr->focused_window;
483 while (tmp) {
484 if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
486 wins[i++] = tmp;
487 tmp->flags.skip_next_animation = 1;
488 tmp->flags.net_show_desktop = 1;
489 wIconifyWindow(tmp);
492 tmp = tmp->prev;
494 wins[i++] = NULL;
496 scr->netdata->show_desktop = wins;
497 updateShowDesktop(scr, True);
498 } else if (scr->netdata->show_desktop != NULL) {
499 /* FIXME: get rid of workspace flashing ! */
500 int ws = scr->current_workspace;
501 WWindow **tmp;
502 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
503 wDeiconifyWindow(*tmp);
504 (*tmp)->flags.net_show_desktop = 0;
506 if (ws != scr->current_workspace)
507 wWorkspaceChange(scr, ws);
508 wfree(scr->netdata->show_desktop);
509 scr->netdata->show_desktop = NULL;
510 updateShowDesktop(scr, False);
514 void wNETWMInitStuff(WScreen * scr)
516 NetData *data;
517 int i;
519 #ifdef DEBUG_WMSPEC
520 printf("wNETWMInitStuff\n");
521 #endif
523 #ifdef HAVE_XINTERNATOMS
525 Atom atoms[atomNr];
526 char *names[atomNr];
528 for (i = 0; i < atomNr; ++i) {
529 names[i] = atomNames[i].name;
531 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
532 for (i = 0; i < atomNr; ++i) {
533 *atomNames[i].atom = atoms[i];
536 #else
537 for (i = 0; i < atomNr; i++) {
538 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
540 #endif
542 data = wmalloc(sizeof(NetData));
543 data->scr = scr;
544 data->strut = NULL;
545 data->show_desktop = NULL;
547 scr->netdata = data;
549 setSupportedHints(scr);
551 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
552 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
553 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
554 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
555 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
556 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
557 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
559 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
560 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
561 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
562 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
564 updateClientList(scr);
565 updateClientListStacking(scr, NULL);
566 updateWorkspaceCount(scr);
567 updateWorkspaceNames(scr);
568 updateShowDesktop(scr, False);
570 wScreenUpdateUsableArea(scr);
573 void wNETWMCleanup(WScreen * scr)
575 int i;
577 for (i = 0; i < atomNr; i++)
578 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
581 void wNETWMUpdateActions(WWindow * wwin, Bool del)
583 Atom action[10]; /* nr of actions atoms defined */
584 int i = 0;
586 if (del) {
587 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
588 return;
591 if (IS_MOVABLE(wwin))
592 action[i++] = net_wm_action_move;
594 if (IS_RESIZABLE(wwin))
595 action[i++] = net_wm_action_resize;
597 if (!WFLAGP(wwin, no_miniaturizable))
598 action[i++] = net_wm_action_minimize;
600 if (!WFLAGP(wwin, no_shadeable))
601 action[i++] = net_wm_action_shade;
603 /* if (!WFLAGP(wwin, no_stickable)) */
604 action[i++] = net_wm_action_stick;
606 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
607 if (IS_RESIZABLE(wwin))
608 action[i++] = net_wm_action_maximize_horz;
610 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
611 if (IS_RESIZABLE(wwin))
612 action[i++] = net_wm_action_maximize_vert;
614 /* if (!WFLAGP(wwin, no_fullscreen)) */
615 action[i++] = net_wm_action_fullscreen;
617 /* if (!WFLAGP(wwin, no_change_desktop)) */
618 action[i++] = net_wm_action_change_desktop;
620 if (!WFLAGP(wwin, no_closable))
621 action[i++] = net_wm_action_close;
623 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
624 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
627 void wNETWMUpdateWorkarea(WScreen * scr, WArea usableArea)
629 long *area;
630 int count, i;
632 /* XXX: not Xinerama compatible,
633 xinerama gets the largest available */
635 if (!scr->netdata || scr->workspace_count == 0)
636 return;
638 count = scr->workspace_count * 4;
639 area = wmalloc(sizeof(long) * count);
640 for (i = 0; i < scr->workspace_count; i++) {
641 area[4 * i + 0] = usableArea.x1;
642 area[4 * i + 1] = usableArea.y1;
643 area[4 * i + 2] = usableArea.x2 - usableArea.x1;
644 area[4 * i + 3] = usableArea.y2 - usableArea.y1;
647 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
648 PropModeReplace, (unsigned char *)area, count);
650 wfree(area);
653 Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
655 WReservedArea *cur;
656 WMRect rect;
658 if (!scr->netdata || !scr->netdata->strut)
659 return False;
661 area->x1 = area->y1 = area->x2 = area->y2 = 0;
663 for (cur = scr->netdata->strut; cur; cur = cur->next) {
664 WWindow *wwin = wWindowFor(cur->window);
665 if (wWindowTouchesHead(wwin, head)) {
666 if (cur->area.x1 > area->x1)
667 area->x1 = cur->area.x1;
668 if (cur->area.y1 > area->y1)
669 area->y1 = cur->area.y1;
670 if (cur->area.x2 > area->x2)
671 area->x2 = cur->area.x2;
672 if (cur->area.y2 > area->y2)
673 area->y2 = cur->area.y2;
677 if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
678 return False;
680 rect = wGetRectForHead(scr, head);
682 area->x1 = rect.pos.x + area->x1;
683 area->x2 = rect.pos.x + rect.size.width - area->x2;
684 area->y1 = rect.pos.y + area->y1;
685 area->y2 = rect.pos.y + rect.size.height - area->y2;
687 return True;
690 static void updateClientList(WScreen * scr)
692 WWindow *wwin;
693 Window *windows;
694 int count;
696 windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
698 count = 0;
699 wwin = scr->focused_window;
700 while (wwin) {
701 windows[count++] = wwin->client_win;
702 wwin = wwin->prev;
704 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
705 PropModeReplace, (unsigned char *)windows, count);
707 wfree(windows);
708 XFlush(dpy);
711 static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
713 WWindow *wwin;
714 Window *client_list;
715 Window *client_list_reverse;
716 int client_count;
717 WCoreWindow *tmp;
718 WMBagIterator iter;
719 int i;
721 /* update client list */
722 i = scr->window_count + 1;
723 client_list = (Window *) wmalloc(sizeof(Window) * i);
724 client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
726 client_count = 0;
727 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
728 while (tmp) {
729 wwin = wWindowFor(tmp->window);
730 /* wwin_excl is a window to exclude from the list
731 (e.g. it's now unmanaged) */
732 if (wwin && (wwin != wwin_excl))
733 client_list[client_count++] = wwin->client_win;
734 tmp = tmp->stacking->under;
738 for (i = 0; i < client_count; i++) {
739 Window w = client_list[client_count - i - 1];
740 client_list_reverse[i] = w;
743 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
744 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
746 wfree(client_list);
747 wfree(client_list_reverse);
749 XFlush(dpy);
752 static void updateWorkspaceCount(WScreen * scr)
753 { /* changeable */
754 long count;
756 count = scr->workspace_count;
758 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
759 32, PropModeReplace, (unsigned char *)&count, 1);
762 static void updateCurrentWorkspace(WScreen * scr)
763 { /* changeable */
764 long count;
766 count = scr->current_workspace;
768 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
769 PropModeReplace, (unsigned char *)&count, 1);
772 static void updateWorkspaceNames(WScreen * scr)
774 char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
775 unsigned int i, len, curr_size;
777 pos = buf;
778 len = 0;
779 for (i = 0; i < scr->workspace_count; i++) {
780 curr_size = strlen(scr->workspaces[i]->name);
781 strcpy(pos, scr->workspaces[i]->name);
782 pos += (curr_size + 1);
783 len += (curr_size + 1);
786 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
787 PropModeReplace, (unsigned char *)buf, len);
790 static void updateFocusHint(WScreen * scr, WWindow * wwin)
791 { /* changeable */
792 Window window;
794 if (!scr->focused_window || !scr->focused_window->flags.focused)
795 window = None;
796 else
797 window = scr->focused_window->client_win;
799 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
800 PropModeReplace, (unsigned char *)&window, 1);
803 static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
805 long l;
807 if (del) {
808 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
809 } else {
810 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
811 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
812 32, PropModeReplace, (unsigned char *)&l, 1);
816 static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
817 { /* changeable */
818 if (del) {
819 if (!wwin->flags.net_state_from_client) {
820 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
822 } else {
823 Atom state[15]; /* nr of defined state atoms */
824 int i = 0;
826 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
827 updateWorkspaceHint(wwin, False, False);
829 if (IS_OMNIPRESENT(wwin))
830 state[i++] = net_wm_state_sticky;
831 if (wwin->flags.shaded)
832 state[i++] = net_wm_state_shaded;
833 if (wwin->flags.maximized & MAX_HORIZONTAL)
834 state[i++] = net_wm_state_maximized_horz;
835 if (wwin->flags.maximized & MAX_VERTICAL)
836 state[i++] = net_wm_state_maximized_vert;
837 if (WFLAGP(wwin, skip_window_list))
838 state[i++] = net_wm_state_skip_taskbar;
839 if (wwin->flags.net_skip_pager)
840 state[i++] = net_wm_state_skip_pager;
841 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
842 state[i++] = net_wm_state_hidden;
843 state[i++] = net_wm_state_skip_pager;
845 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
846 if (!IS_OMNIPRESENT(wwin))
847 updateWorkspaceHint(wwin, True, False);
848 state[i++] = net_wm_state_sticky;
851 if (WFLAGP(wwin, sunken))
852 state[i++] = net_wm_state_below;
853 if (WFLAGP(wwin, floating))
854 state[i++] = net_wm_state_above;
855 if (wwin->flags.fullscreen)
856 state[i++] = net_wm_state_fullscreen;
858 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
859 PropModeReplace, (unsigned char *)state, i);
863 static Bool updateStrut(WWindow * wwin, Bool adding)
865 WScreen *scr;
866 WReservedArea *area;
867 Bool hasState = False;
869 scr = wwin->screen_ptr;
871 if (adding) {
872 Atom type_ret;
873 int fmt_ret;
874 unsigned long nitems_ret;
875 unsigned long bytes_after_ret;
876 long *data = NULL;
878 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
879 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
880 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
882 area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
883 area->area.x1 = data[0];
884 area->area.x2 = data[1];
885 area->area.y1 = data[2];
886 area->area.y2 = data[3];
888 area->window = wwin->client_win;
890 area->next = scr->netdata->strut;
891 scr->netdata->strut = area;
893 XFree(data);
894 hasState = True;
897 } else {
898 /* deleting */
899 area = scr->netdata->strut;
901 if (area) {
902 if (area->window == wwin->client_win) {
903 scr->netdata->strut = area->next;
904 wfree(area);
905 hasState = True;
906 } else {
907 while (area->next && area->next->window != wwin->client_win)
908 area = area->next;
910 if (area->next) {
911 WReservedArea *next;
913 next = area->next->next;
914 wfree(area->next);
915 area->next = next;
917 hasState = True;
923 return hasState;
926 static int getWindowLayer(WWindow * wwin)
928 int layer = WMNormalLevel;
930 if (wwin->type == net_wm_window_type_desktop) {
931 layer = WMDesktopLevel;
932 } else if (wwin->type == net_wm_window_type_dock) {
933 layer = WMDockLevel;
934 } else if (wwin->type == net_wm_window_type_toolbar) {
935 layer = WMMainMenuLevel;
936 } else if (wwin->type == net_wm_window_type_menu) {
937 layer = WMSubmenuLevel;
938 } else if (wwin->type == net_wm_window_type_utility) {
939 } else if (wwin->type == net_wm_window_type_splash) {
940 } else if (wwin->type == net_wm_window_type_dialog) {
941 if (wwin->transient_for) {
942 WWindow *parent = wWindowFor(wwin->transient_for);
943 if (parent && parent->flags.fullscreen) {
944 layer = WMFullscreenLevel;
947 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
948 } else if (wwin->type == net_wm_window_type_normal) {
951 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
952 layer = WMSunkenLevel;
953 if (wwin->client_flags.floating && WMFloatingLevel > layer)
954 layer = WMFloatingLevel;
956 return layer;
959 static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
962 if (state == net_wm_state_sticky) {
963 if (set == _NET_WM_STATE_TOGGLE) {
964 set = !IS_OMNIPRESENT(wwin);
966 if (set != wwin->flags.omnipresent) {
967 wWindowSetOmnipresent(wwin, set);
969 } else if (state == net_wm_state_shaded) {
970 if (set == _NET_WM_STATE_TOGGLE) {
971 set = !wwin->flags.shaded;
973 if (init) {
974 wwin->flags.shaded = set;
975 } else {
976 if (set) {
977 wShadeWindow(wwin);
978 } else {
979 wUnshadeWindow(wwin);
982 } else if (state == net_wm_state_skip_taskbar) {
983 if (set == _NET_WM_STATE_TOGGLE) {
984 set = !wwin->client_flags.skip_window_list;
986 wwin->client_flags.skip_window_list = set;
987 } else if (state == net_wm_state_skip_pager) {
988 if (set == _NET_WM_STATE_TOGGLE) {
989 set = !wwin->flags.net_skip_pager;
991 wwin->flags.net_skip_pager = set;
992 } else if (state == net_wm_state_maximized_vert) {
993 if (set == _NET_WM_STATE_TOGGLE) {
994 set = !(wwin->flags.maximized & MAX_VERTICAL);
996 if (init) {
997 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
998 } else {
999 if (set) {
1000 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1001 } else {
1002 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1005 } else if (state == net_wm_state_maximized_horz) {
1006 if (set == _NET_WM_STATE_TOGGLE) {
1007 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1009 if (init) {
1010 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1011 } else {
1012 if (set) {
1013 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1014 } else {
1015 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1018 } else if (state == net_wm_state_hidden) {
1019 if (set == _NET_WM_STATE_TOGGLE) {
1020 set = !(wwin->flags.miniaturized);
1022 if (init) {
1023 wwin->flags.miniaturized = set;
1024 } else {
1025 if (set) {
1026 wIconifyWindow(wwin);
1027 } else {
1028 wDeiconifyWindow(wwin);
1031 } else if (state == net_wm_state_fullscreen) {
1032 if (set == _NET_WM_STATE_TOGGLE) {
1033 set = !(wwin->flags.fullscreen);
1035 if (init) {
1036 wwin->flags.fullscreen = set;
1037 } else {
1038 if (set) {
1039 wFullscreenWindow(wwin);
1040 } else {
1041 wUnfullscreenWindow(wwin);
1044 } else if (state == net_wm_state_above) {
1045 if (set == _NET_WM_STATE_TOGGLE) {
1046 set = !(wwin->client_flags.floating);
1048 if (init) {
1049 wwin->client_flags.floating = set;
1050 } else {
1051 wwin->client_flags.floating = set;
1052 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1055 } else if (state == net_wm_state_below) {
1056 if (set == _NET_WM_STATE_TOGGLE) {
1057 set = !(wwin->client_flags.sunken);
1059 if (init) {
1060 wwin->client_flags.sunken = set;
1061 } else {
1062 wwin->client_flags.sunken = set;
1063 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1066 } else {
1067 #ifdef DEBUG_WMSPEC
1068 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1069 #endif
1073 static void removeIcon(WWindow * wwin)
1075 if (wwin->icon == NULL)
1076 return;
1077 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1078 XUnmapWindow(dpy, wwin->icon->core->window);
1079 RemoveFromStackList(wwin->icon->core);
1080 wIconDestroy(wwin->icon);
1081 wwin->icon = NULL;
1085 static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
1087 Bool ret = True;
1089 if (type == net_wm_window_type_desktop) {
1090 wwin->client_flags.no_titlebar = 1;
1091 wwin->client_flags.no_resizable = 1;
1092 wwin->client_flags.no_miniaturizable = 1;
1093 wwin->client_flags.no_border = 1;
1094 wwin->client_flags.no_resizebar = 1;
1095 wwin->client_flags.no_shadeable = 1;
1096 wwin->client_flags.no_movable = 1;
1097 wwin->client_flags.omnipresent = 1;
1098 wwin->client_flags.skip_window_list = 1;
1099 wwin->client_flags.dont_move_off = 1;
1100 wwin->client_flags.no_appicon = 1;
1101 wwin->flags.net_skip_pager = 1;
1102 wwin->frame_x = 0;
1103 wwin->frame_y = 0;
1104 } else if (type == net_wm_window_type_dock) {
1105 wwin->client_flags.no_titlebar = 1;
1106 wwin->client_flags.no_resizable = 1;
1107 wwin->client_flags.no_miniaturizable = 1;
1108 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1109 wwin->client_flags.no_resizebar = 1;
1110 wwin->client_flags.no_shadeable = 1;
1111 wwin->client_flags.no_movable = 1;
1112 wwin->client_flags.omnipresent = 1;
1113 wwin->client_flags.skip_window_list = 1;
1114 wwin->client_flags.dont_move_off = 1;
1115 wwin->flags.net_skip_pager = 1;
1116 } else if (type == net_wm_window_type_toolbar) {
1117 wwin->client_flags.no_titlebar = 1;
1118 wwin->client_flags.no_resizable = 1;
1119 wwin->client_flags.no_miniaturizable = 1;
1120 wwin->client_flags.no_resizebar = 1;
1121 wwin->client_flags.no_shadeable = 1;
1122 wwin->client_flags.skip_window_list = 1;
1123 wwin->client_flags.dont_move_off = 1;
1124 wwin->client_flags.no_appicon = 1;
1125 } else if (type == net_wm_window_type_menu) {
1126 wwin->client_flags.no_titlebar = 1;
1127 wwin->client_flags.no_resizable = 1;
1128 wwin->client_flags.no_miniaturizable = 1;
1129 wwin->client_flags.no_resizebar = 1;
1130 wwin->client_flags.no_shadeable = 1;
1131 wwin->client_flags.skip_window_list = 1;
1132 wwin->client_flags.dont_move_off = 1;
1133 wwin->client_flags.no_appicon = 1;
1134 } else if (type == net_wm_window_type_utility) {
1135 wwin->client_flags.no_appicon = 1;
1136 } else if (type == net_wm_window_type_splash) {
1137 wwin->client_flags.no_titlebar = 1;
1138 wwin->client_flags.no_resizable = 1;
1139 wwin->client_flags.no_miniaturizable = 1;
1140 wwin->client_flags.no_resizebar = 1;
1141 wwin->client_flags.no_shadeable = 1;
1142 wwin->client_flags.no_movable = 1;
1143 wwin->client_flags.skip_window_list = 1;
1144 wwin->client_flags.dont_move_off = 1;
1145 wwin->client_flags.no_appicon = 1;
1146 wwin->flags.net_skip_pager = 1;
1147 } else if (type == net_wm_window_type_dialog) {
1148 /* These also seem a bad idea in our context -Dan
1149 // wwin->client_flags.skip_window_list = 1;
1150 // wwin->client_flags.no_appicon = 1;
1152 } else if (type == net_wm_window_type_normal) {
1153 } else {
1154 ret = False;
1157 wwin->type = type;
1158 *layer = getWindowLayer(wwin);
1160 return ret;
1163 void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
1165 if (wwin->type == net_wm_window_type_splash) {
1166 WScreen *scr = wwin->screen_ptr;
1167 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1168 *x = rect.pos.x + (rect.size.width - width) / 2;
1169 *y = rect.pos.y + (rect.size.height - height) / 2;
1173 static void updateWindowType(WWindow * wwin)
1175 Atom type_ret;
1176 int fmt_ret;
1177 unsigned long nitems_ret;
1178 unsigned long bytes_after_ret;
1179 long *data = NULL;
1180 int layer;
1181 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1182 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1183 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1185 int i;
1186 Atom *type = (Atom *) data;
1187 for (i = 0; i < nitems_ret; ++i) {
1188 if (handleWindowType(wwin, type[i], &layer))
1189 break;
1191 XFree(data);
1194 if (wwin->frame != NULL) {
1195 ChangeStackingLevel(wwin->frame->core, layer);
1196 wwin->frame->flags.need_texture_change = 1;
1197 wWindowConfigureBorders(wwin);
1198 wFrameWindowPaint(wwin->frame);
1199 wNETWMUpdateActions(wwin, False);
1203 Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
1205 Atom type_ret;
1206 int fmt_ret;
1207 unsigned long nitems_ret;
1208 unsigned long bytes_after_ret;
1209 long *data = NULL;
1210 Bool hasState = False;
1211 int i;
1213 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1214 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1215 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1217 long desktop = *data;
1218 XFree(data);
1220 if (desktop == -1)
1221 wwin->client_flags.omnipresent = 1;
1222 else
1223 *workspace = desktop;
1225 hasState = True;
1228 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1229 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1230 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1232 Atom *state = (Atom *) data;
1233 for (i = 0; i < nitems_ret; ++i) {
1234 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1236 XFree(data);
1237 hasState = True;
1240 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1241 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1242 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1244 Atom *type = (Atom *) data;
1245 for (i = 0; i < nitems_ret; ++i) {
1246 if (handleWindowType(wwin, type[i], layer))
1247 break;
1249 XFree(data);
1250 hasState = True;
1253 wNETWMUpdateActions(wwin, False);
1254 updateStrut(wwin, False);
1255 if (updateStrut(wwin, True)) {
1256 hasState = True;
1258 wScreenUpdateUsableArea(wwin->screen_ptr);
1260 return hasState;
1263 static Bool updateNetIconInfo(WWindow * wwin)
1266 Atom type_ret;
1267 int fmt_ret;
1268 unsigned long nitems_ret;
1269 unsigned long bytes_after_ret;
1270 long *data = NULL;
1271 Bool hasState = False;
1272 Bool old_state = wwin->flags.net_handle_icon;
1274 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1275 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1276 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1277 long handled = *data;
1278 wwin->flags.net_handle_icon = (handled != 0);
1279 XFree(data);
1280 hasState = True;
1282 } else
1283 wwin->flags.net_handle_icon = False;
1285 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1286 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1287 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1289 #ifdef NETWM_PROPER
1290 if (wwin->flags.net_handle_icon)
1291 #else
1292 wwin->flags.net_handle_icon = True;
1293 #endif
1295 wwin->icon_x = data[0];
1296 wwin->icon_y = data[1];
1297 wwin->icon_w = data[2];
1298 wwin->icon_h = data[3];
1301 XFree(data);
1302 hasState = True;
1304 } else
1305 wwin->flags.net_handle_icon = False;
1307 if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1308 if (wwin->flags.net_handle_icon) {
1309 removeIcon(wwin);
1310 } else {
1311 wwin->flags.miniaturized = False;
1312 wwin->flags.skip_next_animation = True;
1313 wIconifyWindow(wwin);
1317 return hasState;
1320 Bool wNETWMCheckInitialClientState(WWindow * wwin)
1322 Bool hasState = False;
1324 #ifdef DEBUG_WMSPEC
1325 printf("CheckInitialClientState\n");
1326 #endif
1328 wNETWMShowingDesktop(wwin->screen_ptr, False);
1330 hasState |= updateNetIconInfo(wwin);
1332 updateIconImage(wwin->screen_ptr, wwin);
1334 return hasState;
1337 static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
1339 unsigned long nitems_ret, bytes_after_ret;
1340 char *data, *names[32];
1341 int fmt_ret, i, n;
1342 Atom type_ret;
1344 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1345 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1346 &bytes_after_ret, (unsigned char **)&data) != Success) {
1347 return;
1350 if (data == NULL)
1351 return;
1353 if (type_ret != utf8_string || fmt_ret != 8)
1354 return;
1356 n = 0;
1357 names[n] = data;
1358 for (i = 0; i < nitems_ret; i++) {
1359 if (data[i] == 0) {
1360 n++;
1361 names[n] = &data[i];
1362 } else if (*names[n] == 0) {
1363 names[n] = &data[i];
1364 wWorkspaceRename(scr, n, names[n]);
1369 Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
1371 WScreen *scr;
1372 WWindow *wwin;
1373 Bool done = True;
1375 #ifdef DEBUG_WMSPEC
1376 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1377 #endif
1379 scr = wScreenForWindow(event->window);
1380 if (scr) {
1381 /* generic client messages */
1382 if (event->message_type == net_current_desktop) {
1383 wWorkspaceChange(scr, event->data.l[0]);
1384 } else if (event->message_type == net_number_of_desktops) {
1385 long value;
1387 value = event->data.l[0];
1388 if (value > scr->workspace_count) {
1389 wWorkspaceMake(scr, value - scr->workspace_count);
1390 } else if (value < scr->workspace_count) {
1391 int i;
1392 Bool rebuild = False;
1394 for (i = scr->workspace_count - 1; i >= value; i--) {
1395 if (!wWorkspaceDelete(scr, i)) {
1396 rebuild = True;
1397 break;
1401 if (rebuild) {
1402 updateWorkspaceCount(scr);
1405 } else if (event->message_type == net_showing_desktop) {
1406 wNETWMShowingDesktop(scr, event->data.l[0]);
1407 } else if (event->message_type == net_desktop_names) {
1408 handleDesktopNames(event, scr);
1409 } else {
1410 done = False;
1413 if (done)
1414 return True;
1417 /* window specific client messages */
1419 wwin = wWindowFor(event->window);
1420 if (!wwin)
1421 return False;
1423 if (event->message_type == net_active_window) {
1425 * Firefox sends aditional 'net_active_window' signals on startup if
1426 * multiple tabs are open. That causes unnecessary workspace switching if
1427 * those signals come from other workspaces. Therefore we also check if
1428 * we should ignore these spurious focus across workspaces requests (but
1429 * allow the switching if it comes from a pager).
1431 if (wwin->frame->workspace == wwin->screen_ptr->current_workspace
1432 || !WFLAGP(wwin, dont_focus_across_wksp) || event->data.l[0] == 2) {
1433 wNETWMShowingDesktop(scr, False);
1434 wMakeWindowVisible(wwin);
1436 } else if (event->message_type == net_close_window) {
1437 if (!WFLAGP(wwin, no_closable)) {
1438 if (wwin->protocols.DELETE_WINDOW)
1439 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1441 } else if (event->message_type == net_wm_state) {
1442 int maximized = wwin->flags.maximized;
1443 long set = event->data.l[0];
1445 #ifdef DEBUG_WMSPEC
1446 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1447 XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1448 #endif
1450 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1451 if (event->data.l[2])
1452 doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1454 if (wwin->flags.maximized != maximized) {
1455 if (!wwin->flags.maximized) {
1456 wwin->flags.maximized = maximized;
1457 wUnmaximizeWindow(wwin);
1458 } else {
1459 wMaximizeWindow(wwin, wwin->flags.maximized);
1462 updateStateHint(wwin, False, False);
1463 } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1464 updateNetIconInfo(wwin);
1465 } else if (event->message_type == net_wm_desktop) {
1466 long desktop = event->data.l[0];
1467 if (desktop == -1) {
1468 wWindowSetOmnipresent(wwin, True);
1469 } else {
1470 if (IS_OMNIPRESENT(wwin))
1471 wWindowSetOmnipresent(wwin, False);
1472 wWindowChangeWorkspace(wwin, desktop);
1474 } else if (event->message_type == net_wm_icon) {
1475 updateIconImage(scr, wwin);
1476 } else {
1477 done = False;
1480 return done;
1483 Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
1485 Bool ret = True;
1487 if (event->atom == net_wm_strut) {
1488 updateStrut(wwin, False);
1489 updateStrut(wwin, True);
1490 wScreenUpdateUsableArea(wwin->screen_ptr);
1491 } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1492 updateNetIconInfo(wwin);
1493 } else if (event->atom == net_wm_window_type) {
1494 updateWindowType(wwin);
1495 } else if (event->atom == net_wm_name) {
1496 char *name = wNETWMGetWindowName(wwin->client_win);
1497 wWindowUpdateName(wwin, name);
1498 if (name)
1499 wfree(name);
1500 } else if (event->atom == net_wm_icon_name) {
1501 if (wwin->icon) {
1502 char *name = wNETWMGetIconName(wwin->client_win);
1503 wIconChangeTitle(wwin->icon, name);
1505 } else {
1506 ret = False;
1509 return ret;
1512 int wNETWMGetPidForWindow(Window window)
1514 Atom type_ret;
1515 int fmt_ret;
1516 unsigned long nitems_ret;
1517 unsigned long bytes_after_ret;
1518 long *data = NULL;
1519 int pid;
1521 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1522 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1523 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1525 pid = *data;
1526 XFree(data);
1527 } else {
1528 pid = 0;
1531 return pid;
1534 char *wNETWMGetWindowName(Window window)
1536 char *name;
1537 char *ret;
1538 int size;
1540 name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1541 if (name) {
1542 ret = wstrndup(name, size);
1543 XFree(name);
1544 } else
1545 ret = NULL;
1546 return ret;
1549 char *wNETWMGetIconName(Window window)
1551 char *name;
1552 char *ret;
1553 int size;
1555 name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1556 if (name) {
1557 ret = wstrndup(name, size);
1558 XFree(name);
1559 } else
1560 ret = NULL;
1561 return ret;
1564 static void observer(void *self, WMNotification * notif)
1566 WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1567 const char *name = WMGetNotificationName(notif);
1568 void *data = WMGetNotificationClientData(notif);
1569 NetData *ndata = (NetData *) self;
1571 if (strcmp(name, WMNManaged) == 0 && wwin) {
1572 updateClientList(wwin->screen_ptr);
1573 updateClientListStacking(wwin->screen_ptr, NULL);
1574 updateStateHint(wwin, True, False);
1576 updateStrut(wwin, False);
1577 updateStrut(wwin, True);
1578 wScreenUpdateUsableArea(wwin->screen_ptr);
1579 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1580 updateClientList(wwin->screen_ptr);
1581 updateClientListStacking(wwin->screen_ptr, wwin);
1582 updateWorkspaceHint(wwin, False, True);
1583 updateStateHint(wwin, False, True);
1584 wNETWMUpdateActions(wwin, True);
1586 updateStrut(wwin, False);
1587 wScreenUpdateUsableArea(wwin->screen_ptr);
1588 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1589 updateClientListStacking(wwin->screen_ptr, NULL);
1590 updateStateHint(wwin, False, False);
1591 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1592 updateClientListStacking(wwin->screen_ptr, NULL);
1593 updateStateHint(wwin, False, False);
1594 } else if (strcmp(name, WMNChangedFocus) == 0) {
1595 updateFocusHint(ndata->scr, wwin);
1596 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1597 updateWorkspaceHint(wwin, False, False);
1598 updateStateHint(wwin, True, False);
1599 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1600 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1604 static void wsobserver(void *self, WMNotification * notif)
1606 WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1607 const char *name = WMGetNotificationName(notif);
1609 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1610 updateWorkspaceCount(scr);
1611 updateWorkspaceNames(scr);
1612 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1613 updateWorkspaceCount(scr);
1614 updateWorkspaceNames(scr);
1615 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1616 updateCurrentWorkspace(scr);
1617 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1618 updateWorkspaceNames(scr);