1 /* -*-C-*-
2 
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4     1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5     2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Massachusetts
6     Institute of Technology
7 
8 This file is part of MIT/GNU Scheme.
9 
10 MIT/GNU Scheme is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or (at
13 your option) any later version.
14 
15 MIT/GNU Scheme is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with MIT/GNU Scheme; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
23 USA.
24 
25 */
26 
27 #ifndef SCHEME_X11_H
28 #define SCHEME_X11_H
29 
30 #include <X11/Xlib.h>
31 #include <X11/cursorfont.h>
32 #include <X11/keysym.h>
33 #include <X11/Xutil.h>
34 #include <X11/Xatom.h>
35 
36 struct xdisplay
37 {
38   unsigned int allocation_index;
39   Display * display;
40   unsigned int server_ping_timer;
41   Atom wm_protocols;
42   Atom wm_delete_window;
43   Atom wm_take_focus;
44   XEvent cached_event;
45   char cached_event_p;
46 
47   /* X key events have 8-bit modifier masks, three bits of which are
48      defined to be Shift, Lock, and Control, identified with ShiftMask,
49      LockMask, and ControlMask; and five bits of which are unspecified
50      named only mod1 to mod5.  Which ones mean Meta, Super, Hyper, &c.,
51      vary from system to system, however, so, on initializing the display
52      record, we grovel through some tables (XGetKeyboardMapping and
53      XGetModifierMapping) to find which ones the various modifier
54      keysyms are assigned to, and cache them here.
55 
56      Scheme knows about Shift, Control, Meta, Super, and Hyper.  Of
57      these, only Meta, Super, and Hyper are identified by numbered
58      modifier masks.  All other modifiers are ignored. */
59   int modifier_mask_meta;
60   int modifier_mask_super;
61   int modifier_mask_hyper;
62 
63   /* The type of window manager we have.  If we move FRAME_OUTER_WINDOW
64      to x/y 0/0, some window managers (type A) puts the window manager
65      decorations outside the screen and FRAME_OUTER_WINDOW exactly at 0/0.
66      Other window managers (type B) puts the window including decorations
67      at 0/0, so FRAME_OUTER_WINDOW is a bit below 0/0.
68      Record the type of WM in use so we can compensate for type A WMs.  */
69   enum
70     {
71       X_WMTYPE_UNKNOWN,
72       X_WMTYPE_A,
73       X_WMTYPE_B
74     } wm_type;
75 };
76 
77 #define XD_ALLOCATION_INDEX(xd) ((xd) -> allocation_index)
78 #define XD_DISPLAY(xd) ((xd) -> display)
79 #define XD_SERVER_PING_TIMER(xd) ((xd) -> server_ping_timer)
80 #define XD_WM_PROTOCOLS(xd) ((xd) -> wm_protocols)
81 #define XD_WM_DELETE_WINDOW(xd) ((xd) -> wm_delete_window)
82 #define XD_WM_TAKE_FOCUS(xd) ((xd) -> wm_take_focus)
83 #define XD_CACHED_EVENT(xd) ((xd) -> cached_event)
84 #define XD_CACHED_EVENT_P(xd) ((xd) -> cached_event_p)
85 #define XD_MODIFIER_MASK_SHIFT(xd) (ShiftMask)
86 #define XD_MODIFIER_MASK_CONTROL(xd) (ControlMask)
87 #define XD_MODIFIER_MASK_LOCK(xd) (LockMask)
88 #define XD_MODIFIER_MASK_META(xd) ((xd) -> modifier_mask_meta)
89 #define XD_MODIFIER_MASK_SUPER(xd) ((xd) -> modifier_mask_super)
90 #define XD_MODIFIER_MASK_HYPER(xd) ((xd) -> modifier_mask_hyper)
91 #define XD_WM_TYPE(xd) ((xd) -> wm_type)
92 #define XD_TO_OBJECT(xd) (LONG_TO_UNSIGNED_FIXNUM (XD_ALLOCATION_INDEX (xd)))
93 
94 #define X_MODIFIER_MASK_SHIFT_P(modifier_mask, xd) \
95   ((modifier_mask) & (XD_MODIFIER_MASK_SHIFT (xd)))
96 #define X_MODIFIER_MASK_CONTROL_P(modifier_mask, xd) \
97   ((modifier_mask) & (XD_MODIFIER_MASK_CONTROL (xd)))
98 #define X_MODIFIER_MASK_LOCK_P(modifier_mask, xd) \
99   ((modifier_mask) & (XD_MODIFIER_MASK_LOCK (xd)))
100 #define X_MODIFIER_MASK_META_P(modifier_mask, xd) \
101   ((modifier_mask) & (XD_MODIFIER_MASK_META (xd)))
102 #define X_MODIFIER_MASK_SUPER_P(modifier_mask, xd) \
103   ((modifier_mask) & (XD_MODIFIER_MASK_SUPER (xd)))
104 #define X_MODIFIER_MASK_HYPER_P(modifier_mask, xd) \
105   ((modifier_mask) & (XD_MODIFIER_MASK_HYPER (xd)))
106 
107 extern struct xdisplay * x_display_arg (unsigned int arg);
108 
109 struct drawing_attributes
110 {
111   /* Width of the borders, in pixels. */
112   int border_width;
113   int internal_border_width;
114 
115   /* The primary font. */
116   XFontStruct * font;
117 
118   /* Standard pixel values. */
119   unsigned long background_pixel;
120   unsigned long foreground_pixel;
121   unsigned long border_pixel;
122   unsigned long cursor_pixel;
123   unsigned long mouse_pixel;
124 };
125 
126 /* This incomplete type definition is needed because the scope of the
127    implicit definition in the following typedefs is incorrect.  */
128 struct xwindow;
129 
130 typedef void (*x_deallocator_t) (struct xwindow *);
131 typedef void (*x_event_processor_t) (struct xwindow *, XEvent *);
132 typedef SCHEME_OBJECT (*x_coordinate_map_t)
133   (struct xwindow *, unsigned int);
134 typedef void (*x_update_normal_hints_t) (struct xwindow *);
135 
136 struct xwindow_methods
137 {
138   /* Deallocation procedure to do window-specific deallocation.  */
139   x_deallocator_t deallocator;
140 
141   /* Procedure to call on each received event.  */
142   x_event_processor_t event_processor;
143 
144   /* Procedures to map coordinates to Scheme objects. */
145   x_coordinate_map_t x_coordinate_map;
146   x_coordinate_map_t y_coordinate_map;
147 
148   /* Procedure that is called to inform the window manager of
149      adjustments to the window's internal border or font. */
150   x_update_normal_hints_t update_normal_hints;
151 };
152 
153 struct xwindow
154 {
155   unsigned int allocation_index;
156   Window window;
157   struct xdisplay * xd;
158 
159   /* Dimensions of the drawing region in pixels. */
160   unsigned int x_size;
161   unsigned int y_size;
162 
163   /* The clip rectangle. */
164   unsigned int clip_x;
165   unsigned int clip_y;
166   unsigned int clip_width;
167   unsigned int clip_height;
168 
169   struct drawing_attributes attributes;
170 
171   /* Standard graphics contexts. */
172   GC normal_gc;
173   GC reverse_gc;
174   GC cursor_gc;
175 
176   /* The mouse cursor. */
177   Cursor mouse_cursor;
178 
179   struct xwindow_methods methods;
180 
181   unsigned long event_mask;
182 
183   /* Geometry parameters for window-manager decoration window.  */
184   int wm_decor_x;
185   int wm_decor_y;
186   unsigned int wm_decor_pixel_width;
187   unsigned int wm_decor_pixel_height;
188   unsigned int wm_decor_border_width;
189 
190   /* The latest move we made to the window.  Saved so we can
191      compensate for type A WMs (see wm_type above).  */
192   int expected_x;
193   int expected_y;
194 
195   /* Nonzero if we have made a move and need to check if the WM placed
196      us at the right position.  */
197   int check_expected_move_p;
198 
199   /* The offset we need to add to compensate for type A WMs.  */
200   int move_offset_x;
201   int move_offset_y;
202 };
203 
204 #define XW_ALLOCATION_INDEX(xw) ((xw) -> allocation_index)
205 #define XW_XD(xw) ((xw) -> xd)
206 #define XW_WINDOW(xw) ((xw) -> window)
207 #define XW_X_SIZE(xw) ((xw) -> x_size)
208 #define XW_Y_SIZE(xw) ((xw) -> y_size)
209 #define XW_CLIP_X(xw) ((xw) -> clip_x)
210 #define XW_CLIP_Y(xw) ((xw) -> clip_y)
211 #define XW_CLIP_WIDTH(xw) ((xw) -> clip_width)
212 #define XW_CLIP_HEIGHT(xw) ((xw) -> clip_height)
213 #define XW_BORDER_WIDTH(xw) (((xw) -> attributes) . border_width)
214 #define XW_INTERNAL_BORDER_WIDTH(xw)					\
215   (((xw) -> attributes) . internal_border_width)
216 #define XW_FONT(xw) (((xw) -> attributes) . font)
217 #define XW_BACKGROUND_PIXEL(xw) (((xw) -> attributes) . background_pixel)
218 #define XW_FOREGROUND_PIXEL(xw) (((xw) -> attributes) . foreground_pixel)
219 #define XW_BORDER_PIXEL(xw) (((xw) -> attributes) . border_pixel)
220 #define XW_CURSOR_PIXEL(xw) (((xw) -> attributes) . cursor_pixel)
221 #define XW_MOUSE_PIXEL(xw) (((xw) -> attributes) . mouse_pixel)
222 #define XW_NORMAL_GC(xw) ((xw) -> normal_gc)
223 #define XW_REVERSE_GC(xw) ((xw) -> reverse_gc)
224 #define XW_CURSOR_GC(xw) ((xw) -> cursor_gc)
225 #define XW_MOUSE_CURSOR(xw) ((xw) -> mouse_cursor)
226 #define XW_DEALLOCATOR(xw) (((xw) -> methods) . deallocator)
227 #define XW_EVENT_PROCESSOR(xw) (((xw) -> methods) . event_processor)
228 #define XW_X_COORDINATE_MAP(xw) (((xw) -> methods) . x_coordinate_map)
229 #define XW_Y_COORDINATE_MAP(xw) (((xw) -> methods) . y_coordinate_map)
230 #define XW_UPDATE_NORMAL_HINTS(xw) (((xw) -> methods) . update_normal_hints)
231 #define XW_EVENT_MASK(xw) ((xw) -> event_mask)
232 #define XW_WM_DECOR_X(xw) ((xw) -> wm_decor_x)
233 #define XW_WM_DECOR_Y(xw) ((xw) -> wm_decor_y)
234 #define XW_WM_DECOR_PIXEL_WIDTH(xw) ((xw) -> wm_decor_pixel_width)
235 #define XW_WM_DECOR_PIXEL_HEIGHT(xw) ((xw) -> wm_decor_pixel_height)
236 #define XW_WM_DECOR_BORDER_WIDTH(xw) ((xw) -> wm_decor_border_width)
237 #define XW_EXPECTED_X(xw) ((xw) -> expected_x)
238 #define XW_EXPECTED_Y(xw) ((xw) -> expected_y)
239 #define XW_CHECK_EXPECTED_MOVE_P(xw) ((xw) -> check_expected_move_p)
240 #define XW_MOVE_OFFSET_X(xw) ((xw) -> move_offset_x)
241 #define XW_MOVE_OFFSET_Y(xw) ((xw) -> move_offset_y)
242 
243 #define XW_TO_OBJECT(xw) (LONG_TO_UNSIGNED_FIXNUM (XW_ALLOCATION_INDEX (xw)))
244 #define XW_DISPLAY(xw) (XD_DISPLAY (XW_XD (xw)))
245 #define XW_WM_TYPE(xw) (XD_WM_TYPE (XW_XD (xw)))
246 
247 #define FONT_WIDTH(f) (((f) -> max_bounds) . width)
248 #define FONT_HEIGHT(f) (((f) -> ascent) + ((f) -> descent))
249 #define FONT_BASE(f) ((f) -> ascent)
250 
251 extern struct xwindow * x_window_arg (unsigned int arg);
252 
253 struct ximage
254 {
255   unsigned int allocation_index;
256   XImage * image;
257 };
258 
259 #define XI_ALLOCATION_INDEX(xi) ((xi) -> allocation_index)
260 #define XI_IMAGE(xi) ((xi) -> image)
261 #define X_IMAGE_TO_OBJECT(image)					\
262   (LONG_TO_UNSIGNED_FIXNUM (allocate_x_image (image)))
263 
264 extern struct ximage * x_image_arg (unsigned int arg);
265 extern unsigned int allocate_x_image (XImage * image);
266 extern void deallocate_x_image (struct ximage * xi);
267 
268 struct xvisual
269 {
270   unsigned int allocation_index;
271   Visual * visual;
272 };
273 
274 #define XV_ALLOCATION_INDEX(xv) ((xv) -> allocation_index)
275 #define XV_VISUAL(xv) ((xv) -> visual)
276 #define X_VISUAL_TO_OBJECT(visual)					\
277   (LONG_TO_UNSIGNED_FIXNUM (allocate_x_visual (visual)))
278 
279 extern struct xvisual * x_visual_arg (unsigned int arg);
280 extern unsigned int allocate_x_visual (Visual * visual);
281 extern void deallocate_x_visual (struct xvisual * xv);
282 
283 struct xcolormap
284 {
285   unsigned int allocation_index;
286   Colormap colormap;
287   struct xdisplay * xd;
288 };
289 
290 #define XCM_ALLOCATION_INDEX(xcm) ((xcm) -> allocation_index)
291 #define XCM_COLORMAP(xcm) ((xcm) -> colormap)
292 #define XCM_XD(xcm) ((xcm) -> xd)
293 #define X_COLORMAP_TO_OBJECT(colormap, xd)				\
294   (LONG_TO_UNSIGNED_FIXNUM (allocate_x_colormap ((colormap), (xd))))
295 #define XCM_DISPLAY(xcm) (XD_DISPLAY (XCM_XD (xcm)))
296 
297 extern struct xcolormap * x_colormap_arg (unsigned int arg);
298 extern unsigned int allocate_x_colormap
299   (Colormap colormap, struct xdisplay * xd);
300 extern void deallocate_x_colormap (struct xcolormap * xcm);
301 
302 extern int x_debug;
303 
304 extern void * x_malloc (unsigned int size);
305 extern void * x_realloc (void * ptr, unsigned int size);
306 
307 extern const char * x_get_default
308   (Display * display,
309    const char * resource_name,
310    const char * resource_class,
311    const char * property_name,
312    const char * property_class,
313    const char * sdefault);
314 
315 extern void x_default_attributes
316   (Display * display,
317    const char * resource_name,
318    const char * resource_class,
319    struct drawing_attributes * attributes);
320 
321 extern struct xwindow * x_make_window
322   (struct xdisplay * xd,
323    Window window,
324    int x_size,
325    int y_size,
326    struct drawing_attributes * attributes,
327    struct xwindow_methods * methods,
328    unsigned int size);
329 
330 extern void xw_set_wm_input_hint (struct xwindow * xw, int input_hint);
331 extern void xw_set_wm_name (struct xwindow * xw, const char * name);
332 extern void xw_set_wm_icon_name (struct xwindow * xw, const char * name);
333 
334 extern void x_decode_window_map_arg
335   (SCHEME_OBJECT map_arg,
336    const char ** resource_class,
337    const char ** resource_name,
338    int * map_p);
339 
340 extern void xw_make_window_map
341   (struct xwindow * xw,
342    const char * resource_name,
343    const char * resource_class,
344    int map_p);
345 
346 #endif /* defined (SCHEME_X11_H) */
347