Window Maker 0.20.3
[wmaker-crm.git] / src / client.c
blobe19932452186388ad7282e5fb8396d270ff592f3
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
34 #include "WindowMaker.h"
35 #include "wcore.h"
36 #include "framewin.h"
37 #include "window.h"
38 #include "properties.h"
39 #include "actions.h"
40 #include "icon.h"
41 #include "client.h"
42 #include "funcs.h"
43 #include "stacking.h"
44 #include "appicon.h"
45 #include "appmenu.h"
47 /****** Global Variables ******/
49 /* contexts */
50 extern XContext wWinContext;
52 extern Atom _XA_WM_STATE;
53 extern Atom _XA_WM_PROTOCOLS;
54 extern Atom _XA_WM_COLORMAP_WINDOWS;
56 extern Atom _XA_WINDOWMAKER_MENU;
58 extern Atom _XA_GNUSTEP_WM_ATTR;
59 extern Atom _XA_GNUSTEP_WM_RESIZEBAR;
61 #ifdef SHAPE
62 extern Bool wShapeSupported;
63 #endif
67 *--------------------------------------------------------------------
68 * wClientRestore--
69 * Reparent the window back to the root window.
71 *--------------------------------------------------------------------
73 void
74 wClientRestore(WWindow *wwin)
76 #if 0
77 int gx, gy;
79 wClientGetGravityOffsets(wwin, &gx, &gy);
80 /* set the positio of the frame on screen */
81 wwin->frame_x -= gx * FRAME_BORDER_WIDTH;
82 wwin->frame_y -= gy * FRAME_BORDER_WIDTH;
83 /* if gravity is to the south, account for the border sizes */
84 if (gy > 0)
85 wwin->frame_y += (wwin->frame->top_width + wwin->frame->bottom_width);
86 #endif
87 XUnmapWindow(dpy, wwin->client_win);
88 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
89 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
90 wwin->frame_x, wwin->frame_y);
91 if (wwin->flags.mapped)
92 XMapWindow(dpy, wwin->client_win);
94 /* don't let the window get iconified after restart */
95 if (wwin->flags.shaded)
96 wClientSetState(wwin, NormalState, None);
101 *----------------------------------------------------------------------
102 * wClientSetState--
103 * Set the state of the client window to one of the window
104 * states defined in ICCCM (Iconic, Withdrawn, Normal)
106 * Side effects:
107 * The WM_STATE property of the window is updated as well as the
108 * WWindow.state variable.
109 *----------------------------------------------------------------------
111 void
112 wClientSetState(WWindow *wwin, int state, Window icon_win)
114 CARD32 data[2];
116 wwin->state = state;
118 data[0] = (unsigned long) state;
119 data[1] = (unsigned long) icon_win;
121 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
122 PropModeReplace, (unsigned char *) data, 2);
126 void
127 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
129 switch (wwin->normal_hints->win_gravity) {
130 case ForgetGravity:
131 case CenterGravity:
132 case StaticGravity:
133 *ofs_x = 0;
134 *ofs_y = 0;
135 break;
136 case NorthWestGravity:
137 *ofs_x = -1;
138 *ofs_y = -1;
139 break;
140 case NorthGravity:
141 *ofs_x = 0;
142 *ofs_y = -1;
143 break;
144 case NorthEastGravity:
145 *ofs_x = 1;
146 *ofs_y = -1;
147 break;
148 case WestGravity:
149 *ofs_x = -1;
150 *ofs_y = 0;
151 break;
152 case EastGravity:
153 *ofs_x = 1;
154 *ofs_y = 0;
155 break;
156 case SouthWestGravity:
157 *ofs_x = -1;
158 *ofs_y = 1;
159 break;
160 case SouthGravity:
161 *ofs_x = 0;
162 *ofs_y = 1;
163 break;
164 case SouthEastGravity:
165 *ofs_x = 1;
166 *ofs_y = 1;
167 break;
173 void
174 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
176 XWindowChanges xwc;
177 int nx, ny, nwidth, nheight;
178 int ofs_x, ofs_y;
180 if (wwin==NULL) {
182 * configure a window that was not mapped by us
184 xwc.x = xcre->x;
185 xwc.y = xcre->y;
186 xwc.width = xcre->width;
187 xwc.height = xcre->height;
188 xwc.border_width = xcre->border_width;
189 xwc.stack_mode = xcre->detail;
190 xwc.sibling = xcre->above;
191 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
192 return;
194 #ifdef SHAPE
195 if (wShapeSupported) {
196 int junk;
197 unsigned int ujunk;
198 int b_shaped;
200 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
201 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
202 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
203 &ujunk);
204 wwin->flags.shaped = b_shaped;
206 #endif
207 if (xcre->value_mask & CWStackMode) {
208 WObjDescriptor *desc;
209 WWindow *sibling;
211 if ((xcre->value_mask & CWSibling) &&
212 (XFindContext(dpy, xcre->above, wWinContext,
213 (XPointer *)&desc) == XCSUCCESS)
214 && (desc->parent_type==WCLASS_WINDOW)) {
215 sibling=desc->parent;
216 xwc.sibling = sibling->frame->core->window;
217 } else {
218 xwc.sibling = xcre->above;
220 xwc.stack_mode = xcre->detail;
221 XConfigureWindow(dpy, wwin->frame->core->window,
222 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
223 /* fix stacking order */
224 RemakeStackList(wwin->screen_ptr);
227 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
229 if (xcre->value_mask & CWBorderWidth) {
230 wwin->old_border_width = xcre->border_width;
233 if (!wwin->flags.shaded) {
234 /* If the window is shaded, wrong height will be set for the window */
235 if (xcre->value_mask & CWX)
236 nx = xcre->x - FRAME_BORDER_WIDTH;
237 else
238 nx = wwin->frame_x;
240 if (xcre->value_mask & CWY)
241 ny = xcre->y - ((ofs_y < 0) ? 0 : wwin->frame->top_width)
242 - FRAME_BORDER_WIDTH;
243 else
244 ny = wwin->frame_y;
246 if (xcre->value_mask & CWWidth)
247 nwidth = xcre->width;
248 else
249 nwidth = wwin->frame->core->width;
251 if (xcre->value_mask & CWHeight)
252 nheight = xcre->height;
253 else
254 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
256 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
257 wwin->old_geometry.x = nx;
258 wwin->old_geometry.y = ny;
259 wwin->old_geometry.width = nwidth;
260 wwin->old_geometry.height = nheight;
265 void
266 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
268 XEvent event;
270 event.xclient.type = ClientMessage;
271 event.xclient.message_type = _XA_WM_PROTOCOLS;
272 event.xclient.format = 32;
273 event.xclient.display = dpy;
274 event.xclient.window = wwin->client_win;
275 event.xclient.data.l[0] = protocol;
276 event.xclient.data.l[1] = time;
277 event.xclient.data.l[2] = 0;
278 event.xclient.data.l[3] = 0;
279 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
280 XSync(dpy, False);
285 void
286 wClientKill(WWindow *wwin)
288 XKillClient(dpy, wwin->client_win);
289 XFlush(dpy);
293 *----------------------------------------------------------------------
294 * wClientCheckProperty--
295 * Handles PropertyNotify'es, verifying which property was
296 * changed and updating internal state according to that, like redrawing
297 * the icon title when it is changed.
299 * Side effects:
300 * Depends on the changed property.
302 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
303 *----------------------------------------------------------------------
305 void
306 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
308 XWindowAttributes attribs;
309 XWMHints *new_hints;
310 int i, g1, g2;
311 char *tmp;
313 switch (event->atom) {
314 case XA_WM_NAME:
315 /* window title was changed */
316 if (wwin->frame) {
317 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
318 /* the hint was removed */
319 tmp = wstrdup(DEF_WINDOW_TITLE);
321 if (wFrameWindowChangeTitle(wwin->frame, tmp)) {
322 /* only update the menu if the title has actually changed */
323 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
325 if (tmp)
326 free(tmp);
328 break;
330 case XA_WM_ICON_NAME:
331 if (!wwin->icon)
332 break;
333 else {
334 char *new_title;
336 /* icon title was changed */
337 wGetIconName(dpy, wwin->client_win, &new_title);
338 wIconChangeTitle(wwin->icon, new_title);
340 break;
342 case XA_WM_COMMAND:
343 if (wwin->main_window!=None) {
344 WApplication *wapp = wApplicationOf(wwin->main_window);
345 char **argv;
346 int argc;
348 if (wapp && wapp->app_icon) {
349 if (wapp->app_icon->command!=NULL)
350 free(wapp->app_icon->command);
352 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
353 if (argc > 0 && argv != NULL)
354 wapp->app_icon->command = FlattenStringList(argv,argc);
355 if (argv) {
356 XFreeStringList(argv);
361 break;
363 case XA_WM_HINTS:
364 /* WM_HINTS */
366 new_hints = XGetWMHints(dpy, wwin->client_win);
368 /* group leader update
370 * This means that the window is setting the leader after
371 * it was mapped, changing leaders or removing the leader.
373 * Valid state transitions are:
375 * _1 __2
376 * / \ / \
377 * v | v |
378 * (GC) (GC')
379 * / ^ / ^
380 * 3| |4 5| |6
381 * | | | |
382 * v / v /
383 * (G'C) (G'C')
385 * Where G is the window_group hint, C is CLIENT_LEADER property
386 * and ' indicates the hint is unset.
388 * 1,2 - change group leader to new value of window_group
389 * 3 - change leader to value of CLIENT_LEADER
390 * 4 - change leader to value of window_group
391 * 5 - destroy application
392 * 6 - create application
394 if (new_hints && (new_hints->flags & WindowGroupHint)
395 && new_hints->window_group!=None) {
396 g2 = 1;
397 } else {
398 g2 = 0;
400 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
401 && wwin->wm_hints->window_group!=None) {
402 g1 = 1;
403 } else {
404 g1 = 0;
407 if (wwin->client_leader) {
408 if (g1 && g2
409 && wwin->wm_hints->window_group!=new_hints->window_group) {
410 i = 1;
411 } else if (g1 && !g2) {
412 i = 3;
413 } else if (!g1 && g2) {
414 i = 4;
415 } else {
416 i = 0;
418 } else {
419 if (g1 && g2
420 && wwin->wm_hints->window_group!=new_hints->window_group) {
421 i = 2;
422 } else if (g1 && !g2) {
423 i = 5;
424 } else if (!g1 && g2) {
425 i = 6;
426 } else {
427 i = 0;
431 if (wwin->wm_hints)
432 XFree(wwin->wm_hints);
434 wwin->wm_hints = new_hints;
436 /* do action according to state transition */
437 switch (i) {
438 /* 3 - change leader to value of CLIENT_LEADER */
439 case 3:
440 wApplicationDestroy(wApplicationOf(wwin->main_window));
441 wwin->main_window = wwin->client_leader;
442 wwin->group_id = None;
443 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
444 break;
446 /* 1,2,4 - change leader to new value of window_group */
447 case 1:
448 case 2:
449 case 4:
450 wApplicationDestroy(wApplicationOf(wwin->main_window));
451 wwin->main_window = new_hints->window_group;
452 wwin->group_id = wwin->main_window;
453 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
454 break;
456 /* 5 - destroy application */
457 case 5:
458 wApplicationDestroy(wApplicationOf(wwin->main_window));
459 wwin->main_window = None;
460 wwin->group_id = None;
461 break;
463 /* 6 - create application */
464 case 6:
465 wwin->main_window = new_hints->window_group;
466 wwin->group_id = wwin->main_window;
467 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
468 break;
470 #ifdef DEBUG
471 if (i) {
472 printf("window leader update caused state transition %i\n",i);
474 #endif
476 if (wwin->wm_hints) {
477 /* update icon */
478 if ((wwin->wm_hints->flags & IconPixmapHint)
479 || (wwin->wm_hints->flags & IconWindowHint)) {
480 WApplication *wapp;
482 if (wwin->flags.miniaturized) {
483 wIconUpdate(wwin->icon);
485 wapp = wApplicationOf(wwin->main_window);
486 if (wapp && wapp->app_icon) {
487 wIconUpdate(wapp->app_icon->icon);
491 if (wwin->wm_hints->flags & UrgencyHint)
492 wwin->flags.urgent = 1;
493 else
494 wwin->flags.urgent = 0;
495 } else {
496 wwin->group_id = None;
498 break;
500 case XA_WM_NORMAL_HINTS:
501 /* normal (geometry) hints */
503 int foo;
504 unsigned bar;
506 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
507 wClientGetNormalHints(wwin, &attribs, False, &foo, &foo,
508 &bar, &bar);
509 /* TODO: should we check for consistency of the current
510 * size against the new geometry hints? */
512 break;
514 case XA_WM_TRANSIENT_FOR:
516 Window new_owner;
517 WWindow *owner;
519 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
520 new_owner = None;
521 } else {
522 if (new_owner==0 || new_owner == wwin->client_win) {
523 new_owner = wwin->screen_ptr->root_win;
526 if (new_owner!=wwin->transient_for) {
527 owner = wWindowFor(wwin->transient_for);
528 if (owner) {
529 if (owner->flags.semi_focused) {
530 owner->flags.semi_focused = 0;
531 if ((owner->flags.mapped || owner->flags.shaded)
532 && owner->frame)
533 wFrameWindowPaint(owner->frame);
536 owner = wWindowFor(new_owner);
537 if (owner) {
538 if (!owner->flags.semi_focused) {
539 owner->flags.semi_focused = 1;
540 if ((owner->flags.mapped || owner->flags.shaded)
541 && owner->frame)
542 wFrameWindowPaint(owner->frame);
545 wwin->transient_for = new_owner;
546 if (new_owner==None) {
547 if (wwin->window_flags.no_miniaturizable) {
548 wwin->window_flags.no_miniaturizable = 0;
549 wwin->window_flags.no_miniaturize_button = 0;
550 if (wwin->frame)
551 wWindowConfigureBorders(wwin);
553 } else if (!wwin->window_flags.no_miniaturizable) {
554 wwin->window_flags.no_miniaturizable = 1;
555 wwin->window_flags.no_miniaturize_button = 1;
556 if (wwin->frame)
557 wWindowConfigureBorders(wwin);
561 break;
563 default:
564 if (event->atom==_XA_WM_PROTOCOLS) {
565 PropGetProtocols(wwin->client_win, &wwin->protocols);
566 if (wwin->protocols.DELETE_WINDOW) {
567 wwin->window_flags.kill_close = 0;
568 } else {
569 wwin->window_flags.kill_close = 1;
571 if (wwin->frame)
572 wWindowUpdateButtonImages(wwin);
574 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
576 GetColormapWindows(wwin);
577 wColormapInstallForWindow(wwin->screen_ptr, wwin);
579 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
580 WApplication *wapp;
582 wapp = wApplicationOf(wwin->main_window);
583 if (wapp) {
584 if (wapp->menu) {
585 /* update menu */
586 /* TODO: remake appmenu update */
587 wAppMenuDestroy(wapp->menu);
589 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
590 /* make the appmenu be mapped */
591 wSetFocusTo(wwin->screen_ptr, NULL);
592 wSetFocusTo(wwin->screen_ptr,
593 wwin->screen_ptr->focused_window);
595 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
596 GNUstepWMAttributes *attr;
598 PropGetGNUstepWMAttr(wwin->client_win, &attr);
600 wWindowUpdateGNUstepAttr(wwin, attr);
602 XFree(attr);
609 *----------------------------------------------------------------------
610 * wClientGetNormalHints--
611 * Get size (normal) hints and a default geometry for the client
612 * window. The hints are also checked for inconsistency. If geometry is
613 * True, the returned data will account for client specified initial
614 * geometry.
616 * Side effects:
617 * normal_hints is filled with valid data.
618 *----------------------------------------------------------------------
620 void
621 wClientGetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, Bool geometry,
622 int *x, int *y, unsigned *width, unsigned *height)
624 int pre_icccm=0;
626 /* find a position for the window */
627 if (!wwin->normal_hints)
628 wwin->normal_hints = XAllocSizeHints();
630 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
631 wwin->normal_hints->flags = 0;
633 *x = wattribs->x;
634 *y = wattribs->y;
636 *width = wattribs->width;
637 *height = wattribs->height;
639 if (!(wwin->normal_hints->flags & PWinGravity)) {
640 wwin->normal_hints->win_gravity = NorthWestGravity;
642 if (!(wwin->normal_hints->flags & PMinSize)) {
643 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
644 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
646 if (!(wwin->normal_hints->flags & PBaseSize)) {
647 wwin->normal_hints->base_width = 0;
648 wwin->normal_hints->base_height = 0;
650 if (!(wwin->normal_hints->flags & PMaxSize)) {
651 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
652 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
655 if (!(wwin->normal_hints->flags & PResizeInc)) {
656 wwin->normal_hints->width_inc = 1;
657 wwin->normal_hints->height_inc = 1;
658 } else {
659 if (wwin->normal_hints->width_inc <= 0)
660 wwin->normal_hints->width_inc = 1;
661 if (wwin->normal_hints->height_inc <= 0)
662 wwin->normal_hints->height_inc = 1;
665 if (wwin->normal_hints->flags & PAspect) {
666 if (wwin->normal_hints->min_aspect.x < 1)
667 wwin->normal_hints->min_aspect.x = 1;
668 if (wwin->normal_hints->min_aspect.y < 1)
669 wwin->normal_hints->min_aspect.y = 1;
671 if (wwin->normal_hints->max_aspect.x < 1)
672 wwin->normal_hints->max_aspect.x = 1;
673 if (wwin->normal_hints->max_aspect.y < 1)
674 wwin->normal_hints->max_aspect.y = 1;
677 if (wwin->normal_hints->min_width <= 0)
678 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
680 if (wwin->normal_hints->min_height <= 0)
681 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
683 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
684 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
686 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
687 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
690 /* pre ICCCM (old) client */
691 if (pre_icccm && !wwin->screen_ptr->flags.startup && geometry) {
692 #ifdef DEBUG
693 printf("PRE ICCCM\n");
694 #endif
695 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
696 *x = wwin->normal_hints->x;
697 *y = wwin->normal_hints->y;
699 if (wwin->normal_hints->flags & (USSize|PSize)) {
700 *width = wwin->normal_hints->width;
701 *height = wwin->normal_hints->height;
707 void
708 GetColormapWindows(WWindow *wwin)
710 if (wwin->cmap_windows) {
711 XFree(wwin->cmap_windows);
714 wwin->cmap_windows = NULL;
715 wwin->cmap_window_no = 0;
717 if (XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
718 &(wwin->cmap_window_no))!=Success) {
719 wwin->cmap_window_no = 0;
720 wwin->cmap_windows = NULL;