- s/sprintf/snprintf
[wmaker-crm.git] / src / screen.c
blob27f723c968a4e4df46e1c1dec85b0817672f3f48
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 #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
66 #include <proplist.h>
68 #include "defaults.h"
71 #ifdef LITE
72 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
73 |SubstructureNotifyMask|PointerMotionMask \
74 |SubstructureRedirectMask|KeyPressMask|KeyReleaseMask)
75 #else
76 #define EVENT_MASK (LeaveWindowMask|EnterWindowMask|PropertyChangeMask\
77 |SubstructureNotifyMask|PointerMotionMask \
78 |SubstructureRedirectMask|ButtonPressMask|ButtonReleaseMask\
79 |KeyPressMask|KeyReleaseMask)
80 #endif
82 /**** Global variables ****/
84 extern Cursor wCursor[WCUR_LAST];
85 extern WPreferences wPreferences;
86 extern Atom _XA_WINDOWMAKER_STATE;
87 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
90 extern int wScreenCount;
92 #ifdef KEEP_XKB_LOCK_STATUS
93 extern int wXkbSupported;
94 #endif
96 extern WDDomain *WDWindowMaker;
99 /**** Local ****/
101 #define STIPPLE_WIDTH 2
102 #define STIPPLE_HEIGHT 2
103 static char STIPPLE_DATA[] = {0x02, 0x01};
105 static int CantManageScreen = 0;
107 static proplist_t dApplications = NULL;
108 static proplist_t dWorkspace;
109 static proplist_t dDock;
110 static proplist_t dClip;
113 static void
114 make_keys()
116 if (dApplications!=NULL)
117 return;
119 dApplications = PLMakeString("Applications");
120 dWorkspace = PLMakeString("Workspace");
121 dDock = PLMakeString("Dock");
122 dClip = PLMakeString("Clip");
127 *----------------------------------------------------------------------
128 * alreadyRunningError--
129 * X error handler used to catch errors when trying to do
130 * XSelectInput() on the root window. These errors probably mean that
131 * there already is some other window manager running.
133 * Returns:
134 * Nothing, unless something really evil happens...
136 * Side effects:
137 * CantManageScreen is set to 1;
138 *----------------------------------------------------------------------
140 static int
141 alreadyRunningError(Display *dpy, XErrorEvent *error)
143 CantManageScreen = 1;
144 return -1;
149 *----------------------------------------------------------------------
150 * allocButtonPixmaps--
151 * Allocate pixmaps used on window operation buttons (those in the
152 * titlebar). The pixmaps are linked to the program. If XPM is supported
153 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
155 * Returns:
156 * Nothing
158 * Side effects:
159 * Allocates shared pixmaps for the screen. These pixmaps should
160 * not be freed by anybody.
161 *----------------------------------------------------------------------
163 static void
164 allocButtonPixmaps(WScreen *scr)
166 WPixmap *pix;
168 /* create predefined pixmaps */
169 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
170 if (pix)
171 pix->shared = 1;
172 scr->b_pixmaps[WBUT_CLOSE] = pix;
174 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
175 if (pix)
176 pix->shared = 1;
177 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
179 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
180 if (pix)
181 pix->shared = 1;
182 scr->b_pixmaps[WBUT_ICONIFY] = pix;
183 #ifdef XKB_BUTTON_HINT
184 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
185 if (pix)
186 pix->shared = 1;
187 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
188 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
189 if (pix)
190 pix->shared = 1;
191 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
192 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
193 if (pix)
194 pix->shared = 1;
195 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
196 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
197 if (pix)
198 pix->shared = 1;
199 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
200 #endif
203 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
204 if (pix)
205 pix->shared = 1;
206 scr->b_pixmaps[WBUT_KILL] = pix;
210 static void
211 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
213 XSetForeground(dpy, gc, scr->black_pixel);
214 XDrawLine(dpy, d, gc, x, y, x+1, y);
215 XDrawPoint(dpy, d, gc, x, y+1);
216 XSetForeground(dpy, gc, scr->white_pixel);
217 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
218 XDrawPoint(dpy, d, gc, x+1, y+1);
222 static WPixmap*
223 make3Dots(WScreen *scr)
225 WPixmap *wpix;
226 GC gc2, gc;
227 XGCValues gcv;
228 Pixmap pix, mask;
230 gc = scr->copy_gc;
231 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
232 wPreferences.icon_size, scr->w_depth);
233 XSetForeground(dpy, gc, scr->black_pixel);
234 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
235 wPreferences.icon_size);
236 XSetForeground(dpy, gc, scr->white_pixel);
237 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
238 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
239 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
241 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
242 wPreferences.icon_size, 1);
243 gcv.foreground = 0;
244 gcv.graphics_exposures = False;
245 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
246 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
247 wPreferences.icon_size);
248 XSetForeground(dpy, gc2, 1);
249 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
250 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
251 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
253 XFreeGC(dpy, gc2);
255 wpix = wPixmapCreate(scr, pix, mask);
256 wpix->shared = 1;
258 return wpix;
262 static void
263 allocGCs(WScreen *scr)
265 XGCValues gcv;
266 XColor color;
267 unsigned long mtextcolor;
268 int gcm;
270 scr->stipple_bitmap =
271 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
272 STIPPLE_HEIGHT);
274 gcv.stipple = scr->stipple_bitmap;
275 gcv.foreground = scr->white_pixel;
276 gcv.fill_style = FillStippled;
277 gcv.graphics_exposures = False;
278 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
279 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
282 /* selected icon border GCs */
283 gcv.function = GXcopy;
284 gcv.foreground = scr->white_pixel;
285 gcv.background = scr->black_pixel;
286 gcv.line_width = 1;
287 gcv.line_style = LineDoubleDash;
288 gcv.fill_style = FillSolid;
289 gcv.dash_offset = 0;
290 gcv.dashes = 4;
291 gcv.graphics_exposures = False;
293 gcm = GCFunction | GCGraphicsExposures;
294 gcm |= GCForeground | GCBackground;
295 gcm |= GCLineWidth | GCLineStyle;
296 gcm |= GCFillStyle;
297 gcm |= GCDashOffset | GCDashList;
299 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
301 gcm = GCForeground|GCGraphicsExposures;
303 scr->menu_title_pixel[0] = scr->white_pixel;
304 gcv.foreground = scr->white_pixel;
305 gcv.graphics_exposures = False;
306 scr->menu_title_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
308 scr->mtext_pixel = scr->black_pixel;
309 mtextcolor = gcv.foreground = scr->black_pixel;
310 scr->menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
312 /* selected menu entry GC */
313 gcm = GCForeground|GCBackground|GCGraphicsExposures;
314 gcv.foreground = scr->white_pixel;
315 gcv.background = scr->white_pixel;
316 gcv.graphics_exposures = False;
317 scr->select_menu_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
319 /* disabled menu entry GC */
320 scr->dtext_pixel = scr->black_pixel;
321 gcm = GCForeground|GCBackground|GCStipple|GCGraphicsExposures;
322 gcv.stipple = scr->stipple_bitmap;
323 gcv.graphics_exposures = False;
324 scr->disabled_menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
326 /* frame GC */
327 wGetColor(scr, DEF_FRAME_COLOR, &color);
328 gcv.function = GXxor;
329 /* this will raise the probability of the XORed color being different
330 * of the original color in PseudoColor when not all color cells are
331 * initialized */
332 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
333 gcv.plane_mask = (1<<(scr->depth-1))|1;
334 else
335 gcv.plane_mask = AllPlanes;
336 gcv.foreground = color.pixel;
337 if (gcv.foreground == 0)
338 gcv.foreground = 1;
339 gcv.line_width = DEF_FRAME_THICKNESS;
340 gcv.subwindow_mode = IncludeInferiors;
341 gcv.graphics_exposures = False;
342 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
343 |GCFunction|GCSubwindowMode|GCLineWidth
344 |GCPlaneMask, &gcv);
346 /* line GC */
347 gcv.foreground = color.pixel;
349 if (gcv.foreground == 0)
350 /* XOR:ing with a zero is not going to be of much use, so
351 in that case, we somewhat arbitrarily xor with 17 instead. */
352 gcv.foreground = 17;
354 gcv.function = GXxor;
355 gcv.subwindow_mode = IncludeInferiors;
356 gcv.line_width = 1;
357 gcv.cap_style = CapRound;
358 gcv.graphics_exposures = False;
359 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
360 |GCGraphicsExposures;
361 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
363 scr->line_pixel = gcv.foreground;
365 /* copy GC */
366 gcv.foreground = scr->white_pixel;
367 gcv.background = scr->black_pixel;
368 gcv.graphics_exposures = False;
369 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
370 |GCGraphicsExposures, &gcv);
372 /* window title text GC */
373 gcv.graphics_exposures = False;
374 scr->window_title_gc = XCreateGC(dpy, scr->w_win,GCGraphicsExposures,&gcv);
376 /* icon title GC */
377 scr->icon_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
379 /* clip title GC */
380 scr->clip_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
382 /* move/size display GC */
383 gcv.graphics_exposures = False;
384 gcm = GCGraphicsExposures;
385 scr->info_text_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
387 /* misc drawing GC */
388 gcv.graphics_exposures = False;
389 gcm = GCGraphicsExposures;
390 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
392 assert (scr->stipple_bitmap!=None);
395 /* mono GC */
396 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
401 static void
402 createPixmaps(WScreen *scr)
404 WPixmap *pix;
405 RImage *image;
407 /* load pixmaps */
408 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
409 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
410 MENU_RADIO_INDICATOR_XBM_SIZE,
411 MENU_RADIO_INDICATOR_XBM_SIZE,
412 scr->black_pixel, scr->white_pixel);
413 if (pix!=NULL)
414 pix->shared = 1;
415 scr->menu_radio_indicator = pix;
418 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
419 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
420 MENU_CHECK_INDICATOR_XBM_SIZE,
421 MENU_CHECK_INDICATOR_XBM_SIZE,
422 scr->black_pixel, scr->white_pixel);
423 if (pix!=NULL)
424 pix->shared = 1;
425 scr->menu_check_indicator = pix;
427 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
428 (char*)MENU_MINI_INDICATOR_XBM_DATA,
429 MENU_MINI_INDICATOR_XBM_SIZE,
430 MENU_MINI_INDICATOR_XBM_SIZE,
431 scr->black_pixel, scr->white_pixel);
432 if (pix!=NULL)
433 pix->shared = 1;
434 scr->menu_mini_indicator = pix;
436 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
437 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
438 MENU_HIDE_INDICATOR_XBM_SIZE,
439 MENU_HIDE_INDICATOR_XBM_SIZE,
440 scr->black_pixel, scr->white_pixel);
441 if (pix!=NULL)
442 pix->shared = 1;
443 scr->menu_hide_indicator = pix;
445 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
446 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
447 MENU_SHADE_INDICATOR_XBM_SIZE,
448 MENU_SHADE_INDICATOR_XBM_SIZE,
449 scr->black_pixel, scr->white_pixel);
450 if (pix!=NULL)
451 pix->shared = 1;
452 scr->menu_shade_indicator = pix;
455 image = wDefaultGetImage(scr, "Logo", "WMPanel");
457 if (!image) {
458 wwarning(_("could not load logo image for panels: %s"),
459 RMessageForError(RErrorCode));
460 } else {
461 WMSetApplicationIconImage(scr->wmscreen, image);
462 RReleaseImage(image);
465 scr->dock_dots = make3Dots(scr);
467 /* titlebar button pixmaps */
468 allocButtonPixmaps(scr);
473 *----------------------------------------------------------------------
474 * createInternalWindows--
475 * Creates some windows used internally by the program. One to
476 * receive input focus when no other window can get it and another
477 * to display window geometry information during window resize/move.
479 * Returns:
480 * Nothing
482 * Side effects:
483 * Windows are created and some colors are allocated for the
484 * window background.
485 *----------------------------------------------------------------------
487 static void
488 createInternalWindows(WScreen *scr)
490 int vmask;
491 XSetWindowAttributes attribs;
493 /* InputOnly window to get the focus when no other window can get it */
494 vmask = CWEventMask|CWOverrideRedirect;
495 attribs.event_mask = KeyPressMask|FocusChangeMask;
496 attribs.override_redirect = True;
497 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
498 InputOnly,CopyFromParent, vmask, &attribs);
499 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
500 XMapWindow(dpy, scr->no_focus_win);
502 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
504 /* shadow window for dock buttons */
505 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
506 attribs.border_pixel = scr->black_pixel;
507 attribs.save_under = True;
508 attribs.override_redirect = True;
509 attribs.background_pixmap = None;
510 attribs.background_pixel = scr->white_pixel;
511 attribs.cursor = wCursor[WCUR_DEFAULT];
512 vmask |= CWColormap;
513 attribs.colormap = scr->w_colormap;
514 scr->dock_shadow =
515 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
516 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
517 scr->w_visual, vmask, &attribs);
519 /* workspace name balloon for clip */
520 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
521 |CWBorderPixel;
522 attribs.save_under = True;
523 attribs.override_redirect = True;
524 attribs.colormap = scr->w_colormap;
525 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
526 attribs.border_pixel = 0; /* do not care */
527 scr->clip_balloon =
528 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
529 CopyFromParent, scr->w_visual, vmask, &attribs);
532 /* workspace name */
533 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
534 |CWBorderPixel;
535 attribs.save_under = True;
536 attribs.override_redirect = True;
537 attribs.colormap = scr->w_colormap;
538 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
539 attribs.border_pixel = 0; /* do not care */
540 scr->workspace_name =
541 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
542 CopyFromParent, scr->w_visual, vmask, &attribs);
545 * If the window is clicked without having ButtonPress selected, the
546 * resulting event will have event.xbutton.window == root.
548 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
552 #if 0
553 static Bool
554 aquireManagerSelection(WScreen *scr)
556 char buffer[32];
557 XEvent ev;
558 Time timestamp;
560 snprintf(buffer, sizeof(buffer), "WM_S%i", scr->screen);
561 scr->managerAtom = XInternAtom(dpy, buffer, False);
563 /* for race-conditions... */
564 XGrabServer(dpy);
566 /* if there is another manager running, don't try to replace it
567 * (for now, at least) */
568 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
569 XUngrabServer(dpy);
570 return False;
573 /* become the manager for this screen */
575 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
576 0, 0, 0);
578 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
579 /* get a timestamp */
580 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
581 XA_INTEGER, 32, PropModeAppend, NULL, 0);
582 while (1) {
583 XWindowEvent(dpy, scr->managerWindow, &ev);
584 if (ev.type == PropertyNotify) {
585 timestamp = ev.xproperty.time;
586 break;
589 XSelectInput(dpy, scr->managerWindow, NoEvents);
590 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
592 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
594 XUngrabServer(dpy);
596 /* announce our arrival */
598 ev.xclient.type = ClientMessage;
599 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
600 ev.xclient.destination = scr->root_win;
601 ev.xclient.format = 32;
602 ev.xclient.data.l[0] = timestamp;
603 ev.xclient.data.l[1] = scr->managerAtom;
604 ev.xclient.data.l[2] = scr->managerWindow;
605 ev.xclient.data.l[3] = 0;
606 ev.xclient.data.l[4] = 0;
608 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
609 XSync(dpy, False);
611 return True;
613 #endif
616 *----------------------------------------------------------------------
617 * wScreenInit--
618 * Initializes the window manager for the given screen and
619 * allocates a WScreen descriptor for it. Many resources are allocated
620 * for the screen and the root window is setup appropriately.
622 * Returns:
623 * The WScreen descriptor for the screen.
625 * Side effects:
626 * Many resources are allocated and the IconSize property is
627 * set on the root window.
628 * The program can be aborted if some fatal error occurs.
630 * TODO: User specifiable visual.
631 *----------------------------------------------------------------------
633 WScreen*
634 wScreenInit(int screen_number)
636 WScreen *scr;
637 XIconSize icon_size[1];
638 RContextAttributes rattr;
639 extern int wVisualID;
640 long event_mask;
641 WMColor *color;
642 XErrorHandler oldHandler;
644 scr = wmalloc(sizeof(WScreen));
645 memset(scr, 0, sizeof(WScreen));
647 scr->stacking_list = WMCreateTreeBag();
649 /* initialize globals */
650 scr->screen = screen_number;
651 scr->root_win = RootWindow(dpy, screen_number);
652 scr->depth = DefaultDepth(dpy, screen_number);
653 scr->colormap = DefaultColormap(dpy, screen_number);
655 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
656 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
658 scr->usableArea.x2 = scr->scr_width;
659 scr->usableArea.y2 = scr->scr_height;
660 scr->totalUsableArea.x2 = scr->scr_width;
661 scr->totalUsableArea.y2 = scr->scr_height;
663 #if 0
664 if (!aquireManagerSelection(scr)) {
665 wfree(scr);
667 return NULL;
669 #endif
670 CantManageScreen = 0;
671 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
673 event_mask = EVENT_MASK;
675 if (wPreferences.disable_root_mouse) {
676 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
679 XSelectInput(dpy, scr->root_win, event_mask);
681 #ifdef KEEP_XKB_LOCK_STATUS
682 /* Only GroupLock doesn't work correctly in my system since right-alt
683 * can change mode while holding it too - ]d
685 if (wXkbSupported) {
686 XkbSelectEvents(dpy,XkbUseCoreKbd,
687 XkbStateNotifyMask,
688 XkbStateNotifyMask);
690 #endif /* KEEP_XKB_LOCK_STATUS */
692 XSync(dpy, False);
693 XSetErrorHandler(oldHandler);
695 if (CantManageScreen) {
696 wfree(scr);
697 return NULL;
700 #ifndef DEFINABLE_CURSOR
701 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_DEFAULT]);
702 #endif
704 /* screen descriptor for raster graphic library */
705 rattr.flags = RC_RenderMode | RC_ColorsPerChannel | RC_StandardColormap;
706 rattr.render_mode = wPreferences.no_dithering
707 ? RBestMatchRendering
708 : RDitheredRendering;
710 /* if the std colormap stuff works ok, this will be ignored */
711 rattr.colors_per_channel = wPreferences.cmap_size;
712 if (rattr.colors_per_channel<2)
713 rattr.colors_per_channel = 2;
716 /* will only be accounted for in PseudoColor */
717 if (wPreferences.flags.create_stdcmap) {
718 rattr.standard_colormap_mode = RCreateStdColormap;
719 } else {
720 rattr.standard_colormap_mode = RUseStdColormap;
723 if (wVisualID>=0) {
724 rattr.flags |= RC_VisualID;
725 rattr.visualid = wVisualID;
728 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
730 if (!scr->rcontext && RErrorCode == RERR_STDCMAPFAIL) {
731 wwarning(RMessageForError(RErrorCode));
733 rattr.flags &= ~RC_StandardColormap;
734 rattr.standard_colormap_mode = RUseStdColormap;
736 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
739 if (!scr->rcontext) {
740 wwarning(_("could not initialize graphics library context: %s"),
741 RMessageForError(RErrorCode));
742 wAbort(False);
743 } else {
744 char **formats;
745 int i = 0;
747 formats = RSupportedFileFormats();
748 if (formats) {
749 for (i=0; formats[i]!=NULL; i++) {
750 if (strcmp(formats[i], "TIFF")==0) {
751 scr->flags.supports_tiff = 1;
752 break;
758 scr->w_win = scr->rcontext->drawable;
759 scr->w_visual = scr->rcontext->visual;
760 scr->w_depth = scr->rcontext->depth;
761 scr->w_colormap = scr->rcontext->cmap;
763 scr->black_pixel = scr->rcontext->black;
764 scr->white_pixel = scr->rcontext->white;
766 /* create screen descriptor for WINGs */
767 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
768 scr->rcontext);
770 if (!scr->wmscreen) {
771 wfatal(_("could not do initialization of WINGs widget set"));
773 return NULL;
776 color = WMGrayColor(scr->wmscreen);
777 scr->light_pixel = WMColorPixel(color);
778 WMReleaseColor(color);
780 color = WMDarkGrayColor(scr->wmscreen);
781 scr->dark_pixel = WMColorPixel(color);
782 WMReleaseColor(color);
785 XColor xcol;
786 /* frame boder color */
787 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
788 scr->frame_border_pixel = xcol.pixel;
791 /* create GCs with default values */
792 allocGCs(scr);
795 /* for our window manager info notice board. Need to
796 * create before reading the defaults, because it will be used there.
798 scr->info_window = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 10, 10,
799 0, 0, 0);
801 /* read defaults for this screen */
802 wReadDefaults(scr, WDWindowMaker->dictionary);
804 createInternalWindows(scr);
806 #ifdef KWM_HINTS
807 wKWMInitStuff(scr);
808 #endif
810 #ifdef GNOME_STUFF
811 wGNOMEInitStuff(scr);
812 #endif
814 #ifdef OLWM_HINTS
815 wOLWMInitStuff(scr);
816 #endif
818 /* create initial workspace */
819 wWorkspaceNew(scr);
821 /* create shared pixmaps */
822 createPixmaps(scr);
824 /* set icon sizes we can accept from clients */
825 icon_size[0].min_width = 8;
826 icon_size[0].min_height = 8;
827 icon_size[0].max_width = wPreferences.icon_size-4;
828 icon_size[0].max_height = wPreferences.icon_size-4;
829 icon_size[0].width_inc = 1;
830 icon_size[0].height_inc = 1;
831 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
833 /* setup WindowMaker protocols property in the root window*/
834 PropSetWMakerProtocols(scr->root_win);
836 /* setup our noticeboard */
837 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
838 XA_WINDOW, 32, PropModeReplace,
839 (unsigned char*)&scr->info_window, 1);
840 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
841 XA_WINDOW, 32, PropModeReplace,
842 (unsigned char*)&scr->info_window, 1);
845 #ifdef BALLOON_TEXT
846 /* initialize balloon text stuff */
847 wBalloonInitialize(scr);
848 #endif
850 scr->info_text_font = WMBoldSystemFontOfSize(scr->wmscreen, 12);
853 scr->gview = WCreateGeometryView(scr->wmscreen);
854 WMRealizeWidget(scr->gview);
856 wScreenUpdateUsableArea(scr);
858 return scr;
862 void
863 wScreenUpdateUsableArea(WScreen *scr)
865 #ifdef GNOME_STUFF
866 WReservedArea *area;
867 #endif
869 scr->totalUsableArea = scr->usableArea;
872 if (scr->dock && (!scr->dock->lowered
873 || wPreferences.no_window_over_dock)) {
875 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
877 if (scr->dock->on_right_side) {
878 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
879 scr->scr_width - offset);
880 } else {
881 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
885 if (wPreferences.no_window_over_icons) {
886 if (wPreferences.icon_yard & IY_VERT) {
888 if (!(wPreferences.icon_yard & IY_RIGHT)) {
889 scr->totalUsableArea.x1 += wPreferences.icon_size;
890 } else {
891 scr->totalUsableArea.x2 -= wPreferences.icon_size;
893 } else {
895 if (wPreferences.icon_yard & IY_TOP) {
896 scr->totalUsableArea.y1 += wPreferences.icon_size;
897 } else {
898 scr->totalUsableArea.y2 -= wPreferences.icon_size;
903 #ifdef KWM_HINTS
905 WArea area;
907 if (wKWMGetUsableArea(scr, &area)) {
908 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
909 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
910 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
911 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
914 #endif
916 #ifdef GNOME_STUFF
917 area = scr->reservedAreas;
919 while (area) {
920 int th, bh;
921 int lw, rw;
922 int w, h;
924 w = area->area.x2 - area->area.x1;
925 h = area->area.y2 - area->area.y1;
927 th = area->area.y1;
928 bh = scr->scr_height - area->area.y2;
929 lw = area->area.x1;
930 rw = scr->scr_width - area->area.x2;
932 if (WMIN(th, bh) <= WMIN(lw, rw)) {
933 /* horizontal */
934 if (th < bh) {
935 /* on top */
936 if (scr->totalUsableArea.y1 < area->area.y2)
937 scr->totalUsableArea.y1 = area->area.y2;
938 } else {
939 /* on bottom */
940 if (scr->totalUsableArea.y2 > area->area.y1)
941 scr->totalUsableArea.y2 = area->area.y1;
943 } else {
944 /* vertical */
945 if (lw < rw) {
946 /* on left */
947 if (scr->totalUsableArea.x1 < area->area.x2)
948 scr->totalUsableArea.x1 = area->area.x2;
949 } else {
950 /* on right */
951 if (scr->totalUsableArea.x2 > area->area.x1)
952 scr->totalUsableArea.x2 = area->area.x1;
956 area = area->next;
958 #endif /* GNOME_STUFF */
960 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
961 scr->totalUsableArea.x2 = scr->usableArea.x2;
962 scr->totalUsableArea.x1 = scr->usableArea.x1;
964 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
965 scr->totalUsableArea.y2 = scr->usableArea.y2;
966 scr->totalUsableArea.y1 = scr->usableArea.y1;
969 #ifdef not_used
970 #ifdef KWM_HINTS
972 int i;
974 for (i = 0; i < scr->workspace_count; i++) {
975 wKWMSetUsableAreaHint(scr, i);
978 #endif
979 #endif
982 unsigned size = wPreferences.workspace_border_size;
983 unsigned position = wPreferences.workspace_border_position;
985 if (size>0 && position!=WB_NONE) {
986 if (position & WB_LEFTRIGHT) {
987 scr->totalUsableArea.x1 += size;
988 scr->totalUsableArea.x2 -= size;
990 if (position & WB_TOPBOTTOM) {
991 scr->totalUsableArea.y1 += size;
992 scr->totalUsableArea.y2 -= size;
1000 void
1001 wScreenRestoreState(WScreen *scr)
1003 proplist_t state;
1004 char *path;
1007 #ifndef LITE
1008 OpenRootMenu(scr, -10000, -10000, False);
1009 wMenuUnmap(scr->root_menu);
1010 #endif
1012 make_keys();
1014 if (wScreenCount == 1)
1015 path = wdefaultspathfordomain("WMState");
1016 else {
1017 char buf[16];
1018 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1019 path = wdefaultspathfordomain(buf);
1021 scr->session_state = PLGetProplistWithPath(path);
1022 wfree(path);
1023 if (!scr->session_state && wScreenCount>1) {
1024 char buf[16];
1025 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1026 path = wdefaultspathfordomain(buf);
1027 scr->session_state = PLGetProplistWithPath(path);
1028 wfree(path);
1031 if (!wPreferences.flags.noclip) {
1032 state = PLGetDictionaryEntry(scr->session_state, dClip);
1033 scr->clip_icon = wClipRestoreState(scr, state);
1036 wWorkspaceRestoreState(scr);
1038 #ifdef VIRTUAL_DESKTOP
1040 * * create inputonly windows at the border of screen
1041 * */
1042 wWorkspaceManageEdge(scr);
1043 #endif
1045 if (!wPreferences.flags.nodock) {
1046 state = PLGetDictionaryEntry(scr->session_state, dDock);
1047 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
1050 wScreenUpdateUsableArea(scr);
1054 void
1055 wScreenSaveState(WScreen *scr)
1057 WWindow *wwin;
1058 char *str;
1059 proplist_t path, old_state, foo;
1060 CARD32 data[2];
1063 make_keys();
1065 /* Save current workspace, so can go back to it upon restart. */
1066 data[0] = scr->current_workspace;
1067 data[1] = WFLAGS_NONE;
1069 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1070 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1071 (unsigned char *) data, 2);
1073 /* save state of windows */
1074 wwin = scr->focused_window;
1075 while (wwin) {
1076 wWindowSaveState(wwin);
1077 wwin = wwin->prev;
1081 if (wPreferences.flags.noupdates)
1082 return;
1085 old_state = scr->session_state;
1086 scr->session_state = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
1088 PLSetStringCmpHook(NULL);
1090 /* save dock state to file */
1091 if (!wPreferences.flags.nodock) {
1092 wDockSaveState(scr, old_state);
1093 } else {
1094 if ((foo = PLGetDictionaryEntry(old_state, dDock))!=NULL) {
1095 PLInsertDictionaryEntry(scr->session_state, dDock, foo);
1098 if (!wPreferences.flags.noclip) {
1099 wClipSaveState(scr);
1100 } else {
1101 if ((foo = PLGetDictionaryEntry(old_state, dClip))!=NULL) {
1102 PLInsertDictionaryEntry(scr->session_state, dClip, foo);
1106 wWorkspaceSaveState(scr, old_state);
1108 if (wPreferences.save_session_on_exit) {
1109 wSessionSaveState(scr);
1110 } else {
1111 if ((foo = PLGetDictionaryEntry(old_state, dApplications))!=NULL) {
1112 PLInsertDictionaryEntry(scr->session_state, dApplications, foo);
1114 if ((foo = PLGetDictionaryEntry(old_state, dWorkspace))!=NULL) {
1115 PLInsertDictionaryEntry(scr->session_state, dWorkspace, foo);
1119 /* clean up */
1120 PLSetStringCmpHook(StringCompareHook);
1122 wMenuSaveState(scr);
1124 if (wScreenCount == 1)
1125 str = wdefaultspathfordomain("WMState");
1126 else {
1127 char buf[16];
1128 snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
1129 str = wdefaultspathfordomain(buf);
1131 path = PLMakeString(str);
1132 wfree(str);
1133 PLSetFilename(scr->session_state, path);
1134 if (!PLSave(scr->session_state, YES)) {
1135 wsyserror(_("could not save session state in %s"), PLGetString(path));
1137 PLRelease(path);
1138 PLRelease(old_state);
1144 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1146 int moved = 0;
1147 int tol_w, tol_h;
1149 if (width > 20)
1150 tol_w = width/2;
1151 else
1152 tol_w = 20;
1154 if (height > 20)
1155 tol_h = height/2;
1156 else
1157 tol_h = 20;
1159 if (*x+width < 10)
1160 *x = -tol_w, moved = 1;
1161 else if (*x >= scr->scr_width - 10)
1162 *x = scr->scr_width - tol_w - 1, moved = 1;
1164 if (*y < -height + 10)
1165 *y = -tol_h, moved = 1;
1166 else if (*y >= scr->scr_height - 10)
1167 *y = scr->scr_height - tol_h - 1, moved = 1;
1169 return moved;