- fixed issue with invisible modal windows from Qt and Gtk applications
[wmaker-crm.git] / src / wmspec.c
blob27ce25238dfe341da1a6d21f67cfbe4d26d15943
1 /* wmspec.c-- support for the wm-spec Hints
3 * Window Maker window manager
5 * Copyright (c) 1998-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
24 * TODO
25 * ----
27 * This file needs to be checked for all calls to XGetWindowProperty() and
28 * proper checks need to be made on the returned values. Only checking for
29 * return to be Success is not enough. -Dan
32 #include "wconfig.h"
34 #ifdef NETWM_HINTS
36 #include <X11/Xlib.h>
37 #include <X11/Xatom.h>
38 #include <X11/Xarch.h>
39 #include <string.h>
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"
55 #ifdef DEBUG_WMSPEC
56 #include <stdio.h>
57 #endif
59 /* Global variables */
60 extern Atom _XA_WM_DELETE_WINDOW;
61 extern Time LastTimestamp;
62 extern WPreferences wPreferences;
64 /* Root Window Properties */
65 static Atom net_supported;
66 static Atom net_client_list;
67 static Atom net_client_list_stacking;
68 static Atom net_number_of_desktops;
69 static Atom net_desktop_geometry;
70 static Atom net_desktop_viewport;
71 static Atom net_current_desktop;
72 static Atom net_desktop_names;
73 static Atom net_active_window;
74 static Atom net_workarea; /* XXX: not xinerama compatible */
75 static Atom net_supporting_wm_check;
76 static Atom net_virtual_roots; /* N/A */
77 static Atom net_desktop_layout; /* XXX */
78 static Atom net_showing_desktop;
80 /* Other Root Window Messages */
81 static Atom net_close_window;
82 static Atom net_moveresize_window; /* TODO */
83 static Atom net_wm_moveresize; /* TODO */
85 /* Application Window Properties */
86 static Atom net_wm_name;
87 static Atom net_wm_visible_name; /* TODO (unnecessary?) */
88 static Atom net_wm_icon_name;
89 static Atom net_wm_visible_icon_name; /* TODO (unnecessary?) */
90 static Atom net_wm_desktop;
91 static Atom net_wm_window_type;
92 static Atom net_wm_window_type_desktop;
93 static Atom net_wm_window_type_dock;
94 static Atom net_wm_window_type_toolbar;
95 static Atom net_wm_window_type_menu;
96 static Atom net_wm_window_type_utility;
97 static Atom net_wm_window_type_splash;
98 static Atom net_wm_window_type_dialog;
99 static Atom net_wm_window_type_normal;
100 static Atom net_wm_state;
101 static Atom net_wm_state_modal; /* XXX: what is this?!? */
102 static Atom net_wm_state_sticky;
103 static Atom net_wm_state_maximized_vert;
104 static Atom net_wm_state_maximized_horz;
105 static Atom net_wm_state_shaded;
106 static Atom net_wm_state_skip_taskbar;
107 static Atom net_wm_state_skip_pager;
108 static Atom net_wm_state_hidden;
109 static Atom net_wm_state_fullscreen;
110 static Atom net_wm_state_above;
111 static Atom net_wm_state_below;
112 static Atom net_wm_allowed_actions;
113 static Atom net_wm_action_move;
114 static Atom net_wm_action_resize;
115 static Atom net_wm_action_minimize;
116 static Atom net_wm_action_shade;
117 static Atom net_wm_action_stick;
118 static Atom net_wm_action_maximize_horz;
119 static Atom net_wm_action_maximize_vert;
120 static Atom net_wm_action_fullscreen;
121 static Atom net_wm_action_change_desktop;
122 static Atom net_wm_action_close;
123 static Atom net_wm_strut; /* XXX: see net_workarea */
124 static Atom net_wm_strut_partial; /* TODO: doesn't really fit into the current strut scheme */
125 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. */
126 static Atom net_wm_icon;
127 static Atom net_wm_pid; /* TODO */
128 static Atom net_wm_handled_icons; /* FIXME: see net_wm_icon_geometry */
130 /* Window Manager Protocols */
131 static Atom net_wm_ping; /* TODO */
133 static Atom utf8_string;
135 typedef struct {
136 char * name;
137 Atom * atom;
138 } atomitem_t;
140 static atomitem_t atomNames[] = {
141 { "_NET_SUPPORTED", &net_supported },
142 { "_NET_CLIENT_LIST", &net_client_list },
143 { "_NET_CLIENT_LIST_STACKING", &net_client_list_stacking },
144 { "_NET_NUMBER_OF_DESKTOPS", &net_number_of_desktops },
145 { "_NET_DESKTOP_GEOMETRY", &net_desktop_geometry },
146 { "_NET_DESKTOP_VIEWPORT", &net_desktop_viewport },
147 { "_NET_CURRENT_DESKTOP", &net_current_desktop },
148 { "_NET_DESKTOP_NAMES", &net_desktop_names },
149 { "_NET_ACTIVE_WINDOW", &net_active_window },
150 { "_NET_WORKAREA", &net_workarea },
151 { "_NET_SUPPORTING_WM_CHECK", &net_supporting_wm_check },
152 { "_NET_VIRTUAL_ROOTS", &net_virtual_roots },
153 { "_NET_DESKTOP_LAYOUT", &net_desktop_layout },
154 { "_NET_SHOWING_DESKTOP", &net_showing_desktop },
156 { "_NET_CLOSE_WINDOW", &net_close_window },
157 { "_NET_MOVERESIZE_WINDOW", &net_moveresize_window },
158 { "_NET_WM_MOVERESIZE", &net_wm_moveresize },
160 { "_NET_WM_NAME", &net_wm_name },
161 { "_NET_WM_VISIBLE_NAME", &net_wm_visible_name },
162 { "_NET_WM_ICON_NAME", &net_wm_icon_name },
163 { "_NET_WM_VISIBLE_ICON_NAME", &net_wm_visible_icon_name },
164 { "_NET_WM_DESKTOP", &net_wm_desktop },
165 { "_NET_WM_WINDOW_TYPE", &net_wm_window_type },
166 { "_NET_WM_WINDOW_TYPE_DESKTOP", &net_wm_window_type_desktop },
167 { "_NET_WM_WINDOW_TYPE_DOCK", &net_wm_window_type_dock },
168 { "_NET_WM_WINDOW_TYPE_TOOLBAR", &net_wm_window_type_toolbar },
169 { "_NET_WM_WINDOW_TYPE_MENU", &net_wm_window_type_menu },
170 { "_NET_WM_WINDOW_TYPE_UTILITY", &net_wm_window_type_utility },
171 { "_NET_WM_WINDOW_TYPE_SPLASH", &net_wm_window_type_splash },
172 { "_NET_WM_WINDOW_TYPE_DIALOG", &net_wm_window_type_dialog },
173 { "_NET_WM_WINDOW_TYPE_NORMAL", &net_wm_window_type_normal },
174 { "_NET_WM_STATE", &net_wm_state },
175 { "_NET_WM_STATE_MODAL", &net_wm_state_modal },
176 { "_NET_WM_STATE_STICKY", &net_wm_state_sticky },
177 { "_NET_WM_STATE_MAXIMIZED_VERT", &net_wm_state_maximized_vert },
178 { "_NET_WM_STATE_MAXIMIZED_HORZ", &net_wm_state_maximized_horz },
179 { "_NET_WM_STATE_SHADED", &net_wm_state_shaded },
180 { "_NET_WM_STATE_SKIP_TASKBAR", &net_wm_state_skip_taskbar },
181 { "_NET_WM_STATE_SKIP_PAGER", &net_wm_state_skip_pager },
182 { "_NET_WM_STATE_HIDDEN", &net_wm_state_hidden },
183 { "_NET_WM_STATE_FULLSCREEN", &net_wm_state_fullscreen },
184 { "_NET_WM_STATE_ABOVE", &net_wm_state_above },
185 { "_NET_WM_STATE_BELOW", &net_wm_state_below },
186 { "_NET_WM_ALLOWED_ACTIONS", &net_wm_allowed_actions },
187 { "_NET_WM_ACTION_MOVE", &net_wm_action_move },
188 { "_NET_WM_ACTION_RESIZE", &net_wm_action_resize },
189 { "_NET_WM_ACTION_MINIMIZE", &net_wm_action_minimize },
190 { "_NET_WM_ACTION_SHADE", &net_wm_action_shade },
191 { "_NET_WM_ACTION_STICK", &net_wm_action_stick },
192 { "_NET_WM_ACTION_MAXIMIZE_HORZ", &net_wm_action_maximize_horz },
193 { "_NET_WM_ACTION_MAXIMIZE_VERT", &net_wm_action_maximize_vert },
194 { "_NET_WM_ACTION_FULLSCREEN", &net_wm_action_fullscreen },
195 { "_NET_WM_ACTION_CHANGE_DESKTOP", &net_wm_action_change_desktop },
196 { "_NET_WM_ACTION_CLOSE", &net_wm_action_close },
197 { "_NET_WM_STRUT", &net_wm_strut },
198 { "_NET_WM_STRUT_PARTIAL", &net_wm_strut_partial },
199 { "_NET_WM_ICON_GEOMETRY", &net_wm_icon_geometry },
200 { "_NET_WM_ICON", &net_wm_icon },
201 { "_NET_WM_PID", &net_wm_pid },
202 { "_NET_WM_HANDLED_ICONS", &net_wm_handled_icons },
204 { "_NET_WM_PING", &net_wm_ping },
206 { "UTF8_STRING", &utf8_string },
209 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
211 #define _NET_WM_STATE_REMOVE 0
212 #define _NET_WM_STATE_ADD 1
213 #define _NET_WM_STATE_TOGGLE 2
215 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
216 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
217 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
218 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
219 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
220 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
221 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
222 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
223 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
224 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
225 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
227 static void observer(void *self, WMNotification *notif);
228 static void wsobserver(void *self, WMNotification *notif);
230 static void updateClientList(WScreen *scr);
231 static void updateClientListStacking(WScreen *scr, WWindow *);
233 static void updateWorkspaceNames(WScreen *scr);
234 static void updateCurrentWorkspace(WScreen *scr);
235 static void updateWorkspaceCount(WScreen *scr);
237 typedef struct NetData {
238 WScreen *scr;
239 WReservedArea *strut;
240 WWindow **show_desktop;
241 } NetData;
244 static void
245 setSupportedHints(WScreen *scr)
247 Atom atom[atomNr];
248 int i = 0;
250 /* set supported hints list */
251 /* XXX: extend this !!! */
253 atom[i++] = net_client_list;
254 atom[i++] = net_client_list_stacking;
255 atom[i++] = net_number_of_desktops;
256 atom[i++] = net_desktop_geometry;
257 atom[i++] = net_desktop_viewport;
258 atom[i++] = net_current_desktop;
259 atom[i++] = net_desktop_names;
260 atom[i++] = net_active_window;
261 atom[i++] = net_workarea;
262 atom[i++] = net_supporting_wm_check;
263 atom[i++] = net_showing_desktop;
264 #if 0
265 atom[i++] = net_wm_moveresize;
266 #endif
267 atom[i++] = net_wm_desktop;
268 atom[i++] = net_wm_window_type;
269 atom[i++] = net_wm_window_type_desktop;
270 atom[i++] = net_wm_window_type_dock;
271 atom[i++] = net_wm_window_type_toolbar;
272 atom[i++] = net_wm_window_type_menu;
273 atom[i++] = net_wm_window_type_utility;
274 atom[i++] = net_wm_window_type_splash;
275 atom[i++] = net_wm_window_type_dialog;
276 atom[i++] = net_wm_window_type_normal;
278 atom[i++] = net_wm_state;
279 /* atom[i++] = net_wm_state_modal; */ /* XXX: not sure where/when to use it. */
280 atom[i++] = net_wm_state_sticky;
281 atom[i++] = net_wm_state_shaded;
282 atom[i++] = net_wm_state_maximized_horz;
283 atom[i++] = net_wm_state_maximized_vert;
284 atom[i++] = net_wm_state_skip_taskbar;
285 atom[i++] = net_wm_state_skip_pager;
286 atom[i++] = net_wm_state_hidden;
287 atom[i++] = net_wm_state_fullscreen;
288 atom[i++] = net_wm_state_above;
289 atom[i++] = net_wm_state_below;
291 atom[i++] = net_wm_allowed_actions;
292 atom[i++] = net_wm_action_move;
293 atom[i++] = net_wm_action_resize;
294 atom[i++] = net_wm_action_minimize;
295 atom[i++] = net_wm_action_shade;
296 atom[i++] = net_wm_action_stick;
297 atom[i++] = net_wm_action_maximize_horz;
298 atom[i++] = net_wm_action_maximize_vert;
299 atom[i++] = net_wm_action_fullscreen;
300 atom[i++] = net_wm_action_change_desktop;
301 atom[i++] = net_wm_action_close;
303 atom[i++] = net_wm_strut;
304 atom[i++] = net_wm_icon_geometry;
305 atom[i++] = net_wm_icon;
306 atom[i++] = net_wm_handled_icons;
308 atom[i++] = net_wm_name;
309 atom[i++] = net_wm_icon_name;
311 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32,
312 PropModeReplace, (unsigned char*)atom, i);
314 /* set supporting wm hint */
315 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
316 PropModeReplace, (unsigned char*)&scr->info_window, 1);
318 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
319 32, PropModeReplace, (unsigned char*)&scr->info_window, 1);
323 void
324 wNETWMUpdateDesktop(WScreen *scr)
326 long *views, sizes[2];
327 int count, i;
329 if (scr->workspace_count==0)
330 return;
332 count = scr->workspace_count * 2;
333 views = wmalloc(sizeof(long) * count);
334 /*memset(views, 0, sizeof(long) * count);*/
336 #ifdef VIRTUAL_DESKTOP
337 sizes[0] = scr->workspaces[scr->current_workspace]->width;
338 sizes[1] = scr->workspaces[scr->current_workspace]->height;
339 #else
340 sizes[0] = scr->scr_width;
341 sizes[1] = scr->scr_height;
342 #endif
344 for (i=0; i<scr->workspace_count; i++) {
345 #ifdef VIRTUAL_DESKTOP
346 views[2*i + 0] = scr->workspaces[i]->view_x;
347 views[2*i + 1] = scr->workspaces[i]->view_y;
348 #else
349 views[2*i + 0] = 0;
350 views[2*i + 1] = 0;
351 #endif
354 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
355 PropModeReplace, (unsigned char*)sizes, 2);
357 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
358 PropModeReplace, (unsigned char*)views, count);
360 wfree(views);
365 wNETWMGetCurrentDesktopFromHint(WScreen *scr)
367 int count;
368 unsigned char *prop;
370 prop= PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL,
371 0, 1, &count);
372 if (prop)
374 int desktop= *(long*)prop;
375 XFree(prop);
376 return desktop;
378 return -1;
383 * Find the best icon to be used by Window Maker for appicon/miniwindows.
384 * Currently the algorithm is to take the image with the size closest
385 * to icon_size x icon_size, but never bigger than that.
387 * This algorithm is very poorly implemented and needs to be redone (it can
388 * easily select images with very large widths and very small heights over
389 * square images, if the area of the former is closer to the desired one).
391 * The logic can also be changed to accept bigger images and scale them down.
393 static unsigned long*
394 findBestIcon(unsigned long *data, unsigned long items)
396 int size, wanted, d, distance;
397 unsigned long i;
398 unsigned long *icon;
400 /* better use only 75% of icon_size. For 64x64 this means 48x48
401 * This leaves room around the icon for the miniwindow title and
402 * results in better overall aesthetics -Dan */
403 wanted = wPreferences.icon_size * wPreferences.icon_size;
405 for (icon=NULL, distance=LONG_MAX, i=0L; i<items-1; ) {
406 size = data[i] * data[i+1];
407 if (size==0)
408 break;
409 d = wanted - size;
410 if (d>=0 && d<=distance && (i+size+2)<=items) {
411 distance = d;
412 icon = &data[i];
414 i += size+2;
417 return icon;
421 static RImage*
422 makeRImageFromARGBData(unsigned long *data)
424 int size, width, height, i;
425 RImage *image;
426 unsigned char *imgdata;
427 unsigned long pixel;
429 width = data[0];
430 height = data[1];
431 size = width * height;
433 if (size == 0)
434 return NULL;
436 image = RCreateImage(width, height, True);
438 for (imgdata=image->data, i=2; i<size+2; i++, imgdata+=4) {
439 pixel = data[i];
440 #if BYTE_ORDER == BIG_ENDIAN
441 imgdata[2] = (pixel >> 24) & 0xff; /* A */
442 imgdata[1] = (pixel >> 16) & 0xff; /* R */
443 imgdata[0] = (pixel >> 8) & 0xff; /* G */
444 imgdata[3] = (pixel >> 0) & 0xff; /* B */
445 #else /* Little endian */
446 imgdata[3] = (pixel >> 24) & 0xff; /* A */
447 imgdata[0] = (pixel >> 16) & 0xff; /* R */
448 imgdata[1] = (pixel >> 8) & 0xff; /* G */
449 imgdata[2] = (pixel >> 0) & 0xff; /* B */
450 #endif /* endianness */
454 return image;
458 static void
459 updateIconImage(WScreen *scr, WWindow *wwin)
461 unsigned long *property, *data;
462 unsigned long items, rest;
463 Atom type;
464 int format;
465 RImage *image;
467 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
468 False, XA_CARDINAL, &type, &format, &items, &rest,
469 (unsigned char**)&property)!=Success || !property) {
470 return;
473 if (type!=XA_CARDINAL || format!=32 || items<2) {
474 XFree(property);
475 return;
478 data = findBestIcon(property, items);
479 if (!data) {
480 XFree(property);
481 return;
484 image = makeRImageFromARGBData(data);
486 if (image) {
487 if (wwin->net_icon_image)
488 RReleaseImage(wwin->net_icon_image);
489 wwin->net_icon_image = image;
492 XFree(property);
496 static void
497 updateShowDesktop(WScreen * scr, Bool show)
499 long foo;
501 foo = (show == True);
502 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
503 PropModeReplace, (unsigned char *)&foo, 1);
507 void
508 wNETWMShowingDesktop(WScreen *scr, Bool show)
510 if (show && scr->netdata->show_desktop == NULL) {
511 WWindow *tmp, **wins;
512 int i=0;
514 wins = (WWindow **)wmalloc(sizeof(WWindow *)*(scr->window_count+1));
516 tmp = scr->focused_window;
517 while (tmp) {
518 if (!tmp->flags.hidden && !tmp->flags.miniaturized &&
519 !WFLAGP(tmp, skip_window_list)) {
521 wins[i++] = tmp;
522 tmp->flags.skip_next_animation = 1;
523 tmp->flags.net_show_desktop = 1;
524 wIconifyWindow(tmp);
527 tmp = tmp->prev;
529 wins[i++] = NULL;
531 scr->netdata->show_desktop = wins;
532 updateShowDesktop(scr, True);
533 } else if (scr->netdata->show_desktop != NULL) {
534 /* FIXME: get rid of workspace flashing ! */
535 int ws = scr->current_workspace;
536 WWindow **tmp;
537 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
538 wDeiconifyWindow(*tmp);
539 (*tmp)->flags.net_show_desktop = 0;
541 if (ws != scr->current_workspace)
542 wWorkspaceChange(scr, ws);
543 wfree(scr->netdata->show_desktop);
544 scr->netdata->show_desktop = NULL;
545 updateShowDesktop(scr, False);
550 void
551 wNETWMInitStuff(WScreen *scr)
553 NetData *data;
554 int i;
556 #ifdef DEBUG_WMSPEC
557 printf( "wNETWMInitStuff\n");
558 #endif
560 #ifdef HAVE_XINTERNATOMS
562 Atom atoms[atomNr];
563 char * names[atomNr];
565 for (i = 0; i < atomNr; ++i) {
566 names[i] = atomNames[i].name;
568 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
569 for (i = 0; i < atomNr; ++i) {
570 *atomNames[i].atom = atoms[i];
573 #else
574 for (i = 0; i < atomNr; i++) {
575 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
577 #endif
579 data = wmalloc(sizeof(NetData));
580 data->scr = scr;
581 data->strut = NULL;
582 data->show_desktop = NULL;
584 scr->netdata = data;
586 setSupportedHints(scr);
588 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
589 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
590 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
591 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
592 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
593 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
594 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
596 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
597 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
598 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
599 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
601 updateClientList(scr);
602 updateClientListStacking(scr, NULL);
603 updateWorkspaceCount(scr);
604 updateWorkspaceNames(scr);
605 updateShowDesktop(scr, False);
607 wScreenUpdateUsableArea(scr);
611 void
612 wNETWMCleanup(WScreen *scr)
614 int i;
616 for (i= 0; i < atomNr; i++)
617 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
621 void
622 wNETWMUpdateActions(WWindow *wwin, Bool del)
624 Atom action[10]; /* nr of actions atoms defined */
625 int i = 0;
627 if (del) {
628 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
629 return;
632 if (IS_MOVABLE(wwin))
633 action[i++] = net_wm_action_move;
635 if (IS_RESIZABLE(wwin))
636 action[i++] = net_wm_action_resize;
638 if (!WFLAGP(wwin, no_miniaturizable))
639 action[i++] = net_wm_action_minimize;
641 if (!WFLAGP(wwin, no_shadeable))
642 action[i++] = net_wm_action_shade;
644 /* if (!WFLAGP(wwin, no_stickable)) */
645 action[i++] = net_wm_action_stick;
647 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
648 if (IS_RESIZABLE(wwin))
649 action[i++] = net_wm_action_maximize_horz;
651 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
652 if (IS_RESIZABLE(wwin))
653 action[i++] = net_wm_action_maximize_vert;
655 /* if (!WFLAGP(wwin, no_fullscreen)) */
656 action[i++] = net_wm_action_fullscreen;
658 /* if (!WFLAGP(wwin, no_change_desktop)) */
659 action[i++] = net_wm_action_change_desktop;
661 if (!WFLAGP(wwin, no_closable))
662 action[i++] = net_wm_action_close;
664 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
665 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
669 void
670 wNETWMUpdateWorkarea(WScreen *scr, WArea usableArea)
672 long *area;
673 int count, i;
675 /* XXX: not Xinerama compatible,
676 xinerama gets the largest available */
678 if(!scr->netdata || scr->workspace_count==0)
679 return;
681 count = scr->workspace_count * 4;
682 area = wmalloc(sizeof(long) * count);
683 for (i=0; i<scr->workspace_count; i++) {
684 area[4*i + 0] = usableArea.x1;
685 area[4*i + 1] = usableArea.y1;
686 area[4*i + 2] = usableArea.x2 - usableArea.x1;
687 area[4*i + 3] = usableArea.y2 - usableArea.y1;
690 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
691 PropModeReplace, (unsigned char*)area, count);
693 wfree(area);
697 Bool
698 wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
700 WReservedArea *cur;
701 WMRect rect;
703 if(!scr->netdata || !scr->netdata->strut)
704 return False;
706 area->x1 = area->y1 = area->x2 = area->y2 = 0;
708 for(cur = scr->netdata->strut ; cur ; cur = cur->next) {
709 WWindow * wwin = wWindowFor(cur->window);
710 if (wWindowTouchesHead(wwin, head)) {
711 if(cur->area.x1 > area->x1)
712 area->x1 = cur->area.x1;
713 if(cur->area.y1 > area->y1)
714 area->y1 = cur->area.y1;
715 if(cur->area.x2 > area->x2)
716 area->x2 = cur->area.x2;
717 if(cur->area.y2 > area->y2)
718 area->y2 = cur->area.y2;
722 if (area->x1==0 && area->x2==0 &&
723 area->y1==0 && area->y2==0) return False;
725 rect = wGetRectForHead(scr, head);
727 area->x1 = rect.pos.x + area->x1;
728 area->x2 = rect.pos.x + rect.size.width - area->x2;
729 area->y1 = rect.pos.y + area->y1;
730 area->y2 = rect.pos.y + rect.size.height - area->y2;
732 return True;
736 static void
737 updateClientList(WScreen *scr)
739 WWindow *wwin;
740 Window *windows;
741 int count;
743 windows = (Window *)wmalloc(sizeof(Window)*(scr->window_count+1));
745 count = 0;
746 wwin = scr->focused_window;
747 while (wwin) {
748 windows[count++] = wwin->client_win;
749 wwin = wwin->prev;
751 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
752 PropModeReplace, (unsigned char*)windows, count);
754 wfree(windows);
755 XFlush(dpy);
759 static void
760 updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
762 WWindow *wwin;
763 Window *client_list;
764 Window *client_list_reverse;
765 int client_count;
766 WCoreWindow *tmp;
767 WMBagIterator iter;
768 int i;
770 /* update client list */
771 i = scr->window_count + 1;
772 client_list = (Window*)wmalloc(sizeof(Window) * i);
773 client_list_reverse = (Window*)wmalloc(sizeof(Window) * i);
775 client_count = 0;
776 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
777 while (tmp) {
778 wwin = wWindowFor(tmp->window);
779 /* wwin_excl is a window to exclude from the list
780 (e.g. it's now unmanaged) */
781 if(wwin && (wwin != wwin_excl))
782 client_list[client_count++] = wwin->client_win;
783 tmp = tmp->stacking->under;
787 for(i = 0; i < client_count; i++) {
788 Window w = client_list[client_count - i - 1];
789 client_list_reverse[i] = w;
792 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
793 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
795 wfree(client_list);
796 wfree(client_list_reverse);
798 XFlush(dpy);
802 static void
803 updateWorkspaceCount(WScreen *scr) /* changeable */
805 long count;
807 count = scr->workspace_count;
809 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
810 32, PropModeReplace, (unsigned char*)&count, 1);
814 static void
815 updateCurrentWorkspace(WScreen *scr) /* changeable */
817 long count;
819 count = scr->current_workspace;
821 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
822 PropModeReplace, (unsigned char*)&count, 1);
826 static void
827 updateWorkspaceNames(WScreen *scr)
829 char buf[1024], *pos;
830 unsigned int i, len, curr_size;
832 pos = buf;
833 len = 0;
834 for(i = 0; i < scr->workspace_count; i++) {
835 curr_size = strlen(scr->workspaces[i]->name);
836 strcpy(pos, scr->workspaces[i]->name);
837 pos += (curr_size+1);
838 len += (curr_size+1);
841 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
842 PropModeReplace, (unsigned char *)buf, len);
846 static void
847 updateFocusHint(WScreen *scr, WWindow *wwin) /* changeable */
849 Window window;
851 if (!scr->focused_window || !scr->focused_window->flags.focused)
852 window = None;
853 else
854 window = scr->focused_window->client_win;
856 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
857 PropModeReplace, (unsigned char*)&window, 1);
861 static void
862 updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
864 long l;
866 if (del) {
867 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
868 } else {
869 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
870 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
871 32, PropModeReplace, (unsigned char*)&l, 1);
876 static void
877 updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del) /* changeable */
879 if (del) {
880 if (!wwin->flags.net_state_from_client) {
881 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
883 } else {
884 Atom state[15]; /* nr of defined state atoms */
885 int i = 0;
887 if(changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
888 updateWorkspaceHint(wwin, False, False);
890 if (IS_OMNIPRESENT(wwin))
891 state[i++] = net_wm_state_sticky;
892 if (wwin->flags.shaded)
893 state[i++] = net_wm_state_shaded;
894 if (wwin->flags.maximized & MAX_HORIZONTAL)
895 state[i++] = net_wm_state_maximized_horz;
896 if (wwin->flags.maximized & MAX_VERTICAL)
897 state[i++] = net_wm_state_maximized_vert;
898 if (WFLAGP(wwin, skip_window_list))
899 state[i++] = net_wm_state_skip_taskbar;
900 if (wwin->flags.net_skip_pager)
901 state[i++] = net_wm_state_skip_pager;
902 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop){
903 state[i++] = net_wm_state_hidden;
904 state[i++] = net_wm_state_skip_pager;
906 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
907 if (!IS_OMNIPRESENT(wwin))
908 updateWorkspaceHint(wwin, True, False);
909 state[i++] = net_wm_state_sticky;
912 if (WFLAGP(wwin, sunken))
913 state[i++] = net_wm_state_below;
914 if (WFLAGP(wwin, floating))
915 state[i++] = net_wm_state_above;
916 if (wwin->flags.fullscreen)
917 state[i++] = net_wm_state_fullscreen;
919 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
920 PropModeReplace, (unsigned char *)state, i);
925 static Bool
926 updateStrut(WWindow *wwin, Bool adding)
928 WScreen *scr;
929 WReservedArea *area;
930 Bool hasState = False;
932 scr = wwin->screen_ptr;
934 if (adding) {
935 Atom type_ret;
936 int fmt_ret;
937 unsigned long nitems_ret;
938 unsigned long bytes_after_ret;
939 long *data = 0;
941 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
942 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
943 &bytes_after_ret,
944 (unsigned char**)&data)==Success && data) {
946 area = (WReservedArea *)wmalloc(sizeof(WReservedArea));
947 area->area.x1 = data[0];
948 area->area.x2 = data[1];
949 area->area.y1 = data[2];
950 area->area.y2 = data[3];
952 area->window = wwin->client_win;
954 area->next = scr->netdata->strut;
955 scr->netdata->strut = area;
957 XFree(data);
958 hasState = True;
959 #ifdef VIRTUAL_DESKTOP
960 /* just in case wm_window_type didn't set it already */
961 wwin->client_flags.virtual_stick = 1;
962 #endif
965 } else {
966 /* deleting */
967 area = scr->netdata->strut;
969 if(area) {
970 if(area->window == wwin->client_win) {
971 scr->netdata->strut = area->next;
972 wfree(area);
973 hasState = True;
974 } else {
975 while(area->next && area->next->window != wwin->client_win)
976 area = area->next;
978 if(area->next) {
979 WReservedArea *next;
981 next = area->next->next;
982 wfree(area->next);
983 area->next = next;
985 hasState = True;
991 return hasState;
995 static int
996 getWindowLayer(WWindow * wwin)
998 int layer = WMNormalLevel;
1000 if (wwin->type == net_wm_window_type_desktop) {
1001 layer = WMDesktopLevel;
1002 } else if (wwin->type == net_wm_window_type_dock) {
1003 layer = WMDockLevel;
1004 } else if (wwin->type == net_wm_window_type_toolbar) {
1005 layer = WMMainMenuLevel;
1006 } else if (wwin->type == net_wm_window_type_menu) {
1007 layer = WMSubmenuLevel;
1008 } else if (wwin->type == net_wm_window_type_utility) {
1009 } else if (wwin->type == net_wm_window_type_splash) {
1010 } else if (wwin->type == net_wm_window_type_dialog) {
1011 if (wwin->transient_for) {
1012 WWindow *parent = wWindowFor(wwin->transient_for);
1013 if (parent && parent->flags.fullscreen) {
1014 layer = WMFullscreenLevel;
1017 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
1018 } else if (wwin->type == net_wm_window_type_normal) {
1021 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
1022 layer = WMSunkenLevel;
1023 if (wwin->client_flags.floating && WMFloatingLevel > layer)
1024 layer = WMFloatingLevel;
1026 return layer;
1030 static void
1031 doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
1034 if (state == net_wm_state_sticky) {
1035 if (set == _NET_WM_STATE_TOGGLE) {
1036 set = !IS_OMNIPRESENT(wwin);
1038 if (set != wwin->flags.omnipresent) {
1039 wWindowSetOmnipresent(wwin, set);
1041 } else if (state == net_wm_state_shaded) {
1042 if (set == _NET_WM_STATE_TOGGLE) {
1043 set = !wwin->flags.shaded;
1045 if (init) {
1046 wwin->flags.shaded = set;
1047 } else {
1048 if (set) {
1049 wShadeWindow(wwin);
1050 } else {
1051 wUnshadeWindow(wwin);
1054 } else if (state == net_wm_state_skip_taskbar) {
1055 if (set == _NET_WM_STATE_TOGGLE) {
1056 set = !wwin->client_flags.skip_window_list;
1058 wwin->client_flags.skip_window_list = set;
1059 } else if (state == net_wm_state_skip_pager) {
1060 if (set == _NET_WM_STATE_TOGGLE) {
1061 set = !wwin->flags.net_skip_pager;
1063 wwin->flags.net_skip_pager = set;
1064 } else if (state == net_wm_state_maximized_vert) {
1065 if (set == _NET_WM_STATE_TOGGLE) {
1066 set = !(wwin->flags.maximized & MAX_VERTICAL);
1068 if (init) {
1069 wwin->flags.maximized |= (set ? MAX_VERTICAL : 0);
1070 } else {
1071 if (set) {
1072 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1073 } else {
1074 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1077 } else if (state == net_wm_state_maximized_horz) {
1078 if (set == _NET_WM_STATE_TOGGLE) {
1079 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1081 if (init) {
1082 wwin->flags.maximized |= (set ? MAX_HORIZONTAL : 0);
1083 } else {
1084 if (set) {
1085 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1086 } else {
1087 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1090 } else if (state == net_wm_state_hidden) {
1091 if (set == _NET_WM_STATE_TOGGLE) {
1092 set = !(wwin->flags.miniaturized);
1094 if (init) {
1095 wwin->flags.miniaturized = set;
1096 } else {
1097 if (set) {
1098 wIconifyWindow(wwin);
1099 } else {
1100 wDeiconifyWindow(wwin);
1103 } else if (state == net_wm_state_fullscreen) {
1104 if (set == _NET_WM_STATE_TOGGLE) {
1105 set = !(wwin->flags.fullscreen);
1107 if (init) {
1108 wwin->flags.fullscreen = set;
1109 } else {
1110 if (set) {
1111 wFullscreenWindow(wwin);
1112 } else {
1113 wUnfullscreenWindow(wwin);
1116 } else if (state == net_wm_state_above) {
1117 if (set == _NET_WM_STATE_TOGGLE) {
1118 set = !(wwin->client_flags.floating);
1120 if (init) {
1121 wwin->client_flags.floating = set;
1122 } else {
1123 wwin->client_flags.floating = set;
1124 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1127 } else if (state == net_wm_state_below) {
1128 if (set == _NET_WM_STATE_TOGGLE) {
1129 set = !(wwin->client_flags.sunken);
1131 if (init) {
1132 wwin->client_flags.sunken = set;
1133 } else {
1134 wwin->client_flags.sunken = set;
1135 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1138 } else {
1139 #ifdef DEBUG_WMSPEC
1140 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1141 #endif
1146 static void
1147 removeIcon( WWindow * wwin)
1149 if (wwin->icon == NULL)
1150 return;
1151 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1152 XUnmapWindow(dpy, wwin->icon->core->window);
1153 RemoveFromStackList(wwin->icon->core);
1154 wIconDestroy(wwin->icon);
1155 wwin->icon = NULL;
1160 static Bool
1161 handleWindowType(WWindow * wwin, Atom type, int *layer)
1163 Bool ret = True;
1165 if (type == net_wm_window_type_desktop) {
1166 wwin->client_flags.no_titlebar = 1;
1167 wwin->client_flags.no_resizable = 1;
1168 wwin->client_flags.no_miniaturizable = 1;
1169 wwin->client_flags.no_border = 1;
1170 wwin->client_flags.no_resizebar = 1;
1171 wwin->client_flags.no_shadeable = 1;
1172 wwin->client_flags.no_movable = 1;
1173 wwin->client_flags.omnipresent = 1;
1174 wwin->client_flags.skip_window_list = 1;
1175 wwin->client_flags.dont_move_off = 1;
1176 wwin->client_flags.no_appicon = 1;
1177 wwin->flags.net_skip_pager = 1;
1178 wwin->frame_x = 0;
1179 wwin->frame_y = 0;
1180 #ifdef VIRTUAL_DESKTOP
1181 wwin->client_flags.virtual_stick = 1;
1182 #endif
1183 } else if (type == net_wm_window_type_dock) {
1184 wwin->client_flags.no_titlebar = 1;
1185 wwin->client_flags.no_resizable = 1;
1186 wwin->client_flags.no_miniaturizable = 1;
1187 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1188 wwin->client_flags.no_resizebar = 1;
1189 wwin->client_flags.no_shadeable = 1;
1190 wwin->client_flags.no_movable = 1;
1191 wwin->client_flags.omnipresent = 1;
1192 wwin->client_flags.skip_window_list = 1;
1193 wwin->client_flags.dont_move_off = 1;
1194 wwin->flags.net_skip_pager = 1;
1195 #ifdef VIRTUAL_DESKTOP
1196 wwin->client_flags.virtual_stick = 1;
1197 #endif
1198 } else if (type == net_wm_window_type_toolbar) {
1199 wwin->client_flags.no_titlebar = 1;
1200 wwin->client_flags.no_resizable = 1;
1201 wwin->client_flags.no_miniaturizable = 1;
1202 wwin->client_flags.no_resizebar = 1;
1203 wwin->client_flags.no_shadeable = 1;
1204 wwin->client_flags.skip_window_list = 1;
1205 wwin->client_flags.dont_move_off = 1;
1206 wwin->client_flags.no_appicon = 1;
1207 } else if (type == net_wm_window_type_menu) {
1208 wwin->client_flags.no_titlebar = 1;
1209 wwin->client_flags.no_resizable = 1;
1210 wwin->client_flags.no_miniaturizable = 1;
1211 wwin->client_flags.no_resizebar = 1;
1212 wwin->client_flags.no_shadeable = 1;
1213 wwin->client_flags.skip_window_list = 1;
1214 wwin->client_flags.dont_move_off = 1;
1215 wwin->client_flags.no_appicon = 1;
1216 } else if (type == net_wm_window_type_utility) {
1217 wwin->client_flags.no_appicon = 1;
1218 } else if (type == net_wm_window_type_splash) {
1219 wwin->client_flags.no_titlebar = 1;
1220 wwin->client_flags.no_resizable = 1;
1221 wwin->client_flags.no_miniaturizable = 1;
1222 wwin->client_flags.no_resizebar = 1;
1223 wwin->client_flags.no_shadeable = 1;
1224 wwin->client_flags.no_movable = 1;
1225 wwin->client_flags.skip_window_list = 1;
1226 wwin->client_flags.dont_move_off = 1;
1227 wwin->client_flags.no_appicon = 1;
1228 wwin->flags.net_skip_pager = 1;
1229 } else if (type == net_wm_window_type_dialog) {
1230 /* These also seem a bad idea in our context -Dan
1231 // wwin->client_flags.skip_window_list = 1;
1232 // wwin->client_flags.no_appicon = 1;
1234 } else if (type == net_wm_window_type_normal) {
1235 } else {
1236 ret = False;
1239 wwin->type = type;
1240 *layer = getWindowLayer(wwin);
1242 return ret;
1246 void
1247 wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1249 if (wwin->type == net_wm_window_type_splash) {
1250 WScreen * scr = wwin->screen_ptr;
1251 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1252 *x = rect.pos.x + (rect.size.width - width)/2;
1253 *y = rect.pos.y + (rect.size.height - height)/2;
1258 static void
1259 updateWindowType(WWindow * wwin)
1261 Atom type_ret;
1262 int fmt_ret;
1263 unsigned long nitems_ret;
1264 unsigned long bytes_after_ret;
1265 long *data = 0;
1266 int layer;
1267 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1268 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1269 &bytes_after_ret, (unsigned char **)&data)==Success
1270 && data) {
1272 int i;
1273 Atom * type = (Atom *)data;
1274 for (i=0; i<nitems_ret; ++i) {
1275 if (handleWindowType(wwin, type[i], &layer)) break;
1277 XFree(data);
1280 ChangeStackingLevel(wwin->frame->core, layer);
1281 wwin->frame->flags.need_texture_change = 1;
1282 wWindowConfigureBorders(wwin);
1283 wFrameWindowPaint(wwin->frame);
1284 wNETWMUpdateActions(wwin, False);
1288 Bool
1289 wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1291 Atom type_ret;
1292 int fmt_ret;
1293 unsigned long nitems_ret;
1294 unsigned long bytes_after_ret;
1295 long *data = 0;
1296 Bool hasState = False;
1297 int i;
1299 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1300 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1301 &bytes_after_ret,
1302 (unsigned char**)&data)==Success && data) {
1304 long desktop = *data;
1305 XFree(data);
1307 if(desktop == -1)
1308 wwin->client_flags.omnipresent = 1;
1309 else
1310 *workspace = desktop;
1312 hasState = True;
1315 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1316 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1317 &bytes_after_ret,
1318 (unsigned char**)&data)==Success && data) {
1320 Atom * state = (Atom *)data;
1321 for(i=0; i<nitems_ret; ++i) {
1322 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1324 XFree(data);
1325 hasState = True;
1328 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1329 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1330 &bytes_after_ret,
1331 (unsigned char **)&data) == Success && data) {
1333 Atom * type = (Atom *)data;
1334 for (i=0; i<nitems_ret; ++i) {
1335 if (handleWindowType(wwin, type[i], layer)) break;
1337 XFree(data);
1338 hasState = True;
1341 wNETWMUpdateActions(wwin, False);
1342 updateStrut(wwin, False);
1343 if (updateStrut(wwin, True)) {
1344 hasState = True;
1346 wScreenUpdateUsableArea(wwin->screen_ptr);
1348 return hasState;
1352 static Bool
1353 updateNetIconInfo(WWindow *wwin) {
1355 Atom type_ret;
1356 int fmt_ret;
1357 unsigned long nitems_ret;
1358 unsigned long bytes_after_ret;
1359 long *data = 0;
1360 Bool hasState = False;
1361 Bool old_state = wwin->flags.net_handle_icon;
1363 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1364 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1365 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1366 long handled = *data;
1367 wwin->flags.net_handle_icon = (handled != 0);
1368 XFree(data);
1369 hasState = True;
1371 } else wwin->flags.net_handle_icon = False;
1373 if ( XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1374 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1375 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1377 #ifdef NETWM_PROPER
1378 if (wwin->flags.net_handle_icon)
1379 #else
1380 wwin->flags.net_handle_icon = True;
1381 #endif
1383 wwin->icon_x = data[0];
1384 wwin->icon_y = data[1];
1385 wwin->icon_w = data[2];
1386 wwin->icon_h = data[3];
1389 XFree(data);
1390 hasState = True;
1392 } else wwin->flags.net_handle_icon = False;
1394 if (wwin->flags.miniaturized &&
1395 old_state != wwin->flags.net_handle_icon) {
1396 if (wwin->flags.net_handle_icon) {
1397 removeIcon(wwin);
1398 } else {
1399 wwin->flags.miniaturized = False;
1400 wwin->flags.skip_next_animation = True;
1401 wIconifyWindow(wwin);
1405 return hasState;
1409 Bool
1410 wNETWMCheckInitialClientState(WWindow *wwin)
1412 Bool hasState = False;
1414 #ifdef DEBUG_WMSPEC
1415 printf("CheckInitialClientState\n");
1416 #endif
1418 wNETWMShowingDesktop(wwin->screen_ptr, False);
1420 hasState |= updateNetIconInfo(wwin);
1422 updateIconImage(wwin->screen_ptr, wwin);
1424 return hasState;
1428 static void
1429 handleDesktopNames(XClientMessageEvent *event, WScreen *scr)
1431 unsigned long nitems_ret, bytes_after_ret;
1432 char *data, *names[32];
1433 int fmt_ret, i, n;
1434 Atom type_ret;
1436 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1437 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1438 &bytes_after_ret,
1439 (unsigned char**)&data)!=Success) {
1440 return;
1443 if (data == 0)
1444 return;
1446 if (type_ret != utf8_string || fmt_ret != 8)
1447 return;
1449 n = 0;
1450 names[n] = data;
1451 for (i = 0; i < nitems_ret; i++) {
1452 if (data[i] == 0) {
1453 n++;
1454 names[n] = &data[i];
1455 } else if (*names[n] == 0) {
1456 names[n] = &data[i];
1457 wWorkspaceRename(scr, n, names[n]);
1463 Bool
1464 wNETWMProcessClientMessage(XClientMessageEvent *event)
1466 WScreen *scr;
1467 WWindow *wwin;
1468 Bool done = True;
1470 #ifdef DEBUG_WMSPEC
1471 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1472 #endif
1474 scr = wScreenForWindow(event->window);
1475 if (scr) {
1476 /* generic client messages */
1477 if (event->message_type == net_current_desktop) {
1478 wWorkspaceChange(scr, event->data.l[0]);
1479 } else if(event->message_type == net_number_of_desktops) {
1480 long value;
1482 value = event->data.l[0];
1483 if(value > scr->workspace_count) {
1484 wWorkspaceMake(scr, value - scr->workspace_count);
1485 } else if(value < scr->workspace_count) {
1486 int i;
1487 Bool rebuild = False;
1489 for (i = scr->workspace_count-1; i >= value; i--) {
1490 if (!wWorkspaceDelete(scr, i)) {
1491 rebuild = True;
1492 break;
1496 if(rebuild) {
1497 updateWorkspaceCount(scr);
1500 } else if (event->message_type == net_showing_desktop) {
1501 wNETWMShowingDesktop(scr, event->data.l[0]);
1502 #ifdef VIRTUAL_DESKTOP
1503 } else if (event->message_type == net_desktop_viewport) {
1504 wWorkspaceSetViewport(scr, scr->current_workspace,
1505 event->data.l[0], event->data.l[1]);
1506 #endif
1507 } else if (event->message_type == net_desktop_names) {
1508 handleDesktopNames(event, scr);
1509 } else {
1510 done = False;
1513 if (done)
1514 return True;
1517 /* window specific client messages */
1519 wwin = wWindowFor(event->window);
1520 if (!wwin)
1521 return False;
1523 if (event->message_type == net_active_window) {
1524 wNETWMShowingDesktop(scr, False);
1525 wMakeWindowVisible(wwin);
1526 } else if (event->message_type == net_close_window) {
1527 if (!WFLAGP(wwin, no_closable)) {
1528 if (wwin->protocols.DELETE_WINDOW)
1529 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1531 } else if (event->message_type == net_wm_state) {
1532 int maximized = wwin->flags.maximized;
1533 long set = event->data.l[0];
1535 #ifdef DEBUG_WMSPEC
1536 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1537 XGetAtomName(dpy, event->data.l[1]),
1538 XGetAtomName(dpy, event->data.l[2]));
1539 #endif
1541 doStateAtom(wwin, (Atom)event->data.l[1], set, False);
1542 if(event->data.l[2])
1543 doStateAtom(wwin, (Atom)event->data.l[2], set, False);
1545 if(wwin->flags.maximized != maximized) {
1546 if(!wwin->flags.maximized) {
1547 wwin->flags.maximized = maximized;
1548 wUnmaximizeWindow(wwin);
1549 } else {
1550 wMaximizeWindow(wwin, wwin->flags.maximized);
1553 updateStateHint(wwin, False, False);
1554 } else if (event->message_type == net_wm_handled_icons ||
1555 event->message_type == net_wm_icon_geometry) {
1556 updateNetIconInfo(wwin);
1557 } else if (event->message_type == net_wm_desktop) {
1558 long desktop = event->data.l[0];
1559 if (desktop == -1) {
1560 wWindowSetOmnipresent(wwin, True);
1561 } else {
1562 if (IS_OMNIPRESENT(wwin))
1563 wWindowSetOmnipresent(wwin, False);
1564 wWindowChangeWorkspace(wwin, desktop);
1566 } else if (event->message_type == net_wm_icon) {
1567 updateIconImage(scr, wwin);
1568 } else {
1569 done = False;
1572 return done;
1576 Bool
1577 wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1579 Bool ret = True;
1581 if(event->atom == net_wm_strut) {
1582 updateStrut(wwin, False);
1583 updateStrut(wwin, True);
1584 wScreenUpdateUsableArea(wwin->screen_ptr);
1585 } else if (event->atom == net_wm_handled_icons ||
1586 event->atom == net_wm_icon_geometry) {
1587 updateNetIconInfo(wwin);
1588 } else if (event->atom == net_wm_window_type) {
1589 updateWindowType(wwin);
1590 } else if (event->atom == net_wm_name) {
1591 char *name= wNETWMGetWindowName(wwin->client_win);
1592 wWindowUpdateName(wwin, name);
1593 if (name) wfree(name);
1594 } else if (event->atom == net_wm_icon_name) {
1595 if (wwin->icon) {
1596 char *name= wNETWMGetIconName(wwin->client_win);
1597 wIconChangeTitle(wwin->icon, name);
1599 } else {
1600 ret = False;
1603 return ret;
1608 wNETWMGetPidForWindow(Window window)
1610 Atom type_ret;
1611 int fmt_ret;
1612 unsigned long nitems_ret;
1613 unsigned long bytes_after_ret;
1614 long *data = 0;
1615 int pid;
1617 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1618 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1619 &bytes_after_ret,
1620 (unsigned char**)&data)==Success && data) {
1622 pid = *data;
1623 XFree(data);
1624 } else {
1625 pid = 0;
1628 return pid;
1632 char*
1633 wNETWMGetWindowName(Window window)
1635 char *name;
1636 char *ret;
1637 int size;
1639 name= (char*)PropGetCheckProperty(window,
1640 net_wm_name, utf8_string, 0,
1641 0, &size);
1642 if (name) {
1643 ret= wstrndup(name, size);
1644 XFree(name);
1646 else
1647 ret= NULL;
1648 return ret;
1652 char*
1653 wNETWMGetIconName(Window window)
1655 char *name;
1656 char *ret;
1657 int size;
1659 name= (char*)PropGetCheckProperty(window,
1660 net_wm_icon_name, utf8_string, 0,
1661 0, &size);
1662 if (name) {
1663 ret= wstrndup(name, size);
1664 XFree(name);
1666 else
1667 ret= NULL;
1668 return ret;
1672 static void
1673 observer(void *self, WMNotification *notif)
1675 WWindow *wwin = (WWindow*)WMGetNotificationObject(notif);
1676 const char *name = WMGetNotificationName(notif);
1677 void *data = WMGetNotificationClientData(notif);
1678 NetData *ndata = (NetData*)self;
1681 if (strcmp(name, WMNManaged) == 0 && wwin) {
1682 updateClientList(wwin->screen_ptr);
1683 updateClientListStacking(wwin->screen_ptr, NULL);
1684 updateStateHint(wwin, True, False);
1686 updateStrut(wwin, False);
1687 updateStrut(wwin, True);
1688 wScreenUpdateUsableArea(wwin->screen_ptr);
1689 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1690 updateClientList(wwin->screen_ptr);
1691 updateClientListStacking(wwin->screen_ptr, wwin);
1692 updateWorkspaceHint(wwin, False, True);
1693 updateStateHint(wwin, False, True);
1694 wNETWMUpdateActions(wwin, True);
1696 updateStrut(wwin, False);
1697 wScreenUpdateUsableArea(wwin->screen_ptr);
1698 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1699 updateClientListStacking(wwin->screen_ptr, NULL);
1700 updateStateHint(wwin, False, False);
1701 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1702 updateClientListStacking(wwin->screen_ptr, NULL);
1703 updateStateHint(wwin, False, False);
1704 } else if (strcmp(name, WMNChangedFocus) == 0) {
1705 updateFocusHint(ndata->scr, wwin);
1706 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1707 updateWorkspaceHint(wwin, False, False);
1708 updateStateHint(wwin, True, False);
1709 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1710 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1715 static void
1716 wsobserver(void *self, WMNotification *notif)
1718 WScreen *scr = (WScreen*)WMGetNotificationObject(notif);
1719 const char *name = WMGetNotificationName(notif);
1721 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1722 updateWorkspaceCount(scr);
1723 updateWorkspaceNames(scr);
1724 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1725 updateWorkspaceCount(scr);
1726 updateWorkspaceNames(scr);
1727 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1728 updateCurrentWorkspace(scr);
1729 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1730 updateWorkspaceNames(scr);
1734 #endif /* NETWM_HINTS */