*** empty log message ***
[bpt/emacs.git] / src / xfns.c
1 /* Functions for the X window system.
2 Copyright (C) 1989, 92, 93, 94, 95, 96, 1997, 1998, 1999, 2000
3 Free Software Foundation.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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. */
21
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <math.h>
26
27 /* This makes the fields of a Display accessible, in Xlib header files. */
28
29 #define XLIB_ILLEGAL_ACCESS
30
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"
47
48 #ifdef HAVE_X_WINDOWS
49
50 #include <ctype.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53
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
63
64 #ifdef USE_X_TOOLKIT
65 #include <X11/Shell.h>
66
67 #ifndef USE_MOTIF
68 #include <X11/Xaw/Paned.h>
69 #include <X11/Xaw/Label.h>
70 #endif /* USE_MOTIF */
71
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
79
80 #include "widget.h"
81
82 #include "../lwlib/lwlib.h"
83
84 #ifdef USE_MOTIF
85 #include <Xm/Xm.h>
86 #include <Xm/DialogS.h>
87 #include <Xm/FileSB.h>
88 #endif
89
90 /* Do the EDITRES protocol if running X11R5
91 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
92
93 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
94 #define HACK_EDITRES
95 extern void _XEditResCheckMessages ();
96 #endif /* R5 + Athena */
97
98 /* Unique id counter for widgets created by the Lucid Widget Library. */
99
100 extern LWLIB_ID widget_id_tick;
101
102 #ifdef USE_LUCID
103 /* This is part of a kludge--see lwlib/xlwmenu.c. */
104 extern XFontStruct *xlwmenu_default_font;
105 #endif
106
107 extern void free_frame_menubar ();
108 extern double atof ();
109
110 #endif /* USE_X_TOOLKIT */
111
112 #define min(a,b) ((a) < (b) ? (a) : (b))
113 #define max(a,b) ((a) > (b) ? (a) : (b))
114
115 #ifdef HAVE_X11R4
116 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
117 #else
118 #define MAXREQUEST(dpy) ((dpy)->max_request_size)
119 #endif
120
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. */
124
125 int gray_bitmap_width = gray_width;
126 int gray_bitmap_height = gray_height;
127 char *gray_bitmap_bits = gray_bits;
128
129 /* The name we're using in resource queries. Most often "emacs". */
130
131 Lisp_Object Vx_resource_name;
132
133 /* The application class we're using in resource queries.
134 Normally "Emacs". */
135
136 Lisp_Object Vx_resource_class;
137
138 /* Non-zero means we're allowed to display a busy cursor. */
139
140 int display_busy_cursor_p;
141
142 /* The background and shape of the mouse pointer, and shape when not
143 over text or in the modeline. */
144
145 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
146 Lisp_Object Vx_busy_pointer_shape;
147
148 /* The shape when over mouse-sensitive text. */
149
150 Lisp_Object Vx_sensitive_text_pointer_shape;
151
152 /* If non-nil, the pointer shape to indicate that windows can be
153 dragged horizontally. */
154
155 Lisp_Object Vx_window_horizontal_drag_shape;
156
157 /* Color of chars displayed in cursor box. */
158
159 Lisp_Object Vx_cursor_fore_pixel;
160
161 /* Nonzero if using X. */
162
163 static int x_in_use;
164
165 /* Non nil if no window manager is in use. */
166
167 Lisp_Object Vx_no_window_manager;
168
169 /* Search path for bitmap files. */
170
171 Lisp_Object Vx_bitmap_file_path;
172
173 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
174
175 Lisp_Object Vx_pixel_size_width_font_regexp;
176
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.
181
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)))))
208
209 */
210
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;
247
248 /* The below are defined in frame.c. */
249
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;
253
254 extern Lisp_Object Vwindow_system_version;
255
256 Lisp_Object Qface_set_after_frame_default;
257
258 #if GLYPH_DEBUG
259 int image_cache_refcount, dpyinfo_refcount;
260 #endif
261
262
263 \f
264 /* Error if we are not connected to X. */
265
266 void
267 check_x ()
268 {
269 if (! x_in_use)
270 error ("X windows are not in use or not initialized");
271 }
272
273 /* Nonzero if we can use mouse menus.
274 You should not call this unless HAVE_MENUS is defined. */
275
276 int
277 have_menus_p ()
278 {
279 return x_in_use;
280 }
281
282 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
283 and checking validity for X. */
284
285 FRAME_PTR
286 check_x_frame (frame)
287 Lisp_Object frame;
288 {
289 FRAME_PTR f;
290
291 if (NILP (frame))
292 frame = selected_frame;
293 CHECK_LIVE_FRAME (frame, 0);
294 f = XFRAME (frame);
295 if (! FRAME_X_P (f))
296 error ("Non-X frame used");
297 return f;
298 }
299
300 /* Let the user specify an X display with a frame.
301 nil stands for the selected frame--or, if that is not an X frame,
302 the first X display on the list. */
303
304 static struct x_display_info *
305 check_x_display_info (frame)
306 Lisp_Object frame;
307 {
308 struct x_display_info *dpyinfo = NULL;
309
310 if (NILP (frame))
311 {
312 struct frame *sf = XFRAME (selected_frame);
313
314 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
315 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
316 else if (x_display_list != 0)
317 dpyinfo = x_display_list;
318 else
319 error ("X windows are not in use or not initialized");
320 }
321 else if (STRINGP (frame))
322 dpyinfo = x_display_info_for_name (frame);
323 else
324 {
325 FRAME_PTR f;
326
327 CHECK_LIVE_FRAME (frame, 0);
328 f = XFRAME (frame);
329 if (! FRAME_X_P (f))
330 error ("Non-X frame used");
331 dpyinfo = FRAME_X_DISPLAY_INFO (f);
332 }
333
334 return dpyinfo;
335 }
336
337 \f
338 /* Return the Emacs frame-object corresponding to an X window.
339 It could be the frame's main window or an icon window. */
340
341 /* This function can be called during GC, so use GC_xxx type test macros. */
342
343 struct frame *
344 x_window_to_frame (dpyinfo, wdesc)
345 struct x_display_info *dpyinfo;
346 int wdesc;
347 {
348 Lisp_Object tail, frame;
349 struct frame *f;
350
351 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
352 {
353 frame = XCAR (tail);
354 if (!GC_FRAMEP (frame))
355 continue;
356 f = XFRAME (frame);
357 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
358 continue;
359 if (f->output_data.x->busy_window == wdesc)
360 return f;
361 #ifdef USE_X_TOOLKIT
362 if ((f->output_data.x->edit_widget
363 && XtWindow (f->output_data.x->edit_widget) == wdesc)
364 /* A tooltip frame? */
365 || (!f->output_data.x->edit_widget
366 && FRAME_X_WINDOW (f) == wdesc)
367 || f->output_data.x->icon_desc == wdesc)
368 return f;
369 #else /* not USE_X_TOOLKIT */
370 if (FRAME_X_WINDOW (f) == wdesc
371 || f->output_data.x->icon_desc == wdesc)
372 return f;
373 #endif /* not USE_X_TOOLKIT */
374 }
375 return 0;
376 }
377
378 #ifdef USE_X_TOOLKIT
379 /* Like x_window_to_frame but also compares the window with the widget's
380 windows. */
381
382 struct frame *
383 x_any_window_to_frame (dpyinfo, wdesc)
384 struct x_display_info *dpyinfo;
385 int wdesc;
386 {
387 Lisp_Object tail, frame;
388 struct frame *f, *found;
389 struct x_output *x;
390
391 found = NULL;
392 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
393 {
394 frame = XCAR (tail);
395 if (!GC_FRAMEP (frame))
396 continue;
397
398 f = XFRAME (frame);
399 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
400 {
401 /* This frame matches if the window is any of its widgets. */
402 x = f->output_data.x;
403 if (x->busy_window == wdesc)
404 found = f;
405 else if (x->widget)
406 {
407 if (wdesc == XtWindow (x->widget)
408 || wdesc == XtWindow (x->column_widget)
409 || wdesc == XtWindow (x->edit_widget))
410 found = f;
411 /* Match if the window is this frame's menubar. */
412 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
413 found = f;
414 }
415 else if (FRAME_X_WINDOW (f) == wdesc)
416 /* A tooltip frame. */
417 found = f;
418 }
419 }
420
421 return found;
422 }
423
424 /* Likewise, but exclude the menu bar widget. */
425
426 struct frame *
427 x_non_menubar_window_to_frame (dpyinfo, wdesc)
428 struct x_display_info *dpyinfo;
429 int wdesc;
430 {
431 Lisp_Object tail, frame;
432 struct frame *f;
433 struct x_output *x;
434
435 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
436 {
437 frame = XCAR (tail);
438 if (!GC_FRAMEP (frame))
439 continue;
440 f = XFRAME (frame);
441 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
442 continue;
443 x = f->output_data.x;
444 /* This frame matches if the window is any of its widgets. */
445 if (x->busy_window == wdesc)
446 return f;
447 else if (x->widget)
448 {
449 if (wdesc == XtWindow (x->widget)
450 || wdesc == XtWindow (x->column_widget)
451 || wdesc == XtWindow (x->edit_widget))
452 return f;
453 }
454 else if (FRAME_X_WINDOW (f) == wdesc)
455 /* A tooltip frame. */
456 return f;
457 }
458 return 0;
459 }
460
461 /* Likewise, but consider only the menu bar widget. */
462
463 struct frame *
464 x_menubar_window_to_frame (dpyinfo, wdesc)
465 struct x_display_info *dpyinfo;
466 int wdesc;
467 {
468 Lisp_Object tail, frame;
469 struct frame *f;
470 struct x_output *x;
471
472 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
473 {
474 frame = XCAR (tail);
475 if (!GC_FRAMEP (frame))
476 continue;
477 f = XFRAME (frame);
478 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
479 continue;
480 x = f->output_data.x;
481 /* Match if the window is this frame's menubar. */
482 if (x->menubar_widget
483 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
484 return f;
485 }
486 return 0;
487 }
488
489 /* Return the frame whose principal (outermost) window is WDESC.
490 If WDESC is some other (smaller) window, we return 0. */
491
492 struct frame *
493 x_top_window_to_frame (dpyinfo, wdesc)
494 struct x_display_info *dpyinfo;
495 int wdesc;
496 {
497 Lisp_Object tail, frame;
498 struct frame *f;
499 struct x_output *x;
500
501 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
502 {
503 frame = XCAR (tail);
504 if (!GC_FRAMEP (frame))
505 continue;
506 f = XFRAME (frame);
507 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
508 continue;
509 x = f->output_data.x;
510
511 if (x->widget)
512 {
513 /* This frame matches if the window is its topmost widget. */
514 if (wdesc == XtWindow (x->widget))
515 return f;
516 #if 0 /* I don't know why it did this,
517 but it seems logically wrong,
518 and it causes trouble for MapNotify events. */
519 /* Match if the window is this frame's menubar. */
520 if (x->menubar_widget
521 && wdesc == XtWindow (x->menubar_widget))
522 return f;
523 #endif
524 }
525 else if (FRAME_X_WINDOW (f) == wdesc)
526 /* Tooltip frame. */
527 return f;
528 }
529 return 0;
530 }
531 #endif /* USE_X_TOOLKIT */
532
533 \f
534
535 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
536 id, which is just an int that this section returns. Bitmaps are
537 reference counted so they can be shared among frames.
538
539 Bitmap indices are guaranteed to be > 0, so a negative number can
540 be used to indicate no bitmap.
541
542 If you use x_create_bitmap_from_data, then you must keep track of
543 the bitmaps yourself. That is, creating a bitmap from the same
544 data more than once will not be caught. */
545
546
547 /* Functions to access the contents of a bitmap, given an id. */
548
549 int
550 x_bitmap_height (f, id)
551 FRAME_PTR f;
552 int id;
553 {
554 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
555 }
556
557 int
558 x_bitmap_width (f, id)
559 FRAME_PTR f;
560 int id;
561 {
562 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
563 }
564
565 int
566 x_bitmap_pixmap (f, id)
567 FRAME_PTR f;
568 int id;
569 {
570 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
571 }
572
573
574 /* Allocate a new bitmap record. Returns index of new record. */
575
576 static int
577 x_allocate_bitmap_record (f)
578 FRAME_PTR f;
579 {
580 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
581 int i;
582
583 if (dpyinfo->bitmaps == NULL)
584 {
585 dpyinfo->bitmaps_size = 10;
586 dpyinfo->bitmaps
587 = (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
588 dpyinfo->bitmaps_last = 1;
589 return 1;
590 }
591
592 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
593 return ++dpyinfo->bitmaps_last;
594
595 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
596 if (dpyinfo->bitmaps[i].refcount == 0)
597 return i + 1;
598
599 dpyinfo->bitmaps_size *= 2;
600 dpyinfo->bitmaps
601 = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
602 dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
603 return ++dpyinfo->bitmaps_last;
604 }
605
606 /* Add one reference to the reference count of the bitmap with id ID. */
607
608 void
609 x_reference_bitmap (f, id)
610 FRAME_PTR f;
611 int id;
612 {
613 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
614 }
615
616 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
617
618 int
619 x_create_bitmap_from_data (f, bits, width, height)
620 struct frame *f;
621 char *bits;
622 unsigned int width, height;
623 {
624 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
625 Pixmap bitmap;
626 int id;
627
628 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
629 bits, width, height);
630
631 if (! bitmap)
632 return -1;
633
634 id = x_allocate_bitmap_record (f);
635 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
636 dpyinfo->bitmaps[id - 1].file = NULL;
637 dpyinfo->bitmaps[id - 1].refcount = 1;
638 dpyinfo->bitmaps[id - 1].depth = 1;
639 dpyinfo->bitmaps[id - 1].height = height;
640 dpyinfo->bitmaps[id - 1].width = width;
641
642 return id;
643 }
644
645 /* Create bitmap from file FILE for frame F. */
646
647 int
648 x_create_bitmap_from_file (f, file)
649 struct frame *f;
650 Lisp_Object file;
651 {
652 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
653 unsigned int width, height;
654 Pixmap bitmap;
655 int xhot, yhot, result, id;
656 Lisp_Object found;
657 int fd;
658 char *filename;
659
660 /* Look for an existing bitmap with the same name. */
661 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
662 {
663 if (dpyinfo->bitmaps[id].refcount
664 && dpyinfo->bitmaps[id].file
665 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
666 {
667 ++dpyinfo->bitmaps[id].refcount;
668 return id + 1;
669 }
670 }
671
672 /* Search bitmap-file-path for the file, if appropriate. */
673 fd = openp (Vx_bitmap_file_path, file, "", &found, 0);
674 if (fd < 0)
675 return -1;
676 emacs_close (fd);
677
678 filename = (char *) XSTRING (found)->data;
679
680 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
681 filename, &width, &height, &bitmap, &xhot, &yhot);
682 if (result != BitmapSuccess)
683 return -1;
684
685 id = x_allocate_bitmap_record (f);
686 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
687 dpyinfo->bitmaps[id - 1].refcount = 1;
688 dpyinfo->bitmaps[id - 1].file
689 = (char *) xmalloc (STRING_BYTES (XSTRING (file)) + 1);
690 dpyinfo->bitmaps[id - 1].depth = 1;
691 dpyinfo->bitmaps[id - 1].height = height;
692 dpyinfo->bitmaps[id - 1].width = width;
693 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
694
695 return id;
696 }
697
698 /* Remove reference to bitmap with id number ID. */
699
700 void
701 x_destroy_bitmap (f, id)
702 FRAME_PTR f;
703 int id;
704 {
705 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
706
707 if (id > 0)
708 {
709 --dpyinfo->bitmaps[id - 1].refcount;
710 if (dpyinfo->bitmaps[id - 1].refcount == 0)
711 {
712 BLOCK_INPUT;
713 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
714 if (dpyinfo->bitmaps[id - 1].file)
715 {
716 xfree (dpyinfo->bitmaps[id - 1].file);
717 dpyinfo->bitmaps[id - 1].file = NULL;
718 }
719 UNBLOCK_INPUT;
720 }
721 }
722 }
723
724 /* Free all the bitmaps for the display specified by DPYINFO. */
725
726 static void
727 x_destroy_all_bitmaps (dpyinfo)
728 struct x_display_info *dpyinfo;
729 {
730 int i;
731 for (i = 0; i < dpyinfo->bitmaps_last; i++)
732 if (dpyinfo->bitmaps[i].refcount > 0)
733 {
734 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
735 if (dpyinfo->bitmaps[i].file)
736 xfree (dpyinfo->bitmaps[i].file);
737 }
738 dpyinfo->bitmaps_last = 0;
739 }
740 \f
741 /* Connect the frame-parameter names for X frames
742 to the ways of passing the parameter values to the window system.
743
744 The name of a parameter, as a Lisp symbol,
745 has an `x-frame-parameter' property which is an integer in Lisp
746 that is an index in this table. */
747
748 struct x_frame_parm_table
749 {
750 char *name;
751 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
752 };
753
754 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
755 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
756 static void x_change_window_heights P_ ((Lisp_Object, int));
757 static void x_disable_image P_ ((struct frame *, struct image *));
758 static void x_create_im P_ ((struct frame *));
759 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
760 static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
761 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
762 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
763 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
764 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
765 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
766 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
767 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
768 void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
769 void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
770 void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
771 Lisp_Object));
772 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
773 void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
774 void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
775 void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
776 Lisp_Object));
777 void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
778 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
779 void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
780 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
781 void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
782 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
783 void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
784 Lisp_Object));
785 void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
786 Lisp_Object));
787 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
788 Lisp_Object,
789 Lisp_Object,
790 char *, char *,
791 int));
792 static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
793 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
794 Lisp_Object));
795 static void init_color_table P_ ((void));
796 static void free_color_table P_ ((void));
797 static unsigned long *colors_in_color_table P_ ((int *n));
798 static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
799 static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
800
801
802
803 static struct x_frame_parm_table x_frame_parms[] =
804 {
805 "auto-raise", x_set_autoraise,
806 "auto-lower", x_set_autolower,
807 "background-color", x_set_background_color,
808 "border-color", x_set_border_color,
809 "border-width", x_set_border_width,
810 "cursor-color", x_set_cursor_color,
811 "cursor-type", x_set_cursor_type,
812 "font", x_set_font,
813 "foreground-color", x_set_foreground_color,
814 "icon-name", x_set_icon_name,
815 "icon-type", x_set_icon_type,
816 "internal-border-width", x_set_internal_border_width,
817 "menu-bar-lines", x_set_menu_bar_lines,
818 "mouse-color", x_set_mouse_color,
819 "name", x_explicitly_set_name,
820 "scroll-bar-width", x_set_scroll_bar_width,
821 "title", x_set_title,
822 "unsplittable", x_set_unsplittable,
823 "vertical-scroll-bars", x_set_vertical_scroll_bars,
824 "visibility", x_set_visibility,
825 "tool-bar-lines", x_set_tool_bar_lines,
826 "scroll-bar-foreground", x_set_scroll_bar_foreground,
827 "scroll-bar-background", x_set_scroll_bar_background,
828 "screen-gamma", x_set_screen_gamma,
829 "line-spacing", x_set_line_spacing
830 };
831
832 /* Attach the `x-frame-parameter' properties to
833 the Lisp symbol names of parameters relevant to X. */
834
835 void
836 init_x_parm_symbols ()
837 {
838 int i;
839
840 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
841 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
842 make_number (i));
843 }
844 \f
845 /* Change the parameters of frame F as specified by ALIST.
846 If a parameter is not specially recognized, do nothing special;
847 otherwise call the `x_set_...' function for that parameter.
848 Except for certain geometry properties, always call store_frame_param
849 to store the new value in the parameter alist. */
850
851 void
852 x_set_frame_parameters (f, alist)
853 FRAME_PTR f;
854 Lisp_Object alist;
855 {
856 Lisp_Object tail;
857
858 /* If both of these parameters are present, it's more efficient to
859 set them both at once. So we wait until we've looked at the
860 entire list before we set them. */
861 int width, height;
862
863 /* Same here. */
864 Lisp_Object left, top;
865
866 /* Same with these. */
867 Lisp_Object icon_left, icon_top;
868
869 /* Record in these vectors all the parms specified. */
870 Lisp_Object *parms;
871 Lisp_Object *values;
872 int i, p;
873 int left_no_change = 0, top_no_change = 0;
874 int icon_left_no_change = 0, icon_top_no_change = 0;
875
876 struct gcpro gcpro1, gcpro2;
877
878 i = 0;
879 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
880 i++;
881
882 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
883 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
884
885 /* Extract parm names and values into those vectors. */
886
887 i = 0;
888 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
889 {
890 Lisp_Object elt;
891
892 elt = Fcar (tail);
893 parms[i] = Fcar (elt);
894 values[i] = Fcdr (elt);
895 i++;
896 }
897 /* TAIL and ALIST are not used again below here. */
898 alist = tail = Qnil;
899
900 GCPRO2 (*parms, *values);
901 gcpro1.nvars = i;
902 gcpro2.nvars = i;
903
904 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
905 because their values appear in VALUES and strings are not valid. */
906 top = left = Qunbound;
907 icon_left = icon_top = Qunbound;
908
909 /* Provide default values for HEIGHT and WIDTH. */
910 if (FRAME_NEW_WIDTH (f))
911 width = FRAME_NEW_WIDTH (f);
912 else
913 width = FRAME_WIDTH (f);
914
915 if (FRAME_NEW_HEIGHT (f))
916 height = FRAME_NEW_HEIGHT (f);
917 else
918 height = FRAME_HEIGHT (f);
919
920 /* Process foreground_color and background_color before anything else.
921 They are independent of other properties, but other properties (e.g.,
922 cursor_color) are dependent upon them. */
923 for (p = 0; p < i; p++)
924 {
925 Lisp_Object prop, val;
926
927 prop = parms[p];
928 val = values[p];
929 if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
930 {
931 register Lisp_Object param_index, old_value;
932
933 param_index = Fget (prop, Qx_frame_parameter);
934 old_value = get_frame_param (f, prop);
935 store_frame_param (f, prop, val);
936 if (NATNUMP (param_index)
937 && (XFASTINT (param_index)
938 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
939 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
940 }
941 }
942
943 /* Now process them in reverse of specified order. */
944 for (i--; i >= 0; i--)
945 {
946 Lisp_Object prop, val;
947
948 prop = parms[i];
949 val = values[i];
950
951 if (EQ (prop, Qwidth) && NUMBERP (val))
952 width = XFASTINT (val);
953 else if (EQ (prop, Qheight) && NUMBERP (val))
954 height = XFASTINT (val);
955 else if (EQ (prop, Qtop))
956 top = val;
957 else if (EQ (prop, Qleft))
958 left = val;
959 else if (EQ (prop, Qicon_top))
960 icon_top = val;
961 else if (EQ (prop, Qicon_left))
962 icon_left = val;
963 else if (EQ (prop, Qforeground_color) || EQ (prop, Qbackground_color))
964 /* Processed above. */
965 continue;
966 else
967 {
968 register Lisp_Object param_index, old_value;
969
970 param_index = Fget (prop, Qx_frame_parameter);
971 old_value = get_frame_param (f, prop);
972 store_frame_param (f, prop, val);
973 if (NATNUMP (param_index)
974 && (XFASTINT (param_index)
975 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
976 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
977 }
978 }
979
980 /* Don't die if just one of these was set. */
981 if (EQ (left, Qunbound))
982 {
983 left_no_change = 1;
984 if (f->output_data.x->left_pos < 0)
985 left = Fcons (Qplus, Fcons (make_number (f->output_data.x->left_pos), Qnil));
986 else
987 XSETINT (left, f->output_data.x->left_pos);
988 }
989 if (EQ (top, Qunbound))
990 {
991 top_no_change = 1;
992 if (f->output_data.x->top_pos < 0)
993 top = Fcons (Qplus, Fcons (make_number (f->output_data.x->top_pos), Qnil));
994 else
995 XSETINT (top, f->output_data.x->top_pos);
996 }
997
998 /* If one of the icon positions was not set, preserve or default it. */
999 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
1000 {
1001 icon_left_no_change = 1;
1002 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
1003 if (NILP (icon_left))
1004 XSETINT (icon_left, 0);
1005 }
1006 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
1007 {
1008 icon_top_no_change = 1;
1009 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
1010 if (NILP (icon_top))
1011 XSETINT (icon_top, 0);
1012 }
1013
1014 /* Don't set these parameters unless they've been explicitly
1015 specified. The window might be mapped or resized while we're in
1016 this function, and we don't want to override that unless the lisp
1017 code has asked for it.
1018
1019 Don't set these parameters unless they actually differ from the
1020 window's current parameters; the window may not actually exist
1021 yet. */
1022 {
1023 Lisp_Object frame;
1024
1025 check_frame_size (f, &height, &width);
1026
1027 XSETFRAME (frame, f);
1028
1029 if (width != FRAME_WIDTH (f)
1030 || height != FRAME_HEIGHT (f)
1031 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
1032 Fset_frame_size (frame, make_number (width), make_number (height));
1033
1034 if ((!NILP (left) || !NILP (top))
1035 && ! (left_no_change && top_no_change)
1036 && ! (NUMBERP (left) && XINT (left) == f->output_data.x->left_pos
1037 && NUMBERP (top) && XINT (top) == f->output_data.x->top_pos))
1038 {
1039 int leftpos = 0;
1040 int toppos = 0;
1041
1042 /* Record the signs. */
1043 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
1044 if (EQ (left, Qminus))
1045 f->output_data.x->size_hint_flags |= XNegative;
1046 else if (INTEGERP (left))
1047 {
1048 leftpos = XINT (left);
1049 if (leftpos < 0)
1050 f->output_data.x->size_hint_flags |= XNegative;
1051 }
1052 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1053 && CONSP (XCDR (left))
1054 && INTEGERP (XCAR (XCDR (left))))
1055 {
1056 leftpos = - XINT (XCAR (XCDR (left)));
1057 f->output_data.x->size_hint_flags |= XNegative;
1058 }
1059 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1060 && CONSP (XCDR (left))
1061 && INTEGERP (XCAR (XCDR (left))))
1062 {
1063 leftpos = XINT (XCAR (XCDR (left)));
1064 }
1065
1066 if (EQ (top, Qminus))
1067 f->output_data.x->size_hint_flags |= YNegative;
1068 else if (INTEGERP (top))
1069 {
1070 toppos = XINT (top);
1071 if (toppos < 0)
1072 f->output_data.x->size_hint_flags |= YNegative;
1073 }
1074 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1075 && CONSP (XCDR (top))
1076 && INTEGERP (XCAR (XCDR (top))))
1077 {
1078 toppos = - XINT (XCAR (XCDR (top)));
1079 f->output_data.x->size_hint_flags |= YNegative;
1080 }
1081 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1082 && CONSP (XCDR (top))
1083 && INTEGERP (XCAR (XCDR (top))))
1084 {
1085 toppos = XINT (XCAR (XCDR (top)));
1086 }
1087
1088
1089 /* Store the numeric value of the position. */
1090 f->output_data.x->top_pos = toppos;
1091 f->output_data.x->left_pos = leftpos;
1092
1093 f->output_data.x->win_gravity = NorthWestGravity;
1094
1095 /* Actually set that position, and convert to absolute. */
1096 x_set_offset (f, leftpos, toppos, -1);
1097 }
1098
1099 if ((!NILP (icon_left) || !NILP (icon_top))
1100 && ! (icon_left_no_change && icon_top_no_change))
1101 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1102 }
1103
1104 UNGCPRO;
1105 }
1106
1107 /* Store the screen positions of frame F into XPTR and YPTR.
1108 These are the positions of the containing window manager window,
1109 not Emacs's own window. */
1110
1111 void
1112 x_real_positions (f, xptr, yptr)
1113 FRAME_PTR f;
1114 int *xptr, *yptr;
1115 {
1116 int win_x, win_y;
1117 Window child;
1118
1119 /* This is pretty gross, but seems to be the easiest way out of
1120 the problem that arises when restarting window-managers. */
1121
1122 #ifdef USE_X_TOOLKIT
1123 Window outer = (f->output_data.x->widget
1124 ? XtWindow (f->output_data.x->widget)
1125 : FRAME_X_WINDOW (f));
1126 #else
1127 Window outer = f->output_data.x->window_desc;
1128 #endif
1129 Window tmp_root_window;
1130 Window *tmp_children;
1131 unsigned int tmp_nchildren;
1132
1133 while (1)
1134 {
1135 int count = x_catch_errors (FRAME_X_DISPLAY (f));
1136 Window outer_window;
1137
1138 XQueryTree (FRAME_X_DISPLAY (f), outer, &tmp_root_window,
1139 &f->output_data.x->parent_desc,
1140 &tmp_children, &tmp_nchildren);
1141 XFree ((char *) tmp_children);
1142
1143 win_x = win_y = 0;
1144
1145 /* Find the position of the outside upper-left corner of
1146 the inner window, with respect to the outer window. */
1147 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
1148 outer_window = f->output_data.x->parent_desc;
1149 else
1150 outer_window = outer;
1151
1152 XTranslateCoordinates (FRAME_X_DISPLAY (f),
1153
1154 /* From-window, to-window. */
1155 outer_window,
1156 FRAME_X_DISPLAY_INFO (f)->root_window,
1157
1158 /* From-position, to-position. */
1159 0, 0, &win_x, &win_y,
1160
1161 /* Child of win. */
1162 &child);
1163
1164 /* It is possible for the window returned by the XQueryNotify
1165 to become invalid by the time we call XTranslateCoordinates.
1166 That can happen when you restart some window managers.
1167 If so, we get an error in XTranslateCoordinates.
1168 Detect that and try the whole thing over. */
1169 if (! x_had_errors_p (FRAME_X_DISPLAY (f)))
1170 {
1171 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1172 break;
1173 }
1174
1175 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1176 }
1177
1178 *xptr = win_x;
1179 *yptr = win_y;
1180 }
1181
1182 /* Insert a description of internally-recorded parameters of frame X
1183 into the parameter alist *ALISTPTR that is to be given to the user.
1184 Only parameters that are specific to the X window system
1185 and whose values are not correctly recorded in the frame's
1186 param_alist need to be considered here. */
1187
1188 void
1189 x_report_frame_params (f, alistptr)
1190 struct frame *f;
1191 Lisp_Object *alistptr;
1192 {
1193 char buf[16];
1194 Lisp_Object tem;
1195
1196 /* Represent negative positions (off the top or left screen edge)
1197 in a way that Fmodify_frame_parameters will understand correctly. */
1198 XSETINT (tem, f->output_data.x->left_pos);
1199 if (f->output_data.x->left_pos >= 0)
1200 store_in_alist (alistptr, Qleft, tem);
1201 else
1202 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1203
1204 XSETINT (tem, f->output_data.x->top_pos);
1205 if (f->output_data.x->top_pos >= 0)
1206 store_in_alist (alistptr, Qtop, tem);
1207 else
1208 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1209
1210 store_in_alist (alistptr, Qborder_width,
1211 make_number (f->output_data.x->border_width));
1212 store_in_alist (alistptr, Qinternal_border_width,
1213 make_number (f->output_data.x->internal_border_width));
1214 sprintf (buf, "%ld", (long) FRAME_X_WINDOW (f));
1215 store_in_alist (alistptr, Qwindow_id,
1216 build_string (buf));
1217 #ifdef USE_X_TOOLKIT
1218 /* Tooltip frame may not have this widget. */
1219 if (f->output_data.x->widget)
1220 #endif
1221 sprintf (buf, "%ld", (long) FRAME_OUTER_WINDOW (f));
1222 store_in_alist (alistptr, Qouter_window_id,
1223 build_string (buf));
1224 store_in_alist (alistptr, Qicon_name, f->icon_name);
1225 FRAME_SAMPLE_VISIBILITY (f);
1226 store_in_alist (alistptr, Qvisibility,
1227 (FRAME_VISIBLE_P (f) ? Qt
1228 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1229 store_in_alist (alistptr, Qdisplay,
1230 XCAR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
1231
1232 if (f->output_data.x->parent_desc == FRAME_X_DISPLAY_INFO (f)->root_window)
1233 tem = Qnil;
1234 else
1235 XSETFASTINT (tem, f->output_data.x->parent_desc);
1236 store_in_alist (alistptr, Qparent_id, tem);
1237 }
1238 \f
1239
1240
1241 /* Gamma-correct COLOR on frame F. */
1242
1243 void
1244 gamma_correct (f, color)
1245 struct frame *f;
1246 XColor *color;
1247 {
1248 if (f->gamma)
1249 {
1250 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
1251 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
1252 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
1253 }
1254 }
1255
1256
1257 /* Decide if color named COLOR_NAME is valid for use on frame F. If
1258 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
1259 allocate the color. Value is zero if COLOR_NAME is invalid, or
1260 no color could be allocated. */
1261
1262 int
1263 x_defined_color (f, color_name, color, alloc_p)
1264 struct frame *f;
1265 char *color_name;
1266 XColor *color;
1267 int alloc_p;
1268 {
1269 int success_p;
1270 Display *dpy = FRAME_X_DISPLAY (f);
1271 Colormap cmap = FRAME_X_COLORMAP (f);
1272
1273 BLOCK_INPUT;
1274 success_p = XParseColor (dpy, cmap, color_name, color);
1275 if (success_p && alloc_p)
1276 success_p = x_alloc_nearest_color (f, cmap, color);
1277 UNBLOCK_INPUT;
1278
1279 return success_p;
1280 }
1281
1282
1283 /* Return the pixel color value for color COLOR_NAME on frame F. If F
1284 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
1285 Signal an error if color can't be allocated. */
1286
1287 int
1288 x_decode_color (f, color_name, mono_color)
1289 FRAME_PTR f;
1290 Lisp_Object color_name;
1291 int mono_color;
1292 {
1293 XColor cdef;
1294
1295 CHECK_STRING (color_name, 0);
1296
1297 #if 0 /* Don't do this. It's wrong when we're not using the default
1298 colormap, it makes freeing difficult, and it's probably not
1299 an important optimization. */
1300 if (strcmp (XSTRING (color_name)->data, "black") == 0)
1301 return BLACK_PIX_DEFAULT (f);
1302 else if (strcmp (XSTRING (color_name)->data, "white") == 0)
1303 return WHITE_PIX_DEFAULT (f);
1304 #endif
1305
1306 /* Return MONO_COLOR for monochrome frames. */
1307 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
1308 return mono_color;
1309
1310 /* x_defined_color is responsible for coping with failures
1311 by looking for a near-miss. */
1312 if (x_defined_color (f, XSTRING (color_name)->data, &cdef, 1))
1313 return cdef.pixel;
1314
1315 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
1316 Fcons (color_name, Qnil)));
1317 return 0;
1318 }
1319
1320
1321 \f
1322 /* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1323 the previous value of that parameter, NEW_VALUE is the new value. */
1324
1325 static void
1326 x_set_line_spacing (f, new_value, old_value)
1327 struct frame *f;
1328 Lisp_Object new_value, old_value;
1329 {
1330 if (NILP (new_value))
1331 f->extra_line_spacing = 0;
1332 else if (NATNUMP (new_value))
1333 f->extra_line_spacing = XFASTINT (new_value);
1334 else
1335 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
1336 Fcons (new_value, Qnil)));
1337 if (FRAME_VISIBLE_P (f))
1338 redraw_frame (f);
1339 }
1340
1341
1342 /* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
1343 the previous value of that parameter, NEW_VALUE is the new value. */
1344
1345 static void
1346 x_set_screen_gamma (f, new_value, old_value)
1347 struct frame *f;
1348 Lisp_Object new_value, old_value;
1349 {
1350 if (NILP (new_value))
1351 f->gamma = 0;
1352 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1353 /* The value 0.4545 is the normal viewing gamma. */
1354 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1355 else
1356 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
1357 Fcons (new_value, Qnil)));
1358
1359 clear_face_cache (0);
1360 }
1361
1362
1363 /* Functions called only from `x_set_frame_param'
1364 to set individual parameters.
1365
1366 If FRAME_X_WINDOW (f) is 0,
1367 the frame is being created and its X-window does not exist yet.
1368 In that case, just record the parameter's new value
1369 in the standard place; do not attempt to change the window. */
1370
1371 void
1372 x_set_foreground_color (f, arg, oldval)
1373 struct frame *f;
1374 Lisp_Object arg, oldval;
1375 {
1376 unsigned long pixel
1377 = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1378
1379 unload_color (f, f->output_data.x->foreground_pixel);
1380 f->output_data.x->foreground_pixel = pixel;
1381
1382 if (FRAME_X_WINDOW (f) != 0)
1383 {
1384 BLOCK_INPUT;
1385 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1386 f->output_data.x->foreground_pixel);
1387 XSetBackground (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
1388 f->output_data.x->foreground_pixel);
1389 UNBLOCK_INPUT;
1390 update_face_from_frame_parameter (f, Qforeground_color, arg);
1391 if (FRAME_VISIBLE_P (f))
1392 redraw_frame (f);
1393 }
1394 }
1395
1396 void
1397 x_set_background_color (f, arg, oldval)
1398 struct frame *f;
1399 Lisp_Object arg, oldval;
1400 {
1401 unsigned long pixel
1402 = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1403
1404 unload_color (f, f->output_data.x->background_pixel);
1405 f->output_data.x->background_pixel = pixel;
1406
1407 if (FRAME_X_WINDOW (f) != 0)
1408 {
1409 BLOCK_INPUT;
1410 /* The main frame area. */
1411 XSetBackground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
1412 f->output_data.x->background_pixel);
1413 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
1414 f->output_data.x->background_pixel);
1415 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
1416 f->output_data.x->background_pixel);
1417 XSetWindowBackground (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1418 f->output_data.x->background_pixel);
1419 {
1420 Lisp_Object bar;
1421 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar);
1422 bar = XSCROLL_BAR (bar)->next)
1423 XSetWindowBackground (FRAME_X_DISPLAY (f),
1424 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
1425 f->output_data.x->background_pixel);
1426 }
1427 UNBLOCK_INPUT;
1428
1429 update_face_from_frame_parameter (f, Qbackground_color, arg);
1430
1431 if (FRAME_VISIBLE_P (f))
1432 redraw_frame (f);
1433 }
1434 }
1435
1436 void
1437 x_set_mouse_color (f, arg, oldval)
1438 struct frame *f;
1439 Lisp_Object arg, oldval;
1440 {
1441 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
1442 Cursor busy_cursor, horizontal_drag_cursor;
1443 int count;
1444 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1445 unsigned long mask_color = f->output_data.x->background_pixel;
1446
1447 /* Don't let pointers be invisible. */
1448 if (mask_color == pixel
1449 && mask_color == f->output_data.x->background_pixel)
1450 pixel = f->output_data.x->foreground_pixel;
1451
1452 unload_color (f, f->output_data.x->mouse_pixel);
1453 f->output_data.x->mouse_pixel = pixel;
1454
1455 BLOCK_INPUT;
1456
1457 /* It's not okay to crash if the user selects a screwy cursor. */
1458 count = x_catch_errors (FRAME_X_DISPLAY (f));
1459
1460 if (!EQ (Qnil, Vx_pointer_shape))
1461 {
1462 CHECK_NUMBER (Vx_pointer_shape, 0);
1463 cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XINT (Vx_pointer_shape));
1464 }
1465 else
1466 cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_xterm);
1467 x_check_errors (FRAME_X_DISPLAY (f), "bad text pointer cursor: %s");
1468
1469 if (!EQ (Qnil, Vx_nontext_pointer_shape))
1470 {
1471 CHECK_NUMBER (Vx_nontext_pointer_shape, 0);
1472 nontext_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f),
1473 XINT (Vx_nontext_pointer_shape));
1474 }
1475 else
1476 nontext_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_left_ptr);
1477 x_check_errors (FRAME_X_DISPLAY (f), "bad nontext pointer cursor: %s");
1478
1479 if (!EQ (Qnil, Vx_busy_pointer_shape))
1480 {
1481 CHECK_NUMBER (Vx_busy_pointer_shape, 0);
1482 busy_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f),
1483 XINT (Vx_busy_pointer_shape));
1484 }
1485 else
1486 busy_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_watch);
1487 x_check_errors (FRAME_X_DISPLAY (f), "bad busy pointer cursor: %s");
1488
1489 x_check_errors (FRAME_X_DISPLAY (f), "bad nontext pointer cursor: %s");
1490 if (!EQ (Qnil, Vx_mode_pointer_shape))
1491 {
1492 CHECK_NUMBER (Vx_mode_pointer_shape, 0);
1493 mode_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f),
1494 XINT (Vx_mode_pointer_shape));
1495 }
1496 else
1497 mode_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_xterm);
1498 x_check_errors (FRAME_X_DISPLAY (f), "bad modeline pointer cursor: %s");
1499
1500 if (!EQ (Qnil, Vx_sensitive_text_pointer_shape))
1501 {
1502 CHECK_NUMBER (Vx_sensitive_text_pointer_shape, 0);
1503 cross_cursor
1504 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1505 XINT (Vx_sensitive_text_pointer_shape));
1506 }
1507 else
1508 cross_cursor = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_crosshair);
1509
1510 if (!NILP (Vx_window_horizontal_drag_shape))
1511 {
1512 CHECK_NUMBER (Vx_window_horizontal_drag_shape, 0);
1513 horizontal_drag_cursor
1514 = XCreateFontCursor (FRAME_X_DISPLAY (f),
1515 XINT (Vx_window_horizontal_drag_shape));
1516 }
1517 else
1518 horizontal_drag_cursor
1519 = XCreateFontCursor (FRAME_X_DISPLAY (f), XC_sb_h_double_arrow);
1520
1521 /* Check and report errors with the above calls. */
1522 x_check_errors (FRAME_X_DISPLAY (f), "can't set cursor shape: %s");
1523 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1524
1525 {
1526 XColor fore_color, back_color;
1527
1528 fore_color.pixel = f->output_data.x->mouse_pixel;
1529 x_query_color (f, &fore_color);
1530 back_color.pixel = mask_color;
1531 x_query_color (f, &back_color);
1532
1533 XRecolorCursor (FRAME_X_DISPLAY (f), cursor,
1534 &fore_color, &back_color);
1535 XRecolorCursor (FRAME_X_DISPLAY (f), nontext_cursor,
1536 &fore_color, &back_color);
1537 XRecolorCursor (FRAME_X_DISPLAY (f), mode_cursor,
1538 &fore_color, &back_color);
1539 XRecolorCursor (FRAME_X_DISPLAY (f), cross_cursor,
1540 &fore_color, &back_color);
1541 XRecolorCursor (FRAME_X_DISPLAY (f), busy_cursor,
1542 &fore_color, &back_color);
1543 XRecolorCursor (FRAME_X_DISPLAY (f), horizontal_drag_cursor,
1544 &fore_color, &back_color);
1545 }
1546
1547 if (FRAME_X_WINDOW (f) != 0)
1548 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
1549
1550 if (cursor != f->output_data.x->text_cursor
1551 && f->output_data.x->text_cursor != 0)
1552 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->text_cursor);
1553 f->output_data.x->text_cursor = cursor;
1554
1555 if (nontext_cursor != f->output_data.x->nontext_cursor
1556 && f->output_data.x->nontext_cursor != 0)
1557 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->nontext_cursor);
1558 f->output_data.x->nontext_cursor = nontext_cursor;
1559
1560 if (busy_cursor != f->output_data.x->busy_cursor
1561 && f->output_data.x->busy_cursor != 0)
1562 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->busy_cursor);
1563 f->output_data.x->busy_cursor = busy_cursor;
1564
1565 if (mode_cursor != f->output_data.x->modeline_cursor
1566 && f->output_data.x->modeline_cursor != 0)
1567 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->modeline_cursor);
1568 f->output_data.x->modeline_cursor = mode_cursor;
1569
1570 if (cross_cursor != f->output_data.x->cross_cursor
1571 && f->output_data.x->cross_cursor != 0)
1572 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->cross_cursor);
1573 f->output_data.x->cross_cursor = cross_cursor;
1574
1575 if (horizontal_drag_cursor != f->output_data.x->horizontal_drag_cursor
1576 && f->output_data.x->horizontal_drag_cursor != 0)
1577 XFreeCursor (FRAME_X_DISPLAY (f), f->output_data.x->horizontal_drag_cursor);
1578 f->output_data.x->horizontal_drag_cursor = horizontal_drag_cursor;
1579
1580 XFlush (FRAME_X_DISPLAY (f));
1581 UNBLOCK_INPUT;
1582
1583 update_face_from_frame_parameter (f, Qmouse_color, arg);
1584 }
1585
1586 void
1587 x_set_cursor_color (f, arg, oldval)
1588 struct frame *f;
1589 Lisp_Object arg, oldval;
1590 {
1591 unsigned long fore_pixel, pixel;
1592 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1593
1594 if (!NILP (Vx_cursor_fore_pixel))
1595 {
1596 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1597 WHITE_PIX_DEFAULT (f));
1598 fore_pixel_allocated_p = 1;
1599 }
1600 else
1601 fore_pixel = f->output_data.x->background_pixel;
1602
1603 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1604 pixel_allocated_p = 1;
1605
1606 /* Make sure that the cursor color differs from the background color. */
1607 if (pixel == f->output_data.x->background_pixel)
1608 {
1609 if (pixel_allocated_p)
1610 {
1611 x_free_colors (f, &pixel, 1);
1612 pixel_allocated_p = 0;
1613 }
1614
1615 pixel = f->output_data.x->mouse_pixel;
1616 if (pixel == fore_pixel)
1617 {
1618 if (fore_pixel_allocated_p)
1619 {
1620 x_free_colors (f, &fore_pixel, 1);
1621 fore_pixel_allocated_p = 0;
1622 }
1623 fore_pixel = f->output_data.x->background_pixel;
1624 }
1625 }
1626
1627 unload_color (f, f->output_data.x->cursor_foreground_pixel);
1628 if (!fore_pixel_allocated_p)
1629 fore_pixel = x_copy_color (f, fore_pixel);
1630 f->output_data.x->cursor_foreground_pixel = fore_pixel;
1631
1632 unload_color (f, f->output_data.x->cursor_pixel);
1633 if (!pixel_allocated_p)
1634 pixel = x_copy_color (f, pixel);
1635 f->output_data.x->cursor_pixel = pixel;
1636
1637 if (FRAME_X_WINDOW (f) != 0)
1638 {
1639 BLOCK_INPUT;
1640 XSetBackground (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
1641 f->output_data.x->cursor_pixel);
1642 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
1643 fore_pixel);
1644 UNBLOCK_INPUT;
1645
1646 if (FRAME_VISIBLE_P (f))
1647 {
1648 x_update_cursor (f, 0);
1649 x_update_cursor (f, 1);
1650 }
1651 }
1652
1653 update_face_from_frame_parameter (f, Qcursor_color, arg);
1654 }
1655 \f
1656 /* Set the border-color of frame F to value described by ARG.
1657 ARG can be a string naming a color.
1658 The border-color is used for the border that is drawn by the X server.
1659 Note that this does not fully take effect if done before
1660 F has an x-window; it must be redone when the window is created.
1661
1662 Note: this is done in two routines because of the way X10 works.
1663
1664 Note: under X11, this is normally the province of the window manager,
1665 and so emacs' border colors may be overridden. */
1666
1667 void
1668 x_set_border_color (f, arg, oldval)
1669 struct frame *f;
1670 Lisp_Object arg, oldval;
1671 {
1672 int pix;
1673
1674 CHECK_STRING (arg, 0);
1675 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1676 x_set_border_pixel (f, pix);
1677 update_face_from_frame_parameter (f, Qborder_color, arg);
1678 }
1679
1680 /* Set the border-color of frame F to pixel value PIX.
1681 Note that this does not fully take effect if done before
1682 F has an x-window. */
1683
1684 void
1685 x_set_border_pixel (f, pix)
1686 struct frame *f;
1687 int pix;
1688 {
1689 unload_color (f, f->output_data.x->border_pixel);
1690 f->output_data.x->border_pixel = pix;
1691
1692 if (FRAME_X_WINDOW (f) != 0 && f->output_data.x->border_width > 0)
1693 {
1694 BLOCK_INPUT;
1695 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1696 (unsigned long)pix);
1697 UNBLOCK_INPUT;
1698
1699 if (FRAME_VISIBLE_P (f))
1700 redraw_frame (f);
1701 }
1702 }
1703
1704
1705 /* Value is the internal representation of the specified cursor type
1706 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
1707 of the bar cursor. */
1708
1709 enum text_cursor_kinds
1710 x_specified_cursor_type (arg, width)
1711 Lisp_Object arg;
1712 int *width;
1713 {
1714 enum text_cursor_kinds type;
1715
1716 if (EQ (arg, Qbar))
1717 {
1718 type = BAR_CURSOR;
1719 *width = 2;
1720 }
1721 else if (CONSP (arg)
1722 && EQ (XCAR (arg), Qbar)
1723 && INTEGERP (XCDR (arg))
1724 && XINT (XCDR (arg)) >= 0)
1725 {
1726 type = BAR_CURSOR;
1727 *width = XINT (XCDR (arg));
1728 }
1729 else if (NILP (arg))
1730 type = NO_CURSOR;
1731 else
1732 /* Treat anything unknown as "box cursor".
1733 It was bad to signal an error; people have trouble fixing
1734 .Xdefaults with Emacs, when it has something bad in it. */
1735 type = FILLED_BOX_CURSOR;
1736
1737 return type;
1738 }
1739
1740 void
1741 x_set_cursor_type (f, arg, oldval)
1742 FRAME_PTR f;
1743 Lisp_Object arg, oldval;
1744 {
1745 int width;
1746
1747 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
1748 f->output_data.x->cursor_width = width;
1749
1750 /* Make sure the cursor gets redrawn. This is overkill, but how
1751 often do people change cursor types? */
1752 update_mode_lines++;
1753 }
1754 \f
1755 void
1756 x_set_icon_type (f, arg, oldval)
1757 struct frame *f;
1758 Lisp_Object arg, oldval;
1759 {
1760 int result;
1761
1762 if (STRINGP (arg))
1763 {
1764 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1765 return;
1766 }
1767 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1768 return;
1769
1770 BLOCK_INPUT;
1771 if (NILP (arg))
1772 result = x_text_icon (f,
1773 (char *) XSTRING ((!NILP (f->icon_name)
1774 ? f->icon_name
1775 : f->name))->data);
1776 else
1777 result = x_bitmap_icon (f, arg);
1778
1779 if (result)
1780 {
1781 UNBLOCK_INPUT;
1782 error ("No icon window available");
1783 }
1784
1785 XFlush (FRAME_X_DISPLAY (f));
1786 UNBLOCK_INPUT;
1787 }
1788
1789 /* Return non-nil if frame F wants a bitmap icon. */
1790
1791 Lisp_Object
1792 x_icon_type (f)
1793 FRAME_PTR f;
1794 {
1795 Lisp_Object tem;
1796
1797 tem = assq_no_quit (Qicon_type, f->param_alist);
1798 if (CONSP (tem))
1799 return XCDR (tem);
1800 else
1801 return Qnil;
1802 }
1803
1804 void
1805 x_set_icon_name (f, arg, oldval)
1806 struct frame *f;
1807 Lisp_Object arg, oldval;
1808 {
1809 int result;
1810
1811 if (STRINGP (arg))
1812 {
1813 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1814 return;
1815 }
1816 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1817 return;
1818
1819 f->icon_name = arg;
1820
1821 if (f->output_data.x->icon_bitmap != 0)
1822 return;
1823
1824 BLOCK_INPUT;
1825
1826 result = x_text_icon (f,
1827 (char *) XSTRING ((!NILP (f->icon_name)
1828 ? f->icon_name
1829 : !NILP (f->title)
1830 ? f->title
1831 : f->name))->data);
1832
1833 if (result)
1834 {
1835 UNBLOCK_INPUT;
1836 error ("No icon window available");
1837 }
1838
1839 XFlush (FRAME_X_DISPLAY (f));
1840 UNBLOCK_INPUT;
1841 }
1842 \f
1843 void
1844 x_set_font (f, arg, oldval)
1845 struct frame *f;
1846 Lisp_Object arg, oldval;
1847 {
1848 Lisp_Object result;
1849 Lisp_Object fontset_name;
1850 Lisp_Object frame;
1851
1852 CHECK_STRING (arg, 1);
1853
1854 fontset_name = Fquery_fontset (arg, Qnil);
1855
1856 BLOCK_INPUT;
1857 result = (STRINGP (fontset_name)
1858 ? x_new_fontset (f, XSTRING (fontset_name)->data)
1859 : x_new_font (f, XSTRING (arg)->data));
1860 UNBLOCK_INPUT;
1861
1862 if (EQ (result, Qnil))
1863 error ("Font `%s' is not defined", XSTRING (arg)->data);
1864 else if (EQ (result, Qt))
1865 error ("The characters of the given font have varying widths");
1866 else if (STRINGP (result))
1867 {
1868 store_frame_param (f, Qfont, result);
1869 recompute_basic_faces (f);
1870 }
1871 else
1872 abort ();
1873
1874 do_pending_window_change (0);
1875
1876 /* Don't call `face-set-after-frame-default' when faces haven't been
1877 initialized yet. This is the case when called from
1878 Fx_create_frame. In that case, the X widget or window doesn't
1879 exist either, and we can end up in x_report_frame_params with a
1880 null widget which gives a segfault. */
1881 if (FRAME_FACE_CACHE (f))
1882 {
1883 XSETFRAME (frame, f);
1884 call1 (Qface_set_after_frame_default, frame);
1885 }
1886 }
1887
1888 void
1889 x_set_border_width (f, arg, oldval)
1890 struct frame *f;
1891 Lisp_Object arg, oldval;
1892 {
1893 CHECK_NUMBER (arg, 0);
1894
1895 if (XINT (arg) == f->output_data.x->border_width)
1896 return;
1897
1898 if (FRAME_X_WINDOW (f) != 0)
1899 error ("Cannot change the border width of a window");
1900
1901 f->output_data.x->border_width = XINT (arg);
1902 }
1903
1904 void
1905 x_set_internal_border_width (f, arg, oldval)
1906 struct frame *f;
1907 Lisp_Object arg, oldval;
1908 {
1909 int old = f->output_data.x->internal_border_width;
1910
1911 CHECK_NUMBER (arg, 0);
1912 f->output_data.x->internal_border_width = XINT (arg);
1913 if (f->output_data.x->internal_border_width < 0)
1914 f->output_data.x->internal_border_width = 0;
1915
1916 #ifdef USE_X_TOOLKIT
1917 if (f->output_data.x->edit_widget)
1918 widget_store_internal_border (f->output_data.x->edit_widget);
1919 #endif
1920
1921 if (f->output_data.x->internal_border_width == old)
1922 return;
1923
1924 if (FRAME_X_WINDOW (f) != 0)
1925 {
1926 x_set_window_size (f, 0, f->width, f->height);
1927 SET_FRAME_GARBAGED (f);
1928 do_pending_window_change (0);
1929 }
1930 }
1931
1932 void
1933 x_set_visibility (f, value, oldval)
1934 struct frame *f;
1935 Lisp_Object value, oldval;
1936 {
1937 Lisp_Object frame;
1938 XSETFRAME (frame, f);
1939
1940 if (NILP (value))
1941 Fmake_frame_invisible (frame, Qt);
1942 else if (EQ (value, Qicon))
1943 Ficonify_frame (frame);
1944 else
1945 Fmake_frame_visible (frame);
1946 }
1947
1948 \f
1949 /* Change window heights in windows rooted in WINDOW by N lines. */
1950
1951 static void
1952 x_change_window_heights (window, n)
1953 Lisp_Object window;
1954 int n;
1955 {
1956 struct window *w = XWINDOW (window);
1957
1958 XSETFASTINT (w->top, XFASTINT (w->top) + n);
1959 XSETFASTINT (w->height, XFASTINT (w->height) - n);
1960
1961 if (INTEGERP (w->orig_top))
1962 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
1963 if (INTEGERP (w->orig_height))
1964 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
1965
1966 /* Handle just the top child in a vertical split. */
1967 if (!NILP (w->vchild))
1968 x_change_window_heights (w->vchild, n);
1969
1970 /* Adjust all children in a horizontal split. */
1971 for (window = w->hchild; !NILP (window); window = w->next)
1972 {
1973 w = XWINDOW (window);
1974 x_change_window_heights (window, n);
1975 }
1976 }
1977
1978 void
1979 x_set_menu_bar_lines (f, value, oldval)
1980 struct frame *f;
1981 Lisp_Object value, oldval;
1982 {
1983 int nlines;
1984 #ifndef USE_X_TOOLKIT
1985 int olines = FRAME_MENU_BAR_LINES (f);
1986 #endif
1987
1988 /* Right now, menu bars don't work properly in minibuf-only frames;
1989 most of the commands try to apply themselves to the minibuffer
1990 frame itself, and get an error because you can't switch buffers
1991 in or split the minibuffer window. */
1992 if (FRAME_MINIBUF_ONLY_P (f))
1993 return;
1994
1995 if (INTEGERP (value))
1996 nlines = XINT (value);
1997 else
1998 nlines = 0;
1999
2000 /* Make sure we redisplay all windows in this frame. */
2001 windows_or_buffers_changed++;
2002
2003 #ifdef USE_X_TOOLKIT
2004 FRAME_MENU_BAR_LINES (f) = 0;
2005 if (nlines)
2006 {
2007 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2008 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
2009 /* Make sure next redisplay shows the menu bar. */
2010 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
2011 }
2012 else
2013 {
2014 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2015 free_frame_menubar (f);
2016 FRAME_EXTERNAL_MENU_BAR (f) = 0;
2017 if (FRAME_X_P (f))
2018 f->output_data.x->menubar_widget = 0;
2019 }
2020 #else /* not USE_X_TOOLKIT */
2021 FRAME_MENU_BAR_LINES (f) = nlines;
2022 x_change_window_heights (f->root_window, nlines - olines);
2023 #endif /* not USE_X_TOOLKIT */
2024 adjust_glyphs (f);
2025 }
2026
2027
2028 /* Set the number of lines used for the tool bar of frame F to VALUE.
2029 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2030 is the old number of tool bar lines. This function changes the
2031 height of all windows on frame F to match the new tool bar height.
2032 The frame's height doesn't change. */
2033
2034 void
2035 x_set_tool_bar_lines (f, value, oldval)
2036 struct frame *f;
2037 Lisp_Object value, oldval;
2038 {
2039 int delta, nlines, root_height;
2040 Lisp_Object root_window;
2041
2042 /* Use VALUE only if an integer >= 0. */
2043 if (INTEGERP (value) && XINT (value) >= 0)
2044 nlines = XFASTINT (value);
2045 else
2046 nlines = 0;
2047
2048 /* Make sure we redisplay all windows in this frame. */
2049 ++windows_or_buffers_changed;
2050
2051 delta = nlines - FRAME_TOOL_BAR_LINES (f);
2052
2053 /* Don't resize the tool-bar to more than we have room for. */
2054 root_window = FRAME_ROOT_WINDOW (f);
2055 root_height = XINT (XWINDOW (root_window)->height);
2056 if (root_height - delta < 1)
2057 {
2058 delta = root_height - 1;
2059 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2060 }
2061
2062 FRAME_TOOL_BAR_LINES (f) = nlines;
2063 x_change_window_heights (root_window, delta);
2064 adjust_glyphs (f);
2065
2066 /* We also have to make sure that the internal border at the top of
2067 the frame, below the menu bar or tool bar, is redrawn when the
2068 tool bar disappears. This is so because the internal border is
2069 below the tool bar if one is displayed, but is below the menu bar
2070 if there isn't a tool bar. The tool bar draws into the area
2071 below the menu bar. */
2072 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2073 {
2074 updating_frame = f;
2075 clear_frame ();
2076 clear_current_matrices (f);
2077 updating_frame = NULL;
2078 }
2079
2080 /* If the tool bar gets smaller, the internal border below it
2081 has to be cleared. It was formerly part of the display
2082 of the larger tool bar, and updating windows won't clear it. */
2083 if (delta < 0)
2084 {
2085 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2086 int width = PIXEL_WIDTH (f);
2087 int y = nlines * CANON_Y_UNIT (f);
2088
2089 BLOCK_INPUT;
2090 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2091 0, y, width, height, False);
2092 UNBLOCK_INPUT;
2093 }
2094 }
2095
2096
2097 /* Set the foreground color for scroll bars on frame F to VALUE.
2098 VALUE should be a string, a color name. If it isn't a string or
2099 isn't a valid color name, do nothing. OLDVAL is the old value of
2100 the frame parameter. */
2101
2102 void
2103 x_set_scroll_bar_foreground (f, value, oldval)
2104 struct frame *f;
2105 Lisp_Object value, oldval;
2106 {
2107 unsigned long pixel;
2108
2109 if (STRINGP (value))
2110 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
2111 else
2112 pixel = -1;
2113
2114 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
2115 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
2116
2117 f->output_data.x->scroll_bar_foreground_pixel = pixel;
2118 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2119 {
2120 /* Remove all scroll bars because they have wrong colors. */
2121 if (condemn_scroll_bars_hook)
2122 (*condemn_scroll_bars_hook) (f);
2123 if (judge_scroll_bars_hook)
2124 (*judge_scroll_bars_hook) (f);
2125
2126 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
2127 redraw_frame (f);
2128 }
2129 }
2130
2131
2132 /* Set the background color for scroll bars on frame F to VALUE VALUE
2133 should be a string, a color name. If it isn't a string or isn't a
2134 valid color name, do nothing. OLDVAL is the old value of the frame
2135 parameter. */
2136
2137 void
2138 x_set_scroll_bar_background (f, value, oldval)
2139 struct frame *f;
2140 Lisp_Object value, oldval;
2141 {
2142 unsigned long pixel;
2143
2144 if (STRINGP (value))
2145 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
2146 else
2147 pixel = -1;
2148
2149 if (f->output_data.x->scroll_bar_background_pixel != -1)
2150 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
2151
2152 f->output_data.x->scroll_bar_background_pixel = pixel;
2153 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2154 {
2155 /* Remove all scroll bars because they have wrong colors. */
2156 if (condemn_scroll_bars_hook)
2157 (*condemn_scroll_bars_hook) (f);
2158 if (judge_scroll_bars_hook)
2159 (*judge_scroll_bars_hook) (f);
2160
2161 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
2162 redraw_frame (f);
2163 }
2164 }
2165
2166 \f
2167 /* Encode Lisp string STRING as a text in a format appropriate for
2168 XICCC (X Inter Client Communication Conventions).
2169
2170 If STRING contains only ASCII characters, do no conversion and
2171 return the string data of STRING. Otherwise, encode the text by
2172 CODING_SYSTEM, and return a newly allocated memory area which
2173 should be freed by `xfree' by a caller.
2174
2175 Store the byte length of resulting text in *TEXT_BYTES.
2176
2177 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
2178 which means that the `encoding' of the result can be `STRING'.
2179 Otherwise store 0 in *STRINGP, which means that the `encoding' of
2180 the result should be `COMPOUND_TEXT'. */
2181
2182 unsigned char *
2183 x_encode_text (string, coding_system, text_bytes, stringp)
2184 Lisp_Object string, coding_system;
2185 int *text_bytes, *stringp;
2186 {
2187 unsigned char *str = XSTRING (string)->data;
2188 int chars = XSTRING (string)->size;
2189 int bytes = STRING_BYTES (XSTRING (string));
2190 int charset_info;
2191 int bufsize;
2192 unsigned char *buf;
2193 struct coding_system coding;
2194
2195 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
2196 if (charset_info == 0)
2197 {
2198 /* No multibyte character in OBJ. We need not encode it. */
2199 *text_bytes = bytes;
2200 *stringp = 1;
2201 return str;
2202 }
2203
2204 setup_coding_system (coding_system, &coding);
2205 coding.src_multibyte = 1;
2206 coding.dst_multibyte = 0;
2207 coding.mode |= CODING_MODE_LAST_BLOCK;
2208 if (coding.type == coding_type_iso2022)
2209 coding.flags |= CODING_FLAG_ISO_SAFE;
2210 /* We suppress producing escape sequences for composition. */
2211 coding.composing = COMPOSITION_DISABLED;
2212 bufsize = encoding_buffer_size (&coding, bytes);
2213 buf = (unsigned char *) xmalloc (bufsize);
2214 encode_coding (&coding, str, buf, bytes, bufsize);
2215 *text_bytes = coding.produced;
2216 *stringp = (charset_info == 1 || !EQ (coding_system, Qcompound_text));
2217 return buf;
2218 }
2219
2220 \f
2221 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
2222 x_id_name.
2223
2224 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2225 name; if NAME is a string, set F's name to NAME and set
2226 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2227
2228 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2229 suggesting a new name, which lisp code should override; if
2230 F->explicit_name is set, ignore the new name; otherwise, set it. */
2231
2232 void
2233 x_set_name (f, name, explicit)
2234 struct frame *f;
2235 Lisp_Object name;
2236 int explicit;
2237 {
2238 /* Make sure that requests from lisp code override requests from
2239 Emacs redisplay code. */
2240 if (explicit)
2241 {
2242 /* If we're switching from explicit to implicit, we had better
2243 update the mode lines and thereby update the title. */
2244 if (f->explicit_name && NILP (name))
2245 update_mode_lines = 1;
2246
2247 f->explicit_name = ! NILP (name);
2248 }
2249 else if (f->explicit_name)
2250 return;
2251
2252 /* If NAME is nil, set the name to the x_id_name. */
2253 if (NILP (name))
2254 {
2255 /* Check for no change needed in this very common case
2256 before we do any consing. */
2257 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
2258 XSTRING (f->name)->data))
2259 return;
2260 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
2261 }
2262 else
2263 CHECK_STRING (name, 0);
2264
2265 /* Don't change the name if it's already NAME. */
2266 if (! NILP (Fstring_equal (name, f->name)))
2267 return;
2268
2269 f->name = name;
2270
2271 /* For setting the frame title, the title parameter should override
2272 the name parameter. */
2273 if (! NILP (f->title))
2274 name = f->title;
2275
2276 if (FRAME_X_WINDOW (f))
2277 {
2278 BLOCK_INPUT;
2279 #ifdef HAVE_X11R4
2280 {
2281 XTextProperty text, icon;
2282 int bytes, stringp;
2283 Lisp_Object coding_system;
2284
2285 coding_system = Vlocale_coding_system;
2286 if (NILP (coding_system))
2287 coding_system = Qcompound_text;
2288 text.value = x_encode_text (name, coding_system, &bytes, &stringp);
2289 text.encoding = (stringp ? XA_STRING
2290 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2291 text.format = 8;
2292 text.nitems = bytes;
2293
2294 if (NILP (f->icon_name))
2295 {
2296 icon = text;
2297 }
2298 else
2299 {
2300 icon.value = x_encode_text (f->icon_name, coding_system,
2301 &bytes, &stringp);
2302 icon.encoding = (stringp ? XA_STRING
2303 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2304 icon.format = 8;
2305 icon.nitems = bytes;
2306 }
2307 #ifdef USE_X_TOOLKIT
2308 XSetWMName (FRAME_X_DISPLAY (f),
2309 XtWindow (f->output_data.x->widget), &text);
2310 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2311 &icon);
2312 #else /* not USE_X_TOOLKIT */
2313 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2314 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2315 #endif /* not USE_X_TOOLKIT */
2316 if (!NILP (f->icon_name)
2317 && icon.value != XSTRING (f->icon_name)->data)
2318 xfree (icon.value);
2319 if (text.value != XSTRING (name)->data)
2320 xfree (text.value);
2321 }
2322 #else /* not HAVE_X11R4 */
2323 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2324 XSTRING (name)->data);
2325 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2326 XSTRING (name)->data);
2327 #endif /* not HAVE_X11R4 */
2328 UNBLOCK_INPUT;
2329 }
2330 }
2331
2332 /* This function should be called when the user's lisp code has
2333 specified a name for the frame; the name will override any set by the
2334 redisplay code. */
2335 void
2336 x_explicitly_set_name (f, arg, oldval)
2337 FRAME_PTR f;
2338 Lisp_Object arg, oldval;
2339 {
2340 x_set_name (f, arg, 1);
2341 }
2342
2343 /* This function should be called by Emacs redisplay code to set the
2344 name; names set this way will never override names set by the user's
2345 lisp code. */
2346 void
2347 x_implicitly_set_name (f, arg, oldval)
2348 FRAME_PTR f;
2349 Lisp_Object arg, oldval;
2350 {
2351 x_set_name (f, arg, 0);
2352 }
2353 \f
2354 /* Change the title of frame F to NAME.
2355 If NAME is nil, use the frame name as the title.
2356
2357 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2358 name; if NAME is a string, set F's name to NAME and set
2359 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2360
2361 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2362 suggesting a new name, which lisp code should override; if
2363 F->explicit_name is set, ignore the new name; otherwise, set it. */
2364
2365 void
2366 x_set_title (f, name, old_name)
2367 struct frame *f;
2368 Lisp_Object name, old_name;
2369 {
2370 /* Don't change the title if it's already NAME. */
2371 if (EQ (name, f->title))
2372 return;
2373
2374 update_mode_lines = 1;
2375
2376 f->title = name;
2377
2378 if (NILP (name))
2379 name = f->name;
2380 else
2381 CHECK_STRING (name, 0);
2382
2383 if (FRAME_X_WINDOW (f))
2384 {
2385 BLOCK_INPUT;
2386 #ifdef HAVE_X11R4
2387 {
2388 XTextProperty text, icon;
2389 int bytes, stringp;
2390 Lisp_Object coding_system;
2391
2392 coding_system = Vlocale_coding_system;
2393 if (NILP (coding_system))
2394 coding_system = Qcompound_text;
2395 text.value = x_encode_text (name, coding_system, &bytes, &stringp);
2396 text.encoding = (stringp ? XA_STRING
2397 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2398 text.format = 8;
2399 text.nitems = bytes;
2400
2401 if (NILP (f->icon_name))
2402 {
2403 icon = text;
2404 }
2405 else
2406 {
2407 icon.value = x_encode_text (f->icon_name, coding_system,
2408 &bytes, &stringp);
2409 icon.encoding = (stringp ? XA_STRING
2410 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2411 icon.format = 8;
2412 icon.nitems = bytes;
2413 }
2414 #ifdef USE_X_TOOLKIT
2415 XSetWMName (FRAME_X_DISPLAY (f),
2416 XtWindow (f->output_data.x->widget), &text);
2417 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2418 &icon);
2419 #else /* not USE_X_TOOLKIT */
2420 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2421 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2422 #endif /* not USE_X_TOOLKIT */
2423 if (!NILP (f->icon_name)
2424 && icon.value != XSTRING (f->icon_name)->data)
2425 xfree (icon.value);
2426 if (text.value != XSTRING (name)->data)
2427 xfree (text.value);
2428 }
2429 #else /* not HAVE_X11R4 */
2430 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2431 XSTRING (name)->data);
2432 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2433 XSTRING (name)->data);
2434 #endif /* not HAVE_X11R4 */
2435 UNBLOCK_INPUT;
2436 }
2437 }
2438 \f
2439 void
2440 x_set_autoraise (f, arg, oldval)
2441 struct frame *f;
2442 Lisp_Object arg, oldval;
2443 {
2444 f->auto_raise = !EQ (Qnil, arg);
2445 }
2446
2447 void
2448 x_set_autolower (f, arg, oldval)
2449 struct frame *f;
2450 Lisp_Object arg, oldval;
2451 {
2452 f->auto_lower = !EQ (Qnil, arg);
2453 }
2454
2455 void
2456 x_set_unsplittable (f, arg, oldval)
2457 struct frame *f;
2458 Lisp_Object arg, oldval;
2459 {
2460 f->no_split = !NILP (arg);
2461 }
2462
2463 void
2464 x_set_vertical_scroll_bars (f, arg, oldval)
2465 struct frame *f;
2466 Lisp_Object arg, oldval;
2467 {
2468 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2469 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2470 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2471 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
2472 {
2473 FRAME_VERTICAL_SCROLL_BAR_TYPE (f)
2474 = (NILP (arg)
2475 ? vertical_scroll_bar_none
2476 : EQ (Qright, arg)
2477 ? vertical_scroll_bar_right
2478 : vertical_scroll_bar_left);
2479
2480 /* We set this parameter before creating the X window for the
2481 frame, so we can get the geometry right from the start.
2482 However, if the window hasn't been created yet, we shouldn't
2483 call x_set_window_size. */
2484 if (FRAME_X_WINDOW (f))
2485 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2486 do_pending_window_change (0);
2487 }
2488 }
2489
2490 void
2491 x_set_scroll_bar_width (f, arg, oldval)
2492 struct frame *f;
2493 Lisp_Object arg, oldval;
2494 {
2495 int wid = FONT_WIDTH (f->output_data.x->font);
2496
2497 if (NILP (arg))
2498 {
2499 #ifdef USE_TOOLKIT_SCROLL_BARS
2500 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
2501 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2502 FRAME_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
2503 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = width;
2504 #else
2505 /* Make the actual width at least 14 pixels and a multiple of a
2506 character width. */
2507 FRAME_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
2508
2509 /* Use all of that space (aside from required margins) for the
2510 scroll bar. */
2511 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = 0;
2512 #endif
2513
2514 if (FRAME_X_WINDOW (f))
2515 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2516 do_pending_window_change (0);
2517 }
2518 else if (INTEGERP (arg) && XINT (arg) > 0
2519 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2520 {
2521 if (XFASTINT (arg) <= 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM)
2522 XSETINT (arg, 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM + 1);
2523
2524 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
2525 FRAME_SCROLL_BAR_COLS (f) = (XFASTINT (arg) + wid-1) / wid;
2526 if (FRAME_X_WINDOW (f))
2527 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2528 }
2529
2530 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2531 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2532 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
2533 }
2534
2535
2536 \f
2537 /* Subroutines of creating an X frame. */
2538
2539 /* Make sure that Vx_resource_name is set to a reasonable value.
2540 Fix it up, or set it to `emacs' if it is too hopeless. */
2541
2542 static void
2543 validate_x_resource_name ()
2544 {
2545 int len = 0;
2546 /* Number of valid characters in the resource name. */
2547 int good_count = 0;
2548 /* Number of invalid characters in the resource name. */
2549 int bad_count = 0;
2550 Lisp_Object new;
2551 int i;
2552
2553 if (!STRINGP (Vx_resource_class))
2554 Vx_resource_class = build_string (EMACS_CLASS);
2555
2556 if (STRINGP (Vx_resource_name))
2557 {
2558 unsigned char *p = XSTRING (Vx_resource_name)->data;
2559 int i;
2560
2561 len = STRING_BYTES (XSTRING (Vx_resource_name));
2562
2563 /* Only letters, digits, - and _ are valid in resource names.
2564 Count the valid characters and count the invalid ones. */
2565 for (i = 0; i < len; i++)
2566 {
2567 int c = p[i];
2568 if (! ((c >= 'a' && c <= 'z')
2569 || (c >= 'A' && c <= 'Z')
2570 || (c >= '0' && c <= '9')
2571 || c == '-' || c == '_'))
2572 bad_count++;
2573 else
2574 good_count++;
2575 }
2576 }
2577 else
2578 /* Not a string => completely invalid. */
2579 bad_count = 5, good_count = 0;
2580
2581 /* If name is valid already, return. */
2582 if (bad_count == 0)
2583 return;
2584
2585 /* If name is entirely invalid, or nearly so, use `emacs'. */
2586 if (good_count == 0
2587 || (good_count == 1 && bad_count > 0))
2588 {
2589 Vx_resource_name = build_string ("emacs");
2590 return;
2591 }
2592
2593 /* Name is partly valid. Copy it and replace the invalid characters
2594 with underscores. */
2595
2596 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2597
2598 for (i = 0; i < len; i++)
2599 {
2600 int c = XSTRING (new)->data[i];
2601 if (! ((c >= 'a' && c <= 'z')
2602 || (c >= 'A' && c <= 'Z')
2603 || (c >= '0' && c <= '9')
2604 || c == '-' || c == '_'))
2605 XSTRING (new)->data[i] = '_';
2606 }
2607 }
2608
2609
2610 extern char *x_get_string_resource ();
2611
2612 DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
2613 "Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.\n\
2614 This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the\n\
2615 class, where INSTANCE is the name under which Emacs was invoked, or\n\
2616 the name specified by the `-name' or `-rn' command-line arguments.\n\
2617 \n\
2618 The optional arguments COMPONENT and SUBCLASS add to the key and the\n\
2619 class, respectively. You must specify both of them or neither.\n\
2620 If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'\n\
2621 and the class is `Emacs.CLASS.SUBCLASS'.")
2622 (attribute, class, component, subclass)
2623 Lisp_Object attribute, class, component, subclass;
2624 {
2625 register char *value;
2626 char *name_key;
2627 char *class_key;
2628
2629 check_x ();
2630
2631 CHECK_STRING (attribute, 0);
2632 CHECK_STRING (class, 0);
2633
2634 if (!NILP (component))
2635 CHECK_STRING (component, 1);
2636 if (!NILP (subclass))
2637 CHECK_STRING (subclass, 2);
2638 if (NILP (component) != NILP (subclass))
2639 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2640
2641 validate_x_resource_name ();
2642
2643 /* Allocate space for the components, the dots which separate them,
2644 and the final '\0'. Make them big enough for the worst case. */
2645 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
2646 + (STRINGP (component)
2647 ? STRING_BYTES (XSTRING (component)) : 0)
2648 + STRING_BYTES (XSTRING (attribute))
2649 + 3);
2650
2651 class_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_class))
2652 + STRING_BYTES (XSTRING (class))
2653 + (STRINGP (subclass)
2654 ? STRING_BYTES (XSTRING (subclass)) : 0)
2655 + 3);
2656
2657 /* Start with emacs.FRAMENAME for the name (the specific one)
2658 and with `Emacs' for the class key (the general one). */
2659 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2660 strcpy (class_key, XSTRING (Vx_resource_class)->data);
2661
2662 strcat (class_key, ".");
2663 strcat (class_key, XSTRING (class)->data);
2664
2665 if (!NILP (component))
2666 {
2667 strcat (class_key, ".");
2668 strcat (class_key, XSTRING (subclass)->data);
2669
2670 strcat (name_key, ".");
2671 strcat (name_key, XSTRING (component)->data);
2672 }
2673
2674 strcat (name_key, ".");
2675 strcat (name_key, XSTRING (attribute)->data);
2676
2677 value = x_get_string_resource (check_x_display_info (Qnil)->xrdb,
2678 name_key, class_key);
2679
2680 if (value != (char *) 0)
2681 return build_string (value);
2682 else
2683 return Qnil;
2684 }
2685
2686 /* Get an X resource, like Fx_get_resource, but for display DPYINFO. */
2687
2688 Lisp_Object
2689 display_x_get_resource (dpyinfo, attribute, class, component, subclass)
2690 struct x_display_info *dpyinfo;
2691 Lisp_Object attribute, class, component, subclass;
2692 {
2693 register char *value;
2694 char *name_key;
2695 char *class_key;
2696
2697 CHECK_STRING (attribute, 0);
2698 CHECK_STRING (class, 0);
2699
2700 if (!NILP (component))
2701 CHECK_STRING (component, 1);
2702 if (!NILP (subclass))
2703 CHECK_STRING (subclass, 2);
2704 if (NILP (component) != NILP (subclass))
2705 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2706
2707 validate_x_resource_name ();
2708
2709 /* Allocate space for the components, the dots which separate them,
2710 and the final '\0'. Make them big enough for the worst case. */
2711 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
2712 + (STRINGP (component)
2713 ? STRING_BYTES (XSTRING (component)) : 0)
2714 + STRING_BYTES (XSTRING (attribute))
2715 + 3);
2716
2717 class_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_class))
2718 + STRING_BYTES (XSTRING (class))
2719 + (STRINGP (subclass)
2720 ? STRING_BYTES (XSTRING (subclass)) : 0)
2721 + 3);
2722
2723 /* Start with emacs.FRAMENAME for the name (the specific one)
2724 and with `Emacs' for the class key (the general one). */
2725 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2726 strcpy (class_key, XSTRING (Vx_resource_class)->data);
2727
2728 strcat (class_key, ".");
2729 strcat (class_key, XSTRING (class)->data);
2730
2731 if (!NILP (component))
2732 {
2733 strcat (class_key, ".");
2734 strcat (class_key, XSTRING (subclass)->data);
2735
2736 strcat (name_key, ".");
2737 strcat (name_key, XSTRING (component)->data);
2738 }
2739
2740 strcat (name_key, ".");
2741 strcat (name_key, XSTRING (attribute)->data);
2742
2743 value = x_get_string_resource (dpyinfo->xrdb, name_key, class_key);
2744
2745 if (value != (char *) 0)
2746 return build_string (value);
2747 else
2748 return Qnil;
2749 }
2750
2751 /* Used when C code wants a resource value. */
2752
2753 char *
2754 x_get_resource_string (attribute, class)
2755 char *attribute, *class;
2756 {
2757 char *name_key;
2758 char *class_key;
2759 struct frame *sf = SELECTED_FRAME ();
2760
2761 /* Allocate space for the components, the dots which separate them,
2762 and the final '\0'. */
2763 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
2764 + strlen (attribute) + 2);
2765 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2766 + strlen (class) + 2);
2767
2768 sprintf (name_key, "%s.%s",
2769 XSTRING (Vinvocation_name)->data,
2770 attribute);
2771 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2772
2773 return x_get_string_resource (FRAME_X_DISPLAY_INFO (sf)->xrdb,
2774 name_key, class_key);
2775 }
2776
2777 /* Types we might convert a resource string into. */
2778 enum resource_types
2779 {
2780 RES_TYPE_NUMBER,
2781 RES_TYPE_FLOAT,
2782 RES_TYPE_BOOLEAN,
2783 RES_TYPE_STRING,
2784 RES_TYPE_SYMBOL
2785 };
2786
2787 /* Return the value of parameter PARAM.
2788
2789 First search ALIST, then Vdefault_frame_alist, then the X defaults
2790 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2791
2792 Convert the resource to the type specified by desired_type.
2793
2794 If no default is specified, return Qunbound. If you call
2795 x_get_arg, make sure you deal with Qunbound in a reasonable way,
2796 and don't let it get stored in any Lisp-visible variables! */
2797
2798 static Lisp_Object
2799 x_get_arg (dpyinfo, alist, param, attribute, class, type)
2800 struct x_display_info *dpyinfo;
2801 Lisp_Object alist, param;
2802 char *attribute;
2803 char *class;
2804 enum resource_types type;
2805 {
2806 register Lisp_Object tem;
2807
2808 tem = Fassq (param, alist);
2809 if (EQ (tem, Qnil))
2810 tem = Fassq (param, Vdefault_frame_alist);
2811 if (EQ (tem, Qnil))
2812 {
2813
2814 if (attribute)
2815 {
2816 tem = display_x_get_resource (dpyinfo,
2817 build_string (attribute),
2818 build_string (class),
2819 Qnil, Qnil);
2820
2821 if (NILP (tem))
2822 return Qunbound;
2823
2824 switch (type)
2825 {
2826 case RES_TYPE_NUMBER:
2827 return make_number (atoi (XSTRING (tem)->data));
2828
2829 case RES_TYPE_FLOAT:
2830 return make_float (atof (XSTRING (tem)->data));
2831
2832 case RES_TYPE_BOOLEAN:
2833 tem = Fdowncase (tem);
2834 if (!strcmp (XSTRING (tem)->data, "on")
2835 || !strcmp (XSTRING (tem)->data, "true"))
2836 return Qt;
2837 else
2838 return Qnil;
2839
2840 case RES_TYPE_STRING:
2841 return tem;
2842
2843 case RES_TYPE_SYMBOL:
2844 /* As a special case, we map the values `true' and `on'
2845 to Qt, and `false' and `off' to Qnil. */
2846 {
2847 Lisp_Object lower;
2848 lower = Fdowncase (tem);
2849 if (!strcmp (XSTRING (lower)->data, "on")
2850 || !strcmp (XSTRING (lower)->data, "true"))
2851 return Qt;
2852 else if (!strcmp (XSTRING (lower)->data, "off")
2853 || !strcmp (XSTRING (lower)->data, "false"))
2854 return Qnil;
2855 else
2856 return Fintern (tem, Qnil);
2857 }
2858
2859 default:
2860 abort ();
2861 }
2862 }
2863 else
2864 return Qunbound;
2865 }
2866 return Fcdr (tem);
2867 }
2868
2869 /* Like x_get_arg, but also record the value in f->param_alist. */
2870
2871 static Lisp_Object
2872 x_get_and_record_arg (f, alist, param, attribute, class, type)
2873 struct frame *f;
2874 Lisp_Object alist, param;
2875 char *attribute;
2876 char *class;
2877 enum resource_types type;
2878 {
2879 Lisp_Object value;
2880
2881 value = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, param,
2882 attribute, class, type);
2883 if (! NILP (value))
2884 store_frame_param (f, param, value);
2885
2886 return value;
2887 }
2888
2889 /* Record in frame F the specified or default value according to ALIST
2890 of the parameter named PROP (a Lisp symbol).
2891 If no value is specified for PROP, look for an X default for XPROP
2892 on the frame named NAME.
2893 If that is not found either, use the value DEFLT. */
2894
2895 static Lisp_Object
2896 x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
2897 struct frame *f;
2898 Lisp_Object alist;
2899 Lisp_Object prop;
2900 Lisp_Object deflt;
2901 char *xprop;
2902 char *xclass;
2903 enum resource_types type;
2904 {
2905 Lisp_Object tem;
2906
2907 tem = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, prop, xprop, xclass, type);
2908 if (EQ (tem, Qunbound))
2909 tem = deflt;
2910 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
2911 return tem;
2912 }
2913
2914
2915 /* Record in frame F the specified or default value according to ALIST
2916 of the parameter named PROP (a Lisp symbol). If no value is
2917 specified for PROP, look for an X default for XPROP on the frame
2918 named NAME. If that is not found either, use the value DEFLT. */
2919
2920 static Lisp_Object
2921 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
2922 foreground_p)
2923 struct frame *f;
2924 Lisp_Object alist;
2925 Lisp_Object prop;
2926 char *xprop;
2927 char *xclass;
2928 int foreground_p;
2929 {
2930 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2931 Lisp_Object tem;
2932
2933 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
2934 if (EQ (tem, Qunbound))
2935 {
2936 #ifdef USE_TOOLKIT_SCROLL_BARS
2937
2938 /* See if an X resource for the scroll bar color has been
2939 specified. */
2940 tem = display_x_get_resource (dpyinfo,
2941 build_string (foreground_p
2942 ? "foreground"
2943 : "background"),
2944 build_string (""),
2945 build_string ("verticalScrollBar"),
2946 build_string (""));
2947 if (!STRINGP (tem))
2948 {
2949 /* If nothing has been specified, scroll bars will use a
2950 toolkit-dependent default. Because these defaults are
2951 difficult to get at without actually creating a scroll
2952 bar, use nil to indicate that no color has been
2953 specified. */
2954 tem = Qnil;
2955 }
2956
2957 #else /* not USE_TOOLKIT_SCROLL_BARS */
2958
2959 tem = Qnil;
2960
2961 #endif /* not USE_TOOLKIT_SCROLL_BARS */
2962 }
2963
2964 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
2965 return tem;
2966 }
2967
2968
2969 \f
2970 DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
2971 "Parse an X-style geometry string STRING.\n\
2972 Returns an alist of the form ((top . TOP), (left . LEFT) ... ).\n\
2973 The properties returned may include `top', `left', `height', and `width'.\n\
2974 The value of `left' or `top' may be an integer,\n\
2975 or a list (+ N) meaning N pixels relative to top/left corner,\n\
2976 or a list (- N) meaning -N pixels relative to bottom/right corner.")
2977 (string)
2978 Lisp_Object string;
2979 {
2980 int geometry, x, y;
2981 unsigned int width, height;
2982 Lisp_Object result;
2983
2984 CHECK_STRING (string, 0);
2985
2986 geometry = XParseGeometry ((char *) XSTRING (string)->data,
2987 &x, &y, &width, &height);
2988
2989 #if 0
2990 if (!!(geometry & XValue) != !!(geometry & YValue))
2991 error ("Must specify both x and y position, or neither");
2992 #endif
2993
2994 result = Qnil;
2995 if (geometry & XValue)
2996 {
2997 Lisp_Object element;
2998
2999 if (x >= 0 && (geometry & XNegative))
3000 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3001 else if (x < 0 && ! (geometry & XNegative))
3002 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3003 else
3004 element = Fcons (Qleft, make_number (x));
3005 result = Fcons (element, result);
3006 }
3007
3008 if (geometry & YValue)
3009 {
3010 Lisp_Object element;
3011
3012 if (y >= 0 && (geometry & YNegative))
3013 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3014 else if (y < 0 && ! (geometry & YNegative))
3015 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3016 else
3017 element = Fcons (Qtop, make_number (y));
3018 result = Fcons (element, result);
3019 }
3020
3021 if (geometry & WidthValue)
3022 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3023 if (geometry & HeightValue)
3024 result = Fcons (Fcons (Qheight, make_number (height)), result);
3025
3026 return result;
3027 }
3028
3029 /* Calculate the desired size and position of this window,
3030 and return the flags saying which aspects were specified.
3031
3032 This function does not make the coordinates positive. */
3033
3034 #define DEFAULT_ROWS 40
3035 #define DEFAULT_COLS 80
3036
3037 static int
3038 x_figure_window_size (f, parms)
3039 struct frame *f;
3040 Lisp_Object parms;
3041 {
3042 register Lisp_Object tem0, tem1, tem2;
3043 long window_prompting = 0;
3044 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3045
3046 /* Default values if we fall through.
3047 Actually, if that happens we should get
3048 window manager prompting. */
3049 SET_FRAME_WIDTH (f, DEFAULT_COLS);
3050 f->height = DEFAULT_ROWS;
3051 /* Window managers expect that if program-specified
3052 positions are not (0,0), they're intentional, not defaults. */
3053 f->output_data.x->top_pos = 0;
3054 f->output_data.x->left_pos = 0;
3055
3056 tem0 = x_get_arg (dpyinfo, parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3057 tem1 = x_get_arg (dpyinfo, parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3058 tem2 = x_get_arg (dpyinfo, parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
3059 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3060 {
3061 if (!EQ (tem0, Qunbound))
3062 {
3063 CHECK_NUMBER (tem0, 0);
3064 f->height = XINT (tem0);
3065 }
3066 if (!EQ (tem1, Qunbound))
3067 {
3068 CHECK_NUMBER (tem1, 0);
3069 SET_FRAME_WIDTH (f, XINT (tem1));
3070 }
3071 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3072 window_prompting |= USSize;
3073 else
3074 window_prompting |= PSize;
3075 }
3076
3077 f->output_data.x->vertical_scroll_bar_extra
3078 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3079 ? 0
3080 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
3081 f->output_data.x->flags_areas_extra
3082 = FRAME_FLAGS_AREA_WIDTH (f);
3083 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3084 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3085
3086 tem0 = x_get_arg (dpyinfo, parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3087 tem1 = x_get_arg (dpyinfo, parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3088 tem2 = x_get_arg (dpyinfo, parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
3089 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3090 {
3091 if (EQ (tem0, Qminus))
3092 {
3093 f->output_data.x->top_pos = 0;
3094 window_prompting |= YNegative;
3095 }
3096 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3097 && CONSP (XCDR (tem0))
3098 && INTEGERP (XCAR (XCDR (tem0))))
3099 {
3100 f->output_data.x->top_pos = - XINT (XCAR (XCDR (tem0)));
3101 window_prompting |= YNegative;
3102 }
3103 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3104 && CONSP (XCDR (tem0))
3105 && INTEGERP (XCAR (XCDR (tem0))))
3106 {
3107 f->output_data.x->top_pos = XINT (XCAR (XCDR (tem0)));
3108 }
3109 else if (EQ (tem0, Qunbound))
3110 f->output_data.x->top_pos = 0;
3111 else
3112 {
3113 CHECK_NUMBER (tem0, 0);
3114 f->output_data.x->top_pos = XINT (tem0);
3115 if (f->output_data.x->top_pos < 0)
3116 window_prompting |= YNegative;
3117 }
3118
3119 if (EQ (tem1, Qminus))
3120 {
3121 f->output_data.x->left_pos = 0;
3122 window_prompting |= XNegative;
3123 }
3124 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3125 && CONSP (XCDR (tem1))
3126 && INTEGERP (XCAR (XCDR (tem1))))
3127 {
3128 f->output_data.x->left_pos = - XINT (XCAR (XCDR (tem1)));
3129 window_prompting |= XNegative;
3130 }
3131 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3132 && CONSP (XCDR (tem1))
3133 && INTEGERP (XCAR (XCDR (tem1))))
3134 {
3135 f->output_data.x->left_pos = XINT (XCAR (XCDR (tem1)));
3136 }
3137 else if (EQ (tem1, Qunbound))
3138 f->output_data.x->left_pos = 0;
3139 else
3140 {
3141 CHECK_NUMBER (tem1, 0);
3142 f->output_data.x->left_pos = XINT (tem1);
3143 if (f->output_data.x->left_pos < 0)
3144 window_prompting |= XNegative;
3145 }
3146
3147 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3148 window_prompting |= USPosition;
3149 else
3150 window_prompting |= PPosition;
3151 }
3152
3153 return window_prompting;
3154 }
3155
3156 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
3157
3158 Status
3159 XSetWMProtocols (dpy, w, protocols, count)
3160 Display *dpy;
3161 Window w;
3162 Atom *protocols;
3163 int count;
3164 {
3165 Atom prop;
3166 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
3167 if (prop == None) return False;
3168 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
3169 (unsigned char *) protocols, count);
3170 return True;
3171 }
3172 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
3173 \f
3174 #ifdef USE_X_TOOLKIT
3175
3176 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
3177 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
3178 already be present because of the toolkit (Motif adds some of them,
3179 for example, but Xt doesn't). */
3180
3181 static void
3182 hack_wm_protocols (f, widget)
3183 FRAME_PTR f;
3184 Widget widget;
3185 {
3186 Display *dpy = XtDisplay (widget);
3187 Window w = XtWindow (widget);
3188 int need_delete = 1;
3189 int need_focus = 1;
3190 int need_save = 1;
3191
3192 BLOCK_INPUT;
3193 {
3194 Atom type, *atoms = 0;
3195 int format = 0;
3196 unsigned long nitems = 0;
3197 unsigned long bytes_after;
3198
3199 if ((XGetWindowProperty (dpy, w,
3200 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3201 (long)0, (long)100, False, XA_ATOM,
3202 &type, &format, &nitems, &bytes_after,
3203 (unsigned char **) &atoms)
3204 == Success)
3205 && format == 32 && type == XA_ATOM)
3206 while (nitems > 0)
3207 {
3208 nitems--;
3209 if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
3210 need_delete = 0;
3211 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
3212 need_focus = 0;
3213 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
3214 need_save = 0;
3215 }
3216 if (atoms) XFree ((char *) atoms);
3217 }
3218 {
3219 Atom props [10];
3220 int count = 0;
3221 if (need_delete)
3222 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3223 if (need_focus)
3224 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
3225 if (need_save)
3226 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
3227 if (count)
3228 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3229 XA_ATOM, 32, PropModeAppend,
3230 (unsigned char *) props, count);
3231 }
3232 UNBLOCK_INPUT;
3233 }
3234 #endif
3235
3236
3237 \f
3238 /* Support routines for XIC (X Input Context). */
3239
3240 #ifdef HAVE_X_I18N
3241
3242 static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
3243 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
3244
3245
3246 /* Supported XIM styles, ordered by preferenc. */
3247
3248 static XIMStyle supported_xim_styles[] =
3249 {
3250 XIMPreeditPosition | XIMStatusArea,
3251 XIMPreeditPosition | XIMStatusNothing,
3252 XIMPreeditPosition | XIMStatusNone,
3253 XIMPreeditNothing | XIMStatusArea,
3254 XIMPreeditNothing | XIMStatusNothing,
3255 XIMPreeditNothing | XIMStatusNone,
3256 XIMPreeditNone | XIMStatusArea,
3257 XIMPreeditNone | XIMStatusNothing,
3258 XIMPreeditNone | XIMStatusNone,
3259 0,
3260 };
3261
3262
3263 /* Create an X fontset on frame F with base font name
3264 BASE_FONTNAME.. */
3265
3266 static XFontSet
3267 xic_create_xfontset (f, base_fontname)
3268 struct frame *f;
3269 char *base_fontname;
3270 {
3271 XFontSet xfs;
3272 char **missing_list;
3273 int missing_count;
3274 char *def_string;
3275
3276 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
3277 base_fontname, &missing_list,
3278 &missing_count, &def_string);
3279 if (missing_list)
3280 XFreeStringList (missing_list);
3281
3282 /* No need to free def_string. */
3283 return xfs;
3284 }
3285
3286
3287 /* Value is the best input style, given user preferences USER (already
3288 checked to be supported by Emacs), and styles supported by the
3289 input method XIM. */
3290
3291 static XIMStyle
3292 best_xim_style (user, xim)
3293 XIMStyles *user;
3294 XIMStyles *xim;
3295 {
3296 int i, j;
3297
3298 for (i = 0; i < user->count_styles; ++i)
3299 for (j = 0; j < xim->count_styles; ++j)
3300 if (user->supported_styles[i] == xim->supported_styles[j])
3301 return user->supported_styles[i];
3302
3303 /* Return the default style. */
3304 return XIMPreeditNothing | XIMStatusNothing;
3305 }
3306
3307 /* Create XIC for frame F. */
3308
3309 void
3310 create_frame_xic (f)
3311 struct frame *f;
3312 {
3313 XIM xim;
3314 XIC xic = NULL;
3315 XFontSet xfs = NULL;
3316 static XIMStyle xic_style;
3317
3318 if (FRAME_XIC (f))
3319 return;
3320
3321 xim = FRAME_X_XIM (f);
3322 if (xim)
3323 {
3324 XRectangle s_area;
3325 XPoint spot;
3326 XVaNestedList preedit_attr;
3327 XVaNestedList status_attr;
3328 char *base_fontname;
3329 int fontset;
3330
3331 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
3332 spot.x = 0; spot.y = 1;
3333 /* Create X fontset. */
3334 fontset = FRAME_FONTSET (f);
3335 if (fontset < 0)
3336 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3337 else
3338 {
3339 /* Determine the base fontname from the ASCII font name of
3340 FONTSET. */
3341 char *ascii_font = (char *) XSTRING (fontset_ascii (fontset))->data;
3342 char *p = ascii_font;
3343 int i;
3344
3345 for (i = 0; *p; p++)
3346 if (*p == '-') i++;
3347 if (i != 14)
3348 /* As the font name doesn't conform to XLFD, we can't
3349 modify it to get a suitable base fontname for the
3350 frame. */
3351 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3352 else
3353 {
3354 int len = strlen (ascii_font) + 1;
3355 char *p1 = NULL;
3356
3357 for (i = 0, p = ascii_font; i < 8; p++)
3358 {
3359 if (*p == '-')
3360 {
3361 i++;
3362 if (i == 3)
3363 p1 = p + 1;
3364 }
3365 }
3366 base_fontname = (char *) alloca (len);
3367 bzero (base_fontname, len);
3368 strcpy (base_fontname, "-*-*-");
3369 bcopy (p1, base_fontname + 5, p - p1);
3370 strcat (base_fontname, "*-*-*-*-*-*-*");
3371 }
3372 }
3373 xfs = xic_create_xfontset (f, base_fontname);
3374
3375 /* Determine XIC style. */
3376 if (xic_style == 0)
3377 {
3378 XIMStyles supported_list;
3379 supported_list.count_styles = (sizeof supported_xim_styles
3380 / sizeof supported_xim_styles[0]);
3381 supported_list.supported_styles = supported_xim_styles;
3382 xic_style = best_xim_style (&supported_list,
3383 FRAME_X_XIM_STYLES (f));
3384 }
3385
3386 preedit_attr = XVaCreateNestedList (0,
3387 XNFontSet, xfs,
3388 XNForeground,
3389 FRAME_FOREGROUND_PIXEL (f),
3390 XNBackground,
3391 FRAME_BACKGROUND_PIXEL (f),
3392 (xic_style & XIMPreeditPosition
3393 ? XNSpotLocation
3394 : NULL),
3395 &spot,
3396 NULL);
3397 status_attr = XVaCreateNestedList (0,
3398 XNArea,
3399 &s_area,
3400 XNFontSet,
3401 xfs,
3402 XNForeground,
3403 FRAME_FOREGROUND_PIXEL (f),
3404 XNBackground,
3405 FRAME_BACKGROUND_PIXEL (f),
3406 NULL);
3407
3408 xic = XCreateIC (xim,
3409 XNInputStyle, xic_style,
3410 XNClientWindow, FRAME_X_WINDOW(f),
3411 XNFocusWindow, FRAME_X_WINDOW(f),
3412 XNStatusAttributes, status_attr,
3413 XNPreeditAttributes, preedit_attr,
3414 NULL);
3415 XFree (preedit_attr);
3416 XFree (status_attr);
3417 }
3418
3419 FRAME_XIC (f) = xic;
3420 FRAME_XIC_STYLE (f) = xic_style;
3421 FRAME_XIC_FONTSET (f) = xfs;
3422 }
3423
3424
3425 /* Destroy XIC and free XIC fontset of frame F, if any. */
3426
3427 void
3428 free_frame_xic (f)
3429 struct frame *f;
3430 {
3431 if (FRAME_XIC (f) == NULL)
3432 return;
3433
3434 XDestroyIC (FRAME_XIC (f));
3435 if (FRAME_XIC_FONTSET (f))
3436 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3437
3438 FRAME_XIC (f) = NULL;
3439 FRAME_XIC_FONTSET (f) = NULL;
3440 }
3441
3442
3443 /* Place preedit area for XIC of window W's frame to specified
3444 pixel position X/Y. X and Y are relative to window W. */
3445
3446 void
3447 xic_set_preeditarea (w, x, y)
3448 struct window *w;
3449 int x, y;
3450 {
3451 struct frame *f = XFRAME (w->frame);
3452 XVaNestedList attr;
3453 XPoint spot;
3454
3455 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x);
3456 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
3457 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
3458 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3459 XFree (attr);
3460 }
3461
3462
3463 /* Place status area for XIC in bottom right corner of frame F.. */
3464
3465 void
3466 xic_set_statusarea (f)
3467 struct frame *f;
3468 {
3469 XIC xic = FRAME_XIC (f);
3470 XVaNestedList attr;
3471 XRectangle area;
3472 XRectangle *needed;
3473
3474 /* Negotiate geometry of status area. If input method has existing
3475 status area, use its current size. */
3476 area.x = area.y = area.width = area.height = 0;
3477 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
3478 XSetICValues (xic, XNStatusAttributes, attr, NULL);
3479 XFree (attr);
3480
3481 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
3482 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3483 XFree (attr);
3484
3485 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
3486 {
3487 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
3488 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3489 XFree (attr);
3490 }
3491
3492 area.width = needed->width;
3493 area.height = needed->height;
3494 area.x = PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
3495 area.y = (PIXEL_HEIGHT (f) - area.height
3496 - FRAME_MENUBAR_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f));
3497 XFree (needed);
3498
3499 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
3500 XSetICValues(xic, XNStatusAttributes, attr, NULL);
3501 XFree (attr);
3502 }
3503
3504
3505 /* Set X fontset for XIC of frame F, using base font name
3506 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
3507
3508 void
3509 xic_set_xfontset (f, base_fontname)
3510 struct frame *f;
3511 char *base_fontname;
3512 {
3513 XVaNestedList attr;
3514 XFontSet xfs;
3515
3516 xfs = xic_create_xfontset (f, base_fontname);
3517
3518 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
3519 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
3520 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3521 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
3522 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
3523 XFree (attr);
3524
3525 if (FRAME_XIC_FONTSET (f))
3526 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3527 FRAME_XIC_FONTSET (f) = xfs;
3528 }
3529
3530 #endif /* HAVE_X_I18N */
3531
3532
3533 \f
3534 #ifdef USE_X_TOOLKIT
3535
3536 /* Create and set up the X widget for frame F. */
3537
3538 static void
3539 x_window (f, window_prompting, minibuffer_only)
3540 struct frame *f;
3541 long window_prompting;
3542 int minibuffer_only;
3543 {
3544 XClassHint class_hints;
3545 XSetWindowAttributes attributes;
3546 unsigned long attribute_mask;
3547 Widget shell_widget;
3548 Widget pane_widget;
3549 Widget frame_widget;
3550 Arg al [25];
3551 int ac;
3552
3553 BLOCK_INPUT;
3554
3555 /* Use the resource name as the top-level widget name
3556 for looking up resources. Make a non-Lisp copy
3557 for the window manager, so GC relocation won't bother it.
3558
3559 Elsewhere we specify the window name for the window manager. */
3560
3561 {
3562 char *str = (char *) XSTRING (Vx_resource_name)->data;
3563 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3564 strcpy (f->namebuf, str);
3565 }
3566
3567 ac = 0;
3568 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
3569 XtSetArg (al[ac], XtNinput, 1); ac++;
3570 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3571 XtSetArg (al[ac], XtNborderWidth, f->output_data.x->border_width); ac++;
3572 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3573 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3574 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3575 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
3576 applicationShellWidgetClass,
3577 FRAME_X_DISPLAY (f), al, ac);
3578
3579 f->output_data.x->widget = shell_widget;
3580 /* maybe_set_screen_title_format (shell_widget); */
3581
3582 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
3583 (widget_value *) NULL,
3584 shell_widget, False,
3585 (lw_callback) NULL,
3586 (lw_callback) NULL,
3587 (lw_callback) NULL,
3588 (lw_callback) NULL);
3589
3590 ac = 0;
3591 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3592 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3593 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3594 XtSetValues (pane_widget, al, ac);
3595 f->output_data.x->column_widget = pane_widget;
3596
3597 /* mappedWhenManaged to false tells to the paned window to not map/unmap
3598 the emacs screen when changing menubar. This reduces flickering. */
3599
3600 ac = 0;
3601 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3602 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
3603 XtSetArg (al[ac], XtNallowResize, 1); ac++;
3604 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
3605 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
3606 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3607 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3608 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3609 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
3610 al, ac);
3611
3612 f->output_data.x->edit_widget = frame_widget;
3613
3614 XtManageChild (frame_widget);
3615
3616 /* Do some needed geometry management. */
3617 {
3618 int len;
3619 char *tem, shell_position[32];
3620 Arg al[2];
3621 int ac = 0;
3622 int extra_borders = 0;
3623 int menubar_size
3624 = (f->output_data.x->menubar_widget
3625 ? (f->output_data.x->menubar_widget->core.height
3626 + f->output_data.x->menubar_widget->core.border_width)
3627 : 0);
3628
3629 #if 0 /* Experimentally, we now get the right results
3630 for -geometry -0-0 without this. 24 Aug 96, rms. */
3631 if (FRAME_EXTERNAL_MENU_BAR (f))
3632 {
3633 Dimension ibw = 0;
3634 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
3635 menubar_size += ibw;
3636 }
3637 #endif
3638
3639 f->output_data.x->menubar_height = menubar_size;
3640
3641 #ifndef USE_LUCID
3642 /* Motif seems to need this amount added to the sizes
3643 specified for the shell widget. The Athena/Lucid widgets don't.
3644 Both conclusions reached experimentally. -- rms. */
3645 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
3646 &extra_borders, NULL);
3647 extra_borders *= 2;
3648 #endif
3649
3650 /* Convert our geometry parameters into a geometry string
3651 and specify it.
3652 Note that we do not specify here whether the position
3653 is a user-specified or program-specified one.
3654 We pass that information later, in x_wm_set_size_hints. */
3655 {
3656 int left = f->output_data.x->left_pos;
3657 int xneg = window_prompting & XNegative;
3658 int top = f->output_data.x->top_pos;
3659 int yneg = window_prompting & YNegative;
3660 if (xneg)
3661 left = -left;
3662 if (yneg)
3663 top = -top;
3664
3665 if (window_prompting & USPosition)
3666 sprintf (shell_position, "=%dx%d%c%d%c%d",
3667 PIXEL_WIDTH (f) + extra_borders,
3668 PIXEL_HEIGHT (f) + menubar_size + extra_borders,
3669 (xneg ? '-' : '+'), left,
3670 (yneg ? '-' : '+'), top);
3671 else
3672 sprintf (shell_position, "=%dx%d",
3673 PIXEL_WIDTH (f) + extra_borders,
3674 PIXEL_HEIGHT (f) + menubar_size + extra_borders);
3675 }
3676
3677 len = strlen (shell_position) + 1;
3678 /* We don't free this because we don't know whether
3679 it is safe to free it while the frame exists.
3680 It isn't worth the trouble of arranging to free it
3681 when the frame is deleted. */
3682 tem = (char *) xmalloc (len);
3683 strncpy (tem, shell_position, len);
3684 XtSetArg (al[ac], XtNgeometry, tem); ac++;
3685 XtSetValues (shell_widget, al, ac);
3686 }
3687
3688 XtManageChild (pane_widget);
3689 XtRealizeWidget (shell_widget);
3690
3691 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
3692
3693 validate_x_resource_name ();
3694
3695 class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
3696 class_hints.res_class = (char *) XSTRING (Vx_resource_class)->data;
3697 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
3698
3699 #ifdef HAVE_X_I18N
3700 FRAME_XIC (f) = NULL;
3701 #ifdef USE_XIM
3702 create_frame_xic (f);
3703 #endif
3704 #endif
3705
3706 f->output_data.x->wm_hints.input = True;
3707 f->output_data.x->wm_hints.flags |= InputHint;
3708 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3709 &f->output_data.x->wm_hints);
3710
3711 hack_wm_protocols (f, shell_widget);
3712
3713 #ifdef HACK_EDITRES
3714 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
3715 #endif
3716
3717 /* Do a stupid property change to force the server to generate a
3718 PropertyNotify event so that the event_stream server timestamp will
3719 be initialized to something relevant to the time we created the window.
3720 */
3721 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
3722 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3723 XA_ATOM, 32, PropModeAppend,
3724 (unsigned char*) NULL, 0);
3725
3726 /* Make all the standard events reach the Emacs frame. */
3727 attributes.event_mask = STANDARD_EVENT_SET;
3728
3729 #ifdef HAVE_X_I18N
3730 if (FRAME_XIC (f))
3731 {
3732 /* XIM server might require some X events. */
3733 unsigned long fevent = NoEventMask;
3734 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3735 attributes.event_mask |= fevent;
3736 }
3737 #endif /* HAVE_X_I18N */
3738
3739 attribute_mask = CWEventMask;
3740 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
3741 attribute_mask, &attributes);
3742
3743 XtMapWidget (frame_widget);
3744
3745 /* x_set_name normally ignores requests to set the name if the
3746 requested name is the same as the current name. This is the one
3747 place where that assumption isn't correct; f->name is set, but
3748 the X server hasn't been told. */
3749 {
3750 Lisp_Object name;
3751 int explicit = f->explicit_name;
3752
3753 f->explicit_name = 0;
3754 name = f->name;
3755 f->name = Qnil;
3756 x_set_name (f, name, explicit);
3757 }
3758
3759 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3760 f->output_data.x->text_cursor);
3761
3762 UNBLOCK_INPUT;
3763
3764 /* This is a no-op, except under Motif. Make sure main areas are
3765 set to something reasonable, in case we get an error later. */
3766 lw_set_main_areas (pane_widget, 0, frame_widget);
3767 }
3768
3769 #else /* not USE_X_TOOLKIT */
3770
3771 /* Create and set up the X window for frame F. */
3772
3773 void
3774 x_window (f)
3775 struct frame *f;
3776
3777 {
3778 XClassHint class_hints;
3779 XSetWindowAttributes attributes;
3780 unsigned long attribute_mask;
3781
3782 attributes.background_pixel = f->output_data.x->background_pixel;
3783 attributes.border_pixel = f->output_data.x->border_pixel;
3784 attributes.bit_gravity = StaticGravity;
3785 attributes.backing_store = NotUseful;
3786 attributes.save_under = True;
3787 attributes.event_mask = STANDARD_EVENT_SET;
3788 attributes.colormap = FRAME_X_COLORMAP (f);
3789 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
3790 | CWColormap);
3791
3792 BLOCK_INPUT;
3793 FRAME_X_WINDOW (f)
3794 = XCreateWindow (FRAME_X_DISPLAY (f),
3795 f->output_data.x->parent_desc,
3796 f->output_data.x->left_pos,
3797 f->output_data.x->top_pos,
3798 PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
3799 f->output_data.x->border_width,
3800 CopyFromParent, /* depth */
3801 InputOutput, /* class */
3802 FRAME_X_VISUAL (f),
3803 attribute_mask, &attributes);
3804
3805 #ifdef HAVE_X_I18N
3806 #ifdef USE_XIM
3807 create_frame_xic (f);
3808 if (FRAME_XIC (f))
3809 {
3810 /* XIM server might require some X events. */
3811 unsigned long fevent = NoEventMask;
3812 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3813 attributes.event_mask |= fevent;
3814 attribute_mask = CWEventMask;
3815 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3816 attribute_mask, &attributes);
3817 }
3818 #endif
3819 #endif /* HAVE_X_I18N */
3820
3821 validate_x_resource_name ();
3822
3823 class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
3824 class_hints.res_class = (char *) XSTRING (Vx_resource_class)->data;
3825 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
3826
3827 /* The menubar is part of the ordinary display;
3828 it does not count in addition to the height of the window. */
3829 f->output_data.x->menubar_height = 0;
3830
3831 /* This indicates that we use the "Passive Input" input model.
3832 Unless we do this, we don't get the Focus{In,Out} events that we
3833 need to draw the cursor correctly. Accursed bureaucrats.
3834 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
3835
3836 f->output_data.x->wm_hints.input = True;
3837 f->output_data.x->wm_hints.flags |= InputHint;
3838 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3839 &f->output_data.x->wm_hints);
3840 f->output_data.x->wm_hints.icon_pixmap = None;
3841
3842 /* Request "save yourself" and "delete window" commands from wm. */
3843 {
3844 Atom protocols[2];
3845 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3846 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
3847 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
3848 }
3849
3850 /* x_set_name normally ignores requests to set the name if the
3851 requested name is the same as the current name. This is the one
3852 place where that assumption isn't correct; f->name is set, but
3853 the X server hasn't been told. */
3854 {
3855 Lisp_Object name;
3856 int explicit = f->explicit_name;
3857
3858 f->explicit_name = 0;
3859 name = f->name;
3860 f->name = Qnil;
3861 x_set_name (f, name, explicit);
3862 }
3863
3864 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3865 f->output_data.x->text_cursor);
3866
3867 UNBLOCK_INPUT;
3868
3869 if (FRAME_X_WINDOW (f) == 0)
3870 error ("Unable to create window");
3871 }
3872
3873 #endif /* not USE_X_TOOLKIT */
3874
3875 /* Handle the icon stuff for this window. Perhaps later we might
3876 want an x_set_icon_position which can be called interactively as
3877 well. */
3878
3879 static void
3880 x_icon (f, parms)
3881 struct frame *f;
3882 Lisp_Object parms;
3883 {
3884 Lisp_Object icon_x, icon_y;
3885 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3886
3887 /* Set the position of the icon. Note that twm groups all
3888 icons in an icon window. */
3889 icon_x = x_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
3890 icon_y = x_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
3891 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
3892 {
3893 CHECK_NUMBER (icon_x, 0);
3894 CHECK_NUMBER (icon_y, 0);
3895 }
3896 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
3897 error ("Both left and top icon corners of icon must be specified");
3898
3899 BLOCK_INPUT;
3900
3901 if (! EQ (icon_x, Qunbound))
3902 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
3903
3904 /* Start up iconic or window? */
3905 x_wm_set_window_state
3906 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
3907 Qicon)
3908 ? IconicState
3909 : NormalState));
3910
3911 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
3912 ? f->icon_name
3913 : f->name))->data);
3914
3915 UNBLOCK_INPUT;
3916 }
3917
3918 /* Make the GCs needed for this window, setting the
3919 background, border and mouse colors; also create the
3920 mouse cursor and the gray border tile. */
3921
3922 static char cursor_bits[] =
3923 {
3924 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3925 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3926 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3927 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3928 };
3929
3930 static void
3931 x_make_gc (f)
3932 struct frame *f;
3933 {
3934 XGCValues gc_values;
3935
3936 BLOCK_INPUT;
3937
3938 /* Create the GCs of this frame.
3939 Note that many default values are used. */
3940
3941 /* Normal video */
3942 gc_values.font = f->output_data.x->font->fid;
3943 gc_values.foreground = f->output_data.x->foreground_pixel;
3944 gc_values.background = f->output_data.x->background_pixel;
3945 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
3946 f->output_data.x->normal_gc
3947 = XCreateGC (FRAME_X_DISPLAY (f),
3948 FRAME_X_WINDOW (f),
3949 GCLineWidth | GCFont | GCForeground | GCBackground,
3950 &gc_values);
3951
3952 /* Reverse video style. */
3953 gc_values.foreground = f->output_data.x->background_pixel;
3954 gc_values.background = f->output_data.x->foreground_pixel;
3955 f->output_data.x->reverse_gc
3956 = XCreateGC (FRAME_X_DISPLAY (f),
3957 FRAME_X_WINDOW (f),
3958 GCFont | GCForeground | GCBackground | GCLineWidth,
3959 &gc_values);
3960
3961 /* Cursor has cursor-color background, background-color foreground. */
3962 gc_values.foreground = f->output_data.x->background_pixel;
3963 gc_values.background = f->output_data.x->cursor_pixel;
3964 gc_values.fill_style = FillOpaqueStippled;
3965 gc_values.stipple
3966 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
3967 FRAME_X_DISPLAY_INFO (f)->root_window,
3968 cursor_bits, 16, 16);
3969 f->output_data.x->cursor_gc
3970 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3971 (GCFont | GCForeground | GCBackground
3972 | GCFillStyle /* | GCStipple */ | GCLineWidth),
3973 &gc_values);
3974
3975 /* Reliefs. */
3976 f->output_data.x->white_relief.gc = 0;
3977 f->output_data.x->black_relief.gc = 0;
3978
3979 /* Create the gray border tile used when the pointer is not in
3980 the frame. Since this depends on the frame's pixel values,
3981 this must be done on a per-frame basis. */
3982 f->output_data.x->border_tile
3983 = (XCreatePixmapFromBitmapData
3984 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
3985 gray_bits, gray_width, gray_height,
3986 f->output_data.x->foreground_pixel,
3987 f->output_data.x->background_pixel,
3988 DefaultDepth (FRAME_X_DISPLAY (f),
3989 XScreenNumberOfScreen (FRAME_X_SCREEN (f)))));
3990
3991 UNBLOCK_INPUT;
3992 }
3993
3994
3995 /* Free what was was allocated in x_make_gc. */
3996
3997 void
3998 x_free_gcs (f)
3999 struct frame *f;
4000 {
4001 Display *dpy = FRAME_X_DISPLAY (f);
4002
4003 BLOCK_INPUT;
4004
4005 if (f->output_data.x->normal_gc)
4006 {
4007 XFreeGC (dpy, f->output_data.x->normal_gc);
4008 f->output_data.x->normal_gc = 0;
4009 }
4010
4011 if (f->output_data.x->reverse_gc)
4012 {
4013 XFreeGC (dpy, f->output_data.x->reverse_gc);
4014 f->output_data.x->reverse_gc = 0;
4015 }
4016
4017 if (f->output_data.x->cursor_gc)
4018 {
4019 XFreeGC (dpy, f->output_data.x->cursor_gc);
4020 f->output_data.x->cursor_gc = 0;
4021 }
4022
4023 if (f->output_data.x->border_tile)
4024 {
4025 XFreePixmap (dpy, f->output_data.x->border_tile);
4026 f->output_data.x->border_tile = 0;
4027 }
4028
4029 UNBLOCK_INPUT;
4030 }
4031
4032
4033 /* Handler for signals raised during x_create_frame and
4034 x_create_top_frame. FRAME is the frame which is partially
4035 constructed. */
4036
4037 static Lisp_Object
4038 unwind_create_frame (frame)
4039 Lisp_Object frame;
4040 {
4041 struct frame *f = XFRAME (frame);
4042
4043 /* If frame is ``official'', nothing to do. */
4044 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4045 {
4046 #if GLYPH_DEBUG
4047 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4048 #endif
4049
4050 x_free_frame_resources (f);
4051
4052 /* Check that reference counts are indeed correct. */
4053 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4054 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4055 return Qt;
4056 }
4057
4058 return Qnil;
4059 }
4060
4061
4062 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4063 1, 1, 0,
4064 "Make a new X window, which is called a \"frame\" in Emacs terms.\n\
4065 Returns an Emacs frame object.\n\
4066 ALIST is an alist of frame parameters.\n\
4067 If the parameters specify that the frame should not have a minibuffer,\n\
4068 and do not specify a specific minibuffer window to use,\n\
4069 then `default-minibuffer-frame' must be a frame whose minibuffer can\n\
4070 be shared by the new frame.\n\
4071 \n\
4072 This function is an internal primitive--use `make-frame' instead.")
4073 (parms)
4074 Lisp_Object parms;
4075 {
4076 struct frame *f;
4077 Lisp_Object frame, tem;
4078 Lisp_Object name;
4079 int minibuffer_only = 0;
4080 long window_prompting = 0;
4081 int width, height;
4082 int count = BINDING_STACK_SIZE ();
4083 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4084 Lisp_Object display;
4085 struct x_display_info *dpyinfo = NULL;
4086 Lisp_Object parent;
4087 struct kboard *kb;
4088
4089 check_x ();
4090
4091 /* Use this general default value to start with
4092 until we know if this frame has a specified name. */
4093 Vx_resource_name = Vinvocation_name;
4094
4095 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
4096 if (EQ (display, Qunbound))
4097 display = Qnil;
4098 dpyinfo = check_x_display_info (display);
4099 #ifdef MULTI_KBOARD
4100 kb = dpyinfo->kboard;
4101 #else
4102 kb = &the_only_kboard;
4103 #endif
4104
4105 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4106 if (!STRINGP (name)
4107 && ! EQ (name, Qunbound)
4108 && ! NILP (name))
4109 error ("Invalid frame name--not a string or nil");
4110
4111 if (STRINGP (name))
4112 Vx_resource_name = name;
4113
4114 /* See if parent window is specified. */
4115 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4116 if (EQ (parent, Qunbound))
4117 parent = Qnil;
4118 if (! NILP (parent))
4119 CHECK_NUMBER (parent, 0);
4120
4121 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4122 /* No need to protect DISPLAY because that's not used after passing
4123 it to make_frame_without_minibuffer. */
4124 frame = Qnil;
4125 GCPRO4 (parms, parent, name, frame);
4126 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
4127 RES_TYPE_SYMBOL);
4128 if (EQ (tem, Qnone) || NILP (tem))
4129 f = make_frame_without_minibuffer (Qnil, kb, display);
4130 else if (EQ (tem, Qonly))
4131 {
4132 f = make_minibuffer_frame ();
4133 minibuffer_only = 1;
4134 }
4135 else if (WINDOWP (tem))
4136 f = make_frame_without_minibuffer (tem, kb, display);
4137 else
4138 f = make_frame (1);
4139
4140 XSETFRAME (frame, f);
4141
4142 /* Note that X Windows does support scroll bars. */
4143 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4144
4145 f->output_method = output_x_window;
4146 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4147 bzero (f->output_data.x, sizeof (struct x_output));
4148 f->output_data.x->icon_bitmap = -1;
4149 f->output_data.x->fontset = -1;
4150 f->output_data.x->scroll_bar_foreground_pixel = -1;
4151 f->output_data.x->scroll_bar_background_pixel = -1;
4152 record_unwind_protect (unwind_create_frame, frame);
4153
4154 f->icon_name
4155 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
4156 RES_TYPE_STRING);
4157 if (! STRINGP (f->icon_name))
4158 f->icon_name = Qnil;
4159
4160 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4161 #if GLYPH_DEBUG
4162 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
4163 dpyinfo_refcount = dpyinfo->reference_count;
4164 #endif /* GLYPH_DEBUG */
4165 #ifdef MULTI_KBOARD
4166 FRAME_KBOARD (f) = kb;
4167 #endif
4168
4169 /* These colors will be set anyway later, but it's important
4170 to get the color reference counts right, so initialize them! */
4171 {
4172 Lisp_Object black;
4173 struct gcpro gcpro1;
4174
4175 black = build_string ("black");
4176 GCPRO1 (black);
4177 f->output_data.x->foreground_pixel
4178 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4179 f->output_data.x->background_pixel
4180 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4181 f->output_data.x->cursor_pixel
4182 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4183 f->output_data.x->cursor_foreground_pixel
4184 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4185 f->output_data.x->border_pixel
4186 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4187 f->output_data.x->mouse_pixel
4188 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4189 UNGCPRO;
4190 }
4191
4192 /* Specify the parent under which to make this X window. */
4193
4194 if (!NILP (parent))
4195 {
4196 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
4197 f->output_data.x->explicit_parent = 1;
4198 }
4199 else
4200 {
4201 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4202 f->output_data.x->explicit_parent = 0;
4203 }
4204
4205 /* Set the name; the functions to which we pass f expect the name to
4206 be set. */
4207 if (EQ (name, Qunbound) || NILP (name))
4208 {
4209 f->name = build_string (dpyinfo->x_id_name);
4210 f->explicit_name = 0;
4211 }
4212 else
4213 {
4214 f->name = name;
4215 f->explicit_name = 1;
4216 /* use the frame's title when getting resources for this frame. */
4217 specbind (Qx_resource_name, name);
4218 }
4219
4220 /* Extract the window parameters from the supplied values
4221 that are needed to determine window geometry. */
4222 {
4223 Lisp_Object font;
4224
4225 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4226
4227 BLOCK_INPUT;
4228 /* First, try whatever font the caller has specified. */
4229 if (STRINGP (font))
4230 {
4231 tem = Fquery_fontset (font, Qnil);
4232 if (STRINGP (tem))
4233 font = x_new_fontset (f, XSTRING (tem)->data);
4234 else
4235 font = x_new_font (f, XSTRING (font)->data);
4236 }
4237
4238 /* Try out a font which we hope has bold and italic variations. */
4239 if (!STRINGP (font))
4240 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
4241 if (!STRINGP (font))
4242 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4243 if (! STRINGP (font))
4244 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4245 if (! STRINGP (font))
4246 /* This was formerly the first thing tried, but it finds too many fonts
4247 and takes too long. */
4248 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
4249 /* If those didn't work, look for something which will at least work. */
4250 if (! STRINGP (font))
4251 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
4252 UNBLOCK_INPUT;
4253 if (! STRINGP (font))
4254 font = build_string ("fixed");
4255
4256 x_default_parameter (f, parms, Qfont, font,
4257 "font", "Font", RES_TYPE_STRING);
4258 }
4259
4260 #ifdef USE_LUCID
4261 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
4262 whereby it fails to get any font. */
4263 xlwmenu_default_font = f->output_data.x->font;
4264 #endif
4265
4266 x_default_parameter (f, parms, Qborder_width, make_number (2),
4267 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4268
4269 /* This defaults to 2 in order to match xterm. We recognize either
4270 internalBorderWidth or internalBorder (which is what xterm calls
4271 it). */
4272 if (NILP (Fassq (Qinternal_border_width, parms)))
4273 {
4274 Lisp_Object value;
4275
4276 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4277 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4278 if (! EQ (value, Qunbound))
4279 parms = Fcons (Fcons (Qinternal_border_width, value),
4280 parms);
4281 }
4282 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4283 "internalBorderWidth", "internalBorderWidth",
4284 RES_TYPE_NUMBER);
4285 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
4286 "verticalScrollBars", "ScrollBars",
4287 RES_TYPE_SYMBOL);
4288
4289 /* Also do the stuff which must be set before the window exists. */
4290 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4291 "foreground", "Foreground", RES_TYPE_STRING);
4292 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4293 "background", "Background", RES_TYPE_STRING);
4294 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4295 "pointerColor", "Foreground", RES_TYPE_STRING);
4296 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4297 "cursorColor", "Foreground", RES_TYPE_STRING);
4298 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4299 "borderColor", "BorderColor", RES_TYPE_STRING);
4300 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
4301 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4302 x_default_parameter (f, parms, Qline_spacing, Qnil,
4303 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4304
4305 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
4306 "scrollBarForeground",
4307 "ScrollBarForeground", 1);
4308 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
4309 "scrollBarBackground",
4310 "ScrollBarBackground", 0);
4311
4312 /* Init faces before x_default_parameter is called for scroll-bar
4313 parameters because that function calls x_set_scroll_bar_width,
4314 which calls change_frame_size, which calls Fset_window_buffer,
4315 which runs hooks, which call Fvertical_motion. At the end, we
4316 end up in init_iterator with a null face cache, which should not
4317 happen. */
4318 init_frame_faces (f);
4319
4320 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
4321 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4322 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
4323 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4324 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
4325 "bufferPredicate", "BufferPredicate",
4326 RES_TYPE_SYMBOL);
4327 x_default_parameter (f, parms, Qtitle, Qnil,
4328 "title", "Title", RES_TYPE_STRING);
4329
4330 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4331 window_prompting = x_figure_window_size (f, parms);
4332
4333 if (window_prompting & XNegative)
4334 {
4335 if (window_prompting & YNegative)
4336 f->output_data.x->win_gravity = SouthEastGravity;
4337 else
4338 f->output_data.x->win_gravity = NorthEastGravity;
4339 }
4340 else
4341 {
4342 if (window_prompting & YNegative)
4343 f->output_data.x->win_gravity = SouthWestGravity;
4344 else
4345 f->output_data.x->win_gravity = NorthWestGravity;
4346 }
4347
4348 f->output_data.x->size_hint_flags = window_prompting;
4349
4350 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4351 f->no_split = minibuffer_only || EQ (tem, Qt);
4352
4353 /* Create the X widget or window. Add the tool-bar height to the
4354 initial frame height so that the user gets a text display area of
4355 the size he specified with -g or via .Xdefaults. Later changes
4356 of the tool-bar height don't change the frame size. This is done
4357 so that users can create tall Emacs frames without having to
4358 guess how tall the tool-bar will get. */
4359 f->height += FRAME_TOOL_BAR_LINES (f);
4360
4361 #ifdef USE_X_TOOLKIT
4362 x_window (f, window_prompting, minibuffer_only);
4363 #else
4364 x_window (f);
4365 #endif
4366
4367 x_icon (f, parms);
4368 x_make_gc (f);
4369
4370 /* Now consider the frame official. */
4371 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4372 Vframe_list = Fcons (frame, Vframe_list);
4373
4374 /* We need to do this after creating the X window, so that the
4375 icon-creation functions can say whose icon they're describing. */
4376 x_default_parameter (f, parms, Qicon_type, Qnil,
4377 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4378
4379 x_default_parameter (f, parms, Qauto_raise, Qnil,
4380 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4381 x_default_parameter (f, parms, Qauto_lower, Qnil,
4382 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4383 x_default_parameter (f, parms, Qcursor_type, Qbox,
4384 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4385 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
4386 "scrollBarWidth", "ScrollBarWidth",
4387 RES_TYPE_NUMBER);
4388
4389 /* Dimensions, especially f->height, must be done via change_frame_size.
4390 Change will not be effected unless different from the current
4391 f->height. */
4392 width = f->width;
4393 height = f->height;
4394 f->height = 0;
4395 SET_FRAME_WIDTH (f, 0);
4396 change_frame_size (f, height, width, 1, 0, 0);
4397
4398 /* Set up faces after all frame parameters are known. This call
4399 also merges in face attributes specified for new frames. If we
4400 don't do this, the `menu' face for instance won't have the right
4401 colors, and the menu bar won't appear in the specified colors for
4402 new frames. */
4403 call1 (Qface_set_after_frame_default, frame);
4404
4405 #ifdef USE_X_TOOLKIT
4406 /* Create the menu bar. */
4407 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4408 {
4409 /* If this signals an error, we haven't set size hints for the
4410 frame and we didn't make it visible. */
4411 initialize_frame_menubar (f);
4412
4413 /* This is a no-op, except under Motif where it arranges the
4414 main window for the widgets on it. */
4415 lw_set_main_areas (f->output_data.x->column_widget,
4416 f->output_data.x->menubar_widget,
4417 f->output_data.x->edit_widget);
4418 }
4419 #endif /* USE_X_TOOLKIT */
4420
4421 /* Tell the server what size and position, etc, we want, and how
4422 badly we want them. This should be done after we have the menu
4423 bar so that its size can be taken into account. */
4424 BLOCK_INPUT;
4425 x_wm_set_size_hint (f, window_prompting, 0);
4426 UNBLOCK_INPUT;
4427
4428 /* Make the window appear on the frame and enable display, unless
4429 the caller says not to. However, with explicit parent, Emacs
4430 cannot control visibility, so don't try. */
4431 if (! f->output_data.x->explicit_parent)
4432 {
4433 Lisp_Object visibility;
4434
4435 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
4436 RES_TYPE_SYMBOL);
4437 if (EQ (visibility, Qunbound))
4438 visibility = Qt;
4439
4440 if (EQ (visibility, Qicon))
4441 x_iconify_frame (f);
4442 else if (! NILP (visibility))
4443 x_make_frame_visible (f);
4444 else
4445 /* Must have been Qnil. */
4446 ;
4447 }
4448
4449 UNGCPRO;
4450 return unbind_to (count, frame);
4451 }
4452
4453
4454 /* FRAME is used only to get a handle on the X display. We don't pass the
4455 display info directly because we're called from frame.c, which doesn't
4456 know about that structure. */
4457
4458 Lisp_Object
4459 x_get_focus_frame (frame)
4460 struct frame *frame;
4461 {
4462 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
4463 Lisp_Object xfocus;
4464 if (! dpyinfo->x_focus_frame)
4465 return Qnil;
4466
4467 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
4468 return xfocus;
4469 }
4470
4471
4472 /* In certain situations, when the window manager follows a
4473 click-to-focus policy, there seems to be no way around calling
4474 XSetInputFocus to give another frame the input focus .
4475
4476 In an ideal world, XSetInputFocus should generally be avoided so
4477 that applications don't interfere with the window manager's focus
4478 policy. But I think it's okay to use when it's clearly done
4479 following a user-command. */
4480
4481 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4482 "Set the input focus to FRAME.\n\
4483 FRAME nil means use the selected frame.")
4484 (frame)
4485 Lisp_Object frame;
4486 {
4487 struct frame *f = check_x_frame (frame);
4488 Display *dpy = FRAME_X_DISPLAY (f);
4489 int count;
4490
4491 BLOCK_INPUT;
4492 count = x_catch_errors (dpy);
4493 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4494 RevertToParent, CurrentTime);
4495 x_uncatch_errors (dpy, count);
4496 UNBLOCK_INPUT;
4497
4498 return Qnil;
4499 }
4500
4501 \f
4502 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4503 "Internal function called by `color-defined-p', which see.")
4504 (color, frame)
4505 Lisp_Object color, frame;
4506 {
4507 XColor foo;
4508 FRAME_PTR f = check_x_frame (frame);
4509
4510 CHECK_STRING (color, 1);
4511
4512 if (x_defined_color (f, XSTRING (color)->data, &foo, 0))
4513 return Qt;
4514 else
4515 return Qnil;
4516 }
4517
4518 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4519 "Internal function called by `color-values', which see.")
4520 (color, frame)
4521 Lisp_Object color, frame;
4522 {
4523 XColor foo;
4524 FRAME_PTR f = check_x_frame (frame);
4525
4526 CHECK_STRING (color, 1);
4527
4528 if (x_defined_color (f, XSTRING (color)->data, &foo, 0))
4529 {
4530 Lisp_Object rgb[3];
4531
4532 rgb[0] = make_number (foo.red);
4533 rgb[1] = make_number (foo.green);
4534 rgb[2] = make_number (foo.blue);
4535 return Flist (3, rgb);
4536 }
4537 else
4538 return Qnil;
4539 }
4540
4541 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4542 "Internal function called by `display-color-p', which see.")
4543 (display)
4544 Lisp_Object display;
4545 {
4546 struct x_display_info *dpyinfo = check_x_display_info (display);
4547
4548 if (dpyinfo->n_planes <= 2)
4549 return Qnil;
4550
4551 switch (dpyinfo->visual->class)
4552 {
4553 case StaticColor:
4554 case PseudoColor:
4555 case TrueColor:
4556 case DirectColor:
4557 return Qt;
4558
4559 default:
4560 return Qnil;
4561 }
4562 }
4563
4564 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
4565 0, 1, 0,
4566 "Return t if the X display supports shades of gray.\n\
4567 Note that color displays do support shades of gray.\n\
4568 The optional argument DISPLAY specifies which display to ask about.\n\
4569 DISPLAY should be either a frame or a display name (a string).\n\
4570 If omitted or nil, that stands for the selected frame's display.")
4571 (display)
4572 Lisp_Object display;
4573 {
4574 struct x_display_info *dpyinfo = check_x_display_info (display);
4575
4576 if (dpyinfo->n_planes <= 1)
4577 return Qnil;
4578
4579 switch (dpyinfo->visual->class)
4580 {
4581 case StaticColor:
4582 case PseudoColor:
4583 case TrueColor:
4584 case DirectColor:
4585 case StaticGray:
4586 case GrayScale:
4587 return Qt;
4588
4589 default:
4590 return Qnil;
4591 }
4592 }
4593
4594 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
4595 0, 1, 0,
4596 "Returns the width in pixels of the X display DISPLAY.\n\
4597 The optional argument DISPLAY specifies which display to ask about.\n\
4598 DISPLAY should be either a frame or a display name (a string).\n\
4599 If omitted or nil, that stands for the selected frame's display.")
4600 (display)
4601 Lisp_Object display;
4602 {
4603 struct x_display_info *dpyinfo = check_x_display_info (display);
4604
4605 return make_number (dpyinfo->width);
4606 }
4607
4608 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4609 Sx_display_pixel_height, 0, 1, 0,
4610 "Returns the height in pixels of the X display DISPLAY.\n\
4611 The optional argument DISPLAY specifies which display to ask about.\n\
4612 DISPLAY should be either a frame or a display name (a string).\n\
4613 If omitted or nil, that stands for the selected frame's display.")
4614 (display)
4615 Lisp_Object display;
4616 {
4617 struct x_display_info *dpyinfo = check_x_display_info (display);
4618
4619 return make_number (dpyinfo->height);
4620 }
4621
4622 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4623 0, 1, 0,
4624 "Returns the number of bitplanes of the X display DISPLAY.\n\
4625 The optional argument DISPLAY specifies which display to ask about.\n\
4626 DISPLAY should be either a frame or a display name (a string).\n\
4627 If omitted or nil, that stands for the selected frame's display.")
4628 (display)
4629 Lisp_Object display;
4630 {
4631 struct x_display_info *dpyinfo = check_x_display_info (display);
4632
4633 return make_number (dpyinfo->n_planes);
4634 }
4635
4636 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4637 0, 1, 0,
4638 "Returns the number of color cells of the X display DISPLAY.\n\
4639 The optional argument DISPLAY specifies which display to ask about.\n\
4640 DISPLAY should be either a frame or a display name (a string).\n\
4641 If omitted or nil, that stands for the selected frame's display.")
4642 (display)
4643 Lisp_Object display;
4644 {
4645 struct x_display_info *dpyinfo = check_x_display_info (display);
4646
4647 return make_number (DisplayCells (dpyinfo->display,
4648 XScreenNumberOfScreen (dpyinfo->screen)));
4649 }
4650
4651 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4652 Sx_server_max_request_size,
4653 0, 1, 0,
4654 "Returns the maximum request size of the X server of display DISPLAY.\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;
4660 {
4661 struct x_display_info *dpyinfo = check_x_display_info (display);
4662
4663 return make_number (MAXREQUEST (dpyinfo->display));
4664 }
4665
4666 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4667 "Returns the vendor ID string of the X server of display DISPLAY.\n\
4668 The optional argument DISPLAY specifies which display to ask about.\n\
4669 DISPLAY should be either a frame or a display name (a string).\n\
4670 If omitted or nil, that stands for the selected frame's display.")
4671 (display)
4672 Lisp_Object display;
4673 {
4674 struct x_display_info *dpyinfo = check_x_display_info (display);
4675 char *vendor = ServerVendor (dpyinfo->display);
4676
4677 if (! vendor) vendor = "";
4678 return build_string (vendor);
4679 }
4680
4681 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4682 "Returns the version numbers of the X server of display DISPLAY.\n\
4683 The value is a list of three integers: the major and minor\n\
4684 version numbers of the X Protocol in use, and the vendor-specific release\n\
4685 number. See also the function `x-server-vendor'.\n\n\
4686 The optional argument DISPLAY specifies which display to ask about.\n\
4687 DISPLAY should be either a frame or a display name (a string).\n\
4688 If omitted or nil, that stands for the selected frame's display.")
4689 (display)
4690 Lisp_Object display;
4691 {
4692 struct x_display_info *dpyinfo = check_x_display_info (display);
4693 Display *dpy = dpyinfo->display;
4694
4695 return Fcons (make_number (ProtocolVersion (dpy)),
4696 Fcons (make_number (ProtocolRevision (dpy)),
4697 Fcons (make_number (VendorRelease (dpy)), Qnil)));
4698 }
4699
4700 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4701 "Returns the number of screens on the X server of display DISPLAY.\n\
4702 The optional argument DISPLAY specifies which display to ask about.\n\
4703 DISPLAY should be either a frame or a display name (a string).\n\
4704 If omitted or nil, that stands for the selected frame's display.")
4705 (display)
4706 Lisp_Object display;
4707 {
4708 struct x_display_info *dpyinfo = check_x_display_info (display);
4709
4710 return make_number (ScreenCount (dpyinfo->display));
4711 }
4712
4713 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
4714 "Returns the height in millimeters of the X display DISPLAY.\n\
4715 The optional argument DISPLAY specifies which display to ask about.\n\
4716 DISPLAY should be either a frame or a display name (a string).\n\
4717 If omitted or nil, that stands for the selected frame's display.")
4718 (display)
4719 Lisp_Object display;
4720 {
4721 struct x_display_info *dpyinfo = check_x_display_info (display);
4722
4723 return make_number (HeightMMOfScreen (dpyinfo->screen));
4724 }
4725
4726 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4727 "Returns the width in millimeters of the X display DISPLAY.\n\
4728 The optional argument DISPLAY specifies which display to ask about.\n\
4729 DISPLAY should be either a frame or a display name (a string).\n\
4730 If omitted or nil, that stands for the selected frame's display.")
4731 (display)
4732 Lisp_Object display;
4733 {
4734 struct x_display_info *dpyinfo = check_x_display_info (display);
4735
4736 return make_number (WidthMMOfScreen (dpyinfo->screen));
4737 }
4738
4739 DEFUN ("x-display-backing-store", Fx_display_backing_store,
4740 Sx_display_backing_store, 0, 1, 0,
4741 "Returns an indication of whether X display DISPLAY does backing store.\n\
4742 The value may be `always', `when-mapped', or `not-useful'.\n\
4743 The optional argument DISPLAY specifies which display to ask about.\n\
4744 DISPLAY should be either a frame or a display name (a string).\n\
4745 If omitted or nil, that stands for the selected frame's display.")
4746 (display)
4747 Lisp_Object display;
4748 {
4749 struct x_display_info *dpyinfo = check_x_display_info (display);
4750 Lisp_Object result;
4751
4752 switch (DoesBackingStore (dpyinfo->screen))
4753 {
4754 case Always:
4755 result = intern ("always");
4756 break;
4757
4758 case WhenMapped:
4759 result = intern ("when-mapped");
4760 break;
4761
4762 case NotUseful:
4763 result = intern ("not-useful");
4764 break;
4765
4766 default:
4767 error ("Strange value for BackingStore parameter of screen");
4768 result = Qnil;
4769 }
4770
4771 return result;
4772 }
4773
4774 DEFUN ("x-display-visual-class", Fx_display_visual_class,
4775 Sx_display_visual_class, 0, 1, 0,
4776 "Returns the visual class of the X display DISPLAY.\n\
4777 The value is one of the symbols `static-gray', `gray-scale',\n\
4778 `static-color', `pseudo-color', `true-color', or `direct-color'.\n\n\
4779 The optional argument DISPLAY specifies which display to ask about.\n\
4780 DISPLAY should be either a frame or a display name (a string).\n\
4781 If omitted or nil, that stands for the selected frame's display.")
4782 (display)
4783 Lisp_Object display;
4784 {
4785 struct x_display_info *dpyinfo = check_x_display_info (display);
4786 Lisp_Object result;
4787
4788 switch (dpyinfo->visual->class)
4789 {
4790 case StaticGray:
4791 result = intern ("static-gray");
4792 break;
4793 case GrayScale:
4794 result = intern ("gray-scale");
4795 break;
4796 case StaticColor:
4797 result = intern ("static-color");
4798 break;
4799 case PseudoColor:
4800 result = intern ("pseudo-color");
4801 break;
4802 case TrueColor:
4803 result = intern ("true-color");
4804 break;
4805 case DirectColor:
4806 result = intern ("direct-color");
4807 break;
4808 default:
4809 error ("Display has an unknown visual class");
4810 result = Qnil;
4811 }
4812
4813 return result;
4814 }
4815
4816 DEFUN ("x-display-save-under", Fx_display_save_under,
4817 Sx_display_save_under, 0, 1, 0,
4818 "Returns t if the X display DISPLAY supports the save-under feature.\n\
4819 The optional argument DISPLAY specifies which display to ask about.\n\
4820 DISPLAY should be either a frame or a display name (a string).\n\
4821 If omitted or nil, that stands for the selected frame's display.")
4822 (display)
4823 Lisp_Object display;
4824 {
4825 struct x_display_info *dpyinfo = check_x_display_info (display);
4826
4827 if (DoesSaveUnders (dpyinfo->screen) == True)
4828 return Qt;
4829 else
4830 return Qnil;
4831 }
4832 \f
4833 int
4834 x_pixel_width (f)
4835 register struct frame *f;
4836 {
4837 return PIXEL_WIDTH (f);
4838 }
4839
4840 int
4841 x_pixel_height (f)
4842 register struct frame *f;
4843 {
4844 return PIXEL_HEIGHT (f);
4845 }
4846
4847 int
4848 x_char_width (f)
4849 register struct frame *f;
4850 {
4851 return FONT_WIDTH (f->output_data.x->font);
4852 }
4853
4854 int
4855 x_char_height (f)
4856 register struct frame *f;
4857 {
4858 return f->output_data.x->line_height;
4859 }
4860
4861 int
4862 x_screen_planes (f)
4863 register struct frame *f;
4864 {
4865 return FRAME_X_DISPLAY_INFO (f)->n_planes;
4866 }
4867
4868
4869 \f
4870 /************************************************************************
4871 X Displays
4872 ************************************************************************/
4873
4874 \f
4875 /* Mapping visual names to visuals. */
4876
4877 static struct visual_class
4878 {
4879 char *name;
4880 int class;
4881 }
4882 visual_classes[] =
4883 {
4884 {"StaticGray", StaticGray},
4885 {"GrayScale", GrayScale},
4886 {"StaticColor", StaticColor},
4887 {"PseudoColor", PseudoColor},
4888 {"TrueColor", TrueColor},
4889 {"DirectColor", DirectColor},
4890 NULL
4891 };
4892
4893
4894 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4895
4896 /* Value is the screen number of screen SCR. This is a substitute for
4897 the X function with the same name when that doesn't exist. */
4898
4899 int
4900 XScreenNumberOfScreen (scr)
4901 register Screen *scr;
4902 {
4903 Display *dpy = scr->display;
4904 int i;
4905
4906 for (i = 0; i < dpy->nscreens; ++i)
4907 if (scr == dpy->screens[i])
4908 break;
4909
4910 return i;
4911 }
4912
4913 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4914
4915
4916 /* Select the visual that should be used on display DPYINFO. Set
4917 members of DPYINFO appropriately. Called from x_term_init. */
4918
4919 void
4920 select_visual (dpyinfo)
4921 struct x_display_info *dpyinfo;
4922 {
4923 Display *dpy = dpyinfo->display;
4924 Screen *screen = dpyinfo->screen;
4925 Lisp_Object value;
4926
4927 /* See if a visual is specified. */
4928 value = display_x_get_resource (dpyinfo,
4929 build_string ("visualClass"),
4930 build_string ("VisualClass"),
4931 Qnil, Qnil);
4932 if (STRINGP (value))
4933 {
4934 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4935 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4936 depth, a decimal number. NAME is compared with case ignored. */
4937 char *s = (char *) alloca (STRING_BYTES (XSTRING (value)) + 1);
4938 char *dash;
4939 int i, class = -1;
4940 XVisualInfo vinfo;
4941
4942 strcpy (s, XSTRING (value)->data);
4943 dash = index (s, '-');
4944 if (dash)
4945 {
4946 dpyinfo->n_planes = atoi (dash + 1);
4947 *dash = '\0';
4948 }
4949 else
4950 /* We won't find a matching visual with depth 0, so that
4951 an error will be printed below. */
4952 dpyinfo->n_planes = 0;
4953
4954 /* Determine the visual class. */
4955 for (i = 0; visual_classes[i].name; ++i)
4956 if (xstricmp (s, visual_classes[i].name) == 0)
4957 {
4958 class = visual_classes[i].class;
4959 break;
4960 }
4961
4962 /* Look up a matching visual for the specified class. */
4963 if (class == -1
4964 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4965 dpyinfo->n_planes, class, &vinfo))
4966 fatal ("Invalid visual specification `%s'", XSTRING (value)->data);
4967
4968 dpyinfo->visual = vinfo.visual;
4969 }
4970 else
4971 {
4972 int n_visuals;
4973 XVisualInfo *vinfo, vinfo_template;
4974
4975 dpyinfo->visual = DefaultVisualOfScreen (screen);
4976
4977 #ifdef HAVE_X11R4
4978 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4979 #else
4980 vinfo_template.visualid = dpyinfo->visual->visualid;
4981 #endif
4982 vinfo_template.screen = XScreenNumberOfScreen (screen);
4983 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4984 &vinfo_template, &n_visuals);
4985 if (n_visuals != 1)
4986 fatal ("Can't get proper X visual info");
4987
4988 dpyinfo->n_planes = vinfo->depth;
4989 XFree ((char *) vinfo);
4990 }
4991 }
4992
4993
4994 /* Return the X display structure for the display named NAME.
4995 Open a new connection if necessary. */
4996
4997 struct x_display_info *
4998 x_display_info_for_name (name)
4999 Lisp_Object name;
5000 {
5001 Lisp_Object names;
5002 struct x_display_info *dpyinfo;
5003
5004 CHECK_STRING (name, 0);
5005
5006 if (! EQ (Vwindow_system, intern ("x")))
5007 error ("Not using X Windows");
5008
5009 for (dpyinfo = x_display_list, names = x_display_name_list;
5010 dpyinfo;
5011 dpyinfo = dpyinfo->next, names = XCDR (names))
5012 {
5013 Lisp_Object tem;
5014 tem = Fstring_equal (XCAR (XCAR (names)), name);
5015 if (!NILP (tem))
5016 return dpyinfo;
5017 }
5018
5019 /* Use this general default value to start with. */
5020 Vx_resource_name = Vinvocation_name;
5021
5022 validate_x_resource_name ();
5023
5024 dpyinfo = x_term_init (name, (unsigned char *)0,
5025 (char *) XSTRING (Vx_resource_name)->data);
5026
5027 if (dpyinfo == 0)
5028 error ("Cannot connect to X server %s", XSTRING (name)->data);
5029
5030 x_in_use = 1;
5031 XSETFASTINT (Vwindow_system_version, 11);
5032
5033 return dpyinfo;
5034 }
5035
5036
5037 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
5038 1, 3, 0, "Open a connection to an X server.\n\
5039 DISPLAY is the name of the display to connect to.\n\
5040 Optional second arg XRM-STRING is a string of resources in xrdb format.\n\
5041 If the optional third arg MUST-SUCCEED is non-nil,\n\
5042 terminate Emacs if we can't open the connection.")
5043 (display, xrm_string, must_succeed)
5044 Lisp_Object display, xrm_string, must_succeed;
5045 {
5046 unsigned char *xrm_option;
5047 struct x_display_info *dpyinfo;
5048
5049 CHECK_STRING (display, 0);
5050 if (! NILP (xrm_string))
5051 CHECK_STRING (xrm_string, 1);
5052
5053 if (! EQ (Vwindow_system, intern ("x")))
5054 error ("Not using X Windows");
5055
5056 if (! NILP (xrm_string))
5057 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
5058 else
5059 xrm_option = (unsigned char *) 0;
5060
5061 validate_x_resource_name ();
5062
5063 /* This is what opens the connection and sets x_current_display.
5064 This also initializes many symbols, such as those used for input. */
5065 dpyinfo = x_term_init (display, xrm_option,
5066 (char *) XSTRING (Vx_resource_name)->data);
5067
5068 if (dpyinfo == 0)
5069 {
5070 if (!NILP (must_succeed))
5071 fatal ("Cannot connect to X server %s.\n\
5072 Check the DISPLAY environment variable or use `-d'.\n\
5073 Also use the `xhost' program to verify that it is set to permit\n\
5074 connections from your machine.\n",
5075 XSTRING (display)->data);
5076 else
5077 error ("Cannot connect to X server %s", XSTRING (display)->data);
5078 }
5079
5080 x_in_use = 1;
5081
5082 XSETFASTINT (Vwindow_system_version, 11);
5083 return Qnil;
5084 }
5085
5086 DEFUN ("x-close-connection", Fx_close_connection,
5087 Sx_close_connection, 1, 1, 0,
5088 "Close the connection to DISPLAY's X server.\n\
5089 For DISPLAY, specify either a frame or a display name (a string).\n\
5090 If DISPLAY is nil, that stands for the selected frame's display.")
5091 (display)
5092 Lisp_Object display;
5093 {
5094 struct x_display_info *dpyinfo = check_x_display_info (display);
5095 int i;
5096
5097 if (dpyinfo->reference_count > 0)
5098 error ("Display still has frames on it");
5099
5100 BLOCK_INPUT;
5101 /* Free the fonts in the font table. */
5102 for (i = 0; i < dpyinfo->n_fonts; i++)
5103 if (dpyinfo->font_table[i].name)
5104 {
5105 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
5106 xfree (dpyinfo->font_table[i].full_name);
5107 xfree (dpyinfo->font_table[i].name);
5108 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
5109 }
5110
5111 x_destroy_all_bitmaps (dpyinfo);
5112 XSetCloseDownMode (dpyinfo->display, DestroyAll);
5113
5114 #ifdef USE_X_TOOLKIT
5115 XtCloseDisplay (dpyinfo->display);
5116 #else
5117 XCloseDisplay (dpyinfo->display);
5118 #endif
5119
5120 x_delete_display (dpyinfo);
5121 UNBLOCK_INPUT;
5122
5123 return Qnil;
5124 }
5125
5126 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
5127 "Return the list of display names that Emacs has connections to.")
5128 ()
5129 {
5130 Lisp_Object tail, result;
5131
5132 result = Qnil;
5133 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
5134 result = Fcons (XCAR (XCAR (tail)), result);
5135
5136 return result;
5137 }
5138
5139 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
5140 "If ON is non-nil, report X errors as soon as the erring request is made.\n\
5141 If ON is nil, allow buffering of requests.\n\
5142 Turning on synchronization prohibits the Xlib routines from buffering\n\
5143 requests and seriously degrades performance, but makes debugging much\n\
5144 easier.\n\
5145 The optional second argument DISPLAY specifies which display to act on.\n\
5146 DISPLAY should be either a frame or a display name (a string).\n\
5147 If DISPLAY is omitted or nil, that stands for the selected frame's display.")
5148 (on, display)
5149 Lisp_Object display, on;
5150 {
5151 struct x_display_info *dpyinfo = check_x_display_info (display);
5152
5153 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
5154
5155 return Qnil;
5156 }
5157
5158 /* Wait for responses to all X commands issued so far for frame F. */
5159
5160 void
5161 x_sync (f)
5162 FRAME_PTR f;
5163 {
5164 BLOCK_INPUT;
5165 XSync (FRAME_X_DISPLAY (f), False);
5166 UNBLOCK_INPUT;
5167 }
5168
5169 \f
5170 /***********************************************************************
5171 Image types
5172 ***********************************************************************/
5173
5174 /* Value is the number of elements of vector VECTOR. */
5175
5176 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
5177
5178 /* List of supported image types. Use define_image_type to add new
5179 types. Use lookup_image_type to find a type for a given symbol. */
5180
5181 static struct image_type *image_types;
5182
5183 /* The symbol `image' which is the car of the lists used to represent
5184 images in Lisp. */
5185
5186 extern Lisp_Object Qimage;
5187
5188 /* The symbol `xbm' which is used as the type symbol for XBM images. */
5189
5190 Lisp_Object Qxbm;
5191
5192 /* Keywords. */
5193
5194 extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
5195 extern Lisp_Object QCdata;
5196 Lisp_Object QCtype, QCascent, QCmargin, QCrelief;
5197 Lisp_Object QCalgorithm, QCcolor_symbols, QCheuristic_mask;
5198 Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
5199
5200 /* Other symbols. */
5201
5202 Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
5203
5204 /* Time in seconds after which images should be removed from the cache
5205 if not displayed. */
5206
5207 Lisp_Object Vimage_cache_eviction_delay;
5208
5209 /* Function prototypes. */
5210
5211 static void define_image_type P_ ((struct image_type *type));
5212 static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
5213 static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
5214 static void x_laplace P_ ((struct frame *, struct image *));
5215 static void x_emboss P_ ((struct frame *, struct image *));
5216 static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
5217 Lisp_Object));
5218
5219
5220 /* Define a new image type from TYPE. This adds a copy of TYPE to
5221 image_types and adds the symbol *TYPE->type to Vimage_types. */
5222
5223 static void
5224 define_image_type (type)
5225 struct image_type *type;
5226 {
5227 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
5228 The initialized data segment is read-only. */
5229 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
5230 bcopy (type, p, sizeof *p);
5231 p->next = image_types;
5232 image_types = p;
5233 Vimage_types = Fcons (*p->type, Vimage_types);
5234 }
5235
5236
5237 /* Look up image type SYMBOL, and return a pointer to its image_type
5238 structure. Value is null if SYMBOL is not a known image type. */
5239
5240 static INLINE struct image_type *
5241 lookup_image_type (symbol)
5242 Lisp_Object symbol;
5243 {
5244 struct image_type *type;
5245
5246 for (type = image_types; type; type = type->next)
5247 if (EQ (symbol, *type->type))
5248 break;
5249
5250 return type;
5251 }
5252
5253
5254 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
5255 valid image specification is a list whose car is the symbol
5256 `image', and whose rest is a property list. The property list must
5257 contain a value for key `:type'. That value must be the name of a
5258 supported image type. The rest of the property list depends on the
5259 image type. */
5260
5261 int
5262 valid_image_p (object)
5263 Lisp_Object object;
5264 {
5265 int valid_p = 0;
5266
5267 if (CONSP (object) && EQ (XCAR (object), Qimage))
5268 {
5269 Lisp_Object symbol = Fplist_get (XCDR (object), QCtype);
5270 struct image_type *type = lookup_image_type (symbol);
5271
5272 if (type)
5273 valid_p = type->valid_p (object);
5274 }
5275
5276 return valid_p;
5277 }
5278
5279
5280 /* Log error message with format string FORMAT and argument ARG.
5281 Signaling an error, e.g. when an image cannot be loaded, is not a
5282 good idea because this would interrupt redisplay, and the error
5283 message display would lead to another redisplay. This function
5284 therefore simply displays a message. */
5285
5286 static void
5287 image_error (format, arg1, arg2)
5288 char *format;
5289 Lisp_Object arg1, arg2;
5290 {
5291 add_to_log (format, arg1, arg2);
5292 }
5293
5294
5295 \f
5296 /***********************************************************************
5297 Image specifications
5298 ***********************************************************************/
5299
5300 enum image_value_type
5301 {
5302 IMAGE_DONT_CHECK_VALUE_TYPE,
5303 IMAGE_STRING_VALUE,
5304 IMAGE_SYMBOL_VALUE,
5305 IMAGE_POSITIVE_INTEGER_VALUE,
5306 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
5307 IMAGE_ASCENT_VALUE,
5308 IMAGE_INTEGER_VALUE,
5309 IMAGE_FUNCTION_VALUE,
5310 IMAGE_NUMBER_VALUE,
5311 IMAGE_BOOL_VALUE
5312 };
5313
5314 /* Structure used when parsing image specifications. */
5315
5316 struct image_keyword
5317 {
5318 /* Name of keyword. */
5319 char *name;
5320
5321 /* The type of value allowed. */
5322 enum image_value_type type;
5323
5324 /* Non-zero means key must be present. */
5325 int mandatory_p;
5326
5327 /* Used to recognize duplicate keywords in a property list. */
5328 int count;
5329
5330 /* The value that was found. */
5331 Lisp_Object value;
5332 };
5333
5334
5335 static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
5336 int, Lisp_Object));
5337 static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
5338
5339
5340 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
5341 has the format (image KEYWORD VALUE ...). One of the keyword/
5342 value pairs must be `:type TYPE'. KEYWORDS is a vector of
5343 image_keywords structures of size NKEYWORDS describing other
5344 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
5345
5346 static int
5347 parse_image_spec (spec, keywords, nkeywords, type)
5348 Lisp_Object spec;
5349 struct image_keyword *keywords;
5350 int nkeywords;
5351 Lisp_Object type;
5352 {
5353 int i;
5354 Lisp_Object plist;
5355
5356 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
5357 return 0;
5358
5359 plist = XCDR (spec);
5360 while (CONSP (plist))
5361 {
5362 Lisp_Object key, value;
5363
5364 /* First element of a pair must be a symbol. */
5365 key = XCAR (plist);
5366 plist = XCDR (plist);
5367 if (!SYMBOLP (key))
5368 return 0;
5369
5370 /* There must follow a value. */
5371 if (!CONSP (plist))
5372 return 0;
5373 value = XCAR (plist);
5374 plist = XCDR (plist);
5375
5376 /* Find key in KEYWORDS. Error if not found. */
5377 for (i = 0; i < nkeywords; ++i)
5378 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
5379 break;
5380
5381 if (i == nkeywords)
5382 continue;
5383
5384 /* Record that we recognized the keyword. If a keywords
5385 was found more than once, it's an error. */
5386 keywords[i].value = value;
5387 ++keywords[i].count;
5388
5389 if (keywords[i].count > 1)
5390 return 0;
5391
5392 /* Check type of value against allowed type. */
5393 switch (keywords[i].type)
5394 {
5395 case IMAGE_STRING_VALUE:
5396 if (!STRINGP (value))
5397 return 0;
5398 break;
5399
5400 case IMAGE_SYMBOL_VALUE:
5401 if (!SYMBOLP (value))
5402 return 0;
5403 break;
5404
5405 case IMAGE_POSITIVE_INTEGER_VALUE:
5406 if (!INTEGERP (value) || XINT (value) <= 0)
5407 return 0;
5408 break;
5409
5410 case IMAGE_ASCENT_VALUE:
5411 if (SYMBOLP (value) && EQ (value, Qcenter))
5412 break;
5413 else if (INTEGERP (value)
5414 && XINT (value) >= 0
5415 && XINT (value) <= 100)
5416 break;
5417 return 0;
5418
5419 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
5420 if (!INTEGERP (value) || XINT (value) < 0)
5421 return 0;
5422 break;
5423
5424 case IMAGE_DONT_CHECK_VALUE_TYPE:
5425 break;
5426
5427 case IMAGE_FUNCTION_VALUE:
5428 value = indirect_function (value);
5429 if (SUBRP (value)
5430 || COMPILEDP (value)
5431 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
5432 break;
5433 return 0;
5434
5435 case IMAGE_NUMBER_VALUE:
5436 if (!INTEGERP (value) && !FLOATP (value))
5437 return 0;
5438 break;
5439
5440 case IMAGE_INTEGER_VALUE:
5441 if (!INTEGERP (value))
5442 return 0;
5443 break;
5444
5445 case IMAGE_BOOL_VALUE:
5446 if (!NILP (value) && !EQ (value, Qt))
5447 return 0;
5448 break;
5449
5450 default:
5451 abort ();
5452 break;
5453 }
5454
5455 if (EQ (key, QCtype) && !EQ (type, value))
5456 return 0;
5457 }
5458
5459 /* Check that all mandatory fields are present. */
5460 for (i = 0; i < nkeywords; ++i)
5461 if (keywords[i].mandatory_p && keywords[i].count == 0)
5462 return 0;
5463
5464 return NILP (plist);
5465 }
5466
5467
5468 /* Return the value of KEY in image specification SPEC. Value is nil
5469 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
5470 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
5471
5472 static Lisp_Object
5473 image_spec_value (spec, key, found)
5474 Lisp_Object spec, key;
5475 int *found;
5476 {
5477 Lisp_Object tail;
5478
5479 xassert (valid_image_p (spec));
5480
5481 for (tail = XCDR (spec);
5482 CONSP (tail) && CONSP (XCDR (tail));
5483 tail = XCDR (XCDR (tail)))
5484 {
5485 if (EQ (XCAR (tail), key))
5486 {
5487 if (found)
5488 *found = 1;
5489 return XCAR (XCDR (tail));
5490 }
5491 }
5492
5493 if (found)
5494 *found = 0;
5495 return Qnil;
5496 }
5497
5498
5499 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
5500 "Return the size of image SPEC as pair (WIDTH . HEIGHT).\n\
5501 PIXELS non-nil means return the size in pixels, otherwise return the\n\
5502 size in canonical character units.\n\
5503 FRAME is the frame on which the image will be displayed. FRAME nil\n\
5504 or omitted means use the selected frame.")
5505 (spec, pixels, frame)
5506 Lisp_Object spec, pixels, frame;
5507 {
5508 Lisp_Object size;
5509
5510 size = Qnil;
5511 if (valid_image_p (spec))
5512 {
5513 struct frame *f = check_x_frame (frame);
5514 int id = lookup_image (f, spec);
5515 struct image *img = IMAGE_FROM_ID (f, id);
5516 int width = img->width + 2 * img->margin;
5517 int height = img->height + 2 * img->margin;
5518
5519 if (NILP (pixels))
5520 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
5521 make_float ((double) height / CANON_Y_UNIT (f)));
5522 else
5523 size = Fcons (make_number (width), make_number (height));
5524 }
5525 else
5526 error ("Invalid image specification");
5527
5528 return size;
5529 }
5530
5531
5532 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
5533 "Return t if image SPEC has a mask bitmap.\n\
5534 FRAME is the frame on which the image will be displayed. FRAME nil\n\
5535 or omitted means use the selected frame.")
5536 (spec, frame)
5537 Lisp_Object spec, frame;
5538 {
5539 Lisp_Object mask;
5540
5541 mask = Qnil;
5542 if (valid_image_p (spec))
5543 {
5544 struct frame *f = check_x_frame (frame);
5545 int id = lookup_image (f, spec);
5546 struct image *img = IMAGE_FROM_ID (f, id);
5547 if (img->mask)
5548 mask = Qt;
5549 }
5550 else
5551 error ("Invalid image specification");
5552
5553 return mask;
5554 }
5555
5556
5557 \f
5558 /***********************************************************************
5559 Image type independent image structures
5560 ***********************************************************************/
5561
5562 static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
5563 static void free_image P_ ((struct frame *f, struct image *img));
5564
5565
5566 /* Allocate and return a new image structure for image specification
5567 SPEC. SPEC has a hash value of HASH. */
5568
5569 static struct image *
5570 make_image (spec, hash)
5571 Lisp_Object spec;
5572 unsigned hash;
5573 {
5574 struct image *img = (struct image *) xmalloc (sizeof *img);
5575
5576 xassert (valid_image_p (spec));
5577 bzero (img, sizeof *img);
5578 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
5579 xassert (img->type != NULL);
5580 img->spec = spec;
5581 img->data.lisp_val = Qnil;
5582 img->ascent = DEFAULT_IMAGE_ASCENT;
5583 img->hash = hash;
5584 return img;
5585 }
5586
5587
5588 /* Free image IMG which was used on frame F, including its resources. */
5589
5590 static void
5591 free_image (f, img)
5592 struct frame *f;
5593 struct image *img;
5594 {
5595 if (img)
5596 {
5597 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5598
5599 /* Remove IMG from the hash table of its cache. */
5600 if (img->prev)
5601 img->prev->next = img->next;
5602 else
5603 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
5604
5605 if (img->next)
5606 img->next->prev = img->prev;
5607
5608 c->images[img->id] = NULL;
5609
5610 /* Free resources, then free IMG. */
5611 img->type->free (f, img);
5612 xfree (img);
5613 }
5614 }
5615
5616
5617 /* Prepare image IMG for display on frame F. Must be called before
5618 drawing an image. */
5619
5620 void
5621 prepare_image_for_display (f, img)
5622 struct frame *f;
5623 struct image *img;
5624 {
5625 EMACS_TIME t;
5626
5627 /* We're about to display IMG, so set its timestamp to `now'. */
5628 EMACS_GET_TIME (t);
5629 img->timestamp = EMACS_SECS (t);
5630
5631 /* If IMG doesn't have a pixmap yet, load it now, using the image
5632 type dependent loader function. */
5633 if (img->pixmap == None && !img->load_failed_p)
5634 img->load_failed_p = img->type->load (f, img) == 0;
5635 }
5636
5637
5638 /* Value is the number of pixels for the ascent of image IMG when
5639 drawn in face FACE. */
5640
5641 int
5642 image_ascent (img, face)
5643 struct image *img;
5644 struct face *face;
5645 {
5646 int height = img->height + img->margin;
5647 int ascent;
5648
5649 if (img->ascent == CENTERED_IMAGE_ASCENT)
5650 {
5651 if (face->font)
5652 /* This expression is arranged so that if the image can't be
5653 exactly centered, it will be moved slightly up. This is
5654 because a typical font is `top-heavy' (due to the presence
5655 uppercase letters), so the image placement should err towards
5656 being top-heavy too. It also just generally looks better. */
5657 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
5658 else
5659 ascent = height / 2;
5660 }
5661 else
5662 ascent = height * img->ascent / 100.0;
5663
5664 return ascent;
5665 }
5666
5667
5668 \f
5669 /***********************************************************************
5670 Helper functions for X image types
5671 ***********************************************************************/
5672
5673 static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
5674 int, int));
5675 static void x_clear_image P_ ((struct frame *f, struct image *img));
5676 static unsigned long x_alloc_image_color P_ ((struct frame *f,
5677 struct image *img,
5678 Lisp_Object color_name,
5679 unsigned long dflt));
5680
5681
5682 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
5683 free the pixmap if any. MASK_P non-zero means clear the mask
5684 pixmap if any. COLORS_P non-zero means free colors allocated for
5685 the image, if any. */
5686
5687 static void
5688 x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
5689 struct frame *f;
5690 struct image *img;
5691 int pixmap_p, mask_p, colors_p;
5692 {
5693 if (pixmap_p && img->pixmap)
5694 {
5695 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
5696 img->pixmap = None;
5697 }
5698
5699 if (mask_p && img->mask)
5700 {
5701 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
5702 img->mask = None;
5703 }
5704
5705 if (colors_p && img->ncolors)
5706 {
5707 x_free_colors (f, img->colors, img->ncolors);
5708 xfree (img->colors);
5709 img->colors = NULL;
5710 img->ncolors = 0;
5711 }
5712 }
5713
5714 /* Free X resources of image IMG which is used on frame F. */
5715
5716 static void
5717 x_clear_image (f, img)
5718 struct frame *f;
5719 struct image *img;
5720 {
5721 BLOCK_INPUT;
5722 x_clear_image_1 (f, img, 1, 1, 1);
5723 UNBLOCK_INPUT;
5724 }
5725
5726
5727 /* Allocate color COLOR_NAME for image IMG on frame F. If color
5728 cannot be allocated, use DFLT. Add a newly allocated color to
5729 IMG->colors, so that it can be freed again. Value is the pixel
5730 color. */
5731
5732 static unsigned long
5733 x_alloc_image_color (f, img, color_name, dflt)
5734 struct frame *f;
5735 struct image *img;
5736 Lisp_Object color_name;
5737 unsigned long dflt;
5738 {
5739 XColor color;
5740 unsigned long result;
5741
5742 xassert (STRINGP (color_name));
5743
5744 if (x_defined_color (f, XSTRING (color_name)->data, &color, 1))
5745 {
5746 /* This isn't called frequently so we get away with simply
5747 reallocating the color vector to the needed size, here. */
5748 ++img->ncolors;
5749 img->colors =
5750 (unsigned long *) xrealloc (img->colors,
5751 img->ncolors * sizeof *img->colors);
5752 img->colors[img->ncolors - 1] = color.pixel;
5753 result = color.pixel;
5754 }
5755 else
5756 result = dflt;
5757
5758 return result;
5759 }
5760
5761
5762 \f
5763 /***********************************************************************
5764 Image Cache
5765 ***********************************************************************/
5766
5767 static void cache_image P_ ((struct frame *f, struct image *img));
5768
5769
5770 /* Return a new, initialized image cache that is allocated from the
5771 heap. Call free_image_cache to free an image cache. */
5772
5773 struct image_cache *
5774 make_image_cache ()
5775 {
5776 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
5777 int size;
5778
5779 bzero (c, sizeof *c);
5780 c->size = 50;
5781 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
5782 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
5783 c->buckets = (struct image **) xmalloc (size);
5784 bzero (c->buckets, size);
5785 return c;
5786 }
5787
5788
5789 /* Free image cache of frame F. Be aware that X frames share images
5790 caches. */
5791
5792 void
5793 free_image_cache (f)
5794 struct frame *f;
5795 {
5796 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5797 if (c)
5798 {
5799 int i;
5800
5801 /* Cache should not be referenced by any frame when freed. */
5802 xassert (c->refcount == 0);
5803
5804 for (i = 0; i < c->used; ++i)
5805 free_image (f, c->images[i]);
5806 xfree (c->images);
5807 xfree (c->buckets);
5808 xfree (c);
5809 FRAME_X_IMAGE_CACHE (f) = NULL;
5810 }
5811 }
5812
5813
5814 /* Clear image cache of frame F. FORCE_P non-zero means free all
5815 images. FORCE_P zero means clear only images that haven't been
5816 displayed for some time. Should be called from time to time to
5817 reduce the number of loaded images. If image-eviction-seconds is
5818 non-nil, this frees images in the cache which weren't displayed for
5819 at least that many seconds. */
5820
5821 void
5822 clear_image_cache (f, force_p)
5823 struct frame *f;
5824 int force_p;
5825 {
5826 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5827
5828 if (c && INTEGERP (Vimage_cache_eviction_delay))
5829 {
5830 EMACS_TIME t;
5831 unsigned long old;
5832 int i, nfreed;
5833
5834 EMACS_GET_TIME (t);
5835 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
5836
5837 /* Block input so that we won't be interrupted by a SIGIO
5838 while being in an inconsistent state. */
5839 BLOCK_INPUT;
5840
5841 for (i = nfreed = 0; i < c->used; ++i)
5842 {
5843 struct image *img = c->images[i];
5844 if (img != NULL
5845 && (force_p || img->timestamp < old))
5846 {
5847 free_image (f, img);
5848 ++nfreed;
5849 }
5850 }
5851
5852 /* We may be clearing the image cache because, for example,
5853 Emacs was iconified for a longer period of time. In that
5854 case, current matrices may still contain references to
5855 images freed above. So, clear these matrices. */
5856 if (nfreed)
5857 {
5858 Lisp_Object tail, frame;
5859
5860 FOR_EACH_FRAME (tail, frame)
5861 {
5862 struct frame *f = XFRAME (frame);
5863 if (FRAME_X_P (f)
5864 && FRAME_X_IMAGE_CACHE (f) == c)
5865 clear_current_matrices (f);
5866 }
5867
5868 ++windows_or_buffers_changed;
5869 }
5870
5871 UNBLOCK_INPUT;
5872 }
5873 }
5874
5875
5876 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
5877 0, 1, 0,
5878 "Clear the image cache of FRAME.\n\
5879 FRAME nil or omitted means use the selected frame.\n\
5880 FRAME t means clear the image caches of all frames.")
5881 (frame)
5882 Lisp_Object frame;
5883 {
5884 if (EQ (frame, Qt))
5885 {
5886 Lisp_Object tail;
5887
5888 FOR_EACH_FRAME (tail, frame)
5889 if (FRAME_X_P (XFRAME (frame)))
5890 clear_image_cache (XFRAME (frame), 1);
5891 }
5892 else
5893 clear_image_cache (check_x_frame (frame), 1);
5894
5895 return Qnil;
5896 }
5897
5898
5899 /* Return the id of image with Lisp specification SPEC on frame F.
5900 SPEC must be a valid Lisp image specification (see valid_image_p). */
5901
5902 int
5903 lookup_image (f, spec)
5904 struct frame *f;
5905 Lisp_Object spec;
5906 {
5907 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5908 struct image *img;
5909 int i;
5910 unsigned hash;
5911 struct gcpro gcpro1;
5912 EMACS_TIME now;
5913
5914 /* F must be a window-system frame, and SPEC must be a valid image
5915 specification. */
5916 xassert (FRAME_WINDOW_P (f));
5917 xassert (valid_image_p (spec));
5918
5919 GCPRO1 (spec);
5920
5921 /* Look up SPEC in the hash table of the image cache. */
5922 hash = sxhash (spec, 0);
5923 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
5924
5925 for (img = c->buckets[i]; img; img = img->next)
5926 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
5927 break;
5928
5929 /* If not found, create a new image and cache it. */
5930 if (img == NULL)
5931 {
5932 BLOCK_INPUT;
5933 img = make_image (spec, hash);
5934 cache_image (f, img);
5935 img->load_failed_p = img->type->load (f, img) == 0;
5936
5937 /* If we can't load the image, and we don't have a width and
5938 height, use some arbitrary width and height so that we can
5939 draw a rectangle for it. */
5940 if (img->load_failed_p)
5941 {
5942 Lisp_Object value;
5943
5944 value = image_spec_value (spec, QCwidth, NULL);
5945 img->width = (INTEGERP (value)
5946 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
5947 value = image_spec_value (spec, QCheight, NULL);
5948 img->height = (INTEGERP (value)
5949 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
5950 }
5951 else
5952 {
5953 /* Handle image type independent image attributes
5954 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF'. */
5955 Lisp_Object ascent, margin, relief;
5956 Lisp_Object file;
5957
5958 ascent = image_spec_value (spec, QCascent, NULL);
5959 if (INTEGERP (ascent))
5960 img->ascent = XFASTINT (ascent);
5961 else if (EQ (ascent, Qcenter))
5962 img->ascent = CENTERED_IMAGE_ASCENT;
5963
5964 margin = image_spec_value (spec, QCmargin, NULL);
5965 if (INTEGERP (margin) && XINT (margin) >= 0)
5966 img->margin = XFASTINT (margin);
5967
5968 relief = image_spec_value (spec, QCrelief, NULL);
5969 if (INTEGERP (relief))
5970 {
5971 img->relief = XINT (relief);
5972 img->margin += abs (img->relief);
5973 }
5974
5975 /* Manipulation of the image's mask. */
5976 if (img->pixmap)
5977 {
5978 /* `:heuristic-mask t'
5979 `:mask heuristic'
5980 means build a mask heuristically.
5981 `:heuristic-mask (R G B)'
5982 `:mask (heuristic (R G B))'
5983 means build a mask from color (R G B) in the
5984 image.
5985 `:mask nil'
5986 means remove a mask, if any. */
5987
5988 Lisp_Object mask;
5989
5990 mask = image_spec_value (spec, QCheuristic_mask, NULL);
5991 if (!NILP (mask))
5992 x_build_heuristic_mask (f, img, mask);
5993 else
5994 {
5995 int found_p;
5996
5997 mask = image_spec_value (spec, QCmask, &found_p);
5998
5999 if (EQ (mask, Qheuristic))
6000 x_build_heuristic_mask (f, img, Qt);
6001 else if (CONSP (mask)
6002 && EQ (XCAR (mask), Qheuristic))
6003 {
6004 if (CONSP (XCDR (mask)))
6005 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
6006 else
6007 x_build_heuristic_mask (f, img, XCDR (mask));
6008 }
6009 else if (NILP (mask) && found_p && img->mask)
6010 {
6011 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
6012 img->mask = None;
6013 }
6014 }
6015 }
6016
6017 /* Should we apply an image transformation algorithm? */
6018 if (img->pixmap)
6019 {
6020 Lisp_Object algorithm;
6021
6022 algorithm = image_spec_value (spec, QCalgorithm, NULL);
6023 if (EQ (algorithm, Qdisabled))
6024 x_disable_image (f, img);
6025 else if (EQ (algorithm, Qlaplace))
6026 x_laplace (f, img);
6027 else if (EQ (algorithm, Qemboss))
6028 x_emboss (f, img);
6029 else if (CONSP (algorithm)
6030 && EQ (XCAR (algorithm), Qedge_detection))
6031 {
6032 Lisp_Object tem;
6033 tem = XCDR (algorithm);
6034 if (CONSP (tem))
6035 x_edge_detection (f, img,
6036 Fplist_get (tem, QCmatrix),
6037 Fplist_get (tem, QCcolor_adjustment));
6038 }
6039 }
6040 }
6041
6042 UNBLOCK_INPUT;
6043 xassert (!interrupt_input_blocked);
6044 }
6045
6046 /* We're using IMG, so set its timestamp to `now'. */
6047 EMACS_GET_TIME (now);
6048 img->timestamp = EMACS_SECS (now);
6049
6050 UNGCPRO;
6051
6052 /* Value is the image id. */
6053 return img->id;
6054 }
6055
6056
6057 /* Cache image IMG in the image cache of frame F. */
6058
6059 static void
6060 cache_image (f, img)
6061 struct frame *f;
6062 struct image *img;
6063 {
6064 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6065 int i;
6066
6067 /* Find a free slot in c->images. */
6068 for (i = 0; i < c->used; ++i)
6069 if (c->images[i] == NULL)
6070 break;
6071
6072 /* If no free slot found, maybe enlarge c->images. */
6073 if (i == c->used && c->used == c->size)
6074 {
6075 c->size *= 2;
6076 c->images = (struct image **) xrealloc (c->images,
6077 c->size * sizeof *c->images);
6078 }
6079
6080 /* Add IMG to c->images, and assign IMG an id. */
6081 c->images[i] = img;
6082 img->id = i;
6083 if (i == c->used)
6084 ++c->used;
6085
6086 /* Add IMG to the cache's hash table. */
6087 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
6088 img->next = c->buckets[i];
6089 if (img->next)
6090 img->next->prev = img;
6091 img->prev = NULL;
6092 c->buckets[i] = img;
6093 }
6094
6095
6096 /* Call FN on every image in the image cache of frame F. Used to mark
6097 Lisp Objects in the image cache. */
6098
6099 void
6100 forall_images_in_image_cache (f, fn)
6101 struct frame *f;
6102 void (*fn) P_ ((struct image *img));
6103 {
6104 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
6105 {
6106 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6107 if (c)
6108 {
6109 int i;
6110 for (i = 0; i < c->used; ++i)
6111 if (c->images[i])
6112 fn (c->images[i]);
6113 }
6114 }
6115 }
6116
6117
6118 \f
6119 /***********************************************************************
6120 X support code
6121 ***********************************************************************/
6122
6123 static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
6124 XImage **, Pixmap *));
6125 static void x_destroy_x_image P_ ((XImage *));
6126 static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
6127
6128
6129 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
6130 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
6131 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
6132 via xmalloc. Print error messages via image_error if an error
6133 occurs. Value is non-zero if successful. */
6134
6135 static int
6136 x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
6137 struct frame *f;
6138 int width, height, depth;
6139 XImage **ximg;
6140 Pixmap *pixmap;
6141 {
6142 Display *display = FRAME_X_DISPLAY (f);
6143 Screen *screen = FRAME_X_SCREEN (f);
6144 Window window = FRAME_X_WINDOW (f);
6145
6146 xassert (interrupt_input_blocked);
6147
6148 if (depth <= 0)
6149 depth = DefaultDepthOfScreen (screen);
6150 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
6151 depth, ZPixmap, 0, NULL, width, height,
6152 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
6153 if (*ximg == NULL)
6154 {
6155 image_error ("Unable to allocate X image", Qnil, Qnil);
6156 return 0;
6157 }
6158
6159 /* Allocate image raster. */
6160 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
6161
6162 /* Allocate a pixmap of the same size. */
6163 *pixmap = XCreatePixmap (display, window, width, height, depth);
6164 if (*pixmap == None)
6165 {
6166 x_destroy_x_image (*ximg);
6167 *ximg = NULL;
6168 image_error ("Unable to create X pixmap", Qnil, Qnil);
6169 return 0;
6170 }
6171
6172 return 1;
6173 }
6174
6175
6176 /* Destroy XImage XIMG. Free XIMG->data. */
6177
6178 static void
6179 x_destroy_x_image (ximg)
6180 XImage *ximg;
6181 {
6182 xassert (interrupt_input_blocked);
6183 if (ximg)
6184 {
6185 xfree (ximg->data);
6186 ximg->data = NULL;
6187 XDestroyImage (ximg);
6188 }
6189 }
6190
6191
6192 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
6193 are width and height of both the image and pixmap. */
6194
6195 static void
6196 x_put_x_image (f, ximg, pixmap, width, height)
6197 struct frame *f;
6198 XImage *ximg;
6199 Pixmap pixmap;
6200 {
6201 GC gc;
6202
6203 xassert (interrupt_input_blocked);
6204 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
6205 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
6206 XFreeGC (FRAME_X_DISPLAY (f), gc);
6207 }
6208
6209
6210 \f
6211 /***********************************************************************
6212 File Handling
6213 ***********************************************************************/
6214
6215 static Lisp_Object x_find_image_file P_ ((Lisp_Object));
6216 static char *slurp_file P_ ((char *, int *));
6217
6218
6219 /* Find image file FILE. Look in data-directory, then
6220 x-bitmap-file-path. Value is the full name of the file found, or
6221 nil if not found. */
6222
6223 static Lisp_Object
6224 x_find_image_file (file)
6225 Lisp_Object file;
6226 {
6227 Lisp_Object file_found, search_path;
6228 struct gcpro gcpro1, gcpro2;
6229 int fd;
6230
6231 file_found = Qnil;
6232 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
6233 GCPRO2 (file_found, search_path);
6234
6235 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
6236 fd = openp (search_path, file, "", &file_found, 0);
6237
6238 if (fd == -1)
6239 file_found = Qnil;
6240 else
6241 close (fd);
6242
6243 UNGCPRO;
6244 return file_found;
6245 }
6246
6247
6248 /* Read FILE into memory. Value is a pointer to a buffer allocated
6249 with xmalloc holding FILE's contents. Value is null if an error
6250 occurred. *SIZE is set to the size of the file. */
6251
6252 static char *
6253 slurp_file (file, size)
6254 char *file;
6255 int *size;
6256 {
6257 FILE *fp = NULL;
6258 char *buf = NULL;
6259 struct stat st;
6260
6261 if (stat (file, &st) == 0
6262 && (fp = fopen (file, "r")) != NULL
6263 && (buf = (char *) xmalloc (st.st_size),
6264 fread (buf, 1, st.st_size, fp) == st.st_size))
6265 {
6266 *size = st.st_size;
6267 fclose (fp);
6268 }
6269 else
6270 {
6271 if (fp)
6272 fclose (fp);
6273 if (buf)
6274 {
6275 xfree (buf);
6276 buf = NULL;
6277 }
6278 }
6279
6280 return buf;
6281 }
6282
6283
6284 \f
6285 /***********************************************************************
6286 XBM images
6287 ***********************************************************************/
6288
6289 static int xbm_scan P_ ((char **, char *, char *, int *));
6290 static int xbm_load P_ ((struct frame *f, struct image *img));
6291 static int xbm_load_image P_ ((struct frame *f, struct image *img,
6292 char *, char *));
6293 static int xbm_image_p P_ ((Lisp_Object object));
6294 static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
6295 unsigned char **));
6296 static int xbm_file_p P_ ((Lisp_Object));
6297
6298
6299 /* Indices of image specification fields in xbm_format, below. */
6300
6301 enum xbm_keyword_index
6302 {
6303 XBM_TYPE,
6304 XBM_FILE,
6305 XBM_WIDTH,
6306 XBM_HEIGHT,
6307 XBM_DATA,
6308 XBM_FOREGROUND,
6309 XBM_BACKGROUND,
6310 XBM_ASCENT,
6311 XBM_MARGIN,
6312 XBM_RELIEF,
6313 XBM_ALGORITHM,
6314 XBM_HEURISTIC_MASK,
6315 XBM_MASK,
6316 XBM_LAST
6317 };
6318
6319 /* Vector of image_keyword structures describing the format
6320 of valid XBM image specifications. */
6321
6322 static struct image_keyword xbm_format[XBM_LAST] =
6323 {
6324 {":type", IMAGE_SYMBOL_VALUE, 1},
6325 {":file", IMAGE_STRING_VALUE, 0},
6326 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6327 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6328 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6329 {":foreground", IMAGE_STRING_VALUE, 0},
6330 {":background", IMAGE_STRING_VALUE, 0},
6331 {":ascent", IMAGE_ASCENT_VALUE, 0},
6332 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6333 {":relief", IMAGE_INTEGER_VALUE, 0},
6334 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6335 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6336 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6337 };
6338
6339 /* Structure describing the image type XBM. */
6340
6341 static struct image_type xbm_type =
6342 {
6343 &Qxbm,
6344 xbm_image_p,
6345 xbm_load,
6346 x_clear_image,
6347 NULL
6348 };
6349
6350 /* Tokens returned from xbm_scan. */
6351
6352 enum xbm_token
6353 {
6354 XBM_TK_IDENT = 256,
6355 XBM_TK_NUMBER
6356 };
6357
6358
6359 /* Return non-zero if OBJECT is a valid XBM-type image specification.
6360 A valid specification is a list starting with the symbol `image'
6361 The rest of the list is a property list which must contain an
6362 entry `:type xbm..
6363
6364 If the specification specifies a file to load, it must contain
6365 an entry `:file FILENAME' where FILENAME is a string.
6366
6367 If the specification is for a bitmap loaded from memory it must
6368 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
6369 WIDTH and HEIGHT are integers > 0. DATA may be:
6370
6371 1. a string large enough to hold the bitmap data, i.e. it must
6372 have a size >= (WIDTH + 7) / 8 * HEIGHT
6373
6374 2. a bool-vector of size >= WIDTH * HEIGHT
6375
6376 3. a vector of strings or bool-vectors, one for each line of the
6377 bitmap.
6378
6379 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
6380 may not be specified in this case because they are defined in the
6381 XBM file.
6382
6383 Both the file and data forms may contain the additional entries
6384 `:background COLOR' and `:foreground COLOR'. If not present,
6385 foreground and background of the frame on which the image is
6386 displayed is used. */
6387
6388 static int
6389 xbm_image_p (object)
6390 Lisp_Object object;
6391 {
6392 struct image_keyword kw[XBM_LAST];
6393
6394 bcopy (xbm_format, kw, sizeof kw);
6395 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
6396 return 0;
6397
6398 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
6399
6400 if (kw[XBM_FILE].count)
6401 {
6402 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
6403 return 0;
6404 }
6405 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
6406 {
6407 /* In-memory XBM file. */
6408 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
6409 return 0;
6410 }
6411 else
6412 {
6413 Lisp_Object data;
6414 int width, height;
6415
6416 /* Entries for `:width', `:height' and `:data' must be present. */
6417 if (!kw[XBM_WIDTH].count
6418 || !kw[XBM_HEIGHT].count
6419 || !kw[XBM_DATA].count)
6420 return 0;
6421
6422 data = kw[XBM_DATA].value;
6423 width = XFASTINT (kw[XBM_WIDTH].value);
6424 height = XFASTINT (kw[XBM_HEIGHT].value);
6425
6426 /* Check type of data, and width and height against contents of
6427 data. */
6428 if (VECTORP (data))
6429 {
6430 int i;
6431
6432 /* Number of elements of the vector must be >= height. */
6433 if (XVECTOR (data)->size < height)
6434 return 0;
6435
6436 /* Each string or bool-vector in data must be large enough
6437 for one line of the image. */
6438 for (i = 0; i < height; ++i)
6439 {
6440 Lisp_Object elt = XVECTOR (data)->contents[i];
6441
6442 if (STRINGP (elt))
6443 {
6444 if (XSTRING (elt)->size
6445 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
6446 return 0;
6447 }
6448 else if (BOOL_VECTOR_P (elt))
6449 {
6450 if (XBOOL_VECTOR (elt)->size < width)
6451 return 0;
6452 }
6453 else
6454 return 0;
6455 }
6456 }
6457 else if (STRINGP (data))
6458 {
6459 if (XSTRING (data)->size
6460 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
6461 return 0;
6462 }
6463 else if (BOOL_VECTOR_P (data))
6464 {
6465 if (XBOOL_VECTOR (data)->size < width * height)
6466 return 0;
6467 }
6468 else
6469 return 0;
6470 }
6471
6472 return 1;
6473 }
6474
6475
6476 /* Scan a bitmap file. FP is the stream to read from. Value is
6477 either an enumerator from enum xbm_token, or a character for a
6478 single-character token, or 0 at end of file. If scanning an
6479 identifier, store the lexeme of the identifier in SVAL. If
6480 scanning a number, store its value in *IVAL. */
6481
6482 static int
6483 xbm_scan (s, end, sval, ival)
6484 char **s, *end;
6485 char *sval;
6486 int *ival;
6487 {
6488 int c;
6489
6490 loop:
6491
6492 /* Skip white space. */
6493 while (*s < end && (c = *(*s)++, isspace (c)))
6494 ;
6495
6496 if (*s >= end)
6497 c = 0;
6498 else if (isdigit (c))
6499 {
6500 int value = 0, digit;
6501
6502 if (c == '0' && *s < end)
6503 {
6504 c = *(*s)++;
6505 if (c == 'x' || c == 'X')
6506 {
6507 while (*s < end)
6508 {
6509 c = *(*s)++;
6510 if (isdigit (c))
6511 digit = c - '0';
6512 else if (c >= 'a' && c <= 'f')
6513 digit = c - 'a' + 10;
6514 else if (c >= 'A' && c <= 'F')
6515 digit = c - 'A' + 10;
6516 else
6517 break;
6518 value = 16 * value + digit;
6519 }
6520 }
6521 else if (isdigit (c))
6522 {
6523 value = c - '0';
6524 while (*s < end
6525 && (c = *(*s)++, isdigit (c)))
6526 value = 8 * value + c - '0';
6527 }
6528 }
6529 else
6530 {
6531 value = c - '0';
6532 while (*s < end
6533 && (c = *(*s)++, isdigit (c)))
6534 value = 10 * value + c - '0';
6535 }
6536
6537 if (*s < end)
6538 *s = *s - 1;
6539 *ival = value;
6540 c = XBM_TK_NUMBER;
6541 }
6542 else if (isalpha (c) || c == '_')
6543 {
6544 *sval++ = c;
6545 while (*s < end
6546 && (c = *(*s)++, (isalnum (c) || c == '_')))
6547 *sval++ = c;
6548 *sval = 0;
6549 if (*s < end)
6550 *s = *s - 1;
6551 c = XBM_TK_IDENT;
6552 }
6553 else if (c == '/' && **s == '*')
6554 {
6555 /* C-style comment. */
6556 ++*s;
6557 while (**s && (**s != '*' || *(*s + 1) != '/'))
6558 ++*s;
6559 if (**s)
6560 {
6561 *s += 2;
6562 goto loop;
6563 }
6564 }
6565
6566 return c;
6567 }
6568
6569
6570 /* Replacement for XReadBitmapFileData which isn't available under old
6571 X versions. CONTENTS is a pointer to a buffer to parse; END is the
6572 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
6573 the image. Return in *DATA the bitmap data allocated with xmalloc.
6574 Value is non-zero if successful. DATA null means just test if
6575 CONTENTS looks like an in-memory XBM file. */
6576
6577 static int
6578 xbm_read_bitmap_data (contents, end, width, height, data)
6579 char *contents, *end;
6580 int *width, *height;
6581 unsigned char **data;
6582 {
6583 char *s = contents;
6584 char buffer[BUFSIZ];
6585 int padding_p = 0;
6586 int v10 = 0;
6587 int bytes_per_line, i, nbytes;
6588 unsigned char *p;
6589 int value;
6590 int LA1;
6591
6592 #define match() \
6593 LA1 = xbm_scan (&s, end, buffer, &value)
6594
6595 #define expect(TOKEN) \
6596 if (LA1 != (TOKEN)) \
6597 goto failure; \
6598 else \
6599 match ()
6600
6601 #define expect_ident(IDENT) \
6602 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
6603 match (); \
6604 else \
6605 goto failure
6606
6607 *width = *height = -1;
6608 if (data)
6609 *data = NULL;
6610 LA1 = xbm_scan (&s, end, buffer, &value);
6611
6612 /* Parse defines for width, height and hot-spots. */
6613 while (LA1 == '#')
6614 {
6615 match ();
6616 expect_ident ("define");
6617 expect (XBM_TK_IDENT);
6618
6619 if (LA1 == XBM_TK_NUMBER);
6620 {
6621 char *p = strrchr (buffer, '_');
6622 p = p ? p + 1 : buffer;
6623 if (strcmp (p, "width") == 0)
6624 *width = value;
6625 else if (strcmp (p, "height") == 0)
6626 *height = value;
6627 }
6628 expect (XBM_TK_NUMBER);
6629 }
6630
6631 if (*width < 0 || *height < 0)
6632 goto failure;
6633 else if (data == NULL)
6634 goto success;
6635
6636 /* Parse bits. Must start with `static'. */
6637 expect_ident ("static");
6638 if (LA1 == XBM_TK_IDENT)
6639 {
6640 if (strcmp (buffer, "unsigned") == 0)
6641 {
6642 match ();
6643 expect_ident ("char");
6644 }
6645 else if (strcmp (buffer, "short") == 0)
6646 {
6647 match ();
6648 v10 = 1;
6649 if (*width % 16 && *width % 16 < 9)
6650 padding_p = 1;
6651 }
6652 else if (strcmp (buffer, "char") == 0)
6653 match ();
6654 else
6655 goto failure;
6656 }
6657 else
6658 goto failure;
6659
6660 expect (XBM_TK_IDENT);
6661 expect ('[');
6662 expect (']');
6663 expect ('=');
6664 expect ('{');
6665
6666 bytes_per_line = (*width + 7) / 8 + padding_p;
6667 nbytes = bytes_per_line * *height;
6668 p = *data = (char *) xmalloc (nbytes);
6669
6670 if (v10)
6671 {
6672 for (i = 0; i < nbytes; i += 2)
6673 {
6674 int val = value;
6675 expect (XBM_TK_NUMBER);
6676
6677 *p++ = val;
6678 if (!padding_p || ((i + 2) % bytes_per_line))
6679 *p++ = value >> 8;
6680
6681 if (LA1 == ',' || LA1 == '}')
6682 match ();
6683 else
6684 goto failure;
6685 }
6686 }
6687 else
6688 {
6689 for (i = 0; i < nbytes; ++i)
6690 {
6691 int val = value;
6692 expect (XBM_TK_NUMBER);
6693
6694 *p++ = val;
6695
6696 if (LA1 == ',' || LA1 == '}')
6697 match ();
6698 else
6699 goto failure;
6700 }
6701 }
6702
6703 success:
6704 return 1;
6705
6706 failure:
6707
6708 if (data && *data)
6709 {
6710 xfree (*data);
6711 *data = NULL;
6712 }
6713 return 0;
6714
6715 #undef match
6716 #undef expect
6717 #undef expect_ident
6718 }
6719
6720
6721 /* Load XBM image IMG which will be displayed on frame F from buffer
6722 CONTENTS. END is the end of the buffer. Value is non-zero if
6723 successful. */
6724
6725 static int
6726 xbm_load_image (f, img, contents, end)
6727 struct frame *f;
6728 struct image *img;
6729 char *contents, *end;
6730 {
6731 int rc;
6732 unsigned char *data;
6733 int success_p = 0;
6734
6735 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
6736 if (rc)
6737 {
6738 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6739 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
6740 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
6741 Lisp_Object value;
6742
6743 xassert (img->width > 0 && img->height > 0);
6744
6745 /* Get foreground and background colors, maybe allocate colors. */
6746 value = image_spec_value (img->spec, QCforeground, NULL);
6747 if (!NILP (value))
6748 foreground = x_alloc_image_color (f, img, value, foreground);
6749
6750 value = image_spec_value (img->spec, QCbackground, NULL);
6751 if (!NILP (value))
6752 background = x_alloc_image_color (f, img, value, background);
6753
6754 img->pixmap
6755 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6756 FRAME_X_WINDOW (f),
6757 data,
6758 img->width, img->height,
6759 foreground, background,
6760 depth);
6761 xfree (data);
6762
6763 if (img->pixmap == None)
6764 {
6765 x_clear_image (f, img);
6766 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
6767 }
6768 else
6769 success_p = 1;
6770 }
6771 else
6772 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
6773
6774 return success_p;
6775 }
6776
6777
6778 /* Value is non-zero if DATA looks like an in-memory XBM file. */
6779
6780 static int
6781 xbm_file_p (data)
6782 Lisp_Object data;
6783 {
6784 int w, h;
6785 return (STRINGP (data)
6786 && xbm_read_bitmap_data (XSTRING (data)->data,
6787 (XSTRING (data)->data
6788 + STRING_BYTES (XSTRING (data))),
6789 &w, &h, NULL));
6790 }
6791
6792
6793 /* Fill image IMG which is used on frame F with pixmap data. Value is
6794 non-zero if successful. */
6795
6796 static int
6797 xbm_load (f, img)
6798 struct frame *f;
6799 struct image *img;
6800 {
6801 int success_p = 0;
6802 Lisp_Object file_name;
6803
6804 xassert (xbm_image_p (img->spec));
6805
6806 /* If IMG->spec specifies a file name, create a non-file spec from it. */
6807 file_name = image_spec_value (img->spec, QCfile, NULL);
6808 if (STRINGP (file_name))
6809 {
6810 Lisp_Object file;
6811 char *contents;
6812 int size;
6813 struct gcpro gcpro1;
6814
6815 file = x_find_image_file (file_name);
6816 GCPRO1 (file);
6817 if (!STRINGP (file))
6818 {
6819 image_error ("Cannot find image file `%s'", file_name, Qnil);
6820 UNGCPRO;
6821 return 0;
6822 }
6823
6824 contents = slurp_file (XSTRING (file)->data, &size);
6825 if (contents == NULL)
6826 {
6827 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
6828 UNGCPRO;
6829 return 0;
6830 }
6831
6832 success_p = xbm_load_image (f, img, contents, contents + size);
6833 UNGCPRO;
6834 }
6835 else
6836 {
6837 struct image_keyword fmt[XBM_LAST];
6838 Lisp_Object data;
6839 unsigned char *bitmap_data;
6840 int depth;
6841 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
6842 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
6843 char *bits;
6844 int parsed_p, height, width;
6845 int in_memory_file_p = 0;
6846
6847 /* See if data looks like an in-memory XBM file. */
6848 data = image_spec_value (img->spec, QCdata, NULL);
6849 in_memory_file_p = xbm_file_p (data);
6850
6851 /* Parse the image specification. */
6852 bcopy (xbm_format, fmt, sizeof fmt);
6853 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
6854 xassert (parsed_p);
6855
6856 /* Get specified width, and height. */
6857 if (!in_memory_file_p)
6858 {
6859 img->width = XFASTINT (fmt[XBM_WIDTH].value);
6860 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
6861 xassert (img->width > 0 && img->height > 0);
6862 }
6863
6864 /* Get foreground and background colors, maybe allocate colors. */
6865 if (fmt[XBM_FOREGROUND].count)
6866 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
6867 foreground);
6868 if (fmt[XBM_BACKGROUND].count)
6869 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
6870 background);
6871
6872 if (in_memory_file_p)
6873 success_p = xbm_load_image (f, img, XSTRING (data)->data,
6874 (XSTRING (data)->data
6875 + STRING_BYTES (XSTRING (data))));
6876 else
6877 {
6878 if (VECTORP (data))
6879 {
6880 int i;
6881 char *p;
6882 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
6883
6884 p = bits = (char *) alloca (nbytes * img->height);
6885 for (i = 0; i < img->height; ++i, p += nbytes)
6886 {
6887 Lisp_Object line = XVECTOR (data)->contents[i];
6888 if (STRINGP (line))
6889 bcopy (XSTRING (line)->data, p, nbytes);
6890 else
6891 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
6892 }
6893 }
6894 else if (STRINGP (data))
6895 bits = XSTRING (data)->data;
6896 else
6897 bits = XBOOL_VECTOR (data)->data;
6898
6899 /* Create the pixmap. */
6900 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6901 img->pixmap
6902 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6903 FRAME_X_WINDOW (f),
6904 bits,
6905 img->width, img->height,
6906 foreground, background,
6907 depth);
6908 if (img->pixmap)
6909 success_p = 1;
6910 else
6911 {
6912 image_error ("Unable to create pixmap for XBM image `%s'",
6913 img->spec, Qnil);
6914 x_clear_image (f, img);
6915 }
6916 }
6917 }
6918
6919 return success_p;
6920 }
6921
6922
6923 \f
6924 /***********************************************************************
6925 XPM images
6926 ***********************************************************************/
6927
6928 #if HAVE_XPM
6929
6930 static int xpm_image_p P_ ((Lisp_Object object));
6931 static int xpm_load P_ ((struct frame *f, struct image *img));
6932 static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
6933
6934 #include "X11/xpm.h"
6935
6936 /* The symbol `xpm' identifying XPM-format images. */
6937
6938 Lisp_Object Qxpm;
6939
6940 /* Indices of image specification fields in xpm_format, below. */
6941
6942 enum xpm_keyword_index
6943 {
6944 XPM_TYPE,
6945 XPM_FILE,
6946 XPM_DATA,
6947 XPM_ASCENT,
6948 XPM_MARGIN,
6949 XPM_RELIEF,
6950 XPM_ALGORITHM,
6951 XPM_HEURISTIC_MASK,
6952 XPM_MASK,
6953 XPM_COLOR_SYMBOLS,
6954 XPM_LAST
6955 };
6956
6957 /* Vector of image_keyword structures describing the format
6958 of valid XPM image specifications. */
6959
6960 static struct image_keyword xpm_format[XPM_LAST] =
6961 {
6962 {":type", IMAGE_SYMBOL_VALUE, 1},
6963 {":file", IMAGE_STRING_VALUE, 0},
6964 {":data", IMAGE_STRING_VALUE, 0},
6965 {":ascent", IMAGE_ASCENT_VALUE, 0},
6966 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6967 {":relief", IMAGE_INTEGER_VALUE, 0},
6968 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6969 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6970 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6971 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6972 };
6973
6974 /* Structure describing the image type XBM. */
6975
6976 static struct image_type xpm_type =
6977 {
6978 &Qxpm,
6979 xpm_image_p,
6980 xpm_load,
6981 x_clear_image,
6982 NULL
6983 };
6984
6985
6986 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
6987 functions for allocating image colors. Our own functions handle
6988 color allocation failures more gracefully than the ones on the XPM
6989 lib. */
6990
6991 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
6992 #define ALLOC_XPM_COLORS
6993 #endif
6994
6995 #ifdef ALLOC_XPM_COLORS
6996
6997 static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
6998 static void xpm_free_color_cache P_ ((void));
6999 static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
7000 static int xpm_color_bucket P_ ((char *));
7001 static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
7002 XColor *, int));
7003
7004 /* An entry in a hash table used to cache color definitions of named
7005 colors. This cache is necessary to speed up XPM image loading in
7006 case we do color allocations ourselves. Without it, we would need
7007 a call to XParseColor per pixel in the image. */
7008
7009 struct xpm_cached_color
7010 {
7011 /* Next in collision chain. */
7012 struct xpm_cached_color *next;
7013
7014 /* Color definition (RGB and pixel color). */
7015 XColor color;
7016
7017 /* Color name. */
7018 char name[1];
7019 };
7020
7021 /* The hash table used for the color cache, and its bucket vector
7022 size. */
7023
7024 #define XPM_COLOR_CACHE_BUCKETS 1001
7025 struct xpm_cached_color **xpm_color_cache;
7026
7027 /* Initialize the color cache. */
7028
7029 static void
7030 xpm_init_color_cache (f, attrs)
7031 struct frame *f;
7032 XpmAttributes *attrs;
7033 {
7034 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
7035 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
7036 memset (xpm_color_cache, 0, nbytes);
7037 init_color_table ();
7038
7039 if (attrs->valuemask & XpmColorSymbols)
7040 {
7041 int i;
7042 XColor color;
7043
7044 for (i = 0; i < attrs->numsymbols; ++i)
7045 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7046 attrs->colorsymbols[i].value, &color))
7047 {
7048 color.pixel = lookup_rgb_color (f, color.red, color.green,
7049 color.blue);
7050 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
7051 }
7052 }
7053 }
7054
7055
7056 /* Free the color cache. */
7057
7058 static void
7059 xpm_free_color_cache ()
7060 {
7061 struct xpm_cached_color *p, *next;
7062 int i;
7063
7064 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
7065 for (p = xpm_color_cache[i]; p; p = next)
7066 {
7067 next = p->next;
7068 xfree (p);
7069 }
7070
7071 xfree (xpm_color_cache);
7072 xpm_color_cache = NULL;
7073 free_color_table ();
7074 }
7075
7076
7077 /* Return the bucket index for color named COLOR_NAME in the color
7078 cache. */
7079
7080 static int
7081 xpm_color_bucket (color_name)
7082 char *color_name;
7083 {
7084 unsigned h = 0;
7085 char *s;
7086
7087 for (s = color_name; *s; ++s)
7088 h = (h << 2) ^ *s;
7089 return h %= XPM_COLOR_CACHE_BUCKETS;
7090 }
7091
7092
7093 /* On frame F, cache values COLOR for color with name COLOR_NAME.
7094 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
7095 entry added. */
7096
7097 static struct xpm_cached_color *
7098 xpm_cache_color (f, color_name, color, bucket)
7099 struct frame *f;
7100 char *color_name;
7101 XColor *color;
7102 int bucket;
7103 {
7104 size_t nbytes;
7105 struct xpm_cached_color *p;
7106
7107 if (bucket < 0)
7108 bucket = xpm_color_bucket (color_name);
7109
7110 nbytes = sizeof *p + strlen (color_name);
7111 p = (struct xpm_cached_color *) xmalloc (nbytes);
7112 strcpy (p->name, color_name);
7113 p->color = *color;
7114 p->next = xpm_color_cache[bucket];
7115 xpm_color_cache[bucket] = p;
7116 return p;
7117 }
7118
7119
7120 /* Look up color COLOR_NAME for frame F in the color cache. If found,
7121 return the cached definition in *COLOR. Otherwise, make a new
7122 entry in the cache and allocate the color. Value is zero if color
7123 allocation failed. */
7124
7125 static int
7126 xpm_lookup_color (f, color_name, color)
7127 struct frame *f;
7128 char *color_name;
7129 XColor *color;
7130 {
7131 struct xpm_cached_color *p;
7132 int h = xpm_color_bucket (color_name);
7133
7134 for (p = xpm_color_cache[h]; p; p = p->next)
7135 if (strcmp (p->name, color_name) == 0)
7136 break;
7137
7138 if (p != NULL)
7139 *color = p->color;
7140 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7141 color_name, color))
7142 {
7143 color->pixel = lookup_rgb_color (f, color->red, color->green,
7144 color->blue);
7145 p = xpm_cache_color (f, color_name, color, h);
7146 }
7147
7148 return p != NULL;
7149 }
7150
7151
7152 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
7153 CLOSURE is a pointer to the frame on which we allocate the
7154 color. Return in *COLOR the allocated color. Value is non-zero
7155 if successful. */
7156
7157 static int
7158 xpm_alloc_color (dpy, cmap, color_name, color, closure)
7159 Display *dpy;
7160 Colormap cmap;
7161 char *color_name;
7162 XColor *color;
7163 void *closure;
7164 {
7165 return xpm_lookup_color ((struct frame *) closure, color_name, color);
7166 }
7167
7168
7169 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
7170 is a pointer to the frame on which we allocate the color. Value is
7171 non-zero if successful. */
7172
7173 static int
7174 xpm_free_colors (dpy, cmap, pixels, npixels, closure)
7175 Display *dpy;
7176 Colormap cmap;
7177 Pixel *pixels;
7178 int npixels;
7179 void *closure;
7180 {
7181 return 1;
7182 }
7183
7184 #endif /* ALLOC_XPM_COLORS */
7185
7186
7187 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
7188 for XPM images. Such a list must consist of conses whose car and
7189 cdr are strings. */
7190
7191 static int
7192 xpm_valid_color_symbols_p (color_symbols)
7193 Lisp_Object color_symbols;
7194 {
7195 while (CONSP (color_symbols))
7196 {
7197 Lisp_Object sym = XCAR (color_symbols);
7198 if (!CONSP (sym)
7199 || !STRINGP (XCAR (sym))
7200 || !STRINGP (XCDR (sym)))
7201 break;
7202 color_symbols = XCDR (color_symbols);
7203 }
7204
7205 return NILP (color_symbols);
7206 }
7207
7208
7209 /* Value is non-zero if OBJECT is a valid XPM image specification. */
7210
7211 static int
7212 xpm_image_p (object)
7213 Lisp_Object object;
7214 {
7215 struct image_keyword fmt[XPM_LAST];
7216 bcopy (xpm_format, fmt, sizeof fmt);
7217 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
7218 /* Either `:file' or `:data' must be present. */
7219 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
7220 /* Either no `:color-symbols' or it's a list of conses
7221 whose car and cdr are strings. */
7222 && (fmt[XPM_COLOR_SYMBOLS].count == 0
7223 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
7224 }
7225
7226
7227 /* Load image IMG which will be displayed on frame F. Value is
7228 non-zero if successful. */
7229
7230 static int
7231 xpm_load (f, img)
7232 struct frame *f;
7233 struct image *img;
7234 {
7235 int rc, i;
7236 XpmAttributes attrs;
7237 Lisp_Object specified_file, color_symbols;
7238
7239 /* Configure the XPM lib. Use the visual of frame F. Allocate
7240 close colors. Return colors allocated. */
7241 bzero (&attrs, sizeof attrs);
7242 attrs.visual = FRAME_X_VISUAL (f);
7243 attrs.colormap = FRAME_X_COLORMAP (f);
7244 attrs.valuemask |= XpmVisual;
7245 attrs.valuemask |= XpmColormap;
7246
7247 #ifdef ALLOC_XPM_COLORS
7248 /* Allocate colors with our own functions which handle
7249 failing color allocation more gracefully. */
7250 attrs.color_closure = f;
7251 attrs.alloc_color = xpm_alloc_color;
7252 attrs.free_colors = xpm_free_colors;
7253 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
7254 #else /* not ALLOC_XPM_COLORS */
7255 /* Let the XPM lib allocate colors. */
7256 attrs.valuemask |= XpmReturnAllocPixels;
7257 #ifdef XpmAllocCloseColors
7258 attrs.alloc_close_colors = 1;
7259 attrs.valuemask |= XpmAllocCloseColors;
7260 #else /* not XpmAllocCloseColors */
7261 attrs.closeness = 600;
7262 attrs.valuemask |= XpmCloseness;
7263 #endif /* not XpmAllocCloseColors */
7264 #endif /* ALLOC_XPM_COLORS */
7265
7266 /* If image specification contains symbolic color definitions, add
7267 these to `attrs'. */
7268 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
7269 if (CONSP (color_symbols))
7270 {
7271 Lisp_Object tail;
7272 XpmColorSymbol *xpm_syms;
7273 int i, size;
7274
7275 attrs.valuemask |= XpmColorSymbols;
7276
7277 /* Count number of symbols. */
7278 attrs.numsymbols = 0;
7279 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
7280 ++attrs.numsymbols;
7281
7282 /* Allocate an XpmColorSymbol array. */
7283 size = attrs.numsymbols * sizeof *xpm_syms;
7284 xpm_syms = (XpmColorSymbol *) alloca (size);
7285 bzero (xpm_syms, size);
7286 attrs.colorsymbols = xpm_syms;
7287
7288 /* Fill the color symbol array. */
7289 for (tail = color_symbols, i = 0;
7290 CONSP (tail);
7291 ++i, tail = XCDR (tail))
7292 {
7293 Lisp_Object name = XCAR (XCAR (tail));
7294 Lisp_Object color = XCDR (XCAR (tail));
7295 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
7296 strcpy (xpm_syms[i].name, XSTRING (name)->data);
7297 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
7298 strcpy (xpm_syms[i].value, XSTRING (color)->data);
7299 }
7300 }
7301
7302 /* Create a pixmap for the image, either from a file, or from a
7303 string buffer containing data in the same format as an XPM file. */
7304 #ifdef ALLOC_XPM_COLORS
7305 xpm_init_color_cache (f, &attrs);
7306 #endif
7307
7308 specified_file = image_spec_value (img->spec, QCfile, NULL);
7309 if (STRINGP (specified_file))
7310 {
7311 Lisp_Object file = x_find_image_file (specified_file);
7312 if (!STRINGP (file))
7313 {
7314 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7315 return 0;
7316 }
7317
7318 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7319 XSTRING (file)->data, &img->pixmap, &img->mask,
7320 &attrs);
7321 }
7322 else
7323 {
7324 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
7325 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7326 XSTRING (buffer)->data,
7327 &img->pixmap, &img->mask,
7328 &attrs);
7329 }
7330
7331 if (rc == XpmSuccess)
7332 {
7333 #ifdef ALLOC_XPM_COLORS
7334 img->colors = colors_in_color_table (&img->ncolors);
7335 #else /* not ALLOC_XPM_COLORS */
7336 img->ncolors = attrs.nalloc_pixels;
7337 img->colors = (unsigned long *) xmalloc (img->ncolors
7338 * sizeof *img->colors);
7339 for (i = 0; i < attrs.nalloc_pixels; ++i)
7340 {
7341 img->colors[i] = attrs.alloc_pixels[i];
7342 #ifdef DEBUG_X_COLORS
7343 register_color (img->colors[i]);
7344 #endif
7345 }
7346 #endif /* not ALLOC_XPM_COLORS */
7347
7348 img->width = attrs.width;
7349 img->height = attrs.height;
7350 xassert (img->width > 0 && img->height > 0);
7351
7352 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
7353 XpmFreeAttributes (&attrs);
7354 }
7355 else
7356 {
7357 switch (rc)
7358 {
7359 case XpmOpenFailed:
7360 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
7361 break;
7362
7363 case XpmFileInvalid:
7364 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
7365 break;
7366
7367 case XpmNoMemory:
7368 image_error ("Out of memory (%s)", img->spec, Qnil);
7369 break;
7370
7371 case XpmColorFailed:
7372 image_error ("Color allocation error (%s)", img->spec, Qnil);
7373 break;
7374
7375 default:
7376 image_error ("Unknown error (%s)", img->spec, Qnil);
7377 break;
7378 }
7379 }
7380
7381 #ifdef ALLOC_XPM_COLORS
7382 xpm_free_color_cache ();
7383 #endif
7384 return rc == XpmSuccess;
7385 }
7386
7387 #endif /* HAVE_XPM != 0 */
7388
7389 \f
7390 /***********************************************************************
7391 Color table
7392 ***********************************************************************/
7393
7394 /* An entry in the color table mapping an RGB color to a pixel color. */
7395
7396 struct ct_color
7397 {
7398 int r, g, b;
7399 unsigned long pixel;
7400
7401 /* Next in color table collision list. */
7402 struct ct_color *next;
7403 };
7404
7405 /* The bucket vector size to use. Must be prime. */
7406
7407 #define CT_SIZE 101
7408
7409 /* Value is a hash of the RGB color given by R, G, and B. */
7410
7411 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
7412
7413 /* The color hash table. */
7414
7415 struct ct_color **ct_table;
7416
7417 /* Number of entries in the color table. */
7418
7419 int ct_colors_allocated;
7420
7421 /* Initialize the color table. */
7422
7423 static void
7424 init_color_table ()
7425 {
7426 int size = CT_SIZE * sizeof (*ct_table);
7427 ct_table = (struct ct_color **) xmalloc (size);
7428 bzero (ct_table, size);
7429 ct_colors_allocated = 0;
7430 }
7431
7432
7433 /* Free memory associated with the color table. */
7434
7435 static void
7436 free_color_table ()
7437 {
7438 int i;
7439 struct ct_color *p, *next;
7440
7441 for (i = 0; i < CT_SIZE; ++i)
7442 for (p = ct_table[i]; p; p = next)
7443 {
7444 next = p->next;
7445 xfree (p);
7446 }
7447
7448 xfree (ct_table);
7449 ct_table = NULL;
7450 }
7451
7452
7453 /* Value is a pixel color for RGB color R, G, B on frame F. If an
7454 entry for that color already is in the color table, return the
7455 pixel color of that entry. Otherwise, allocate a new color for R,
7456 G, B, and make an entry in the color table. */
7457
7458 static unsigned long
7459 lookup_rgb_color (f, r, g, b)
7460 struct frame *f;
7461 int r, g, b;
7462 {
7463 unsigned hash = CT_HASH_RGB (r, g, b);
7464 int i = hash % CT_SIZE;
7465 struct ct_color *p;
7466
7467 for (p = ct_table[i]; p; p = p->next)
7468 if (p->r == r && p->g == g && p->b == b)
7469 break;
7470
7471 if (p == NULL)
7472 {
7473 XColor color;
7474 Colormap cmap;
7475 int rc;
7476
7477 color.red = r;
7478 color.green = g;
7479 color.blue = b;
7480
7481 cmap = FRAME_X_COLORMAP (f);
7482 rc = x_alloc_nearest_color (f, cmap, &color);
7483
7484 if (rc)
7485 {
7486 ++ct_colors_allocated;
7487
7488 p = (struct ct_color *) xmalloc (sizeof *p);
7489 p->r = r;
7490 p->g = g;
7491 p->b = b;
7492 p->pixel = color.pixel;
7493 p->next = ct_table[i];
7494 ct_table[i] = p;
7495 }
7496 else
7497 return FRAME_FOREGROUND_PIXEL (f);
7498 }
7499
7500 return p->pixel;
7501 }
7502
7503
7504 /* Look up pixel color PIXEL which is used on frame F in the color
7505 table. If not already present, allocate it. Value is PIXEL. */
7506
7507 static unsigned long
7508 lookup_pixel_color (f, pixel)
7509 struct frame *f;
7510 unsigned long pixel;
7511 {
7512 int i = pixel % CT_SIZE;
7513 struct ct_color *p;
7514
7515 for (p = ct_table[i]; p; p = p->next)
7516 if (p->pixel == pixel)
7517 break;
7518
7519 if (p == NULL)
7520 {
7521 XColor color;
7522 Colormap cmap;
7523 int rc;
7524
7525 cmap = FRAME_X_COLORMAP (f);
7526 color.pixel = pixel;
7527 x_query_color (f, &color);
7528 rc = x_alloc_nearest_color (f, cmap, &color);
7529
7530 if (rc)
7531 {
7532 ++ct_colors_allocated;
7533
7534 p = (struct ct_color *) xmalloc (sizeof *p);
7535 p->r = color.red;
7536 p->g = color.green;
7537 p->b = color.blue;
7538 p->pixel = pixel;
7539 p->next = ct_table[i];
7540 ct_table[i] = p;
7541 }
7542 else
7543 return FRAME_FOREGROUND_PIXEL (f);
7544 }
7545
7546 return p->pixel;
7547 }
7548
7549
7550 /* Value is a vector of all pixel colors contained in the color table,
7551 allocated via xmalloc. Set *N to the number of colors. */
7552
7553 static unsigned long *
7554 colors_in_color_table (n)
7555 int *n;
7556 {
7557 int i, j;
7558 struct ct_color *p;
7559 unsigned long *colors;
7560
7561 if (ct_colors_allocated == 0)
7562 {
7563 *n = 0;
7564 colors = NULL;
7565 }
7566 else
7567 {
7568 colors = (unsigned long *) xmalloc (ct_colors_allocated
7569 * sizeof *colors);
7570 *n = ct_colors_allocated;
7571
7572 for (i = j = 0; i < CT_SIZE; ++i)
7573 for (p = ct_table[i]; p; p = p->next)
7574 colors[j++] = p->pixel;
7575 }
7576
7577 return colors;
7578 }
7579
7580
7581 \f
7582 /***********************************************************************
7583 Algorithms
7584 ***********************************************************************/
7585
7586 static void x_laplace_write_row P_ ((struct frame *, long *,
7587 int, XImage *, int));
7588 static void x_laplace_read_row P_ ((struct frame *, Colormap,
7589 XColor *, int, XImage *, int));
7590 static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
7591 static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
7592 static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
7593
7594 /* Non-zero means draw a cross on images having `:algorithm
7595 disabled'. */
7596
7597 int cross_disabled_images;
7598
7599 /* Edge detection matrices for different edge-detection
7600 strategies. */
7601
7602 static int emboss_matrix[9] = {
7603 /* x - 1 x x + 1 */
7604 2, -1, 0, /* y - 1 */
7605 -1, 0, 1, /* y */
7606 0, 1, -2 /* y + 1 */
7607 };
7608
7609 static int laplace_matrix[9] = {
7610 /* x - 1 x x + 1 */
7611 1, 0, 0, /* y - 1 */
7612 0, 0, 0, /* y */
7613 0, 0, -1 /* y + 1 */
7614 };
7615
7616 /* Value is the intensity of the color whose red/green/blue values
7617 are R, G, and B. */
7618
7619 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
7620
7621
7622 /* On frame F, return an array of XColor structures describing image
7623 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
7624 non-zero means also fill the red/green/blue members of the XColor
7625 structures. Value is a pointer to the array of XColors structures,
7626 allocated with xmalloc; it must be freed by the caller. */
7627
7628 static XColor *
7629 x_to_xcolors (f, img, rgb_p)
7630 struct frame *f;
7631 struct image *img;
7632 int rgb_p;
7633 {
7634 int x, y;
7635 XColor *colors, *p;
7636 XImage *ximg;
7637
7638 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
7639
7640 /* Get the X image IMG->pixmap. */
7641 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
7642 0, 0, img->width, img->height, ~0, ZPixmap);
7643
7644 /* Fill the `pixel' members of the XColor array. I wished there
7645 were an easy and portable way to circumvent XGetPixel. */
7646 p = colors;
7647 for (y = 0; y < img->height; ++y)
7648 {
7649 XColor *row = p;
7650
7651 for (x = 0; x < img->width; ++x, ++p)
7652 p->pixel = XGetPixel (ximg, x, y);
7653
7654 if (rgb_p)
7655 x_query_colors (f, row, img->width);
7656 }
7657
7658 XDestroyImage (ximg);
7659 return colors;
7660 }
7661
7662
7663 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
7664 RGB members are set. F is the frame on which this all happens.
7665 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
7666
7667 static void
7668 x_from_xcolors (f, img, colors)
7669 struct frame *f;
7670 struct image *img;
7671 XColor *colors;
7672 {
7673 int x, y;
7674 XImage *oimg;
7675 Pixmap pixmap;
7676 XColor *p;
7677
7678 init_color_table ();
7679
7680 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
7681 &oimg, &pixmap);
7682 p = colors;
7683 for (y = 0; y < img->height; ++y)
7684 for (x = 0; x < img->width; ++x, ++p)
7685 {
7686 unsigned long pixel;
7687 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
7688 XPutPixel (oimg, x, y, pixel);
7689 }
7690
7691 xfree (colors);
7692 x_clear_image_1 (f, img, 1, 0, 1);
7693
7694 x_put_x_image (f, oimg, pixmap, img->width, img->height);
7695 x_destroy_x_image (oimg);
7696 img->pixmap = pixmap;
7697 img->colors = colors_in_color_table (&img->ncolors);
7698 free_color_table ();
7699 }
7700
7701
7702 /* On frame F, perform edge-detection on image IMG.
7703
7704 MATRIX is a nine-element array specifying the transformation
7705 matrix. See emboss_matrix for an example.
7706
7707 COLOR_ADJUST is a color adjustment added to each pixel of the
7708 outgoing image. */
7709
7710 static void
7711 x_detect_edges (f, img, matrix, color_adjust)
7712 struct frame *f;
7713 struct image *img;
7714 int matrix[9], color_adjust;
7715 {
7716 XColor *colors = x_to_xcolors (f, img, 1);
7717 XColor *new, *p;
7718 int x, y, i, sum;
7719
7720 for (i = sum = 0; i < 9; ++i)
7721 sum += abs (matrix[i]);
7722
7723 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
7724
7725 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
7726
7727 for (y = 0; y < img->height; ++y)
7728 {
7729 p = COLOR (new, 0, y);
7730 p->red = p->green = p->blue = 0xffff/2;
7731 p = COLOR (new, img->width - 1, y);
7732 p->red = p->green = p->blue = 0xffff/2;
7733 }
7734
7735 for (x = 1; x < img->width - 1; ++x)
7736 {
7737 p = COLOR (new, x, 0);
7738 p->red = p->green = p->blue = 0xffff/2;
7739 p = COLOR (new, x, img->height - 1);
7740 p->red = p->green = p->blue = 0xffff/2;
7741 }
7742
7743 for (y = 1; y < img->height - 1; ++y)
7744 {
7745 p = COLOR (new, 1, y);
7746
7747 for (x = 1; x < img->width - 1; ++x, ++p)
7748 {
7749 int r, g, b, y1, x1;
7750
7751 r = g = b = i = 0;
7752 for (y1 = y - 1; y1 < y + 2; ++y1)
7753 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
7754 if (matrix[i])
7755 {
7756 XColor *t = COLOR (colors, x1, y1);
7757 r += matrix[i] * t->red;
7758 g += matrix[i] * t->green;
7759 b += matrix[i] * t->blue;
7760 }
7761
7762 r = (r / sum + color_adjust) & 0xffff;
7763 g = (g / sum + color_adjust) & 0xffff;
7764 b = (b / sum + color_adjust) & 0xffff;
7765 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
7766 }
7767 }
7768
7769 xfree (colors);
7770 x_from_xcolors (f, img, new);
7771
7772 #undef COLOR
7773 }
7774
7775
7776 /* Perform the pre-defined `emboss' edge-detection on image IMG
7777 on frame F. */
7778
7779 static void
7780 x_emboss (f, img)
7781 struct frame *f;
7782 struct image *img;
7783 {
7784 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
7785 }
7786
7787
7788 /* Perform the pre-defined `laplace' edge-detection on image IMG
7789 on frame F. */
7790
7791 static void
7792 x_laplace (f, img)
7793 struct frame *f;
7794 struct image *img;
7795 {
7796 x_detect_edges (f, img, laplace_matrix, 45000);
7797 }
7798
7799
7800 /* Perform edge-detection on image IMG on frame F, with specified
7801 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
7802
7803 MATRIX must be either
7804
7805 - a list of at least 9 numbers in row-major form
7806 - a vector of at least 9 numbers
7807
7808 COLOR_ADJUST nil means use a default; otherwise it must be a
7809 number. */
7810
7811 static void
7812 x_edge_detection (f, img, matrix, color_adjust)
7813 struct frame *f;
7814 struct image *img;
7815 Lisp_Object matrix, color_adjust;
7816 {
7817 int i = 0;
7818 int trans[9];
7819
7820 if (CONSP (matrix))
7821 {
7822 for (i = 0;
7823 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
7824 ++i, matrix = XCDR (matrix))
7825 trans[i] = XFLOATINT (XCAR (matrix));
7826 }
7827 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
7828 {
7829 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
7830 trans[i] = XFLOATINT (AREF (matrix, i));
7831 }
7832
7833 if (NILP (color_adjust))
7834 color_adjust = make_number (0xffff / 2);
7835
7836 if (i == 9 && NUMBERP (color_adjust))
7837 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
7838 }
7839
7840
7841 /* Transform image IMG on frame F so that it looks disabled. */
7842
7843 static void
7844 x_disable_image (f, img)
7845 struct frame *f;
7846 struct image *img;
7847 {
7848 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7849
7850 if (dpyinfo->n_planes >= 2)
7851 {
7852 /* Color (or grayscale). Convert to gray, and equalize. Just
7853 drawing such images with a stipple can look very odd, so
7854 we're using this method instead. */
7855 XColor *colors = x_to_xcolors (f, img, 1);
7856 XColor *p, *end;
7857 const int h = 15000;
7858 const int l = 30000;
7859
7860 for (p = colors, end = colors + img->width * img->height;
7861 p < end;
7862 ++p)
7863 {
7864 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
7865 int i2 = (0xffff - h - l) * i / 0xffff + l;
7866 p->red = p->green = p->blue = i2;
7867 }
7868
7869 x_from_xcolors (f, img, colors);
7870 }
7871
7872 /* Draw a cross over the disabled image, if we must or if we
7873 should. */
7874 if (dpyinfo->n_planes < 2 || cross_disabled_images)
7875 {
7876 Display *dpy = FRAME_X_DISPLAY (f);
7877 GC gc;
7878
7879 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
7880 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
7881 XDrawLine (dpy, img->pixmap, gc, 0, 0,
7882 img->width - 1, img->height - 1);
7883 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
7884 img->width - 1, 0);
7885 XFreeGC (dpy, gc);
7886
7887 if (img->mask)
7888 {
7889 gc = XCreateGC (dpy, img->mask, 0, NULL);
7890 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
7891 XDrawLine (dpy, img->mask, gc, 0, 0,
7892 img->width - 1, img->height - 1);
7893 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
7894 img->width - 1, 0);
7895 XFreeGC (dpy, gc);
7896 }
7897 }
7898 }
7899
7900
7901 /* Build a mask for image IMG which is used on frame F. FILE is the
7902 name of an image file, for error messages. HOW determines how to
7903 determine the background color of IMG. If it is a list '(R G B)',
7904 with R, G, and B being integers >= 0, take that as the color of the
7905 background. Otherwise, determine the background color of IMG
7906 heuristically. Value is non-zero if successful. */
7907
7908 static int
7909 x_build_heuristic_mask (f, img, how)
7910 struct frame *f;
7911 struct image *img;
7912 Lisp_Object how;
7913 {
7914 Display *dpy = FRAME_X_DISPLAY (f);
7915 XImage *ximg, *mask_img;
7916 int x, y, rc, look_at_corners_p;
7917 unsigned long bg = 0;
7918
7919 if (img->mask)
7920 {
7921 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
7922 img->mask = None;
7923 }
7924
7925 /* Create an image and pixmap serving as mask. */
7926 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
7927 &mask_img, &img->mask);
7928 if (!rc)
7929 return 0;
7930
7931 /* Get the X image of IMG->pixmap. */
7932 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
7933 ~0, ZPixmap);
7934
7935 /* Determine the background color of ximg. If HOW is `(R G B)'
7936 take that as color. Otherwise, try to determine the color
7937 heuristically. */
7938 look_at_corners_p = 1;
7939
7940 if (CONSP (how))
7941 {
7942 int rgb[3], i = 0;
7943
7944 while (i < 3
7945 && CONSP (how)
7946 && NATNUMP (XCAR (how)))
7947 {
7948 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
7949 how = XCDR (how);
7950 }
7951
7952 if (i == 3 && NILP (how))
7953 {
7954 char color_name[30];
7955 XColor exact, color;
7956 Colormap cmap;
7957
7958 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
7959
7960 cmap = FRAME_X_COLORMAP (f);
7961 if (XLookupColor (dpy, cmap, color_name, &exact, &color))
7962 {
7963 bg = color.pixel;
7964 look_at_corners_p = 0;
7965 }
7966 }
7967 }
7968
7969 if (look_at_corners_p)
7970 {
7971 unsigned long corners[4];
7972 int i, best_count;
7973
7974 /* Get the colors at the corners of ximg. */
7975 corners[0] = XGetPixel (ximg, 0, 0);
7976 corners[1] = XGetPixel (ximg, img->width - 1, 0);
7977 corners[2] = XGetPixel (ximg, img->width - 1, img->height - 1);
7978 corners[3] = XGetPixel (ximg, 0, img->height - 1);
7979
7980 /* Choose the most frequently found color as background. */
7981 for (i = best_count = 0; i < 4; ++i)
7982 {
7983 int j, n;
7984
7985 for (j = n = 0; j < 4; ++j)
7986 if (corners[i] == corners[j])
7987 ++n;
7988
7989 if (n > best_count)
7990 bg = corners[i], best_count = n;
7991 }
7992 }
7993
7994 /* Set all bits in mask_img to 1 whose color in ximg is different
7995 from the background color bg. */
7996 for (y = 0; y < img->height; ++y)
7997 for (x = 0; x < img->width; ++x)
7998 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
7999
8000 /* Put mask_img into img->mask. */
8001 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8002 x_destroy_x_image (mask_img);
8003 XDestroyImage (ximg);
8004
8005 return 1;
8006 }
8007
8008
8009 \f
8010 /***********************************************************************
8011 PBM (mono, gray, color)
8012 ***********************************************************************/
8013
8014 static int pbm_image_p P_ ((Lisp_Object object));
8015 static int pbm_load P_ ((struct frame *f, struct image *img));
8016 static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
8017
8018 /* The symbol `pbm' identifying images of this type. */
8019
8020 Lisp_Object Qpbm;
8021
8022 /* Indices of image specification fields in gs_format, below. */
8023
8024 enum pbm_keyword_index
8025 {
8026 PBM_TYPE,
8027 PBM_FILE,
8028 PBM_DATA,
8029 PBM_ASCENT,
8030 PBM_MARGIN,
8031 PBM_RELIEF,
8032 PBM_ALGORITHM,
8033 PBM_HEURISTIC_MASK,
8034 PBM_MASK,
8035 PBM_FOREGROUND,
8036 PBM_BACKGROUND,
8037 PBM_LAST
8038 };
8039
8040 /* Vector of image_keyword structures describing the format
8041 of valid user-defined image specifications. */
8042
8043 static struct image_keyword pbm_format[PBM_LAST] =
8044 {
8045 {":type", IMAGE_SYMBOL_VALUE, 1},
8046 {":file", IMAGE_STRING_VALUE, 0},
8047 {":data", IMAGE_STRING_VALUE, 0},
8048 {":ascent", IMAGE_ASCENT_VALUE, 0},
8049 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8050 {":relief", IMAGE_INTEGER_VALUE, 0},
8051 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8052 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8053 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8054 {":foreground", IMAGE_STRING_VALUE, 0},
8055 {":background", IMAGE_STRING_VALUE, 0}
8056 };
8057
8058 /* Structure describing the image type `pbm'. */
8059
8060 static struct image_type pbm_type =
8061 {
8062 &Qpbm,
8063 pbm_image_p,
8064 pbm_load,
8065 x_clear_image,
8066 NULL
8067 };
8068
8069
8070 /* Return non-zero if OBJECT is a valid PBM image specification. */
8071
8072 static int
8073 pbm_image_p (object)
8074 Lisp_Object object;
8075 {
8076 struct image_keyword fmt[PBM_LAST];
8077
8078 bcopy (pbm_format, fmt, sizeof fmt);
8079
8080 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
8081 return 0;
8082
8083 /* Must specify either :data or :file. */
8084 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
8085 }
8086
8087
8088 /* Scan a decimal number from *S and return it. Advance *S while
8089 reading the number. END is the end of the string. Value is -1 at
8090 end of input. */
8091
8092 static int
8093 pbm_scan_number (s, end)
8094 unsigned char **s, *end;
8095 {
8096 int c = 0, val = -1;
8097
8098 while (*s < end)
8099 {
8100 /* Skip white-space. */
8101 while (*s < end && (c = *(*s)++, isspace (c)))
8102 ;
8103
8104 if (c == '#')
8105 {
8106 /* Skip comment to end of line. */
8107 while (*s < end && (c = *(*s)++, c != '\n'))
8108 ;
8109 }
8110 else if (isdigit (c))
8111 {
8112 /* Read decimal number. */
8113 val = c - '0';
8114 while (*s < end && (c = *(*s)++, isdigit (c)))
8115 val = 10 * val + c - '0';
8116 break;
8117 }
8118 else
8119 break;
8120 }
8121
8122 return val;
8123 }
8124
8125
8126 /* Load PBM image IMG for use on frame F. */
8127
8128 static int
8129 pbm_load (f, img)
8130 struct frame *f;
8131 struct image *img;
8132 {
8133 int raw_p, x, y;
8134 int width, height, max_color_idx = 0;
8135 XImage *ximg;
8136 Lisp_Object file, specified_file;
8137 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
8138 struct gcpro gcpro1;
8139 unsigned char *contents = NULL;
8140 unsigned char *end, *p;
8141 int size;
8142
8143 specified_file = image_spec_value (img->spec, QCfile, NULL);
8144 file = Qnil;
8145 GCPRO1 (file);
8146
8147 if (STRINGP (specified_file))
8148 {
8149 file = x_find_image_file (specified_file);
8150 if (!STRINGP (file))
8151 {
8152 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8153 UNGCPRO;
8154 return 0;
8155 }
8156
8157 contents = slurp_file (XSTRING (file)->data, &size);
8158 if (contents == NULL)
8159 {
8160 image_error ("Error reading `%s'", file, Qnil);
8161 UNGCPRO;
8162 return 0;
8163 }
8164
8165 p = contents;
8166 end = contents + size;
8167 }
8168 else
8169 {
8170 Lisp_Object data;
8171 data = image_spec_value (img->spec, QCdata, NULL);
8172 p = XSTRING (data)->data;
8173 end = p + STRING_BYTES (XSTRING (data));
8174 }
8175
8176 /* Check magic number. */
8177 if (end - p < 2 || *p++ != 'P')
8178 {
8179 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
8180 error:
8181 xfree (contents);
8182 UNGCPRO;
8183 return 0;
8184 }
8185
8186 switch (*p++)
8187 {
8188 case '1':
8189 raw_p = 0, type = PBM_MONO;
8190 break;
8191
8192 case '2':
8193 raw_p = 0, type = PBM_GRAY;
8194 break;
8195
8196 case '3':
8197 raw_p = 0, type = PBM_COLOR;
8198 break;
8199
8200 case '4':
8201 raw_p = 1, type = PBM_MONO;
8202 break;
8203
8204 case '5':
8205 raw_p = 1, type = PBM_GRAY;
8206 break;
8207
8208 case '6':
8209 raw_p = 1, type = PBM_COLOR;
8210 break;
8211
8212 default:
8213 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
8214 goto error;
8215 }
8216
8217 /* Read width, height, maximum color-component. Characters
8218 starting with `#' up to the end of a line are ignored. */
8219 width = pbm_scan_number (&p, end);
8220 height = pbm_scan_number (&p, end);
8221
8222 if (type != PBM_MONO)
8223 {
8224 max_color_idx = pbm_scan_number (&p, end);
8225 if (raw_p && max_color_idx > 255)
8226 max_color_idx = 255;
8227 }
8228
8229 if (width < 0
8230 || height < 0
8231 || (type != PBM_MONO && max_color_idx < 0))
8232 goto error;
8233
8234 if (!x_create_x_image_and_pixmap (f, width, height, 0,
8235 &ximg, &img->pixmap))
8236 goto error;
8237
8238 /* Initialize the color hash table. */
8239 init_color_table ();
8240
8241 if (type == PBM_MONO)
8242 {
8243 int c = 0, g;
8244 struct image_keyword fmt[PBM_LAST];
8245 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
8246 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
8247
8248 /* Parse the image specification. */
8249 bcopy (pbm_format, fmt, sizeof fmt);
8250 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
8251
8252 /* Get foreground and background colors, maybe allocate colors. */
8253 if (fmt[PBM_FOREGROUND].count)
8254 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
8255 if (fmt[PBM_BACKGROUND].count)
8256 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
8257
8258 for (y = 0; y < height; ++y)
8259 for (x = 0; x < width; ++x)
8260 {
8261 if (raw_p)
8262 {
8263 if ((x & 7) == 0)
8264 c = *p++;
8265 g = c & 0x80;
8266 c <<= 1;
8267 }
8268 else
8269 g = pbm_scan_number (&p, end);
8270
8271 XPutPixel (ximg, x, y, g ? fg : bg);
8272 }
8273 }
8274 else
8275 {
8276 for (y = 0; y < height; ++y)
8277 for (x = 0; x < width; ++x)
8278 {
8279 int r, g, b;
8280
8281 if (type == PBM_GRAY)
8282 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
8283 else if (raw_p)
8284 {
8285 r = *p++;
8286 g = *p++;
8287 b = *p++;
8288 }
8289 else
8290 {
8291 r = pbm_scan_number (&p, end);
8292 g = pbm_scan_number (&p, end);
8293 b = pbm_scan_number (&p, end);
8294 }
8295
8296 if (r < 0 || g < 0 || b < 0)
8297 {
8298 xfree (ximg->data);
8299 ximg->data = NULL;
8300 XDestroyImage (ximg);
8301 image_error ("Invalid pixel value in image `%s'",
8302 img->spec, Qnil);
8303 goto error;
8304 }
8305
8306 /* RGB values are now in the range 0..max_color_idx.
8307 Scale this to the range 0..0xffff supported by X. */
8308 r = (double) r * 65535 / max_color_idx;
8309 g = (double) g * 65535 / max_color_idx;
8310 b = (double) b * 65535 / max_color_idx;
8311 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8312 }
8313 }
8314
8315 /* Store in IMG->colors the colors allocated for the image, and
8316 free the color table. */
8317 img->colors = colors_in_color_table (&img->ncolors);
8318 free_color_table ();
8319
8320 /* Put the image into a pixmap. */
8321 x_put_x_image (f, ximg, img->pixmap, width, height);
8322 x_destroy_x_image (ximg);
8323
8324 img->width = width;
8325 img->height = height;
8326
8327 UNGCPRO;
8328 xfree (contents);
8329 return 1;
8330 }
8331
8332
8333 \f
8334 /***********************************************************************
8335 PNG
8336 ***********************************************************************/
8337
8338 #if HAVE_PNG
8339
8340 #include <png.h>
8341
8342 /* Function prototypes. */
8343
8344 static int png_image_p P_ ((Lisp_Object object));
8345 static int png_load P_ ((struct frame *f, struct image *img));
8346
8347 /* The symbol `png' identifying images of this type. */
8348
8349 Lisp_Object Qpng;
8350
8351 /* Indices of image specification fields in png_format, below. */
8352
8353 enum png_keyword_index
8354 {
8355 PNG_TYPE,
8356 PNG_DATA,
8357 PNG_FILE,
8358 PNG_ASCENT,
8359 PNG_MARGIN,
8360 PNG_RELIEF,
8361 PNG_ALGORITHM,
8362 PNG_HEURISTIC_MASK,
8363 PNG_MASK,
8364 PNG_LAST
8365 };
8366
8367 /* Vector of image_keyword structures describing the format
8368 of valid user-defined image specifications. */
8369
8370 static struct image_keyword png_format[PNG_LAST] =
8371 {
8372 {":type", IMAGE_SYMBOL_VALUE, 1},
8373 {":data", IMAGE_STRING_VALUE, 0},
8374 {":file", IMAGE_STRING_VALUE, 0},
8375 {":ascent", IMAGE_ASCENT_VALUE, 0},
8376 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8377 {":relief", IMAGE_INTEGER_VALUE, 0},
8378 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8379 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8380 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8381 };
8382
8383 /* Structure describing the image type `png'. */
8384
8385 static struct image_type png_type =
8386 {
8387 &Qpng,
8388 png_image_p,
8389 png_load,
8390 x_clear_image,
8391 NULL
8392 };
8393
8394
8395 /* Return non-zero if OBJECT is a valid PNG image specification. */
8396
8397 static int
8398 png_image_p (object)
8399 Lisp_Object object;
8400 {
8401 struct image_keyword fmt[PNG_LAST];
8402 bcopy (png_format, fmt, sizeof fmt);
8403
8404 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
8405 return 0;
8406
8407 /* Must specify either the :data or :file keyword. */
8408 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
8409 }
8410
8411
8412 /* Error and warning handlers installed when the PNG library
8413 is initialized. */
8414
8415 static void
8416 my_png_error (png_ptr, msg)
8417 png_struct *png_ptr;
8418 char *msg;
8419 {
8420 xassert (png_ptr != NULL);
8421 image_error ("PNG error: %s", build_string (msg), Qnil);
8422 longjmp (png_ptr->jmpbuf, 1);
8423 }
8424
8425
8426 static void
8427 my_png_warning (png_ptr, msg)
8428 png_struct *png_ptr;
8429 char *msg;
8430 {
8431 xassert (png_ptr != NULL);
8432 image_error ("PNG warning: %s", build_string (msg), Qnil);
8433 }
8434
8435 /* Memory source for PNG decoding. */
8436
8437 struct png_memory_storage
8438 {
8439 unsigned char *bytes; /* The data */
8440 size_t len; /* How big is it? */
8441 int index; /* Where are we? */
8442 };
8443
8444
8445 /* Function set as reader function when reading PNG image from memory.
8446 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
8447 bytes from the input to DATA. */
8448
8449 static void
8450 png_read_from_memory (png_ptr, data, length)
8451 png_structp png_ptr;
8452 png_bytep data;
8453 png_size_t length;
8454 {
8455 struct png_memory_storage *tbr
8456 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
8457
8458 if (length > tbr->len - tbr->index)
8459 png_error (png_ptr, "Read error");
8460
8461 bcopy (tbr->bytes + tbr->index, data, length);
8462 tbr->index = tbr->index + length;
8463 }
8464
8465 /* Load PNG image IMG for use on frame F. Value is non-zero if
8466 successful. */
8467
8468 static int
8469 png_load (f, img)
8470 struct frame *f;
8471 struct image *img;
8472 {
8473 Lisp_Object file, specified_file;
8474 Lisp_Object specified_data;
8475 int x, y, i;
8476 XImage *ximg, *mask_img = NULL;
8477 struct gcpro gcpro1;
8478 png_struct *png_ptr = NULL;
8479 png_info *info_ptr = NULL, *end_info = NULL;
8480 FILE *volatile fp = NULL;
8481 png_byte sig[8];
8482 png_byte * volatile pixels = NULL;
8483 png_byte ** volatile rows = NULL;
8484 png_uint_32 width, height;
8485 int bit_depth, color_type, interlace_type;
8486 png_byte channels;
8487 png_uint_32 row_bytes;
8488 int transparent_p;
8489 char *gamma_str;
8490 double screen_gamma, image_gamma;
8491 int intent;
8492 struct png_memory_storage tbr; /* Data to be read */
8493
8494 /* Find out what file to load. */
8495 specified_file = image_spec_value (img->spec, QCfile, NULL);
8496 specified_data = image_spec_value (img->spec, QCdata, NULL);
8497 file = Qnil;
8498 GCPRO1 (file);
8499
8500 if (NILP (specified_data))
8501 {
8502 file = x_find_image_file (specified_file);
8503 if (!STRINGP (file))
8504 {
8505 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8506 UNGCPRO;
8507 return 0;
8508 }
8509
8510 /* Open the image file. */
8511 fp = fopen (XSTRING (file)->data, "rb");
8512 if (!fp)
8513 {
8514 image_error ("Cannot open image file `%s'", file, Qnil);
8515 UNGCPRO;
8516 fclose (fp);
8517 return 0;
8518 }
8519
8520 /* Check PNG signature. */
8521 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
8522 || !png_check_sig (sig, sizeof sig))
8523 {
8524 image_error ("Not a PNG file: `%s'", file, Qnil);
8525 UNGCPRO;
8526 fclose (fp);
8527 return 0;
8528 }
8529 }
8530 else
8531 {
8532 /* Read from memory. */
8533 tbr.bytes = XSTRING (specified_data)->data;
8534 tbr.len = STRING_BYTES (XSTRING (specified_data));
8535 tbr.index = 0;
8536
8537 /* Check PNG signature. */
8538 if (tbr.len < sizeof sig
8539 || !png_check_sig (tbr.bytes, sizeof sig))
8540 {
8541 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
8542 UNGCPRO;
8543 return 0;
8544 }
8545
8546 /* Need to skip past the signature. */
8547 tbr.bytes += sizeof (sig);
8548 }
8549
8550 /* Initialize read and info structs for PNG lib. */
8551 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
8552 my_png_error, my_png_warning);
8553 if (!png_ptr)
8554 {
8555 if (fp) fclose (fp);
8556 UNGCPRO;
8557 return 0;
8558 }
8559
8560 info_ptr = png_create_info_struct (png_ptr);
8561 if (!info_ptr)
8562 {
8563 png_destroy_read_struct (&png_ptr, NULL, NULL);
8564 if (fp) fclose (fp);
8565 UNGCPRO;
8566 return 0;
8567 }
8568
8569 end_info = png_create_info_struct (png_ptr);
8570 if (!end_info)
8571 {
8572 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
8573 if (fp) fclose (fp);
8574 UNGCPRO;
8575 return 0;
8576 }
8577
8578 /* Set error jump-back. We come back here when the PNG library
8579 detects an error. */
8580 if (setjmp (png_ptr->jmpbuf))
8581 {
8582 error:
8583 if (png_ptr)
8584 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8585 xfree (pixels);
8586 xfree (rows);
8587 if (fp) fclose (fp);
8588 UNGCPRO;
8589 return 0;
8590 }
8591
8592 /* Read image info. */
8593 if (!NILP (specified_data))
8594 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
8595 else
8596 png_init_io (png_ptr, fp);
8597
8598 png_set_sig_bytes (png_ptr, sizeof sig);
8599 png_read_info (png_ptr, info_ptr);
8600 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
8601 &interlace_type, NULL, NULL);
8602
8603 /* If image contains simply transparency data, we prefer to
8604 construct a clipping mask. */
8605 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
8606 transparent_p = 1;
8607 else
8608 transparent_p = 0;
8609
8610 /* This function is easier to write if we only have to handle
8611 one data format: RGB or RGBA with 8 bits per channel. Let's
8612 transform other formats into that format. */
8613
8614 /* Strip more than 8 bits per channel. */
8615 if (bit_depth == 16)
8616 png_set_strip_16 (png_ptr);
8617
8618 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
8619 if available. */
8620 png_set_expand (png_ptr);
8621
8622 /* Convert grayscale images to RGB. */
8623 if (color_type == PNG_COLOR_TYPE_GRAY
8624 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
8625 png_set_gray_to_rgb (png_ptr);
8626
8627 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
8628 gamma_str = getenv ("SCREEN_GAMMA");
8629 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
8630
8631 /* Tell the PNG lib to handle gamma correction for us. */
8632
8633 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
8634 if (png_get_sRGB (png_ptr, info_ptr, &intent))
8635 /* There is a special chunk in the image specifying the gamma. */
8636 png_set_sRGB (png_ptr, info_ptr, intent);
8637 else
8638 #endif
8639 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
8640 /* Image contains gamma information. */
8641 png_set_gamma (png_ptr, screen_gamma, image_gamma);
8642 else
8643 /* Use a default of 0.5 for the image gamma. */
8644 png_set_gamma (png_ptr, screen_gamma, 0.5);
8645
8646 /* Handle alpha channel by combining the image with a background
8647 color. Do this only if a real alpha channel is supplied. For
8648 simple transparency, we prefer a clipping mask. */
8649 if (!transparent_p)
8650 {
8651 png_color_16 *image_background;
8652
8653 if (png_get_bKGD (png_ptr, info_ptr, &image_background))
8654 /* Image contains a background color with which to
8655 combine the image. */
8656 png_set_background (png_ptr, image_background,
8657 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
8658 else
8659 {
8660 /* Image does not contain a background color with which
8661 to combine the image data via an alpha channel. Use
8662 the frame's background instead. */
8663 XColor color;
8664 Colormap cmap;
8665 png_color_16 frame_background;
8666
8667 cmap = FRAME_X_COLORMAP (f);
8668 color.pixel = FRAME_BACKGROUND_PIXEL (f);
8669 x_query_color (f, &color);
8670
8671 bzero (&frame_background, sizeof frame_background);
8672 frame_background.red = color.red;
8673 frame_background.green = color.green;
8674 frame_background.blue = color.blue;
8675
8676 png_set_background (png_ptr, &frame_background,
8677 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
8678 }
8679 }
8680
8681 /* Update info structure. */
8682 png_read_update_info (png_ptr, info_ptr);
8683
8684 /* Get number of channels. Valid values are 1 for grayscale images
8685 and images with a palette, 2 for grayscale images with transparency
8686 information (alpha channel), 3 for RGB images, and 4 for RGB
8687 images with alpha channel, i.e. RGBA. If conversions above were
8688 sufficient we should only have 3 or 4 channels here. */
8689 channels = png_get_channels (png_ptr, info_ptr);
8690 xassert (channels == 3 || channels == 4);
8691
8692 /* Number of bytes needed for one row of the image. */
8693 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
8694
8695 /* Allocate memory for the image. */
8696 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
8697 rows = (png_byte **) xmalloc (height * sizeof *rows);
8698 for (i = 0; i < height; ++i)
8699 rows[i] = pixels + i * row_bytes;
8700
8701 /* Read the entire image. */
8702 png_read_image (png_ptr, rows);
8703 png_read_end (png_ptr, info_ptr);
8704 if (fp)
8705 {
8706 fclose (fp);
8707 fp = NULL;
8708 }
8709
8710 /* Create the X image and pixmap. */
8711 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
8712 &img->pixmap))
8713 goto error;
8714
8715 /* Create an image and pixmap serving as mask if the PNG image
8716 contains an alpha channel. */
8717 if (channels == 4
8718 && !transparent_p
8719 && !x_create_x_image_and_pixmap (f, width, height, 1,
8720 &mask_img, &img->mask))
8721 {
8722 x_destroy_x_image (ximg);
8723 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
8724 img->pixmap = None;
8725 goto error;
8726 }
8727
8728 /* Fill the X image and mask from PNG data. */
8729 init_color_table ();
8730
8731 for (y = 0; y < height; ++y)
8732 {
8733 png_byte *p = rows[y];
8734
8735 for (x = 0; x < width; ++x)
8736 {
8737 unsigned r, g, b;
8738
8739 r = *p++ << 8;
8740 g = *p++ << 8;
8741 b = *p++ << 8;
8742 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8743
8744 /* An alpha channel, aka mask channel, associates variable
8745 transparency with an image. Where other image formats
8746 support binary transparency---fully transparent or fully
8747 opaque---PNG allows up to 254 levels of partial transparency.
8748 The PNG library implements partial transparency by combining
8749 the image with a specified background color.
8750
8751 I'm not sure how to handle this here nicely: because the
8752 background on which the image is displayed may change, for
8753 real alpha channel support, it would be necessary to create
8754 a new image for each possible background.
8755
8756 What I'm doing now is that a mask is created if we have
8757 boolean transparency information. Otherwise I'm using
8758 the frame's background color to combine the image with. */
8759
8760 if (channels == 4)
8761 {
8762 if (mask_img)
8763 XPutPixel (mask_img, x, y, *p > 0);
8764 ++p;
8765 }
8766 }
8767 }
8768
8769 /* Remember colors allocated for this image. */
8770 img->colors = colors_in_color_table (&img->ncolors);
8771 free_color_table ();
8772
8773 /* Clean up. */
8774 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8775 xfree (rows);
8776 xfree (pixels);
8777
8778 img->width = width;
8779 img->height = height;
8780
8781 /* Put the image into the pixmap, then free the X image and its buffer. */
8782 x_put_x_image (f, ximg, img->pixmap, width, height);
8783 x_destroy_x_image (ximg);
8784
8785 /* Same for the mask. */
8786 if (mask_img)
8787 {
8788 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8789 x_destroy_x_image (mask_img);
8790 }
8791
8792 UNGCPRO;
8793 return 1;
8794 }
8795
8796 #endif /* HAVE_PNG != 0 */
8797
8798
8799 \f
8800 /***********************************************************************
8801 JPEG
8802 ***********************************************************************/
8803
8804 #if HAVE_JPEG
8805
8806 /* Work around a warning about HAVE_STDLIB_H being redefined in
8807 jconfig.h. */
8808 #ifdef HAVE_STDLIB_H
8809 #define HAVE_STDLIB_H_1
8810 #undef HAVE_STDLIB_H
8811 #endif /* HAVE_STLIB_H */
8812
8813 #include <jpeglib.h>
8814 #include <jerror.h>
8815 #include <setjmp.h>
8816
8817 #ifdef HAVE_STLIB_H_1
8818 #define HAVE_STDLIB_H 1
8819 #endif
8820
8821 static int jpeg_image_p P_ ((Lisp_Object object));
8822 static int jpeg_load P_ ((struct frame *f, struct image *img));
8823
8824 /* The symbol `jpeg' identifying images of this type. */
8825
8826 Lisp_Object Qjpeg;
8827
8828 /* Indices of image specification fields in gs_format, below. */
8829
8830 enum jpeg_keyword_index
8831 {
8832 JPEG_TYPE,
8833 JPEG_DATA,
8834 JPEG_FILE,
8835 JPEG_ASCENT,
8836 JPEG_MARGIN,
8837 JPEG_RELIEF,
8838 JPEG_ALGORITHM,
8839 JPEG_HEURISTIC_MASK,
8840 JPEG_MASK,
8841 JPEG_LAST
8842 };
8843
8844 /* Vector of image_keyword structures describing the format
8845 of valid user-defined image specifications. */
8846
8847 static struct image_keyword jpeg_format[JPEG_LAST] =
8848 {
8849 {":type", IMAGE_SYMBOL_VALUE, 1},
8850 {":data", IMAGE_STRING_VALUE, 0},
8851 {":file", IMAGE_STRING_VALUE, 0},
8852 {":ascent", IMAGE_ASCENT_VALUE, 0},
8853 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
8854 {":relief", IMAGE_INTEGER_VALUE, 0},
8855 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8856 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8857 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
8858 };
8859
8860 /* Structure describing the image type `jpeg'. */
8861
8862 static struct image_type jpeg_type =
8863 {
8864 &Qjpeg,
8865 jpeg_image_p,
8866 jpeg_load,
8867 x_clear_image,
8868 NULL
8869 };
8870
8871
8872 /* Return non-zero if OBJECT is a valid JPEG image specification. */
8873
8874 static int
8875 jpeg_image_p (object)
8876 Lisp_Object object;
8877 {
8878 struct image_keyword fmt[JPEG_LAST];
8879
8880 bcopy (jpeg_format, fmt, sizeof fmt);
8881
8882 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
8883 return 0;
8884
8885 /* Must specify either the :data or :file keyword. */
8886 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
8887 }
8888
8889
8890 struct my_jpeg_error_mgr
8891 {
8892 struct jpeg_error_mgr pub;
8893 jmp_buf setjmp_buffer;
8894 };
8895
8896
8897 static void
8898 my_error_exit (cinfo)
8899 j_common_ptr cinfo;
8900 {
8901 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
8902 longjmp (mgr->setjmp_buffer, 1);
8903 }
8904
8905
8906 /* Init source method for JPEG data source manager. Called by
8907 jpeg_read_header() before any data is actually read. See
8908 libjpeg.doc from the JPEG lib distribution. */
8909
8910 static void
8911 our_init_source (cinfo)
8912 j_decompress_ptr cinfo;
8913 {
8914 }
8915
8916
8917 /* Fill input buffer method for JPEG data source manager. Called
8918 whenever more data is needed. We read the whole image in one step,
8919 so this only adds a fake end of input marker at the end. */
8920
8921 static boolean
8922 our_fill_input_buffer (cinfo)
8923 j_decompress_ptr cinfo;
8924 {
8925 /* Insert a fake EOI marker. */
8926 struct jpeg_source_mgr *src = cinfo->src;
8927 static JOCTET buffer[2];
8928
8929 buffer[0] = (JOCTET) 0xFF;
8930 buffer[1] = (JOCTET) JPEG_EOI;
8931
8932 src->next_input_byte = buffer;
8933 src->bytes_in_buffer = 2;
8934 return TRUE;
8935 }
8936
8937
8938 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
8939 is the JPEG data source manager. */
8940
8941 static void
8942 our_skip_input_data (cinfo, num_bytes)
8943 j_decompress_ptr cinfo;
8944 long num_bytes;
8945 {
8946 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
8947
8948 if (src)
8949 {
8950 if (num_bytes > src->bytes_in_buffer)
8951 ERREXIT (cinfo, JERR_INPUT_EOF);
8952
8953 src->bytes_in_buffer -= num_bytes;
8954 src->next_input_byte += num_bytes;
8955 }
8956 }
8957
8958
8959 /* Method to terminate data source. Called by
8960 jpeg_finish_decompress() after all data has been processed. */
8961
8962 static void
8963 our_term_source (cinfo)
8964 j_decompress_ptr cinfo;
8965 {
8966 }
8967
8968
8969 /* Set up the JPEG lib for reading an image from DATA which contains
8970 LEN bytes. CINFO is the decompression info structure created for
8971 reading the image. */
8972
8973 static void
8974 jpeg_memory_src (cinfo, data, len)
8975 j_decompress_ptr cinfo;
8976 JOCTET *data;
8977 unsigned int len;
8978 {
8979 struct jpeg_source_mgr *src;
8980
8981 if (cinfo->src == NULL)
8982 {
8983 /* First time for this JPEG object? */
8984 cinfo->src = (struct jpeg_source_mgr *)
8985 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
8986 sizeof (struct jpeg_source_mgr));
8987 src = (struct jpeg_source_mgr *) cinfo->src;
8988 src->next_input_byte = data;
8989 }
8990
8991 src = (struct jpeg_source_mgr *) cinfo->src;
8992 src->init_source = our_init_source;
8993 src->fill_input_buffer = our_fill_input_buffer;
8994 src->skip_input_data = our_skip_input_data;
8995 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
8996 src->term_source = our_term_source;
8997 src->bytes_in_buffer = len;
8998 src->next_input_byte = data;
8999 }
9000
9001
9002 /* Load image IMG for use on frame F. Patterned after example.c
9003 from the JPEG lib. */
9004
9005 static int
9006 jpeg_load (f, img)
9007 struct frame *f;
9008 struct image *img;
9009 {
9010 struct jpeg_decompress_struct cinfo;
9011 struct my_jpeg_error_mgr mgr;
9012 Lisp_Object file, specified_file;
9013 Lisp_Object specified_data;
9014 FILE * volatile fp = NULL;
9015 JSAMPARRAY buffer;
9016 int row_stride, x, y;
9017 XImage *ximg = NULL;
9018 int rc;
9019 unsigned long *colors;
9020 int width, height;
9021 struct gcpro gcpro1;
9022
9023 /* Open the JPEG file. */
9024 specified_file = image_spec_value (img->spec, QCfile, NULL);
9025 specified_data = image_spec_value (img->spec, QCdata, NULL);
9026 file = Qnil;
9027 GCPRO1 (file);
9028
9029 if (NILP (specified_data))
9030 {
9031 file = x_find_image_file (specified_file);
9032 if (!STRINGP (file))
9033 {
9034 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9035 UNGCPRO;
9036 return 0;
9037 }
9038
9039 fp = fopen (XSTRING (file)->data, "r");
9040 if (fp == NULL)
9041 {
9042 image_error ("Cannot open `%s'", file, Qnil);
9043 UNGCPRO;
9044 return 0;
9045 }
9046 }
9047
9048 /* Customize libjpeg's error handling to call my_error_exit when an
9049 error is detected. This function will perform a longjmp. */
9050 cinfo.err = jpeg_std_error (&mgr.pub);
9051 mgr.pub.error_exit = my_error_exit;
9052
9053 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
9054 {
9055 if (rc == 1)
9056 {
9057 /* Called from my_error_exit. Display a JPEG error. */
9058 char buffer[JMSG_LENGTH_MAX];
9059 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
9060 image_error ("Error reading JPEG image `%s': %s", img->spec,
9061 build_string (buffer));
9062 }
9063
9064 /* Close the input file and destroy the JPEG object. */
9065 if (fp)
9066 fclose ((FILE *) fp);
9067 jpeg_destroy_decompress (&cinfo);
9068
9069 /* If we already have an XImage, free that. */
9070 x_destroy_x_image (ximg);
9071
9072 /* Free pixmap and colors. */
9073 x_clear_image (f, img);
9074
9075 UNGCPRO;
9076 return 0;
9077 }
9078
9079 /* Create the JPEG decompression object. Let it read from fp.
9080 Read the JPEG image header. */
9081 jpeg_create_decompress (&cinfo);
9082
9083 if (NILP (specified_data))
9084 jpeg_stdio_src (&cinfo, (FILE *) fp);
9085 else
9086 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
9087 STRING_BYTES (XSTRING (specified_data)));
9088
9089 jpeg_read_header (&cinfo, TRUE);
9090
9091 /* Customize decompression so that color quantization will be used.
9092 Start decompression. */
9093 cinfo.quantize_colors = TRUE;
9094 jpeg_start_decompress (&cinfo);
9095 width = img->width = cinfo.output_width;
9096 height = img->height = cinfo.output_height;
9097
9098 /* Create X image and pixmap. */
9099 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9100 longjmp (mgr.setjmp_buffer, 2);
9101
9102 /* Allocate colors. When color quantization is used,
9103 cinfo.actual_number_of_colors has been set with the number of
9104 colors generated, and cinfo.colormap is a two-dimensional array
9105 of color indices in the range 0..cinfo.actual_number_of_colors.
9106 No more than 255 colors will be generated. */
9107 {
9108 int i, ir, ig, ib;
9109
9110 if (cinfo.out_color_components > 2)
9111 ir = 0, ig = 1, ib = 2;
9112 else if (cinfo.out_color_components > 1)
9113 ir = 0, ig = 1, ib = 0;
9114 else
9115 ir = 0, ig = 0, ib = 0;
9116
9117 /* Use the color table mechanism because it handles colors that
9118 cannot be allocated nicely. Such colors will be replaced with
9119 a default color, and we don't have to care about which colors
9120 can be freed safely, and which can't. */
9121 init_color_table ();
9122 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
9123 * sizeof *colors);
9124
9125 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
9126 {
9127 /* Multiply RGB values with 255 because X expects RGB values
9128 in the range 0..0xffff. */
9129 int r = cinfo.colormap[ir][i] << 8;
9130 int g = cinfo.colormap[ig][i] << 8;
9131 int b = cinfo.colormap[ib][i] << 8;
9132 colors[i] = lookup_rgb_color (f, r, g, b);
9133 }
9134
9135 /* Remember those colors actually allocated. */
9136 img->colors = colors_in_color_table (&img->ncolors);
9137 free_color_table ();
9138 }
9139
9140 /* Read pixels. */
9141 row_stride = width * cinfo.output_components;
9142 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
9143 row_stride, 1);
9144 for (y = 0; y < height; ++y)
9145 {
9146 jpeg_read_scanlines (&cinfo, buffer, 1);
9147 for (x = 0; x < cinfo.output_width; ++x)
9148 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
9149 }
9150
9151 /* Clean up. */
9152 jpeg_finish_decompress (&cinfo);
9153 jpeg_destroy_decompress (&cinfo);
9154 if (fp)
9155 fclose ((FILE *) fp);
9156
9157 /* Put the image into the pixmap. */
9158 x_put_x_image (f, ximg, img->pixmap, width, height);
9159 x_destroy_x_image (ximg);
9160 UNGCPRO;
9161 return 1;
9162 }
9163
9164 #endif /* HAVE_JPEG */
9165
9166
9167 \f
9168 /***********************************************************************
9169 TIFF
9170 ***********************************************************************/
9171
9172 #if HAVE_TIFF
9173
9174 #include <tiffio.h>
9175
9176 static int tiff_image_p P_ ((Lisp_Object object));
9177 static int tiff_load P_ ((struct frame *f, struct image *img));
9178
9179 /* The symbol `tiff' identifying images of this type. */
9180
9181 Lisp_Object Qtiff;
9182
9183 /* Indices of image specification fields in tiff_format, below. */
9184
9185 enum tiff_keyword_index
9186 {
9187 TIFF_TYPE,
9188 TIFF_DATA,
9189 TIFF_FILE,
9190 TIFF_ASCENT,
9191 TIFF_MARGIN,
9192 TIFF_RELIEF,
9193 TIFF_ALGORITHM,
9194 TIFF_HEURISTIC_MASK,
9195 TIFF_MASK,
9196 TIFF_LAST
9197 };
9198
9199 /* Vector of image_keyword structures describing the format
9200 of valid user-defined image specifications. */
9201
9202 static struct image_keyword tiff_format[TIFF_LAST] =
9203 {
9204 {":type", IMAGE_SYMBOL_VALUE, 1},
9205 {":data", IMAGE_STRING_VALUE, 0},
9206 {":file", IMAGE_STRING_VALUE, 0},
9207 {":ascent", IMAGE_ASCENT_VALUE, 0},
9208 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9209 {":relief", IMAGE_INTEGER_VALUE, 0},
9210 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9211 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9212 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9213 };
9214
9215 /* Structure describing the image type `tiff'. */
9216
9217 static struct image_type tiff_type =
9218 {
9219 &Qtiff,
9220 tiff_image_p,
9221 tiff_load,
9222 x_clear_image,
9223 NULL
9224 };
9225
9226
9227 /* Return non-zero if OBJECT is a valid TIFF image specification. */
9228
9229 static int
9230 tiff_image_p (object)
9231 Lisp_Object object;
9232 {
9233 struct image_keyword fmt[TIFF_LAST];
9234 bcopy (tiff_format, fmt, sizeof fmt);
9235
9236 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
9237 return 0;
9238
9239 /* Must specify either the :data or :file keyword. */
9240 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
9241 }
9242
9243
9244 /* Reading from a memory buffer for TIFF images Based on the PNG
9245 memory source, but we have to provide a lot of extra functions.
9246 Blah.
9247
9248 We really only need to implement read and seek, but I am not
9249 convinced that the TIFF library is smart enough not to destroy
9250 itself if we only hand it the function pointers we need to
9251 override. */
9252
9253 typedef struct
9254 {
9255 unsigned char *bytes;
9256 size_t len;
9257 int index;
9258 }
9259 tiff_memory_source;
9260
9261
9262 static size_t
9263 tiff_read_from_memory (data, buf, size)
9264 thandle_t data;
9265 tdata_t buf;
9266 tsize_t size;
9267 {
9268 tiff_memory_source *src = (tiff_memory_source *) data;
9269
9270 if (size > src->len - src->index)
9271 return (size_t) -1;
9272 bcopy (src->bytes + src->index, buf, size);
9273 src->index += size;
9274 return size;
9275 }
9276
9277
9278 static size_t
9279 tiff_write_from_memory (data, buf, size)
9280 thandle_t data;
9281 tdata_t buf;
9282 tsize_t size;
9283 {
9284 return (size_t) -1;
9285 }
9286
9287
9288 static toff_t
9289 tiff_seek_in_memory (data, off, whence)
9290 thandle_t data;
9291 toff_t off;
9292 int whence;
9293 {
9294 tiff_memory_source *src = (tiff_memory_source *) data;
9295 int idx;
9296
9297 switch (whence)
9298 {
9299 case SEEK_SET: /* Go from beginning of source. */
9300 idx = off;
9301 break;
9302
9303 case SEEK_END: /* Go from end of source. */
9304 idx = src->len + off;
9305 break;
9306
9307 case SEEK_CUR: /* Go from current position. */
9308 idx = src->index + off;
9309 break;
9310
9311 default: /* Invalid `whence'. */
9312 return -1;
9313 }
9314
9315 if (idx > src->len || idx < 0)
9316 return -1;
9317
9318 src->index = idx;
9319 return src->index;
9320 }
9321
9322
9323 static int
9324 tiff_close_memory (data)
9325 thandle_t data;
9326 {
9327 /* NOOP */
9328 return 0;
9329 }
9330
9331
9332 static int
9333 tiff_mmap_memory (data, pbase, psize)
9334 thandle_t data;
9335 tdata_t *pbase;
9336 toff_t *psize;
9337 {
9338 /* It is already _IN_ memory. */
9339 return 0;
9340 }
9341
9342
9343 static void
9344 tiff_unmap_memory (data, base, size)
9345 thandle_t data;
9346 tdata_t base;
9347 toff_t size;
9348 {
9349 /* We don't need to do this. */
9350 }
9351
9352
9353 static toff_t
9354 tiff_size_of_memory (data)
9355 thandle_t data;
9356 {
9357 return ((tiff_memory_source *) data)->len;
9358 }
9359
9360
9361 /* Load TIFF image IMG for use on frame F. Value is non-zero if
9362 successful. */
9363
9364 static int
9365 tiff_load (f, img)
9366 struct frame *f;
9367 struct image *img;
9368 {
9369 Lisp_Object file, specified_file;
9370 Lisp_Object specified_data;
9371 TIFF *tiff;
9372 int width, height, x, y;
9373 uint32 *buf;
9374 int rc;
9375 XImage *ximg;
9376 struct gcpro gcpro1;
9377 tiff_memory_source memsrc;
9378
9379 specified_file = image_spec_value (img->spec, QCfile, NULL);
9380 specified_data = image_spec_value (img->spec, QCdata, NULL);
9381 file = Qnil;
9382 GCPRO1 (file);
9383
9384 if (NILP (specified_data))
9385 {
9386 /* Read from a file */
9387 file = x_find_image_file (specified_file);
9388 if (!STRINGP (file))
9389 {
9390 image_error ("Cannot find image file `%s'", file, Qnil);
9391 UNGCPRO;
9392 return 0;
9393 }
9394
9395 /* Try to open the image file. */
9396 tiff = TIFFOpen (XSTRING (file)->data, "r");
9397 if (tiff == NULL)
9398 {
9399 image_error ("Cannot open `%s'", file, Qnil);
9400 UNGCPRO;
9401 return 0;
9402 }
9403 }
9404 else
9405 {
9406 /* Memory source! */
9407 memsrc.bytes = XSTRING (specified_data)->data;
9408 memsrc.len = STRING_BYTES (XSTRING (specified_data));
9409 memsrc.index = 0;
9410
9411 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
9412 (TIFFReadWriteProc) tiff_read_from_memory,
9413 (TIFFReadWriteProc) tiff_write_from_memory,
9414 tiff_seek_in_memory,
9415 tiff_close_memory,
9416 tiff_size_of_memory,
9417 tiff_mmap_memory,
9418 tiff_unmap_memory);
9419
9420 if (!tiff)
9421 {
9422 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
9423 UNGCPRO;
9424 return 0;
9425 }
9426 }
9427
9428 /* Get width and height of the image, and allocate a raster buffer
9429 of width x height 32-bit values. */
9430 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
9431 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
9432 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
9433
9434 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
9435 TIFFClose (tiff);
9436 if (!rc)
9437 {
9438 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
9439 xfree (buf);
9440 UNGCPRO;
9441 return 0;
9442 }
9443
9444 /* Create the X image and pixmap. */
9445 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9446 {
9447 xfree (buf);
9448 UNGCPRO;
9449 return 0;
9450 }
9451
9452 /* Initialize the color table. */
9453 init_color_table ();
9454
9455 /* Process the pixel raster. Origin is in the lower-left corner. */
9456 for (y = 0; y < height; ++y)
9457 {
9458 uint32 *row = buf + y * width;
9459
9460 for (x = 0; x < width; ++x)
9461 {
9462 uint32 abgr = row[x];
9463 int r = TIFFGetR (abgr) << 8;
9464 int g = TIFFGetG (abgr) << 8;
9465 int b = TIFFGetB (abgr) << 8;
9466 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
9467 }
9468 }
9469
9470 /* Remember the colors allocated for the image. Free the color table. */
9471 img->colors = colors_in_color_table (&img->ncolors);
9472 free_color_table ();
9473
9474 /* Put the image into the pixmap, then free the X image and its buffer. */
9475 x_put_x_image (f, ximg, img->pixmap, width, height);
9476 x_destroy_x_image (ximg);
9477 xfree (buf);
9478
9479 img->width = width;
9480 img->height = height;
9481
9482 UNGCPRO;
9483 return 1;
9484 }
9485
9486 #endif /* HAVE_TIFF != 0 */
9487
9488
9489 \f
9490 /***********************************************************************
9491 GIF
9492 ***********************************************************************/
9493
9494 #if HAVE_GIF
9495
9496 #include <gif_lib.h>
9497
9498 static int gif_image_p P_ ((Lisp_Object object));
9499 static int gif_load P_ ((struct frame *f, struct image *img));
9500
9501 /* The symbol `gif' identifying images of this type. */
9502
9503 Lisp_Object Qgif;
9504
9505 /* Indices of image specification fields in gif_format, below. */
9506
9507 enum gif_keyword_index
9508 {
9509 GIF_TYPE,
9510 GIF_DATA,
9511 GIF_FILE,
9512 GIF_ASCENT,
9513 GIF_MARGIN,
9514 GIF_RELIEF,
9515 GIF_ALGORITHM,
9516 GIF_HEURISTIC_MASK,
9517 GIF_MASK,
9518 GIF_IMAGE,
9519 GIF_LAST
9520 };
9521
9522 /* Vector of image_keyword structures describing the format
9523 of valid user-defined image specifications. */
9524
9525 static struct image_keyword gif_format[GIF_LAST] =
9526 {
9527 {":type", IMAGE_SYMBOL_VALUE, 1},
9528 {":data", IMAGE_STRING_VALUE, 0},
9529 {":file", IMAGE_STRING_VALUE, 0},
9530 {":ascent", IMAGE_ASCENT_VALUE, 0},
9531 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9532 {":relief", IMAGE_INTEGER_VALUE, 0},
9533 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9534 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9535 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9536 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0}
9537 };
9538
9539 /* Structure describing the image type `gif'. */
9540
9541 static struct image_type gif_type =
9542 {
9543 &Qgif,
9544 gif_image_p,
9545 gif_load,
9546 x_clear_image,
9547 NULL
9548 };
9549
9550
9551 /* Return non-zero if OBJECT is a valid GIF image specification. */
9552
9553 static int
9554 gif_image_p (object)
9555 Lisp_Object object;
9556 {
9557 struct image_keyword fmt[GIF_LAST];
9558 bcopy (gif_format, fmt, sizeof fmt);
9559
9560 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
9561 return 0;
9562
9563 /* Must specify either the :data or :file keyword. */
9564 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
9565 }
9566
9567
9568 /* Reading a GIF image from memory
9569 Based on the PNG memory stuff to a certain extent. */
9570
9571 typedef struct
9572 {
9573 unsigned char *bytes;
9574 size_t len;
9575 int index;
9576 }
9577 gif_memory_source;
9578
9579
9580 /* Make the current memory source available to gif_read_from_memory.
9581 It's done this way because not all versions of libungif support
9582 a UserData field in the GifFileType structure. */
9583 static gif_memory_source *current_gif_memory_src;
9584
9585 static int
9586 gif_read_from_memory (file, buf, len)
9587 GifFileType *file;
9588 GifByteType *buf;
9589 int len;
9590 {
9591 gif_memory_source *src = current_gif_memory_src;
9592
9593 if (len > src->len - src->index)
9594 return -1;
9595
9596 bcopy (src->bytes + src->index, buf, len);
9597 src->index += len;
9598 return len;
9599 }
9600
9601
9602 /* Load GIF image IMG for use on frame F. Value is non-zero if
9603 successful. */
9604
9605 static int
9606 gif_load (f, img)
9607 struct frame *f;
9608 struct image *img;
9609 {
9610 Lisp_Object file, specified_file;
9611 Lisp_Object specified_data;
9612 int rc, width, height, x, y, i;
9613 XImage *ximg;
9614 ColorMapObject *gif_color_map;
9615 unsigned long pixel_colors[256];
9616 GifFileType *gif;
9617 struct gcpro gcpro1;
9618 Lisp_Object image;
9619 int ino, image_left, image_top, image_width, image_height;
9620 gif_memory_source memsrc;
9621 unsigned char *raster;
9622
9623 specified_file = image_spec_value (img->spec, QCfile, NULL);
9624 specified_data = image_spec_value (img->spec, QCdata, NULL);
9625 file = Qnil;
9626 GCPRO1 (file);
9627
9628 if (NILP (specified_data))
9629 {
9630 file = x_find_image_file (specified_file);
9631 if (!STRINGP (file))
9632 {
9633 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9634 UNGCPRO;
9635 return 0;
9636 }
9637
9638 /* Open the GIF file. */
9639 gif = DGifOpenFileName (XSTRING (file)->data);
9640 if (gif == NULL)
9641 {
9642 image_error ("Cannot open `%s'", file, Qnil);
9643 UNGCPRO;
9644 return 0;
9645 }
9646 }
9647 else
9648 {
9649 /* Read from memory! */
9650 current_gif_memory_src = &memsrc;
9651 memsrc.bytes = XSTRING (specified_data)->data;
9652 memsrc.len = STRING_BYTES (XSTRING (specified_data));
9653 memsrc.index = 0;
9654
9655 gif = DGifOpen(&memsrc, gif_read_from_memory);
9656 if (!gif)
9657 {
9658 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
9659 UNGCPRO;
9660 return 0;
9661 }
9662 }
9663
9664 /* Read entire contents. */
9665 rc = DGifSlurp (gif);
9666 if (rc == GIF_ERROR)
9667 {
9668 image_error ("Error reading `%s'", img->spec, Qnil);
9669 DGifCloseFile (gif);
9670 UNGCPRO;
9671 return 0;
9672 }
9673
9674 image = image_spec_value (img->spec, QCindex, NULL);
9675 ino = INTEGERP (image) ? XFASTINT (image) : 0;
9676 if (ino >= gif->ImageCount)
9677 {
9678 image_error ("Invalid image number `%s' in image `%s'",
9679 image, img->spec);
9680 DGifCloseFile (gif);
9681 UNGCPRO;
9682 return 0;
9683 }
9684
9685 width = img->width = gif->SWidth;
9686 height = img->height = gif->SHeight;
9687
9688 /* Create the X image and pixmap. */
9689 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9690 {
9691 DGifCloseFile (gif);
9692 UNGCPRO;
9693 return 0;
9694 }
9695
9696 /* Allocate colors. */
9697 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
9698 if (!gif_color_map)
9699 gif_color_map = gif->SColorMap;
9700 init_color_table ();
9701 bzero (pixel_colors, sizeof pixel_colors);
9702
9703 for (i = 0; i < gif_color_map->ColorCount; ++i)
9704 {
9705 int r = gif_color_map->Colors[i].Red << 8;
9706 int g = gif_color_map->Colors[i].Green << 8;
9707 int b = gif_color_map->Colors[i].Blue << 8;
9708 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
9709 }
9710
9711 img->colors = colors_in_color_table (&img->ncolors);
9712 free_color_table ();
9713
9714 /* Clear the part of the screen image that are not covered by
9715 the image from the GIF file. Full animated GIF support
9716 requires more than can be done here (see the gif89 spec,
9717 disposal methods). Let's simply assume that the part
9718 not covered by a sub-image is in the frame's background color. */
9719 image_top = gif->SavedImages[ino].ImageDesc.Top;
9720 image_left = gif->SavedImages[ino].ImageDesc.Left;
9721 image_width = gif->SavedImages[ino].ImageDesc.Width;
9722 image_height = gif->SavedImages[ino].ImageDesc.Height;
9723
9724 for (y = 0; y < image_top; ++y)
9725 for (x = 0; x < width; ++x)
9726 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9727
9728 for (y = image_top + image_height; y < height; ++y)
9729 for (x = 0; x < width; ++x)
9730 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9731
9732 for (y = image_top; y < image_top + image_height; ++y)
9733 {
9734 for (x = 0; x < image_left; ++x)
9735 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9736 for (x = image_left + image_width; x < width; ++x)
9737 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
9738 }
9739
9740 /* Read the GIF image into the X image. We use a local variable
9741 `raster' here because RasterBits below is a char *, and invites
9742 problems with bytes >= 0x80. */
9743 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
9744
9745 if (gif->SavedImages[ino].ImageDesc.Interlace)
9746 {
9747 static int interlace_start[] = {0, 4, 2, 1};
9748 static int interlace_increment[] = {8, 8, 4, 2};
9749 int pass, inc;
9750 int row = interlace_start[0];
9751
9752 pass = 0;
9753
9754 for (y = 0; y < image_height; y++)
9755 {
9756 if (row >= image_height)
9757 {
9758 row = interlace_start[++pass];
9759 while (row >= image_height)
9760 row = interlace_start[++pass];
9761 }
9762
9763 for (x = 0; x < image_width; x++)
9764 {
9765 int i = raster[(y * image_width) + x];
9766 XPutPixel (ximg, x + image_left, row + image_top,
9767 pixel_colors[i]);
9768 }
9769
9770 row += interlace_increment[pass];
9771 }
9772 }
9773 else
9774 {
9775 for (y = 0; y < image_height; ++y)
9776 for (x = 0; x < image_width; ++x)
9777 {
9778 int i = raster[y * image_width + x];
9779 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
9780 }
9781 }
9782
9783 DGifCloseFile (gif);
9784
9785 /* Put the image into the pixmap, then free the X image and its buffer. */
9786 x_put_x_image (f, ximg, img->pixmap, width, height);
9787 x_destroy_x_image (ximg);
9788
9789 UNGCPRO;
9790 return 1;
9791 }
9792
9793 #endif /* HAVE_GIF != 0 */
9794
9795
9796 \f
9797 /***********************************************************************
9798 Ghostscript
9799 ***********************************************************************/
9800
9801 static int gs_image_p P_ ((Lisp_Object object));
9802 static int gs_load P_ ((struct frame *f, struct image *img));
9803 static void gs_clear_image P_ ((struct frame *f, struct image *img));
9804
9805 /* The symbol `postscript' identifying images of this type. */
9806
9807 Lisp_Object Qpostscript;
9808
9809 /* Keyword symbols. */
9810
9811 Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
9812
9813 /* Indices of image specification fields in gs_format, below. */
9814
9815 enum gs_keyword_index
9816 {
9817 GS_TYPE,
9818 GS_PT_WIDTH,
9819 GS_PT_HEIGHT,
9820 GS_FILE,
9821 GS_LOADER,
9822 GS_BOUNDING_BOX,
9823 GS_ASCENT,
9824 GS_MARGIN,
9825 GS_RELIEF,
9826 GS_ALGORITHM,
9827 GS_HEURISTIC_MASK,
9828 GS_MASK,
9829 GS_LAST
9830 };
9831
9832 /* Vector of image_keyword structures describing the format
9833 of valid user-defined image specifications. */
9834
9835 static struct image_keyword gs_format[GS_LAST] =
9836 {
9837 {":type", IMAGE_SYMBOL_VALUE, 1},
9838 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9839 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9840 {":file", IMAGE_STRING_VALUE, 1},
9841 {":loader", IMAGE_FUNCTION_VALUE, 0},
9842 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
9843 {":ascent", IMAGE_ASCENT_VALUE, 0},
9844 {":margin", IMAGE_POSITIVE_INTEGER_VALUE, 0},
9845 {":relief", IMAGE_INTEGER_VALUE, 0},
9846 {":algorithm", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9847 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9848 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
9849 };
9850
9851 /* Structure describing the image type `ghostscript'. */
9852
9853 static struct image_type gs_type =
9854 {
9855 &Qpostscript,
9856 gs_image_p,
9857 gs_load,
9858 gs_clear_image,
9859 NULL
9860 };
9861
9862
9863 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9864
9865 static void
9866 gs_clear_image (f, img)
9867 struct frame *f;
9868 struct image *img;
9869 {
9870 /* IMG->data.ptr_val may contain a recorded colormap. */
9871 xfree (img->data.ptr_val);
9872 x_clear_image (f, img);
9873 }
9874
9875
9876 /* Return non-zero if OBJECT is a valid Ghostscript image
9877 specification. */
9878
9879 static int
9880 gs_image_p (object)
9881 Lisp_Object object;
9882 {
9883 struct image_keyword fmt[GS_LAST];
9884 Lisp_Object tem;
9885 int i;
9886
9887 bcopy (gs_format, fmt, sizeof fmt);
9888
9889 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
9890 return 0;
9891
9892 /* Bounding box must be a list or vector containing 4 integers. */
9893 tem = fmt[GS_BOUNDING_BOX].value;
9894 if (CONSP (tem))
9895 {
9896 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9897 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9898 return 0;
9899 if (!NILP (tem))
9900 return 0;
9901 }
9902 else if (VECTORP (tem))
9903 {
9904 if (XVECTOR (tem)->size != 4)
9905 return 0;
9906 for (i = 0; i < 4; ++i)
9907 if (!INTEGERP (XVECTOR (tem)->contents[i]))
9908 return 0;
9909 }
9910 else
9911 return 0;
9912
9913 return 1;
9914 }
9915
9916
9917 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
9918 if successful. */
9919
9920 static int
9921 gs_load (f, img)
9922 struct frame *f;
9923 struct image *img;
9924 {
9925 char buffer[100];
9926 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9927 struct gcpro gcpro1, gcpro2;
9928 Lisp_Object frame;
9929 double in_width, in_height;
9930 Lisp_Object pixel_colors = Qnil;
9931
9932 /* Compute pixel size of pixmap needed from the given size in the
9933 image specification. Sizes in the specification are in pt. 1 pt
9934 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9935 info. */
9936 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9937 in_width = XFASTINT (pt_width) / 72.0;
9938 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
9939 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9940 in_height = XFASTINT (pt_height) / 72.0;
9941 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
9942
9943 /* Create the pixmap. */
9944 xassert (img->pixmap == None);
9945 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9946 img->width, img->height,
9947 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
9948
9949 if (!img->pixmap)
9950 {
9951 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
9952 return 0;
9953 }
9954
9955 /* Call the loader to fill the pixmap. It returns a process object
9956 if successful. We do not record_unwind_protect here because
9957 other places in redisplay like calling window scroll functions
9958 don't either. Let the Lisp loader use `unwind-protect' instead. */
9959 GCPRO2 (window_and_pixmap_id, pixel_colors);
9960
9961 sprintf (buffer, "%lu %lu",
9962 (unsigned long) FRAME_X_WINDOW (f),
9963 (unsigned long) img->pixmap);
9964 window_and_pixmap_id = build_string (buffer);
9965
9966 sprintf (buffer, "%lu %lu",
9967 FRAME_FOREGROUND_PIXEL (f),
9968 FRAME_BACKGROUND_PIXEL (f));
9969 pixel_colors = build_string (buffer);
9970
9971 XSETFRAME (frame, f);
9972 loader = image_spec_value (img->spec, QCloader, NULL);
9973 if (NILP (loader))
9974 loader = intern ("gs-load-image");
9975
9976 img->data.lisp_val = call6 (loader, frame, img->spec,
9977 make_number (img->width),
9978 make_number (img->height),
9979 window_and_pixmap_id,
9980 pixel_colors);
9981 UNGCPRO;
9982 return PROCESSP (img->data.lisp_val);
9983 }
9984
9985
9986 /* Kill the Ghostscript process that was started to fill PIXMAP on
9987 frame F. Called from XTread_socket when receiving an event
9988 telling Emacs that Ghostscript has finished drawing. */
9989
9990 void
9991 x_kill_gs_process (pixmap, f)
9992 Pixmap pixmap;
9993 struct frame *f;
9994 {
9995 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9996 int class, i;
9997 struct image *img;
9998
9999 /* Find the image containing PIXMAP. */
10000 for (i = 0; i < c->used; ++i)
10001 if (c->images[i]->pixmap == pixmap)
10002 break;
10003
10004 /* Kill the GS process. We should have found PIXMAP in the image
10005 cache and its image should contain a process object. */
10006 xassert (i < c->used);
10007 img = c->images[i];
10008 xassert (PROCESSP (img->data.lisp_val));
10009 Fkill_process (img->data.lisp_val, Qnil);
10010 img->data.lisp_val = Qnil;
10011
10012 /* On displays with a mutable colormap, figure out the colors
10013 allocated for the image by looking at the pixels of an XImage for
10014 img->pixmap. */
10015 class = FRAME_X_VISUAL (f)->class;
10016 if (class != StaticColor && class != StaticGray && class != TrueColor)
10017 {
10018 XImage *ximg;
10019
10020 BLOCK_INPUT;
10021
10022 /* Try to get an XImage for img->pixmep. */
10023 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
10024 0, 0, img->width, img->height, ~0, ZPixmap);
10025 if (ximg)
10026 {
10027 int x, y;
10028
10029 /* Initialize the color table. */
10030 init_color_table ();
10031
10032 /* For each pixel of the image, look its color up in the
10033 color table. After having done so, the color table will
10034 contain an entry for each color used by the image. */
10035 for (y = 0; y < img->height; ++y)
10036 for (x = 0; x < img->width; ++x)
10037 {
10038 unsigned long pixel = XGetPixel (ximg, x, y);
10039 lookup_pixel_color (f, pixel);
10040 }
10041
10042 /* Record colors in the image. Free color table and XImage. */
10043 img->colors = colors_in_color_table (&img->ncolors);
10044 free_color_table ();
10045 XDestroyImage (ximg);
10046
10047 #if 0 /* This doesn't seem to be the case. If we free the colors
10048 here, we get a BadAccess later in x_clear_image when
10049 freeing the colors. */
10050 /* We have allocated colors once, but Ghostscript has also
10051 allocated colors on behalf of us. So, to get the
10052 reference counts right, free them once. */
10053 if (img->ncolors)
10054 x_free_colors (f, img->colors, img->ncolors);
10055 #endif
10056 }
10057 else
10058 image_error ("Cannot get X image of `%s'; colors will not be freed",
10059 img->spec, Qnil);
10060
10061 UNBLOCK_INPUT;
10062 }
10063 }
10064
10065
10066 \f
10067 /***********************************************************************
10068 Window properties
10069 ***********************************************************************/
10070
10071 DEFUN ("x-change-window-property", Fx_change_window_property,
10072 Sx_change_window_property, 2, 3, 0,
10073 "Change window property PROP to VALUE on the X window of FRAME.\n\
10074 PROP and VALUE must be strings. FRAME nil or omitted means use the\n\
10075 selected frame. Value is VALUE.")
10076 (prop, value, frame)
10077 Lisp_Object frame, prop, value;
10078 {
10079 struct frame *f = check_x_frame (frame);
10080 Atom prop_atom;
10081
10082 CHECK_STRING (prop, 1);
10083 CHECK_STRING (value, 2);
10084
10085 BLOCK_INPUT;
10086 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10087 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10088 prop_atom, XA_STRING, 8, PropModeReplace,
10089 XSTRING (value)->data, XSTRING (value)->size);
10090
10091 /* Make sure the property is set when we return. */
10092 XFlush (FRAME_X_DISPLAY (f));
10093 UNBLOCK_INPUT;
10094
10095 return value;
10096 }
10097
10098
10099 DEFUN ("x-delete-window-property", Fx_delete_window_property,
10100 Sx_delete_window_property, 1, 2, 0,
10101 "Remove window property PROP from X window of FRAME.\n\
10102 FRAME nil or omitted means use the selected frame. Value is PROP.")
10103 (prop, frame)
10104 Lisp_Object prop, frame;
10105 {
10106 struct frame *f = check_x_frame (frame);
10107 Atom prop_atom;
10108
10109 CHECK_STRING (prop, 1);
10110 BLOCK_INPUT;
10111 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10112 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
10113
10114 /* Make sure the property is removed when we return. */
10115 XFlush (FRAME_X_DISPLAY (f));
10116 UNBLOCK_INPUT;
10117
10118 return prop;
10119 }
10120
10121
10122 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
10123 1, 2, 0,
10124 "Value is the value of window property PROP on FRAME.\n\
10125 If FRAME is nil or omitted, use the selected frame. Value is nil\n\
10126 if FRAME hasn't a property with name PROP or if PROP has no string\n\
10127 value.")
10128 (prop, frame)
10129 Lisp_Object prop, frame;
10130 {
10131 struct frame *f = check_x_frame (frame);
10132 Atom prop_atom;
10133 int rc;
10134 Lisp_Object prop_value = Qnil;
10135 char *tmp_data = NULL;
10136 Atom actual_type;
10137 int actual_format;
10138 unsigned long actual_size, bytes_remaining;
10139
10140 CHECK_STRING (prop, 1);
10141 BLOCK_INPUT;
10142 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10143 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10144 prop_atom, 0, 0, False, XA_STRING,
10145 &actual_type, &actual_format, &actual_size,
10146 &bytes_remaining, (unsigned char **) &tmp_data);
10147 if (rc == Success)
10148 {
10149 int size = bytes_remaining;
10150
10151 XFree (tmp_data);
10152 tmp_data = NULL;
10153
10154 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10155 prop_atom, 0, bytes_remaining,
10156 False, XA_STRING,
10157 &actual_type, &actual_format,
10158 &actual_size, &bytes_remaining,
10159 (unsigned char **) &tmp_data);
10160 if (rc == Success)
10161 prop_value = make_string (tmp_data, size);
10162
10163 XFree (tmp_data);
10164 }
10165
10166 UNBLOCK_INPUT;
10167 return prop_value;
10168 }
10169
10170
10171 \f
10172 /***********************************************************************
10173 Busy cursor
10174 ***********************************************************************/
10175
10176 /* If non-null, an asynchronous timer that, when it expires, displays
10177 a busy cursor on all frames. */
10178
10179 static struct atimer *busy_cursor_atimer;
10180
10181 /* Non-zero means a busy cursor is currently shown. */
10182
10183 static int busy_cursor_shown_p;
10184
10185 /* Number of seconds to wait before displaying a busy cursor. */
10186
10187 static Lisp_Object Vbusy_cursor_delay;
10188
10189 /* Default number of seconds to wait before displaying a busy
10190 cursor. */
10191
10192 #define DEFAULT_BUSY_CURSOR_DELAY 1
10193
10194 /* Function prototypes. */
10195
10196 static void show_busy_cursor P_ ((struct atimer *));
10197 static void hide_busy_cursor P_ ((void));
10198
10199
10200 /* Cancel a currently active busy-cursor timer, and start a new one. */
10201
10202 void
10203 start_busy_cursor ()
10204 {
10205 EMACS_TIME delay;
10206 int secs, usecs = 0;
10207
10208 cancel_busy_cursor ();
10209
10210 if (INTEGERP (Vbusy_cursor_delay)
10211 && XINT (Vbusy_cursor_delay) > 0)
10212 secs = XFASTINT (Vbusy_cursor_delay);
10213 else if (FLOATP (Vbusy_cursor_delay)
10214 && XFLOAT_DATA (Vbusy_cursor_delay) > 0)
10215 {
10216 Lisp_Object tem;
10217 tem = Ftruncate (Vbusy_cursor_delay, Qnil);
10218 secs = XFASTINT (tem);
10219 usecs = (XFLOAT_DATA (Vbusy_cursor_delay) - secs) * 1000000;
10220 }
10221 else
10222 secs = DEFAULT_BUSY_CURSOR_DELAY;
10223
10224 EMACS_SET_SECS_USECS (delay, secs, usecs);
10225 busy_cursor_atimer = start_atimer (ATIMER_RELATIVE, delay,
10226 show_busy_cursor, NULL);
10227 }
10228
10229
10230 /* Cancel the busy cursor timer if active, hide a busy cursor if
10231 shown. */
10232
10233 void
10234 cancel_busy_cursor ()
10235 {
10236 if (busy_cursor_atimer)
10237 {
10238 cancel_atimer (busy_cursor_atimer);
10239 busy_cursor_atimer = NULL;
10240 }
10241
10242 if (busy_cursor_shown_p)
10243 hide_busy_cursor ();
10244 }
10245
10246
10247 /* Timer function of busy_cursor_atimer. TIMER is equal to
10248 busy_cursor_atimer.
10249
10250 Display a busy cursor on all frames by mapping the frames'
10251 busy_window. Set the busy_p flag in the frames' output_data.x
10252 structure to indicate that a busy cursor is shown on the
10253 frames. */
10254
10255 static void
10256 show_busy_cursor (timer)
10257 struct atimer *timer;
10258 {
10259 /* The timer implementation will cancel this timer automatically
10260 after this function has run. Set busy_cursor_atimer to null
10261 so that we know the timer doesn't have to be canceled. */
10262 busy_cursor_atimer = NULL;
10263
10264 if (!busy_cursor_shown_p)
10265 {
10266 Lisp_Object rest, frame;
10267
10268 BLOCK_INPUT;
10269
10270 FOR_EACH_FRAME (rest, frame)
10271 {
10272 struct frame *f = XFRAME (frame);
10273
10274 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
10275 {
10276 Display *dpy = FRAME_X_DISPLAY (f);
10277
10278 #ifdef USE_X_TOOLKIT
10279 if (f->output_data.x->widget)
10280 #else
10281 if (FRAME_OUTER_WINDOW (f))
10282 #endif
10283 {
10284 f->output_data.x->busy_p = 1;
10285
10286 if (!f->output_data.x->busy_window)
10287 {
10288 unsigned long mask = CWCursor;
10289 XSetWindowAttributes attrs;
10290
10291 attrs.cursor = f->output_data.x->busy_cursor;
10292
10293 f->output_data.x->busy_window
10294 = XCreateWindow (dpy, FRAME_OUTER_WINDOW (f),
10295 0, 0, 32000, 32000, 0, 0,
10296 InputOnly,
10297 CopyFromParent,
10298 mask, &attrs);
10299 }
10300
10301 XMapRaised (dpy, f->output_data.x->busy_window);
10302 XFlush (dpy);
10303 }
10304 }
10305 }
10306
10307 busy_cursor_shown_p = 1;
10308 UNBLOCK_INPUT;
10309 }
10310 }
10311
10312
10313 /* Hide the busy cursor on all frames, if it is currently shown. */
10314
10315 static void
10316 hide_busy_cursor ()
10317 {
10318 if (busy_cursor_shown_p)
10319 {
10320 Lisp_Object rest, frame;
10321
10322 BLOCK_INPUT;
10323 FOR_EACH_FRAME (rest, frame)
10324 {
10325 struct frame *f = XFRAME (frame);
10326
10327 if (FRAME_X_P (f)
10328 /* Watch out for newly created frames. */
10329 && f->output_data.x->busy_window)
10330 {
10331 XUnmapWindow (FRAME_X_DISPLAY (f), f->output_data.x->busy_window);
10332 /* Sync here because XTread_socket looks at the busy_p flag
10333 that is reset to zero below. */
10334 XSync (FRAME_X_DISPLAY (f), False);
10335 f->output_data.x->busy_p = 0;
10336 }
10337 }
10338
10339 busy_cursor_shown_p = 0;
10340 UNBLOCK_INPUT;
10341 }
10342 }
10343
10344
10345 \f
10346 /***********************************************************************
10347 Tool tips
10348 ***********************************************************************/
10349
10350 static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
10351 Lisp_Object));
10352
10353 /* The frame of a currently visible tooltip. */
10354
10355 Lisp_Object tip_frame;
10356
10357 /* If non-nil, a timer started that hides the last tooltip when it
10358 fires. */
10359
10360 Lisp_Object tip_timer;
10361 Window tip_window;
10362
10363
10364 static Lisp_Object
10365 unwind_create_tip_frame (frame)
10366 Lisp_Object frame;
10367 {
10368 Lisp_Object deleted;
10369
10370 deleted = unwind_create_frame (frame);
10371 if (EQ (deleted, Qt))
10372 {
10373 tip_window = None;
10374 tip_frame = Qnil;
10375 }
10376
10377 return deleted;
10378 }
10379
10380
10381 /* Create a frame for a tooltip on the display described by DPYINFO.
10382 PARMS is a list of frame parameters. Value is the frame.
10383
10384 Note that functions called here, esp. x_default_parameter can
10385 signal errors, for instance when a specified color name is
10386 undefined. We have to make sure that we're in a consistent state
10387 when this happens. */
10388
10389 static Lisp_Object
10390 x_create_tip_frame (dpyinfo, parms)
10391 struct x_display_info *dpyinfo;
10392 Lisp_Object parms;
10393 {
10394 struct frame *f;
10395 Lisp_Object frame, tem;
10396 Lisp_Object name;
10397 long window_prompting = 0;
10398 int width, height;
10399 int count = BINDING_STACK_SIZE ();
10400 struct gcpro gcpro1, gcpro2, gcpro3;
10401 struct kboard *kb;
10402
10403 check_x ();
10404
10405 /* Use this general default value to start with until we know if
10406 this frame has a specified name. */
10407 Vx_resource_name = Vinvocation_name;
10408
10409 #ifdef MULTI_KBOARD
10410 kb = dpyinfo->kboard;
10411 #else
10412 kb = &the_only_kboard;
10413 #endif
10414
10415 /* Get the name of the frame to use for resource lookup. */
10416 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
10417 if (!STRINGP (name)
10418 && !EQ (name, Qunbound)
10419 && !NILP (name))
10420 error ("Invalid frame name--not a string or nil");
10421 Vx_resource_name = name;
10422
10423 frame = Qnil;
10424 GCPRO3 (parms, name, frame);
10425 f = make_frame (1);
10426 XSETFRAME (frame, f);
10427 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
10428 record_unwind_protect (unwind_create_tip_frame, frame);
10429
10430 /* By setting the output method, we're essentially saying that
10431 the frame is live, as per FRAME_LIVE_P. If we get a signal
10432 from this point on, x_destroy_window might screw up reference
10433 counts etc. */
10434 f->output_method = output_x_window;
10435 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
10436 bzero (f->output_data.x, sizeof (struct x_output));
10437 f->output_data.x->icon_bitmap = -1;
10438 f->output_data.x->fontset = -1;
10439 f->output_data.x->scroll_bar_foreground_pixel = -1;
10440 f->output_data.x->scroll_bar_background_pixel = -1;
10441 f->icon_name = Qnil;
10442 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
10443 #if GLYPH_DEBUG
10444 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
10445 dpyinfo_refcount = dpyinfo->reference_count;
10446 #endif /* GLYPH_DEBUG */
10447 #ifdef MULTI_KBOARD
10448 FRAME_KBOARD (f) = kb;
10449 #endif
10450 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10451 f->output_data.x->explicit_parent = 0;
10452
10453 /* These colors will be set anyway later, but it's important
10454 to get the color reference counts right, so initialize them! */
10455 {
10456 Lisp_Object black;
10457 struct gcpro gcpro1;
10458
10459 black = build_string ("black");
10460 GCPRO1 (black);
10461 f->output_data.x->foreground_pixel
10462 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10463 f->output_data.x->background_pixel
10464 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10465 f->output_data.x->cursor_pixel
10466 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10467 f->output_data.x->cursor_foreground_pixel
10468 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10469 f->output_data.x->border_pixel
10470 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10471 f->output_data.x->mouse_pixel
10472 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10473 UNGCPRO;
10474 }
10475
10476 /* Set the name; the functions to which we pass f expect the name to
10477 be set. */
10478 if (EQ (name, Qunbound) || NILP (name))
10479 {
10480 f->name = build_string (dpyinfo->x_id_name);
10481 f->explicit_name = 0;
10482 }
10483 else
10484 {
10485 f->name = name;
10486 f->explicit_name = 1;
10487 /* use the frame's title when getting resources for this frame. */
10488 specbind (Qx_resource_name, name);
10489 }
10490
10491 /* Extract the window parameters from the supplied values that are
10492 needed to determine window geometry. */
10493 {
10494 Lisp_Object font;
10495
10496 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
10497
10498 BLOCK_INPUT;
10499 /* First, try whatever font the caller has specified. */
10500 if (STRINGP (font))
10501 {
10502 tem = Fquery_fontset (font, Qnil);
10503 if (STRINGP (tem))
10504 font = x_new_fontset (f, XSTRING (tem)->data);
10505 else
10506 font = x_new_font (f, XSTRING (font)->data);
10507 }
10508
10509 /* Try out a font which we hope has bold and italic variations. */
10510 if (!STRINGP (font))
10511 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
10512 if (!STRINGP (font))
10513 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
10514 if (! STRINGP (font))
10515 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
10516 if (! STRINGP (font))
10517 /* This was formerly the first thing tried, but it finds too many fonts
10518 and takes too long. */
10519 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
10520 /* If those didn't work, look for something which will at least work. */
10521 if (! STRINGP (font))
10522 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
10523 UNBLOCK_INPUT;
10524 if (! STRINGP (font))
10525 font = build_string ("fixed");
10526
10527 x_default_parameter (f, parms, Qfont, font,
10528 "font", "Font", RES_TYPE_STRING);
10529 }
10530
10531 x_default_parameter (f, parms, Qborder_width, make_number (2),
10532 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
10533
10534 /* This defaults to 2 in order to match xterm. We recognize either
10535 internalBorderWidth or internalBorder (which is what xterm calls
10536 it). */
10537 if (NILP (Fassq (Qinternal_border_width, parms)))
10538 {
10539 Lisp_Object value;
10540
10541 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
10542 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
10543 if (! EQ (value, Qunbound))
10544 parms = Fcons (Fcons (Qinternal_border_width, value),
10545 parms);
10546 }
10547
10548 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
10549 "internalBorderWidth", "internalBorderWidth",
10550 RES_TYPE_NUMBER);
10551
10552 /* Also do the stuff which must be set before the window exists. */
10553 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
10554 "foreground", "Foreground", RES_TYPE_STRING);
10555 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
10556 "background", "Background", RES_TYPE_STRING);
10557 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
10558 "pointerColor", "Foreground", RES_TYPE_STRING);
10559 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
10560 "cursorColor", "Foreground", RES_TYPE_STRING);
10561 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
10562 "borderColor", "BorderColor", RES_TYPE_STRING);
10563
10564 /* Init faces before x_default_parameter is called for scroll-bar
10565 parameters because that function calls x_set_scroll_bar_width,
10566 which calls change_frame_size, which calls Fset_window_buffer,
10567 which runs hooks, which call Fvertical_motion. At the end, we
10568 end up in init_iterator with a null face cache, which should not
10569 happen. */
10570 init_frame_faces (f);
10571
10572 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10573 window_prompting = x_figure_window_size (f, parms);
10574
10575 if (window_prompting & XNegative)
10576 {
10577 if (window_prompting & YNegative)
10578 f->output_data.x->win_gravity = SouthEastGravity;
10579 else
10580 f->output_data.x->win_gravity = NorthEastGravity;
10581 }
10582 else
10583 {
10584 if (window_prompting & YNegative)
10585 f->output_data.x->win_gravity = SouthWestGravity;
10586 else
10587 f->output_data.x->win_gravity = NorthWestGravity;
10588 }
10589
10590 f->output_data.x->size_hint_flags = window_prompting;
10591 {
10592 XSetWindowAttributes attrs;
10593 unsigned long mask;
10594
10595 BLOCK_INPUT;
10596 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
10597 if (DoesSaveUnders (dpyinfo->screen))
10598 mask |= CWSaveUnder;
10599
10600 /* Window managers look at the override-redirect flag to determine
10601 whether or net to give windows a decoration (Xlib spec, chapter
10602 3.2.8). */
10603 attrs.override_redirect = True;
10604 attrs.save_under = True;
10605 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
10606 /* Arrange for getting MapNotify and UnmapNotify events. */
10607 attrs.event_mask = StructureNotifyMask;
10608 tip_window
10609 = FRAME_X_WINDOW (f)
10610 = XCreateWindow (FRAME_X_DISPLAY (f),
10611 FRAME_X_DISPLAY_INFO (f)->root_window,
10612 /* x, y, width, height */
10613 0, 0, 1, 1,
10614 /* Border. */
10615 1,
10616 CopyFromParent, InputOutput, CopyFromParent,
10617 mask, &attrs);
10618 UNBLOCK_INPUT;
10619 }
10620
10621 x_make_gc (f);
10622
10623 x_default_parameter (f, parms, Qauto_raise, Qnil,
10624 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
10625 x_default_parameter (f, parms, Qauto_lower, Qnil,
10626 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
10627 x_default_parameter (f, parms, Qcursor_type, Qbox,
10628 "cursorType", "CursorType", RES_TYPE_SYMBOL);
10629
10630 /* Dimensions, especially f->height, must be done via change_frame_size.
10631 Change will not be effected unless different from the current
10632 f->height. */
10633 width = f->width;
10634 height = f->height;
10635 f->height = 0;
10636 SET_FRAME_WIDTH (f, 0);
10637 change_frame_size (f, height, width, 1, 0, 0);
10638
10639 f->no_split = 1;
10640
10641 UNGCPRO;
10642
10643 /* It is now ok to make the frame official even if we get an error
10644 below. And the frame needs to be on Vframe_list or making it
10645 visible won't work. */
10646 Vframe_list = Fcons (frame, Vframe_list);
10647 tip_frame = frame;
10648
10649 /* Now that the frame is official, it counts as a reference to
10650 its display. */
10651 FRAME_X_DISPLAY_INFO (f)->reference_count++;
10652
10653 /* Discard the unwind_protect. */
10654 return unbind_to (count, frame);
10655 }
10656
10657
10658 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
10659 "Show STRING in a \"tooltip\" window on frame FRAME.\n\
10660 A tooltip window is a small X window displaying a string.\n\
10661 \n\
10662 FRAME nil or omitted means use the selected frame.\n\
10663 \n\
10664 PARMS is an optional list of frame parameters which can be\n\
10665 used to change the tooltip's appearance.\n\
10666 \n\
10667 Automatically hide the tooltip after TIMEOUT seconds.\n\
10668 TIMEOUT nil means use the default timeout of 5 seconds.\n\
10669 \n\
10670 If the list of frame parameters PARAMS contains a `left' parameters,\n\
10671 the tooltip is displayed at that x-position. Otherwise it is\n\
10672 displayed at the mouse position, with offset DX added (default is 5 if\n\
10673 DX isn't specified). Likewise for the y-position; if a `top' frame\n\
10674 parameter is specified, it determines the y-position of the tooltip\n\
10675 window, otherwise it is displayed at the mouse position, with offset\n\
10676 DY added (default is -10).")
10677 (string, frame, parms, timeout, dx, dy)
10678 Lisp_Object string, frame, parms, timeout, dx, dy;
10679 {
10680 struct frame *f;
10681 struct window *w;
10682 Window root, child;
10683 Lisp_Object buffer, top, left;
10684 struct buffer *old_buffer;
10685 struct text_pos pos;
10686 int i, width, height;
10687 int root_x, root_y, win_x, win_y;
10688 unsigned pmask;
10689 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
10690 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10691 int count = specpdl_ptr - specpdl;
10692
10693 specbind (Qinhibit_redisplay, Qt);
10694
10695 GCPRO4 (string, parms, frame, timeout);
10696
10697 CHECK_STRING (string, 0);
10698 f = check_x_frame (frame);
10699 if (NILP (timeout))
10700 timeout = make_number (5);
10701 else
10702 CHECK_NATNUM (timeout, 2);
10703
10704 if (NILP (dx))
10705 dx = make_number (5);
10706 else
10707 CHECK_NUMBER (dx, 5);
10708
10709 if (NILP (dy))
10710 dy = make_number (-10);
10711 else
10712 CHECK_NUMBER (dy, 6);
10713
10714 /* Hide a previous tip, if any. */
10715 Fx_hide_tip ();
10716
10717 /* Add default values to frame parameters. */
10718 if (NILP (Fassq (Qname, parms)))
10719 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
10720 if (NILP (Fassq (Qinternal_border_width, parms)))
10721 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
10722 if (NILP (Fassq (Qborder_width, parms)))
10723 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
10724 if (NILP (Fassq (Qborder_color, parms)))
10725 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
10726 if (NILP (Fassq (Qbackground_color, parms)))
10727 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
10728 parms);
10729
10730 /* Create a frame for the tooltip, and record it in the global
10731 variable tip_frame. */
10732 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms);
10733 f = XFRAME (frame);
10734
10735 /* Set up the frame's root window. Currently we use a size of 80
10736 columns x 40 lines. If someone wants to show a larger tip, he
10737 will loose. I don't think this is a realistic case. */
10738 w = XWINDOW (FRAME_ROOT_WINDOW (f));
10739 w->left = w->top = make_number (0);
10740 w->width = make_number (80);
10741 w->height = make_number (40);
10742 adjust_glyphs (f);
10743 w->pseudo_window_p = 1;
10744
10745 /* Display the tooltip text in a temporary buffer. */
10746 buffer = Fget_buffer_create (build_string (" *tip*"));
10747 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
10748 old_buffer = current_buffer;
10749 set_buffer_internal_1 (XBUFFER (buffer));
10750 Ferase_buffer ();
10751 Finsert (1, &string);
10752 clear_glyph_matrix (w->desired_matrix);
10753 clear_glyph_matrix (w->current_matrix);
10754 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
10755 try_window (FRAME_ROOT_WINDOW (f), pos);
10756
10757 /* Compute width and height of the tooltip. */
10758 width = height = 0;
10759 for (i = 0; i < w->desired_matrix->nrows; ++i)
10760 {
10761 struct glyph_row *row = &w->desired_matrix->rows[i];
10762 struct glyph *last;
10763 int row_width;
10764
10765 /* Stop at the first empty row at the end. */
10766 if (!row->enabled_p || !row->displays_text_p)
10767 break;
10768
10769 /* Let the row go over the full width of the frame. */
10770 row->full_width_p = 1;
10771
10772 /* There's a glyph at the end of rows that is used to place
10773 the cursor there. Don't include the width of this glyph. */
10774 if (row->used[TEXT_AREA])
10775 {
10776 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
10777 row_width = row->pixel_width - last->pixel_width;
10778 }
10779 else
10780 row_width = row->pixel_width;
10781
10782 height += row->height;
10783 width = max (width, row_width);
10784 }
10785
10786 /* Add the frame's internal border to the width and height the X
10787 window should have. */
10788 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
10789 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
10790
10791 /* User-specified position? */
10792 left = Fcdr (Fassq (Qleft, parms));
10793 top = Fcdr (Fassq (Qtop, parms));
10794
10795 /* Move the tooltip window where the mouse pointer is. Resize and
10796 show it. */
10797 BLOCK_INPUT;
10798 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
10799 &root, &child, &root_x, &root_y, &win_x, &win_y, &pmask);
10800 UNBLOCK_INPUT;
10801
10802 root_x += XINT (dx);
10803 root_y += XINT (dy);
10804
10805 if (INTEGERP (left))
10806 root_x = XINT (left);
10807 if (INTEGERP (top))
10808 root_y = XINT (top);
10809
10810 BLOCK_INPUT;
10811 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10812 root_x, root_y - height, width, height);
10813 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10814 UNBLOCK_INPUT;
10815
10816 /* Draw into the window. */
10817 w->must_be_updated_p = 1;
10818 update_single_window (w, 1);
10819
10820 /* Restore original current buffer. */
10821 set_buffer_internal_1 (old_buffer);
10822 windows_or_buffers_changed = old_windows_or_buffers_changed;
10823
10824 /* Let the tip disappear after timeout seconds. */
10825 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
10826 intern ("x-hide-tip"));
10827
10828 UNGCPRO;
10829 return unbind_to (count, Qnil);
10830 }
10831
10832
10833 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
10834 "Hide the current tooltip window, if there is any.\n\
10835 Value is t is tooltip was open, nil otherwise.")
10836 ()
10837 {
10838 int count;
10839 Lisp_Object deleted, frame, timer;
10840 struct gcpro gcpro1, gcpro2;
10841
10842 /* Return quickly if nothing to do. */
10843 if (NILP (tip_timer) && NILP (tip_frame))
10844 return Qnil;
10845
10846 frame = tip_frame;
10847 timer = tip_timer;
10848 GCPRO2 (frame, timer);
10849 tip_frame = tip_timer = deleted = Qnil;
10850
10851 count = BINDING_STACK_SIZE ();
10852 specbind (Qinhibit_redisplay, Qt);
10853 specbind (Qinhibit_quit, Qt);
10854
10855 if (!NILP (timer))
10856 call1 (intern ("cancel-timer"), timer);
10857
10858 if (FRAMEP (frame))
10859 {
10860 Fdelete_frame (frame, Qnil);
10861 deleted = Qt;
10862
10863 #ifdef USE_LUCID
10864 /* Bloodcurdling hack alert: The Lucid menu bar widget's
10865 redisplay procedure is not called when a tip frame over menu
10866 items is unmapped. Redisplay the menu manually... */
10867 {
10868 struct frame *f = SELECTED_FRAME ();
10869 Widget w = f->output_data.x->menubar_widget;
10870 extern void xlwmenu_redisplay P_ ((Widget));
10871
10872 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
10873 && w != None)
10874 {
10875 BLOCK_INPUT;
10876 xlwmenu_redisplay (w);
10877 UNBLOCK_INPUT;
10878 }
10879 }
10880 #endif /* USE_LUCID */
10881 }
10882
10883 UNGCPRO;
10884 return unbind_to (count, deleted);
10885 }
10886
10887
10888 \f
10889 /***********************************************************************
10890 File selection dialog
10891 ***********************************************************************/
10892
10893 #ifdef USE_MOTIF
10894
10895 /* Callback for "OK" and "Cancel" on file selection dialog. */
10896
10897 static void
10898 file_dialog_cb (widget, client_data, call_data)
10899 Widget widget;
10900 XtPointer call_data, client_data;
10901 {
10902 int *result = (int *) client_data;
10903 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
10904 *result = cb->reason;
10905 }
10906
10907
10908 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
10909 "Read file name, prompting with PROMPT in directory DIR.\n\
10910 Use a file selection dialog.\n\
10911 Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
10912 specified. Don't let the user enter a file name in the file\n\
10913 selection dialog's entry field, if MUSTMATCH is non-nil.")
10914 (prompt, dir, default_filename, mustmatch)
10915 Lisp_Object prompt, dir, default_filename, mustmatch;
10916 {
10917 int result;
10918 struct frame *f = SELECTED_FRAME ();
10919 Lisp_Object file = Qnil;
10920 Widget dialog, text, list, help;
10921 Arg al[10];
10922 int ac = 0;
10923 extern XtAppContext Xt_app_con;
10924 char *title;
10925 XmString dir_xmstring, pattern_xmstring;
10926 int popup_activated_flag;
10927 int count = specpdl_ptr - specpdl;
10928 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
10929
10930 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
10931 CHECK_STRING (prompt, 0);
10932 CHECK_STRING (dir, 1);
10933
10934 /* Prevent redisplay. */
10935 specbind (Qinhibit_redisplay, Qt);
10936
10937 BLOCK_INPUT;
10938
10939 /* Create the dialog with PROMPT as title, using DIR as initial
10940 directory and using "*" as pattern. */
10941 dir = Fexpand_file_name (dir, Qnil);
10942 dir_xmstring = XmStringCreateLocalized (XSTRING (dir)->data);
10943 pattern_xmstring = XmStringCreateLocalized ("*");
10944
10945 XtSetArg (al[ac], XmNtitle, XSTRING (prompt)->data); ++ac;
10946 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
10947 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
10948 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
10949 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
10950 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
10951 "fsb", al, ac);
10952 XmStringFree (dir_xmstring);
10953 XmStringFree (pattern_xmstring);
10954
10955 /* Add callbacks for OK and Cancel. */
10956 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
10957 (XtPointer) &result);
10958 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
10959 (XtPointer) &result);
10960
10961 /* Disable the help button since we can't display help. */
10962 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
10963 XtSetSensitive (help, False);
10964
10965 /* Mark OK button as default. */
10966 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
10967 XmNshowAsDefault, True, NULL);
10968
10969 /* If MUSTMATCH is non-nil, disable the file entry field of the
10970 dialog, so that the user must select a file from the files list
10971 box. We can't remove it because we wouldn't have a way to get at
10972 the result file name, then. */
10973 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
10974 if (!NILP (mustmatch))
10975 {
10976 Widget label;
10977 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
10978 XtSetSensitive (text, False);
10979 XtSetSensitive (label, False);
10980 }
10981
10982 /* Manage the dialog, so that list boxes get filled. */
10983 XtManageChild (dialog);
10984
10985 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
10986 must include the path for this to work. */
10987 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
10988 if (STRINGP (default_filename))
10989 {
10990 XmString default_xmstring;
10991 int item_pos;
10992
10993 default_xmstring
10994 = XmStringCreateLocalized (XSTRING (default_filename)->data);
10995
10996 if (!XmListItemExists (list, default_xmstring))
10997 {
10998 /* Add a new item if DEFAULT_FILENAME is not in the list. */
10999 XmListAddItem (list, default_xmstring, 0);
11000 item_pos = 0;
11001 }
11002 else
11003 item_pos = XmListItemPos (list, default_xmstring);
11004 XmStringFree (default_xmstring);
11005
11006 /* Select the item and scroll it into view. */
11007 XmListSelectPos (list, item_pos, True);
11008 XmListSetPos (list, item_pos);
11009 }
11010
11011 #ifdef HAVE_MOTIF_2_1
11012
11013 /* Process events until the user presses Cancel or OK. */
11014 result = 0;
11015 while (result == 0 || XtAppPending (Xt_app_con))
11016 XtAppProcessEvent (Xt_app_con, XtIMAll);
11017
11018 #else /* not HAVE_MOTIF_2_1 */
11019
11020 /* Process all events until the user presses Cancel or OK. */
11021 for (result = 0; result == 0;)
11022 {
11023 XEvent event;
11024 Widget widget, parent;
11025
11026 XtAppNextEvent (Xt_app_con, &event);
11027
11028 /* See if the receiver of the event is one of the widgets of
11029 the file selection dialog. If so, dispatch it. If not,
11030 discard it. */
11031 widget = XtWindowToWidget (event.xany.display, event.xany.window);
11032 parent = widget;
11033 while (parent && parent != dialog)
11034 parent = XtParent (parent);
11035
11036 if (parent == dialog
11037 || (event.type == Expose
11038 && !process_expose_from_menu (event)))
11039 XtDispatchEvent (&event);
11040 }
11041
11042 #endif /* not HAVE_MOTIF_2_1 */
11043
11044 /* Get the result. */
11045 if (result == XmCR_OK)
11046 {
11047 XmString text;
11048 String data;
11049
11050 XtVaGetValues (dialog, XmNtextString, &text, NULL);
11051 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
11052 XmStringFree (text);
11053 file = build_string (data);
11054 XtFree (data);
11055 }
11056 else
11057 file = Qnil;
11058
11059 /* Clean up. */
11060 XtUnmanageChild (dialog);
11061 XtDestroyWidget (dialog);
11062 UNBLOCK_INPUT;
11063 UNGCPRO;
11064
11065 /* Make "Cancel" equivalent to C-g. */
11066 if (NILP (file))
11067 Fsignal (Qquit, Qnil);
11068
11069 return unbind_to (count, file);
11070 }
11071
11072 #endif /* USE_MOTIF */
11073
11074
11075 \f
11076 /***********************************************************************
11077 Keyboard
11078 ***********************************************************************/
11079
11080 #ifdef HAVE_XKBGETKEYBOARD
11081 #include <X11/XKBlib.h>
11082 #include <X11/keysym.h>
11083 #endif
11084
11085 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
11086 Sx_backspace_delete_keys_p, 0, 1, 0,
11087 "Check if both Backspace and Delete keys are on the keyboard of FRAME.\n\
11088 FRAME nil means use the selected frame.\n\
11089 Value is t if we know that both keys are present, and are mapped to the\n\
11090 usual X keysyms.")
11091 (frame)
11092 Lisp_Object frame;
11093 {
11094 #ifdef HAVE_XKBGETKEYBOARD
11095 XkbDescPtr kb;
11096 struct frame *f = check_x_frame (frame);
11097 Display *dpy = FRAME_X_DISPLAY (f);
11098 Lisp_Object have_keys;
11099 int major, minor, op, event, error;
11100
11101 BLOCK_INPUT;
11102
11103 /* Check library version in case we're dynamically linked. */
11104 major = XkbMajorVersion;
11105 minor = XkbMinorVersion;
11106 if (!XkbLibraryVersion (&major, &minor))
11107 return Qnil;
11108
11109 /* Check that the server supports XKB. */
11110 major = XkbMajorVersion;
11111 minor = XkbMinorVersion;
11112 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
11113 return Qnil;
11114
11115 have_keys = Qnil;
11116 kb = XkbGetKeyboard (dpy, XkbAllComponentsMask, XkbUseCoreKbd);
11117 if (kb)
11118 {
11119 int delete_keycode = 0, backspace_keycode = 0, i;
11120
11121 for (i = kb->min_key_code;
11122 (i < kb->max_key_code
11123 && (delete_keycode == 0 || backspace_keycode == 0));
11124 ++i)
11125 {
11126 /* The XKB symbolic key names can be seen most easily
11127 in the PS file generated by `xkbprint -label name $DISPLAY'. */
11128 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
11129 delete_keycode = i;
11130 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
11131 backspace_keycode = i;
11132 }
11133
11134 XkbFreeKeyboard (kb, 0, True);
11135
11136 if (delete_keycode
11137 && backspace_keycode
11138 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
11139 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
11140 have_keys = Qt;
11141 }
11142 UNBLOCK_INPUT;
11143 return have_keys;
11144 #else /* not HAVE_XKBGETKEYBOARD */
11145 return Qnil;
11146 #endif /* not HAVE_XKBGETKEYBOARD */
11147 }
11148
11149
11150 \f
11151 /***********************************************************************
11152 Initialization
11153 ***********************************************************************/
11154
11155 void
11156 syms_of_xfns ()
11157 {
11158 /* This is zero if not using X windows. */
11159 x_in_use = 0;
11160
11161 /* The section below is built by the lisp expression at the top of the file,
11162 just above where these variables are declared. */
11163 /*&&& init symbols here &&&*/
11164 Qauto_raise = intern ("auto-raise");
11165 staticpro (&Qauto_raise);
11166 Qauto_lower = intern ("auto-lower");
11167 staticpro (&Qauto_lower);
11168 Qbar = intern ("bar");
11169 staticpro (&Qbar);
11170 Qborder_color = intern ("border-color");
11171 staticpro (&Qborder_color);
11172 Qborder_width = intern ("border-width");
11173 staticpro (&Qborder_width);
11174 Qbox = intern ("box");
11175 staticpro (&Qbox);
11176 Qcursor_color = intern ("cursor-color");
11177 staticpro (&Qcursor_color);
11178 Qcursor_type = intern ("cursor-type");
11179 staticpro (&Qcursor_type);
11180 Qgeometry = intern ("geometry");
11181 staticpro (&Qgeometry);
11182 Qicon_left = intern ("icon-left");
11183 staticpro (&Qicon_left);
11184 Qicon_top = intern ("icon-top");
11185 staticpro (&Qicon_top);
11186 Qicon_type = intern ("icon-type");
11187 staticpro (&Qicon_type);
11188 Qicon_name = intern ("icon-name");
11189 staticpro (&Qicon_name);
11190 Qinternal_border_width = intern ("internal-border-width");
11191 staticpro (&Qinternal_border_width);
11192 Qleft = intern ("left");
11193 staticpro (&Qleft);
11194 Qright = intern ("right");
11195 staticpro (&Qright);
11196 Qmouse_color = intern ("mouse-color");
11197 staticpro (&Qmouse_color);
11198 Qnone = intern ("none");
11199 staticpro (&Qnone);
11200 Qparent_id = intern ("parent-id");
11201 staticpro (&Qparent_id);
11202 Qscroll_bar_width = intern ("scroll-bar-width");
11203 staticpro (&Qscroll_bar_width);
11204 Qsuppress_icon = intern ("suppress-icon");
11205 staticpro (&Qsuppress_icon);
11206 Qundefined_color = intern ("undefined-color");
11207 staticpro (&Qundefined_color);
11208 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
11209 staticpro (&Qvertical_scroll_bars);
11210 Qvisibility = intern ("visibility");
11211 staticpro (&Qvisibility);
11212 Qwindow_id = intern ("window-id");
11213 staticpro (&Qwindow_id);
11214 Qouter_window_id = intern ("outer-window-id");
11215 staticpro (&Qouter_window_id);
11216 Qx_frame_parameter = intern ("x-frame-parameter");
11217 staticpro (&Qx_frame_parameter);
11218 Qx_resource_name = intern ("x-resource-name");
11219 staticpro (&Qx_resource_name);
11220 Quser_position = intern ("user-position");
11221 staticpro (&Quser_position);
11222 Quser_size = intern ("user-size");
11223 staticpro (&Quser_size);
11224 Qscroll_bar_foreground = intern ("scroll-bar-foreground");
11225 staticpro (&Qscroll_bar_foreground);
11226 Qscroll_bar_background = intern ("scroll-bar-background");
11227 staticpro (&Qscroll_bar_background);
11228 Qscreen_gamma = intern ("screen-gamma");
11229 staticpro (&Qscreen_gamma);
11230 Qline_spacing = intern ("line-spacing");
11231 staticpro (&Qline_spacing);
11232 Qcenter = intern ("center");
11233 staticpro (&Qcenter);
11234 Qcompound_text = intern ("compound-text");
11235 staticpro (&Qcompound_text);
11236 /* This is the end of symbol initialization. */
11237
11238 /* Text property `display' should be nonsticky by default. */
11239 Vtext_property_default_nonsticky
11240 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
11241
11242
11243 Qlaplace = intern ("laplace");
11244 staticpro (&Qlaplace);
11245 Qemboss = intern ("emboss");
11246 staticpro (&Qemboss);
11247 Qedge_detection = intern ("edge-detection");
11248 staticpro (&Qedge_detection);
11249 Qheuristic = intern ("heuristic");
11250 staticpro (&Qheuristic);
11251 QCmatrix = intern (":matrix");
11252 staticpro (&QCmatrix);
11253 QCcolor_adjustment = intern (":color-adjustment");
11254 staticpro (&QCcolor_adjustment);
11255 QCmask = intern (":mask");
11256 staticpro (&QCmask);
11257
11258 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
11259 staticpro (&Qface_set_after_frame_default);
11260
11261 Fput (Qundefined_color, Qerror_conditions,
11262 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
11263 Fput (Qundefined_color, Qerror_message,
11264 build_string ("Undefined color"));
11265
11266 init_x_parm_symbols ();
11267
11268 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
11269 "Non-nil means always draw a cross over disabled images.\n\
11270 Disabled images are those having an `:algorithm disabled' property.\n\
11271 A cross is always drawn on black & white displays.");
11272 cross_disabled_images = 0;
11273
11274 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
11275 "List of directories to search for bitmap files for X.");
11276 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
11277
11278 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
11279 "The shape of the pointer when over text.\n\
11280 Changing the value does not affect existing frames\n\
11281 unless you set the mouse color.");
11282 Vx_pointer_shape = Qnil;
11283
11284 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
11285 "The name Emacs uses to look up X resources.\n\
11286 `x-get-resource' uses this as the first component of the instance name\n\
11287 when requesting resource values.\n\
11288 Emacs initially sets `x-resource-name' to the name under which Emacs\n\
11289 was invoked, or to the value specified with the `-name' or `-rn'\n\
11290 switches, if present.\n\
11291 \n\
11292 It may be useful to bind this variable locally around a call\n\
11293 to `x-get-resource'. See also the variable `x-resource-class'.");
11294 Vx_resource_name = Qnil;
11295
11296 DEFVAR_LISP ("x-resource-class", &Vx_resource_class,
11297 "The class Emacs uses to look up X resources.\n\
11298 `x-get-resource' uses this as the first component of the instance class\n\
11299 when requesting resource values.\n\
11300 Emacs initially sets `x-resource-class' to \"Emacs\".\n\
11301 \n\
11302 Setting this variable permanently is not a reasonable thing to do,\n\
11303 but binding this variable locally around a call to `x-get-resource'\n\
11304 is a reasonable practice. See also the variable `x-resource-name'.");
11305 Vx_resource_class = build_string (EMACS_CLASS);
11306
11307 #if 0 /* This doesn't really do anything. */
11308 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
11309 "The shape of the pointer when not over text.\n\
11310 This variable takes effect when you create a new frame\n\
11311 or when you set the mouse color.");
11312 #endif
11313 Vx_nontext_pointer_shape = Qnil;
11314
11315 DEFVAR_LISP ("x-busy-pointer-shape", &Vx_busy_pointer_shape,
11316 "The shape of the pointer when Emacs is busy.\n\
11317 This variable takes effect when you create a new frame\n\
11318 or when you set the mouse color.");
11319 Vx_busy_pointer_shape = Qnil;
11320
11321 DEFVAR_BOOL ("display-busy-cursor", &display_busy_cursor_p,
11322 "Non-zero means Emacs displays a busy cursor on window systems.");
11323 display_busy_cursor_p = 1;
11324
11325 DEFVAR_LISP ("busy-cursor-delay", &Vbusy_cursor_delay,
11326 "*Seconds to wait before displaying a busy-cursor.\n\
11327 Value must be an integer or float.");
11328 Vbusy_cursor_delay = make_number (DEFAULT_BUSY_CURSOR_DELAY);
11329
11330 #if 0 /* This doesn't really do anything. */
11331 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
11332 "The shape of the pointer when over the mode line.\n\
11333 This variable takes effect when you create a new frame\n\
11334 or when you set the mouse color.");
11335 #endif
11336 Vx_mode_pointer_shape = Qnil;
11337
11338 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
11339 &Vx_sensitive_text_pointer_shape,
11340 "The shape of the pointer when over mouse-sensitive text.\n\
11341 This variable takes effect when you create a new frame\n\
11342 or when you set the mouse color.");
11343 Vx_sensitive_text_pointer_shape = Qnil;
11344
11345 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
11346 &Vx_window_horizontal_drag_shape,
11347 "Pointer shape to use for indicating a window can be dragged horizontally.\n\
11348 This variable takes effect when you create a new frame\n\
11349 or when you set the mouse color.");
11350 Vx_window_horizontal_drag_shape = Qnil;
11351
11352 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
11353 "A string indicating the foreground color of the cursor box.");
11354 Vx_cursor_fore_pixel = Qnil;
11355
11356 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
11357 "Non-nil if no X window manager is in use.\n\
11358 Emacs doesn't try to figure this out; this is always nil\n\
11359 unless you set it to something else.");
11360 /* We don't have any way to find this out, so set it to nil
11361 and maybe the user would like to set it to t. */
11362 Vx_no_window_manager = Qnil;
11363
11364 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
11365 &Vx_pixel_size_width_font_regexp,
11366 "Regexp matching a font name whose width is the same as `PIXEL_SIZE'.\n\
11367 \n\
11368 Since Emacs gets width of a font matching with this regexp from\n\
11369 PIXEL_SIZE field of the name, font finding mechanism gets faster for\n\
11370 such a font. This is especially effective for such large fonts as\n\
11371 Chinese, Japanese, and Korean.");
11372 Vx_pixel_size_width_font_regexp = Qnil;
11373
11374 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
11375 "Time after which cached images are removed from the cache.\n\
11376 When an image has not been displayed this many seconds, remove it\n\
11377 from the image cache. Value must be an integer or nil with nil\n\
11378 meaning don't clear the cache.");
11379 Vimage_cache_eviction_delay = make_number (30 * 60);
11380
11381 #ifdef USE_X_TOOLKIT
11382 Fprovide (intern ("x-toolkit"));
11383 #endif
11384 #ifdef USE_MOTIF
11385 Fprovide (intern ("motif"));
11386 #endif
11387
11388 defsubr (&Sx_get_resource);
11389
11390 /* X window properties. */
11391 defsubr (&Sx_change_window_property);
11392 defsubr (&Sx_delete_window_property);
11393 defsubr (&Sx_window_property);
11394
11395 defsubr (&Sxw_display_color_p);
11396 defsubr (&Sx_display_grayscale_p);
11397 defsubr (&Sxw_color_defined_p);
11398 defsubr (&Sxw_color_values);
11399 defsubr (&Sx_server_max_request_size);
11400 defsubr (&Sx_server_vendor);
11401 defsubr (&Sx_server_version);
11402 defsubr (&Sx_display_pixel_width);
11403 defsubr (&Sx_display_pixel_height);
11404 defsubr (&Sx_display_mm_width);
11405 defsubr (&Sx_display_mm_height);
11406 defsubr (&Sx_display_screens);
11407 defsubr (&Sx_display_planes);
11408 defsubr (&Sx_display_color_cells);
11409 defsubr (&Sx_display_visual_class);
11410 defsubr (&Sx_display_backing_store);
11411 defsubr (&Sx_display_save_under);
11412 defsubr (&Sx_parse_geometry);
11413 defsubr (&Sx_create_frame);
11414 defsubr (&Sx_open_connection);
11415 defsubr (&Sx_close_connection);
11416 defsubr (&Sx_display_list);
11417 defsubr (&Sx_synchronize);
11418 defsubr (&Sx_focus_frame);
11419 defsubr (&Sx_backspace_delete_keys_p);
11420
11421 /* Setting callback functions for fontset handler. */
11422 get_font_info_func = x_get_font_info;
11423
11424 #if 0 /* This function pointer doesn't seem to be used anywhere.
11425 And the pointer assigned has the wrong type, anyway. */
11426 list_fonts_func = x_list_fonts;
11427 #endif
11428
11429 load_font_func = x_load_font;
11430 find_ccl_program_func = x_find_ccl_program;
11431 query_font_func = x_query_font;
11432 set_frame_fontset_func = x_set_font;
11433 check_window_system_func = check_x;
11434
11435 /* Images. */
11436 Qxbm = intern ("xbm");
11437 staticpro (&Qxbm);
11438 QCtype = intern (":type");
11439 staticpro (&QCtype);
11440 QCalgorithm = intern (":algorithm");
11441 staticpro (&QCalgorithm);
11442 QCheuristic_mask = intern (":heuristic-mask");
11443 staticpro (&QCheuristic_mask);
11444 QCcolor_symbols = intern (":color-symbols");
11445 staticpro (&QCcolor_symbols);
11446 QCascent = intern (":ascent");
11447 staticpro (&QCascent);
11448 QCmargin = intern (":margin");
11449 staticpro (&QCmargin);
11450 QCrelief = intern (":relief");
11451 staticpro (&QCrelief);
11452 Qpostscript = intern ("postscript");
11453 staticpro (&Qpostscript);
11454 QCloader = intern (":loader");
11455 staticpro (&QCloader);
11456 QCbounding_box = intern (":bounding-box");
11457 staticpro (&QCbounding_box);
11458 QCpt_width = intern (":pt-width");
11459 staticpro (&QCpt_width);
11460 QCpt_height = intern (":pt-height");
11461 staticpro (&QCpt_height);
11462 QCindex = intern (":index");
11463 staticpro (&QCindex);
11464 Qpbm = intern ("pbm");
11465 staticpro (&Qpbm);
11466
11467 #if HAVE_XPM
11468 Qxpm = intern ("xpm");
11469 staticpro (&Qxpm);
11470 #endif
11471
11472 #if HAVE_JPEG
11473 Qjpeg = intern ("jpeg");
11474 staticpro (&Qjpeg);
11475 #endif
11476
11477 #if HAVE_TIFF
11478 Qtiff = intern ("tiff");
11479 staticpro (&Qtiff);
11480 #endif
11481
11482 #if HAVE_GIF
11483 Qgif = intern ("gif");
11484 staticpro (&Qgif);
11485 #endif
11486
11487 #if HAVE_PNG
11488 Qpng = intern ("png");
11489 staticpro (&Qpng);
11490 #endif
11491
11492 defsubr (&Sclear_image_cache);
11493 defsubr (&Simage_size);
11494 defsubr (&Simage_mask_p);
11495
11496 busy_cursor_atimer = NULL;
11497 busy_cursor_shown_p = 0;
11498
11499 defsubr (&Sx_show_tip);
11500 defsubr (&Sx_hide_tip);
11501 tip_timer = Qnil;
11502 staticpro (&tip_timer);
11503 tip_frame = Qnil;
11504 staticpro (&tip_frame);
11505
11506 #ifdef USE_MOTIF
11507 defsubr (&Sx_file_dialog);
11508 #endif
11509 }
11510
11511
11512 void
11513 init_xfns ()
11514 {
11515 image_types = NULL;
11516 Vimage_types = Qnil;
11517
11518 define_image_type (&xbm_type);
11519 define_image_type (&gs_type);
11520 define_image_type (&pbm_type);
11521
11522 #if HAVE_XPM
11523 define_image_type (&xpm_type);
11524 #endif
11525
11526 #if HAVE_JPEG
11527 define_image_type (&jpeg_type);
11528 #endif
11529
11530 #if HAVE_TIFF
11531 define_image_type (&tiff_type);
11532 #endif
11533
11534 #if HAVE_GIF
11535 define_image_type (&gif_type);
11536 #endif
11537
11538 #if HAVE_PNG
11539 define_image_type (&png_type);
11540 #endif
11541 }
11542
11543 #endif /* HAVE_X_WINDOWS */