fixed many bugs, removed linked list
[wmaker-crm.git] / src / screen.c
blob4e6d5dc3e285670d02c0e24bd804b3164c98aa36
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 #ifdef KEEP_XKB_LOCK_STATUS
92 extern int wXkbSupported;
93 #endif
95 extern WDDomain *WDWindowMaker;
98 /**** Local ****/
100 #define STIPPLE_WIDTH 2
101 #define STIPPLE_HEIGHT 2
102 static char STIPPLE_DATA[] = {0x02, 0x01};
104 static int CantManageScreen = 0;
106 static proplist_t dApplications = NULL;
107 static proplist_t dWorkspace;
108 static proplist_t dDock;
109 static proplist_t dClip;
112 static void
113 make_keys()
115 if (dApplications!=NULL)
116 return;
118 dApplications = PLMakeString("Applications");
119 dWorkspace = PLMakeString("Workspace");
120 dDock = PLMakeString("Dock");
121 dClip = PLMakeString("Clip");
126 *----------------------------------------------------------------------
127 * alreadyRunningError--
128 * X error handler used to catch errors when trying to do
129 * XSelectInput() on the root window. These errors probably mean that
130 * there already is some other window manager running.
132 * Returns:
133 * Nothing, unless something really evil happens...
135 * Side effects:
136 * CantManageScreen is set to 1;
137 *----------------------------------------------------------------------
139 static int
140 alreadyRunningError(Display *dpy, XErrorEvent *error)
142 CantManageScreen = 1;
143 return -1;
148 *----------------------------------------------------------------------
149 * allocButtonPixmaps--
150 * Allocate pixmaps used on window operation buttons (those in the
151 * titlebar). The pixmaps are linked to the program. If XPM is supported
152 * XPM pixmaps are used otherwise, equivalent bitmaps are used.
154 * Returns:
155 * Nothing
157 * Side effects:
158 * Allocates shared pixmaps for the screen. These pixmaps should
159 * not be freed by anybody.
160 *----------------------------------------------------------------------
162 static void
163 allocButtonPixmaps(WScreen *scr)
165 WPixmap *pix;
167 /* create predefined pixmaps */
168 pix = wPixmapCreateFromXPMData(scr, PRED_CLOSE_XPM);
169 if (pix)
170 pix->shared = 1;
171 scr->b_pixmaps[WBUT_CLOSE] = pix;
173 pix = wPixmapCreateFromXPMData(scr, PRED_BROKEN_CLOSE_XPM);
174 if (pix)
175 pix->shared = 1;
176 scr->b_pixmaps[WBUT_BROKENCLOSE] = pix;
178 pix = wPixmapCreateFromXPMData(scr, PRED_ICONIFY_XPM);
179 if (pix)
180 pix->shared = 1;
181 scr->b_pixmaps[WBUT_ICONIFY] = pix;
182 #ifdef XKB_BUTTON_HINT
183 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP1_XPM);
184 if (pix)
185 pix->shared = 1;
186 scr->b_pixmaps[WBUT_XKBGROUP1] = pix;
187 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP2_XPM);
188 if (pix)
189 pix->shared = 1;
190 scr->b_pixmaps[WBUT_XKBGROUP2] = pix;
191 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP3_XPM);
192 if (pix)
193 pix->shared = 1;
194 scr->b_pixmaps[WBUT_XKBGROUP3] = pix;
195 pix = wPixmapCreateFromXPMData(scr, PRED_XKBGROUP4_XPM);
196 if (pix)
197 pix->shared = 1;
198 scr->b_pixmaps[WBUT_XKBGROUP4] = pix;
199 #endif
202 pix = wPixmapCreateFromXPMData(scr, PRED_KILL_XPM);
203 if (pix)
204 pix->shared = 1;
205 scr->b_pixmaps[WBUT_KILL] = pix;
209 static void
210 draw_dot(WScreen *scr, Drawable d, int x, int y, GC gc)
212 XSetForeground(dpy, gc, scr->black_pixel);
213 XDrawLine(dpy, d, gc, x, y, x+1, y);
214 XDrawPoint(dpy, d, gc, x, y+1);
215 XSetForeground(dpy, gc, scr->white_pixel);
216 XDrawLine(dpy, d, gc, x+2, y, x+2, y+1);
217 XDrawPoint(dpy, d, gc, x+1, y+1);
221 static WPixmap*
222 make3Dots(WScreen *scr)
224 WPixmap *wpix;
225 GC gc2, gc;
226 XGCValues gcv;
227 Pixmap pix, mask;
229 gc = scr->copy_gc;
230 pix = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
231 wPreferences.icon_size, scr->w_depth);
232 XSetForeground(dpy, gc, scr->black_pixel);
233 XFillRectangle(dpy, pix, gc, 0, 0, wPreferences.icon_size,
234 wPreferences.icon_size);
235 XSetForeground(dpy, gc, scr->white_pixel);
236 draw_dot(scr, pix, 4, wPreferences.icon_size-6, gc);
237 draw_dot(scr, pix, 9, wPreferences.icon_size-6, gc);
238 draw_dot(scr, pix, 14, wPreferences.icon_size-6, gc);
240 mask = XCreatePixmap(dpy, scr->w_win, wPreferences.icon_size,
241 wPreferences.icon_size, 1);
242 gcv.foreground = 0;
243 gcv.graphics_exposures = False;
244 gc2 = XCreateGC(dpy, mask, GCForeground|GCGraphicsExposures, &gcv);
245 XFillRectangle(dpy, mask, gc2, 0, 0, wPreferences.icon_size,
246 wPreferences.icon_size);
247 XSetForeground(dpy, gc2, 1);
248 XFillRectangle(dpy, mask, gc2, 4, wPreferences.icon_size-6, 3, 2);
249 XFillRectangle(dpy, mask, gc2, 9, wPreferences.icon_size-6, 3, 2);
250 XFillRectangle(dpy, mask, gc2, 14, wPreferences.icon_size-6, 3, 2);
252 XFreeGC(dpy, gc2);
254 wpix = wPixmapCreate(scr, pix, mask);
255 wpix->shared = 1;
257 return wpix;
261 static void
262 allocGCs(WScreen *scr)
264 XGCValues gcv;
265 XColor color;
266 unsigned long mtextcolor;
267 int gcm;
269 scr->stipple_bitmap =
270 XCreateBitmapFromData(dpy, scr->w_win, STIPPLE_DATA, STIPPLE_WIDTH,
271 STIPPLE_HEIGHT);
273 gcv.stipple = scr->stipple_bitmap;
274 gcv.foreground = scr->white_pixel;
275 gcv.fill_style = FillStippled;
276 gcv.graphics_exposures = False;
277 gcm = GCForeground|GCStipple|GCFillStyle|GCGraphicsExposures;
278 scr->stipple_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
281 /* selected icon border GCs */
282 gcv.function = GXcopy;
283 gcv.foreground = scr->white_pixel;
284 gcv.background = scr->black_pixel;
285 gcv.line_width = 1;
286 gcv.line_style = LineDoubleDash;
287 gcv.fill_style = FillSolid;
288 gcv.dash_offset = 0;
289 gcv.dashes = 4;
290 gcv.graphics_exposures = False;
292 gcm = GCFunction | GCGraphicsExposures;
293 gcm |= GCForeground | GCBackground;
294 gcm |= GCLineWidth | GCLineStyle;
295 gcm |= GCFillStyle;
296 gcm |= GCDashOffset | GCDashList;
298 scr->icon_select_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
300 gcm = GCForeground|GCGraphicsExposures;
302 scr->menu_title_pixel[0] = scr->white_pixel;
303 gcv.foreground = scr->white_pixel;
304 gcv.graphics_exposures = False;
305 scr->menu_title_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
307 scr->mtext_pixel = scr->black_pixel;
308 mtextcolor = gcv.foreground = scr->black_pixel;
309 scr->menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
311 /* selected menu entry GC */
312 gcm = GCForeground|GCBackground|GCGraphicsExposures;
313 gcv.foreground = scr->white_pixel;
314 gcv.background = scr->white_pixel;
315 gcv.graphics_exposures = False;
316 scr->select_menu_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
318 /* disabled menu entry GC */
319 scr->dtext_pixel = scr->black_pixel;
320 gcm = GCForeground|GCBackground|GCStipple|GCGraphicsExposures;
321 gcv.stipple = scr->stipple_bitmap;
322 gcv.graphics_exposures = False;
323 scr->disabled_menu_entry_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
325 /* frame GC */
326 wGetColor(scr, DEF_FRAME_COLOR, &color);
327 gcv.function = GXxor;
328 /* this will raise the probability of the XORed color being different
329 * of the original color in PseudoColor when not all color cells are
330 * initialized */
331 if (DefaultVisual(dpy, scr->screen)->class==PseudoColor)
332 gcv.plane_mask = (1<<(scr->depth-1))|1;
333 else
334 gcv.plane_mask = AllPlanes;
335 gcv.foreground = color.pixel;
336 if (gcv.foreground == 0)
337 gcv.foreground = 1;
338 gcv.line_width = DEF_FRAME_THICKNESS;
339 gcv.subwindow_mode = IncludeInferiors;
340 gcv.graphics_exposures = False;
341 scr->frame_gc = XCreateGC(dpy, scr->root_win, GCForeground|GCGraphicsExposures
342 |GCFunction|GCSubwindowMode|GCLineWidth
343 |GCPlaneMask, &gcv);
345 /* line GC */
346 gcv.foreground = color.pixel;
348 if (gcv.foreground == 0)
349 /* XOR:ing with a zero is not going to be of much use, so
350 in that case, we somewhat arbitrarily xor with 17 instead. */
351 gcv.foreground = 17;
353 gcv.function = GXxor;
354 gcv.subwindow_mode = IncludeInferiors;
355 gcv.line_width = 1;
356 gcv.cap_style = CapRound;
357 gcv.graphics_exposures = False;
358 gcm = GCForeground|GCFunction|GCSubwindowMode|GCLineWidth|GCCapStyle
359 |GCGraphicsExposures;
360 scr->line_gc = XCreateGC(dpy, scr->root_win, gcm, &gcv);
362 scr->line_pixel = gcv.foreground;
364 /* copy GC */
365 gcv.foreground = scr->white_pixel;
366 gcv.background = scr->black_pixel;
367 gcv.graphics_exposures = False;
368 scr->copy_gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground
369 |GCGraphicsExposures, &gcv);
371 /* window title text GC */
372 gcv.graphics_exposures = False;
373 scr->window_title_gc = XCreateGC(dpy, scr->w_win,GCGraphicsExposures,&gcv);
375 /* icon title GC */
376 scr->icon_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
378 /* clip title GC */
379 scr->clip_title_gc = XCreateGC(dpy, scr->w_win, GCGraphicsExposures, &gcv);
381 /* move/size display GC */
382 gcv.graphics_exposures = False;
383 gcm = GCGraphicsExposures;
384 scr->info_text_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
386 /* misc drawing GC */
387 gcv.graphics_exposures = False;
388 gcm = GCGraphicsExposures;
389 scr->draw_gc = XCreateGC(dpy, scr->w_win, gcm, &gcv);
391 assert (scr->stipple_bitmap!=None);
394 /* mono GC */
395 scr->mono_gc = XCreateGC(dpy, scr->stipple_bitmap, gcm, &gcv);
400 static void
401 createPixmaps(WScreen *scr)
403 WPixmap *pix;
404 WMPixmap *wmpix;
405 RImage *image;
406 Pixmap p, m;
408 /* load pixmaps */
409 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_RADIO_INDICATOR_XBM_DATA,
410 (char*)MENU_RADIO_INDICATOR_XBM_DATA,
411 MENU_RADIO_INDICATOR_XBM_SIZE,
412 MENU_RADIO_INDICATOR_XBM_SIZE,
413 scr->black_pixel, scr->white_pixel);
414 if (pix!=NULL)
415 pix->shared = 1;
416 scr->menu_radio_indicator = pix;
419 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_CHECK_INDICATOR_XBM_DATA,
420 (char*)MENU_CHECK_INDICATOR_XBM_DATA,
421 MENU_CHECK_INDICATOR_XBM_SIZE,
422 MENU_CHECK_INDICATOR_XBM_SIZE,
423 scr->black_pixel, scr->white_pixel);
424 if (pix!=NULL)
425 pix->shared = 1;
426 scr->menu_check_indicator = pix;
428 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_MINI_INDICATOR_XBM_DATA,
429 (char*)MENU_MINI_INDICATOR_XBM_DATA,
430 MENU_MINI_INDICATOR_XBM_SIZE,
431 MENU_MINI_INDICATOR_XBM_SIZE,
432 scr->black_pixel, scr->white_pixel);
433 if (pix!=NULL)
434 pix->shared = 1;
435 scr->menu_mini_indicator = pix;
437 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_HIDE_INDICATOR_XBM_DATA,
438 (char*)MENU_HIDE_INDICATOR_XBM_DATA,
439 MENU_HIDE_INDICATOR_XBM_SIZE,
440 MENU_HIDE_INDICATOR_XBM_SIZE,
441 scr->black_pixel, scr->white_pixel);
442 if (pix!=NULL)
443 pix->shared = 1;
444 scr->menu_hide_indicator = pix;
446 pix = wPixmapCreateFromXBMData(scr, (char*)MENU_SHADE_INDICATOR_XBM_DATA,
447 (char*)MENU_SHADE_INDICATOR_XBM_DATA,
448 MENU_SHADE_INDICATOR_XBM_SIZE,
449 MENU_SHADE_INDICATOR_XBM_SIZE,
450 scr->black_pixel, scr->white_pixel);
451 if (pix!=NULL)
452 pix->shared = 1;
453 scr->menu_shade_indicator = pix;
456 image = wDefaultGetImage(scr, "Logo", "WMPanel");
458 if (!image) {
459 wwarning(_("could not load logo image for panels: %s"),
460 RMessageForError(RErrorCode));
461 } else {
462 if (!RConvertImageMask(scr->rcontext, image, &p, &m, 128)) {
463 wwarning(_("error making logo image for panel:%s"), RMessageForError(RErrorCode));
464 } else {
465 wmpix = WMCreatePixmapFromXPixmaps(scr->wmscreen, p, m,
466 image->width, image->height,
467 scr->depth);
468 WMSetApplicationIconImage(scr->wmscreen, wmpix);
469 WMReleasePixmap(wmpix);
471 RDestroyImage(image);
474 scr->dock_dots = make3Dots(scr);
476 /* titlebar button pixmaps */
477 allocButtonPixmaps(scr);
482 *----------------------------------------------------------------------
483 * createInternalWindows--
484 * Creates some windows used internally by the program. One to
485 * receive input focus when no other window can get it and another
486 * to display window geometry information during window resize/move.
488 * Returns:
489 * Nothing
491 * Side effects:
492 * Windows are created and some colors are allocated for the
493 * window background.
494 *----------------------------------------------------------------------
496 static void
497 createInternalWindows(WScreen *scr)
499 int vmask;
500 XSetWindowAttributes attribs;
502 /* window for displaying geometry information during resizes and moves */
503 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
504 attribs.border_pixel = scr->black_pixel;
505 attribs.save_under = True;
506 attribs.override_redirect = True;
507 attribs.cursor = wCursor[WCUR_DEFAULT];
508 attribs.background_pixmap = None;
509 if (scr->widget_texture)
510 attribs.background_pixel = scr->widget_texture->normal.pixel;
511 else
512 attribs.background_pixel = scr->light_pixel;
513 vmask |= CWColormap;
514 attribs.colormap = scr->w_colormap;
516 wGetGeometryWindowSize(scr, &scr->geometry_display_width,
517 &scr->geometry_display_height);
518 scr->geometry_display =
519 XCreateWindow(dpy, scr->root_win, 1, 1,
520 scr->geometry_display_width,
521 scr->geometry_display_height,
522 1, scr->w_depth, CopyFromParent, scr->w_visual,
523 vmask, &attribs);
525 /* InputOnly window to get the focus when no other window can get it */
526 vmask = CWEventMask|CWOverrideRedirect;
527 attribs.event_mask = KeyPressMask|FocusChangeMask;
528 attribs.override_redirect = True;
529 scr->no_focus_win=XCreateWindow(dpy,scr->root_win,-10, -10, 4, 4, 0, 0,
530 InputOnly,CopyFromParent, vmask, &attribs);
531 XSelectInput(dpy, scr->no_focus_win, KeyPressMask|KeyReleaseMask);
532 XMapWindow(dpy, scr->no_focus_win);
534 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
536 /* shadow window for dock buttons */
537 vmask = CWBorderPixel|CWBackPixmap|CWBackPixel|CWCursor|CWSaveUnder|CWOverrideRedirect;
538 attribs.border_pixel = scr->black_pixel;
539 attribs.save_under = True;
540 attribs.override_redirect = True;
541 attribs.background_pixmap = None;
542 attribs.background_pixel = scr->white_pixel;
543 vmask |= CWColormap;
544 attribs.colormap = scr->w_colormap;
545 scr->dock_shadow =
546 XCreateWindow(dpy, scr->root_win, 0, 0, wPreferences.icon_size,
547 wPreferences.icon_size, 0, scr->w_depth, CopyFromParent,
548 scr->w_visual, vmask, &attribs);
550 /* workspace name balloon for clip */
551 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
552 |CWBorderPixel;
553 attribs.save_under = True;
554 attribs.override_redirect = True;
555 attribs.colormap = scr->w_colormap;
556 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
557 attribs.border_pixel = 0; /* do not care */
558 scr->clip_balloon =
559 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
560 CopyFromParent, scr->w_visual, vmask, &attribs);
563 /* workspace name */
564 vmask = CWBackPixel|CWSaveUnder|CWOverrideRedirect|CWColormap
565 |CWBorderPixel;
566 attribs.save_under = True;
567 attribs.override_redirect = True;
568 attribs.colormap = scr->w_colormap;
569 attribs.background_pixel = scr->icon_back_texture->normal.pixel;
570 attribs.border_pixel = 0; /* do not care */
571 scr->workspace_name =
572 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, scr->w_depth,
573 CopyFromParent, scr->w_visual, vmask, &attribs);
576 /* for our window manager info notice board */
577 scr->info_window =
578 XCreateWindow(dpy, scr->root_win, 0, 0, 10, 10, 0, CopyFromParent,
579 CopyFromParent, CopyFromParent, CWOverrideRedirect,
580 &attribs);
583 * If the window is clicked without having ButtonPress selected, the
584 * resulting event will have event.xbutton.window == root.
586 XSelectInput(dpy, scr->clip_balloon, ButtonPressMask);
590 #if 0
591 static Bool
592 aquireManagerSelection(WScreen *scr)
594 char buffer[32];
595 XEvent ev;
596 Time timestamp;
598 sprintf(buffer, "WM_S%i", scr->screen);
599 scr->managerAtom = XInternAtom(dpy, buffer, False);
601 /* for race-conditions... */
602 XGrabServer(dpy);
604 /* if there is another manager running, don't try to replace it
605 * (for now, at least) */
606 if (XGetSelectionOwner(dpy, scr->managerAtom) != None) {
607 XUngrabServer(dpy);
608 return False;
611 /* become the manager for this screen */
613 scr->managerWindow = XCreateSimpleWindow(dpy, scr->root_win, 0, 0, 1, 1,
614 0, 0, 0);
616 XSelectInput(dpy, scr->managerWindow, PropertyChangeMask);
617 /* get a timestamp */
618 XChangeProperty(dpy, scr->managerWindow, scr->managerAtom,
619 XA_INTEGER, 32, PropModeAppend, NULL, 0);
620 while (1) {
621 XWindowEvent(dpy, scr->managerWindow, &ev);
622 if (ev.type == PropertyNotify) {
623 timestamp = ev.xproperty.time;
624 break;
627 XSelectInput(dpy, scr->managerWindow, NoEvents);
628 XDeleteProperty(dpy, scr->managerWindow, scr->managerAtom);
630 XSetSelectionOwner(dpy, scr->managerAtom, scr->managerWindow, CurrentTime);
632 XUngrabServer(dpy);
634 /* announce our arrival */
636 ev.xclient.type = ClientMessage;
637 ev.xclient.message_type = XInternAtom(dpy, "MANAGER", False);
638 ev.xclient.destination = scr->root_win;
639 ev.xclient.format = 32;
640 ev.xclient.data.l[0] = timestamp;
641 ev.xclient.data.l[1] = scr->managerAtom;
642 ev.xclient.data.l[2] = scr->managerWindow;
643 ev.xclient.data.l[3] = 0;
644 ev.xclient.data.l[4] = 0;
646 XSendEvent(dpy, scr->root_win, False, StructureNotify, &ev);
647 XSync(dpy, False);
649 return True;
651 #endif
654 *----------------------------------------------------------------------
655 * wScreenInit--
656 * Initializes the window manager for the given screen and
657 * allocates a WScreen descriptor for it. Many resources are allocated
658 * for the screen and the root window is setup appropriately.
660 * Returns:
661 * The WScreen descriptor for the screen.
663 * Side effects:
664 * Many resources are allocated and the IconSize property is
665 * set on the root window.
666 * The program can be aborted if some fatal error occurs.
668 * TODO: User specifiable visual.
669 *----------------------------------------------------------------------
671 WScreen*
672 wScreenInit(int screen_number)
674 WScreen *scr;
675 XIconSize icon_size[1];
676 RContextAttributes rattr;
677 extern int wVisualID;
678 long event_mask;
679 WMColor *color;
680 XErrorHandler oldHandler;
682 scr = wmalloc(sizeof(WScreen));
683 memset(scr, 0, sizeof(WScreen));
685 /* initialize globals */
686 scr->screen = screen_number;
687 scr->root_win = RootWindow(dpy, screen_number);
688 scr->depth = DefaultDepth(dpy, screen_number);
689 scr->colormap = DefaultColormap(dpy, screen_number);
691 scr->scr_width = WidthOfScreen(ScreenOfDisplay(dpy, screen_number));
692 scr->scr_height = HeightOfScreen(ScreenOfDisplay(dpy, screen_number));
694 scr->usableArea.x2 = scr->scr_width;
695 scr->usableArea.y2 = scr->scr_height;
696 scr->totalUsableArea.x2 = scr->scr_width;
697 scr->totalUsableArea.y2 = scr->scr_height;
699 #if 0
700 if (!aquireManagerSelection(scr)) {
701 free(scr);
703 return NULL;
705 #endif
706 CantManageScreen = 0;
707 oldHandler = XSetErrorHandler((XErrorHandler)alreadyRunningError);
709 event_mask = EVENT_MASK;
711 if (wPreferences.disable_root_mouse) {
712 event_mask &= ~(ButtonPressMask|ButtonReleaseMask);
715 XSelectInput(dpy, scr->root_win, event_mask);
717 #ifdef KEEP_XKB_LOCK_STATUS
718 if (wXkbSupported) {
719 XkbSelectEvents(dpy,XkbUseCoreKbd,
720 XkbStateNotifyMask,
721 XkbStateNotifyMask);
723 #endif /* KEEP_XKB_LOCK_STATUS */
725 XSync(dpy, False);
726 XSetErrorHandler(oldHandler);
728 if (CantManageScreen) {
729 free(scr);
730 return NULL;
733 XDefineCursor(dpy, scr->root_win, wCursor[WCUR_DEFAULT]);
735 /* screen descriptor for raster graphic library */
736 rattr.flags = RC_RenderMode | RC_ColorsPerChannel;
737 rattr.render_mode = wPreferences.no_dithering
738 ? RBestMatchRendering
739 : RDitheredRendering;
741 rattr.colors_per_channel = wPreferences.cmap_size;
742 if (rattr.colors_per_channel<2)
743 rattr.colors_per_channel = 2;
745 if (wVisualID>=0) {
746 rattr.flags |= RC_VisualID;
747 rattr.visualid = wVisualID;
749 scr->rcontext = RCreateContext(dpy, screen_number, &rattr);
750 if (!scr->rcontext) {
751 wwarning(_("could not initialize graphics library context: %s"),
752 RMessageForError(RErrorCode));
753 wAbort(False);
754 } else {
755 char **formats;
756 int i = 0;
758 formats = RSupportedFileFormats();
759 if (formats) {
760 for (i=0; formats[i]!=NULL; i++) {
761 if (strcmp(formats[i], "TIFF")==0) {
762 scr->flags.supports_tiff = 1;
763 break;
769 scr->w_win = scr->rcontext->drawable;
770 scr->w_visual = scr->rcontext->visual;
771 scr->w_depth = scr->rcontext->depth;
772 scr->w_colormap = scr->rcontext->cmap;
774 scr->black_pixel = scr->rcontext->black;
775 scr->white_pixel = scr->rcontext->white;
777 /* create screen descriptor for WINGs */
778 scr->wmscreen = WMCreateScreenWithRContext(dpy, screen_number,
779 scr->rcontext);
781 if (!scr->wmscreen) {
782 wfatal(_("could not do initialization of WINGs widget set"));
784 return NULL;
787 color = WMGrayColor(scr->wmscreen);
788 scr->light_pixel = WMColorPixel(color);
789 WMReleaseColor(color);
791 color = WMDarkGrayColor(scr->wmscreen);
792 scr->dark_pixel = WMColorPixel(color);
793 WMReleaseColor(color);
796 XColor xcol;
797 /* frame boder color */
798 wGetColor(scr, FRAME_BORDER_COLOR, &xcol);
799 scr->frame_border_pixel = xcol.pixel;
802 /* create GCs with default values */
803 allocGCs(scr);
805 /* read defaults for this screen */
806 wReadDefaults(scr, WDWindowMaker->dictionary);
808 createInternalWindows(scr);
810 #ifdef KWM_HINTS
811 wKWMInitStuff(scr);
812 #endif
814 #ifdef GNOME_STUFF
815 wGNOMEInitStuff(scr);
816 #endif
818 #ifdef OLWM_HINTS
819 wOLWMInitStuff(scr);
820 #endif
822 /* create initial workspace */
823 wWorkspaceNew(scr);
825 /* create shared pixmaps */
826 createPixmaps(scr);
828 /* set icon sizes we can accept from clients */
829 icon_size[0].min_width = 8;
830 icon_size[0].min_height = 8;
831 icon_size[0].max_width = wPreferences.icon_size-4;
832 icon_size[0].max_height = wPreferences.icon_size-4;
833 icon_size[0].width_inc = 1;
834 icon_size[0].height_inc = 1;
835 XSetIconSizes(dpy, scr->root_win, icon_size, 1);
837 /* setup WindowMaker protocols property in the root window*/
838 PropSetWMakerProtocols(scr->root_win);
840 /* setup our noticeboard */
841 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_NOTICEBOARD,
842 XA_WINDOW, 32, PropModeReplace,
843 (unsigned char*)&scr->info_window, 1);
844 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_NOTICEBOARD,
845 XA_WINDOW, 32, PropModeReplace,
846 (unsigned char*)&scr->info_window, 1);
849 #ifdef BALLOON_TEXT
850 /* initialize balloon text stuff */
851 wBalloonInitialize(scr);
852 #endif
854 wScreenUpdateUsableArea(scr);
857 return scr;
861 void
862 wScreenUpdateUsableArea(WScreen *scr)
864 #ifdef GNOME_STUFF
865 WReservedArea *area;
866 #endif
868 scr->totalUsableArea = scr->usableArea;
871 if (scr->dock && (!scr->dock->lowered
872 || wPreferences.no_window_over_dock)) {
874 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
876 if (scr->dock->on_right_side) {
877 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2,
878 scr->scr_width - offset);
879 } else {
880 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, offset);
884 if (wPreferences.no_window_over_icons) {
885 if (wPreferences.icon_yard & IY_VERT) {
887 if (!(wPreferences.icon_yard & IY_RIGHT)) {
888 scr->totalUsableArea.x1 += wPreferences.icon_size;
889 } else {
890 scr->totalUsableArea.x2 -= wPreferences.icon_size;
892 } else {
894 if (wPreferences.icon_yard & IY_TOP) {
895 scr->totalUsableArea.y1 += wPreferences.icon_size;
896 } else {
897 scr->totalUsableArea.y2 -= wPreferences.icon_size;
902 #ifdef KWM_HINTS
904 WArea area;
906 if (wKWMGetUsableArea(scr, &area)) {
907 scr->totalUsableArea.x1 = WMAX(scr->totalUsableArea.x1, area.x1);
908 scr->totalUsableArea.y1 = WMAX(scr->totalUsableArea.y1, area.y1);
909 scr->totalUsableArea.x2 = WMIN(scr->totalUsableArea.x2, area.x2);
910 scr->totalUsableArea.y2 = WMIN(scr->totalUsableArea.y2, area.y2);
913 #endif
915 #ifdef GNOME_STUFF
916 area = scr->reservedAreas;
918 while (area) {
919 int th, bh;
920 int lw, rw;
921 int w, h;
923 w = area->area.x2 - area->area.x1;
924 h = area->area.y2 - area->area.y1;
926 th = area->area.y1;
927 bh = scr->scr_height - area->area.y2;
928 lw = area->area.x1;
929 rw = scr->scr_width - area->area.x2;
931 if (WMIN(th, bh) < WMIN(lw, rw)) {
932 /* horizontal */
933 if (th < bh) {
934 /* on top */
935 if (scr->totalUsableArea.y1 < area->area.y2)
936 scr->totalUsableArea.y1 = area->area.y2;
937 } else {
938 /* on bottom */
939 if (scr->totalUsableArea.y2 > area->area.y1)
940 scr->totalUsableArea.y2 = area->area.y1;
942 } else {
943 /* vertical */
944 if (lw < rw) {
945 /* on left */
946 if (scr->totalUsableArea.x1 < area->area.x2)
947 scr->totalUsableArea.x1 = area->area.x2;
948 } else {
949 /* on right */
950 if (scr->totalUsableArea.x2 > area->area.x1)
951 scr->totalUsableArea.x2 = area->area.x1;
955 area = area->next;
957 #endif /* GNOME_STUFF */
959 if (scr->totalUsableArea.x2 - scr->totalUsableArea.x1 < scr->scr_width/2) {
960 scr->totalUsableArea.x2 = scr->usableArea.x2;
961 scr->totalUsableArea.x1 = scr->usableArea.x1;
963 if (scr->totalUsableArea.y2 - scr->totalUsableArea.y1 < scr->scr_height/2) {
964 scr->totalUsableArea.y2 = scr->usableArea.y2;
965 scr->totalUsableArea.y1 = scr->usableArea.y1;
968 #ifdef not_used
969 #ifdef KWM_HINTS
971 int i;
973 for (i = 0; i < scr->workspace_count; i++) {
974 wKWMSetUsableAreaHint(scr, i);
977 #endif
978 #endif
982 void
983 wScreenRestoreState(WScreen *scr)
985 proplist_t state;
986 char *path;
989 #ifndef LITE
990 OpenRootMenu(scr, -10000, -10000, False);
991 wMenuUnmap(scr->root_menu);
992 #endif
994 make_keys();
996 if (wScreenCount == 1)
997 path = wdefaultspathfordomain("WMState");
998 else {
999 char buf[16];
1000 sprintf(buf, "WMState.%i", scr->screen);
1001 path = wdefaultspathfordomain(buf);
1003 scr->session_state = PLGetProplistWithPath(path);
1004 free(path);
1005 if (!scr->session_state && wScreenCount>1) {
1006 char buf[16];
1007 sprintf(buf, "WMState.%i", scr->screen);
1008 path = wdefaultspathfordomain(buf);
1009 scr->session_state = PLGetProplistWithPath(path);
1010 free(path);
1013 if (!wPreferences.flags.noclip) {
1014 state = PLGetDictionaryEntry(scr->session_state, dClip);
1015 scr->clip_icon = wClipRestoreState(scr, state);
1018 wWorkspaceRestoreState(scr);
1020 if (!wPreferences.flags.nodock) {
1021 state = PLGetDictionaryEntry(scr->session_state, dDock);
1022 scr->dock = wDockRestoreState(scr, state, WM_DOCK);
1025 wScreenUpdateUsableArea(scr);
1029 void
1030 wScreenSaveState(WScreen *scr)
1032 WWorkspaceState wstate;
1033 WWindow *wwin;
1034 char *str;
1035 proplist_t path, old_state, foo;
1036 CARD32 data[2];
1039 make_keys();
1042 * Save current workspace, so can go back to it upon restart.
1044 wstate.workspace = scr->current_workspace;
1046 data[0] = wstate.flags;
1047 data[1] = wstate.workspace;
1049 XChangeProperty(dpy, scr->root_win, _XA_WINDOWMAKER_STATE,
1050 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1051 (unsigned char *) data, 2);
1053 /* save state of windows */
1054 wwin = scr->focused_window;
1055 while (wwin) {
1056 wWindowSaveState(wwin);
1057 wwin = wwin->prev;
1061 if (wPreferences.flags.noupdates)
1062 return;
1065 old_state = scr->session_state;
1066 scr->session_state = PLMakeDictionaryFromEntries(NULL, NULL, NULL);
1068 PLSetStringCmpHook(NULL);
1070 /* save dock state to file */
1071 if (!wPreferences.flags.nodock) {
1072 wDockSaveState(scr, old_state);
1073 } else {
1074 if ((foo = PLGetDictionaryEntry(old_state, dDock))!=NULL) {
1075 PLInsertDictionaryEntry(scr->session_state, dDock, foo);
1078 if (!wPreferences.flags.noclip) {
1079 wClipSaveState(scr);
1080 } else {
1081 if ((foo = PLGetDictionaryEntry(old_state, dClip))!=NULL) {
1082 PLInsertDictionaryEntry(scr->session_state, dClip, foo);
1086 wWorkspaceSaveState(scr, old_state);
1088 if (wPreferences.save_session_on_exit) {
1089 wSessionSaveState(scr);
1090 } else {
1091 if ((foo = PLGetDictionaryEntry(old_state, dApplications))!=NULL) {
1092 PLInsertDictionaryEntry(scr->session_state, dApplications, foo);
1094 if ((foo = PLGetDictionaryEntry(old_state, dWorkspace))!=NULL) {
1095 PLInsertDictionaryEntry(scr->session_state, dWorkspace, foo);
1099 /* clean up */
1100 PLSetStringCmpHook(StringCompareHook);
1102 wMenuSaveState(scr);
1104 if (wScreenCount == 1)
1105 str = wdefaultspathfordomain("WMState");
1106 else {
1107 char buf[16];
1108 sprintf(buf, "WMState.%i", scr->screen);
1109 str = wdefaultspathfordomain(buf);
1111 path = PLMakeString(str);
1112 free(str);
1113 PLSetFilename(scr->session_state, path);
1114 if (!PLSave(scr->session_state, YES)) {
1115 wsyserror(_("could not save session state in %s"), PLGetString(path));
1117 PLRelease(path);
1118 PLRelease(old_state);
1124 wScreenBringInside(WScreen *scr, int *x, int *y, int width, int height)
1126 int moved = 0;
1127 int tol_w, tol_h;
1129 if (width > 20)
1130 tol_w = width/2;
1131 else
1132 tol_w = 20;
1134 if (height > 20)
1135 tol_h = height/2;
1136 else
1137 tol_h = 20;
1139 if (*x+width < 10)
1140 *x = -tol_w, moved = 1;
1141 else if (*x >= scr->scr_width - 10)
1142 *x = scr->scr_width - tol_w - 1, moved = 1;
1144 if (*y < -height + 10)
1145 *y = -tol_h, moved = 1;
1146 else if (*y >= scr->scr_height - 10)
1147 *y = scr->scr_height - tol_h - 1, moved = 1;
1149 return moved;