- removed the collapse appicons thing
[wmaker-crm.git] / src / client.c
blob878e69fa0a1d1322b13bc431cac8213e25164347
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"
46 #ifdef KWM_HINTS
47 #include "kwm.h"
48 #endif
50 /****** Global Variables ******/
52 /* contexts */
53 extern XContext wWinContext;
55 extern Atom _XA_WM_STATE;
56 extern Atom _XA_WM_PROTOCOLS;
57 extern Atom _XA_WM_COLORMAP_WINDOWS;
59 extern Atom _XA_WINDOWMAKER_MENU;
61 extern Atom _XA_GNUSTEP_WM_ATTR;
62 extern Atom _XA_GNUSTEP_WM_RESIZEBAR;
64 #ifdef SHAPE
65 extern Bool wShapeSupported;
66 #endif
70 *--------------------------------------------------------------------
71 * wClientRestore--
72 * Reparent the window back to the root window.
74 *--------------------------------------------------------------------
76 void
77 wClientRestore(WWindow *wwin)
79 #if 0
80 int gx, gy;
82 wClientGetGravityOffsets(wwin, &gx, &gy);
83 /* set the positio of the frame on screen */
84 wwin->frame_x -= gx * FRAME_BORDER_WIDTH;
85 wwin->frame_y -= gy * FRAME_BORDER_WIDTH;
86 /* if gravity is to the south, account for the border sizes */
87 if (gy > 0)
88 wwin->frame_y += (wwin->frame->top_width + wwin->frame->bottom_width);
89 #endif
90 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
91 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
92 wwin->frame_x, wwin->frame_y);
94 /* don't let the window get iconified after restart */
96 if (wwin->flags.shaded)
97 wClientSetState(wwin, NormalState, None);
103 *----------------------------------------------------------------------
104 * wClientSetState--
105 * Set the state of the client window to one of the window
106 * states defined in ICCCM (Iconic, Withdrawn, Normal)
108 * Side effects:
109 * The WM_STATE property of the window is updated as well as the
110 * WWindow.state variable.
111 *----------------------------------------------------------------------
113 void
114 wClientSetState(WWindow *wwin, int state, Window icon_win)
116 CARD32 data[2];
118 wwin->state = state;
120 data[0] = (unsigned long) state;
121 data[1] = (unsigned long) icon_win;
123 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
124 PropModeReplace, (unsigned char *) data, 2);
128 void
129 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
131 switch (wwin->normal_hints->win_gravity) {
132 case ForgetGravity:
133 case CenterGravity:
134 case StaticGravity:
135 *ofs_x = 0;
136 *ofs_y = 0;
137 break;
138 case NorthWestGravity:
139 *ofs_x = -1;
140 *ofs_y = -1;
141 break;
142 case NorthGravity:
143 *ofs_x = 0;
144 *ofs_y = -1;
145 break;
146 case NorthEastGravity:
147 *ofs_x = 1;
148 *ofs_y = -1;
149 break;
150 case WestGravity:
151 *ofs_x = -1;
152 *ofs_y = 0;
153 break;
154 case EastGravity:
155 *ofs_x = 1;
156 *ofs_y = 0;
157 break;
158 case SouthWestGravity:
159 *ofs_x = -1;
160 *ofs_y = 1;
161 break;
162 case SouthGravity:
163 *ofs_x = 0;
164 *ofs_y = 1;
165 break;
166 case SouthEastGravity:
167 *ofs_x = 1;
168 *ofs_y = 1;
169 break;
175 void
176 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
178 XWindowChanges xwc;
179 int nx, ny, nwidth, nheight;
180 int ofs_x, ofs_y;
182 if (wwin==NULL) {
184 * configure a window that was not mapped by us
186 xwc.x = xcre->x;
187 xwc.y = xcre->y;
188 xwc.width = xcre->width;
189 xwc.height = xcre->height;
190 xwc.border_width = xcre->border_width;
191 xwc.stack_mode = xcre->detail;
192 xwc.sibling = xcre->above;
193 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
194 return;
196 #ifdef SHAPE
197 if (wShapeSupported) {
198 int junk;
199 unsigned int ujunk;
200 int b_shaped;
202 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
203 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
204 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
205 &ujunk);
206 wwin->flags.shaped = b_shaped;
208 #endif
209 if (xcre->value_mask & CWStackMode) {
210 WObjDescriptor *desc;
211 WWindow *sibling;
213 if ((xcre->value_mask & CWSibling) &&
214 (XFindContext(dpy, xcre->above, wWinContext,
215 (XPointer *)&desc) == XCSUCCESS)
216 && (desc->parent_type==WCLASS_WINDOW)) {
217 sibling=desc->parent;
218 xwc.sibling = sibling->frame->core->window;
219 } else {
220 xwc.sibling = xcre->above;
222 xwc.stack_mode = xcre->detail;
223 XConfigureWindow(dpy, wwin->frame->core->window,
224 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
225 /* fix stacking order */
226 RemakeStackList(wwin->screen_ptr);
229 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
231 if (xcre->value_mask & CWBorderWidth) {
232 wwin->old_border_width = xcre->border_width;
235 if (!wwin->flags.shaded) {
236 /* If the window is shaded, wrong height will be set for the window */
237 if (xcre->value_mask & CWX) {
238 nx = xcre->x;
239 if (!WFLAGP(wwin, no_border))
240 nx -= FRAME_BORDER_WIDTH;
242 else
243 nx = wwin->frame_x;
245 if (xcre->value_mask & CWY) {
246 ny = xcre->y - ((ofs_y < 0) ? 0 : wwin->frame->top_width);
247 if (!WFLAGP(wwin, no_border))
248 ny -= FRAME_BORDER_WIDTH;
250 else
251 ny = wwin->frame_y;
253 if (xcre->value_mask & CWWidth)
254 nwidth = xcre->width;
255 else
256 nwidth = wwin->frame->core->width;
258 if (xcre->value_mask & CWHeight)
259 nheight = xcre->height;
260 else
261 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
263 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
264 wwin->old_geometry.x = nx;
265 wwin->old_geometry.y = ny;
266 wwin->old_geometry.width = nwidth;
267 wwin->old_geometry.height = nheight;
272 void
273 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
275 XEvent event;
277 event.xclient.type = ClientMessage;
278 event.xclient.message_type = _XA_WM_PROTOCOLS;
279 event.xclient.format = 32;
280 event.xclient.display = dpy;
281 event.xclient.window = wwin->client_win;
282 event.xclient.data.l[0] = protocol;
283 event.xclient.data.l[1] = time;
284 event.xclient.data.l[2] = 0;
285 event.xclient.data.l[3] = 0;
286 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
287 XSync(dpy, False);
292 void
293 wClientKill(WWindow *wwin)
295 XKillClient(dpy, wwin->client_win);
297 XFlush(dpy);
303 *----------------------------------------------------------------------
304 * wClientCheckProperty--
305 * Handles PropertyNotify'es, verifying which property was
306 * changed and updating internal state according to that, like redrawing
307 * the icon title when it is changed.
309 * Side effects:
310 * Depends on the changed property.
312 * TODO: _GNUSTEP_WM_ATTR
313 *----------------------------------------------------------------------
315 void
316 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
318 XWindowAttributes attribs;
319 XWMHints *new_hints;
320 int i, g1, g2;
321 char *tmp = NULL;
323 switch (event->atom) {
324 case XA_WM_NAME:
325 /* window title was changed */
326 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
327 wWindowUpdateName(wwin, NULL);
328 } else {
329 wWindowUpdateName(wwin, tmp);
331 if (tmp)
332 XFree(tmp);
333 break;
335 case XA_WM_ICON_NAME:
336 #ifdef KWM_HINTS
337 wKWMSendEventMessage(wwin, WKWMChangedClient);
338 #endif
339 if (!wwin->icon)
340 break;
341 else {
342 char *new_title;
344 /* icon title was changed */
345 wGetIconName(dpy, wwin->client_win, &new_title);
346 wIconChangeTitle(wwin->icon, new_title);
348 break;
350 case XA_WM_COMMAND:
351 if (wwin->main_window!=None) {
352 WApplication *wapp = wApplicationOf(wwin->main_window);
353 char **argv;
354 int argc;
356 if (!wapp || !wapp->app_icon)
357 break;
359 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
360 if (argc > 0 && argv != NULL) {
361 if (wapp->app_icon->command)
362 wfree(wapp->app_icon->command);
363 wapp->app_icon->command = wtokenjoin(argv, argc);
365 if (argv) {
366 XFreeStringList(argv);
370 break;
372 case XA_WM_HINTS:
373 /* WM_HINTS */
375 new_hints = XGetWMHints(dpy, wwin->client_win);
377 /* group leader update
379 * This means that the window is setting the leader after
380 * it was mapped, changing leaders or removing the leader.
382 * Valid state transitions are:
384 * _1 __2
385 * / \ / \
386 * v | v |
387 * (GC) (GC')
388 * / ^ / ^
389 * 3| |4 5| |6
390 * | | | |
391 * v / v /
392 * (G'C) (G'C')
394 * Where G is the window_group hint, C is CLIENT_LEADER property
395 * and ' indicates the hint is unset.
397 * 1,2 - change group leader to new value of window_group
398 * 3 - change leader to value of CLIENT_LEADER
399 * 4 - change leader to value of window_group
400 * 5 - destroy application
401 * 6 - create application
403 if (new_hints && (new_hints->flags & WindowGroupHint)
404 && new_hints->window_group!=None) {
405 g2 = 1;
406 } else {
407 g2 = 0;
409 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
410 && wwin->wm_hints->window_group!=None) {
411 g1 = 1;
412 } else {
413 g1 = 0;
416 if (wwin->client_leader) {
417 if (g1 && g2
418 && wwin->wm_hints->window_group!=new_hints->window_group) {
419 i = 1;
420 } else if (g1 && !g2) {
421 i = 3;
422 } else if (!g1 && g2) {
423 i = 4;
424 } else {
425 i = 0;
427 } else {
428 if (g1 && g2
429 && wwin->wm_hints->window_group!=new_hints->window_group) {
430 i = 2;
431 } else if (g1 && !g2) {
432 i = 5;
433 } else if (!g1 && g2) {
434 i = 6;
435 } else {
436 i = 0;
440 if (wwin->fake_group!=NULL) {
441 i = 7;
444 if (wwin->wm_hints)
445 XFree(wwin->wm_hints);
447 wwin->wm_hints = new_hints;
449 /* do action according to state transition */
450 switch (i) {
451 /* 3 - change leader to value of CLIENT_LEADER */
452 case 3:
453 wApplicationDestroy(wApplicationOf(wwin->main_window));
454 wwin->main_window = wwin->client_leader;
455 wwin->group_id = None;
456 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
457 break;
459 /* 1,2,4 - change leader to new value of window_group */
460 case 1:
461 case 2:
462 case 4:
463 wApplicationDestroy(wApplicationOf(wwin->main_window));
464 wwin->main_window = new_hints->window_group;
465 wwin->group_id = wwin->main_window;
466 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
467 break;
469 /* 5 - destroy application */
470 case 5:
471 wApplicationDestroy(wApplicationOf(wwin->main_window));
472 wwin->main_window = None;
473 wwin->group_id = None;
474 break;
476 /* 6 - create application */
477 case 6:
478 wwin->main_window = new_hints->window_group;
479 wwin->group_id = wwin->main_window;
480 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
481 break;
482 /* 7 - we have a fake window group id, so just ignore anything else */
483 case 7:
484 break;
486 #ifdef DEBUG
487 if (i) {
488 printf("window leader update caused state transition %i\n",i);
490 #endif
492 if (wwin->wm_hints) {
493 /* update icon */
494 if ((wwin->wm_hints->flags & IconPixmapHint)
495 || (wwin->wm_hints->flags & IconWindowHint)) {
496 WApplication *wapp;
498 if (wwin->flags.miniaturized && wwin->icon) {
499 wIconUpdate(wwin->icon);
501 wapp = wApplicationOf(wwin->main_window);
502 if (wapp && wapp->app_icon) {
503 wIconUpdate(wapp->app_icon->icon);
505 #ifdef KWM_HINTS
506 wKWMSendEventMessage(wwin, WKWMIconChange);
507 #endif
510 if (wwin->wm_hints->flags & UrgencyHint)
511 wwin->flags.urgent = 1;
512 else
513 wwin->flags.urgent = 0;
514 } else if (wwin->fake_group!=NULL) {
515 wwin->group_id = wwin->fake_group->window;
516 } else {
517 wwin->group_id = None;
519 break;
521 case XA_WM_NORMAL_HINTS:
522 /* normal (geometry) hints */
524 int foo;
525 unsigned bar;
527 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
528 wClientGetNormalHints(wwin, &attribs, False, &foo, &foo,
529 &bar, &bar);
530 /* TODO: should we check for consistency of the current
531 * size against the new geometry hints? */
533 break;
535 case XA_WM_TRANSIENT_FOR:
537 Window new_owner;
538 WWindow *owner;
540 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
541 new_owner = None;
542 } else {
543 if (new_owner==0 || new_owner == wwin->client_win) {
544 new_owner = wwin->screen_ptr->root_win;
547 if (new_owner!=wwin->transient_for) {
548 owner = wWindowFor(wwin->transient_for);
549 if (owner) {
550 if (owner->flags.semi_focused) {
551 owner->flags.semi_focused = 0;
552 if ((owner->flags.mapped || owner->flags.shaded)
553 && owner->frame)
554 wFrameWindowPaint(owner->frame);
557 owner = wWindowFor(new_owner);
558 if (owner) {
559 if (!owner->flags.semi_focused) {
560 owner->flags.semi_focused = 1;
561 if ((owner->flags.mapped || owner->flags.shaded)
562 && owner->frame)
563 wFrameWindowPaint(owner->frame);
566 wwin->transient_for = new_owner;
567 if (new_owner==None) {
568 if (WFLAGP(wwin, no_miniaturizable)) {
569 WSETUFLAG(wwin, no_miniaturizable, 0);
570 WSETUFLAG(wwin, no_miniaturize_button, 0);
571 if (wwin->frame)
572 wWindowConfigureBorders(wwin);
574 } else if (!WFLAGP(wwin, no_miniaturizable)) {
575 WSETUFLAG(wwin, no_miniaturizable, 1);
576 WSETUFLAG(wwin, no_miniaturize_button, 1);
577 if (wwin->frame)
578 wWindowConfigureBorders(wwin);
582 break;
584 default:
585 if (event->atom==_XA_WM_PROTOCOLS) {
587 PropGetProtocols(wwin->client_win, &wwin->protocols);
589 WSETUFLAG(wwin, kill_close, !wwin->protocols.DELETE_WINDOW);
591 if (wwin->frame)
592 wWindowUpdateButtonImages(wwin);
594 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
596 GetColormapWindows(wwin);
597 wColormapInstallForWindow(wwin->screen_ptr, wwin);
599 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
600 WApplication *wapp;
602 wapp = wApplicationOf(wwin->main_window);
603 if (wapp) {
604 if (wapp->menu) {
605 /* update menu */
606 /* TODO: remake appmenu update */
607 wAppMenuDestroy(wapp->menu);
609 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
610 /* make the appmenu be mapped */
611 wSetFocusTo(wwin->screen_ptr, NULL);
612 wSetFocusTo(wwin->screen_ptr,
613 wwin->screen_ptr->focused_window);
615 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
616 GNUstepWMAttributes *attr;
618 PropGetGNUstepWMAttr(wwin->client_win, &attr);
620 wWindowUpdateGNUstepAttr(wwin, attr);
622 XFree(attr);
623 } else {
624 #ifdef KWM_HINTS
625 Bool done;
627 done = wKWMCheckClientHintChange(wwin, event);
628 #endif /* KWM_HINTS */
635 *----------------------------------------------------------------------
636 * wClientGetNormalHints--
637 * Get size (normal) hints and a default geometry for the client
638 * window. The hints are also checked for inconsistency. If geometry is
639 * True, the returned data will account for client specified initial
640 * geometry.
642 * Side effects:
643 * normal_hints is filled with valid data.
644 *----------------------------------------------------------------------
646 void
647 wClientGetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, Bool geometry,
648 int *x, int *y, unsigned *width, unsigned *height)
650 int pre_icccm = 0; /* not used */
652 /* find a position for the window */
653 if (!wwin->normal_hints)
654 wwin->normal_hints = XAllocSizeHints();
656 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
657 wwin->normal_hints->flags = 0;
659 *x = wattribs->x;
660 *y = wattribs->y;
662 *width = wattribs->width;
663 *height = wattribs->height;
665 if (!(wwin->normal_hints->flags & PWinGravity)) {
666 wwin->normal_hints->win_gravity = NorthWestGravity;
668 if (!(wwin->normal_hints->flags & PMinSize)) {
669 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
670 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
672 if (!(wwin->normal_hints->flags & PBaseSize)) {
673 wwin->normal_hints->base_width = 0;
674 wwin->normal_hints->base_height = 0;
676 if (!(wwin->normal_hints->flags & PMaxSize)) {
677 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
678 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
681 /* some buggy apps set weird hints.. */
682 if (wwin->normal_hints->min_width <= 0)
683 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
685 if (wwin->normal_hints->min_height <= 0)
686 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
689 if (wwin->normal_hints->max_width < wwin->normal_hints->min_width)
690 wwin->normal_hints->max_width = wwin->normal_hints->min_width;
692 if (wwin->normal_hints->max_height < wwin->normal_hints->min_height)
693 wwin->normal_hints->max_height = wwin->normal_hints->min_height;
695 if (!(wwin->normal_hints->flags & PResizeInc)) {
696 wwin->normal_hints->width_inc = 1;
697 wwin->normal_hints->height_inc = 1;
698 } else {
699 if (wwin->normal_hints->width_inc <= 0)
700 wwin->normal_hints->width_inc = 1;
701 if (wwin->normal_hints->height_inc <= 0)
702 wwin->normal_hints->height_inc = 1;
705 if (wwin->normal_hints->flags & PAspect) {
706 if (wwin->normal_hints->min_aspect.x < 1)
707 wwin->normal_hints->min_aspect.x = 1;
708 if (wwin->normal_hints->min_aspect.y < 1)
709 wwin->normal_hints->min_aspect.y = 1;
711 if (wwin->normal_hints->max_aspect.x < 1)
712 wwin->normal_hints->max_aspect.x = 1;
713 if (wwin->normal_hints->max_aspect.y < 1)
714 wwin->normal_hints->max_aspect.y = 1;
717 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
718 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
720 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
721 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
724 #ifdef IGNORE_PPOSITION
725 wwin->normal_hints->flags &= ~PPosition;
726 #endif
728 if (pre_icccm && !wwin->screen_ptr->flags.startup && geometry) {
729 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
730 *x = wwin->normal_hints->x;
731 *y = wwin->normal_hints->y;
733 if (wwin->normal_hints->flags & (USSize|PSize)) {
734 *width = wwin->normal_hints->width;
735 *height = wwin->normal_hints->height;
741 void
742 GetColormapWindows(WWindow *wwin)
744 #ifndef NO_CRASHES
745 if (wwin->cmap_windows) {
746 XFree(wwin->cmap_windows);
749 wwin->cmap_windows = NULL;
750 wwin->cmap_window_no = 0;
752 if (!XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
753 &(wwin->cmap_window_no))
754 || !wwin->cmap_windows) {
755 wwin->cmap_window_no = 0;
756 wwin->cmap_windows = NULL;
759 #endif