(comment-indent): Save excursion around call to comment-indent-function.
[emacs.git] / src / xfns.c
blob322b48c11799d33a5af7cf6ea4a0c542f3d79357
1 /* Functions for the X window system.
2 Copyright (C) 1989, 92, 93, 94, 95, 96, 1997, 1998, 1999, 2000
3 Free Software Foundation.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <math.h>
27 /* This makes the fields of a Display accessible, in Xlib header files. */
29 #define XLIB_ILLEGAL_ACCESS
31 #include "lisp.h"
32 #include "xterm.h"
33 #include "frame.h"
34 #include "window.h"
35 #include "buffer.h"
36 #include "intervals.h"
37 #include "dispextern.h"
38 #include "keyboard.h"
39 #include "blockinput.h"
40 #include <epaths.h>
41 #include "charset.h"
42 #include "coding.h"
43 #include "fontset.h"
44 #include "systime.h"
45 #include "termhooks.h"
46 #include "atimer.h"
48 #ifdef HAVE_X_WINDOWS
50 #include <ctype.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
54 #ifndef VMS
55 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
56 #include "bitmaps/gray.xbm"
57 #else
58 #include <X11/bitmaps/gray>
59 #endif
60 #else
61 #include "[.bitmaps]gray.xbm"
62 #endif
64 #ifdef USE_X_TOOLKIT
65 #include <X11/Shell.h>
67 #ifndef USE_MOTIF
68 #include <X11/Xaw/Paned.h>
69 #include <X11/Xaw/Label.h>
70 #endif /* USE_MOTIF */
72 #ifdef USG
73 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
74 #include <X11/Xos.h>
75 #define USG
76 #else
77 #include <X11/Xos.h>
78 #endif
80 #include "widget.h"
82 #include "../lwlib/lwlib.h"
84 #ifdef USE_MOTIF
85 #include <Xm/Xm.h>
86 #include <Xm/DialogS.h>
87 #include <Xm/FileSB.h>
88 #endif
90 /* Do the EDITRES protocol if running X11R5
91 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
93 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
94 #define HACK_EDITRES
95 extern void _XEditResCheckMessages ();
96 #endif /* R5 + Athena */
98 /* Unique id counter for widgets created by the Lucid Widget Library. */
100 extern LWLIB_ID widget_id_tick;
102 #ifdef USE_LUCID
103 /* This is part of a kludge--see lwlib/xlwmenu.c. */
104 extern XFontStruct *xlwmenu_default_font;
105 #endif
107 extern void free_frame_menubar ();
108 extern double atof ();
110 #endif /* USE_X_TOOLKIT */
112 #define min(a,b) ((a) < (b) ? (a) : (b))
113 #define max(a,b) ((a) > (b) ? (a) : (b))
115 #ifdef HAVE_X11R4
116 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
117 #else
118 #define MAXREQUEST(dpy) ((dpy)->max_request_size)
119 #endif
121 /* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
122 it, and including `bitmaps/gray' more than once is a problem when
123 config.h defines `static' as an empty replacement string. */
125 int gray_bitmap_width = gray_width;
126 int gray_bitmap_height = gray_height;
127 unsigned char *gray_bitmap_bits = gray_bits;
129 /* The name we're using in resource queries. Most often "emacs". */
131 Lisp_Object Vx_resource_name;
133 /* The application class we're using in resource queries.
134 Normally "Emacs". */
136 Lisp_Object Vx_resource_class;
138 /* Non-zero means we're allowed to display a busy cursor. */
140 int display_busy_cursor_p;
142 /* The background and shape of the mouse pointer, and shape when not
143 over text or in the modeline. */
145 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
146 Lisp_Object Vx_busy_pointer_shape;
148 /* The shape when over mouse-sensitive text. */
150 Lisp_Object Vx_sensitive_text_pointer_shape;
152 /* If non-nil, the pointer shape to indicate that windows can be
153 dragged horizontally. */
155 Lisp_Object Vx_window_horizontal_drag_shape;
157 /* Color of chars displayed in cursor box. */
159 Lisp_Object Vx_cursor_fore_pixel;
161 /* Nonzero if using X. */
163 static int x_in_use;
165 /* Non nil if no window manager is in use. */
167 Lisp_Object Vx_no_window_manager;
169 /* Search path for bitmap files. */
171 Lisp_Object Vx_bitmap_file_path;
173 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
175 Lisp_Object Vx_pixel_size_width_font_regexp;
177 /* Evaluate this expression to rebuild the section of syms_of_xfns
178 that initializes and staticpros the symbols declared below. Note
179 that Emacs 18 has a bug that keeps C-x C-e from being able to
180 evaluate this expression.
182 (progn
183 ;; Accumulate a list of the symbols we want to initialize from the
184 ;; declarations at the top of the file.
185 (goto-char (point-min))
186 (search-forward "/\*&&& symbols declared here &&&*\/\n")
187 (let (symbol-list)
188 (while (looking-at "Lisp_Object \\(Q[a-z_]+\\)")
189 (setq symbol-list
190 (cons (buffer-substring (match-beginning 1) (match-end 1))
191 symbol-list))
192 (forward-line 1))
193 (setq symbol-list (nreverse symbol-list))
194 ;; Delete the section of syms_of_... where we initialize the symbols.
195 (search-forward "\n /\*&&& init symbols here &&&*\/\n")
196 (let ((start (point)))
197 (while (looking-at "^ Q")
198 (forward-line 2))
199 (kill-region start (point)))
200 ;; Write a new symbol initialization section.
201 (while symbol-list
202 (insert (format " %s = intern (\"" (car symbol-list)))
203 (let ((start (point)))
204 (insert (substring (car symbol-list) 1))
205 (subst-char-in-region start (point) ?_ ?-))
206 (insert (format "\");\n staticpro (&%s);\n" (car symbol-list)))
207 (setq symbol-list (cdr symbol-list)))))
211 /*&&& symbols declared here &&&*/
212 Lisp_Object Qauto_raise;
213 Lisp_Object Qauto_lower;
214 Lisp_Object Qbar;
215 Lisp_Object Qborder_color;
216 Lisp_Object Qborder_width;
217 Lisp_Object Qbox;
218 Lisp_Object Qcursor_color;
219 Lisp_Object Qcursor_type;
220 Lisp_Object Qgeometry;
221 Lisp_Object Qicon_left;
222 Lisp_Object Qicon_top;
223 Lisp_Object Qicon_type;
224 Lisp_Object Qicon_name;
225 Lisp_Object Qinternal_border_width;
226 Lisp_Object Qleft;
227 Lisp_Object Qright;
228 Lisp_Object Qmouse_color;
229 Lisp_Object Qnone;
230 Lisp_Object Qouter_window_id;
231 Lisp_Object Qparent_id;
232 Lisp_Object Qscroll_bar_width;
233 Lisp_Object Qsuppress_icon;
234 extern Lisp_Object Qtop;
235 Lisp_Object Qundefined_color;
236 Lisp_Object Qvertical_scroll_bars;
237 Lisp_Object Qvisibility;
238 Lisp_Object Qwindow_id;
239 Lisp_Object Qx_frame_parameter;
240 Lisp_Object Qx_resource_name;
241 Lisp_Object Quser_position;
242 Lisp_Object Quser_size;
243 extern Lisp_Object Qdisplay;
244 Lisp_Object Qscroll_bar_foreground, Qscroll_bar_background;
245 Lisp_Object Qscreen_gamma, Qline_spacing, Qcenter;
246 Lisp_Object Qcompound_text;
248 /* The below are defined in frame.c. */
250 extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
251 extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
252 extern Lisp_Object Qtool_bar_lines;
254 extern Lisp_Object Vwindow_system_version;
256 Lisp_Object Qface_set_after_frame_default;
259 /* Error if we are not connected to X. */
261 void
262 check_x ()
264 if (! x_in_use)
265 error ("X windows are not in use or not initialized");
268 /* Nonzero if we can use mouse menus.
269 You should not call this unless HAVE_MENUS is defined. */
272 have_menus_p ()
274 return x_in_use;
277 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
278 and checking validity for X. */
280 FRAME_PTR
281 check_x_frame (frame)
282 Lisp_Object frame;
284 FRAME_PTR f;
286 if (NILP (frame))
287 frame = selected_frame;
288 CHECK_LIVE_FRAME (frame, 0);
289 f = XFRAME (frame);
290 if (! FRAME_X_P (f))
291 error ("Non-X frame used");
292 return f;
295 /* Let the user specify an X display with a frame.
296 nil stands for the selected frame--or, if that is not an X frame,
297 the first X display on the list. */
299 static struct x_display_info *
300 check_x_display_info (frame)
301 Lisp_Object frame;
303 struct x_display_info *dpyinfo = NULL;
305 if (NILP (frame))
307 struct frame *sf = XFRAME (selected_frame);
309 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
310 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
311 else if (x_display_list != 0)
312 dpyinfo = x_display_list;
313 else
314 error ("X windows are not in use or not initialized");
316 else if (STRINGP (frame))
317 dpyinfo = x_display_info_for_name (frame);
318 else
320 FRAME_PTR f;
322 CHECK_LIVE_FRAME (frame, 0);
323 f = XFRAME (frame);
324 if (! FRAME_X_P (f))
325 error ("Non-X frame used");
326 dpyinfo = FRAME_X_DISPLAY_INFO (f);
329 return dpyinfo;
333 /* Return the Emacs frame-object corresponding to an X window.
334 It could be the frame's main window or an icon window. */
336 /* This function can be called during GC, so use GC_xxx type test macros. */
338 struct frame *
339 x_window_to_frame (dpyinfo, wdesc)
340 struct x_display_info *dpyinfo;
341 int wdesc;
343 Lisp_Object tail, frame;
344 struct frame *f;
346 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
348 frame = XCAR (tail);
349 if (!GC_FRAMEP (frame))
350 continue;
351 f = XFRAME (frame);
352 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
353 continue;
354 if (f->output_data.x->busy_window == wdesc)
355 return f;
356 #ifdef USE_X_TOOLKIT
357 if ((f->output_data.x->edit_widget
358 && XtWindow (f->output_data.x->edit_widget) == wdesc)
359 /* A tooltip frame? */
360 || (!f->output_data.x->edit_widget
361 && FRAME_X_WINDOW (f) == wdesc)
362 || f->output_data.x->icon_desc == wdesc)
363 return f;
364 #else /* not USE_X_TOOLKIT */
365 if (FRAME_X_WINDOW (f) == wdesc
366 || f->output_data.x->icon_desc == wdesc)
367 return f;
368 #endif /* not USE_X_TOOLKIT */
370 return 0;
373 #ifdef USE_X_TOOLKIT
374 /* Like x_window_to_frame but also compares the window with the widget's
375 windows. */
377 struct frame *
378 x_any_window_to_frame (dpyinfo, wdesc)
379 struct x_display_info *dpyinfo;
380 int wdesc;
382 Lisp_Object tail, frame;
383 struct frame *f, *found;
384 struct x_output *x;
386 found = NULL;
387 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
389 frame = XCAR (tail);
390 if (!GC_FRAMEP (frame))
391 continue;
393 f = XFRAME (frame);
394 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
396 /* This frame matches if the window is any of its widgets. */
397 x = f->output_data.x;
398 if (x->busy_window == wdesc)
399 found = f;
400 else if (x->widget)
402 if (wdesc == XtWindow (x->widget)
403 || wdesc == XtWindow (x->column_widget)
404 || wdesc == XtWindow (x->edit_widget))
405 found = f;
406 /* Match if the window is this frame's menubar. */
407 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
408 found = f;
410 else if (FRAME_X_WINDOW (f) == wdesc)
411 /* A tooltip frame. */
412 found = f;
416 return found;
419 /* Likewise, but exclude the menu bar widget. */
421 struct frame *
422 x_non_menubar_window_to_frame (dpyinfo, wdesc)
423 struct x_display_info *dpyinfo;
424 int wdesc;
426 Lisp_Object tail, frame;
427 struct frame *f;
428 struct x_output *x;
430 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
432 frame = XCAR (tail);
433 if (!GC_FRAMEP (frame))
434 continue;
435 f = XFRAME (frame);
436 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
437 continue;
438 x = f->output_data.x;
439 /* This frame matches if the window is any of its widgets. */
440 if (x->busy_window == wdesc)
441 return f;
442 else if (x->widget)
444 if (wdesc == XtWindow (x->widget)
445 || wdesc == XtWindow (x->column_widget)
446 || wdesc == XtWindow (x->edit_widget))
447 return f;
449 else if (FRAME_X_WINDOW (f) == wdesc)
450 /* A tooltip frame. */
451 return f;
453 return 0;
456 /* Likewise, but consider only the menu bar widget. */
458 struct frame *
459 x_menubar_window_to_frame (dpyinfo, wdesc)
460 struct x_display_info *dpyinfo;
461 int wdesc;
463 Lisp_Object tail, frame;
464 struct frame *f;
465 struct x_output *x;
467 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
469 frame = XCAR (tail);
470 if (!GC_FRAMEP (frame))
471 continue;
472 f = XFRAME (frame);
473 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
474 continue;
475 x = f->output_data.x;
476 /* Match if the window is this frame's menubar. */
477 if (x->menubar_widget
478 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
479 return f;
481 return 0;
484 /* Return the frame whose principal (outermost) window is WDESC.
485 If WDESC is some other (smaller) window, we return 0. */
487 struct frame *
488 x_top_window_to_frame (dpyinfo, wdesc)
489 struct x_display_info *dpyinfo;
490 int wdesc;
492 Lisp_Object tail, frame;
493 struct frame *f;
494 struct x_output *x;
496 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
498 frame = XCAR (tail);
499 if (!GC_FRAMEP (frame))
500 continue;
501 f = XFRAME (frame);
502 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
503 continue;
504 x = f->output_data.x;
506 if (x->widget)
508 /* This frame matches if the window is its topmost widget. */
509 if (wdesc == XtWindow (x->widget))
510 return f;
511 #if 0 /* I don't know why it did this,
512 but it seems logically wrong,
513 and it causes trouble for MapNotify events. */
514 /* Match if the window is this frame's menubar. */
515 if (x->menubar_widget
516 && wdesc == XtWindow (x->menubar_widget))
517 return f;
518 #endif
520 else if (FRAME_X_WINDOW (f) == wdesc)
521 /* Tooltip frame. */
522 return f;
524 return 0;
526 #endif /* USE_X_TOOLKIT */
530 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
531 id, which is just an int that this section returns. Bitmaps are
532 reference counted so they can be shared among frames.
534 Bitmap indices are guaranteed to be > 0, so a negative number can
535 be used to indicate no bitmap.
537 If you use x_create_bitmap_from_data, then you must keep track of
538 the bitmaps yourself. That is, creating a bitmap from the same
539 data more than once will not be caught. */
542 /* Functions to access the contents of a bitmap, given an id. */
545 x_bitmap_height (f, id)
546 FRAME_PTR f;
547 int id;
549 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
553 x_bitmap_width (f, id)
554 FRAME_PTR f;
555 int id;
557 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
561 x_bitmap_pixmap (f, id)
562 FRAME_PTR f;
563 int id;
565 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
569 /* Allocate a new bitmap record. Returns index of new record. */
571 static int
572 x_allocate_bitmap_record (f)
573 FRAME_PTR f;
575 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
576 int i;
578 if (dpyinfo->bitmaps == NULL)
580 dpyinfo->bitmaps_size = 10;
581 dpyinfo->bitmaps
582 = (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
583 dpyinfo->bitmaps_last = 1;
584 return 1;
587 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
588 return ++dpyinfo->bitmaps_last;
590 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
591 if (dpyinfo->bitmaps[i].refcount == 0)
592 return i + 1;
594 dpyinfo->bitmaps_size *= 2;
595 dpyinfo->bitmaps
596 = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
597 dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
598 return ++dpyinfo->bitmaps_last;
601 /* Add one reference to the reference count of the bitmap with id ID. */
603 void
604 x_reference_bitmap (f, id)
605 FRAME_PTR f;
606 int id;
608 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
611 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
614 x_create_bitmap_from_data (f, bits, width, height)
615 struct frame *f;
616 char *bits;
617 unsigned int width, height;
619 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
620 Pixmap bitmap;
621 int id;
623 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
624 bits, width, height);
626 if (! bitmap)
627 return -1;
629 id = x_allocate_bitmap_record (f);
630 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
631 dpyinfo->bitmaps[id - 1].file = NULL;
632 dpyinfo->bitmaps[id - 1].refcount = 1;
633 dpyinfo->bitmaps[id - 1].depth = 1;
634 dpyinfo->bitmaps[id - 1].height = height;
635 dpyinfo->bitmaps[id - 1].width = width;
637 return id;
640 /* Create bitmap from file FILE for frame F. */
643 x_create_bitmap_from_file (f, file)
644 struct frame *f;
645 Lisp_Object file;
647 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
648 unsigned int width, height;
649 Pixmap bitmap;
650 int xhot, yhot, result, id;
651 Lisp_Object found;
652 int fd;
653 char *filename;
655 /* Look for an existing bitmap with the same name. */
656 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
658 if (dpyinfo->bitmaps[id].refcount
659 && dpyinfo->bitmaps[id].file
660 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
662 ++dpyinfo->bitmaps[id].refcount;
663 return id + 1;
667 /* Search bitmap-file-path for the file, if appropriate. */
668 fd = openp (Vx_bitmap_file_path, file, "", &found, 0);
669 if (fd < 0)
670 return -1;
671 emacs_close (fd);
673 filename = (char *) XSTRING (found)->data;
675 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
676 filename, &width, &height, &bitmap, &xhot, &yhot);
677 if (result != BitmapSuccess)
678 return -1;
680 id = x_allocate_bitmap_record (f);
681 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
682 dpyinfo->bitmaps[id - 1].refcount = 1;
683 dpyinfo->bitmaps[id - 1].file
684 = (char *) xmalloc (STRING_BYTES (XSTRING (file)) + 1);
685 dpyinfo->bitmaps[id - 1].depth = 1;
686 dpyinfo->bitmaps[id - 1].height = height;
687 dpyinfo->bitmaps[id - 1].width = width;
688 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
690 return id;
693 /* Remove reference to bitmap with id number ID. */
695 void
696 x_destroy_bitmap (f, id)
697 FRAME_PTR f;
698 int id;
700 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
702 if (id > 0)
704 --dpyinfo->bitmaps[id - 1].refcount;
705 if (dpyinfo->bitmaps[id - 1].refcount == 0)
707 BLOCK_INPUT;
708 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
709 if (dpyinfo->bitmaps[id - 1].file)
711 xfree (dpyinfo->bitmaps[id - 1].file);
712 dpyinfo->bitmaps[id - 1].file = NULL;
714 UNBLOCK_INPUT;
719 /* Free all the bitmaps for the display specified by DPYINFO. */
721 static void
722 x_destroy_all_bitmaps (dpyinfo)
723 struct x_display_info *dpyinfo;
725 int i;
726 for (i = 0; i < dpyinfo->bitmaps_last; i++)
727 if (dpyinfo->bitmaps[i].refcount > 0)
729 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
730 if (dpyinfo->bitmaps[i].file)
731 xfree (dpyinfo->bitmaps[i].file);
733 dpyinfo->bitmaps_last = 0;
736 /* Connect the frame-parameter names for X frames
737 to the ways of passing the parameter values to the window system.
739 The name of a parameter, as a Lisp symbol,
740 has an `x-frame-parameter' property which is an integer in Lisp
741 that is an index in this table. */
743 struct x_frame_parm_table
745 char *name;
746 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
749 static void x_change_window_heights P_ ((Lisp_Object, int));
750 static void x_disable_image P_ ((struct frame *, struct image *));
751 static void x_create_im P_ ((struct frame *));
752 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
753 static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
754 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
755 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
756 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
757 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
758 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
759 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
760 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
761 void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
762 void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
763 void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
764 Lisp_Object));
765 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
766 void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
767 void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
768 void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
769 Lisp_Object));
770 void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
771 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
772 void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
773 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
774 void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
775 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
776 void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
777 Lisp_Object));
778 void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
779 Lisp_Object));
780 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
781 Lisp_Object,
782 Lisp_Object,
783 char *, char *,
784 int));
785 static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
786 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
787 Lisp_Object));
788 static void init_color_table P_ ((void));
789 static void free_color_table P_ ((void));
790 static unsigned long *colors_in_color_table P_ ((int *n));
791 static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
792 static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
796 static struct x_frame_parm_table x_frame_parms[] =
798 "auto-raise", x_set_autoraise,
799 "auto-lower", x_set_autolower,
800 "background-color", x_set_background_color,
801 "border-color", x_set_border_color,
802 "border-width", x_set_border_width,
803 "cursor-color", x_set_cursor_color,
804 "cursor-type", x_set_cursor_type,
805 "font", x_set_font,
806 "foreground-color", x_set_foreground_color,
807 "icon-name", x_set_icon_name,
808 "icon-type", x_set_icon_type,
809 "internal-border-width", x_set_internal_border_width,
810 "menu-bar-lines", x_set_menu_bar_lines,
811 "mouse-color", x_set_mouse_color,
812 "name", x_explicitly_set_name,
813 "scroll-bar-width", x_set_scroll_bar_width,
814 "title", x_set_title,
815 "unsplittable", x_set_unsplittable,
816 "vertical-scroll-bars", x_set_vertical_scroll_bars,
817 "visibility", x_set_visibility,
818 "tool-bar-lines", x_set_tool_bar_lines,
819 "scroll-bar-foreground", x_set_scroll_bar_foreground,
820 "scroll-bar-background", x_set_scroll_bar_background,
821 "screen-gamma", x_set_screen_gamma,
822 "line-spacing", x_set_line_spacing
825 /* Attach the `x-frame-parameter' properties to
826 the Lisp symbol names of parameters relevant to X. */
828 void
829 init_x_parm_symbols ()
831 int i;
833 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
834 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
835 make_number (i));
838 /* Change the parameters of frame F as specified by ALIST.
839 If a parameter is not specially recognized, do nothing special;
840 otherwise call the `x_set_...' function for that parameter.
841 Except for certain geometry properties, always call store_frame_param
842 to store the new value in the parameter alist. */
844 void
845 x_set_frame_parameters (f, alist)
846 FRAME_PTR f;
847 Lisp_Object alist;
849 Lisp_Object tail;
851 /* If both of these parameters are present, it's more efficient to
852 set them both at once. So we wait until we've looked at the
853 entire list before we set them. */
854 int width, height;
856 /* Same here. */
857 Lisp_Object left, top;
859 /* Same with these. */
860 Lisp_Object icon_left, icon_top;
862 /* Record in these vectors all the parms specified. */
863 Lisp_Object *parms;
864 Lisp_Object *values;
865 int i, p;
866 int left_no_change = 0, top_no_change = 0;
867 int icon_left_no_change = 0, icon_top_no_change = 0;
869 struct gcpro gcpro1, gcpro2;
871 i = 0;
872 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
873 i++;
875 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
876 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
878 /* Extract parm names and values into those vectors. */
880 i = 0;
881 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
883 Lisp_Object elt;
885 elt = Fcar (tail);
886 parms[i] = Fcar (elt);
887 values[i] = Fcdr (elt);
888 i++;
890 /* TAIL and ALIST are not used again below here. */
891 alist = tail = Qnil;
893 GCPRO2 (*parms, *values);
894 gcpro1.nvars = i;
895 gcpro2.nvars = i;
897 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
898 because their values appear in VALUES and strings are not valid. */
899 top = left = Qunbound;
900 icon_left = icon_top = Qunbound;
902 /* Provide default values for HEIGHT and WIDTH. */
903 if (FRAME_NEW_WIDTH (f))
904 width = FRAME_NEW_WIDTH (f);
905 else
906 width = FRAME_WIDTH (f);
908 if (FRAME_NEW_HEIGHT (f))
909 height = FRAME_NEW_HEIGHT (f);
910 else
911 height = FRAME_HEIGHT (f);
913 /* Process foreground_color and background_color before anything else.
914 They are independent of other properties, but other properties (e.g.,
915 cursor_color) are dependent upon them. */
916 for (p = 0; p < i; p++)
918 Lisp_Object prop, val;
920 prop = parms[p];
921 val = values[p];
922 if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
924 register Lisp_Object param_index, old_value;
926 param_index = Fget (prop, Qx_frame_parameter);
927 old_value = get_frame_param (f, prop);
928 store_frame_param (f, prop, val);
929 if (NATNUMP (param_index)
930 && (XFASTINT (param_index)
931 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
932 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
936 /* Now process them in reverse of specified order. */
937 for (i--; i >= 0; i--)
939 Lisp_Object prop, val;
941 prop = parms[i];
942 val = values[i];
944 if (EQ (prop, Qwidth) && NUMBERP (val))
945 width = XFASTINT (val);
946 else if (EQ (prop, Qheight) && NUMBERP (val))
947 height = XFASTINT (val);
948 else if (EQ (prop, Qtop))
949 top = val;
950 else if (EQ (prop, Qleft))
951 left = val;
952 else if (EQ (prop, Qicon_top))
953 icon_top = val;
954 else if (EQ (prop, Qicon_left))
955 icon_left = val;
956 else if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
957 /* Processed above. */
958 continue;
959 else
961 register Lisp_Object param_index, old_value;
963 param_index = Fget (prop, Qx_frame_parameter);
964 old_value = get_frame_param (f, prop);
965 store_frame_param (f, prop, val);
966 if (NATNUMP (param_index)
967 && (XFASTINT (param_index)
968 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
969 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
973 /* Don't die if just one of these was set. */
974 if (EQ (left, Qunbound))
976 left_no_change = 1;
977 if (f->output_data.x->left_pos < 0)
978 left = Fcons (Qplus, Fcons (make_number (f->output_data.x->left_pos), Qnil));
979 else
980 XSETINT (left, f->output_data.x->left_pos);
982 if (EQ (top, Qunbound))
984 top_no_change = 1;
985 if (f->output_data.x->top_pos < 0)
986 top = Fcons (Qplus, Fcons (make_number (f->output_data.x->top_pos), Qnil));
987 else
988 XSETINT (top, f->output_data.x->top_pos);
991 /* If one of the icon positions was not set, preserve or default it. */
992 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
994 icon_left_no_change = 1;
995 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
996 if (NILP (icon_left))
997 XSETINT (icon_left, 0);
999 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
1001 icon_top_no_change = 1;
1002 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
1003 if (NILP (icon_top))
1004 XSETINT (icon_top, 0);
1007 /* Don't set these parameters unless they've been explicitly
1008 specified. The window might be mapped or resized while we're in
1009 this function, and we don't want to override that unless the lisp
1010 code has asked for it.
1012 Don't set these parameters unless they actually differ from the
1013 window's current parameters; the window may not actually exist
1014 yet. */
1016 Lisp_Object frame;
1018 check_frame_size (f, &height, &width);
1020 XSETFRAME (frame, f);
1022 if (width != FRAME_WIDTH (f)
1023 || height != FRAME_HEIGHT (f)
1024 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
1025 Fset_frame_size (frame, make_number (width), make_number (height));
1027 if ((!NILP (left) || !NILP (top))
1028 && ! (left_no_change && top_no_change)
1029 && ! (NUMBERP (left) && XINT (left) == f->output_data.x->left_pos
1030 && NUMBERP (top) && XINT (top) == f->output_data.x->top_pos))
1032 int leftpos = 0;
1033 int toppos = 0;
1035 /* Record the signs. */
1036 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
1037 if (EQ (left, Qminus))
1038 f->output_data.x->size_hint_flags |= XNegative;
1039 else if (INTEGERP (left))
1041 leftpos = XINT (left);
1042 if (leftpos < 0)
1043 f->output_data.x->size_hint_flags |= XNegative;
1045 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1046 && CONSP (XCDR (left))
1047 && INTEGERP (XCAR (XCDR (left))))
1049 leftpos = - XINT (XCAR (XCDR (left)));
1050 f->output_data.x->size_hint_flags |= XNegative;
1052 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1053 && CONSP (XCDR (left))
1054 && INTEGERP (XCAR (XCDR (left))))
1056 leftpos = XINT (XCAR (XCDR (left)));
1059 if (EQ (top, Qminus))
1060 f->output_data.x->size_hint_flags |= YNegative;
1061 else if (INTEGERP (top))
1063 toppos = XINT (top);
1064 if (toppos < 0)
1065 f->output_data.x->size_hint_flags |= YNegative;
1067 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1068 && CONSP (XCDR (top))
1069 && INTEGERP (XCAR (XCDR (top))))
1071 toppos = - XINT (XCAR (XCDR (top)));
1072 f->output_data.x->size_hint_flags |= YNegative;
1074 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1075 && CONSP (XCDR (top))
1076 && INTEGERP (XCAR (XCDR (top))))
1078 toppos = XINT (XCAR (XCDR (top)));
1082 /* Store the numeric value of the position. */
1083 f->output_data.x->top_pos = toppos;
1084 f->output_data.x->left_pos = leftpos;
1086 f->output_data.x->win_gravity = NorthWestGravity;
1088 /* Actually set that position, and convert to absolute. */
1089 x_set_offset (f, leftpos, toppos, -1);
1092 if ((!NILP (icon_left) || !NILP (icon_top))
1093 && ! (icon_left_no_change && icon_top_no_change))
1094 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1097 UNGCPRO;
1100 /* Store the screen positions of frame F into XPTR and YPTR.
1101 These are the positions of the containing window manager window,
1102 not Emacs's own window. */
1104 void
1105 x_real_positions (f, xptr, yptr)
1106 FRAME_PTR f;
1107 int *xptr, *yptr;
1109 int win_x, win_y;
1110 Window child;
1112 /* This is pretty gross, but seems to be the easiest way out of
1113 the problem that arises when restarting window-managers. */
1115 #ifdef USE_X_TOOLKIT
1116 Window outer = (f->output_data.x->widget
1117 ? XtWindow (f->output_data.x->widget)
1118 : FRAME_X_WINDOW (f));
1119 #else
1120 Window outer = f->output_data.x->window_desc;
1121 #endif
1122 Window tmp_root_window;
1123 Window *tmp_children;
1124 unsigned int tmp_nchildren;
1126 while (1)
1128 int count = x_catch_errors (FRAME_X_DISPLAY (f));
1129 Window outer_window;
1131 XQueryTree (FRAME_X_DISPLAY (f), outer, &tmp_root_window,
1132 &f->output_data.x->parent_desc,
1133 &tmp_children, &tmp_nchildren);
1134 XFree ((char *) tmp_children);
1136 win_x = win_y = 0;
1138 /* Find the position of the outside upper-left corner of
1139 the inner window, with respect to the outer window. */
1140 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
1141 outer_window = f->output_data.x->parent_desc;
1142 else
1143 outer_window = outer;
1145 XTranslateCoordinates (FRAME_X_DISPLAY (f),
1147 /* From-window, to-window. */
1148 outer_window,
1149 FRAME_X_DISPLAY_INFO (f)->root_window,
1151 /* From-position, to-position. */
1152 0, 0, &win_x, &win_y,
1154 /* Child of win. */
1155 &child);
1157 /* It is possible for the window returned by the XQueryNotify
1158 to become invalid by the time we call XTranslateCoordinates.
1159 That can happen when you restart some window managers.
1160 If so, we get an error in XTranslateCoordinates.
1161 Detect that and try the whole thing over. */
1162 if (! x_had_errors_p (FRAME_X_DISPLAY (f)))
1164 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1165 break;
1168 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1171 *xptr = win_x;
1172 *yptr = win_y;
1175 /* Insert a description of internally-recorded parameters of frame X
1176 into the parameter alist *ALISTPTR that is to be given to the user.
1177 Only parameters that are specific to the X window system
1178 and whose values are not correctly recorded in the frame's
1179 param_alist need to be considered here. */
1181 void
1182 x_report_frame_params (f, alistptr)
1183 struct frame *f;
1184 Lisp_Object *alistptr;
1186 char buf[16];
1187 Lisp_Object tem;
1189 /* Represent negative positions (off the top or left screen edge)
1190 in a way that Fmodify_frame_parameters will understand correctly. */
1191 XSETINT (tem, f->output_data.x->left_pos);
1192 if (f->output_data.x->left_pos >= 0)
1193 store_in_alist (alistptr, Qleft, tem);
1194 else
1195 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1197 XSETINT (tem, f->output_data.x->top_pos);
1198 if (f->output_data.x->top_pos >= 0)
1199 store_in_alist (alistptr, Qtop, tem);
1200 else
1201 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1203 store_in_alist (alistptr, Qborder_width,
1204 make_number (f->output_data.x->border_width));
1205 store_in_alist (alistptr, Qinternal_border_width,
1206 make_number (f->output_data.x->internal_border_width));
1207 sprintf (buf, "%ld", (long) FRAME_X_WINDOW (f));
1208 store_in_alist (alistptr, Qwindow_id,
1209 build_string (buf));
1210 #ifdef USE_X_TOOLKIT
1211 /* Tooltip frame may not have this widget. */
1212 if (f->output_data.x->widget)
1213 #endif
1214 sprintf (buf, "%ld", (long) FRAME_OUTER_WINDOW (f));
1215 store_in_alist (alistptr, Qouter_window_id,
1216 build_string (buf));
1217 store_in_alist (alistptr, Qicon_name, f->icon_name);
1218 FRAME_SAMPLE_VISIBILITY (f);
1219 store_in_alist (alistptr, Qvisibility,
1220 (FRAME_VISIBLE_P (f) ? Qt
1221 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1222 store_in_alist (alistptr, Qdisplay,
1223 XCAR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
1225 if (f->output_data.x->parent_desc == FRAME_X_DISPLAY_INFO (f)->root_window)
1226 tem = Qnil;
1227 else
1228 XSETFASTINT (tem, f->output_data.x->parent_desc);
1229 store_in_alist (alistptr, Qparent_id, tem);
1234 /* Gamma-correct COLOR on frame F. */
1236 void
1237 gamma_correct (f, color)
1238 struct frame *f;
1239 XColor *color;
1241 if (f->gamma)
1243 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
1244 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
1245 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
1250 /* Decide if color named COLOR_NAME is valid for use on frame F. If
1251 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
1252 allocate the color. Value is zero if COLOR_NAME is invalid, or
1253 no color could be allocated. */
1256 x_defined_color (f, color_name, color, alloc_p)
1257 struct frame *f;
1258 char *color_name;
1259 XColor *color;
1260 int alloc_p;
1262 int success_p;
1263 Display *dpy = FRAME_X_DISPLAY (f);
1264 Colormap cmap = FRAME_X_COLORMAP (f);
1266 BLOCK_INPUT;
1267 success_p = XParseColor (dpy, cmap, color_name, color);
1268 if (success_p && alloc_p)
1269 success_p = x_alloc_nearest_color (f, cmap, color);
1270 UNBLOCK_INPUT;
1272 return success_p;
1276 /* Return the pixel color value for color COLOR_NAME on frame F. If F
1277 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
1278 Signal an error if color can't be allocated. */
1281 x_decode_color (f, color_name, mono_color)
1282 FRAME_PTR f;
1283 Lisp_Object color_name;
1284 int mono_color;
1286 XColor cdef;
1288 CHECK_STRING (color_name, 0);
1290 #if 0 /* Don't do this. It's wrong when we're not using the default
1291 colormap, it makes freeing difficult, and it's probably not
1292 an important optimization. */
1293 if (strcmp (XSTRING (color_name)->data, "black") == 0)
1294 return BLACK_PIX_DEFAULT (f);
1295 else if (strcmp (XSTRING (color_name)->data, "white") == 0)
1296 return WHITE_PIX_DEFAULT (f);
1297 #endif
1299 /* Return MONO_COLOR for monochrome frames. */
1300 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
1301 return mono_color;
1303 /* x_defined_color is responsible for coping with failures
1304 by looking for a near-miss. */
1305 if (x_defined_color (f, XSTRING (color_name)->data, &cdef, 1))
1306 return cdef.pixel;
1308 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
1309 Fcons (color_name, Qnil)));
1310 return 0;
1315 /* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1316 the previous value of that parameter, NEW_VALUE is the new value. */
1318 static void
1319 x_set_line_spacing (f, new_value, old_value)
1320 struct frame *f;
1321 Lisp_Object new_value, old_value;
1323 if (NILP (new_value))
1324 f->extra_line_spacing = 0;
1325 else if (NATNUMP (new_value))
1326 f->extra_line_spacing = XFASTINT (new_value);
1327 else
1328 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
1329 Fcons (new_value, Qnil)));
1330 if (FRAME_VISIBLE_P (f))
1331 redraw_frame (f);
1335 /* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
1336 the previous value of that parameter, NEW_VALUE is the new value. */
1338 static void
1339 x_set_screen_gamma (f, new_value, old_value)
1340 struct frame *f;
1341 Lisp_Object new_value, old_value;
1343 if (NILP (new_value))
1344 f->gamma = 0;
1345 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1346 /* The value 0.4545 is the normal viewing gamma. */
1347 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1348 else
1349 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
1350 Fcons (new_value, Qnil)));
1352 clear_face_cache (0);
1356 /* Functions called only from `x_set_frame_param'
1357 to set individual parameters.
1359 If FRAME_X_WINDOW (f) is 0,
1360 the frame is being created and its X-window does not exist yet.
1361 In that case, just record the parameter's new value
1362 in the standard place; do not attempt to change the window. */
1364 void
1365 x_set_foreground_color (f, arg, oldval)
1366 struct frame *f;
1367 Lisp_Object arg, oldval;
1369 unsigned long pixel
1370 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1372 unload_color (f, f->output_data.x->foreground_pixel);
1373 f->output_data.x->foreground_pixel = pixel;
1375 if (FRAME_X_WINDOW (f) != 0)
1377 BLOCK_INPUT;
1378 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1379 f->output_data.x->foreground_pixel);
1380 XSetBackground (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
1381 f->output_data.x->foreground_pixel);
1382 UNBLOCK_INPUT;
1383 update_face_from_frame_parameter (f, Qforeground_color, arg);
1384 if (FRAME_VISIBLE_P (f))
1385 redraw_frame (f);
1389 void
1390 x_set_background_color (f, arg, oldval)
1391 struct frame *f;
1392 Lisp_Object arg, oldval;
1394 unsigned long pixel
1395 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1397 unload_color (f, f->output_data.x->background_pixel);
1398 f->output_data.x->background_pixel = pixel;
1400 if (FRAME_X_WINDOW (f) != 0)
1402 BLOCK_INPUT;
1403 /* The main frame area. */
1404 XSetBackground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1405 f->output_data.x->background_pixel);
1406 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
1407 f->output_data.x->background_pixel);
1408 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
1409 f->output_data.x->background_pixel);
1410 XSetWindowBackground (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1411 f->output_data.x->background_pixel);
1413 Lisp_Object bar;
1414 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar);
1415 bar = XSCROLL_BAR (bar)->next)
1416 XSetWindowBackground (FRAME_X_DISPLAY (f),
1417 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
1418 f->output_data.x->background_pixel);
1420 UNBLOCK_INPUT;
1422 update_face_from_frame_parameter (f, Qbackground_color, arg);
1424 if (FRAME_VISIBLE_P (f))
1425 redraw_frame (f);
1429 void
1430 x_set_mouse_color (f, arg, oldval)
1431 struct frame *f;
1432 Lisp_Object arg, oldval;
1434 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
1435 Cursor busy_cursor, horizontal_drag_cursor;
1436 int count;
1437 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1438 unsigned long mask_color = f->output_data.x->background_pixel;
1440 /* Don't let pointers be invisible. */
1441 if (mask_color == pixel
1442 && mask_color == f->output_data.x->background_pixel)
1443 pixel = f->output_data.x->foreground_pixel;
1445 unload_color (f, f->output_data.x->mouse_pixel);
1446 f->output_data.x->mouse_pixel = pixel;
1448 BLOCK_INPUT;
1450 /* It's not okay to crash if the user selects a screwy cursor. */
1451 count = x_catch_errors (FRAME_X_DISPLAY (f));
1453 if (!EQ (Qnil, Vx_pointer_shape))
1455 CHECK_NUMBER (Vx_pointer_shape, 0);
1456 cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XINT (Vx_pointer_shape));
1458 else
1459 cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_xterm);
1460 x_check_errors (FRAME_X_DISPLAY (f), "bad text pointer cursor: %s");
1462 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1464 CHECK_NUMBER (Vx_nontext_pointer_shape, 0);
1465 nontext_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f),
1466 XINT (Vx_nontext_pointer_shape));
1468 else
1469 nontext_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_left_ptr);
1470 x_check_errors (FRAME_X_DISPLAY (f), "bad nontext pointer cursor: %s");
1472 if (!EQ (Qnil, Vx_busy_pointer_shape))
1474 CHECK_NUMBER (Vx_busy_pointer_shape, 0);
1475 busy_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f),
1476 XINT (Vx_busy_pointer_shape));
1478 else
1479 busy_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_watch);
1480 x_check_errors (FRAME_X_DISPLAY (f), "bad busy pointer cursor: %s");
1482 x_check_errors (FRAME_X_DISPLAY (f), "bad nontext pointer cursor: %s");
1483 if (!EQ (Qnil, Vx_mode_pointer_shape))
1485 CHECK_NUMBER (Vx_mode_pointer_shape, 0);
1486 mode_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f),
1487 XINT (Vx_mode_pointer_shape));
1489 else
1490 mode_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_xterm);
1491 x_check_errors (FRAME_X_DISPLAY (f), "bad modeline pointer cursor: %s");
1493 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1495 CHECK_NUMBER (Vx_sensitive_text_pointer_shape, 0);
1496 cross_cursor
1497 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1498 XINT (Vx_sensitive_text_pointer_shape));
1500 else
1501 cross_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_crosshair);
1503 if (!NILP (Vx_window_horizontal_drag_shape))
1505 CHECK_NUMBER (Vx_window_horizontal_drag_shape, 0);
1506 horizontal_drag_cursor
1507 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1508 XINT (Vx_window_horizontal_drag_shape));
1510 else
1511 horizontal_drag_cursor
1512 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1514 /* Check and report errors with the above calls. */
1515 x_check_errors (FRAME_X_DISPLAY (f), "can't set cursor shape: %s");
1516 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1519 XColor fore_color, back_color;
1521 fore_color.pixel = f->output_data.x->mouse_pixel;
1522 x_query_color (f, &fore_color);
1523 back_color.pixel = mask_color;
1524 x_query_color (f, &back_color);
1526 XRecolorCursor (FRAME_X_DISPLAY (f), cursor,
1527 &fore_color, &back_color);
1528 XRecolorCursor (FRAME_X_DISPLAY (f), nontext_cursor,
1529 &fore_color, &back_color);
1530 XRecolorCursor (FRAME_X_DISPLAY (f), mode_cursor,
1531 &fore_color, &back_color);
1532 XRecolorCursor (FRAME_X_DISPLAY (f), cross_cursor,
1533 &fore_color, &back_color);
1534 XRecolorCursor (FRAME_X_DISPLAY (f), busy_cursor,
1535 &fore_color, &back_color);
1536 XRecolorCursor (FRAME_X_DISPLAY (f), horizontal_drag_cursor,
1537 &fore_color, &back_color);
1540 if (FRAME_X_WINDOW (f) != 0)
1541 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
1543 if (cursor != f->output_data.x->text_cursor
1544 && f->output_data.x->text_cursor != 0)
1545 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
1546 f->output_data.x->text_cursor = cursor;
1548 if (nontext_cursor != f->output_data.x->nontext_cursor
1549 && f->output_data.x->nontext_cursor != 0)
1550 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
1551 f->output_data.x->nontext_cursor = nontext_cursor;
1553 if (busy_cursor != f->output_data.x->busy_cursor
1554 && f->output_data.x->busy_cursor != 0)
1555 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->busy_cursor);
1556 f->output_data.x->busy_cursor = busy_cursor;
1558 if (mode_cursor != f->output_data.x->modeline_cursor
1559 && f->output_data.x->modeline_cursor != 0)
1560 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
1561 f->output_data.x->modeline_cursor = mode_cursor;
1563 if (cross_cursor != f->output_data.x->cross_cursor
1564 && f->output_data.x->cross_cursor != 0)
1565 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->cross_cursor);
1566 f->output_data.x->cross_cursor = cross_cursor;
1568 if (horizontal_drag_cursor != f->output_data.x->horizontal_drag_cursor
1569 && f->output_data.x->horizontal_drag_cursor != 0)
1570 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
1571 f->output_data.x->horizontal_drag_cursor = horizontal_drag_cursor;
1573 XFlush (FRAME_X_DISPLAY (f));
1574 UNBLOCK_INPUT;
1576 update_face_from_frame_parameter (f, Qmouse_color, arg);
1579 void
1580 x_set_cursor_color (f, arg, oldval)
1581 struct frame *f;
1582 Lisp_Object arg, oldval;
1584 unsigned long fore_pixel, pixel;
1585 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1587 if (!NILP (Vx_cursor_fore_pixel))
1589 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1590 WHITE_PIX_DEFAULT (f));
1591 fore_pixel_allocated_p = 1;
1593 else
1594 fore_pixel = f->output_data.x->background_pixel;
1596 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1597 pixel_allocated_p = 1;
1599 /* Make sure that the cursor color differs from the background color. */
1600 if (pixel == f->output_data.x->background_pixel)
1602 if (pixel_allocated_p)
1604 x_free_colors (f, &pixel, 1);
1605 pixel_allocated_p = 0;
1608 pixel = f->output_data.x->mouse_pixel;
1609 if (pixel == fore_pixel)
1611 if (fore_pixel_allocated_p)
1613 x_free_colors (f, &fore_pixel, 1);
1614 fore_pixel_allocated_p = 0;
1616 fore_pixel = f->output_data.x->background_pixel;
1620 unload_color (f, f->output_data.x->cursor_foreground_pixel);
1621 if (!fore_pixel_allocated_p)
1622 fore_pixel = x_copy_color (f, fore_pixel);
1623 f->output_data.x->cursor_foreground_pixel = fore_pixel;
1625 unload_color (f, f->output_data.x->cursor_pixel);
1626 if (!pixel_allocated_p)
1627 pixel = x_copy_color (f, pixel);
1628 f->output_data.x->cursor_pixel = pixel;
1630 if (FRAME_X_WINDOW (f) != 0)
1632 BLOCK_INPUT;
1633 XSetBackground (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
1634 f->output_data.x->cursor_pixel);
1635 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
1636 fore_pixel);
1637 UNBLOCK_INPUT;
1639 if (FRAME_VISIBLE_P (f))
1641 x_update_cursor (f, 0);
1642 x_update_cursor (f, 1);
1646 update_face_from_frame_parameter (f, Qcursor_color, arg);
1649 /* Set the border-color of frame F to value described by ARG.
1650 ARG can be a string naming a color.
1651 The border-color is used for the border that is drawn by the X server.
1652 Note that this does not fully take effect if done before
1653 F has an x-window; it must be redone when the window is created.
1655 Note: this is done in two routines because of the way X10 works.
1657 Note: under X11, this is normally the province of the window manager,
1658 and so emacs' border colors may be overridden. */
1660 void
1661 x_set_border_color (f, arg, oldval)
1662 struct frame *f;
1663 Lisp_Object arg, oldval;
1665 int pix;
1667 CHECK_STRING (arg, 0);
1668 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1669 x_set_border_pixel (f, pix);
1670 update_face_from_frame_parameter (f, Qborder_color, arg);
1673 /* Set the border-color of frame F to pixel value PIX.
1674 Note that this does not fully take effect if done before
1675 F has an x-window. */
1677 void
1678 x_set_border_pixel (f, pix)
1679 struct frame *f;
1680 int pix;
1682 unload_color (f, f->output_data.x->border_pixel);
1683 f->output_data.x->border_pixel = pix;
1685 if (FRAME_X_WINDOW (f) != 0 && f->output_data.x->border_width > 0)
1687 BLOCK_INPUT;
1688 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1689 (unsigned long)pix);
1690 UNBLOCK_INPUT;
1692 if (FRAME_VISIBLE_P (f))
1693 redraw_frame (f);
1698 /* Value is the internal representation of the specified cursor type
1699 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
1700 of the bar cursor. */
1702 enum text_cursor_kinds
1703 x_specified_cursor_type (arg, width)
1704 Lisp_Object arg;
1705 int *width;
1707 enum text_cursor_kinds type;
1709 if (EQ (arg, Qbar))
1711 type = BAR_CURSOR;
1712 *width = 2;
1714 else if (CONSP (arg)
1715 && EQ (XCAR (arg), Qbar)
1716 && INTEGERP (XCDR (arg))
1717 && XINT (XCDR (arg)) >= 0)
1719 type = BAR_CURSOR;
1720 *width = XINT (XCDR (arg));
1722 else if (NILP (arg))
1723 type = NO_CURSOR;
1724 else
1725 /* Treat anything unknown as "box cursor".
1726 It was bad to signal an error; people have trouble fixing
1727 .Xdefaults with Emacs, when it has something bad in it. */
1728 type = FILLED_BOX_CURSOR;
1730 return type;
1733 void
1734 x_set_cursor_type (f, arg, oldval)
1735 FRAME_PTR f;
1736 Lisp_Object arg, oldval;
1738 int width;
1740 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
1741 f->output_data.x->cursor_width = width;
1743 /* Make sure the cursor gets redrawn. This is overkill, but how
1744 often do people change cursor types? */
1745 update_mode_lines++;
1748 void
1749 x_set_icon_type (f, arg, oldval)
1750 struct frame *f;
1751 Lisp_Object arg, oldval;
1753 int result;
1755 if (STRINGP (arg))
1757 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1758 return;
1760 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1761 return;
1763 BLOCK_INPUT;
1764 if (NILP (arg))
1765 result = x_text_icon (f,
1766 (char *) XSTRING ((!NILP (f->icon_name)
1767 ? f->icon_name
1768 : f->name))->data);
1769 else
1770 result = x_bitmap_icon (f, arg);
1772 if (result)
1774 UNBLOCK_INPUT;
1775 error ("No icon window available");
1778 XFlush (FRAME_X_DISPLAY (f));
1779 UNBLOCK_INPUT;
1782 /* Return non-nil if frame F wants a bitmap icon. */
1784 Lisp_Object
1785 x_icon_type (f)
1786 FRAME_PTR f;
1788 Lisp_Object tem;
1790 tem = assq_no_quit (Qicon_type, f->param_alist);
1791 if (CONSP (tem))
1792 return XCDR (tem);
1793 else
1794 return Qnil;
1797 void
1798 x_set_icon_name (f, arg, oldval)
1799 struct frame *f;
1800 Lisp_Object arg, oldval;
1802 int result;
1804 if (STRINGP (arg))
1806 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1807 return;
1809 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1810 return;
1812 f->icon_name = arg;
1814 if (f->output_data.x->icon_bitmap != 0)
1815 return;
1817 BLOCK_INPUT;
1819 result = x_text_icon (f,
1820 (char *) XSTRING ((!NILP (f->icon_name)
1821 ? f->icon_name
1822 : !NILP (f->title)
1823 ? f->title
1824 : f->name))->data);
1826 if (result)
1828 UNBLOCK_INPUT;
1829 error ("No icon window available");
1832 XFlush (FRAME_X_DISPLAY (f));
1833 UNBLOCK_INPUT;
1836 void
1837 x_set_font (f, arg, oldval)
1838 struct frame *f;
1839 Lisp_Object arg, oldval;
1841 Lisp_Object result;
1842 Lisp_Object fontset_name;
1843 Lisp_Object frame;
1845 CHECK_STRING (arg, 1);
1847 fontset_name = Fquery_fontset (arg, Qnil);
1849 BLOCK_INPUT;
1850 result = (STRINGP (fontset_name)
1851 ? x_new_fontset (f, XSTRING (fontset_name)->data)
1852 : x_new_font (f, XSTRING (arg)->data));
1853 UNBLOCK_INPUT;
1855 if (EQ (result, Qnil))
1856 error ("Font `%s' is not defined", XSTRING (arg)->data);
1857 else if (EQ (result, Qt))
1858 error ("The characters of the given font have varying widths");
1859 else if (STRINGP (result))
1861 store_frame_param (f, Qfont, result);
1862 recompute_basic_faces (f);
1864 else
1865 abort ();
1867 do_pending_window_change (0);
1869 /* Don't call `face-set-after-frame-default' when faces haven't been
1870 initialized yet. This is the case when called from
1871 Fx_create_frame. In that case, the X widget or window doesn't
1872 exist either, and we can end up in x_report_frame_params with a
1873 null widget which gives a segfault. */
1874 if (FRAME_FACE_CACHE (f))
1876 XSETFRAME (frame, f);
1877 call1 (Qface_set_after_frame_default, frame);
1881 void
1882 x_set_border_width (f, arg, oldval)
1883 struct frame *f;
1884 Lisp_Object arg, oldval;
1886 CHECK_NUMBER (arg, 0);
1888 if (XINT (arg) == f->output_data.x->border_width)
1889 return;
1891 if (FRAME_X_WINDOW (f) != 0)
1892 error ("Cannot change the border width of a window");
1894 f->output_data.x->border_width = XINT (arg);
1897 void
1898 x_set_internal_border_width (f, arg, oldval)
1899 struct frame *f;
1900 Lisp_Object arg, oldval;
1902 int old = f->output_data.x->internal_border_width;
1904 CHECK_NUMBER (arg, 0);
1905 f->output_data.x->internal_border_width = XINT (arg);
1906 if (f->output_data.x->internal_border_width < 0)
1907 f->output_data.x->internal_border_width = 0;
1909 #ifdef USE_X_TOOLKIT
1910 if (f->output_data.x->edit_widget)
1911 widget_store_internal_border (f->output_data.x->edit_widget);
1912 #endif
1914 if (f->output_data.x->internal_border_width == old)
1915 return;
1917 if (FRAME_X_WINDOW (f) != 0)
1919 x_set_window_size (f, 0, f->width, f->height);
1920 SET_FRAME_GARBAGED (f);
1921 do_pending_window_change (0);
1925 void
1926 x_set_visibility (f, value, oldval)
1927 struct frame *f;
1928 Lisp_Object value, oldval;
1930 Lisp_Object frame;
1931 XSETFRAME (frame, f);
1933 if (NILP (value))
1934 Fmake_frame_invisible (frame, Qt);
1935 else if (EQ (value, Qicon))
1936 Ficonify_frame (frame);
1937 else
1938 Fmake_frame_visible (frame);
1942 /* Change window heights in windows rooted in WINDOW by N lines. */
1944 static void
1945 x_change_window_heights (window, n)
1946 Lisp_Object window;
1947 int n;
1949 struct window *w = XWINDOW (window);
1951 XSETFASTINT (w->top, XFASTINT (w->top) + n);
1952 XSETFASTINT (w->height, XFASTINT (w->height) - n);
1954 if (INTEGERP (w->orig_top))
1955 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
1956 if (INTEGERP (w->orig_height))
1957 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
1959 /* Handle just the top child in a vertical split. */
1960 if (!NILP (w->vchild))
1961 x_change_window_heights (w->vchild, n);
1963 /* Adjust all children in a horizontal split. */
1964 for (window = w->hchild; !NILP (window); window = w->next)
1966 w = XWINDOW (window);
1967 x_change_window_heights (window, n);
1971 void
1972 x_set_menu_bar_lines (f, value, oldval)
1973 struct frame *f;
1974 Lisp_Object value, oldval;
1976 int nlines;
1977 #ifndef USE_X_TOOLKIT
1978 int olines = FRAME_MENU_BAR_LINES (f);
1979 #endif
1981 /* Right now, menu bars don't work properly in minibuf-only frames;
1982 most of the commands try to apply themselves to the minibuffer
1983 frame itself, and get an error because you can't switch buffers
1984 in or split the minibuffer window. */
1985 if (FRAME_MINIBUF_ONLY_P (f))
1986 return;
1988 if (INTEGERP (value))
1989 nlines = XINT (value);
1990 else
1991 nlines = 0;
1993 /* Make sure we redisplay all windows in this frame. */
1994 windows_or_buffers_changed++;
1996 #ifdef USE_X_TOOLKIT
1997 FRAME_MENU_BAR_LINES (f) = 0;
1998 if (nlines)
2000 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2001 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
2002 /* Make sure next redisplay shows the menu bar. */
2003 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
2005 else
2007 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2008 free_frame_menubar (f);
2009 FRAME_EXTERNAL_MENU_BAR (f) = 0;
2010 if (FRAME_X_P (f))
2011 f->output_data.x->menubar_widget = 0;
2013 #else /* not USE_X_TOOLKIT */
2014 FRAME_MENU_BAR_LINES (f) = nlines;
2015 x_change_window_heights (f->root_window, nlines - olines);
2016 #endif /* not USE_X_TOOLKIT */
2017 adjust_glyphs (f);
2021 /* Set the number of lines used for the tool bar of frame F to VALUE.
2022 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2023 is the old number of tool bar lines. This function changes the
2024 height of all windows on frame F to match the new tool bar height.
2025 The frame's height doesn't change. */
2027 void
2028 x_set_tool_bar_lines (f, value, oldval)
2029 struct frame *f;
2030 Lisp_Object value, oldval;
2032 int delta, nlines, root_height;
2033 Lisp_Object root_window;
2035 /* Use VALUE only if an integer >= 0. */
2036 if (INTEGERP (value) && XINT (value) >= 0)
2037 nlines = XFASTINT (value);
2038 else
2039 nlines = 0;
2041 /* Make sure we redisplay all windows in this frame. */
2042 ++windows_or_buffers_changed;
2044 delta = nlines - FRAME_TOOL_BAR_LINES (f);
2046 /* Don't resize the tool-bar to more than we have room for. */
2047 root_window = FRAME_ROOT_WINDOW (f);
2048 root_height = XINT (XWINDOW (root_window)->height);
2049 if (root_height - delta < 1)
2051 delta = root_height - 1;
2052 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2055 FRAME_TOOL_BAR_LINES (f) = nlines;
2056 x_change_window_heights (root_window, delta);
2057 adjust_glyphs (f);
2059 /* We also have to make sure that the internal border at the top of
2060 the frame, below the menu bar or tool bar, is redrawn when the
2061 tool bar disappears. This is so because the internal border is
2062 below the tool bar if one is displayed, but is below the menu bar
2063 if there isn't a tool bar. The tool bar draws into the area
2064 below the menu bar. */
2065 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2067 updating_frame = f;
2068 clear_frame ();
2069 clear_current_matrices (f);
2070 updating_frame = NULL;
2073 /* If the tool bar gets smaller, the internal border below it
2074 has to be cleared. It was formerly part of the display
2075 of the larger tool bar, and updating windows won't clear it. */
2076 if (delta < 0)
2078 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2079 int width = PIXEL_WIDTH (f);
2080 int y = nlines * CANON_Y_UNIT (f);
2082 BLOCK_INPUT;
2083 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2084 0, y, width, height, False);
2085 UNBLOCK_INPUT;
2090 /* Set the foreground color for scroll bars on frame F to VALUE.
2091 VALUE should be a string, a color name. If it isn't a string or
2092 isn't a valid color name, do nothing. OLDVAL is the old value of
2093 the frame parameter. */
2095 void
2096 x_set_scroll_bar_foreground (f, value, oldval)
2097 struct frame *f;
2098 Lisp_Object value, oldval;
2100 unsigned long pixel;
2102 if (STRINGP (value))
2103 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
2104 else
2105 pixel = -1;
2107 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
2108 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
2110 f->output_data.x->scroll_bar_foreground_pixel = pixel;
2111 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2113 /* Remove all scroll bars because they have wrong colors. */
2114 if (condemn_scroll_bars_hook)
2115 (*condemn_scroll_bars_hook) (f);
2116 if (judge_scroll_bars_hook)
2117 (*judge_scroll_bars_hook) (f);
2119 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
2120 redraw_frame (f);
2125 /* Set the background color for scroll bars on frame F to VALUE VALUE
2126 should be a string, a color name. If it isn't a string or isn't a
2127 valid color name, do nothing. OLDVAL is the old value of the frame
2128 parameter. */
2130 void
2131 x_set_scroll_bar_background (f, value, oldval)
2132 struct frame *f;
2133 Lisp_Object value, oldval;
2135 unsigned long pixel;
2137 if (STRINGP (value))
2138 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
2139 else
2140 pixel = -1;
2142 if (f->output_data.x->scroll_bar_background_pixel != -1)
2143 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
2145 f->output_data.x->scroll_bar_background_pixel = pixel;
2146 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2148 /* Remove all scroll bars because they have wrong colors. */
2149 if (condemn_scroll_bars_hook)
2150 (*condemn_scroll_bars_hook) (f);
2151 if (judge_scroll_bars_hook)
2152 (*judge_scroll_bars_hook) (f);
2154 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
2155 redraw_frame (f);
2160 /* Encode Lisp string STRING as a text in a format appropriate for
2161 XICCC (X Inter Client Communication Conventions).
2163 If STRING contains only ASCII characters, do no conversion and
2164 return the string data of STRING. Otherwise, encode the text by
2165 CODING_SYSTEM, and return a newly allocated memory area which
2166 should be freed by `xfree' by a caller.
2168 Store the byte length of resulting text in *TEXT_BYTES.
2170 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
2171 which means that the `encoding' of the result can be `STRING'.
2172 Otherwise store 0 in *STRINGP, which means that the `encoding' of
2173 the result should be `COMPOUND_TEXT'. */
2175 unsigned char *
2176 x_encode_text (string, coding_system, text_bytes, stringp)
2177 Lisp_Object string, coding_system;
2178 int *text_bytes, *stringp;
2180 unsigned char *str = XSTRING (string)->data;
2181 int chars = XSTRING (string)->size;
2182 int bytes = STRING_BYTES (XSTRING (string));
2183 int charset_info;
2184 int bufsize;
2185 unsigned char *buf;
2186 struct coding_system coding;
2188 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
2189 if (charset_info == 0)
2191 /* No multibyte character in OBJ. We need not encode it. */
2192 *text_bytes = bytes;
2193 *stringp = 1;
2194 return str;
2197 setup_coding_system (coding_system, &coding);
2198 coding.src_multibyte = 1;
2199 coding.dst_multibyte = 0;
2200 coding.mode |= CODING_MODE_LAST_BLOCK;
2201 if (coding.type == coding_type_iso2022)
2202 coding.flags |= CODING_FLAG_ISO_SAFE;
2203 /* We suppress producing escape sequences for composition. */
2204 coding.composing = COMPOSITION_DISABLED;
2205 bufsize = encoding_buffer_size (&coding, bytes);
2206 buf = (unsigned char *) xmalloc (bufsize);
2207 encode_coding (&coding, str, buf, bytes, bufsize);
2208 *text_bytes = coding.produced;
2209 *stringp = (charset_info == 1 || !EQ (coding_system, Qcompound_text));
2210 return buf;
2214 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
2215 x_id_name.
2217 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2218 name; if NAME is a string, set F's name to NAME and set
2219 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2221 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2222 suggesting a new name, which lisp code should override; if
2223 F->explicit_name is set, ignore the new name; otherwise, set it. */
2225 void
2226 x_set_name (f, name, explicit)
2227 struct frame *f;
2228 Lisp_Object name;
2229 int explicit;
2231 /* Make sure that requests from lisp code override requests from
2232 Emacs redisplay code. */
2233 if (explicit)
2235 /* If we're switching from explicit to implicit, we had better
2236 update the mode lines and thereby update the title. */
2237 if (f->explicit_name && NILP (name))
2238 update_mode_lines = 1;
2240 f->explicit_name = ! NILP (name);
2242 else if (f->explicit_name)
2243 return;
2245 /* If NAME is nil, set the name to the x_id_name. */
2246 if (NILP (name))
2248 /* Check for no change needed in this very common case
2249 before we do any consing. */
2250 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
2251 XSTRING (f->name)->data))
2252 return;
2253 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
2255 else
2256 CHECK_STRING (name, 0);
2258 /* Don't change the name if it's already NAME. */
2259 if (! NILP (Fstring_equal (name, f->name)))
2260 return;
2262 f->name = name;
2264 /* For setting the frame title, the title parameter should override
2265 the name parameter. */
2266 if (! NILP (f->title))
2267 name = f->title;
2269 if (FRAME_X_WINDOW (f))
2271 BLOCK_INPUT;
2272 #ifdef HAVE_X11R4
2274 XTextProperty text, icon;
2275 int bytes, stringp;
2276 Lisp_Object coding_system;
2278 coding_system = Vlocale_coding_system;
2279 if (NILP (coding_system))
2280 coding_system = Qcompound_text;
2281 text.value = x_encode_text (name, coding_system, &bytes, &stringp);
2282 text.encoding = (stringp ? XA_STRING
2283 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2284 text.format = 8;
2285 text.nitems = bytes;
2287 if (NILP (f->icon_name))
2289 icon = text;
2291 else
2293 icon.value = x_encode_text (f->icon_name, coding_system,
2294 &bytes, &stringp);
2295 icon.encoding = (stringp ? XA_STRING
2296 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2297 icon.format = 8;
2298 icon.nitems = bytes;
2300 #ifdef USE_X_TOOLKIT
2301 XSetWMName (FRAME_X_DISPLAY (f),
2302 XtWindow (f->output_data.x->widget), &text);
2303 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2304 &icon);
2305 #else /* not USE_X_TOOLKIT */
2306 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2307 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2308 #endif /* not USE_X_TOOLKIT */
2309 if (!NILP (f->icon_name)
2310 && icon.value != XSTRING (f->icon_name)->data)
2311 xfree (icon.value);
2312 if (text.value != XSTRING (name)->data)
2313 xfree (text.value);
2315 #else /* not HAVE_X11R4 */
2316 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2317 XSTRING (name)->data);
2318 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2319 XSTRING (name)->data);
2320 #endif /* not HAVE_X11R4 */
2321 UNBLOCK_INPUT;
2325 /* This function should be called when the user's lisp code has
2326 specified a name for the frame; the name will override any set by the
2327 redisplay code. */
2328 void
2329 x_explicitly_set_name (f, arg, oldval)
2330 FRAME_PTR f;
2331 Lisp_Object arg, oldval;
2333 x_set_name (f, arg, 1);
2336 /* This function should be called by Emacs redisplay code to set the
2337 name; names set this way will never override names set by the user's
2338 lisp code. */
2339 void
2340 x_implicitly_set_name (f, arg, oldval)
2341 FRAME_PTR f;
2342 Lisp_Object arg, oldval;
2344 x_set_name (f, arg, 0);
2347 /* Change the title of frame F to NAME.
2348 If NAME is nil, use the frame name as the title.
2350 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2351 name; if NAME is a string, set F's name to NAME and set
2352 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2354 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2355 suggesting a new name, which lisp code should override; if
2356 F->explicit_name is set, ignore the new name; otherwise, set it. */
2358 void
2359 x_set_title (f, name, old_name)
2360 struct frame *f;
2361 Lisp_Object name, old_name;
2363 /* Don't change the title if it's already NAME. */
2364 if (EQ (name, f->title))
2365 return;
2367 update_mode_lines = 1;
2369 f->title = name;
2371 if (NILP (name))
2372 name = f->name;
2373 else
2374 CHECK_STRING (name, 0);
2376 if (FRAME_X_WINDOW (f))
2378 BLOCK_INPUT;
2379 #ifdef HAVE_X11R4
2381 XTextProperty text, icon;
2382 int bytes, stringp;
2383 Lisp_Object coding_system;
2385 coding_system = Vlocale_coding_system;
2386 if (NILP (coding_system))
2387 coding_system = Qcompound_text;
2388 text.value = x_encode_text (name, coding_system, &bytes, &stringp);
2389 text.encoding = (stringp ? XA_STRING
2390 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2391 text.format = 8;
2392 text.nitems = bytes;
2394 if (NILP (f->icon_name))
2396 icon = text;
2398 else
2400 icon.value = x_encode_text (f->icon_name, coding_system,
2401 &bytes, &stringp);
2402 icon.encoding = (stringp ? XA_STRING
2403 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2404 icon.format = 8;
2405 icon.nitems = bytes;
2407 #ifdef USE_X_TOOLKIT
2408 XSetWMName (FRAME_X_DISPLAY (f),
2409 XtWindow (f->output_data.x->widget), &text);
2410 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2411 &icon);
2412 #else /* not USE_X_TOOLKIT */
2413 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2414 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2415 #endif /* not USE_X_TOOLKIT */
2416 if (!NILP (f->icon_name)
2417 && icon.value != XSTRING (f->icon_name)->data)
2418 xfree (icon.value);
2419 if (text.value != XSTRING (name)->data)
2420 xfree (text.value);
2422 #else /* not HAVE_X11R4 */
2423 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2424 XSTRING (name)->data);
2425 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2426 XSTRING (name)->data);
2427 #endif /* not HAVE_X11R4 */
2428 UNBLOCK_INPUT;
2432 void
2433 x_set_autoraise (f, arg, oldval)
2434 struct frame *f;
2435 Lisp_Object arg, oldval;
2437 f->auto_raise = !EQ (Qnil, arg);
2440 void
2441 x_set_autolower (f, arg, oldval)
2442 struct frame *f;
2443 Lisp_Object arg, oldval;
2445 f->auto_lower = !EQ (Qnil, arg);
2448 void
2449 x_set_unsplittable (f, arg, oldval)
2450 struct frame *f;
2451 Lisp_Object arg, oldval;
2453 f->no_split = !NILP (arg);
2456 void
2457 x_set_vertical_scroll_bars (f, arg, oldval)
2458 struct frame *f;
2459 Lisp_Object arg, oldval;
2461 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2462 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2463 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2464 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
2466 FRAME_VERTICAL_SCROLL_BAR_TYPE (f)
2467 = (NILP (arg)
2468 ? vertical_scroll_bar_none
2469 : EQ (Qright, arg)
2470 ? vertical_scroll_bar_right
2471 : vertical_scroll_bar_left);
2473 /* We set this parameter before creating the X window for the
2474 frame, so we can get the geometry right from the start.
2475 However, if the window hasn't been created yet, we shouldn't
2476 call x_set_window_size. */
2477 if (FRAME_X_WINDOW (f))
2478 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2479 do_pending_window_change (0);
2483 void
2484 x_set_scroll_bar_width (f, arg, oldval)
2485 struct frame *f;
2486 Lisp_Object arg, oldval;
2488 int wid = FONT_WIDTH (f->output_data.x->font);
2490 if (NILP (arg))
2492 #ifdef USE_TOOLKIT_SCROLL_BARS
2493 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
2494 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2495 FRAME_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
2496 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = width;
2497 #else
2498 /* Make the actual width at least 14 pixels and a multiple of a
2499 character width. */
2500 FRAME_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
2502 /* Use all of that space (aside from required margins) for the
2503 scroll bar. */
2504 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = 0;
2505 #endif
2507 if (FRAME_X_WINDOW (f))
2508 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2509 do_pending_window_change (0);
2511 else if (INTEGERP (arg) && XINT (arg) > 0
2512 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2514 if (XFASTINT (arg) <= 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM)
2515 XSETINT (arg, 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM + 1);
2517 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
2518 FRAME_SCROLL_BAR_COLS (f) = (XFASTINT (arg) + wid-1) / wid;
2519 if (FRAME_X_WINDOW (f))
2520 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2523 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2524 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2525 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
2530 /* Subroutines of creating an X frame. */
2532 /* Make sure that Vx_resource_name is set to a reasonable value.
2533 Fix it up, or set it to `emacs' if it is too hopeless. */
2535 static void
2536 validate_x_resource_name ()
2538 int len = 0;
2539 /* Number of valid characters in the resource name. */
2540 int good_count = 0;
2541 /* Number of invalid characters in the resource name. */
2542 int bad_count = 0;
2543 Lisp_Object new;
2544 int i;
2546 if (!STRINGP (Vx_resource_class))
2547 Vx_resource_class = build_string (EMACS_CLASS);
2549 if (STRINGP (Vx_resource_name))
2551 unsigned char *p = XSTRING (Vx_resource_name)->data;
2552 int i;
2554 len = STRING_BYTES (XSTRING (Vx_resource_name));
2556 /* Only letters, digits, - and _ are valid in resource names.
2557 Count the valid characters and count the invalid ones. */
2558 for (i = 0; i < len; i++)
2560 int c = p[i];
2561 if (! ((c >= 'a' && c <= 'z')
2562 || (c >= 'A' && c <= 'Z')
2563 || (c >= '0' && c <= '9')
2564 || c == '-' || c == '_'))
2565 bad_count++;
2566 else
2567 good_count++;
2570 else
2571 /* Not a string => completely invalid. */
2572 bad_count = 5, good_count = 0;
2574 /* If name is valid already, return. */
2575 if (bad_count == 0)
2576 return;
2578 /* If name is entirely invalid, or nearly so, use `emacs'. */
2579 if (good_count == 0
2580 || (good_count == 1 && bad_count > 0))
2582 Vx_resource_name = build_string ("emacs");
2583 return;
2586 /* Name is partly valid. Copy it and replace the invalid characters
2587 with underscores. */
2589 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2591 for (i = 0; i < len; i++)
2593 int c = XSTRING (new)->data[i];
2594 if (! ((c >= 'a' && c <= 'z')
2595 || (c >= 'A' && c <= 'Z')
2596 || (c >= '0' && c <= '9')
2597 || c == '-' || c == '_'))
2598 XSTRING (new)->data[i] = '_';
2603 extern char *x_get_string_resource ();
2605 DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
2606 "Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.\n\
2607 This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the\n\
2608 class, where INSTANCE is the name under which Emacs was invoked, or\n\
2609 the name specified by the `-name' or `-rn' command-line arguments.\n\
2611 The optional arguments COMPONENT and SUBCLASS add to the key and the\n\
2612 class, respectively. You must specify both of them or neither.\n\
2613 If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'\n\
2614 and the class is `Emacs.CLASS.SUBCLASS'.")
2615 (attribute, class, component, subclass)
2616 Lisp_Object attribute, class, component, subclass;
2618 register char *value;
2619 char *name_key;
2620 char *class_key;
2622 check_x ();
2624 CHECK_STRING (attribute, 0);
2625 CHECK_STRING (class, 0);
2627 if (!NILP (component))
2628 CHECK_STRING (component, 1);
2629 if (!NILP (subclass))
2630 CHECK_STRING (subclass, 2);
2631 if (NILP (component) != NILP (subclass))
2632 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2634 validate_x_resource_name ();
2636 /* Allocate space for the components, the dots which separate them,
2637 and the final '\0'. Make them big enough for the worst case. */
2638 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
2639 + (STRINGP (component)
2640 ? STRING_BYTES (XSTRING (component)) : 0)
2641 + STRING_BYTES (XSTRING (attribute))
2642 + 3);
2644 class_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_class))
2645 + STRING_BYTES (XSTRING (class))
2646 + (STRINGP (subclass)
2647 ? STRING_BYTES (XSTRING (subclass)) : 0)
2648 + 3);
2650 /* Start with emacs.FRAMENAME for the name (the specific one)
2651 and with `Emacs' for the class key (the general one). */
2652 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2653 strcpy (class_key, XSTRING (Vx_resource_class)->data);
2655 strcat (class_key, ".");
2656 strcat (class_key, XSTRING (class)->data);
2658 if (!NILP (component))
2660 strcat (class_key, ".");
2661 strcat (class_key, XSTRING (subclass)->data);
2663 strcat (name_key, ".");
2664 strcat (name_key, XSTRING (component)->data);
2667 strcat (name_key, ".");
2668 strcat (name_key, XSTRING (attribute)->data);
2670 value = x_get_string_resource (check_x_display_info (Qnil)->xrdb,
2671 name_key, class_key);
2673 if (value != (char *) 0)
2674 return build_string (value);
2675 else
2676 return Qnil;
2679 /* Get an X resource, like Fx_get_resource, but for display DPYINFO. */
2681 Lisp_Object
2682 display_x_get_resource (dpyinfo, attribute, class, component, subclass)
2683 struct x_display_info *dpyinfo;
2684 Lisp_Object attribute, class, component, subclass;
2686 register char *value;
2687 char *name_key;
2688 char *class_key;
2690 CHECK_STRING (attribute, 0);
2691 CHECK_STRING (class, 0);
2693 if (!NILP (component))
2694 CHECK_STRING (component, 1);
2695 if (!NILP (subclass))
2696 CHECK_STRING (subclass, 2);
2697 if (NILP (component) != NILP (subclass))
2698 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2700 validate_x_resource_name ();
2702 /* Allocate space for the components, the dots which separate them,
2703 and the final '\0'. Make them big enough for the worst case. */
2704 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
2705 + (STRINGP (component)
2706 ? STRING_BYTES (XSTRING (component)) : 0)
2707 + STRING_BYTES (XSTRING (attribute))
2708 + 3);
2710 class_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_class))
2711 + STRING_BYTES (XSTRING (class))
2712 + (STRINGP (subclass)
2713 ? STRING_BYTES (XSTRING (subclass)) : 0)
2714 + 3);
2716 /* Start with emacs.FRAMENAME for the name (the specific one)
2717 and with `Emacs' for the class key (the general one). */
2718 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2719 strcpy (class_key, XSTRING (Vx_resource_class)->data);
2721 strcat (class_key, ".");
2722 strcat (class_key, XSTRING (class)->data);
2724 if (!NILP (component))
2726 strcat (class_key, ".");
2727 strcat (class_key, XSTRING (subclass)->data);
2729 strcat (name_key, ".");
2730 strcat (name_key, XSTRING (component)->data);
2733 strcat (name_key, ".");
2734 strcat (name_key, XSTRING (attribute)->data);
2736 value = x_get_string_resource (dpyinfo->xrdb, name_key, class_key);
2738 if (value != (char *) 0)
2739 return build_string (value);
2740 else
2741 return Qnil;
2744 /* Used when C code wants a resource value. */
2746 char *
2747 x_get_resource_string (attribute, class)
2748 char *attribute, *class;
2750 char *name_key;
2751 char *class_key;
2752 struct frame *sf = SELECTED_FRAME ();
2754 /* Allocate space for the components, the dots which separate them,
2755 and the final '\0'. */
2756 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
2757 + strlen (attribute) + 2);
2758 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2759 + strlen (class) + 2);
2761 sprintf (name_key, "%s.%s",
2762 XSTRING (Vinvocation_name)->data,
2763 attribute);
2764 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2766 return x_get_string_resource (FRAME_X_DISPLAY_INFO (sf)->xrdb,
2767 name_key, class_key);
2770 /* Types we might convert a resource string into. */
2771 enum resource_types
2773 RES_TYPE_NUMBER,
2774 RES_TYPE_FLOAT,
2775 RES_TYPE_BOOLEAN,
2776 RES_TYPE_STRING,
2777 RES_TYPE_SYMBOL
2780 /* Return the value of parameter PARAM.
2782 First search ALIST, then Vdefault_frame_alist, then the X defaults
2783 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2785 Convert the resource to the type specified by desired_type.
2787 If no default is specified, return Qunbound. If you call
2788 x_get_arg, make sure you deal with Qunbound in a reasonable way,
2789 and don't let it get stored in any Lisp-visible variables! */
2791 static Lisp_Object
2792 x_get_arg (dpyinfo, alist, param, attribute, class, type)
2793 struct x_display_info *dpyinfo;
2794 Lisp_Object alist, param;
2795 char *attribute;
2796 char *class;
2797 enum resource_types type;
2799 register Lisp_Object tem;
2801 tem = Fassq (param, alist);
2802 if (EQ (tem, Qnil))
2803 tem = Fassq (param, Vdefault_frame_alist);
2804 if (EQ (tem, Qnil))
2807 if (attribute)
2809 tem = display_x_get_resource (dpyinfo,
2810 build_string (attribute),
2811 build_string (class),
2812 Qnil, Qnil);
2814 if (NILP (tem))
2815 return Qunbound;
2817 switch (type)
2819 case RES_TYPE_NUMBER:
2820 return make_number (atoi (XSTRING (tem)->data));
2822 case RES_TYPE_FLOAT:
2823 return make_float (atof (XSTRING (tem)->data));
2825 case RES_TYPE_BOOLEAN:
2826 tem = Fdowncase (tem);
2827 if (!strcmp (XSTRING (tem)->data, "on")
2828 || !strcmp (XSTRING (tem)->data, "true"))
2829 return Qt;
2830 else
2831 return Qnil;
2833 case RES_TYPE_STRING:
2834 return tem;
2836 case RES_TYPE_SYMBOL:
2837 /* As a special case, we map the values `true' and `on'
2838 to Qt, and `false' and `off' to Qnil. */
2840 Lisp_Object lower;
2841 lower = Fdowncase (tem);
2842 if (!strcmp (XSTRING (lower)->data, "on")
2843 || !strcmp (XSTRING (lower)->data, "true"))
2844 return Qt;
2845 else if (!strcmp (XSTRING (lower)->data, "off")
2846 || !strcmp (XSTRING (lower)->data, "false"))
2847 return Qnil;
2848 else
2849 return Fintern (tem, Qnil);
2852 default:
2853 abort ();
2856 else
2857 return Qunbound;
2859 return Fcdr (tem);
2862 /* Like x_get_arg, but also record the value in f->param_alist. */
2864 static Lisp_Object
2865 x_get_and_record_arg (f, alist, param, attribute, class, type)
2866 struct frame *f;
2867 Lisp_Object alist, param;
2868 char *attribute;
2869 char *class;
2870 enum resource_types type;
2872 Lisp_Object value;
2874 value = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, param,
2875 attribute, class, type);
2876 if (! NILP (value))
2877 store_frame_param (f, param, value);
2879 return value;
2882 /* Record in frame F the specified or default value according to ALIST
2883 of the parameter named PROP (a Lisp symbol).
2884 If no value is specified for PROP, look for an X default for XPROP
2885 on the frame named NAME.
2886 If that is not found either, use the value DEFLT. */
2888 static Lisp_Object
2889 x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
2890 struct frame *f;
2891 Lisp_Object alist;
2892 Lisp_Object prop;
2893 Lisp_Object deflt;
2894 char *xprop;
2895 char *xclass;
2896 enum resource_types type;
2898 Lisp_Object tem;
2900 tem = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, prop, xprop, xclass, type);
2901 if (EQ (tem, Qunbound))
2902 tem = deflt;
2903 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
2904 return tem;
2908 /* Record in frame F the specified or default value according to ALIST
2909 of the parameter named PROP (a Lisp symbol). If no value is
2910 specified for PROP, look for an X default for XPROP on the frame
2911 named NAME. If that is not found either, use the value DEFLT. */
2913 static Lisp_Object
2914 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
2915 foreground_p)
2916 struct frame *f;
2917 Lisp_Object alist;
2918 Lisp_Object prop;
2919 char *xprop;
2920 char *xclass;
2921 int foreground_p;
2923 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2924 Lisp_Object tem;
2926 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
2927 if (EQ (tem, Qunbound))
2929 #ifdef USE_TOOLKIT_SCROLL_BARS
2931 /* See if an X resource for the scroll bar color has been
2932 specified. */
2933 tem = display_x_get_resource (dpyinfo,
2934 build_string (foreground_p
2935 ? "foreground"
2936 : "background"),
2937 build_string (""),
2938 build_string ("verticalScrollBar"),
2939 build_string (""));
2940 if (!STRINGP (tem))
2942 /* If nothing has been specified, scroll bars will use a
2943 toolkit-dependent default. Because these defaults are
2944 difficult to get at without actually creating a scroll
2945 bar, use nil to indicate that no color has been
2946 specified. */
2947 tem = Qnil;
2950 #else /* not USE_TOOLKIT_SCROLL_BARS */
2952 tem = Qnil;
2954 #endif /* not USE_TOOLKIT_SCROLL_BARS */
2957 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
2958 return tem;
2963 DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
2964 "Parse an X-style geometry string STRING.\n\
2965 Returns an alist of the form ((top . TOP), (left . LEFT) ... ).\n\
2966 The properties returned may include `top', `left', `height', and `width'.\n\
2967 The value of `left' or `top' may be an integer,\n\
2968 or a list (+ N) meaning N pixels relative to top/left corner,\n\
2969 or a list (- N) meaning -N pixels relative to bottom/right corner.")
2970 (string)
2971 Lisp_Object string;
2973 int geometry, x, y;
2974 unsigned int width, height;
2975 Lisp_Object result;
2977 CHECK_STRING (string, 0);
2979 geometry = XParseGeometry ((char *) XSTRING (string)->data,
2980 &x, &y, &width, &height);
2982 #if 0
2983 if (!!(geometry & XValue) != !!(geometry & YValue))
2984 error ("Must specify both x and y position, or neither");
2985 #endif
2987 result = Qnil;
2988 if (geometry & XValue)
2990 Lisp_Object element;
2992 if (x >= 0 && (geometry & XNegative))
2993 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
2994 else if (x < 0 && ! (geometry & XNegative))
2995 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
2996 else
2997 element = Fcons (Qleft, make_number (x));
2998 result = Fcons (element, result);
3001 if (geometry & YValue)
3003 Lisp_Object element;
3005 if (y >= 0 && (geometry & YNegative))
3006 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3007 else if (y < 0 && ! (geometry & YNegative))
3008 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3009 else
3010 element = Fcons (Qtop, make_number (y));
3011 result = Fcons (element, result);
3014 if (geometry & WidthValue)
3015 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3016 if (geometry & HeightValue)
3017 result = Fcons (Fcons (Qheight, make_number (height)), result);
3019 return result;
3022 /* Calculate the desired size and position of this window,
3023 and return the flags saying which aspects were specified.
3025 This function does not make the coordinates positive. */
3027 #define DEFAULT_ROWS 40
3028 #define DEFAULT_COLS 80
3030 static int
3031 x_figure_window_size (f, parms)
3032 struct frame *f;
3033 Lisp_Object parms;
3035 register Lisp_Object tem0, tem1, tem2;
3036 long window_prompting = 0;
3037 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3039 /* Default values if we fall through.
3040 Actually, if that happens we should get
3041 window manager prompting. */
3042 SET_FRAME_WIDTH (f, DEFAULT_COLS);
3043 f->height = DEFAULT_ROWS;
3044 /* Window managers expect that if program-specified
3045 positions are not (0,0), they're intentional, not defaults. */
3046 f->output_data.x->top_pos = 0;
3047 f->output_data.x->left_pos = 0;
3049 tem0 = x_get_arg (dpyinfo, parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3050 tem1 = x_get_arg (dpyinfo, parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3051 tem2 = x_get_arg (dpyinfo, parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
3052 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3054 if (!EQ (tem0, Qunbound))
3056 CHECK_NUMBER (tem0, 0);
3057 f->height = XINT (tem0);
3059 if (!EQ (tem1, Qunbound))
3061 CHECK_NUMBER (tem1, 0);
3062 SET_FRAME_WIDTH (f, XINT (tem1));
3064 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3065 window_prompting |= USSize;
3066 else
3067 window_prompting |= PSize;
3070 f->output_data.x->vertical_scroll_bar_extra
3071 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3073 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
3074 f->output_data.x->flags_areas_extra
3075 = FRAME_FLAGS_AREA_WIDTH (f);
3076 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3077 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3079 tem0 = x_get_arg (dpyinfo, parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3080 tem1 = x_get_arg (dpyinfo, parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3081 tem2 = x_get_arg (dpyinfo, parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
3082 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3084 if (EQ (tem0, Qminus))
3086 f->output_data.x->top_pos = 0;
3087 window_prompting |= YNegative;
3089 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3090 && CONSP (XCDR (tem0))
3091 && INTEGERP (XCAR (XCDR (tem0))))
3093 f->output_data.x->top_pos = - XINT (XCAR (XCDR (tem0)));
3094 window_prompting |= YNegative;
3096 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3097 && CONSP (XCDR (tem0))
3098 && INTEGERP (XCAR (XCDR (tem0))))
3100 f->output_data.x->top_pos = XINT (XCAR (XCDR (tem0)));
3102 else if (EQ (tem0, Qunbound))
3103 f->output_data.x->top_pos = 0;
3104 else
3106 CHECK_NUMBER (tem0, 0);
3107 f->output_data.x->top_pos = XINT (tem0);
3108 if (f->output_data.x->top_pos < 0)
3109 window_prompting |= YNegative;
3112 if (EQ (tem1, Qminus))
3114 f->output_data.x->left_pos = 0;
3115 window_prompting |= XNegative;
3117 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3118 && CONSP (XCDR (tem1))
3119 && INTEGERP (XCAR (XCDR (tem1))))
3121 f->output_data.x->left_pos = - XINT (XCAR (XCDR (tem1)));
3122 window_prompting |= XNegative;
3124 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3125 && CONSP (XCDR (tem1))
3126 && INTEGERP (XCAR (XCDR (tem1))))
3128 f->output_data.x->left_pos = XINT (XCAR (XCDR (tem1)));
3130 else if (EQ (tem1, Qunbound))
3131 f->output_data.x->left_pos = 0;
3132 else
3134 CHECK_NUMBER (tem1, 0);
3135 f->output_data.x->left_pos = XINT (tem1);
3136 if (f->output_data.x->left_pos < 0)
3137 window_prompting |= XNegative;
3140 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3141 window_prompting |= USPosition;
3142 else
3143 window_prompting |= PPosition;
3146 return window_prompting;
3149 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
3151 Status
3152 XSetWMProtocols (dpy, w, protocols, count)
3153 Display *dpy;
3154 Window w;
3155 Atom *protocols;
3156 int count;
3158 Atom prop;
3159 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
3160 if (prop == None) return False;
3161 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
3162 (unsigned char *) protocols, count);
3163 return True;
3165 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
3167 #ifdef USE_X_TOOLKIT
3169 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
3170 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
3171 already be present because of the toolkit (Motif adds some of them,
3172 for example, but Xt doesn't). */
3174 static void
3175 hack_wm_protocols (f, widget)
3176 FRAME_PTR f;
3177 Widget widget;
3179 Display *dpy = XtDisplay (widget);
3180 Window w = XtWindow (widget);
3181 int need_delete = 1;
3182 int need_focus = 1;
3183 int need_save = 1;
3185 BLOCK_INPUT;
3187 Atom type, *atoms = 0;
3188 int format = 0;
3189 unsigned long nitems = 0;
3190 unsigned long bytes_after;
3192 if ((XGetWindowProperty (dpy, w,
3193 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3194 (long)0, (long)100, False, XA_ATOM,
3195 &type, &format, &nitems, &bytes_after,
3196 (unsigned char **) &atoms)
3197 == Success)
3198 && format == 32 && type == XA_ATOM)
3199 while (nitems > 0)
3201 nitems--;
3202 if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
3203 need_delete = 0;
3204 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
3205 need_focus = 0;
3206 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
3207 need_save = 0;
3209 if (atoms) XFree ((char *) atoms);
3212 Atom props [10];
3213 int count = 0;
3214 if (need_delete)
3215 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3216 if (need_focus)
3217 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
3218 if (need_save)
3219 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
3220 if (count)
3221 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3222 XA_ATOM, 32, PropModeAppend,
3223 (unsigned char *) props, count);
3225 UNBLOCK_INPUT;
3227 #endif
3231 /* Support routines for XIC (X Input Context). */
3233 #ifdef HAVE_X_I18N
3235 static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
3236 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
3239 /* Supported XIM styles, ordered by preferenc. */
3241 static XIMStyle supported_xim_styles[] =
3243 XIMPreeditPosition | XIMStatusArea,
3244 XIMPreeditPosition | XIMStatusNothing,
3245 XIMPreeditPosition | XIMStatusNone,
3246 XIMPreeditNothing | XIMStatusArea,
3247 XIMPreeditNothing | XIMStatusNothing,
3248 XIMPreeditNothing | XIMStatusNone,
3249 XIMPreeditNone | XIMStatusArea,
3250 XIMPreeditNone | XIMStatusNothing,
3251 XIMPreeditNone | XIMStatusNone,
3256 /* Create an X fontset on frame F with base font name
3257 BASE_FONTNAME.. */
3259 static XFontSet
3260 xic_create_xfontset (f, base_fontname)
3261 struct frame *f;
3262 char *base_fontname;
3264 XFontSet xfs;
3265 char **missing_list;
3266 int missing_count;
3267 char *def_string;
3269 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
3270 base_fontname, &missing_list,
3271 &missing_count, &def_string);
3272 if (missing_list)
3273 XFreeStringList (missing_list);
3275 /* No need to free def_string. */
3276 return xfs;
3280 /* Value is the best input style, given user preferences USER (already
3281 checked to be supported by Emacs), and styles supported by the
3282 input method XIM. */
3284 static XIMStyle
3285 best_xim_style (user, xim)
3286 XIMStyles *user;
3287 XIMStyles *xim;
3289 int i, j;
3291 for (i = 0; i < user->count_styles; ++i)
3292 for (j = 0; j < xim->count_styles; ++j)
3293 if (user->supported_styles[i] == xim->supported_styles[j])
3294 return user->supported_styles[i];
3296 /* Return the default style. */
3297 return XIMPreeditNothing | XIMStatusNothing;
3300 /* Create XIC for frame F. */
3302 void
3303 create_frame_xic (f)
3304 struct frame *f;
3306 XIM xim;
3307 XIC xic = NULL;
3308 XFontSet xfs = NULL;
3309 static XIMStyle xic_style;
3311 if (FRAME_XIC (f))
3312 return;
3314 xim = FRAME_X_XIM (f);
3315 if (xim)
3317 XRectangle s_area;
3318 XPoint spot;
3319 XVaNestedList preedit_attr;
3320 XVaNestedList status_attr;
3321 char *base_fontname;
3322 int fontset;
3324 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
3325 spot.x = 0; spot.y = 1;
3326 /* Create X fontset. */
3327 fontset = FRAME_FONTSET (f);
3328 if (fontset < 0)
3329 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3330 else
3332 /* Determine the base fontname from the ASCII font name of
3333 FONTSET. */
3334 char *ascii_font = (char *) XSTRING (fontset_ascii (fontset))->data;
3335 char *p = ascii_font;
3336 int i;
3338 for (i = 0; *p; p++)
3339 if (*p == '-') i++;
3340 if (i != 14)
3341 /* As the font name doesn't conform to XLFD, we can't
3342 modify it to get a suitable base fontname for the
3343 frame. */
3344 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3345 else
3347 int len = strlen (ascii_font) + 1;
3348 char *p1 = NULL;
3350 for (i = 0, p = ascii_font; i < 8; p++)
3352 if (*p == '-')
3354 i++;
3355 if (i == 3)
3356 p1 = p + 1;
3359 base_fontname = (char *) alloca (len);
3360 bzero (base_fontname, len);
3361 strcpy (base_fontname, "-*-*-");
3362 bcopy (p1, base_fontname + 5, p - p1);
3363 strcat (base_fontname, "*-*-*-*-*-*-*");
3366 xfs = xic_create_xfontset (f, base_fontname);
3368 /* Determine XIC style. */
3369 if (xic_style == 0)
3371 XIMStyles supported_list;
3372 supported_list.count_styles = (sizeof supported_xim_styles
3373 / sizeof supported_xim_styles[0]);
3374 supported_list.supported_styles = supported_xim_styles;
3375 xic_style = best_xim_style (&supported_list,
3376 FRAME_X_XIM_STYLES (f));
3379 preedit_attr = XVaCreateNestedList (0,
3380 XNFontSet, xfs,
3381 XNForeground,
3382 FRAME_FOREGROUND_PIXEL (f),
3383 XNBackground,
3384 FRAME_BACKGROUND_PIXEL (f),
3385 (xic_style & XIMPreeditPosition
3386 ? XNSpotLocation
3387 : NULL),
3388 &spot,
3389 NULL);
3390 status_attr = XVaCreateNestedList (0,
3391 XNArea,
3392 &s_area,
3393 XNFontSet,
3394 xfs,
3395 XNForeground,
3396 FRAME_FOREGROUND_PIXEL (f),
3397 XNBackground,
3398 FRAME_BACKGROUND_PIXEL (f),
3399 NULL);
3401 xic = XCreateIC (xim,
3402 XNInputStyle, xic_style,
3403 XNClientWindow, FRAME_X_WINDOW(f),
3404 XNFocusWindow, FRAME_X_WINDOW(f),
3405 XNStatusAttributes, status_attr,
3406 XNPreeditAttributes, preedit_attr,
3407 NULL);
3408 XFree (preedit_attr);
3409 XFree (status_attr);
3412 FRAME_XIC (f) = xic;
3413 FRAME_XIC_STYLE (f) = xic_style;
3414 FRAME_XIC_FONTSET (f) = xfs;
3418 /* Destroy XIC and free XIC fontset of frame F, if any. */
3420 void
3421 free_frame_xic (f)
3422 struct frame *f;
3424 if (FRAME_XIC (f) == NULL)
3425 return;
3427 XDestroyIC (FRAME_XIC (f));
3428 if (FRAME_XIC_FONTSET (f))
3429 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3431 FRAME_XIC (f) = NULL;
3432 FRAME_XIC_FONTSET (f) = NULL;
3436 /* Place preedit area for XIC of window W's frame to specified
3437 pixel position X/Y. X and Y are relative to window W. */
3439 void
3440 xic_set_preeditarea (w, x, y)
3441 struct window *w;
3442 int x, y;
3444 struct frame *f = XFRAME (w->frame);
3445 XVaNestedList attr;
3446 XPoint spot;
3448 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x);
3449 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
3450 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
3451 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3452 XFree (attr);
3456 /* Place status area for XIC in bottom right corner of frame F.. */
3458 void
3459 xic_set_statusarea (f)
3460 struct frame *f;
3462 XIC xic = FRAME_XIC (f);
3463 XVaNestedList attr;
3464 XRectangle area;
3465 XRectangle *needed;
3467 /* Negotiate geometry of status area. If input method has existing
3468 status area, use its current size. */
3469 area.x = area.y = area.width = area.height = 0;
3470 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
3471 XSetICValues (xic, XNStatusAttributes, attr, NULL);
3472 XFree (attr);
3474 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
3475 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3476 XFree (attr);
3478 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
3480 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
3481 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3482 XFree (attr);
3485 area.width = needed->width;
3486 area.height = needed->height;
3487 area.x = PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
3488 area.y = (PIXEL_HEIGHT (f) - area.height
3489 - FRAME_MENUBAR_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f));
3490 XFree (needed);
3492 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
3493 XSetICValues(xic, XNStatusAttributes, attr, NULL);
3494 XFree (attr);
3498 /* Set X fontset for XIC of frame F, using base font name
3499 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
3501 void
3502 xic_set_xfontset (f, base_fontname)
3503 struct frame *f;
3504 char *base_fontname;
3506 XVaNestedList attr;
3507 XFontSet xfs;
3509 xfs = xic_create_xfontset (f, base_fontname);
3511 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
3512 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
3513 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3514 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
3515 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
3516 XFree (attr);
3518 if (FRAME_XIC_FONTSET (f))
3519 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3520 FRAME_XIC_FONTSET (f) = xfs;
3523 #endif /* HAVE_X_I18N */
3527 #ifdef USE_X_TOOLKIT
3529 /* Create and set up the X widget for frame F. */
3531 static void
3532 x_window (f, window_prompting, minibuffer_only)
3533 struct frame *f;
3534 long window_prompting;
3535 int minibuffer_only;
3537 XClassHint class_hints;
3538 XSetWindowAttributes attributes;
3539 unsigned long attribute_mask;
3540 Widget shell_widget;
3541 Widget pane_widget;
3542 Widget frame_widget;
3543 Arg al [25];
3544 int ac;
3546 BLOCK_INPUT;
3548 /* Use the resource name as the top-level widget name
3549 for looking up resources. Make a non-Lisp copy
3550 for the window manager, so GC relocation won't bother it.
3552 Elsewhere we specify the window name for the window manager. */
3555 char *str = (char *) XSTRING (Vx_resource_name)->data;
3556 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3557 strcpy (f->namebuf, str);
3560 ac = 0;
3561 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
3562 XtSetArg (al[ac], XtNinput, 1); ac++;
3563 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3564 XtSetArg (al[ac], XtNborderWidth, f->output_data.x->border_width); ac++;
3565 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3566 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3567 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3568 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
3569 applicationShellWidgetClass,
3570 FRAME_X_DISPLAY (f), al, ac);
3572 f->output_data.x->widget = shell_widget;
3573 /* maybe_set_screen_title_format (shell_widget); */
3575 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
3576 (widget_value *) NULL,
3577 shell_widget, False,
3578 (lw_callback) NULL,
3579 (lw_callback) NULL,
3580 (lw_callback) NULL,
3581 (lw_callback) NULL);
3583 ac = 0;
3584 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3585 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3586 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3587 XtSetValues (pane_widget, al, ac);
3588 f->output_data.x->column_widget = pane_widget;
3590 /* mappedWhenManaged to false tells to the paned window to not map/unmap
3591 the emacs screen when changing menubar. This reduces flickering. */
3593 ac = 0;
3594 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3595 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
3596 XtSetArg (al[ac], XtNallowResize, 1); ac++;
3597 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
3598 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
3599 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3600 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3601 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3602 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
3603 al, ac);
3605 f->output_data.x->edit_widget = frame_widget;
3607 XtManageChild (frame_widget);
3609 /* Do some needed geometry management. */
3611 int len;
3612 char *tem, shell_position[32];
3613 Arg al[2];
3614 int ac = 0;
3615 int extra_borders = 0;
3616 int menubar_size
3617 = (f->output_data.x->menubar_widget
3618 ? (f->output_data.x->menubar_widget->core.height
3619 + f->output_data.x->menubar_widget->core.border_width)
3620 : 0);
3622 #if 0 /* Experimentally, we now get the right results
3623 for -geometry -0-0 without this. 24 Aug 96, rms. */
3624 if (FRAME_EXTERNAL_MENU_BAR (f))
3626 Dimension ibw = 0;
3627 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
3628 menubar_size += ibw;
3630 #endif
3632 f->output_data.x->menubar_height = menubar_size;
3634 #ifndef USE_LUCID
3635 /* Motif seems to need this amount added to the sizes
3636 specified for the shell widget. The Athena/Lucid widgets don't.
3637 Both conclusions reached experimentally. -- rms. */
3638 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
3639 &extra_borders, NULL);
3640 extra_borders *= 2;
3641 #endif
3643 /* Convert our geometry parameters into a geometry string
3644 and specify it.
3645 Note that we do not specify here whether the position
3646 is a user-specified or program-specified one.
3647 We pass that information later, in x_wm_set_size_hints. */
3649 int left = f->output_data.x->left_pos;
3650 int xneg = window_prompting & XNegative;
3651 int top = f->output_data.x->top_pos;
3652 int yneg = window_prompting & YNegative;
3653 if (xneg)
3654 left = -left;
3655 if (yneg)
3656 top = -top;
3658 if (window_prompting & USPosition)
3659 sprintf (shell_position, "=%dx%d%c%d%c%d",
3660 PIXEL_WIDTH (f) + extra_borders,
3661 PIXEL_HEIGHT (f) + menubar_size + extra_borders,
3662 (xneg ? '-' : '+'), left,
3663 (yneg ? '-' : '+'), top);
3664 else
3665 sprintf (shell_position, "=%dx%d",
3666 PIXEL_WIDTH (f) + extra_borders,
3667 PIXEL_HEIGHT (f) + menubar_size + extra_borders);
3670 len = strlen (shell_position) + 1;
3671 /* We don't free this because we don't know whether
3672 it is safe to free it while the frame exists.
3673 It isn't worth the trouble of arranging to free it
3674 when the frame is deleted. */
3675 tem = (char *) xmalloc (len);
3676 strncpy (tem, shell_position, len);
3677 XtSetArg (al[ac], XtNgeometry, tem); ac++;
3678 XtSetValues (shell_widget, al, ac);
3681 XtManageChild (pane_widget);
3682 XtRealizeWidget (shell_widget);
3684 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
3686 validate_x_resource_name ();
3688 class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
3689 class_hints.res_class = (char *) XSTRING (Vx_resource_class)->data;
3690 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
3692 #ifdef HAVE_X_I18N
3693 FRAME_XIC (f) = NULL;
3694 #ifdef USE_XIM
3695 create_frame_xic (f);
3696 #endif
3697 #endif
3699 f->output_data.x->wm_hints.input = True;
3700 f->output_data.x->wm_hints.flags |= InputHint;
3701 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3702 &f->output_data.x->wm_hints);
3704 hack_wm_protocols (f, shell_widget);
3706 #ifdef HACK_EDITRES
3707 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
3708 #endif
3710 /* Do a stupid property change to force the server to generate a
3711 PropertyNotify event so that the event_stream server timestamp will
3712 be initialized to something relevant to the time we created the window.
3714 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
3715 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3716 XA_ATOM, 32, PropModeAppend,
3717 (unsigned char*) NULL, 0);
3719 /* Make all the standard events reach the Emacs frame. */
3720 attributes.event_mask = STANDARD_EVENT_SET;
3722 #ifdef HAVE_X_I18N
3723 if (FRAME_XIC (f))
3725 /* XIM server might require some X events. */
3726 unsigned long fevent = NoEventMask;
3727 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3728 attributes.event_mask |= fevent;
3730 #endif /* HAVE_X_I18N */
3732 attribute_mask = CWEventMask;
3733 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
3734 attribute_mask, &attributes);
3736 XtMapWidget (frame_widget);
3738 /* x_set_name normally ignores requests to set the name if the
3739 requested name is the same as the current name. This is the one
3740 place where that assumption isn't correct; f->name is set, but
3741 the X server hasn't been told. */
3743 Lisp_Object name;
3744 int explicit = f->explicit_name;
3746 f->explicit_name = 0;
3747 name = f->name;
3748 f->name = Qnil;
3749 x_set_name (f, name, explicit);
3752 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3753 f->output_data.x->text_cursor);
3755 UNBLOCK_INPUT;
3757 /* This is a no-op, except under Motif. Make sure main areas are
3758 set to something reasonable, in case we get an error later. */
3759 lw_set_main_areas (pane_widget, 0, frame_widget);
3762 #else /* not USE_X_TOOLKIT */
3764 /* Create and set up the X window for frame F. */
3766 void
3767 x_window (f)
3768 struct frame *f;
3771 XClassHint class_hints;
3772 XSetWindowAttributes attributes;
3773 unsigned long attribute_mask;
3775 attributes.background_pixel = f->output_data.x->background_pixel;
3776 attributes.border_pixel = f->output_data.x->border_pixel;
3777 attributes.bit_gravity = StaticGravity;
3778 attributes.backing_store = NotUseful;
3779 attributes.save_under = True;
3780 attributes.event_mask = STANDARD_EVENT_SET;
3781 attributes.colormap = FRAME_X_COLORMAP (f);
3782 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
3783 | CWColormap);
3785 BLOCK_INPUT;
3786 FRAME_X_WINDOW (f)
3787 = XCreateWindow (FRAME_X_DISPLAY (f),
3788 f->output_data.x->parent_desc,
3789 f->output_data.x->left_pos,
3790 f->output_data.x->top_pos,
3791 PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
3792 f->output_data.x->border_width,
3793 CopyFromParent, /* depth */
3794 InputOutput, /* class */
3795 FRAME_X_VISUAL (f),
3796 attribute_mask, &attributes);
3798 #ifdef HAVE_X_I18N
3799 #ifdef USE_XIM
3800 create_frame_xic (f);
3801 if (FRAME_XIC (f))
3803 /* XIM server might require some X events. */
3804 unsigned long fevent = NoEventMask;
3805 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3806 attributes.event_mask |= fevent;
3807 attribute_mask = CWEventMask;
3808 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3809 attribute_mask, &attributes);
3811 #endif
3812 #endif /* HAVE_X_I18N */
3814 validate_x_resource_name ();
3816 class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
3817 class_hints.res_class = (char *) XSTRING (Vx_resource_class)->data;
3818 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
3820 /* The menubar is part of the ordinary display;
3821 it does not count in addition to the height of the window. */
3822 f->output_data.x->menubar_height = 0;
3824 /* This indicates that we use the "Passive Input" input model.
3825 Unless we do this, we don't get the Focus{In,Out} events that we
3826 need to draw the cursor correctly. Accursed bureaucrats.
3827 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
3829 f->output_data.x->wm_hints.input = True;
3830 f->output_data.x->wm_hints.flags |= InputHint;
3831 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3832 &f->output_data.x->wm_hints);
3833 f->output_data.x->wm_hints.icon_pixmap = None;
3835 /* Request "save yourself" and "delete window" commands from wm. */
3837 Atom protocols[2];
3838 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3839 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
3840 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
3843 /* x_set_name normally ignores requests to set the name if the
3844 requested name is the same as the current name. This is the one
3845 place where that assumption isn't correct; f->name is set, but
3846 the X server hasn't been told. */
3848 Lisp_Object name;
3849 int explicit = f->explicit_name;
3851 f->explicit_name = 0;
3852 name = f->name;
3853 f->name = Qnil;
3854 x_set_name (f, name, explicit);
3857 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3858 f->output_data.x->text_cursor);
3860 UNBLOCK_INPUT;
3862 if (FRAME_X_WINDOW (f) == 0)
3863 error ("Unable to create window");
3866 #endif /* not USE_X_TOOLKIT */
3868 /* Handle the icon stuff for this window. Perhaps later we might
3869 want an x_set_icon_position which can be called interactively as
3870 well. */
3872 static void
3873 x_icon (f, parms)
3874 struct frame *f;
3875 Lisp_Object parms;
3877 Lisp_Object icon_x, icon_y;
3878 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3880 /* Set the position of the icon. Note that twm groups all
3881 icons in an icon window. */
3882 icon_x = x_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3883 icon_y = x_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3884 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3886 CHECK_NUMBER (icon_x, 0);
3887 CHECK_NUMBER (icon_y, 0);
3889 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3890 error ("Both left and top icon corners of icon must be specified");
3892 BLOCK_INPUT;
3894 if (! EQ (icon_x, Qunbound))
3895 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3897 /* Start up iconic or window? */
3898 x_wm_set_window_state
3899 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
3900 Qicon)
3901 ? IconicState
3902 : NormalState));
3904 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
3905 ? f->icon_name
3906 : f->name))->data);
3908 UNBLOCK_INPUT;
3911 /* Make the GCs needed for this window, setting the
3912 background, border and mouse colors; also create the
3913 mouse cursor and the gray border tile. */
3915 static char cursor_bits[] =
3917 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3918 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3919 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3920 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3923 static void
3924 x_make_gc (f)
3925 struct frame *f;
3927 XGCValues gc_values;
3929 BLOCK_INPUT;
3931 /* Create the GCs of this frame.
3932 Note that many default values are used. */
3934 /* Normal video */
3935 gc_values.font = f->output_data.x->font->fid;
3936 gc_values.foreground = f->output_data.x->foreground_pixel;
3937 gc_values.background = f->output_data.x->background_pixel;
3938 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
3939 f->output_data.x->normal_gc = XCreateGC (FRAME_X_DISPLAY (f),
3940 FRAME_X_WINDOW (f),
3941 GCLineWidth | GCFont
3942 | GCForeground | GCBackground,
3943 &gc_values);
3945 /* Reverse video style. */
3946 gc_values.foreground = f->output_data.x->background_pixel;
3947 gc_values.background = f->output_data.x->foreground_pixel;
3948 f->output_data.x->reverse_gc = XCreateGC (FRAME_X_DISPLAY (f),
3949 FRAME_X_WINDOW (f),
3950 GCFont | GCForeground | GCBackground
3951 | GCLineWidth,
3952 &gc_values);
3954 /* Cursor has cursor-color background, background-color foreground. */
3955 gc_values.foreground = f->output_data.x->background_pixel;
3956 gc_values.background = f->output_data.x->cursor_pixel;
3957 gc_values.fill_style = FillOpaqueStippled;
3958 gc_values.stipple
3959 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
3960 FRAME_X_DISPLAY_INFO (f)->root_window,
3961 cursor_bits, 16, 16);
3962 f->output_data.x->cursor_gc
3963 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3964 (GCFont | GCForeground | GCBackground
3965 | GCFillStyle /* | GCStipple */ | GCLineWidth),
3966 &gc_values);
3968 /* Reliefs. */
3969 f->output_data.x->white_relief.gc = 0;
3970 f->output_data.x->black_relief.gc = 0;
3972 /* Create the gray border tile used when the pointer is not in
3973 the frame. Since this depends on the frame's pixel values,
3974 this must be done on a per-frame basis. */
3975 f->output_data.x->border_tile
3976 = (XCreatePixmapFromBitmapData
3977 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
3978 gray_bits, gray_width, gray_height,
3979 f->output_data.x->foreground_pixel,
3980 f->output_data.x->background_pixel,
3981 DefaultDepth (FRAME_X_DISPLAY (f),
3982 XScreenNumberOfScreen (FRAME_X_SCREEN (f)))));
3984 UNBLOCK_INPUT;
3987 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
3988 1, 1, 0,
3989 "Make a new X window, which is called a \"frame\" in Emacs terms.\n\
3990 Returns an Emacs frame object.\n\
3991 ALIST is an alist of frame parameters.\n\
3992 If the parameters specify that the frame should not have a minibuffer,\n\
3993 and do not specify a specific minibuffer window to use,\n\
3994 then `default-minibuffer-frame' must be a frame whose minibuffer can\n\
3995 be shared by the new frame.\n\
3997 This function is an internal primitive--use `make-frame' instead.")
3998 (parms)
3999 Lisp_Object parms;
4001 struct frame *f;
4002 Lisp_Object frame, tem;
4003 Lisp_Object name;
4004 int minibuffer_only = 0;
4005 long window_prompting = 0;
4006 int width, height;
4007 int count = specpdl_ptr - specpdl;
4008 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4009 Lisp_Object display;
4010 struct x_display_info *dpyinfo = NULL;
4011 Lisp_Object parent;
4012 struct kboard *kb;
4014 check_x ();
4016 /* Use this general default value to start with
4017 until we know if this frame has a specified name. */
4018 Vx_resource_name = Vinvocation_name;
4020 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
4021 if (EQ (display, Qunbound))
4022 display = Qnil;
4023 dpyinfo = check_x_display_info (display);
4024 #ifdef MULTI_KBOARD
4025 kb = dpyinfo->kboard;
4026 #else
4027 kb = &the_only_kboard;
4028 #endif
4030 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4031 if (!STRINGP (name)
4032 && ! EQ (name, Qunbound)
4033 && ! NILP (name))
4034 error ("Invalid frame name--not a string or nil");
4036 if (STRINGP (name))
4037 Vx_resource_name = name;
4039 /* See if parent window is specified. */
4040 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4041 if (EQ (parent, Qunbound))
4042 parent = Qnil;
4043 if (! NILP (parent))
4044 CHECK_NUMBER (parent, 0);
4046 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4047 /* No need to protect DISPLAY because that's not used after passing
4048 it to make_frame_without_minibuffer. */
4049 frame = Qnil;
4050 GCPRO4 (parms, parent, name, frame);
4051 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
4052 RES_TYPE_SYMBOL);
4053 if (EQ (tem, Qnone) || NILP (tem))
4054 f = make_frame_without_minibuffer (Qnil, kb, display);
4055 else if (EQ (tem, Qonly))
4057 f = make_minibuffer_frame ();
4058 minibuffer_only = 1;
4060 else if (WINDOWP (tem))
4061 f = make_frame_without_minibuffer (tem, kb, display);
4062 else
4063 f = make_frame (1);
4065 XSETFRAME (frame, f);
4067 /* Note that X Windows does support scroll bars. */
4068 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4070 f->output_method = output_x_window;
4071 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4072 bzero (f->output_data.x, sizeof (struct x_output));
4073 f->output_data.x->icon_bitmap = -1;
4074 f->output_data.x->fontset = -1;
4075 f->output_data.x->scroll_bar_foreground_pixel = -1;
4076 f->output_data.x->scroll_bar_background_pixel = -1;
4078 f->icon_name
4079 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
4080 RES_TYPE_STRING);
4081 if (! STRINGP (f->icon_name))
4082 f->icon_name = Qnil;
4084 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4085 #ifdef MULTI_KBOARD
4086 FRAME_KBOARD (f) = kb;
4087 #endif
4089 /* These colors will be set anyway later, but it's important
4090 to get the color reference counts right, so initialize them! */
4092 Lisp_Object black;
4093 struct gcpro gcpro1;
4095 black = build_string ("black");
4096 GCPRO1 (black);
4097 f->output_data.x->foreground_pixel
4098 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4099 f->output_data.x->background_pixel
4100 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4101 f->output_data.x->cursor_pixel
4102 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4103 f->output_data.x->cursor_foreground_pixel
4104 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4105 f->output_data.x->border_pixel
4106 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4107 f->output_data.x->mouse_pixel
4108 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4109 UNGCPRO;
4112 /* Specify the parent under which to make this X window. */
4114 if (!NILP (parent))
4116 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
4117 f->output_data.x->explicit_parent = 1;
4119 else
4121 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4122 f->output_data.x->explicit_parent = 0;
4125 /* Set the name; the functions to which we pass f expect the name to
4126 be set. */
4127 if (EQ (name, Qunbound) || NILP (name))
4129 f->name = build_string (dpyinfo->x_id_name);
4130 f->explicit_name = 0;
4132 else
4134 f->name = name;
4135 f->explicit_name = 1;
4136 /* use the frame's title when getting resources for this frame. */
4137 specbind (Qx_resource_name, name);
4140 /* Extract the window parameters from the supplied values
4141 that are needed to determine window geometry. */
4143 Lisp_Object font;
4145 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4147 BLOCK_INPUT;
4148 /* First, try whatever font the caller has specified. */
4149 if (STRINGP (font))
4151 tem = Fquery_fontset (font, Qnil);
4152 if (STRINGP (tem))
4153 font = x_new_fontset (f, XSTRING (tem)->data);
4154 else
4155 font = x_new_font (f, XSTRING (font)->data);
4158 /* Try out a font which we hope has bold and italic variations. */
4159 if (!STRINGP (font))
4160 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
4161 if (!STRINGP (font))
4162 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4163 if (! STRINGP (font))
4164 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4165 if (! STRINGP (font))
4166 /* This was formerly the first thing tried, but it finds too many fonts
4167 and takes too long. */
4168 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
4169 /* If those didn't work, look for something which will at least work. */
4170 if (! STRINGP (font))
4171 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
4172 UNBLOCK_INPUT;
4173 if (! STRINGP (font))
4174 font = build_string ("fixed");
4176 x_default_parameter (f, parms, Qfont, font,
4177 "font", "Font", RES_TYPE_STRING);
4180 #ifdef USE_LUCID
4181 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
4182 whereby it fails to get any font. */
4183 xlwmenu_default_font = f->output_data.x->font;
4184 #endif
4186 x_default_parameter (f, parms, Qborder_width, make_number (2),
4187 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4189 /* This defaults to 2 in order to match xterm. We recognize either
4190 internalBorderWidth or internalBorder (which is what xterm calls
4191 it). */
4192 if (NILP (Fassq (Qinternal_border_width, parms)))
4194 Lisp_Object value;
4196 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4197 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4198 if (! EQ (value, Qunbound))
4199 parms = Fcons (Fcons (Qinternal_border_width, value),
4200 parms);
4202 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4203 "internalBorderWidth", "internalBorderWidth",
4204 RES_TYPE_NUMBER);
4205 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
4206 "verticalScrollBars", "ScrollBars",
4207 RES_TYPE_SYMBOL);
4209 /* Also do the stuff which must be set before the window exists. */
4210 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4211 "foreground", "Foreground", RES_TYPE_STRING);
4212 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4213 "background", "Background", RES_TYPE_STRING);
4214 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4215 "pointerColor", "Foreground", RES_TYPE_STRING);
4216 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4217 "cursorColor", "Foreground", RES_TYPE_STRING);
4218 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4219 "borderColor", "BorderColor", RES_TYPE_STRING);
4220 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
4221 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4222 x_default_parameter (f, parms, Qline_spacing, Qnil,
4223 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4225 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
4226 "scrollBarForeground",
4227 "ScrollBarForeground", 1);
4228 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
4229 "scrollBarBackground",
4230 "ScrollBarBackground", 0);
4232 /* Init faces before x_default_parameter is called for scroll-bar
4233 parameters because that function calls x_set_scroll_bar_width,
4234 which calls change_frame_size, which calls Fset_window_buffer,
4235 which runs hooks, which call Fvertical_motion. At the end, we
4236 end up in init_iterator with a null face cache, which should not
4237 happen. */
4238 init_frame_faces (f);
4240 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
4241 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4242 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
4243 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4244 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
4245 "bufferPredicate", "BufferPredicate",
4246 RES_TYPE_SYMBOL);
4247 x_default_parameter (f, parms, Qtitle, Qnil,
4248 "title", "Title", RES_TYPE_STRING);
4250 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4251 window_prompting = x_figure_window_size (f, parms);
4253 if (window_prompting & XNegative)
4255 if (window_prompting & YNegative)
4256 f->output_data.x->win_gravity = SouthEastGravity;
4257 else
4258 f->output_data.x->win_gravity = NorthEastGravity;
4260 else
4262 if (window_prompting & YNegative)
4263 f->output_data.x->win_gravity = SouthWestGravity;
4264 else
4265 f->output_data.x->win_gravity = NorthWestGravity;
4268 f->output_data.x->size_hint_flags = window_prompting;
4270 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4271 f->no_split = minibuffer_only || EQ (tem, Qt);
4273 /* Create the X widget or window. Add the tool-bar height to the
4274 initial frame height so that the user gets a text display area of
4275 the size he specified with -g or via .Xdefaults. Later changes
4276 of the tool-bar height don't change the frame size. This is done
4277 so that users can create tall Emacs frames without having to
4278 guess how tall the tool-bar will get. */
4279 f->height += FRAME_TOOL_BAR_LINES (f);
4281 #ifdef USE_X_TOOLKIT
4282 x_window (f, window_prompting, minibuffer_only);
4283 #else
4284 x_window (f);
4285 #endif
4287 x_icon (f, parms);
4288 x_make_gc (f);
4290 /* Now consider the frame official. */
4291 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4292 Vframe_list = Fcons (frame, Vframe_list);
4294 /* We need to do this after creating the X window, so that the
4295 icon-creation functions can say whose icon they're describing. */
4296 x_default_parameter (f, parms, Qicon_type, Qnil,
4297 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4299 x_default_parameter (f, parms, Qauto_raise, Qnil,
4300 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4301 x_default_parameter (f, parms, Qauto_lower, Qnil,
4302 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4303 x_default_parameter (f, parms, Qcursor_type, Qbox,
4304 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4305 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
4306 "scrollBarWidth", "ScrollBarWidth",
4307 RES_TYPE_NUMBER);
4309 /* Dimensions, especially f->height, must be done via change_frame_size.
4310 Change will not be effected unless different from the current
4311 f->height. */
4312 width = f->width;
4313 height = f->height;
4314 f->height = 0;
4315 SET_FRAME_WIDTH (f, 0);
4316 change_frame_size (f, height, width, 1, 0, 0);
4318 #ifdef USE_X_TOOLKIT
4319 /* Create the menu bar. */
4320 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4322 /* If this signals an error, we haven't set size hints for the
4323 frame and we didn't make it visible. */
4324 initialize_frame_menubar (f);
4326 /* This is a no-op, except under Motif where it arranges the
4327 main window for the widgets on it. */
4328 lw_set_main_areas (f->output_data.x->column_widget,
4329 f->output_data.x->menubar_widget,
4330 f->output_data.x->edit_widget);
4332 #endif /* USE_X_TOOLKIT */
4334 /* Tell the server what size and position, etc, we want, and how
4335 badly we want them. This should be done after we have the menu
4336 bar so that its size can be taken into account. */
4337 BLOCK_INPUT;
4338 x_wm_set_size_hint (f, window_prompting, 0);
4339 UNBLOCK_INPUT;
4341 /* Make the window appear on the frame and enable display, unless
4342 the caller says not to. However, with explicit parent, Emacs
4343 cannot control visibility, so don't try. */
4344 if (! f->output_data.x->explicit_parent)
4346 Lisp_Object visibility;
4348 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
4349 RES_TYPE_SYMBOL);
4350 if (EQ (visibility, Qunbound))
4351 visibility = Qt;
4353 if (EQ (visibility, Qicon))
4354 x_iconify_frame (f);
4355 else if (! NILP (visibility))
4356 x_make_frame_visible (f);
4357 else
4358 /* Must have been Qnil. */
4362 UNGCPRO;
4363 return unbind_to (count, frame);
4366 /* FRAME is used only to get a handle on the X display. We don't pass the
4367 display info directly because we're called from frame.c, which doesn't
4368 know about that structure. */
4370 Lisp_Object
4371 x_get_focus_frame (frame)
4372 struct frame *frame;
4374 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
4375 Lisp_Object xfocus;
4376 if (! dpyinfo->x_focus_frame)
4377 return Qnil;
4379 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
4380 return xfocus;
4384 /* In certain situations, when the window manager follows a
4385 click-to-focus policy, there seems to be no way around calling
4386 XSetInputFocus to give another frame the input focus .
4388 In an ideal world, XSetInputFocus should generally be avoided so
4389 that applications don't interfere with the window manager's focus
4390 policy. But I think it's okay to use when it's clearly done
4391 following a user-command. */
4393 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4394 "Set the input focus to FRAME.\n\
4395 FRAME nil means use the selected frame.")
4396 (frame)
4397 Lisp_Object frame;
4399 struct frame *f = check_x_frame (frame);
4400 Display *dpy = FRAME_X_DISPLAY (f);
4401 int count;
4403 BLOCK_INPUT;
4404 count = x_catch_errors (dpy);
4405 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4406 RevertToParent, CurrentTime);
4407 x_uncatch_errors (dpy, count);
4408 UNBLOCK_INPUT;
4410 return Qnil;
4414 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4415 "Internal function called by `color-defined-p', which see.")
4416 (color, frame)
4417 Lisp_Object color, frame;
4419 XColor foo;
4420 FRAME_PTR f = check_x_frame (frame);
4422 CHECK_STRING (color, 1);
4424 if (x_defined_color (f, XSTRING (color)->data, &foo, 0))
4425 return Qt;
4426 else
4427 return Qnil;
4430 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4431 "Internal function called by `color-values', which see.")
4432 (color, frame)
4433 Lisp_Object color, frame;
4435 XColor foo;
4436 FRAME_PTR f = check_x_frame (frame);
4438 CHECK_STRING (color, 1);
4440 if (x_defined_color (f, XSTRING (color)->data, &foo, 0))
4442 Lisp_Object rgb[3];
4444 rgb[0] = make_number (foo.red);
4445 rgb[1] = make_number (foo.green);
4446 rgb[2] = make_number (foo.blue);
4447 return Flist (3, rgb);
4449 else
4450 return Qnil;
4453 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4454 "Internal function called by `display-color-p', which see.")
4455 (display)
4456 Lisp_Object display;
4458 struct x_display_info *dpyinfo = check_x_display_info (display);
4460 if (dpyinfo->n_planes <= 2)
4461 return Qnil;
4463 switch (dpyinfo->visual->class)
4465 case StaticColor:
4466 case PseudoColor:
4467 case TrueColor:
4468 case DirectColor:
4469 return Qt;
4471 default:
4472 return Qnil;
4476 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
4477 0, 1, 0,
4478 "Return t if the X display supports shades of gray.\n\
4479 Note that color displays do support shades of gray.\n\
4480 The optional argument DISPLAY specifies which display to ask about.\n\
4481 DISPLAY should be either a frame or a display name (a string).\n\
4482 If omitted or nil, that stands for the selected frame's display.")
4483 (display)
4484 Lisp_Object display;
4486 struct x_display_info *dpyinfo = check_x_display_info (display);
4488 if (dpyinfo->n_planes <= 1)
4489 return Qnil;
4491 switch (dpyinfo->visual->class)
4493 case StaticColor:
4494 case PseudoColor:
4495 case TrueColor:
4496 case DirectColor:
4497 case StaticGray:
4498 case GrayScale:
4499 return Qt;
4501 default:
4502 return Qnil;
4506 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
4507 0, 1, 0,
4508 "Returns the width in pixels of the X display DISPLAY.\n\
4509 The optional argument DISPLAY specifies which display to ask about.\n\
4510 DISPLAY should be either a frame or a display name (a string).\n\
4511 If omitted or nil, that stands for the selected frame's display.")
4512 (display)
4513 Lisp_Object display;
4515 struct x_display_info *dpyinfo = check_x_display_info (display);
4517 return make_number (dpyinfo->width);
4520 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4521 Sx_display_pixel_height, 0, 1, 0,
4522 "Returns the height in pixels of the X display DISPLAY.\n\
4523 The optional argument DISPLAY specifies which display to ask about.\n\
4524 DISPLAY should be either a frame or a display name (a string).\n\
4525 If omitted or nil, that stands for the selected frame's display.")
4526 (display)
4527 Lisp_Object display;
4529 struct x_display_info *dpyinfo = check_x_display_info (display);
4531 return make_number (dpyinfo->height);
4534 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4535 0, 1, 0,
4536 "Returns the number of bitplanes of the X display DISPLAY.\n\
4537 The optional argument DISPLAY specifies which display to ask about.\n\
4538 DISPLAY should be either a frame or a display name (a string).\n\
4539 If omitted or nil, that stands for the selected frame's display.")
4540 (display)
4541 Lisp_Object display;
4543 struct x_display_info *dpyinfo = check_x_display_info (display);
4545 return make_number (dpyinfo->n_planes);
4548 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4549 0, 1, 0,
4550 "Returns the number of color cells of the X display DISPLAY.\n\
4551 The optional argument DISPLAY specifies which display to ask about.\n\
4552 DISPLAY should be either a frame or a display name (a string).\n\
4553 If omitted or nil, that stands for the selected frame's display.")
4554 (display)
4555 Lisp_Object display;
4557 struct x_display_info *dpyinfo = check_x_display_info (display);
4559 return make_number (DisplayCells (dpyinfo->display,
4560 XScreenNumberOfScreen (dpyinfo->screen)));
4563 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4564 Sx_server_max_request_size,
4565 0, 1, 0,
4566 "Returns the maximum request size of the X server of display DISPLAY.\n\
4567 The optional argument DISPLAY specifies which display to ask about.\n\
4568 DISPLAY should be either a frame or a display name (a string).\n\
4569 If omitted or nil, that stands for the selected frame's display.")
4570 (display)
4571 Lisp_Object display;
4573 struct x_display_info *dpyinfo = check_x_display_info (display);
4575 return make_number (MAXREQUEST (dpyinfo->display));
4578 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4579 "Returns the vendor ID string of the X server of display DISPLAY.\n\
4580 The optional argument DISPLAY specifies which display to ask about.\n\
4581 DISPLAY should be either a frame or a display name (a string).\n\
4582 If omitted or nil, that stands for the selected frame's display.")
4583 (display)
4584 Lisp_Object display;
4586 struct x_display_info *dpyinfo = check_x_display_info (display);
4587 char *vendor = ServerVendor (dpyinfo->display);
4589 if (! vendor) vendor = "";
4590 return build_string (vendor);
4593 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4594 "Returns the version numbers of the X server of display DISPLAY.\n\
4595 The value is a list of three integers: the major and minor\n\
4596 version numbers of the X Protocol in use, and the vendor-specific release\n\
4597 number. See also the function `x-server-vendor'.\n\n\
4598 The optional argument DISPLAY specifies which display to ask about.\n\
4599 DISPLAY should be either a frame or a display name (a string).\n\
4600 If omitted or nil, that stands for the selected frame's display.")
4601 (display)
4602 Lisp_Object display;
4604 struct x_display_info *dpyinfo = check_x_display_info (display);
4605 Display *dpy = dpyinfo->display;
4607 return Fcons (make_number (ProtocolVersion (dpy)),
4608 Fcons (make_number (ProtocolRevision (dpy)),
4609 Fcons (make_number (VendorRelease (dpy)), Qnil)));
4612 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4613 "Returns the number of screens on the X server of display DISPLAY.\n\
4614 The optional argument DISPLAY specifies which display to ask about.\n\
4615 DISPLAY should be either a frame or a display name (a string).\n\
4616 If omitted or nil, that stands for the selected frame's display.")
4617 (display)
4618 Lisp_Object display;
4620 struct x_display_info *dpyinfo = check_x_display_info (display);
4622 return make_number (ScreenCount (dpyinfo->display));
4625 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
4626 "Returns the height in millimeters of the X display DISPLAY.\n\
4627 The optional argument DISPLAY specifies which display to ask about.\n\
4628 DISPLAY should be either a frame or a display name (a string).\n\
4629 If omitted or nil, that stands for the selected frame's display.")
4630 (display)
4631 Lisp_Object display;
4633 struct x_display_info *dpyinfo = check_x_display_info (display);
4635 return make_number (HeightMMOfScreen (dpyinfo->screen));
4638 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4639 "Returns the width in millimeters of the X display DISPLAY.\n\
4640 The optional argument DISPLAY specifies which display to ask about.\n\
4641 DISPLAY should be either a frame or a display name (a string).\n\
4642 If omitted or nil, that stands for the selected frame's display.")
4643 (display)
4644 Lisp_Object display;
4646 struct x_display_info *dpyinfo = check_x_display_info (display);
4648 return make_number (WidthMMOfScreen (dpyinfo->screen));
4651 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4652 Sx_display_backing_store, 0, 1, 0,
4653 "Returns an indication of whether X display DISPLAY does backing store.\n\
4654 The value may be `always', `when-mapped', or `not-useful'.\n\
4655 The optional argument DISPLAY specifies which display to ask about.\n\
4656 DISPLAY should be either a frame or a display name (a string).\n\
4657 If omitted or nil, that stands for the selected frame's display.")
4658 (display)
4659 Lisp_Object display;
4661 struct x_display_info *dpyinfo = check_x_display_info (display);
4662 Lisp_Object result;
4664 switch (DoesBackingStore (dpyinfo->screen))
4666 case Always:
4667 result = intern ("always");
4668 break;
4670 case WhenMapped:
4671 result = intern ("when-mapped");
4672 break;
4674 case NotUseful:
4675 result = intern ("not-useful");
4676 break;
4678 default:
4679 error ("Strange value for BackingStore parameter of screen");
4680 result = Qnil;
4683 return result;
4686 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4687 Sx_display_visual_class, 0, 1, 0,
4688 "Returns the visual class of the X display DISPLAY.\n\
4689 The value is one of the symbols `static-gray', `gray-scale',\n\
4690 `static-color', `pseudo-color', `true-color', or `direct-color'.\n\n\
4691 The optional argument DISPLAY specifies which display to ask about.\n\
4692 DISPLAY should be either a frame or a display name (a string).\n\
4693 If omitted or nil, that stands for the selected frame's display.")
4694 (display)
4695 Lisp_Object display;
4697 struct x_display_info *dpyinfo = check_x_display_info (display);
4698 Lisp_Object result;
4700 switch (dpyinfo->visual->class)
4702 case StaticGray:
4703 result = intern ("static-gray");
4704 break;
4705 case GrayScale:
4706 result = intern ("gray-scale");
4707 break;
4708 case StaticColor:
4709 result = intern ("static-color");
4710 break;
4711 case PseudoColor:
4712 result = intern ("pseudo-color");
4713 break;
4714 case TrueColor:
4715 result = intern ("true-color");
4716 break;
4717 case DirectColor:
4718 result = intern ("direct-color");
4719 break;
4720 default:
4721 error ("Display has an unknown visual class");
4722 result = Qnil;
4725 return result;
4728 DEFUN ("x-display-save-under", Fx_display_save_under,
4729 Sx_display_save_under, 0, 1, 0,
4730 "Returns t if the X display DISPLAY supports the save-under feature.\n\
4731 The optional argument DISPLAY specifies which display to ask about.\n\
4732 DISPLAY should be either a frame or a display name (a string).\n\
4733 If omitted or nil, that stands for the selected frame's display.")
4734 (display)
4735 Lisp_Object display;
4737 struct x_display_info *dpyinfo = check_x_display_info (display);
4739 if (DoesSaveUnders (dpyinfo->screen) == True)
4740 return Qt;
4741 else
4742 return Qnil;
4746 x_pixel_width (f)
4747 register struct frame *f;
4749 return PIXEL_WIDTH (f);
4753 x_pixel_height (f)
4754 register struct frame *f;
4756 return PIXEL_HEIGHT (f);
4760 x_char_width (f)
4761 register struct frame *f;
4763 return FONT_WIDTH (f->output_data.x->font);
4767 x_char_height (f)
4768 register struct frame *f;
4770 return f->output_data.x->line_height;
4774 x_screen_planes (f)
4775 register struct frame *f;
4777 return FRAME_X_DISPLAY_INFO (f)->n_planes;
4782 /************************************************************************
4783 X Displays
4784 ************************************************************************/
4787 /* Mapping visual names to visuals. */
4789 static struct visual_class
4791 char *name;
4792 int class;
4794 visual_classes[] =
4796 {"StaticGray", StaticGray},
4797 {"GrayScale", GrayScale},
4798 {"StaticColor", StaticColor},
4799 {"PseudoColor", PseudoColor},
4800 {"TrueColor", TrueColor},
4801 {"DirectColor", DirectColor},
4802 NULL
4806 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4808 /* Value is the screen number of screen SCR. This is a substitute for
4809 the X function with the same name when that doesn't exist. */
4812 XScreenNumberOfScreen (scr)
4813 register Screen *scr;
4815 Display *dpy = scr->display;
4816 int i;
4818 for (i = 0; i < dpy->nscreens; ++i)
4819 if (scr == dpy->screens[i])
4820 break;
4822 return i;
4825 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4828 /* Select the visual that should be used on display DPYINFO. Set
4829 members of DPYINFO appropriately. Called from x_term_init. */
4831 void
4832 select_visual (dpyinfo)
4833 struct x_display_info *dpyinfo;
4835 Display *dpy = dpyinfo->display;
4836 Screen *screen = dpyinfo->screen;
4837 Lisp_Object value;
4839 /* See if a visual is specified. */
4840 value = display_x_get_resource (dpyinfo,
4841 build_string ("visualClass"),
4842 build_string ("VisualClass"),
4843 Qnil, Qnil);
4844 if (STRINGP (value))
4846 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4847 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4848 depth, a decimal number. NAME is compared with case ignored. */
4849 char *s = (char *) alloca (STRING_BYTES (XSTRING (value)) + 1);
4850 char *dash;
4851 int i, class = -1;
4852 XVisualInfo vinfo;
4854 strcpy (s, XSTRING (value)->data);
4855 dash = index (s, '-');
4856 if (dash)
4858 dpyinfo->n_planes = atoi (dash + 1);
4859 *dash = '\0';
4861 else
4862 /* We won't find a matching visual with depth 0, so that
4863 an error will be printed below. */
4864 dpyinfo->n_planes = 0;
4866 /* Determine the visual class. */
4867 for (i = 0; visual_classes[i].name; ++i)
4868 if (xstricmp (s, visual_classes[i].name) == 0)
4870 class = visual_classes[i].class;
4871 break;
4874 /* Look up a matching visual for the specified class. */
4875 if (class == -1
4876 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4877 dpyinfo->n_planes, class, &vinfo))
4878 fatal ("Invalid visual specification `%s'", XSTRING (value)->data);
4880 dpyinfo->visual = vinfo.visual;
4882 else
4884 int n_visuals;
4885 XVisualInfo *vinfo, vinfo_template;
4887 dpyinfo->visual = DefaultVisualOfScreen (screen);
4889 #ifdef HAVE_X11R4
4890 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4891 #else
4892 vinfo_template.visualid = dpyinfo->visual->visualid;
4893 #endif
4894 vinfo_template.screen = XScreenNumberOfScreen (screen);
4895 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4896 &vinfo_template, &n_visuals);
4897 if (n_visuals != 1)
4898 fatal ("Can't get proper X visual info");
4900 dpyinfo->n_planes = vinfo->depth;
4901 XFree ((char *) vinfo);
4906 /* Return the X display structure for the display named NAME.
4907 Open a new connection if necessary. */
4909 struct x_display_info *
4910 x_display_info_for_name (name)
4911 Lisp_Object name;
4913 Lisp_Object names;
4914 struct x_display_info *dpyinfo;
4916 CHECK_STRING (name, 0);
4918 if (! EQ (Vwindow_system, intern ("x")))
4919 error ("Not using X Windows");
4921 for (dpyinfo = x_display_list, names = x_display_name_list;
4922 dpyinfo;
4923 dpyinfo = dpyinfo->next, names = XCDR (names))
4925 Lisp_Object tem;
4926 tem = Fstring_equal (XCAR (XCAR (names)), name);
4927 if (!NILP (tem))
4928 return dpyinfo;
4931 /* Use this general default value to start with. */
4932 Vx_resource_name = Vinvocation_name;
4934 validate_x_resource_name ();
4936 dpyinfo = x_term_init (name, (unsigned char *)0,
4937 (char *) XSTRING (Vx_resource_name)->data);
4939 if (dpyinfo == 0)
4940 error ("Cannot connect to X server %s", XSTRING (name)->data);
4942 x_in_use = 1;
4943 XSETFASTINT (Vwindow_system_version, 11);
4945 return dpyinfo;
4949 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4950 1, 3, 0, "Open a connection to an X server.\n\
4951 DISPLAY is the name of the display to connect to.\n\
4952 Optional second arg XRM-STRING is a string of resources in xrdb format.\n\
4953 If the optional third arg MUST-SUCCEED is non-nil,\n\
4954 terminate Emacs if we can't open the connection.")
4955 (display, xrm_string, must_succeed)
4956 Lisp_Object display, xrm_string, must_succeed;
4958 unsigned char *xrm_option;
4959 struct x_display_info *dpyinfo;
4961 CHECK_STRING (display, 0);
4962 if (! NILP (xrm_string))
4963 CHECK_STRING (xrm_string, 1);
4965 if (! EQ (Vwindow_system, intern ("x")))
4966 error ("Not using X Windows");
4968 if (! NILP (xrm_string))
4969 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
4970 else
4971 xrm_option = (unsigned char *) 0;
4973 validate_x_resource_name ();
4975 /* This is what opens the connection and sets x_current_display.
4976 This also initializes many symbols, such as those used for input. */
4977 dpyinfo = x_term_init (display, xrm_option,
4978 (char *) XSTRING (Vx_resource_name)->data);
4980 if (dpyinfo == 0)
4982 if (!NILP (must_succeed))
4983 fatal ("Cannot connect to X server %s.\n\
4984 Check the DISPLAY environment variable or use `-d'.\n\
4985 Also use the `xhost' program to verify that it is set to permit\n\
4986 connections from your machine.\n",
4987 XSTRING (display)->data);
4988 else
4989 error ("Cannot connect to X server %s", XSTRING (display)->data);
4992 x_in_use = 1;
4994 XSETFASTINT (Vwindow_system_version, 11);
4995 return Qnil;
4998 DEFUN ("x-close-connection", Fx_close_connection,
4999 Sx_close_connection, 1, 1, 0,
5000 "Close the connection to DISPLAY's X server.\n\
5001 For DISPLAY, specify either a frame or a display name (a string).\n\
5002 If DISPLAY is nil, that stands for the selected frame's display.")
5003 (display)
5004 Lisp_Object display;
5006 struct x_display_info *dpyinfo = check_x_display_info (display);
5007 int i;
5009 if (dpyinfo->reference_count > 0)
5010 error ("Display still has frames on it");
5012 BLOCK_INPUT;
5013 /* Free the fonts in the font table. */
5014 for (i = 0; i < dpyinfo->n_fonts; i++)
5015 if (dpyinfo->font_table[i].name)
5017 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
5018 xfree (dpyinfo->font_table[i].full_name);
5019 xfree (dpyinfo->font_table[i].name);
5020 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
5023 x_destroy_all_bitmaps (dpyinfo);
5024 XSetCloseDownMode (dpyinfo->display, DestroyAll);
5026 #ifdef USE_X_TOOLKIT
5027 XtCloseDisplay (dpyinfo->display);
5028 #else
5029 XCloseDisplay (dpyinfo->display);
5030 #endif
5032 x_delete_display (dpyinfo);
5033 UNBLOCK_INPUT;
5035 return Qnil;
5038 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
5039 "Return the list of display names that Emacs has connections to.")
5042 Lisp_Object tail, result;
5044 result = Qnil;
5045 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
5046 result = Fcons (XCAR (XCAR (tail)), result);
5048 return result;
5051 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
5052 "If ON is non-nil, report X errors as soon as the erring request is made.\n\
5053 If ON is nil, allow buffering of requests.\n\
5054 Turning on synchronization prohibits the Xlib routines from buffering\n\
5055 requests and seriously degrades performance, but makes debugging much\n\
5056 easier.\n\
5057 The optional second argument DISPLAY specifies which display to act on.\n\
5058 DISPLAY should be either a frame or a display name (a string).\n\
5059 If DISPLAY is omitted or nil, that stands for the selected frame's display.")
5060 (on, display)
5061 Lisp_Object display, on;
5063 struct x_display_info *dpyinfo = check_x_display_info (display);
5065 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
5067 return Qnil;
5070 /* Wait for responses to all X commands issued so far for frame F. */
5072 void
5073 x_sync (f)
5074 FRAME_PTR f;
5076 BLOCK_INPUT;
5077 XSync (FRAME_X_DISPLAY (f), False);
5078 UNBLOCK_INPUT;
5082 /***********************************************************************
5083 Image types
5084 ***********************************************************************/
5086 /* Value is the number of elements of vector VECTOR. */
5088 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
5090 /* List of supported image types. Use define_image_type to add new
5091 types. Use lookup_image_type to find a type for a given symbol. */
5093 static struct image_type *image_types;
5095 /* The symbol `image' which is the car of the lists used to represent
5096 images in Lisp. */
5098 extern Lisp_Object Qimage;
5100 /* The symbol `xbm' which is used as the type symbol for XBM images. */
5102 Lisp_Object Qxbm;
5104 /* Keywords. */
5106 extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
5107 extern Lisp_Object QCdata;
5108 Lisp_Object QCtype, QCascent, QCmargin, QCrelief;
5109 Lisp_Object QCalgorithm, QCcolor_symbols, QCheuristic_mask;
5110 Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
5112 /* Other symbols. */
5114 Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
5116 /* Time in seconds after which images should be removed from the cache
5117 if not displayed. */
5119 Lisp_Object Vimage_cache_eviction_delay;
5121 /* Function prototypes. */
5123 static void define_image_type P_ ((struct image_type *type));
5124 static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
5125 static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
5126 static void x_laplace P_ ((struct frame *, struct image *));
5127 static void x_emboss P_ ((struct frame *, struct image *));
5128 static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
5129 Lisp_Object));
5132 /* Define a new image type from TYPE. This adds a copy of TYPE to
5133 image_types and adds the symbol *TYPE->type to Vimage_types. */
5135 static void
5136 define_image_type (type)
5137 struct image_type *type;
5139 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
5140 The initialized data segment is read-only. */
5141 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
5142 bcopy (type, p, sizeof *p);
5143 p->next = image_types;
5144 image_types = p;
5145 Vimage_types = Fcons (*p->type, Vimage_types);
5149 /* Look up image type SYMBOL, and return a pointer to its image_type
5150 structure. Value is null if SYMBOL is not a known image type. */
5152 static INLINE struct image_type *
5153 lookup_image_type (symbol)
5154 Lisp_Object symbol;
5156 struct image_type *type;
5158 for (type = image_types; type; type = type->next)
5159 if (EQ (symbol, *type->type))
5160 break;
5162 return type;
5166 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
5167 valid image specification is a list whose car is the symbol
5168 `image', and whose rest is a property list. The property list must
5169 contain a value for key `:type'. That value must be the name of a
5170 supported image type. The rest of the property list depends on the
5171 image type. */
5174 valid_image_p (object)
5175 Lisp_Object object;
5177 int valid_p = 0;
5179 if (CONSP (object) && EQ (XCAR (object), Qimage))
5181 Lisp_Object symbol = Fplist_get (XCDR (object), QCtype);
5182 struct image_type *type = lookup_image_type (symbol);
5184 if (type)
5185 valid_p = type->valid_p (object);
5188 return valid_p;
5192 /* Log error message with format string FORMAT and argument ARG.
5193 Signaling an error, e.g. when an image cannot be loaded, is not a
5194 good idea because this would interrupt redisplay, and the error
5195 message display would lead to another redisplay. This function
5196 therefore simply displays a message. */
5198 static void
5199 image_error (format, arg1, arg2)
5200 char *format;
5201 Lisp_Object arg1, arg2;
5203 add_to_log (format, arg1, arg2);
5208 /***********************************************************************
5209 Image specifications
5210 ***********************************************************************/
5212 enum image_value_type
5214 IMAGE_DONT_CHECK_VALUE_TYPE,
5215 IMAGE_STRING_VALUE,
5216 IMAGE_SYMBOL_VALUE,
5217 IMAGE_POSITIVE_INTEGER_VALUE,
5218 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
5219 IMAGE_ASCENT_VALUE,
5220 IMAGE_INTEGER_VALUE,
5221 IMAGE_FUNCTION_VALUE,
5222 IMAGE_NUMBER_VALUE,
5223 IMAGE_BOOL_VALUE
5226 /* Structure used when parsing image specifications. */
5228 struct image_keyword
5230 /* Name of keyword. */
5231 char *name;
5233 /* The type of value allowed. */
5234 enum image_value_type type;
5236 /* Non-zero means key must be present. */
5237 int mandatory_p;
5239 /* Used to recognize duplicate keywords in a property list. */
5240 int count;
5242 /* The value that was found. */
5243 Lisp_Object value;
5247 static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
5248 int, Lisp_Object));
5249 static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
5252 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
5253 has the format (image KEYWORD VALUE ...). One of the keyword/
5254 value pairs must be `:type TYPE'. KEYWORDS is a vector of
5255 image_keywords structures of size NKEYWORDS describing other
5256 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
5258 static int
5259 parse_image_spec (spec, keywords, nkeywords, type)
5260 Lisp_Object spec;
5261 struct image_keyword *keywords;
5262 int nkeywords;
5263 Lisp_Object type;
5265 int i;
5266 Lisp_Object plist;
5268 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
5269 return 0;
5271 plist = XCDR (spec);
5272 while (CONSP (plist))
5274 Lisp_Object key, value;
5276 /* First element of a pair must be a symbol. */
5277 key = XCAR (plist);
5278 plist = XCDR (plist);
5279 if (!SYMBOLP (key))
5280 return 0;
5282 /* There must follow a value. */
5283 if (!CONSP (plist))
5284 return 0;
5285 value = XCAR (plist);
5286 plist = XCDR (plist);
5288 /* Find key in KEYWORDS. Error if not found. */
5289 for (i = 0; i < nkeywords; ++i)
5290 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
5291 break;
5293 if (i == nkeywords)
5294 continue;
5296 /* Record that we recognized the keyword. If a keywords
5297 was found more than once, it's an error. */
5298 keywords[i].value = value;
5299 ++keywords[i].count;
5301 if (keywords[i].count > 1)
5302 return 0;
5304 /* Check type of value against allowed type. */
5305 switch (keywords[i].type)
5307 case IMAGE_STRING_VALUE:
5308 if (!STRINGP (value))
5309 return 0;
5310 break;
5312 case IMAGE_SYMBOL_VALUE:
5313 if (!SYMBOLP (value))
5314 return 0;
5315 break;
5317 case IMAGE_POSITIVE_INTEGER_VALUE:
5318 if (!INTEGERP (value) || XINT (value) <= 0)
5319 return 0;
5320 break;
5322 case IMAGE_ASCENT_VALUE:
5323 if (SYMBOLP (value) && EQ (value, Qcenter))
5324 break;
5325 else if (INTEGERP (value)
5326 && XINT (value) >= 0
5327 && XINT (value) <= 100)
5328 break;
5329 return 0;
5331 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
5332 if (!INTEGERP (value) || XINT (value) < 0)
5333 return 0;
5334 break;
5336 case IMAGE_DONT_CHECK_VALUE_TYPE:
5337 break;
5339 case IMAGE_FUNCTION_VALUE:
5340 value = indirect_function (value);
5341 if (SUBRP (value)
5342 || COMPILEDP (value)
5343 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
5344 break;
5345 return 0;
5347 case IMAGE_NUMBER_VALUE:
5348 if (!INTEGERP (value) && !FLOATP (value))
5349 return 0;
5350 break;
5352 case IMAGE_INTEGER_VALUE:
5353 if (!INTEGERP (value))
5354 return 0;
5355 break;
5357 case IMAGE_BOOL_VALUE:
5358 if (!NILP (value) && !EQ (value, Qt))
5359 return 0;
5360 break;
5362 default:
5363 abort ();
5364 break;
5367 if (EQ (key, QCtype) && !EQ (type, value))
5368 return 0;
5371 /* Check that all mandatory fields are present. */
5372 for (i = 0; i < nkeywords; ++i)
5373 if (keywords[i].mandatory_p && keywords[i].count == 0)
5374 return 0;
5376 return NILP (plist);
5380 /* Return the value of KEY in image specification SPEC. Value is nil
5381 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
5382 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
5384 static Lisp_Object
5385 image_spec_value (spec, key, found)
5386 Lisp_Object spec, key;
5387 int *found;
5389 Lisp_Object tail;
5391 xassert (valid_image_p (spec));
5393 for (tail = XCDR (spec);
5394 CONSP (tail) && CONSP (XCDR (tail));
5395 tail = XCDR (XCDR (tail)))
5397 if (EQ (XCAR (tail), key))
5399 if (found)
5400 *found = 1;
5401 return XCAR (XCDR (tail));
5405 if (found)
5406 *found = 0;
5407 return Qnil;
5411 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
5412 "Return the size of image SPEC as pair (WIDTH . HEIGHT).\n\
5413 PIXELS non-nil means return the size in pixels, otherwise return the\n\
5414 size in canonical character units.\n\
5415 FRAME is the frame on which the image will be displayed. FRAME nil\n\
5416 or omitted means use the selected frame.")
5417 (spec, pixels, frame)
5418 Lisp_Object spec, pixels, frame;
5420 Lisp_Object size;
5422 size = Qnil;
5423 if (valid_image_p (spec))
5425 struct frame *f = check_x_frame (frame);
5426 int id = lookup_image (f, spec);
5427 struct image *img = IMAGE_FROM_ID (f, id);
5428 int width = img->width + 2 * img->margin;
5429 int height = img->height + 2 * img->margin;
5431 if (NILP (pixels))
5432 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
5433 make_float ((double) height / CANON_Y_UNIT (f)));
5434 else
5435 size = Fcons (make_number (width), make_number (height));
5437 else
5438 error ("Invalid image specification");
5440 return size;
5444 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
5445 "Return t if image SPEC has a mask bitmap.\n\
5446 FRAME is the frame on which the image will be displayed. FRAME nil\n\
5447 or omitted means use the selected frame.")
5448 (spec, frame)
5449 Lisp_Object spec, frame;
5451 Lisp_Object mask;
5453 mask = Qnil;
5454 if (valid_image_p (spec))
5456 struct frame *f = check_x_frame (frame);
5457 int id = lookup_image (f, spec);
5458 struct image *img = IMAGE_FROM_ID (f, id);
5459 if (img->mask)
5460 mask = Qt;
5462 else
5463 error ("Invalid image specification");
5465 return mask;
5470 /***********************************************************************
5471 Image type independent image structures
5472 ***********************************************************************/
5474 static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
5475 static void free_image P_ ((struct frame *f, struct image *img));
5478 /* Allocate and return a new image structure for image specification
5479 SPEC. SPEC has a hash value of HASH. */
5481 static struct image *
5482 make_image (spec, hash)
5483 Lisp_Object spec;
5484 unsigned hash;
5486 struct image *img = (struct image *) xmalloc (sizeof *img);
5488 xassert (valid_image_p (spec));
5489 bzero (img, sizeof *img);
5490 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
5491 xassert (img->type != NULL);
5492 img->spec = spec;
5493 img->data.lisp_val = Qnil;
5494 img->ascent = DEFAULT_IMAGE_ASCENT;
5495 img->hash = hash;
5496 return img;
5500 /* Free image IMG which was used on frame F, including its resources. */
5502 static void
5503 free_image (f, img)
5504 struct frame *f;
5505 struct image *img;
5507 if (img)
5509 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5511 /* Remove IMG from the hash table of its cache. */
5512 if (img->prev)
5513 img->prev->next = img->next;
5514 else
5515 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
5517 if (img->next)
5518 img->next->prev = img->prev;
5520 c->images[img->id] = NULL;
5522 /* Free resources, then free IMG. */
5523 img->type->free (f, img);
5524 xfree (img);
5529 /* Prepare image IMG for display on frame F. Must be called before
5530 drawing an image. */
5532 void
5533 prepare_image_for_display (f, img)
5534 struct frame *f;
5535 struct image *img;
5537 EMACS_TIME t;
5539 /* We're about to display IMG, so set its timestamp to `now'. */
5540 EMACS_GET_TIME (t);
5541 img->timestamp = EMACS_SECS (t);
5543 /* If IMG doesn't have a pixmap yet, load it now, using the image
5544 type dependent loader function. */
5545 if (img->pixmap == None && !img->load_failed_p)
5546 img->load_failed_p = img->type->load (f, img) == 0;
5550 /* Value is the number of pixels for the ascent of image IMG when
5551 drawn in face FACE. */
5554 image_ascent (img, face)
5555 struct image *img;
5556 struct face *face;
5558 int height = img->height + img->margin;
5559 int ascent;
5561 if (img->ascent == CENTERED_IMAGE_ASCENT)
5563 if (face->font)
5564 /* This expression is arranged so that if the image can't be
5565 exactly centered, it will be moved slightly up. This is
5566 because a typical font is `top-heavy' (due to the presence
5567 uppercase letters), so the image placement should err towards
5568 being top-heavy too. It also just generally looks better. */
5569 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
5570 else
5571 ascent = height / 2;
5573 else
5574 ascent = height * img->ascent / 100.0;
5576 return ascent;
5581 /***********************************************************************
5582 Helper functions for X image types
5583 ***********************************************************************/
5585 static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
5586 int, int));
5587 static void x_clear_image P_ ((struct frame *f, struct image *img));
5588 static unsigned long x_alloc_image_color P_ ((struct frame *f,
5589 struct image *img,
5590 Lisp_Object color_name,
5591 unsigned long dflt));
5594 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
5595 free the pixmap if any. MASK_P non-zero means clear the mask
5596 pixmap if any. COLORS_P non-zero means free colors allocated for
5597 the image, if any. */
5599 static void
5600 x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
5601 struct frame *f;
5602 struct image *img;
5603 int pixmap_p, mask_p, colors_p;
5605 if (pixmap_p && img->pixmap)
5607 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
5608 img->pixmap = None;
5611 if (mask_p && img->mask)
5613 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
5614 img->mask = None;
5617 if (colors_p && img->ncolors)
5619 x_free_colors (f, img->colors, img->ncolors);
5620 xfree (img->colors);
5621 img->colors = NULL;
5622 img->ncolors = 0;
5626 /* Free X resources of image IMG which is used on frame F. */
5628 static void
5629 x_clear_image (f, img)
5630 struct frame *f;
5631 struct image *img;
5633 BLOCK_INPUT;
5634 x_clear_image_1 (f, img, 1, 1, 1);
5635 UNBLOCK_INPUT;
5639 /* Allocate color COLOR_NAME for image IMG on frame F. If color
5640 cannot be allocated, use DFLT. Add a newly allocated color to
5641 IMG->colors, so that it can be freed again. Value is the pixel
5642 color. */
5644 static unsigned long
5645 x_alloc_image_color (f, img, color_name, dflt)
5646 struct frame *f;
5647 struct image *img;
5648 Lisp_Object color_name;
5649 unsigned long dflt;
5651 XColor color;
5652 unsigned long result;
5654 xassert (STRINGP (color_name));
5656 if (x_defined_color (f, XSTRING (color_name)->data, &color, 1))
5658 /* This isn't called frequently so we get away with simply
5659 reallocating the color vector to the needed size, here. */
5660 ++img->ncolors;
5661 img->colors =
5662 (unsigned long *) xrealloc (img->colors,
5663 img->ncolors * sizeof *img->colors);
5664 img->colors[img->ncolors - 1] = color.pixel;
5665 result = color.pixel;
5667 else
5668 result = dflt;
5670 return result;
5675 /***********************************************************************
5676 Image Cache
5677 ***********************************************************************/
5679 static void cache_image P_ ((struct frame *f, struct image *img));
5682 /* Return a new, initialized image cache that is allocated from the
5683 heap. Call free_image_cache to free an image cache. */
5685 struct image_cache *
5686 make_image_cache ()
5688 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
5689 int size;
5691 bzero (c, sizeof *c);
5692 c->size = 50;
5693 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
5694 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5695 c->buckets = (struct image **) xmalloc (size);
5696 bzero (c->buckets, size);
5697 return c;
5701 /* Free image cache of frame F. Be aware that X frames share images
5702 caches. */
5704 void
5705 free_image_cache (f)
5706 struct frame *f;
5708 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5709 if (c)
5711 int i;
5713 /* Cache should not be referenced by any frame when freed. */
5714 xassert (c->refcount == 0);
5716 for (i = 0; i < c->used; ++i)
5717 free_image (f, c->images[i]);
5718 xfree (c->images);
5719 xfree (c->buckets);
5720 xfree (c);
5721 FRAME_X_IMAGE_CACHE (f) = NULL;
5726 /* Clear image cache of frame F. FORCE_P non-zero means free all
5727 images. FORCE_P zero means clear only images that haven't been
5728 displayed for some time. Should be called from time to time to
5729 reduce the number of loaded images. If image-eviction-seconds is
5730 non-nil, this frees images in the cache which weren't displayed for
5731 at least that many seconds. */
5733 void
5734 clear_image_cache (f, force_p)
5735 struct frame *f;
5736 int force_p;
5738 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5740 if (c && INTEGERP (Vimage_cache_eviction_delay))
5742 EMACS_TIME t;
5743 unsigned long old;
5744 int i, nfreed;
5746 EMACS_GET_TIME (t);
5747 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
5749 /* Block input so that we won't be interrupted by a SIGIO
5750 while being in an inconsistent state. */
5751 BLOCK_INPUT;
5753 for (i = nfreed = 0; i < c->used; ++i)
5755 struct image *img = c->images[i];
5756 if (img != NULL
5757 && (force_p || img->timestamp < old))
5759 free_image (f, img);
5760 ++nfreed;
5764 /* We may be clearing the image cache because, for example,
5765 Emacs was iconified for a longer period of time. In that
5766 case, current matrices may still contain references to
5767 images freed above. So, clear these matrices. */
5768 if (nfreed)
5770 Lisp_Object tail, frame;
5772 FOR_EACH_FRAME (tail, frame)
5774 struct frame *f = XFRAME (frame);
5775 if (FRAME_X_P (f)
5776 && FRAME_X_IMAGE_CACHE (f) == c)
5777 clear_current_matrices (f);
5780 ++windows_or_buffers_changed;
5783 UNBLOCK_INPUT;
5788 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
5789 0, 1, 0,
5790 "Clear the image cache of FRAME.\n\
5791 FRAME nil or omitted means use the selected frame.\n\
5792 FRAME t means clear the image caches of all frames.")
5793 (frame)
5794 Lisp_Object frame;
5796 if (EQ (frame, Qt))
5798 Lisp_Object tail;
5800 FOR_EACH_FRAME (tail, frame)
5801 if (FRAME_X_P (XFRAME (frame)))
5802 clear_image_cache (XFRAME (frame), 1);
5804 else
5805 clear_image_cache (check_x_frame (frame), 1);
5807 return Qnil;
5811 /* Return the id of image with Lisp specification SPEC on frame F.
5812 SPEC must be a valid Lisp image specification (see valid_image_p). */
5815 lookup_image (f, spec)
5816 struct frame *f;
5817 Lisp_Object spec;
5819 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5820 struct image *img;
5821 int i;
5822 unsigned hash;
5823 struct gcpro gcpro1;
5824 EMACS_TIME now;
5826 /* F must be a window-system frame, and SPEC must be a valid image
5827 specification. */
5828 xassert (FRAME_WINDOW_P (f));
5829 xassert (valid_image_p (spec));
5831 GCPRO1 (spec);
5833 /* Look up SPEC in the hash table of the image cache. */
5834 hash = sxhash (spec, 0);
5835 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
5837 for (img = c->buckets[i]; img; img = img->next)
5838 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
5839 break;
5841 /* If not found, create a new image and cache it. */
5842 if (img == NULL)
5844 BLOCK_INPUT;
5845 img = make_image (spec, hash);
5846 cache_image (f, img);
5847 img->load_failed_p = img->type->load (f, img) == 0;
5849 /* If we can't load the image, and we don't have a width and
5850 height, use some arbitrary width and height so that we can
5851 draw a rectangle for it. */
5852 if (img->load_failed_p)
5854 Lisp_Object value;
5856 value = image_spec_value (spec, QCwidth, NULL);
5857 img->width = (INTEGERP (value)
5858 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
5859 value = image_spec_value (spec, QCheight, NULL);
5860 img->height = (INTEGERP (value)
5861 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
5863 else
5865 /* Handle image type independent image attributes
5866 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF'. */
5867 Lisp_Object ascent, margin, relief;
5868 Lisp_Object file;
5870 ascent = image_spec_value (spec, QCascent, NULL);
5871 if (INTEGERP (ascent))
5872 img->ascent = XFASTINT (ascent);
5873 else if (EQ (ascent, Qcenter))
5874 img->ascent = CENTERED_IMAGE_ASCENT;
5876 margin = image_spec_value (spec, QCmargin, NULL);
5877 if (INTEGERP (margin) && XINT (margin) >= 0)
5878 img->margin = XFASTINT (margin);
5880 relief = image_spec_value (spec, QCrelief, NULL);
5881 if (INTEGERP (relief))
5883 img->relief = XINT (relief);
5884 img->margin += abs (img->relief);
5887 /* Manipulation of the image's mask. */
5888 if (img->pixmap)
5890 /* `:heuristic-mask t'
5891 `:mask heuristic'
5892 means build a mask heuristically.
5893 `:heuristic-mask (R G B)'
5894 `:mask (heuristic (R G B))'
5895 means build a mask from color (R G B) in the
5896 image.
5897 `:mask nil'
5898 means remove a mask, if any. */
5900 Lisp_Object mask;
5902 mask = image_spec_value (spec, QCheuristic_mask, NULL);
5903 if (!NILP (mask))
5904 x_build_heuristic_mask (f, img, mask);
5905 else
5907 int found_p;
5909 mask = image_spec_value (spec, QCmask, &found_p);
5911 if (EQ (mask, Qheuristic))
5912 x_build_heuristic_mask (f, img, Qt);
5913 else if (CONSP (mask)
5914 && EQ (XCAR (mask), Qheuristic))
5916 if (CONSP (XCDR (mask)))
5917 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
5918 else
5919 x_build_heuristic_mask (f, img, XCDR (mask));
5921 else if (NILP (mask) && found_p && img->mask)
5923 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
5924 img->mask = None;
5929 /* Should we apply an image transformation algorithm? */
5930 if (img->pixmap)
5932 Lisp_Object algorithm;
5934 algorithm = image_spec_value (spec, QCalgorithm, NULL);
5935 if (EQ (algorithm, Qdisabled))
5936 x_disable_image (f, img);
5937 else if (EQ (algorithm, Qlaplace))
5938 x_laplace (f, img);
5939 else if (EQ (algorithm, Qemboss))
5940 x_emboss (f, img);
5941 else if (CONSP (algorithm)
5942 && EQ (XCAR (algorithm), Qedge_detection))
5944 Lisp_Object tem;
5945 tem = XCDR (algorithm);
5946 if (CONSP (tem))
5947 x_edge_detection (f, img,
5948 Fplist_get (tem, QCmatrix),
5949 Fplist_get (tem, QCcolor_adjustment));
5954 UNBLOCK_INPUT;
5955 xassert (!interrupt_input_blocked);
5958 /* We're using IMG, so set its timestamp to `now'. */
5959 EMACS_GET_TIME (now);
5960 img->timestamp = EMACS_SECS (now);
5962 UNGCPRO;
5964 /* Value is the image id. */
5965 return img->id;
5969 /* Cache image IMG in the image cache of frame F. */
5971 static void
5972 cache_image (f, img)
5973 struct frame *f;
5974 struct image *img;
5976 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5977 int i;
5979 /* Find a free slot in c->images. */
5980 for (i = 0; i < c->used; ++i)
5981 if (c->images[i] == NULL)
5982 break;
5984 /* If no free slot found, maybe enlarge c->images. */
5985 if (i == c->used && c->used == c->size)
5987 c->size *= 2;
5988 c->images = (struct image **) xrealloc (c->images,
5989 c->size * sizeof *c->images);
5992 /* Add IMG to c->images, and assign IMG an id. */
5993 c->images[i] = img;
5994 img->id = i;
5995 if (i == c->used)
5996 ++c->used;
5998 /* Add IMG to the cache's hash table. */
5999 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
6000 img->next = c->buckets[i];
6001 if (img->next)
6002 img->next->prev = img;
6003 img->prev = NULL;
6004 c->buckets[i] = img;
6008 /* Call FN on every image in the image cache of frame F. Used to mark
6009 Lisp Objects in the image cache. */
6011 void
6012 forall_images_in_image_cache (f, fn)
6013 struct frame *f;
6014 void (*fn) P_ ((struct image *img));
6016 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
6018 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6019 if (c)
6021 int i;
6022 for (i = 0; i < c->used; ++i)
6023 if (c->images[i])
6024 fn (c->images[i]);
6031 /***********************************************************************
6032 X support code
6033 ***********************************************************************/
6035 static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
6036 XImage **, Pixmap *));
6037 static void x_destroy_x_image P_ ((XImage *));
6038 static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
6041 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
6042 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
6043 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
6044 via xmalloc. Print error messages via image_error if an error
6045 occurs. Value is non-zero if successful. */
6047 static int
6048 x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
6049 struct frame *f;
6050 int width, height, depth;
6051 XImage **ximg;
6052 Pixmap *pixmap;
6054 Display *display = FRAME_X_DISPLAY (f);
6055 Screen *screen = FRAME_X_SCREEN (f);
6056 Window window = FRAME_X_WINDOW (f);
6058 xassert (interrupt_input_blocked);
6060 if (depth <= 0)
6061 depth = DefaultDepthOfScreen (screen);
6062 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
6063 depth, ZPixmap, 0, NULL, width, height,
6064 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
6065 if (*ximg == NULL)
6067 image_error ("Unable to allocate X image", Qnil, Qnil);
6068 return 0;
6071 /* Allocate image raster. */
6072 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
6074 /* Allocate a pixmap of the same size. */
6075 *pixmap = XCreatePixmap (display, window, width, height, depth);
6076 if (*pixmap == None)
6078 x_destroy_x_image (*ximg);
6079 *ximg = NULL;
6080 image_error ("Unable to create X pixmap", Qnil, Qnil);
6081 return 0;
6084 return 1;
6088 /* Destroy XImage XIMG. Free XIMG->data. */
6090 static void
6091 x_destroy_x_image (ximg)
6092 XImage *ximg;
6094 xassert (interrupt_input_blocked);
6095 if (ximg)
6097 xfree (ximg->data);
6098 ximg->data = NULL;
6099 XDestroyImage (ximg);
6104 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
6105 are width and height of both the image and pixmap. */
6107 static void
6108 x_put_x_image (f, ximg, pixmap, width, height)
6109 struct frame *f;
6110 XImage *ximg;
6111 Pixmap pixmap;
6113 GC gc;
6115 xassert (interrupt_input_blocked);
6116 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
6117 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
6118 XFreeGC (FRAME_X_DISPLAY (f), gc);
6123 /***********************************************************************
6124 File Handling
6125 ***********************************************************************/
6127 static Lisp_Object x_find_image_file P_ ((Lisp_Object));
6128 static char *slurp_file P_ ((char *, int *));
6131 /* Find image file FILE. Look in data-directory, then
6132 x-bitmap-file-path. Value is the full name of the file found, or
6133 nil if not found. */
6135 static Lisp_Object
6136 x_find_image_file (file)
6137 Lisp_Object file;
6139 Lisp_Object file_found, search_path;
6140 struct gcpro gcpro1, gcpro2;
6141 int fd;
6143 file_found = Qnil;
6144 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
6145 GCPRO2 (file_found, search_path);
6147 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
6148 fd = openp (search_path, file, "", &file_found, 0);
6150 if (fd == -1)
6151 file_found = Qnil;
6152 else
6153 close (fd);
6155 UNGCPRO;
6156 return file_found;
6160 /* Read FILE into memory. Value is a pointer to a buffer allocated
6161 with xmalloc holding FILE's contents. Value is null if an error
6162 occurred. *SIZE is set to the size of the file. */
6164 static char *
6165 slurp_file (file, size)
6166 char *file;
6167 int *size;
6169 FILE *fp = NULL;
6170 char *buf = NULL;
6171 struct stat st;
6173 if (stat (file, &st) == 0
6174 && (fp = fopen (file, "r")) != NULL
6175 && (buf = (char *) xmalloc (st.st_size),
6176 fread (buf, 1, st.st_size, fp) == st.st_size))
6178 *size = st.st_size;
6179 fclose (fp);
6181 else
6183 if (fp)
6184 fclose (fp);
6185 if (buf)
6187 xfree (buf);
6188 buf = NULL;
6192 return buf;
6197 /***********************************************************************
6198 XBM images
6199 ***********************************************************************/
6201 static int xbm_scan P_ ((char **, char *, char *, int *));
6202 static int xbm_load P_ ((struct frame *f, struct image *img));
6203 static int xbm_load_image P_ ((struct frame *f, struct image *img,
6204 char *, char *));
6205 static int xbm_image_p P_ ((Lisp_Object object));
6206 static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
6207 unsigned char **));
6208 static int xbm_file_p P_ ((Lisp_Object));
6211 /* Indices of image specification fields in xbm_format, below. */
6213 enum xbm_keyword_index
6215 XBM_TYPE,
6216 XBM_FILE,
6217 XBM_WIDTH,
6218 XBM_HEIGHT,
6219 XBM_DATA,
6220 XBM_FOREGROUND,
6221 XBM_BACKGROUND,
6222 XBM_ASCENT,
6223 XBM_MARGIN,
6224 XBM_RELIEF,
6225 XBM_ALGORITHM,
6226 XBM_HEURISTIC_MASK,
6227 XBM_MASK,
6228 XBM_LAST
6231 /* Vector of image_keyword structures describing the format
6232 of valid XBM image specifications. */
6234 static struct image_keyword xbm_format[XBM_LAST] =
6236 {":type", IMAGE_SYMBOL_VALUE, 1},
6237 {":file", IMAGE_STRING_VALUE, 0},
6238 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6239 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6240 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6241 {":foreground", IMAGE_STRING_VALUE, 0},
6242 {":background", IMAGE_STRING_VALUE, 0},
6243 {":ascent", IMAGE_ASCENT_VALUE, 0},
6244 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6245 {":relief", IMAGE_INTEGER_VALUE, 0},
6246 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6247 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6248 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6251 /* Structure describing the image type XBM. */
6253 static struct image_type xbm_type =
6255 &Qxbm,
6256 xbm_image_p,
6257 xbm_load,
6258 x_clear_image,
6259 NULL
6262 /* Tokens returned from xbm_scan. */
6264 enum xbm_token
6266 XBM_TK_IDENT = 256,
6267 XBM_TK_NUMBER
6271 /* Return non-zero if OBJECT is a valid XBM-type image specification.
6272 A valid specification is a list starting with the symbol `image'
6273 The rest of the list is a property list which must contain an
6274 entry `:type xbm..
6276 If the specification specifies a file to load, it must contain
6277 an entry `:file FILENAME' where FILENAME is a string.
6279 If the specification is for a bitmap loaded from memory it must
6280 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
6281 WIDTH and HEIGHT are integers > 0. DATA may be:
6283 1. a string large enough to hold the bitmap data, i.e. it must
6284 have a size >= (WIDTH + 7) / 8 * HEIGHT
6286 2. a bool-vector of size >= WIDTH * HEIGHT
6288 3. a vector of strings or bool-vectors, one for each line of the
6289 bitmap.
6291 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
6292 may not be specified in this case because they are defined in the
6293 XBM file.
6295 Both the file and data forms may contain the additional entries
6296 `:background COLOR' and `:foreground COLOR'. If not present,
6297 foreground and background of the frame on which the image is
6298 displayed is used. */
6300 static int
6301 xbm_image_p (object)
6302 Lisp_Object object;
6304 struct image_keyword kw[XBM_LAST];
6306 bcopy (xbm_format, kw, sizeof kw);
6307 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
6308 return 0;
6310 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
6312 if (kw[XBM_FILE].count)
6314 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
6315 return 0;
6317 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
6319 /* In-memory XBM file. */
6320 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
6321 return 0;
6323 else
6325 Lisp_Object data;
6326 int width, height;
6328 /* Entries for `:width', `:height' and `:data' must be present. */
6329 if (!kw[XBM_WIDTH].count
6330 || !kw[XBM_HEIGHT].count
6331 || !kw[XBM_DATA].count)
6332 return 0;
6334 data = kw[XBM_DATA].value;
6335 width = XFASTINT (kw[XBM_WIDTH].value);
6336 height = XFASTINT (kw[XBM_HEIGHT].value);
6338 /* Check type of data, and width and height against contents of
6339 data. */
6340 if (VECTORP (data))
6342 int i;
6344 /* Number of elements of the vector must be >= height. */
6345 if (XVECTOR (data)->size < height)
6346 return 0;
6348 /* Each string or bool-vector in data must be large enough
6349 for one line of the image. */
6350 for (i = 0; i < height; ++i)
6352 Lisp_Object elt = XVECTOR (data)->contents[i];
6354 if (STRINGP (elt))
6356 if (XSTRING (elt)->size
6357 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
6358 return 0;
6360 else if (BOOL_VECTOR_P (elt))
6362 if (XBOOL_VECTOR (elt)->size < width)
6363 return 0;
6365 else
6366 return 0;
6369 else if (STRINGP (data))
6371 if (XSTRING (data)->size
6372 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
6373 return 0;
6375 else if (BOOL_VECTOR_P (data))
6377 if (XBOOL_VECTOR (data)->size < width * height)
6378 return 0;
6380 else
6381 return 0;
6384 return 1;
6388 /* Scan a bitmap file. FP is the stream to read from. Value is
6389 either an enumerator from enum xbm_token, or a character for a
6390 single-character token, or 0 at end of file. If scanning an
6391 identifier, store the lexeme of the identifier in SVAL. If
6392 scanning a number, store its value in *IVAL. */
6394 static int
6395 xbm_scan (s, end, sval, ival)
6396 char **s, *end;
6397 char *sval;
6398 int *ival;
6400 int c;
6402 loop:
6404 /* Skip white space. */
6405 while (*s < end && (c = *(*s)++, isspace (c)))
6408 if (*s >= end)
6409 c = 0;
6410 else if (isdigit (c))
6412 int value = 0, digit;
6414 if (c == '0' && *s < end)
6416 c = *(*s)++;
6417 if (c == 'x' || c == 'X')
6419 while (*s < end)
6421 c = *(*s)++;
6422 if (isdigit (c))
6423 digit = c - '0';
6424 else if (c >= 'a' && c <= 'f')
6425 digit = c - 'a' + 10;
6426 else if (c >= 'A' && c <= 'F')
6427 digit = c - 'A' + 10;
6428 else
6429 break;
6430 value = 16 * value + digit;
6433 else if (isdigit (c))
6435 value = c - '0';
6436 while (*s < end
6437 && (c = *(*s)++, isdigit (c)))
6438 value = 8 * value + c - '0';
6441 else
6443 value = c - '0';
6444 while (*s < end
6445 && (c = *(*s)++, isdigit (c)))
6446 value = 10 * value + c - '0';
6449 if (*s < end)
6450 *s = *s - 1;
6451 *ival = value;
6452 c = XBM_TK_NUMBER;
6454 else if (isalpha (c) || c == '_')
6456 *sval++ = c;
6457 while (*s < end
6458 && (c = *(*s)++, (isalnum (c) || c == '_')))
6459 *sval++ = c;
6460 *sval = 0;
6461 if (*s < end)
6462 *s = *s - 1;
6463 c = XBM_TK_IDENT;
6465 else if (c == '/' && **s == '*')
6467 /* C-style comment. */
6468 ++*s;
6469 while (**s && (**s != '*' || *(*s + 1) != '/'))
6470 ++*s;
6471 if (**s)
6473 *s += 2;
6474 goto loop;
6478 return c;
6482 /* Replacement for XReadBitmapFileData which isn't available under old
6483 X versions. CONTENTS is a pointer to a buffer to parse; END is the
6484 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
6485 the image. Return in *DATA the bitmap data allocated with xmalloc.
6486 Value is non-zero if successful. DATA null means just test if
6487 CONTENTS looks like an in-memory XBM file. */
6489 static int
6490 xbm_read_bitmap_data (contents, end, width, height, data)
6491 char *contents, *end;
6492 int *width, *height;
6493 unsigned char **data;
6495 char *s = contents;
6496 char buffer[BUFSIZ];
6497 int padding_p = 0;
6498 int v10 = 0;
6499 int bytes_per_line, i, nbytes;
6500 unsigned char *p;
6501 int value;
6502 int LA1;
6504 #define match() \
6505 LA1 = xbm_scan (&s, end, buffer, &value)
6507 #define expect(TOKEN) \
6508 if (LA1 != (TOKEN)) \
6509 goto failure; \
6510 else \
6511 match ()
6513 #define expect_ident(IDENT) \
6514 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
6515 match (); \
6516 else \
6517 goto failure
6519 *width = *height = -1;
6520 if (data)
6521 *data = NULL;
6522 LA1 = xbm_scan (&s, end, buffer, &value);
6524 /* Parse defines for width, height and hot-spots. */
6525 while (LA1 == '#')
6527 match ();
6528 expect_ident ("define");
6529 expect (XBM_TK_IDENT);
6531 if (LA1 == XBM_TK_NUMBER);
6533 char *p = strrchr (buffer, '_');
6534 p = p ? p + 1 : buffer;
6535 if (strcmp (p, "width") == 0)
6536 *width = value;
6537 else if (strcmp (p, "height") == 0)
6538 *height = value;
6540 expect (XBM_TK_NUMBER);
6543 if (*width < 0 || *height < 0)
6544 goto failure;
6545 else if (data == NULL)
6546 goto success;
6548 /* Parse bits. Must start with `static'. */
6549 expect_ident ("static");
6550 if (LA1 == XBM_TK_IDENT)
6552 if (strcmp (buffer, "unsigned") == 0)
6554 match ();
6555 expect_ident ("char");
6557 else if (strcmp (buffer, "short") == 0)
6559 match ();
6560 v10 = 1;
6561 if (*width % 16 && *width % 16 < 9)
6562 padding_p = 1;
6564 else if (strcmp (buffer, "char") == 0)
6565 match ();
6566 else
6567 goto failure;
6569 else
6570 goto failure;
6572 expect (XBM_TK_IDENT);
6573 expect ('[');
6574 expect (']');
6575 expect ('=');
6576 expect ('{');
6578 bytes_per_line = (*width + 7) / 8 + padding_p;
6579 nbytes = bytes_per_line * *height;
6580 p = *data = (char *) xmalloc (nbytes);
6582 if (v10)
6584 for (i = 0; i < nbytes; i += 2)
6586 int val = value;
6587 expect (XBM_TK_NUMBER);
6589 *p++ = val;
6590 if (!padding_p || ((i + 2) % bytes_per_line))
6591 *p++ = value >> 8;
6593 if (LA1 == ',' || LA1 == '}')
6594 match ();
6595 else
6596 goto failure;
6599 else
6601 for (i = 0; i < nbytes; ++i)
6603 int val = value;
6604 expect (XBM_TK_NUMBER);
6606 *p++ = val;
6608 if (LA1 == ',' || LA1 == '}')
6609 match ();
6610 else
6611 goto failure;
6615 success:
6616 return 1;
6618 failure:
6620 if (data && *data)
6622 xfree (*data);
6623 *data = NULL;
6625 return 0;
6627 #undef match
6628 #undef expect
6629 #undef expect_ident
6633 /* Load XBM image IMG which will be displayed on frame F from buffer
6634 CONTENTS. END is the end of the buffer. Value is non-zero if
6635 successful. */
6637 static int
6638 xbm_load_image (f, img, contents, end)
6639 struct frame *f;
6640 struct image *img;
6641 char *contents, *end;
6643 int rc;
6644 unsigned char *data;
6645 int success_p = 0;
6647 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
6648 if (rc)
6650 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6651 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
6652 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
6653 Lisp_Object value;
6655 xassert (img->width > 0 && img->height > 0);
6657 /* Get foreground and background colors, maybe allocate colors. */
6658 value = image_spec_value (img->spec, QCforeground, NULL);
6659 if (!NILP (value))
6660 foreground = x_alloc_image_color (f, img, value, foreground);
6662 value = image_spec_value (img->spec, QCbackground, NULL);
6663 if (!NILP (value))
6664 background = x_alloc_image_color (f, img, value, background);
6666 img->pixmap
6667 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6668 FRAME_X_WINDOW (f),
6669 data,
6670 img->width, img->height,
6671 foreground, background,
6672 depth);
6673 xfree (data);
6675 if (img->pixmap == None)
6677 x_clear_image (f, img);
6678 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
6680 else
6681 success_p = 1;
6683 else
6684 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
6686 return success_p;
6690 /* Value is non-zero if DATA looks like an in-memory XBM file. */
6692 static int
6693 xbm_file_p (data)
6694 Lisp_Object data;
6696 int w, h;
6697 return (STRINGP (data)
6698 && xbm_read_bitmap_data (XSTRING (data)->data,
6699 (XSTRING (data)->data
6700 + STRING_BYTES (XSTRING (data))),
6701 &w, &h, NULL));
6705 /* Fill image IMG which is used on frame F with pixmap data. Value is
6706 non-zero if successful. */
6708 static int
6709 xbm_load (f, img)
6710 struct frame *f;
6711 struct image *img;
6713 int success_p = 0;
6714 Lisp_Object file_name;
6716 xassert (xbm_image_p (img->spec));
6718 /* If IMG->spec specifies a file name, create a non-file spec from it. */
6719 file_name = image_spec_value (img->spec, QCfile, NULL);
6720 if (STRINGP (file_name))
6722 Lisp_Object file;
6723 char *contents;
6724 int size;
6725 struct gcpro gcpro1;
6727 file = x_find_image_file (file_name);
6728 GCPRO1 (file);
6729 if (!STRINGP (file))
6731 image_error ("Cannot find image file `%s'", file_name, Qnil);
6732 UNGCPRO;
6733 return 0;
6736 contents = slurp_file (XSTRING (file)->data, &size);
6737 if (contents == NULL)
6739 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
6740 UNGCPRO;
6741 return 0;
6744 success_p = xbm_load_image (f, img, contents, contents + size);
6745 UNGCPRO;
6747 else
6749 struct image_keyword fmt[XBM_LAST];
6750 Lisp_Object data;
6751 unsigned char *bitmap_data;
6752 int depth;
6753 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
6754 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
6755 char *bits;
6756 int parsed_p, height, width;
6757 int in_memory_file_p = 0;
6759 /* See if data looks like an in-memory XBM file. */
6760 data = image_spec_value (img->spec, QCdata, NULL);
6761 in_memory_file_p = xbm_file_p (data);
6763 /* Parse the image specification. */
6764 bcopy (xbm_format, fmt, sizeof fmt);
6765 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
6766 xassert (parsed_p);
6768 /* Get specified width, and height. */
6769 if (!in_memory_file_p)
6771 img->width = XFASTINT (fmt[XBM_WIDTH].value);
6772 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
6773 xassert (img->width > 0 && img->height > 0);
6776 /* Get foreground and background colors, maybe allocate colors. */
6777 if (fmt[XBM_FOREGROUND].count)
6778 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
6779 foreground);
6780 if (fmt[XBM_BACKGROUND].count)
6781 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
6782 background);
6784 if (in_memory_file_p)
6785 success_p = xbm_load_image (f, img, XSTRING (data)->data,
6786 (XSTRING (data)->data
6787 + STRING_BYTES (XSTRING (data))));
6788 else
6790 if (VECTORP (data))
6792 int i;
6793 char *p;
6794 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
6796 p = bits = (char *) alloca (nbytes * img->height);
6797 for (i = 0; i < img->height; ++i, p += nbytes)
6799 Lisp_Object line = XVECTOR (data)->contents[i];
6800 if (STRINGP (line))
6801 bcopy (XSTRING (line)->data, p, nbytes);
6802 else
6803 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
6806 else if (STRINGP (data))
6807 bits = XSTRING (data)->data;
6808 else
6809 bits = XBOOL_VECTOR (data)->data;
6811 /* Create the pixmap. */
6812 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6813 img->pixmap
6814 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6815 FRAME_X_WINDOW (f),
6816 bits,
6817 img->width, img->height,
6818 foreground, background,
6819 depth);
6820 if (img->pixmap)
6821 success_p = 1;
6822 else
6824 image_error ("Unable to create pixmap for XBM image `%s'",
6825 img->spec, Qnil);
6826 x_clear_image (f, img);
6831 return success_p;
6836 /***********************************************************************
6837 XPM images
6838 ***********************************************************************/
6840 #if HAVE_XPM
6842 static int xpm_image_p P_ ((Lisp_Object object));
6843 static int xpm_load P_ ((struct frame *f, struct image *img));
6844 static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
6846 #include "X11/xpm.h"
6848 /* The symbol `xpm' identifying XPM-format images. */
6850 Lisp_Object Qxpm;
6852 /* Indices of image specification fields in xpm_format, below. */
6854 enum xpm_keyword_index
6856 XPM_TYPE,
6857 XPM_FILE,
6858 XPM_DATA,
6859 XPM_ASCENT,
6860 XPM_MARGIN,
6861 XPM_RELIEF,
6862 XPM_ALGORITHM,
6863 XPM_HEURISTIC_MASK,
6864 XPM_MASK,
6865 XPM_COLOR_SYMBOLS,
6866 XPM_LAST
6869 /* Vector of image_keyword structures describing the format
6870 of valid XPM image specifications. */
6872 static struct image_keyword xpm_format[XPM_LAST] =
6874 {":type", IMAGE_SYMBOL_VALUE, 1},
6875 {":file", IMAGE_STRING_VALUE, 0},
6876 {":data", IMAGE_STRING_VALUE, 0},
6877 {":ascent", IMAGE_ASCENT_VALUE, 0},
6878 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6879 {":relief", IMAGE_INTEGER_VALUE, 0},
6880 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6881 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6882 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6883 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6886 /* Structure describing the image type XBM. */
6888 static struct image_type xpm_type =
6890 &Qxpm,
6891 xpm_image_p,
6892 xpm_load,
6893 x_clear_image,
6894 NULL
6898 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
6899 functions for allocating image colors. Our own functions handle
6900 color allocation failures more gracefully than the ones on the XPM
6901 lib. */
6903 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
6904 #define ALLOC_XPM_COLORS
6905 #endif
6907 #ifdef ALLOC_XPM_COLORS
6909 static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
6910 static void xpm_free_color_cache P_ ((void));
6911 static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
6912 static int xpm_color_bucket P_ ((char *));
6913 static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
6914 XColor *, int));
6916 /* An entry in a hash table used to cache color definitions of named
6917 colors. This cache is necessary to speed up XPM image loading in
6918 case we do color allocations ourselves. Without it, we would need
6919 a call to XParseColor per pixel in the image. */
6921 struct xpm_cached_color
6923 /* Next in collision chain. */
6924 struct xpm_cached_color *next;
6926 /* Color definition (RGB and pixel color). */
6927 XColor color;
6929 /* Color name. */
6930 char name[1];
6933 /* The hash table used for the color cache, and its bucket vector
6934 size. */
6936 #define XPM_COLOR_CACHE_BUCKETS 1001
6937 struct xpm_cached_color **xpm_color_cache;
6939 /* Initialize the color cache. */
6941 static void
6942 xpm_init_color_cache (f, attrs)
6943 struct frame *f;
6944 XpmAttributes *attrs;
6946 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
6947 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
6948 memset (xpm_color_cache, 0, nbytes);
6949 init_color_table ();
6951 if (attrs->valuemask & XpmColorSymbols)
6953 int i;
6954 XColor color;
6956 for (i = 0; i < attrs->numsymbols; ++i)
6957 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6958 attrs->colorsymbols[i].value, &color))
6960 color.pixel = lookup_rgb_color (f, color.red, color.green,
6961 color.blue);
6962 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
6968 /* Free the color cache. */
6970 static void
6971 xpm_free_color_cache ()
6973 struct xpm_cached_color *p, *next;
6974 int i;
6976 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
6977 for (p = xpm_color_cache[i]; p; p = next)
6979 next = p->next;
6980 xfree (p);
6983 xfree (xpm_color_cache);
6984 xpm_color_cache = NULL;
6985 free_color_table ();
6989 /* Return the bucket index for color named COLOR_NAME in the color
6990 cache. */
6992 static int
6993 xpm_color_bucket (color_name)
6994 char *color_name;
6996 unsigned h = 0;
6997 char *s;
6999 for (s = color_name; *s; ++s)
7000 h = (h << 2) ^ *s;
7001 return h %= XPM_COLOR_CACHE_BUCKETS;
7005 /* On frame F, cache values COLOR for color with name COLOR_NAME.
7006 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
7007 entry added. */
7009 static struct xpm_cached_color *
7010 xpm_cache_color (f, color_name, color, bucket)
7011 struct frame *f;
7012 char *color_name;
7013 XColor *color;
7014 int bucket;
7016 size_t nbytes;
7017 struct xpm_cached_color *p;
7019 if (bucket < 0)
7020 bucket = xpm_color_bucket (color_name);
7022 nbytes = sizeof *p + strlen (color_name);
7023 p = (struct xpm_cached_color *) xmalloc (nbytes);
7024 strcpy (p->name, color_name);
7025 p->color = *color;
7026 p->next = xpm_color_cache[bucket];
7027 xpm_color_cache[bucket] = p;
7028 return p;
7032 /* Look up color COLOR_NAME for frame F in the color cache. If found,
7033 return the cached definition in *COLOR. Otherwise, make a new
7034 entry in the cache and allocate the color. Value is zero if color
7035 allocation failed. */
7037 static int
7038 xpm_lookup_color (f, color_name, color)
7039 struct frame *f;
7040 char *color_name;
7041 XColor *color;
7043 struct xpm_cached_color *p;
7044 int h = xpm_color_bucket (color_name);
7046 for (p = xpm_color_cache[h]; p; p = p->next)
7047 if (strcmp (p->name, color_name) == 0)
7048 break;
7050 if (p != NULL)
7051 *color = p->color;
7052 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7053 color_name, color))
7055 color->pixel = lookup_rgb_color (f, color->red, color->green,
7056 color->blue);
7057 p = xpm_cache_color (f, color_name, color, h);
7060 return p != NULL;
7064 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
7065 CLOSURE is a pointer to the frame on which we allocate the
7066 color. Return in *COLOR the allocated color. Value is non-zero
7067 if successful. */
7069 static int
7070 xpm_alloc_color (dpy, cmap, color_name, color, closure)
7071 Display *dpy;
7072 Colormap cmap;
7073 char *color_name;
7074 XColor *color;
7075 void *closure;
7077 return xpm_lookup_color ((struct frame *) closure, color_name, color);
7081 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
7082 is a pointer to the frame on which we allocate the color. Value is
7083 non-zero if successful. */
7085 static int
7086 xpm_free_colors (dpy, cmap, pixels, npixels, closure)
7087 Display *dpy;
7088 Colormap cmap;
7089 Pixel *pixels;
7090 int npixels;
7091 void *closure;
7093 return 1;
7096 #endif /* ALLOC_XPM_COLORS */
7099 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
7100 for XPM images. Such a list must consist of conses whose car and
7101 cdr are strings. */
7103 static int
7104 xpm_valid_color_symbols_p (color_symbols)
7105 Lisp_Object color_symbols;
7107 while (CONSP (color_symbols))
7109 Lisp_Object sym = XCAR (color_symbols);
7110 if (!CONSP (sym)
7111 || !STRINGP (XCAR (sym))
7112 || !STRINGP (XCDR (sym)))
7113 break;
7114 color_symbols = XCDR (color_symbols);
7117 return NILP (color_symbols);
7121 /* Value is non-zero if OBJECT is a valid XPM image specification. */
7123 static int
7124 xpm_image_p (object)
7125 Lisp_Object object;
7127 struct image_keyword fmt[XPM_LAST];
7128 bcopy (xpm_format, fmt, sizeof fmt);
7129 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
7130 /* Either `:file' or `:data' must be present. */
7131 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
7132 /* Either no `:color-symbols' or it's a list of conses
7133 whose car and cdr are strings. */
7134 && (fmt[XPM_COLOR_SYMBOLS].count == 0
7135 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
7139 /* Load image IMG which will be displayed on frame F. Value is
7140 non-zero if successful. */
7142 static int
7143 xpm_load (f, img)
7144 struct frame *f;
7145 struct image *img;
7147 int rc, i;
7148 XpmAttributes attrs;
7149 Lisp_Object specified_file, color_symbols;
7151 /* Configure the XPM lib. Use the visual of frame F. Allocate
7152 close colors. Return colors allocated. */
7153 bzero (&attrs, sizeof attrs);
7154 attrs.visual = FRAME_X_VISUAL (f);
7155 attrs.colormap = FRAME_X_COLORMAP (f);
7156 attrs.valuemask |= XpmVisual;
7157 attrs.valuemask |= XpmColormap;
7159 #ifdef ALLOC_XPM_COLORS
7160 /* Allocate colors with our own functions which handle
7161 failing color allocation more gracefully. */
7162 attrs.color_closure = f;
7163 attrs.alloc_color = xpm_alloc_color;
7164 attrs.free_colors = xpm_free_colors;
7165 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
7166 #else /* not ALLOC_XPM_COLORS */
7167 /* Let the XPM lib allocate colors. */
7168 attrs.valuemask |= XpmReturnAllocPixels;
7169 #ifdef XpmAllocCloseColors
7170 attrs.alloc_close_colors = 1;
7171 attrs.valuemask |= XpmAllocCloseColors;
7172 #else /* not XpmAllocCloseColors */
7173 attrs.closeness = 600;
7174 attrs.valuemask |= XpmCloseness;
7175 #endif /* not XpmAllocCloseColors */
7176 #endif /* ALLOC_XPM_COLORS */
7178 /* If image specification contains symbolic color definitions, add
7179 these to `attrs'. */
7180 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
7181 if (CONSP (color_symbols))
7183 Lisp_Object tail;
7184 XpmColorSymbol *xpm_syms;
7185 int i, size;
7187 attrs.valuemask |= XpmColorSymbols;
7189 /* Count number of symbols. */
7190 attrs.numsymbols = 0;
7191 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
7192 ++attrs.numsymbols;
7194 /* Allocate an XpmColorSymbol array. */
7195 size = attrs.numsymbols * sizeof *xpm_syms;
7196 xpm_syms = (XpmColorSymbol *) alloca (size);
7197 bzero (xpm_syms, size);
7198 attrs.colorsymbols = xpm_syms;
7200 /* Fill the color symbol array. */
7201 for (tail = color_symbols, i = 0;
7202 CONSP (tail);
7203 ++i, tail = XCDR (tail))
7205 Lisp_Object name = XCAR (XCAR (tail));
7206 Lisp_Object color = XCDR (XCAR (tail));
7207 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
7208 strcpy (xpm_syms[i].name, XSTRING (name)->data);
7209 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
7210 strcpy (xpm_syms[i].value, XSTRING (color)->data);
7214 /* Create a pixmap for the image, either from a file, or from a
7215 string buffer containing data in the same format as an XPM file. */
7216 #ifdef ALLOC_XPM_COLORS
7217 xpm_init_color_cache (f, &attrs);
7218 #endif
7220 specified_file = image_spec_value (img->spec, QCfile, NULL);
7221 if (STRINGP (specified_file))
7223 Lisp_Object file = x_find_image_file (specified_file);
7224 if (!STRINGP (file))
7226 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7227 return 0;
7230 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7231 XSTRING (file)->data, &img->pixmap, &img->mask,
7232 &attrs);
7234 else
7236 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
7237 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7238 XSTRING (buffer)->data,
7239 &img->pixmap, &img->mask,
7240 &attrs);
7243 if (rc == XpmSuccess)
7245 #ifdef ALLOC_XPM_COLORS
7246 img->colors = colors_in_color_table (&img->ncolors);
7247 #else /* not ALLOC_XPM_COLORS */
7248 img->ncolors = attrs.nalloc_pixels;
7249 img->colors = (unsigned long *) xmalloc (img->ncolors
7250 * sizeof *img->colors);
7251 for (i = 0; i < attrs.nalloc_pixels; ++i)
7253 img->colors[i] = attrs.alloc_pixels[i];
7254 #ifdef DEBUG_X_COLORS
7255 register_color (img->colors[i]);
7256 #endif
7258 #endif /* not ALLOC_XPM_COLORS */
7260 img->width = attrs.width;
7261 img->height = attrs.height;
7262 xassert (img->width > 0 && img->height > 0);
7264 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
7265 XpmFreeAttributes (&attrs);
7267 else
7269 switch (rc)
7271 case XpmOpenFailed:
7272 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
7273 break;
7275 case XpmFileInvalid:
7276 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
7277 break;
7279 case XpmNoMemory:
7280 image_error ("Out of memory (%s)", img->spec, Qnil);
7281 break;
7283 case XpmColorFailed:
7284 image_error ("Color allocation error (%s)", img->spec, Qnil);
7285 break;
7287 default:
7288 image_error ("Unknown error (%s)", img->spec, Qnil);
7289 break;
7293 #ifdef ALLOC_XPM_COLORS
7294 xpm_free_color_cache ();
7295 #endif
7296 return rc == XpmSuccess;
7299 #endif /* HAVE_XPM != 0 */
7302 /***********************************************************************
7303 Color table
7304 ***********************************************************************/
7306 /* An entry in the color table mapping an RGB color to a pixel color. */
7308 struct ct_color
7310 int r, g, b;
7311 unsigned long pixel;
7313 /* Next in color table collision list. */
7314 struct ct_color *next;
7317 /* The bucket vector size to use. Must be prime. */
7319 #define CT_SIZE 101
7321 /* Value is a hash of the RGB color given by R, G, and B. */
7323 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
7325 /* The color hash table. */
7327 struct ct_color **ct_table;
7329 /* Number of entries in the color table. */
7331 int ct_colors_allocated;
7333 /* Initialize the color table. */
7335 static void
7336 init_color_table ()
7338 int size = CT_SIZE * sizeof (*ct_table);
7339 ct_table = (struct ct_color **) xmalloc (size);
7340 bzero (ct_table, size);
7341 ct_colors_allocated = 0;
7345 /* Free memory associated with the color table. */
7347 static void
7348 free_color_table ()
7350 int i;
7351 struct ct_color *p, *next;
7353 for (i = 0; i < CT_SIZE; ++i)
7354 for (p = ct_table[i]; p; p = next)
7356 next = p->next;
7357 xfree (p);
7360 xfree (ct_table);
7361 ct_table = NULL;
7365 /* Value is a pixel color for RGB color R, G, B on frame F. If an
7366 entry for that color already is in the color table, return the
7367 pixel color of that entry. Otherwise, allocate a new color for R,
7368 G, B, and make an entry in the color table. */
7370 static unsigned long
7371 lookup_rgb_color (f, r, g, b)
7372 struct frame *f;
7373 int r, g, b;
7375 unsigned hash = CT_HASH_RGB (r, g, b);
7376 int i = hash % CT_SIZE;
7377 struct ct_color *p;
7379 for (p = ct_table[i]; p; p = p->next)
7380 if (p->r == r && p->g == g && p->b == b)
7381 break;
7383 if (p == NULL)
7385 XColor color;
7386 Colormap cmap;
7387 int rc;
7389 color.red = r;
7390 color.green = g;
7391 color.blue = b;
7393 cmap = FRAME_X_COLORMAP (f);
7394 rc = x_alloc_nearest_color (f, cmap, &color);
7396 if (rc)
7398 ++ct_colors_allocated;
7400 p = (struct ct_color *) xmalloc (sizeof *p);
7401 p->r = r;
7402 p->g = g;
7403 p->b = b;
7404 p->pixel = color.pixel;
7405 p->next = ct_table[i];
7406 ct_table[i] = p;
7408 else
7409 return FRAME_FOREGROUND_PIXEL (f);
7412 return p->pixel;
7416 /* Look up pixel color PIXEL which is used on frame F in the color
7417 table. If not already present, allocate it. Value is PIXEL. */
7419 static unsigned long
7420 lookup_pixel_color (f, pixel)
7421 struct frame *f;
7422 unsigned long pixel;
7424 int i = pixel % CT_SIZE;
7425 struct ct_color *p;
7427 for (p = ct_table[i]; p; p = p->next)
7428 if (p->pixel == pixel)
7429 break;
7431 if (p == NULL)
7433 XColor color;
7434 Colormap cmap;
7435 int rc;
7437 cmap = FRAME_X_COLORMAP (f);
7438 color.pixel = pixel;
7439 x_query_color (f, &color);
7440 rc = x_alloc_nearest_color (f, cmap, &color);
7442 if (rc)
7444 ++ct_colors_allocated;
7446 p = (struct ct_color *) xmalloc (sizeof *p);
7447 p->r = color.red;
7448 p->g = color.green;
7449 p->b = color.blue;
7450 p->pixel = pixel;
7451 p->next = ct_table[i];
7452 ct_table[i] = p;
7454 else
7455 return FRAME_FOREGROUND_PIXEL (f);
7458 return p->pixel;
7462 /* Value is a vector of all pixel colors contained in the color table,
7463 allocated via xmalloc. Set *N to the number of colors. */
7465 static unsigned long *
7466 colors_in_color_table (n)
7467 int *n;
7469 int i, j;
7470 struct ct_color *p;
7471 unsigned long *colors;
7473 if (ct_colors_allocated == 0)
7475 *n = 0;
7476 colors = NULL;
7478 else
7480 colors = (unsigned long *) xmalloc (ct_colors_allocated
7481 * sizeof *colors);
7482 *n = ct_colors_allocated;
7484 for (i = j = 0; i < CT_SIZE; ++i)
7485 for (p = ct_table[i]; p; p = p->next)
7486 colors[j++] = p->pixel;
7489 return colors;
7494 /***********************************************************************
7495 Algorithms
7496 ***********************************************************************/
7498 static void x_laplace_write_row P_ ((struct frame *, long *,
7499 int, XImage *, int));
7500 static void x_laplace_read_row P_ ((struct frame *, Colormap,
7501 XColor *, int, XImage *, int));
7502 static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
7503 static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
7504 static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
7506 /* Non-zero means draw a cross on images having `:algorithm
7507 disabled'. */
7509 int cross_disabled_images;
7511 /* Edge detection matrices for different edge-detection
7512 strategies. */
7514 static int emboss_matrix[9] = {
7515 /* x - 1 x x + 1 */
7516 2, -1, 0, /* y - 1 */
7517 -1, 0, 1, /* y */
7518 0, 1, -2 /* y + 1 */
7521 static int laplace_matrix[9] = {
7522 /* x - 1 x x + 1 */
7523 1, 0, 0, /* y - 1 */
7524 0, 0, 0, /* y */
7525 0, 0, -1 /* y + 1 */
7528 /* Value is the intensity of the color whose red/green/blue values
7529 are R, G, and B. */
7531 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
7534 /* On frame F, return an array of XColor structures describing image
7535 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
7536 non-zero means also fill the red/green/blue members of the XColor
7537 structures. Value is a pointer to the array of XColors structures,
7538 allocated with xmalloc; it must be freed by the caller. */
7540 static XColor *
7541 x_to_xcolors (f, img, rgb_p)
7542 struct frame *f;
7543 struct image *img;
7544 int rgb_p;
7546 int x, y;
7547 XColor *colors, *p;
7548 XImage *ximg;
7550 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
7552 /* Get the X image IMG->pixmap. */
7553 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
7554 0, 0, img->width, img->height, ~0, ZPixmap);
7556 /* Fill the `pixel' members of the XColor array. I wished there
7557 were an easy and portable way to circumvent XGetPixel. */
7558 p = colors;
7559 for (y = 0; y < img->height; ++y)
7561 XColor *row = p;
7563 for (x = 0; x < img->width; ++x, ++p)
7564 p->pixel = XGetPixel (ximg, x, y);
7566 if (rgb_p)
7567 x_query_colors (f, row, img->width);
7570 XDestroyImage (ximg);
7571 return colors;
7575 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
7576 RGB members are set. F is the frame on which this all happens.
7577 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
7579 static void
7580 x_from_xcolors (f, img, colors)
7581 struct frame *f;
7582 struct image *img;
7583 XColor *colors;
7585 int x, y;
7586 XImage *oimg;
7587 Pixmap pixmap;
7588 XColor *p;
7590 init_color_table ();
7592 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
7593 &oimg, &pixmap);
7594 p = colors;
7595 for (y = 0; y < img->height; ++y)
7596 for (x = 0; x < img->width; ++x, ++p)
7598 unsigned long pixel;
7599 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
7600 XPutPixel (oimg, x, y, pixel);
7603 xfree (colors);
7604 x_clear_image_1 (f, img, 1, 0, 1);
7606 x_put_x_image (f, oimg, pixmap, img->width, img->height);
7607 x_destroy_x_image (oimg);
7608 img->pixmap = pixmap;
7609 img->colors = colors_in_color_table (&img->ncolors);
7610 free_color_table ();
7614 /* On frame F, perform edge-detection on image IMG.
7616 MATRIX is a nine-element array specifying the transformation
7617 matrix. See emboss_matrix for an example.
7619 COLOR_ADJUST is a color adjustment added to each pixel of the
7620 outgoing image. */
7622 static void
7623 x_detect_edges (f, img, matrix, color_adjust)
7624 struct frame *f;
7625 struct image *img;
7626 int matrix[9], color_adjust;
7628 XColor *colors = x_to_xcolors (f, img, 1);
7629 XColor *new, *p;
7630 int x, y, i, sum;
7632 for (i = sum = 0; i < 9; ++i)
7633 sum += abs (matrix[i]);
7635 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
7637 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
7639 for (y = 0; y < img->height; ++y)
7641 p = COLOR (new, 0, y);
7642 p->red = p->green = p->blue = 0xffff/2;
7643 p = COLOR (new, img->width - 1, y);
7644 p->red = p->green = p->blue = 0xffff/2;
7647 for (x = 1; x < img->width - 1; ++x)
7649 p = COLOR (new, x, 0);
7650 p->red = p->green = p->blue = 0xffff/2;
7651 p = COLOR (new, x, img->height - 1);
7652 p->red = p->green = p->blue = 0xffff/2;
7655 for (y = 1; y < img->height - 1; ++y)
7657 p = COLOR (new, 1, y);
7659 for (x = 1; x < img->width - 1; ++x, ++p)
7661 int r, g, b, y1, x1;
7663 r = g = b = i = 0;
7664 for (y1 = y - 1; y1 < y + 2; ++y1)
7665 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
7666 if (matrix[i])
7668 XColor *t = COLOR (colors, x1, y1);
7669 r += matrix[i] * t->red;
7670 g += matrix[i] * t->green;
7671 b += matrix[i] * t->blue;
7674 r = (r / sum + color_adjust) & 0xffff;
7675 g = (g / sum + color_adjust) & 0xffff;
7676 b = (b / sum + color_adjust) & 0xffff;
7677 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
7681 xfree (colors);
7682 x_from_xcolors (f, img, new);
7684 #undef COLOR
7688 /* Perform the pre-defined `emboss' edge-detection on image IMG
7689 on frame F. */
7691 static void
7692 x_emboss (f, img)
7693 struct frame *f;
7694 struct image *img;
7696 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
7700 /* Perform the pre-defined `laplace' edge-detection on image IMG
7701 on frame F. */
7703 static void
7704 x_laplace (f, img)
7705 struct frame *f;
7706 struct image *img;
7708 x_detect_edges (f, img, laplace_matrix, 45000);
7712 /* Perform edge-detection on image IMG on frame F, with specified
7713 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
7715 MATRIX must be either
7717 - a list of at least 9 numbers in row-major form
7718 - a vector of at least 9 numbers
7720 COLOR_ADJUST nil means use a default; otherwise it must be a
7721 number. */
7723 static void
7724 x_edge_detection (f, img, matrix, color_adjust)
7725 struct frame *f;
7726 struct image *img;
7727 Lisp_Object matrix, color_adjust;
7729 int i = 0;
7730 int trans[9];
7732 if (CONSP (matrix))
7734 for (i = 0;
7735 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
7736 ++i, matrix = XCDR (matrix))
7737 trans[i] = XFLOATINT (XCAR (matrix));
7739 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
7741 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
7742 trans[i] = XFLOATINT (AREF (matrix, i));
7745 if (NILP (color_adjust))
7746 color_adjust = make_number (0xffff / 2);
7748 if (i == 9 && NUMBERP (color_adjust))
7749 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
7753 /* Transform image IMG on frame F so that it looks disabled. */
7755 static void
7756 x_disable_image (f, img)
7757 struct frame *f;
7758 struct image *img;
7760 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7762 if (dpyinfo->n_planes >= 2)
7764 /* Color (or grayscale). Convert to gray, and equalize. Just
7765 drawing such images with a stipple can look very odd, so
7766 we're using this method instead. */
7767 XColor *colors = x_to_xcolors (f, img, 1);
7768 XColor *p, *end;
7769 const int h = 15000;
7770 const int l = 30000;
7772 for (p = colors, end = colors + img->width * img->height;
7773 p < end;
7774 ++p)
7776 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
7777 int i2 = (0xffff - h - l) * i / 0xffff + l;
7778 p->red = p->green = p->blue = i2;
7781 x_from_xcolors (f, img, colors);
7784 /* Draw a cross over the disabled image, if we must or if we
7785 should. */
7786 if (dpyinfo->n_planes < 2 || cross_disabled_images)
7788 Display *dpy = FRAME_X_DISPLAY (f);
7789 GC gc;
7791 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
7792 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
7793 XDrawLine (dpy, img->pixmap, gc, 0, 0,
7794 img->width - 1, img->height - 1);
7795 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
7796 img->width - 1, 0);
7797 XFreeGC (dpy, gc);
7799 if (img->mask)
7801 gc = XCreateGC (dpy, img->mask, 0, NULL);
7802 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
7803 XDrawLine (dpy, img->mask, gc, 0, 0,
7804 img->width - 1, img->height - 1);
7805 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
7806 img->width - 1, 0);
7807 XFreeGC (dpy, gc);
7813 /* Build a mask for image IMG which is used on frame F. FILE is the
7814 name of an image file, for error messages. HOW determines how to
7815 determine the background color of IMG. If it is a list '(R G B)',
7816 with R, G, and B being integers >= 0, take that as the color of the
7817 background. Otherwise, determine the background color of IMG
7818 heuristically. Value is non-zero if successful. */
7820 static int
7821 x_build_heuristic_mask (f, img, how)
7822 struct frame *f;
7823 struct image *img;
7824 Lisp_Object how;
7826 Display *dpy = FRAME_X_DISPLAY (f);
7827 XImage *ximg, *mask_img;
7828 int x, y, rc, look_at_corners_p;
7829 unsigned long bg = 0;
7831 if (img->mask)
7833 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
7834 img->mask = None;
7837 /* Create an image and pixmap serving as mask. */
7838 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
7839 &mask_img, &img->mask);
7840 if (!rc)
7841 return 0;
7843 /* Get the X image of IMG->pixmap. */
7844 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
7845 ~0, ZPixmap);
7847 /* Determine the background color of ximg. If HOW is `(R G B)'
7848 take that as color. Otherwise, try to determine the color
7849 heuristically. */
7850 look_at_corners_p = 1;
7852 if (CONSP (how))
7854 int rgb[3], i = 0;
7856 while (i < 3
7857 && CONSP (how)
7858 && NATNUMP (XCAR (how)))
7860 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
7861 how = XCDR (how);
7864 if (i == 3 && NILP (how))
7866 char color_name[30];
7867 XColor exact, color;
7868 Colormap cmap;
7870 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
7872 cmap = FRAME_X_COLORMAP (f);
7873 if (XLookupColor (dpy, cmap, color_name, &exact, &color))
7875 bg = color.pixel;
7876 look_at_corners_p = 0;
7881 if (look_at_corners_p)
7883 unsigned long corners[4];
7884 int i, best_count;
7886 /* Get the colors at the corners of ximg. */
7887 corners[0] = XGetPixel (ximg, 0, 0);
7888 corners[1] = XGetPixel (ximg, img->width - 1, 0);
7889 corners[2] = XGetPixel (ximg, img->width - 1, img->height - 1);
7890 corners[3] = XGetPixel (ximg, 0, img->height - 1);
7892 /* Choose the most frequently found color as background. */
7893 for (i = best_count = 0; i < 4; ++i)
7895 int j, n;
7897 for (j = n = 0; j < 4; ++j)
7898 if (corners[i] == corners[j])
7899 ++n;
7901 if (n > best_count)
7902 bg = corners[i], best_count = n;
7906 /* Set all bits in mask_img to 1 whose color in ximg is different
7907 from the background color bg. */
7908 for (y = 0; y < img->height; ++y)
7909 for (x = 0; x < img->width; ++x)
7910 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
7912 /* Put mask_img into img->mask. */
7913 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
7914 x_destroy_x_image (mask_img);
7915 XDestroyImage (ximg);
7917 return 1;
7922 /***********************************************************************
7923 PBM (mono, gray, color)
7924 ***********************************************************************/
7926 static int pbm_image_p P_ ((Lisp_Object object));
7927 static int pbm_load P_ ((struct frame *f, struct image *img));
7928 static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
7930 /* The symbol `pbm' identifying images of this type. */
7932 Lisp_Object Qpbm;
7934 /* Indices of image specification fields in gs_format, below. */
7936 enum pbm_keyword_index
7938 PBM_TYPE,
7939 PBM_FILE,
7940 PBM_DATA,
7941 PBM_ASCENT,
7942 PBM_MARGIN,
7943 PBM_RELIEF,
7944 PBM_ALGORITHM,
7945 PBM_HEURISTIC_MASK,
7946 PBM_MASK,
7947 PBM_FOREGROUND,
7948 PBM_BACKGROUND,
7949 PBM_LAST
7952 /* Vector of image_keyword structures describing the format
7953 of valid user-defined image specifications. */
7955 static struct image_keyword pbm_format[PBM_LAST] =
7957 {":type", IMAGE_SYMBOL_VALUE, 1},
7958 {":file", IMAGE_STRING_VALUE, 0},
7959 {":data", IMAGE_STRING_VALUE, 0},
7960 {":ascent", IMAGE_ASCENT_VALUE, 0},
7961 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
7962 {":relief", IMAGE_INTEGER_VALUE, 0},
7963 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7964 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7965 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7966 {":foreground", IMAGE_STRING_VALUE, 0},
7967 {":background", IMAGE_STRING_VALUE, 0}
7970 /* Structure describing the image type `pbm'. */
7972 static struct image_type pbm_type =
7974 &Qpbm,
7975 pbm_image_p,
7976 pbm_load,
7977 x_clear_image,
7978 NULL
7982 /* Return non-zero if OBJECT is a valid PBM image specification. */
7984 static int
7985 pbm_image_p (object)
7986 Lisp_Object object;
7988 struct image_keyword fmt[PBM_LAST];
7990 bcopy (pbm_format, fmt, sizeof fmt);
7992 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
7993 return 0;
7995 /* Must specify either :data or :file. */
7996 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
8000 /* Scan a decimal number from *S and return it. Advance *S while
8001 reading the number. END is the end of the string. Value is -1 at
8002 end of input. */
8004 static int
8005 pbm_scan_number (s, end)
8006 unsigned char **s, *end;
8008 int c = 0, val = -1;
8010 while (*s < end)
8012 /* Skip white-space. */
8013 while (*s < end && (c = *(*s)++, isspace (c)))
8016 if (c == '#')
8018 /* Skip comment to end of line. */
8019 while (*s < end && (c = *(*s)++, c != '\n'))
8022 else if (isdigit (c))
8024 /* Read decimal number. */
8025 val = c - '0';
8026 while (*s < end && (c = *(*s)++, isdigit (c)))
8027 val = 10 * val + c - '0';
8028 break;
8030 else
8031 break;
8034 return val;
8038 /* Load PBM image IMG for use on frame F. */
8040 static int
8041 pbm_load (f, img)
8042 struct frame *f;
8043 struct image *img;
8045 int raw_p, x, y;
8046 int width, height, max_color_idx = 0;
8047 XImage *ximg;
8048 Lisp_Object file, specified_file;
8049 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
8050 struct gcpro gcpro1;
8051 unsigned char *contents = NULL;
8052 unsigned char *end, *p;
8053 int size;
8055 specified_file = image_spec_value (img->spec, QCfile, NULL);
8056 file = Qnil;
8057 GCPRO1 (file);
8059 if (STRINGP (specified_file))
8061 file = x_find_image_file (specified_file);
8062 if (!STRINGP (file))
8064 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8065 UNGCPRO;
8066 return 0;
8069 contents = slurp_file (XSTRING (file)->data, &size);
8070 if (contents == NULL)
8072 image_error ("Error reading `%s'", file, Qnil);
8073 UNGCPRO;
8074 return 0;
8077 p = contents;
8078 end = contents + size;
8080 else
8082 Lisp_Object data;
8083 data = image_spec_value (img->spec, QCdata, NULL);
8084 p = XSTRING (data)->data;
8085 end = p + STRING_BYTES (XSTRING (data));
8088 /* Check magic number. */
8089 if (end - p < 2 || *p++ != 'P')
8091 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
8092 error:
8093 xfree (contents);
8094 UNGCPRO;
8095 return 0;
8098 switch (*p++)
8100 case '1':
8101 raw_p = 0, type = PBM_MONO;
8102 break;
8104 case '2':
8105 raw_p = 0, type = PBM_GRAY;
8106 break;
8108 case '3':
8109 raw_p = 0, type = PBM_COLOR;
8110 break;
8112 case '4':
8113 raw_p = 1, type = PBM_MONO;
8114 break;
8116 case '5':
8117 raw_p = 1, type = PBM_GRAY;
8118 break;
8120 case '6':
8121 raw_p = 1, type = PBM_COLOR;
8122 break;
8124 default:
8125 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
8126 goto error;
8129 /* Read width, height, maximum color-component. Characters
8130 starting with `#' up to the end of a line are ignored. */
8131 width = pbm_scan_number (&p, end);
8132 height = pbm_scan_number (&p, end);
8134 if (type != PBM_MONO)
8136 max_color_idx = pbm_scan_number (&p, end);
8137 if (raw_p && max_color_idx > 255)
8138 max_color_idx = 255;
8141 if (width < 0
8142 || height < 0
8143 || (type != PBM_MONO && max_color_idx < 0))
8144 goto error;
8146 if (!x_create_x_image_and_pixmap (f, width, height, 0,
8147 &ximg, &img->pixmap))
8148 goto error;
8150 /* Initialize the color hash table. */
8151 init_color_table ();
8153 if (type == PBM_MONO)
8155 int c = 0, g;
8156 struct image_keyword fmt[PBM_LAST];
8157 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
8158 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
8160 /* Parse the image specification. */
8161 bcopy (pbm_format, fmt, sizeof fmt);
8162 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
8164 /* Get foreground and background colors, maybe allocate colors. */
8165 if (fmt[PBM_FOREGROUND].count)
8166 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
8167 if (fmt[PBM_BACKGROUND].count)
8168 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
8170 for (y = 0; y < height; ++y)
8171 for (x = 0; x < width; ++x)
8173 if (raw_p)
8175 if ((x & 7) == 0)
8176 c = *p++;
8177 g = c & 0x80;
8178 c <<= 1;
8180 else
8181 g = pbm_scan_number (&p, end);
8183 XPutPixel (ximg, x, y, g ? fg : bg);
8186 else
8188 for (y = 0; y < height; ++y)
8189 for (x = 0; x < width; ++x)
8191 int r, g, b;
8193 if (type == PBM_GRAY)
8194 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
8195 else if (raw_p)
8197 r = *p++;
8198 g = *p++;
8199 b = *p++;
8201 else
8203 r = pbm_scan_number (&p, end);
8204 g = pbm_scan_number (&p, end);
8205 b = pbm_scan_number (&p, end);
8208 if (r < 0 || g < 0 || b < 0)
8210 xfree (ximg->data);
8211 ximg->data = NULL;
8212 XDestroyImage (ximg);
8213 image_error ("Invalid pixel value in image `%s'",
8214 img->spec, Qnil);
8215 goto error;
8218 /* RGB values are now in the range 0..max_color_idx.
8219 Scale this to the range 0..0xffff supported by X. */
8220 r = (double) r * 65535 / max_color_idx;
8221 g = (double) g * 65535 / max_color_idx;
8222 b = (double) b * 65535 / max_color_idx;
8223 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8227 /* Store in IMG->colors the colors allocated for the image, and
8228 free the color table. */
8229 img->colors = colors_in_color_table (&img->ncolors);
8230 free_color_table ();
8232 /* Put the image into a pixmap. */
8233 x_put_x_image (f, ximg, img->pixmap, width, height);
8234 x_destroy_x_image (ximg);
8236 img->width = width;
8237 img->height = height;
8239 UNGCPRO;
8240 xfree (contents);
8241 return 1;
8246 /***********************************************************************
8248 ***********************************************************************/
8250 #if HAVE_PNG
8252 #include <png.h>
8254 /* Function prototypes. */
8256 static int png_image_p P_ ((Lisp_Object object));
8257 static int png_load P_ ((struct frame *f, struct image *img));
8259 /* The symbol `png' identifying images of this type. */
8261 Lisp_Object Qpng;
8263 /* Indices of image specification fields in png_format, below. */
8265 enum png_keyword_index
8267 PNG_TYPE,
8268 PNG_DATA,
8269 PNG_FILE,
8270 PNG_ASCENT,
8271 PNG_MARGIN,
8272 PNG_RELIEF,
8273 PNG_ALGORITHM,
8274 PNG_HEURISTIC_MASK,
8275 PNG_MASK,
8276 PNG_LAST
8279 /* Vector of image_keyword structures describing the format
8280 of valid user-defined image specifications. */
8282 static struct image_keyword png_format[PNG_LAST] =
8284 {":type", IMAGE_SYMBOL_VALUE, 1},
8285 {":data", IMAGE_STRING_VALUE, 0},
8286 {":file", IMAGE_STRING_VALUE, 0},
8287 {":ascent", IMAGE_ASCENT_VALUE, 0},
8288 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8289 {":relief", IMAGE_INTEGER_VALUE, 0},
8290 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8291 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8292 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8295 /* Structure describing the image type `png'. */
8297 static struct image_type png_type =
8299 &Qpng,
8300 png_image_p,
8301 png_load,
8302 x_clear_image,
8303 NULL
8307 /* Return non-zero if OBJECT is a valid PNG image specification. */
8309 static int
8310 png_image_p (object)
8311 Lisp_Object object;
8313 struct image_keyword fmt[PNG_LAST];
8314 bcopy (png_format, fmt, sizeof fmt);
8316 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
8317 return 0;
8319 /* Must specify either the :data or :file keyword. */
8320 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
8324 /* Error and warning handlers installed when the PNG library
8325 is initialized. */
8327 static void
8328 my_png_error (png_ptr, msg)
8329 png_struct *png_ptr;
8330 char *msg;
8332 xassert (png_ptr != NULL);
8333 image_error ("PNG error: %s", build_string (msg), Qnil);
8334 longjmp (png_ptr->jmpbuf, 1);
8338 static void
8339 my_png_warning (png_ptr, msg)
8340 png_struct *png_ptr;
8341 char *msg;
8343 xassert (png_ptr != NULL);
8344 image_error ("PNG warning: %s", build_string (msg), Qnil);
8347 /* Memory source for PNG decoding. */
8349 struct png_memory_storage
8351 unsigned char *bytes; /* The data */
8352 size_t len; /* How big is it? */
8353 int index; /* Where are we? */
8357 /* Function set as reader function when reading PNG image from memory.
8358 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
8359 bytes from the input to DATA. */
8361 static void
8362 png_read_from_memory (png_ptr, data, length)
8363 png_structp png_ptr;
8364 png_bytep data;
8365 png_size_t length;
8367 struct png_memory_storage *tbr
8368 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
8370 if (length > tbr->len - tbr->index)
8371 png_error (png_ptr, "Read error");
8373 bcopy (tbr->bytes + tbr->index, data, length);
8374 tbr->index = tbr->index + length;
8377 /* Load PNG image IMG for use on frame F. Value is non-zero if
8378 successful. */
8380 static int
8381 png_load (f, img)
8382 struct frame *f;
8383 struct image *img;
8385 Lisp_Object file, specified_file;
8386 Lisp_Object specified_data;
8387 int x, y, i;
8388 XImage *ximg, *mask_img = NULL;
8389 struct gcpro gcpro1;
8390 png_struct *png_ptr = NULL;
8391 png_info *info_ptr = NULL, *end_info = NULL;
8392 FILE *volatile fp = NULL;
8393 png_byte sig[8];
8394 png_byte * volatile pixels = NULL;
8395 png_byte ** volatile rows = NULL;
8396 png_uint_32 width, height;
8397 int bit_depth, color_type, interlace_type;
8398 png_byte channels;
8399 png_uint_32 row_bytes;
8400 int transparent_p;
8401 char *gamma_str;
8402 double screen_gamma, image_gamma;
8403 int intent;
8404 struct png_memory_storage tbr; /* Data to be read */
8406 /* Find out what file to load. */
8407 specified_file = image_spec_value (img->spec, QCfile, NULL);
8408 specified_data = image_spec_value (img->spec, QCdata, NULL);
8409 file = Qnil;
8410 GCPRO1 (file);
8412 if (NILP (specified_data))
8414 file = x_find_image_file (specified_file);
8415 if (!STRINGP (file))
8417 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8418 UNGCPRO;
8419 return 0;
8422 /* Open the image file. */
8423 fp = fopen (XSTRING (file)->data, "rb");
8424 if (!fp)
8426 image_error ("Cannot open image file `%s'", file, Qnil);
8427 UNGCPRO;
8428 fclose (fp);
8429 return 0;
8432 /* Check PNG signature. */
8433 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
8434 || !png_check_sig (sig, sizeof sig))
8436 image_error ("Not a PNG file: `%s'", file, Qnil);
8437 UNGCPRO;
8438 fclose (fp);
8439 return 0;
8442 else
8444 /* Read from memory. */
8445 tbr.bytes = XSTRING (specified_data)->data;
8446 tbr.len = STRING_BYTES (XSTRING (specified_data));
8447 tbr.index = 0;
8449 /* Check PNG signature. */
8450 if (tbr.len < sizeof sig
8451 || !png_check_sig (tbr.bytes, sizeof sig))
8453 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
8454 UNGCPRO;
8455 return 0;
8458 /* Need to skip past the signature. */
8459 tbr.bytes += sizeof (sig);
8462 /* Initialize read and info structs for PNG lib. */
8463 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
8464 my_png_error, my_png_warning);
8465 if (!png_ptr)
8467 if (fp) fclose (fp);
8468 UNGCPRO;
8469 return 0;
8472 info_ptr = png_create_info_struct (png_ptr);
8473 if (!info_ptr)
8475 png_destroy_read_struct (&png_ptr, NULL, NULL);
8476 if (fp) fclose (fp);
8477 UNGCPRO;
8478 return 0;
8481 end_info = png_create_info_struct (png_ptr);
8482 if (!end_info)
8484 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
8485 if (fp) fclose (fp);
8486 UNGCPRO;
8487 return 0;
8490 /* Set error jump-back. We come back here when the PNG library
8491 detects an error. */
8492 if (setjmp (png_ptr->jmpbuf))
8494 error:
8495 if (png_ptr)
8496 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8497 xfree (pixels);
8498 xfree (rows);
8499 if (fp) fclose (fp);
8500 UNGCPRO;
8501 return 0;
8504 /* Read image info. */
8505 if (!NILP (specified_data))
8506 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
8507 else
8508 png_init_io (png_ptr, fp);
8510 png_set_sig_bytes (png_ptr, sizeof sig);
8511 png_read_info (png_ptr, info_ptr);
8512 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
8513 &interlace_type, NULL, NULL);
8515 /* If image contains simply transparency data, we prefer to
8516 construct a clipping mask. */
8517 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
8518 transparent_p = 1;
8519 else
8520 transparent_p = 0;
8522 /* This function is easier to write if we only have to handle
8523 one data format: RGB or RGBA with 8 bits per channel. Let's
8524 transform other formats into that format. */
8526 /* Strip more than 8 bits per channel. */
8527 if (bit_depth == 16)
8528 png_set_strip_16 (png_ptr);
8530 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
8531 if available. */
8532 png_set_expand (png_ptr);
8534 /* Convert grayscale images to RGB. */
8535 if (color_type == PNG_COLOR_TYPE_GRAY
8536 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
8537 png_set_gray_to_rgb (png_ptr);
8539 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
8540 gamma_str = getenv ("SCREEN_GAMMA");
8541 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
8543 /* Tell the PNG lib to handle gamma correction for us. */
8545 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
8546 if (png_get_sRGB (png_ptr, info_ptr, &intent))
8547 /* There is a special chunk in the image specifying the gamma. */
8548 png_set_sRGB (png_ptr, info_ptr, intent);
8549 else
8550 #endif
8551 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
8552 /* Image contains gamma information. */
8553 png_set_gamma (png_ptr, screen_gamma, image_gamma);
8554 else
8555 /* Use a default of 0.5 for the image gamma. */
8556 png_set_gamma (png_ptr, screen_gamma, 0.5);
8558 /* Handle alpha channel by combining the image with a background
8559 color. Do this only if a real alpha channel is supplied. For
8560 simple transparency, we prefer a clipping mask. */
8561 if (!transparent_p)
8563 png_color_16 *image_background;
8565 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
8566 /* Image contains a background color with which to
8567 combine the image. */
8568 png_set_background (png_ptr, image_background,
8569 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
8570 else
8572 /* Image does not contain a background color with which
8573 to combine the image data via an alpha channel. Use
8574 the frame's background instead. */
8575 XColor color;
8576 Colormap cmap;
8577 png_color_16 frame_background;
8579 cmap = FRAME_X_COLORMAP (f);
8580 color.pixel = FRAME_BACKGROUND_PIXEL (f);
8581 x_query_color (f, &color);
8583 bzero (&frame_background, sizeof frame_background);
8584 frame_background.red = color.red;
8585 frame_background.green = color.green;
8586 frame_background.blue = color.blue;
8588 png_set_background (png_ptr, &frame_background,
8589 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
8593 /* Update info structure. */
8594 png_read_update_info (png_ptr, info_ptr);
8596 /* Get number of channels. Valid values are 1 for grayscale images
8597 and images with a palette, 2 for grayscale images with transparency
8598 information (alpha channel), 3 for RGB images, and 4 for RGB
8599 images with alpha channel, i.e. RGBA. If conversions above were
8600 sufficient we should only have 3 or 4 channels here. */
8601 channels = png_get_channels (png_ptr, info_ptr);
8602 xassert (channels == 3 || channels == 4);
8604 /* Number of bytes needed for one row of the image. */
8605 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
8607 /* Allocate memory for the image. */
8608 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
8609 rows = (png_byte **) xmalloc (height * sizeof *rows);
8610 for (i = 0; i < height; ++i)
8611 rows[i] = pixels + i * row_bytes;
8613 /* Read the entire image. */
8614 png_read_image (png_ptr, rows);
8615 png_read_end (png_ptr, info_ptr);
8616 if (fp)
8618 fclose (fp);
8619 fp = NULL;
8622 /* Create the X image and pixmap. */
8623 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
8624 &img->pixmap))
8625 goto error;
8627 /* Create an image and pixmap serving as mask if the PNG image
8628 contains an alpha channel. */
8629 if (channels == 4
8630 && !transparent_p
8631 && !x_create_x_image_and_pixmap (f, width, height, 1,
8632 &mask_img, &img->mask))
8634 x_destroy_x_image (ximg);
8635 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
8636 img->pixmap = None;
8637 goto error;
8640 /* Fill the X image and mask from PNG data. */
8641 init_color_table ();
8643 for (y = 0; y < height; ++y)
8645 png_byte *p = rows[y];
8647 for (x = 0; x < width; ++x)
8649 unsigned r, g, b;
8651 r = *p++ << 8;
8652 g = *p++ << 8;
8653 b = *p++ << 8;
8654 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8656 /* An alpha channel, aka mask channel, associates variable
8657 transparency with an image. Where other image formats
8658 support binary transparency---fully transparent or fully
8659 opaque---PNG allows up to 254 levels of partial transparency.
8660 The PNG library implements partial transparency by combining
8661 the image with a specified background color.
8663 I'm not sure how to handle this here nicely: because the
8664 background on which the image is displayed may change, for
8665 real alpha channel support, it would be necessary to create
8666 a new image for each possible background.
8668 What I'm doing now is that a mask is created if we have
8669 boolean transparency information. Otherwise I'm using
8670 the frame's background color to combine the image with. */
8672 if (channels == 4)
8674 if (mask_img)
8675 XPutPixel (mask_img, x, y, *p > 0);
8676 ++p;
8681 /* Remember colors allocated for this image. */
8682 img->colors = colors_in_color_table (&img->ncolors);
8683 free_color_table ();
8685 /* Clean up. */
8686 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8687 xfree (rows);
8688 xfree (pixels);
8690 img->width = width;
8691 img->height = height;
8693 /* Put the image into the pixmap, then free the X image and its buffer. */
8694 x_put_x_image (f, ximg, img->pixmap, width, height);
8695 x_destroy_x_image (ximg);
8697 /* Same for the mask. */
8698 if (mask_img)
8700 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8701 x_destroy_x_image (mask_img);
8704 UNGCPRO;
8705 return 1;
8708 #endif /* HAVE_PNG != 0 */
8712 /***********************************************************************
8713 JPEG
8714 ***********************************************************************/
8716 #if HAVE_JPEG
8718 /* Work around a warning about HAVE_STDLIB_H being redefined in
8719 jconfig.h. */
8720 #ifdef HAVE_STDLIB_H
8721 #define HAVE_STDLIB_H_1
8722 #undef HAVE_STDLIB_H
8723 #endif /* HAVE_STLIB_H */
8725 #include <jpeglib.h>
8726 #include <jerror.h>
8727 #include <setjmp.h>
8729 #ifdef HAVE_STLIB_H_1
8730 #define HAVE_STDLIB_H 1
8731 #endif
8733 static int jpeg_image_p P_ ((Lisp_Object object));
8734 static int jpeg_load P_ ((struct frame *f, struct image *img));
8736 /* The symbol `jpeg' identifying images of this type. */
8738 Lisp_Object Qjpeg;
8740 /* Indices of image specification fields in gs_format, below. */
8742 enum jpeg_keyword_index
8744 JPEG_TYPE,
8745 JPEG_DATA,
8746 JPEG_FILE,
8747 JPEG_ASCENT,
8748 JPEG_MARGIN,
8749 JPEG_RELIEF,
8750 JPEG_ALGORITHM,
8751 JPEG_HEURISTIC_MASK,
8752 JPEG_MASK,
8753 JPEG_LAST
8756 /* Vector of image_keyword structures describing the format
8757 of valid user-defined image specifications. */
8759 static struct image_keyword jpeg_format[JPEG_LAST] =
8761 {":type", IMAGE_SYMBOL_VALUE, 1},
8762 {":data", IMAGE_STRING_VALUE, 0},
8763 {":file", IMAGE_STRING_VALUE, 0},
8764 {":ascent", IMAGE_ASCENT_VALUE, 0},
8765 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8766 {":relief", IMAGE_INTEGER_VALUE, 0},
8767 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8768 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8769 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8772 /* Structure describing the image type `jpeg'. */
8774 static struct image_type jpeg_type =
8776 &Qjpeg,
8777 jpeg_image_p,
8778 jpeg_load,
8779 x_clear_image,
8780 NULL
8784 /* Return non-zero if OBJECT is a valid JPEG image specification. */
8786 static int
8787 jpeg_image_p (object)
8788 Lisp_Object object;
8790 struct image_keyword fmt[JPEG_LAST];
8792 bcopy (jpeg_format, fmt, sizeof fmt);
8794 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
8795 return 0;
8797 /* Must specify either the :data or :file keyword. */
8798 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
8802 struct my_jpeg_error_mgr
8804 struct jpeg_error_mgr pub;
8805 jmp_buf setjmp_buffer;
8809 static void
8810 my_error_exit (cinfo)
8811 j_common_ptr cinfo;
8813 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
8814 longjmp (mgr->setjmp_buffer, 1);
8818 /* Init source method for JPEG data source manager. Called by
8819 jpeg_read_header() before any data is actually read. See
8820 libjpeg.doc from the JPEG lib distribution. */
8822 static void
8823 our_init_source (cinfo)
8824 j_decompress_ptr cinfo;
8829 /* Fill input buffer method for JPEG data source manager. Called
8830 whenever more data is needed. We read the whole image in one step,
8831 so this only adds a fake end of input marker at the end. */
8833 static boolean
8834 our_fill_input_buffer (cinfo)
8835 j_decompress_ptr cinfo;
8837 /* Insert a fake EOI marker. */
8838 struct jpeg_source_mgr *src = cinfo->src;
8839 static JOCTET buffer[2];
8841 buffer[0] = (JOCTET) 0xFF;
8842 buffer[1] = (JOCTET) JPEG_EOI;
8844 src->next_input_byte = buffer;
8845 src->bytes_in_buffer = 2;
8846 return TRUE;
8850 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
8851 is the JPEG data source manager. */
8853 static void
8854 our_skip_input_data (cinfo, num_bytes)
8855 j_decompress_ptr cinfo;
8856 long num_bytes;
8858 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
8860 if (src)
8862 if (num_bytes > src->bytes_in_buffer)
8863 ERREXIT (cinfo, JERR_INPUT_EOF);
8865 src->bytes_in_buffer -= num_bytes;
8866 src->next_input_byte += num_bytes;
8871 /* Method to terminate data source. Called by
8872 jpeg_finish_decompress() after all data has been processed. */
8874 static void
8875 our_term_source (cinfo)
8876 j_decompress_ptr cinfo;
8881 /* Set up the JPEG lib for reading an image from DATA which contains
8882 LEN bytes. CINFO is the decompression info structure created for
8883 reading the image. */
8885 static void
8886 jpeg_memory_src (cinfo, data, len)
8887 j_decompress_ptr cinfo;
8888 JOCTET *data;
8889 unsigned int len;
8891 struct jpeg_source_mgr *src;
8893 if (cinfo->src == NULL)
8895 /* First time for this JPEG object? */
8896 cinfo->src = (struct jpeg_source_mgr *)
8897 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
8898 sizeof (struct jpeg_source_mgr));
8899 src = (struct jpeg_source_mgr *) cinfo->src;
8900 src->next_input_byte = data;
8903 src = (struct jpeg_source_mgr *) cinfo->src;
8904 src->init_source = our_init_source;
8905 src->fill_input_buffer = our_fill_input_buffer;
8906 src->skip_input_data = our_skip_input_data;
8907 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
8908 src->term_source = our_term_source;
8909 src->bytes_in_buffer = len;
8910 src->next_input_byte = data;
8914 /* Load image IMG for use on frame F. Patterned after example.c
8915 from the JPEG lib. */
8917 static int
8918 jpeg_load (f, img)
8919 struct frame *f;
8920 struct image *img;
8922 struct jpeg_decompress_struct cinfo;
8923 struct my_jpeg_error_mgr mgr;
8924 Lisp_Object file, specified_file;
8925 Lisp_Object specified_data;
8926 FILE * volatile fp = NULL;
8927 JSAMPARRAY buffer;
8928 int row_stride, x, y;
8929 XImage *ximg = NULL;
8930 int rc;
8931 unsigned long *colors;
8932 int width, height;
8933 struct gcpro gcpro1;
8935 /* Open the JPEG file. */
8936 specified_file = image_spec_value (img->spec, QCfile, NULL);
8937 specified_data = image_spec_value (img->spec, QCdata, NULL);
8938 file = Qnil;
8939 GCPRO1 (file);
8941 if (NILP (specified_data))
8943 file = x_find_image_file (specified_file);
8944 if (!STRINGP (file))
8946 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8947 UNGCPRO;
8948 return 0;
8951 fp = fopen (XSTRING (file)->data, "r");
8952 if (fp == NULL)
8954 image_error ("Cannot open `%s'", file, Qnil);
8955 UNGCPRO;
8956 return 0;
8960 /* Customize libjpeg's error handling to call my_error_exit when an
8961 error is detected. This function will perform a longjmp. */
8962 cinfo.err = jpeg_std_error (&mgr.pub);
8963 mgr.pub.error_exit = my_error_exit;
8965 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
8967 if (rc == 1)
8969 /* Called from my_error_exit. Display a JPEG error. */
8970 char buffer[JMSG_LENGTH_MAX];
8971 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
8972 image_error ("Error reading JPEG image `%s': %s", img->spec,
8973 build_string (buffer));
8976 /* Close the input file and destroy the JPEG object. */
8977 if (fp)
8978 fclose ((FILE *) fp);
8979 jpeg_destroy_decompress (&cinfo);
8981 /* If we already have an XImage, free that. */
8982 x_destroy_x_image (ximg);
8984 /* Free pixmap and colors. */
8985 x_clear_image (f, img);
8987 UNGCPRO;
8988 return 0;
8991 /* Create the JPEG decompression object. Let it read from fp.
8992 Read the JPEG image header. */
8993 jpeg_create_decompress (&cinfo);
8995 if (NILP (specified_data))
8996 jpeg_stdio_src (&cinfo, (FILE *) fp);
8997 else
8998 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
8999 STRING_BYTES (XSTRING (specified_data)));
9001 jpeg_read_header (&cinfo, TRUE);
9003 /* Customize decompression so that color quantization will be used.
9004 Start decompression. */
9005 cinfo.quantize_colors = TRUE;
9006 jpeg_start_decompress (&cinfo);
9007 width = img->width = cinfo.output_width;
9008 height = img->height = cinfo.output_height;
9010 /* Create X image and pixmap. */
9011 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9012 longjmp (mgr.setjmp_buffer, 2);
9014 /* Allocate colors. When color quantization is used,
9015 cinfo.actual_number_of_colors has been set with the number of
9016 colors generated, and cinfo.colormap is a two-dimensional array
9017 of color indices in the range 0..cinfo.actual_number_of_colors.
9018 No more than 255 colors will be generated. */
9020 int i, ir, ig, ib;
9022 if (cinfo.out_color_components > 2)
9023 ir = 0, ig = 1, ib = 2;
9024 else if (cinfo.out_color_components > 1)
9025 ir = 0, ig = 1, ib = 0;
9026 else
9027 ir = 0, ig = 0, ib = 0;
9029 /* Use the color table mechanism because it handles colors that
9030 cannot be allocated nicely. Such colors will be replaced with
9031 a default color, and we don't have to care about which colors
9032 can be freed safely, and which can't. */
9033 init_color_table ();
9034 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
9035 * sizeof *colors);
9037 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
9039 /* Multiply RGB values with 255 because X expects RGB values
9040 in the range 0..0xffff. */
9041 int r = cinfo.colormap[ir][i] << 8;
9042 int g = cinfo.colormap[ig][i] << 8;
9043 int b = cinfo.colormap[ib][i] << 8;
9044 colors[i] = lookup_rgb_color (f, r, g, b);
9047 /* Remember those colors actually allocated. */
9048 img->colors = colors_in_color_table (&img->ncolors);
9049 free_color_table ();
9052 /* Read pixels. */
9053 row_stride = width * cinfo.output_components;
9054 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
9055 row_stride, 1);
9056 for (y = 0; y < height; ++y)
9058 jpeg_read_scanlines (&cinfo, buffer, 1);
9059 for (x = 0; x < cinfo.output_width; ++x)
9060 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
9063 /* Clean up. */
9064 jpeg_finish_decompress (&cinfo);
9065 jpeg_destroy_decompress (&cinfo);
9066 if (fp)
9067 fclose ((FILE *) fp);
9069 /* Put the image into the pixmap. */
9070 x_put_x_image (f, ximg, img->pixmap, width, height);
9071 x_destroy_x_image (ximg);
9072 UNGCPRO;
9073 return 1;
9076 #endif /* HAVE_JPEG */
9080 /***********************************************************************
9081 TIFF
9082 ***********************************************************************/
9084 #if HAVE_TIFF
9086 #include <tiffio.h>
9088 static int tiff_image_p P_ ((Lisp_Object object));
9089 static int tiff_load P_ ((struct frame *f, struct image *img));
9091 /* The symbol `tiff' identifying images of this type. */
9093 Lisp_Object Qtiff;
9095 /* Indices of image specification fields in tiff_format, below. */
9097 enum tiff_keyword_index
9099 TIFF_TYPE,
9100 TIFF_DATA,
9101 TIFF_FILE,
9102 TIFF_ASCENT,
9103 TIFF_MARGIN,
9104 TIFF_RELIEF,
9105 TIFF_ALGORITHM,
9106 TIFF_HEURISTIC_MASK,
9107 TIFF_MASK,
9108 TIFF_LAST
9111 /* Vector of image_keyword structures describing the format
9112 of valid user-defined image specifications. */
9114 static struct image_keyword tiff_format[TIFF_LAST] =
9116 {":type", IMAGE_SYMBOL_VALUE, 1},
9117 {":data", IMAGE_STRING_VALUE, 0},
9118 {":file", IMAGE_STRING_VALUE, 0},
9119 {":ascent", IMAGE_ASCENT_VALUE, 0},
9120 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9121 {":relief", IMAGE_INTEGER_VALUE, 0},
9122 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9123 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9124 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9127 /* Structure describing the image type `tiff'. */
9129 static struct image_type tiff_type =
9131 &Qtiff,
9132 tiff_image_p,
9133 tiff_load,
9134 x_clear_image,
9135 NULL
9139 /* Return non-zero if OBJECT is a valid TIFF image specification. */
9141 static int
9142 tiff_image_p (object)
9143 Lisp_Object object;
9145 struct image_keyword fmt[TIFF_LAST];
9146 bcopy (tiff_format, fmt, sizeof fmt);
9148 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
9149 return 0;
9151 /* Must specify either the :data or :file keyword. */
9152 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
9156 /* Reading from a memory buffer for TIFF images Based on the PNG
9157 memory source, but we have to provide a lot of extra functions.
9158 Blah.
9160 We really only need to implement read and seek, but I am not
9161 convinced that the TIFF library is smart enough not to destroy
9162 itself if we only hand it the function pointers we need to
9163 override. */
9165 typedef struct
9167 unsigned char *bytes;
9168 size_t len;
9169 int index;
9171 tiff_memory_source;
9174 static size_t
9175 tiff_read_from_memory (data, buf, size)
9176 thandle_t data;
9177 tdata_t buf;
9178 tsize_t size;
9180 tiff_memory_source *src = (tiff_memory_source *) data;
9182 if (size > src->len - src->index)
9183 return (size_t) -1;
9184 bcopy (src->bytes + src->index, buf, size);
9185 src->index += size;
9186 return size;
9190 static size_t
9191 tiff_write_from_memory (data, buf, size)
9192 thandle_t data;
9193 tdata_t buf;
9194 tsize_t size;
9196 return (size_t) -1;
9200 static toff_t
9201 tiff_seek_in_memory (data, off, whence)
9202 thandle_t data;
9203 toff_t off;
9204 int whence;
9206 tiff_memory_source *src = (tiff_memory_source *) data;
9207 int idx;
9209 switch (whence)
9211 case SEEK_SET: /* Go from beginning of source. */
9212 idx = off;
9213 break;
9215 case SEEK_END: /* Go from end of source. */
9216 idx = src->len + off;
9217 break;
9219 case SEEK_CUR: /* Go from current position. */
9220 idx = src->index + off;
9221 break;
9223 default: /* Invalid `whence'. */
9224 return -1;
9227 if (idx > src->len || idx < 0)
9228 return -1;
9230 src->index = idx;
9231 return src->index;
9235 static int
9236 tiff_close_memory (data)
9237 thandle_t data;
9239 /* NOOP */
9240 return 0;
9244 static int
9245 tiff_mmap_memory (data, pbase, psize)
9246 thandle_t data;
9247 tdata_t *pbase;
9248 toff_t *psize;
9250 /* It is already _IN_ memory. */
9251 return 0;
9255 static void
9256 tiff_unmap_memory (data, base, size)
9257 thandle_t data;
9258 tdata_t base;
9259 toff_t size;
9261 /* We don't need to do this. */
9265 static toff_t
9266 tiff_size_of_memory (data)
9267 thandle_t data;
9269 return ((tiff_memory_source *) data)->len;
9273 /* Load TIFF image IMG for use on frame F. Value is non-zero if
9274 successful. */
9276 static int
9277 tiff_load (f, img)
9278 struct frame *f;
9279 struct image *img;
9281 Lisp_Object file, specified_file;
9282 Lisp_Object specified_data;
9283 TIFF *tiff;
9284 int width, height, x, y;
9285 uint32 *buf;
9286 int rc;
9287 XImage *ximg;
9288 struct gcpro gcpro1;
9289 tiff_memory_source memsrc;
9291 specified_file = image_spec_value (img->spec, QCfile, NULL);
9292 specified_data = image_spec_value (img->spec, QCdata, NULL);
9293 file = Qnil;
9294 GCPRO1 (file);
9296 if (NILP (specified_data))
9298 /* Read from a file */
9299 file = x_find_image_file (specified_file);
9300 if (!STRINGP (file))
9302 image_error ("Cannot find image file `%s'", file, Qnil);
9303 UNGCPRO;
9304 return 0;
9307 /* Try to open the image file. */
9308 tiff = TIFFOpen (XSTRING (file)->data, "r");
9309 if (tiff == NULL)
9311 image_error ("Cannot open `%s'", file, Qnil);
9312 UNGCPRO;
9313 return 0;
9316 else
9318 /* Memory source! */
9319 memsrc.bytes = XSTRING (specified_data)->data;
9320 memsrc.len = STRING_BYTES (XSTRING (specified_data));
9321 memsrc.index = 0;
9323 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
9324 (TIFFReadWriteProc) tiff_read_from_memory,
9325 (TIFFReadWriteProc) tiff_write_from_memory,
9326 tiff_seek_in_memory,
9327 tiff_close_memory,
9328 tiff_size_of_memory,
9329 tiff_mmap_memory,
9330 tiff_unmap_memory);
9332 if (!tiff)
9334 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
9335 UNGCPRO;
9336 return 0;
9340 /* Get width and height of the image, and allocate a raster buffer
9341 of width x height 32-bit values. */
9342 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
9343 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
9344 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
9346 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
9347 TIFFClose (tiff);
9348 if (!rc)
9350 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
9351 xfree (buf);
9352 UNGCPRO;
9353 return 0;
9356 /* Create the X image and pixmap. */
9357 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9359 xfree (buf);
9360 UNGCPRO;
9361 return 0;
9364 /* Initialize the color table. */
9365 init_color_table ();
9367 /* Process the pixel raster. Origin is in the lower-left corner. */
9368 for (y = 0; y < height; ++y)
9370 uint32 *row = buf + y * width;
9372 for (x = 0; x < width; ++x)
9374 uint32 abgr = row[x];
9375 int r = TIFFGetR (abgr) << 8;
9376 int g = TIFFGetG (abgr) << 8;
9377 int b = TIFFGetB (abgr) << 8;
9378 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
9382 /* Remember the colors allocated for the image. Free the color table. */
9383 img->colors = colors_in_color_table (&img->ncolors);
9384 free_color_table ();
9386 /* Put the image into the pixmap, then free the X image and its buffer. */
9387 x_put_x_image (f, ximg, img->pixmap, width, height);
9388 x_destroy_x_image (ximg);
9389 xfree (buf);
9391 img->width = width;
9392 img->height = height;
9394 UNGCPRO;
9395 return 1;
9398 #endif /* HAVE_TIFF != 0 */
9402 /***********************************************************************
9404 ***********************************************************************/
9406 #if HAVE_GIF
9408 #include <gif_lib.h>
9410 static int gif_image_p P_ ((Lisp_Object object));
9411 static int gif_load P_ ((struct frame *f, struct image *img));
9413 /* The symbol `gif' identifying images of this type. */
9415 Lisp_Object Qgif;
9417 /* Indices of image specification fields in gif_format, below. */
9419 enum gif_keyword_index
9421 GIF_TYPE,
9422 GIF_DATA,
9423 GIF_FILE,
9424 GIF_ASCENT,
9425 GIF_MARGIN,
9426 GIF_RELIEF,
9427 GIF_ALGORITHM,
9428 GIF_HEURISTIC_MASK,
9429 GIF_MASK,
9430 GIF_IMAGE,
9431 GIF_LAST
9434 /* Vector of image_keyword structures describing the format
9435 of valid user-defined image specifications. */
9437 static struct image_keyword gif_format[GIF_LAST] =
9439 {":type", IMAGE_SYMBOL_VALUE, 1},
9440 {":data", IMAGE_STRING_VALUE, 0},
9441 {":file", IMAGE_STRING_VALUE, 0},
9442 {":ascent", IMAGE_ASCENT_VALUE, 0},
9443 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9444 {":relief", IMAGE_INTEGER_VALUE, 0},
9445 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9446 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9447 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9448 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
9451 /* Structure describing the image type `gif'. */
9453 static struct image_type gif_type =
9455 &Qgif,
9456 gif_image_p,
9457 gif_load,
9458 x_clear_image,
9459 NULL
9463 /* Return non-zero if OBJECT is a valid GIF image specification. */
9465 static int
9466 gif_image_p (object)
9467 Lisp_Object object;
9469 struct image_keyword fmt[GIF_LAST];
9470 bcopy (gif_format, fmt, sizeof fmt);
9472 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
9473 return 0;
9475 /* Must specify either the :data or :file keyword. */
9476 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
9480 /* Reading a GIF image from memory
9481 Based on the PNG memory stuff to a certain extent. */
9483 typedef struct
9485 unsigned char *bytes;
9486 size_t len;
9487 int index;
9489 gif_memory_source;
9492 /* Make the current memory source available to gif_read_from_memory.
9493 It's done this way because not all versions of libungif support
9494 a UserData field in the GifFileType structure. */
9495 static gif_memory_source *current_gif_memory_src;
9497 static int
9498 gif_read_from_memory (file, buf, len)
9499 GifFileType *file;
9500 GifByteType *buf;
9501 int len;
9503 gif_memory_source *src = current_gif_memory_src;
9505 if (len > src->len - src->index)
9506 return -1;
9508 bcopy (src->bytes + src->index, buf, len);
9509 src->index += len;
9510 return len;
9514 /* Load GIF image IMG for use on frame F. Value is non-zero if
9515 successful. */
9517 static int
9518 gif_load (f, img)
9519 struct frame *f;
9520 struct image *img;
9522 Lisp_Object file, specified_file;
9523 Lisp_Object specified_data;
9524 int rc, width, height, x, y, i;
9525 XImage *ximg;
9526 ColorMapObject *gif_color_map;
9527 unsigned long pixel_colors[256];
9528 GifFileType *gif;
9529 struct gcpro gcpro1;
9530 Lisp_Object image;
9531 int ino, image_left, image_top, image_width, image_height;
9532 gif_memory_source memsrc;
9533 unsigned char *raster;
9535 specified_file = image_spec_value (img->spec, QCfile, NULL);
9536 specified_data = image_spec_value (img->spec, QCdata, NULL);
9537 file = Qnil;
9538 GCPRO1 (file);
9540 if (NILP (specified_data))
9542 file = x_find_image_file (specified_file);
9543 if (!STRINGP (file))
9545 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9546 UNGCPRO;
9547 return 0;
9550 /* Open the GIF file. */
9551 gif = DGifOpenFileName (XSTRING (file)->data);
9552 if (gif == NULL)
9554 image_error ("Cannot open `%s'", file, Qnil);
9555 UNGCPRO;
9556 return 0;
9559 else
9561 /* Read from memory! */
9562 current_gif_memory_src = &memsrc;
9563 memsrc.bytes = XSTRING (specified_data)->data;
9564 memsrc.len = STRING_BYTES (XSTRING (specified_data));
9565 memsrc.index = 0;
9567 gif = DGifOpen(&memsrc, gif_read_from_memory);
9568 if (!gif)
9570 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
9571 UNGCPRO;
9572 return 0;
9576 /* Read entire contents. */
9577 rc = DGifSlurp (gif);
9578 if (rc == GIF_ERROR)
9580 image_error ("Error reading `%s'", img->spec, Qnil);
9581 DGifCloseFile (gif);
9582 UNGCPRO;
9583 return 0;
9586 image = image_spec_value (img->spec, QCindex, NULL);
9587 ino = INTEGERP (image) ? XFASTINT (image) : 0;
9588 if (ino >= gif->ImageCount)
9590 image_error ("Invalid image number `%s' in image `%s'",
9591 image, img->spec);
9592 DGifCloseFile (gif);
9593 UNGCPRO;
9594 return 0;
9597 width = img->width = gif->SWidth;
9598 height = img->height = gif->SHeight;
9600 /* Create the X image and pixmap. */
9601 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9603 DGifCloseFile (gif);
9604 UNGCPRO;
9605 return 0;
9608 /* Allocate colors. */
9609 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
9610 if (!gif_color_map)
9611 gif_color_map = gif->SColorMap;
9612 init_color_table ();
9613 bzero (pixel_colors, sizeof pixel_colors);
9615 for (i = 0; i < gif_color_map->ColorCount; ++i)
9617 int r = gif_color_map->Colors[i].Red << 8;
9618 int g = gif_color_map->Colors[i].Green << 8;
9619 int b = gif_color_map->Colors[i].Blue << 8;
9620 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
9623 img->colors = colors_in_color_table (&img->ncolors);
9624 free_color_table ();
9626 /* Clear the part of the screen image that are not covered by
9627 the image from the GIF file. Full animated GIF support
9628 requires more than can be done here (see the gif89 spec,
9629 disposal methods). Let's simply assume that the part
9630 not covered by a sub-image is in the frame's background color. */
9631 image_top = gif->SavedImages[ino].ImageDesc.Top;
9632 image_left = gif->SavedImages[ino].ImageDesc.Left;
9633 image_width = gif->SavedImages[ino].ImageDesc.Width;
9634 image_height = gif->SavedImages[ino].ImageDesc.Height;
9636 for (y = 0; y < image_top; ++y)
9637 for (x = 0; x < width; ++x)
9638 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9640 for (y = image_top + image_height; y < height; ++y)
9641 for (x = 0; x < width; ++x)
9642 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9644 for (y = image_top; y < image_top + image_height; ++y)
9646 for (x = 0; x < image_left; ++x)
9647 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9648 for (x = image_left + image_width; x < width; ++x)
9649 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9652 /* Read the GIF image into the X image. We use a local variable
9653 `raster' here because RasterBits below is a char *, and invites
9654 problems with bytes >= 0x80. */
9655 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
9657 if (gif->SavedImages[ino].ImageDesc.Interlace)
9659 static int interlace_start[] = {0, 4, 2, 1};
9660 static int interlace_increment[] = {8, 8, 4, 2};
9661 int pass, inc;
9662 int row = interlace_start[0];
9664 pass = 0;
9666 for (y = 0; y < image_height; y++)
9668 if (row >= image_height)
9670 row = interlace_start[++pass];
9671 while (row >= image_height)
9672 row = interlace_start[++pass];
9675 for (x = 0; x < image_width; x++)
9677 int i = raster[(y * image_width) + x];
9678 XPutPixel (ximg, x + image_left, row + image_top,
9679 pixel_colors[i]);
9682 row += interlace_increment[pass];
9685 else
9687 for (y = 0; y < image_height; ++y)
9688 for (x = 0; x < image_width; ++x)
9690 int i = raster[y * image_width + x];
9691 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
9695 DGifCloseFile (gif);
9697 /* Put the image into the pixmap, then free the X image and its buffer. */
9698 x_put_x_image (f, ximg, img->pixmap, width, height);
9699 x_destroy_x_image (ximg);
9701 UNGCPRO;
9702 return 1;
9705 #endif /* HAVE_GIF != 0 */
9709 /***********************************************************************
9710 Ghostscript
9711 ***********************************************************************/
9713 static int gs_image_p P_ ((Lisp_Object object));
9714 static int gs_load P_ ((struct frame *f, struct image *img));
9715 static void gs_clear_image P_ ((struct frame *f, struct image *img));
9717 /* The symbol `postscript' identifying images of this type. */
9719 Lisp_Object Qpostscript;
9721 /* Keyword symbols. */
9723 Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
9725 /* Indices of image specification fields in gs_format, below. */
9727 enum gs_keyword_index
9729 GS_TYPE,
9730 GS_PT_WIDTH,
9731 GS_PT_HEIGHT,
9732 GS_FILE,
9733 GS_LOADER,
9734 GS_BOUNDING_BOX,
9735 GS_ASCENT,
9736 GS_MARGIN,
9737 GS_RELIEF,
9738 GS_ALGORITHM,
9739 GS_HEURISTIC_MASK,
9740 GS_MASK,
9741 GS_LAST
9744 /* Vector of image_keyword structures describing the format
9745 of valid user-defined image specifications. */
9747 static struct image_keyword gs_format[GS_LAST] =
9749 {":type", IMAGE_SYMBOL_VALUE, 1},
9750 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9751 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9752 {":file", IMAGE_STRING_VALUE, 1},
9753 {":loader", IMAGE_FUNCTION_VALUE, 0},
9754 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
9755 {":ascent", IMAGE_ASCENT_VALUE, 0},
9756 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9757 {":relief", IMAGE_INTEGER_VALUE, 0},
9758 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9759 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9760 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9763 /* Structure describing the image type `ghostscript'. */
9765 static struct image_type gs_type =
9767 &Qpostscript,
9768 gs_image_p,
9769 gs_load,
9770 gs_clear_image,
9771 NULL
9775 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9777 static void
9778 gs_clear_image (f, img)
9779 struct frame *f;
9780 struct image *img;
9782 /* IMG->data.ptr_val may contain a recorded colormap. */
9783 xfree (img->data.ptr_val);
9784 x_clear_image (f, img);
9788 /* Return non-zero if OBJECT is a valid Ghostscript image
9789 specification. */
9791 static int
9792 gs_image_p (object)
9793 Lisp_Object object;
9795 struct image_keyword fmt[GS_LAST];
9796 Lisp_Object tem;
9797 int i;
9799 bcopy (gs_format, fmt, sizeof fmt);
9801 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
9802 return 0;
9804 /* Bounding box must be a list or vector containing 4 integers. */
9805 tem = fmt[GS_BOUNDING_BOX].value;
9806 if (CONSP (tem))
9808 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9809 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9810 return 0;
9811 if (!NILP (tem))
9812 return 0;
9814 else if (VECTORP (tem))
9816 if (XVECTOR (tem)->size != 4)
9817 return 0;
9818 for (i = 0; i < 4; ++i)
9819 if (!INTEGERP (XVECTOR (tem)->contents[i]))
9820 return 0;
9822 else
9823 return 0;
9825 return 1;
9829 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
9830 if successful. */
9832 static int
9833 gs_load (f, img)
9834 struct frame *f;
9835 struct image *img;
9837 char buffer[100];
9838 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9839 struct gcpro gcpro1, gcpro2;
9840 Lisp_Object frame;
9841 double in_width, in_height;
9842 Lisp_Object pixel_colors = Qnil;
9844 /* Compute pixel size of pixmap needed from the given size in the
9845 image specification. Sizes in the specification are in pt. 1 pt
9846 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9847 info. */
9848 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9849 in_width = XFASTINT (pt_width) / 72.0;
9850 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
9851 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9852 in_height = XFASTINT (pt_height) / 72.0;
9853 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
9855 /* Create the pixmap. */
9856 xassert (img->pixmap == None);
9857 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9858 img->width, img->height,
9859 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
9861 if (!img->pixmap)
9863 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
9864 return 0;
9867 /* Call the loader to fill the pixmap. It returns a process object
9868 if successful. We do not record_unwind_protect here because
9869 other places in redisplay like calling window scroll functions
9870 don't either. Let the Lisp loader use `unwind-protect' instead. */
9871 GCPRO2 (window_and_pixmap_id, pixel_colors);
9873 sprintf (buffer, "%lu %lu",
9874 (unsigned long) FRAME_X_WINDOW (f),
9875 (unsigned long) img->pixmap);
9876 window_and_pixmap_id = build_string (buffer);
9878 sprintf (buffer, "%lu %lu",
9879 FRAME_FOREGROUND_PIXEL (f),
9880 FRAME_BACKGROUND_PIXEL (f));
9881 pixel_colors = build_string (buffer);
9883 XSETFRAME (frame, f);
9884 loader = image_spec_value (img->spec, QCloader, NULL);
9885 if (NILP (loader))
9886 loader = intern ("gs-load-image");
9888 img->data.lisp_val = call6 (loader, frame, img->spec,
9889 make_number (img->width),
9890 make_number (img->height),
9891 window_and_pixmap_id,
9892 pixel_colors);
9893 UNGCPRO;
9894 return PROCESSP (img->data.lisp_val);
9898 /* Kill the Ghostscript process that was started to fill PIXMAP on
9899 frame F. Called from XTread_socket when receiving an event
9900 telling Emacs that Ghostscript has finished drawing. */
9902 void
9903 x_kill_gs_process (pixmap, f)
9904 Pixmap pixmap;
9905 struct frame *f;
9907 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9908 int class, i;
9909 struct image *img;
9911 /* Find the image containing PIXMAP. */
9912 for (i = 0; i < c->used; ++i)
9913 if (c->images[i]->pixmap == pixmap)
9914 break;
9916 /* Kill the GS process. We should have found PIXMAP in the image
9917 cache and its image should contain a process object. */
9918 xassert (i < c->used);
9919 img = c->images[i];
9920 xassert (PROCESSP (img->data.lisp_val));
9921 Fkill_process (img->data.lisp_val, Qnil);
9922 img->data.lisp_val = Qnil;
9924 /* On displays with a mutable colormap, figure out the colors
9925 allocated for the image by looking at the pixels of an XImage for
9926 img->pixmap. */
9927 class = FRAME_X_VISUAL (f)->class;
9928 if (class != StaticColor && class != StaticGray && class != TrueColor)
9930 XImage *ximg;
9932 BLOCK_INPUT;
9934 /* Try to get an XImage for img->pixmep. */
9935 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
9936 0, 0, img->width, img->height, ~0, ZPixmap);
9937 if (ximg)
9939 int x, y;
9941 /* Initialize the color table. */
9942 init_color_table ();
9944 /* For each pixel of the image, look its color up in the
9945 color table. After having done so, the color table will
9946 contain an entry for each color used by the image. */
9947 for (y = 0; y < img->height; ++y)
9948 for (x = 0; x < img->width; ++x)
9950 unsigned long pixel = XGetPixel (ximg, x, y);
9951 lookup_pixel_color (f, pixel);
9954 /* Record colors in the image. Free color table and XImage. */
9955 img->colors = colors_in_color_table (&img->ncolors);
9956 free_color_table ();
9957 XDestroyImage (ximg);
9959 #if 0 /* This doesn't seem to be the case. If we free the colors
9960 here, we get a BadAccess later in x_clear_image when
9961 freeing the colors. */
9962 /* We have allocated colors once, but Ghostscript has also
9963 allocated colors on behalf of us. So, to get the
9964 reference counts right, free them once. */
9965 if (img->ncolors)
9966 x_free_colors (f, img->colors, img->ncolors);
9967 #endif
9969 else
9970 image_error ("Cannot get X image of `%s'; colors will not be freed",
9971 img->spec, Qnil);
9973 UNBLOCK_INPUT;
9979 /***********************************************************************
9980 Window properties
9981 ***********************************************************************/
9983 DEFUN ("x-change-window-property", Fx_change_window_property,
9984 Sx_change_window_property, 2, 3, 0,
9985 "Change window property PROP to VALUE on the X window of FRAME.\n\
9986 PROP and VALUE must be strings. FRAME nil or omitted means use the\n\
9987 selected frame. Value is VALUE.")
9988 (prop, value, frame)
9989 Lisp_Object frame, prop, value;
9991 struct frame *f = check_x_frame (frame);
9992 Atom prop_atom;
9994 CHECK_STRING (prop, 1);
9995 CHECK_STRING (value, 2);
9997 BLOCK_INPUT;
9998 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
9999 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10000 prop_atom, XA_STRING, 8, PropModeReplace,
10001 XSTRING (value)->data, XSTRING (value)->size);
10003 /* Make sure the property is set when we return. */
10004 XFlush (FRAME_X_DISPLAY (f));
10005 UNBLOCK_INPUT;
10007 return value;
10011 DEFUN ("x-delete-window-property", Fx_delete_window_property,
10012 Sx_delete_window_property, 1, 2, 0,
10013 "Remove window property PROP from X window of FRAME.\n\
10014 FRAME nil or omitted means use the selected frame. Value is PROP.")
10015 (prop, frame)
10016 Lisp_Object prop, frame;
10018 struct frame *f = check_x_frame (frame);
10019 Atom prop_atom;
10021 CHECK_STRING (prop, 1);
10022 BLOCK_INPUT;
10023 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10024 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
10026 /* Make sure the property is removed when we return. */
10027 XFlush (FRAME_X_DISPLAY (f));
10028 UNBLOCK_INPUT;
10030 return prop;
10034 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
10035 1, 2, 0,
10036 "Value is the value of window property PROP on FRAME.\n\
10037 If FRAME is nil or omitted, use the selected frame. Value is nil\n\
10038 if FRAME hasn't a property with name PROP or if PROP has no string\n\
10039 value.")
10040 (prop, frame)
10041 Lisp_Object prop, frame;
10043 struct frame *f = check_x_frame (frame);
10044 Atom prop_atom;
10045 int rc;
10046 Lisp_Object prop_value = Qnil;
10047 char *tmp_data = NULL;
10048 Atom actual_type;
10049 int actual_format;
10050 unsigned long actual_size, bytes_remaining;
10052 CHECK_STRING (prop, 1);
10053 BLOCK_INPUT;
10054 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10055 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10056 prop_atom, 0, 0, False, XA_STRING,
10057 &actual_type, &actual_format, &actual_size,
10058 &bytes_remaining, (unsigned char **) &tmp_data);
10059 if (rc == Success)
10061 int size = bytes_remaining;
10063 XFree (tmp_data);
10064 tmp_data = NULL;
10066 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10067 prop_atom, 0, bytes_remaining,
10068 False, XA_STRING,
10069 &actual_type, &actual_format,
10070 &actual_size, &bytes_remaining,
10071 (unsigned char **) &tmp_data);
10072 if (rc == Success)
10073 prop_value = make_string (tmp_data, size);
10075 XFree (tmp_data);
10078 UNBLOCK_INPUT;
10079 return prop_value;
10084 /***********************************************************************
10085 Busy cursor
10086 ***********************************************************************/
10088 /* If non-null, an asynchronous timer that, when it expires, displays
10089 a busy cursor on all frames. */
10091 static struct atimer *busy_cursor_atimer;
10093 /* Non-zero means a busy cursor is currently shown. */
10095 static int busy_cursor_shown_p;
10097 /* Number of seconds to wait before displaying a busy cursor. */
10099 static Lisp_Object Vbusy_cursor_delay;
10101 /* Default number of seconds to wait before displaying a busy
10102 cursor. */
10104 #define DEFAULT_BUSY_CURSOR_DELAY 1
10106 /* Function prototypes. */
10108 static void show_busy_cursor P_ ((struct atimer *));
10109 static void hide_busy_cursor P_ ((void));
10112 /* Cancel a currently active busy-cursor timer, and start a new one. */
10114 void
10115 start_busy_cursor ()
10117 EMACS_TIME delay;
10118 int secs, usecs = 0;
10120 cancel_busy_cursor ();
10122 if (INTEGERP (Vbusy_cursor_delay)
10123 && XINT (Vbusy_cursor_delay) > 0)
10124 secs = XFASTINT (Vbusy_cursor_delay);
10125 else if (FLOATP (Vbusy_cursor_delay)
10126 && XFLOAT_DATA (Vbusy_cursor_delay) > 0)
10128 Lisp_Object tem;
10129 tem = Ftruncate (Vbusy_cursor_delay, Qnil);
10130 secs = XFASTINT (tem);
10131 usecs = (XFLOAT_DATA (Vbusy_cursor_delay) - secs) * 1000000;
10133 else
10134 secs = DEFAULT_BUSY_CURSOR_DELAY;
10136 EMACS_SET_SECS_USECS (delay, secs, usecs);
10137 busy_cursor_atimer = start_atimer (ATIMER_RELATIVE, delay,
10138 show_busy_cursor, NULL);
10142 /* Cancel the busy cursor timer if active, hide a busy cursor if
10143 shown. */
10145 void
10146 cancel_busy_cursor ()
10148 if (busy_cursor_atimer)
10150 cancel_atimer (busy_cursor_atimer);
10151 busy_cursor_atimer = NULL;
10154 if (busy_cursor_shown_p)
10155 hide_busy_cursor ();
10159 /* Timer function of busy_cursor_atimer. TIMER is equal to
10160 busy_cursor_atimer.
10162 Display a busy cursor on all frames by mapping the frames'
10163 busy_window. Set the busy_p flag in the frames' output_data.x
10164 structure to indicate that a busy cursor is shown on the
10165 frames. */
10167 static void
10168 show_busy_cursor (timer)
10169 struct atimer *timer;
10171 /* The timer implementation will cancel this timer automatically
10172 after this function has run. Set busy_cursor_atimer to null
10173 so that we know the timer doesn't have to be canceled. */
10174 busy_cursor_atimer = NULL;
10176 if (!busy_cursor_shown_p)
10178 Lisp_Object rest, frame;
10180 BLOCK_INPUT;
10182 FOR_EACH_FRAME (rest, frame)
10183 if (FRAME_X_P (XFRAME (frame)))
10185 struct frame *f = XFRAME (frame);
10187 f->output_data.x->busy_p = 1;
10189 if (!f->output_data.x->busy_window)
10191 unsigned long mask = CWCursor;
10192 XSetWindowAttributes attrs;
10194 attrs.cursor = f->output_data.x->busy_cursor;
10196 f->output_data.x->busy_window
10197 = XCreateWindow (FRAME_X_DISPLAY (f),
10198 FRAME_OUTER_WINDOW (f),
10199 0, 0, 32000, 32000, 0, 0,
10200 InputOnly,
10201 CopyFromParent,
10202 mask, &attrs);
10205 XMapRaised (FRAME_X_DISPLAY (f), f->output_data.x->busy_window);
10206 XFlush (FRAME_X_DISPLAY (f));
10209 busy_cursor_shown_p = 1;
10210 UNBLOCK_INPUT;
10215 /* Hide the busy cursor on all frames, if it is currently shown. */
10217 static void
10218 hide_busy_cursor ()
10220 if (busy_cursor_shown_p)
10222 Lisp_Object rest, frame;
10224 BLOCK_INPUT;
10225 FOR_EACH_FRAME (rest, frame)
10227 struct frame *f = XFRAME (frame);
10229 if (FRAME_X_P (f)
10230 /* Watch out for newly created frames. */
10231 && f->output_data.x->busy_window)
10233 XUnmapWindow (FRAME_X_DISPLAY (f), f->output_data.x->busy_window);
10234 /* Sync here because XTread_socket looks at the busy_p flag
10235 that is reset to zero below. */
10236 XSync (FRAME_X_DISPLAY (f), False);
10237 f->output_data.x->busy_p = 0;
10241 busy_cursor_shown_p = 0;
10242 UNBLOCK_INPUT;
10248 /***********************************************************************
10249 Tool tips
10250 ***********************************************************************/
10252 static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
10253 Lisp_Object));
10255 /* The frame of a currently visible tooltip, or null. */
10257 struct frame *tip_frame;
10259 /* If non-nil, a timer started that hides the last tooltip when it
10260 fires. */
10262 Lisp_Object tip_timer;
10263 Window tip_window;
10265 /* Create a frame for a tooltip on the display described by DPYINFO.
10266 PARMS is a list of frame parameters. Value is the frame. */
10268 static Lisp_Object
10269 x_create_tip_frame (dpyinfo, parms)
10270 struct x_display_info *dpyinfo;
10271 Lisp_Object parms;
10273 struct frame *f;
10274 Lisp_Object frame, tem;
10275 Lisp_Object name;
10276 long window_prompting = 0;
10277 int width, height;
10278 int count = specpdl_ptr - specpdl;
10279 struct gcpro gcpro1, gcpro2, gcpro3;
10280 struct kboard *kb;
10282 check_x ();
10284 /* Use this general default value to start with until we know if
10285 this frame has a specified name. */
10286 Vx_resource_name = Vinvocation_name;
10288 #ifdef MULTI_KBOARD
10289 kb = dpyinfo->kboard;
10290 #else
10291 kb = &the_only_kboard;
10292 #endif
10294 /* Get the name of the frame to use for resource lookup. */
10295 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
10296 if (!STRINGP (name)
10297 && !EQ (name, Qunbound)
10298 && !NILP (name))
10299 error ("Invalid frame name--not a string or nil");
10300 Vx_resource_name = name;
10302 frame = Qnil;
10303 GCPRO3 (parms, name, frame);
10304 tip_frame = f = make_frame (1);
10305 XSETFRAME (frame, f);
10306 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
10308 f->output_method = output_x_window;
10309 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
10310 bzero (f->output_data.x, sizeof (struct x_output));
10311 f->output_data.x->icon_bitmap = -1;
10312 f->output_data.x->fontset = -1;
10313 f->output_data.x->scroll_bar_foreground_pixel = -1;
10314 f->output_data.x->scroll_bar_background_pixel = -1;
10315 f->icon_name = Qnil;
10316 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
10317 #ifdef MULTI_KBOARD
10318 FRAME_KBOARD (f) = kb;
10319 #endif
10320 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10321 f->output_data.x->explicit_parent = 0;
10323 /* These colors will be set anyway later, but it's important
10324 to get the color reference counts right, so initialize them! */
10326 Lisp_Object black;
10327 struct gcpro gcpro1;
10329 black = build_string ("black");
10330 GCPRO1 (black);
10331 f->output_data.x->foreground_pixel
10332 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10333 f->output_data.x->background_pixel
10334 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10335 f->output_data.x->cursor_pixel
10336 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10337 f->output_data.x->cursor_foreground_pixel
10338 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10339 f->output_data.x->border_pixel
10340 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10341 f->output_data.x->mouse_pixel
10342 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10343 UNGCPRO;
10346 /* Set the name; the functions to which we pass f expect the name to
10347 be set. */
10348 if (EQ (name, Qunbound) || NILP (name))
10350 f->name = build_string (dpyinfo->x_id_name);
10351 f->explicit_name = 0;
10353 else
10355 f->name = name;
10356 f->explicit_name = 1;
10357 /* use the frame's title when getting resources for this frame. */
10358 specbind (Qx_resource_name, name);
10361 /* Extract the window parameters from the supplied values
10362 that are needed to determine window geometry. */
10364 Lisp_Object font;
10366 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
10368 BLOCK_INPUT;
10369 /* First, try whatever font the caller has specified. */
10370 if (STRINGP (font))
10372 tem = Fquery_fontset (font, Qnil);
10373 if (STRINGP (tem))
10374 font = x_new_fontset (f, XSTRING (tem)->data);
10375 else
10376 font = x_new_font (f, XSTRING (font)->data);
10379 /* Try out a font which we hope has bold and italic variations. */
10380 if (!STRINGP (font))
10381 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
10382 if (!STRINGP (font))
10383 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
10384 if (! STRINGP (font))
10385 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
10386 if (! STRINGP (font))
10387 /* This was formerly the first thing tried, but it finds too many fonts
10388 and takes too long. */
10389 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
10390 /* If those didn't work, look for something which will at least work. */
10391 if (! STRINGP (font))
10392 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
10393 UNBLOCK_INPUT;
10394 if (! STRINGP (font))
10395 font = build_string ("fixed");
10397 x_default_parameter (f, parms, Qfont, font,
10398 "font", "Font", RES_TYPE_STRING);
10401 x_default_parameter (f, parms, Qborder_width, make_number (2),
10402 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
10404 /* This defaults to 2 in order to match xterm. We recognize either
10405 internalBorderWidth or internalBorder (which is what xterm calls
10406 it). */
10407 if (NILP (Fassq (Qinternal_border_width, parms)))
10409 Lisp_Object value;
10411 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
10412 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
10413 if (! EQ (value, Qunbound))
10414 parms = Fcons (Fcons (Qinternal_border_width, value),
10415 parms);
10418 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
10419 "internalBorderWidth", "internalBorderWidth",
10420 RES_TYPE_NUMBER);
10422 /* Also do the stuff which must be set before the window exists. */
10423 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
10424 "foreground", "Foreground", RES_TYPE_STRING);
10425 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
10426 "background", "Background", RES_TYPE_STRING);
10427 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
10428 "pointerColor", "Foreground", RES_TYPE_STRING);
10429 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
10430 "cursorColor", "Foreground", RES_TYPE_STRING);
10431 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
10432 "borderColor", "BorderColor", RES_TYPE_STRING);
10434 /* Init faces before x_default_parameter is called for scroll-bar
10435 parameters because that function calls x_set_scroll_bar_width,
10436 which calls change_frame_size, which calls Fset_window_buffer,
10437 which runs hooks, which call Fvertical_motion. At the end, we
10438 end up in init_iterator with a null face cache, which should not
10439 happen. */
10440 init_frame_faces (f);
10442 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10443 window_prompting = x_figure_window_size (f, parms);
10445 if (window_prompting & XNegative)
10447 if (window_prompting & YNegative)
10448 f->output_data.x->win_gravity = SouthEastGravity;
10449 else
10450 f->output_data.x->win_gravity = NorthEastGravity;
10452 else
10454 if (window_prompting & YNegative)
10455 f->output_data.x->win_gravity = SouthWestGravity;
10456 else
10457 f->output_data.x->win_gravity = NorthWestGravity;
10460 f->output_data.x->size_hint_flags = window_prompting;
10462 XSetWindowAttributes attrs;
10463 unsigned long mask;
10465 BLOCK_INPUT;
10466 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
10467 if (DoesSaveUnders (dpyinfo->screen))
10468 mask |= CWSaveUnder;
10470 /* Window managers look at the override-redirect flag to determine
10471 whether or net to give windows a decoration (Xlib spec, chapter
10472 3.2.8). */
10473 attrs.override_redirect = True;
10474 attrs.save_under = True;
10475 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
10476 /* Arrange for getting MapNotify and UnmapNotify events. */
10477 attrs.event_mask = StructureNotifyMask;
10478 tip_window
10479 = FRAME_X_WINDOW (f)
10480 = XCreateWindow (FRAME_X_DISPLAY (f),
10481 FRAME_X_DISPLAY_INFO (f)->root_window,
10482 /* x, y, width, height */
10483 0, 0, 1, 1,
10484 /* Border. */
10486 CopyFromParent, InputOutput, CopyFromParent,
10487 mask, &attrs);
10488 UNBLOCK_INPUT;
10491 x_make_gc (f);
10493 x_default_parameter (f, parms, Qauto_raise, Qnil,
10494 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
10495 x_default_parameter (f, parms, Qauto_lower, Qnil,
10496 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
10497 x_default_parameter (f, parms, Qcursor_type, Qbox,
10498 "cursorType", "CursorType", RES_TYPE_SYMBOL);
10500 /* Dimensions, especially f->height, must be done via change_frame_size.
10501 Change will not be effected unless different from the current
10502 f->height. */
10503 width = f->width;
10504 height = f->height;
10505 f->height = 0;
10506 SET_FRAME_WIDTH (f, 0);
10507 change_frame_size (f, height, width, 1, 0, 0);
10509 f->no_split = 1;
10511 UNGCPRO;
10513 /* It is now ok to make the frame official even if we get an error
10514 below. And the frame needs to be on Vframe_list or making it
10515 visible won't work. */
10516 Vframe_list = Fcons (frame, Vframe_list);
10518 /* Now that the frame is official, it counts as a reference to
10519 its display. */
10520 FRAME_X_DISPLAY_INFO (f)->reference_count++;
10522 return unbind_to (count, frame);
10526 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
10527 "Show STRING in a \"tooltip\" window on frame FRAME.\n\
10528 A tooltip window is a small X window displaying a string.\n\
10530 FRAME nil or omitted means use the selected frame.\n\
10532 PARMS is an optional list of frame parameters which can be\n\
10533 used to change the tooltip's appearance.\n\
10535 Automatically hide the tooltip after TIMEOUT seconds.\n\
10536 TIMEOUT nil means use the default timeout of 5 seconds.\n\
10538 If the list of frame parameters PARAMS contains a `left' parameters,\n\
10539 the tooltip is displayed at that x-position. Otherwise it is\n\
10540 displayed at the mouse position, with offset DX added (default is 5 if\n\
10541 DX isn't specified). Likewise for the y-position; if a `top' frame\n\
10542 parameter is specified, it determines the y-position of the tooltip\n\
10543 window, otherwise it is displayed at the mouse position, with offset\n\
10544 DY added (default is -10).")
10545 (string, frame, parms, timeout, dx, dy)
10546 Lisp_Object string, frame, parms, timeout, dx, dy;
10548 struct frame *f;
10549 struct window *w;
10550 Window root, child;
10551 Lisp_Object buffer, top, left;
10552 struct buffer *old_buffer;
10553 struct text_pos pos;
10554 int i, width, height;
10555 int root_x, root_y, win_x, win_y;
10556 unsigned pmask;
10557 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10558 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10559 int count = specpdl_ptr - specpdl;
10561 specbind (Qinhibit_redisplay, Qt);
10563 GCPRO4 (string, parms, frame, timeout);
10565 CHECK_STRING (string, 0);
10566 f = check_x_frame (frame);
10567 if (NILP (timeout))
10568 timeout = make_number (5);
10569 else
10570 CHECK_NATNUM (timeout, 2);
10572 if (NILP (dx))
10573 dx = make_number (5);
10574 else
10575 CHECK_NUMBER (dx, 5);
10577 if (NILP (dy))
10578 dy = make_number (-10);
10579 else
10580 CHECK_NUMBER (dy, 6);
10582 /* Hide a previous tip, if any. */
10583 Fx_hide_tip ();
10585 /* Add default values to frame parameters. */
10586 if (NILP (Fassq (Qname, parms)))
10587 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
10588 if (NILP (Fassq (Qinternal_border_width, parms)))
10589 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
10590 if (NILP (Fassq (Qborder_width, parms)))
10591 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
10592 if (NILP (Fassq (Qborder_color, parms)))
10593 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
10594 if (NILP (Fassq (Qbackground_color, parms)))
10595 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
10596 parms);
10598 /* Create a frame for the tooltip, and record it in the global
10599 variable tip_frame. */
10600 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms);
10601 tip_frame = f = XFRAME (frame);
10603 /* Set up the frame's root window. Currently we use a size of 80
10604 columns x 40 lines. If someone wants to show a larger tip, he
10605 will loose. I don't think this is a realistic case. */
10606 w = XWINDOW (FRAME_ROOT_WINDOW (f));
10607 w->left = w->top = make_number (0);
10608 w->width = make_number (80);
10609 w->height = make_number (40);
10610 adjust_glyphs (f);
10611 w->pseudo_window_p = 1;
10613 /* Display the tooltip text in a temporary buffer. */
10614 buffer = Fget_buffer_create (build_string (" *tip*"));
10615 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
10616 old_buffer = current_buffer;
10617 set_buffer_internal_1 (XBUFFER (buffer));
10618 Ferase_buffer ();
10619 Finsert (1, &string);
10620 clear_glyph_matrix (w->desired_matrix);
10621 clear_glyph_matrix (w->current_matrix);
10622 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
10623 try_window (FRAME_ROOT_WINDOW (f), pos);
10625 /* Compute width and height of the tooltip. */
10626 width = height = 0;
10627 for (i = 0; i < w->desired_matrix->nrows; ++i)
10629 struct glyph_row *row = &w->desired_matrix->rows[i];
10630 struct glyph *last;
10631 int row_width;
10633 /* Stop at the first empty row at the end. */
10634 if (!row->enabled_p || !row->displays_text_p)
10635 break;
10637 /* Let the row go over the full width of the frame. */
10638 row->full_width_p = 1;
10640 /* There's a glyph at the end of rows that is used to place
10641 the cursor there. Don't include the width of this glyph. */
10642 if (row->used[TEXT_AREA])
10644 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
10645 row_width = row->pixel_width - last->pixel_width;
10647 else
10648 row_width = row->pixel_width;
10650 height += row->height;
10651 width = max (width, row_width);
10654 /* Add the frame's internal border to the width and height the X
10655 window should have. */
10656 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
10657 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
10659 /* User-specified position? */
10660 left = Fcdr (Fassq (Qleft, parms));
10661 top = Fcdr (Fassq (Qtop, parms));
10663 /* Move the tooltip window where the mouse pointer is. Resize and
10664 show it. */
10665 BLOCK_INPUT;
10666 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
10667 &root, &child, &root_x, &root_y, &win_x, &win_y, &pmask);
10668 UNBLOCK_INPUT;
10670 root_x += XINT (dx);
10671 root_y += XINT (dy);
10673 if (INTEGERP (left))
10674 root_x = XINT (left);
10675 if (INTEGERP (top))
10676 root_y = XINT (top);
10678 BLOCK_INPUT;
10679 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10680 root_x, root_y - height, width, height);
10681 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10682 UNBLOCK_INPUT;
10684 /* Draw into the window. */
10685 w->must_be_updated_p = 1;
10686 update_single_window (w, 1);
10688 /* Restore original current buffer. */
10689 set_buffer_internal_1 (old_buffer);
10690 windows_or_buffers_changed = old_windows_or_buffers_changed;
10692 /* Let the tip disappear after timeout seconds. */
10693 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
10694 intern ("x-hide-tip"));
10696 UNGCPRO;
10697 return unbind_to (count, Qnil);
10701 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
10702 "Hide the current tooltip window, if there is any.\n\
10703 Value is t is tooltip was open, nil otherwise.")
10706 int count = specpdl_ptr - specpdl;
10707 int deleted_p = 0;
10709 specbind (Qinhibit_redisplay, Qt);
10711 if (!NILP (tip_timer))
10713 call1 (intern ("cancel-timer"), tip_timer);
10714 tip_timer = Qnil;
10717 if (tip_frame)
10719 Lisp_Object frame;
10721 XSETFRAME (frame, tip_frame);
10722 Fdelete_frame (frame, Qt);
10723 tip_frame = NULL;
10724 deleted_p = 1;
10726 #ifdef USE_LUCID
10727 /* Bloodcurdling hack alert: The Lucid menu bar widget's
10728 redisplay procedure is not called when a tip frame over menu
10729 items is unmapped. Redisplay the menu manually... */
10731 struct frame *f = SELECTED_FRAME ();
10732 Widget w = f->output_data.x->menubar_widget;
10733 extern void xlwmenu_redisplay P_ ((Widget));
10735 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
10736 && w != None)
10738 BLOCK_INPUT;
10739 xlwmenu_redisplay (w);
10740 UNBLOCK_INPUT;
10743 #endif /* USE_LUCID */
10746 return unbind_to (count, deleted_p ? Qt : Qnil);
10751 /***********************************************************************
10752 File selection dialog
10753 ***********************************************************************/
10755 #ifdef USE_MOTIF
10757 /* Callback for "OK" and "Cancel" on file selection dialog. */
10759 static void
10760 file_dialog_cb (widget, client_data, call_data)
10761 Widget widget;
10762 XtPointer call_data, client_data;
10764 int *result = (int *) client_data;
10765 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
10766 *result = cb->reason;
10770 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
10771 "Read file name, prompting with PROMPT in directory DIR.\n\
10772 Use a file selection dialog.\n\
10773 Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
10774 specified. Don't let the user enter a file name in the file\n\
10775 selection dialog's entry field, if MUSTMATCH is non-nil.")
10776 (prompt, dir, default_filename, mustmatch)
10777 Lisp_Object prompt, dir, default_filename, mustmatch;
10779 int result;
10780 struct frame *f = SELECTED_FRAME ();
10781 Lisp_Object file = Qnil;
10782 Widget dialog, text, list, help;
10783 Arg al[10];
10784 int ac = 0;
10785 extern XtAppContext Xt_app_con;
10786 char *title;
10787 XmString dir_xmstring, pattern_xmstring;
10788 int popup_activated_flag;
10789 int count = specpdl_ptr - specpdl;
10790 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
10792 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
10793 CHECK_STRING (prompt, 0);
10794 CHECK_STRING (dir, 1);
10796 /* Prevent redisplay. */
10797 specbind (Qinhibit_redisplay, Qt);
10799 BLOCK_INPUT;
10801 /* Create the dialog with PROMPT as title, using DIR as initial
10802 directory and using "*" as pattern. */
10803 dir = Fexpand_file_name (dir, Qnil);
10804 dir_xmstring = XmStringCreateLocalized (XSTRING (dir)->data);
10805 pattern_xmstring = XmStringCreateLocalized ("*");
10807 XtSetArg (al[ac], XmNtitle, XSTRING (prompt)->data); ++ac;
10808 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
10809 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
10810 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
10811 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
10812 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
10813 "fsb", al, ac);
10814 XmStringFree (dir_xmstring);
10815 XmStringFree (pattern_xmstring);
10817 /* Add callbacks for OK and Cancel. */
10818 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
10819 (XtPointer) &result);
10820 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
10821 (XtPointer) &result);
10823 /* Disable the help button since we can't display help. */
10824 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
10825 XtSetSensitive (help, False);
10827 /* Mark OK button as default. */
10828 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
10829 XmNshowAsDefault, True, NULL);
10831 /* If MUSTMATCH is non-nil, disable the file entry field of the
10832 dialog, so that the user must select a file from the files list
10833 box. We can't remove it because we wouldn't have a way to get at
10834 the result file name, then. */
10835 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
10836 if (!NILP (mustmatch))
10838 Widget label;
10839 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
10840 XtSetSensitive (text, False);
10841 XtSetSensitive (label, False);
10844 /* Manage the dialog, so that list boxes get filled. */
10845 XtManageChild (dialog);
10847 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
10848 must include the path for this to work. */
10849 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
10850 if (STRINGP (default_filename))
10852 XmString default_xmstring;
10853 int item_pos;
10855 default_xmstring
10856 = XmStringCreateLocalized (XSTRING (default_filename)->data);
10858 if (!XmListItemExists (list, default_xmstring))
10860 /* Add a new item if DEFAULT_FILENAME is not in the list. */
10861 XmListAddItem (list, default_xmstring, 0);
10862 item_pos = 0;
10864 else
10865 item_pos = XmListItemPos (list, default_xmstring);
10866 XmStringFree (default_xmstring);
10868 /* Select the item and scroll it into view. */
10869 XmListSelectPos (list, item_pos, True);
10870 XmListSetPos (list, item_pos);
10873 #ifdef HAVE_MOTIF_2_1
10875 /* Process events until the user presses Cancel or OK. */
10876 result = 0;
10877 while (result == 0 || XtAppPending (Xt_app_con))
10878 XtAppProcessEvent (Xt_app_con, XtIMAll);
10880 #else /* not HAVE_MOTIF_2_1 */
10882 /* Process all events until the user presses Cancel or OK. */
10883 for (result = 0; result == 0;)
10885 XEvent event;
10886 Widget widget, parent;
10888 XtAppNextEvent (Xt_app_con, &event);
10890 /* See if the receiver of the event is one of the widgets of
10891 the file selection dialog. If so, dispatch it. If not,
10892 discard it. */
10893 widget = XtWindowToWidget (event.xany.display, event.xany.window);
10894 parent = widget;
10895 while (parent && parent != dialog)
10896 parent = XtParent (parent);
10898 if (parent == dialog
10899 || (event.type == Expose
10900 && !process_expose_from_menu (event)))
10901 XtDispatchEvent (&event);
10904 #endif /* not HAVE_MOTIF_2_1 */
10906 /* Get the result. */
10907 if (result == XmCR_OK)
10909 XmString text;
10910 String data;
10912 XtVaGetValues (dialog, XmNtextString, &text, NULL);
10913 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
10914 XmStringFree (text);
10915 file = build_string (data);
10916 XtFree (data);
10918 else
10919 file = Qnil;
10921 /* Clean up. */
10922 XtUnmanageChild (dialog);
10923 XtDestroyWidget (dialog);
10924 UNBLOCK_INPUT;
10925 UNGCPRO;
10927 /* Make "Cancel" equivalent to C-g. */
10928 if (NILP (file))
10929 Fsignal (Qquit, Qnil);
10931 return unbind_to (count, file);
10934 #endif /* USE_MOTIF */
10938 /***********************************************************************
10939 Initialization
10940 ***********************************************************************/
10942 void
10943 syms_of_xfns ()
10945 /* This is zero if not using X windows. */
10946 x_in_use = 0;
10948 /* The section below is built by the lisp expression at the top of the file,
10949 just above where these variables are declared. */
10950 /*&&& init symbols here &&&*/
10951 Qauto_raise = intern ("auto-raise");
10952 staticpro (&Qauto_raise);
10953 Qauto_lower = intern ("auto-lower");
10954 staticpro (&Qauto_lower);
10955 Qbar = intern ("bar");
10956 staticpro (&Qbar);
10957 Qborder_color = intern ("border-color");
10958 staticpro (&Qborder_color);
10959 Qborder_width = intern ("border-width");
10960 staticpro (&Qborder_width);
10961 Qbox = intern ("box");
10962 staticpro (&Qbox);
10963 Qcursor_color = intern ("cursor-color");
10964 staticpro (&Qcursor_color);
10965 Qcursor_type = intern ("cursor-type");
10966 staticpro (&Qcursor_type);
10967 Qgeometry = intern ("geometry");
10968 staticpro (&Qgeometry);
10969 Qicon_left = intern ("icon-left");
10970 staticpro (&Qicon_left);
10971 Qicon_top = intern ("icon-top");
10972 staticpro (&Qicon_top);
10973 Qicon_type = intern ("icon-type");
10974 staticpro (&Qicon_type);
10975 Qicon_name = intern ("icon-name");
10976 staticpro (&Qicon_name);
10977 Qinternal_border_width = intern ("internal-border-width");
10978 staticpro (&Qinternal_border_width);
10979 Qleft = intern ("left");
10980 staticpro (&Qleft);
10981 Qright = intern ("right");
10982 staticpro (&Qright);
10983 Qmouse_color = intern ("mouse-color");
10984 staticpro (&Qmouse_color);
10985 Qnone = intern ("none");
10986 staticpro (&Qnone);
10987 Qparent_id = intern ("parent-id");
10988 staticpro (&Qparent_id);
10989 Qscroll_bar_width = intern ("scroll-bar-width");
10990 staticpro (&Qscroll_bar_width);
10991 Qsuppress_icon = intern ("suppress-icon");
10992 staticpro (&Qsuppress_icon);
10993 Qundefined_color = intern ("undefined-color");
10994 staticpro (&Qundefined_color);
10995 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
10996 staticpro (&Qvertical_scroll_bars);
10997 Qvisibility = intern ("visibility");
10998 staticpro (&Qvisibility);
10999 Qwindow_id = intern ("window-id");
11000 staticpro (&Qwindow_id);
11001 Qouter_window_id = intern ("outer-window-id");
11002 staticpro (&Qouter_window_id);
11003 Qx_frame_parameter = intern ("x-frame-parameter");
11004 staticpro (&Qx_frame_parameter);
11005 Qx_resource_name = intern ("x-resource-name");
11006 staticpro (&Qx_resource_name);
11007 Quser_position = intern ("user-position");
11008 staticpro (&Quser_position);
11009 Quser_size = intern ("user-size");
11010 staticpro (&Quser_size);
11011 Qscroll_bar_foreground = intern ("scroll-bar-foreground");
11012 staticpro (&Qscroll_bar_foreground);
11013 Qscroll_bar_background = intern ("scroll-bar-background");
11014 staticpro (&Qscroll_bar_background);
11015 Qscreen_gamma = intern ("screen-gamma");
11016 staticpro (&Qscreen_gamma);
11017 Qline_spacing = intern ("line-spacing");
11018 staticpro (&Qline_spacing);
11019 Qcenter = intern ("center");
11020 staticpro (&Qcenter);
11021 Qcompound_text = intern ("compound-text");
11022 staticpro (&Qcompound_text);
11023 /* This is the end of symbol initialization. */
11025 /* Text property `display' should be nonsticky by default. */
11026 Vtext_property_default_nonsticky
11027 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
11030 Qlaplace = intern ("laplace");
11031 staticpro (&Qlaplace);
11032 Qemboss = intern ("emboss");
11033 staticpro (&Qemboss);
11034 Qedge_detection = intern ("edge-detection");
11035 staticpro (&Qedge_detection);
11036 Qheuristic = intern ("heuristic");
11037 staticpro (&Qheuristic);
11038 QCmatrix = intern (":matrix");
11039 staticpro (&QCmatrix);
11040 QCcolor_adjustment = intern (":color-adjustment");
11041 staticpro (&QCcolor_adjustment);
11042 QCmask = intern (":mask");
11043 staticpro (&QCmask);
11045 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
11046 staticpro (&Qface_set_after_frame_default);
11048 Fput (Qundefined_color, Qerror_conditions,
11049 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
11050 Fput (Qundefined_color, Qerror_message,
11051 build_string ("Undefined color"));
11053 init_x_parm_symbols ();
11055 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
11056 "Non-nil means always draw a cross over disabled images.\n\
11057 Disabled images are those having an `:algorithm disabled' property.\n\
11058 A cross is always drawn on black & white displays.");
11059 cross_disabled_images = 0;
11061 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
11062 "List of directories to search for bitmap files for X.");
11063 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
11065 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
11066 "The shape of the pointer when over text.\n\
11067 Changing the value does not affect existing frames\n\
11068 unless you set the mouse color.");
11069 Vx_pointer_shape = Qnil;
11071 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
11072 "The name Emacs uses to look up X resources.\n\
11073 `x-get-resource' uses this as the first component of the instance name\n\
11074 when requesting resource values.\n\
11075 Emacs initially sets `x-resource-name' to the name under which Emacs\n\
11076 was invoked, or to the value specified with the `-name' or `-rn'\n\
11077 switches, if present.\n\
11079 It may be useful to bind this variable locally around a call\n\
11080 to `x-get-resource'. See also the variable `x-resource-class'.");
11081 Vx_resource_name = Qnil;
11083 DEFVAR_LISP ("x-resource-class", &Vx_resource_class,
11084 "The class Emacs uses to look up X resources.\n\
11085 `x-get-resource' uses this as the first component of the instance class\n\
11086 when requesting resource values.\n\
11087 Emacs initially sets `x-resource-class' to \"Emacs\".\n\
11089 Setting this variable permanently is not a reasonable thing to do,\n\
11090 but binding this variable locally around a call to `x-get-resource'\n\
11091 is a reasonable practice. See also the variable `x-resource-name'.");
11092 Vx_resource_class = build_string (EMACS_CLASS);
11094 #if 0 /* This doesn't really do anything. */
11095 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
11096 "The shape of the pointer when not over text.\n\
11097 This variable takes effect when you create a new frame\n\
11098 or when you set the mouse color.");
11099 #endif
11100 Vx_nontext_pointer_shape = Qnil;
11102 DEFVAR_LISP ("x-busy-pointer-shape", &Vx_busy_pointer_shape,
11103 "The shape of the pointer when Emacs is busy.\n\
11104 This variable takes effect when you create a new frame\n\
11105 or when you set the mouse color.");
11106 Vx_busy_pointer_shape = Qnil;
11108 DEFVAR_BOOL ("display-busy-cursor", &display_busy_cursor_p,
11109 "Non-zero means Emacs displays a busy cursor on window systems.");
11110 display_busy_cursor_p = 1;
11112 DEFVAR_LISP ("busy-cursor-delay", &Vbusy_cursor_delay,
11113 "*Seconds to wait before displaying a busy-cursor.\n\
11114 Value must be an integer or float.");
11115 Vbusy_cursor_delay = make_number (DEFAULT_BUSY_CURSOR_DELAY);
11117 #if 0 /* This doesn't really do anything. */
11118 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
11119 "The shape of the pointer when over the mode line.\n\
11120 This variable takes effect when you create a new frame\n\
11121 or when you set the mouse color.");
11122 #endif
11123 Vx_mode_pointer_shape = Qnil;
11125 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
11126 &Vx_sensitive_text_pointer_shape,
11127 "The shape of the pointer when over mouse-sensitive text.\n\
11128 This variable takes effect when you create a new frame\n\
11129 or when you set the mouse color.");
11130 Vx_sensitive_text_pointer_shape = Qnil;
11132 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
11133 &Vx_window_horizontal_drag_shape,
11134 "Pointer shape to use for indicating a window can be dragged horizontally.\n\
11135 This variable takes effect when you create a new frame\n\
11136 or when you set the mouse color.");
11137 Vx_window_horizontal_drag_shape = Qnil;
11139 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
11140 "A string indicating the foreground color of the cursor box.");
11141 Vx_cursor_fore_pixel = Qnil;
11143 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
11144 "Non-nil if no X window manager is in use.\n\
11145 Emacs doesn't try to figure this out; this is always nil\n\
11146 unless you set it to something else.");
11147 /* We don't have any way to find this out, so set it to nil
11148 and maybe the user would like to set it to t. */
11149 Vx_no_window_manager = Qnil;
11151 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
11152 &Vx_pixel_size_width_font_regexp,
11153 "Regexp matching a font name whose width is the same as `PIXEL_SIZE'.\n\
11155 Since Emacs gets width of a font matching with this regexp from\n\
11156 PIXEL_SIZE field of the name, font finding mechanism gets faster for\n\
11157 such a font. This is especially effective for such large fonts as\n\
11158 Chinese, Japanese, and Korean.");
11159 Vx_pixel_size_width_font_regexp = Qnil;
11161 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
11162 "Time after which cached images are removed from the cache.\n\
11163 When an image has not been displayed this many seconds, remove it\n\
11164 from the image cache. Value must be an integer or nil with nil\n\
11165 meaning don't clear the cache.");
11166 Vimage_cache_eviction_delay = make_number (30 * 60);
11168 #ifdef USE_X_TOOLKIT
11169 Fprovide (intern ("x-toolkit"));
11170 #endif
11171 #ifdef USE_MOTIF
11172 Fprovide (intern ("motif"));
11173 #endif
11175 defsubr (&Sx_get_resource);
11177 /* X window properties. */
11178 defsubr (&Sx_change_window_property);
11179 defsubr (&Sx_delete_window_property);
11180 defsubr (&Sx_window_property);
11182 defsubr (&Sxw_display_color_p);
11183 defsubr (&Sx_display_grayscale_p);
11184 defsubr (&Sxw_color_defined_p);
11185 defsubr (&Sxw_color_values);
11186 defsubr (&Sx_server_max_request_size);
11187 defsubr (&Sx_server_vendor);
11188 defsubr (&Sx_server_version);
11189 defsubr (&Sx_display_pixel_width);
11190 defsubr (&Sx_display_pixel_height);
11191 defsubr (&Sx_display_mm_width);
11192 defsubr (&Sx_display_mm_height);
11193 defsubr (&Sx_display_screens);
11194 defsubr (&Sx_display_planes);
11195 defsubr (&Sx_display_color_cells);
11196 defsubr (&Sx_display_visual_class);
11197 defsubr (&Sx_display_backing_store);
11198 defsubr (&Sx_display_save_under);
11199 defsubr (&Sx_parse_geometry);
11200 defsubr (&Sx_create_frame);
11201 defsubr (&Sx_open_connection);
11202 defsubr (&Sx_close_connection);
11203 defsubr (&Sx_display_list);
11204 defsubr (&Sx_synchronize);
11205 defsubr (&Sx_focus_frame);
11207 /* Setting callback functions for fontset handler. */
11208 get_font_info_func = x_get_font_info;
11210 #if 0 /* This function pointer doesn't seem to be used anywhere.
11211 And the pointer assigned has the wrong type, anyway. */
11212 list_fonts_func = x_list_fonts;
11213 #endif
11215 load_font_func = x_load_font;
11216 find_ccl_program_func = x_find_ccl_program;
11217 query_font_func = x_query_font;
11218 set_frame_fontset_func = x_set_font;
11219 check_window_system_func = check_x;
11221 /* Images. */
11222 Qxbm = intern ("xbm");
11223 staticpro (&Qxbm);
11224 QCtype = intern (":type");
11225 staticpro (&QCtype);
11226 QCalgorithm = intern (":algorithm");
11227 staticpro (&QCalgorithm);
11228 QCheuristic_mask = intern (":heuristic-mask");
11229 staticpro (&QCheuristic_mask);
11230 QCcolor_symbols = intern (":color-symbols");
11231 staticpro (&QCcolor_symbols);
11232 QCascent = intern (":ascent");
11233 staticpro (&QCascent);
11234 QCmargin = intern (":margin");
11235 staticpro (&QCmargin);
11236 QCrelief = intern (":relief");
11237 staticpro (&QCrelief);
11238 Qpostscript = intern ("postscript");
11239 staticpro (&Qpostscript);
11240 QCloader = intern (":loader");
11241 staticpro (&QCloader);
11242 QCbounding_box = intern (":bounding-box");
11243 staticpro (&QCbounding_box);
11244 QCpt_width = intern (":pt-width");
11245 staticpro (&QCpt_width);
11246 QCpt_height = intern (":pt-height");
11247 staticpro (&QCpt_height);
11248 QCindex = intern (":index");
11249 staticpro (&QCindex);
11250 Qpbm = intern ("pbm");
11251 staticpro (&Qpbm);
11253 #if HAVE_XPM
11254 Qxpm = intern ("xpm");
11255 staticpro (&Qxpm);
11256 #endif
11258 #if HAVE_JPEG
11259 Qjpeg = intern ("jpeg");
11260 staticpro (&Qjpeg);
11261 #endif
11263 #if HAVE_TIFF
11264 Qtiff = intern ("tiff");
11265 staticpro (&Qtiff);
11266 #endif
11268 #if HAVE_GIF
11269 Qgif = intern ("gif");
11270 staticpro (&Qgif);
11271 #endif
11273 #if HAVE_PNG
11274 Qpng = intern ("png");
11275 staticpro (&Qpng);
11276 #endif
11278 defsubr (&Sclear_image_cache);
11279 defsubr (&Simage_size);
11280 defsubr (&Simage_mask_p);
11282 busy_cursor_atimer = NULL;
11283 busy_cursor_shown_p = 0;
11285 defsubr (&Sx_show_tip);
11286 defsubr (&Sx_hide_tip);
11287 staticpro (&tip_timer);
11288 tip_timer = Qnil;
11290 #ifdef USE_MOTIF
11291 defsubr (&Sx_file_dialog);
11292 #endif
11296 void
11297 init_xfns ()
11299 image_types = NULL;
11300 Vimage_types = Qnil;
11302 define_image_type (&xbm_type);
11303 define_image_type (&gs_type);
11304 define_image_type (&pbm_type);
11306 #if HAVE_XPM
11307 define_image_type (&xpm_type);
11308 #endif
11310 #if HAVE_JPEG
11311 define_image_type (&jpeg_type);
11312 #endif
11314 #if HAVE_TIFF
11315 define_image_type (&tiff_type);
11316 #endif
11318 #if HAVE_GIF
11319 define_image_type (&gif_type);
11320 #endif
11322 #if HAVE_PNG
11323 define_image_type (&png_type);
11324 #endif
11327 #endif /* HAVE_X_WINDOWS */