]> icculus.org git repositories - mikachu/openbox.git/blob - src/BaseDisplay.cc
no more use of LinkedList in BaseDisplay
[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 #include <algorithm>
95
96 // X error handler to handle any and all X errors while the application is
97 // running
98 static Bool internal_error = False;
99 static Window last_bad_window = None;
100
101 BaseDisplay *base_display;
102
103 static int handleXErrors(Display *d, XErrorEvent *e) {
104 #ifdef    DEBUG
105   char errtxt[128];
106
107   XGetErrorText(d, e->error_code, errtxt, 128);
108   fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayXError,
109                      "%s:  X error: %s(%d) opcodes %d/%d\n  resource 0x%lx\n"),
110           base_display->getApplicationName(), errtxt, e->error_code,
111           e->request_code, e->minor_code, e->resourceid);
112 #endif // DEBUG
113
114   if (e->error_code == BadWindow) last_bad_window = e->resourceid;
115   if (internal_error) abort();
116
117   return(False);
118 }
119
120
121 // signal handler to allow for proper and gentle shutdown
122
123 #ifndef   HAVE_SIGACTION
124 static RETSIGTYPE signalhandler(int sig) {
125 #else //  HAVE_SIGACTION
126 static void signalhandler(int sig) {
127 #endif // HAVE_SIGACTION
128
129   static int re_enter = 0;
130
131   switch (sig) {
132   case SIGCHLD:
133     int status;
134     waitpid(-1, &status, WNOHANG | WUNTRACED);
135
136 #ifndef   HAVE_SIGACTION
137     // assume broken, braindead sysv signal semantics
138     signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
139 #endif // HAVE_SIGACTION
140
141     break;
142
143   default:
144     if (base_display->handleSignal(sig)) {
145
146 #ifndef   HAVE_SIGACTION
147       // assume broken, braindead sysv signal semantics
148       signal(sig, (RETSIGTYPE (*)(int)) signalhandler);
149 #endif // HAVE_SIGACTION
150
151       return;
152     }
153
154     fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplaySignalCaught,
155                                      "%s:  signal %d caught\n"),
156             base_display->getApplicationName(), sig);
157
158     if (! base_display->isStartup() && ! re_enter) {
159       internal_error = True;
160
161       re_enter = 1;
162       fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayShuttingDown,
163                                        "shutting down\n"));
164       base_display->shutdown();
165     }
166
167     if (sig != SIGTERM && sig != SIGINT) {
168       fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayAborting,
169                                        "aborting... dumping core\n"));
170       abort();
171     }
172
173     exit(0);
174
175     break;
176   }
177 }
178
179
180 // convenience functions
181 #ifndef    __EMX__
182 void bexec(const char *command, char* displaystring) {
183   if (! fork()) {
184     setsid();
185     putenv(displaystring);
186     execl("/bin/sh", "/bin/sh", "-c", command, NULL);
187     exit(0);
188   }
189 }
190 #endif // !__EMX__
191
192 char *bstrdup(const char *s) {
193   const int l = strlen(s) + 1;
194   char *n = new char[l];
195   strncpy(n, s, l);
196   return n;
197 }
198
199 BaseDisplay::BaseDisplay(const char *app_name, char *dpy_name) {
200   application_name = bstrdup(app_name);
201
202   _startup = True;
203   _shutdown = False;
204   server_grabs = 0;
205   last_bad_window = None;
206
207   ::base_display = this;
208
209 #ifdef    HAVE_SIGACTION
210   struct sigaction action;
211
212   action.sa_handler = signalhandler;
213   action.sa_mask = sigset_t();
214   action.sa_flags = SA_NOCLDSTOP | SA_NODEFER;
215
216   sigaction(SIGPIPE, &action, NULL);
217   sigaction(SIGSEGV, &action, NULL);
218   sigaction(SIGFPE, &action, NULL);
219   sigaction(SIGTERM, &action, NULL);
220   sigaction(SIGINT, &action, NULL);
221   sigaction(SIGCHLD, &action, NULL);
222   sigaction(SIGHUP, &action, NULL);
223   sigaction(SIGUSR1, &action, NULL);
224   sigaction(SIGUSR2, &action, NULL);
225 #else // !HAVE_SIGACTION
226   signal(SIGPIPE, (RETSIGTYPE (*)(int)) signalhandler);
227   signal(SIGSEGV, (RETSIGTYPE (*)(int)) signalhandler);
228   signal(SIGFPE, (RETSIGTYPE (*)(int)) signalhandler);
229   signal(SIGTERM, (RETSIGTYPE (*)(int)) signalhandler);
230   signal(SIGINT, (RETSIGTYPE (*)(int)) signalhandler);
231   signal(SIGUSR1, (RETSIGTYPE (*)(int)) signalhandler);
232   signal(SIGUSR2, (RETSIGTYPE (*)(int)) signalhandler);
233   signal(SIGHUP, (RETSIGTYPE (*)(int)) signalhandler);
234   signal(SIGCHLD, (RETSIGTYPE (*)(int)) signalhandler);
235 #endif // HAVE_SIGACTION
236
237   if (! (display = XOpenDisplay(dpy_name))) {
238     fprintf(stderr, i18n->getMessage(BaseDisplaySet, BaseDisplayXConnectFail,
239                "BaseDisplay::BaseDisplay: connection to X server failed.\n"));
240     ::exit(2);
241   } else if (fcntl(ConnectionNumber(display), F_SETFD, 1) == -1) {
242     fprintf(stderr,
243             i18n->getMessage(BaseDisplaySet, BaseDisplayCloseOnExecFail,
244                "BaseDisplay::BaseDisplay: couldn't mark display connection "
245                "as close-on-exec\n"));
246     ::exit(2);
247   }
248
249   number_of_screens = ScreenCount(display);
250   display_name = XDisplayName(dpy_name);
251
252 #ifdef    SHAPE
253   shape.extensions = XShapeQueryExtension(display, &shape.event_basep,
254                                           &shape.error_basep);
255 #else // !SHAPE
256   shape.extensions = False;
257 #endif // SHAPE
258
259   xa_wm_colormap_windows =
260     XInternAtom(display, "WM_COLORMAP_WINDOWS", False);
261   xa_wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False);
262   xa_wm_state = XInternAtom(display, "WM_STATE", False);
263   xa_wm_change_state = XInternAtom(display, "WM_CHANGE_STATE", False);
264   xa_wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
265   xa_wm_take_focus = XInternAtom(display, "WM_TAKE_FOCUS", False);
266   motif_wm_hints = XInternAtom(display, "_MOTIF_WM_HINTS", False);
267
268   openbox_hints = XInternAtom(display, "_BLACKBOX_HINTS", False);
269   openbox_attributes = XInternAtom(display, "_BLACKBOX_ATTRIBUTES", False);
270   openbox_change_attributes =
271     XInternAtom(display, "_BLACKBOX_CHANGE_ATTRIBUTES", False);
272
273   openbox_structure_messages =
274     XInternAtom(display, "_BLACKBOX_STRUCTURE_MESSAGES", False);
275   openbox_notify_startup =
276     XInternAtom(display, "_BLACKBOX_NOTIFY_STARTUP", False);
277   openbox_notify_window_add =
278     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_ADD", False);
279   openbox_notify_window_del =
280     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_DEL", False);
281   openbox_notify_current_workspace =
282     XInternAtom(display, "_BLACKBOX_NOTIFY_CURRENT_WORKSPACE", False);
283   openbox_notify_workspace_count =
284     XInternAtom(display, "_BLACKBOX_NOTIFY_WORKSPACE_COUNT", False);
285   openbox_notify_window_focus =
286     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_FOCUS", False);
287   openbox_notify_window_raise =
288     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_RAISE", False);
289   openbox_notify_window_lower =
290     XInternAtom(display, "_BLACKBOX_NOTIFY_WINDOW_LOWER", False);
291
292   openbox_change_workspace =
293     XInternAtom(display, "_BLACKBOX_CHANGE_WORKSPACE", False);
294   openbox_change_window_focus =
295     XInternAtom(display, "_BLACKBOX_CHANGE_WINDOW_FOCUS", False);
296   openbox_cycle_window_focus =
297     XInternAtom(display, "_BLACKBOX_CYCLE_WINDOW_FOCUS", False);
298
299 #ifdef    NEWWMSPEC
300
301   net_supported = XInternAtom(display, "_NET_SUPPORTED", False);
302   net_client_list = XInternAtom(display, "_NET_CLIENT_LIST", False);
303   net_client_list_stacking = XInternAtom(display, "_NET_CLIENT_LIST_STACKING", False);
304   net_number_of_desktops = XInternAtom(display, "_NET_NUMBER_OF_DESKTOPS", False);
305   net_desktop_geometry = XInternAtom(display, "_NET_DESKTOP_GEOMETRY", False);
306   net_desktop_viewport = XInternAtom(display, "_NET_DESKTOP_VIEWPORT", False);
307   net_current_desktop = XInternAtom(display, "_NET_CURRENT_DESKTOP", False);
308   net_desktop_names = XInternAtom(display, "_NET_DESKTOP_NAMES", False);
309   net_active_window = XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
310   net_workarea = XInternAtom(display, "_NET_WORKAREA", False);
311   net_supporting_wm_check = XInternAtom(display, "_NET_SUPPORTING_WM_CHECK", False);
312   net_virtual_roots = XInternAtom(display, "_NET_VIRTUAL_ROOTS", False);
313
314   net_close_window = XInternAtom(display, "_NET_CLOSE_WINDOW", False);
315   net_wm_moveresize = XInternAtom(display, "_NET_WM_MOVERESIZE", False);
316
317   net_properties = XInternAtom(display, "_NET_PROPERTIES", False);
318   net_wm_name = XInternAtom(display, "_NET_WM_NAME", False);
319   net_wm_desktop = XInternAtom(display, "_NET_WM_DESKTOP", False);
320   net_wm_window_type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
321   net_wm_state = XInternAtom(display, "_NET_WM_STATE", False);
322   net_wm_strut = XInternAtom(display, "_NET_WM_STRUT", False);
323   net_wm_icon_geometry = XInternAtom(display, "_NET_WM_ICON_GEOMETRY", False);
324   net_wm_icon = XInternAtom(display, "_NET_WM_ICON", False);
325   net_wm_pid = XInternAtom(display, "_NET_WM_PID", False);
326   net_wm_handled_icons = XInternAtom(display, "_NET_WM_HANDLED_ICONS", False);
327
328   net_wm_ping = XInternAtom(display, "_NET_WM_PING", False);
329
330 #endif // NEWWMSPEC
331
332   cursor.session = XCreateFontCursor(display, XC_left_ptr);
333   cursor.move = XCreateFontCursor(display, XC_fleur);
334   cursor.ll_angle = XCreateFontCursor(display, XC_ll_angle);
335   cursor.lr_angle = XCreateFontCursor(display, XC_lr_angle);
336   cursor.ul_angle = XCreateFontCursor(display, XC_ul_angle);
337   cursor.ur_angle = XCreateFontCursor(display, XC_ur_angle);
338
339   XSetErrorHandler((XErrorHandler) handleXErrors);
340
341   screenInfoList.reserve(ScreenCount(display));
342   for (int i = 0; i < number_of_screens; i++)
343     screenInfoList.push_back(new ScreenInfo(*this, i));
344
345 #ifndef   NOCLOBBER
346   NumLockMask = ScrollLockMask = 0;
347
348   const XModifierKeymap* const modmap = XGetModifierMapping(display);
349   if (modmap && modmap->max_keypermod > 0) {
350     const int mask_table[] = {
351       ShiftMask, LockMask, ControlMask, Mod1Mask,
352       Mod2Mask, Mod3Mask, Mod4Mask, Mod5Mask
353     };
354     const size_t size = (sizeof(mask_table) / sizeof(mask_table[0])) *
355       modmap->max_keypermod;
356     // get the values of the keyboard lock modifiers
357     // Note: Caps lock is not retrieved the same way as Scroll and Num lock
358     // since it doesn't need to be.
359     const KeyCode num_lock_code = XKeysymToKeycode(display, XK_Num_Lock);
360     const KeyCode scroll_lock_code = XKeysymToKeycode(display, XK_Scroll_Lock);
361     
362     for (size_t cnt = 0; cnt < size; ++cnt) {
363       if (! modmap->modifiermap[cnt]) continue;
364
365       if (num_lock_code == modmap->modifiermap[cnt])
366         NumLockMask = mask_table[cnt / modmap->max_keypermod];
367       if (scroll_lock_code == modmap->modifiermap[cnt])
368         ScrollLockMask = mask_table[cnt / modmap->max_keypermod];
369     }
370   }
371
372   MaskList[0] = 0;
373   MaskList[1] = LockMask;
374   MaskList[2] = NumLockMask;
375   MaskList[3] = ScrollLockMask;
376   MaskList[4] = LockMask | NumLockMask;
377   MaskList[5] = NumLockMask  | ScrollLockMask;
378   MaskList[6] = LockMask | ScrollLockMask;
379   MaskList[7] = LockMask | NumLockMask | ScrollLockMask;
380   MaskListLength = sizeof(MaskList) / sizeof(MaskList[0]);
381   
382   if (modmap) XFreeModifiermap(const_cast<XModifierKeymap*>(modmap));
383 #else
384   NumLockMask = Mod2Mask;
385   ScrollLockMask = Mod5Mask;
386 #endif // NOCLOBBER
387 }
388
389
390 BaseDisplay::~BaseDisplay(void) {
391   std::for_each(screenInfoList.begin(), screenInfoList.end(),
392                 PointerAssassin());
393   // we don't create the BTimers, we don't delete them
394   
395   if (application_name != NULL)
396     delete [] application_name;
397
398   XCloseDisplay(display);
399 }
400
401
402 void BaseDisplay::eventLoop(void) {
403   run();
404
405   int xfd = ConnectionNumber(display);
406
407   while ((! _shutdown) && (! internal_error)) {
408     if (XPending(display)) {
409       XEvent e;
410       XNextEvent(display, &e);
411
412       if (last_bad_window != None && e.xany.window == last_bad_window) {
413 #ifdef    DEBUG
414       fprintf(stderr, i18n->getMessage(BaseDisplaySet,
415                                        BaseDisplayBadWindowRemove,
416                          "BaseDisplay::eventLoop(): removing bad window "
417                          "from event queue\n"));
418 #endif // DEBUG
419       } else {
420         last_bad_window = None;
421         process_event(&e);
422       }
423     } else {
424       fd_set rfds;
425       timeval now, tm, *timeout = (timeval *) 0;
426
427       FD_ZERO(&rfds);
428       FD_SET(xfd, &rfds);
429
430       if (!timerList.empty()) {
431         gettimeofday(&now, 0);
432
433         tm.tv_sec = tm.tv_usec = 0l;
434
435         BTimer *timer = timerList.front();
436         ASSERT(timer != NULL);
437
438         tm.tv_sec = timer->getStartTime().tv_sec +
439           timer->getTimeout().tv_sec - now.tv_sec;
440         tm.tv_usec = timer->getStartTime().tv_usec +
441           timer->getTimeout().tv_usec - now.tv_usec;
442
443         while (tm.tv_usec >= 1000000) {
444           tm.tv_sec++;
445           tm.tv_usec -= 1000000;
446         }
447
448         while (tm.tv_usec < 0) {
449           if (tm.tv_sec > 0) {
450             tm.tv_sec--;
451             tm.tv_usec += 1000000;
452           } else {
453             tm.tv_usec = 0;
454             break;
455           }
456         }
457
458         timeout = &tm;
459       }
460
461       select(xfd + 1, &rfds, 0, 0, timeout);
462
463       // check for timer timeout
464       gettimeofday(&now, 0);
465
466       TimerList::iterator it;
467       for (it = timerList.begin(); it != timerList.end(); ) {
468         BTimer *timer = *it;
469         ++it;   // the timer may be removed from the list, so move ahead now
470         ASSERT(timer != NULL);
471         tm.tv_sec = timer->getStartTime().tv_sec +
472           timer->getTimeout().tv_sec;
473         tm.tv_usec = timer->getStartTime().tv_usec +
474           timer->getTimeout().tv_usec;
475
476         if ((now.tv_sec < tm.tv_sec) ||
477             (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))
478           break;
479
480         timer->fireTimeout();
481
482         // restart the current timer so that the start time is updated
483         if (! timer->doOnce())
484           timer->start();
485         else {
486           timer->stop();
487 //          delete timer; // USE THIS?
488         }
489       }
490     }
491   }
492 }
493
494
495 const Bool BaseDisplay::validateWindow(Window window) {
496   XEvent event;
497   if (XCheckTypedWindowEvent(display, window, DestroyNotify, &event)) {
498     XPutBackEvent(display, &event);
499
500     return False;
501   }
502
503   return True;
504 }
505
506
507 void BaseDisplay::grab(void) {
508   if (! server_grabs++)
509     XGrabServer(display);
510 }
511
512
513 void BaseDisplay::ungrab(void) {
514   if (! --server_grabs)
515     XUngrabServer(display);
516
517   if (server_grabs < 0) server_grabs = 0;
518 }
519
520
521 void BaseDisplay::addTimer(BTimer *timer) {
522   ASSERT(timer != (BTimer *) 0);
523   printf("ADDING TIMER\n");
524
525   TimerList::iterator it;
526   for (it = timerList.begin(); it != timerList.end(); ++it) {
527     BTimer *tmp = *it;
528     if ((tmp->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
529         ((tmp->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
530          (tmp->getTimeout().tv_usec >= timer->getTimeout().tv_usec)))
531       break;
532   }
533
534   timerList.insert(it, timer);
535 }
536
537
538 void BaseDisplay::removeTimer(BTimer *timer) {
539   printf("REMOVING TIMER\n");
540   timerList.remove(timer);
541 }
542
543
544 /*
545  * Grabs a button, but also grabs the button in every possible combination with
546  * the keyboard lock keys, so that they do not cancel out the event.
547  */
548 void BaseDisplay::grabButton(unsigned int button, unsigned int modifiers,
549                              Window grab_window, Bool owner_events,
550                              unsigned int event_mask, int pointer_mode,
551                              int keybaord_mode, Window confine_to,
552                              Cursor cursor) const
553 {
554 #ifndef   NOCLOBBER
555   for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
556     XGrabButton(display, button, modifiers | MaskList[cnt], grab_window,
557                 owner_events, event_mask, pointer_mode, keybaord_mode,
558                 confine_to, cursor);
559 #else  // NOCLOBBER
560     XGrabButton(display, button, modifiers, grab_window,
561                 owner_events, event_mask, pointer_mode, keybaord_mode,
562                 confine_to, cursor);
563 #endif // NOCLOBBER
564 }
565
566 /*
567  * Releases the grab on a button, and ungrabs all possible combinations of the
568  * keyboard lock keys.
569  */
570 void BaseDisplay::ungrabButton(unsigned int button, unsigned int modifiers,
571                                Window grab_window) const {
572 #ifndef   NOCLOBBER
573   for (size_t cnt = 0; cnt < MaskListLength; ++cnt)
574     XUngrabButton(display, button, modifiers | MaskList[cnt], grab_window);
575 #else  // NOCLOBBER
576     XUngrabButton(display, button, modifiers, grab_window);
577 #endif // NOCLOBBER
578 }
579
580
581 ScreenInfo::ScreenInfo(BaseDisplay &d, int num) : basedisplay(d),
582   screen_number(num)
583 {
584
585   root_window = RootWindow(basedisplay.getXDisplay(), screen_number);
586   depth = DefaultDepth(basedisplay.getXDisplay(), screen_number);
587
588   m_size = Size(WidthOfScreen(ScreenOfDisplay(basedisplay.getXDisplay(),
589                                               screen_number)),
590                 HeightOfScreen(ScreenOfDisplay(basedisplay.getXDisplay(),
591                                                screen_number)));
592
593   // search for a TrueColor Visual... if we can't find one... we will use the
594   // default visual for the screen
595   XVisualInfo vinfo_template, *vinfo_return;
596   int vinfo_nitems;
597
598   vinfo_template.screen = screen_number;
599   vinfo_template.c_class = TrueColor;
600
601   visual = (Visual *) 0;
602
603   if ((vinfo_return = XGetVisualInfo(basedisplay.getXDisplay(),
604                                      VisualScreenMask | VisualClassMask,
605                                      &vinfo_template, &vinfo_nitems)) &&
606       vinfo_nitems > 0) {
607     for (int i = 0; i < vinfo_nitems; i++) {
608       if (depth < (vinfo_return + i)->depth) {
609         depth = (vinfo_return + i)->depth;
610         visual = (vinfo_return + i)->visual;
611       }
612     }
613
614     XFree(vinfo_return);
615   }
616
617   if (visual) {
618     colormap = XCreateColormap(basedisplay.getXDisplay(), root_window,
619                                visual, AllocNone);
620   } else {
621     visual = DefaultVisual(basedisplay.getXDisplay(), screen_number);
622     colormap = DefaultColormap(basedisplay.getXDisplay(), screen_number);
623   }
624 }