Changes relate to modelock.
[wmaker-crm.git] / src / screen.c
blobea0a083febfc70a438b88344c98c3eb8e7bbbace
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;
178 #ifdef XKB_BUTTON_HINT
179 pix = wPixmapCreateFromXPMData(scr, PRED_THAI_XPM);
180 if (pix)
181 pix->shared = 1;
182 scr->b_pixmaps[WBUT_THAI] = pix;
183 pix = wPixmapCreateFromXPMData(scr, PRED_ENGL_XPM);
184 if (pix)
185 pix->shared = 1;
186 scr->b_pixmaps[WBUT_ENGL] = pix;
187 #endif
190 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
191 if (pix)
192 pix->shared = 1;
193 scr->b_pixmaps[WBUT_KILL] = pix;
197 static void
198 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
200 XSetForeground(dpy, gc, scr->black_pixel);
201 XDrawLine(dpy, d, gc, x, y, x+1, y);
202 XDrawPoint(dpy, d, gc, x, y+1);
203 XSetForeground(dpy, gc, scr->white_pixel);
204 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
205 XDrawPoint(dpy, d, gc, x+1, y+1);
209 static WPixmap*
210 make3Dots(WScreen *scr)
212 WPixmap *wpix;
213 GC gc2, gc;
214 XGCValues gcv;
215 Pixmap pix, mask;
217 gc = scr->copy_gc;
218 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
219 wPreferences.icon_size, scr->w_depth);
220 XSetForeground(dpy, gc, scr->black_pixel);
221 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
222 wPreferences.icon_size);
223 XSetForeground(dpy, gc, scr->white_pixel);
224 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
225 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
226 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
228 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
229 wPreferences.icon_size, 1);
230 gcv.foreground = 0;
231 gcv.graphics_exposures = False;
232 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
233 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
234 wPreferences.icon_size);
235 XSetForeground(dpy, gc2, 1);
236 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
237 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
238 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
240 XFreeGC(dpy, gc2);
242 wpix = wPixmapCreate(scr, pix, mask);
243 wpix->shared = 1;
245 return wpix;
249 static void
250 allocGCs(WScreen *scr)
252 XGCValues gcv;
253 XColor color;
254 unsigned long mtextcolor;
255 int gcm;
257 scr->stipple_bitmap =
258 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
259 STIPPLE_HEIGHT);
261 gcv.stipple = scr->stipple_bitmap;
262 gcv.foreground = scr->white_pixel;
263 gcv.fill_style = FillStippled;
264 gcv.graphics_exposures = False;
265 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
266 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
269 /* selected icon border GCs */
270 gcv.function = GXcopy;
271 gcv.foreground = scr->white_pixel;
272 gcv.background = scr->black_pixel;
273 gcv.line_width = 1;
274 gcv.line_style = LineDoubleDash;
275 gcv.fill_style = FillSolid;
276 gcv.dash_offset = 0;
277 gcv.dashes = 4;
278 gcv.graphics_exposures = False;
280 gcm = GCFunction | GCGraphicsExposures;
281 gcm |= GCForeground | GCBackground;
282 gcm |= GCLineWidth | GCLineStyle;
283 gcm |= GCFillStyle;
284 gcm |= GCDashOffset | GCDashList;
286 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
288 gcm = GCForeground|GCGraphicsExposures;
290 scr->menu_title_pixel[0] = scr->white_pixel;
291 gcv.foreground = scr->white_pixel;
292 gcv.graphics_exposures = False;
293 scr->menu_title_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
295 scr->mtext_pixel = scr->black_pixel;
296 mtextcolor = gcv.foreground = scr->black_pixel;
297 scr->menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
299 /* selected menu entry GC */
300 gcm = GCForeground|GCBackground|GCGraphicsExposures;
301 gcv.foreground = scr->white_pixel;
302 gcv.background = scr->white_pixel;
303 gcv.graphics_exposures = False;
304 scr->select_menu_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
306 /* disabled menu entry GC */
307 scr->dtext_pixel = scr->black_pixel;
308 gcm = GCForeground|GCBackground|GCStipple|GCGraphicsExposures;
309 gcv.stipple = scr->stipple_bitmap;
310 gcv.graphics_exposures = False;
311 scr->disabled_menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
313 /* frame GC */
314 wGetColor(scr, DEF_FRAME_COLOR, &color);
315 gcv.function = GXxor;
316 /* this will raise the probability of the XORed color being different
317 * of the original color in PseudoColor when not all color cells are
318 * initialized */
319 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
320 gcv.plane_mask = (1<<(scr->depth-1))|1;
321 else
322 gcv.plane_mask = AllPlanes;
323 gcv.foreground = color.pixel;
324 if (gcv.foreground == 0)
325 gcv.foreground = 1;
326 gcv.line_width = DEF_FRAME_THICKNESS;
327 gcv.subwindow_mode = IncludeInferiors;
328 gcv.graphics_exposures = False;
329 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
330 |GCFunction|GCSubwindowMode|GCLineWidth
331 |GCPlaneMask, &gcv);
333 /* line GC */
334 gcv.foreground = color.pixel;
336 if (gcv.foreground == 0)
337 /* XOR:ing with a zero is not going to be of much use, so
338 in that case, we somewhat arbitrarily xor with 17 instead. */
339 gcv.foreground = 17;
341 gcv.function = GXxor;
342 gcv.subwindow_mode = IncludeInferiors;
343 gcv.line_width = 1;
344 gcv.cap_style = CapRound;
345 gcv.graphics_exposures = False;
346 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
347 |GCGraphicsExposures;
348 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
350 scr->line_pixel = gcv.foreground;
352 /* copy GC */
353 gcv.foreground = scr->white_pixel;
354 gcv.background = scr->black_pixel;
355 gcv.graphics_exposures = False;
356 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
357 |GCGraphicsExposures, &gcv);
359 /* window title text GC */
360 gcv.graphics_exposures = False;
361 scr->window_title_gc = XCreateGC(dpy, scr->w_win,GCGraphicsExposures,&gcv);
363 /* icon title GC */
364 scr->icon_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
366 /* clip title GC */
367 scr->clip_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
369 /* move/size display GC */
370 gcv.graphics_exposures = False;
371 gcm = GCGraphicsExposures;
372 scr->info_text_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
374 /* misc drawing GC */
375 gcv.graphics_exposures = False;
376 gcm = GCGraphicsExposures;
377 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
379 assert (scr->stipple_bitmap!=None);
382 /* mono GC */
383 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
388 static void
389 createPixmaps(WScreen *scr)
391 WPixmap *pix;
392 WMPixmap *wmpix;
393 RImage *image;
394 Pixmap p, m;
396 /* load pixmaps */
397 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
398 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
399 MENU_RADIO_INDICATOR_XBM_SIZE,
400 MENU_RADIO_INDICATOR_XBM_SIZE,
401 scr->black_pixel, scr->white_pixel);
402 if (pix!=NULL)
403 pix->shared = 1;
404 scr->menu_radio_indicator = pix;
407 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
408 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
409 MENU_CHECK_INDICATOR_XBM_SIZE,
410 MENU_CHECK_INDICATOR_XBM_SIZE,
411 scr->black_pixel, scr->white_pixel);
412 if (pix!=NULL)
413 pix->shared = 1;
414 scr->menu_check_indicator = pix;
416 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
417 (char*)MENU_MINI_INDICATOR_XBM_DATA,
418 MENU_MINI_INDICATOR_XBM_SIZE,
419 MENU_MINI_INDICATOR_XBM_SIZE,
420 scr->black_pixel, scr->white_pixel);
421 if (pix!=NULL)
422 pix->shared = 1;
423 scr->menu_mini_indicator = pix;
425 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
426 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
427 MENU_HIDE_INDICATOR_XBM_SIZE,
428 MENU_HIDE_INDICATOR_XBM_SIZE,
429 scr->black_pixel, scr->white_pixel);
430 if (pix!=NULL)
431 pix->shared = 1;
432 scr->menu_hide_indicator = pix;
434 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
435 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
436 MENU_SHADE_INDICATOR_XBM_SIZE,
437 MENU_SHADE_INDICATOR_XBM_SIZE,
438 scr->black_pixel, scr->white_pixel);
439 if (pix!=NULL)
440 pix->shared = 1;
441 scr->menu_shade_indicator = pix;
444 image = wDefaultGetImage(scr, "Logo", "WMPanel");
446 if (!image) {
447 wwarning(_("could not load logo image for panels: %s"),
448 RMessageForError(RErrorCode));
449 } else {
450 if (!RConvertImageMask(scr->rcontext, image, &p, &m, 128)) {
451 wwarning(_("error making logo image for panel:%s"), RMessageForError(RErrorCode));
452 } else {
453 wmpix = WMCreatePixmapFromXPixmaps(scr->wmscreen, p, m,
454 image->width, image->height,
455 scr->depth);
456 WMSetApplicationIconImage(scr->wmscreen, wmpix);
457 WMReleasePixmap(wmpix);
459 RDestroyImage(image);
462 scr->dock_dots = make3Dots(scr);
464 #ifndef NEWSTUFF
465 /* titlebar button pixmaps */
466 allocButtonPixmaps(scr);
467 #endif
472 *----------------------------------------------------------------------
473 * createInternalWindows--
474 * Creates some windows used internally by the program. One to
475 * receive input focus when no other window can get it and another
476 * to display window geometry information during window resize/move.
478 * Returns:
479 * Nothing
481 * Side effects:
482 * Windows are created and some colors are allocated for the
483 * window background.
484 *----------------------------------------------------------------------
486 static void
487 createInternalWindows(WScreen *scr)
489 int vmask;
490 XSetWindowAttributes attribs;
492 /* window for displaying geometry information during resizes and moves */
493 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
494 attribs.border_pixel = 0;
495 attribs.save_under = True;
496 attribs.override_redirect = True;
497 attribs.cursor = wCursor[WCUR_DEFAULT];
498 attribs.background_pixmap = None;
499 if (scr->widget_texture)
500 attribs.background_pixel = scr->widget_texture->normal.pixel;
501 else
502 attribs.background_pixel = scr->light_pixel;
503 vmask |= CWColormap;
504 attribs.colormap = scr->w_colormap;
506 wGetGeometryWindowSize(scr, &scr->geometry_display_width,
507 &scr->geometry_display_height);
508 scr->geometry_display =
509 XCreateWindow(dpy, scr->root_win, 1, 1,
510 scr->geometry_display_width,
511 scr->geometry_display_height,
512 1, scr->w_depth, CopyFromParent, scr->w_visual,
513 vmask, &attribs);
515 /* InputOnly window to get the focus when no other window can get it */
516 vmask = CWEventMask|CWOverrideRedirect;
517 attribs.event_mask = KeyPressMask|FocusChangeMask;
518 attribs.override_redirect = True;
519 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
520 InputOnly,CopyFromParent, vmask, &attribs);
521 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
522 XMapWindow(dpy, scr->no_focus_win);
524 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
526 /* shadow window for dock buttons */
527 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
528 attribs.border_pixel = 0;
529 attribs.save_under = True;
530 attribs.override_redirect = True;
531 attribs.background_pixmap = None;
532 attribs.background_pixel = scr->white_pixel;
533 vmask |= CWColormap;
534 attribs.colormap = scr->w_colormap;
535 scr->dock_shadow =
536 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
537 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
538 scr->w_visual, vmask, &attribs);
540 /* workspace name balloon for clip */
541 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
542 |CWBorderPixel;
543 attribs.save_under = True;
544 attribs.override_redirect = True;
545 attribs.colormap = scr->w_colormap;
546 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
547 attribs.border_pixel = 0; /* do not care */
548 scr->clip_balloon =
549 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
550 CopyFromParent, scr->w_visual, vmask, &attribs);
553 /* workspace name */
554 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
555 |CWBorderPixel;
556 attribs.save_under = True;
557 attribs.override_redirect = True;
558 attribs.colormap = scr->w_colormap;
559 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
560 attribs.border_pixel = 0; /* do not care */
561 scr->workspace_name =
562 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
563 CopyFromParent, scr->w_visual, vmask, &attribs);
566 /* for our window manager info notice board */
567 scr->info_window =
568 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, CopyFromParent,
569 CopyFromParent, CopyFromParent, CWOverrideRedirect,
570 &attribs);
573 * If the window is clicked without having ButtonPress selected, the
574 * resulting event will have event.xbutton.window == root.
576 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
580 #if 0
581 static Bool
582 aquireManagerSelection(WScreen *scr)
584 char buffer[32];
585 XEvent ev;
586 Time timestamp;
588 sprintf(buffer, "WM_S%i", scr->screen);
589 scr->managerAtom = XInternAtom(dpy, buffer, False);
591 /* for race-conditions... */
592 XGrabServer(dpy);
594 /* if there is another manager running, don't try to replace it
595 * (for now, at least) */
596 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
597 XUngrabServer(dpy);
598 return False;
601 /* become the manager for this screen */
603 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
604 0, 0, 0);
606 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
607 /* get a timestamp */
608 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
609 XA_INTEGER, 32, PropModeAppend, NULL, 0);
610 while (1) {
611 XWindowEvent(dpy, scr->managerWindow, &ev);
612 if (ev.type == PropertyNotify) {
613 timestamp = ev.xproperty.time;
614 break;
617 XSelectInput(dpy, scr->managerWindow, NoEvents);
618 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
620 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
622 XUngrabServer(dpy);
624 /* announce our arrival */
626 ev.xclient.type = ClientMessage;
627 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
628 ev.xclient.destination = scr->root_win;
629 ev.xclient.format = 32;
630 ev.xclient.data.l[0] = timestamp;
631 ev.xclient.data.l[1] = scr->managerAtom;
632 ev.xclient.data.l[2] = scr->managerWindow;
633 ev.xclient.data.l[3] = 0;
634 ev.xclient.data.l[4] = 0;
636 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
637 XSync(dpy, False);
639 return True;
641 #endif
644 *----------------------------------------------------------------------
645 * wScreenInit--
646 * Initializes the window manager for the given screen and
647 * allocates a WScreen descriptor for it. Many resources are allocated
648 * for the screen and the root window is setup appropriately.
650 * Returns:
651 * The WScreen descriptor for the screen.
653 * Side effects:
654 * Many resources are allocated and the IconSize property is
655 * set on the root window.
656 * The program can be aborted if some fatal error occurs.
658 * TODO: User specifiable visual.
659 *----------------------------------------------------------------------
661 WScreen*
662 wScreenInit(int screen_number)
664 WScreen *scr;
665 XIconSize icon_size[1];
666 RContextAttributes rattr;
667 extern int wVisualID;
668 long event_mask;
669 WMColor *color;
670 XErrorHandler oldHandler;
672 scr = wmalloc(sizeof(WScreen));
673 memset(scr, 0, sizeof(WScreen));
675 /* initialize globals */
676 scr->screen = screen_number;
677 scr->root_win = RootWindow(dpy, screen_number);
678 scr->depth = DefaultDepth(dpy, screen_number);
679 scr->colormap = DefaultColormap(dpy, screen_number);
681 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
682 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
684 scr->usableArea.x2 = scr->scr_width;
685 scr->usableArea.y2 = scr->scr_height;
686 scr->totalUsableArea.x2 = scr->scr_width;
687 scr->totalUsableArea.y2 = scr->scr_height;
689 #if 0
690 if (!aquireManagerSelection(scr)) {
691 free(scr);
693 return NULL;
695 #endif
696 CantManageScreen = 0;
697 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
699 event_mask = EVENT_MASK;
701 if (wPreferences.disable_root_mouse) {
702 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
705 XSelectInput(dpy, scr->root_win, event_mask);
707 #ifdef KEEP_XKB_LOCK_STATUS
708 XkbSelectEvents(dpy,XkbUseCoreKbd,XkbIndicatorStateNotifyMask,
709 XkbIndicatorStateNotifyMask);
710 #endif /* KEEP_XKB_LOCK_STATUS */
712 XSync(dpy, False);
713 XSetErrorHandler(oldHandler);
715 if (CantManageScreen) {
716 free(scr);
717 return NULL;
720 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_DEFAULT]);
722 /* screen descriptor for raster graphic library */
723 rattr.flags = RC_RenderMode | RC_ColorsPerChannel;
724 rattr.render_mode = wPreferences.no_dithering
725 ? RBestMatchRendering
726 : RDitheredRendering;
728 rattr.colors_per_channel = wPreferences.cmap_size;
729 if (rattr.colors_per_channel<2)
730 rattr.colors_per_channel = 2;
732 if (wVisualID>=0) {
733 rattr.flags |= RC_VisualID;
734 rattr.visualid = wVisualID;
736 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
737 if (!scr->rcontext) {
738 wwarning(_("could not initialize graphics library context: %s"),
739 RMessageForError(RErrorCode));
740 wAbort(False);
741 } else {
742 char **formats;
743 int i = 0;
745 formats = RSupportedFileFormats();
746 if (formats) {
747 for (i=0; formats[i]!=NULL; i++) {
748 if (strcmp(formats[i], "TIFF")==0) {
749 scr->flags.supports_tiff = 1;
750 break;
756 scr->w_win = scr->rcontext->drawable;
757 scr->w_visual = scr->rcontext->visual;
758 scr->w_depth = scr->rcontext->depth;
759 scr->w_colormap = scr->rcontext->cmap;
761 scr->black_pixel = scr->rcontext->black;
762 scr->white_pixel = scr->rcontext->white;
764 /* create screen descriptor for WINGs */
765 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
766 scr->rcontext);
768 if (!scr->wmscreen) {
769 wfatal(_("could not do initialization of WINGs widget set"));
771 return NULL;
774 color = WMGrayColor(scr->wmscreen);
775 scr->light_pixel = WMColorPixel(color);
776 WMReleaseColor(color);
778 color = WMDarkGrayColor(scr->wmscreen);
779 scr->dark_pixel = WMColorPixel(color);
780 WMReleaseColor(color);
783 XColor xcol;
784 /* frame boder color */
785 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
786 scr->frame_border_pixel = xcol.pixel;
789 /* create GCs with default values */
790 allocGCs(scr);
792 /* read defaults for this screen */
793 wReadDefaults(scr, WDWindowMaker->dictionary);
795 createInternalWindows(scr);
797 #ifdef KWM_HINTS
798 wKWMInitStuff(scr);
799 #endif
801 #ifdef GNOME_STUFF
802 wGNOMEInitStuff(scr);
803 #endif
805 #ifdef OLWM_HINTS
806 wOLWMInitStuff(scr);
807 #endif
809 /* create initial workspace */
810 wWorkspaceNew(scr);
812 /* create shared pixmaps */
813 createPixmaps(scr);
815 /* set icon sizes we can accept from clients */
816 icon_size[0].min_width = 8;
817 icon_size[0].min_height = 8;
818 icon_size[0].max_width = wPreferences.icon_size-4;
819 icon_size[0].max_height = wPreferences.icon_size-4;
820 icon_size[0].width_inc = 1;
821 icon_size[0].height_inc = 1;
822 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
824 /* setup WindowMaker protocols property in the root window*/
825 PropSetWMakerProtocols(scr->root_win);
827 /* setup our noticeboard */
828 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
829 XA_WINDOW, 32, PropModeReplace,
830 (unsigned char*)&scr->info_window, 1);
831 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
832 XA_WINDOW, 32, PropModeReplace,
833 (unsigned char*)&scr->info_window, 1);
836 #ifdef BALLOON_TEXT
837 /* initialize balloon text stuff */
838 wBalloonInitialize(scr);
839 #endif
841 wScreenUpdateUsableArea(scr);
843 #ifndef LITE
844 /* kluge to load menu configurations at startup */
845 OpenRootMenu(scr, -10000, -10000, False);
846 wMenuUnmap(scr->root_menu);
847 #endif
849 return scr;
853 void
854 wScreenUpdateUsableArea(WScreen *scr)
856 #ifdef GNOME_STUFF
857 WReservedArea *area;
858 #endif
860 scr->totalUsableArea = scr->usableArea;
863 if (scr->dock && (!scr->dock->lowered
864 || wPreferences.no_window_over_dock)) {
866 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
868 if (scr->dock->on_right_side) {
869 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
870 scr->scr_width - offset);
871 } else {
872 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
876 if (wPreferences.no_window_over_icons) {
877 if (wPreferences.icon_yard & IY_VERT) {
879 if (!(wPreferences.icon_yard & IY_RIGHT)) {
880 scr->totalUsableArea.x1 += wPreferences.icon_size;
881 } else {
882 scr->totalUsableArea.x2 -= wPreferences.icon_size;
884 } else {
886 if (wPreferences.icon_yard & IY_TOP) {
887 scr->totalUsableArea.y1 += wPreferences.icon_size;
888 } else {
889 scr->totalUsableArea.y2 -= wPreferences.icon_size;
894 #ifdef KWM_HINTS
896 WArea area;
898 if (wKWMGetUsableArea(scr, &area)) {
899 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
900 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
901 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
902 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
905 #endif
907 #ifdef GNOME_STUFF
908 area = scr->reservedAreas;
910 while (area) {
911 int th, bh;
912 int lw, rw;
913 int w, h;
915 w = area->area.x2 - area->area.x1;
916 h = area->area.y2 - area->area.y1;
918 th = area->area.y1;
919 bh = scr->scr_height - area->area.y2;
920 lw = area->area.x1;
921 rw = scr->scr_width - area->area.x2;
923 if (WMIN(th, bh) < WMIN(lw, rw)) {
924 /* horizontal */
925 if (th < bh) {
926 /* on top */
927 if (scr->totalUsableArea.y1 < area->area.y2)
928 scr->totalUsableArea.y1 = area->area.y2;
929 } else {
930 /* on bottom */
931 if (scr->totalUsableArea.y2 > area->area.y1)
932 scr->totalUsableArea.y2 = area->area.y1;
934 } else {
935 /* vertical */
936 if (lw < rw) {
937 /* on left */
938 if (scr->totalUsableArea.x1 < area->area.x2)
939 scr->totalUsableArea.x1 = area->area.x2;
940 } else {
941 /* on right */
942 if (scr->totalUsableArea.x2 > area->area.x1)
943 scr->totalUsableArea.x2 = area->area.x1;
947 area = area->next;
949 #endif /* GNOME_STUFF */
951 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
952 scr->totalUsableArea.x2 = scr->usableArea.x2;
953 scr->totalUsableArea.x1 = scr->usableArea.x1;
955 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
956 scr->totalUsableArea.y2 = scr->usableArea.y2;
957 scr->totalUsableArea.y1 = scr->usableArea.y1;
960 #ifdef not_used
961 #ifdef KWM_HINTS
963 int i;
965 for (i = 0; i < scr->workspace_count; i++) {
966 wKWMSetUsableAreaHint(scr, i);
969 #endif
970 #endif
974 void
975 wScreenRestoreState(WScreen *scr)
977 proplist_t state;
978 char *path;
980 make_keys();
982 if (wScreenCount == 1)
983 path = wdefaultspathfordomain("WMState");
984 else {
985 char buf[16];
986 sprintf(buf, "WMState.%i", scr->screen);
987 path = wdefaultspathfordomain(buf);
989 scr->session_state = PLGetProplistWithPath(path);
990 free(path);
991 if (!scr->session_state && wScreenCount>1) {
992 char buf[16];
993 sprintf(buf, "WMState.%i", scr->screen);
994 path = wdefaultspathfordomain(buf);
995 scr->session_state = PLGetProplistWithPath(path);
996 free(path);
999 if (!wPreferences.flags.noclip) {
1000 state = PLGetDictionaryEntry(scr->session_state, dClip);
1001 scr->clip_icon = wClipRestoreState(scr, state);
1004 wWorkspaceRestoreState(scr);
1006 if (!wPreferences.flags.nodock) {
1007 state = PLGetDictionaryEntry(scr->session_state, dDock);
1008 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
1011 wScreenUpdateUsableArea(scr);
1015 void
1016 wScreenSaveState(WScreen *scr)
1018 WWorkspaceState wstate;
1019 WWindow *wwin;
1020 char *str;
1021 proplist_t path, old_state, foo;
1022 CARD32 data[2];
1025 make_keys();
1028 * Save current workspace, so can go back to it upon restart.
1030 wstate.workspace = scr->current_workspace;
1032 data[0] = wstate.flags;
1033 data[1] = wstate.workspace;
1035 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1036 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1037 (unsigned char *) data, 2);
1039 /* save state of windows */
1040 wwin = scr->focused_window;
1041 while (wwin) {
1042 wWindowSaveState(wwin);
1043 wwin = wwin->prev;
1047 if (wPreferences.flags.noupdates)
1048 return;
1051 old_state = scr->session_state;
1052 scr->session_state = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
1054 PLSetStringCmpHook(NULL);
1056 /* save dock state to file */
1057 if (!wPreferences.flags.nodock) {
1058 wDockSaveState(scr);
1059 } else {
1060 if ((foo = PLGetDictionaryEntry(old_state, dDock))!=NULL) {
1061 PLInsertDictionaryEntry(scr->session_state, dDock, foo);
1064 if (!wPreferences.flags.noclip) {
1065 wClipSaveState(scr);
1066 } else {
1067 if ((foo = PLGetDictionaryEntry(old_state, dClip))!=NULL) {
1068 PLInsertDictionaryEntry(scr->session_state, dClip, foo);
1072 wWorkspaceSaveState(scr, old_state);
1074 if (wPreferences.save_session_on_exit) {
1075 wSessionSaveState(scr);
1076 } else {
1077 if ((foo = PLGetDictionaryEntry(old_state, dApplications))!=NULL) {
1078 PLInsertDictionaryEntry(scr->session_state, dApplications, foo);
1080 if ((foo = PLGetDictionaryEntry(old_state, dWorkspace))!=NULL) {
1081 PLInsertDictionaryEntry(scr->session_state, dWorkspace, foo);
1085 /* clean up */
1086 PLSetStringCmpHook(StringCompareHook);
1088 wMenuSaveState(scr);
1090 if (wScreenCount == 1)
1091 str = wdefaultspathfordomain("WMState");
1092 else {
1093 char buf[16];
1094 sprintf(buf, "WMState.%i", scr->screen);
1095 str = wdefaultspathfordomain(buf);
1097 path = PLMakeString(str);
1098 free(str);
1099 PLSetFilename(scr->session_state, path);
1100 if (!PLSave(scr->session_state, YES)) {
1101 wsyserror(_("could not save session state in %s"), PLGetString(path));
1103 PLRelease(path);
1104 PLRelease(old_state);
1110 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1112 int moved = 0;
1113 int tol_w, tol_h;
1115 if (width > 20)
1116 tol_w = width/2;
1117 else
1118 tol_w = 20;
1120 if (height > 20)
1121 tol_h = height/2;
1122 else
1123 tol_h = 20;
1125 if (*x+width < 10)
1126 *x = -tol_w, moved = 1;
1127 else if (*x >= scr->scr_width - 10)
1128 *x = scr->scr_width - tol_w - 1, moved = 1;
1130 if (*y < -height + 10)
1131 *y = -tol_h, moved = 1;
1132 else if (*y >= scr->scr_height - 10)
1133 *y = scr->scr_height - tol_h - 1, moved = 1;
1135 return moved;