]> icculus.org git repositories - mikachu/openbox.git/blob - src/BaseDisplay.cc
removed all 'using namespace std;' calls.
[mikachu/openbox.git] / src / BaseDisplay.cc
1 // BaseDisplay.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #define   _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 #  include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #include <X11/Xlib.h>
34 #include <X11/Xatom.h>
35 #include <X11/Xutil.h>
36 #include <X11/cursorfont.h>
37 #include <X11/keysym.h>
38
39 #ifdef    SHAPE
40 #  include <X11/extensions/shape.h>
41 #endif // SHAPE
42
43 #ifdef    HAVE_FCNTL_H
44 #  include <fcntl.h>
45 #endif // HAVE_FCNTL_H
46
47 #ifdef    HAVE_STDIO_H
48 #  include <stdio.h>
49 #endif // HAVE_STDIO_H
50
51 #ifdef    HAVE_STDLIB_H
52 #  include <stdlib.h>
53 #endif // HAVE_STDLIB_H
54
55 #ifdef    HAVE_STRING_H
56 #  include <string.h>
57 #endif // HAVE_STRING_H
58
59 #ifdef    HAVE_UNISTD_H
60 #  include <sys/types.h>
61 #  include <unistd.h>
62 #endif // HAVE_UNISTD_H
63
64 #ifdef    HAVE_SYS_SELECT_H
65 #  include <sys/select.h>
66 #endif // HAVE_SYS_SELECT_H
67
68 #ifdef    HAVE_SIGNAL_H
69 #  include <signal.h>
70 #endif // HAVE_SIGNAL_H
71
72 #ifndef   SA_NODEFER
73 #  ifdef   SA_INTERRUPT
74 #    define SA_NODEFER SA_INTERRUPT
75 #  else // !SA_INTERRUPT
76 #    define SA_NODEFER (0)
77 #  endif // SA_INTERRUPT
78 #endif // SA_NODEFER
79
80 #ifdef    HAVE_SYS_WAIT_H
81 #  include <sys/types.h>
82 #  include <sys/wait.h>
83 #endif // HAVE_SYS_WAIT_H
84
85 #if defined(HAVE_PROCESS_H) && defined(__EMX__)
86 #  include <process.h>
87 #endif //   HAVE_PROCESS_H             __EMX__
88
89 #include "i18n.h"
90 #include "BaseDisplay.h"
91 #include "LinkedList.h"
92 #include "Timer.h"
93
94 // X error handler to handle any and all X errors while the application is
95 // running
96 static Bool internal_error = False;
97 static Window last_bad_window = None;
98
99 BaseDisplay *base_display;
100
101 static int handleXErrors(Display *d, XErrorEvent *e) {
102 #ifdef    DEBUG
103   char errtxt[128];
104
105   XGetErrorText(d, e->error_code, errtxt, 128);
106   fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayXError,
107                      "%s:  X error: %s(%d) opcodes %d/%d\n  resource 0x%lx\n"),
108           base_display->getApplicationName(), errtxt, e->error_code,
109           e->request_code, e->minor_code, e->resourceid);
110 #endif // DEBUG
111
112   if (e->error_code == BadWindow) last_bad_window = e->resourceid;
113   if (internal_error) abort();
114
115   return(False);
116 }
117
118
119 // signal handler to allow for proper and gentle shutdown
120
121 #ifndef   HAVE_SIGACTION
122 static RETSIGTYPE signalhandler(int sig) {
123 #else //  HAVE_SIGACTION
124 static void signalhandler(int sig) {
125 #endif // HAVE_SIGACTION
126
127   static int re_enter = 0;
128
129   switch (sig) {
130   case SIGCHLD:
131     int status;
132     waitpid(-1, &status, WNOHANG | WUNTRACED);
133
134 #ifndef   HAVE_SIGACTION
135     // assume broken, braindead sysv signal semantics
136     signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
137 #endif // HAVE_SIGACTION
138
139     break;
140
141   default:
142     if (base_display->handleSignal(sig)) {
143
144 #ifndef   HAVE_SIGACTION
145       // assume broken, braindead sysv signal semantics
146       signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
147 #endif // HAVE_SIGACTION
148
149       return;
150     }
151
152     fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplaySignalCaught,
153                                      "%s:  signal %d caught\n"),
154             base_display->getApplicationName(), sig);
155
156     if (! base_display->isStartup() && ! re_enter) {
157       internal_error = True;
158
159       re_enter = 1;
160       fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayShuttingDown,
161                                        "shutting down\n"));
162       base_display->shutdown();
163     }
164
165     if (sig != SIGTERM && sig != SIGINT) {
166       fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayAborting,
167                                        "aborting... dumping core\n"));
168       abort();
169     }
170
171     exit(0);
172
173     break;
174   }
175 }
176
177
178 // convenience functions
179 #ifndef    __EMX__
180 void bexec(const char *command, char* displaystring) {
181   if (! fork()) {
182     setsid();
183     putenv(displaystring);
184     execl("/bin/sh", "/bin/sh", "-c", command, NULL);
185     exit(0);
186   }
187 }
188 #endif // !__EMX__
189
190 char *bstrdup(const char *s) {
191   const int l = strlen(s) + 1;
192   char *n = new char[l];
193   strncpy(n, s, l);
194   return n;
195 }
196
197 BaseDisplay::BaseDisplay(const char *app_name, char *dpy_name) {
198   application_name = bstrdup(app_name);
199
200   _startup = True;
201   _shutdown = False;
202   server_grabs = 0;
203   last_bad_window = None;
204
205   ::base_display = this;
206
207 #ifdef    HAVE_SIGACTION
208   struct sigaction action;
209
210   action.sa_handler = signalhandler;
211   action.sa_mask = sigset_t();
212   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
213
214   sigaction(SIGPIPE, &action, NULL);
215   sigaction(SIGSEGV, &action, NULL);
216   sigaction(SIGFPE, &action, NULL);
217   sigaction(SIGTERM, &action, NULL);
218   sigaction(SIGINT, &action, NULL);
219   sigaction(SIGCHLD, &action, NULL);
220   sigaction(SIGHUP, &action, NULL);
221   sigaction(SIGUSR1, &action, NULL);
222   sigaction(SIGUSR2, &action, NULL);
223 #else // !HAVE_SIGACTION
224   signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
225   signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
226   signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
227   signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
228   signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
229   signal(SIGUSR1, (RETSIGTYPE (*)(int)) signalhandler);
230   signal(SIGUSR2, (RETSIGTYPE (*)(int)) signalhandler);
231   signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
232   signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
233 #endif // HAVE_SIGACTION
234
235   if (! (display = XOpenDisplay(dpy_name))) {
236     fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayXConnectFail,
237                "BaseDisplay::BaseDisplay: connection to X server failed.\n"));
238     ::exit(2);
239   } else if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
240     fprintf(stderr,
241             i18n->getMessage(BaseDisplaySet, BaseDisplayCloseOnExecFail,
242                "BaseDisplay::BaseDisplay: couldn't mark display connection "
243                "as close-on-exec\n"));
244     ::exit(2);
245   }
246
247   number_of_screens = ScreenCount(display);
248   display_name = XDisplayName(dpy_name);
249
250 #ifdef    SHAPE
251   shape.extensions = XShapeQueryExtension(display, &shape.event_basep,
252                                           &shape.error_basep);
253 #else // !SHAPE
254   shape.extensions = False;
255 #endif // SHAPE
256
257   xa_wm_colormap_windows =
258     XInternAtom(display, "WM_COLORMAP_WINDOWS", False);
259   xa_wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False);
260   xa_wm_state = XInternAtom(display, "WM_STATE", False);
261   xa_wm_change_state = XInternAtom(display, "WM_CHANGE_STATE", False);
262   xa_wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
263   xa_wm_take_focus = XInternAtom(display, "WM_TAKE_FOCUS", False);
264   motif_wm_hints = XInternAtom(display, "_MOTIF_WM_HINTS", False);
265
266   openbox_hints = XInternAtom(display, "_BLACKBOX_HINTS", False);
267   openbox_attributes = XInternAtom(display, "_BLACKBOX_ATTRIBUTES", False);
268   openbox_change_attributes =
269     XInternAtom(display, "_BLACKBOX_CHANGE_ATTRIBUTES", False);
270
271   openbox_structure_messages =
272     XInternAtom(display, "_BLACKBOX_STRUCTURE_MESSAGES", False);
273   openbox_notify_startup =
274     XInternAtom(display, "_BLACKBOX_NOTIFY_STARTUP", False);
275   openbox_notify_window_add =
276     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_ADD", False);
277   openbox_notify_window_del =
278     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_DEL", False);
279   openbox_notify_current_workspace =
280     XInternAtom(display, "_BLACKBOX_NOTIFY_CURRENT_WORKSPACE", False);
281   openbox_notify_workspace_count =
282     XInternAtom(display, "_BLACKBOX_NOTIFY_WORKSPACE_COUNT", False);
283   openbox_notify_window_focus =
284     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_FOCUS", False);
285   openbox_notify_window_raise =
286     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_RAISE", False);
287   openbox_notify_window_lower =
288     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_LOWER", False);
289
290   openbox_change_workspace =
291     XInternAtom(display, "_BLACKBOX_CHANGE_WORKSPACE", False);
292   openbox_change_window_focus =
293     XInternAtom(display, "_BLACKBOX_CHANGE_WINDOW_FOCUS", False);
294   openbox_cycle_window_focus =
295     XInternAtom(display, "_BLACKBOX_CYCLE_WINDOW_FOCUS", False);
296
297 #ifdef    NEWWMSPEC
298
299   net_supported = XInternAtom(display, "_NET_SUPPORTED", False);
300   net_client_list = XInternAtom(display, "_NET_CLIENT_LIST", False);
301   net_client_list_stacking = XInternAtom(display, "_NET_CLIENT_LIST_STACKING", False);
302   net_number_of_desktops = XInternAtom(display, "_NET_NUMBER_OF_DESKTOPS", False);
303   net_desktop_geometry = XInternAtom(display, "_NET_DESKTOP_GEOMETRY", False);
304   net_desktop_viewport = XInternAtom(display, "_NET_DESKTOP_VIEWPORT", False);
305   net_current_desktop = XInternAtom(display, "_NET_CURRENT_DESKTOP", False);
306   net_desktop_names = XInternAtom(display, "_NET_DESKTOP_NAMES", False);
307   net_active_window = XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
308   net_workarea = XInternAtom(display, "_NET_WORKAREA", False);
309   net_supporting_wm_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
310   net_virtual_roots = XInternAtom(display, "_NET_VIRTUAL_ROOTS", False);
311
312   net_close_window = XInternAtom(display, "_NET_CLOSE_WINDOW", False);
313   net_wm_moveresize = XInternAtom(display, "_NET_WM_MOVERESIZE", False);
314
315   net_properties = XInternAtom(display, "_NET_PROPERTIES", False);
316   net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
317   net_wm_desktop = XInternAtom(display, "_NET_WM_DESKTOP", False);
318   net_wm_window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
319   net_wm_state = XInternAtom(display, "_NET_WM_STATE", False);
320   net_wm_strut = XInternAtom(display, "_NET_WM_STRUT", False);
321   net_wm_icon_geometry = XInternAtom(display, "_NET_WM_ICON_GEOMETRY", False);
322   net_wm_icon = XInternAtom(display, "_NET_WM_ICON", False);
323   net_wm_pid = XInternAtom(display, "_NET_WM_PID", False);
324   net_wm_handled_icons = XInternAtom(display, "_NET_WM_HANDLED_ICONS", False);
325
326   net_wm_ping = XInternAtom(display, "_NET_WM_PING", False);
327
328 #endif // NEWWMSPEC
329
330   cursor.session = XCreateFontCursor(display, XC_left_ptr);
331   cursor.move = XCreateFontCursor(display, XC_fleur);
332   cursor.ll_angle = XCreateFontCursor(display, XC_ll_angle);
333   cursor.lr_angle = XCreateFontCursor(display, XC_lr_angle);
334   cursor.ul_angle = XCreateFontCursor(display, XC_ul_angle);
335   cursor.ur_angle = XCreateFontCursor(display, XC_ur_angle);
336
337   XSetErrorHandler((XErrorHandler) handleXErrors);
338
339   timerList = new LinkedList<BTimer>;
340
341   screenInfoList = new LinkedList<ScreenInfo>;
342   for (int i = 0; i < number_of_screens; i++) {
343     ScreenInfo *screeninfo = new ScreenInfo(*this, i);
344     screenInfoList->insert(screeninfo);
345   }
346
347 #ifndef   NOCLOBBER
348   NumLockMask = ScrollLockMask = 0;
349
350   const XModifierKeymap* const modmap = XGetModifierMapping(display);
351   if (modmap && modmap->max_keypermod > 0) {
352     const int mask_table[] = {
353       ShiftMask, LockMask, ControlMask, Mod1Mask,
354       Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
355     };
356     const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
357       modmap->max_keypermod;
358     // get the values of the keyboard lock modifiers
359     // Note: Caps lock is not retrieved the same way as Scroll and Num lock
360     // since it doesn't need to be.
361     const KeyCode num_lock_code = XKeysymToKeycode(display, XK_Num_Lock);
362     const KeyCode scroll_lock_code = XKeysymToKeycode(display, XK_Scroll_Lock);
363     
364     for (size_t cnt = 0; cnt < size; ++cnt) {
365       if (! modmap->modifiermap[cnt]) continue;
366
367       if (num_lock_code == modmap->modifiermap[cnt])
368         NumLockMask = mask_table[cnt / modmap->max_keypermod];
369       if (scroll_lock_code == modmap->modifiermap[cnt])
370         ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
371     }
372   }
373
374   MaskList[0] = 0;
375   MaskList[1] = LockMask;
376   MaskList[2] = NumLockMask;
377   MaskList[3] = ScrollLockMask;
378   MaskList[4] = LockMask | NumLockMask;
379   MaskList[5] = NumLockMask  | ScrollLockMask;
380   MaskList[6] = LockMask | ScrollLockMask;
381   MaskList[7] = LockMask | NumLockMask | ScrollLockMask;
382   MaskListLength = sizeof(MaskList) / sizeof(MaskList[0]);
383   
384   if (modmap) XFreeModifiermap(const_cast<XModifierKeymap*>(modmap));
385 #else
386   NumLockMask = Mod2Mask;
387   ScrollLockMask = Mod5Mask;
388 #endif // NOCLOBBER
389 }
390
391
392 BaseDisplay::~BaseDisplay(void) {
393   while (screenInfoList->count()) {
394     ScreenInfo *si = screenInfoList->first();
395
396     screenInfoList->remove(si);
397     delete si;
398   }
399
400   delete screenInfoList;
401
402   // we don't create the BTimers, we don't delete them
403   while (timerList->count())
404     timerList->remove(0);
405
406   delete timerList;
407   
408   if (application_name != NULL)
409     delete [] application_name;
410
411   XCloseDisplay(display);
412 }
413
414
415 void BaseDisplay::eventLoop(void) {
416   run();
417
418   int xfd = ConnectionNumber(display);
419
420   while ((! _shutdown) && (! internal_error)) {
421     if (XPending(display)) {
422       XEvent e;
423       XNextEvent(display, &e);
424
425       if (last_bad_window != None && e.xany.window == last_bad_window) {
426 #ifdef    DEBUG
427       fprintf(stderr, i18n->getMessage(BaseDisplaySet,
428                                        BaseDisplayBadWindowRemove,
429                          "BaseDisplay::eventLoop(): removing bad window "
430                          "from event queue\n"));
431 #endif // DEBUG
432       } else {
433         last_bad_window = None;
434         process_event(&e);
435       }
436     } else {
437       fd_set rfds;
438       timeval now, tm, *timeout = (timeval *) 0;
439
440       FD_ZERO(&rfds);
441       FD_SET(xfd, &rfds);
442
443       if (timerList->count()) {
444         gettimeofday(&now, 0);
445
446         tm.tv_sec = tm.tv_usec = 0l;
447
448         BTimer *timer = timerList->first();
449
450         tm.tv_sec = timer->getStartTime().tv_sec +
451           timer->getTimeout().tv_sec - now.tv_sec;
452         tm.tv_usec = timer->getStartTime().tv_usec +
453           timer->getTimeout().tv_usec - now.tv_usec;
454
455         while (tm.tv_usec >= 1000000) {
456           tm.tv_sec++;
457           tm.tv_usec -= 1000000;
458         }
459
460         while (tm.tv_usec < 0) {
461           if (tm.tv_sec > 0) {
462             tm.tv_sec--;
463             tm.tv_usec += 1000000;
464           } else {
465             tm.tv_usec = 0;
466             break;
467           }
468         }
469
470         timeout = &tm;
471       }
472
473       select(xfd + 1, &rfds, 0, 0, timeout);
474
475       // check for timer timeout
476       gettimeofday(&now, 0);
477
478       LinkedListIterator<BTimer> it(timerList);
479       for(BTimer *timer = it.current(); timer; it++, timer = it.current()) {
480         tm.tv_sec = timer->getStartTime().tv_sec +
481           timer->getTimeout().tv_sec;
482         tm.tv_usec = timer->getStartTime().tv_usec +
483           timer->getTimeout().tv_usec;
484
485         if ((now.tv_sec < tm.tv_sec) ||
486             (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))
487           break;
488
489         timer->fireTimeout();
490
491         // restart the current timer so that the start time is updated
492         if (! timer->doOnce()) timer->start();
493         else timer->stop();
494       }
495     }
496   }
497 }
498
499
500 const Bool BaseDisplay::validateWindow(Window window) {
501   XEvent event;
502   if (XCheckTypedWindowEvent(display, window, DestroyNotify, &event)) {
503     XPutBackEvent(display, &event);
504
505     return False;
506   }
507
508   return True;
509 }
510
511
512 void BaseDisplay::grab(void) {
513   if (! server_grabs++)
514     XGrabServer(display);
515 }
516
517
518 void BaseDisplay::ungrab(void) {
519   if (! --server_grabs)
520     XUngrabServer(display);
521
522   if (server_grabs < 0) server_grabs = 0;
523 }
524
525
526 void BaseDisplay::addTimer(BTimer *timer) {
527   if (! timer) return;
528
529   LinkedListIterator<BTimer> it(timerList);
530   int index = 0;
531   for (BTimer *tmp = it.current(); tmp; it++, index++, tmp = it.current())
532     if ((tmp->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
533         ((tmp->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
534          (tmp->getTimeout().tv_usec >= timer->getTimeout().tv_usec)))
535       break;
536
537   timerList->insert(timer, index);
538 }
539
540
541 void BaseDisplay::removeTimer(BTimer *timer) {
542   timerList->remove(timer);
543 }
544
545
546 /*
547  * Grabs a button, but also grabs the button in every possible combination with
548  * the keyboard lock keys, so that they do not cancel out the event.
549  */
550 void BaseDisplay::grabButton(unsigned int button, unsigned int modifiers,
551                              Window grab_window, Bool owner_events,
552                              unsigned int event_mask, int pointer_mode,
553                              int keybaord_mode, Window confine_to,
554                              Cursor cursor) const
555 {
556 #ifndef   NOCLOBBER
557   for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
558     XGrabButton(display, button, modifiers | MaskList[cnt], grab_window,
559                 owner_events, event_mask, pointer_mode, keybaord_mode,
560                 confine_to, cursor);
561 #else  // NOCLOBBER
562     XGrabButton(display, button, modifiers, grab_window,
563                 owner_events, event_mask, pointer_mode, keybaord_mode,
564                 confine_to, cursor);
565 #endif // NOCLOBBER
566 }
567
568 /*
569  * Releases the grab on a button, and ungrabs all possible combinations of the
570  * keyboard lock keys.
571  */
572 void BaseDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
573                                Window grab_window) const {
574 #ifndef   NOCLOBBER
575   for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
576     XUngrabButton(display, button, modifiers | MaskList[cnt], grab_window);
577 #else  // NOCLOBBER
578     XUngrabButton(display, button, modifiers, grab_window);
579 #endif // NOCLOBBER
580 }
581
582
583 ScreenInfo::ScreenInfo(BaseDisplay &d, int num) : basedisplay(d),
584   screen_number(num)
585 {
586
587   root_window = RootWindow(basedisplay.getXDisplay(), screen_number);
588   depth = DefaultDepth(basedisplay.getXDisplay(), screen_number);
589
590   m_size = Size(WidthOfScreen(ScreenOfDisplay(basedisplay.getXDisplay(),
591                                               screen_number)),
592                 HeightOfScreen(ScreenOfDisplay(basedisplay.getXDisplay(),
593                                                screen_number)));
594
595   // search for a TrueColor Visual... if we can't find one... we will use the
596   // default visual for the screen
597   XVisualInfo vinfo_template, *vinfo_return;
598   int vinfo_nitems;
599
600   vinfo_template.screen = screen_number;
601   vinfo_template.c_class = TrueColor;
602
603   visual = (Visual *) 0;
604
605   if ((vinfo_return = XGetVisualInfo(basedisplay.getXDisplay(),
606                                      VisualScreenMask | VisualClassMask,
607                                      &vinfo_template, &vinfo_nitems)) &&
608       vinfo_nitems > 0) {
609     for (int i = 0; i < vinfo_nitems; i++) {
610       if (depth < (vinfo_return + i)->depth) {
611         depth = (vinfo_return + i)->depth;
612         visual = (vinfo_return + i)->visual;
613       }
614     }
615
616     XFree(vinfo_return);
617   }
618
619   if (visual) {
620     colormap = XCreateColormap(basedisplay.getXDisplay(), root_window,
621                                visual, AllocNone);
622   } else {
623     visual = DefaultVisual(basedisplay.getXDisplay(), screen_number);
624     colormap = DefaultColormap(basedisplay.getXDisplay(), screen_number);
625   }
626 }