]> icculus.org git repositories - mikachu/openbox.git/blob - otk/screeninfo.cc
add an OBDisplay class and the old ScreenInfo class to the toolkit.
[mikachu/openbox.git] / otk / screeninfo.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "screeninfo.hh"
8 #include "display.hh"
9
10 using std::string;
11
12 namespace otk {
13
14 ScreenInfo::ScreenInfo(unsigned int num) {
15   screen_number = num;
16
17   root_window = RootWindow(ob::OBDisplay::display, screen_number);
18
19   rect.setSize(WidthOfScreen(ScreenOfDisplay(OBDisplay::display,
20                                              screen_number)),
21                HeightOfScreen(ScreenOfDisplay(OBDisplay::display,
22                                               screen_number)));
23   /*
24     If the default depth is at least 8 we will use that,
25     otherwise we try to find the largest TrueColor visual.
26     Preference is given to 24 bit over larger depths if 24 bit is an option.
27   */
28
29   depth = DefaultDepth(OBDisplay::display, screen_number);
30   visual = DefaultVisual(OBDisplay::display, screen_number);
31   colormap = DefaultColormap(OBDisplay::display, screen_number);
32   
33   if (depth < 8) {
34     // search for a TrueColor Visual... if we can't find one...
35     // we will use the default visual for the screen
36     XVisualInfo vinfo_template, *vinfo_return;
37     int vinfo_nitems;
38     int best = -1;
39
40     vinfo_template.screen = screen_number;
41     vinfo_template.c_class = TrueColor;
42
43     vinfo_return = XGetVisualInfo(OBDisplay::display,
44                                   VisualScreenMask | VisualClassMask,
45                                   &vinfo_template, &vinfo_nitems);
46     if (vinfo_return) {
47       int max_depth = 1;
48       for (int i = 0; i < vinfo_nitems; ++i) {
49         if (vinfo_return[i].depth > max_depth) {
50           if (max_depth == 24 && vinfo_return[i].depth > 24)
51             break;          // prefer 24 bit over 32
52           max_depth = vinfo_return[i].depth;
53           best = i;
54         }
55       }
56       if (max_depth < depth) best = -1;
57     }
58
59     if (best != -1) {
60       depth = vinfo_return[best].depth;
61       visual = vinfo_return[best].visual;
62       colormap = XCreateColormap(OBDisplay::display, root_window, visual,
63                                  AllocNone);
64     }
65
66     XFree(vinfo_return);
67   }
68
69   // get the default display string and strip the screen number
70   string default_string = DisplayString(OBDisplay::display);
71   const string::size_type pos = default_string.rfind(".");
72   if (pos != string::npos)
73     default_string.resize(pos);
74
75   display_string = string("DISPLAY=") + default_string + '.' +
76     itostring(static_cast<unsigned long>(screen_number));
77   
78 #ifdef    XINERAMA
79   xinerama_active = False;
80
81   if (d->hasXineramaExtensions()) {
82     if (d->getXineramaMajorVersion() == 1) {
83       // we know the version 1(.1?) protocol
84
85       /*
86         in this version of Xinerama, we can't query on a per-screen basis, but
87         in future versions we should be able, so the 'activeness' is checked
88         on a pre-screen basis anyways.
89       */
90       if (XineramaIsActive(OBDisplay::display)) {
91         /*
92           If Xinerama is being used, there there is only going to be one screen
93           present. We still, of course, want to use the screen class, but that
94           is why no screen number is used in this function call. There should
95           never be more than one screen present with Xinerama active.
96         */
97         int num;
98         XineramaScreenInfo *info = XineramaQueryScreens(OBDisplay::display,
99                                                         &num);
100         if (num > 0 && info) {
101           xinerama_areas.reserve(num);
102           for (int i = 0; i < num; ++i) {
103             xinerama_areas.push_back(Rect(info[i].x_org, info[i].y_org,
104                                           info[i].width, info[i].height));
105           }
106           XFree(info);
107
108           // if we can't find any xinerama regions, then we act as if it is not
109           // active, even though it said it was
110           xinerama_active = True;
111         }
112       }
113     }
114   }
115 #endif // XINERAMA
116 }
117
118 }