Display.c 6.7 KB

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