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>
32 #include <X11/StringDefs.h>
33 #include <X11/IntrinsicP.h> /* CoreP.h needs this */
34 #include <X11/CoreP.h> /* foul, but we need this to use our own
35 window inside a widget instead of one
37 #include <X11/StringDefs.h>
40 /* Define a queue for X-events. One such queue is used for mouse clicks.
41 Another is used for expose events. */
43 #define EVENT_BUFFER_SIZE 64
45 /* Max and Min sizes in character columns. */
53 /* HAVE_X11R4 is defined if we have the features of X11R4. It should
54 be defined when we're using X11R5, since X11R5 has the features of
55 X11R4. If, in the future, we find we need more of these flags
56 (HAVE_X11R5, for example), code should always be written to test
57 the most recent flag first:
67 If you ever find yourself writing a "#ifdef HAVE_FOO" clause that
68 looks a lot like another one, consider moving the text into a macro
69 whose definition is configuration-dependent, but whose usage is
70 universal - like the stuff in systime.h.
72 It turns out that we can auto-detect whether we're being compiled
73 with X11R3 or X11R4 by looking for the flag macros for R4 structure
74 members that R3 doesn't have. */
76 /* AIX 3.1's X is somewhere between X11R3 and X11R4. It has
77 PBaseSize, but not XWithdrawWindow, XSetWMName, XSetWMNormalHints,
84 #ifdef XlibSpecificationRelease
85 #if XlibSpecificationRelease >= 5
90 #define PIX_TYPE unsigned long
91 #define XDISPLAY x_current_display,
92 #define XFlushQueue() XFlush(x_current_display)
93 #define BLACK_PIX_DEFAULT BlackPixel (x_current_display, \
94 XDefaultScreen (x_current_display))
95 #define WHITE_PIX_DEFAULT WhitePixel (x_current_display, \
96 XDefaultScreen (x_current_display))
97 #define DISPLAY_SCREEN_ARG x_current_display, \
98 XDefaultScreen (x_current_display)
99 #define DISPLAY_CELLS DisplayCells (x_current_display, XDefaultScreen (x_current_display))
100 #define ROOT_WINDOW RootWindow (x_current_display, DefaultScreen (x_current_display))
101 #define FONT_TYPE XFontStruct
104 #define XExposeRegionEvent XExposeEvent
105 #define Bitmap Pixmap /* In X11, Bitmaps are are kind of
107 #define WINDOWINFO_TYPE XWindowAttributes
108 #define XGetWindowInfo(w, i) XGetWindowAttributes (x_current_display, \
110 #define XGetFont(f) XLoadQueryFont (x_current_display, (f))
111 #define XLoseFont(f) XFreeFont (x_current_display, (f))
112 #define XStuffPending() XPending (x_current_display)
113 #define XClear(w) XClearWindow (x_current_display, (w))
114 #define XWarpMousePointer(w,x,y) XWarpPointer (x_current_display, None, w, \
116 #define XHandleError XSetErrorHandler
117 #define XHandleIOError XSetIOErrorHandler
119 #define XChangeWindowSize(w,x,y) XResizeWindow(x_current_display,w,x,y)
121 #define FONT_WIDTH(f) ((f)->max_bounds.width)
122 #define FONT_HEIGHT(f) ((f)->ascent + (f)->descent)
123 #define FONT_BASE(f) ((f)->ascent)
125 /* The mask of events that text windows always want to receive. This
126 does not include mouse movement events. It is used when the window
127 is created (in x_window) and when we ask/unask for mouse movement
128 events (in XTmouse_tracking_enable).
130 We do include ButtonReleases in this set because elisp isn't always
131 fast enough to catch them when it wants them, and they're rare
132 enough that they don't use much processor time. */
134 #define STANDARD_EVENT_SET \
138 | ButtonReleaseMask \
139 | PointerMotionMask \
140 | PointerMotionHintMask \
141 | StructureNotifyMask \
145 | VisibilityChangeMask)
149 #define ConnectionNumber(dpy) dpyno()
152 #define XFlushQueue() XFlush()
153 #define BLACK_PIX_DEFAULT BlackPixel
154 #define WHITE_PIX_DEFAULT WhitePixel
155 #define DISPLAY_SCREEN_ARG
156 #define DISPLAY_CELLS DisplayCells ()
157 #define ROOT_WINDOW RootWindow
159 #define FONT_TYPE FontInfo
161 #define WINDOWINFO_TYPE WindowInfo
162 #define XGetWindowInfo(w, i) XQueryWindow ((w), (i))
163 #define XGetFont(f) XOpenFont ((f))
164 #define XLoseFont(f) XCloseFont ((f))
165 #define XStuffPending() XPending ()
166 #define XWarpMousePointer(w,x,y) XWarpMouse (w,x,y)
167 #define XHandleError XErrorHandler
168 #define XHandleIOError XIOErrorHandler
170 #define FONT_WIDTH(f) ((f)->width)
171 #define FONT_HEIGHT(f) ((f)->height)
172 #define FONT_BASE(f) ((f)->base)
174 #define XChangeWindowSize(w,x,y) XChangeWindow(w,x,y)
180 int rindex
; /* Index at which to fetch next. */
181 int windex
; /* Index at which to store next. */
182 XEvent xrep
[EVENT_BUFFER_SIZE
];
185 /* Queue for mouse clicks. */
186 extern struct event_queue x_mouse_queue
;
188 /* This is the X connection that we are using. */
190 extern Display
*x_current_display
;
192 /* This checks to make sure we have a display. */
193 extern void check_x ();
195 extern struct frame
*x_window_to_frame ();
198 extern struct frame
*x_any_window_to_frame ();
201 /* The frame (if any) which has the X window that has keyboard focus.
202 Zero if none. This is examined by Ffocus_frame in xfns.c */
204 extern struct frame
*x_focus_frame
;
207 /* Variables associated with the X display screen this emacs is using. */
209 /* How many screens this X display has. */
210 extern int x_screen_count
;
212 /* The vendor supporting this X server. */
213 extern Lisp_Object Vx_vendor
;
215 /* The vendor's release number for this X server. */
216 extern int x_release
;
218 /* Height of this X screen in pixels. */
219 extern int x_screen_height
;
221 /* Height of this X screen in millimeters. */
222 extern int x_screen_height_mm
;
224 /* Width of this X screen in pixels. */
225 extern int x_screen_width
;
227 /* Width of this X screen in millimeters. */
228 extern int x_screen_width_mm
;
230 /* Does this X screen do backing store? */
231 extern Lisp_Object Vx_backing_store
;
233 /* Does this X screen do save-unders? */
234 extern int x_save_under
;
236 /* Number of planes for this screen. */
237 extern int x_screen_planes
;
239 /* X Visual type of this screen. */
240 extern Lisp_Object Vx_screen_visual
;
242 #endif /* HAVE_X11 */
244 enum text_cursor_kinds
{
245 filled_box_cursor
, hollow_box_cursor
, bar_cursor
248 /* Each X frame object points to its own struct x_display object
249 in the display.x field. The x_display structure contains all
250 the information that is specific to X windows. */
254 /* Position of the X window (x and y offsets in root window). */
258 /* Border width of the X window as known by the X window system. */
261 /* Size of the X window in pixels. */
262 int pixel_height
, pixel_width
;
265 /* The tiled border used when the mouse is out of the frame. */
268 /* Here are the Graphics Contexts for the default font. */
269 GC normal_gc
; /* Normal video */
270 GC reverse_gc
; /* Reverse video */
271 GC cursor_gc
; /* cursor drawing */
272 #endif /* HAVE_X11 */
274 /* Width of the internal border. This is a line of background color
275 just inside the window's border. When the frame is selected,
276 a highlighting is displayed inside the internal border. */
277 int internal_border_width
;
279 /* The X window used for this frame.
280 May be zero while the frame object is being created
281 and the X window has not yet been created. */
284 /* The X window used for the bitmap icon;
285 or 0 if we don't have a bitmap icon. */
288 /* The X window that is the parent of this X window.
289 Usually but not always RootWindow. */
293 /* The widget of this screen. This is the window of a "shell" widget. */
295 /* The XmPanedWindows... */
296 Widget column_widget
;
297 /* The widget of the edit portion of this screen; the window in
298 "window_desc" is inside of this. */
301 Widget menubar_widget
;
304 /* 1 for bitmap icon, 0 for text icon. */
305 int icon_bitmap_flag
;
309 /* Pixel values used for various purposes.
310 border_pixel may be -1 meaning use a gray tile. */
311 PIX_TYPE background_pixel
;
312 PIX_TYPE foreground_pixel
;
313 PIX_TYPE cursor_pixel
;
314 PIX_TYPE border_pixel
;
315 PIX_TYPE mouse_pixel
;
316 PIX_TYPE cursor_foreground_pixel
;
318 /* Descriptor for the cursor in use for this window. */
321 Cursor nontext_cursor
;
322 Cursor modeline_cursor
;
327 /* The name that was associated with the icon, the last time
328 it was refreshed. Usually the same as the name of the
329 buffer in the currently selected window in the frame */
332 /* Flag to set when the X window needs to be completely repainted. */
335 /* What kind of text cursor is drawn in this window right now?
336 (If there is no cursor (phys_cursor_x < 0), then this means nothing.) */
337 enum text_cursor_kinds current_cursor
;
339 /* What kind of text cursor should we draw in the future?
340 This should always be filled_box_cursor or bar_cursor. */
341 enum text_cursor_kinds desired_cursor
;
343 /* These are the current window manager hints. It seems that
344 XSetWMHints, when presented with an unset bit in the `flags'
345 member of the hints structure, does not leave the corresponding
346 attribute unchanged; rather, it resets that attribute to its
347 default value. For example, unless you set the `icon_pixmap'
348 field and the `IconPixmapHint' bit, XSetWMHints will forget what
349 your icon pixmap was. This is rather troublesome, since some of
350 the members (for example, `input' and `icon_pixmap') want to stay
351 the same throughout the execution of Emacs. So, we keep this
352 structure around, just leaving values in it and adding new bits
353 to the mask as we go. */
356 /* The size of the extra width currently allotted for vertical
357 scroll bars, in pixels. */
358 int vertical_scroll_bar_extra
;
360 /* Table of parameter faces for this frame. Any X resources (pixel
361 values, fonts) referred to here have been allocated explicitly
362 for this face, and should be freed if we change the face. */
363 struct face
**param_faces
;
366 /* Table of computed faces for this frame. These are the faces
367 whose indexes go into the upper bits of a glyph, computed by
368 combining the parameter faces specified by overlays, text
369 properties, and what have you. The X resources mentioned here
370 are all shared with parameter faces. */
371 struct face
**computed_faces
;
372 int n_computed_faces
; /* How many are valid */
373 int size_computed_faces
; /* How many are allocated */
376 /* Get at the computed faces of an X window frame. */
377 #define FRAME_PARAM_FACES(f) ((f)->display.x->param_faces)
378 #define FRAME_N_PARAM_FACES(f) ((f)->display.x->n_param_faces)
379 #define FRAME_DEFAULT_PARAM_FACE(f) (FRAME_PARAM_FACES (f)[0])
380 #define FRAME_MODE_LINE_PARAM_FACE(f) (FRAME_PARAM_FACES (f)[1])
382 #define FRAME_COMPUTED_FACES(f) ((f)->display.x->computed_faces)
383 #define FRAME_N_COMPUTED_FACES(f) ((f)->display.x->n_computed_faces)
384 #define FRAME_SIZE_COMPUTED_FACES(f) ((f)->display.x->size_computed_faces)
385 #define FRAME_DEFAULT_FACE(f) ((f)->display.x->computed_faces[0])
386 #define FRAME_MODE_LINE_FACE(f) ((f)->display.x->computed_faces[1])
388 /* Return the window associated with the frame F. */
389 #define FRAME_X_WINDOW(f) ((f)->display.x->window_desc)
391 /* These two really ought to be called FRAME_PIXEL_{WIDTH,HEIGHT}. */
392 #define PIXEL_WIDTH(f) ((f)->display.x->pixel_width)
393 #define PIXEL_HEIGHT(f) ((f)->display.x->pixel_height)
395 #define FRAME_DESIRED_CURSOR(f) ((f)->display.x->desired_cursor)
398 /* When X windows are used, a glyph may be a 16 bit unsigned datum.
399 The high order byte is the face number and is used as an index
400 in the face table. A face is a font plus:
401 1) the unhighlighted foreground color,
402 2) the unhighlighted background color.
403 For highlighting, the two colors are exchanged.
404 Face number 0 is unused. The low order byte of a glyph gives
405 the character within the font. All fonts are assumed to be
406 fixed width, and to have the same height and width. */
410 /* Face declared in dispextern.h */
416 FONT_TYPE
*font
; /* Font info for specified font. */
417 int fg
; /* Unhighlighted foreground. */
418 int bg
; /* Unhighlighted background. */
422 #define MAX_FACES_AND_GLYPHS 256
423 extern struct face
*x_face_table
[];
426 /* X-specific scroll bar stuff. */
428 /* We represent scroll bars as lisp vectors. This allows us to place
429 references to them in windows without worrying about whether we'll
430 end up with windows referring to dead scroll bars; the garbage
431 collector will free it when its time comes.
433 We use struct scroll_bar as a template for accessing fields of the
438 /* These fields are shared by all vectors. */
439 int size_from_Lisp_Vector_struct
;
440 struct Lisp_Vector
*next_from_Lisp_Vector_struct
;
442 /* The window we're a scroll bar for. */
445 /* The next and previous in the chain of scroll bars in this frame. */
446 Lisp_Object next
, prev
;
448 /* The X window representing this scroll bar. Since this is a full
449 32-bit quantity, we store it split into two 32-bit values. */
450 Lisp_Object x_window_low
, x_window_high
;
452 /* The position and size of the scroll bar in pixels, relative to the
454 Lisp_Object top
, left
, width
, height
;
456 /* The starting and ending positions of the handle, relative to the
457 handle area (i.e. zero is the top position, not
458 SCROLL_BAR_TOP_BORDER). If they're equal, that means the handle
459 hasn't been drawn yet.
461 These are not actually the locations where the beginning and end
462 are drawn; in order to keep handles from becoming invisible when
463 editing large files, we establish a minimum height by always
464 drawing handle bottoms VERTICAL_SCROLL_BAR_MIN_HANDLE pixels below
465 where they would be normally; the bottom and top are in a
466 different co-ordinate system. */
467 Lisp_Object start
, end
;
469 /* If the scroll bar handle is currently being dragged by the user,
470 this is the number of pixels from the top of the handle to the
471 place where the user grabbed it. If the handle isn't currently
472 being dragged, this is Qnil. */
473 Lisp_Object dragging
;
476 /* The number of elements a vector holding a struct scroll_bar needs. */
477 #define SCROLL_BAR_VEC_SIZE \
478 ((sizeof (struct scroll_bar) - sizeof (int) - sizeof (struct Lisp_Vector *)) \
479 / sizeof (Lisp_Object))
481 /* Turning a lisp vector value into a pointer to a struct scroll_bar. */
482 #define XSCROLL_BAR(vec) ((struct scroll_bar *) XPNTR (vec))
485 /* Building a 32-bit C integer from two 16-bit lisp integers. */
486 #define SCROLL_BAR_PACK(low, high) (XINT (high) << 16 | XINT (low))
488 /* Setting two lisp integers to the low and high words of a 32-bit C int. */
489 #define SCROLL_BAR_UNPACK(low, high, int32) \
490 (XSET ((low), Lisp_Int, (int32) & 0xffff), \
491 XSET ((high), Lisp_Int, ((int32) >> 16) & 0xffff))
494 /* Extract the X window id of the scroll bar from a struct scroll_bar. */
495 #define SCROLL_BAR_X_WINDOW(ptr) \
496 ((Window) SCROLL_BAR_PACK ((ptr)->x_window_low, (ptr)->x_window_high))
498 /* Store a window id in a struct scroll_bar. */
499 #define SET_SCROLL_BAR_X_WINDOW(ptr, id) \
500 (SCROLL_BAR_UNPACK ((ptr)->x_window_low, (ptr)->x_window_high, (int) id))
503 /* Return the outside pixel width for a vertical scroll bar on frame F. */
504 #define VERTICAL_SCROLL_BAR_PIXEL_WIDTH(f) (2*FONT_WIDTH ((f)->display.x->font))
506 /* Return the outside pixel height for a vertical scroll bar HEIGHT
507 rows high on frame F. */
508 #define VERTICAL_SCROLL_BAR_PIXEL_HEIGHT(f, height) \
509 ((height) * FONT_HEIGHT ((f)->display.x->font))
511 /* Return the inside width of a vertical scroll bar, given the outside
513 #define VERTICAL_SCROLL_BAR_INSIDE_WIDTH(width) \
514 ((width) - VERTICAL_SCROLL_BAR_LEFT_BORDER - VERTICAL_SCROLL_BAR_RIGHT_BORDER)
516 /* Return the length of the rectangle within which the top of the
517 handle must stay. This isn't equivalent to the inside height,
518 because the scroll bar handle has a minimum height.
520 This is the real range of motion for the scroll bar, so when we're
521 scaling buffer positions to scroll bar positions, we use this, not
522 VERTICAL_SCROLL_BAR_INSIDE_HEIGHT. */
523 #define VERTICAL_SCROLL_BAR_TOP_RANGE(height) \
524 (VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (height) - VERTICAL_SCROLL_BAR_MIN_HANDLE)
526 /* Return the inside height of vertical scroll bar, given the outside
527 height. See VERTICAL_SCROLL_BAR_TOP_RANGE too. */
528 #define VERTICAL_SCROLL_BAR_INSIDE_HEIGHT(height) \
529 ((height) - VERTICAL_SCROLL_BAR_TOP_BORDER - VERTICAL_SCROLL_BAR_BOTTOM_BORDER)
532 /* Border widths for scroll bars.
534 Scroll bar windows don't have any X borders; their border width is
535 set to zero, and we redraw borders ourselves. This makes the code
536 a bit cleaner, since we don't have to convert between outside width
537 (used when relating to the rest of the screen) and inside width
538 (used when sizing and drawing the scroll bar window itself).
540 The handle moves up and down/back and forth in a rectangle inset
541 from the edges of the scroll bar. These are widths by which we
542 inset the handle boundaries from the scroll bar edges. */
543 #define VERTICAL_SCROLL_BAR_LEFT_BORDER (2)
544 #define VERTICAL_SCROLL_BAR_RIGHT_BORDER (3)
545 #define VERTICAL_SCROLL_BAR_TOP_BORDER (2)
546 #define VERTICAL_SCROLL_BAR_BOTTOM_BORDER (2)
548 /* Minimum lengths for scroll bar handles, in pixels. */
549 #define VERTICAL_SCROLL_BAR_MIN_HANDLE (5)
552 /* Manipulating pixel sizes and character sizes.
553 Knowledge of which factors affect the overall size of the window should
554 be hidden in these macros, if that's possible.
556 /* Return the upper/left pixel position of the character cell on frame F
558 #define CHAR_TO_PIXEL_ROW(f, row) \
559 ((f)->display.x->internal_border_width \
560 + (row) * FONT_HEIGHT ((f)->display.x->font))
561 #define CHAR_TO_PIXEL_COL(f, col) \
562 ((f)->display.x->internal_border_width \
563 + (col) * FONT_WIDTH ((f)->display.x->font))
565 /* Return the pixel width/height of frame F if it has
566 WIDTH columns/HEIGHT rows. */
567 #define CHAR_TO_PIXEL_WIDTH(f, width) \
568 (CHAR_TO_PIXEL_COL (f, width) \
569 + (f)->display.x->vertical_scroll_bar_extra \
570 + (f)->display.x->internal_border_width)
571 #define CHAR_TO_PIXEL_HEIGHT(f, height) \
572 (CHAR_TO_PIXEL_ROW (f, height) \
573 + (f)->display.x->internal_border_width)
576 /* Return the row/column (zero-based) of the character cell containing
577 the pixel on FRAME at ROW/COL. */
578 #define PIXEL_TO_CHAR_ROW(f, row) \
579 (((row) - (f)->display.x->internal_border_width) \
580 / FONT_HEIGHT ((f)->display.x->font))
581 #define PIXEL_TO_CHAR_COL(f, col) \
582 (((col) - (f)->display.x->internal_border_width) \
583 / FONT_WIDTH ((f)->display.x->font))
585 /* How many columns/rows of text can we fit in WIDTH/HEIGHT pixels on
587 #define PIXEL_TO_CHAR_WIDTH(f, width) \
588 (PIXEL_TO_CHAR_COL (f, ((width) \
589 - (f)->display.x->internal_border_width \
590 - (f)->display.x->vertical_scroll_bar_extra)))
591 #define PIXEL_TO_CHAR_HEIGHT(f, height) \
592 (PIXEL_TO_CHAR_ROW (f, ((height) \
593 - (f)->display.x->internal_border_width)))
595 /* If a struct input_event has a kind which is selection_request_event
596 or selection_clear_event, then its contents are really described
597 by this structure. */
599 /* For an event of kind selection_request_event,
600 this structure really describes the contents. */
601 struct selection_input_event
606 Atom selection
, target
, property
;
610 #define SELECTION_EVENT_DISPLAY(eventp) \
611 (((struct selection_input_event *) (eventp))->display)
612 #define SELECTION_EVENT_REQUESTOR(eventp) \
613 (((struct selection_input_event *) (eventp))->requestor)
614 #define SELECTION_EVENT_SELECTION(eventp) \
615 (((struct selection_input_event *) (eventp))->selection)
616 #define SELECTION_EVENT_TARGET(eventp) \
617 (((struct selection_input_event *) (eventp))->target)
618 #define SELECTION_EVENT_PROPERTY(eventp) \
619 (((struct selection_input_event *) (eventp))->property)
620 #define SELECTION_EVENT_TIME(eventp) \
621 (((struct selection_input_event *) (eventp))->time)
624 /* Interface to the face code functions. */
626 /* Create the first two computed faces for a frame -- the ones that
628 extern void init_frame_faces (/* FRAME_PTR */);
630 /* Free the resources for the faces associated with a frame. */
631 extern void free_frame_faces (/* FRAME_PTR */);
633 /* Given a computed face, find or make an equivalent display face
634 in face_vector, and return a pointer to it. */
635 extern struct face
*intern_face (/* FRAME_PTR, struct face * */);
637 /* Given a frame and a face name, return the face's ID number, or
638 zero if it isn't a recognized face name. */
639 extern int face_name_id_number (/* FRAME_PTR, Lisp_Object */);
641 /* Return non-zero if FONT1 and FONT2 have the same size bounding box.
642 We assume that they're both character-cell fonts. */
643 extern int same_size_fonts (/* XFontStruct *, XFontStruct * */);
645 /* Recompute the GC's for the default and modeline faces.
646 We call this after changing frame parameters on which those GC's
648 extern void recompute_basic_faces (/* FRAME_PTR */);
650 /* Return the face ID associated with a buffer position POS. Store
651 into *ENDPTR the next position at which a different face is
652 needed. This does not take account of glyphs that specify their
653 own face codes. F is the frame in use for display, and W is a
654 window displaying the current buffer.
656 REGION_BEG, REGION_END delimit the region, so it can be highlighted. */
657 extern int compute_char_face (/* FRAME_PTR frame,
660 int region_beg, int region_end,
662 /* Return the face ID to use to display a special glyph which selects
663 FACE_CODE as the face ID, assuming that ordinarily the face would
664 be BASIC_FACE. F is the frame. */
665 extern int compute_glyph_face (/* FRAME_PTR, int */);