PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/Build/source/texk/web2c/window/x11-Xlib.c

https://bitbucket.org/preining/tex-live
C | 282 lines | 142 code | 51 blank | 89 comment | 17 complexity | da5d1c213b8d215dde9887e5a9e67dba MD5 | raw file
  1. /*
  2. X Window System version 11 (release 3 et al.) interface for Metafont.
  3. Modified from Tim Morgan's X Version 11 routines by Richard Johnson.
  4. Modified from that by Karl Berry <karl@umb.edu>. 8/3/89
  5. MF can now understand geometry (sort of, at least on a Sun running 3.4
  6. and using uwm) in the resource database, as in the following in
  7. .Xdefaults to put a window of width 500 and height 600 at (200,50):
  8. Metafont*geometry: 500x600+200+200
  9. You cannot give the geometry on the command line (who would want to)?
  10. The width and height specified in the resource must not be larger than
  11. the screenwidth and screendepth defined in ../mf/cmf.ch.
  12. If they are, then I reset them to the maximum.
  13. We don't handle Expose events in general. This means that the window
  14. cannot be moved, resized, or obscured. The problem is that I don't know
  15. when we can look for such events. Adding a check to the main loop of
  16. Metafont was more than I wanted to do. Another problem is that Metafont
  17. does not keep track of the contents of the screen, and so I don't see
  18. how to know what to redraw. The right way to do this is probably to fork
  19. a process, and keep a Pixmap around of the contents of the window.
  20. I could never have done this without David Rosenthal's Hello World
  21. program for X. See $X/mit/doc/HelloWorld.
  22. All section numbers refer to Xlib -- C Language X Interface. */
  23. #define EXTERN extern
  24. #include "../mfd.h"
  25. #ifdef X11WIN
  26. #undef input /* the XWMHints structure has a field named `input' */
  27. #undef output
  28. #include <X11/Xatom.h>
  29. #include <X11/Xlib.h>
  30. #include <X11/Xutil.h>
  31. /* Variables for communicating between the routines we'll write. */
  32. static Display *my_display;
  33. static int my_screen;
  34. static Window my_window;
  35. static GC my_gc;
  36. static int white, black;
  37. /* Window manager hints. */
  38. static XWMHints wm_hints = {
  39. (InputHint|StateHint), /* flags telling which values are set */
  40. False, /* We don't expect input. */
  41. NormalState, /* Initial state. */
  42. 0, /* icon pixmap */
  43. 0, /* icon window */
  44. 0, 0, /* icon location (should get from resource?) */
  45. 0, /* icon mask */
  46. 0 /* window group */
  47. };
  48. /* Some constants for the resource database, etc. */
  49. #define PROGRAM_NAME "Metafont"
  50. #define ARG_GEOMETRY "geometry"
  51. #define BORDER_WIDTH 1 /* Should get this from resource. */
  52. #define DEFAULT_X_POSITION 0
  53. #define DEFAULT_Y_POSITION 0
  54. #include <mfdisplay.h>
  55. /* Return 1 (i.e., true) if display opened successfully, else 0. */
  56. int
  57. mf_x11_initscreen(void)
  58. {
  59. char *geometry;
  60. int geometry_found = 0;
  61. char default_geometry[100];
  62. XSizeHints sizehints;
  63. XGCValues gcvalues;
  64. /* We want the default display. (section 2.1 Opening the display) */
  65. my_display = XOpenDisplay(NULL);
  66. if (my_display == NULL) return 0;
  67. /* Given a display, we can get the screen and the ``black'' and
  68. ``white'' pixels. (section 2.2.1 Display macros) */
  69. my_screen = DefaultScreen(my_display);
  70. white = WhitePixel(my_display, my_screen);
  71. black = BlackPixel(my_display, my_screen);
  72. sizehints.x = DEFAULT_X_POSITION;
  73. sizehints.y = DEFAULT_Y_POSITION;
  74. sizehints.width = screenwidth;
  75. sizehints.height = screendepth;
  76. sizehints.flags = PPosition|PSize;
  77. sprintf (default_geometry, "%ux%u+%u+%u", screenwidth, screendepth,
  78. DEFAULT_X_POSITION, DEFAULT_Y_POSITION);
  79. /* Look up the geometry for this window. (Section 10.2 Obtaining X
  80. environment defaults) */
  81. geometry = XGetDefault(my_display, PROGRAM_NAME, ARG_GEOMETRY);
  82. if (geometry != NULL) {
  83. /* (section 10.3 Parsing window geometry) */
  84. int bitmask = XGeometry(my_display, my_screen,
  85. geometry, default_geometry,
  86. BORDER_WIDTH,
  87. 1, 1, /* ``Font'' width and height. */
  88. 0, 0, /* Interior padding. */
  89. &(sizehints.x), &(sizehints.y),
  90. &(sizehints.width), &(sizehints.height));
  91. /* (section 9.1.6 Setting and getting window sizing hints) */
  92. if (bitmask & (XValue|YValue)) {
  93. sizehints.flags |= USPosition;
  94. geometry_found = 1;
  95. }
  96. if (bitmask & (WidthValue|HeightValue)) {
  97. sizehints.flags |= USSize;
  98. if (sizehints.width > screenwidth) sizehints.width = screenwidth;
  99. if (sizehints.height > screendepth) sizehints.height = screendepth;
  100. geometry_found = 1;
  101. }
  102. }
  103. /* Our window is pretty simple. (section 3.3 Creating windows) */
  104. my_window = XCreateSimpleWindow(my_display,
  105. DefaultRootWindow(my_display), /* parent */
  106. sizehints.x, sizehints.y, /* upper left */
  107. sizehints.width, sizehints.height,
  108. BORDER_WIDTH,
  109. black, /* border color */
  110. white); /* background color */
  111. /* (section 9.1.1 Setting standard properties) */
  112. XSetStandardProperties(my_display, my_window,
  113. PROGRAM_NAME, /* window name */
  114. PROGRAM_NAME, /* icon name */
  115. None, /* pixmap for icon */
  116. 0, 0, /* argv and argc for restarting */
  117. &sizehints);
  118. XSetWMHints(my_display, my_window, &wm_hints);
  119. /* We need a graphics context if we're going to draw anything.
  120. (section 5.3 Manipulating graphics context/state) */
  121. gcvalues.foreground = black;
  122. gcvalues.background = white;
  123. /* A ``thin'' line. This is much faster than a line of length 1,
  124. although the manual cautions that the results might be less
  125. consistent across screens. */
  126. gcvalues.line_width = 0;
  127. my_gc = XCreateGC(my_display, my_window,
  128. GCForeground|GCBackground|GCLineWidth,
  129. &gcvalues);
  130. /* (section 3.5 Mapping windows) This is the confusing part of the
  131. program, at least to me. If no geometry spec was found, then the
  132. window manager puts up the blinking rectangle, and the user clicks,
  133. all before the following call returns. But if a geometry spec was
  134. found, then we want to do a whole mess of other things, because the
  135. window manager is going to send us an expose event so that we can
  136. bring our window up -- and this is one expose event we have to
  137. handle. */
  138. XMapWindow(my_display, my_window);
  139. if (geometry_found) {
  140. /* The window manager sends us an Expose event. Yuck.
  141. */
  142. XEvent my_event;
  143. /* We certainly don't want to handle anything else.
  144. (section 8.5 Selecting events)
  145. */
  146. XSelectInput(my_display, my_window, ExposureMask);
  147. /* We also want to do this right now. This is the confusion. From
  148. stepping through the program under the debugger, it appears
  149. that it is this call to XSync (given the previous call to
  150. XSelectInput) that actually brings the window up -- and yet
  151. without the remaining code, the thing doesn't work right. Very
  152. strange. (section 8.6 Handling the output buffer)
  153. */
  154. XSync(my_display, 0);
  155. /* Now get the event. (section 8.8.1 Returning the next event)
  156. */
  157. XNextEvent(my_display, &my_event);
  158. /* Ignore all but the last of the Expose events.
  159. (section 8.4.5.1 Expose event processing)
  160. */
  161. if (my_event.type == Expose && my_event.xexpose.count == 0) {
  162. /* Apparently the network might STILL have my_events coming in.
  163. Let's throw away Expose my_events again. (section 8.8.3
  164. Selecting my_events using a window or my_event mask)
  165. */
  166. while (XCheckTypedEvent(my_display, Expose, &my_event)) ;
  167. /* Finally, let's draw the blank screen.
  168. */
  169. XClearWindow(my_display, my_window);
  170. }
  171. }
  172. /* That's it. */
  173. return 1;
  174. }
  175. /* Make sure the screen is up to date. (section 8.6 Handling the output
  176. buffer) */
  177. void
  178. mf_x11_updatescreen(void)
  179. {
  180. XFlush(my_display);
  181. }
  182. /* Blank the rectangular inside the given coordinates. We don't need to
  183. reset the foreground to black because we always set it at the beginning
  184. of paintrow (below). */
  185. void
  186. mf_x11_blankrectangle(screencol left,
  187. screencol right,
  188. screenrow top,
  189. screenrow bottom)
  190. {
  191. XSetForeground(my_display, my_gc, white);
  192. XFillRectangle(my_display, my_window, my_gc,
  193. (int) left,
  194. (int) top,
  195. (unsigned) (right - left + 1),
  196. (unsigned) (bottom - top + 1));
  197. }
  198. /* Paint a row with the given ``transition specifications''. We might be
  199. able to do something here with drawing many lines. */
  200. void
  201. mf_x11_paintrow(screenrow row,
  202. pixelcolor init_color,
  203. transspec tvect,
  204. screencol vector_size)
  205. {
  206. register int color, col;
  207. color = (init_color == 0) ? white : black;
  208. do {
  209. col = *tvect++;
  210. XSetForeground(my_display, my_gc, color);
  211. /* (section 6.3.2 Drawing single and multiple lines)
  212. */
  213. XDrawLine(my_display, my_window, my_gc, col, (int) row,
  214. (int) *tvect, (int) row);
  215. color = (color == white) ? black : white;
  216. } while (--vector_size > 0);
  217. }
  218. #else
  219. int x11_dummy;
  220. #endif /* X11WIN */