- better checks if the icon sizes are valid and there is enough data to
[wmaker-crm.git] / src / wmspec.c
blob09a838b58fada41170d289441a66d07210bbe152
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 <string.h>
40 #include "WindowMaker.h"
41 #include "window.h"
42 #include "screen.h"
43 #include "workspace.h"
44 #include "framewin.h"
45 #include "actions.h"
46 #include "client.h"
47 #include "wmspec.h"
48 #include "icon.h"
49 #include "stacking.h"
50 #include "xinerama.h"
51 #include "properties.h"
54 #ifdef DEBUG_WMSPEC
55 #include <stdio.h>
56 #endif
58 /* Global variables */
59 extern Atom _XA_WM_DELETE_WINDOW;
60 extern Time LastTimestamp;
61 extern WPreferences wPreferences;
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;
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 */
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 */
129 /* Window Manager Protocols */
130 static Atom net_wm_ping; /* TODO */
132 static Atom utf8_string;
134 typedef struct {
135 char * name;
136 Atom * atom;
137 } atomitem_t;
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 },
155 { "_NET_CLOSE_WINDOW", &net_close_window },
156 { "_NET_MOVERESIZE_WINDOW", &net_moveresize_window },
157 { "_NET_WM_MOVERESIZE", &net_wm_moveresize },
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 },
203 { "_NET_WM_PING", &net_wm_ping },
205 { "UTF8_STRING", &utf8_string },
208 #define atomNr (sizeof(atomNames)/sizeof(atomitem_t))
210 #define _NET_WM_STATE_REMOVE 0
211 #define _NET_WM_STATE_ADD 1
212 #define _NET_WM_STATE_TOGGLE 2
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 */
226 static void observer(void *self, WMNotification *notif);
227 static void wsobserver(void *self, WMNotification *notif);
229 static void updateClientList(WScreen *scr);
230 static void updateClientListStacking(WScreen *scr, WWindow *);
232 static void updateWorkspaceNames(WScreen *scr);
233 static void updateCurrentWorkspace(WScreen *scr);
234 static void updateWorkspaceCount(WScreen *scr);
236 typedef struct NetData {
237 WScreen *scr;
238 WReservedArea *strut;
239 WWindow **show_desktop;
240 } NetData;
243 static void
244 setSupportedHints(WScreen *scr)
246 Atom atom[atomNr];
247 int i = 0;
249 /* set supported hints list */
250 /* XXX: extend this !!! */
252 atom[i++] = net_client_list;
253 atom[i++] = net_client_list_stacking;
254 atom[i++] = net_number_of_desktops;
255 atom[i++] = net_desktop_geometry;
256 atom[i++] = net_desktop_viewport;
257 atom[i++] = net_current_desktop;
258 atom[i++] = net_desktop_names;
259 atom[i++] = net_active_window;
260 atom[i++] = net_workarea;
261 atom[i++] = net_supporting_wm_check;
262 atom[i++] = net_showing_desktop;
263 #if 0
264 atom[i++] = net_wm_moveresize;
265 #endif
266 atom[i++] = net_wm_desktop;
267 atom[i++] = net_wm_window_type;
268 atom[i++] = net_wm_window_type_desktop;
269 atom[i++] = net_wm_window_type_dock;
270 atom[i++] = net_wm_window_type_toolbar;
271 atom[i++] = net_wm_window_type_menu;
272 atom[i++] = net_wm_window_type_utility;
273 atom[i++] = net_wm_window_type_splash;
274 atom[i++] = net_wm_window_type_dialog;
275 atom[i++] = net_wm_window_type_normal;
277 atom[i++] = net_wm_state;
278 /* atom[i++] = net_wm_state_modal; */ /* XXX: not sure where/when to use it. */
279 atom[i++] = net_wm_state_sticky;
280 atom[i++] = net_wm_state_shaded;
281 atom[i++] = net_wm_state_maximized_horz;
282 atom[i++] = net_wm_state_maximized_vert;
283 atom[i++] = net_wm_state_skip_taskbar;
284 atom[i++] = net_wm_state_skip_pager;
285 atom[i++] = net_wm_state_hidden;
286 atom[i++] = net_wm_state_fullscreen;
287 atom[i++] = net_wm_state_above;
288 atom[i++] = net_wm_state_below;
290 atom[i++] = net_wm_allowed_actions;
291 atom[i++] = net_wm_action_move;
292 atom[i++] = net_wm_action_resize;
293 atom[i++] = net_wm_action_minimize;
294 atom[i++] = net_wm_action_shade;
295 atom[i++] = net_wm_action_stick;
296 atom[i++] = net_wm_action_maximize_horz;
297 atom[i++] = net_wm_action_maximize_vert;
298 atom[i++] = net_wm_action_fullscreen;
299 atom[i++] = net_wm_action_change_desktop;
300 atom[i++] = net_wm_action_close;
302 atom[i++] = net_wm_strut;
303 atom[i++] = net_wm_icon_geometry;
304 atom[i++] = net_wm_icon;
305 atom[i++] = net_wm_handled_icons;
307 atom[i++] = net_wm_name;
308 atom[i++] = net_wm_icon_name;
310 XChangeProperty(dpy, scr->root_win, net_supported, XA_ATOM, 32,
311 PropModeReplace, (unsigned char*)atom, i);
313 /* set supporting wm hint */
314 XChangeProperty(dpy, scr->root_win, net_supporting_wm_check, XA_WINDOW, 32,
315 PropModeReplace, (unsigned char*)&scr->info_window, 1);
317 XChangeProperty(dpy, scr->info_window, net_supporting_wm_check, XA_WINDOW,
318 32, PropModeReplace, (unsigned char*)&scr->info_window, 1);
322 void
323 wNETWMUpdateDesktop(WScreen *scr)
325 CARD32 *views, sizes[2];
326 int count, i;
328 if (scr->workspace_count==0)
329 return;
331 count = scr->workspace_count * 2;
332 views = wmalloc(sizeof(CARD32) * count);
333 /*memset(views, 0, sizeof(CARD32) * count);*/
335 #ifdef VIRTUAL_DESKTOP
336 sizes[0] = scr->workspaces[scr->current_workspace]->width;
337 sizes[1] = scr->workspaces[scr->current_workspace]->height;
338 #else
339 sizes[0] = scr->scr_width;
340 sizes[1] = scr->scr_height;
341 #endif
343 for (i=0; i<scr->workspace_count; i++) {
344 #ifdef VIRTUAL_DESKTOP
345 views[2*i + 0] = scr->workspaces[i]->view_x;
346 views[2*i + 1] = scr->workspaces[i]->view_y;
347 #else
348 views[2*i + 0] = 0;
349 views[2*i + 1] = 0;
350 #endif
353 XChangeProperty(dpy, scr->root_win, net_desktop_geometry, XA_CARDINAL, 32,
354 PropModeReplace, (unsigned char*)sizes, 2);
356 XChangeProperty(dpy, scr->root_win, net_desktop_viewport, XA_CARDINAL, 32,
357 PropModeReplace, (unsigned char*)views, count);
359 wfree(views);
364 wNETWMGetCurrentDesktopFromHint(WScreen *scr)
366 int count;
367 unsigned char *prop;
369 prop= PropGetCheckProperty(scr->root_win, net_current_desktop, XA_CARDINAL,
370 0, 1, &count);
371 if (prop)
373 int desktop= *(CARD32*)prop;
374 XFree(prop);
375 return desktop;
377 return -1;
382 * Find the best icon to be used by Window Maker for appicon/miniwindows.
383 * Currently the algorithm is to take the image with the size closest
384 * to icon_size x icon_size, but never bigger than that.
386 * This algorithm is very poorly implemented and needs to be redone (it can
387 * easily select images with very large widths and very small heights over
388 * square images, if the area of the former is closer to the desired one).
390 * The logic can also be changed to accept bigger images and scale them down.
392 static CARD32*
393 findBestIcon(CARD32 *data, unsigned long items)
395 int size, wanted, d, distance;
396 unsigned long i;
397 CARD32 *icon;
399 /* better use only 75% of icon_size. For 64x64 this means 48x48
400 * This leaves room around the icon for the miniwindow title and
401 * results in better overall aesthetics -Dan */
402 wanted = wPreferences.icon_size * wPreferences.icon_size;
404 for (icon=NULL, distance=LONG_MAX, i=0L; i<items-1; ) {
405 size = data[i] * data[i+1];
406 if (size==0)
407 break;
408 d = wanted - size;
409 if (d>=0 && d<=distance && (i+size+2)<=items) {
410 distance = d;
411 icon = &data[i];
413 i += size+2;
416 return icon;
420 static RImage*
421 makeRImageFromARGBData(CARD32 *data)
423 int size, width, height, i;
424 RImage *image;
425 unsigned char *imgdata;
426 CARD32 pixel;
428 width = data[0];
429 height = data[1];
430 size = width * height;
432 if (size == 0)
433 return NULL;
435 image = RCreateImage(width, height, True);
437 for (imgdata=image->data, i=2; i<size+2; i++, imgdata+=4) {
438 pixel = data[i];
439 imgdata[3] = (pixel >> 24) & 0xff; /* A */
440 imgdata[0] = (pixel >> 16) & 0xff; /* R */
441 imgdata[1] = (pixel >> 8) & 0xff; /* G */
442 imgdata[2] = (pixel >> 0) & 0xff; /* B */
445 return image;
449 static void
450 updateIconImage(WScreen *scr, WWindow *wwin)
452 CARD32 *property, *data;
453 unsigned long items, rest;
454 Atom type;
455 int format;
456 RImage *image;
458 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_icon, 0L, LONG_MAX,
459 False, XA_CARDINAL, &type, &format, &items, &rest,
460 (unsigned char**)&property)!=Success || !property) {
461 return;
464 if (type!=XA_CARDINAL || format!=32 || items<2) {
465 XFree(property);
466 return;
469 data = findBestIcon(property, items);
470 if (!data) {
471 XFree(property);
472 return;
475 image = makeRImageFromARGBData(data);
477 if (image) {
478 if (wwin->net_icon_image)
479 RReleaseImage(wwin->net_icon_image);
480 wwin->net_icon_image = image;
483 XFree(property);
487 static void
488 updateShowDesktop(WScreen * scr, Bool show)
490 CARD32 foo;
492 foo = (show == True);
493 XChangeProperty(dpy, scr->root_win, net_showing_desktop, XA_CARDINAL, 32,
494 PropModeReplace, (unsigned char *)&foo, 1);
498 void
499 wNETWMShowingDesktop(WScreen *scr, Bool show)
501 if (show && scr->netdata->show_desktop == NULL) {
502 WWindow *tmp, **wins;
503 int i=0;
505 wins = (WWindow **)wmalloc(sizeof(WWindow *)*(scr->window_count+1));
507 tmp = scr->focused_window;
508 while (tmp) {
509 if (!tmp->flags.hidden && !tmp->flags.miniaturized &&
510 !WFLAGP(tmp, skip_window_list)) {
512 wins[i++] = tmp;
513 tmp->flags.skip_next_animation = 1;
514 tmp->flags.net_show_desktop = 1;
515 wIconifyWindow(tmp);
518 tmp = tmp->prev;
520 wins[i++] = NULL;
522 scr->netdata->show_desktop = wins;
523 updateShowDesktop(scr, True);
524 } else if (scr->netdata->show_desktop != NULL) {
525 /* FIXME: get rid of workspace flashing ! */
526 int ws = scr->current_workspace;
527 WWindow **tmp;
528 for (tmp = scr->netdata->show_desktop; *tmp; ++tmp) {
529 wDeiconifyWindow(*tmp);
530 (*tmp)->flags.net_show_desktop = 0;
532 if (ws != scr->current_workspace)
533 wWorkspaceChange(scr, ws);
534 wfree(scr->netdata->show_desktop);
535 scr->netdata->show_desktop = NULL;
536 updateShowDesktop(scr, False);
541 void
542 wNETWMInitStuff(WScreen *scr)
544 NetData *data;
545 int i;
547 #ifdef DEBUG_WMSPEC
548 printf( "wNETWMInitStuff\n");
549 #endif
551 #ifdef HAVE_XINTERNATOMS
553 Atom atoms[atomNr];
554 char * names[atomNr];
556 for (i = 0; i < atomNr; ++i) {
557 names[i] = atomNames[i].name;
559 XInternAtoms(dpy, &names[0], atomNr, False, atoms);
560 for (i = 0; i < atomNr; ++i) {
561 *atomNames[i].atom = atoms[i];
564 #else
565 for (i = 0; i < atomNr; i++) {
566 *atomNames[i].atom = XInternAtom(dpy, atomNames[i].name, False);
568 #endif
570 data = wmalloc(sizeof(NetData));
571 data->scr = scr;
572 data->strut = NULL;
573 data->show_desktop = NULL;
575 scr->netdata = data;
577 setSupportedHints(scr);
579 WMAddNotificationObserver(observer, data, WMNManaged, NULL);
580 WMAddNotificationObserver(observer, data, WMNUnmanaged, NULL);
581 WMAddNotificationObserver(observer, data, WMNChangedWorkspace, NULL);
582 WMAddNotificationObserver(observer, data, WMNChangedState, NULL);
583 WMAddNotificationObserver(observer, data, WMNChangedFocus, NULL);
584 WMAddNotificationObserver(observer, data, WMNChangedStacking, NULL);
585 WMAddNotificationObserver(observer, data, WMNChangedName, NULL);
587 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceCreated, NULL);
588 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceDestroyed, NULL);
589 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceChanged, NULL);
590 WMAddNotificationObserver(wsobserver, data, WMNWorkspaceNameChanged, NULL);
592 updateClientList(scr);
593 updateClientListStacking(scr, NULL);
594 updateWorkspaceCount(scr);
595 updateWorkspaceNames(scr);
596 updateShowDesktop(scr, False);
598 wScreenUpdateUsableArea(scr);
602 void
603 wNETWMCleanup(WScreen *scr)
605 int i;
607 for (i= 0; i < atomNr; i++)
608 XDeleteProperty(dpy, scr->root_win, *atomNames[i].atom);
612 void
613 wNETWMUpdateActions(WWindow *wwin, Bool del)
615 Atom action[10]; /* nr of actions atoms defined */
616 int i = 0;
618 if (del) {
619 XDeleteProperty(dpy, wwin->client_win, net_wm_allowed_actions);
620 return;
623 if (IS_MOVABLE(wwin))
624 action[i++] = net_wm_action_move;
626 if (IS_RESIZABLE(wwin))
627 action[i++] = net_wm_action_resize;
629 if (!WFLAGP(wwin, no_miniaturizable))
630 action[i++] = net_wm_action_minimize;
632 if (!WFLAGP(wwin, no_shadeable))
633 action[i++] = net_wm_action_shade;
635 /* if (!WFLAGP(wwin, no_stickable)) */
636 action[i++] = net_wm_action_stick;
638 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_HORIZONTAL)) */
639 if (IS_RESIZABLE(wwin))
640 action[i++] = net_wm_action_maximize_horz;
642 /* if (!(WFLAGP(wwin, no_maximizeable) & MAX_VERTICAL)) */
643 if (IS_RESIZABLE(wwin))
644 action[i++] = net_wm_action_maximize_vert;
646 /* if (!WFLAGP(wwin, no_fullscreen)) */
647 action[i++] = net_wm_action_fullscreen;
649 /* if (!WFLAGP(wwin, no_change_desktop)) */
650 action[i++] = net_wm_action_change_desktop;
652 if (!WFLAGP(wwin, no_closable))
653 action[i++] = net_wm_action_close;
655 XChangeProperty(dpy, wwin->client_win, net_wm_allowed_actions,
656 XA_ATOM, 32, PropModeReplace, (unsigned char *)action, i);
660 void
661 wNETWMUpdateWorkarea(WScreen *scr, WArea usableArea)
663 CARD32 *area;
664 int count, i;
666 /* XXX: not Xinerama compatible,
667 xinerama gets the largest available */
669 if(!scr->netdata || scr->workspace_count==0)
670 return;
672 count = scr->workspace_count * 4;
673 area = wmalloc(sizeof(CARD32) * count);
674 for (i=0; i<scr->workspace_count; i++) {
675 area[4*i + 0] = usableArea.x1;
676 area[4*i + 1] = usableArea.y1;
677 area[4*i + 2] = usableArea.x2 - usableArea.x1;
678 area[4*i + 3] = usableArea.y2 - usableArea.y1;
681 XChangeProperty(dpy, scr->root_win, net_workarea, XA_CARDINAL, 32,
682 PropModeReplace, (unsigned char*)area, count);
684 wfree(area);
688 Bool
689 wNETWMGetUsableArea(WScreen *scr, int head, WArea *area)
691 WReservedArea *cur;
692 WMRect rect;
694 if(!scr->netdata || !scr->netdata->strut)
695 return False;
697 area->x1 = area->y1 = area->x2 = area->y2 = 0;
699 for(cur = scr->netdata->strut ; cur ; cur = cur->next) {
700 WWindow * wwin = wWindowFor(cur->window);
701 if (wWindowTouchesHead(wwin, head)) {
702 if(cur->area.x1 > area->x1)
703 area->x1 = cur->area.x1;
704 if(cur->area.y1 > area->y1)
705 area->y1 = cur->area.y1;
706 if(cur->area.x2 > area->x2)
707 area->x2 = cur->area.x2;
708 if(cur->area.y2 > area->y2)
709 area->y2 = cur->area.y2;
713 if (area->x1==0 && area->x2==0 &&
714 area->y1==0 && area->y2==0) return False;
716 rect = wGetRectForHead(scr, head);
718 area->x1 = rect.pos.x + area->x1;
719 area->x2 = rect.pos.x + rect.size.width - area->x2;
720 area->y1 = rect.pos.y + area->y1;
721 area->y2 = rect.pos.y + rect.size.height - area->y2;
723 return True;
727 static void
728 updateClientList(WScreen *scr)
730 WWindow *wwin;
731 Window *windows;
732 int count;
734 windows = (Window *)wmalloc(sizeof(Window)*(scr->window_count+1));
736 count = 0;
737 wwin = scr->focused_window;
738 while (wwin) {
739 windows[count++] = wwin->client_win;
740 wwin = wwin->prev;
742 XChangeProperty(dpy, scr->root_win, net_client_list, XA_WINDOW, 32,
743 PropModeReplace, (unsigned char*)windows, count);
745 wfree(windows);
746 XFlush(dpy);
750 static void
751 updateClientListStacking(WScreen *scr, WWindow *wwin_excl)
753 WWindow *wwin;
754 Window *client_list;
755 Window *client_list_reverse;
756 int client_count;
757 WCoreWindow *tmp;
758 WMBagIterator iter;
759 int i;
761 /* update client list */
762 i = scr->window_count + 1;
763 client_list = (Window*)wmalloc(sizeof(Window) * i);
764 client_list_reverse = (Window*)wmalloc(sizeof(Window) * i);
766 client_count = 0;
767 WM_ETARETI_BAG(scr->stacking_list, tmp, iter) {
768 while (tmp) {
769 wwin = wWindowFor(tmp->window);
770 /* wwin_excl is a window to exclude from the list
771 (e.g. it's now unmanaged) */
772 if(wwin && (wwin != wwin_excl))
773 client_list[client_count++] = wwin->client_win;
774 tmp = tmp->stacking->under;
778 for(i = 0; i < client_count; i++) {
779 Window w = client_list[client_count - i - 1];
780 client_list_reverse[i] = w;
783 XChangeProperty(dpy, scr->root_win, net_client_list_stacking, XA_WINDOW, 32,
784 PropModeReplace, (unsigned char *)client_list_reverse, client_count);
786 wfree(client_list);
787 wfree(client_list_reverse);
789 XFlush(dpy);
793 static void
794 updateWorkspaceCount(WScreen *scr) /* changeable */
796 CARD32 count;
798 count = scr->workspace_count;
800 XChangeProperty(dpy, scr->root_win, net_number_of_desktops, XA_CARDINAL,
801 32, PropModeReplace, (unsigned char*)&count, 1);
805 static void
806 updateCurrentWorkspace(WScreen *scr) /* changeable */
808 CARD32 count;
810 count = scr->current_workspace;
812 XChangeProperty(dpy, scr->root_win, net_current_desktop, XA_CARDINAL, 32,
813 PropModeReplace, (unsigned char*)&count, 1);
817 static void
818 updateWorkspaceNames(WScreen *scr)
820 char buf[1024], *pos;
821 unsigned int i, len, curr_size;
823 pos = buf;
824 len = 0;
825 for(i = 0; i < scr->workspace_count; i++) {
826 curr_size = strlen(scr->workspaces[i]->name);
827 strcpy(pos, scr->workspaces[i]->name);
828 pos += (curr_size+1);
829 len += (curr_size+1);
832 XChangeProperty(dpy, scr->root_win, net_desktop_names, utf8_string, 8,
833 PropModeReplace, (unsigned char *)buf, len);
837 static void
838 updateFocusHint(WScreen *scr, WWindow *wwin) /* changeable */
840 Window window;
842 if (!scr->focused_window || !scr->focused_window->flags.focused)
843 window = None;
844 else
845 window = scr->focused_window->client_win;
847 XChangeProperty(dpy, scr->root_win, net_active_window, XA_WINDOW, 32,
848 PropModeReplace, (unsigned char*)&window, 1);
852 static void
853 updateWorkspaceHint(WWindow *wwin, Bool fake, Bool del)
855 CARD32 l;
857 if (del) {
858 XDeleteProperty(dpy, wwin->client_win, net_wm_desktop);
859 } else {
860 l = ((fake || IS_OMNIPRESENT(wwin)) ? -1 : wwin->frame->workspace);
861 XChangeProperty(dpy, wwin->client_win, net_wm_desktop, XA_CARDINAL,
862 32, PropModeReplace, (unsigned char*)&l, 1);
867 static void
868 updateStateHint(WWindow *wwin, Bool changedWorkspace, Bool del) /* changeable */
870 if (del) {
871 if (!wwin->flags.net_state_from_client) {
872 XDeleteProperty(dpy, wwin->client_win, net_wm_state);
874 } else {
875 Atom state[11]; /* nr of defined state atoms */
876 int i = 0;
878 if(changedWorkspace || (wPreferences.sticky_icons && !IS_OMNIPRESENT(wwin)))
879 updateWorkspaceHint(wwin, False, False);
881 if (IS_OMNIPRESENT(wwin))
882 state[i++] = net_wm_state_sticky;
883 if (wwin->flags.shaded)
884 state[i++] = net_wm_state_shaded;
885 if (wwin->flags.maximized & MAX_HORIZONTAL)
886 state[i++] = net_wm_state_maximized_horz;
887 if (wwin->flags.maximized & MAX_VERTICAL)
888 state[i++] = net_wm_state_maximized_vert;
889 if (WFLAGP(wwin, skip_window_list))
890 state[i++] = net_wm_state_skip_taskbar;
891 if (wwin->flags.net_skip_pager)
892 state[i++] = net_wm_state_skip_pager;
893 if ((wwin->flags.hidden || wwin->flags.miniaturized) && !wwin->flags.net_show_desktop){
894 state[i++] = net_wm_state_hidden;
895 state[i++] = net_wm_state_skip_pager;
897 if (wwin->flags.miniaturized && wPreferences.sticky_icons) {
898 if( !IS_OMNIPRESENT(wwin))
899 updateWorkspaceHint(wwin, True, False);
900 state[i++] = net_wm_state_sticky;
903 if (WFLAGP(wwin, sunken))
904 state[i++] = net_wm_state_below;
905 if (WFLAGP(wwin, floating))
906 state[i++] = net_wm_state_above;
907 if (wwin->flags.fullscreen)
908 state[i++] = net_wm_state_fullscreen;
910 XChangeProperty(dpy, wwin->client_win, net_wm_state, XA_ATOM, 32,
911 PropModeReplace, (unsigned char *)state, i);
916 static Bool
917 updateStrut(WWindow *wwin, Bool adding)
919 WScreen *scr;
920 WReservedArea *area;
921 Bool hasState = False;
923 scr = wwin->screen_ptr;
925 if (adding) {
926 Atom type_ret;
927 int fmt_ret;
928 unsigned long nitems_ret;
929 unsigned long bytes_after_ret;
930 long *data = 0;
932 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_strut, 0, 4, False,
933 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
934 &bytes_after_ret,
935 (unsigned char**)&data)==Success && data) {
937 area = (WReservedArea *)wmalloc(sizeof(WReservedArea));
938 area->area.x1 = data[0];
939 area->area.x2 = data[1];
940 area->area.y1 = data[2];
941 area->area.y2 = data[3];
943 area->window = wwin->client_win;
945 area->next = scr->netdata->strut;
946 scr->netdata->strut = area;
948 XFree(data);
949 hasState = True;
950 #ifdef VIRTUAL_DESKTOP
951 /* just in case wm_window_type didn't set it already */
952 wwin->client_flags.virtual_stick = 1;
953 #endif
956 } else {
957 /* deleting */
958 area = scr->netdata->strut;
960 if(area) {
961 if(area->window == wwin->client_win) {
962 scr->netdata->strut = area->next;
963 wfree(area);
964 hasState = True;
965 } else {
966 while(area->next && area->next->window != wwin->client_win)
967 area = area->next;
969 if(area->next) {
970 WReservedArea *next;
972 next = area->next->next;
973 wfree(area->next);
974 area->next = next;
976 hasState = True;
982 return hasState;
986 static int
987 getWindowLayer(WWindow * wwin)
989 int layer = WMNormalLevel;
991 if (wwin->type == net_wm_window_type_desktop) {
992 layer = WMDesktopLevel;
993 } else if (wwin->type == net_wm_window_type_dock) {
994 layer = WMDockLevel;
995 } else if (wwin->type == net_wm_window_type_toolbar) {
996 layer = WMMainMenuLevel;
997 } else if (wwin->type == net_wm_window_type_menu) {
998 layer = WMSubmenuLevel;
999 } else if (wwin->type == net_wm_window_type_utility) {
1000 } else if (wwin->type == net_wm_window_type_splash) {
1001 } else if (wwin->type == net_wm_window_type_dialog) {
1002 if (wwin->transient_for) {
1003 WWindow *parent = wWindowFor(wwin->transient_for);
1004 if (parent && parent->flags.fullscreen) {
1005 layer = WMFullscreenLevel;
1008 /* //layer = WMPopUpLevel; // this seems a bad idea -Dan */
1009 } else if (wwin->type == net_wm_window_type_normal) {
1012 if (wwin->client_flags.sunken && WMSunkenLevel < layer)
1013 layer = WMSunkenLevel;
1014 if (wwin->client_flags.floating && WMFloatingLevel > layer)
1015 layer = WMFloatingLevel;
1017 return layer;
1021 static void
1022 doStateAtom(WWindow *wwin, Atom state, int set, Bool init)
1025 if (state == net_wm_state_sticky) {
1026 if (set == _NET_WM_STATE_TOGGLE) {
1027 set = !IS_OMNIPRESENT(wwin);
1029 if (set != wwin->flags.omnipresent) {
1030 wWindowSetOmnipresent(wwin, set);
1032 } else if (state == net_wm_state_shaded) {
1033 if (set == _NET_WM_STATE_TOGGLE) {
1034 set = !wwin->flags.shaded;
1036 if (init) {
1037 wwin->flags.shaded = set;
1038 } else {
1039 if (set) {
1040 wShadeWindow(wwin);
1041 } else {
1042 wUnshadeWindow(wwin);
1045 } else if (state == net_wm_state_skip_taskbar) {
1046 if (set == _NET_WM_STATE_TOGGLE) {
1047 set = !wwin->client_flags.skip_window_list;
1049 wwin->client_flags.skip_window_list = set;
1050 } else if (state == net_wm_state_skip_pager) {
1051 if (set == _NET_WM_STATE_TOGGLE) {
1052 set = !wwin->flags.net_skip_pager;
1054 wwin->flags.net_skip_pager = set;
1055 } else if (state == net_wm_state_maximized_vert) {
1056 if (set == _NET_WM_STATE_TOGGLE) {
1057 set = !(wwin->flags.maximized & MAX_VERTICAL);
1059 if (init) {
1060 wwin->flags.maximized = set;
1061 } else {
1062 if (set) {
1063 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_VERTICAL);
1064 } else {
1065 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_VERTICAL);
1068 } else if (state == net_wm_state_maximized_horz) {
1069 if (set == _NET_WM_STATE_TOGGLE) {
1070 set = !(wwin->flags.maximized & MAX_HORIZONTAL);
1072 if (init) {
1073 wwin->flags.maximized = set;
1074 } else {
1075 if (set) {
1076 wMaximizeWindow(wwin, wwin->flags.maximized | MAX_HORIZONTAL);
1077 } else {
1078 wMaximizeWindow(wwin, wwin->flags.maximized & ~MAX_HORIZONTAL);
1081 } else if (state == net_wm_state_hidden) {
1082 if (set == _NET_WM_STATE_TOGGLE) {
1083 set = !(wwin->flags.miniaturized);
1085 if (init) {
1086 wwin->flags.miniaturized = set;
1087 } else {
1088 if (set) {
1089 wIconifyWindow(wwin);
1090 } else {
1091 wDeiconifyWindow(wwin);
1094 } else if (state == net_wm_state_fullscreen) {
1095 if (set == _NET_WM_STATE_TOGGLE) {
1096 set = !(wwin->flags.fullscreen);
1098 if (init) {
1099 wwin->flags.fullscreen = set;
1100 } else {
1101 if (set) {
1102 wFullscreenWindow(wwin);
1103 } else {
1104 wUnfullscreenWindow(wwin);
1107 } else if (state == net_wm_state_above) {
1108 if (set == _NET_WM_STATE_TOGGLE) {
1109 set = !(wwin->client_flags.floating);
1111 if (init) {
1112 wwin->client_flags.floating = set;
1113 } else {
1114 wwin->client_flags.floating = set;
1115 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1118 } else if (state == net_wm_state_below) {
1119 if (set == _NET_WM_STATE_TOGGLE) {
1120 set = !(wwin->client_flags.sunken);
1122 if (init) {
1123 wwin->client_flags.sunken = set;
1124 } else {
1125 wwin->client_flags.sunken = set;
1126 ChangeStackingLevel(wwin->frame->core, getWindowLayer(wwin));
1129 } else {
1130 #ifdef DEBUG_WMSPEC
1131 printf("doStateAtom unknown atom %s set %d\n", XGetAtomName(dpy, state), set);
1132 #endif
1137 static void
1138 removeIcon( WWindow * wwin)
1140 if (wwin->icon == NULL)
1141 return;
1142 if (wwin->flags.miniaturized && wwin->icon->mapped) {
1143 XUnmapWindow(dpy, wwin->icon->core->window);
1144 RemoveFromStackList(wwin->icon->core);
1145 wIconDestroy(wwin->icon);
1146 wwin->icon = NULL;
1151 static Bool
1152 handleWindowType(WWindow * wwin, Atom type, int *layer)
1154 Bool ret = True;
1156 if (type == net_wm_window_type_desktop) {
1157 wwin->client_flags.no_titlebar = 1;
1158 wwin->client_flags.no_resizable = 1;
1159 wwin->client_flags.no_miniaturizable = 1;
1160 wwin->client_flags.no_border = 1;
1161 wwin->client_flags.no_resizebar = 1;
1162 wwin->client_flags.no_shadeable = 1;
1163 wwin->client_flags.no_movable = 1;
1164 wwin->client_flags.omnipresent = 1;
1165 wwin->client_flags.skip_window_list = 1;
1166 wwin->client_flags.dont_move_off = 1;
1167 wwin->client_flags.no_appicon = 1;
1168 wwin->flags.net_skip_pager = 1;
1169 wwin->frame_x = 0;
1170 wwin->frame_y = 0;
1171 #ifdef VIRTUAL_DESKTOP
1172 wwin->client_flags.virtual_stick = 1;
1173 #endif
1174 } else if (type == net_wm_window_type_dock) {
1175 wwin->client_flags.no_titlebar = 1;
1176 wwin->client_flags.no_resizable = 1;
1177 wwin->client_flags.no_miniaturizable = 1;
1178 wwin->client_flags.no_border = 1; /* XXX: really not a single decoration. */
1179 wwin->client_flags.no_resizebar = 1;
1180 wwin->client_flags.no_shadeable = 1;
1181 wwin->client_flags.no_movable = 1;
1182 wwin->client_flags.omnipresent = 1;
1183 wwin->client_flags.skip_window_list = 1;
1184 wwin->client_flags.dont_move_off = 1;
1185 wwin->flags.net_skip_pager = 1;
1186 #ifdef VIRTUAL_DESKTOP
1187 wwin->client_flags.virtual_stick = 1;
1188 #endif
1189 } else if (type == net_wm_window_type_toolbar) {
1190 wwin->client_flags.no_titlebar = 1;
1191 wwin->client_flags.no_resizable = 1;
1192 wwin->client_flags.no_miniaturizable = 1;
1193 wwin->client_flags.no_resizebar = 1;
1194 wwin->client_flags.no_shadeable = 1;
1195 wwin->client_flags.skip_window_list = 1;
1196 wwin->client_flags.dont_move_off = 1;
1197 wwin->client_flags.no_appicon = 1;
1198 } else if (type == net_wm_window_type_menu) {
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_utility) {
1208 wwin->client_flags.no_appicon = 1;
1209 } else if (type == net_wm_window_type_splash) {
1210 wwin->client_flags.no_titlebar = 1;
1211 wwin->client_flags.no_resizable = 1;
1212 wwin->client_flags.no_miniaturizable = 1;
1213 wwin->client_flags.no_resizebar = 1;
1214 wwin->client_flags.no_shadeable = 1;
1215 wwin->client_flags.no_movable = 1;
1216 wwin->client_flags.skip_window_list = 1;
1217 wwin->client_flags.dont_move_off = 1;
1218 wwin->client_flags.no_appicon = 1;
1219 wwin->flags.net_skip_pager = 1;
1220 } else if (type == net_wm_window_type_dialog) {
1221 /* These also seem a bad idea in our context -Dan
1222 // wwin->client_flags.skip_window_list = 1;
1223 // wwin->client_flags.no_appicon = 1;
1225 } else if (type == net_wm_window_type_normal) {
1226 } else {
1227 ret = False;
1230 wwin->type = type;
1231 *layer = getWindowLayer(wwin);
1233 return ret;
1237 void
1238 wNETWMPositionSplash(WWindow *wwin, int *x, int *y, int width, int height)
1240 if (wwin->type == net_wm_window_type_splash) {
1241 WScreen * scr = wwin->screen_ptr;
1242 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1243 *x = rect.pos.x + (rect.size.width - width)/2;
1244 *y = rect.pos.y + (rect.size.height - height)/2;
1249 static void
1250 updateWindowType(WWindow * wwin)
1252 Atom type_ret;
1253 int fmt_ret;
1254 unsigned long nitems_ret;
1255 unsigned long bytes_after_ret;
1256 long *data = 0;
1257 int layer;
1258 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1,
1259 False, XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1260 &bytes_after_ret, (unsigned char **)&data)==Success
1261 && data) {
1263 int i;
1264 Atom * type = (Atom *)data;
1265 for (i=0; i<nitems_ret; ++i) {
1266 if (handleWindowType(wwin, type[i], &layer)) break;
1268 XFree(data);
1271 ChangeStackingLevel(wwin->frame->core, layer);
1272 wwin->frame->flags.need_texture_change = 1;
1273 wWindowConfigureBorders(wwin);
1274 wFrameWindowPaint(wwin->frame);
1275 wNETWMUpdateActions(wwin, False);
1279 Bool
1280 wNETWMCheckClientHints(WWindow *wwin, int *layer, int *workspace)
1282 Atom type_ret;
1283 int fmt_ret;
1284 unsigned long nitems_ret;
1285 unsigned long bytes_after_ret;
1286 long *data = 0;
1287 Bool hasState = False;
1288 int i;
1290 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_desktop, 0, 1, False,
1291 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1292 &bytes_after_ret,
1293 (unsigned char**)&data)==Success && data) {
1295 long desktop = *data;
1296 XFree(data);
1298 if(desktop == -1)
1299 wwin->client_flags.omnipresent = 1;
1300 else
1301 *workspace = desktop;
1303 hasState = True;
1306 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_state, 0, 1, False,
1307 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1308 &bytes_after_ret,
1309 (unsigned char**)&data)==Success && data) {
1311 Atom * state = (Atom *)data;
1312 for(i=0; i<nitems_ret; ++i) {
1313 doStateAtom(wwin, state[i], _NET_WM_STATE_ADD, True);
1315 XFree(data);
1316 hasState = True;
1319 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_window_type, 0, 1, False,
1320 XA_ATOM, &type_ret, &fmt_ret, &nitems_ret,
1321 &bytes_after_ret,
1322 (unsigned char **)&data) == Success && data) {
1324 Atom * type = (Atom *)data;
1325 for (i=0; i<nitems_ret; ++i) {
1326 if (handleWindowType(wwin, type[i], layer)) break;
1328 XFree(data);
1329 hasState = True;
1332 wNETWMUpdateActions(wwin, False);
1333 updateStrut(wwin, False);
1334 if (updateStrut(wwin, True)) {
1335 hasState = True;
1337 wScreenUpdateUsableArea(wwin->screen_ptr);
1339 return hasState;
1343 static Bool
1344 updateNetIconInfo(WWindow *wwin) {
1346 Atom type_ret;
1347 int fmt_ret;
1348 unsigned long nitems_ret;
1349 unsigned long bytes_after_ret;
1350 long *data = 0;
1351 Bool hasState = False;
1352 Bool old_state = wwin->flags.net_handle_icon;
1354 if (XGetWindowProperty(dpy, wwin->client_win, net_wm_handled_icons, 0, 1, False,
1355 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1356 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1357 long handled = *data;
1358 wwin->flags.net_handle_icon = (handled != 0);
1359 XFree(data);
1360 hasState = True;
1362 } else wwin->flags.net_handle_icon = False;
1364 if ( XGetWindowProperty(dpy, wwin->client_win, net_wm_icon_geometry, 0, 4, False,
1365 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1366 &bytes_after_ret, (unsigned char **)&data) == Success && data) {
1368 #ifdef NETWM_PROPER
1369 if (wwin->flags.net_handle_icon)
1370 #else
1371 wwin->flags.net_handle_icon = True;
1372 #endif
1374 wwin->icon_x = data[0];
1375 wwin->icon_y = data[1];
1376 wwin->icon_w = data[2];
1377 wwin->icon_h = data[3];
1380 XFree(data);
1381 hasState = True;
1383 } else wwin->flags.net_handle_icon = False;
1385 if (wwin->flags.miniaturized &&
1386 old_state != wwin->flags.net_handle_icon) {
1387 if (wwin->flags.net_handle_icon) {
1388 removeIcon(wwin);
1389 } else {
1390 wwin->flags.miniaturized = False;
1391 wwin->flags.skip_next_animation = True;
1392 wIconifyWindow(wwin);
1396 return hasState;
1400 Bool
1401 wNETWMCheckInitialClientState(WWindow *wwin)
1403 Bool hasState = False;
1405 #ifdef DEBUG_WMSPEC
1406 printf("CheckInitialClientState\n");
1407 #endif
1409 wNETWMShowingDesktop(wwin->screen_ptr, False);
1411 hasState |= updateNetIconInfo(wwin);
1413 updateIconImage(wwin->screen_ptr, wwin);
1415 return hasState;
1419 static void
1420 handleDesktopNames(XClientMessageEvent *event, WScreen *scr)
1422 unsigned long nitems_ret, bytes_after_ret;
1423 char *data, *names[32];
1424 int fmt_ret, i, n;
1425 Atom type_ret;
1427 if (XGetWindowProperty(dpy, scr->root_win, net_desktop_names, 0, 1, False,
1428 utf8_string, &type_ret, &fmt_ret, &nitems_ret,
1429 &bytes_after_ret,
1430 (unsigned char**)&data)!=Success) {
1431 return;
1434 if (data == 0)
1435 return;
1437 if (type_ret != utf8_string || fmt_ret != 8)
1438 return;
1440 n = 0;
1441 names[n] = data;
1442 for (i = 0; i < nitems_ret; i++) {
1443 if (data[i] == 0) {
1444 n++;
1445 names[n] = &data[i];
1446 } else if (*names[n] == 0) {
1447 names[n] = &data[i];
1448 wWorkspaceRename(scr, n, names[n]);
1454 Bool
1455 wNETWMProcessClientMessage(XClientMessageEvent *event)
1457 WScreen *scr;
1458 WWindow *wwin;
1459 Bool done = True;
1461 #ifdef DEBUG_WMSPEC
1462 printf("processClientMessage type %s\n", XGetAtomName(dpy, event->message_type));
1463 #endif
1465 scr = wScreenForWindow(event->window);
1466 if (scr) {
1467 /* generic client messages */
1468 if (event->message_type == net_current_desktop) {
1469 wWorkspaceChange(scr, event->data.l[0]);
1470 } else if(event->message_type == net_number_of_desktops) {
1471 long value;
1473 value = event->data.l[0];
1474 if(value > scr->workspace_count) {
1475 wWorkspaceMake(scr, value - scr->workspace_count);
1476 } else if(value < scr->workspace_count) {
1477 int i;
1478 Bool rebuild = False;
1480 for (i = scr->workspace_count-1; i >= value; i--) {
1481 if (!wWorkspaceDelete(scr, i)) {
1482 rebuild = True;
1483 break;
1487 if(rebuild) {
1488 updateWorkspaceCount(scr);
1491 } else if (event->message_type == net_showing_desktop) {
1492 wNETWMShowingDesktop(scr, event->data.l[0]);
1493 #ifdef VIRTUAL_DESKTOP
1494 } else if (event->message_type == net_desktop_viewport) {
1495 wWorkspaceSetViewport(scr, scr->current_workspace,
1496 event->data.l[0], event->data.l[1]);
1497 #endif
1498 } else if (event->message_type == net_desktop_names) {
1499 handleDesktopNames(event, scr);
1500 } else {
1501 done = False;
1504 if (done)
1505 return True;
1508 /* window specific client messages */
1510 wwin = wWindowFor(event->window);
1511 if (!wwin)
1512 return False;
1514 if (event->message_type == net_active_window) {
1515 wNETWMShowingDesktop(scr, False);
1516 wMakeWindowVisible(wwin);
1517 } else if (event->message_type == net_close_window) {
1518 if (!WFLAGP(wwin, no_closable)) {
1519 if (wwin->protocols.DELETE_WINDOW)
1520 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
1522 } else if (event->message_type == net_wm_state) {
1523 int maximized = wwin->flags.maximized;
1524 long set = event->data.l[0];
1526 #ifdef DEBUG_WMSPEC
1527 printf("net_wm_state set %d a1 %s a2 %s\n", set,
1528 XGetAtomName(dpy, event->data.l[1]),
1529 XGetAtomName(dpy, event->data.l[2]));
1530 #endif
1532 doStateAtom(wwin, (Atom)event->data.l[1], set, False);
1533 if(event->data.l[2])
1534 doStateAtom(wwin, (Atom)event->data.l[2], set, False);
1536 if(wwin->flags.maximized != maximized) {
1537 if(!wwin->flags.maximized) {
1538 wwin->flags.maximized = maximized;
1539 wUnmaximizeWindow(wwin);
1540 } else {
1541 wMaximizeWindow(wwin, wwin->flags.maximized);
1544 updateStateHint(wwin, False, False);
1545 } else if (event->message_type == net_wm_handled_icons ||
1546 event->message_type == net_wm_icon_geometry) {
1547 updateNetIconInfo(wwin);
1548 } else if (event->message_type == net_wm_desktop) {
1549 long desktop = event->data.l[0];
1550 if (desktop == -1) {
1551 wWindowSetOmnipresent(wwin, True);
1552 } else {
1553 if (IS_OMNIPRESENT(wwin))
1554 wWindowSetOmnipresent(wwin, False);
1555 wWindowChangeWorkspace(wwin, desktop);
1557 } else if (event->message_type == net_wm_icon) {
1558 updateIconImage(scr, wwin);
1559 } else {
1560 done = False;
1563 return done;
1567 Bool
1568 wNETWMCheckClientHintChange(WWindow *wwin, XPropertyEvent *event)
1570 Bool ret = True;
1572 if(event->atom == net_wm_strut) {
1573 updateStrut(wwin, False);
1574 updateStrut(wwin, True);
1575 wScreenUpdateUsableArea(wwin->screen_ptr);
1576 } else if (event->atom == net_wm_handled_icons ||
1577 event->atom == net_wm_icon_geometry) {
1578 updateNetIconInfo(wwin);
1579 } else if (event->atom == net_wm_window_type) {
1580 updateWindowType(wwin);
1581 } else if (event->atom == net_wm_name) {
1582 char *name= wNETWMGetWindowName(wwin->client_win);
1583 wWindowUpdateName(wwin, name);
1584 if (name) wfree(name);
1585 } else if (event->atom == net_wm_icon_name) {
1586 if (wwin->icon) {
1587 char *name= wNETWMGetIconName(wwin->client_win);
1588 wIconChangeTitle(wwin->icon, name);
1590 } else {
1591 ret = False;
1594 return ret;
1599 wNETWMGetPidForWindow(Window window)
1601 Atom type_ret;
1602 int fmt_ret;
1603 unsigned long nitems_ret;
1604 unsigned long bytes_after_ret;
1605 long *data = 0;
1606 int pid;
1608 if (XGetWindowProperty(dpy, window, net_wm_pid, 0, 1, False,
1609 XA_CARDINAL, &type_ret, &fmt_ret, &nitems_ret,
1610 &bytes_after_ret,
1611 (unsigned char**)&data)==Success && data) {
1613 pid = *data;
1614 XFree(data);
1615 } else {
1616 pid = 0;
1619 return pid;
1623 char*
1624 wNETWMGetWindowName(Window window)
1626 char *name;
1627 char *ret;
1628 int size;
1630 name= (char*)PropGetCheckProperty(window,
1631 net_wm_name, utf8_string, 0,
1632 0, &size);
1633 if (name) {
1634 ret= wstrndup(name, size);
1635 XFree(name);
1637 else
1638 ret= NULL;
1639 return ret;
1643 char*
1644 wNETWMGetIconName(Window window)
1646 char *name;
1647 char *ret;
1648 int size;
1650 name= (char*)PropGetCheckProperty(window,
1651 net_wm_icon_name, utf8_string, 0,
1652 0, &size);
1653 if (name) {
1654 ret= wstrndup(name, size);
1655 XFree(name);
1657 else
1658 ret= NULL;
1659 return ret;
1663 static void
1664 observer(void *self, WMNotification *notif)
1666 WWindow *wwin = (WWindow*)WMGetNotificationObject(notif);
1667 const char *name = WMGetNotificationName(notif);
1668 void *data = WMGetNotificationClientData(notif);
1669 NetData *ndata = (NetData*)self;
1672 if (strcmp(name, WMNManaged) == 0 && wwin) {
1673 updateClientList(wwin->screen_ptr);
1674 updateClientListStacking(wwin->screen_ptr, NULL);
1675 updateStateHint(wwin, True, False);
1677 updateStrut(wwin, False);
1678 updateStrut(wwin, True);
1679 wScreenUpdateUsableArea(wwin->screen_ptr);
1680 } else if (strcmp(name, WMNUnmanaged) == 0 && wwin) {
1681 updateClientList(wwin->screen_ptr);
1682 updateClientListStacking(wwin->screen_ptr, wwin);
1683 updateWorkspaceHint(wwin, False, True);
1684 updateStateHint(wwin, False, True);
1685 wNETWMUpdateActions(wwin, True);
1687 updateStrut(wwin, False);
1688 wScreenUpdateUsableArea(wwin->screen_ptr);
1689 } else if (strcmp(name, WMNResetStacking) == 0 && wwin) {
1690 updateClientListStacking(wwin->screen_ptr, NULL);
1691 updateStateHint(wwin, False, False);
1692 } else if (strcmp(name, WMNChangedStacking) == 0 && wwin) {
1693 updateClientListStacking(wwin->screen_ptr, NULL);
1694 updateStateHint(wwin, False, False);
1695 } else if (strcmp(name, WMNChangedFocus) == 0) {
1696 updateFocusHint(ndata->scr, wwin);
1697 } else if (strcmp(name, WMNChangedWorkspace) == 0 && wwin) {
1698 updateWorkspaceHint(wwin, False, False);
1699 updateStateHint(wwin, True, False);
1700 } else if (strcmp(name, WMNChangedState) == 0 && wwin) {
1701 updateStateHint(wwin, !strcmp(data, "omnipresent"), False);
1706 static void
1707 wsobserver(void *self, WMNotification *notif)
1709 WScreen *scr = (WScreen*)WMGetNotificationObject(notif);
1710 const char *name = WMGetNotificationName(notif);
1712 if (strcmp(name, WMNWorkspaceCreated) == 0) {
1713 updateWorkspaceCount(scr);
1714 updateWorkspaceNames(scr);
1715 } else if (strcmp(name, WMNWorkspaceDestroyed) == 0) {
1716 updateWorkspaceCount(scr);
1717 updateWorkspaceNames(scr);
1718 } else if (strcmp(name, WMNWorkspaceChanged) == 0) {
1719 updateCurrentWorkspace(scr);
1720 } else if (strcmp(name, WMNWorkspaceNameChanged) == 0) {
1721 updateWorkspaceNames(scr);
1725 #endif /* NETWM_HINTS */