Update for 0.51.0
[wmaker-crm.git] / src / screen.c
blob5cc50365d71ff406910becb3e26ab1e75e674b4d
1 /* screen.c - screen management
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
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
36 #include <wraster.h>
38 #include "WindowMaker.h"
39 #include "def_pixmaps.h"
40 #include "screen.h"
41 #include "texture.h"
42 #include "pixmap.h"
43 #include "menu.h"
44 #include "funcs.h"
45 #include "actions.h"
46 #include "properties.h"
47 #include "dock.h"
48 #include "resources.h"
49 #include "workspace.h"
50 #include "session.h"
51 #include "balloon.h"
52 #ifdef KWM_HINTS
53 # include "kwm.h"
54 #endif
55 #ifdef GNOME_STUFF
56 # include "gnome.h"
57 #endif
58 #ifdef OLWM_HINTS
59 # include "openlook.h"
60 #endif
62 #include <proplist.h>
64 #include "defaults.h"
67 #ifdef LITE
68 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
69 |SubstructureNotifyMask|PointerMotionMask \
70 |SubstructureRedirectMask|KeyPressMask|KeyReleaseMask)
71 #else
72 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
73 |SubstructureNotifyMask|PointerMotionMask \
74 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
75 |KeyPressMask|KeyReleaseMask)
76 #endif
78 /**** Global variables ****/
80 extern Cursor wCursor[WCUR_LAST];
81 extern WPreferences wPreferences;
82 extern Atom _XA_WINDOWMAKER_STATE;
83 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
86 extern int wScreenCount;
88 extern WDDomain *WDWindowMaker;
91 /**** Local ****/
93 #define STIPPLE_WIDTH 2
94 #define STIPPLE_HEIGHT 2
95 static char STIPPLE_DATA[] = {0x02, 0x01};
97 static int CantManageScreen = 0;
99 static proplist_t dApplications = NULL;
100 static proplist_t dWorkspace;
101 static proplist_t dDock;
102 static proplist_t dClip;
105 static void
106 make_keys()
108 if (dApplications!=NULL)
109 return;
111 dApplications = PLMakeString("Applications");
112 dWorkspace = PLMakeString("Workspace");
113 dDock = PLMakeString("Dock");
114 dClip = PLMakeString("Clip");
119 *----------------------------------------------------------------------
120 * alreadyRunningError--
121 * X error handler used to catch errors when trying to do
122 * XSelectInput() on the root window. These errors probably mean that
123 * there already is some other window manager running.
125 * Returns:
126 * Nothing, unless something really evil happens...
128 * Side effects:
129 * CantManageScreen is set to 1;
130 *----------------------------------------------------------------------
132 static int
133 alreadyRunningError(Display *dpy, XErrorEvent *error)
135 CantManageScreen = 1;
136 return -1;
141 *----------------------------------------------------------------------
142 * allocButtonPixmaps--
143 * Allocate pixmaps used on window operation buttons (those in the
144 * titlebar). The pixmaps are linked to the program. If XPM is supported
145 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
147 * Returns:
148 * Nothing
150 * Side effects:
151 * Allocates shared pixmaps for the screen. These pixmaps should
152 * not be freed by anybody.
153 *----------------------------------------------------------------------
155 static void
156 allocButtonPixmaps(WScreen *scr)
158 WPixmap *pix;
160 /* create predefined pixmaps */
161 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
162 if (pix)
163 pix->shared = 1;
164 scr->b_pixmaps[WBUT_CLOSE] = pix;
166 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
167 if (pix)
168 pix->shared = 1;
169 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
171 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
172 if (pix)
173 pix->shared = 1;
174 scr->b_pixmaps[WBUT_ICONIFY] = pix;
176 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
177 if (pix)
178 pix->shared = 1;
179 scr->b_pixmaps[WBUT_KILL] = pix;
184 static void
185 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
187 XSetForeground(dpy, gc, scr->black_pixel);
188 XDrawLine(dpy, d, gc, x, y, x+1, y);
189 XDrawPoint(dpy, d, gc, x, y+1);
190 XSetForeground(dpy, gc, scr->white_pixel);
191 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
192 XDrawPoint(dpy, d, gc, x+1, y+1);
196 static WPixmap*
197 make3Dots(WScreen *scr)
199 WPixmap *wpix;
200 GC gc2, gc;
201 XGCValues gcv;
202 Pixmap pix, mask;
204 gc = scr->copy_gc;
205 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
206 wPreferences.icon_size, scr->w_depth);
207 XSetForeground(dpy, gc, scr->black_pixel);
208 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
209 wPreferences.icon_size);
210 XSetForeground(dpy, gc, scr->white_pixel);
211 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
212 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
213 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
215 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
216 wPreferences.icon_size, 1);
217 gcv.foreground = 0;
218 gcv.graphics_exposures = False;
219 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
220 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
221 wPreferences.icon_size);
222 XSetForeground(dpy, gc2, 1);
223 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
224 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
225 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
227 XFreeGC(dpy, gc2);
229 wpix = wPixmapCreate(scr, pix, mask);
230 wpix->shared = 1;
232 return wpix;
236 static void
237 allocGCs(WScreen *scr)
239 XGCValues gcv;
240 XColor color;
241 unsigned long mtextcolor;
242 int gcm;
244 scr->stipple_bitmap =
245 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
246 STIPPLE_HEIGHT);
248 gcv.stipple = scr->stipple_bitmap;
249 gcv.foreground = scr->white_pixel;
250 gcv.fill_style = FillStippled;
251 gcv.graphics_exposures = False;
252 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
253 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
256 /* selected icon border GCs */
257 gcv.function = GXcopy;
258 gcv.foreground = scr->white_pixel;
259 gcv.background = scr->black_pixel;
260 gcv.line_width = 1;
261 gcv.line_style = LineDoubleDash;
262 gcv.fill_style = FillSolid;
263 gcv.dash_offset = 0;
264 gcv.dashes = 4;
265 gcv.graphics_exposures = False;
267 gcm = GCFunction | GCGraphicsExposures;
268 gcm |= GCForeground | GCBackground;
269 gcm |= GCLineWidth | GCLineStyle;
270 gcm |= GCFillStyle;
271 gcm |= GCDashOffset | GCDashList;
273 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
275 gcm = GCForeground|GCGraphicsExposures;
277 scr->menu_title_pixel[0] = scr->white_pixel;
278 gcv.foreground = scr->white_pixel;
279 gcv.graphics_exposures = False;
280 scr->menu_title_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
282 scr->mtext_pixel = scr->black_pixel;
283 mtextcolor = gcv.foreground = scr->black_pixel;
284 scr->menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
286 /* selected menu entry GC */
287 gcm = GCForeground|GCBackground|GCGraphicsExposures;
288 gcv.foreground = scr->white_pixel;
289 gcv.background = scr->white_pixel;
290 gcv.graphics_exposures = False;
291 scr->select_menu_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
293 /* disabled menu entry GC */
294 scr->dtext_pixel = scr->black_pixel;
295 gcm = GCForeground|GCBackground|GCStipple|GCGraphicsExposures;
296 gcv.stipple = scr->stipple_bitmap;
297 gcv.graphics_exposures = False;
298 scr->disabled_menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
300 /* frame GC */
301 wGetColor(scr, DEF_FRAME_COLOR, &color);
302 gcv.function = GXxor;
303 /* this will raise the probability of the XORed color being different
304 * of the original color in PseudoColor when not all color cells are
305 * initialized */
306 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
307 gcv.plane_mask = (1<<(scr->depth-1))|1;
308 else
309 gcv.plane_mask = AllPlanes;
310 gcv.foreground = color.pixel;
311 if (gcv.foreground == 0)
312 gcv.foreground = 1;
313 gcv.line_width = DEF_FRAME_THICKNESS;
314 gcv.subwindow_mode = IncludeInferiors;
315 gcv.graphics_exposures = False;
316 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
317 |GCFunction|GCSubwindowMode|GCLineWidth
318 |GCPlaneMask, &gcv);
320 /* line GC */
321 gcv.foreground = color.pixel;
323 if (gcv.foreground == 0)
324 /* XOR:ing with a zero is not going to be of much use, so
325 in that case, we somewhat arbitrarily xor with 17 instead. */
326 gcv.foreground = 17;
328 gcv.function = GXxor;
329 gcv.subwindow_mode = IncludeInferiors;
330 gcv.line_width = 1;
331 gcv.cap_style = CapRound;
332 gcv.graphics_exposures = False;
333 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
334 |GCGraphicsExposures;
335 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
337 scr->line_pixel = gcv.foreground;
339 /* copy GC */
340 gcv.foreground = scr->white_pixel;
341 gcv.background = scr->black_pixel;
342 gcv.graphics_exposures = False;
343 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
344 |GCGraphicsExposures, &gcv);
346 /* window title text GC */
347 gcv.graphics_exposures = False;
348 scr->window_title_gc = XCreateGC(dpy, scr->w_win,GCGraphicsExposures,&gcv);
350 /* icon title GC */
351 scr->icon_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
353 /* clip title GC */
354 scr->clip_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
356 /* move/size display GC */
357 gcv.graphics_exposures = False;
358 gcm = GCGraphicsExposures;
359 scr->info_text_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
361 /* misc drawing GC */
362 gcv.graphics_exposures = False;
363 gcm = GCGraphicsExposures;
364 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
369 static void
370 createPixmaps(WScreen *scr)
372 WPixmap *pix;
373 WMPixmap *wmpix;
374 RImage *image;
375 Pixmap p, m;
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"));
429 } else {
430 if (!RConvertImageMask(scr->rcontext, image, &p, &m, 128)) {
431 wwarning(_("error making logo image for panel:%s"), RMessageForError(RErrorCode));
432 } else {
433 wmpix = WMCreatePixmapFromXPixmaps(scr->wmscreen, p, m,
434 image->width, image->height,
435 scr->depth);
436 WMSetApplicationIconImage(scr->wmscreen, wmpix);
437 WMReleasePixmap(wmpix);
439 RDestroyImage(image);
442 if (!wPreferences.flags.nodock || !wPreferences.flags.noclip) {
443 scr->dock_dots = make3Dots(scr);
446 /* titlebar button pixmaps */
447 allocButtonPixmaps(scr);
452 *----------------------------------------------------------------------
453 * createInternalWindows--
454 * Creates some windows used internally by the program. One to
455 * receive input focus when no other window can get it and another
456 * to display window geometry information during window resize/move.
458 * Returns:
459 * Nothing
461 * Side effects:
462 * Windows are created and some colors are allocated for the
463 * window background.
464 *----------------------------------------------------------------------
466 static void
467 createInternalWindows(WScreen *scr)
469 int vmask;
470 XSetWindowAttributes attribs;
472 /* window for displaying geometry information during resizes and moves */
473 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
474 attribs.border_pixel = 0;
475 attribs.save_under = True;
476 attribs.override_redirect = True;
477 attribs.cursor = wCursor[WCUR_DEFAULT];
478 attribs.background_pixmap = None;
479 if (scr->resizebar_texture[0])
480 attribs.background_pixel = scr->resizebar_texture[0]->normal.pixel;
481 else
482 attribs.background_pixel = scr->light_pixel;
483 vmask |= CWColormap;
484 attribs.colormap = scr->w_colormap;
486 wGetGeometryWindowSize(scr, &scr->geometry_display_width,
487 &scr->geometry_display_height);
488 scr->geometry_display =
489 XCreateWindow(dpy, scr->root_win, 1, 1,
490 scr->geometry_display_width,
491 scr->geometry_display_height,
492 1, scr->w_depth, CopyFromParent, scr->w_visual,
493 vmask, &attribs);
495 /* InputOnly window to get the focus when no other window can get it */
496 vmask = CWEventMask|CWOverrideRedirect;
497 attribs.event_mask = KeyPressMask|FocusChangeMask;
498 attribs.override_redirect = True;
499 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
500 InputOnly,CopyFromParent, vmask, &attribs);
501 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
502 XMapWindow(dpy, scr->no_focus_win);
504 XSetInputFocus (dpy, scr->no_focus_win, RevertToParent, CurrentTime);
506 /* shadow window for dock buttons */
507 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
508 attribs.border_pixel = 0;
509 attribs.save_under = True;
510 attribs.override_redirect = True;
511 attribs.background_pixmap = None;
512 attribs.background_pixel = scr->white_pixel;
513 vmask |= CWColormap;
514 attribs.colormap = scr->w_colormap;
515 scr->dock_shadow =
516 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
517 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
518 scr->w_visual, vmask, &attribs);
520 /* workspace name balloon for clip */
521 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
522 |CWBorderPixel;
523 attribs.save_under = True;
524 attribs.override_redirect = True;
525 attribs.colormap = scr->w_colormap;
526 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
527 attribs.border_pixel = 0; /* do not care */
528 scr->clip_balloon =
529 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
530 CopyFromParent, scr->w_visual, vmask, &attribs);
532 /* for our window manager info notice board */
533 scr->info_window =
534 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, CopyFromParent,
535 CopyFromParent, CopyFromParent, 0, NULL);
538 * If the window is clicked without having ButtonPress selected, the
539 * resulting event will have event.xbutton.window == root.
541 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
545 #if 0
546 static Bool
547 aquireManagerSelection(WScreen *scr)
549 char buffer[32];
550 XEvent ev;
551 Time timestamp;
553 sprintf(buffer, "WM_S%i", scr->screen);
554 scr->managerAtom = XInternAtom(dpy, buffer, False);
556 /* for race-conditions... */
557 XGrabServer(dpy);
559 /* if there is another manager running, don't try to replace it
560 * (for now, at least) */
561 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
562 XUngrabServer(dpy);
563 return False;
566 /* become the manager for this screen */
568 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
569 0, 0, 0);
571 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
572 /* get a timestamp */
573 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
574 XA_INTEGER, 32, PropModeAppend, NULL, 0);
575 while (1) {
576 XWindowEvent(dpy, scr->managerWindow, &ev);
577 if (ev.type == PropertyNotify) {
578 timestamp = ev.xproperty.time;
579 break;
582 XSelectInput(dpy, scr->managerWindow, NoEvents);
583 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
585 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
587 XUngrabServer(dpy);
589 /* announce our arrival */
591 ev.xclient.type = ClientMessage;
592 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
593 ev.xclient.destination = scr->root_win;
594 ev.xclient.format = 32;
595 ev.xclient.data.l[0] = timestamp;
596 ev.xclient.data.l[1] = scr->managerAtom;
597 ev.xclient.data.l[2] = scr->managerWindow;
598 ev.xclient.data.l[3] = 0;
599 ev.xclient.data.l[4] = 0;
601 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
602 XSync(dpy, False);
604 return True;
606 #endif
609 *----------------------------------------------------------------------
610 * wScreenInit--
611 * Initializes the window manager for the given screen and
612 * allocates a WScreen descriptor for it. Many resources are allocated
613 * for the screen and the root window is setup appropriately.
615 * Returns:
616 * The WScreen descriptor for the screen.
618 * Side effects:
619 * Many resources are allocated and the IconSize property is
620 * set on the root window.
621 * The program can be aborted if some fatal error occurs.
623 * TODO: User specifiable visual.
624 *----------------------------------------------------------------------
626 WScreen*
627 wScreenInit(int screen_number)
629 WScreen *scr;
630 XIconSize icon_size[1];
631 RContextAttributes rattr;
632 extern int wVisualID;
633 long event_mask;
634 WMColor *color;
635 XErrorHandler oldHandler;
637 scr = wmalloc(sizeof(WScreen));
638 memset(scr, 0, sizeof(WScreen));
640 /* initialize globals */
641 scr->screen = screen_number;
642 scr->root_win = RootWindow(dpy, screen_number);
643 scr->depth = DefaultDepth(dpy, screen_number);
644 scr->colormap = DefaultColormap(dpy, screen_number);
646 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
647 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
649 scr->usableArea.x2 = scr->scr_width;
650 scr->usableArea.y2 = scr->scr_height;
651 scr->totalUsableArea.x2 = scr->scr_width;
652 scr->totalUsableArea.y2 = scr->scr_height;
654 #if 0
655 if (!aquireManagerSelection(scr)) {
656 free(scr);
658 return NULL;
660 #endif
661 CantManageScreen = 0;
662 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
664 event_mask = EVENT_MASK;
666 if (wPreferences.disable_root_mouse) {
667 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
670 XSelectInput(dpy, scr->root_win, event_mask);
672 XSync(dpy, False);
673 XSetErrorHandler(oldHandler);
675 if (CantManageScreen) {
676 free(scr);
677 return NULL;
680 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_DEFAULT]);
682 /* screen descriptor for raster graphic library */
683 rattr.flags = RC_RenderMode | RC_ColorsPerChannel;
684 rattr.render_mode = wPreferences.no_dithering?RM_MATCH:RM_DITHER;
685 rattr.colors_per_channel = wPreferences.cmap_size;
686 if (rattr.colors_per_channel<2)
687 rattr.colors_per_channel = 2;
689 if (wVisualID>=0) {
690 rattr.flags |= RC_VisualID;
691 rattr.visualid = wVisualID;
693 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
694 if (!scr->rcontext) {
695 wwarning(_("could not initialize graphics library context: %s"),
696 RMessageForError(RErrorCode));
697 wAbort(False);
698 } else {
699 char **formats;
700 int i = 0;
702 formats = RSupportedFileFormats();
703 if (formats) {
704 for (i=0; formats[i]!=NULL; i++) {
705 if (strcmp(formats[i], "TIFF")==0) {
706 scr->flags.supports_tiff = 1;
707 break;
710 RFreeStringList(formats);
714 scr->w_win = scr->rcontext->drawable;
715 scr->w_visual = scr->rcontext->visual;
716 scr->w_depth = scr->rcontext->depth;
717 scr->w_colormap = scr->rcontext->cmap;
719 scr->black_pixel = scr->rcontext->black;
720 scr->white_pixel = scr->rcontext->white;
722 /* create screen descriptor for WINGs */
723 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
724 scr->rcontext);
726 if (!scr->wmscreen) {
727 wfatal(_("could not do initialization of WINGs widget set"));
729 return NULL;
732 color = WMGrayColor(scr->wmscreen);
733 scr->light_pixel = WMColorPixel(color);
734 WMReleaseColor(color);
736 color = WMDarkGrayColor(scr->wmscreen);
737 scr->dark_pixel = WMColorPixel(color);
738 WMReleaseColor(color);
741 XColor xcol;
742 /* frame boder color */
743 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
744 scr->frame_border_pixel = xcol.pixel;
747 /* create GCs with default values */
748 allocGCs(scr);
750 /* read defaults for this screen */
751 wReadDefaults(scr, WDWindowMaker->dictionary);
753 createInternalWindows(scr);
755 #ifdef KWM_HINTS
756 wKWMInitStuff(scr);
757 #endif
759 #ifdef GNOME_STUFF
760 wGNOMEInitStuff(scr);
761 #endif
763 #ifdef OLWM_HINTS
764 wOLWMInitStuff(scr);
765 #endif
767 /* create initial workspace */
768 wWorkspaceNew(scr);
770 /* create shared pixmaps */
771 createPixmaps(scr);
773 /* set icon sizes we can accept from clients */
774 icon_size[0].min_width = 8;
775 icon_size[0].min_height = 8;
776 icon_size[0].max_width = wPreferences.icon_size-4;
777 icon_size[0].max_height = wPreferences.icon_size-4;
778 icon_size[0].width_inc = 1;
779 icon_size[0].height_inc = 1;
780 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
782 /* setup WindowMaker protocols property in the root window*/
783 PropSetWMakerProtocols(scr->root_win);
785 /* setup our noticeboard */
786 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
787 XA_WINDOW, 32, PropModeReplace,
788 (unsigned char*)&scr->info_window, 1);
789 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
790 XA_WINDOW, 32, PropModeReplace,
791 (unsigned char*)&scr->info_window, 1);
794 #ifdef BALLOON_TEXT
795 /* initialize balloon text stuff */
796 wBalloonInitialize(scr);
797 #endif
799 wScreenUpdateUsableArea(scr);
801 #ifndef LITE
802 /* kluge to load menu configurations at startup */
803 OpenRootMenu(scr, -10000, -10000, False);
804 wMenuUnmap(scr->root_menu);
805 #endif
807 return scr;
811 void
812 wScreenUpdateUsableArea(WScreen *scr)
814 #ifdef GNOME_STUFF
815 WReservedArea *area;
816 #endif
818 scr->totalUsableArea = scr->usableArea;
821 if (scr->dock && (!scr->dock->lowered
822 || wPreferences.no_window_over_dock)) {
824 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
826 if (scr->dock->on_right_side) {
827 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
828 scr->scr_width - offset);
829 } else {
830 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
834 if (wPreferences.no_window_over_icons) {
835 if (wPreferences.icon_yard & IY_VERT) {
837 if (!(wPreferences.icon_yard & IY_RIGHT)) {
838 scr->totalUsableArea.x1 += wPreferences.icon_size;
839 } else {
840 scr->totalUsableArea.x2 -= wPreferences.icon_size;
842 } else {
844 if (wPreferences.icon_yard & IY_TOP) {
845 scr->totalUsableArea.y1 += wPreferences.icon_size;
846 } else {
847 scr->totalUsableArea.y2 -= wPreferences.icon_size;
852 #ifdef KWM_HINTS
854 WArea area;
856 if (wKWMGetUsableArea(scr, &area)) {
857 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
858 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
859 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
860 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
863 #endif
865 #ifdef GNOME_STUFF
866 area = scr->reservedAreas;
868 while (area) {
869 int th, bh;
870 int lw, rw;
871 int w, h;
873 w = area->area.x2 - area->area.x1;
874 h = area->area.y2 - area->area.y1;
876 th = area->area.y1;
877 bh = scr->scr_height - area->area.y2;
878 lw = area->area.x1;
879 rw = scr->scr_width - area->area.x2;
881 if (WMIN(th, bh) < WMIN(lw, rw)) {
882 /* horizontal */
883 if (th < bh) {
884 /* on top */
885 if (scr->totalUsableArea.y1 < area->area.y2)
886 scr->totalUsableArea.y1 = area->area.y2;
887 } else {
888 /* on bottom */
889 if (scr->totalUsableArea.y2 > area->area.y1)
890 scr->totalUsableArea.y2 = area->area.y1;
892 } else {
893 /* vertical */
894 if (lw < rw) {
895 /* on left */
896 if (scr->totalUsableArea.x1 < area->area.x2)
897 scr->totalUsableArea.x1 = area->area.x2;
898 } else {
899 /* on right */
900 if (scr->totalUsableArea.x2 > area->area.x1)
901 scr->totalUsableArea.x2 = area->area.x1;
905 area = area->next;
907 #endif /* GNOME_STUFF */
909 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
910 scr->totalUsableArea.x2 = scr->usableArea.x2;
911 scr->totalUsableArea.x1 = scr->usableArea.x1;
913 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
914 scr->totalUsableArea.y2 = scr->usableArea.y2;
915 scr->totalUsableArea.y1 = scr->usableArea.y1;
918 #ifdef not_used
919 #ifdef KWM_HINTS
921 int i;
923 for (i = 0; i < scr->workspace_count; i++) {
924 wKWMSetUsableAreaHint(scr, i);
927 #endif
928 #endif
932 void
933 wScreenRestoreState(WScreen *scr)
935 proplist_t state;
936 char *path;
938 make_keys();
940 if (wScreenCount == 1)
941 path = wdefaultspathfordomain("WMState");
942 else {
943 char buf[16];
944 sprintf(buf, "WMState.%i", scr->screen);
945 path = wdefaultspathfordomain(buf);
947 scr->session_state = PLGetProplistWithPath(path);
948 free(path);
949 if (!scr->session_state && wScreenCount>1) {
950 char buf[16];
951 sprintf(buf, "WMState.%i", scr->screen);
952 path = wdefaultspathfordomain(buf);
953 scr->session_state = PLGetProplistWithPath(path);
954 free(path);
957 if (!wPreferences.flags.noclip) {
958 state = PLGetDictionaryEntry(scr->session_state, dClip);
959 scr->clip_icon = wClipRestoreState(scr, state);
962 wWorkspaceRestoreState(scr);
964 if (!wPreferences.flags.nodock) {
965 state = PLGetDictionaryEntry(scr->session_state, dDock);
966 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
969 wScreenUpdateUsableArea(scr);
973 void
974 wScreenSaveState(WScreen *scr)
976 WWorkspaceState wstate;
977 WWindow *wwin;
978 char *str;
979 proplist_t path, old_state, foo;
980 CARD32 data[2];
983 make_keys();
986 * Save current workspace, so can go back to it upon restart.
988 wstate.workspace = scr->current_workspace;
990 data[0] = wstate.flags;
991 data[1] = wstate.workspace;
993 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
994 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
995 (unsigned char *) data, 2);
997 /* save state of windows */
998 wwin = scr->focused_window;
999 while (wwin) {
1000 wWindowSaveState(wwin);
1001 wwin = wwin->prev;
1005 if (wPreferences.flags.noupdates)
1006 return;
1009 old_state = scr->session_state;
1010 scr->session_state = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
1012 PLSetStringCmpHook(NULL);
1014 /* save dock state to file */
1015 if (!wPreferences.flags.nodock) {
1016 wDockSaveState(scr);
1017 } else {
1018 if ((foo = PLGetDictionaryEntry(old_state, dDock))!=NULL) {
1019 PLInsertDictionaryEntry(scr->session_state, dDock, foo);
1022 if (!wPreferences.flags.noclip) {
1023 wClipSaveState(scr);
1024 } else {
1025 if ((foo = PLGetDictionaryEntry(old_state, dClip))!=NULL) {
1026 PLInsertDictionaryEntry(scr->session_state, dClip, foo);
1030 wWorkspaceSaveState(scr, old_state);
1032 if (wPreferences.save_session_on_exit) {
1033 wSessionSaveState(scr);
1034 } else {
1035 if ((foo = PLGetDictionaryEntry(old_state, dApplications))!=NULL) {
1036 PLInsertDictionaryEntry(scr->session_state, dApplications, foo);
1038 if ((foo = PLGetDictionaryEntry(old_state, dWorkspace))!=NULL) {
1039 PLInsertDictionaryEntry(scr->session_state, dWorkspace, foo);
1043 /* clean up */
1044 PLSetStringCmpHook(StringCompareHook);
1046 wMenuSaveState(scr);
1048 if (wScreenCount == 1)
1049 str = wdefaultspathfordomain("WMState");
1050 else {
1051 char buf[16];
1052 sprintf(buf, "WMState.%i", scr->screen);
1053 str = wdefaultspathfordomain(buf);
1055 path = PLMakeString(str);
1056 free(str);
1057 PLSetFilename(scr->session_state, path);
1058 if (!PLSave(scr->session_state, YES)) {
1059 wwarning(_("could not save session state in %s"), PLGetString(path));
1061 PLRelease(path);
1062 PLRelease(old_state);
1068 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1070 int moved = 0;
1071 int tol_w, tol_h;
1073 if (width > 20)
1074 tol_w = width/2;
1075 else
1076 tol_w = 20;
1078 if (height > 20)
1079 tol_h = height/2;
1080 else
1081 tol_h = 20;
1083 if (*x+width < 10)
1084 *x = -tol_w, moved = 1;
1085 else if (*x >= scr->scr_width - 10)
1086 *x = scr->scr_width - tol_w - 1, moved = 1;
1088 if (*y < -height + 10)
1089 *y = -tol_h, moved = 1;
1090 else if (*y >= scr->scr_height - 10)
1091 *y = scr->scr_height - tol_h - 1, moved = 1;
1093 return moved;