changed indentation to use spaces only
[wmaker-crm.git] / src / screen.c
blob61f4407bada089431b5fe90268a47ddaacff2bf9
1 /* screen.c - screen management
3 * Window Maker window manager
5 * Copyright (c) 1997-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.
23 #include "wconfig.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include <X11/Xlib.h>
30 #include <X11/Xutil.h>
31 #include <X11/Xatom.h>
32 #ifdef SHAPE
33 #include <X11/extensions/shape.h>
34 #endif
35 #ifdef KEEP_XKB_LOCK_STATUS
36 #include <X11/XKBlib.h>
37 #endif /* KEEP_XKB_LOCK_STATUS */
39 #include <wraster.h>
41 #include "WindowMaker.h"
42 #include "def_pixmaps.h"
43 #include "screen.h"
44 #include "texture.h"
45 #include "pixmap.h"
46 #include "menu.h"
47 #include "funcs.h"
48 #include "actions.h"
49 #include "properties.h"
50 #include "dock.h"
51 #include "resources.h"
52 #include "workspace.h"
53 #include "session.h"
54 #include "balloon.h"
55 #include "geomview.h"
56 #ifdef KWM_HINTS
57 # include "kwm.h"
58 #endif
59 #ifdef GNOME_STUFF
60 # include "gnome.h"
61 #endif
62 #ifdef OLWM_HINTS
63 # include "openlook.h"
64 #endif
65 #ifdef NETWM_HINTS
66 # include "wmspec.h"
67 #endif
69 #include "xinerama.h"
71 #include <WINGs/WUtil.h>
73 #include "defaults.h"
76 #ifdef LITE
77 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
78 |SubstructureNotifyMask|PointerMotionMask \
79 |SubstructureRedirectMask|KeyPressMask|KeyReleaseMask)
80 #else
81 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
82 |SubstructureNotifyMask|PointerMotionMask \
83 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
84 |KeyPressMask|KeyReleaseMask)
85 #endif
87 /**** Global variables ****/
89 extern Cursor wCursor[WCUR_LAST];
90 extern WPreferences wPreferences;
91 extern Atom _XA_WINDOWMAKER_STATE;
92 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
95 extern int wScreenCount;
97 #ifdef KEEP_XKB_LOCK_STATUS
98 extern int wXkbSupported;
99 #endif
101 extern WDDomain *WDWindowMaker;
104 /**** Local ****/
106 #define STIPPLE_WIDTH 2
107 #define STIPPLE_HEIGHT 2
108 static char STIPPLE_DATA[] = {0x02, 0x01};
110 static int CantManageScreen = 0;
112 static WMPropList *dApplications = NULL;
113 static WMPropList *dWorkspace;
114 static WMPropList *dDock;
115 static WMPropList *dClip;
118 static void
119 make_keys()
121 if (dApplications!=NULL)
122 return;
124 dApplications = WMCreatePLString("Applications");
125 dWorkspace = WMCreatePLString("Workspace");
126 dDock = WMCreatePLString("Dock");
127 dClip = WMCreatePLString("Clip");
132 *----------------------------------------------------------------------
133 * alreadyRunningError--
134 * X error handler used to catch errors when trying to do
135 * XSelectInput() on the root window. These errors probably mean that
136 * there already is some other window manager running.
138 * Returns:
139 * Nothing, unless something really evil happens...
141 * Side effects:
142 * CantManageScreen is set to 1;
143 *----------------------------------------------------------------------
145 static int
146 alreadyRunningError(Display *dpy, XErrorEvent *error)
148 CantManageScreen = 1;
149 return -1;
154 *----------------------------------------------------------------------
155 * allocButtonPixmaps--
156 * Allocate pixmaps used on window operation buttons (those in the
157 * titlebar). The pixmaps are linked to the program. If XPM is supported
158 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
160 * Returns:
161 * Nothing
163 * Side effects:
164 * Allocates shared pixmaps for the screen. These pixmaps should
165 * not be freed by anybody.
166 *----------------------------------------------------------------------
168 static void
169 allocButtonPixmaps(WScreen *scr)
171 WPixmap *pix;
173 /* create predefined pixmaps */
174 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
175 if (pix)
176 pix->shared = 1;
177 scr->b_pixmaps[WBUT_CLOSE] = pix;
179 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
180 if (pix)
181 pix->shared = 1;
182 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
184 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
185 if (pix)
186 pix->shared = 1;
187 scr->b_pixmaps[WBUT_ICONIFY] = pix;
188 #ifdef XKB_BUTTON_HINT
189 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
190 if (pix)
191 pix->shared = 1;
192 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
193 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
194 if (pix)
195 pix->shared = 1;
196 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
197 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
198 if (pix)
199 pix->shared = 1;
200 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
201 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
202 if (pix)
203 pix->shared = 1;
204 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
205 #endif
208 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
209 if (pix)
210 pix->shared = 1;
211 scr->b_pixmaps[WBUT_KILL] = pix;
215 static void
216 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
218 XSetForeground(dpy, gc, scr->black_pixel);
219 XDrawLine(dpy, d, gc, x, y, x+1, y);
220 XDrawPoint(dpy, d, gc, x, y+1);
221 XSetForeground(dpy, gc, scr->white_pixel);
222 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
223 XDrawPoint(dpy, d, gc, x+1, y+1);
227 static WPixmap*
228 make3Dots(WScreen *scr)
230 WPixmap *wpix;
231 GC gc2, gc;
232 XGCValues gcv;
233 Pixmap pix, mask;
235 gc = scr->copy_gc;
236 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
237 wPreferences.icon_size, scr->w_depth);
238 XSetForeground(dpy, gc, scr->black_pixel);
239 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
240 wPreferences.icon_size);
241 XSetForeground(dpy, gc, scr->white_pixel);
242 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
243 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
244 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
246 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
247 wPreferences.icon_size, 1);
248 gcv.foreground = 0;
249 gcv.graphics_exposures = False;
250 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
251 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
252 wPreferences.icon_size);
253 XSetForeground(dpy, gc2, 1);
254 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
255 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
256 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
258 XFreeGC(dpy, gc2);
260 wpix = wPixmapCreate(scr, pix, mask);
261 wpix->shared = 1;
263 return wpix;
267 static void
268 allocGCs(WScreen *scr)
270 XGCValues gcv;
271 XColor color;
272 int gcm;
274 scr->stipple_bitmap =
275 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
276 STIPPLE_HEIGHT);
278 gcv.stipple = scr->stipple_bitmap;
279 gcv.foreground = scr->white_pixel;
280 gcv.fill_style = FillStippled;
281 gcv.graphics_exposures = False;
282 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
283 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
286 /* selected icon border GCs */
287 gcv.function = GXcopy;
288 gcv.foreground = scr->white_pixel;
289 gcv.background = scr->black_pixel;
290 gcv.line_width = 1;
291 gcv.line_style = LineDoubleDash;
292 gcv.fill_style = FillSolid;
293 gcv.dash_offset = 0;
294 gcv.dashes = 4;
295 gcv.graphics_exposures = False;
297 gcm = GCFunction | GCGraphicsExposures;
298 gcm |= GCForeground | GCBackground;
299 gcm |= GCLineWidth | GCLineStyle;
300 gcm |= GCFillStyle;
301 gcm |= GCDashOffset | GCDashList;
303 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
305 scr->menu_title_color[0] = WMRetainColor(scr->white);
307 /* don't retain scr->black here because we may alter its alpha */
308 scr->mtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
309 scr->dtext_color = WMCreateRGBColor(scr->wmscreen, 0, 0, 0, True);
311 /* frame GC */
312 wGetColor(scr, DEF_FRAME_COLOR, &color);
313 gcv.function = GXxor;
314 /* this will raise the probability of the XORed color being different
315 * of the original color in PseudoColor when not all color cells are
316 * initialized */
317 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
318 gcv.plane_mask = (1<<(scr->depth-1))|1;
319 else
320 gcv.plane_mask = AllPlanes;
321 gcv.foreground = color.pixel;
322 if (gcv.foreground == 0)
323 gcv.foreground = 1;
324 gcv.line_width = DEF_FRAME_THICKNESS;
325 gcv.subwindow_mode = IncludeInferiors;
326 gcv.graphics_exposures = False;
327 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
328 |GCFunction|GCSubwindowMode|GCLineWidth
329 |GCPlaneMask, &gcv);
331 /* line GC */
332 gcv.foreground = color.pixel;
334 if (gcv.foreground == 0)
335 /* XOR:ing with a zero is not going to be of much use, so
336 in that case, we somewhat arbitrarily xor with 17 instead. */
337 gcv.foreground = 17;
339 gcv.function = GXxor;
340 gcv.subwindow_mode = IncludeInferiors;
341 gcv.line_width = 1;
342 gcv.cap_style = CapRound;
343 gcv.graphics_exposures = False;
344 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
345 |GCGraphicsExposures;
346 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
348 scr->line_pixel = gcv.foreground;
350 /* copy GC */
351 gcv.foreground = scr->white_pixel;
352 gcv.background = scr->black_pixel;
353 gcv.graphics_exposures = False;
354 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
355 |GCGraphicsExposures, &gcv);
357 /* misc drawing GC */
358 gcv.graphics_exposures = False;
359 gcm = GCGraphicsExposures;
360 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
362 assert (scr->stipple_bitmap!=None);
365 /* mono GC */
366 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
371 static void
372 createPixmaps(WScreen *scr)
374 WPixmap *pix;
375 RImage *image;
377 /* load pixmaps */
378 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
379 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
380 MENU_RADIO_INDICATOR_XBM_SIZE,
381 MENU_RADIO_INDICATOR_XBM_SIZE,
382 scr->black_pixel, scr->white_pixel);
383 if (pix!=NULL)
384 pix->shared = 1;
385 scr->menu_radio_indicator = pix;
388 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
389 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
390 MENU_CHECK_INDICATOR_XBM_SIZE,
391 MENU_CHECK_INDICATOR_XBM_SIZE,
392 scr->black_pixel, scr->white_pixel);
393 if (pix!=NULL)
394 pix->shared = 1;
395 scr->menu_check_indicator = pix;
397 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
398 (char*)MENU_MINI_INDICATOR_XBM_DATA,
399 MENU_MINI_INDICATOR_XBM_SIZE,
400 MENU_MINI_INDICATOR_XBM_SIZE,
401 scr->black_pixel, scr->white_pixel);
402 if (pix!=NULL)
403 pix->shared = 1;
404 scr->menu_mini_indicator = pix;
406 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
407 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
408 MENU_HIDE_INDICATOR_XBM_SIZE,
409 MENU_HIDE_INDICATOR_XBM_SIZE,
410 scr->black_pixel, scr->white_pixel);
411 if (pix!=NULL)
412 pix->shared = 1;
413 scr->menu_hide_indicator = pix;
415 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
416 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
417 MENU_SHADE_INDICATOR_XBM_SIZE,
418 MENU_SHADE_INDICATOR_XBM_SIZE,
419 scr->black_pixel, scr->white_pixel);
420 if (pix!=NULL)
421 pix->shared = 1;
422 scr->menu_shade_indicator = pix;
425 image = wDefaultGetImage(scr, "Logo", "WMPanel");
427 if (!image) {
428 wwarning(_("could not load logo image for panels: %s"),
429 RMessageForError(RErrorCode));
430 } else {
431 WMSetApplicationIconImage(scr->wmscreen, image);
432 RReleaseImage(image);
435 scr->dock_dots = make3Dots(scr);
437 /* titlebar button pixmaps */
438 allocButtonPixmaps(scr);
443 *----------------------------------------------------------------------
444 * createInternalWindows--
445 * Creates some windows used internally by the program. One to
446 * receive input focus when no other window can get it and another
447 * to display window geometry information during window resize/move.
449 * Returns:
450 * Nothing
452 * Side effects:
453 * Windows are created and some colors are allocated for the
454 * window background.
455 *----------------------------------------------------------------------
457 static void
458 createInternalWindows(WScreen *scr)
460 int vmask;
461 XSetWindowAttributes attribs;
463 /* InputOnly window to get the focus when no other window can get it */
464 vmask = CWEventMask|CWOverrideRedirect;
465 attribs.event_mask = KeyPressMask|FocusChangeMask;
466 attribs.override_redirect = True;
467 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
468 InputOnly,CopyFromParent, vmask, &attribs);
469 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
470 XMapWindow(dpy, scr->no_focus_win);
472 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
474 /* shadow window for dock buttons */
475 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
476 attribs.border_pixel = scr->black_pixel;
477 attribs.save_under = True;
478 attribs.override_redirect = True;
479 attribs.background_pixmap = None;
480 attribs.background_pixel = scr->white_pixel;
481 attribs.cursor = wCursor[WCUR_DEFAULT];
482 vmask |= CWColormap;
483 attribs.colormap = scr->w_colormap;
484 scr->dock_shadow =
485 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
486 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
487 scr->w_visual, vmask, &attribs);
489 /* workspace name balloon for clip */
490 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
491 |CWBorderPixel;
492 attribs.save_under = True;
493 attribs.override_redirect = True;
494 attribs.colormap = scr->w_colormap;
495 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
496 attribs.border_pixel = 0; /* do not care */
497 scr->clip_balloon =
498 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
499 CopyFromParent, scr->w_visual, vmask, &attribs);
502 /* workspace name */
503 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
504 |CWBorderPixel;
505 attribs.save_under = True;
506 attribs.override_redirect = True;
507 attribs.colormap = scr->w_colormap;
508 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
509 attribs.border_pixel = 0; /* do not care */
510 scr->workspace_name =
511 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
512 CopyFromParent, scr->w_visual, vmask, &attribs);
515 * If the window is clicked without having ButtonPress selected, the
516 * resulting event will have event.xbutton.window == root.
518 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
522 #if 0
523 static Bool
524 aquireManagerSelection(WScreen *scr)
526 char buffer[32];
527 XEvent ev;
528 Time timestamp;
530 snprintf(buffer, sizeof(buffer), "WM_S%i", scr->screen);
531 scr->managerAtom = XInternAtom(dpy, buffer, False);
533 /* for race-conditions... */
534 XGrabServer(dpy);
536 /* if there is another manager running, don't try to replace it
537 * (for now, at least) */
538 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
539 XUngrabServer(dpy);
540 return False;
543 /* become the manager for this screen */
545 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
546 0, 0, 0);
548 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
549 /* get a timestamp */
550 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
551 XA_INTEGER, 32, PropModeAppend, NULL, 0);
552 while (1) {
553 XWindowEvent(dpy, scr->managerWindow, &ev);
554 if (ev.type == PropertyNotify) {
555 timestamp = ev.xproperty.time;
556 break;
559 XSelectInput(dpy, scr->managerWindow, NoEvents);
560 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
562 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
564 XUngrabServer(dpy);
566 /* announce our arrival */
568 ev.xclient.type = ClientMessage;
569 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
570 ev.xclient.destination = scr->root_win;
571 ev.xclient.format = 32;
572 ev.xclient.data.l[0] = timestamp;
573 ev.xclient.data.l[1] = scr->managerAtom;
574 ev.xclient.data.l[2] = scr->managerWindow;
575 ev.xclient.data.l[3] = 0;
576 ev.xclient.data.l[4] = 0;
578 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
579 XSync(dpy, False);
581 return True;
583 #endif
586 *----------------------------------------------------------------------
587 * wScreenInit--
588 * Initializes the window manager for the given screen and
589 * allocates a WScreen descriptor for it. Many resources are allocated
590 * for the screen and the root window is setup appropriately.
592 * Returns:
593 * The WScreen descriptor for the screen.
595 * Side effects:
596 * Many resources are allocated and the IconSize property is
597 * set on the root window.
598 * The program can be aborted if some fatal error occurs.
600 * TODO: User specifiable visual.
601 *----------------------------------------------------------------------
603 WScreen*
604 wScreenInit(int screen_number)
606 WScreen *scr;
607 XIconSize icon_size[1];
608 RContextAttributes rattr;
609 extern int wVisualID;
610 long event_mask;
611 XErrorHandler oldHandler;
612 int i;
614 scr = wmalloc(sizeof(WScreen));
615 memset(scr, 0, sizeof(WScreen));
617 scr->stacking_list = WMCreateTreeBag();
619 /* initialize globals */
620 scr->screen = screen_number;
621 scr->root_win = RootWindow(dpy, screen_number);
622 scr->depth = DefaultDepth(dpy, screen_number);
623 scr->colormap = DefaultColormap(dpy, screen_number);
625 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
626 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
628 wInitXinerama(scr);
630 scr->usableArea = (WArea *)wmalloc(sizeof(WArea)*wXineramaHeads(scr));
631 scr->totalUsableArea = (WArea *)wmalloc(sizeof(WArea)*wXineramaHeads(scr));
633 for (i=0; i<wXineramaHeads(scr); ++i) {
634 WMRect rect = wGetRectForHead(scr, i);
635 scr->usableArea[i].x1 = scr->totalUsableArea[i].x1 = rect.pos.x;
636 scr->usableArea[i].y1 = scr->totalUsableArea[i].y1 = rect.pos.y;
637 scr->usableArea[i].x2 = scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
638 scr->usableArea[i].y2 = scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
641 scr->fakeGroupLeaders = WMCreateArray(16);
643 #if 0
644 if (!aquireManagerSelection(scr)) {
645 wfree(scr);
647 return NULL;
649 #endif
650 CantManageScreen = 0;
651 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
653 event_mask = EVENT_MASK;
655 if (wPreferences.disable_root_mouse) {
656 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
659 XSelectInput(dpy, scr->root_win, event_mask);
661 #ifdef KEEP_XKB_LOCK_STATUS
662 /* Only GroupLock doesn't work correctly in my system since right-alt
663 * can change mode while holding it too - ]d
665 if (wXkbSupported) {
666 XkbSelectEvents(dpy,XkbUseCoreKbd,
667 XkbStateNotifyMask,
668 XkbStateNotifyMask);
670 #endif /* KEEP_XKB_LOCK_STATUS */
672 XSync(dpy, False);
673 XSetErrorHandler(oldHandler);
675 if (CantManageScreen) {
676 wfree(scr);
677 return NULL;
680 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_ROOT]);
682 /* screen descriptor for raster graphic library */
683 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
684 rattr.render_mode = wPreferences.no_dithering
685 ? RBestMatchRendering
686 : RDitheredRendering;
688 /* if the std colormap stuff works ok, this will be ignored */
689 rattr.colors_per_channel = wPreferences.cmap_size;
690 if (rattr.colors_per_channel<2)
691 rattr.colors_per_channel = 2;
694 /* will only be accounted for in PseudoColor */
695 if (wPreferences.flags.create_stdcmap) {
696 rattr.standard_colormap_mode = RCreateStdColormap;
697 } else {
698 rattr.standard_colormap_mode = RUseStdColormap;
701 if (wVisualID>=0) {
702 rattr.flags |= RC_VisualID;
703 rattr.visualid = wVisualID;
706 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
708 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
709 wwarning(RMessageForError(RErrorCode));
711 rattr.flags &= ~RC_StandardColormap;
712 rattr.standard_colormap_mode = RUseStdColormap;
714 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
717 if (!scr->rcontext) {
718 wwarning(_("could not initialize graphics library context: %s"),
719 RMessageForError(RErrorCode));
720 wAbort(False);
721 } else {
722 char **formats;
723 int i = 0;
725 formats = RSupportedFileFormats();
726 if (formats) {
727 for (i=0; formats[i]!=NULL; i++) {
728 if (strcmp(formats[i], "TIFF")==0) {
729 scr->flags.supports_tiff = 1;
730 break;
736 scr->w_win = scr->rcontext->drawable;
737 scr->w_visual = scr->rcontext->visual;
738 scr->w_depth = scr->rcontext->depth;
739 scr->w_colormap = scr->rcontext->cmap;
741 /* create screen descriptor for WINGs */
742 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
743 scr->rcontext);
745 if (!scr->wmscreen) {
746 wfatal(_("could not initialize WINGs widget set"));
747 return NULL;
750 scr->black = WMBlackColor(scr->wmscreen);
751 scr->white = WMWhiteColor(scr->wmscreen);
752 scr->gray = WMGrayColor(scr->wmscreen);
753 scr->darkGray = WMDarkGrayColor(scr->wmscreen);
755 scr->black_pixel = WMColorPixel(scr->black); /*scr->rcontext->black;*/
756 scr->white_pixel = WMColorPixel(scr->white); /*scr->rcontext->white;*/
757 scr->light_pixel = WMColorPixel(scr->gray);
758 scr->dark_pixel = WMColorPixel(scr->darkGray);
761 XColor xcol;
762 /* frame boder color */
763 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
764 scr->frame_border_pixel = xcol.pixel;
767 /* create GCs with default values */
768 allocGCs(scr);
770 /* for our window manager info notice board. Need to
771 * create before reading the defaults, because it will be used there.
773 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10,
774 0, 0, 0);
776 /* read defaults for this screen */
777 wReadDefaults(scr, WDWindowMaker->dictionary);
779 createInternalWindows(scr);
781 #ifdef KWM_HINTS
782 wKWMInitStuff(scr);
783 #endif
785 #ifdef GNOME_STUFF
786 wGNOMEInitStuff(scr);
787 #endif
789 #ifdef OLWM_HINTS
790 wOLWMInitStuff(scr);
791 #endif
793 #ifdef NETWM_HINTS
794 wNETWMInitStuff(scr);
795 #endif
797 /* create initial workspace */
798 wWorkspaceNew(scr);
800 /* create shared pixmaps */
801 createPixmaps(scr);
803 /* set icon sizes we can accept from clients */
804 icon_size[0].min_width = 8;
805 icon_size[0].min_height = 8;
806 icon_size[0].max_width = wPreferences.icon_size-4;
807 icon_size[0].max_height = wPreferences.icon_size-4;
808 icon_size[0].width_inc = 1;
809 icon_size[0].height_inc = 1;
810 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
812 /* setup WindowMaker protocols property in the root window*/
813 PropSetWMakerProtocols(scr->root_win);
815 /* setup our noticeboard */
816 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
817 XA_WINDOW, 32, PropModeReplace,
818 (unsigned char*)&scr->info_window, 1);
819 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
820 XA_WINDOW, 32, PropModeReplace,
821 (unsigned char*)&scr->info_window, 1);
824 #ifdef BALLOON_TEXT
825 /* initialize balloon text stuff */
826 wBalloonInitialize(scr);
827 #endif
829 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
831 scr->tech_draw_font= XLoadQueryFont(dpy, "-adobe-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*");
833 scr->gview = WCreateGeometryView(scr->wmscreen);
834 WMRealizeWidget(scr->gview);
836 wScreenUpdateUsableArea(scr);
838 return scr;
842 void
843 wScreenUpdateUsableArea(WScreen *scr)
846 * scr->totalUsableArea[] will become the usableArea used for Windowplacement,
847 * scr->usableArea[] will be used for iconplacement, hence no iconyard nor
848 * border.
851 int i;
852 unsigned long best_area = 0, tmp_area;
853 WArea area;
854 int dock_head = scr->xine_info.primary_head;
856 if (scr->dock) {
857 WMRect rect;
858 rect.pos.x = scr->dock->x_pos;
859 rect.pos.y = scr->dock->y_pos;
860 rect.size.width = wPreferences.icon_size;
861 rect.size.height = wPreferences.icon_size;
862 dock_head = wGetHeadForRect(scr, rect);
865 for (i=0; i<wXineramaHeads(scr); ++i) {
866 WMRect rect = wGetRectForHead(scr, i);
867 scr->totalUsableArea[i].x1 = rect.pos.x;
868 scr->totalUsableArea[i].y1 = rect.pos.y;
869 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
870 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
872 if (scr->dock && dock_head==i &&
873 (!scr->dock->lowered || wPreferences.no_window_over_dock)) {
874 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
876 if (scr->dock->on_right_side) {
877 scr->totalUsableArea[i].x2 -= offset;
878 } else {
879 scr->totalUsableArea[i].x1 += offset;
883 #ifdef NETWM_HINTS
885 WArea area;
886 if (wNETWMGetUsableArea(scr, i, &area)) {
887 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
888 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
889 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
890 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
893 #endif
895 #ifdef GNOME_STUFF
897 WArea area;
898 if (wGNOMEGetUsableArea(scr, i, &area)) {
899 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
900 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
901 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
902 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
905 #endif
907 #ifdef KWM_HINTS
909 WArea area;
910 if (wKWMGetUsableArea(scr, i, &area)) {
911 scr->totalUsableArea[i].x1 = WMAX(scr->totalUsableArea[i].x1, area.x1);
912 scr->totalUsableArea[i].y1 = WMAX(scr->totalUsableArea[i].y1, area.y1);
913 scr->totalUsableArea[i].x2 = WMIN(scr->totalUsableArea[i].x2, area.x2);
914 scr->totalUsableArea[i].y2 = WMIN(scr->totalUsableArea[i].y2, area.y2);
917 #endif
919 scr->usableArea[i] = scr->totalUsableArea[i];
921 #if 0
922 printf("usableArea[%d]: %d %d %d %d\n", i,
923 scr->usableArea[i].x1, scr->usableArea[i].x2,
924 scr->usableArea[i].y1, scr->usableArea[i].y2);
925 #endif
927 if (wPreferences.no_window_over_icons) {
928 if (wPreferences.icon_yard & IY_VERT) {
929 if (wPreferences.icon_yard & IY_RIGHT) {
930 scr->totalUsableArea[i].x2 -= wPreferences.icon_size;
931 } else {
932 scr->totalUsableArea[i].x1 += wPreferences.icon_size;
934 } else {
935 if (wPreferences.icon_yard & IY_TOP) {
936 scr->totalUsableArea[i].y1 += wPreferences.icon_size;
937 } else {
938 scr->totalUsableArea[i].y2 -= wPreferences.icon_size;
943 if (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1 < rect.size.width/2) {
944 scr->totalUsableArea[i].x1 = rect.pos.x;
945 scr->totalUsableArea[i].x2 = rect.pos.x + rect.size.width;
948 if (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1 < rect.size.height/2) {
949 scr->totalUsableArea[i].y1 = rect.pos.y;
950 scr->totalUsableArea[i].y2 = rect.pos.y + rect.size.height;
953 tmp_area = (scr->totalUsableArea[i].x2 - scr->totalUsableArea[i].x1) *
954 (scr->totalUsableArea[i].y2 - scr->totalUsableArea[i].y1);
956 if (tmp_area > best_area) {
957 best_area = tmp_area;
958 area = scr->totalUsableArea[i];
962 unsigned size = wPreferences.workspace_border_size;
963 unsigned position = wPreferences.workspace_border_position;
965 if (size>0 && position!=WB_NONE) {
966 if (position & WB_LEFTRIGHT) {
967 scr->totalUsableArea[i].x1 += size;
968 scr->totalUsableArea[i].x2 -= size;
970 if (position & WB_TOPBOTTOM) {
971 scr->totalUsableArea[i].y1 += size;
972 scr->totalUsableArea[i].y2 -= size;
978 #ifdef NETWM_HINTS
979 wNETWMUpdateWorkarea(scr, area);
980 #endif
982 if (wPreferences.auto_arrange_icons) {
983 wArrangeIcons(scr, True);
987 #if 0
988 void
989 wScreenUpdateUsableArea(WScreen *scr)
991 scr->totalUsableArea = scr->usableArea;
993 if (scr->dock && (!scr->dock->lowered
994 || wPreferences.no_window_over_dock)) {
996 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
998 if (scr->dock->on_right_side) {
999 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
1000 scr->scr_width - offset);
1001 } else {
1002 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
1006 if (wPreferences.no_window_over_icons) {
1007 if (wPreferences.icon_yard & IY_VERT) {
1009 if (!(wPreferences.icon_yard & IY_RIGHT)) {
1010 scr->totalUsableArea.x1 += wPreferences.icon_size;
1011 } else {
1012 scr->totalUsableArea.x2 -= wPreferences.icon_size;
1014 } else {
1016 if (wPreferences.icon_yard & IY_TOP) {
1017 scr->totalUsableArea.y1 += wPreferences.icon_size;
1018 } else {
1019 scr->totalUsableArea.y2 -= wPreferences.icon_size;
1024 #ifdef NETWM_HINTS
1026 WArea area;
1027 if (wNETWMGetUsableArea(scr, &area)) {
1028 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
1029 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
1030 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
1031 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
1034 #endif
1036 #ifdef KWM_HINTS
1038 WArea area;
1039 if (wKWMGetUsableArea(scr, &area)) {
1040 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
1041 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
1042 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
1043 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
1046 #endif
1048 #ifdef GNOME_STUFF
1050 WReservedArea *area = scr->reservedAreas;
1052 while (area) {
1053 int th, bh;
1054 int lw, rw;
1055 int w, h;
1057 w = area->area.x2 - area->area.x1;
1058 h = area->area.y2 - area->area.y1;
1060 th = area->area.y1;
1061 bh = scr->scr_height - area->area.y2;
1062 lw = area->area.x1;
1063 rw = scr->scr_width - area->area.x2;
1065 if (WMIN(th, bh) <= WMIN(lw, rw)) {
1066 /* horizontal */
1067 if (th < bh) {
1068 /* on top */
1069 if (scr->totalUsableArea.y1 < area->area.y2)
1070 scr->totalUsableArea.y1 = area->area.y2;
1071 } else {
1072 /* on bottom */
1073 if (scr->totalUsableArea.y2 > area->area.y1)
1074 scr->totalUsableArea.y2 = area->area.y1;
1076 } else {
1077 /* vertical */
1078 if (lw < rw) {
1079 /* on left */
1080 if (scr->totalUsableArea.x1 < area->area.x2)
1081 scr->totalUsableArea.x1 = area->area.x2;
1082 } else {
1083 /* on right */
1084 if (scr->totalUsableArea.x2 > area->area.x1)
1085 scr->totalUsableArea.x2 = area->area.x1;
1089 area = area->next;
1092 #endif /* GNOME_STUFF */
1094 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
1095 scr->totalUsableArea.x2 = scr->usableArea.x2;
1096 scr->totalUsableArea.x1 = scr->usableArea.x1;
1098 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
1099 scr->totalUsableArea.y2 = scr->usableArea.y2;
1100 scr->totalUsableArea.y1 = scr->usableArea.y1;
1103 #ifdef not_used
1104 #ifdef KWM_HINTS
1106 int i;
1108 for (i = 0; i < scr->workspace_count; i++) {
1109 wKWMSetUsableAreaHint(scr, i);
1112 #endif
1113 #endif
1115 #ifdef NETWM_HINTS
1116 wNETWMUpdateWorkarea(scr);
1117 #endif
1120 unsigned size = wPreferences.workspace_border_size;
1121 unsigned position = wPreferences.workspace_border_position;
1123 if (size>0 && position!=WB_NONE) {
1124 if (position & WB_LEFTRIGHT) {
1125 scr->totalUsableArea.x1 += size;
1126 scr->totalUsableArea.x2 -= size;
1128 if (position & WB_TOPBOTTOM) {
1129 scr->totalUsableArea.y1 += size;
1130 scr->totalUsableArea.y2 -= size;
1135 if (wPreferences.auto_arrange_icons) {
1136 wArrangeIcons(scr, True);
1139 #endif
1142 void
1143 wScreenRestoreState(WScreen *scr)
1145 WMPropList *state;
1146 char *path;
1149 #ifndef LITE
1150 OpenRootMenu(scr, -10000, -10000, False);
1151 wMenuUnmap(scr->root_menu);
1152 #endif
1154 make_keys();
1156 if (wScreenCount == 1) {
1157 path = wdefaultspathfordomain("WMState");
1158 } else {
1159 char buf[16];
1160 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1161 path = wdefaultspathfordomain(buf);
1163 scr->session_state = WMReadPropListFromFile(path);
1164 wfree(path);
1165 if (!scr->session_state && wScreenCount>1) {
1166 path = wdefaultspathfordomain("WMState");
1167 scr->session_state = WMReadPropListFromFile(path);
1168 wfree(path);
1171 if (!scr->session_state) {
1172 scr->session_state = WMCreatePLDictionary(NULL, NULL);
1175 if (!wPreferences.flags.nodock) {
1176 state = WMGetFromPLDictionary(scr->session_state, dDock);
1177 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
1180 if (!wPreferences.flags.noclip) {
1181 state = WMGetFromPLDictionary(scr->session_state, dClip);
1182 scr->clip_icon = wClipRestoreState(scr, state);
1185 wWorkspaceRestoreState(scr);
1187 #ifdef VIRTUAL_DESKTOP
1188 /* create inputonly windows at the border of screen */
1189 wWorkspaceManageEdge(scr);
1190 #endif
1192 wScreenUpdateUsableArea(scr);
1196 void
1197 wScreenSaveState(WScreen *scr)
1199 WWindow *wwin;
1200 char *str;
1201 WMPropList *old_state, *foo;
1202 CARD32 data[2];
1205 make_keys();
1207 /* Save current workspace, so can go back to it upon restart. */
1208 data[0] = scr->current_workspace;
1209 data[1] = WFLAGS_NONE;
1211 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1212 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1213 (unsigned char *) data, 2);
1215 /* save state of windows */
1216 wwin = scr->focused_window;
1217 while (wwin) {
1218 wWindowSaveState(wwin);
1219 wwin = wwin->prev;
1223 if (wPreferences.flags.noupdates)
1224 return;
1227 old_state = scr->session_state;
1228 scr->session_state = WMCreatePLDictionary(NULL, NULL);
1230 WMPLSetCaseSensitive(True);
1232 /* save dock state to file */
1233 if (!wPreferences.flags.nodock) {
1234 wDockSaveState(scr, old_state);
1235 } else {
1236 if ((foo = WMGetFromPLDictionary(old_state, dDock))!=NULL) {
1237 WMPutInPLDictionary(scr->session_state, dDock, foo);
1240 if (!wPreferences.flags.noclip) {
1241 wClipSaveState(scr);
1242 } else {
1243 if ((foo = WMGetFromPLDictionary(old_state, dClip))!=NULL) {
1244 WMPutInPLDictionary(scr->session_state, dClip, foo);
1248 wWorkspaceSaveState(scr, old_state);
1250 if (wPreferences.save_session_on_exit) {
1251 wSessionSaveState(scr);
1252 } else {
1253 if ((foo = WMGetFromPLDictionary(old_state, dApplications))!=NULL) {
1254 WMPutInPLDictionary(scr->session_state, dApplications, foo);
1256 if ((foo = WMGetFromPLDictionary(old_state, dWorkspace))!=NULL) {
1257 WMPutInPLDictionary(scr->session_state, dWorkspace, foo);
1261 /* clean up */
1262 WMPLSetCaseSensitive(False);
1264 wMenuSaveState(scr);
1266 if (wScreenCount == 1) {
1267 str = wdefaultspathfordomain("WMState");
1268 } else {
1269 char buf[16];
1270 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1271 str = wdefaultspathfordomain(buf);
1273 if (!WMWritePropListToFile(scr->session_state, str, True)) {
1274 wsyserror(_("could not save session state in %s"), str);
1276 wfree(str);
1277 WMReleasePropList(old_state);
1283 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1285 int moved = 0;
1286 int tol_w, tol_h;
1288 * With respect to the head that contains most of the window.
1290 int sx1, sy1, sx2, sy2;
1292 WMRect rect;
1293 int head, flags;
1295 rect.pos.x = *x;
1296 rect.pos.y = *y;
1297 rect.size.width = width;
1298 rect.size.height = height;
1300 head = wGetRectPlacementInfo(scr, rect, &flags);
1301 rect = wGetRectForHead(scr, head);
1303 sx1 = rect.pos.x;
1304 sy1 = rect.pos.y;
1305 sx2 = sx1 + rect.size.width;
1306 sy2 = sy1 + rect.size.height;
1308 #if 0 /* NOTE: gives funky group movement */
1309 if (flags & XFLAG_MULTIPLE) {
1311 * since we span multiple heads, pull window totaly inside
1313 if (*x < sx1)
1314 *x = sx1, moved = 1;
1315 else if (*x + width > sx2)
1316 *x = sx2 - width, moved = 1;
1318 if (*y < sy1)
1319 *y = sy1, moved = 1;
1320 else if (*y + height > sy2)
1321 *y = sy2 - height, moved = 1;
1323 return moved;
1325 #endif
1327 if (width > 20)
1328 tol_w = width/2;
1329 else
1330 tol_w = 20;
1332 if (height > 20)
1333 tol_h = height/2;
1334 else
1335 tol_h = 20;
1337 if (*x + width < sx1 + 10)
1338 *x = sx1 - tol_w, moved = 1;
1339 else if (*x >= sx2 - 10)
1340 *x = sx2 - tol_w - 1, moved = 1;
1342 if (*y < sy1 - height + 10)
1343 *y = sy1 - tol_h, moved = 1;
1344 else if (*y >= sy2 - 10)
1345 *y = sy2 - tol_h - 1, moved = 1;
1347 return moved;
1353 wScreenKeepInside(WScreen *scr, int *x, int *y, int width, int height)
1355 int moved = 0;
1356 int sx1, sy1, sx2, sy2;
1357 WMRect rect;
1358 int head;
1360 rect.pos.x = *x;
1361 rect.pos.y = *y;
1362 rect.size.width = width;
1363 rect.size.height = height;
1365 head = wGetHeadForRect(scr, rect);
1366 rect = wGetRectForHead(scr, head);
1368 sx1 = rect.pos.x;
1369 sy1 = rect.pos.y;
1370 sx2 = sx1 + rect.size.width;
1371 sy2 = sy1 + rect.size.height;
1373 if (*x < sx1)
1374 *x = sx1, moved = 1;
1375 else if (*x + width > sx2)
1376 *x = sx2 - width, moved = 1;
1378 if (*y < sy1)
1379 *y = sy1, moved = 1;
1380 else if (*y + height > sy2)
1381 *y = sy2 - height, moved = 1;
1383 return moved;