Fix crash bug in screen.c, hide window when it is --no-clip --no-dock.
[wmaker-crm.git] / src / screen.c
blob1af44cf44348e5e5969f26ba49d3adaf2aa45f72
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
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 #ifdef KWM_HINTS
56 # include "kwm.h"
57 #endif
58 #ifdef GNOME_STUFF
59 # include "gnome.h"
60 #endif
61 #ifdef OLWM_HINTS
62 # include "openlook.h"
63 #endif
65 #include <proplist.h>
67 #include "defaults.h"
70 #ifdef LITE
71 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
72 |SubstructureNotifyMask|PointerMotionMask \
73 |SubstructureRedirectMask|KeyPressMask|KeyReleaseMask)
74 #else
75 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
76 |SubstructureNotifyMask|PointerMotionMask \
77 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
78 |KeyPressMask|KeyReleaseMask)
79 #endif
81 /**** Global variables ****/
83 extern Cursor wCursor[WCUR_LAST];
84 extern WPreferences wPreferences;
85 extern Atom _XA_WINDOWMAKER_STATE;
86 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
89 extern int wScreenCount;
91 extern WDDomain *WDWindowMaker;
94 /**** Local ****/
96 #define STIPPLE_WIDTH 2
97 #define STIPPLE_HEIGHT 2
98 static char STIPPLE_DATA[] = {0x02, 0x01};
100 static int CantManageScreen = 0;
102 static proplist_t dApplications = NULL;
103 static proplist_t dWorkspace;
104 static proplist_t dDock;
105 static proplist_t dClip;
108 static void
109 make_keys()
111 if (dApplications!=NULL)
112 return;
114 dApplications = PLMakeString("Applications");
115 dWorkspace = PLMakeString("Workspace");
116 dDock = PLMakeString("Dock");
117 dClip = PLMakeString("Clip");
122 *----------------------------------------------------------------------
123 * alreadyRunningError--
124 * X error handler used to catch errors when trying to do
125 * XSelectInput() on the root window. These errors probably mean that
126 * there already is some other window manager running.
128 * Returns:
129 * Nothing, unless something really evil happens...
131 * Side effects:
132 * CantManageScreen is set to 1;
133 *----------------------------------------------------------------------
135 static int
136 alreadyRunningError(Display *dpy, XErrorEvent *error)
138 CantManageScreen = 1;
139 return -1;
144 *----------------------------------------------------------------------
145 * allocButtonPixmaps--
146 * Allocate pixmaps used on window operation buttons (those in the
147 * titlebar). The pixmaps are linked to the program. If XPM is supported
148 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
150 * Returns:
151 * Nothing
153 * Side effects:
154 * Allocates shared pixmaps for the screen. These pixmaps should
155 * not be freed by anybody.
156 *----------------------------------------------------------------------
158 static void
159 allocButtonPixmaps(WScreen *scr)
161 WPixmap *pix;
163 /* create predefined pixmaps */
164 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
165 if (pix)
166 pix->shared = 1;
167 scr->b_pixmaps[WBUT_CLOSE] = pix;
169 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
170 if (pix)
171 pix->shared = 1;
172 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
174 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
175 if (pix)
176 pix->shared = 1;
177 scr->b_pixmaps[WBUT_ICONIFY] = pix;
179 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
180 if (pix)
181 pix->shared = 1;
182 scr->b_pixmaps[WBUT_KILL] = pix;
186 static void
187 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
189 XSetForeground(dpy, gc, scr->black_pixel);
190 XDrawLine(dpy, d, gc, x, y, x+1, y);
191 XDrawPoint(dpy, d, gc, x, y+1);
192 XSetForeground(dpy, gc, scr->white_pixel);
193 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
194 XDrawPoint(dpy, d, gc, x+1, y+1);
198 static WPixmap*
199 make3Dots(WScreen *scr)
201 WPixmap *wpix;
202 GC gc2, gc;
203 XGCValues gcv;
204 Pixmap pix, mask;
206 gc = scr->copy_gc;
207 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
208 wPreferences.icon_size, scr->w_depth);
209 XSetForeground(dpy, gc, scr->black_pixel);
210 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
211 wPreferences.icon_size);
212 XSetForeground(dpy, gc, scr->white_pixel);
213 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
214 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
215 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
217 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
218 wPreferences.icon_size, 1);
219 gcv.foreground = 0;
220 gcv.graphics_exposures = False;
221 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
222 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
223 wPreferences.icon_size);
224 XSetForeground(dpy, gc2, 1);
225 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
226 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
227 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
229 XFreeGC(dpy, gc2);
231 wpix = wPixmapCreate(scr, pix, mask);
232 wpix->shared = 1;
234 return wpix;
238 static void
239 allocGCs(WScreen *scr)
241 XGCValues gcv;
242 XColor color;
243 unsigned long mtextcolor;
244 int gcm;
246 scr->stipple_bitmap =
247 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
248 STIPPLE_HEIGHT);
250 gcv.stipple = scr->stipple_bitmap;
251 gcv.foreground = scr->white_pixel;
252 gcv.fill_style = FillStippled;
253 gcv.graphics_exposures = False;
254 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
255 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
258 /* selected icon border GCs */
259 gcv.function = GXcopy;
260 gcv.foreground = scr->white_pixel;
261 gcv.background = scr->black_pixel;
262 gcv.line_width = 1;
263 gcv.line_style = LineDoubleDash;
264 gcv.fill_style = FillSolid;
265 gcv.dash_offset = 0;
266 gcv.dashes = 4;
267 gcv.graphics_exposures = False;
269 gcm = GCFunction | GCGraphicsExposures;
270 gcm |= GCForeground | GCBackground;
271 gcm |= GCLineWidth | GCLineStyle;
272 gcm |= GCFillStyle;
273 gcm |= GCDashOffset | GCDashList;
275 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
277 gcm = GCForeground|GCGraphicsExposures;
279 scr->menu_title_pixel[0] = scr->white_pixel;
280 gcv.foreground = scr->white_pixel;
281 gcv.graphics_exposures = False;
282 scr->menu_title_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
284 scr->mtext_pixel = scr->black_pixel;
285 mtextcolor = gcv.foreground = scr->black_pixel;
286 scr->menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
288 /* selected menu entry GC */
289 gcm = GCForeground|GCBackground|GCGraphicsExposures;
290 gcv.foreground = scr->white_pixel;
291 gcv.background = scr->white_pixel;
292 gcv.graphics_exposures = False;
293 scr->select_menu_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
295 /* disabled menu entry GC */
296 scr->dtext_pixel = scr->black_pixel;
297 gcm = GCForeground|GCBackground|GCStipple|GCGraphicsExposures;
298 gcv.stipple = scr->stipple_bitmap;
299 gcv.graphics_exposures = False;
300 scr->disabled_menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
302 /* frame GC */
303 wGetColor(scr, DEF_FRAME_COLOR, &color);
304 gcv.function = GXxor;
305 /* this will raise the probability of the XORed color being different
306 * of the original color in PseudoColor when not all color cells are
307 * initialized */
308 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
309 gcv.plane_mask = (1<<(scr->depth-1))|1;
310 else
311 gcv.plane_mask = AllPlanes;
312 gcv.foreground = color.pixel;
313 if (gcv.foreground == 0)
314 gcv.foreground = 1;
315 gcv.line_width = DEF_FRAME_THICKNESS;
316 gcv.subwindow_mode = IncludeInferiors;
317 gcv.graphics_exposures = False;
318 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
319 |GCFunction|GCSubwindowMode|GCLineWidth
320 |GCPlaneMask, &gcv);
322 /* line GC */
323 gcv.foreground = color.pixel;
325 if (gcv.foreground == 0)
326 /* XOR:ing with a zero is not going to be of much use, so
327 in that case, we somewhat arbitrarily xor with 17 instead. */
328 gcv.foreground = 17;
330 gcv.function = GXxor;
331 gcv.subwindow_mode = IncludeInferiors;
332 gcv.line_width = 1;
333 gcv.cap_style = CapRound;
334 gcv.graphics_exposures = False;
335 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
336 |GCGraphicsExposures;
337 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
339 scr->line_pixel = gcv.foreground;
341 /* copy GC */
342 gcv.foreground = scr->white_pixel;
343 gcv.background = scr->black_pixel;
344 gcv.graphics_exposures = False;
345 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
346 |GCGraphicsExposures, &gcv);
348 /* window title text GC */
349 gcv.graphics_exposures = False;
350 scr->window_title_gc = XCreateGC(dpy, scr->w_win,GCGraphicsExposures,&gcv);
352 /* icon title GC */
353 scr->icon_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
355 /* clip title GC */
356 scr->clip_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
358 /* move/size display GC */
359 gcv.graphics_exposures = False;
360 gcm = GCGraphicsExposures;
361 scr->info_text_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
363 /* misc drawing GC */
364 gcv.graphics_exposures = False;
365 gcm = GCGraphicsExposures;
366 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
368 assert (scr->stipple_bitmap!=None);
371 /* mono GC */
372 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
377 static void
378 createPixmaps(WScreen *scr)
380 WPixmap *pix;
381 WMPixmap *wmpix;
382 RImage *image;
383 Pixmap p, m;
385 /* load pixmaps */
386 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
387 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
388 MENU_RADIO_INDICATOR_XBM_SIZE,
389 MENU_RADIO_INDICATOR_XBM_SIZE,
390 scr->black_pixel, scr->white_pixel);
391 if (pix!=NULL)
392 pix->shared = 1;
393 scr->menu_radio_indicator = pix;
396 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
397 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
398 MENU_CHECK_INDICATOR_XBM_SIZE,
399 MENU_CHECK_INDICATOR_XBM_SIZE,
400 scr->black_pixel, scr->white_pixel);
401 if (pix!=NULL)
402 pix->shared = 1;
403 scr->menu_check_indicator = pix;
405 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
406 (char*)MENU_MINI_INDICATOR_XBM_DATA,
407 MENU_MINI_INDICATOR_XBM_SIZE,
408 MENU_MINI_INDICATOR_XBM_SIZE,
409 scr->black_pixel, scr->white_pixel);
410 if (pix!=NULL)
411 pix->shared = 1;
412 scr->menu_mini_indicator = pix;
414 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
415 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
416 MENU_HIDE_INDICATOR_XBM_SIZE,
417 MENU_HIDE_INDICATOR_XBM_SIZE,
418 scr->black_pixel, scr->white_pixel);
419 if (pix!=NULL)
420 pix->shared = 1;
421 scr->menu_hide_indicator = pix;
423 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
424 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
425 MENU_SHADE_INDICATOR_XBM_SIZE,
426 MENU_SHADE_INDICATOR_XBM_SIZE,
427 scr->black_pixel, scr->white_pixel);
428 if (pix!=NULL)
429 pix->shared = 1;
430 scr->menu_shade_indicator = pix;
433 image = wDefaultGetImage(scr, "Logo", "WMPanel");
435 if (!image) {
436 wwarning(_("could not load logo image for panels: %s"),
437 RMessageForError(RErrorCode));
438 } else {
439 if (!RConvertImageMask(scr->rcontext, image, &p, &m, 128)) {
440 wwarning(_("error making logo image for panel:%s"), RMessageForError(RErrorCode));
441 } else {
442 wmpix = WMCreatePixmapFromXPixmaps(scr->wmscreen, p, m,
443 image->width, image->height,
444 scr->depth);
445 WMSetApplicationIconImage(scr->wmscreen, wmpix);
446 WMReleasePixmap(wmpix);
448 RDestroyImage(image);
451 scr->dock_dots = make3Dots(scr);
453 #ifndef NEWSTUFF
454 /* titlebar button pixmaps */
455 allocButtonPixmaps(scr);
456 #endif
461 *----------------------------------------------------------------------
462 * createInternalWindows--
463 * Creates some windows used internally by the program. One to
464 * receive input focus when no other window can get it and another
465 * to display window geometry information during window resize/move.
467 * Returns:
468 * Nothing
470 * Side effects:
471 * Windows are created and some colors are allocated for the
472 * window background.
473 *----------------------------------------------------------------------
475 static void
476 createInternalWindows(WScreen *scr)
478 int vmask;
479 XSetWindowAttributes attribs;
481 /* window for displaying geometry information during resizes and moves */
482 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
483 attribs.border_pixel = 0;
484 attribs.save_under = True;
485 attribs.override_redirect = True;
486 attribs.cursor = wCursor[WCUR_DEFAULT];
487 attribs.background_pixmap = None;
488 if (scr->widget_texture)
489 attribs.background_pixel = scr->widget_texture->normal.pixel;
490 else
491 attribs.background_pixel = scr->light_pixel;
492 vmask |= CWColormap;
493 attribs.colormap = scr->w_colormap;
495 wGetGeometryWindowSize(scr, &scr->geometry_display_width,
496 &scr->geometry_display_height);
497 scr->geometry_display =
498 XCreateWindow(dpy, scr->root_win, 1, 1,
499 scr->geometry_display_width,
500 scr->geometry_display_height,
501 1, scr->w_depth, CopyFromParent, scr->w_visual,
502 vmask, &attribs);
504 /* InputOnly window to get the focus when no other window can get it */
505 vmask = CWEventMask|CWOverrideRedirect;
506 attribs.event_mask = KeyPressMask|FocusChangeMask;
507 attribs.override_redirect = True;
508 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
509 InputOnly,CopyFromParent, vmask, &attribs);
510 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
511 XMapWindow(dpy, scr->no_focus_win);
513 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
515 /* shadow window for dock buttons */
516 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
517 attribs.border_pixel = 0;
518 attribs.save_under = True;
519 attribs.override_redirect = True;
520 attribs.background_pixmap = None;
521 attribs.background_pixel = scr->white_pixel;
522 vmask |= CWColormap;
523 attribs.colormap = scr->w_colormap;
524 scr->dock_shadow =
525 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
526 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
527 scr->w_visual, vmask, &attribs);
529 /* workspace name balloon for clip */
530 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
531 |CWBorderPixel;
532 attribs.save_under = True;
533 attribs.override_redirect = True;
534 attribs.colormap = scr->w_colormap;
535 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
536 attribs.border_pixel = 0; /* do not care */
537 scr->clip_balloon =
538 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
539 CopyFromParent, scr->w_visual, vmask, &attribs);
542 /* workspace name */
543 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
544 |CWBorderPixel;
545 attribs.save_under = True;
546 attribs.override_redirect = True;
547 attribs.colormap = scr->w_colormap;
548 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
549 attribs.border_pixel = 0; /* do not care */
550 scr->workspace_name =
551 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
552 CopyFromParent, scr->w_visual, vmask, &attribs);
555 /* for our window manager info notice board */
556 scr->info_window =
557 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, CopyFromParent,
558 CopyFromParent, CopyFromParent, CWOverrideRedirect,
559 &attribs);
562 * If the window is clicked without having ButtonPress selected, the
563 * resulting event will have event.xbutton.window == root.
565 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
569 #if 0
570 static Bool
571 aquireManagerSelection(WScreen *scr)
573 char buffer[32];
574 XEvent ev;
575 Time timestamp;
577 sprintf(buffer, "WM_S%i", scr->screen);
578 scr->managerAtom = XInternAtom(dpy, buffer, False);
580 /* for race-conditions... */
581 XGrabServer(dpy);
583 /* if there is another manager running, don't try to replace it
584 * (for now, at least) */
585 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
586 XUngrabServer(dpy);
587 return False;
590 /* become the manager for this screen */
592 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
593 0, 0, 0);
595 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
596 /* get a timestamp */
597 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
598 XA_INTEGER, 32, PropModeAppend, NULL, 0);
599 while (1) {
600 XWindowEvent(dpy, scr->managerWindow, &ev);
601 if (ev.type == PropertyNotify) {
602 timestamp = ev.xproperty.time;
603 break;
606 XSelectInput(dpy, scr->managerWindow, NoEvents);
607 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
609 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
611 XUngrabServer(dpy);
613 /* announce our arrival */
615 ev.xclient.type = ClientMessage;
616 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
617 ev.xclient.destination = scr->root_win;
618 ev.xclient.format = 32;
619 ev.xclient.data.l[0] = timestamp;
620 ev.xclient.data.l[1] = scr->managerAtom;
621 ev.xclient.data.l[2] = scr->managerWindow;
622 ev.xclient.data.l[3] = 0;
623 ev.xclient.data.l[4] = 0;
625 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
626 XSync(dpy, False);
628 return True;
630 #endif
633 *----------------------------------------------------------------------
634 * wScreenInit--
635 * Initializes the window manager for the given screen and
636 * allocates a WScreen descriptor for it. Many resources are allocated
637 * for the screen and the root window is setup appropriately.
639 * Returns:
640 * The WScreen descriptor for the screen.
642 * Side effects:
643 * Many resources are allocated and the IconSize property is
644 * set on the root window.
645 * The program can be aborted if some fatal error occurs.
647 * TODO: User specifiable visual.
648 *----------------------------------------------------------------------
650 WScreen*
651 wScreenInit(int screen_number)
653 WScreen *scr;
654 XIconSize icon_size[1];
655 RContextAttributes rattr;
656 extern int wVisualID;
657 long event_mask;
658 WMColor *color;
659 XErrorHandler oldHandler;
661 scr = wmalloc(sizeof(WScreen));
662 memset(scr, 0, sizeof(WScreen));
664 /* initialize globals */
665 scr->screen = screen_number;
666 scr->root_win = RootWindow(dpy, screen_number);
667 scr->depth = DefaultDepth(dpy, screen_number);
668 scr->colormap = DefaultColormap(dpy, screen_number);
670 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
671 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
673 scr->usableArea.x2 = scr->scr_width;
674 scr->usableArea.y2 = scr->scr_height;
675 scr->totalUsableArea.x2 = scr->scr_width;
676 scr->totalUsableArea.y2 = scr->scr_height;
678 #if 0
679 if (!aquireManagerSelection(scr)) {
680 free(scr);
682 return NULL;
684 #endif
685 CantManageScreen = 0;
686 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
688 event_mask = EVENT_MASK;
690 if (wPreferences.disable_root_mouse) {
691 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
694 XSelectInput(dpy, scr->root_win, event_mask);
696 #ifdef KEEP_XKB_LOCK_STATUS
697 XkbSelectEvents(dpy,XkbUseCoreKbd,XkbIndicatorStateNotifyMask,
698 XkbIndicatorStateNotifyMask);
699 #endif /* KEEP_XKB_LOCK_STATUS */
701 XSync(dpy, False);
702 XSetErrorHandler(oldHandler);
704 if (CantManageScreen) {
705 free(scr);
706 return NULL;
709 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_DEFAULT]);
711 /* screen descriptor for raster graphic library */
712 rattr.flags = RC_RenderMode | RC_ColorsPerChannel;
713 rattr.render_mode = wPreferences.no_dithering?RM_MATCH:RM_DITHER;
714 rattr.colors_per_channel = wPreferences.cmap_size;
715 if (rattr.colors_per_channel<2)
716 rattr.colors_per_channel = 2;
718 if (wVisualID>=0) {
719 rattr.flags |= RC_VisualID;
720 rattr.visualid = wVisualID;
722 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
723 if (!scr->rcontext) {
724 wwarning(_("could not initialize graphics library context: %s"),
725 RMessageForError(RErrorCode));
726 wAbort(False);
727 } else {
728 char **formats;
729 int i = 0;
731 formats = RSupportedFileFormats();
732 if (formats) {
733 for (i=0; formats[i]!=NULL; i++) {
734 if (strcmp(formats[i], "TIFF")==0) {
735 scr->flags.supports_tiff = 1;
736 break;
742 scr->w_win = scr->rcontext->drawable;
743 scr->w_visual = scr->rcontext->visual;
744 scr->w_depth = scr->rcontext->depth;
745 scr->w_colormap = scr->rcontext->cmap;
747 scr->black_pixel = scr->rcontext->black;
748 scr->white_pixel = scr->rcontext->white;
750 /* create screen descriptor for WINGs */
751 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
752 scr->rcontext);
754 if (!scr->wmscreen) {
755 wfatal(_("could not do initialization of WINGs widget set"));
757 return NULL;
760 color = WMGrayColor(scr->wmscreen);
761 scr->light_pixel = WMColorPixel(color);
762 WMReleaseColor(color);
764 color = WMDarkGrayColor(scr->wmscreen);
765 scr->dark_pixel = WMColorPixel(color);
766 WMReleaseColor(color);
769 XColor xcol;
770 /* frame boder color */
771 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
772 scr->frame_border_pixel = xcol.pixel;
775 scr->workspace_name_font = wLoadFont(DEF_WORKSPACE_NAME_FONT);
777 /* create GCs with default values */
778 allocGCs(scr);
780 /* read defaults for this screen */
781 wReadDefaults(scr, WDWindowMaker->dictionary);
783 createInternalWindows(scr);
785 #ifdef KWM_HINTS
786 wKWMInitStuff(scr);
787 #endif
789 #ifdef GNOME_STUFF
790 wGNOMEInitStuff(scr);
791 #endif
793 #ifdef OLWM_HINTS
794 wOLWMInitStuff(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 wScreenUpdateUsableArea(scr);
831 #ifndef LITE
832 /* kluge to load menu configurations at startup */
833 OpenRootMenu(scr, -10000, -10000, False);
834 wMenuUnmap(scr->root_menu);
835 #endif
837 return scr;
841 void
842 wScreenUpdateUsableArea(WScreen *scr)
844 #ifdef GNOME_STUFF
845 WReservedArea *area;
846 #endif
848 scr->totalUsableArea = scr->usableArea;
851 if (scr->dock && (!scr->dock->lowered
852 || wPreferences.no_window_over_dock)) {
854 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
856 if (scr->dock->on_right_side) {
857 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
858 scr->scr_width - offset);
859 } else {
860 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
864 if (wPreferences.no_window_over_icons) {
865 if (wPreferences.icon_yard & IY_VERT) {
867 if (!(wPreferences.icon_yard & IY_RIGHT)) {
868 scr->totalUsableArea.x1 += wPreferences.icon_size;
869 } else {
870 scr->totalUsableArea.x2 -= wPreferences.icon_size;
872 } else {
874 if (wPreferences.icon_yard & IY_TOP) {
875 scr->totalUsableArea.y1 += wPreferences.icon_size;
876 } else {
877 scr->totalUsableArea.y2 -= wPreferences.icon_size;
882 #ifdef KWM_HINTS
884 WArea area;
886 if (wKWMGetUsableArea(scr, &area)) {
887 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
888 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
889 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
890 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
893 #endif
895 #ifdef GNOME_STUFF
896 area = scr->reservedAreas;
898 while (area) {
899 int th, bh;
900 int lw, rw;
901 int w, h;
903 w = area->area.x2 - area->area.x1;
904 h = area->area.y2 - area->area.y1;
906 th = area->area.y1;
907 bh = scr->scr_height - area->area.y2;
908 lw = area->area.x1;
909 rw = scr->scr_width - area->area.x2;
911 if (WMIN(th, bh) < WMIN(lw, rw)) {
912 /* horizontal */
913 if (th < bh) {
914 /* on top */
915 if (scr->totalUsableArea.y1 < area->area.y2)
916 scr->totalUsableArea.y1 = area->area.y2;
917 } else {
918 /* on bottom */
919 if (scr->totalUsableArea.y2 > area->area.y1)
920 scr->totalUsableArea.y2 = area->area.y1;
922 } else {
923 /* vertical */
924 if (lw < rw) {
925 /* on left */
926 if (scr->totalUsableArea.x1 < area->area.x2)
927 scr->totalUsableArea.x1 = area->area.x2;
928 } else {
929 /* on right */
930 if (scr->totalUsableArea.x2 > area->area.x1)
931 scr->totalUsableArea.x2 = area->area.x1;
935 area = area->next;
937 #endif /* GNOME_STUFF */
939 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
940 scr->totalUsableArea.x2 = scr->usableArea.x2;
941 scr->totalUsableArea.x1 = scr->usableArea.x1;
943 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
944 scr->totalUsableArea.y2 = scr->usableArea.y2;
945 scr->totalUsableArea.y1 = scr->usableArea.y1;
948 #ifdef not_used
949 #ifdef KWM_HINTS
951 int i;
953 for (i = 0; i < scr->workspace_count; i++) {
954 wKWMSetUsableAreaHint(scr, i);
957 #endif
958 #endif
962 void
963 wScreenRestoreState(WScreen *scr)
965 proplist_t state;
966 char *path;
968 make_keys();
970 if (wScreenCount == 1)
971 path = wdefaultspathfordomain("WMState");
972 else {
973 char buf[16];
974 sprintf(buf, "WMState.%i", scr->screen);
975 path = wdefaultspathfordomain(buf);
977 scr->session_state = PLGetProplistWithPath(path);
978 free(path);
979 if (!scr->session_state && wScreenCount>1) {
980 char buf[16];
981 sprintf(buf, "WMState.%i", scr->screen);
982 path = wdefaultspathfordomain(buf);
983 scr->session_state = PLGetProplistWithPath(path);
984 free(path);
987 if (!wPreferences.flags.noclip) {
988 state = PLGetDictionaryEntry(scr->session_state, dClip);
989 scr->clip_icon = wClipRestoreState(scr, state);
992 wWorkspaceRestoreState(scr);
994 if (!wPreferences.flags.nodock) {
995 state = PLGetDictionaryEntry(scr->session_state, dDock);
996 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
999 wScreenUpdateUsableArea(scr);
1003 void
1004 wScreenSaveState(WScreen *scr)
1006 WWorkspaceState wstate;
1007 WWindow *wwin;
1008 char *str;
1009 proplist_t path, old_state, foo;
1010 CARD32 data[2];
1013 make_keys();
1016 * Save current workspace, so can go back to it upon restart.
1018 wstate.workspace = scr->current_workspace;
1020 data[0] = wstate.flags;
1021 data[1] = wstate.workspace;
1023 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1024 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1025 (unsigned char *) data, 2);
1027 /* save state of windows */
1028 wwin = scr->focused_window;
1029 while (wwin) {
1030 wWindowSaveState(wwin);
1031 wwin = wwin->prev;
1035 if (wPreferences.flags.noupdates)
1036 return;
1039 old_state = scr->session_state;
1040 scr->session_state = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
1042 PLSetStringCmpHook(NULL);
1044 /* save dock state to file */
1045 if (!wPreferences.flags.nodock) {
1046 wDockSaveState(scr);
1047 } else {
1048 if ((foo = PLGetDictionaryEntry(old_state, dDock))!=NULL) {
1049 PLInsertDictionaryEntry(scr->session_state, dDock, foo);
1052 if (!wPreferences.flags.noclip) {
1053 wClipSaveState(scr);
1054 } else {
1055 if ((foo = PLGetDictionaryEntry(old_state, dClip))!=NULL) {
1056 PLInsertDictionaryEntry(scr->session_state, dClip, foo);
1060 wWorkspaceSaveState(scr, old_state);
1062 if (wPreferences.save_session_on_exit) {
1063 wSessionSaveState(scr);
1064 } else {
1065 if ((foo = PLGetDictionaryEntry(old_state, dApplications))!=NULL) {
1066 PLInsertDictionaryEntry(scr->session_state, dApplications, foo);
1068 if ((foo = PLGetDictionaryEntry(old_state, dWorkspace))!=NULL) {
1069 PLInsertDictionaryEntry(scr->session_state, dWorkspace, foo);
1073 /* clean up */
1074 PLSetStringCmpHook(StringCompareHook);
1076 wMenuSaveState(scr);
1078 if (wScreenCount == 1)
1079 str = wdefaultspathfordomain("WMState");
1080 else {
1081 char buf[16];
1082 sprintf(buf, "WMState.%i", scr->screen);
1083 str = wdefaultspathfordomain(buf);
1085 path = PLMakeString(str);
1086 free(str);
1087 PLSetFilename(scr->session_state, path);
1088 if (!PLSave(scr->session_state, YES)) {
1089 wwarning(_("could not save session state in %s"), PLGetString(path));
1091 PLRelease(path);
1092 PLRelease(old_state);
1098 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1100 int moved = 0;
1101 int tol_w, tol_h;
1103 if (width > 20)
1104 tol_w = width/2;
1105 else
1106 tol_w = 20;
1108 if (height > 20)
1109 tol_h = height/2;
1110 else
1111 tol_h = 20;
1113 if (*x+width < 10)
1114 *x = -tol_w, moved = 1;
1115 else if (*x >= scr->scr_width - 10)
1116 *x = scr->scr_width - tol_w - 1, moved = 1;
1118 if (*y < -height + 10)
1119 *y = -tol_h, moved = 1;
1120 else if (*y >= scr->scr_height - 10)
1121 *y = scr->scr_height - tol_h - 1, moved = 1;
1123 return moved;