Change to the linux kernel coding style
[wmaker-crm.git] / src / wmspec.c
1 /*  wmspec.c-- support for the wm-spec Hints
2  *
3  *  Window Maker window manager
4  *
5  *  Copyright (c) 1998-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20  *  USA.
21  */
22
23 /*
24  * TODO
25  * ----
26  *
27  * This file needs to be checked for all calls to XGetWindowProperty() and
28  * proper checks need to be made on the returned values. Only checking for
29  * return to be Success is not enough. -Dan
30  */
31
32 #include "wconfig.h"
33
34 #ifdef NETWM_HINTS
35
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/Xarch.h>
39 #include <string.h>
40
41 #include "WindowMaker.h"
42 #include "window.h"
43 #include "screen.h"
44 #include "workspace.h"
45 #include "framewin.h"
46 #include "actions.h"
47 #include "client.h"
48 #include "wmspec.h"
49 #include "icon.h"
50 #include "stacking.h"
51 #include "xinerama.h"
52 #include "properties.h"
53
54 #ifdef DEBUG_WMSPEC
55 #include <stdio.h>
56 #endif
57
58 /* Global variables */
59 extern Atom _XA_WM_DELETE_WINDOW;
60 extern Time LastTimestamp;
61 extern WPreferences wPreferences;
62
63 /* Root Window Properties */
64 static Atom net_supported;
65 static Atom net_client_list;
66 static Atom net_client_list_stacking;
67 static Atom net_number_of_desktops;
68 static Atom net_desktop_geometry;
69 static Atom net_desktop_viewport;
70 static Atom net_current_desktop;
71 static Atom net_desktop_names;
72 static Atom net_active_window;
73 static Atom net_workarea;       /* XXX: not xinerama compatible */
74 static Atom net_supporting_wm_check;
75 static Atom net_virtual_roots;  /* N/A */
76 static Atom net_desktop_layout; /* XXX */
77 static Atom net_showing_desktop;
78
79 /* Other Root Window Messages */
80 static Atom net_close_window;
81 static Atom net_moveresize_window;      /* TODO */
82 static Atom net_wm_moveresize;  /* TODO */
83
84 /* Application Window Properties */
85 static Atom net_wm_name;
86 static Atom net_wm_visible_name;        /* TODO (unnecessary?) */
87 static Atom net_wm_icon_name;
88 static Atom net_wm_visible_icon_name;   /* TODO (unnecessary?) */
89 static Atom net_wm_desktop;
90 static Atom net_wm_window_type;
91 static Atom net_wm_window_type_desktop;
92 static Atom net_wm_window_type_dock;
93 static Atom net_wm_window_type_toolbar;
94 static Atom net_wm_window_type_menu;
95 static Atom net_wm_window_type_utility;
96 static Atom net_wm_window_type_splash;
97 static Atom net_wm_window_type_dialog;
98 static Atom net_wm_window_type_normal;
99 static Atom net_wm_state;
100 static Atom net_wm_state_modal; /* XXX: what is this?!? */
101 static Atom net_wm_state_sticky;
102 static Atom net_wm_state_maximized_vert;
103 static Atom net_wm_state_maximized_horz;
104 static Atom net_wm_state_shaded;
105 static Atom net_wm_state_skip_taskbar;
106 static Atom net_wm_state_skip_pager;
107 static Atom net_wm_state_hidden;
108 static Atom net_wm_state_fullscreen;
109 static Atom net_wm_state_above;
110 static Atom net_wm_state_below;
111 static Atom net_wm_allowed_actions;
112 static Atom net_wm_action_move;
113 static Atom net_wm_action_resize;
114 static Atom net_wm_action_minimize;
115 static Atom net_wm_action_shade;
116 static Atom net_wm_action_stick;
117 static Atom net_wm_action_maximize_horz;
118 static Atom net_wm_action_maximize_vert;
119 static Atom net_wm_action_fullscreen;
120 static Atom net_wm_action_change_desktop;
121 static Atom net_wm_action_close;
122 static Atom net_wm_strut;       /* XXX: see net_workarea */
123 static Atom net_wm_strut_partial;       /* TODO: doesn't really fit into the current strut scheme */
124 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. */
125 static Atom net_wm_icon;
126 static Atom net_wm_pid;         /* TODO */
127 static Atom net_wm_handled_icons;       /* FIXME: see net_wm_icon_geometry */
128
129 /* Window Manager Protocols */
130 static Atom net_wm_ping;        /* TODO */
131
132 static Atom utf8_string;
133
134 typedef struct {
135         char *name;
136         Atom *atom;
137 } atomitem_t;
138
139 static atomitem_t atomNames[] = {
140         {"_NET_SUPPORTED", &net_supported},
141         {"_NET_CLIENT_LIST", &net_client_list},
142         {"_NET_CLIENT_LIST_STACKING", &net_client_list_stacking},
143         {"_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops},
144         {"_NET_DESKTOP_GEOMETRY", &net_desktop_geometry},
145         {"_NET_DESKTOP_VIEWPORT", &net_desktop_viewport},
146         {"_NET_CURRENT_DESKTOP", &net_current_desktop},
147         {"_NET_DESKTOP_NAMES", &net_desktop_names},
148         {"_NET_ACTIVE_WINDOW", &net_active_window},
149         {"_NET_WORKAREA", &net_workarea},
150         {"_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check},
151         {"_NET_VIRTUAL_ROOTS", &net_virtual_roots},
152         {"_NET_DESKTOP_LAYOUT", &net_desktop_layout},
153         {"_NET_SHOWING_DESKTOP", &net_showing_desktop},
154
155         {"_NET_CLOSE_WINDOW", &net_close_window},
156         {"_NET_MOVERESIZE_WINDOW", &net_moveresize_window},
157         {"_NET_WM_MOVERESIZE", &net_wm_moveresize},
158
159         {"_NET_WM_NAME", &net_wm_name},
160         {"_NET_WM_VISIBLE_NAME", &net_wm_visible_name},
161         {"_NET_WM_ICON_NAME", &net_wm_icon_name},
162         {"_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name},
163         {"_NET_WM_DESKTOP", &net_wm_desktop},
164         {"_NET_WM_WINDOW_TYPE", &net_wm_window_type},
165         {"_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop},
166         {"_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock},
167         {"_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar},
168         {"_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu},
169         {"_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility},
170         {"_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash},
171         {"_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog},
172         {"_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal},
173         {"_NET_WM_STATE", &net_wm_state},
174         {"_NET_WM_STATE_MODAL", &net_wm_state_modal},
175         {"_NET_WM_STATE_STICKY", &net_wm_state_sticky},
176         {"_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert},
177         {"_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz},
178         {"_NET_WM_STATE_SHADED", &net_wm_state_shaded},
179         {"_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar},
180         {"_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager},
181         {"_NET_WM_STATE_HIDDEN", &net_wm_state_hidden},
182         {"_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen},
183         {"_NET_WM_STATE_ABOVE", &net_wm_state_above},
184         {"_NET_WM_STATE_BELOW", &net_wm_state_below},
185         {"_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions},
186         {"_NET_WM_ACTION_MOVE", &net_wm_action_move},
187         {"_NET_WM_ACTION_RESIZE", &net_wm_action_resize},
188         {"_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize},
189         {"_NET_WM_ACTION_SHADE", &net_wm_action_shade},
190         {"_NET_WM_ACTION_STICK", &net_wm_action_stick},
191         {"_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz},
192         {"_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert},
193         {"_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen},
194         {"_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop},
195         {"_NET_WM_ACTION_CLOSE", &net_wm_action_close},
196         {"_NET_WM_STRUT", &net_wm_strut},
197         {"_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial},
198         {"_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry},
199         {"_NET_WM_ICON", &net_wm_icon},
200         {"_NET_WM_PID", &net_wm_pid},
201         {"_NET_WM_HANDLED_ICONS", &net_wm_handled_icons},
202
203         {"_NET_WM_PING", &net_wm_ping},
204
205         {"UTF8_STRING", &utf8_string},
206 };
207
208 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
209
210 #define _NET_WM_STATE_REMOVE 0
211 #define _NET_WM_STATE_ADD 1
212 #define _NET_WM_STATE_TOGGLE 2
213
214 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
215 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
216 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
217 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
218 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
219 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
220 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
221 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
222 #define _NET_WM_MOVERESIZE_MOVE              8  /* movement only */
223 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9  /* size via keyboard */
224 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10  /* move via keyboard */
225
226 static void observer(void *self, WMNotification * notif);
227 static void wsobserver(void *self, WMNotification * notif);
228
229 static void updateClientList(WScreen * scr);
230 static void updateClientListStacking(WScreen * scr, WWindow *);
231
232 static void updateWorkspaceNames(WScreen * scr);
233 static void updateCurrentWorkspace(WScreen * scr);
234 static void updateWorkspaceCount(WScreen * scr);
235
236 typedef struct NetData {
237         WScreen *scr;
238         WReservedArea *strut;
239         WWindow **show_desktop;
240 } NetData;
241
242 static void setSupportedHints(WScreen * scr)
243 {
244         Atom atom[atomNr];
245         int i = 0;
246
247         /* set supported hints list */
248         /* XXX: extend this !!! */
249
250         atom[i++] = net_client_list;
251         atom[i++] = net_client_list_stacking;
252         atom[i++] = net_number_of_desktops;
253         atom[i++] = net_desktop_geometry;
254         atom[i++] = net_desktop_viewport;
255         atom[i++] = net_current_desktop;
256         atom[i++] = net_desktop_names;
257         atom[i++] = net_active_window;
258         atom[i++] = net_workarea;
259         atom[i++] = net_supporting_wm_check;
260         atom[i++] = net_showing_desktop;
261 #if 0
262         atom[i++] = net_wm_moveresize;
263 #endif
264         atom[i++] = net_wm_desktop;
265         atom[i++] = net_wm_window_type;
266         atom[i++] = net_wm_window_type_desktop;
267         atom[i++] = net_wm_window_type_dock;
268         atom[i++] = net_wm_window_type_toolbar;
269         atom[i++] = net_wm_window_type_menu;
270         atom[i++] = net_wm_window_type_utility;
271         atom[i++] = net_wm_window_type_splash;
272         atom[i++] = net_wm_window_type_dialog;
273         atom[i++] = net_wm_window_type_normal;
274
275         atom[i++] = net_wm_state;
276         /*    atom[i++] = net_wm_state_modal; *//* XXX: not sure where/when to use it. */
277         atom[i++] = net_wm_state_sticky;
278         atom[i++] = net_wm_state_shaded;
279         atom[i++] = net_wm_state_maximized_horz;
280         atom[i++] = net_wm_state_maximized_vert;
281         atom[i++] = net_wm_state_skip_taskbar;
282         atom[i++] = net_wm_state_skip_pager;
283         atom[i++] = net_wm_state_hidden;
284         atom[i++] = net_wm_state_fullscreen;
285         atom[i++] = net_wm_state_above;
286         atom[i++] = net_wm_state_below;
287
288         atom[i++] = net_wm_allowed_actions;
289         atom[i++] = net_wm_action_move;
290         atom[i++] = net_wm_action_resize;
291         atom[i++] = net_wm_action_minimize;
292         atom[i++] = net_wm_action_shade;
293         atom[i++] = net_wm_action_stick;
294         atom[i++] = net_wm_action_maximize_horz;
295         atom[i++] = net_wm_action_maximize_vert;
296         atom[i++] = net_wm_action_fullscreen;
297         atom[i++] = net_wm_action_change_desktop;
298         atom[i++] = net_wm_action_close;
299
300         atom[i++] = net_wm_strut;
301         atom[i++] = net_wm_icon_geometry;
302         atom[i++] = net_wm_icon;
303         atom[i++] = net_wm_handled_icons;
304
305         atom[i++] = net_wm_name;
306         atom[i++] = net_wm_icon_name;
307
308         XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32, PropModeReplace, (unsigned char *)atom, i);
309
310         /* set supporting wm hint */
311         XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
312                         PropModeReplace, (unsigned char *)&scr->info_window, 1);
313
314         XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
315                         32, PropModeReplace, (unsigned char *)&scr->info_window, 1);
316 }
317
318 void wNETWMUpdateDesktop(WScreen * scr)
319 {
320         long *views, sizes[2];
321         int count, i;
322
323         if (scr->workspace_count == 0)
324                 return;
325
326         count = scr->workspace_count * 2;
327         views = wmalloc(sizeof(long) * count);
328         /*memset(views, 0, sizeof(long) * count); */
329
330 #ifdef VIRTUAL_DESKTOP
331         sizes[0] = scr->workspaces[scr->current_workspace]->width;
332         sizes[1] = scr->workspaces[scr->current_workspace]->height;
333 #else
334         sizes[0] = scr->scr_width;
335         sizes[1] = scr->scr_height;
336 #endif
337
338         for (i = 0; i < scr->workspace_count; i++) {
339 #ifdef VIRTUAL_DESKTOP
340                 views[2 * i + 0] = scr->workspaces[i]->view_x;
341                 views[2 * i + 1] = scr->workspaces[i]->view_y;
342 #else
343                 views[2 * i + 0] = 0;
344                 views[2 * i + 1] = 0;
345 #endif
346         }
347
348         XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
349                         PropModeReplace, (unsigned char *)sizes, 2);
350
351         XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
352                         PropModeReplace, (unsigned char *)views, count);
353
354         wfree(views);
355 }
356
357 int wNETWMGetCurrentDesktopFromHint(WScreen * scr)
358 {
359         int count;
360         unsigned char *prop;
361
362         prop = PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL, 0, 1, &count);
363         if (prop) {
364                 int desktop = *(long *)prop;
365                 XFree(prop);
366                 return desktop;
367         }
368         return -1;
369 }
370
371 /*
372  * Find the best icon to be used by Window Maker for appicon/miniwindows.
373  * Currently the algorithm is to take the image with the size closest
374  * to icon_size x icon_size, but never bigger than that.
375  *
376  * This algorithm is very poorly implemented and needs to be redone (it can
377  * easily select images with very large widths and very small heights over
378  * square images, if the area of the former is closer to the desired one).
379  *
380  * The logic can also be changed to accept bigger images and scale them down.
381  */
382 static unsigned long *findBestIcon(unsigned long *data, unsigned long items)
383 {
384         int size, wanted, d;
385         unsigned long i, distance;
386         unsigned long *icon;
387
388         /* better use only 75% of icon_size. For 64x64 this means 48x48
389          * This leaves room around the icon for the miniwindow title and
390          * results in better overall aesthetics -Dan */
391         wanted = wPreferences.icon_size * wPreferences.icon_size;
392
393         for (icon = NULL, distance = LONG_MAX, i = 0L; i < items - 1;) {
394                 size = data[i] * data[i + 1];
395                 if (size == 0)
396                         break;
397                 d = wanted - size;
398                 if (d >= 0 && d <= distance && (i + size + 2) <= items) {
399                         distance = d;
400                         icon = &data[i];
401                 }
402                 i += size + 2;
403         }
404
405         return icon;
406 }
407
408 static RImage *makeRImageFromARGBData(unsigned long *data)
409 {
410         int size, width, height, i;
411         RImage *image;
412         unsigned char *imgdata;
413         unsigned long pixel;
414
415         width = data[0];
416         height = data[1];
417         size = width * height;
418
419         if (size == 0)
420                 return NULL;
421
422         image = RCreateImage(width, height, True);
423
424         for (imgdata = image->data, i = 2; i < size + 2; i++, imgdata += 4) {
425                 pixel = data[i];
426 #if BYTE_ORDER == BIG_ENDIAN
427                 imgdata[2] = (pixel >> 24) & 0xff;      /* A */
428                 imgdata[1] = (pixel >> 16) & 0xff;      /* R */
429                 imgdata[0] = (pixel >> 8) & 0xff;       /* G */
430                 imgdata[3] = (pixel >> 0) & 0xff;       /* B */
431 #else                           /* Little endian */
432                 imgdata[3] = (pixel >> 24) & 0xff;      /* A */
433                 imgdata[0] = (pixel >> 16) & 0xff;      /* R */
434                 imgdata[1] = (pixel >> 8) & 0xff;       /* G */
435                 imgdata[2] = (pixel >> 0) & 0xff;       /* B */
436 #endif                          /* endianness */
437
438         }
439
440         return image;
441 }
442
443 static void updateIconImage(WScreen * scr, WWindow * wwin)
444 {
445         unsigned long *property, *data;
446         unsigned long items, rest;
447         Atom type;
448         int format;
449         RImage *image;
450
451         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
452                                False, XA_CARDINAL, &type, &format, &items, &rest,
453                                (unsigned char **)&property) != Success || !property) {
454                 return;
455         }
456
457         if (type != XA_CARDINAL || format != 32 || items < 2) {
458                 XFree(property);
459                 return;
460         }
461
462         data = findBestIcon(property, items);
463         if (!data) {
464                 XFree(property);
465                 return;
466         }
467
468         image = makeRImageFromARGBData(data);
469
470         if (image) {
471                 if (wwin->net_icon_image)
472                         RReleaseImage(wwin->net_icon_image);
473                 wwin->net_icon_image = image;
474         }
475
476         XFree(property);
477 }
478
479 static void updateShowDesktop(WScreen * scr, Bool show)
480 {
481         long foo;
482
483         foo = (show == True);
484         XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
485                         PropModeReplace, (unsigned char *)&foo, 1);
486 }
487
488 void wNETWMShowingDesktop(WScreen * scr, Bool show)
489 {
490         if (show && scr->netdata->show_desktop == NULL) {
491                 WWindow *tmp, **wins;
492                 int i = 0;
493
494                 wins = (WWindow **) wmalloc(sizeof(WWindow *) * (scr->window_count + 1));
495
496                 tmp = scr->focused_window;
497                 while (tmp) {
498                         if (!tmp->flags.hidden && !tmp->flags.miniaturized && !WFLAGP(tmp, skip_window_list)) {
499
500                                 wins[i++] = tmp;
501                                 tmp->flags.skip_next_animation = 1;
502                                 tmp->flags.net_show_desktop = 1;
503                                 wIconifyWindow(tmp);
504                         }
505
506                         tmp = tmp->prev;
507                 }
508                 wins[i++] = NULL;
509
510                 scr->netdata->show_desktop = wins;
511                 updateShowDesktop(scr, True);
512         } else if (scr->netdata->show_desktop != NULL) {
513                 /* FIXME: get rid of workspace flashing ! */
514                 int ws = scr->current_workspace;
515                 WWindow **tmp;
516                 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
517                         wDeiconifyWindow(*tmp);
518                         (*tmp)->flags.net_show_desktop = 0;
519                 }
520                 if (ws != scr->current_workspace)
521                         wWorkspaceChange(scr, ws);
522                 wfree(scr->netdata->show_desktop);
523                 scr->netdata->show_desktop = NULL;
524                 updateShowDesktop(scr, False);
525         }
526 }
527
528 void wNETWMInitStuff(WScreen * scr)
529 {
530         NetData *data;
531         int i;
532
533 #ifdef DEBUG_WMSPEC
534         printf("wNETWMInitStuff\n");
535 #endif
536
537 #ifdef HAVE_XINTERNATOMS
538         {
539                 Atom atoms[atomNr];
540                 char *names[atomNr];
541
542                 for (i = 0; i < atomNr; ++i) {
543                         names[i] = atomNames[i].name;
544                 }
545                 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
546                 for (i = 0; i < atomNr; ++i) {
547                         *atomNames[i].atom = atoms[i];
548                 }
549         }
550 #else
551         for (i = 0; i < atomNr; i++) {
552                 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
553         }
554 #endif
555
556         data = wmalloc(sizeof(NetData));
557         data->scr = scr;
558         data->strut = NULL;
559         data->show_desktop = NULL;
560
561         scr->netdata = data;
562
563         setSupportedHints(scr);
564
565         WMAddNotificationObserver(observer, data, WMNManaged, NULL);
566         WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
567         WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
568         WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
569         WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
570         WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
571         WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
572
573         WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
574         WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
575         WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
576         WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
577
578         updateClientList(scr);
579         updateClientListStacking(scr, NULL);
580         updateWorkspaceCount(scr);
581         updateWorkspaceNames(scr);
582         updateShowDesktop(scr, False);
583
584         wScreenUpdateUsableArea(scr);
585 }
586
587 void wNETWMCleanup(WScreen * scr)
588 {
589         int i;
590
591         for (i = 0; i < atomNr; i++)
592                 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
593 }
594
595 void wNETWMUpdateActions(WWindow * wwin, Bool del)
596 {
597         Atom action[10];        /* nr of actions atoms defined */
598         int i = 0;
599
600         if (del) {
601                 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
602                 return;
603         }
604
605         if (IS_MOVABLE(wwin))
606                 action[i++] = net_wm_action_move;
607
608         if (IS_RESIZABLE(wwin))
609                 action[i++] = net_wm_action_resize;
610
611         if (!WFLAGP(wwin, no_miniaturizable))
612                 action[i++] = net_wm_action_minimize;
613
614         if (!WFLAGP(wwin, no_shadeable))
615                 action[i++] = net_wm_action_shade;
616
617         /*    if (!WFLAGP(wwin, no_stickable)) */
618         action[i++] = net_wm_action_stick;
619
620         /*    if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
621         if (IS_RESIZABLE(wwin))
622                 action[i++] = net_wm_action_maximize_horz;
623
624         /*    if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
625         if (IS_RESIZABLE(wwin))
626                 action[i++] = net_wm_action_maximize_vert;
627
628         /*    if (!WFLAGP(wwin, no_fullscreen)) */
629         action[i++] = net_wm_action_fullscreen;
630
631         /*    if (!WFLAGP(wwin, no_change_desktop)) */
632         action[i++] = net_wm_action_change_desktop;
633
634         if (!WFLAGP(wwin, no_closable))
635                 action[i++] = net_wm_action_close;
636
637         XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
638                         XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
639 }
640
641 void wNETWMUpdateWorkarea(WScreen * scr, WArea usableArea)
642 {
643         long *area;
644         int count, i;
645
646         /* XXX: not Xinerama compatible,
647            xinerama gets the largest available */
648
649         if (!scr->netdata || scr->workspace_count == 0)
650                 return;
651
652         count = scr->workspace_count * 4;
653         area = wmalloc(sizeof(long) * count);
654         for (i = 0; i < scr->workspace_count; i++) {
655                 area[4 * i + 0] = usableArea.x1;
656                 area[4 * i + 1] = usableArea.y1;
657                 area[4 * i + 2] = usableArea.x2 - usableArea.x1;
658                 area[4 * i + 3] = usableArea.y2 - usableArea.y1;
659         }
660
661         XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
662                         PropModeReplace, (unsigned char *)area, count);
663
664         wfree(area);
665 }
666
667 Bool wNETWMGetUsableArea(WScreen * scr, int head, WArea * area)
668 {
669         WReservedArea *cur;
670         WMRect rect;
671
672         if (!scr->netdata || !scr->netdata->strut)
673                 return False;
674
675         area->x1 = area->y1 = area->x2 = area->y2 = 0;
676
677         for (cur = scr->netdata->strut; cur; cur = cur->next) {
678                 WWindow *wwin = wWindowFor(cur->window);
679                 if (wWindowTouchesHead(wwin, head)) {
680                         if (cur->area.x1 > area->x1)
681                                 area->x1 = cur->area.x1;
682                         if (cur->area.y1 > area->y1)
683                                 area->y1 = cur->area.y1;
684                         if (cur->area.x2 > area->x2)
685                                 area->x2 = cur->area.x2;
686                         if (cur->area.y2 > area->y2)
687                                 area->y2 = cur->area.y2;
688                 }
689         }
690
691         if (area->x1 == 0 && area->x2 == 0 && area->y1 == 0 && area->y2 == 0)
692                 return False;
693
694         rect = wGetRectForHead(scr, head);
695
696         area->x1 = rect.pos.x + area->x1;
697         area->x2 = rect.pos.x + rect.size.width - area->x2;
698         area->y1 = rect.pos.y + area->y1;
699         area->y2 = rect.pos.y + rect.size.height - area->y2;
700
701         return True;
702 }
703
704 static void updateClientList(WScreen * scr)
705 {
706         WWindow *wwin;
707         Window *windows;
708         int count;
709
710         windows = (Window *) wmalloc(sizeof(Window) * (scr->window_count + 1));
711
712         count = 0;
713         wwin = scr->focused_window;
714         while (wwin) {
715                 windows[count++] = wwin->client_win;
716                 wwin = wwin->prev;
717         }
718         XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
719                         PropModeReplace, (unsigned char *)windows, count);
720
721         wfree(windows);
722         XFlush(dpy);
723 }
724
725 static void updateClientListStacking(WScreen * scr, WWindow * wwin_excl)
726 {
727         WWindow *wwin;
728         Window *client_list;
729         Window *client_list_reverse;
730         int client_count;
731         WCoreWindow *tmp;
732         WMBagIterator iter;
733         int i;
734
735         /* update client list */
736         i = scr->window_count + 1;
737         client_list = (Window *) wmalloc(sizeof(Window) * i);
738         client_list_reverse = (Window *) wmalloc(sizeof(Window) * i);
739
740         client_count = 0;
741         WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
742                 while (tmp) {
743                         wwin = wWindowFor(tmp->window);
744                         /* wwin_excl is a window to exclude from the list
745                            (e.g. it's now unmanaged) */
746                         if (wwin && (wwin != wwin_excl))
747                                 client_list[client_count++] = wwin->client_win;
748                         tmp = tmp->stacking->under;
749                 }
750         }
751
752         for (i = 0; i < client_count; i++) {
753                 Window w = client_list[client_count - i - 1];
754                 client_list_reverse[i] = w;
755         }
756
757         XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
758                         PropModeReplace, (unsigned char *)client_list_reverse, client_count);
759
760         wfree(client_list);
761         wfree(client_list_reverse);
762
763         XFlush(dpy);
764 }
765
766 static void updateWorkspaceCount(WScreen * scr)
767 {                               /* changeable */
768         long count;
769
770         count = scr->workspace_count;
771
772         XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
773                         32, PropModeReplace, (unsigned char *)&count, 1);
774 }
775
776 static void updateCurrentWorkspace(WScreen * scr)
777 {                               /* changeable */
778         long count;
779
780         count = scr->current_workspace;
781
782         XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
783                         PropModeReplace, (unsigned char *)&count, 1);
784 }
785
786 static void updateWorkspaceNames(WScreen * scr)
787 {
788         char buf[MAX_WORKSPACES * (MAX_WORKSPACENAME_WIDTH + 1)], *pos;
789         unsigned int i, len, curr_size;
790
791         pos = buf;
792         len = 0;
793         for (i = 0; i < scr->workspace_count; i++) {
794                 curr_size = strlen(scr->workspaces[i]->name);
795                 strcpy(pos, scr->workspaces[i]->name);
796                 pos += (curr_size + 1);
797                 len += (curr_size + 1);
798         }
799
800         XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
801                         PropModeReplace, (unsigned char *)buf, len);
802 }
803
804 static void updateFocusHint(WScreen * scr, WWindow * wwin)
805 {                               /* changeable */
806         Window window;
807
808         if (!scr->focused_window || !scr->focused_window->flags.focused)
809                 window = None;
810         else
811                 window = scr->focused_window->client_win;
812
813         XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
814                         PropModeReplace, (unsigned char *)&window, 1);
815 }
816
817 static void updateWorkspaceHint(WWindow * wwin, Bool fake, Bool del)
818 {
819         long l;
820
821         if (del) {
822                 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
823         } else {
824                 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
825                 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
826                                 32, PropModeReplace, (unsigned char *)&l, 1);
827         }
828 }
829
830 static void updateStateHint(WWindow * wwin, Bool changedWorkspace, Bool del)
831 {                               /* changeable */
832         if (del) {
833                 if (!wwin->flags.net_state_from_client) {
834                         XDeleteProperty(dpy, wwin->client_win, net_wm_state);
835                 }
836         } else {
837                 Atom state[15]; /* nr of defined state atoms */
838                 int i = 0;
839
840                 if (changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
841                         updateWorkspaceHint(wwin, False, False);
842
843                 if (IS_OMNIPRESENT(wwin))
844                         state[i++] = net_wm_state_sticky;
845                 if (wwin->flags.shaded)
846                         state[i++] = net_wm_state_shaded;
847                 if (wwin->flags.maximized & MAX_HORIZONTAL)
848                         state[i++] = net_wm_state_maximized_horz;
849                 if (wwin->flags.maximized & MAX_VERTICAL)
850                         state[i++] = net_wm_state_maximized_vert;
851                 if (WFLAGP(wwin, skip_window_list))
852                         state[i++] = net_wm_state_skip_taskbar;
853                 if (wwin->flags.net_skip_pager)
854                         state[i++] = net_wm_state_skip_pager;
855                 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop) {
856                         state[i++] = net_wm_state_hidden;
857                         state[i++] = net_wm_state_skip_pager;
858
859                         if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
860                                 if (!IS_OMNIPRESENT(wwin))
861                                         updateWorkspaceHint(wwin, True, False);
862                                 state[i++] = net_wm_state_sticky;
863                         }
864                 }
865                 if (WFLAGP(wwin, sunken))
866                         state[i++] = net_wm_state_below;
867                 if (WFLAGP(wwin, floating))
868                         state[i++] = net_wm_state_above;
869                 if (wwin->flags.fullscreen)
870                         state[i++] = net_wm_state_fullscreen;
871
872                 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
873                                 PropModeReplace, (unsigned char *)state, i);
874         }
875 }
876
877 static Bool updateStrut(WWindow * wwin, Bool adding)
878 {
879         WScreen *scr;
880         WReservedArea *area;
881         Bool hasState = False;
882
883         scr = wwin->screen_ptr;
884
885         if (adding) {
886                 Atom type_ret;
887                 int fmt_ret;
888                 unsigned long nitems_ret;
889                 unsigned long bytes_after_ret;
890                 long *data = 0;
891
892                 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
893                                        XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
894                                        &bytes_after_ret, (unsigned char **)&data) == Success && data) {
895
896                         area = (WReservedArea *) wmalloc(sizeof(WReservedArea));
897                         area->area.x1 = data[0];
898                         area->area.x2 = data[1];
899                         area->area.y1 = data[2];
900                         area->area.y2 = data[3];
901
902                         area->window = wwin->client_win;
903
904                         area->next = scr->netdata->strut;
905                         scr->netdata->strut = area;
906
907                         XFree(data);
908                         hasState = True;
909 #ifdef VIRTUAL_DESKTOP
910                         /* just in case wm_window_type didn't set it already */
911                         wwin->client_flags.virtual_stick = 1;
912 #endif
913                 }
914
915         } else {
916                 /* deleting */
917                 area = scr->netdata->strut;
918
919                 if (area) {
920                         if (area->window == wwin->client_win) {
921                                 scr->netdata->strut = area->next;
922                                 wfree(area);
923                                 hasState = True;
924                         } else {
925                                 while (area->next && area->next->window != wwin->client_win)
926                                         area = area->next;
927
928                                 if (area->next) {
929                                         WReservedArea *next;
930
931                                         next = area->next->next;
932                                         wfree(area->next);
933                                         area->next = next;
934
935                                         hasState = True;
936                                 }
937                         }
938                 }
939         }
940
941         return hasState;
942 }
943
944 static int getWindowLayer(WWindow * wwin)
945 {
946         int layer = WMNormalLevel;
947
948         if (wwin->type == net_wm_window_type_desktop) {
949                 layer = WMDesktopLevel;
950         } else if (wwin->type == net_wm_window_type_dock) {
951                 layer = WMDockLevel;
952         } else if (wwin->type == net_wm_window_type_toolbar) {
953                 layer = WMMainMenuLevel;
954         } else if (wwin->type == net_wm_window_type_menu) {
955                 layer = WMSubmenuLevel;
956         } else if (wwin->type == net_wm_window_type_utility) {
957         } else if (wwin->type == net_wm_window_type_splash) {
958         } else if (wwin->type == net_wm_window_type_dialog) {
959                 if (wwin->transient_for) {
960                         WWindow *parent = wWindowFor(wwin->transient_for);
961                         if (parent && parent->flags.fullscreen) {
962                                 layer = WMFullscreenLevel;
963                         }
964                 }
965                 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
966         } else if (wwin->type == net_wm_window_type_normal) {
967         }
968
969         if (wwin->client_flags.sunken && WMSunkenLevel < layer)
970                 layer = WMSunkenLevel;
971         if (wwin->client_flags.floating && WMFloatingLevel > layer)
972                 layer = WMFloatingLevel;
973
974         return layer;
975 }
976
977 static void doStateAtom(WWindow * wwin, Atom state, int set, Bool init)
978 {
979
980         if (state == net_wm_state_sticky) {
981                 if (set == _NET_WM_STATE_TOGGLE) {
982                         set = !IS_OMNIPRESENT(wwin);
983                 }
984                 if (set != wwin->flags.omnipresent) {
985                         wWindowSetOmnipresent(wwin, set);
986                 }
987         } else if (state == net_wm_state_shaded) {
988                 if (set == _NET_WM_STATE_TOGGLE) {
989                         set = !wwin->flags.shaded;
990                 }
991                 if (init) {
992                         wwin->flags.shaded = set;
993                 } else {
994                         if (set) {
995                                 wShadeWindow(wwin);
996                         } else {
997                                 wUnshadeWindow(wwin);
998                         }
999                 }
1000         } else if (state == net_wm_state_skip_taskbar) {
1001                 if (set == _NET_WM_STATE_TOGGLE) {
1002                         set = !wwin->client_flags.skip_window_list;
1003                 }
1004                 wwin->client_flags.skip_window_list = set;
1005         } else if (state == net_wm_state_skip_pager) {
1006                 if (set == _NET_WM_STATE_TOGGLE) {
1007                         set = !wwin->flags.net_skip_pager;
1008                 }
1009                 wwin->flags.net_skip_pager = set;
1010         } else if (state == net_wm_state_maximized_vert) {
1011                 if (set == _NET_WM_STATE_TOGGLE) {
1012                         set = !(wwin->flags.maximized & MAX_VERTICAL);
1013                 }
1014                 if (init) {
1015                         wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1016                 } else {
1017                         if (set) {
1018                                 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1019                         } else {
1020                                 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1021                         }
1022                 }
1023         } else if (state == net_wm_state_maximized_horz) {
1024                 if (set == _NET_WM_STATE_TOGGLE) {
1025                         set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1026                 }
1027                 if (init) {
1028                         wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1029                 } else {
1030                         if (set) {
1031                                 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1032                         } else {
1033                                 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1034                         }
1035                 }
1036         } else if (state == net_wm_state_hidden) {
1037                 if (set == _NET_WM_STATE_TOGGLE) {
1038                         set = !(wwin->flags.miniaturized);
1039                 }
1040                 if (init) {
1041                         wwin->flags.miniaturized = set;
1042                 } else {
1043                         if (set) {
1044                                 wIconifyWindow(wwin);
1045                         } else {
1046                                 wDeiconifyWindow(wwin);
1047                         }
1048                 }
1049         } else if (state == net_wm_state_fullscreen) {
1050                 if (set == _NET_WM_STATE_TOGGLE) {
1051                         set = !(wwin->flags.fullscreen);
1052                 }
1053                 if (init) {
1054                         wwin->flags.fullscreen = set;
1055                 } else {
1056                         if (set) {
1057                                 wFullscreenWindow(wwin);
1058                         } else {
1059                                 wUnfullscreenWindow(wwin);
1060                         }
1061                 }
1062         } else if (state == net_wm_state_above) {
1063                 if (set == _NET_WM_STATE_TOGGLE) {
1064                         set = !(wwin->client_flags.floating);
1065                 }
1066                 if (init) {
1067                         wwin->client_flags.floating = set;
1068                 } else {
1069                         wwin->client_flags.floating = set;
1070                         ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1071                 }
1072
1073         } else if (state == net_wm_state_below) {
1074                 if (set == _NET_WM_STATE_TOGGLE) {
1075                         set = !(wwin->client_flags.sunken);
1076                 }
1077                 if (init) {
1078                         wwin->client_flags.sunken = set;
1079                 } else {
1080                         wwin->client_flags.sunken = set;
1081                         ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1082                 }
1083
1084         } else {
1085 #ifdef DEBUG_WMSPEC
1086                 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1087 #endif
1088         }
1089 }
1090
1091 static void removeIcon(WWindow * wwin)
1092 {
1093         if (wwin->icon == NULL)
1094                 return;
1095         if (wwin->flags.miniaturized && wwin->icon->mapped) {
1096                 XUnmapWindow(dpy, wwin->icon->core->window);
1097                 RemoveFromStackList(wwin->icon->core);
1098                 wIconDestroy(wwin->icon);
1099                 wwin->icon = NULL;
1100         }
1101 }
1102
1103 static Bool handleWindowType(WWindow * wwin, Atom type, int *layer)
1104 {
1105         Bool ret = True;
1106
1107         if (type == net_wm_window_type_desktop) {
1108                 wwin->client_flags.no_titlebar = 1;
1109                 wwin->client_flags.no_resizable = 1;
1110                 wwin->client_flags.no_miniaturizable = 1;
1111                 wwin->client_flags.no_border = 1;
1112                 wwin->client_flags.no_resizebar = 1;
1113                 wwin->client_flags.no_shadeable = 1;
1114                 wwin->client_flags.no_movable = 1;
1115                 wwin->client_flags.omnipresent = 1;
1116                 wwin->client_flags.skip_window_list = 1;
1117                 wwin->client_flags.dont_move_off = 1;
1118                 wwin->client_flags.no_appicon = 1;
1119                 wwin->flags.net_skip_pager = 1;
1120                 wwin->frame_x = 0;
1121                 wwin->frame_y = 0;
1122 #ifdef VIRTUAL_DESKTOP
1123                 wwin->client_flags.virtual_stick = 1;
1124 #endif
1125         } else if (type == net_wm_window_type_dock) {
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_border = 1;       /* XXX: really not a single decoration. */
1130                 wwin->client_flags.no_resizebar = 1;
1131                 wwin->client_flags.no_shadeable = 1;
1132                 wwin->client_flags.no_movable = 1;
1133                 wwin->client_flags.omnipresent = 1;
1134                 wwin->client_flags.skip_window_list = 1;
1135                 wwin->client_flags.dont_move_off = 1;
1136                 wwin->flags.net_skip_pager = 1;
1137 #ifdef VIRTUAL_DESKTOP
1138                 wwin->client_flags.virtual_stick = 1;
1139 #endif
1140         } else if (type == net_wm_window_type_toolbar) {
1141                 wwin->client_flags.no_titlebar = 1;
1142                 wwin->client_flags.no_resizable = 1;
1143                 wwin->client_flags.no_miniaturizable = 1;
1144                 wwin->client_flags.no_resizebar = 1;
1145                 wwin->client_flags.no_shadeable = 1;
1146                 wwin->client_flags.skip_window_list = 1;
1147                 wwin->client_flags.dont_move_off = 1;
1148                 wwin->client_flags.no_appicon = 1;
1149         } else if (type == net_wm_window_type_menu) {
1150                 wwin->client_flags.no_titlebar = 1;
1151                 wwin->client_flags.no_resizable = 1;
1152                 wwin->client_flags.no_miniaturizable = 1;
1153                 wwin->client_flags.no_resizebar = 1;
1154                 wwin->client_flags.no_shadeable = 1;
1155                 wwin->client_flags.skip_window_list = 1;
1156                 wwin->client_flags.dont_move_off = 1;
1157                 wwin->client_flags.no_appicon = 1;
1158         } else if (type == net_wm_window_type_utility) {
1159                 wwin->client_flags.no_appicon = 1;
1160         } else if (type == net_wm_window_type_splash) {
1161                 wwin->client_flags.no_titlebar = 1;
1162                 wwin->client_flags.no_resizable = 1;
1163                 wwin->client_flags.no_miniaturizable = 1;
1164                 wwin->client_flags.no_resizebar = 1;
1165                 wwin->client_flags.no_shadeable = 1;
1166                 wwin->client_flags.no_movable = 1;
1167                 wwin->client_flags.skip_window_list = 1;
1168                 wwin->client_flags.dont_move_off = 1;
1169                 wwin->client_flags.no_appicon = 1;
1170                 wwin->flags.net_skip_pager = 1;
1171         } else if (type == net_wm_window_type_dialog) {
1172                 /* These also seem a bad idea in our context -Dan
1173                    // wwin->client_flags.skip_window_list = 1;
1174                    // wwin->client_flags.no_appicon = 1;
1175                  */
1176         } else if (type == net_wm_window_type_normal) {
1177         } else {
1178                 ret = False;
1179         }
1180
1181         wwin->type = type;
1182         *layer = getWindowLayer(wwin);
1183
1184         return ret;
1185 }
1186
1187 void wNETWMPositionSplash(WWindow * wwin, int *x, int *y, int width, int height)
1188 {
1189         if (wwin->type == net_wm_window_type_splash) {
1190                 WScreen *scr = wwin->screen_ptr;
1191                 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1192                 *x = rect.pos.x + (rect.size.width - width) / 2;
1193                 *y = rect.pos.y + (rect.size.height - height) / 2;
1194         }
1195 }
1196
1197 static void updateWindowType(WWindow * wwin)
1198 {
1199         Atom type_ret;
1200         int fmt_ret;
1201         unsigned long nitems_ret;
1202         unsigned long bytes_after_ret;
1203         long *data = 0;
1204         int layer;
1205         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1206                                False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1207                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1208
1209                 int i;
1210                 Atom *type = (Atom *) data;
1211                 for (i = 0; i < nitems_ret; ++i) {
1212                         if (handleWindowType(wwin, type[i], &layer))
1213                                 break;
1214                 }
1215                 XFree(data);
1216         }
1217
1218         ChangeStackingLevel(wwin->frame->core, layer);
1219         wwin->frame->flags.need_texture_change = 1;
1220         wWindowConfigureBorders(wwin);
1221         wFrameWindowPaint(wwin->frame);
1222         wNETWMUpdateActions(wwin, False);
1223 }
1224
1225 Bool wNETWMCheckClientHints(WWindow * wwin, int *layer, int *workspace)
1226 {
1227         Atom type_ret;
1228         int fmt_ret;
1229         unsigned long nitems_ret;
1230         unsigned long bytes_after_ret;
1231         long *data = 0;
1232         Bool hasState = False;
1233         int i;
1234
1235         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1236                                XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1237                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1238
1239                 long desktop = *data;
1240                 XFree(data);
1241
1242                 if (desktop == -1)
1243                         wwin->client_flags.omnipresent = 1;
1244                 else
1245                         *workspace = desktop;
1246
1247                 hasState = True;
1248         }
1249
1250         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1251                                XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1252                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1253
1254                 Atom *state = (Atom *) data;
1255                 for (i = 0; i < nitems_ret; ++i) {
1256                         doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1257                 }
1258                 XFree(data);
1259                 hasState = True;
1260         }
1261
1262         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1263                                XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1264                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1265
1266                 Atom *type = (Atom *) data;
1267                 for (i = 0; i < nitems_ret; ++i) {
1268                         if (handleWindowType(wwin, type[i], layer))
1269                                 break;
1270                 }
1271                 XFree(data);
1272                 hasState = True;
1273         }
1274
1275         wNETWMUpdateActions(wwin, False);
1276         updateStrut(wwin, False);
1277         if (updateStrut(wwin, True)) {
1278                 hasState = True;
1279         }
1280         wScreenUpdateUsableArea(wwin->screen_ptr);
1281
1282         return hasState;
1283 }
1284
1285 static Bool updateNetIconInfo(WWindow * wwin)
1286 {
1287
1288         Atom type_ret;
1289         int fmt_ret;
1290         unsigned long nitems_ret;
1291         unsigned long bytes_after_ret;
1292         long *data = 0;
1293         Bool hasState = False;
1294         Bool old_state = wwin->flags.net_handle_icon;
1295
1296         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1297                                XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1298                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1299                 long handled = *data;
1300                 wwin->flags.net_handle_icon = (handled != 0);
1301                 XFree(data);
1302                 hasState = True;
1303
1304         } else
1305                 wwin->flags.net_handle_icon = False;
1306
1307         if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1308                                XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1309                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1310
1311 #ifdef NETWM_PROPER
1312                 if (wwin->flags.net_handle_icon)
1313 #else
1314                 wwin->flags.net_handle_icon = True;
1315 #endif
1316                 {
1317                         wwin->icon_x = data[0];
1318                         wwin->icon_y = data[1];
1319                         wwin->icon_w = data[2];
1320                         wwin->icon_h = data[3];
1321                 }
1322
1323                 XFree(data);
1324                 hasState = True;
1325
1326         } else
1327                 wwin->flags.net_handle_icon = False;
1328
1329         if (wwin->flags.miniaturized && old_state != wwin->flags.net_handle_icon) {
1330                 if (wwin->flags.net_handle_icon) {
1331                         removeIcon(wwin);
1332                 } else {
1333                         wwin->flags.miniaturized = False;
1334                         wwin->flags.skip_next_animation = True;
1335                         wIconifyWindow(wwin);
1336                 }
1337         }
1338
1339         return hasState;
1340 }
1341
1342 Bool wNETWMCheckInitialClientState(WWindow * wwin)
1343 {
1344         Bool hasState = False;
1345
1346 #ifdef DEBUG_WMSPEC
1347         printf("CheckInitialClientState\n");
1348 #endif
1349
1350         wNETWMShowingDesktop(wwin->screen_ptr, False);
1351
1352         hasState |= updateNetIconInfo(wwin);
1353
1354         updateIconImage(wwin->screen_ptr, wwin);
1355
1356         return hasState;
1357 }
1358
1359 static void handleDesktopNames(XClientMessageEvent * event, WScreen * scr)
1360 {
1361         unsigned long nitems_ret, bytes_after_ret;
1362         char *data, *names[32];
1363         int fmt_ret, i, n;
1364         Atom type_ret;
1365
1366         if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1367                                utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1368                                &bytes_after_ret, (unsigned char **)&data) != Success) {
1369                 return;
1370         }
1371
1372         if (data == 0)
1373                 return;
1374
1375         if (type_ret != utf8_string || fmt_ret != 8)
1376                 return;
1377
1378         n = 0;
1379         names[n] = data;
1380         for (i = 0; i < nitems_ret; i++) {
1381                 if (data[i] == 0) {
1382                         n++;
1383                         names[n] = &data[i];
1384                 } else if (*names[n] == 0) {
1385                         names[n] = &data[i];
1386                         wWorkspaceRename(scr, n, names[n]);
1387                 }
1388         }
1389 }
1390
1391 Bool wNETWMProcessClientMessage(XClientMessageEvent * event)
1392 {
1393         WScreen *scr;
1394         WWindow *wwin;
1395         Bool done = True;
1396
1397 #ifdef DEBUG_WMSPEC
1398         printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1399 #endif
1400
1401         scr = wScreenForWindow(event->window);
1402         if (scr) {
1403                 /* generic client messages */
1404                 if (event->message_type == net_current_desktop) {
1405                         wWorkspaceChange(scr, event->data.l[0]);
1406                 } else if (event->message_type == net_number_of_desktops) {
1407                         long value;
1408
1409                         value = event->data.l[0];
1410                         if (value > scr->workspace_count) {
1411                                 wWorkspaceMake(scr, value - scr->workspace_count);
1412                         } else if (value < scr->workspace_count) {
1413                                 int i;
1414                                 Bool rebuild = False;
1415
1416                                 for (i = scr->workspace_count - 1; i >= value; i--) {
1417                                         if (!wWorkspaceDelete(scr, i)) {
1418                                                 rebuild = True;
1419                                                 break;
1420                                         }
1421                                 }
1422
1423                                 if (rebuild) {
1424                                         updateWorkspaceCount(scr);
1425                                 }
1426                         }
1427                 } else if (event->message_type == net_showing_desktop) {
1428                         wNETWMShowingDesktop(scr, event->data.l[0]);
1429 #ifdef VIRTUAL_DESKTOP
1430                 } else if (event->message_type == net_desktop_viewport) {
1431                         wWorkspaceSetViewport(scr, scr->current_workspace, event->data.l[0], event->data.l[1]);
1432 #endif
1433                 } else if (event->message_type == net_desktop_names) {
1434                         handleDesktopNames(event, scr);
1435                 } else {
1436                         done = False;
1437                 }
1438
1439                 if (done)
1440                         return True;
1441         }
1442
1443         /* window specific client messages */
1444
1445         wwin = wWindowFor(event->window);
1446         if (!wwin)
1447                 return False;
1448
1449         if (event->message_type == net_active_window) {
1450                 wNETWMShowingDesktop(scr, False);
1451                 wMakeWindowVisible(wwin);
1452         } else if (event->message_type == net_close_window) {
1453                 if (!WFLAGP(wwin, no_closable)) {
1454                         if (wwin->protocols.DELETE_WINDOW)
1455                                 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1456                 }
1457         } else if (event->message_type == net_wm_state) {
1458                 int maximized = wwin->flags.maximized;
1459                 long set = event->data.l[0];
1460
1461 #ifdef DEBUG_WMSPEC
1462                 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1463                        XGetAtomName(dpy, event->data.l[1]), XGetAtomName(dpy, event->data.l[2]));
1464 #endif
1465
1466                 doStateAtom(wwin, (Atom) event->data.l[1], set, False);
1467                 if (event->data.l[2])
1468                         doStateAtom(wwin, (Atom) event->data.l[2], set, False);
1469
1470                 if (wwin->flags.maximized != maximized) {
1471                         if (!wwin->flags.maximized) {
1472                                 wwin->flags.maximized = maximized;
1473                                 wUnmaximizeWindow(wwin);
1474                         } else {
1475                                 wMaximizeWindow(wwin, wwin->flags.maximized);
1476                         }
1477                 }
1478                 updateStateHint(wwin, False, False);
1479         } else if (event->message_type == net_wm_handled_icons || event->message_type == net_wm_icon_geometry) {
1480                 updateNetIconInfo(wwin);
1481         } else if (event->message_type == net_wm_desktop) {
1482                 long desktop = event->data.l[0];
1483                 if (desktop == -1) {
1484                         wWindowSetOmnipresent(wwin, True);
1485                 } else {
1486                         if (IS_OMNIPRESENT(wwin))
1487                                 wWindowSetOmnipresent(wwin, False);
1488                         wWindowChangeWorkspace(wwin, desktop);
1489                 }
1490         } else if (event->message_type == net_wm_icon) {
1491                 updateIconImage(scr, wwin);
1492         } else {
1493                 done = False;
1494         }
1495
1496         return done;
1497 }
1498
1499 Bool wNETWMCheckClientHintChange(WWindow * wwin, XPropertyEvent * event)
1500 {
1501         Bool ret = True;
1502
1503         if (event->atom == net_wm_strut) {
1504                 updateStrut(wwin, False);
1505                 updateStrut(wwin, True);
1506                 wScreenUpdateUsableArea(wwin->screen_ptr);
1507         } else if (event->atom == net_wm_handled_icons || event->atom == net_wm_icon_geometry) {
1508                 updateNetIconInfo(wwin);
1509         } else if (event->atom == net_wm_window_type) {
1510                 updateWindowType(wwin);
1511         } else if (event->atom == net_wm_name) {
1512                 char *name = wNETWMGetWindowName(wwin->client_win);
1513                 wWindowUpdateName(wwin, name);
1514                 if (name)
1515                         wfree(name);
1516         } else if (event->atom == net_wm_icon_name) {
1517                 if (wwin->icon) {
1518                         char *name = wNETWMGetIconName(wwin->client_win);
1519                         wIconChangeTitle(wwin->icon, name);
1520                 }
1521         } else {
1522                 ret = False;
1523         }
1524
1525         return ret;
1526 }
1527
1528 int wNETWMGetPidForWindow(Window window)
1529 {
1530         Atom type_ret;
1531         int fmt_ret;
1532         unsigned long nitems_ret;
1533         unsigned long bytes_after_ret;
1534         long *data = 0;
1535         int pid;
1536
1537         if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1538                                XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1539                                &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1540
1541                 pid = *data;
1542                 XFree(data);
1543         } else {
1544                 pid = 0;
1545         }
1546
1547         return pid;
1548 }
1549
1550 char *wNETWMGetWindowName(Window window)
1551 {
1552         char *name;
1553         char *ret;
1554         int size;
1555
1556         name = (char *)PropGetCheckProperty(window, net_wm_name, utf8_string, 0, 0, &size);
1557         if (name) {
1558                 ret = wstrndup(name, size);
1559                 XFree(name);
1560         } else
1561                 ret = NULL;
1562         return ret;
1563 }
1564
1565 char *wNETWMGetIconName(Window window)
1566 {
1567         char *name;
1568         char *ret;
1569         int size;
1570
1571         name = (char *)PropGetCheckProperty(window, net_wm_icon_name, utf8_string, 0, 0, &size);
1572         if (name) {
1573                 ret = wstrndup(name, size);
1574                 XFree(name);
1575         } else
1576                 ret = NULL;
1577         return ret;
1578 }
1579
1580 static void observer(void *self, WMNotification * notif)
1581 {
1582         WWindow *wwin = (WWindow *) WMGetNotificationObject(notif);
1583         const char *name = WMGetNotificationName(notif);
1584         void *data = WMGetNotificationClientData(notif);
1585         NetData *ndata = (NetData *) self;
1586
1587         if (strcmp(name, WMNManaged) == 0 && wwin) {
1588                 updateClientList(wwin->screen_ptr);
1589                 updateClientListStacking(wwin->screen_ptr, NULL);
1590                 updateStateHint(wwin, True, False);
1591
1592                 updateStrut(wwin, False);
1593                 updateStrut(wwin, True);
1594                 wScreenUpdateUsableArea(wwin->screen_ptr);
1595         } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1596                 updateClientList(wwin->screen_ptr);
1597                 updateClientListStacking(wwin->screen_ptr, wwin);
1598                 updateWorkspaceHint(wwin, False, True);
1599                 updateStateHint(wwin, False, True);
1600                 wNETWMUpdateActions(wwin, True);
1601
1602                 updateStrut(wwin, False);
1603                 wScreenUpdateUsableArea(wwin->screen_ptr);
1604         } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1605                 updateClientListStacking(wwin->screen_ptr, NULL);
1606                 updateStateHint(wwin, False, False);
1607         } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1608                 updateClientListStacking(wwin->screen_ptr, NULL);
1609                 updateStateHint(wwin, False, False);
1610         } else if (strcmp(name, WMNChangedFocus) == 0) {
1611                 updateFocusHint(ndata->scr, wwin);
1612         } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1613                 updateWorkspaceHint(wwin, False, False);
1614                 updateStateHint(wwin, True, False);
1615         } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1616                 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1617         }
1618 }
1619
1620 static void wsobserver(void *self, WMNotification * notif)
1621 {
1622         WScreen *scr = (WScreen *) WMGetNotificationObject(notif);
1623         const char *name = WMGetNotificationName(notif);
1624
1625         if (strcmp(name, WMNWorkspaceCreated) == 0) {
1626                 updateWorkspaceCount(scr);
1627                 updateWorkspaceNames(scr);
1628         } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1629                 updateWorkspaceCount(scr);
1630                 updateWorkspaceNames(scr);
1631         } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1632                 updateCurrentWorkspace(scr);
1633         } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1634                 updateWorkspaceNames(scr);
1635         }
1636 }
1637
1638 #endif                          /* NETWM_HINTS */