1 /* Definitions and headers for communication with X protocol.
2 Copyright (C) 1989, 1993 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include <X11/cursorfont.h>
23 #include <X11/Xutil.h>
24 #include <X11/keysym.h>
25 #include <X11/Xatom.h>
26 #include <X11/Xresource.h>
31 /* Define a queue for X-events. One such queue is used for mouse clicks.
32 Another is used for expose events. */
34 #define EVENT_BUFFER_SIZE 64
36 /* Max and Min sizes in character columns. */
44 /* HAVE_X11R4 is defined if we have the features of X11R4. It should
45 be defined when we're using X11R5, since X11R5 has the features of
46 X11R4. If, in the future, we find we need more of these flags
47 (HAVE_X11R5, for example), code should always be written to test
48 the most recent flag first:
58 If you ever find yourself writing a "#ifdef HAVE_FOO" clause that
59 looks a lot like another one, consider moving the text into a macro
60 whose definition is configuration-dependent, but whose usage is
61 universal - like the stuff in systime.h.
63 It turns out that we can auto-detect whether we're being compiled
64 with X11R3 or X11R4 by looking for the flag macros for R4 structure
65 members that R3 doesn't have. */
70 #ifdef XlibSpecificationRelease
71 #if XlibSpecificationRelease >= 5
76 #define PIX_TYPE unsigned long
77 #define XDISPLAY x_current_display,
78 #define XFlushQueue() XFlush(x_current_display)
79 #define BLACK_PIX_DEFAULT BlackPixel (x_current_display, \
80 XDefaultScreen (x_current_display))
81 #define WHITE_PIX_DEFAULT WhitePixel (x_current_display, \
82 XDefaultScreen (x_current_display))
83 #define DISPLAY_SCREEN_ARG x_current_display, \
84 XDefaultScreen (x_current_display)
85 #define DISPLAY_CELLS DisplayCells (x_current_display, XDefaultScreen (x_current_display))
86 #define ROOT_WINDOW RootWindow (x_current_display, DefaultScreen (x_current_display))
87 #define FONT_TYPE XFontStruct
90 #define XExposeRegionEvent XExposeEvent
91 #define Bitmap Pixmap /* In X11, Bitmaps are are kind of
93 #define WINDOWINFO_TYPE XWindowAttributes
94 #define XGetWindowInfo(w, i) XGetWindowAttributes (x_current_display, \
96 #define XGetFont(f) XLoadQueryFont (x_current_display, (f))
97 #define XLoseFont(f) XFreeFont (x_current_display, (f))
98 #define XStuffPending() XPending (x_current_display)
99 #define XClear(w) XClearWindow (x_current_display, (w))
100 #define XWarpMousePointer(w,x,y) XWarpPointer (x_current_display, None, w, \
102 #define XHandleError XSetErrorHandler
103 #define XHandleIOError XSetIOErrorHandler
105 #define XChangeWindowSize(w,x,y) XResizeWindow(x_current_display,w,x,y)
107 #define FONT_WIDTH(f) ((f)->max_bounds.width)
108 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
109 #define FONT_BASE(f) ((f)->ascent)
111 /* The mask of events that text windows always want to receive. This
112 does not include mouse movement events. It is used when the window
113 is created (in x_window) and when we ask/unask for mouse movement
114 events (in XTmouse_tracking_enable).
116 We do include ButtonReleases in this set because elisp isn't always
117 fast enough to catch them when it wants them, and they're rare
118 enough that they don't use much processor time. */
120 #define STANDARD_EVENT_SET \
124 | ButtonReleaseMask \
125 | PointerMotionMask \
126 | PointerMotionHintMask \
127 | StructureNotifyMask \
131 | VisibilityChangeMask)
135 #define ConnectionNumber(dpy) dpyno()
138 #define XFlushQueue() XFlush()
139 #define BLACK_PIX_DEFAULT BlackPixel
140 #define WHITE_PIX_DEFAULT WhitePixel
141 #define DISPLAY_SCREEN_ARG
142 #define DISPLAY_CELLS DisplayCells ()
143 #define ROOT_WINDOW RootWindow
145 #define FONT_TYPE FontInfo
147 #define WINDOWINFO_TYPE WindowInfo
148 #define XGetWindowInfo(w, i) XQueryWindow ((w), (i))
149 #define XGetFont(f) XOpenFont ((f))
150 #define XLoseFont(f) XCloseFont ((f))
151 #define XStuffPending() XPending ()
152 #define XWarpMousePointer(w,x,y) XWarpMouse (w,x,y)
153 #define XHandleError XErrorHandler
154 #define XHandleIOError XIOErrorHandler
156 #define FONT_WIDTH(f) ((f)->width)
157 #define FONT_HEIGHT(f) ((f)->height)
158 #define FONT_BASE(f) ((f)->base)
160 #define XChangeWindowSize(w,x,y) XChangeWindow(w,x,y)
166 int rindex
; /* Index at which to fetch next. */
167 int windex
; /* Index at which to store next. */
168 XEvent xrep
[EVENT_BUFFER_SIZE
];
171 /* Queue for mouse clicks. */
172 extern struct event_queue x_mouse_queue
;
174 /* This is the X connection that we are using. */
176 extern Display
*x_current_display
;
178 extern struct frame
*x_window_to_frame ();
180 /* The frame (if any) which has the X window that has keyboard focus.
181 Zero if none. This is examined by Ffocus_frame in xfns.c */
183 extern struct frame
*x_focus_frame
;
186 /* Variables associated with the X display screen this emacs is using. */
188 /* How many screens this X display has. */
189 extern int x_screen_count
;
191 /* The vendor supporting this X server. */
192 extern Lisp_Object Vx_vendor
;
194 /* The vendor's release number for this X server. */
195 extern int x_release
;
197 /* Height of this X screen in pixels. */
198 extern int x_screen_height
;
200 /* Height of this X screen in millimeters. */
201 extern int x_screen_height_mm
;
203 /* Width of this X screen in pixels. */
204 extern int x_screen_width
;
206 /* Width of this X screen in millimeters. */
207 extern int x_screen_width_mm
;
209 /* Does this X screen do backing store? */
210 extern Lisp_Object Vx_backing_store
;
212 /* Does this X screen do save-unders? */
213 extern int x_save_under
;
215 /* Number of planes for this screen. */
216 extern int x_screen_planes
;
218 /* X Visual type of this screen. */
219 extern Lisp_Object Vx_screen_visual
;
221 #endif /* HAVE_X11 */
223 enum text_cursor_kinds
{
224 filled_box_cursor
, hollow_box_cursor
, bar_cursor
227 /* Each X frame object points to its own struct x_display object
228 in the display.x field. The x_display structure contains all
229 the information that is specific to X windows. */
233 /* Position of the X window (x and y offsets in root window). */
237 /* Border width of the X window as known by the X window system. */
240 /* Size of the X window in pixels. */
241 int pixel_height
, pixel_width
;
244 /* The tiled border used when the mouse is out of the frame. */
247 /* Here are the Graphics Contexts for the default font. */
248 GC normal_gc
; /* Normal video */
249 GC reverse_gc
; /* Reverse video */
250 GC cursor_gc
; /* cursor drawing */
251 #endif /* HAVE_X11 */
253 /* Width of the internal border. This is a line of background color
254 just inside the window's border. When the frame is selected,
255 a highlighting is displayed inside the internal border. */
256 int internal_border_width
;
258 /* The X window used for this frame.
259 May be zero while the frame object is being created
260 and the X window has not yet been created. */
263 /* The X window used for the bitmap icon;
264 or 0 if we don't have a bitmap icon. */
267 /* The X window that is the parent of this X window.
268 Usually but not always RootWindow. */
271 /* 1 for bitmap icon, 0 for text icon. */
272 int icon_bitmap_flag
;
276 /* Pixel values used for various purposes.
277 border_pixel may be -1 meaning use a gray tile. */
278 PIX_TYPE background_pixel
;
279 PIX_TYPE foreground_pixel
;
280 PIX_TYPE cursor_pixel
;
281 PIX_TYPE border_pixel
;
282 PIX_TYPE mouse_pixel
;
283 PIX_TYPE cursor_foreground_pixel
;
285 /* Descriptor for the cursor in use for this window. */
288 Cursor nontext_cursor
;
289 Cursor modeline_cursor
;
294 /* The name that was associated with the icon, the last time
295 it was refreshed. Usually the same as the name of the
296 buffer in the currently selected window in the frame */
299 /* Flag to set when the X window needs to be completely repainted. */
302 /* What kind of text cursor is drawn in this window right now?
303 (If there is no cursor (phys_cursor_x < 0), then this means nothing.) */
304 enum text_cursor_kinds current_cursor
;
306 /* What kind of text cursor should we draw in the future?
307 This should always be filled_box_cursor or bar_cursor. */
308 enum text_cursor_kinds desired_cursor
;
310 /* These are the current window manager hints. It seems that
311 XSetWMHints, when presented with an unset bit in the `flags'
312 member of the hints structure, does not leave the corresponding
313 attribute unchanged; rather, it resets that attribute to its
314 default value. For example, unless you set the `icon_pixmap'
315 field and the `IconPixmapHint' bit, XSetWMHints will forget what
316 your icon pixmap was. This is rather troublesome, since some of
317 the members (for example, `input' and `icon_pixmap') want to stay
318 the same throughout the execution of Emacs. So, we keep this
319 structure around, just leaving values in it and adding new bits
320 to the mask as we go. */
323 /* The size of the extra width currently allotted for vertical
324 scroll bars, in pixels. */
325 int vertical_scroll_bar_extra
;
327 /* Table of parameter faces for this frame. Any X resources (pixel
328 values, fonts) referred to here have been allocated explicitly
329 for this face, and should be freed if we change the face. */
330 struct face
**param_faces
;
333 /* Table of computed faces for this frame. These are the faces
334 whose indexes go into the upper bits of a glyph, computed by
335 combining the parameter faces specified by overlays, text
336 properties, and what have you. The X resources mentioned here
337 are all shared with parameter faces. */
338 struct face
**computed_faces
;
339 int n_computed_faces
; /* How many are valid */
340 int size_computed_faces
; /* How many are allocated */
343 /* Get at the computed faces of an X window frame. */
344 #define FRAME_PARAM_FACES(f) ((f)->display.x->param_faces)
345 #define FRAME_N_PARAM_FACES(f) ((f)->display.x->n_param_faces)
346 #define FRAME_DEFAULT_PARAM_FACE(f) (FRAME_PARAM_FACES (f)[0])
347 #define FRAME_MODE_LINE_PARAM_FACE(f) (FRAME_PARAM_FACES (f)[1])
349 #define FRAME_COMPUTED_FACES(f) ((f)->display.x->computed_faces)
350 #define FRAME_N_COMPUTED_FACES(f) ((f)->display.x->n_computed_faces)
351 #define FRAME_SIZE_COMPUTED_FACES(f) ((f)->display.x->size_computed_faces)
352 #define FRAME_DEFAULT_FACE(f) ((f)->display.x->computed_faces[0])
353 #define FRAME_MODE_LINE_FACE(f) ((f)->display.x->computed_faces[1])
355 /* Return the window associated with the frame F. */
356 #define FRAME_X_WINDOW(f) ((f)->display.x->window_desc)
358 /* These two really ought to be called FRAME_PIXEL_{WIDTH,HEIGHT}. */
359 #define PIXEL_WIDTH(f) ((f)->display.x->pixel_width)
360 #define PIXEL_HEIGHT(f) ((f)->display.x->pixel_height)
362 #define FRAME_DESIRED_CURSOR(f) ((f)->display.x->desired_cursor)
365 /* When X windows are used, a glyph may be a 16 bit unsigned datum.
366 The high order byte is the face number and is used as an index
367 in the face table. A face is a font plus:
368 1) the unhighlighted foreground color,
369 2) the unhighlighted background color.
370 For highlighting, the two colors are exchanged.
371 Face number 0 is unused. The low order byte of a glyph gives
372 the character within the font. All fonts are assumed to be
373 fixed width, and to have the same height and width. */
377 /* Face declared in dispextern.h */
383 FONT_TYPE
*font
; /* Font info for specified font. */
384 int fg
; /* Unhighlighted foreground. */
385 int bg
; /* Unhighlighted background. */
389 #define MAX_FACES_AND_GLYPHS 256
390 extern struct face
*x_face_table
[];
393 /* X-specific scroll bar stuff. */
395 /* We represent scroll bars as lisp vectors. This allows us to place
396 references to them in windows without worrying about whether we'll
397 end up with windows referring to dead scroll bars; the garbage
398 collector will free it when its time comes.
400 We use struct scroll_bar as a template for accessing fields of the
405 /* These fields are shared by all vectors. */
406 int size_from_Lisp_Vector_struct
;
407 struct Lisp_Vector
*next_from_Lisp_Vector_struct
;
409 /* The window we're a scroll bar for. */
412 /* The next and previous in the chain of scroll bars in this frame. */
413 Lisp_Object next
, prev
;
415 /* The X window representing this scroll bar. Since this is a full
416 32-bit quantity, we store it split into two 32-bit values. */
417 Lisp_Object x_window_low
, x_window_high
;
419 /* The position and size of the scroll bar in pixels, relative to the
421 Lisp_Object top
, left
, width
, height
;
423 /* The starting and ending positions of the handle, relative to the
424 handle area (i.e. zero is the top position, not
425 SCROLL_BAR_TOP_BORDER). If they're equal, that means the handle
426 hasn't been drawn yet.
428 These are not actually the locations where the beginning and end
429 are drawn; in order to keep handles from becoming invisible when
430 editing large files, we establish a minimum height by always
431 drawing handle bottoms VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below
432 where they would be normally; the bottom and top are in a
433 different co-ordinate system. */
434 Lisp_Object start
, end
;
436 /* If the scroll bar handle is currently being dragged by the user,
437 this is the number of pixels from the top of the handle to the
438 place where the user grabbed it. If the handle isn't currently
439 being dragged, this is Qnil. */
440 Lisp_Object dragging
;
443 /* The number of elements a vector holding a struct scroll_bar needs. */
444 #define SCROLL_BAR_VEC_SIZE \
445 ((sizeof (struct scroll_bar) - sizeof (int) - sizeof (struct Lisp_Vector *)) \
446 / sizeof (Lisp_Object))
448 /* Turning a lisp vector value into a pointer to a struct scroll_bar. */
449 #define XSCROLL_BAR(vec) ((struct scroll_bar *) XPNTR (vec))
452 /* Building a 32-bit C integer from two 16-bit lisp integers. */
453 #define SCROLL_BAR_PACK(low, high) (XINT (high) << 16 | XINT (low))
455 /* Setting two lisp integers to the low and high words of a 32-bit C int. */
456 #define SCROLL_BAR_UNPACK(low, high, int32) \
457 (XSET ((low), Lisp_Int, (int32) & 0xffff), \
458 XSET ((high), Lisp_Int, ((int32) >> 16) & 0xffff))
461 /* Extract the X window id of the scroll bar from a struct scroll_bar. */
462 #define SCROLL_BAR_X_WINDOW(ptr) \
463 ((Window) SCROLL_BAR_PACK ((ptr)->x_window_low, (ptr)->x_window_high))
465 /* Store a window id in a struct scroll_bar. */
466 #define SET_SCROLL_BAR_X_WINDOW(ptr, id) \
467 (SCROLL_BAR_UNPACK ((ptr)->x_window_low, (ptr)->x_window_high, (int) id))
470 /* Return the outside pixel width for a vertical scroll bar on frame F. */
471 #define VERTICAL_SCROLL_BAR_PIXEL_WIDTH(f) (2*FONT_WIDTH ((f)->display.x->font))
473 /* Return the outside pixel height for a vertical scroll bar HEIGHT
474 rows high on frame F. */
475 #define VERTICAL_SCROLL_BAR_PIXEL_HEIGHT(f, height) \
476 ((height) * FONT_HEIGHT ((f)->display.x->font))
478 /* Return the inside width of a vertical scroll bar, given the outside
480 #define VERTICAL_SCROLL_BAR_INSIDE_WIDTH(width) \
481 ((width) - VERTICAL_SCROLL_BAR_LEFT_BORDER - VERTICAL_SCROLL_BAR_RIGHT_BORDER)
483 /* Return the length of the rectangle within which the top of the
484 handle must stay. This isn't equivalent to the inside height,
485 because the scroll bar handle has a minimum height.
487 This is the real range of motion for the scroll bar, so when we're
488 scaling buffer positions to scroll bar positions, we use this, not
489 VERTICAL_SCROLL_BAR_INSIDE_HEIGHT. */
490 #define VERTICAL_SCROLL_BAR_TOP_RANGE(height) \
491 (VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (height) - VERTICAL_SCROLL_BAR_MIN_HANDLE)
493 /* Return the inside height of vertical scroll bar, given the outside
494 height. See VERTICAL_SCROLL_BAR_TOP_RANGE too. */
495 #define VERTICAL_SCROLL_BAR_INSIDE_HEIGHT(height) \
496 ((height) - VERTICAL_SCROLL_BAR_TOP_BORDER - VERTICAL_SCROLL_BAR_BOTTOM_BORDER)
499 /* Border widths for scroll bars.
501 Scroll bar windows don't have any X borders; their border width is
502 set to zero, and we redraw borders ourselves. This makes the code
503 a bit cleaner, since we don't have to convert between outside width
504 (used when relating to the rest of the screen) and inside width
505 (used when sizing and drawing the scroll bar window itself).
507 The handle moves up and down/back and forth in a rectangle inset
508 from the edges of the scroll bar. These are widths by which we
509 inset the handle boundaries from the scroll bar edges. */
510 #define VERTICAL_SCROLL_BAR_LEFT_BORDER (2)
511 #define VERTICAL_SCROLL_BAR_RIGHT_BORDER (3)
512 #define VERTICAL_SCROLL_BAR_TOP_BORDER (2)
513 #define VERTICAL_SCROLL_BAR_BOTTOM_BORDER (2)
515 /* Minimum lengths for scroll bar handles, in pixels. */
516 #define VERTICAL_SCROLL_BAR_MIN_HANDLE (5)
519 /* Manipulating pixel sizes and character sizes.
520 Knowledge of which factors affect the overall size of the window should
521 be hidden in these macros, if that's possible.
523 /* Return the upper/left pixel position of the character cell on frame F
525 #define CHAR_TO_PIXEL_ROW(f, row) \
526 ((f)->display.x->internal_border_width \
527 + (row) * FONT_HEIGHT ((f)->display.x->font))
528 #define CHAR_TO_PIXEL_COL(f, col) \
529 ((f)->display.x->internal_border_width \
530 + (col) * FONT_WIDTH ((f)->display.x->font))
532 /* Return the pixel width/height of frame F if it has
533 WIDTH columns/HEIGHT rows. */
534 #define CHAR_TO_PIXEL_WIDTH(f, width) \
535 (CHAR_TO_PIXEL_COL (f, width) \
536 + (f)->display.x->vertical_scroll_bar_extra \
537 + (f)->display.x->internal_border_width)
538 #define CHAR_TO_PIXEL_HEIGHT(f, height) \
539 (CHAR_TO_PIXEL_ROW (f, height) \
540 + (f)->display.x->internal_border_width)
543 /* Return the row/column (zero-based) of the character cell containing
544 the pixel on FRAME at ROW/COL. */
545 #define PIXEL_TO_CHAR_ROW(f, row) \
546 (((row) - (f)->display.x->internal_border_width) \
547 / FONT_HEIGHT ((f)->display.x->font))
548 #define PIXEL_TO_CHAR_COL(f, col) \
549 (((col) - (f)->display.x->internal_border_width) \
550 / FONT_WIDTH ((f)->display.x->font))
552 /* How many columns/rows of text can we fit in WIDTH/HEIGHT pixels on
554 #define PIXEL_TO_CHAR_WIDTH(f, width) \
555 (PIXEL_TO_CHAR_COL (f, ((width) \
556 - (f)->display.x->internal_border_width \
557 - (f)->display.x->vertical_scroll_bar_extra)))
558 #define PIXEL_TO_CHAR_HEIGHT(f, height) \
559 (PIXEL_TO_CHAR_ROW (f, ((height) \
560 - (f)->display.x->internal_border_width)))
562 /* If a struct input_event has a kind which is selection_request_event
563 or selection_clear_event, then its contents are really described
564 by this structure. */
566 /* For an event of kind selection_request_event,
567 this structure really describes the contents. */
568 struct selection_input_event
573 Atom selection
, target
, property
;
577 #define SELECTION_EVENT_DISPLAY(eventp) \
578 (((struct selection_input_event *) (eventp))->display)
579 #define SELECTION_EVENT_REQUESTOR(eventp) \
580 (((struct selection_input_event *) (eventp))->requestor)
581 #define SELECTION_EVENT_SELECTION(eventp) \
582 (((struct selection_input_event *) (eventp))->selection)
583 #define SELECTION_EVENT_TARGET(eventp) \
584 (((struct selection_input_event *) (eventp))->target)
585 #define SELECTION_EVENT_PROPERTY(eventp) \
586 (((struct selection_input_event *) (eventp))->property)
587 #define SELECTION_EVENT_TIME(eventp) \
588 (((struct selection_input_event *) (eventp))->time)
591 /* Interface to the face code functions. */
593 /* Create the first two computed faces for a frame -- the ones that
595 extern void init_frame_faces (/* FRAME_PTR */);
597 /* Free the resources for the faces associated with a frame. */
598 extern void free_frame_faces (/* FRAME_PTR */);
600 /* Given a computed face, find or make an equivalent display face
601 in face_vector, and return a pointer to it. */
602 extern struct face
*intern_face (/* FRAME_PTR, struct face * */);
604 /* Given a frame and a face name, return the face's ID number, or
605 zero if it isn't a recognized face name. */
606 extern int face_name_id_number (/* FRAME_PTR, Lisp_Object */);
608 /* Return non-zero if FONT1 and FONT2 have the same size bounding box.
609 We assume that they're both character-cell fonts. */
610 extern int same_size_fonts (/* XFontStruct *, XFontStruct * */);
612 /* Recompute the GC's for the default and modeline faces.
613 We call this after changing frame parameters on which those GC's
615 extern void recompute_basic_faces (/* FRAME_PTR */);
617 /* Return the face ID associated with a buffer position POS. Store
618 into *ENDPTR the next position at which a different face is
619 needed. This does not take account of glyphs that specify their
620 own face codes. F is the frame in use for display, and W is a
621 window displaying the current buffer.
623 REGION_BEG, REGION_END delimit the region, so it can be highlighted. */
624 extern int compute_char_face (/* FRAME_PTR frame,
627 int region_beg, int region_end,
629 /* Return the face ID to use to display a special glyph which selects
630 FACE_CODE as the face ID, assuming that ordinarily the face would
631 be BASIC_FACE. F is the frame. */
632 extern int compute_glyph_face (/* FRAME_PTR, int */);