Initial revision
[wmaker-crm.git] / src / client.c
bloba0999f96f6a155a7481da162bf461fee030091ab
1 /*
2 * WindowMaker 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;
62 *--------------------------------------------------------------------
63 * wClientRestore--
64 * Reparent the window back to the root window.
66 *--------------------------------------------------------------------
68 void
69 wClientRestore(WWindow *wwin)
71 int gx, gy;
73 wClientGetGravityOffsets(wwin, &gx, &gy);
74 /* set the positio of the frame on screen */
75 wwin->frame_x -= gx * (wwin->old_border_width - FRAME_BORDER_WIDTH);
76 wwin->frame_y -= gy * (wwin->old_border_width - FRAME_BORDER_WIDTH);
77 /* if gravity is to the south, account for the border sizes */
78 if (gy > 0)
79 wwin->frame_y += (wwin->frame->top_width + wwin->frame->bottom_width);
81 XUnmapWindow(dpy, wwin->client_win);
82 XSetWindowBorderWidth(dpy, wwin->client_win, wwin->old_border_width);
83 XReparentWindow(dpy, wwin->client_win, wwin->screen_ptr->root_win,
84 wwin->frame_x, wwin->frame_y);
85 if (wwin->flags.mapped)
86 XMapWindow(dpy, wwin->client_win);
88 /* don't let the window get iconified after restart */
89 if (wwin->flags.shaded)
90 wClientSetState(wwin, NormalState, None);
95 *----------------------------------------------------------------------
96 * wClientSetState--
97 * Set the state of the client window to one of the window
98 * states defined in ICCCM (Iconic, Withdrawn, Normal)
100 * Side effects:
101 * The WM_STATE property of the window is updated as well as the
102 * WWindow.state variable.
103 *----------------------------------------------------------------------
105 void
106 wClientSetState(WWindow *wwin, int state, Window icon_win)
108 unsigned long data[2];
110 wwin->state = state;
112 data[0] = (unsigned long) state;
113 data[1] = (unsigned long) icon_win;
115 XChangeProperty(dpy, wwin->client_win, _XA_WM_STATE, _XA_WM_STATE, 32,
116 PropModeReplace, (unsigned char *) data, 2);
120 void
121 wClientGetGravityOffsets(WWindow *wwin, int *ofs_x, int *ofs_y)
123 switch (wwin->normal_hints->win_gravity) {
124 case ForgetGravity:
125 case CenterGravity:
126 case StaticGravity:
127 *ofs_x = 0;
128 *ofs_y = 0;
129 break;
130 case NorthWestGravity:
131 *ofs_x = -1;
132 *ofs_y = -1;
133 break;
134 case NorthGravity:
135 *ofs_x = 0;
136 *ofs_y = -1;
137 break;
138 case NorthEastGravity:
139 *ofs_x = 1;
140 *ofs_y = -1;
141 break;
142 case WestGravity:
143 *ofs_x = -1;
144 *ofs_y = 0;
145 break;
146 case EastGravity:
147 *ofs_x = 1;
148 *ofs_y = 0;
149 break;
150 case SouthWestGravity:
151 *ofs_x = -1;
152 *ofs_y = 1;
153 break;
154 case SouthGravity:
155 *ofs_x = 0;
156 *ofs_y = 1;
157 break;
158 case SouthEastGravity:
159 *ofs_x = 1;
160 *ofs_y = 1;
161 break;
167 void
168 wClientConfigure(WWindow *wwin, XConfigureRequestEvent *xcre)
170 XWindowChanges xwc;
171 int nx, ny, nwidth, nheight;
172 int ofs_x, ofs_y;
174 if (wwin==NULL) {
176 * configure a window that was not mapped by us
178 xwc.x = xcre->x;
179 xwc.y = xcre->y;
180 xwc.width = xcre->width;
181 xwc.height = xcre->height;
182 xwc.border_width = xcre->border_width;
183 xwc.stack_mode = xcre->detail;
184 xwc.sibling = xcre->above;
185 XConfigureWindow(dpy, xcre->window, xcre->value_mask, &xwc);
186 return;
188 #ifdef SHAPE
190 int junk;
191 unsigned int ujunk;
192 int b_shaped;
194 XShapeSelectInput(dpy, wwin->client_win, ShapeNotifyMask);
195 XShapeQueryExtents(dpy, wwin->client_win, &b_shaped, &junk, &junk,
196 &ujunk, &ujunk, &junk, &junk, &junk, &ujunk,
197 &ujunk);
198 wwin->flags.shaped = b_shaped;
200 #endif
201 if (xcre->value_mask & CWStackMode) {
202 WObjDescriptor *desc;
203 WWindow *sibling;
205 if ((xcre->value_mask & CWSibling) &&
206 (XFindContext(dpy, xcre->above, wWinContext,
207 (XPointer *)&desc) == XCSUCCESS)
208 && (desc->parent_type==WCLASS_WINDOW)) {
209 sibling=desc->parent;
210 xwc.sibling = sibling->frame->core->window;
211 } else {
212 xwc.sibling = xcre->above;
214 xwc.stack_mode = xcre->detail;
215 XConfigureWindow(dpy, wwin->frame->core->window,
216 xcre->value_mask & (CWSibling | CWStackMode), &xwc);
217 /* fix stacking order */
218 RemakeStackList(wwin->screen_ptr);
221 wClientGetGravityOffsets(wwin, &ofs_x, &ofs_y);
223 if (xcre->value_mask & CWBorderWidth) {
224 wwin->old_border_width = xcre->border_width;
227 if (!wwin->flags.shaded) {
228 /* If the window is shaded, wrong height will be set for the window */
229 if (xcre->value_mask & CWX)
230 nx = xcre->x - FRAME_BORDER_WIDTH;
231 else
232 nx = wwin->frame_x;
234 if (xcre->value_mask & CWY)
235 ny = xcre->y - FRAME_BORDER_WIDTH - ((ofs_y < 0) ? 0 : wwin->frame->top_width);
236 else
237 ny = wwin->frame_y;
239 if (xcre->value_mask & CWWidth)
240 nwidth = xcre->width;
241 else
242 nwidth = wwin->frame->core->width;
244 if (xcre->value_mask & CWHeight)
245 nheight = xcre->height;
246 else
247 nheight = wwin->frame->core->height - wwin->frame->top_width - wwin->frame->bottom_width;
249 wWindowConfigure(wwin, nx, ny, nwidth, nheight);
250 wwin->old_geometry.x = nx;
251 wwin->old_geometry.y = ny;
252 wwin->old_geometry.width = nwidth;
253 wwin->old_geometry.height = nheight;
258 void
259 wClientSendProtocol(WWindow *wwin, Atom protocol, Time time)
261 XEvent event;
263 event.xclient.type = ClientMessage;
264 event.xclient.message_type = _XA_WM_PROTOCOLS;
265 event.xclient.format = 32;
266 event.xclient.display = dpy;
267 event.xclient.window = wwin->client_win;
268 event.xclient.data.l[0] = protocol;
269 event.xclient.data.l[1] = time;
270 event.xclient.data.l[2] = 0;
271 event.xclient.data.l[3] = 0;
272 XSendEvent(dpy, wwin->client_win, False, NoEventMask, &event);
273 XSync(dpy, False);
278 void
279 wClientKill(WWindow *wwin)
281 XKillClient(dpy, wwin->client_win);
282 XFlush(dpy);
286 *----------------------------------------------------------------------
287 * wClientCheckProperty--
288 * Handles PropertyNotify'es, verifying which property was
289 * changed and updating internal state according to that, like redrawing
290 * the icon title when it is changed.
292 * Side effects:
293 * Depends on the changed property.
295 * TODO: CLIENT_LEADER, _GNUSTEP_WM_ATTR
296 *----------------------------------------------------------------------
298 void
299 wClientCheckProperty(WWindow *wwin, XPropertyEvent *event)
301 XWindowAttributes attribs;
302 XWMHints *new_hints;
303 int i, g1, g2;
304 char *tmp;
306 switch (event->atom) {
307 case XA_WM_NAME:
308 /* window title was changed */
309 if (wwin->frame) {
310 if (!wFetchName(dpy, wwin->client_win, &tmp)) {
311 /* the hint was removed */
312 tmp = wstrdup(DEF_WINDOW_TITLE);
314 if (wFrameWindowChangeTitle(wwin->frame, tmp)) {
315 /* only update the menu if the title has actually changed */
316 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE);
318 if (tmp)
319 free(tmp);
321 break;
323 case XA_WM_ICON_NAME:
324 if (!wwin->icon)
325 break;
326 else {
327 char *new_title;
329 /* icon title was changed */
330 wGetIconName(dpy, wwin->client_win, &new_title);
331 wIconChangeTitle(wwin->icon, new_title);
333 break;
335 case XA_WM_COMMAND:
336 if (wwin->main_window!=None) {
337 WApplication *wapp = wApplicationOf(wwin->main_window);
338 char **argv;
339 int argc;
341 if (wapp && wapp->app_icon) {
342 if (wapp->app_icon->command!=NULL)
343 free(wapp->app_icon->command);
345 if (XGetCommand(dpy, wwin->main_window, &argv, &argc)) {
346 if (argc > 0 && argv != NULL)
347 wapp->app_icon->command = FlattenStringList(argv,argc);
348 if (argv) {
349 XFreeStringList(argv);
354 break;
356 case XA_WM_HINTS:
357 /* WM_HINTS */
359 new_hints = XGetWMHints(dpy, wwin->client_win);
361 /* group leader update
363 * This means that the window is setting the leader after
364 * it was mapped, changing leaders or removing the leader.
366 * Valid state transitions are:
368 * _1 __2
369 * / \ / \
370 * v | v |
371 * (GC) (GC')
372 * / ^ / ^
373 * 3| |4 5| |6
374 * | | | |
375 * v / v /
376 * (G'C) (G'C')
378 * Where G is the window_group hint, C is CLIENT_LEADER property
379 * and ' indicates the hint is unset.
381 * 1,2 - change group leader to new value of window_group
382 * 3 - change leader to value of CLIENT_LEADER
383 * 4 - change leader to value of window_group
384 * 5 - destroy application
385 * 6 - create application
387 if (new_hints && (new_hints->flags & WindowGroupHint)
388 && new_hints->window_group!=None) {
389 g2 = 1;
390 } else {
391 g2 = 0;
393 if (wwin->wm_hints && (wwin->wm_hints->flags & WindowGroupHint)
394 && wwin->wm_hints->window_group!=None) {
395 g1 = 1;
396 } else {
397 g1 = 0;
400 if (wwin->client_leader) {
401 if (g1 && g2
402 && wwin->wm_hints->window_group!=new_hints->window_group) {
403 i = 1;
404 } else if (g1 && !g2) {
405 i = 3;
406 } else if (!g1 && g2) {
407 i = 4;
408 } else {
409 i = 0;
411 } else {
412 if (g1 && g2
413 && wwin->wm_hints->window_group!=new_hints->window_group) {
414 i = 2;
415 } else if (g1 && !g2) {
416 i = 5;
417 } else if (!g1 && g2) {
418 i = 6;
419 } else {
420 i = 0;
424 if (wwin->wm_hints)
425 XFree(wwin->wm_hints);
427 wwin->wm_hints = new_hints;
429 /* do action according to state transition */
430 switch (i) {
431 /* 3 - change leader to value of CLIENT_LEADER */
432 case 3:
433 wApplicationDestroy(wApplicationOf(wwin->main_window));
434 wwin->main_window = wwin->client_leader;
435 wwin->group_id = None;
436 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
437 break;
439 /* 1,2,4 - change leader to new value of window_group */
440 case 1:
441 case 2:
442 case 4:
443 wApplicationDestroy(wApplicationOf(wwin->main_window));
444 wwin->main_window = new_hints->window_group;
445 wwin->group_id = wwin->main_window;
446 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
447 break;
449 /* 5 - destroy application */
450 case 5:
451 wApplicationDestroy(wApplicationOf(wwin->main_window));
452 wwin->main_window = None;
453 wwin->group_id = None;
454 break;
456 /* 6 - create application */
457 case 6:
458 wwin->main_window = new_hints->window_group;
459 wwin->group_id = wwin->main_window;
460 wApplicationCreate(wwin->screen_ptr, wwin->main_window);
461 break;
463 #ifdef DEBUG
464 if (i) {
465 printf("window leader update caused state transition %i\n",i);
467 #endif
469 if (wwin->wm_hints) {
470 /* update icon */
471 if ((wwin->wm_hints->flags & IconPixmapHint)
472 || (wwin->wm_hints->flags & IconWindowHint)) {
473 WApplication *wapp;
475 if (wwin->flags.miniaturized) {
476 wIconUpdate(wwin->icon);
478 wapp = wApplicationOf(wwin->main_window);
479 if (wapp && wapp->app_icon) {
480 wIconUpdate(wapp->app_icon->icon);
484 if (wwin->wm_hints->flags & UrgencyHint)
485 wwin->flags.urgent = 1;
486 else
487 wwin->flags.urgent = 0;
488 } else {
489 wwin->group_id = None;
491 break;
493 case XA_WM_NORMAL_HINTS:
494 /* normal (geometry) hints */
496 XGetWindowAttributes(dpy, wwin->client_win, &attribs);
497 GetNormalHints(wwin, &attribs, False);
498 break;
500 case XA_WM_TRANSIENT_FOR:
502 Window new_owner;
503 WWindow *owner;
505 if (!XGetTransientForHint(dpy, wwin->client_win, &new_owner)) {
506 new_owner = None;
507 } else {
508 if (new_owner==0 || new_owner == wwin->client_win) {
509 new_owner = wwin->screen_ptr->root_win;
512 if (new_owner!=wwin->transient_for) {
513 owner = wWindowFor(wwin->transient_for);
514 if (owner) {
515 if (owner->flags.semi_focused) {
516 owner->flags.semi_focused = 0;
517 if ((owner->flags.mapped || owner->flags.shaded)
518 && owner->frame)
519 wFrameWindowPaint(owner->frame);
522 owner = wWindowFor(new_owner);
523 if (owner) {
524 if (!owner->flags.semi_focused) {
525 owner->flags.semi_focused = 1;
526 if ((owner->flags.mapped || owner->flags.shaded)
527 && owner->frame)
528 wFrameWindowPaint(owner->frame);
531 wwin->transient_for = new_owner;
532 if (new_owner==None) {
533 if (wwin->window_flags.no_miniaturizable) {
534 wwin->window_flags.no_miniaturizable = 0;
535 wwin->window_flags.no_miniaturize_button = 0;
536 if (wwin->frame)
537 wWindowConfigureBorders(wwin);
539 } else if (!wwin->window_flags.no_miniaturizable) {
540 wwin->window_flags.no_miniaturizable = 1;
541 wwin->window_flags.no_miniaturize_button = 1;
542 if (wwin->frame)
543 wWindowConfigureBorders(wwin);
547 break;
549 default:
550 if (event->atom==_XA_WM_PROTOCOLS) {
551 PropGetProtocols(wwin->client_win, &wwin->protocols);
552 if (wwin->protocols.DELETE_WINDOW) {
553 wwin->window_flags.kill_close = 0;
554 } else {
555 wwin->window_flags.kill_close = 1;
557 if (wwin->frame)
558 wWindowUpdateButtonImages(wwin);
560 } else if (event->atom==_XA_WM_COLORMAP_WINDOWS) {
562 GetColormapWindows(wwin);
563 wColormapInstallForWindow(wwin->screen_ptr, wwin);
565 } else if (event->atom==_XA_WINDOWMAKER_MENU) {
566 WApplication *wapp;
568 wapp = wApplicationOf(wwin->main_window);
569 if (wapp) {
570 if (wapp->menu) {
571 /* update menu */
572 /* TODO: remake appmenu update */
573 wAppMenuDestroy(wapp->menu);
575 wapp->menu = wAppMenuGet(wwin->screen_ptr, wwin->main_window);
576 /* make the appmenu be mapped */
577 wSetFocusTo(wwin->screen_ptr, NULL);
578 wSetFocusTo(wwin->screen_ptr,
579 wwin->screen_ptr->focused_window);
581 } else if (event->atom==_XA_GNUSTEP_WM_ATTR) {
582 GNUstepWMAttributes *attr;
584 PropGetGNUstepWMAttr(wwin->client_win, &attr);
586 wWindowUpdateGNUstepAttr(wwin, attr);
588 XFree(attr);
595 *----------------------------------------------------------------------
596 * GetNormalHints--
597 * Get size (normal) hints and a default geometry for the client
598 * window. The hints are also checked for inconsistency.
600 * Side effects:
601 * normal_hints is filled with valid data and geometry information
602 * about the client is also changed.
603 *----------------------------------------------------------------------
605 void
606 GetNormalHints(WWindow *wwin, XWindowAttributes *wattribs, int geometry)
608 int pre_icccm=0;
610 /* find a position for the window */
611 if (!wwin->normal_hints)
612 wwin->normal_hints = XAllocSizeHints();
614 if (!PropGetNormalHints(wwin->client_win, wwin->normal_hints, &pre_icccm)) {
615 wwin->normal_hints->flags = 0;
617 wwin->client.x = wattribs->x;
618 wwin->client.y = wattribs->y;
620 wwin->client.width = wattribs->width;
621 wwin->client.height = wattribs->height;
623 if (!(wwin->normal_hints->flags & PWinGravity)) {
624 wwin->normal_hints->win_gravity = NorthWestGravity;
626 if (!(wwin->normal_hints->flags & PMinSize)) {
627 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
628 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
630 if (!(wwin->normal_hints->flags & PBaseSize)) {
631 wwin->normal_hints->base_width = 0;
632 wwin->normal_hints->base_height = 0;
634 if (!(wwin->normal_hints->flags & PMaxSize)) {
635 wwin->normal_hints->max_width = wwin->screen_ptr->scr_width*2;
636 wwin->normal_hints->max_height = wwin->screen_ptr->scr_height*2;
639 if (!(wwin->normal_hints->flags & PResizeInc)) {
640 wwin->normal_hints->width_inc = 1;
641 wwin->normal_hints->height_inc = 1;
642 } else {
643 if (wwin->normal_hints->width_inc <= 0)
644 wwin->normal_hints->width_inc = 1;
645 if (wwin->normal_hints->height_inc <= 0)
646 wwin->normal_hints->height_inc = 1;
649 if (wwin->normal_hints->flags & PAspect) {
650 if (wwin->normal_hints->min_aspect.x < 1)
651 wwin->normal_hints->min_aspect.x = 1;
652 if (wwin->normal_hints->min_aspect.y < 1)
653 wwin->normal_hints->min_aspect.y = 1;
655 if (wwin->normal_hints->max_aspect.x < 1)
656 wwin->normal_hints->max_aspect.x = 1;
657 if (wwin->normal_hints->max_aspect.y < 1)
658 wwin->normal_hints->max_aspect.y = 1;
661 if (wwin->normal_hints->min_width <= 0)
662 wwin->normal_hints->min_width = MIN_WINDOW_SIZE;
664 if (wwin->normal_hints->min_height <= 0)
665 wwin->normal_hints->min_height = MIN_WINDOW_SIZE;
667 if (wwin->normal_hints->min_height > wwin->normal_hints->max_height) {
668 wwin->normal_hints->min_height = wwin->normal_hints->max_height;
670 if (wwin->normal_hints->min_width > wwin->normal_hints->max_width) {
671 wwin->normal_hints->min_width = wwin->normal_hints->max_width;
674 /* pre ICCCM (old) client */
675 if (pre_icccm && !wwin->screen_ptr->flags.startup && geometry) {
676 #ifdef DEBUG
677 printf("PRE ICCCM\n");
678 #endif
679 if (wwin->normal_hints->flags & (USPosition|PPosition)) {
680 wwin->client.x = wwin->normal_hints->x;
681 wwin->client.y = wwin->normal_hints->y;
683 if (wwin->normal_hints->flags & (USSize|PSize)) {
684 wwin->client.width = wwin->normal_hints->width;
685 wwin->client.height = wwin->normal_hints->height;
691 void
692 GetColormapWindows(WWindow *wwin)
694 if (wwin->cmap_windows) {
695 XFree(wwin->cmap_windows);
697 if (!XGetWMColormapWindows(dpy, wwin->client_win, &(wwin->cmap_windows),
698 &(wwin->cmap_window_no))) {
699 wwin->cmap_window_no = 0;
700 wwin->cmap_windows = NULL;