Display.c revision 706f2543
1/*
2
3Copyright 1993 by Davor Matic
4
5Permission to use, copy, modify, distribute, and sell this software
6and its documentation for any purpose is hereby granted without fee,
7provided that the above copyright notice appear in all copies and that
8both that copyright notice and this permission notice appear in
9supporting documentation.  Davor Matic makes no representations about
10the suitability of this software for any purpose.  It is provided "as
11is" without express or implied warranty.
12
13*/
14
15
16#ifdef HAVE_XNEST_CONFIG_H
17#include <xnest-config.h>
18#endif
19
20#include <X11/X.h>
21#include <X11/Xproto.h>
22#include "screenint.h"
23#include "input.h"
24#include "misc.h"
25#include "scrnintstr.h"
26#include "servermd.h"
27
28#include "Xnest.h"
29
30#include "Display.h"
31#include "Init.h"
32#include "Args.h"
33
34#include "icon"
35#include "screensaver"
36
37Display *xnestDisplay = NULL;
38XVisualInfo *xnestVisuals;
39int xnestNumVisuals;
40int xnestDefaultVisualIndex;
41Colormap *xnestDefaultColormaps;
42static int xnestNumDefaultColormaps;
43int *xnestDepths;
44int xnestNumDepths;
45XPixmapFormatValues *xnestPixmapFormats;
46int xnestNumPixmapFormats;
47Pixel xnestBlackPixel;
48Pixel xnestWhitePixel;
49Drawable xnestDefaultDrawables[MAXDEPTH + 1];
50Pixmap xnestIconBitmap;
51Pixmap xnestScreenSaverPixmap;
52XlibGC xnestBitmapGC;
53unsigned long xnestEventMask;
54
55void
56xnestOpenDisplay(int argc, char *argv[])
57{
58  XVisualInfo vi;
59  long mask;
60  int i, j;
61
62  if (!xnestDoFullGeneration) return;
63
64  xnestCloseDisplay();
65
66  xnestDisplay = XOpenDisplay(xnestDisplayName);
67  if (xnestDisplay == NULL)
68    FatalError("Unable to open display \"%s\".\n",
69	       XDisplayName(xnestDisplayName));
70
71  if (xnestSynchronize)
72    XSynchronize(xnestDisplay, True);
73
74  mask = VisualScreenMask;
75  vi.screen = DefaultScreen(xnestDisplay);
76  xnestVisuals = XGetVisualInfo(xnestDisplay, mask, &vi, &xnestNumVisuals);
77  if (xnestNumVisuals == 0 || xnestVisuals == NULL)
78    FatalError("Unable to find any visuals.\n");
79
80  if (xnestUserDefaultClass || xnestUserDefaultDepth) {
81    xnestDefaultVisualIndex = UNDEFINED;
82    for (i = 0; i < xnestNumVisuals; i++)
83      if ((!xnestUserDefaultClass ||
84	   xnestVisuals[i].class == xnestDefaultClass)
85	  &&
86	  (!xnestUserDefaultDepth ||
87	   xnestVisuals[i].depth == xnestDefaultDepth)) {
88	xnestDefaultVisualIndex = i;
89	break;
90      }
91    if (xnestDefaultVisualIndex == UNDEFINED)
92      FatalError("Unable to find desired default visual.\n");
93  }
94  else {
95    vi.visualid = XVisualIDFromVisual(DefaultVisual(xnestDisplay,
96				      DefaultScreen(xnestDisplay)));
97    xnestDefaultVisualIndex = 0;
98    for (i = 0; i < xnestNumVisuals; i++)
99      if (vi.visualid == xnestVisuals[i].visualid)
100	xnestDefaultVisualIndex = i;
101  }
102
103  xnestNumDefaultColormaps = xnestNumVisuals;
104  xnestDefaultColormaps = (Colormap *)malloc(xnestNumDefaultColormaps *
105					     sizeof(Colormap));
106  for (i = 0; i < xnestNumDefaultColormaps; i++)
107    xnestDefaultColormaps[i] = XCreateColormap(xnestDisplay,
108					       DefaultRootWindow(xnestDisplay),
109					       xnestVisuals[i].visual,
110					       AllocNone);
111
112  xnestDepths = XListDepths(xnestDisplay, DefaultScreen(xnestDisplay),
113			    &xnestNumDepths);
114
115  xnestPixmapFormats = XListPixmapFormats(xnestDisplay,
116					  &xnestNumPixmapFormats);
117
118  xnestBlackPixel = BlackPixel(xnestDisplay, DefaultScreen(xnestDisplay));
119  xnestWhitePixel = WhitePixel(xnestDisplay, DefaultScreen(xnestDisplay));
120
121  if (xnestParentWindow != (Window) 0)
122    xnestEventMask = StructureNotifyMask;
123  else
124    xnestEventMask = 0L;
125
126  for (i = 0; i <= MAXDEPTH; i++)
127    xnestDefaultDrawables[i] = None;
128
129  for (i = 0; i < xnestNumPixmapFormats; i++)
130    for (j = 0; j < xnestNumDepths; j++)
131      if (xnestPixmapFormats[i].depth == 1 ||
132	  xnestPixmapFormats[i].depth == xnestDepths[j]) {
133	xnestDefaultDrawables[xnestPixmapFormats[i].depth] =
134	  XCreatePixmap(xnestDisplay, DefaultRootWindow(xnestDisplay),
135			1, 1, xnestPixmapFormats[i].depth);
136      }
137
138  xnestBitmapGC = XCreateGC(xnestDisplay, xnestDefaultDrawables[1], 0L, NULL);
139
140  if (!(xnestUserGeometry & XValue))
141    xnestX = 0;
142
143  if (!(xnestUserGeometry & YValue))
144    xnestY = 0;
145
146  if (xnestParentWindow == 0) {
147    if (!(xnestUserGeometry & WidthValue))
148      xnestWidth = 3 * DisplayWidth(xnestDisplay,
149				    DefaultScreen(xnestDisplay)) / 4;
150
151    if (!(xnestUserGeometry & HeightValue))
152      xnestHeight = 3 * DisplayHeight(xnestDisplay,
153				      DefaultScreen(xnestDisplay)) / 4;
154  }
155
156  if (!xnestUserBorderWidth)
157    xnestBorderWidth = 1;
158
159  xnestIconBitmap =
160    XCreateBitmapFromData(xnestDisplay,
161			  DefaultRootWindow(xnestDisplay),
162			  (char *)icon_bits,
163			  icon_width,
164			  icon_height);
165
166  xnestScreenSaverPixmap =
167    XCreatePixmapFromBitmapData(xnestDisplay,
168				DefaultRootWindow(xnestDisplay),
169				(char *)screensaver_bits,
170				screensaver_width,
171				screensaver_height,
172				xnestWhitePixel,
173				xnestBlackPixel,
174				DefaultDepth(xnestDisplay,
175					     DefaultScreen(xnestDisplay)));
176}
177
178void
179xnestCloseDisplay(void)
180{
181  if (!xnestDoFullGeneration || !xnestDisplay) return;
182
183  /*
184    If xnestDoFullGeneration all x resources will be destroyed upon closing
185    the display connection.  There is no need to generate extra protocol.
186    */
187
188  free(xnestDefaultColormaps);
189  XFree(xnestVisuals);
190  XFree(xnestDepths);
191  XFree(xnestPixmapFormats);
192  XCloseDisplay(xnestDisplay);
193}
194