Don't include strings.h.
[bpt/emacs.git] / src / xfns.c
CommitLineData
01f1ba30 1/* Functions for the X window system.
edf36fe6 2 Copyright (C) 1989, 92, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
333b20bb 3 Free Software Foundation.
01f1ba30
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
1113d9db 9the Free Software Foundation; either version 2, or (at your option)
01f1ba30
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
01f1ba30 21
c389a86d 22#include <config.h>
68c45bf0 23#include <signal.h>
333b20bb 24#include <stdio.h>
d62c8769 25#include <math.h>
c389a86d 26
3ecaf7e5
RS
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30
40e6f148 31/* This makes the fields of a Display accessible, in Xlib header files. */
333b20bb 32
40e6f148
RS
33#define XLIB_ILLEGAL_ACCESS
34
01f1ba30
JB
35#include "lisp.h"
36#include "xterm.h"
f676886a 37#include "frame.h"
01f1ba30
JB
38#include "window.h"
39#include "buffer.h"
58cad5ed 40#include "intervals.h"
01f1ba30 41#include "dispextern.h"
1f98fa48 42#include "keyboard.h"
9ac0d9e0 43#include "blockinput.h"
57bda87a 44#include <epaths.h>
0f8c4c4f 45#include "character.h"
942ea06d 46#include "charset.h"
96db09e4 47#include "coding.h"
942ea06d 48#include "fontset.h"
333b20bb
GM
49#include "systime.h"
50#include "termhooks.h"
4ae9a85e 51#include "atimer.h"
01f1ba30
JB
52
53#ifdef HAVE_X_WINDOWS
67ba84d1 54
67ba84d1 55#include <ctype.h>
63cec32f
GM
56#include <sys/types.h>
57#include <sys/stat.h>
01f1ba30 58
0a93081c 59#ifndef VMS
0505a740 60#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
ef493a27
RS
61#include "bitmaps/gray.xbm"
62#else
dbc4e1c1 63#include <X11/bitmaps/gray>
ef493a27 64#endif
0a93081c
JB
65#else
66#include "[.bitmaps]gray.xbm"
67#endif
dbc4e1c1 68
9ef48a9d
RS
69#ifdef USE_X_TOOLKIT
70#include <X11/Shell.h>
71
398ffa92 72#ifndef USE_MOTIF
9ef48a9d
RS
73#include <X11/Xaw/Paned.h>
74#include <X11/Xaw/Label.h>
398ffa92 75#endif /* USE_MOTIF */
9ef48a9d
RS
76
77#ifdef USG
78#undef USG /* ####KLUDGE for Solaris 2.2 and up */
79#include <X11/Xos.h>
80#define USG
81#else
82#include <X11/Xos.h>
83#endif
84
85#include "widget.h"
86
87#include "../lwlib/lwlib.h"
88
333b20bb
GM
89#ifdef USE_MOTIF
90#include <Xm/Xm.h>
91#include <Xm/DialogS.h>
92#include <Xm/FileSB.h>
93#endif
94
3b882b1d
RS
95/* Do the EDITRES protocol if running X11R5
96 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
333b20bb 97
3b882b1d 98#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
6c32dd68 99#define HACK_EDITRES
b9dc4443 100extern void _XEditResCheckMessages ();
6c32dd68
PR
101#endif /* R5 + Athena */
102
333b20bb
GM
103/* Unique id counter for widgets created by the Lucid Widget Library. */
104
6c32dd68
PR
105extern LWLIB_ID widget_id_tick;
106
e3881aa0 107#ifdef USE_LUCID
82c90203 108/* This is part of a kludge--see lwlib/xlwmenu.c. */
03e2c340 109extern XFontStruct *xlwmenu_default_font;
e3881aa0 110#endif
9ef48a9d 111
6bc20398 112extern void free_frame_menubar ();
d62c8769 113extern double atof ();
333b20bb 114
fc2cdd9a
GM
115#ifdef USE_MOTIF
116
117/* LessTif/Motif version info. */
118
119static Lisp_Object Vmotif_version_string;
120
121#endif /* USE_MOTIF */
122
9ef48a9d
RS
123#endif /* USE_X_TOOLKIT */
124
9d317b2c
RS
125#ifdef HAVE_X11R4
126#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
127#else
128#define MAXREQUEST(dpy) ((dpy)->max_request_size)
129#endif
130
333b20bb
GM
131/* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
132 it, and including `bitmaps/gray' more than once is a problem when
133 config.h defines `static' as an empty replacement string. */
134
135int gray_bitmap_width = gray_width;
136int gray_bitmap_height = gray_height;
62906360 137char *gray_bitmap_bits = gray_bits;
333b20bb 138
498e9ac3 139/* The name we're using in resource queries. Most often "emacs". */
333b20bb 140
d387c960 141Lisp_Object Vx_resource_name;
ac63d3d6 142
498e9ac3
RS
143/* The application class we're using in resource queries.
144 Normally "Emacs". */
333b20bb 145
498e9ac3
RS
146Lisp_Object Vx_resource_class;
147
0af913d7 148/* Non-zero means we're allowed to display an hourglass cursor. */
333b20bb 149
0af913d7 150int display_hourglass_p;
333b20bb 151
01f1ba30 152/* The background and shape of the mouse pointer, and shape when not
b9dc4443 153 over text or in the modeline. */
333b20bb 154
01f1ba30 155Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
0af913d7 156Lisp_Object Vx_hourglass_pointer_shape;
333b20bb 157
ca0ecbf5 158/* The shape when over mouse-sensitive text. */
333b20bb 159
ca0ecbf5 160Lisp_Object Vx_sensitive_text_pointer_shape;
01f1ba30 161
8fb4ec9c
GM
162/* If non-nil, the pointer shape to indicate that windows can be
163 dragged horizontally. */
164
165Lisp_Object Vx_window_horizontal_drag_shape;
166
b9dc4443 167/* Color of chars displayed in cursor box. */
333b20bb 168
01f1ba30
JB
169Lisp_Object Vx_cursor_fore_pixel;
170
b9dc4443 171/* Nonzero if using X. */
333b20bb 172
b9dc4443 173static int x_in_use;
01f1ba30 174
b9dc4443 175/* Non nil if no window manager is in use. */
333b20bb 176
01f1ba30
JB
177Lisp_Object Vx_no_window_manager;
178
f1c7b5a6 179/* Search path for bitmap files. */
333b20bb 180
f1c7b5a6
RS
181Lisp_Object Vx_bitmap_file_path;
182
942ea06d 183/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
333b20bb 184
942ea06d
KH
185Lisp_Object Vx_pixel_size_width_font_regexp;
186
f9942c9e
JB
187Lisp_Object Qauto_raise;
188Lisp_Object Qauto_lower;
dbc4e1c1 189Lisp_Object Qbar;
f9942c9e
JB
190Lisp_Object Qborder_color;
191Lisp_Object Qborder_width;
dbc4e1c1 192Lisp_Object Qbox;
f9942c9e 193Lisp_Object Qcursor_color;
dbc4e1c1 194Lisp_Object Qcursor_type;
f9942c9e 195Lisp_Object Qgeometry;
f9942c9e
JB
196Lisp_Object Qicon_left;
197Lisp_Object Qicon_top;
198Lisp_Object Qicon_type;
80534dd6 199Lisp_Object Qicon_name;
f9942c9e
JB
200Lisp_Object Qinternal_border_width;
201Lisp_Object Qleft;
1ab3d87e 202Lisp_Object Qright;
f9942c9e 203Lisp_Object Qmouse_color;
baaed68e 204Lisp_Object Qnone;
2cbebefb 205Lisp_Object Qouter_window_id;
f9942c9e 206Lisp_Object Qparent_id;
4701395c 207Lisp_Object Qscroll_bar_width;
8af1d7ca 208Lisp_Object Qsuppress_icon;
333b20bb 209extern Lisp_Object Qtop;
01f1ba30 210Lisp_Object Qundefined_color;
a3c87d4e 211Lisp_Object Qvertical_scroll_bars;
49795535 212Lisp_Object Qvisibility;
f9942c9e 213Lisp_Object Qwindow_id;
f676886a 214Lisp_Object Qx_frame_parameter;
9ef48a9d 215Lisp_Object Qx_resource_name;
4fe1de12
RS
216Lisp_Object Quser_position;
217Lisp_Object Quser_size;
0cafb359 218extern Lisp_Object Qdisplay;
333b20bb 219Lisp_Object Qscroll_bar_foreground, Qscroll_bar_background;
7c7ff7f5 220Lisp_Object Qscreen_gamma, Qline_spacing, Qcenter;
ae782866 221Lisp_Object Qcompound_text, Qcancel_timer;
ea0a1f53 222Lisp_Object Qwait_for_wm;
49d41073
EZ
223Lisp_Object Qfullscreen;
224Lisp_Object Qfullwidth;
225Lisp_Object Qfullheight;
226Lisp_Object Qfullboth;
01f1ba30 227
b9dc4443 228/* The below are defined in frame.c. */
333b20bb 229
baaed68e 230extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
c2304e02 231extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
9ea173e8 232extern Lisp_Object Qtool_bar_lines;
f9942c9e 233
01f1ba30
JB
234extern Lisp_Object Vwindow_system_version;
235
a367641f 236Lisp_Object Qface_set_after_frame_default;
333b20bb 237
f1d2ce7f 238#if GLYPH_DEBUG
eaf1eea9
GM
239int image_cache_refcount, dpyinfo_refcount;
240#endif
241
242
01f1ba30 243\f
11ae94fe 244/* Error if we are not connected to X. */
333b20bb 245
7fc9de26 246void
11ae94fe
RS
247check_x ()
248{
b9dc4443 249 if (! x_in_use)
11ae94fe
RS
250 error ("X windows are not in use or not initialized");
251}
252
1c59f5df
RS
253/* Nonzero if we can use mouse menus.
254 You should not call this unless HAVE_MENUS is defined. */
75cc8ee5
RS
255
256int
1c59f5df 257have_menus_p ()
75cc8ee5 258{
b9dc4443
RS
259 return x_in_use;
260}
261
262/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
263 and checking validity for X. */
264
265FRAME_PTR
266check_x_frame (frame)
267 Lisp_Object frame;
268{
269 FRAME_PTR f;
270
271 if (NILP (frame))
0fe92f72 272 frame = selected_frame;
b7826503 273 CHECK_LIVE_FRAME (frame);
0fe92f72 274 f = XFRAME (frame);
b9dc4443 275 if (! FRAME_X_P (f))
1c59f5df 276 error ("Non-X frame used");
b9dc4443 277 return f;
75cc8ee5
RS
278}
279
b9dc4443
RS
280/* Let the user specify an X display with a frame.
281 nil stands for the selected frame--or, if that is not an X frame,
282 the first X display on the list. */
283
284static struct x_display_info *
285check_x_display_info (frame)
286 Lisp_Object frame;
287{
8ec8a5ec
GM
288 struct x_display_info *dpyinfo = NULL;
289
b9dc4443
RS
290 if (NILP (frame))
291 {
0fe92f72
GM
292 struct frame *sf = XFRAME (selected_frame);
293
294 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
8ec8a5ec 295 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
b9dc4443 296 else if (x_display_list != 0)
8ec8a5ec 297 dpyinfo = x_display_list;
b9dc4443
RS
298 else
299 error ("X windows are not in use or not initialized");
300 }
301 else if (STRINGP (frame))
8ec8a5ec 302 dpyinfo = x_display_info_for_name (frame);
b9dc4443
RS
303 else
304 {
305 FRAME_PTR f;
306
b7826503 307 CHECK_LIVE_FRAME (frame);
b9dc4443
RS
308 f = XFRAME (frame);
309 if (! FRAME_X_P (f))
1c59f5df 310 error ("Non-X frame used");
8ec8a5ec 311 dpyinfo = FRAME_X_DISPLAY_INFO (f);
b9dc4443 312 }
8ec8a5ec
GM
313
314 return dpyinfo;
b9dc4443 315}
333b20bb 316
b9dc4443 317\f
f676886a
JB
318/* Return the Emacs frame-object corresponding to an X window.
319 It could be the frame's main window or an icon window. */
01f1ba30 320
34ca5317 321/* This function can be called during GC, so use GC_xxx type test macros. */
bcb2db92 322
f676886a 323struct frame *
2d271e2e
KH
324x_window_to_frame (dpyinfo, wdesc)
325 struct x_display_info *dpyinfo;
01f1ba30
JB
326 int wdesc;
327{
f676886a
JB
328 Lisp_Object tail, frame;
329 struct frame *f;
01f1ba30 330
8e713be6 331 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
01f1ba30 332 {
8e713be6 333 frame = XCAR (tail);
34ca5317 334 if (!GC_FRAMEP (frame))
01f1ba30 335 continue;
f676886a 336 f = XFRAME (frame);
2d764c78 337 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
2d271e2e 338 continue;
0af913d7 339 if (f->output_data.x->hourglass_window == wdesc)
17cbbf95 340 return f;
9ef48a9d 341#ifdef USE_X_TOOLKIT
7556890b
RS
342 if ((f->output_data.x->edit_widget
343 && XtWindow (f->output_data.x->edit_widget) == wdesc)
333b20bb
GM
344 /* A tooltip frame? */
345 || (!f->output_data.x->edit_widget
346 && FRAME_X_WINDOW (f) == wdesc)
7556890b 347 || f->output_data.x->icon_desc == wdesc)
9ef48a9d
RS
348 return f;
349#else /* not USE_X_TOOLKIT */
fe24a618 350 if (FRAME_X_WINDOW (f) == wdesc
7556890b 351 || f->output_data.x->icon_desc == wdesc)
f676886a 352 return f;
9ef48a9d
RS
353#endif /* not USE_X_TOOLKIT */
354 }
355 return 0;
356}
357
358#ifdef USE_X_TOOLKIT
359/* Like x_window_to_frame but also compares the window with the widget's
360 windows. */
361
362struct frame *
2d271e2e
KH
363x_any_window_to_frame (dpyinfo, wdesc)
364 struct x_display_info *dpyinfo;
9ef48a9d
RS
365 int wdesc;
366{
367 Lisp_Object tail, frame;
17cbbf95 368 struct frame *f, *found;
7556890b 369 struct x_output *x;
9ef48a9d 370
17cbbf95
GM
371 found = NULL;
372 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
9ef48a9d 373 {
8e713be6 374 frame = XCAR (tail);
34ca5317 375 if (!GC_FRAMEP (frame))
9ef48a9d 376 continue;
17cbbf95 377
9ef48a9d 378 f = XFRAME (frame);
17cbbf95 379 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
333b20bb 380 {
17cbbf95
GM
381 /* This frame matches if the window is any of its widgets. */
382 x = f->output_data.x;
0af913d7 383 if (x->hourglass_window == wdesc)
17cbbf95
GM
384 found = f;
385 else if (x->widget)
386 {
387 if (wdesc == XtWindow (x->widget)
388 || wdesc == XtWindow (x->column_widget)
389 || wdesc == XtWindow (x->edit_widget))
390 found = f;
391 /* Match if the window is this frame's menubar. */
392 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
393 found = f;
394 }
395 else if (FRAME_X_WINDOW (f) == wdesc)
396 /* A tooltip frame. */
397 found = f;
333b20bb 398 }
01f1ba30 399 }
17cbbf95
GM
400
401 return found;
01f1ba30 402}
5e65b9ab 403
5fbc3f3a
KH
404/* Likewise, but exclude the menu bar widget. */
405
406struct frame *
407x_non_menubar_window_to_frame (dpyinfo, wdesc)
408 struct x_display_info *dpyinfo;
409 int wdesc;
410{
411 Lisp_Object tail, frame;
412 struct frame *f;
7556890b 413 struct x_output *x;
5fbc3f3a 414
8e713be6 415 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
5fbc3f3a 416 {
8e713be6 417 frame = XCAR (tail);
5fbc3f3a
KH
418 if (!GC_FRAMEP (frame))
419 continue;
420 f = XFRAME (frame);
2d764c78 421 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
5fbc3f3a 422 continue;
7556890b 423 x = f->output_data.x;
5fbc3f3a 424 /* This frame matches if the window is any of its widgets. */
0af913d7 425 if (x->hourglass_window == wdesc)
17cbbf95
GM
426 return f;
427 else if (x->widget)
333b20bb
GM
428 {
429 if (wdesc == XtWindow (x->widget)
430 || wdesc == XtWindow (x->column_widget)
431 || wdesc == XtWindow (x->edit_widget))
432 return f;
433 }
434 else if (FRAME_X_WINDOW (f) == wdesc)
435 /* A tooltip frame. */
5fbc3f3a
KH
436 return f;
437 }
438 return 0;
439}
440
fd3a3022
RS
441/* Likewise, but consider only the menu bar widget. */
442
443struct frame *
444x_menubar_window_to_frame (dpyinfo, wdesc)
445 struct x_display_info *dpyinfo;
446 int wdesc;
447{
448 Lisp_Object tail, frame;
449 struct frame *f;
7556890b 450 struct x_output *x;
fd3a3022 451
8e713be6 452 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
fd3a3022 453 {
8e713be6 454 frame = XCAR (tail);
fd3a3022
RS
455 if (!GC_FRAMEP (frame))
456 continue;
457 f = XFRAME (frame);
2d764c78 458 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
fd3a3022 459 continue;
7556890b 460 x = f->output_data.x;
fd3a3022 461 /* Match if the window is this frame's menubar. */
333b20bb
GM
462 if (x->menubar_widget
463 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
fd3a3022
RS
464 return f;
465 }
466 return 0;
467}
468
5e65b9ab
RS
469/* Return the frame whose principal (outermost) window is WDESC.
470 If WDESC is some other (smaller) window, we return 0. */
471
472struct frame *
2d271e2e
KH
473x_top_window_to_frame (dpyinfo, wdesc)
474 struct x_display_info *dpyinfo;
5e65b9ab
RS
475 int wdesc;
476{
477 Lisp_Object tail, frame;
478 struct frame *f;
7556890b 479 struct x_output *x;
5e65b9ab 480
8e713be6 481 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
5e65b9ab 482 {
8e713be6 483 frame = XCAR (tail);
34ca5317 484 if (!GC_FRAMEP (frame))
5e65b9ab
RS
485 continue;
486 f = XFRAME (frame);
2d764c78 487 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
2d271e2e 488 continue;
7556890b 489 x = f->output_data.x;
333b20bb
GM
490
491 if (x->widget)
492 {
493 /* This frame matches if the window is its topmost widget. */
494 if (wdesc == XtWindow (x->widget))
495 return f;
7a994728
KH
496#if 0 /* I don't know why it did this,
497 but it seems logically wrong,
498 and it causes trouble for MapNotify events. */
333b20bb
GM
499 /* Match if the window is this frame's menubar. */
500 if (x->menubar_widget
501 && wdesc == XtWindow (x->menubar_widget))
502 return f;
7a994728 503#endif
333b20bb
GM
504 }
505 else if (FRAME_X_WINDOW (f) == wdesc)
506 /* Tooltip frame. */
507 return f;
5e65b9ab
RS
508 }
509 return 0;
510}
9ef48a9d 511#endif /* USE_X_TOOLKIT */
01f1ba30 512
01f1ba30 513\f
203c1d73
RS
514
515/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
516 id, which is just an int that this section returns. Bitmaps are
517 reference counted so they can be shared among frames.
518
519 Bitmap indices are guaranteed to be > 0, so a negative number can
520 be used to indicate no bitmap.
521
522 If you use x_create_bitmap_from_data, then you must keep track of
523 the bitmaps yourself. That is, creating a bitmap from the same
b9dc4443 524 data more than once will not be caught. */
203c1d73
RS
525
526
f1c7b5a6
RS
527/* Functions to access the contents of a bitmap, given an id. */
528
529int
530x_bitmap_height (f, id)
531 FRAME_PTR f;
532 int id;
533{
08a90d6a 534 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
f1c7b5a6
RS
535}
536
537int
538x_bitmap_width (f, id)
539 FRAME_PTR f;
540 int id;
541{
08a90d6a 542 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
f1c7b5a6
RS
543}
544
545int
546x_bitmap_pixmap (f, id)
547 FRAME_PTR f;
548 int id;
549{
08a90d6a 550 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
f1c7b5a6
RS
551}
552
553
203c1d73
RS
554/* Allocate a new bitmap record. Returns index of new record. */
555
556static int
08a90d6a
RS
557x_allocate_bitmap_record (f)
558 FRAME_PTR f;
203c1d73 559{
08a90d6a
RS
560 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
561 int i;
562
563 if (dpyinfo->bitmaps == NULL)
203c1d73 564 {
08a90d6a
RS
565 dpyinfo->bitmaps_size = 10;
566 dpyinfo->bitmaps
567 = (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
568 dpyinfo->bitmaps_last = 1;
203c1d73
RS
569 return 1;
570 }
571
08a90d6a
RS
572 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
573 return ++dpyinfo->bitmaps_last;
203c1d73 574
08a90d6a
RS
575 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
576 if (dpyinfo->bitmaps[i].refcount == 0)
577 return i + 1;
203c1d73 578
08a90d6a
RS
579 dpyinfo->bitmaps_size *= 2;
580 dpyinfo->bitmaps
581 = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
582 dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
583 return ++dpyinfo->bitmaps_last;
203c1d73
RS
584}
585
586/* Add one reference to the reference count of the bitmap with id ID. */
587
588void
f1c7b5a6
RS
589x_reference_bitmap (f, id)
590 FRAME_PTR f;
203c1d73
RS
591 int id;
592{
08a90d6a 593 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
203c1d73
RS
594}
595
596/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
597
598int
599x_create_bitmap_from_data (f, bits, width, height)
600 struct frame *f;
601 char *bits;
602 unsigned int width, height;
603{
08a90d6a 604 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
203c1d73
RS
605 Pixmap bitmap;
606 int id;
607
b9dc4443 608 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
203c1d73
RS
609 bits, width, height);
610
611 if (! bitmap)
612 return -1;
613
08a90d6a
RS
614 id = x_allocate_bitmap_record (f);
615 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
616 dpyinfo->bitmaps[id - 1].file = NULL;
617 dpyinfo->bitmaps[id - 1].refcount = 1;
618 dpyinfo->bitmaps[id - 1].depth = 1;
619 dpyinfo->bitmaps[id - 1].height = height;
620 dpyinfo->bitmaps[id - 1].width = width;
203c1d73
RS
621
622 return id;
623}
624
625/* Create bitmap from file FILE for frame F. */
626
627int
628x_create_bitmap_from_file (f, file)
629 struct frame *f;
f1c7b5a6 630 Lisp_Object file;
203c1d73 631{
08a90d6a 632 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
203c1d73
RS
633 unsigned int width, height;
634 Pixmap bitmap;
635 int xhot, yhot, result, id;
f1c7b5a6
RS
636 Lisp_Object found;
637 int fd;
638 char *filename;
203c1d73
RS
639
640 /* Look for an existing bitmap with the same name. */
08a90d6a 641 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
203c1d73 642 {
08a90d6a
RS
643 if (dpyinfo->bitmaps[id].refcount
644 && dpyinfo->bitmaps[id].file
645 && !strcmp (dpyinfo->bitmaps[id].file, (char *) XSTRING (file)->data))
203c1d73 646 {
08a90d6a 647 ++dpyinfo->bitmaps[id].refcount;
203c1d73
RS
648 return id + 1;
649 }
650 }
651
f1c7b5a6 652 /* Search bitmap-file-path for the file, if appropriate. */
c0ec53ad 653 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, 0);
f1c7b5a6
RS
654 if (fd < 0)
655 return -1;
68c45bf0 656 emacs_close (fd);
f1c7b5a6
RS
657
658 filename = (char *) XSTRING (found)->data;
659
b9dc4443 660 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
f1c7b5a6 661 filename, &width, &height, &bitmap, &xhot, &yhot);
203c1d73
RS
662 if (result != BitmapSuccess)
663 return -1;
664
08a90d6a
RS
665 id = x_allocate_bitmap_record (f);
666 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
667 dpyinfo->bitmaps[id - 1].refcount = 1;
9f2a85b2 668 dpyinfo->bitmaps[id - 1].file
fc932ac6 669 = (char *) xmalloc (STRING_BYTES (XSTRING (file)) + 1);
08a90d6a
RS
670 dpyinfo->bitmaps[id - 1].depth = 1;
671 dpyinfo->bitmaps[id - 1].height = height;
672 dpyinfo->bitmaps[id - 1].width = width;
673 strcpy (dpyinfo->bitmaps[id - 1].file, XSTRING (file)->data);
203c1d73
RS
674
675 return id;
676}
677
678/* Remove reference to bitmap with id number ID. */
679
968b1234 680void
f1c7b5a6
RS
681x_destroy_bitmap (f, id)
682 FRAME_PTR f;
203c1d73
RS
683 int id;
684{
08a90d6a
RS
685 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
686
203c1d73
RS
687 if (id > 0)
688 {
08a90d6a
RS
689 --dpyinfo->bitmaps[id - 1].refcount;
690 if (dpyinfo->bitmaps[id - 1].refcount == 0)
203c1d73 691 {
ed662bdd 692 BLOCK_INPUT;
08a90d6a
RS
693 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
694 if (dpyinfo->bitmaps[id - 1].file)
203c1d73 695 {
333b20bb 696 xfree (dpyinfo->bitmaps[id - 1].file);
08a90d6a 697 dpyinfo->bitmaps[id - 1].file = NULL;
203c1d73 698 }
ed662bdd 699 UNBLOCK_INPUT;
203c1d73
RS
700 }
701 }
702}
703
08a90d6a 704/* Free all the bitmaps for the display specified by DPYINFO. */
203c1d73 705
08a90d6a
RS
706static void
707x_destroy_all_bitmaps (dpyinfo)
708 struct x_display_info *dpyinfo;
203c1d73 709{
08a90d6a
RS
710 int i;
711 for (i = 0; i < dpyinfo->bitmaps_last; i++)
712 if (dpyinfo->bitmaps[i].refcount > 0)
713 {
714 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
715 if (dpyinfo->bitmaps[i].file)
333b20bb 716 xfree (dpyinfo->bitmaps[i].file);
08a90d6a
RS
717 }
718 dpyinfo->bitmaps_last = 0;
203c1d73
RS
719}
720\f
f676886a 721/* Connect the frame-parameter names for X frames
01f1ba30
JB
722 to the ways of passing the parameter values to the window system.
723
724 The name of a parameter, as a Lisp symbol,
f676886a 725 has an `x-frame-parameter' property which is an integer in Lisp
9fb026ab 726 that is an index in this table. */
01f1ba30 727
f676886a 728struct x_frame_parm_table
01f1ba30
JB
729{
730 char *name;
d62c8769 731 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
01f1ba30
JB
732};
733
eaf1eea9
GM
734static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
735static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
52de7ce9 736static void x_change_window_heights P_ ((Lisp_Object, int));
14819cb3 737static void x_disable_image P_ ((struct frame *, struct image *));
d62c8769 738void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
563b67aa 739static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
ea0a1f53 740static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
49d41073 741static void x_set_fullscreen P_ ((struct frame *, Lisp_Object, Lisp_Object));
d62c8769
GM
742void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
743void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
744void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
745void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
746void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
747void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
748void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
b3ba0aa8 749static void x_set_fringe_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
d62c8769
GM
750void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
751void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
752void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
753 Lisp_Object));
754void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
755void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
756void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
757void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
758 Lisp_Object));
759void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
760void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
761void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
762void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
763void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
9ea173e8 764void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
333b20bb
GM
765void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
766 Lisp_Object));
767void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
768 Lisp_Object));
769static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
770 Lisp_Object,
771 Lisp_Object,
772 char *, char *,
773 int));
d62c8769 774static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
4a8e312c
GM
775static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
776 Lisp_Object));
b243755a
GM
777static void init_color_table P_ ((void));
778static void free_color_table P_ ((void));
779static unsigned long *colors_in_color_table P_ ((int *n));
780static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
781static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
782
783
01f1ba30 784
f676886a 785static struct x_frame_parm_table x_frame_parms[] =
01f1ba30 786{
9908a324
PJ
787 {"auto-raise", x_set_autoraise},
788 {"auto-lower", x_set_autolower},
789 {"background-color", x_set_background_color},
790 {"border-color", x_set_border_color},
791 {"border-width", x_set_border_width},
792 {"cursor-color", x_set_cursor_color},
793 {"cursor-type", x_set_cursor_type},
794 {"font", x_set_font},
795 {"foreground-color", x_set_foreground_color},
796 {"icon-name", x_set_icon_name},
797 {"icon-type", x_set_icon_type},
798 {"internal-border-width", x_set_internal_border_width},
799 {"menu-bar-lines", x_set_menu_bar_lines},
800 {"mouse-color", x_set_mouse_color},
801 {"name", x_explicitly_set_name},
802 {"scroll-bar-width", x_set_scroll_bar_width},
803 {"title", x_set_title},
804 {"unsplittable", x_set_unsplittable},
805 {"vertical-scroll-bars", x_set_vertical_scroll_bars},
806 {"visibility", x_set_visibility},
807 {"tool-bar-lines", x_set_tool_bar_lines},
808 {"scroll-bar-foreground", x_set_scroll_bar_foreground},
809 {"scroll-bar-background", x_set_scroll_bar_background},
810 {"screen-gamma", x_set_screen_gamma},
811 {"line-spacing", x_set_line_spacing},
812 {"left-fringe", x_set_fringe_width},
813 {"right-fringe", x_set_fringe_width},
49d41073
EZ
814 {"wait-for-wm", x_set_wait_for_wm},
815 {"fullscreen", x_set_fullscreen},
816
01f1ba30
JB
817};
818
f676886a 819/* Attach the `x-frame-parameter' properties to
01f1ba30
JB
820 the Lisp symbol names of parameters relevant to X. */
821
201d8c78 822void
01f1ba30
JB
823init_x_parm_symbols ()
824{
825 int i;
826
d043f1a4 827 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
f676886a 828 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
01f1ba30
JB
829 make_number (i));
830}
831\f
49d41073
EZ
832
833/* Really try to move where we want to be in case of fullscreen. Some WMs
834 moves the window where we tell them. Some (mwm, twm) moves the outer
835 window manager window there instead.
836 Try to compensate for those WM here. */
837static void
838x_fullscreen_move (f, new_top, new_left)
839 struct frame *f;
840 int new_top;
841 int new_left;
842{
843 if (new_top != f->output_data.x->top_pos
844 || new_left != f->output_data.x->left_pos)
845 {
846 int move_x = new_left + f->output_data.x->x_pixels_outer_diff;
847 int move_y = new_top + f->output_data.x->y_pixels_outer_diff;
848
849 f->output_data.x->want_fullscreen |= FULLSCREEN_MOVE_WAIT;
850 x_set_offset (f, move_x, move_y, 1);
851 }
852}
853
e8cc313b 854/* Change the parameters of frame F as specified by ALIST.
64362cd4
GM
855 If a parameter is not specially recognized, do nothing special;
856 otherwise call the `x_set_...' function for that parameter.
857 Except for certain geometry properties, always call store_frame_param
858 to store the new value in the parameter alist. */
d043f1a4 859
f9942c9e
JB
860void
861x_set_frame_parameters (f, alist)
862 FRAME_PTR f;
863 Lisp_Object alist;
864{
865 Lisp_Object tail;
866
867 /* If both of these parameters are present, it's more efficient to
868 set them both at once. So we wait until we've looked at the
869 entire list before we set them. */
e4f79258 870 int width, height;
f9942c9e
JB
871
872 /* Same here. */
873 Lisp_Object left, top;
f9942c9e 874
a59e4f3d
RS
875 /* Same with these. */
876 Lisp_Object icon_left, icon_top;
877
f5e70acd
RS
878 /* Record in these vectors all the parms specified. */
879 Lisp_Object *parms;
880 Lisp_Object *values;
a797a73d 881 int i, p;
e1d962d7 882 int left_no_change = 0, top_no_change = 0;
a59e4f3d 883 int icon_left_no_change = 0, icon_top_no_change = 0;
5f9338d5 884 int fullscreen_is_being_set = 0;
203c1d73 885
7589a1d9
RS
886 struct gcpro gcpro1, gcpro2;
887
f5e70acd
RS
888 i = 0;
889 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
890 i++;
891
892 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
893 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
f9942c9e 894
f5e70acd
RS
895 /* Extract parm names and values into those vectors. */
896
897 i = 0;
f9942c9e
JB
898 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
899 {
333b20bb 900 Lisp_Object elt;
f9942c9e
JB
901
902 elt = Fcar (tail);
f5e70acd
RS
903 parms[i] = Fcar (elt);
904 values[i] = Fcdr (elt);
905 i++;
906 }
7589a1d9
RS
907 /* TAIL and ALIST are not used again below here. */
908 alist = tail = Qnil;
909
910 GCPRO2 (*parms, *values);
911 gcpro1.nvars = i;
912 gcpro2.nvars = i;
f5e70acd 913
7589a1d9
RS
914 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
915 because their values appear in VALUES and strings are not valid. */
e4f79258 916 top = left = Qunbound;
a59e4f3d 917 icon_left = icon_top = Qunbound;
f9942c9e 918
e4f79258
RS
919 /* Provide default values for HEIGHT and WIDTH. */
920 if (FRAME_NEW_WIDTH (f))
921 width = FRAME_NEW_WIDTH (f);
922 else
923 width = FRAME_WIDTH (f);
924
925 if (FRAME_NEW_HEIGHT (f))
926 height = FRAME_NEW_HEIGHT (f);
927 else
928 height = FRAME_HEIGHT (f);
929
a797a73d
GV
930 /* Process foreground_color and background_color before anything else.
931 They are independent of other properties, but other properties (e.g.,
932 cursor_color) are dependent upon them. */
b3ba0aa8 933 /* Process default font as well, since fringe widths depends on it. */
49d41073 934 /* Also, process fullscreen, width and height depend upon that */
a797a73d
GV
935 for (p = 0; p < i; p++)
936 {
937 Lisp_Object prop, val;
938
939 prop = parms[p];
940 val = values[p];
b3ba0aa8
KS
941 if (EQ (prop, Qforeground_color)
942 || EQ (prop, Qbackground_color)
49d41073
EZ
943 || EQ (prop, Qfont)
944 || EQ (prop, Qfullscreen))
a797a73d
GV
945 {
946 register Lisp_Object param_index, old_value;
947
a797a73d 948 old_value = get_frame_param (f, prop);
f0b9a067 949 fullscreen_is_being_set |= EQ (prop, Qfullscreen);
c7e609d5
MB
950
951 if (NILP (Fequal (val, old_value)))
952 {
953 store_frame_param (f, prop, val);
954
955 param_index = Fget (prop, Qx_frame_parameter);
956 if (NATNUMP (param_index)
957 && (XFASTINT (param_index)
958 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
959 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
960 }
a797a73d
GV
961 }
962 }
963
f5e70acd
RS
964 /* Now process them in reverse of specified order. */
965 for (i--; i >= 0; i--)
966 {
967 Lisp_Object prop, val;
968
969 prop = parms[i];
970 val = values[i];
971
e4f79258
RS
972 if (EQ (prop, Qwidth) && NUMBERP (val))
973 width = XFASTINT (val);
974 else if (EQ (prop, Qheight) && NUMBERP (val))
975 height = XFASTINT (val);
f5e70acd 976 else if (EQ (prop, Qtop))
f9942c9e 977 top = val;
f5e70acd 978 else if (EQ (prop, Qleft))
f9942c9e 979 left = val;
a59e4f3d
RS
980 else if (EQ (prop, Qicon_top))
981 icon_top = val;
982 else if (EQ (prop, Qicon_left))
983 icon_left = val;
b3ba0aa8
KS
984 else if (EQ (prop, Qforeground_color)
985 || EQ (prop, Qbackground_color)
49d41073
EZ
986 || EQ (prop, Qfont)
987 || EQ (prop, Qfullscreen))
a797a73d
GV
988 /* Processed above. */
989 continue;
f9942c9e
JB
990 else
991 {
98381190 992 register Lisp_Object param_index, old_value;
ea96210c 993
98381190 994 old_value = get_frame_param (f, prop);
c7e609d5 995
9f7e52b4 996 store_frame_param (f, prop, val);
c7e609d5 997
9f7e52b4
GM
998 param_index = Fget (prop, Qx_frame_parameter);
999 if (NATNUMP (param_index)
1000 && (XFASTINT (param_index)
1001 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
1002 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
f9942c9e
JB
1003 }
1004 }
1005
11378c41
RS
1006 /* Don't die if just one of these was set. */
1007 if (EQ (left, Qunbound))
e1d962d7
RS
1008 {
1009 left_no_change = 1;
7556890b
RS
1010 if (f->output_data.x->left_pos < 0)
1011 left = Fcons (Qplus, Fcons (make_number (f->output_data.x->left_pos), Qnil));
e1d962d7 1012 else
7556890b 1013 XSETINT (left, f->output_data.x->left_pos);
e1d962d7 1014 }
11378c41 1015 if (EQ (top, Qunbound))
e1d962d7
RS
1016 {
1017 top_no_change = 1;
7556890b
RS
1018 if (f->output_data.x->top_pos < 0)
1019 top = Fcons (Qplus, Fcons (make_number (f->output_data.x->top_pos), Qnil));
e1d962d7 1020 else
7556890b 1021 XSETINT (top, f->output_data.x->top_pos);
e1d962d7 1022 }
11378c41 1023
a59e4f3d
RS
1024 /* If one of the icon positions was not set, preserve or default it. */
1025 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
1026 {
1027 icon_left_no_change = 1;
1028 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
1029 if (NILP (icon_left))
1030 XSETINT (icon_left, 0);
1031 }
1032 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
1033 {
1034 icon_top_no_change = 1;
1035 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
1036 if (NILP (icon_top))
1037 XSETINT (icon_top, 0);
1038 }
1039
5f9338d5 1040 if (FRAME_VISIBLE_P (f) && fullscreen_is_being_set)
49d41073
EZ
1041 {
1042 /* If the frame is visible already and the fullscreen parameter is
1043 being set, it is too late to set WM manager hints to specify
1044 size and position.
1045 Here we first get the width, height and position that applies to
1046 fullscreen. We then move the frame to the appropriate
1047 position. Resize of the frame is taken care of in the code after
5f9338d5 1048 this if-statement. */
49d41073
EZ
1049 int new_left, new_top;
1050
1051 x_fullscreen_adjust (f, &width, &height, &new_top, &new_left);
1052 x_fullscreen_move (f, new_top, new_left);
1053 }
1054
499ea23b 1055 /* Don't set these parameters unless they've been explicitly
d387c960
JB
1056 specified. The window might be mapped or resized while we're in
1057 this function, and we don't want to override that unless the lisp
1058 code has asked for it.
1059
1060 Don't set these parameters unless they actually differ from the
1061 window's current parameters; the window may not actually exist
1062 yet. */
f9942c9e
JB
1063 {
1064 Lisp_Object frame;
1065
1f11a5ca
RS
1066 check_frame_size (f, &height, &width);
1067
191ed777 1068 XSETFRAME (frame, f);
11378c41 1069
e4f79258
RS
1070 if (width != FRAME_WIDTH (f)
1071 || height != FRAME_HEIGHT (f)
d6f80ae9 1072 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
e4f79258 1073 Fset_frame_size (frame, make_number (width), make_number (height));
f10f0b79
RS
1074
1075 if ((!NILP (left) || !NILP (top))
e1d962d7 1076 && ! (left_no_change && top_no_change)
7556890b
RS
1077 && ! (NUMBERP (left) && XINT (left) == f->output_data.x->left_pos
1078 && NUMBERP (top) && XINT (top) == f->output_data.x->top_pos))
f10f0b79 1079 {
e1d962d7
RS
1080 int leftpos = 0;
1081 int toppos = 0;
f10f0b79
RS
1082
1083 /* Record the signs. */
7556890b 1084 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
e1d962d7 1085 if (EQ (left, Qminus))
7556890b 1086 f->output_data.x->size_hint_flags |= XNegative;
e1d962d7
RS
1087 else if (INTEGERP (left))
1088 {
1089 leftpos = XINT (left);
1090 if (leftpos < 0)
7556890b 1091 f->output_data.x->size_hint_flags |= XNegative;
e1d962d7 1092 }
8e713be6
KR
1093 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1094 && CONSP (XCDR (left))
1095 && INTEGERP (XCAR (XCDR (left))))
e1d962d7 1096 {
8e713be6 1097 leftpos = - XINT (XCAR (XCDR (left)));
7556890b 1098 f->output_data.x->size_hint_flags |= XNegative;
e1d962d7 1099 }
8e713be6
KR
1100 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1101 && CONSP (XCDR (left))
1102 && INTEGERP (XCAR (XCDR (left))))
e1d962d7 1103 {
8e713be6 1104 leftpos = XINT (XCAR (XCDR (left)));
e1d962d7
RS
1105 }
1106
1107 if (EQ (top, Qminus))
7556890b 1108 f->output_data.x->size_hint_flags |= YNegative;
e1d962d7
RS
1109 else if (INTEGERP (top))
1110 {
1111 toppos = XINT (top);
1112 if (toppos < 0)
7556890b 1113 f->output_data.x->size_hint_flags |= YNegative;
e1d962d7 1114 }
8e713be6
KR
1115 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1116 && CONSP (XCDR (top))
1117 && INTEGERP (XCAR (XCDR (top))))
e1d962d7 1118 {
8e713be6 1119 toppos = - XINT (XCAR (XCDR (top)));
7556890b 1120 f->output_data.x->size_hint_flags |= YNegative;
e1d962d7 1121 }
8e713be6
KR
1122 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1123 && CONSP (XCDR (top))
1124 && INTEGERP (XCAR (XCDR (top))))
e1d962d7 1125 {
8e713be6 1126 toppos = XINT (XCAR (XCDR (top)));
e1d962d7
RS
1127 }
1128
1129
1130 /* Store the numeric value of the position. */
7556890b
RS
1131 f->output_data.x->top_pos = toppos;
1132 f->output_data.x->left_pos = leftpos;
e1d962d7 1133
7556890b 1134 f->output_data.x->win_gravity = NorthWestGravity;
f10f0b79
RS
1135
1136 /* Actually set that position, and convert to absolute. */
f0e72e79 1137 x_set_offset (f, leftpos, toppos, -1);
f10f0b79 1138 }
a59e4f3d
RS
1139
1140 if ((!NILP (icon_left) || !NILP (icon_top))
1141 && ! (icon_left_no_change && icon_top_no_change))
1142 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
f9942c9e 1143 }
7589a1d9
RS
1144
1145 UNGCPRO;
f9942c9e 1146}
01f1ba30 1147
08a90d6a 1148/* Store the screen positions of frame F into XPTR and YPTR.
e9445337
RS
1149 These are the positions of the containing window manager window,
1150 not Emacs's own window. */
1151
1152void
1153x_real_positions (f, xptr, yptr)
1154 FRAME_PTR f;
1155 int *xptr, *yptr;
1156{
49d41073
EZ
1157 int win_x, win_y, outer_x, outer_y;
1158 int real_x = 0, real_y = 0;
1159 int had_errors = 0;
1160 Window win = f->output_data.x->parent_desc;
e9445337 1161
49d41073 1162 int count;
043835a3 1163
49d41073
EZ
1164 BLOCK_INPUT;
1165
1166 count = x_catch_errors (FRAME_X_DISPLAY (f));
043835a3 1167
49d41073
EZ
1168 if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
1169 win = FRAME_OUTER_WINDOW (f);
1170
1171 /* This loop traverses up the containment tree until we hit the root
1172 window. Window managers may intersect many windows between our window
1173 and the root window. The window we find just before the root window
1174 should be the outer WM window. */
1175 for (;;)
e9445337 1176 {
49d41073
EZ
1177 Window wm_window, rootw;
1178 Window *tmp_children;
1179 unsigned int tmp_nchildren;
ca7bac79 1180
49d41073
EZ
1181 XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
1182 &wm_window, &tmp_children, &tmp_nchildren);
72dc3bc7 1183 XFree ((char *) tmp_children);
08a90d6a 1184
49d41073 1185 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
08a90d6a 1186
49d41073
EZ
1187 if (wm_window == rootw || had_errors)
1188 break;
08a90d6a 1189
49d41073
EZ
1190 win = wm_window;
1191 }
1192
1193 if (! had_errors)
1194 {
1195 int ign;
1196 Window child, rootw;
1197
1198 /* Get the real coordinates for the WM window upper left corner */
1199 XGetGeometry (FRAME_X_DISPLAY (f), win,
1200 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
1201
1202 /* Translate real coordinates to coordinates relative to our
1203 window. For our window, the upper left corner is 0, 0.
1204 Since the upper left corner of the WM window is outside
1205 our window, win_x and win_y will be negative:
1206
1207 ------------------ ---> x
1208 | title |
1209 | ----------------- v y
1210 | | our window
1211 */
8a07bba0 1212 XTranslateCoordinates (FRAME_X_DISPLAY (f),
e9445337 1213
8a07bba0 1214 /* From-window, to-window. */
8a07bba0 1215 FRAME_X_DISPLAY_INFO (f)->root_window,
49d41073 1216 FRAME_X_WINDOW (f),
e9445337 1217
8a07bba0 1218 /* From-position, to-position. */
49d41073 1219 real_x, real_y, &win_x, &win_y,
08a90d6a 1220
8a07bba0
RS
1221 /* Child of win. */
1222 &child);
e9445337 1223
49d41073 1224 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
845e9d85 1225 {
49d41073
EZ
1226 outer_x = win_x;
1227 outer_y = win_y;
845e9d85 1228 }
49d41073
EZ
1229 else
1230 {
1231 XTranslateCoordinates (FRAME_X_DISPLAY (f),
ca7bac79 1232
49d41073
EZ
1233 /* From-window, to-window. */
1234 FRAME_X_DISPLAY_INFO (f)->root_window,
1235 FRAME_OUTER_WINDOW (f),
1236
1237 /* From-position, to-position. */
1238 real_x, real_y, &outer_x, &outer_y,
1239
1240 /* Child of win. */
1241 &child);
e9445337 1242 }
08a90d6a 1243
49d41073
EZ
1244 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
1245 }
1246
1247 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1248
1249 UNBLOCK_INPUT;
1250
1251 if (had_errors) return;
1252
1253 f->output_data.x->x_pixels_diff = -win_x;
1254 f->output_data.x->y_pixels_diff = -win_y;
1255 f->output_data.x->x_pixels_outer_diff = -outer_x;
1256 f->output_data.x->y_pixels_outer_diff = -outer_y;
1257
1258 *xptr = real_x;
1259 *yptr = real_y;
e9445337
RS
1260}
1261
f676886a 1262/* Insert a description of internally-recorded parameters of frame X
01f1ba30
JB
1263 into the parameter alist *ALISTPTR that is to be given to the user.
1264 Only parameters that are specific to the X window system
f676886a 1265 and whose values are not correctly recorded in the frame's
01f1ba30
JB
1266 param_alist need to be considered here. */
1267
968b1234 1268void
f676886a
JB
1269x_report_frame_params (f, alistptr)
1270 struct frame *f;
01f1ba30
JB
1271 Lisp_Object *alistptr;
1272{
1273 char buf[16];
9b002b8d
KH
1274 Lisp_Object tem;
1275
1276 /* Represent negative positions (off the top or left screen edge)
1277 in a way that Fmodify_frame_parameters will understand correctly. */
7556890b
RS
1278 XSETINT (tem, f->output_data.x->left_pos);
1279 if (f->output_data.x->left_pos >= 0)
9b002b8d
KH
1280 store_in_alist (alistptr, Qleft, tem);
1281 else
1282 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1283
7556890b
RS
1284 XSETINT (tem, f->output_data.x->top_pos);
1285 if (f->output_data.x->top_pos >= 0)
9b002b8d
KH
1286 store_in_alist (alistptr, Qtop, tem);
1287 else
1288 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
01f1ba30 1289
f9942c9e 1290 store_in_alist (alistptr, Qborder_width,
7556890b 1291 make_number (f->output_data.x->border_width));
f9942c9e 1292 store_in_alist (alistptr, Qinternal_border_width,
7556890b 1293 make_number (f->output_data.x->internal_border_width));
30bf44e0
KS
1294 store_in_alist (alistptr, Qleft_fringe,
1295 make_number (f->output_data.x->left_fringe_width));
1296 store_in_alist (alistptr, Qright_fringe,
1297 make_number (f->output_data.x->right_fringe_width));
99f7c77f
EZ
1298 store_in_alist (alistptr, Qscroll_bar_width,
1299 make_number (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
1300 ? FRAME_SCROLL_BAR_PIXEL_WIDTH(f)
1301 : 0));
7c118b57 1302 sprintf (buf, "%ld", (long) FRAME_X_WINDOW (f));
f9942c9e 1303 store_in_alist (alistptr, Qwindow_id,
01f1ba30 1304 build_string (buf));
333b20bb
GM
1305#ifdef USE_X_TOOLKIT
1306 /* Tooltip frame may not have this widget. */
1307 if (f->output_data.x->widget)
1308#endif
1309 sprintf (buf, "%ld", (long) FRAME_OUTER_WINDOW (f));
2cbebefb
RS
1310 store_in_alist (alistptr, Qouter_window_id,
1311 build_string (buf));
f468da95 1312 store_in_alist (alistptr, Qicon_name, f->icon_name);
a8ccd803 1313 FRAME_SAMPLE_VISIBILITY (f);
d043f1a4
RS
1314 store_in_alist (alistptr, Qvisibility,
1315 (FRAME_VISIBLE_P (f) ? Qt
1316 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
34ae77b5 1317 store_in_alist (alistptr, Qdisplay,
8e713be6 1318 XCAR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
e4f79258 1319
8c239ac3
RS
1320 if (f->output_data.x->parent_desc == FRAME_X_DISPLAY_INFO (f)->root_window)
1321 tem = Qnil;
1322 else
1323 XSETFASTINT (tem, f->output_data.x->parent_desc);
1324 store_in_alist (alistptr, Qparent_id, tem);
01f1ba30
JB
1325}
1326\f
82978295 1327
d62c8769
GM
1328
1329/* Gamma-correct COLOR on frame F. */
1330
1331void
1332gamma_correct (f, color)
1333 struct frame *f;
1334 XColor *color;
1335{
1336 if (f->gamma)
1337 {
1338 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
1339 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
1340 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
1341 }
1342}
1343
1344
7b746c38
GM
1345/* Decide if color named COLOR_NAME is valid for use on frame F. If
1346 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
1347 allocate the color. Value is zero if COLOR_NAME is invalid, or
1348 no color could be allocated. */
e12d55b2 1349
01f1ba30 1350int
7b746c38
GM
1351x_defined_color (f, color_name, color, alloc_p)
1352 struct frame *f;
1353 char *color_name;
1354 XColor *color;
1355 int alloc_p;
01f1ba30 1356{
7b746c38
GM
1357 int success_p;
1358 Display *dpy = FRAME_X_DISPLAY (f);
1359 Colormap cmap = FRAME_X_COLORMAP (f);
01f1ba30
JB
1360
1361 BLOCK_INPUT;
7b746c38
GM
1362 success_p = XParseColor (dpy, cmap, color_name, color);
1363 if (success_p && alloc_p)
1364 success_p = x_alloc_nearest_color (f, cmap, color);
01f1ba30
JB
1365 UNBLOCK_INPUT;
1366
7b746c38 1367 return success_p;
01f1ba30
JB
1368}
1369
9b2956e2
GM
1370
1371/* Return the pixel color value for color COLOR_NAME on frame F. If F
1372 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
1373 Signal an error if color can't be allocated. */
01f1ba30
JB
1374
1375int
9b2956e2 1376x_decode_color (f, color_name, mono_color)
b9dc4443 1377 FRAME_PTR f;
9b2956e2
GM
1378 Lisp_Object color_name;
1379 int mono_color;
01f1ba30 1380{
b9dc4443 1381 XColor cdef;
01f1ba30 1382
b7826503 1383 CHECK_STRING (color_name);
01f1ba30 1384
9b2956e2
GM
1385#if 0 /* Don't do this. It's wrong when we're not using the default
1386 colormap, it makes freeing difficult, and it's probably not
1387 an important optimization. */
1388 if (strcmp (XSTRING (color_name)->data, "black") == 0)
b9dc4443 1389 return BLACK_PIX_DEFAULT (f);
9b2956e2 1390 else if (strcmp (XSTRING (color_name)->data, "white") == 0)
b9dc4443 1391 return WHITE_PIX_DEFAULT (f);
9b2956e2 1392#endif
01f1ba30 1393
9b2956e2 1394 /* Return MONO_COLOR for monochrome frames. */
b9dc4443 1395 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
9b2956e2 1396 return mono_color;
01f1ba30 1397
2d764c78 1398 /* x_defined_color is responsible for coping with failures
95626e11 1399 by looking for a near-miss. */
9b2956e2 1400 if (x_defined_color (f, XSTRING (color_name)->data, &cdef, 1))
95626e11
RS
1401 return cdef.pixel;
1402
c301be26
GM
1403 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
1404 Fcons (color_name, Qnil)));
1405 return 0;
01f1ba30 1406}
9b2956e2
GM
1407
1408
01f1ba30 1409\f
563b67aa
GM
1410/* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1411 the previous value of that parameter, NEW_VALUE is the new value. */
1412
1413static void
1414x_set_line_spacing (f, new_value, old_value)
1415 struct frame *f;
1416 Lisp_Object new_value, old_value;
1417{
1418 if (NILP (new_value))
1419 f->extra_line_spacing = 0;
1420 else if (NATNUMP (new_value))
1421 f->extra_line_spacing = XFASTINT (new_value);
1422 else
1a948b17 1423 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
563b67aa
GM
1424 Fcons (new_value, Qnil)));
1425 if (FRAME_VISIBLE_P (f))
1426 redraw_frame (f);
1427}
1428
1429
ea0a1f53
GM
1430/* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
1431 the previous value of that parameter, NEW_VALUE is the new value.
1432 See also the comment of wait_for_wm in struct x_output. */
1433
1434static void
1435x_set_wait_for_wm (f, new_value, old_value)
1436 struct frame *f;
1437 Lisp_Object new_value, old_value;
1438{
1439 f->output_data.x->wait_for_wm = !NILP (new_value);
1440}
1441
1442
49d41073
EZ
1443/* Change the `fullscreen' frame parameter of frame F. OLD_VALUE is
1444 the previous value of that parameter, NEW_VALUE is the new value. */
1445
1446static void
1447x_set_fullscreen (f, new_value, old_value)
1448 struct frame *f;
1449 Lisp_Object new_value, old_value;
1450{
1451 if (NILP (new_value))
1452 f->output_data.x->want_fullscreen = FULLSCREEN_NONE;
1453 else if (EQ (new_value, Qfullboth))
1454 f->output_data.x->want_fullscreen = FULLSCREEN_BOTH;
1455 else if (EQ (new_value, Qfullwidth))
1456 f->output_data.x->want_fullscreen = FULLSCREEN_WIDTH;
1457 else if (EQ (new_value, Qfullheight))
1458 f->output_data.x->want_fullscreen = FULLSCREEN_HEIGHT;
1459}
1460
1461
d62c8769 1462/* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
ea0a1f53
GM
1463 the previous value of that parameter, NEW_VALUE is the new
1464 value. */
d62c8769
GM
1465
1466static void
1467x_set_screen_gamma (f, new_value, old_value)
1468 struct frame *f;
1469 Lisp_Object new_value, old_value;
1470{
1471 if (NILP (new_value))
1472 f->gamma = 0;
1473 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1474 /* The value 0.4545 is the normal viewing gamma. */
1475 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1476 else
1a948b17 1477 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
d62c8769
GM
1478 Fcons (new_value, Qnil)));
1479
1480 clear_face_cache (0);
1481}
1482
1483
f676886a 1484/* Functions called only from `x_set_frame_param'
01f1ba30
JB
1485 to set individual parameters.
1486
fe24a618 1487 If FRAME_X_WINDOW (f) is 0,
f676886a 1488 the frame is being created and its X-window does not exist yet.
01f1ba30
JB
1489 In that case, just record the parameter's new value
1490 in the standard place; do not attempt to change the window. */
1491
1492void
f676886a
JB
1493x_set_foreground_color (f, arg, oldval)
1494 struct frame *f;
01f1ba30
JB
1495 Lisp_Object arg, oldval;
1496{
09393d07
GM
1497 struct x_output *x = f->output_data.x;
1498 unsigned long fg, old_fg;
a76206dc 1499
09393d07
GM
1500 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1501 old_fg = x->foreground_pixel;
1502 x->foreground_pixel = fg;
a76206dc 1503
fe24a618 1504 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 1505 {
09393d07 1506 Display *dpy = FRAME_X_DISPLAY (f);
36d42089 1507
09393d07
GM
1508 BLOCK_INPUT;
1509 XSetForeground (dpy, x->normal_gc, fg);
1510 XSetBackground (dpy, x->reverse_gc, fg);
36d42089 1511
09393d07
GM
1512 if (x->cursor_pixel == old_fg)
1513 {
1514 unload_color (f, x->cursor_pixel);
1515 x->cursor_pixel = x_copy_color (f, fg);
1516 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
1517 }
1518
01f1ba30 1519 UNBLOCK_INPUT;
09393d07 1520
05c8abbe 1521 update_face_from_frame_parameter (f, Qforeground_color, arg);
09393d07 1522
179956b9 1523 if (FRAME_VISIBLE_P (f))
f676886a 1524 redraw_frame (f);
01f1ba30 1525 }
09393d07
GM
1526
1527 unload_color (f, old_fg);
01f1ba30
JB
1528}
1529
1530void
f676886a
JB
1531x_set_background_color (f, arg, oldval)
1532 struct frame *f;
01f1ba30
JB
1533 Lisp_Object arg, oldval;
1534{
09393d07
GM
1535 struct x_output *x = f->output_data.x;
1536 unsigned long bg;
01f1ba30 1537
09393d07
GM
1538 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1539 unload_color (f, x->background_pixel);
1540 x->background_pixel = bg;
a76206dc 1541
fe24a618 1542 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 1543 {
09393d07 1544 Display *dpy = FRAME_X_DISPLAY (f);
36d42089 1545
09393d07
GM
1546 BLOCK_INPUT;
1547 XSetBackground (dpy, x->normal_gc, bg);
1548 XSetForeground (dpy, x->reverse_gc, bg);
1549 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
1550 XSetForeground (dpy, x->cursor_gc, bg);
1551
f76e0368
GM
1552#ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
1553 toolkit scroll bars. */
1554 {
1555 Lisp_Object bar;
1556 for (bar = FRAME_SCROLL_BARS (f);
1557 !NILP (bar);
1558 bar = XSCROLL_BAR (bar)->next)
1559 {
1560 Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
1561 XSetWindowBackground (dpy, window, bg);
1562 }
1563 }
1564#endif /* USE_TOOLKIT_SCROLL_BARS */
01f1ba30 1565
09393d07 1566 UNBLOCK_INPUT;
05c8abbe 1567 update_face_from_frame_parameter (f, Qbackground_color, arg);
ea96210c 1568
179956b9 1569 if (FRAME_VISIBLE_P (f))
f676886a 1570 redraw_frame (f);
01f1ba30
JB
1571 }
1572}
1573
1574void
f676886a
JB
1575x_set_mouse_color (f, arg, oldval)
1576 struct frame *f;
01f1ba30
JB
1577 Lisp_Object arg, oldval;
1578{
09393d07
GM
1579 struct x_output *x = f->output_data.x;
1580 Display *dpy = FRAME_X_DISPLAY (f);
95f80c78 1581 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
0af913d7 1582 Cursor hourglass_cursor, horizontal_drag_cursor;
1dc6cfa6 1583 int count;
51a1d2d8 1584 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
09393d07 1585 unsigned long mask_color = x->background_pixel;
a76206dc 1586
51a1d2d8 1587 /* Don't let pointers be invisible. */
09393d07 1588 if (mask_color == pixel)
bcf26b38
GM
1589 {
1590 x_free_colors (f, &pixel, 1);
09393d07 1591 pixel = x_copy_color (f, x->foreground_pixel);
bcf26b38 1592 }
a76206dc 1593
09393d07
GM
1594 unload_color (f, x->mouse_pixel);
1595 x->mouse_pixel = pixel;
01f1ba30
JB
1596
1597 BLOCK_INPUT;
fe24a618 1598
eb8c3be9 1599 /* It's not okay to crash if the user selects a screwy cursor. */
09393d07 1600 count = x_catch_errors (dpy);
fe24a618 1601
09393d07 1602 if (!NILP (Vx_pointer_shape))
01f1ba30 1603 {
b7826503 1604 CHECK_NUMBER (Vx_pointer_shape);
09393d07 1605 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
01f1ba30
JB
1606 }
1607 else
09393d07
GM
1608 cursor = XCreateFontCursor (dpy, XC_xterm);
1609 x_check_errors (dpy, "bad text pointer cursor: %s");
01f1ba30 1610
09393d07 1611 if (!NILP (Vx_nontext_pointer_shape))
01f1ba30 1612 {
b7826503 1613 CHECK_NUMBER (Vx_nontext_pointer_shape);
09393d07
GM
1614 nontext_cursor
1615 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
01f1ba30
JB
1616 }
1617 else
09393d07
GM
1618 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
1619 x_check_errors (dpy, "bad nontext pointer cursor: %s");
01f1ba30 1620
09393d07 1621 if (!NILP (Vx_hourglass_pointer_shape))
333b20bb 1622 {
b7826503 1623 CHECK_NUMBER (Vx_hourglass_pointer_shape);
09393d07
GM
1624 hourglass_cursor
1625 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
333b20bb
GM
1626 }
1627 else
09393d07
GM
1628 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
1629 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
333b20bb 1630
09393d07
GM
1631 x_check_errors (dpy, "bad nontext pointer cursor: %s");
1632 if (!NILP (Vx_mode_pointer_shape))
01f1ba30 1633 {
b7826503 1634 CHECK_NUMBER (Vx_mode_pointer_shape);
09393d07 1635 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
01f1ba30
JB
1636 }
1637 else
09393d07
GM
1638 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
1639 x_check_errors (dpy, "bad modeline pointer cursor: %s");
95f80c78 1640
09393d07 1641 if (!NILP (Vx_sensitive_text_pointer_shape))
95f80c78 1642 {
b7826503 1643 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
ca0ecbf5 1644 cross_cursor
09393d07 1645 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
95f80c78
FP
1646 }
1647 else
09393d07 1648 cross_cursor = XCreateFontCursor (dpy, XC_crosshair);
01f1ba30 1649
8fb4ec9c
GM
1650 if (!NILP (Vx_window_horizontal_drag_shape))
1651 {
b7826503 1652 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
8fb4ec9c 1653 horizontal_drag_cursor
09393d07 1654 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
8fb4ec9c
GM
1655 }
1656 else
1657 horizontal_drag_cursor
09393d07 1658 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
8fb4ec9c 1659
fe24a618 1660 /* Check and report errors with the above calls. */
09393d07
GM
1661 x_check_errors (dpy, "can't set cursor shape: %s");
1662 x_uncatch_errors (dpy, count);
fe24a618 1663
01f1ba30
JB
1664 {
1665 XColor fore_color, back_color;
1666
09393d07 1667 fore_color.pixel = x->mouse_pixel;
a31fedb7 1668 x_query_color (f, &fore_color);
01f1ba30 1669 back_color.pixel = mask_color;
a31fedb7
GM
1670 x_query_color (f, &back_color);
1671
09393d07
GM
1672 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1673 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1674 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1675 XRecolorCursor (dpy, cross_cursor, &fore_color, &back_color);
1676 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1677 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
01f1ba30 1678 }
01f1ba30 1679
fe24a618 1680 if (FRAME_X_WINDOW (f) != 0)
09393d07
GM
1681 XDefineCursor (dpy, FRAME_X_WINDOW (f), cursor);
1682
1683 if (cursor != x->text_cursor
1684 && x->text_cursor != 0)
1685 XFreeCursor (dpy, x->text_cursor);
1686 x->text_cursor = cursor;
1687
1688 if (nontext_cursor != x->nontext_cursor
1689 && x->nontext_cursor != 0)
1690 XFreeCursor (dpy, x->nontext_cursor);
1691 x->nontext_cursor = nontext_cursor;
1692
1693 if (hourglass_cursor != x->hourglass_cursor
1694 && x->hourglass_cursor != 0)
1695 XFreeCursor (dpy, x->hourglass_cursor);
1696 x->hourglass_cursor = hourglass_cursor;
1697
1698 if (mode_cursor != x->modeline_cursor
1699 && x->modeline_cursor != 0)
1700 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1701 x->modeline_cursor = mode_cursor;
333b20bb 1702
09393d07
GM
1703 if (cross_cursor != x->cross_cursor
1704 && x->cross_cursor != 0)
1705 XFreeCursor (dpy, x->cross_cursor);
1706 x->cross_cursor = cross_cursor;
01f1ba30 1707
09393d07
GM
1708 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1709 && x->horizontal_drag_cursor != 0)
1710 XFreeCursor (dpy, x->horizontal_drag_cursor);
1711 x->horizontal_drag_cursor = horizontal_drag_cursor;
8fb4ec9c 1712
09393d07 1713 XFlush (dpy);
01f1ba30 1714 UNBLOCK_INPUT;
05c8abbe
GM
1715
1716 update_face_from_frame_parameter (f, Qmouse_color, arg);
01f1ba30
JB
1717}
1718
1719void
f676886a
JB
1720x_set_cursor_color (f, arg, oldval)
1721 struct frame *f;
01f1ba30
JB
1722 Lisp_Object arg, oldval;
1723{
a76206dc 1724 unsigned long fore_pixel, pixel;
10168ebb 1725 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
09393d07 1726 struct x_output *x = f->output_data.x;
01f1ba30 1727
10168ebb
GM
1728 if (!NILP (Vx_cursor_fore_pixel))
1729 {
1730 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1731 WHITE_PIX_DEFAULT (f));
1732 fore_pixel_allocated_p = 1;
1733 }
01f1ba30 1734 else
09393d07 1735 fore_pixel = x->background_pixel;
10168ebb 1736
a76206dc 1737 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
10168ebb 1738 pixel_allocated_p = 1;
a76206dc 1739
f9942c9e 1740 /* Make sure that the cursor color differs from the background color. */
09393d07 1741 if (pixel == x->background_pixel)
01f1ba30 1742 {
10168ebb
GM
1743 if (pixel_allocated_p)
1744 {
1745 x_free_colors (f, &pixel, 1);
1746 pixel_allocated_p = 0;
1747 }
1748
09393d07 1749 pixel = x->mouse_pixel;
a76206dc 1750 if (pixel == fore_pixel)
10168ebb
GM
1751 {
1752 if (fore_pixel_allocated_p)
1753 {
1754 x_free_colors (f, &fore_pixel, 1);
1755 fore_pixel_allocated_p = 0;
1756 }
09393d07 1757 fore_pixel = x->background_pixel;
10168ebb 1758 }
01f1ba30 1759 }
a76206dc 1760
09393d07 1761 unload_color (f, x->cursor_foreground_pixel);
10168ebb
GM
1762 if (!fore_pixel_allocated_p)
1763 fore_pixel = x_copy_color (f, fore_pixel);
09393d07 1764 x->cursor_foreground_pixel = fore_pixel;
01f1ba30 1765
09393d07 1766 unload_color (f, x->cursor_pixel);
10168ebb
GM
1767 if (!pixel_allocated_p)
1768 pixel = x_copy_color (f, pixel);
09393d07 1769 x->cursor_pixel = pixel;
a76206dc 1770
fe24a618 1771 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 1772 {
01f1ba30 1773 BLOCK_INPUT;
09393d07
GM
1774 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1775 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
01f1ba30 1776 UNBLOCK_INPUT;
01f1ba30 1777
179956b9 1778 if (FRAME_VISIBLE_P (f))
01f1ba30 1779 {
cedadcfa
RS
1780 x_update_cursor (f, 0);
1781 x_update_cursor (f, 1);
01f1ba30
JB
1782 }
1783 }
05c8abbe
GM
1784
1785 update_face_from_frame_parameter (f, Qcursor_color, arg);
01f1ba30 1786}
943b580d 1787\f
f676886a 1788/* Set the border-color of frame F to value described by ARG.
01f1ba30
JB
1789 ARG can be a string naming a color.
1790 The border-color is used for the border that is drawn by the X server.
1791 Note that this does not fully take effect if done before
f676886a 1792 F has an x-window; it must be redone when the window is created.
01f1ba30
JB
1793
1794 Note: this is done in two routines because of the way X10 works.
1795
1796 Note: under X11, this is normally the province of the window manager,
b9dc4443 1797 and so emacs' border colors may be overridden. */
01f1ba30
JB
1798
1799void
f676886a
JB
1800x_set_border_color (f, arg, oldval)
1801 struct frame *f;
01f1ba30
JB
1802 Lisp_Object arg, oldval;
1803{
01f1ba30
JB
1804 int pix;
1805
b7826503 1806 CHECK_STRING (arg);
b9dc4443 1807 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
f676886a 1808 x_set_border_pixel (f, pix);
05c8abbe 1809 update_face_from_frame_parameter (f, Qborder_color, arg);
01f1ba30
JB
1810}
1811
f676886a 1812/* Set the border-color of frame F to pixel value PIX.
01f1ba30 1813 Note that this does not fully take effect if done before
f676886a 1814 F has an x-window. */
01f1ba30 1815
968b1234 1816void
f676886a
JB
1817x_set_border_pixel (f, pix)
1818 struct frame *f;
01f1ba30
JB
1819 int pix;
1820{
a76206dc 1821 unload_color (f, f->output_data.x->border_pixel);
7556890b 1822 f->output_data.x->border_pixel = pix;
01f1ba30 1823
7556890b 1824 if (FRAME_X_WINDOW (f) != 0 && f->output_data.x->border_width > 0)
01f1ba30 1825 {
01f1ba30 1826 BLOCK_INPUT;
b9dc4443 1827 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
270958e8 1828 (unsigned long)pix);
01f1ba30
JB
1829 UNBLOCK_INPUT;
1830
179956b9 1831 if (FRAME_VISIBLE_P (f))
f676886a 1832 redraw_frame (f);
01f1ba30
JB
1833 }
1834}
1835
0d1469d6
GM
1836
1837/* Value is the internal representation of the specified cursor type
1838 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
1839 of the bar cursor. */
1840
1841enum text_cursor_kinds
1842x_specified_cursor_type (arg, width)
1843 Lisp_Object arg;
1844 int *width;
dbc4e1c1 1845{
0d1469d6
GM
1846 enum text_cursor_kinds type;
1847
dbc4e1c1 1848 if (EQ (arg, Qbar))
c3211206 1849 {
0d1469d6
GM
1850 type = BAR_CURSOR;
1851 *width = 2;
c3211206 1852 }
08ac8554
GM
1853 else if (CONSP (arg)
1854 && EQ (XCAR (arg), Qbar)
1855 && INTEGERP (XCDR (arg))
1856 && XINT (XCDR (arg)) >= 0)
c3211206 1857 {
0d1469d6
GM
1858 type = BAR_CURSOR;
1859 *width = XINT (XCDR (arg));
c3211206 1860 }
08ac8554 1861 else if (NILP (arg))
0d1469d6 1862 type = NO_CURSOR;
dbc4e1c1 1863 else
c3211206
RS
1864 /* Treat anything unknown as "box cursor".
1865 It was bad to signal an error; people have trouble fixing
1866 .Xdefaults with Emacs, when it has something bad in it. */
0d1469d6
GM
1867 type = FILLED_BOX_CURSOR;
1868
1869 return type;
1870}
1871
1872void
1873x_set_cursor_type (f, arg, oldval)
1874 FRAME_PTR f;
1875 Lisp_Object arg, oldval;
1876{
1877 int width;
1878
1879 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
1880 f->output_data.x->cursor_width = width;
dbc4e1c1
JB
1881
1882 /* Make sure the cursor gets redrawn. This is overkill, but how
1883 often do people change cursor types? */
1884 update_mode_lines++;
1885}
943b580d 1886\f
01f1ba30 1887void
f676886a
JB
1888x_set_icon_type (f, arg, oldval)
1889 struct frame *f;
01f1ba30
JB
1890 Lisp_Object arg, oldval;
1891{
01f1ba30
JB
1892 int result;
1893
203c1d73
RS
1894 if (STRINGP (arg))
1895 {
1896 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1897 return;
1898 }
1899 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
01f1ba30
JB
1900 return;
1901
1902 BLOCK_INPUT;
265a9e55 1903 if (NILP (arg))
80534dd6 1904 result = x_text_icon (f,
f468da95
RS
1905 (char *) XSTRING ((!NILP (f->icon_name)
1906 ? f->icon_name
80534dd6 1907 : f->name))->data);
f1c7b5a6
RS
1908 else
1909 result = x_bitmap_icon (f, arg);
01f1ba30
JB
1910
1911 if (result)
1912 {
01f1ba30 1913 UNBLOCK_INPUT;
0fb53770 1914 error ("No icon window available");
01f1ba30
JB
1915 }
1916
b9dc4443 1917 XFlush (FRAME_X_DISPLAY (f));
01f1ba30
JB
1918 UNBLOCK_INPUT;
1919}
1920
f1c7b5a6 1921/* Return non-nil if frame F wants a bitmap icon. */
0fb53770 1922
f1c7b5a6 1923Lisp_Object
0fb53770
RS
1924x_icon_type (f)
1925 FRAME_PTR f;
1926{
1927 Lisp_Object tem;
1928
1929 tem = assq_no_quit (Qicon_type, f->param_alist);
f1c7b5a6 1930 if (CONSP (tem))
8e713be6 1931 return XCDR (tem);
f1c7b5a6
RS
1932 else
1933 return Qnil;
0fb53770
RS
1934}
1935
80534dd6
KH
1936void
1937x_set_icon_name (f, arg, oldval)
1938 struct frame *f;
1939 Lisp_Object arg, oldval;
1940{
80534dd6
KH
1941 int result;
1942
1943 if (STRINGP (arg))
1944 {
1945 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1946 return;
1947 }
1948 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1949 return;
1950
f468da95 1951 f->icon_name = arg;
80534dd6 1952
7556890b 1953 if (f->output_data.x->icon_bitmap != 0)
80534dd6
KH
1954 return;
1955
1956 BLOCK_INPUT;
1957
1958 result = x_text_icon (f,
f468da95
RS
1959 (char *) XSTRING ((!NILP (f->icon_name)
1960 ? f->icon_name
943b580d
RS
1961 : !NILP (f->title)
1962 ? f->title
80534dd6
KH
1963 : f->name))->data);
1964
1965 if (result)
1966 {
1967 UNBLOCK_INPUT;
1968 error ("No icon window available");
1969 }
1970
80534dd6
KH
1971 XFlush (FRAME_X_DISPLAY (f));
1972 UNBLOCK_INPUT;
1973}
943b580d 1974\f
01f1ba30 1975void
f676886a
JB
1976x_set_font (f, arg, oldval)
1977 struct frame *f;
01f1ba30
JB
1978 Lisp_Object arg, oldval;
1979{
ea96210c 1980 Lisp_Object result;
942ea06d 1981 Lisp_Object fontset_name;
a367641f 1982 Lisp_Object frame;
57c5889c 1983 int old_fontset = f->output_data.x->fontset;
01f1ba30 1984
b7826503 1985 CHECK_STRING (arg);
01f1ba30 1986
49965a29 1987 fontset_name = Fquery_fontset (arg, Qnil);
942ea06d 1988
01f1ba30 1989 BLOCK_INPUT;
942ea06d
KH
1990 result = (STRINGP (fontset_name)
1991 ? x_new_fontset (f, XSTRING (fontset_name)->data)
5f652afc 1992 : x_new_fontset (f, XSTRING (arg)->data));
01f1ba30
JB
1993 UNBLOCK_INPUT;
1994
ea96210c 1995 if (EQ (result, Qnil))
1c59f5df 1996 error ("Font `%s' is not defined", XSTRING (arg)->data);
ea96210c 1997 else if (EQ (result, Qt))
26e18ed9 1998 error ("The characters of the given font have varying widths");
ea96210c
JB
1999 else if (STRINGP (result))
2000 {
57c5889c
GM
2001 if (STRINGP (fontset_name))
2002 {
2003 /* Fontset names are built from ASCII font names, so the
2004 names may be equal despite there was a change. */
2005 if (old_fontset == f->output_data.x->fontset)
2006 return;
2007 }
5f652afc
KH
2008 store_frame_param (f, Qfont, result);
2009 if (!NILP (Fequal (result, oldval)))
1d090605 2010 return;
57c5889c 2011
333b20bb 2012 recompute_basic_faces (f);
ea96210c
JB
2013 }
2014 else
2015 abort ();
a367641f 2016
8938a4fb 2017 do_pending_window_change (0);
95aa0336 2018
333b20bb
GM
2019 /* Don't call `face-set-after-frame-default' when faces haven't been
2020 initialized yet. This is the case when called from
2021 Fx_create_frame. In that case, the X widget or window doesn't
2022 exist either, and we can end up in x_report_frame_params with a
2023 null widget which gives a segfault. */
2024 if (FRAME_FACE_CACHE (f))
2025 {
2026 XSETFRAME (frame, f);
2027 call1 (Qface_set_after_frame_default, frame);
2028 }
01f1ba30
JB
2029}
2030
b3ba0aa8
KS
2031static void
2032x_set_fringe_width (f, new_value, old_value)
2033 struct frame *f;
2034 Lisp_Object new_value, old_value;
2035{
2036 x_compute_fringe_widths (f, 1);
2037}
2038
01f1ba30 2039void
f676886a
JB
2040x_set_border_width (f, arg, oldval)
2041 struct frame *f;
01f1ba30
JB
2042 Lisp_Object arg, oldval;
2043{
b7826503 2044 CHECK_NUMBER (arg);
01f1ba30 2045
7556890b 2046 if (XINT (arg) == f->output_data.x->border_width)
01f1ba30
JB
2047 return;
2048
fe24a618 2049 if (FRAME_X_WINDOW (f) != 0)
01f1ba30
JB
2050 error ("Cannot change the border width of a window");
2051
7556890b 2052 f->output_data.x->border_width = XINT (arg);
01f1ba30
JB
2053}
2054
2055void
f676886a
JB
2056x_set_internal_border_width (f, arg, oldval)
2057 struct frame *f;
01f1ba30
JB
2058 Lisp_Object arg, oldval;
2059{
7556890b 2060 int old = f->output_data.x->internal_border_width;
01f1ba30 2061
b7826503 2062 CHECK_NUMBER (arg);
7556890b
RS
2063 f->output_data.x->internal_border_width = XINT (arg);
2064 if (f->output_data.x->internal_border_width < 0)
2065 f->output_data.x->internal_border_width = 0;
01f1ba30 2066
d3b06468 2067#ifdef USE_X_TOOLKIT
2a8a07d4 2068 if (f->output_data.x->edit_widget)
968b1234 2069 widget_store_internal_border (f->output_data.x->edit_widget);
d3b06468 2070#endif
2a8a07d4 2071
7556890b 2072 if (f->output_data.x->internal_border_width == old)
01f1ba30
JB
2073 return;
2074
fe24a618 2075 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 2076 {
363f7e15 2077 x_set_window_size (f, 0, f->width, f->height);
f676886a 2078 SET_FRAME_GARBAGED (f);
8938a4fb 2079 do_pending_window_change (0);
01f1ba30 2080 }
ea42193a
GM
2081 else
2082 SET_FRAME_GARBAGED (f);
01f1ba30
JB
2083}
2084
d043f1a4
RS
2085void
2086x_set_visibility (f, value, oldval)
2087 struct frame *f;
2088 Lisp_Object value, oldval;
2089{
2090 Lisp_Object frame;
191ed777 2091 XSETFRAME (frame, f);
d043f1a4
RS
2092
2093 if (NILP (value))
363f7e15 2094 Fmake_frame_invisible (frame, Qt);
49795535 2095 else if (EQ (value, Qicon))
d043f1a4 2096 Ficonify_frame (frame);
49795535
JB
2097 else
2098 Fmake_frame_visible (frame);
d043f1a4 2099}
52de7ce9 2100
943b580d 2101\f
52de7ce9
GM
2102/* Change window heights in windows rooted in WINDOW by N lines. */
2103
d043f1a4 2104static void
52de7ce9 2105x_change_window_heights (window, n)
d043f1a4
RS
2106 Lisp_Object window;
2107 int n;
2108{
47c0f58b 2109 struct window *w = XWINDOW (window);
d043f1a4 2110
e33f7330
KH
2111 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2112 XSETFASTINT (w->height, XFASTINT (w->height) - n);
d043f1a4 2113
4336c705
GM
2114 if (INTEGERP (w->orig_top))
2115 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2116 if (INTEGERP (w->orig_height))
2117 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2118
47c0f58b
RS
2119 /* Handle just the top child in a vertical split. */
2120 if (!NILP (w->vchild))
52de7ce9 2121 x_change_window_heights (w->vchild, n);
d043f1a4 2122
47c0f58b
RS
2123 /* Adjust all children in a horizontal split. */
2124 for (window = w->hchild; !NILP (window); window = w->next)
2125 {
2126 w = XWINDOW (window);
52de7ce9 2127 x_change_window_heights (window, n);
d043f1a4
RS
2128 }
2129}
2130
2131void
2132x_set_menu_bar_lines (f, value, oldval)
2133 struct frame *f;
2134 Lisp_Object value, oldval;
2135{
2136 int nlines;
b6d7acec 2137#ifndef USE_X_TOOLKIT
d043f1a4 2138 int olines = FRAME_MENU_BAR_LINES (f);
b6d7acec 2139#endif
d043f1a4 2140
f64ba6ea
JB
2141 /* Right now, menu bars don't work properly in minibuf-only frames;
2142 most of the commands try to apply themselves to the minibuffer
333b20bb 2143 frame itself, and get an error because you can't switch buffers
f64ba6ea 2144 in or split the minibuffer window. */
519066d2 2145 if (FRAME_MINIBUF_ONLY_P (f))
f64ba6ea
JB
2146 return;
2147
6a5e54e2 2148 if (INTEGERP (value))
d043f1a4
RS
2149 nlines = XINT (value);
2150 else
2151 nlines = 0;
2152
3d09b6be
RS
2153 /* Make sure we redisplay all windows in this frame. */
2154 windows_or_buffers_changed++;
2155
9ef48a9d
RS
2156#ifdef USE_X_TOOLKIT
2157 FRAME_MENU_BAR_LINES (f) = 0;
2158 if (nlines)
0d8ef3f4
RS
2159 {
2160 FRAME_EXTERNAL_MENU_BAR (f) = 1;
97a1ff91 2161 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
0d8ef3f4
RS
2162 /* Make sure next redisplay shows the menu bar. */
2163 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
2164 }
9ef48a9d
RS
2165 else
2166 {
6bc20398
FP
2167 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2168 free_frame_menubar (f);
9ef48a9d 2169 FRAME_EXTERNAL_MENU_BAR (f) = 0;
97a1ff91
RS
2170 if (FRAME_X_P (f))
2171 f->output_data.x->menubar_widget = 0;
9ef48a9d
RS
2172 }
2173#else /* not USE_X_TOOLKIT */
d043f1a4 2174 FRAME_MENU_BAR_LINES (f) = nlines;
52de7ce9 2175 x_change_window_heights (f->root_window, nlines - olines);
9ef48a9d 2176#endif /* not USE_X_TOOLKIT */
333b20bb
GM
2177 adjust_glyphs (f);
2178}
2179
2180
2181/* Set the number of lines used for the tool bar of frame F to VALUE.
2182 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2183 is the old number of tool bar lines. This function changes the
2184 height of all windows on frame F to match the new tool bar height.
2185 The frame's height doesn't change. */
2186
2187void
9ea173e8 2188x_set_tool_bar_lines (f, value, oldval)
333b20bb
GM
2189 struct frame *f;
2190 Lisp_Object value, oldval;
2191{
52de7ce9
GM
2192 int delta, nlines, root_height;
2193 Lisp_Object root_window;
333b20bb 2194
e870b7ba
GM
2195 /* Treat tool bars like menu bars. */
2196 if (FRAME_MINIBUF_ONLY_P (f))
2197 return;
2198
333b20bb
GM
2199 /* Use VALUE only if an integer >= 0. */
2200 if (INTEGERP (value) && XINT (value) >= 0)
2201 nlines = XFASTINT (value);
2202 else
2203 nlines = 0;
2204
2205 /* Make sure we redisplay all windows in this frame. */
2206 ++windows_or_buffers_changed;
2207
9ea173e8 2208 delta = nlines - FRAME_TOOL_BAR_LINES (f);
52de7ce9
GM
2209
2210 /* Don't resize the tool-bar to more than we have room for. */
2211 root_window = FRAME_ROOT_WINDOW (f);
2212 root_height = XINT (XWINDOW (root_window)->height);
2213 if (root_height - delta < 1)
2214 {
2215 delta = root_height - 1;
2216 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2217 }
2218
9ea173e8 2219 FRAME_TOOL_BAR_LINES (f) = nlines;
52de7ce9 2220 x_change_window_heights (root_window, delta);
333b20bb 2221 adjust_glyphs (f);
ccba751c
GM
2222
2223 /* We also have to make sure that the internal border at the top of
2224 the frame, below the menu bar or tool bar, is redrawn when the
2225 tool bar disappears. This is so because the internal border is
2226 below the tool bar if one is displayed, but is below the menu bar
2227 if there isn't a tool bar. The tool bar draws into the area
2228 below the menu bar. */
2229 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2230 {
2231 updating_frame = f;
2232 clear_frame ();
fb3cd89b 2233 clear_current_matrices (f);
ccba751c
GM
2234 updating_frame = NULL;
2235 }
b6f91066
GM
2236
2237 /* If the tool bar gets smaller, the internal border below it
2238 has to be cleared. It was formerly part of the display
2239 of the larger tool bar, and updating windows won't clear it. */
2240 if (delta < 0)
2241 {
2242 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2243 int width = PIXEL_WIDTH (f);
2244 int y = nlines * CANON_Y_UNIT (f);
2245
2246 BLOCK_INPUT;
161d30fd
GM
2247 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2248 0, y, width, height, False);
b6f91066 2249 UNBLOCK_INPUT;
ddc24747
GM
2250
2251 if (WINDOWP (f->tool_bar_window))
2252 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
b6f91066 2253 }
333b20bb
GM
2254}
2255
2256
2257/* Set the foreground color for scroll bars on frame F to VALUE.
2258 VALUE should be a string, a color name. If it isn't a string or
2259 isn't a valid color name, do nothing. OLDVAL is the old value of
2260 the frame parameter. */
2261
2262void
2263x_set_scroll_bar_foreground (f, value, oldval)
2264 struct frame *f;
2265 Lisp_Object value, oldval;
2266{
2267 unsigned long pixel;
2268
2269 if (STRINGP (value))
2270 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
2271 else
2272 pixel = -1;
2273
2274 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
2275 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
2276
2277 f->output_data.x->scroll_bar_foreground_pixel = pixel;
2278 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2279 {
2280 /* Remove all scroll bars because they have wrong colors. */
2281 if (condemn_scroll_bars_hook)
2282 (*condemn_scroll_bars_hook) (f);
2283 if (judge_scroll_bars_hook)
2284 (*judge_scroll_bars_hook) (f);
05c8abbe
GM
2285
2286 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
333b20bb
GM
2287 redraw_frame (f);
2288 }
2289}
2290
2291
2292/* Set the background color for scroll bars on frame F to VALUE VALUE
2293 should be a string, a color name. If it isn't a string or isn't a
2294 valid color name, do nothing. OLDVAL is the old value of the frame
2295 parameter. */
2296
2297void
2298x_set_scroll_bar_background (f, value, oldval)
2299 struct frame *f;
2300 Lisp_Object value, oldval;
2301{
2302 unsigned long pixel;
2303
2304 if (STRINGP (value))
2305 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
2306 else
2307 pixel = -1;
2308
2309 if (f->output_data.x->scroll_bar_background_pixel != -1)
2310 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
2311
f15340b7
MB
2312#ifdef USE_TOOLKIT_SCROLL_BARS
2313 /* Scrollbar shadow colors. */
2314 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
2315 {
2316 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
2317 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2318 }
2319 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
2320 {
2321 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
2322 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2323 }
2324#endif /* USE_TOOLKIT_SCROLL_BARS */
2325
333b20bb
GM
2326 f->output_data.x->scroll_bar_background_pixel = pixel;
2327 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2328 {
2329 /* Remove all scroll bars because they have wrong colors. */
2330 if (condemn_scroll_bars_hook)
2331 (*condemn_scroll_bars_hook) (f);
2332 if (judge_scroll_bars_hook)
2333 (*judge_scroll_bars_hook) (f);
2334
05c8abbe 2335 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
333b20bb
GM
2336 redraw_frame (f);
2337 }
d043f1a4 2338}
333b20bb 2339
943b580d 2340\f
3a258507 2341/* Encode Lisp string STRING as a text in a format appropriate for
96db09e4
KH
2342 XICCC (X Inter Client Communication Conventions).
2343
2344 If STRING contains only ASCII characters, do no conversion and
2345 return the string data of STRING. Otherwise, encode the text by
2346 CODING_SYSTEM, and return a newly allocated memory area which
2347 should be freed by `xfree' by a caller.
2348
37323f34
EZ
2349 SELECTIONP non-zero means the string is being encoded for an X
2350 selection, so it is safe to run pre-write conversions (which
2351 may run Lisp code).
2352
96db09e4
KH
2353 Store the byte length of resulting text in *TEXT_BYTES.
2354
d60660d6 2355 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
96db09e4 2356 which means that the `encoding' of the result can be `STRING'.
d60660d6 2357 Otherwise store 0 in *STRINGP, which means that the `encoding' of
96db09e4
KH
2358 the result should be `COMPOUND_TEXT'. */
2359
2360unsigned char *
37323f34 2361x_encode_text (string, coding_system, selectionp, text_bytes, stringp)
96db09e4 2362 Lisp_Object string, coding_system;
d60660d6 2363 int *text_bytes, *stringp;
37323f34 2364 int selectionp;
96db09e4 2365{
0f8c4c4f 2366 int result = string_xstring_p (string);
96db09e4
KH
2367 struct coding_system coding;
2368
0f8c4c4f 2369 if (result == 0)
96db09e4
KH
2370 {
2371 /* No multibyte character in OBJ. We need not encode it. */
0f8c4c4f 2372 *text_bytes = STRING_BYTES (XSTRING (string));
d60660d6 2373 *stringp = 1;
0f8c4c4f 2374 return XSTRING (string)->data;
96db09e4
KH
2375 }
2376
2377 setup_coding_system (coding_system, &coding);
0f8c4c4f 2378 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
35bc5887 2379 /* We suppress producing escape sequences for composition. */
0f8c4c4f 2380 coding.common_flags &= ~CODING_ANNOTATION_MASK;
b954d586
KH
2381 coding.dst_bytes = XSTRING (string)->size * 2;
2382 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes);
0f8c4c4f
KH
2383 encode_coding_object (&coding, string, 0, 0,
2384 XSTRING (string)->size,
b954d586 2385 STRING_BYTES (XSTRING (string)), Qnil);
96db09e4 2386 *text_bytes = coding.produced;
0f8c4c4f 2387 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
b954d586 2388 return coding.destination;
96db09e4
KH
2389}
2390
2391\f
75f9d625 2392/* Change the name of frame F to NAME. If NAME is nil, set F's name to
f945b920
JB
2393 x_id_name.
2394
2395 If EXPLICIT is non-zero, that indicates that lisp code is setting the
75f9d625
DM
2396 name; if NAME is a string, set F's name to NAME and set
2397 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
f945b920
JB
2398
2399 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2400 suggesting a new name, which lisp code should override; if
2401 F->explicit_name is set, ignore the new name; otherwise, set it. */
2402
2403void
2404x_set_name (f, name, explicit)
2405 struct frame *f;
2406 Lisp_Object name;
2407 int explicit;
2408{
2409 /* Make sure that requests from lisp code override requests from
2410 Emacs redisplay code. */
2411 if (explicit)
2412 {
2413 /* If we're switching from explicit to implicit, we had better
2414 update the mode lines and thereby update the title. */
2415 if (f->explicit_name && NILP (name))
cf177271 2416 update_mode_lines = 1;
f945b920
JB
2417
2418 f->explicit_name = ! NILP (name);
2419 }
2420 else if (f->explicit_name)
2421 return;
2422
2423 /* If NAME is nil, set the name to the x_id_name. */
2424 if (NILP (name))
f10f0b79
RS
2425 {
2426 /* Check for no change needed in this very common case
2427 before we do any consing. */
08a90d6a
RS
2428 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
2429 XSTRING (f->name)->data))
f10f0b79 2430 return;
08a90d6a 2431 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
f10f0b79 2432 }
62265f1c 2433 else
b7826503 2434 CHECK_STRING (name);
01f1ba30 2435
f945b920
JB
2436 /* Don't change the name if it's already NAME. */
2437 if (! NILP (Fstring_equal (name, f->name)))
daa37602
JB
2438 return;
2439
943b580d
RS
2440 f->name = name;
2441
2442 /* For setting the frame title, the title parameter should override
2443 the name parameter. */
2444 if (! NILP (f->title))
2445 name = f->title;
2446
fe24a618 2447 if (FRAME_X_WINDOW (f))
01f1ba30 2448 {
01f1ba30 2449 BLOCK_INPUT;
fe24a618
JB
2450#ifdef HAVE_X11R4
2451 {
80534dd6 2452 XTextProperty text, icon;
d60660d6 2453 int bytes, stringp;
11270583 2454 Lisp_Object coding_system;
80534dd6 2455
11270583
KH
2456 coding_system = Vlocale_coding_system;
2457 if (NILP (coding_system))
2458 coding_system = Qcompound_text;
37323f34 2459 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp);
d60660d6 2460 text.encoding = (stringp ? XA_STRING
96db09e4 2461 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
fe24a618 2462 text.format = 8;
96db09e4 2463 text.nitems = bytes;
80534dd6 2464
96db09e4
KH
2465 if (NILP (f->icon_name))
2466 {
2467 icon = text;
2468 }
2469 else
2470 {
37323f34 2471 icon.value = x_encode_text (f->icon_name, coding_system, 0,
d60660d6
KH
2472 &bytes, &stringp);
2473 icon.encoding = (stringp ? XA_STRING
96db09e4
KH
2474 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2475 icon.format = 8;
2476 icon.nitems = bytes;
2477 }
9ef48a9d 2478#ifdef USE_X_TOOLKIT
b9dc4443 2479 XSetWMName (FRAME_X_DISPLAY (f),
7556890b
RS
2480 XtWindow (f->output_data.x->widget), &text);
2481 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
80534dd6 2482 &icon);
9ef48a9d 2483#else /* not USE_X_TOOLKIT */
b9dc4443 2484 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
80534dd6 2485 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
9ef48a9d 2486#endif /* not USE_X_TOOLKIT */
96db09e4
KH
2487 if (!NILP (f->icon_name)
2488 && icon.value != XSTRING (f->icon_name)->data)
2489 xfree (icon.value);
2490 if (text.value != XSTRING (name)->data)
2491 xfree (text.value);
fe24a618 2492 }
9ef48a9d 2493#else /* not HAVE_X11R4 */
b9dc4443 2494 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
fe24a618 2495 XSTRING (name)->data);
b9dc4443 2496 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
fe24a618 2497 XSTRING (name)->data);
9ef48a9d 2498#endif /* not HAVE_X11R4 */
01f1ba30
JB
2499 UNBLOCK_INPUT;
2500 }
f945b920
JB
2501}
2502
2503/* This function should be called when the user's lisp code has
2504 specified a name for the frame; the name will override any set by the
2505 redisplay code. */
2506void
2507x_explicitly_set_name (f, arg, oldval)
2508 FRAME_PTR f;
2509 Lisp_Object arg, oldval;
2510{
2511 x_set_name (f, arg, 1);
2512}
2513
2514/* This function should be called by Emacs redisplay code to set the
2515 name; names set this way will never override names set by the user's
2516 lisp code. */
25250031 2517void
f945b920
JB
2518x_implicitly_set_name (f, arg, oldval)
2519 FRAME_PTR f;
2520 Lisp_Object arg, oldval;
2521{
2522 x_set_name (f, arg, 0);
01f1ba30 2523}
943b580d
RS
2524\f
2525/* Change the title of frame F to NAME.
2526 If NAME is nil, use the frame name as the title.
01f1ba30 2527
943b580d
RS
2528 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2529 name; if NAME is a string, set F's name to NAME and set
2530 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2531
2532 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2533 suggesting a new name, which lisp code should override; if
2534 F->explicit_name is set, ignore the new name; otherwise, set it. */
2535
2536void
d62c8769 2537x_set_title (f, name, old_name)
943b580d 2538 struct frame *f;
d62c8769 2539 Lisp_Object name, old_name;
943b580d
RS
2540{
2541 /* Don't change the title if it's already NAME. */
2542 if (EQ (name, f->title))
2543 return;
2544
2545 update_mode_lines = 1;
2546
2547 f->title = name;
2548
2549 if (NILP (name))
2550 name = f->name;
beb403b3 2551 else
b7826503 2552 CHECK_STRING (name);
943b580d
RS
2553
2554 if (FRAME_X_WINDOW (f))
2555 {
2556 BLOCK_INPUT;
2557#ifdef HAVE_X11R4
2558 {
2559 XTextProperty text, icon;
d60660d6 2560 int bytes, stringp;
11270583 2561 Lisp_Object coding_system;
943b580d 2562
11270583
KH
2563 coding_system = Vlocale_coding_system;
2564 if (NILP (coding_system))
2565 coding_system = Qcompound_text;
37323f34 2566 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp);
d60660d6 2567 text.encoding = (stringp ? XA_STRING
96db09e4 2568 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
943b580d 2569 text.format = 8;
96db09e4 2570 text.nitems = bytes;
943b580d 2571
96db09e4
KH
2572 if (NILP (f->icon_name))
2573 {
2574 icon = text;
2575 }
2576 else
2577 {
37323f34 2578 icon.value = x_encode_text (f->icon_name, coding_system, 0,
d60660d6
KH
2579 &bytes, &stringp);
2580 icon.encoding = (stringp ? XA_STRING
96db09e4
KH
2581 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2582 icon.format = 8;
2583 icon.nitems = bytes;
2584 }
943b580d
RS
2585#ifdef USE_X_TOOLKIT
2586 XSetWMName (FRAME_X_DISPLAY (f),
2587 XtWindow (f->output_data.x->widget), &text);
2588 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2589 &icon);
2590#else /* not USE_X_TOOLKIT */
2591 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2592 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2593#endif /* not USE_X_TOOLKIT */
96db09e4
KH
2594 if (!NILP (f->icon_name)
2595 && icon.value != XSTRING (f->icon_name)->data)
2596 xfree (icon.value);
2597 if (text.value != XSTRING (name)->data)
2598 xfree (text.value);
943b580d
RS
2599 }
2600#else /* not HAVE_X11R4 */
2601 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2602 XSTRING (name)->data);
2603 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2604 XSTRING (name)->data);
2605#endif /* not HAVE_X11R4 */
2606 UNBLOCK_INPUT;
2607 }
2608}
2609\f
01f1ba30 2610void
f676886a
JB
2611x_set_autoraise (f, arg, oldval)
2612 struct frame *f;
01f1ba30
JB
2613 Lisp_Object arg, oldval;
2614{
f676886a 2615 f->auto_raise = !EQ (Qnil, arg);
01f1ba30
JB
2616}
2617
2618void
f676886a
JB
2619x_set_autolower (f, arg, oldval)
2620 struct frame *f;
01f1ba30
JB
2621 Lisp_Object arg, oldval;
2622{
f676886a 2623 f->auto_lower = !EQ (Qnil, arg);
01f1ba30 2624}
179956b9 2625
eac358ef
KH
2626void
2627x_set_unsplittable (f, arg, oldval)
2628 struct frame *f;
2629 Lisp_Object arg, oldval;
2630{
2631 f->no_split = !NILP (arg);
2632}
2633
179956b9 2634void
a3c87d4e 2635x_set_vertical_scroll_bars (f, arg, oldval)
179956b9
JB
2636 struct frame *f;
2637 Lisp_Object arg, oldval;
2638{
1ab3d87e
RS
2639 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2640 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2641 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2642 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
179956b9 2643 {
1ab3d87e
RS
2644 FRAME_VERTICAL_SCROLL_BAR_TYPE (f)
2645 = (NILP (arg)
2646 ? vertical_scroll_bar_none
2647 : EQ (Qright, arg)
2648 ? vertical_scroll_bar_right
2649 : vertical_scroll_bar_left);
179956b9 2650
cf177271
JB
2651 /* We set this parameter before creating the X window for the
2652 frame, so we can get the geometry right from the start.
2653 However, if the window hasn't been created yet, we shouldn't
2654 call x_set_window_size. */
2655 if (FRAME_X_WINDOW (f))
363f7e15 2656 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
8938a4fb 2657 do_pending_window_change (0);
179956b9
JB
2658 }
2659}
4701395c
KH
2660
2661void
2662x_set_scroll_bar_width (f, arg, oldval)
2663 struct frame *f;
2664 Lisp_Object arg, oldval;
2665{
a672c74d
RS
2666 int wid = FONT_WIDTH (f->output_data.x->font);
2667
dff9a538
KH
2668 if (NILP (arg))
2669 {
c6e9d03b
GM
2670#ifdef USE_TOOLKIT_SCROLL_BARS
2671 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
333b20bb
GM
2672 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2673 FRAME_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
2674 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = width;
2675#else
2676 /* Make the actual width at least 14 pixels and a multiple of a
2677 character width. */
a672c74d 2678 FRAME_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
333b20bb
GM
2679
2680 /* Use all of that space (aside from required margins) for the
2681 scroll bar. */
dff9a538 2682 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = 0;
333b20bb 2683#endif
a672c74d 2684
a90ab372
RS
2685 if (FRAME_X_WINDOW (f))
2686 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
8938a4fb 2687 do_pending_window_change (0);
dff9a538
KH
2688 }
2689 else if (INTEGERP (arg) && XINT (arg) > 0
2690 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
4701395c 2691 {
09d8c7ac
RS
2692 if (XFASTINT (arg) <= 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM)
2693 XSETINT (arg, 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM + 1);
0a26b136 2694
4701395c
KH
2695 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
2696 FRAME_SCROLL_BAR_COLS (f) = (XFASTINT (arg) + wid-1) / wid;
2697 if (FRAME_X_WINDOW (f))
2698 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2699 }
dca97592 2700
8938a4fb 2701 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
333b20bb
GM
2702 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2703 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
4701395c 2704}
333b20bb
GM
2705
2706
01f1ba30 2707\f
f676886a 2708/* Subroutines of creating an X frame. */
01f1ba30 2709
b7975ee4
KH
2710/* Make sure that Vx_resource_name is set to a reasonable value.
2711 Fix it up, or set it to `emacs' if it is too hopeless. */
2712
d387c960
JB
2713static void
2714validate_x_resource_name ()
2715{
333b20bb 2716 int len = 0;
0e78b377
RS
2717 /* Number of valid characters in the resource name. */
2718 int good_count = 0;
2719 /* Number of invalid characters in the resource name. */
2720 int bad_count = 0;
2721 Lisp_Object new;
2722 int i;
2723
498e9ac3
RS
2724 if (!STRINGP (Vx_resource_class))
2725 Vx_resource_class = build_string (EMACS_CLASS);
2726
cf204347
RS
2727 if (STRINGP (Vx_resource_name))
2728 {
cf204347
RS
2729 unsigned char *p = XSTRING (Vx_resource_name)->data;
2730 int i;
2731
fc932ac6 2732 len = STRING_BYTES (XSTRING (Vx_resource_name));
0e78b377
RS
2733
2734 /* Only letters, digits, - and _ are valid in resource names.
2735 Count the valid characters and count the invalid ones. */
cf204347
RS
2736 for (i = 0; i < len; i++)
2737 {
2738 int c = p[i];
2739 if (! ((c >= 'a' && c <= 'z')
2740 || (c >= 'A' && c <= 'Z')
2741 || (c >= '0' && c <= '9')
2742 || c == '-' || c == '_'))
0e78b377
RS
2743 bad_count++;
2744 else
2745 good_count++;
cf204347
RS
2746 }
2747 }
2748 else
0e78b377
RS
2749 /* Not a string => completely invalid. */
2750 bad_count = 5, good_count = 0;
2751
2752 /* If name is valid already, return. */
2753 if (bad_count == 0)
2754 return;
2755
2756 /* If name is entirely invalid, or nearly so, use `emacs'. */
2757 if (good_count == 0
2758 || (good_count == 1 && bad_count > 0))
2759 {
b7975ee4 2760 Vx_resource_name = build_string ("emacs");
0e78b377
RS
2761 return;
2762 }
2763
2764 /* Name is partly valid. Copy it and replace the invalid characters
2765 with underscores. */
2766
2767 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2768
2769 for (i = 0; i < len; i++)
2770 {
2771 int c = XSTRING (new)->data[i];
2772 if (! ((c >= 'a' && c <= 'z')
2773 || (c >= 'A' && c <= 'Z')
2774 || (c >= '0' && c <= '9')
2775 || c == '-' || c == '_'))
2776 XSTRING (new)->data[i] = '_';
2777 }
d387c960
JB
2778}
2779
2780
01f1ba30 2781extern char *x_get_string_resource ();
01f1ba30 2782
cf177271 2783DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
03265352 2784 doc: /* Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.
c061c855
GM
2785This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the
2786class, where INSTANCE is the name under which Emacs was invoked, or
2787the name specified by the `-name' or `-rn' command-line arguments.
2788
2789The optional arguments COMPONENT and SUBCLASS add to the key and the
2790class, respectively. You must specify both of them or neither.
2791If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'
7ee72033
MB
2792and the class is `Emacs.CLASS.SUBCLASS'. */)
2793 (attribute, class, component, subclass)
cf177271 2794 Lisp_Object attribute, class, component, subclass;
01f1ba30
JB
2795{
2796 register char *value;
2797 char *name_key;
2798 char *class_key;
2799
11ae94fe
RS
2800 check_x ();
2801
b7826503
PJ
2802 CHECK_STRING (attribute);
2803 CHECK_STRING (class);
cf177271 2804
8fabe6f4 2805 if (!NILP (component))
b7826503 2806 CHECK_STRING (component);
8fabe6f4 2807 if (!NILP (subclass))
b7826503 2808 CHECK_STRING (subclass);
8fabe6f4
RS
2809 if (NILP (component) != NILP (subclass))
2810 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2811
d387c960
JB
2812 validate_x_resource_name ();
2813
b7975ee4
KH
2814 /* Allocate space for the components, the dots which separate them,
2815 and the final '\0'. Make them big enough for the worst case. */
fc932ac6 2816 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
b7975ee4 2817 + (STRINGP (component)
fc932ac6
RS
2818 ? STRING_BYTES (XSTRING (component)) : 0)
2819 + STRING_BYTES (XSTRING (attribute))
b7975ee4
KH
2820 + 3);
2821
fc932ac6
RS
2822 class_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_class))
2823 + STRING_BYTES (XSTRING (class))
b7975ee4 2824 + (STRINGP (subclass)
fc932ac6 2825 ? STRING_BYTES (XSTRING (subclass)) : 0)
b7975ee4
KH
2826 + 3);
2827
2828 /* Start with emacs.FRAMENAME for the name (the specific one)
2829 and with `Emacs' for the class key (the general one). */
2830 strcpy (name_key, XSTRING (Vx_resource_name)->data);
498e9ac3 2831 strcpy (class_key, XSTRING (Vx_resource_class)->data);
b7975ee4
KH
2832
2833 strcat (class_key, ".");
2834 strcat (class_key, XSTRING (class)->data);
2835
2836 if (!NILP (component))
01f1ba30 2837 {
b7975ee4
KH
2838 strcat (class_key, ".");
2839 strcat (class_key, XSTRING (subclass)->data);
2840
2841 strcat (name_key, ".");
2842 strcat (name_key, XSTRING (component)->data);
01f1ba30
JB
2843 }
2844
b7975ee4
KH
2845 strcat (name_key, ".");
2846 strcat (name_key, XSTRING (attribute)->data);
2847
b9dc4443
RS
2848 value = x_get_string_resource (check_x_display_info (Qnil)->xrdb,
2849 name_key, class_key);
01f1ba30
JB
2850
2851 if (value != (char *) 0)
2852 return build_string (value);
2853 else
2854 return Qnil;
2855}
2856
abb4b7ec
RS
2857/* Get an X resource, like Fx_get_resource, but for display DPYINFO. */
2858
333b20bb 2859Lisp_Object
abb4b7ec
RS
2860display_x_get_resource (dpyinfo, attribute, class, component, subclass)
2861 struct x_display_info *dpyinfo;
2862 Lisp_Object attribute, class, component, subclass;
2863{
2864 register char *value;
2865 char *name_key;
2866 char *class_key;
2867
b7826503
PJ
2868 CHECK_STRING (attribute);
2869 CHECK_STRING (class);
abb4b7ec
RS
2870
2871 if (!NILP (component))
b7826503 2872 CHECK_STRING (component);
abb4b7ec 2873 if (!NILP (subclass))
b7826503 2874 CHECK_STRING (subclass);
abb4b7ec
RS
2875 if (NILP (component) != NILP (subclass))
2876 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2877
2878 validate_x_resource_name ();
2879
2880 /* Allocate space for the components, the dots which separate them,
2881 and the final '\0'. Make them big enough for the worst case. */
fc932ac6 2882 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_name))
abb4b7ec 2883 + (STRINGP (component)
fc932ac6
RS
2884 ? STRING_BYTES (XSTRING (component)) : 0)
2885 + STRING_BYTES (XSTRING (attribute))
abb4b7ec
RS
2886 + 3);
2887
fc932ac6
RS
2888 class_key = (char *) alloca (STRING_BYTES (XSTRING (Vx_resource_class))
2889 + STRING_BYTES (XSTRING (class))
abb4b7ec 2890 + (STRINGP (subclass)
fc932ac6 2891 ? STRING_BYTES (XSTRING (subclass)) : 0)
abb4b7ec
RS
2892 + 3);
2893
2894 /* Start with emacs.FRAMENAME for the name (the specific one)
2895 and with `Emacs' for the class key (the general one). */
2896 strcpy (name_key, XSTRING (Vx_resource_name)->data);
2897 strcpy (class_key, XSTRING (Vx_resource_class)->data);
2898
2899 strcat (class_key, ".");
2900 strcat (class_key, XSTRING (class)->data);
2901
2902 if (!NILP (component))
2903 {
2904 strcat (class_key, ".");
2905 strcat (class_key, XSTRING (subclass)->data);
2906
2907 strcat (name_key, ".");
2908 strcat (name_key, XSTRING (component)->data);
2909 }
2910
2911 strcat (name_key, ".");
2912 strcat (name_key, XSTRING (attribute)->data);
2913
2914 value = x_get_string_resource (dpyinfo->xrdb, name_key, class_key);
2915
2916 if (value != (char *) 0)
2917 return build_string (value);
2918 else
2919 return Qnil;
2920}
2921
3402e1a4
RS
2922/* Used when C code wants a resource value. */
2923
2924char *
2925x_get_resource_string (attribute, class)
2926 char *attribute, *class;
2927{
3402e1a4
RS
2928 char *name_key;
2929 char *class_key;
0fe92f72 2930 struct frame *sf = SELECTED_FRAME ();
3402e1a4
RS
2931
2932 /* Allocate space for the components, the dots which separate them,
2933 and the final '\0'. */
fc932ac6 2934 name_key = (char *) alloca (STRING_BYTES (XSTRING (Vinvocation_name))
3402e1a4
RS
2935 + strlen (attribute) + 2);
2936 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2937 + strlen (class) + 2);
2938
2939 sprintf (name_key, "%s.%s",
2940 XSTRING (Vinvocation_name)->data,
2941 attribute);
2942 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2943
0fe92f72 2944 return x_get_string_resource (FRAME_X_DISPLAY_INFO (sf)->xrdb,
b9dc4443 2945 name_key, class_key);
3402e1a4
RS
2946}
2947
60fb3ee1
JB
2948/* Types we might convert a resource string into. */
2949enum resource_types
333b20bb
GM
2950{
2951 RES_TYPE_NUMBER,
d62c8769 2952 RES_TYPE_FLOAT,
333b20bb
GM
2953 RES_TYPE_BOOLEAN,
2954 RES_TYPE_STRING,
2955 RES_TYPE_SYMBOL
2956};
60fb3ee1 2957
01f1ba30 2958/* Return the value of parameter PARAM.
60fb3ee1 2959
f676886a 2960 First search ALIST, then Vdefault_frame_alist, then the X defaults
cf177271 2961 database, using ATTRIBUTE as the attribute name and CLASS as its class.
60fb3ee1
JB
2962
2963 Convert the resource to the type specified by desired_type.
2964
f9942c9e
JB
2965 If no default is specified, return Qunbound. If you call
2966 x_get_arg, make sure you deal with Qunbound in a reasonable way,
a59e4f3d 2967 and don't let it get stored in any Lisp-visible variables! */
01f1ba30
JB
2968
2969static Lisp_Object
abb4b7ec
RS
2970x_get_arg (dpyinfo, alist, param, attribute, class, type)
2971 struct x_display_info *dpyinfo;
3c254570 2972 Lisp_Object alist, param;
60fb3ee1 2973 char *attribute;
cf177271 2974 char *class;
60fb3ee1 2975 enum resource_types type;
01f1ba30
JB
2976{
2977 register Lisp_Object tem;
2978
2979 tem = Fassq (param, alist);
2980 if (EQ (tem, Qnil))
f676886a 2981 tem = Fassq (param, Vdefault_frame_alist);
f9942c9e 2982 if (EQ (tem, Qnil))
01f1ba30 2983 {
60fb3ee1 2984
f9942c9e 2985 if (attribute)
60fb3ee1 2986 {
abb4b7ec
RS
2987 tem = display_x_get_resource (dpyinfo,
2988 build_string (attribute),
2989 build_string (class),
2990 Qnil, Qnil);
f9942c9e
JB
2991
2992 if (NILP (tem))
2993 return Qunbound;
2994
2995 switch (type)
2996 {
333b20bb 2997 case RES_TYPE_NUMBER:
f9942c9e
JB
2998 return make_number (atoi (XSTRING (tem)->data));
2999
d62c8769
GM
3000 case RES_TYPE_FLOAT:
3001 return make_float (atof (XSTRING (tem)->data));
3002
333b20bb 3003 case RES_TYPE_BOOLEAN:
f9942c9e
JB
3004 tem = Fdowncase (tem);
3005 if (!strcmp (XSTRING (tem)->data, "on")
3006 || !strcmp (XSTRING (tem)->data, "true"))
3007 return Qt;
3008 else
3009 return Qnil;
3010
333b20bb 3011 case RES_TYPE_STRING:
f9942c9e
JB
3012 return tem;
3013
333b20bb 3014 case RES_TYPE_SYMBOL:
49795535
JB
3015 /* As a special case, we map the values `true' and `on'
3016 to Qt, and `false' and `off' to Qnil. */
3017 {
98381190
KH
3018 Lisp_Object lower;
3019 lower = Fdowncase (tem);
26ae6b61
KH
3020 if (!strcmp (XSTRING (lower)->data, "on")
3021 || !strcmp (XSTRING (lower)->data, "true"))
49795535 3022 return Qt;
26ae6b61
KH
3023 else if (!strcmp (XSTRING (lower)->data, "off")
3024 || !strcmp (XSTRING (lower)->data, "false"))
49795535
JB
3025 return Qnil;
3026 else
89032215 3027 return Fintern (tem, Qnil);
49795535 3028 }
f945b920 3029
f9942c9e
JB
3030 default:
3031 abort ();
3032 }
60fb3ee1 3033 }
f9942c9e
JB
3034 else
3035 return Qunbound;
01f1ba30
JB
3036 }
3037 return Fcdr (tem);
3038}
3039
e4f79258
RS
3040/* Like x_get_arg, but also record the value in f->param_alist. */
3041
3042static Lisp_Object
3043x_get_and_record_arg (f, alist, param, attribute, class, type)
3044 struct frame *f;
3045 Lisp_Object alist, param;
3046 char *attribute;
3047 char *class;
3048 enum resource_types type;
3049{
3050 Lisp_Object value;
3051
abb4b7ec
RS
3052 value = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, param,
3053 attribute, class, type);
e4f79258
RS
3054 if (! NILP (value))
3055 store_frame_param (f, param, value);
3056
3057 return value;
3058}
3059
f676886a 3060/* Record in frame F the specified or default value according to ALIST
e8cc313b
KH
3061 of the parameter named PROP (a Lisp symbol).
3062 If no value is specified for PROP, look for an X default for XPROP
f676886a 3063 on the frame named NAME.
01f1ba30
JB
3064 If that is not found either, use the value DEFLT. */
3065
3066static Lisp_Object
cf177271 3067x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
f676886a 3068 struct frame *f;
01f1ba30 3069 Lisp_Object alist;
f9942c9e 3070 Lisp_Object prop;
01f1ba30
JB
3071 Lisp_Object deflt;
3072 char *xprop;
cf177271 3073 char *xclass;
60fb3ee1 3074 enum resource_types type;
01f1ba30 3075{
01f1ba30
JB
3076 Lisp_Object tem;
3077
abb4b7ec 3078 tem = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, prop, xprop, xclass, type);
f9942c9e 3079 if (EQ (tem, Qunbound))
01f1ba30 3080 tem = deflt;
f9942c9e 3081 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
01f1ba30
JB
3082 return tem;
3083}
333b20bb
GM
3084
3085
3086/* Record in frame F the specified or default value according to ALIST
3087 of the parameter named PROP (a Lisp symbol). If no value is
3088 specified for PROP, look for an X default for XPROP on the frame
3089 named NAME. If that is not found either, use the value DEFLT. */
3090
3091static Lisp_Object
3092x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
3093 foreground_p)
3094 struct frame *f;
3095 Lisp_Object alist;
3096 Lisp_Object prop;
3097 char *xprop;
3098 char *xclass;
3099 int foreground_p;
3100{
3101 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3102 Lisp_Object tem;
3103
3104 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
3105 if (EQ (tem, Qunbound))
3106 {
3107#ifdef USE_TOOLKIT_SCROLL_BARS
3108
3109 /* See if an X resource for the scroll bar color has been
3110 specified. */
3111 tem = display_x_get_resource (dpyinfo,
3112 build_string (foreground_p
3113 ? "foreground"
3114 : "background"),
c0ec53ad 3115 empty_string,
333b20bb 3116 build_string ("verticalScrollBar"),
c0ec53ad 3117 empty_string);
333b20bb
GM
3118 if (!STRINGP (tem))
3119 {
3120 /* If nothing has been specified, scroll bars will use a
3121 toolkit-dependent default. Because these defaults are
3122 difficult to get at without actually creating a scroll
3123 bar, use nil to indicate that no color has been
3124 specified. */
3125 tem = Qnil;
3126 }
3127
3128#else /* not USE_TOOLKIT_SCROLL_BARS */
3129
3130 tem = Qnil;
3131
3132#endif /* not USE_TOOLKIT_SCROLL_BARS */
3133 }
3134
3135 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3136 return tem;
3137}
3138
3139
01f1ba30 3140\f
8af1d7ca 3141DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
7ee72033 3142 doc: /* Parse an X-style geometry string STRING.
c061c855
GM
3143Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
3144The properties returned may include `top', `left', `height', and `width'.
3145The value of `left' or `top' may be an integer,
3146or a list (+ N) meaning N pixels relative to top/left corner,
7ee72033
MB
3147or a list (- N) meaning -N pixels relative to bottom/right corner. */)
3148 (string)
a6605e5c 3149 Lisp_Object string;
01f1ba30
JB
3150{
3151 int geometry, x, y;
3152 unsigned int width, height;
f83f10ba 3153 Lisp_Object result;
01f1ba30 3154
b7826503 3155 CHECK_STRING (string);
01f1ba30
JB
3156
3157 geometry = XParseGeometry ((char *) XSTRING (string)->data,
3158 &x, &y, &width, &height);
3159
f83f10ba
RS
3160#if 0
3161 if (!!(geometry & XValue) != !!(geometry & YValue))
3162 error ("Must specify both x and y position, or neither");
3163#endif
3164
3165 result = Qnil;
3166 if (geometry & XValue)
01f1ba30 3167 {
f83f10ba
RS
3168 Lisp_Object element;
3169
e1d962d7
RS
3170 if (x >= 0 && (geometry & XNegative))
3171 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3172 else if (x < 0 && ! (geometry & XNegative))
3173 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
f83f10ba
RS
3174 else
3175 element = Fcons (Qleft, make_number (x));
3176 result = Fcons (element, result);
3177 }
3178
3179 if (geometry & YValue)
3180 {
3181 Lisp_Object element;
3182
e1d962d7
RS
3183 if (y >= 0 && (geometry & YNegative))
3184 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3185 else if (y < 0 && ! (geometry & YNegative))
3186 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
f83f10ba
RS
3187 else
3188 element = Fcons (Qtop, make_number (y));
3189 result = Fcons (element, result);
01f1ba30 3190 }
f83f10ba
RS
3191
3192 if (geometry & WidthValue)
3193 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3194 if (geometry & HeightValue)
3195 result = Fcons (Fcons (Qheight, make_number (height)), result);
3196
3197 return result;
01f1ba30
JB
3198}
3199
01f1ba30 3200/* Calculate the desired size and position of this window,
f83f10ba 3201 and return the flags saying which aspects were specified.
8fc2766b
RS
3202
3203 This function does not make the coordinates positive. */
01f1ba30
JB
3204
3205#define DEFAULT_ROWS 40
3206#define DEFAULT_COLS 80
3207
f9942c9e 3208static int
f676886a
JB
3209x_figure_window_size (f, parms)
3210 struct frame *f;
01f1ba30
JB
3211 Lisp_Object parms;
3212{
4fe1de12 3213 register Lisp_Object tem0, tem1, tem2;
01f1ba30 3214 long window_prompting = 0;
abb4b7ec 3215 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
01f1ba30
JB
3216
3217 /* Default values if we fall through.
3218 Actually, if that happens we should get
b9dc4443 3219 window manager prompting. */
1ab3d87e 3220 SET_FRAME_WIDTH (f, DEFAULT_COLS);
f676886a 3221 f->height = DEFAULT_ROWS;
bd0b85c3
RS
3222 /* Window managers expect that if program-specified
3223 positions are not (0,0), they're intentional, not defaults. */
7556890b
RS
3224 f->output_data.x->top_pos = 0;
3225 f->output_data.x->left_pos = 0;
01f1ba30 3226
333b20bb
GM
3227 tem0 = x_get_arg (dpyinfo, parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3228 tem1 = x_get_arg (dpyinfo, parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3229 tem2 = x_get_arg (dpyinfo, parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
f83f10ba 3230 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
01f1ba30 3231 {
f83f10ba
RS
3232 if (!EQ (tem0, Qunbound))
3233 {
b7826503 3234 CHECK_NUMBER (tem0);
f83f10ba
RS
3235 f->height = XINT (tem0);
3236 }
3237 if (!EQ (tem1, Qunbound))
3238 {
b7826503 3239 CHECK_NUMBER (tem1);
1ab3d87e 3240 SET_FRAME_WIDTH (f, XINT (tem1));
f83f10ba
RS
3241 }
3242 if (!NILP (tem2) && !EQ (tem2, Qunbound))
4fe1de12
RS
3243 window_prompting |= USSize;
3244 else
3245 window_prompting |= PSize;
01f1ba30 3246 }
01f1ba30 3247
7556890b 3248 f->output_data.x->vertical_scroll_bar_extra
a444c70b
KH
3249 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3250 ? 0
7556890b 3251 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
b3ba0aa8
KS
3252
3253 x_compute_fringe_widths (f, 0);
3254
7556890b
RS
3255 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3256 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
01f1ba30 3257
333b20bb
GM
3258 tem0 = x_get_arg (dpyinfo, parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3259 tem1 = x_get_arg (dpyinfo, parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3260 tem2 = x_get_arg (dpyinfo, parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
f83f10ba 3261 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
01f1ba30 3262 {
f83f10ba
RS
3263 if (EQ (tem0, Qminus))
3264 {
7556890b 3265 f->output_data.x->top_pos = 0;
f83f10ba
RS
3266 window_prompting |= YNegative;
3267 }
8e713be6
KR
3268 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3269 && CONSP (XCDR (tem0))
3270 && INTEGERP (XCAR (XCDR (tem0))))
e1d962d7 3271 {
8e713be6 3272 f->output_data.x->top_pos = - XINT (XCAR (XCDR (tem0)));
e1d962d7
RS
3273 window_prompting |= YNegative;
3274 }
8e713be6
KR
3275 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3276 && CONSP (XCDR (tem0))
3277 && INTEGERP (XCAR (XCDR (tem0))))
e1d962d7 3278 {
8e713be6 3279 f->output_data.x->top_pos = XINT (XCAR (XCDR (tem0)));
e1d962d7 3280 }
f83f10ba 3281 else if (EQ (tem0, Qunbound))
7556890b 3282 f->output_data.x->top_pos = 0;
f83f10ba
RS
3283 else
3284 {
b7826503 3285 CHECK_NUMBER (tem0);
7556890b
RS
3286 f->output_data.x->top_pos = XINT (tem0);
3287 if (f->output_data.x->top_pos < 0)
f83f10ba
RS
3288 window_prompting |= YNegative;
3289 }
3290
3291 if (EQ (tem1, Qminus))
3292 {
7556890b 3293 f->output_data.x->left_pos = 0;
f83f10ba
RS
3294 window_prompting |= XNegative;
3295 }
8e713be6
KR
3296 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3297 && CONSP (XCDR (tem1))
3298 && INTEGERP (XCAR (XCDR (tem1))))
e1d962d7 3299 {
8e713be6 3300 f->output_data.x->left_pos = - XINT (XCAR (XCDR (tem1)));
e1d962d7
RS
3301 window_prompting |= XNegative;
3302 }
8e713be6
KR
3303 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3304 && CONSP (XCDR (tem1))
3305 && INTEGERP (XCAR (XCDR (tem1))))
e1d962d7 3306 {
8e713be6 3307 f->output_data.x->left_pos = XINT (XCAR (XCDR (tem1)));
e1d962d7 3308 }
f83f10ba 3309 else if (EQ (tem1, Qunbound))
7556890b 3310 f->output_data.x->left_pos = 0;
f83f10ba
RS
3311 else
3312 {
b7826503 3313 CHECK_NUMBER (tem1);
7556890b
RS
3314 f->output_data.x->left_pos = XINT (tem1);
3315 if (f->output_data.x->left_pos < 0)
f83f10ba
RS
3316 window_prompting |= XNegative;
3317 }
3318
c3724dc2 3319 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
4fe1de12
RS
3320 window_prompting |= USPosition;
3321 else
3322 window_prompting |= PPosition;
01f1ba30 3323 }
f83f10ba 3324
49d41073
EZ
3325 if (f->output_data.x->want_fullscreen != FULLSCREEN_NONE)
3326 {
3327 int left, top;
3328 int width, height;
3329
3330 /* It takes both for some WM:s to place it where we want */
3331 window_prompting = USPosition | PPosition;
3332 x_fullscreen_adjust (f, &width, &height, &top, &left);
3333 f->width = width;
3334 f->height = height;
3335 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3336 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3337 f->output_data.x->left_pos = left;
3338 f->output_data.x->top_pos = top;
3339 }
3340
739f2f53 3341 return window_prompting;
01f1ba30
JB
3342}
3343
f58534a3
RS
3344#if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
3345
3346Status
3347XSetWMProtocols (dpy, w, protocols, count)
3348 Display *dpy;
3349 Window w;
3350 Atom *protocols;
3351 int count;
3352{
3353 Atom prop;
3354 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
3355 if (prop == None) return False;
3356 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
3357 (unsigned char *) protocols, count);
3358 return True;
3359}
9ef48a9d
RS
3360#endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
3361\f
3362#ifdef USE_X_TOOLKIT
3363
8e3d10a9
RS
3364/* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
3365 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
59aa6c90
RS
3366 already be present because of the toolkit (Motif adds some of them,
3367 for example, but Xt doesn't). */
9ef48a9d
RS
3368
3369static void
b9dc4443
RS
3370hack_wm_protocols (f, widget)
3371 FRAME_PTR f;
9ef48a9d
RS
3372 Widget widget;
3373{
3374 Display *dpy = XtDisplay (widget);
3375 Window w = XtWindow (widget);
3376 int need_delete = 1;
3377 int need_focus = 1;
59aa6c90 3378 int need_save = 1;
9ef48a9d
RS
3379
3380 BLOCK_INPUT;
3381 {
3382 Atom type, *atoms = 0;
3383 int format = 0;
3384 unsigned long nitems = 0;
3385 unsigned long bytes_after;
3386
270958e8
KH
3387 if ((XGetWindowProperty (dpy, w,
3388 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
34d5ae1e 3389 (long)0, (long)100, False, XA_ATOM,
270958e8
KH
3390 &type, &format, &nitems, &bytes_after,
3391 (unsigned char **) &atoms)
3392 == Success)
9ef48a9d
RS
3393 && format == 32 && type == XA_ATOM)
3394 while (nitems > 0)
3395 {
3396 nitems--;
b9dc4443
RS
3397 if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
3398 need_delete = 0;
3399 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
3400 need_focus = 0;
3401 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
3402 need_save = 0;
9ef48a9d
RS
3403 }
3404 if (atoms) XFree ((char *) atoms);
3405 }
3406 {
3407 Atom props [10];
3408 int count = 0;
b9dc4443
RS
3409 if (need_delete)
3410 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3411 if (need_focus)
3412 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
3413 if (need_save)
3414 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
9ef48a9d 3415 if (count)
b9dc4443
RS
3416 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3417 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
3418 (unsigned char *) props, count);
3419 }
3420 UNBLOCK_INPUT;
3421}
3422#endif
86779fac
GM
3423
3424
5a7df7d7
GM
3425\f
3426/* Support routines for XIC (X Input Context). */
86779fac 3427
5a7df7d7
GM
3428#ifdef HAVE_X_I18N
3429
3430static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
3431static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
3432
3433
3434/* Supported XIM styles, ordered by preferenc. */
3435
3436static XIMStyle supported_xim_styles[] =
3437{
3438 XIMPreeditPosition | XIMStatusArea,
3439 XIMPreeditPosition | XIMStatusNothing,
3440 XIMPreeditPosition | XIMStatusNone,
3441 XIMPreeditNothing | XIMStatusArea,
3442 XIMPreeditNothing | XIMStatusNothing,
3443 XIMPreeditNothing | XIMStatusNone,
3444 XIMPreeditNone | XIMStatusArea,
3445 XIMPreeditNone | XIMStatusNothing,
3446 XIMPreeditNone | XIMStatusNone,
3447 0,
3448};
3449
3450
3451/* Create an X fontset on frame F with base font name
3452 BASE_FONTNAME.. */
3453
3454static XFontSet
3455xic_create_xfontset (f, base_fontname)
86779fac 3456 struct frame *f;
5a7df7d7 3457 char *base_fontname;
86779fac 3458{
5a7df7d7
GM
3459 XFontSet xfs;
3460 char **missing_list;
3461 int missing_count;
3462 char *def_string;
86779fac 3463
5a7df7d7
GM
3464 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
3465 base_fontname, &missing_list,
3466 &missing_count, &def_string);
3467 if (missing_list)
3468 XFreeStringList (missing_list);
3469
3470 /* No need to free def_string. */
3471 return xfs;
3472}
3473
3474
3475/* Value is the best input style, given user preferences USER (already
3476 checked to be supported by Emacs), and styles supported by the
3477 input method XIM. */
3478
3479static XIMStyle
3480best_xim_style (user, xim)
3481 XIMStyles *user;
3482 XIMStyles *xim;
3483{
3484 int i, j;
3485
3486 for (i = 0; i < user->count_styles; ++i)
3487 for (j = 0; j < xim->count_styles; ++j)
3488 if (user->supported_styles[i] == xim->supported_styles[j])
3489 return user->supported_styles[i];
3490
3491 /* Return the default style. */
3492 return XIMPreeditNothing | XIMStatusNothing;
3493}
3494
3495/* Create XIC for frame F. */
3496
5df79d3d
GM
3497static XIMStyle xic_style;
3498
5a7df7d7
GM
3499void
3500create_frame_xic (f)
3501 struct frame *f;
3502{
5a7df7d7
GM
3503 XIM xim;
3504 XIC xic = NULL;
3505 XFontSet xfs = NULL;
86779fac 3506
5a7df7d7
GM
3507 if (FRAME_XIC (f))
3508 return;
3509
3510 xim = FRAME_X_XIM (f);
3511 if (xim)
3512 {
d9d57cb2
DL
3513 XRectangle s_area;
3514 XPoint spot;
5a7df7d7
GM
3515 XVaNestedList preedit_attr;
3516 XVaNestedList status_attr;
3517 char *base_fontname;
3518 int fontset;
3519
d9d57cb2
DL
3520 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
3521 spot.x = 0; spot.y = 1;
5a7df7d7
GM
3522 /* Create X fontset. */
3523 fontset = FRAME_FONTSET (f);
3524 if (fontset < 0)
3525 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3526 else
3527 {
6ecb43ce
KH
3528 /* Determine the base fontname from the ASCII font name of
3529 FONTSET. */
3530 char *ascii_font = (char *) XSTRING (fontset_ascii (fontset))->data;
3531 char *p = ascii_font;
5a7df7d7 3532 int i;
6ecb43ce
KH
3533
3534 for (i = 0; *p; p++)
3535 if (*p == '-') i++;
3536 if (i != 14)
3537 /* As the font name doesn't conform to XLFD, we can't
3538 modify it to get a suitable base fontname for the
3539 frame. */
3540 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3541 else
3542 {
3543 int len = strlen (ascii_font) + 1;
8ec8a5ec 3544 char *p1 = NULL;
6ecb43ce
KH
3545
3546 for (i = 0, p = ascii_font; i < 8; p++)
3547 {
3548 if (*p == '-')
3549 {
3550 i++;
3551 if (i == 3)
3552 p1 = p + 1;
3553 }
3554 }
3555 base_fontname = (char *) alloca (len);
3556 bzero (base_fontname, len);
3557 strcpy (base_fontname, "-*-*-");
3558 bcopy (p1, base_fontname + 5, p - p1);
3559 strcat (base_fontname, "*-*-*-*-*-*-*");
3560 }
5a7df7d7
GM
3561 }
3562 xfs = xic_create_xfontset (f, base_fontname);
86779fac 3563
5a7df7d7
GM
3564 /* Determine XIC style. */
3565 if (xic_style == 0)
3566 {
3567 XIMStyles supported_list;
3568 supported_list.count_styles = (sizeof supported_xim_styles
3569 / sizeof supported_xim_styles[0]);
3570 supported_list.supported_styles = supported_xim_styles;
3571 xic_style = best_xim_style (&supported_list,
3572 FRAME_X_XIM_STYLES (f));
3573 }
86779fac 3574
5a7df7d7
GM
3575 preedit_attr = XVaCreateNestedList (0,
3576 XNFontSet, xfs,
3577 XNForeground,
3578 FRAME_FOREGROUND_PIXEL (f),
3579 XNBackground,
3580 FRAME_BACKGROUND_PIXEL (f),
3581 (xic_style & XIMPreeditPosition
3582 ? XNSpotLocation
3583 : NULL),
3584 &spot,
3585 NULL);
3586 status_attr = XVaCreateNestedList (0,
3587 XNArea,
3588 &s_area,
3589 XNFontSet,
3590 xfs,
3591 XNForeground,
3592 FRAME_FOREGROUND_PIXEL (f),
3593 XNBackground,
3594 FRAME_BACKGROUND_PIXEL (f),
3595 NULL);
3596
3597 xic = XCreateIC (xim,
3598 XNInputStyle, xic_style,
3599 XNClientWindow, FRAME_X_WINDOW(f),
3600 XNFocusWindow, FRAME_X_WINDOW(f),
3601 XNStatusAttributes, status_attr,
3602 XNPreeditAttributes, preedit_attr,
3603 NULL);
3604 XFree (preedit_attr);
3605 XFree (status_attr);
3606 }
3607
3608 FRAME_XIC (f) = xic;
3609 FRAME_XIC_STYLE (f) = xic_style;
3610 FRAME_XIC_FONTSET (f) = xfs;
86779fac
GM
3611}
3612
5a7df7d7
GM
3613
3614/* Destroy XIC and free XIC fontset of frame F, if any. */
3615
3616void
3617free_frame_xic (f)
3618 struct frame *f;
3619{
3620 if (FRAME_XIC (f) == NULL)
3621 return;
3622
3623 XDestroyIC (FRAME_XIC (f));
3624 if (FRAME_XIC_FONTSET (f))
3625 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3626
3627 FRAME_XIC (f) = NULL;
3628 FRAME_XIC_FONTSET (f) = NULL;
3629}
3630
3631
3632/* Place preedit area for XIC of window W's frame to specified
3633 pixel position X/Y. X and Y are relative to window W. */
3634
3635void
3636xic_set_preeditarea (w, x, y)
3637 struct window *w;
3638 int x, y;
3639{
3640 struct frame *f = XFRAME (w->frame);
3641 XVaNestedList attr;
3642 XPoint spot;
3643
3644 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x);
3645 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
3646 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
3647 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3648 XFree (attr);
3649}
3650
3651
3652/* Place status area for XIC in bottom right corner of frame F.. */
3653
3654void
3655xic_set_statusarea (f)
3656 struct frame *f;
3657{
3658 XIC xic = FRAME_XIC (f);
3659 XVaNestedList attr;
3660 XRectangle area;
3661 XRectangle *needed;
3662
3663 /* Negotiate geometry of status area. If input method has existing
3664 status area, use its current size. */
3665 area.x = area.y = area.width = area.height = 0;
3666 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
3667 XSetICValues (xic, XNStatusAttributes, attr, NULL);
3668 XFree (attr);
3669
3670 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
3671 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3672 XFree (attr);
3673
3674 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
3675 {
3676 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
3677 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3678 XFree (attr);
3679 }
3680
3681 area.width = needed->width;
3682 area.height = needed->height;
3683 area.x = PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
3684 area.y = (PIXEL_HEIGHT (f) - area.height
3685 - FRAME_MENUBAR_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f));
3686 XFree (needed);
3687
3688 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
3689 XSetICValues(xic, XNStatusAttributes, attr, NULL);
3690 XFree (attr);
3691}
3692
3693
3694/* Set X fontset for XIC of frame F, using base font name
3695 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
3696
3697void
3698xic_set_xfontset (f, base_fontname)
3699 struct frame *f;
3700 char *base_fontname;
3701{
3702 XVaNestedList attr;
3703 XFontSet xfs;
3704
3705 xfs = xic_create_xfontset (f, base_fontname);
3706
3707 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
3708 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
3709 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3710 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
3711 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
3712 XFree (attr);
3713
3714 if (FRAME_XIC_FONTSET (f))
3715 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3716 FRAME_XIC_FONTSET (f) = xfs;
3717}
3718
3719#endif /* HAVE_X_I18N */
3720
3721
9ef48a9d 3722\f
8fc2766b
RS
3723#ifdef USE_X_TOOLKIT
3724
3725/* Create and set up the X widget for frame F. */
f58534a3 3726
01f1ba30 3727static void
a7f7d550
FP
3728x_window (f, window_prompting, minibuffer_only)
3729 struct frame *f;
3730 long window_prompting;
3731 int minibuffer_only;
01f1ba30 3732{
9ef48a9d 3733 XClassHint class_hints;
31ac8d8c
FP
3734 XSetWindowAttributes attributes;
3735 unsigned long attribute_mask;
9ef48a9d
RS
3736 Widget shell_widget;
3737 Widget pane_widget;
6c32dd68 3738 Widget frame_widget;
9ef48a9d
RS
3739 Arg al [25];
3740 int ac;
3741
3742 BLOCK_INPUT;
3743
b7975ee4
KH
3744 /* Use the resource name as the top-level widget name
3745 for looking up resources. Make a non-Lisp copy
3746 for the window manager, so GC relocation won't bother it.
3747
3748 Elsewhere we specify the window name for the window manager. */
3749
cca176a0 3750 {
b7975ee4
KH
3751 char *str = (char *) XSTRING (Vx_resource_name)->data;
3752 f->namebuf = (char *) xmalloc (strlen (str) + 1);
cca176a0
KH
3753 strcpy (f->namebuf, str);
3754 }
9ef48a9d
RS
3755
3756 ac = 0;
3757 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
3758 XtSetArg (al[ac], XtNinput, 1); ac++;
97787173 3759 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
7556890b 3760 XtSetArg (al[ac], XtNborderWidth, f->output_data.x->border_width); ac++;
9b2956e2
GM
3761 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3762 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3763 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
cca176a0 3764 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
7a994728 3765 applicationShellWidgetClass,
82c90203 3766 FRAME_X_DISPLAY (f), al, ac);
9ef48a9d 3767
7556890b 3768 f->output_data.x->widget = shell_widget;
9ef48a9d
RS
3769 /* maybe_set_screen_title_format (shell_widget); */
3770
6c32dd68
PR
3771 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
3772 (widget_value *) NULL,
3773 shell_widget, False,
3774 (lw_callback) NULL,
3775 (lw_callback) NULL,
b6e11efd 3776 (lw_callback) NULL,
6c32dd68 3777 (lw_callback) NULL);
9ef48a9d 3778
9b2956e2
GM
3779 ac = 0;
3780 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3781 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3782 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3783 XtSetValues (pane_widget, al, ac);
7556890b 3784 f->output_data.x->column_widget = pane_widget;
a7f7d550 3785
9ef48a9d 3786 /* mappedWhenManaged to false tells to the paned window to not map/unmap
5e65b9ab 3787 the emacs screen when changing menubar. This reduces flickering. */
9ef48a9d
RS
3788
3789 ac = 0;
3790 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3791 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
3792 XtSetArg (al[ac], XtNallowResize, 1); ac++;
3793 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
3794 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
9b2956e2
GM
3795 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3796 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3797 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3798 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
3799 al, ac);
9ef48a9d 3800
7556890b 3801 f->output_data.x->edit_widget = frame_widget;
9ef48a9d 3802
6c32dd68 3803 XtManageChild (frame_widget);
a7f7d550
FP
3804
3805 /* Do some needed geometry management. */
3806 {
3807 int len;
3808 char *tem, shell_position[32];
3809 Arg al[2];
3810 int ac = 0;
5031cc10 3811 int extra_borders = 0;
8fc2766b 3812 int menubar_size
7556890b
RS
3813 = (f->output_data.x->menubar_widget
3814 ? (f->output_data.x->menubar_widget->core.height
3815 + f->output_data.x->menubar_widget->core.border_width)
8fc2766b 3816 : 0);
a7f7d550 3817
f7008aff
RS
3818#if 0 /* Experimentally, we now get the right results
3819 for -geometry -0-0 without this. 24 Aug 96, rms. */
01cbdba5
RS
3820 if (FRAME_EXTERNAL_MENU_BAR (f))
3821 {
dd254b21 3822 Dimension ibw = 0;
01cbdba5
RS
3823 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
3824 menubar_size += ibw;
3825 }
f7008aff 3826#endif
01cbdba5 3827
7556890b 3828 f->output_data.x->menubar_height = menubar_size;
00983aba 3829
440b0bfd 3830#ifndef USE_LUCID
5031cc10
KH
3831 /* Motif seems to need this amount added to the sizes
3832 specified for the shell widget. The Athena/Lucid widgets don't.
3833 Both conclusions reached experimentally. -- rms. */
440b0bfd
RS
3834 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
3835 &extra_borders, NULL);
3836 extra_borders *= 2;
3837#endif
5031cc10 3838
97787173
RS
3839 /* Convert our geometry parameters into a geometry string
3840 and specify it.
3841 Note that we do not specify here whether the position
3842 is a user-specified or program-specified one.
3843 We pass that information later, in x_wm_set_size_hints. */
3844 {
7556890b 3845 int left = f->output_data.x->left_pos;
97787173 3846 int xneg = window_prompting & XNegative;
7556890b 3847 int top = f->output_data.x->top_pos;
97787173
RS
3848 int yneg = window_prompting & YNegative;
3849 if (xneg)
3850 left = -left;
3851 if (yneg)
3852 top = -top;
c760f47e
KH
3853
3854 if (window_prompting & USPosition)
5031cc10
KH
3855 sprintf (shell_position, "=%dx%d%c%d%c%d",
3856 PIXEL_WIDTH (f) + extra_borders,
3857 PIXEL_HEIGHT (f) + menubar_size + extra_borders,
c760f47e
KH
3858 (xneg ? '-' : '+'), left,
3859 (yneg ? '-' : '+'), top);
3860 else
5031cc10
KH
3861 sprintf (shell_position, "=%dx%d",
3862 PIXEL_WIDTH (f) + extra_borders,
3863 PIXEL_HEIGHT (f) + menubar_size + extra_borders);
97787173
RS
3864 }
3865
a7f7d550 3866 len = strlen (shell_position) + 1;
77110caa
RS
3867 /* We don't free this because we don't know whether
3868 it is safe to free it while the frame exists.
3869 It isn't worth the trouble of arranging to free it
3870 when the frame is deleted. */
a7f7d550
FP
3871 tem = (char *) xmalloc (len);
3872 strncpy (tem, shell_position, len);
3873 XtSetArg (al[ac], XtNgeometry, tem); ac++;
3874 XtSetValues (shell_widget, al, ac);
3875 }
3876
9ef48a9d
RS
3877 XtManageChild (pane_widget);
3878 XtRealizeWidget (shell_widget);
3879
6c32dd68 3880 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
9ef48a9d
RS
3881
3882 validate_x_resource_name ();
b7975ee4 3883
9ef48a9d 3884 class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
498e9ac3 3885 class_hints.res_class = (char *) XSTRING (Vx_resource_class)->data;
b9dc4443 3886 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
5a7df7d7
GM
3887
3888#ifdef HAVE_X_I18N
3889 FRAME_XIC (f) = NULL;
4bd777b8 3890#ifdef USE_XIM
5a7df7d7 3891 create_frame_xic (f);
4bd777b8 3892#endif
5a7df7d7 3893#endif
64d16748 3894
7556890b
RS
3895 f->output_data.x->wm_hints.input = True;
3896 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 3897 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3898 &f->output_data.x->wm_hints);
b8228beb 3899
c4ec904f 3900 hack_wm_protocols (f, shell_widget);
9ef48a9d 3901
6c32dd68
PR
3902#ifdef HACK_EDITRES
3903 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
3904#endif
3905
9ef48a9d 3906 /* Do a stupid property change to force the server to generate a
333b20bb 3907 PropertyNotify event so that the event_stream server timestamp will
9ef48a9d
RS
3908 be initialized to something relevant to the time we created the window.
3909 */
6c32dd68 3910 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
b9dc4443
RS
3911 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3912 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
3913 (unsigned char*) NULL, 0);
3914
5a7df7d7 3915 /* Make all the standard events reach the Emacs frame. */
31ac8d8c 3916 attributes.event_mask = STANDARD_EVENT_SET;
5a7df7d7
GM
3917
3918#ifdef HAVE_X_I18N
3919 if (FRAME_XIC (f))
3920 {
3921 /* XIM server might require some X events. */
3922 unsigned long fevent = NoEventMask;
3923 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3924 attributes.event_mask |= fevent;
3925 }
3926#endif /* HAVE_X_I18N */
3927
31ac8d8c
FP
3928 attribute_mask = CWEventMask;
3929 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
3930 attribute_mask, &attributes);
3931
6c32dd68 3932 XtMapWidget (frame_widget);
9ef48a9d 3933
8fc2766b
RS
3934 /* x_set_name normally ignores requests to set the name if the
3935 requested name is the same as the current name. This is the one
3936 place where that assumption isn't correct; f->name is set, but
3937 the X server hasn't been told. */
3938 {
3939 Lisp_Object name;
3940 int explicit = f->explicit_name;
3941
3942 f->explicit_name = 0;
3943 name = f->name;
3944 f->name = Qnil;
3945 x_set_name (f, name, explicit);
3946 }
3947
b9dc4443 3948 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3949 f->output_data.x->text_cursor);
8fc2766b
RS
3950
3951 UNBLOCK_INPUT;
3952
495fa05e
GM
3953 /* This is a no-op, except under Motif. Make sure main areas are
3954 set to something reasonable, in case we get an error later. */
3955 lw_set_main_areas (pane_widget, 0, frame_widget);
8fc2766b
RS
3956}
3957
9ef48a9d
RS
3958#else /* not USE_X_TOOLKIT */
3959
8fc2766b
RS
3960/* Create and set up the X window for frame F. */
3961
201d8c78 3962void
8fc2766b
RS
3963x_window (f)
3964 struct frame *f;
3965
3966{
3967 XClassHint class_hints;
3968 XSetWindowAttributes attributes;
3969 unsigned long attribute_mask;
3970
7556890b
RS
3971 attributes.background_pixel = f->output_data.x->background_pixel;
3972 attributes.border_pixel = f->output_data.x->border_pixel;
01f1ba30
JB
3973 attributes.bit_gravity = StaticGravity;
3974 attributes.backing_store = NotUseful;
3975 attributes.save_under = True;
3976 attributes.event_mask = STANDARD_EVENT_SET;
9b2956e2
GM
3977 attributes.colormap = FRAME_X_COLORMAP (f);
3978 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
3979 | CWColormap);
01f1ba30
JB
3980
3981 BLOCK_INPUT;
fe24a618 3982 FRAME_X_WINDOW (f)
b9dc4443 3983 = XCreateWindow (FRAME_X_DISPLAY (f),
7556890b
RS
3984 f->output_data.x->parent_desc,
3985 f->output_data.x->left_pos,
3986 f->output_data.x->top_pos,
f676886a 3987 PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
7556890b 3988 f->output_data.x->border_width,
01f1ba30
JB
3989 CopyFromParent, /* depth */
3990 InputOutput, /* class */
383d6ffc 3991 FRAME_X_VISUAL (f),
01f1ba30 3992 attribute_mask, &attributes);
5a7df7d7
GM
3993
3994#ifdef HAVE_X_I18N
4bd777b8 3995#ifdef USE_XIM
5a7df7d7
GM
3996 create_frame_xic (f);
3997 if (FRAME_XIC (f))
3998 {
3999 /* XIM server might require some X events. */
4000 unsigned long fevent = NoEventMask;
4001 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
4002 attributes.event_mask |= fevent;
4003 attribute_mask = CWEventMask;
4004 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4005 attribute_mask, &attributes);
4006 }
4bd777b8 4007#endif
5a7df7d7
GM
4008#endif /* HAVE_X_I18N */
4009
d387c960 4010 validate_x_resource_name ();
b7975ee4 4011
d387c960 4012 class_hints.res_name = (char *) XSTRING (Vx_resource_name)->data;
498e9ac3 4013 class_hints.res_class = (char *) XSTRING (Vx_resource_class)->data;
b9dc4443 4014 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
01f1ba30 4015
00983aba
KH
4016 /* The menubar is part of the ordinary display;
4017 it does not count in addition to the height of the window. */
7556890b 4018 f->output_data.x->menubar_height = 0;
00983aba 4019
179956b9
JB
4020 /* This indicates that we use the "Passive Input" input model.
4021 Unless we do this, we don't get the Focus{In,Out} events that we
4022 need to draw the cursor correctly. Accursed bureaucrats.
b9dc4443 4023 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
179956b9 4024
7556890b
RS
4025 f->output_data.x->wm_hints.input = True;
4026 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 4027 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 4028 &f->output_data.x->wm_hints);
6d078211 4029 f->output_data.x->wm_hints.icon_pixmap = None;
179956b9 4030
032e4ebe
RS
4031 /* Request "save yourself" and "delete window" commands from wm. */
4032 {
4033 Atom protocols[2];
b9dc4443
RS
4034 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
4035 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
4036 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
032e4ebe 4037 }
9ef48a9d 4038
e373f201
JB
4039 /* x_set_name normally ignores requests to set the name if the
4040 requested name is the same as the current name. This is the one
4041 place where that assumption isn't correct; f->name is set, but
4042 the X server hasn't been told. */
4043 {
98381190 4044 Lisp_Object name;
cf177271 4045 int explicit = f->explicit_name;
e373f201 4046
cf177271 4047 f->explicit_name = 0;
98381190
KH
4048 name = f->name;
4049 f->name = Qnil;
cf177271 4050 x_set_name (f, name, explicit);
e373f201
JB
4051 }
4052
b9dc4443 4053 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 4054 f->output_data.x->text_cursor);
9ef48a9d 4055
01f1ba30
JB
4056 UNBLOCK_INPUT;
4057
fe24a618 4058 if (FRAME_X_WINDOW (f) == 0)
9ef48a9d 4059 error ("Unable to create window");
01f1ba30
JB
4060}
4061
8fc2766b
RS
4062#endif /* not USE_X_TOOLKIT */
4063
01f1ba30
JB
4064/* Handle the icon stuff for this window. Perhaps later we might
4065 want an x_set_icon_position which can be called interactively as
b9dc4443 4066 well. */
01f1ba30
JB
4067
4068static void
f676886a
JB
4069x_icon (f, parms)
4070 struct frame *f;
01f1ba30
JB
4071 Lisp_Object parms;
4072{
f9942c9e 4073 Lisp_Object icon_x, icon_y;
abb4b7ec 4074 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
01f1ba30
JB
4075
4076 /* Set the position of the icon. Note that twm groups all
b9dc4443 4077 icons in an icon window. */
333b20bb
GM
4078 icon_x = x_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4079 icon_y = x_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
f9942c9e 4080 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
01f1ba30 4081 {
b7826503
PJ
4082 CHECK_NUMBER (icon_x);
4083 CHECK_NUMBER (icon_y);
01f1ba30 4084 }
f9942c9e 4085 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
01f1ba30 4086 error ("Both left and top icon corners of icon must be specified");
01f1ba30 4087
f9942c9e
JB
4088 BLOCK_INPUT;
4089
fe24a618
JB
4090 if (! EQ (icon_x, Qunbound))
4091 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
f9942c9e 4092
01f1ba30 4093 /* Start up iconic or window? */
49795535 4094 x_wm_set_window_state
333b20bb
GM
4095 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
4096 Qicon)
49795535
JB
4097 ? IconicState
4098 : NormalState));
01f1ba30 4099
f468da95
RS
4100 x_text_icon (f, (char *) XSTRING ((!NILP (f->icon_name)
4101 ? f->icon_name
4102 : f->name))->data);
80534dd6 4103
01f1ba30
JB
4104 UNBLOCK_INPUT;
4105}
4106
b243755a 4107/* Make the GCs needed for this window, setting the
01f1ba30
JB
4108 background, border and mouse colors; also create the
4109 mouse cursor and the gray border tile. */
4110
f945b920
JB
4111static char cursor_bits[] =
4112 {
4113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4115 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4116 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4117 };
4118
01f1ba30 4119static void
f676886a
JB
4120x_make_gc (f)
4121 struct frame *f;
01f1ba30
JB
4122{
4123 XGCValues gc_values;
01f1ba30 4124
6afb1d07
JB
4125 BLOCK_INPUT;
4126
b243755a 4127 /* Create the GCs of this frame.
9ef48a9d 4128 Note that many default values are used. */
01f1ba30
JB
4129
4130 /* Normal video */
7556890b
RS
4131 gc_values.font = f->output_data.x->font->fid;
4132 gc_values.foreground = f->output_data.x->foreground_pixel;
4133 gc_values.background = f->output_data.x->background_pixel;
9ef48a9d 4134 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
959e647d
GM
4135 f->output_data.x->normal_gc
4136 = XCreateGC (FRAME_X_DISPLAY (f),
4137 FRAME_X_WINDOW (f),
4138 GCLineWidth | GCFont | GCForeground | GCBackground,
4139 &gc_values);
01f1ba30 4140
b9dc4443 4141 /* Reverse video style. */
7556890b
RS
4142 gc_values.foreground = f->output_data.x->background_pixel;
4143 gc_values.background = f->output_data.x->foreground_pixel;
959e647d
GM
4144 f->output_data.x->reverse_gc
4145 = XCreateGC (FRAME_X_DISPLAY (f),
4146 FRAME_X_WINDOW (f),
4147 GCFont | GCForeground | GCBackground | GCLineWidth,
4148 &gc_values);
01f1ba30 4149
9ef48a9d 4150 /* Cursor has cursor-color background, background-color foreground. */
7556890b
RS
4151 gc_values.foreground = f->output_data.x->background_pixel;
4152 gc_values.background = f->output_data.x->cursor_pixel;
01f1ba30
JB
4153 gc_values.fill_style = FillOpaqueStippled;
4154 gc_values.stipple
b9dc4443
RS
4155 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
4156 FRAME_X_DISPLAY_INFO (f)->root_window,
01f1ba30 4157 cursor_bits, 16, 16);
7556890b 4158 f->output_data.x->cursor_gc
b9dc4443 4159 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
01f1ba30 4160 (GCFont | GCForeground | GCBackground
ac1f48a4 4161 | GCFillStyle /* | GCStipple */ | GCLineWidth),
01f1ba30
JB
4162 &gc_values);
4163
333b20bb
GM
4164 /* Reliefs. */
4165 f->output_data.x->white_relief.gc = 0;
4166 f->output_data.x->black_relief.gc = 0;
4167
01f1ba30 4168 /* Create the gray border tile used when the pointer is not in
f676886a 4169 the frame. Since this depends on the frame's pixel values,
9ef48a9d 4170 this must be done on a per-frame basis. */
7556890b 4171 f->output_data.x->border_tile
d043f1a4 4172 = (XCreatePixmapFromBitmapData
b9dc4443 4173 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
d043f1a4 4174 gray_bits, gray_width, gray_height,
7556890b
RS
4175 f->output_data.x->foreground_pixel,
4176 f->output_data.x->background_pixel,
ab452f99 4177 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
6afb1d07
JB
4178
4179 UNBLOCK_INPUT;
01f1ba30 4180}
01f1ba30 4181
959e647d
GM
4182
4183/* Free what was was allocated in x_make_gc. */
4184
4185void
4186x_free_gcs (f)
4187 struct frame *f;
4188{
4189 Display *dpy = FRAME_X_DISPLAY (f);
4190
4191 BLOCK_INPUT;
4192
4193 if (f->output_data.x->normal_gc)
4194 {
4195 XFreeGC (dpy, f->output_data.x->normal_gc);
4196 f->output_data.x->normal_gc = 0;
4197 }
4198
4199 if (f->output_data.x->reverse_gc)
4200 {
4201 XFreeGC (dpy, f->output_data.x->reverse_gc);
4202 f->output_data.x->reverse_gc = 0;
4203 }
4204
4205 if (f->output_data.x->cursor_gc)
4206 {
4207 XFreeGC (dpy, f->output_data.x->cursor_gc);
4208 f->output_data.x->cursor_gc = 0;
4209 }
4210
4211 if (f->output_data.x->border_tile)
4212 {
4213 XFreePixmap (dpy, f->output_data.x->border_tile);
4214 f->output_data.x->border_tile = 0;
4215 }
4216
4217 UNBLOCK_INPUT;
4218}
4219
4220
eaf1eea9
GM
4221/* Handler for signals raised during x_create_frame and
4222 x_create_top_frame. FRAME is the frame which is partially
4223 constructed. */
4224
4225static Lisp_Object
4226unwind_create_frame (frame)
4227 Lisp_Object frame;
4228{
4229 struct frame *f = XFRAME (frame);
4230
4231 /* If frame is ``official'', nothing to do. */
4232 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4233 {
f1d2ce7f 4234#if GLYPH_DEBUG
eaf1eea9
GM
4235 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4236#endif
4237
4238 x_free_frame_resources (f);
4239
4240 /* Check that reference counts are indeed correct. */
4241 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4242 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a 4243 return Qt;
eaf1eea9
GM
4244 }
4245
4246 return Qnil;
4247}
4248
4249
f676886a 4250DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
01f1ba30 4251 1, 1, 0,
7ee72033 4252 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
c061c855
GM
4253Returns an Emacs frame object.
4254ALIST is an alist of frame parameters.
4255If the parameters specify that the frame should not have a minibuffer,
4256and do not specify a specific minibuffer window to use,
4257then `default-minibuffer-frame' must be a frame whose minibuffer can
4258be shared by the new frame.
4259
7ee72033
MB
4260This function is an internal primitive--use `make-frame' instead. */)
4261 (parms)
01f1ba30
JB
4262 Lisp_Object parms;
4263{
f676886a 4264 struct frame *f;
2365c027 4265 Lisp_Object frame, tem;
01f1ba30
JB
4266 Lisp_Object name;
4267 int minibuffer_only = 0;
4268 long window_prompting = 0;
4269 int width, height;
eaf1eea9 4270 int count = BINDING_STACK_SIZE ();
ecaca587 4271 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
b9dc4443 4272 Lisp_Object display;
333b20bb 4273 struct x_display_info *dpyinfo = NULL;
a59e4f3d 4274 Lisp_Object parent;
e557f19d 4275 struct kboard *kb;
01f1ba30 4276
11ae94fe 4277 check_x ();
01f1ba30 4278
b7975ee4
KH
4279 /* Use this general default value to start with
4280 until we know if this frame has a specified name. */
4281 Vx_resource_name = Vinvocation_name;
4282
333b20bb 4283 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
b9dc4443
RS
4284 if (EQ (display, Qunbound))
4285 display = Qnil;
4286 dpyinfo = check_x_display_info (display);
e557f19d
KH
4287#ifdef MULTI_KBOARD
4288 kb = dpyinfo->kboard;
4289#else
4290 kb = &the_only_kboard;
4291#endif
b9dc4443 4292
333b20bb 4293 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
6a5e54e2 4294 if (!STRINGP (name)
cf177271
JB
4295 && ! EQ (name, Qunbound)
4296 && ! NILP (name))
08a90d6a 4297 error ("Invalid frame name--not a string or nil");
01f1ba30 4298
b7975ee4
KH
4299 if (STRINGP (name))
4300 Vx_resource_name = name;
4301
a59e4f3d 4302 /* See if parent window is specified. */
333b20bb 4303 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
a59e4f3d
RS
4304 if (EQ (parent, Qunbound))
4305 parent = Qnil;
4306 if (! NILP (parent))
b7826503 4307 CHECK_NUMBER (parent);
a59e4f3d 4308
ecaca587
RS
4309 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4310 /* No need to protect DISPLAY because that's not used after passing
4311 it to make_frame_without_minibuffer. */
4312 frame = Qnil;
4313 GCPRO4 (parms, parent, name, frame);
333b20bb
GM
4314 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
4315 RES_TYPE_SYMBOL);
f9942c9e 4316 if (EQ (tem, Qnone) || NILP (tem))
2526c290 4317 f = make_frame_without_minibuffer (Qnil, kb, display);
f9942c9e 4318 else if (EQ (tem, Qonly))
01f1ba30 4319 {
f676886a 4320 f = make_minibuffer_frame ();
01f1ba30
JB
4321 minibuffer_only = 1;
4322 }
6a5e54e2 4323 else if (WINDOWP (tem))
2526c290 4324 f = make_frame_without_minibuffer (tem, kb, display);
f9942c9e
JB
4325 else
4326 f = make_frame (1);
01f1ba30 4327
ecaca587
RS
4328 XSETFRAME (frame, f);
4329
a3c87d4e
JB
4330 /* Note that X Windows does support scroll bars. */
4331 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
179956b9 4332
08a90d6a 4333 f->output_method = output_x_window;
7556890b
RS
4334 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4335 bzero (f->output_data.x, sizeof (struct x_output));
4336 f->output_data.x->icon_bitmap = -1;
0ecca023 4337 f->output_data.x->fontset = -1;
333b20bb
GM
4338 f->output_data.x->scroll_bar_foreground_pixel = -1;
4339 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
4340#ifdef USE_TOOLKIT_SCROLL_BARS
4341 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4342 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4343#endif /* USE_TOOLKIT_SCROLL_BARS */
eaf1eea9 4344 record_unwind_protect (unwind_create_frame, frame);
08a90d6a 4345
f468da95 4346 f->icon_name
333b20bb
GM
4347 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
4348 RES_TYPE_STRING);
f468da95
RS
4349 if (! STRINGP (f->icon_name))
4350 f->icon_name = Qnil;
80534dd6 4351
08a90d6a 4352 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 4353#if GLYPH_DEBUG
eaf1eea9
GM
4354 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
4355 dpyinfo_refcount = dpyinfo->reference_count;
4356#endif /* GLYPH_DEBUG */
73410c76 4357#ifdef MULTI_KBOARD
e557f19d 4358 FRAME_KBOARD (f) = kb;
73410c76 4359#endif
08a90d6a 4360
9b2956e2
GM
4361 /* These colors will be set anyway later, but it's important
4362 to get the color reference counts right, so initialize them! */
4363 {
4364 Lisp_Object black;
4365 struct gcpro gcpro1;
cefecbcf
GM
4366
4367 /* Function x_decode_color can signal an error. Make
4368 sure to initialize color slots so that we won't try
4369 to free colors we haven't allocated. */
4370 f->output_data.x->foreground_pixel = -1;
4371 f->output_data.x->background_pixel = -1;
4372 f->output_data.x->cursor_pixel = -1;
4373 f->output_data.x->cursor_foreground_pixel = -1;
4374 f->output_data.x->border_pixel = -1;
4375 f->output_data.x->mouse_pixel = -1;
9b2956e2
GM
4376
4377 black = build_string ("black");
4378 GCPRO1 (black);
4379 f->output_data.x->foreground_pixel
4380 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4381 f->output_data.x->background_pixel
4382 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4383 f->output_data.x->cursor_pixel
4384 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4385 f->output_data.x->cursor_foreground_pixel
4386 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4387 f->output_data.x->border_pixel
4388 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4389 f->output_data.x->mouse_pixel
4390 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4391 UNGCPRO;
4392 }
4393
a59e4f3d
RS
4394 /* Specify the parent under which to make this X window. */
4395
4396 if (!NILP (parent))
4397 {
8c239ac3 4398 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
7556890b 4399 f->output_data.x->explicit_parent = 1;
a59e4f3d
RS
4400 }
4401 else
4402 {
7556890b
RS
4403 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4404 f->output_data.x->explicit_parent = 0;
a59e4f3d
RS
4405 }
4406
cf177271
JB
4407 /* Set the name; the functions to which we pass f expect the name to
4408 be set. */
4409 if (EQ (name, Qunbound) || NILP (name))
4410 {
08a90d6a 4411 f->name = build_string (dpyinfo->x_id_name);
cf177271
JB
4412 f->explicit_name = 0;
4413 }
4414 else
4415 {
4416 f->name = name;
4417 f->explicit_name = 1;
9ef48a9d
RS
4418 /* use the frame's title when getting resources for this frame. */
4419 specbind (Qx_resource_name, name);
cf177271 4420 }
01f1ba30 4421
01f1ba30
JB
4422 /* Extract the window parameters from the supplied values
4423 that are needed to determine window geometry. */
d387c960
JB
4424 {
4425 Lisp_Object font;
4426
333b20bb 4427 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2ee3abaa 4428
6817eab4 4429 BLOCK_INPUT;
e5e548e3
RS
4430 /* First, try whatever font the caller has specified. */
4431 if (STRINGP (font))
5f652afc 4432 font = x_new_fontset (f, XSTRING (font)->data);
333b20bb 4433
e5e548e3 4434 /* Try out a font which we hope has bold and italic variations. */
333b20bb 4435 if (!STRINGP (font))
5f652afc 4436 font = x_new_fontset (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
e5e548e3 4437 if (!STRINGP (font))
5f652afc 4438 font = x_new_fontset (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
e5e548e3 4439 if (! STRINGP (font))
5f652afc 4440 font = x_new_fontset (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
e5e548e3
RS
4441 if (! STRINGP (font))
4442 /* This was formerly the first thing tried, but it finds too many fonts
4443 and takes too long. */
5f652afc 4444 font = x_new_fontset (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
e5e548e3
RS
4445 /* If those didn't work, look for something which will at least work. */
4446 if (! STRINGP (font))
5f652afc 4447 font = x_new_fontset (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
6817eab4
JB
4448 UNBLOCK_INPUT;
4449 if (! STRINGP (font))
e5e548e3
RS
4450 font = build_string ("fixed");
4451
477f8642 4452 x_default_parameter (f, parms, Qfont, font,
333b20bb 4453 "font", "Font", RES_TYPE_STRING);
d387c960 4454 }
9ef48a9d 4455
e3881aa0 4456#ifdef USE_LUCID
82c90203
RS
4457 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
4458 whereby it fails to get any font. */
7556890b 4459 xlwmenu_default_font = f->output_data.x->font;
dd254b21 4460#endif
82c90203 4461
cf177271 4462 x_default_parameter (f, parms, Qborder_width, make_number (2),
333b20bb
GM
4463 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4464
4e397688 4465 /* This defaults to 1 in order to match xterm. We recognize either
ddf768c3
JB
4466 internalBorderWidth or internalBorder (which is what xterm calls
4467 it). */
4468 if (NILP (Fassq (Qinternal_border_width, parms)))
4469 {
4470 Lisp_Object value;
4471
abb4b7ec 4472 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
333b20bb 4473 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
ddf768c3
JB
4474 if (! EQ (value, Qunbound))
4475 parms = Fcons (Fcons (Qinternal_border_width, value),
4476 parms);
4477 }
dca97592 4478 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
333b20bb
GM
4479 "internalBorderWidth", "internalBorderWidth",
4480 RES_TYPE_NUMBER);
1ab3d87e 4481 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
333b20bb
GM
4482 "verticalScrollBars", "ScrollBars",
4483 RES_TYPE_SYMBOL);
01f1ba30 4484
b9dc4443 4485 /* Also do the stuff which must be set before the window exists. */
cf177271 4486 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
333b20bb 4487 "foreground", "Foreground", RES_TYPE_STRING);
cf177271 4488 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
0b60fc91 4489 "background", "Background", RES_TYPE_STRING);
cf177271 4490 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
333b20bb 4491 "pointerColor", "Foreground", RES_TYPE_STRING);
cf177271 4492 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
333b20bb 4493 "cursorColor", "Foreground", RES_TYPE_STRING);
cf177271 4494 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
333b20bb 4495 "borderColor", "BorderColor", RES_TYPE_STRING);
d62c8769
GM
4496 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
4497 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
563b67aa
GM
4498 x_default_parameter (f, parms, Qline_spacing, Qnil,
4499 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
b3ba0aa8
KS
4500 x_default_parameter (f, parms, Qleft_fringe, Qnil,
4501 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4502 x_default_parameter (f, parms, Qright_fringe, Qnil,
4503 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
333b20bb
GM
4504
4505 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
4506 "scrollBarForeground",
4507 "ScrollBarForeground", 1);
4508 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
4509 "scrollBarBackground",
4510 "ScrollBarBackground", 0);
4511
4512 /* Init faces before x_default_parameter is called for scroll-bar
4513 parameters because that function calls x_set_scroll_bar_width,
4514 which calls change_frame_size, which calls Fset_window_buffer,
4515 which runs hooks, which call Fvertical_motion. At the end, we
4516 end up in init_iterator with a null face cache, which should not
4517 happen. */
4518 init_frame_faces (f);
4519
c7bcb20d 4520 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
333b20bb 4521 "menuBar", "MenuBar", RES_TYPE_NUMBER);
e33455ca 4522 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
333b20bb 4523 "toolBar", "ToolBar", RES_TYPE_NUMBER);
79873d50 4524 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
333b20bb
GM
4525 "bufferPredicate", "BufferPredicate",
4526 RES_TYPE_SYMBOL);
c2304e02 4527 x_default_parameter (f, parms, Qtitle, Qnil,
333b20bb 4528 "title", "Title", RES_TYPE_STRING);
ea0a1f53
GM
4529 x_default_parameter (f, parms, Qwait_for_wm, Qt,
4530 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
49d41073
EZ
4531 x_default_parameter (f, parms, Qfullscreen, Qnil,
4532 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
90eb1019 4533
7556890b 4534 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
35f59f6b
GM
4535
4536 /* Add the tool-bar height to the initial frame height so that the
4537 user gets a text display area of the size he specified with -g or
4538 via .Xdefaults. Later changes of the tool-bar height don't
4539 change the frame size. This is done so that users can create
4540 tall Emacs frames without having to guess how tall the tool-bar
4541 will get. */
4542 if (FRAME_TOOL_BAR_LINES (f))
4543 {
4544 int margin, relief, bar_height;
4545
8ed86491 4546 relief = (tool_bar_button_relief >= 0
35f59f6b
GM
4547 ? tool_bar_button_relief
4548 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
4549
4550 if (INTEGERP (Vtool_bar_button_margin)
4551 && XINT (Vtool_bar_button_margin) > 0)
4552 margin = XFASTINT (Vtool_bar_button_margin);
4553 else if (CONSP (Vtool_bar_button_margin)
4554 && INTEGERP (XCDR (Vtool_bar_button_margin))
4555 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4556 margin = XFASTINT (XCDR (Vtool_bar_button_margin));
4557 else
4558 margin = 0;
4559
4560 bar_height = DEFAULT_TOOL_BAR_IMAGE_HEIGHT + 2 * margin + 2 * relief;
4561 f->height += (bar_height + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
4562 }
4563
4564 /* Compute the size of the X window. */
f676886a 4565 window_prompting = x_figure_window_size (f, parms);
01f1ba30 4566
f83f10ba 4567 if (window_prompting & XNegative)
2365c027 4568 {
f83f10ba 4569 if (window_prompting & YNegative)
7556890b 4570 f->output_data.x->win_gravity = SouthEastGravity;
f83f10ba 4571 else
7556890b 4572 f->output_data.x->win_gravity = NorthEastGravity;
f83f10ba
RS
4573 }
4574 else
4575 {
4576 if (window_prompting & YNegative)
7556890b 4577 f->output_data.x->win_gravity = SouthWestGravity;
f83f10ba 4578 else
7556890b 4579 f->output_data.x->win_gravity = NorthWestGravity;
2365c027
RS
4580 }
4581
7556890b 4582 f->output_data.x->size_hint_flags = window_prompting;
38d22040 4583
495fa05e
GM
4584 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4585 f->no_split = minibuffer_only || EQ (tem, Qt);
4586
6a1bcd01 4587 /* Create the X widget or window. */
a7f7d550
FP
4588#ifdef USE_X_TOOLKIT
4589 x_window (f, window_prompting, minibuffer_only);
4590#else
f676886a 4591 x_window (f);
a7f7d550 4592#endif
495fa05e 4593
f676886a
JB
4594 x_icon (f, parms);
4595 x_make_gc (f);
01f1ba30 4596
495fa05e
GM
4597 /* Now consider the frame official. */
4598 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4599 Vframe_list = Fcons (frame, Vframe_list);
4600
f9942c9e
JB
4601 /* We need to do this after creating the X window, so that the
4602 icon-creation functions can say whose icon they're describing. */
cf177271 4603 x_default_parameter (f, parms, Qicon_type, Qnil,
333b20bb 4604 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
f9942c9e 4605
cf177271 4606 x_default_parameter (f, parms, Qauto_raise, Qnil,
333b20bb 4607 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
cf177271 4608 x_default_parameter (f, parms, Qauto_lower, Qnil,
333b20bb 4609 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
dbc4e1c1 4610 x_default_parameter (f, parms, Qcursor_type, Qbox,
333b20bb 4611 "cursorType", "CursorType", RES_TYPE_SYMBOL);
28d7281d
GM
4612 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
4613 "scrollBarWidth", "ScrollBarWidth",
4614 RES_TYPE_NUMBER);
f9942c9e 4615
f676886a 4616 /* Dimensions, especially f->height, must be done via change_frame_size.
01f1ba30 4617 Change will not be effected unless different from the current
b9dc4443 4618 f->height. */
f676886a
JB
4619 width = f->width;
4620 height = f->height;
6a1bcd01 4621
1ab3d87e
RS
4622 f->height = 0;
4623 SET_FRAME_WIDTH (f, 0);
8938a4fb 4624 change_frame_size (f, height, width, 1, 0, 0);
d043f1a4 4625
4a967a9b
GM
4626 /* Set up faces after all frame parameters are known. This call
4627 also merges in face attributes specified for new frames. If we
4628 don't do this, the `menu' face for instance won't have the right
4629 colors, and the menu bar won't appear in the specified colors for
4630 new frames. */
4631 call1 (Qface_set_after_frame_default, frame);
4632
495fa05e
GM
4633#ifdef USE_X_TOOLKIT
4634 /* Create the menu bar. */
4635 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4636 {
4637 /* If this signals an error, we haven't set size hints for the
4638 frame and we didn't make it visible. */
4639 initialize_frame_menubar (f);
4640
4641 /* This is a no-op, except under Motif where it arranges the
4642 main window for the widgets on it. */
4643 lw_set_main_areas (f->output_data.x->column_widget,
4644 f->output_data.x->menubar_widget,
4645 f->output_data.x->edit_widget);
4646 }
4647#endif /* USE_X_TOOLKIT */
4648
4649 /* Tell the server what size and position, etc, we want, and how
4650 badly we want them. This should be done after we have the menu
4651 bar so that its size can be taken into account. */
01f1ba30 4652 BLOCK_INPUT;
7989f084 4653 x_wm_set_size_hint (f, window_prompting, 0);
01f1ba30
JB
4654 UNBLOCK_INPUT;
4655
495fa05e
GM
4656 /* Make the window appear on the frame and enable display, unless
4657 the caller says not to. However, with explicit parent, Emacs
4658 cannot control visibility, so don't try. */
7556890b 4659 if (! f->output_data.x->explicit_parent)
a59e4f3d
RS
4660 {
4661 Lisp_Object visibility;
49795535 4662
333b20bb
GM
4663 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
4664 RES_TYPE_SYMBOL);
a59e4f3d
RS
4665 if (EQ (visibility, Qunbound))
4666 visibility = Qt;
49795535 4667
a59e4f3d
RS
4668 if (EQ (visibility, Qicon))
4669 x_iconify_frame (f);
4670 else if (! NILP (visibility))
4671 x_make_frame_visible (f);
4672 else
4673 /* Must have been Qnil. */
4674 ;
4675 }
01f1ba30 4676
495fa05e 4677 UNGCPRO;
9e57df62
GM
4678
4679 /* Make sure windows on this frame appear in calls to next-window
4680 and similar functions. */
4681 Vwindow_list = Qnil;
4682
9ef48a9d 4683 return unbind_to (count, frame);
01f1ba30
JB
4684}
4685
eaf1eea9 4686
0d17d282
KH
4687/* FRAME is used only to get a handle on the X display. We don't pass the
4688 display info directly because we're called from frame.c, which doesn't
4689 know about that structure. */
e4f79258 4690
87498171 4691Lisp_Object
0d17d282
KH
4692x_get_focus_frame (frame)
4693 struct frame *frame;
87498171 4694{
0d17d282 4695 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
87498171 4696 Lisp_Object xfocus;
0d17d282 4697 if (! dpyinfo->x_focus_frame)
87498171
KH
4698 return Qnil;
4699
0d17d282 4700 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
87498171
KH
4701 return xfocus;
4702}
f0614854 4703
3decc1e7
GM
4704
4705/* In certain situations, when the window manager follows a
4706 click-to-focus policy, there seems to be no way around calling
4707 XSetInputFocus to give another frame the input focus .
4708
4709 In an ideal world, XSetInputFocus should generally be avoided so
4710 that applications don't interfere with the window manager's focus
4711 policy. But I think it's okay to use when it's clearly done
4712 following a user-command. */
4713
4714DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
7ee72033
MB
4715 doc: /* Set the input focus to FRAME.
4716FRAME nil means use the selected frame. */)
4717 (frame)
3decc1e7
GM
4718 Lisp_Object frame;
4719{
4720 struct frame *f = check_x_frame (frame);
4721 Display *dpy = FRAME_X_DISPLAY (f);
4722 int count;
4723
4724 BLOCK_INPUT;
4725 count = x_catch_errors (dpy);
4726 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4727 RevertToParent, CurrentTime);
4728 x_uncatch_errors (dpy, count);
4729 UNBLOCK_INPUT;
4730
4731 return Qnil;
4732}
4733
f0614854 4734\f
2d764c78 4735DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
7ee72033
MB
4736 doc: /* Internal function called by `color-defined-p', which see. */)
4737 (color, frame)
b9dc4443 4738 Lisp_Object color, frame;
e12d55b2 4739{
b9dc4443
RS
4740 XColor foo;
4741 FRAME_PTR f = check_x_frame (frame);
e12d55b2 4742
b7826503 4743 CHECK_STRING (color);
b9dc4443 4744
2d764c78 4745 if (x_defined_color (f, XSTRING (color)->data, &foo, 0))
e12d55b2
RS
4746 return Qt;
4747 else
4748 return Qnil;
4749}
4750
2d764c78 4751DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
7ee72033
MB
4752 doc: /* Internal function called by `color-values', which see. */)
4753 (color, frame)
b9dc4443 4754 Lisp_Object color, frame;
01f1ba30 4755{
b9dc4443
RS
4756 XColor foo;
4757 FRAME_PTR f = check_x_frame (frame);
4758
b7826503 4759 CHECK_STRING (color);
01f1ba30 4760
2d764c78 4761 if (x_defined_color (f, XSTRING (color)->data, &foo, 0))
57c82a63
RS
4762 {
4763 Lisp_Object rgb[3];
4764
4765 rgb[0] = make_number (foo.red);
4766 rgb[1] = make_number (foo.green);
4767 rgb[2] = make_number (foo.blue);
4768 return Flist (3, rgb);
4769 }
01f1ba30
JB
4770 else
4771 return Qnil;
4772}
4773
2d764c78 4774DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
7ee72033
MB
4775 doc: /* Internal function called by `display-color-p', which see. */)
4776 (display)
08a90d6a 4777 Lisp_Object display;
01f1ba30 4778{
08a90d6a 4779 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 4780
b9dc4443 4781 if (dpyinfo->n_planes <= 2)
01f1ba30
JB
4782 return Qnil;
4783
b9dc4443 4784 switch (dpyinfo->visual->class)
01f1ba30
JB
4785 {
4786 case StaticColor:
4787 case PseudoColor:
4788 case TrueColor:
4789 case DirectColor:
4790 return Qt;
4791
4792 default:
4793 return Qnil;
4794 }
4795}
4796
d0c9d219 4797DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
c061c855 4798 0, 1, 0,
7ee72033 4799 doc: /* Return t if the X display supports shades of gray.
c061c855
GM
4800Note that color displays do support shades of gray.
4801The optional argument DISPLAY specifies which display to ask about.
4802DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4803If omitted or nil, that stands for the selected frame's display. */)
4804 (display)
08a90d6a 4805 Lisp_Object display;
d0c9d219 4806{
08a90d6a 4807 struct x_display_info *dpyinfo = check_x_display_info (display);
d0c9d219 4808
ae6b58f9 4809 if (dpyinfo->n_planes <= 1)
b9dc4443
RS
4810 return Qnil;
4811
ae6b58f9
RS
4812 switch (dpyinfo->visual->class)
4813 {
4814 case StaticColor:
4815 case PseudoColor:
4816 case TrueColor:
4817 case DirectColor:
4818 case StaticGray:
4819 case GrayScale:
4820 return Qt;
4821
4822 default:
4823 return Qnil;
4824 }
d0c9d219
RS
4825}
4826
41beb8fc 4827DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
c061c855 4828 0, 1, 0,
7ee72033 4829 doc: /* Returns the width in pixels of the X display DISPLAY.
c061c855
GM
4830The optional argument DISPLAY specifies which display to ask about.
4831DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4832If omitted or nil, that stands for the selected frame's display. */)
4833 (display)
08a90d6a 4834 Lisp_Object display;
41beb8fc 4835{
08a90d6a 4836 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4837
4838 return make_number (dpyinfo->width);
41beb8fc
RS
4839}
4840
4841DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
c061c855 4842 Sx_display_pixel_height, 0, 1, 0,
7ee72033 4843 doc: /* Returns the height in pixels of the X display DISPLAY.
c061c855
GM
4844The optional argument DISPLAY specifies which display to ask about.
4845DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4846If omitted or nil, that stands for the selected frame's display. */)
4847 (display)
08a90d6a 4848 Lisp_Object display;
41beb8fc 4849{
08a90d6a 4850 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4851
4852 return make_number (dpyinfo->height);
41beb8fc
RS
4853}
4854
4855DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
c061c855 4856 0, 1, 0,
7ee72033 4857 doc: /* Returns the number of bitplanes of the X display DISPLAY.
c061c855
GM
4858The optional argument DISPLAY specifies which display to ask about.
4859DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4860If omitted or nil, that stands for the selected frame's display. */)
4861 (display)
08a90d6a 4862 Lisp_Object display;
41beb8fc 4863{
08a90d6a 4864 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4865
4866 return make_number (dpyinfo->n_planes);
41beb8fc
RS
4867}
4868
4869DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
c061c855 4870 0, 1, 0,
7ee72033 4871 doc: /* Returns the number of color cells of the X display DISPLAY.
c061c855
GM
4872The optional argument DISPLAY specifies which display to ask about.
4873DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4874If omitted or nil, that stands for the selected frame's display. */)
4875 (display)
08a90d6a 4876 Lisp_Object display;
41beb8fc 4877{
08a90d6a 4878 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4879
4880 return make_number (DisplayCells (dpyinfo->display,
4881 XScreenNumberOfScreen (dpyinfo->screen)));
41beb8fc
RS
4882}
4883
9d317b2c
RS
4884DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4885 Sx_server_max_request_size,
c061c855 4886 0, 1, 0,
7ee72033 4887 doc: /* Returns the maximum request size of the X server of display DISPLAY.
c061c855
GM
4888The optional argument DISPLAY specifies which display to ask about.
4889DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4890If omitted or nil, that stands for the selected frame's display. */)
4891 (display)
08a90d6a 4892 Lisp_Object display;
9d317b2c 4893{
08a90d6a 4894 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4895
4896 return make_number (MAXREQUEST (dpyinfo->display));
9d317b2c
RS
4897}
4898
41beb8fc 4899DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
7ee72033 4900 doc: /* Returns the vendor ID string of the X server of display DISPLAY.
c061c855
GM
4901The optional argument DISPLAY specifies which display to ask about.
4902DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4903If omitted or nil, that stands for the selected frame's display. */)
4904 (display)
08a90d6a 4905 Lisp_Object display;
41beb8fc 4906{
08a90d6a 4907 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4908 char *vendor = ServerVendor (dpyinfo->display);
4909
41beb8fc
RS
4910 if (! vendor) vendor = "";
4911 return build_string (vendor);
4912}
4913
4914DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
7ee72033 4915 doc: /* Returns the version numbers of the X server of display DISPLAY.
c061c855
GM
4916The value is a list of three integers: the major and minor
4917version numbers of the X Protocol in use, and the vendor-specific release
4918number. See also the function `x-server-vendor'.
4919
4920The optional argument DISPLAY specifies which display to ask about.
4921DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4922If omitted or nil, that stands for the selected frame's display. */)
4923 (display)
08a90d6a 4924 Lisp_Object display;
41beb8fc 4925{
08a90d6a 4926 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443 4927 Display *dpy = dpyinfo->display;
11ae94fe 4928
41beb8fc
RS
4929 return Fcons (make_number (ProtocolVersion (dpy)),
4930 Fcons (make_number (ProtocolRevision (dpy)),
4931 Fcons (make_number (VendorRelease (dpy)), Qnil)));
4932}
4933
4934DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
7ee72033 4935 doc: /* Return the number of screens on the X server of display DISPLAY.
c061c855
GM
4936The optional argument DISPLAY specifies which display to ask about.
4937DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4938If omitted or nil, that stands for the selected frame's display. */)
4939 (display)
08a90d6a 4940 Lisp_Object display;
41beb8fc 4941{
08a90d6a 4942 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4943
4944 return make_number (ScreenCount (dpyinfo->display));
41beb8fc
RS
4945}
4946
4947DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
7ee72033 4948 doc: /* Return the height in millimeters of the X display DISPLAY.
c061c855
GM
4949The optional argument DISPLAY specifies which display to ask about.
4950DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4951If omitted or nil, that stands for the selected frame's display. */)
4952 (display)
08a90d6a 4953 Lisp_Object display;
41beb8fc 4954{
08a90d6a 4955 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4956
4957 return make_number (HeightMMOfScreen (dpyinfo->screen));
41beb8fc
RS
4958}
4959
4960DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
7ee72033 4961 doc: /* Return the width in millimeters of the X display DISPLAY.
c061c855
GM
4962The optional argument DISPLAY specifies which display to ask about.
4963DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4964If omitted or nil, that stands for the selected frame's display. */)
4965 (display)
08a90d6a 4966 Lisp_Object display;
41beb8fc 4967{
08a90d6a 4968 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4969
4970 return make_number (WidthMMOfScreen (dpyinfo->screen));
41beb8fc
RS
4971}
4972
4973DEFUN ("x-display-backing-store", Fx_display_backing_store,
c061c855 4974 Sx_display_backing_store, 0, 1, 0,
7ee72033 4975 doc: /* Returns an indication of whether X display DISPLAY does backing store.
c061c855
GM
4976The value may be `always', `when-mapped', or `not-useful'.
4977The optional argument DISPLAY specifies which display to ask about.
4978DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4979If omitted or nil, that stands for the selected frame's display. */)
4980 (display)
08a90d6a 4981 Lisp_Object display;
41beb8fc 4982{
08a90d6a 4983 struct x_display_info *dpyinfo = check_x_display_info (display);
8ec8a5ec 4984 Lisp_Object result;
11ae94fe 4985
b9dc4443 4986 switch (DoesBackingStore (dpyinfo->screen))
41beb8fc
RS
4987 {
4988 case Always:
8ec8a5ec
GM
4989 result = intern ("always");
4990 break;
41beb8fc
RS
4991
4992 case WhenMapped:
8ec8a5ec
GM
4993 result = intern ("when-mapped");
4994 break;
41beb8fc
RS
4995
4996 case NotUseful:
8ec8a5ec
GM
4997 result = intern ("not-useful");
4998 break;
41beb8fc
RS
4999
5000 default:
5001 error ("Strange value for BackingStore parameter of screen");
8ec8a5ec 5002 result = Qnil;
41beb8fc 5003 }
8ec8a5ec
GM
5004
5005 return result;
41beb8fc
RS
5006}
5007
5008DEFUN ("x-display-visual-class", Fx_display_visual_class,
c061c855 5009 Sx_display_visual_class, 0, 1, 0,
7ee72033 5010 doc: /* Return the visual class of the X display DISPLAY.
c061c855
GM
5011The value is one of the symbols `static-gray', `gray-scale',
5012`static-color', `pseudo-color', `true-color', or `direct-color'.
5013
5014The optional argument DISPLAY specifies which display to ask about.
5015DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
5016If omitted or nil, that stands for the selected frame's display. */)
5017 (display)
08a90d6a 5018 Lisp_Object display;
41beb8fc 5019{
08a90d6a 5020 struct x_display_info *dpyinfo = check_x_display_info (display);
8ec8a5ec 5021 Lisp_Object result;
11ae94fe 5022
b9dc4443 5023 switch (dpyinfo->visual->class)
41beb8fc 5024 {
8ec8a5ec
GM
5025 case StaticGray:
5026 result = intern ("static-gray");
5027 break;
5028 case GrayScale:
5029 result = intern ("gray-scale");
5030 break;
5031 case StaticColor:
5032 result = intern ("static-color");
5033 break;
5034 case PseudoColor:
5035 result = intern ("pseudo-color");
5036 break;
5037 case TrueColor:
5038 result = intern ("true-color");
5039 break;
5040 case DirectColor:
5041 result = intern ("direct-color");
5042 break;
41beb8fc
RS
5043 default:
5044 error ("Display has an unknown visual class");
8ec8a5ec 5045 result = Qnil;
41beb8fc 5046 }
8ec8a5ec
GM
5047
5048 return result;
41beb8fc
RS
5049}
5050
5051DEFUN ("x-display-save-under", Fx_display_save_under,
c061c855 5052 Sx_display_save_under, 0, 1, 0,
7ee72033 5053 doc: /* Returns t if the X display DISPLAY supports the save-under feature.
c061c855
GM
5054The optional argument DISPLAY specifies which display to ask about.
5055DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
5056If omitted or nil, that stands for the selected frame's display. */)
5057 (display)
08a90d6a 5058 Lisp_Object display;
41beb8fc 5059{
08a90d6a 5060 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 5061
b9dc4443 5062 if (DoesSaveUnders (dpyinfo->screen) == True)
41beb8fc
RS
5063 return Qt;
5064 else
5065 return Qnil;
5066}
5067\f
b9dc4443 5068int
55caf99c
RS
5069x_pixel_width (f)
5070 register struct frame *f;
01f1ba30 5071{
55caf99c 5072 return PIXEL_WIDTH (f);
01f1ba30
JB
5073}
5074
b9dc4443 5075int
55caf99c
RS
5076x_pixel_height (f)
5077 register struct frame *f;
01f1ba30 5078{
55caf99c
RS
5079 return PIXEL_HEIGHT (f);
5080}
5081
b9dc4443 5082int
55caf99c
RS
5083x_char_width (f)
5084 register struct frame *f;
5085{
7556890b 5086 return FONT_WIDTH (f->output_data.x->font);
55caf99c
RS
5087}
5088
b9dc4443 5089int
55caf99c
RS
5090x_char_height (f)
5091 register struct frame *f;
5092{
7556890b 5093 return f->output_data.x->line_height;
01f1ba30 5094}
b9dc4443
RS
5095
5096int
f03f2489
RS
5097x_screen_planes (f)
5098 register struct frame *f;
b9dc4443 5099{
f03f2489 5100 return FRAME_X_DISPLAY_INFO (f)->n_planes;
b9dc4443 5101}
01f1ba30 5102
a6ad00c0
GM
5103
5104\f
5105/************************************************************************
5106 X Displays
5107 ************************************************************************/
5108
01f1ba30 5109\f
a6ad00c0
GM
5110/* Mapping visual names to visuals. */
5111
5112static struct visual_class
5113{
5114 char *name;
5115 int class;
5116}
5117visual_classes[] =
5118{
5119 {"StaticGray", StaticGray},
5120 {"GrayScale", GrayScale},
5121 {"StaticColor", StaticColor},
5122 {"PseudoColor", PseudoColor},
5123 {"TrueColor", TrueColor},
5124 {"DirectColor", DirectColor},
9908a324 5125 {NULL, 0}
a6ad00c0
GM
5126};
5127
5128
404daac1 5129#ifndef HAVE_XSCREENNUMBEROFSCREEN
a6ad00c0
GM
5130
5131/* Value is the screen number of screen SCR. This is a substitute for
5132 the X function with the same name when that doesn't exist. */
5133
404daac1
RS
5134int
5135XScreenNumberOfScreen (scr)
5136 register Screen *scr;
5137{
a6ad00c0
GM
5138 Display *dpy = scr->display;
5139 int i;
3df34fdb 5140
a6ad00c0 5141 for (i = 0; i < dpy->nscreens; ++i)
fbd5ceb2 5142 if (scr == dpy->screens + i)
a6ad00c0 5143 break;
404daac1 5144
a6ad00c0 5145 return i;
404daac1 5146}
a6ad00c0 5147
404daac1
RS
5148#endif /* not HAVE_XSCREENNUMBEROFSCREEN */
5149
01f1ba30 5150
a6ad00c0
GM
5151/* Select the visual that should be used on display DPYINFO. Set
5152 members of DPYINFO appropriately. Called from x_term_init. */
fe24a618 5153
a6ad00c0
GM
5154void
5155select_visual (dpyinfo)
5156 struct x_display_info *dpyinfo;
5157{
5158 Display *dpy = dpyinfo->display;
5159 Screen *screen = dpyinfo->screen;
5160 Lisp_Object value;
fe24a618 5161
a6ad00c0
GM
5162 /* See if a visual is specified. */
5163 value = display_x_get_resource (dpyinfo,
5164 build_string ("visualClass"),
5165 build_string ("VisualClass"),
5166 Qnil, Qnil);
5167 if (STRINGP (value))
5168 {
5169 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
5170 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
5171 depth, a decimal number. NAME is compared with case ignored. */
5172 char *s = (char *) alloca (STRING_BYTES (XSTRING (value)) + 1);
5173 char *dash;
5174 int i, class = -1;
5175 XVisualInfo vinfo;
5176
5177 strcpy (s, XSTRING (value)->data);
5178 dash = index (s, '-');
5179 if (dash)
5180 {
5181 dpyinfo->n_planes = atoi (dash + 1);
5182 *dash = '\0';
5183 }
5184 else
5185 /* We won't find a matching visual with depth 0, so that
5186 an error will be printed below. */
5187 dpyinfo->n_planes = 0;
f0614854 5188
a6ad00c0
GM
5189 /* Determine the visual class. */
5190 for (i = 0; visual_classes[i].name; ++i)
5191 if (xstricmp (s, visual_classes[i].name) == 0)
5192 {
5193 class = visual_classes[i].class;
5194 break;
5195 }
01f1ba30 5196
a6ad00c0
GM
5197 /* Look up a matching visual for the specified class. */
5198 if (class == -1
5199 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
5200 dpyinfo->n_planes, class, &vinfo))
5201 fatal ("Invalid visual specification `%s'", XSTRING (value)->data);
5202
5203 dpyinfo->visual = vinfo.visual;
5204 }
01f1ba30
JB
5205 else
5206 {
a6ad00c0
GM
5207 int n_visuals;
5208 XVisualInfo *vinfo, vinfo_template;
5209
5210 dpyinfo->visual = DefaultVisualOfScreen (screen);
5211
5212#ifdef HAVE_X11R4
5213 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
5214#else
5215 vinfo_template.visualid = dpyinfo->visual->visualid;
5216#endif
5217 vinfo_template.screen = XScreenNumberOfScreen (screen);
5218 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
5219 &vinfo_template, &n_visuals);
5220 if (n_visuals != 1)
5221 fatal ("Can't get proper X visual info");
5222
94ac875b 5223 dpyinfo->n_planes = vinfo->depth;
a6ad00c0
GM
5224 XFree ((char *) vinfo);
5225 }
01f1ba30 5226}
01f1ba30 5227
a6ad00c0 5228
b9dc4443
RS
5229/* Return the X display structure for the display named NAME.
5230 Open a new connection if necessary. */
5231
5232struct x_display_info *
5233x_display_info_for_name (name)
5234 Lisp_Object name;
5235{
08a90d6a 5236 Lisp_Object names;
b9dc4443
RS
5237 struct x_display_info *dpyinfo;
5238
b7826503 5239 CHECK_STRING (name);
b9dc4443 5240
806048df
RS
5241 if (! EQ (Vwindow_system, intern ("x")))
5242 error ("Not using X Windows");
5243
08a90d6a
RS
5244 for (dpyinfo = x_display_list, names = x_display_name_list;
5245 dpyinfo;
8e713be6 5246 dpyinfo = dpyinfo->next, names = XCDR (names))
b9dc4443
RS
5247 {
5248 Lisp_Object tem;
8e713be6 5249 tem = Fstring_equal (XCAR (XCAR (names)), name);
08a90d6a 5250 if (!NILP (tem))
b9dc4443
RS
5251 return dpyinfo;
5252 }
5253
b7975ee4
KH
5254 /* Use this general default value to start with. */
5255 Vx_resource_name = Vinvocation_name;
5256
b9dc4443
RS
5257 validate_x_resource_name ();
5258
9b207e8e 5259 dpyinfo = x_term_init (name, (char *)0,
b7975ee4 5260 (char *) XSTRING (Vx_resource_name)->data);
b9dc4443 5261
08a90d6a 5262 if (dpyinfo == 0)
1b4ec1c8 5263 error ("Cannot connect to X server %s", XSTRING (name)->data);
08a90d6a 5264
b9dc4443
RS
5265 x_in_use = 1;
5266 XSETFASTINT (Vwindow_system_version, 11);
5267
5268 return dpyinfo;
5269}
5270
a6ad00c0 5271
01f1ba30 5272DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
c061c855 5273 1, 3, 0,
7ee72033 5274 doc: /* Open a connection to an X server.
c061c855
GM
5275DISPLAY is the name of the display to connect to.
5276Optional second arg XRM-STRING is a string of resources in xrdb format.
5277If the optional third arg MUST-SUCCEED is non-nil,
7ee72033
MB
5278terminate Emacs if we can't open the connection. */)
5279 (display, xrm_string, must_succeed)
08a90d6a 5280 Lisp_Object display, xrm_string, must_succeed;
01f1ba30 5281{
01f1ba30 5282 unsigned char *xrm_option;
b9dc4443 5283 struct x_display_info *dpyinfo;
01f1ba30 5284
b7826503 5285 CHECK_STRING (display);
d387c960 5286 if (! NILP (xrm_string))
b7826503 5287 CHECK_STRING (xrm_string);
01f1ba30 5288
806048df
RS
5289 if (! EQ (Vwindow_system, intern ("x")))
5290 error ("Not using X Windows");
5291
d387c960
JB
5292 if (! NILP (xrm_string))
5293 xrm_option = (unsigned char *) XSTRING (xrm_string)->data;
01f1ba30
JB
5294 else
5295 xrm_option = (unsigned char *) 0;
d387c960
JB
5296
5297 validate_x_resource_name ();
5298
e1b1bee8 5299 /* This is what opens the connection and sets x_current_display.
b9dc4443
RS
5300 This also initializes many symbols, such as those used for input. */
5301 dpyinfo = x_term_init (display, xrm_option,
b7975ee4 5302 (char *) XSTRING (Vx_resource_name)->data);
f1c16f36 5303
08a90d6a
RS
5304 if (dpyinfo == 0)
5305 {
5306 if (!NILP (must_succeed))
10ffbc14
GM
5307 fatal ("Cannot connect to X server %s.\n\
5308Check the DISPLAY environment variable or use `-d'.\n\
5309Also use the `xhost' program to verify that it is set to permit\n\
1b4ec1c8 5310connections from your machine.\n",
08a90d6a
RS
5311 XSTRING (display)->data);
5312 else
1b4ec1c8 5313 error ("Cannot connect to X server %s", XSTRING (display)->data);
08a90d6a
RS
5314 }
5315
b9dc4443 5316 x_in_use = 1;
01f1ba30 5317
b9dc4443 5318 XSETFASTINT (Vwindow_system_version, 11);
01f1ba30
JB
5319 return Qnil;
5320}
5321
08a90d6a
RS
5322DEFUN ("x-close-connection", Fx_close_connection,
5323 Sx_close_connection, 1, 1, 0,
7ee72033 5324 doc: /* Close the connection to DISPLAY's X server.
c061c855 5325For DISPLAY, specify either a frame or a display name (a string).
7ee72033
MB
5326If DISPLAY is nil, that stands for the selected frame's display. */)
5327 (display)
c061c855 5328 Lisp_Object display;
01f1ba30 5329{
08a90d6a 5330 struct x_display_info *dpyinfo = check_x_display_info (display);
08a90d6a 5331 int i;
3457bc6e 5332
08a90d6a
RS
5333 if (dpyinfo->reference_count > 0)
5334 error ("Display still has frames on it");
01f1ba30 5335
08a90d6a
RS
5336 BLOCK_INPUT;
5337 /* Free the fonts in the font table. */
5338 for (i = 0; i < dpyinfo->n_fonts; i++)
333b20bb
GM
5339 if (dpyinfo->font_table[i].name)
5340 {
6ecb43ce
KH
5341 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
5342 xfree (dpyinfo->font_table[i].full_name);
333b20bb 5343 xfree (dpyinfo->font_table[i].name);
333b20bb
GM
5344 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
5345 }
5346
08a90d6a
RS
5347 x_destroy_all_bitmaps (dpyinfo);
5348 XSetCloseDownMode (dpyinfo->display, DestroyAll);
82c90203
RS
5349
5350#ifdef USE_X_TOOLKIT
5351 XtCloseDisplay (dpyinfo->display);
5352#else
08a90d6a 5353 XCloseDisplay (dpyinfo->display);
82c90203 5354#endif
08a90d6a
RS
5355
5356 x_delete_display (dpyinfo);
5357 UNBLOCK_INPUT;
3457bc6e 5358
01f1ba30
JB
5359 return Qnil;
5360}
5361
08a90d6a 5362DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7ee72033
MB
5363 doc: /* Return the list of display names that Emacs has connections to. */)
5364 ()
08a90d6a
RS
5365{
5366 Lisp_Object tail, result;
5367
5368 result = Qnil;
8e713be6
KR
5369 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
5370 result = Fcons (XCAR (XCAR (tail)), result);
08a90d6a
RS
5371
5372 return result;
5373}
5374
5375DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7ee72033 5376 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
c061c855
GM
5377If ON is nil, allow buffering of requests.
5378Turning on synchronization prohibits the Xlib routines from buffering
5379requests and seriously degrades performance, but makes debugging much
5380easier.
5381The optional second argument DISPLAY specifies which display to act on.
5382DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
5383If DISPLAY is omitted or nil, that stands for the selected frame's display. */)
5384 (on, display)
08a90d6a 5385 Lisp_Object display, on;
01f1ba30 5386{
08a90d6a 5387 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 5388
b9dc4443 5389 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
01f1ba30
JB
5390
5391 return Qnil;
5392}
5393
b9dc4443 5394/* Wait for responses to all X commands issued so far for frame F. */
6b7b1820
RS
5395
5396void
b9dc4443
RS
5397x_sync (f)
5398 FRAME_PTR f;
6b7b1820 5399{
4e87f4d2 5400 BLOCK_INPUT;
b9dc4443 5401 XSync (FRAME_X_DISPLAY (f), False);
4e87f4d2 5402 UNBLOCK_INPUT;
6b7b1820 5403}
333b20bb 5404
01f1ba30 5405\f
333b20bb
GM
5406/***********************************************************************
5407 Image types
5408 ***********************************************************************/
f1c16f36 5409
333b20bb
GM
5410/* Value is the number of elements of vector VECTOR. */
5411
5412#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
5413
5414/* List of supported image types. Use define_image_type to add new
5415 types. Use lookup_image_type to find a type for a given symbol. */
5416
5417static struct image_type *image_types;
5418
333b20bb
GM
5419/* The symbol `image' which is the car of the lists used to represent
5420 images in Lisp. */
5421
5422extern Lisp_Object Qimage;
5423
5424/* The symbol `xbm' which is used as the type symbol for XBM images. */
5425
5426Lisp_Object Qxbm;
5427
5428/* Keywords. */
5429
0fe92f72 5430extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
90ebdb19
GM
5431extern Lisp_Object QCdata;
5432Lisp_Object QCtype, QCascent, QCmargin, QCrelief;
d2dc8167 5433Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
4a8e312c 5434Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
333b20bb
GM
5435
5436/* Other symbols. */
5437
4a8e312c 5438Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
333b20bb
GM
5439
5440/* Time in seconds after which images should be removed from the cache
5441 if not displayed. */
5442
fcf431dc 5443Lisp_Object Vimage_cache_eviction_delay;
333b20bb
GM
5444
5445/* Function prototypes. */
5446
5447static void define_image_type P_ ((struct image_type *type));
5448static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
5449static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
5450static void x_laplace P_ ((struct frame *, struct image *));
4a8e312c 5451static void x_emboss P_ ((struct frame *, struct image *));
45158a91
GM
5452static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
5453 Lisp_Object));
333b20bb
GM
5454
5455
5456/* Define a new image type from TYPE. This adds a copy of TYPE to
5457 image_types and adds the symbol *TYPE->type to Vimage_types. */
5458
5459static void
5460define_image_type (type)
5461 struct image_type *type;
5462{
5463 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
5464 The initialized data segment is read-only. */
5465 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
5466 bcopy (type, p, sizeof *p);
5467 p->next = image_types;
5468 image_types = p;
5469 Vimage_types = Fcons (*p->type, Vimage_types);
5470}
5471
5472
5473/* Look up image type SYMBOL, and return a pointer to its image_type
5474 structure. Value is null if SYMBOL is not a known image type. */
5475
5476static INLINE struct image_type *
5477lookup_image_type (symbol)
5478 Lisp_Object symbol;
5479{
5480 struct image_type *type;
5481
5482 for (type = image_types; type; type = type->next)
5483 if (EQ (symbol, *type->type))
5484 break;
5485
5486 return type;
5487}
5488
5489
5490/* Value is non-zero if OBJECT is a valid Lisp image specification. A
5491 valid image specification is a list whose car is the symbol
5492 `image', and whose rest is a property list. The property list must
5493 contain a value for key `:type'. That value must be the name of a
5494 supported image type. The rest of the property list depends on the
5495 image type. */
5496
5497int
5498valid_image_p (object)
5499 Lisp_Object object;
5500{
5501 int valid_p = 0;
5502
5503 if (CONSP (object) && EQ (XCAR (object), Qimage))
5504 {
1783ffa2
GM
5505 Lisp_Object tem;
5506
5507 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
5508 if (EQ (XCAR (tem), QCtype))
5509 {
5510 tem = XCDR (tem);
5511 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
5512 {
5513 struct image_type *type;
5514 type = lookup_image_type (XCAR (tem));
5515 if (type)
5516 valid_p = type->valid_p (object);
5517 }
5518
5519 break;
5520 }
333b20bb
GM
5521 }
5522
5523 return valid_p;
5524}
5525
5526
7ab1745f
GM
5527/* Log error message with format string FORMAT and argument ARG.
5528 Signaling an error, e.g. when an image cannot be loaded, is not a
5529 good idea because this would interrupt redisplay, and the error
5530 message display would lead to another redisplay. This function
5531 therefore simply displays a message. */
333b20bb
GM
5532
5533static void
5534image_error (format, arg1, arg2)
5535 char *format;
5536 Lisp_Object arg1, arg2;
5537{
7ab1745f 5538 add_to_log (format, arg1, arg2);
333b20bb
GM
5539}
5540
5541
5542\f
5543/***********************************************************************
5544 Image specifications
5545 ***********************************************************************/
5546
5547enum image_value_type
5548{
5549 IMAGE_DONT_CHECK_VALUE_TYPE,
5550 IMAGE_STRING_VALUE,
6f1be3b9 5551 IMAGE_STRING_OR_NIL_VALUE,
333b20bb
GM
5552 IMAGE_SYMBOL_VALUE,
5553 IMAGE_POSITIVE_INTEGER_VALUE,
3ed61e75 5554 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
333b20bb 5555 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
7c7ff7f5 5556 IMAGE_ASCENT_VALUE,
333b20bb
GM
5557 IMAGE_INTEGER_VALUE,
5558 IMAGE_FUNCTION_VALUE,
5559 IMAGE_NUMBER_VALUE,
5560 IMAGE_BOOL_VALUE
5561};
5562
5563/* Structure used when parsing image specifications. */
5564
5565struct image_keyword
5566{
5567 /* Name of keyword. */
5568 char *name;
5569
5570 /* The type of value allowed. */
5571 enum image_value_type type;
5572
5573 /* Non-zero means key must be present. */
5574 int mandatory_p;
5575
5576 /* Used to recognize duplicate keywords in a property list. */
5577 int count;
5578
5579 /* The value that was found. */
5580 Lisp_Object value;
5581};
5582
5583
bfd2209f
GM
5584static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
5585 int, Lisp_Object));
333b20bb
GM
5586static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
5587
5588
5589/* Parse image spec SPEC according to KEYWORDS. A valid image spec
5590 has the format (image KEYWORD VALUE ...). One of the keyword/
5591 value pairs must be `:type TYPE'. KEYWORDS is a vector of
5592 image_keywords structures of size NKEYWORDS describing other
bfd2209f 5593 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
333b20bb
GM
5594
5595static int
bfd2209f 5596parse_image_spec (spec, keywords, nkeywords, type)
333b20bb
GM
5597 Lisp_Object spec;
5598 struct image_keyword *keywords;
5599 int nkeywords;
5600 Lisp_Object type;
333b20bb
GM
5601{
5602 int i;
5603 Lisp_Object plist;
5604
5605 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
5606 return 0;
5607
5608 plist = XCDR (spec);
5609 while (CONSP (plist))
5610 {
5611 Lisp_Object key, value;
5612
5613 /* First element of a pair must be a symbol. */
5614 key = XCAR (plist);
5615 plist = XCDR (plist);
5616 if (!SYMBOLP (key))
5617 return 0;
5618
5619 /* There must follow a value. */
5620 if (!CONSP (plist))
5621 return 0;
5622 value = XCAR (plist);
5623 plist = XCDR (plist);
5624
5625 /* Find key in KEYWORDS. Error if not found. */
5626 for (i = 0; i < nkeywords; ++i)
5627 if (strcmp (keywords[i].name, XSYMBOL (key)->name->data) == 0)
5628 break;
5629
5630 if (i == nkeywords)
bfd2209f 5631 continue;
333b20bb
GM
5632
5633 /* Record that we recognized the keyword. If a keywords
5634 was found more than once, it's an error. */
5635 keywords[i].value = value;
5636 ++keywords[i].count;
5637
5638 if (keywords[i].count > 1)
5639 return 0;
5640
5641 /* Check type of value against allowed type. */
5642 switch (keywords[i].type)
5643 {
5644 case IMAGE_STRING_VALUE:
5645 if (!STRINGP (value))
5646 return 0;
5647 break;
5648
6f1be3b9
GM
5649 case IMAGE_STRING_OR_NIL_VALUE:
5650 if (!STRINGP (value) && !NILP (value))
5651 return 0;
5652 break;
5653
333b20bb
GM
5654 case IMAGE_SYMBOL_VALUE:
5655 if (!SYMBOLP (value))
5656 return 0;
5657 break;
5658
5659 case IMAGE_POSITIVE_INTEGER_VALUE:
5660 if (!INTEGERP (value) || XINT (value) <= 0)
5661 return 0;
5662 break;
5663
3ed61e75
GM
5664 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
5665 if (INTEGERP (value) && XINT (value) >= 0)
5666 break;
5667 if (CONSP (value)
5668 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
5669 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
5670 break;
5671 return 0;
5672
7c7ff7f5
GM
5673 case IMAGE_ASCENT_VALUE:
5674 if (SYMBOLP (value) && EQ (value, Qcenter))
5675 break;
5676 else if (INTEGERP (value)
5677 && XINT (value) >= 0
5678 && XINT (value) <= 100)
5679 break;
5680 return 0;
5681
333b20bb
GM
5682 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
5683 if (!INTEGERP (value) || XINT (value) < 0)
5684 return 0;
5685 break;
5686
5687 case IMAGE_DONT_CHECK_VALUE_TYPE:
5688 break;
5689
5690 case IMAGE_FUNCTION_VALUE:
5691 value = indirect_function (value);
5692 if (SUBRP (value)
5693 || COMPILEDP (value)
5694 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
5695 break;
5696 return 0;
5697
5698 case IMAGE_NUMBER_VALUE:
5699 if (!INTEGERP (value) && !FLOATP (value))
5700 return 0;
5701 break;
5702
5703 case IMAGE_INTEGER_VALUE:
5704 if (!INTEGERP (value))
5705 return 0;
5706 break;
5707
5708 case IMAGE_BOOL_VALUE:
5709 if (!NILP (value) && !EQ (value, Qt))
5710 return 0;
5711 break;
5712
5713 default:
5714 abort ();
5715 break;
5716 }
5717
5718 if (EQ (key, QCtype) && !EQ (type, value))
5719 return 0;
5720 }
5721
5722 /* Check that all mandatory fields are present. */
5723 for (i = 0; i < nkeywords; ++i)
5724 if (keywords[i].mandatory_p && keywords[i].count == 0)
5725 return 0;
5726
5727 return NILP (plist);
5728}
5729
5730
5731/* Return the value of KEY in image specification SPEC. Value is nil
5732 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
5733 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
5734
5735static Lisp_Object
5736image_spec_value (spec, key, found)
5737 Lisp_Object spec, key;
5738 int *found;
5739{
5740 Lisp_Object tail;
5741
5742 xassert (valid_image_p (spec));
5743
5744 for (tail = XCDR (spec);
5745 CONSP (tail) && CONSP (XCDR (tail));
5746 tail = XCDR (XCDR (tail)))
5747 {
5748 if (EQ (XCAR (tail), key))
5749 {
5750 if (found)
5751 *found = 1;
5752 return XCAR (XCDR (tail));
5753 }
5754 }
5755
5756 if (found)
5757 *found = 0;
5758 return Qnil;
5759}
5760
5761
42677916 5762DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
7ee72033 5763 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
c061c855
GM
5764PIXELS non-nil means return the size in pixels, otherwise return the
5765size in canonical character units.
5766FRAME is the frame on which the image will be displayed. FRAME nil
7ee72033
MB
5767or omitted means use the selected frame. */)
5768 (spec, pixels, frame)
42677916
GM
5769 Lisp_Object spec, pixels, frame;
5770{
5771 Lisp_Object size;
5772
5773 size = Qnil;
5774 if (valid_image_p (spec))
5775 {
5776 struct frame *f = check_x_frame (frame);
83676598 5777 int id = lookup_image (f, spec);
42677916 5778 struct image *img = IMAGE_FROM_ID (f, id);
3ed61e75
GM
5779 int width = img->width + 2 * img->hmargin;
5780 int height = img->height + 2 * img->vmargin;
42677916
GM
5781
5782 if (NILP (pixels))
5783 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
5784 make_float ((double) height / CANON_Y_UNIT (f)));
5785 else
5786 size = Fcons (make_number (width), make_number (height));
5787 }
5788 else
5789 error ("Invalid image specification");
5790
5791 return size;
5792}
5793
333b20bb 5794
b243755a 5795DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
7ee72033 5796 doc: /* Return t if image SPEC has a mask bitmap.
c061c855 5797FRAME is the frame on which the image will be displayed. FRAME nil
7ee72033
MB
5798or omitted means use the selected frame. */)
5799 (spec, frame)
b243755a
GM
5800 Lisp_Object spec, frame;
5801{
5802 Lisp_Object mask;
5803
5804 mask = Qnil;
5805 if (valid_image_p (spec))
5806 {
5807 struct frame *f = check_x_frame (frame);
83676598 5808 int id = lookup_image (f, spec);
b243755a
GM
5809 struct image *img = IMAGE_FROM_ID (f, id);
5810 if (img->mask)
5811 mask = Qt;
5812 }
5813 else
5814 error ("Invalid image specification");
5815
5816 return mask;
5817}
5818
5819
333b20bb
GM
5820\f
5821/***********************************************************************
5822 Image type independent image structures
5823 ***********************************************************************/
5824
5825static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
5826static void free_image P_ ((struct frame *f, struct image *img));
5827
5828
5829/* Allocate and return a new image structure for image specification
5830 SPEC. SPEC has a hash value of HASH. */
5831
5832static struct image *
5833make_image (spec, hash)
5834 Lisp_Object spec;
5835 unsigned hash;
5836{
5837 struct image *img = (struct image *) xmalloc (sizeof *img);
5838
5839 xassert (valid_image_p (spec));
5840 bzero (img, sizeof *img);
5841 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
5842 xassert (img->type != NULL);
5843 img->spec = spec;
5844 img->data.lisp_val = Qnil;
5845 img->ascent = DEFAULT_IMAGE_ASCENT;
5846 img->hash = hash;
5847 return img;
5848}
5849
5850
5851/* Free image IMG which was used on frame F, including its resources. */
5852
5853static void
5854free_image (f, img)
5855 struct frame *f;
5856 struct image *img;
5857{
5858 if (img)
5859 {
5860 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5861
5862 /* Remove IMG from the hash table of its cache. */
5863 if (img->prev)
5864 img->prev->next = img->next;
5865 else
5866 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
5867
5868 if (img->next)
5869 img->next->prev = img->prev;
5870
5871 c->images[img->id] = NULL;
5872
5873 /* Free resources, then free IMG. */
5874 img->type->free (f, img);
5875 xfree (img);
5876 }
5877}
5878
5879
5880/* Prepare image IMG for display on frame F. Must be called before
5881 drawing an image. */
5882
5883void
5884prepare_image_for_display (f, img)
5885 struct frame *f;
5886 struct image *img;
5887{
5888 EMACS_TIME t;
5889
5890 /* We're about to display IMG, so set its timestamp to `now'. */
5891 EMACS_GET_TIME (t);
5892 img->timestamp = EMACS_SECS (t);
5893
5894 /* If IMG doesn't have a pixmap yet, load it now, using the image
5895 type dependent loader function. */
dd00328a 5896 if (img->pixmap == None && !img->load_failed_p)
209061be 5897 img->load_failed_p = img->type->load (f, img) == 0;
333b20bb
GM
5898}
5899
5900
7c7ff7f5
GM
5901/* Value is the number of pixels for the ascent of image IMG when
5902 drawn in face FACE. */
5903
5904int
5905image_ascent (img, face)
5906 struct image *img;
5907 struct face *face;
5908{
3ed61e75 5909 int height = img->height + img->vmargin;
7c7ff7f5
GM
5910 int ascent;
5911
5912 if (img->ascent == CENTERED_IMAGE_ASCENT)
5913 {
5914 if (face->font)
3694cb3f
MB
5915 /* This expression is arranged so that if the image can't be
5916 exactly centered, it will be moved slightly up. This is
5917 because a typical font is `top-heavy' (due to the presence
5918 uppercase letters), so the image placement should err towards
5919 being top-heavy too. It also just generally looks better. */
5920 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
7c7ff7f5
GM
5921 else
5922 ascent = height / 2;
5923 }
5924 else
5925 ascent = height * img->ascent / 100.0;
5926
5927 return ascent;
5928}
5929
f20a3b7a
MB
5930\f
5931/* Image background colors. */
5932
5933static unsigned long
5934four_corners_best (ximg, width, height)
5935 XImage *ximg;
5936 unsigned long width, height;
5937{
b350c2e5
GM
5938 unsigned long corners[4], best;
5939 int i, best_count;
f20a3b7a 5940
b350c2e5
GM
5941 /* Get the colors at the corners of ximg. */
5942 corners[0] = XGetPixel (ximg, 0, 0);
5943 corners[1] = XGetPixel (ximg, width - 1, 0);
5944 corners[2] = XGetPixel (ximg, width - 1, height - 1);
5945 corners[3] = XGetPixel (ximg, 0, height - 1);
f20a3b7a 5946
b350c2e5
GM
5947 /* Choose the most frequently found color as background. */
5948 for (i = best_count = 0; i < 4; ++i)
5949 {
5950 int j, n;
f20a3b7a 5951
b350c2e5
GM
5952 for (j = n = 0; j < 4; ++j)
5953 if (corners[i] == corners[j])
5954 ++n;
f20a3b7a 5955
b350c2e5
GM
5956 if (n > best_count)
5957 best = corners[i], best_count = n;
5958 }
f20a3b7a 5959
b350c2e5 5960 return best;
f20a3b7a
MB
5961}
5962
5963/* Return the `background' field of IMG. If IMG doesn't have one yet,
5964 it is guessed heuristically. If non-zero, XIMG is an existing XImage
5965 object to use for the heuristic. */
5966
5967unsigned long
5968image_background (img, f, ximg)
5969 struct image *img;
5970 struct frame *f;
5971 XImage *ximg;
5972{
5973 if (! img->background_valid)
5974 /* IMG doesn't have a background yet, try to guess a reasonable value. */
5975 {
5976 int free_ximg = !ximg;
5977
5978 if (! ximg)
5979 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
5980 0, 0, img->width, img->height, ~0, ZPixmap);
5981
5982 img->background = four_corners_best (ximg, img->width, img->height);
5983
5984 if (free_ximg)
5985 XDestroyImage (ximg);
5986
5987 img->background_valid = 1;
5988 }
5989
5990 return img->background;
5991}
5992
5993/* Return the `background_transparent' field of IMG. If IMG doesn't
5994 have one yet, it is guessed heuristically. If non-zero, MASK is an
5995 existing XImage object to use for the heuristic. */
5996
5997int
5998image_background_transparent (img, f, mask)
5999 struct image *img;
6000 struct frame *f;
6001 XImage *mask;
6002{
6003 if (! img->background_transparent_valid)
6004 /* IMG doesn't have a background yet, try to guess a reasonable value. */
6005 {
6006 if (img->mask)
6007 {
6008 int free_mask = !mask;
6009
6010 if (! mask)
6011 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
6012 0, 0, img->width, img->height, ~0, ZPixmap);
6013
6014 img->background_transparent
6015 = !four_corners_best (mask, img->width, img->height);
6016
6017 if (free_mask)
6018 XDestroyImage (mask);
6019 }
6020 else
6021 img->background_transparent = 0;
6022
6023 img->background_transparent_valid = 1;
6024 }
6025
6026 return img->background_transparent;
6027}
7c7ff7f5 6028
333b20bb
GM
6029\f
6030/***********************************************************************
6031 Helper functions for X image types
6032 ***********************************************************************/
6033
dd00328a
GM
6034static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
6035 int, int));
333b20bb
GM
6036static void x_clear_image P_ ((struct frame *f, struct image *img));
6037static unsigned long x_alloc_image_color P_ ((struct frame *f,
6038 struct image *img,
6039 Lisp_Object color_name,
6040 unsigned long dflt));
6041
dd00328a
GM
6042
6043/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
6044 free the pixmap if any. MASK_P non-zero means clear the mask
6045 pixmap if any. COLORS_P non-zero means free colors allocated for
6046 the image, if any. */
333b20bb
GM
6047
6048static void
dd00328a 6049x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
333b20bb
GM
6050 struct frame *f;
6051 struct image *img;
dd00328a 6052 int pixmap_p, mask_p, colors_p;
333b20bb 6053{
dd00328a 6054 if (pixmap_p && img->pixmap)
333b20bb 6055 {
333b20bb 6056 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
dd00328a 6057 img->pixmap = None;
f20a3b7a 6058 img->background_valid = 0;
f4779de9
GM
6059 }
6060
dd00328a 6061 if (mask_p && img->mask)
f4779de9
GM
6062 {
6063 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
dd00328a 6064 img->mask = None;
f20a3b7a 6065 img->background_transparent_valid = 0;
333b20bb
GM
6066 }
6067
dd00328a 6068 if (colors_p && img->ncolors)
333b20bb 6069 {
462d5d40 6070 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
6071 xfree (img->colors);
6072 img->colors = NULL;
6073 img->ncolors = 0;
6074 }
dd00328a
GM
6075}
6076
6077/* Free X resources of image IMG which is used on frame F. */
6078
6079static void
6080x_clear_image (f, img)
6081 struct frame *f;
6082 struct image *img;
6083{
6084 BLOCK_INPUT;
6085 x_clear_image_1 (f, img, 1, 1, 1);
f4779de9 6086 UNBLOCK_INPUT;
333b20bb
GM
6087}
6088
6089
6090/* Allocate color COLOR_NAME for image IMG on frame F. If color
6091 cannot be allocated, use DFLT. Add a newly allocated color to
6092 IMG->colors, so that it can be freed again. Value is the pixel
6093 color. */
6094
6095static unsigned long
6096x_alloc_image_color (f, img, color_name, dflt)
6097 struct frame *f;
6098 struct image *img;
6099 Lisp_Object color_name;
6100 unsigned long dflt;
6101{
6102 XColor color;
6103 unsigned long result;
6104
6105 xassert (STRINGP (color_name));
6106
2d764c78 6107 if (x_defined_color (f, XSTRING (color_name)->data, &color, 1))
333b20bb
GM
6108 {
6109 /* This isn't called frequently so we get away with simply
6110 reallocating the color vector to the needed size, here. */
6111 ++img->ncolors;
6112 img->colors =
6113 (unsigned long *) xrealloc (img->colors,
6114 img->ncolors * sizeof *img->colors);
6115 img->colors[img->ncolors - 1] = color.pixel;
6116 result = color.pixel;
6117 }
6118 else
6119 result = dflt;
6120
6121 return result;
6122}
6123
6124
6125\f
6126/***********************************************************************
6127 Image Cache
6128 ***********************************************************************/
6129
6130static void cache_image P_ ((struct frame *f, struct image *img));
ad18ffb1 6131static void postprocess_image P_ ((struct frame *, struct image *));
333b20bb
GM
6132
6133
6134/* Return a new, initialized image cache that is allocated from the
6135 heap. Call free_image_cache to free an image cache. */
6136
6137struct image_cache *
6138make_image_cache ()
6139{
6140 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
6141 int size;
6142
6143 bzero (c, sizeof *c);
6144 c->size = 50;
6145 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
6146 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
6147 c->buckets = (struct image **) xmalloc (size);
6148 bzero (c->buckets, size);
6149 return c;
6150}
6151
6152
6153/* Free image cache of frame F. Be aware that X frames share images
6154 caches. */
6155
6156void
6157free_image_cache (f)
6158 struct frame *f;
6159{
6160 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6161 if (c)
6162 {
6163 int i;
6164
6165 /* Cache should not be referenced by any frame when freed. */
6166 xassert (c->refcount == 0);
6167
6168 for (i = 0; i < c->used; ++i)
6169 free_image (f, c->images[i]);
6170 xfree (c->images);
333b20bb 6171 xfree (c->buckets);
e3130015 6172 xfree (c);
333b20bb
GM
6173 FRAME_X_IMAGE_CACHE (f) = NULL;
6174 }
6175}
6176
6177
6178/* Clear image cache of frame F. FORCE_P non-zero means free all
6179 images. FORCE_P zero means clear only images that haven't been
6180 displayed for some time. Should be called from time to time to
6181 reduce the number of loaded images. If image-eviction-seconds is
6182 non-nil, this frees images in the cache which weren't displayed for
6183 at least that many seconds. */
6184
6185void
6186clear_image_cache (f, force_p)
6187 struct frame *f;
6188 int force_p;
6189{
6190 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6191
83676598 6192 if (c && INTEGERP (Vimage_cache_eviction_delay))
333b20bb
GM
6193 {
6194 EMACS_TIME t;
6195 unsigned long old;
f4779de9 6196 int i, nfreed;
333b20bb
GM
6197
6198 EMACS_GET_TIME (t);
fcf431dc 6199 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
f4779de9
GM
6200
6201 /* Block input so that we won't be interrupted by a SIGIO
6202 while being in an inconsistent state. */
6203 BLOCK_INPUT;
333b20bb 6204
f4779de9 6205 for (i = nfreed = 0; i < c->used; ++i)
333b20bb
GM
6206 {
6207 struct image *img = c->images[i];
6208 if (img != NULL
f4779de9 6209 && (force_p || img->timestamp < old))
333b20bb
GM
6210 {
6211 free_image (f, img);
f4779de9 6212 ++nfreed;
333b20bb
GM
6213 }
6214 }
6215
6216 /* We may be clearing the image cache because, for example,
6217 Emacs was iconified for a longer period of time. In that
6218 case, current matrices may still contain references to
6219 images freed above. So, clear these matrices. */
f4779de9 6220 if (nfreed)
333b20bb 6221 {
f4779de9
GM
6222 Lisp_Object tail, frame;
6223
6224 FOR_EACH_FRAME (tail, frame)
6225 {
6226 struct frame *f = XFRAME (frame);
6227 if (FRAME_X_P (f)
6228 && FRAME_X_IMAGE_CACHE (f) == c)
83676598 6229 clear_current_matrices (f);
f4779de9
GM
6230 }
6231
333b20bb
GM
6232 ++windows_or_buffers_changed;
6233 }
f4779de9
GM
6234
6235 UNBLOCK_INPUT;
333b20bb
GM
6236 }
6237}
6238
6239
6240DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
6241 0, 1, 0,
7ee72033 6242 doc: /* Clear the image cache of FRAME.
c061c855 6243FRAME nil or omitted means use the selected frame.
7ee72033
MB
6244FRAME t means clear the image caches of all frames. */)
6245 (frame)
333b20bb
GM
6246 Lisp_Object frame;
6247{
6248 if (EQ (frame, Qt))
6249 {
6250 Lisp_Object tail;
6251
6252 FOR_EACH_FRAME (tail, frame)
6253 if (FRAME_X_P (XFRAME (frame)))
6254 clear_image_cache (XFRAME (frame), 1);
6255 }
6256 else
6257 clear_image_cache (check_x_frame (frame), 1);
6258
6259 return Qnil;
6260}
6261
6262
ad18ffb1
GM
6263/* Compute masks and transform image IMG on frame F, as specified
6264 by the image's specification, */
6265
6266static void
6267postprocess_image (f, img)
6268 struct frame *f;
6269 struct image *img;
6270{
6271 /* Manipulation of the image's mask. */
6272 if (img->pixmap)
6273 {
6274 Lisp_Object conversion, spec;
6275 Lisp_Object mask;
6276
6277 spec = img->spec;
6278
6279 /* `:heuristic-mask t'
6280 `:mask heuristic'
6281 means build a mask heuristically.
6282 `:heuristic-mask (R G B)'
6283 `:mask (heuristic (R G B))'
6284 means build a mask from color (R G B) in the
6285 image.
6286 `:mask nil'
6287 means remove a mask, if any. */
6288
6289 mask = image_spec_value (spec, QCheuristic_mask, NULL);
6290 if (!NILP (mask))
6291 x_build_heuristic_mask (f, img, mask);
6292 else
6293 {
6294 int found_p;
6295
6296 mask = image_spec_value (spec, QCmask, &found_p);
6297
6298 if (EQ (mask, Qheuristic))
6299 x_build_heuristic_mask (f, img, Qt);
6300 else if (CONSP (mask)
6301 && EQ (XCAR (mask), Qheuristic))
6302 {
6303 if (CONSP (XCDR (mask)))
6304 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
6305 else
6306 x_build_heuristic_mask (f, img, XCDR (mask));
6307 }
6308 else if (NILP (mask) && found_p && img->mask)
6309 {
6310 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
6311 img->mask = None;
6312 }
6313 }
6314
6315
6316 /* Should we apply an image transformation algorithm? */
6317 conversion = image_spec_value (spec, QCconversion, NULL);
6318 if (EQ (conversion, Qdisabled))
6319 x_disable_image (f, img);
6320 else if (EQ (conversion, Qlaplace))
6321 x_laplace (f, img);
6322 else if (EQ (conversion, Qemboss))
6323 x_emboss (f, img);
6324 else if (CONSP (conversion)
6325 && EQ (XCAR (conversion), Qedge_detection))
6326 {
6327 Lisp_Object tem;
6328 tem = XCDR (conversion);
6329 if (CONSP (tem))
6330 x_edge_detection (f, img,
6331 Fplist_get (tem, QCmatrix),
6332 Fplist_get (tem, QCcolor_adjustment));
6333 }
6334 }
6335}
6336
6337
333b20bb 6338/* Return the id of image with Lisp specification SPEC on frame F.
83676598 6339 SPEC must be a valid Lisp image specification (see valid_image_p). */
333b20bb
GM
6340
6341int
83676598 6342lookup_image (f, spec)
333b20bb
GM
6343 struct frame *f;
6344 Lisp_Object spec;
6345{
6346 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6347 struct image *img;
6348 int i;
6349 unsigned hash;
6350 struct gcpro gcpro1;
4f7ca1f1 6351 EMACS_TIME now;
333b20bb
GM
6352
6353 /* F must be a window-system frame, and SPEC must be a valid image
6354 specification. */
6355 xassert (FRAME_WINDOW_P (f));
6356 xassert (valid_image_p (spec));
6357
6358 GCPRO1 (spec);
6359
6360 /* Look up SPEC in the hash table of the image cache. */
6361 hash = sxhash (spec, 0);
6362 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
6363
6364 for (img = c->buckets[i]; img; img = img->next)
6365 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
6366 break;
6367
6368 /* If not found, create a new image and cache it. */
6369 if (img == NULL)
6370 {
ad18ffb1
GM
6371 extern Lisp_Object Qpostscript;
6372
28c7826c 6373 BLOCK_INPUT;
333b20bb
GM
6374 img = make_image (spec, hash);
6375 cache_image (f, img);
83676598 6376 img->load_failed_p = img->type->load (f, img) == 0;
333b20bb
GM
6377
6378 /* If we can't load the image, and we don't have a width and
6379 height, use some arbitrary width and height so that we can
6380 draw a rectangle for it. */
83676598 6381 if (img->load_failed_p)
333b20bb
GM
6382 {
6383 Lisp_Object value;
6384
6385 value = image_spec_value (spec, QCwidth, NULL);
6386 img->width = (INTEGERP (value)
6387 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
6388 value = image_spec_value (spec, QCheight, NULL);
6389 img->height = (INTEGERP (value)
6390 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
6391 }
6392 else
6393 {
6394 /* Handle image type independent image attributes
f20a3b7a
MB
6395 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
6396 `:background COLOR'. */
6397 Lisp_Object ascent, margin, relief, bg;
333b20bb
GM
6398
6399 ascent = image_spec_value (spec, QCascent, NULL);
6400 if (INTEGERP (ascent))
6401 img->ascent = XFASTINT (ascent);
7c7ff7f5
GM
6402 else if (EQ (ascent, Qcenter))
6403 img->ascent = CENTERED_IMAGE_ASCENT;
333b20bb
GM
6404
6405 margin = image_spec_value (spec, QCmargin, NULL);
6406 if (INTEGERP (margin) && XINT (margin) >= 0)
3ed61e75
GM
6407 img->vmargin = img->hmargin = XFASTINT (margin);
6408 else if (CONSP (margin) && INTEGERP (XCAR (margin))
6409 && INTEGERP (XCDR (margin)))
6410 {
6411 if (XINT (XCAR (margin)) > 0)
6412 img->hmargin = XFASTINT (XCAR (margin));
6413 if (XINT (XCDR (margin)) > 0)
6414 img->vmargin = XFASTINT (XCDR (margin));
6415 }
333b20bb
GM
6416
6417 relief = image_spec_value (spec, QCrelief, NULL);
6418 if (INTEGERP (relief))
6419 {
6420 img->relief = XINT (relief);
3ed61e75
GM
6421 img->hmargin += abs (img->relief);
6422 img->vmargin += abs (img->relief);
333b20bb
GM
6423 }
6424
f20a3b7a
MB
6425 if (! img->background_valid)
6426 {
6427 bg = image_spec_value (img->spec, QCbackground, NULL);
6428 if (!NILP (bg))
6429 {
6430 img->background
6431 = x_alloc_image_color (f, img, bg,
6432 FRAME_BACKGROUND_PIXEL (f));
6433 img->background_valid = 1;
6434 }
6435 }
6436
ad18ffb1
GM
6437 /* Do image transformations and compute masks, unless we
6438 don't have the image yet. */
6439 if (!EQ (*img->type->type, Qpostscript))
6440 postprocess_image (f, img);
333b20bb 6441 }
dd00328a 6442
28c7826c
GM
6443 UNBLOCK_INPUT;
6444 xassert (!interrupt_input_blocked);
333b20bb
GM
6445 }
6446
4f7ca1f1
GM
6447 /* We're using IMG, so set its timestamp to `now'. */
6448 EMACS_GET_TIME (now);
6449 img->timestamp = EMACS_SECS (now);
6450
333b20bb
GM
6451 UNGCPRO;
6452
6453 /* Value is the image id. */
6454 return img->id;
6455}
6456
6457
6458/* Cache image IMG in the image cache of frame F. */
6459
6460static void
6461cache_image (f, img)
6462 struct frame *f;
6463 struct image *img;
6464{
6465 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6466 int i;
6467
6468 /* Find a free slot in c->images. */
6469 for (i = 0; i < c->used; ++i)
6470 if (c->images[i] == NULL)
6471 break;
6472
6473 /* If no free slot found, maybe enlarge c->images. */
6474 if (i == c->used && c->used == c->size)
6475 {
6476 c->size *= 2;
6477 c->images = (struct image **) xrealloc (c->images,
6478 c->size * sizeof *c->images);
6479 }
6480
6481 /* Add IMG to c->images, and assign IMG an id. */
6482 c->images[i] = img;
6483 img->id = i;
6484 if (i == c->used)
6485 ++c->used;
6486
6487 /* Add IMG to the cache's hash table. */
6488 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
6489 img->next = c->buckets[i];
6490 if (img->next)
6491 img->next->prev = img;
6492 img->prev = NULL;
6493 c->buckets[i] = img;
6494}
6495
6496
6497/* Call FN on every image in the image cache of frame F. Used to mark
6498 Lisp Objects in the image cache. */
6499
6500void
6501forall_images_in_image_cache (f, fn)
6502 struct frame *f;
6503 void (*fn) P_ ((struct image *img));
6504{
6505 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
6506 {
6507 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6508 if (c)
6509 {
6510 int i;
6511 for (i = 0; i < c->used; ++i)
6512 if (c->images[i])
6513 fn (c->images[i]);
6514 }
6515 }
6516}
6517
6518
6519\f
6520/***********************************************************************
6521 X support code
6522 ***********************************************************************/
6523
45158a91
GM
6524static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
6525 XImage **, Pixmap *));
333b20bb
GM
6526static void x_destroy_x_image P_ ((XImage *));
6527static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
6528
6529
6530/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
6531 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
6532 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
6533 via xmalloc. Print error messages via image_error if an error
45158a91 6534 occurs. Value is non-zero if successful. */
333b20bb
GM
6535
6536static int
45158a91 6537x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
333b20bb 6538 struct frame *f;
333b20bb
GM
6539 int width, height, depth;
6540 XImage **ximg;
6541 Pixmap *pixmap;
6542{
6543 Display *display = FRAME_X_DISPLAY (f);
6544 Screen *screen = FRAME_X_SCREEN (f);
6545 Window window = FRAME_X_WINDOW (f);
6546
6547 xassert (interrupt_input_blocked);
6548
6549 if (depth <= 0)
6550 depth = DefaultDepthOfScreen (screen);
6551 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
6552 depth, ZPixmap, 0, NULL, width, height,
6553 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
6554 if (*ximg == NULL)
6555 {
45158a91 6556 image_error ("Unable to allocate X image", Qnil, Qnil);
333b20bb
GM
6557 return 0;
6558 }
6559
6560 /* Allocate image raster. */
6561 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
6562
6563 /* Allocate a pixmap of the same size. */
6564 *pixmap = XCreatePixmap (display, window, width, height, depth);
dd00328a 6565 if (*pixmap == None)
333b20bb
GM
6566 {
6567 x_destroy_x_image (*ximg);
6568 *ximg = NULL;
45158a91 6569 image_error ("Unable to create X pixmap", Qnil, Qnil);
333b20bb
GM
6570 return 0;
6571 }
6572
6573 return 1;
6574}
6575
6576
6577/* Destroy XImage XIMG. Free XIMG->data. */
6578
6579static void
6580x_destroy_x_image (ximg)
6581 XImage *ximg;
6582{
6583 xassert (interrupt_input_blocked);
6584 if (ximg)
6585 {
6586 xfree (ximg->data);
6587 ximg->data = NULL;
6588 XDestroyImage (ximg);
6589 }
6590}
6591
6592
6593/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
6594 are width and height of both the image and pixmap. */
6595
ea6b19ca 6596static void
333b20bb
GM
6597x_put_x_image (f, ximg, pixmap, width, height)
6598 struct frame *f;
6599 XImage *ximg;
6600 Pixmap pixmap;
c11dc00b 6601 int width, height;
333b20bb
GM
6602{
6603 GC gc;
6604
6605 xassert (interrupt_input_blocked);
6606 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
6607 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
6608 XFreeGC (FRAME_X_DISPLAY (f), gc);
6609}
6610
6611
6612\f
6613/***********************************************************************
5be6c3b0 6614 File Handling
333b20bb
GM
6615 ***********************************************************************/
6616
6617static Lisp_Object x_find_image_file P_ ((Lisp_Object));
5be6c3b0
GM
6618static char *slurp_file P_ ((char *, int *));
6619
333b20bb
GM
6620
6621/* Find image file FILE. Look in data-directory, then
6622 x-bitmap-file-path. Value is the full name of the file found, or
6623 nil if not found. */
6624
6625static Lisp_Object
6626x_find_image_file (file)
6627 Lisp_Object file;
6628{
6629 Lisp_Object file_found, search_path;
6630 struct gcpro gcpro1, gcpro2;
6631 int fd;
6632
6633 file_found = Qnil;
6634 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
6635 GCPRO2 (file_found, search_path);
6636
6637 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
c0ec53ad 6638 fd = openp (search_path, file, Qnil, &file_found, 0);
333b20bb 6639
939d6465 6640 if (fd == -1)
333b20bb
GM
6641 file_found = Qnil;
6642 else
6643 close (fd);
6644
6645 UNGCPRO;
6646 return file_found;
6647}
6648
6649
5be6c3b0
GM
6650/* Read FILE into memory. Value is a pointer to a buffer allocated
6651 with xmalloc holding FILE's contents. Value is null if an error
b243755a 6652 occurred. *SIZE is set to the size of the file. */
5be6c3b0
GM
6653
6654static char *
6655slurp_file (file, size)
6656 char *file;
6657 int *size;
6658{
6659 FILE *fp = NULL;
6660 char *buf = NULL;
6661 struct stat st;
6662
6663 if (stat (file, &st) == 0
6664 && (fp = fopen (file, "r")) != NULL
6665 && (buf = (char *) xmalloc (st.st_size),
6666 fread (buf, 1, st.st_size, fp) == st.st_size))
6667 {
6668 *size = st.st_size;
6669 fclose (fp);
6670 }
6671 else
6672 {
6673 if (fp)
6674 fclose (fp);
6675 if (buf)
6676 {
6677 xfree (buf);
6678 buf = NULL;
6679 }
6680 }
6681
6682 return buf;
6683}
6684
6685
333b20bb
GM
6686\f
6687/***********************************************************************
6688 XBM images
6689 ***********************************************************************/
6690
5be6c3b0 6691static int xbm_scan P_ ((char **, char *, char *, int *));
333b20bb 6692static int xbm_load P_ ((struct frame *f, struct image *img));
5be6c3b0
GM
6693static int xbm_load_image P_ ((struct frame *f, struct image *img,
6694 char *, char *));
333b20bb 6695static int xbm_image_p P_ ((Lisp_Object object));
5be6c3b0
GM
6696static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
6697 unsigned char **));
6698static int xbm_file_p P_ ((Lisp_Object));
333b20bb
GM
6699
6700
6701/* Indices of image specification fields in xbm_format, below. */
6702
6703enum xbm_keyword_index
6704{
6705 XBM_TYPE,
6706 XBM_FILE,
6707 XBM_WIDTH,
6708 XBM_HEIGHT,
6709 XBM_DATA,
6710 XBM_FOREGROUND,
6711 XBM_BACKGROUND,
6712 XBM_ASCENT,
6713 XBM_MARGIN,
6714 XBM_RELIEF,
6715 XBM_ALGORITHM,
6716 XBM_HEURISTIC_MASK,
4a8e312c 6717 XBM_MASK,
333b20bb
GM
6718 XBM_LAST
6719};
6720
6721/* Vector of image_keyword structures describing the format
6722 of valid XBM image specifications. */
6723
6724static struct image_keyword xbm_format[XBM_LAST] =
6725{
6726 {":type", IMAGE_SYMBOL_VALUE, 1},
6727 {":file", IMAGE_STRING_VALUE, 0},
6728 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6729 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6730 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6f1be3b9
GM
6731 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
6732 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
7c7ff7f5 6733 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 6734 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 6735 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 6736 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c
GM
6737 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6738 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
333b20bb
GM
6739};
6740
6741/* Structure describing the image type XBM. */
6742
6743static struct image_type xbm_type =
6744{
6745 &Qxbm,
6746 xbm_image_p,
6747 xbm_load,
6748 x_clear_image,
6749 NULL
6750};
6751
6752/* Tokens returned from xbm_scan. */
6753
6754enum xbm_token
6755{
6756 XBM_TK_IDENT = 256,
6757 XBM_TK_NUMBER
6758};
6759
6760
6761/* Return non-zero if OBJECT is a valid XBM-type image specification.
6762 A valid specification is a list starting with the symbol `image'
6763 The rest of the list is a property list which must contain an
6764 entry `:type xbm..
6765
6766 If the specification specifies a file to load, it must contain
6767 an entry `:file FILENAME' where FILENAME is a string.
6768
6769 If the specification is for a bitmap loaded from memory it must
6770 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
6771 WIDTH and HEIGHT are integers > 0. DATA may be:
6772
6773 1. a string large enough to hold the bitmap data, i.e. it must
6774 have a size >= (WIDTH + 7) / 8 * HEIGHT
6775
6776 2. a bool-vector of size >= WIDTH * HEIGHT
6777
6778 3. a vector of strings or bool-vectors, one for each line of the
6779 bitmap.
6780
5be6c3b0
GM
6781 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
6782 may not be specified in this case because they are defined in the
6783 XBM file.
6784
333b20bb
GM
6785 Both the file and data forms may contain the additional entries
6786 `:background COLOR' and `:foreground COLOR'. If not present,
6787 foreground and background of the frame on which the image is
e3130015 6788 displayed is used. */
333b20bb
GM
6789
6790static int
6791xbm_image_p (object)
6792 Lisp_Object object;
6793{
6794 struct image_keyword kw[XBM_LAST];
6795
6796 bcopy (xbm_format, kw, sizeof kw);
bfd2209f 6797 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
333b20bb
GM
6798 return 0;
6799
6800 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
6801
6802 if (kw[XBM_FILE].count)
6803 {
6804 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
6805 return 0;
6806 }
5be6c3b0
GM
6807 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
6808 {
6809 /* In-memory XBM file. */
6810 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
6811 return 0;
6812 }
333b20bb
GM
6813 else
6814 {
6815 Lisp_Object data;
6816 int width, height;
6817
6818 /* Entries for `:width', `:height' and `:data' must be present. */
6819 if (!kw[XBM_WIDTH].count
6820 || !kw[XBM_HEIGHT].count
6821 || !kw[XBM_DATA].count)
6822 return 0;
6823
6824 data = kw[XBM_DATA].value;
6825 width = XFASTINT (kw[XBM_WIDTH].value);
6826 height = XFASTINT (kw[XBM_HEIGHT].value);
6827
6828 /* Check type of data, and width and height against contents of
6829 data. */
6830 if (VECTORP (data))
6831 {
6832 int i;
6833
6834 /* Number of elements of the vector must be >= height. */
6835 if (XVECTOR (data)->size < height)
6836 return 0;
6837
6838 /* Each string or bool-vector in data must be large enough
6839 for one line of the image. */
6840 for (i = 0; i < height; ++i)
6841 {
6842 Lisp_Object elt = XVECTOR (data)->contents[i];
6843
6844 if (STRINGP (elt))
6845 {
6846 if (XSTRING (elt)->size
6847 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
6848 return 0;
6849 }
6850 else if (BOOL_VECTOR_P (elt))
6851 {
6852 if (XBOOL_VECTOR (elt)->size < width)
6853 return 0;
6854 }
6855 else
6856 return 0;
6857 }
6858 }
6859 else if (STRINGP (data))
6860 {
6861 if (XSTRING (data)->size
6862 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
6863 return 0;
6864 }
6865 else if (BOOL_VECTOR_P (data))
6866 {
6867 if (XBOOL_VECTOR (data)->size < width * height)
6868 return 0;
6869 }
6870 else
6871 return 0;
6872 }
6873
333b20bb
GM
6874 return 1;
6875}
6876
6877
6878/* Scan a bitmap file. FP is the stream to read from. Value is
6879 either an enumerator from enum xbm_token, or a character for a
6880 single-character token, or 0 at end of file. If scanning an
6881 identifier, store the lexeme of the identifier in SVAL. If
6882 scanning a number, store its value in *IVAL. */
6883
6884static int
5be6c3b0
GM
6885xbm_scan (s, end, sval, ival)
6886 char **s, *end;
333b20bb
GM
6887 char *sval;
6888 int *ival;
6889{
6890 int c;
0a695da7
GM
6891
6892 loop:
333b20bb
GM
6893
6894 /* Skip white space. */
5be6c3b0 6895 while (*s < end && (c = *(*s)++, isspace (c)))
333b20bb
GM
6896 ;
6897
5be6c3b0 6898 if (*s >= end)
333b20bb
GM
6899 c = 0;
6900 else if (isdigit (c))
6901 {
6902 int value = 0, digit;
6903
5be6c3b0 6904 if (c == '0' && *s < end)
333b20bb 6905 {
5be6c3b0 6906 c = *(*s)++;
333b20bb
GM
6907 if (c == 'x' || c == 'X')
6908 {
5be6c3b0 6909 while (*s < end)
333b20bb 6910 {
5be6c3b0 6911 c = *(*s)++;
333b20bb
GM
6912 if (isdigit (c))
6913 digit = c - '0';
6914 else if (c >= 'a' && c <= 'f')
6915 digit = c - 'a' + 10;
6916 else if (c >= 'A' && c <= 'F')
6917 digit = c - 'A' + 10;
6918 else
6919 break;
6920 value = 16 * value + digit;
6921 }
6922 }
6923 else if (isdigit (c))
6924 {
6925 value = c - '0';
5be6c3b0
GM
6926 while (*s < end
6927 && (c = *(*s)++, isdigit (c)))
333b20bb
GM
6928 value = 8 * value + c - '0';
6929 }
6930 }
6931 else
6932 {
6933 value = c - '0';
5be6c3b0
GM
6934 while (*s < end
6935 && (c = *(*s)++, isdigit (c)))
333b20bb
GM
6936 value = 10 * value + c - '0';
6937 }
6938
5be6c3b0
GM
6939 if (*s < end)
6940 *s = *s - 1;
333b20bb
GM
6941 *ival = value;
6942 c = XBM_TK_NUMBER;
6943 }
6944 else if (isalpha (c) || c == '_')
6945 {
6946 *sval++ = c;
5be6c3b0
GM
6947 while (*s < end
6948 && (c = *(*s)++, (isalnum (c) || c == '_')))
333b20bb
GM
6949 *sval++ = c;
6950 *sval = 0;
5be6c3b0
GM
6951 if (*s < end)
6952 *s = *s - 1;
333b20bb
GM
6953 c = XBM_TK_IDENT;
6954 }
0a695da7
GM
6955 else if (c == '/' && **s == '*')
6956 {
6957 /* C-style comment. */
6958 ++*s;
6959 while (**s && (**s != '*' || *(*s + 1) != '/'))
6960 ++*s;
6961 if (**s)
6962 {
6963 *s += 2;
6964 goto loop;
6965 }
6966 }
333b20bb
GM
6967
6968 return c;
6969}
6970
6971
6972/* Replacement for XReadBitmapFileData which isn't available under old
5be6c3b0
GM
6973 X versions. CONTENTS is a pointer to a buffer to parse; END is the
6974 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
6975 the image. Return in *DATA the bitmap data allocated with xmalloc.
6976 Value is non-zero if successful. DATA null means just test if
b243755a 6977 CONTENTS looks like an in-memory XBM file. */
333b20bb
GM
6978
6979static int
5be6c3b0
GM
6980xbm_read_bitmap_data (contents, end, width, height, data)
6981 char *contents, *end;
333b20bb
GM
6982 int *width, *height;
6983 unsigned char **data;
6984{
5be6c3b0 6985 char *s = contents;
333b20bb
GM
6986 char buffer[BUFSIZ];
6987 int padding_p = 0;
6988 int v10 = 0;
6989 int bytes_per_line, i, nbytes;
6990 unsigned char *p;
6991 int value;
6992 int LA1;
6993
6994#define match() \
5be6c3b0 6995 LA1 = xbm_scan (&s, end, buffer, &value)
333b20bb
GM
6996
6997#define expect(TOKEN) \
6998 if (LA1 != (TOKEN)) \
6999 goto failure; \
7000 else \
7001 match ()
7002
7003#define expect_ident(IDENT) \
7004 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
7005 match (); \
7006 else \
7007 goto failure
7008
333b20bb 7009 *width = *height = -1;
5be6c3b0
GM
7010 if (data)
7011 *data = NULL;
7012 LA1 = xbm_scan (&s, end, buffer, &value);
333b20bb
GM
7013
7014 /* Parse defines for width, height and hot-spots. */
7015 while (LA1 == '#')
7016 {
333b20bb
GM
7017 match ();
7018 expect_ident ("define");
7019 expect (XBM_TK_IDENT);
7020
7021 if (LA1 == XBM_TK_NUMBER);
7022 {
7023 char *p = strrchr (buffer, '_');
7024 p = p ? p + 1 : buffer;
7025 if (strcmp (p, "width") == 0)
7026 *width = value;
7027 else if (strcmp (p, "height") == 0)
7028 *height = value;
7029 }
7030 expect (XBM_TK_NUMBER);
7031 }
7032
7033 if (*width < 0 || *height < 0)
7034 goto failure;
5be6c3b0
GM
7035 else if (data == NULL)
7036 goto success;
333b20bb
GM
7037
7038 /* Parse bits. Must start with `static'. */
7039 expect_ident ("static");
7040 if (LA1 == XBM_TK_IDENT)
7041 {
7042 if (strcmp (buffer, "unsigned") == 0)
7043 {
7044 match ();
7045 expect_ident ("char");
7046 }
7047 else if (strcmp (buffer, "short") == 0)
7048 {
7049 match ();
7050 v10 = 1;
7051 if (*width % 16 && *width % 16 < 9)
7052 padding_p = 1;
7053 }
7054 else if (strcmp (buffer, "char") == 0)
7055 match ();
7056 else
7057 goto failure;
7058 }
7059 else
7060 goto failure;
7061
7062 expect (XBM_TK_IDENT);
7063 expect ('[');
7064 expect (']');
7065 expect ('=');
7066 expect ('{');
7067
7068 bytes_per_line = (*width + 7) / 8 + padding_p;
7069 nbytes = bytes_per_line * *height;
7070 p = *data = (char *) xmalloc (nbytes);
7071
7072 if (v10)
7073 {
333b20bb
GM
7074 for (i = 0; i < nbytes; i += 2)
7075 {
7076 int val = value;
7077 expect (XBM_TK_NUMBER);
7078
7079 *p++ = val;
7080 if (!padding_p || ((i + 2) % bytes_per_line))
7081 *p++ = value >> 8;
7082
7083 if (LA1 == ',' || LA1 == '}')
7084 match ();
7085 else
7086 goto failure;
7087 }
7088 }
7089 else
7090 {
7091 for (i = 0; i < nbytes; ++i)
7092 {
7093 int val = value;
7094 expect (XBM_TK_NUMBER);
7095
7096 *p++ = val;
7097
7098 if (LA1 == ',' || LA1 == '}')
7099 match ();
7100 else
7101 goto failure;
7102 }
7103 }
7104
5be6c3b0 7105 success:
333b20bb
GM
7106 return 1;
7107
7108 failure:
7109
5be6c3b0 7110 if (data && *data)
333b20bb
GM
7111 {
7112 xfree (*data);
7113 *data = NULL;
7114 }
7115 return 0;
7116
7117#undef match
7118#undef expect
7119#undef expect_ident
7120}
7121
7122
5be6c3b0
GM
7123/* Load XBM image IMG which will be displayed on frame F from buffer
7124 CONTENTS. END is the end of the buffer. Value is non-zero if
7125 successful. */
333b20bb
GM
7126
7127static int
5be6c3b0 7128xbm_load_image (f, img, contents, end)
333b20bb
GM
7129 struct frame *f;
7130 struct image *img;
5be6c3b0 7131 char *contents, *end;
333b20bb
GM
7132{
7133 int rc;
7134 unsigned char *data;
7135 int success_p = 0;
333b20bb 7136
5be6c3b0 7137 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
333b20bb
GM
7138 if (rc)
7139 {
7140 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
7141 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
7142 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
7143 Lisp_Object value;
7144
7145 xassert (img->width > 0 && img->height > 0);
7146
7147 /* Get foreground and background colors, maybe allocate colors. */
7148 value = image_spec_value (img->spec, QCforeground, NULL);
7149 if (!NILP (value))
7150 foreground = x_alloc_image_color (f, img, value, foreground);
333b20bb
GM
7151 value = image_spec_value (img->spec, QCbackground, NULL);
7152 if (!NILP (value))
f20a3b7a
MB
7153 {
7154 background = x_alloc_image_color (f, img, value, background);
7155 img->background = background;
7156 img->background_valid = 1;
7157 }
333b20bb 7158
333b20bb
GM
7159 img->pixmap
7160 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
7161 FRAME_X_WINDOW (f),
7162 data,
7163 img->width, img->height,
7164 foreground, background,
7165 depth);
7166 xfree (data);
7167
dd00328a 7168 if (img->pixmap == None)
333b20bb
GM
7169 {
7170 x_clear_image (f, img);
5be6c3b0 7171 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
7172 }
7173 else
7174 success_p = 1;
333b20bb
GM
7175 }
7176 else
45158a91 7177 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
333b20bb 7178
333b20bb
GM
7179 return success_p;
7180}
7181
7182
5be6c3b0
GM
7183/* Value is non-zero if DATA looks like an in-memory XBM file. */
7184
7185static int
7186xbm_file_p (data)
7187 Lisp_Object data;
7188{
7189 int w, h;
7190 return (STRINGP (data)
7191 && xbm_read_bitmap_data (XSTRING (data)->data,
7192 (XSTRING (data)->data
7193 + STRING_BYTES (XSTRING (data))),
7194 &w, &h, NULL));
7195}
7196
7197
333b20bb
GM
7198/* Fill image IMG which is used on frame F with pixmap data. Value is
7199 non-zero if successful. */
7200
7201static int
7202xbm_load (f, img)
7203 struct frame *f;
7204 struct image *img;
7205{
7206 int success_p = 0;
7207 Lisp_Object file_name;
7208
7209 xassert (xbm_image_p (img->spec));
7210
7211 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7212 file_name = image_spec_value (img->spec, QCfile, NULL);
7213 if (STRINGP (file_name))
5be6c3b0
GM
7214 {
7215 Lisp_Object file;
7216 char *contents;
7217 int size;
7218 struct gcpro gcpro1;
7219
7220 file = x_find_image_file (file_name);
7221 GCPRO1 (file);
7222 if (!STRINGP (file))
7223 {
7224 image_error ("Cannot find image file `%s'", file_name, Qnil);
7225 UNGCPRO;
7226 return 0;
7227 }
7228
7229 contents = slurp_file (XSTRING (file)->data, &size);
7230 if (contents == NULL)
7231 {
7232 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
7233 UNGCPRO;
7234 return 0;
7235 }
7236
7237 success_p = xbm_load_image (f, img, contents, contents + size);
7238 UNGCPRO;
7239 }
333b20bb
GM
7240 else
7241 {
7242 struct image_keyword fmt[XBM_LAST];
7243 Lisp_Object data;
7244 int depth;
7245 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
7246 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
7247 char *bits;
9b207e8e 7248 int parsed_p;
5be6c3b0
GM
7249 int in_memory_file_p = 0;
7250
7251 /* See if data looks like an in-memory XBM file. */
7252 data = image_spec_value (img->spec, QCdata, NULL);
7253 in_memory_file_p = xbm_file_p (data);
333b20bb 7254
5be6c3b0 7255 /* Parse the image specification. */
333b20bb 7256 bcopy (xbm_format, fmt, sizeof fmt);
bfd2209f 7257 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
333b20bb
GM
7258 xassert (parsed_p);
7259
7260 /* Get specified width, and height. */
5be6c3b0
GM
7261 if (!in_memory_file_p)
7262 {
7263 img->width = XFASTINT (fmt[XBM_WIDTH].value);
7264 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
7265 xassert (img->width > 0 && img->height > 0);
7266 }
333b20bb 7267
333b20bb 7268 /* Get foreground and background colors, maybe allocate colors. */
6f1be3b9
GM
7269 if (fmt[XBM_FOREGROUND].count
7270 && STRINGP (fmt[XBM_FOREGROUND].value))
333b20bb
GM
7271 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
7272 foreground);
6f1be3b9
GM
7273 if (fmt[XBM_BACKGROUND].count
7274 && STRINGP (fmt[XBM_BACKGROUND].value))
333b20bb
GM
7275 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
7276 background);
7277
5be6c3b0
GM
7278 if (in_memory_file_p)
7279 success_p = xbm_load_image (f, img, XSTRING (data)->data,
7280 (XSTRING (data)->data
7281 + STRING_BYTES (XSTRING (data))));
7282 else
333b20bb 7283 {
5be6c3b0
GM
7284 if (VECTORP (data))
7285 {
7286 int i;
7287 char *p;
7288 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
333b20bb 7289
5be6c3b0
GM
7290 p = bits = (char *) alloca (nbytes * img->height);
7291 for (i = 0; i < img->height; ++i, p += nbytes)
7292 {
7293 Lisp_Object line = XVECTOR (data)->contents[i];
7294 if (STRINGP (line))
7295 bcopy (XSTRING (line)->data, p, nbytes);
7296 else
7297 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
7298 }
7299 }
7300 else if (STRINGP (data))
7301 bits = XSTRING (data)->data;
7302 else
7303 bits = XBOOL_VECTOR (data)->data;
7304
7305 /* Create the pixmap. */
7306 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
7307 img->pixmap
7308 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
7309 FRAME_X_WINDOW (f),
7310 bits,
7311 img->width, img->height,
7312 foreground, background,
7313 depth);
7314 if (img->pixmap)
7315 success_p = 1;
7316 else
333b20bb 7317 {
5be6c3b0
GM
7318 image_error ("Unable to create pixmap for XBM image `%s'",
7319 img->spec, Qnil);
7320 x_clear_image (f, img);
333b20bb
GM
7321 }
7322 }
333b20bb
GM
7323 }
7324
7325 return success_p;
7326}
7327
7328
7329\f
7330/***********************************************************************
7331 XPM images
7332 ***********************************************************************/
7333
7334#if HAVE_XPM
7335
7336static int xpm_image_p P_ ((Lisp_Object object));
7337static int xpm_load P_ ((struct frame *f, struct image *img));
7338static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
7339
7340#include "X11/xpm.h"
7341
7342/* The symbol `xpm' identifying XPM-format images. */
7343
7344Lisp_Object Qxpm;
7345
7346/* Indices of image specification fields in xpm_format, below. */
7347
7348enum xpm_keyword_index
7349{
7350 XPM_TYPE,
7351 XPM_FILE,
7352 XPM_DATA,
7353 XPM_ASCENT,
7354 XPM_MARGIN,
7355 XPM_RELIEF,
7356 XPM_ALGORITHM,
7357 XPM_HEURISTIC_MASK,
4a8e312c 7358 XPM_MASK,
333b20bb 7359 XPM_COLOR_SYMBOLS,
f20a3b7a 7360 XPM_BACKGROUND,
333b20bb
GM
7361 XPM_LAST
7362};
7363
7364/* Vector of image_keyword structures describing the format
7365 of valid XPM image specifications. */
7366
7367static struct image_keyword xpm_format[XPM_LAST] =
7368{
7369 {":type", IMAGE_SYMBOL_VALUE, 1},
7370 {":file", IMAGE_STRING_VALUE, 0},
7371 {":data", IMAGE_STRING_VALUE, 0},
7c7ff7f5 7372 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 7373 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 7374 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 7375 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 7376 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 7377 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
7378 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7379 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
7380};
7381
7382/* Structure describing the image type XBM. */
7383
7384static struct image_type xpm_type =
7385{
7386 &Qxpm,
7387 xpm_image_p,
7388 xpm_load,
7389 x_clear_image,
7390 NULL
7391};
7392
7393
b243755a
GM
7394/* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
7395 functions for allocating image colors. Our own functions handle
7396 color allocation failures more gracefully than the ones on the XPM
7397 lib. */
7398
7399#if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
7400#define ALLOC_XPM_COLORS
7401#endif
7402
7403#ifdef ALLOC_XPM_COLORS
7404
f72c62ad 7405static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
b243755a
GM
7406static void xpm_free_color_cache P_ ((void));
7407static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
f72c62ad
GM
7408static int xpm_color_bucket P_ ((char *));
7409static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
7410 XColor *, int));
b243755a
GM
7411
7412/* An entry in a hash table used to cache color definitions of named
7413 colors. This cache is necessary to speed up XPM image loading in
7414 case we do color allocations ourselves. Without it, we would need
7415 a call to XParseColor per pixel in the image. */
7416
7417struct xpm_cached_color
7418{
7419 /* Next in collision chain. */
7420 struct xpm_cached_color *next;
7421
7422 /* Color definition (RGB and pixel color). */
7423 XColor color;
7424
7425 /* Color name. */
7426 char name[1];
7427};
7428
7429/* The hash table used for the color cache, and its bucket vector
7430 size. */
7431
7432#define XPM_COLOR_CACHE_BUCKETS 1001
7433struct xpm_cached_color **xpm_color_cache;
7434
b243755a
GM
7435/* Initialize the color cache. */
7436
7437static void
f72c62ad
GM
7438xpm_init_color_cache (f, attrs)
7439 struct frame *f;
7440 XpmAttributes *attrs;
b243755a
GM
7441{
7442 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
7443 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
7444 memset (xpm_color_cache, 0, nbytes);
7445 init_color_table ();
f72c62ad
GM
7446
7447 if (attrs->valuemask & XpmColorSymbols)
7448 {
7449 int i;
7450 XColor color;
7451
7452 for (i = 0; i < attrs->numsymbols; ++i)
7453 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7454 attrs->colorsymbols[i].value, &color))
7455 {
7456 color.pixel = lookup_rgb_color (f, color.red, color.green,
7457 color.blue);
7458 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
7459 }
7460 }
b243755a
GM
7461}
7462
7463
7464/* Free the color cache. */
7465
7466static void
7467xpm_free_color_cache ()
7468{
7469 struct xpm_cached_color *p, *next;
7470 int i;
7471
7472 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
7473 for (p = xpm_color_cache[i]; p; p = next)
7474 {
7475 next = p->next;
7476 xfree (p);
7477 }
7478
7479 xfree (xpm_color_cache);
7480 xpm_color_cache = NULL;
7481 free_color_table ();
7482}
7483
7484
f72c62ad
GM
7485/* Return the bucket index for color named COLOR_NAME in the color
7486 cache. */
7487
7488static int
7489xpm_color_bucket (color_name)
7490 char *color_name;
7491{
7492 unsigned h = 0;
7493 char *s;
7494
7495 for (s = color_name; *s; ++s)
7496 h = (h << 2) ^ *s;
7497 return h %= XPM_COLOR_CACHE_BUCKETS;
7498}
7499
7500
7501/* On frame F, cache values COLOR for color with name COLOR_NAME.
7502 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
7503 entry added. */
7504
7505static struct xpm_cached_color *
7506xpm_cache_color (f, color_name, color, bucket)
7507 struct frame *f;
7508 char *color_name;
7509 XColor *color;
7510 int bucket;
7511{
7512 size_t nbytes;
7513 struct xpm_cached_color *p;
7514
7515 if (bucket < 0)
7516 bucket = xpm_color_bucket (color_name);
7517
7518 nbytes = sizeof *p + strlen (color_name);
7519 p = (struct xpm_cached_color *) xmalloc (nbytes);
7520 strcpy (p->name, color_name);
7521 p->color = *color;
7522 p->next = xpm_color_cache[bucket];
7523 xpm_color_cache[bucket] = p;
7524 return p;
7525}
7526
7527
b243755a
GM
7528/* Look up color COLOR_NAME for frame F in the color cache. If found,
7529 return the cached definition in *COLOR. Otherwise, make a new
7530 entry in the cache and allocate the color. Value is zero if color
7531 allocation failed. */
7532
7533static int
7534xpm_lookup_color (f, color_name, color)
7535 struct frame *f;
7536 char *color_name;
7537 XColor *color;
7538{
b243755a 7539 struct xpm_cached_color *p;
83676598 7540 int h = xpm_color_bucket (color_name);
b243755a
GM
7541
7542 for (p = xpm_color_cache[h]; p; p = p->next)
7543 if (strcmp (p->name, color_name) == 0)
7544 break;
7545
7546 if (p != NULL)
7547 *color = p->color;
7548 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7549 color_name, color))
7550 {
b243755a
GM
7551 color->pixel = lookup_rgb_color (f, color->red, color->green,
7552 color->blue);
f72c62ad 7553 p = xpm_cache_color (f, color_name, color, h);
b243755a 7554 }
f72c62ad 7555
b243755a
GM
7556 return p != NULL;
7557}
7558
7559
7560/* Callback for allocating color COLOR_NAME. Called from the XPM lib.
7561 CLOSURE is a pointer to the frame on which we allocate the
7562 color. Return in *COLOR the allocated color. Value is non-zero
7563 if successful. */
7564
7565static int
7566xpm_alloc_color (dpy, cmap, color_name, color, closure)
7567 Display *dpy;
7568 Colormap cmap;
7569 char *color_name;
7570 XColor *color;
7571 void *closure;
7572{
7573 return xpm_lookup_color ((struct frame *) closure, color_name, color);
7574}
7575
7576
7577/* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
7578 is a pointer to the frame on which we allocate the color. Value is
7579 non-zero if successful. */
7580
7581static int
7582xpm_free_colors (dpy, cmap, pixels, npixels, closure)
7583 Display *dpy;
7584 Colormap cmap;
7585 Pixel *pixels;
7586 int npixels;
7587 void *closure;
7588{
7589 return 1;
7590}
7591
7592#endif /* ALLOC_XPM_COLORS */
7593
7594
333b20bb
GM
7595/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
7596 for XPM images. Such a list must consist of conses whose car and
7597 cdr are strings. */
7598
7599static int
7600xpm_valid_color_symbols_p (color_symbols)
7601 Lisp_Object color_symbols;
7602{
7603 while (CONSP (color_symbols))
7604 {
7605 Lisp_Object sym = XCAR (color_symbols);
7606 if (!CONSP (sym)
7607 || !STRINGP (XCAR (sym))
7608 || !STRINGP (XCDR (sym)))
7609 break;
7610 color_symbols = XCDR (color_symbols);
7611 }
7612
7613 return NILP (color_symbols);
7614}
7615
7616
7617/* Value is non-zero if OBJECT is a valid XPM image specification. */
7618
7619static int
7620xpm_image_p (object)
7621 Lisp_Object object;
7622{
7623 struct image_keyword fmt[XPM_LAST];
7624 bcopy (xpm_format, fmt, sizeof fmt);
bfd2209f 7625 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
333b20bb
GM
7626 /* Either `:file' or `:data' must be present. */
7627 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
7628 /* Either no `:color-symbols' or it's a list of conses
7629 whose car and cdr are strings. */
7630 && (fmt[XPM_COLOR_SYMBOLS].count == 0
7c7ff7f5 7631 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
333b20bb
GM
7632}
7633
7634
7635/* Load image IMG which will be displayed on frame F. Value is
7636 non-zero if successful. */
7637
7638static int
7639xpm_load (f, img)
7640 struct frame *f;
7641 struct image *img;
7642{
9b207e8e 7643 int rc;
333b20bb
GM
7644 XpmAttributes attrs;
7645 Lisp_Object specified_file, color_symbols;
7646
7647 /* Configure the XPM lib. Use the visual of frame F. Allocate
7648 close colors. Return colors allocated. */
7649 bzero (&attrs, sizeof attrs);
9b2956e2
GM
7650 attrs.visual = FRAME_X_VISUAL (f);
7651 attrs.colormap = FRAME_X_COLORMAP (f);
333b20bb 7652 attrs.valuemask |= XpmVisual;
9b2956e2 7653 attrs.valuemask |= XpmColormap;
b243755a
GM
7654
7655#ifdef ALLOC_XPM_COLORS
7656 /* Allocate colors with our own functions which handle
7657 failing color allocation more gracefully. */
7658 attrs.color_closure = f;
7659 attrs.alloc_color = xpm_alloc_color;
7660 attrs.free_colors = xpm_free_colors;
7661 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
7662#else /* not ALLOC_XPM_COLORS */
7663 /* Let the XPM lib allocate colors. */
333b20bb 7664 attrs.valuemask |= XpmReturnAllocPixels;
e4c082be 7665#ifdef XpmAllocCloseColors
333b20bb
GM
7666 attrs.alloc_close_colors = 1;
7667 attrs.valuemask |= XpmAllocCloseColors;
b243755a 7668#else /* not XpmAllocCloseColors */
e4c082be
RS
7669 attrs.closeness = 600;
7670 attrs.valuemask |= XpmCloseness;
b243755a
GM
7671#endif /* not XpmAllocCloseColors */
7672#endif /* ALLOC_XPM_COLORS */
333b20bb
GM
7673
7674 /* If image specification contains symbolic color definitions, add
7675 these to `attrs'. */
7676 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
7677 if (CONSP (color_symbols))
7678 {
7679 Lisp_Object tail;
7680 XpmColorSymbol *xpm_syms;
7681 int i, size;
7682
7683 attrs.valuemask |= XpmColorSymbols;
7684
7685 /* Count number of symbols. */
7686 attrs.numsymbols = 0;
7687 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
7688 ++attrs.numsymbols;
7689
7690 /* Allocate an XpmColorSymbol array. */
7691 size = attrs.numsymbols * sizeof *xpm_syms;
7692 xpm_syms = (XpmColorSymbol *) alloca (size);
7693 bzero (xpm_syms, size);
7694 attrs.colorsymbols = xpm_syms;
7695
7696 /* Fill the color symbol array. */
7697 for (tail = color_symbols, i = 0;
7698 CONSP (tail);
7699 ++i, tail = XCDR (tail))
7700 {
7701 Lisp_Object name = XCAR (XCAR (tail));
7702 Lisp_Object color = XCDR (XCAR (tail));
7703 xpm_syms[i].name = (char *) alloca (XSTRING (name)->size + 1);
7704 strcpy (xpm_syms[i].name, XSTRING (name)->data);
7705 xpm_syms[i].value = (char *) alloca (XSTRING (color)->size + 1);
7706 strcpy (xpm_syms[i].value, XSTRING (color)->data);
7707 }
7708 }
7709
7710 /* Create a pixmap for the image, either from a file, or from a
7711 string buffer containing data in the same format as an XPM file. */
b243755a 7712#ifdef ALLOC_XPM_COLORS
f72c62ad 7713 xpm_init_color_cache (f, &attrs);
b243755a
GM
7714#endif
7715
333b20bb
GM
7716 specified_file = image_spec_value (img->spec, QCfile, NULL);
7717 if (STRINGP (specified_file))
7718 {
7719 Lisp_Object file = x_find_image_file (specified_file);
7720 if (!STRINGP (file))
7721 {
45158a91 7722 image_error ("Cannot find image file `%s'", specified_file, Qnil);
333b20bb
GM
7723 return 0;
7724 }
7725
7726 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7727 XSTRING (file)->data, &img->pixmap, &img->mask,
7728 &attrs);
7729 }
7730 else
7731 {
7732 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
7733 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7734 XSTRING (buffer)->data,
7735 &img->pixmap, &img->mask,
7736 &attrs);
7737 }
333b20bb
GM
7738
7739 if (rc == XpmSuccess)
7740 {
b243755a
GM
7741#ifdef ALLOC_XPM_COLORS
7742 img->colors = colors_in_color_table (&img->ncolors);
7743#else /* not ALLOC_XPM_COLORS */
f47a9ec4
KR
7744 int i;
7745
333b20bb
GM
7746 img->ncolors = attrs.nalloc_pixels;
7747 img->colors = (unsigned long *) xmalloc (img->ncolors
7748 * sizeof *img->colors);
7749 for (i = 0; i < attrs.nalloc_pixels; ++i)
3b4ae1cc
GM
7750 {
7751 img->colors[i] = attrs.alloc_pixels[i];
7752#ifdef DEBUG_X_COLORS
7753 register_color (img->colors[i]);
7754#endif
7755 }
b243755a 7756#endif /* not ALLOC_XPM_COLORS */
333b20bb
GM
7757
7758 img->width = attrs.width;
7759 img->height = attrs.height;
7760 xassert (img->width > 0 && img->height > 0);
7761
7762 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
333b20bb 7763 XpmFreeAttributes (&attrs);
333b20bb
GM
7764 }
7765 else
7766 {
7767 switch (rc)
7768 {
7769 case XpmOpenFailed:
7770 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
7771 break;
7772
7773 case XpmFileInvalid:
7774 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
7775 break;
7776
7777 case XpmNoMemory:
7778 image_error ("Out of memory (%s)", img->spec, Qnil);
7779 break;
7780
7781 case XpmColorFailed:
7782 image_error ("Color allocation error (%s)", img->spec, Qnil);
7783 break;
7784
7785 default:
7786 image_error ("Unknown error (%s)", img->spec, Qnil);
7787 break;
7788 }
7789 }
7790
b243755a
GM
7791#ifdef ALLOC_XPM_COLORS
7792 xpm_free_color_cache ();
7793#endif
333b20bb
GM
7794 return rc == XpmSuccess;
7795}
7796
7797#endif /* HAVE_XPM != 0 */
7798
7799\f
7800/***********************************************************************
7801 Color table
7802 ***********************************************************************/
7803
7804/* An entry in the color table mapping an RGB color to a pixel color. */
7805
7806struct ct_color
7807{
7808 int r, g, b;
7809 unsigned long pixel;
7810
7811 /* Next in color table collision list. */
7812 struct ct_color *next;
7813};
7814
7815/* The bucket vector size to use. Must be prime. */
7816
7817#define CT_SIZE 101
7818
7819/* Value is a hash of the RGB color given by R, G, and B. */
7820
7821#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
7822
7823/* The color hash table. */
7824
7825struct ct_color **ct_table;
7826
7827/* Number of entries in the color table. */
7828
7829int ct_colors_allocated;
7830
333b20bb
GM
7831/* Initialize the color table. */
7832
7833static void
7834init_color_table ()
7835{
7836 int size = CT_SIZE * sizeof (*ct_table);
7837 ct_table = (struct ct_color **) xmalloc (size);
7838 bzero (ct_table, size);
7839 ct_colors_allocated = 0;
7840}
7841
7842
7843/* Free memory associated with the color table. */
7844
7845static void
7846free_color_table ()
7847{
7848 int i;
7849 struct ct_color *p, *next;
7850
7851 for (i = 0; i < CT_SIZE; ++i)
7852 for (p = ct_table[i]; p; p = next)
7853 {
7854 next = p->next;
7855 xfree (p);
7856 }
7857
7858 xfree (ct_table);
7859 ct_table = NULL;
7860}
7861
7862
7863/* Value is a pixel color for RGB color R, G, B on frame F. If an
7864 entry for that color already is in the color table, return the
7865 pixel color of that entry. Otherwise, allocate a new color for R,
7866 G, B, and make an entry in the color table. */
7867
7868static unsigned long
7869lookup_rgb_color (f, r, g, b)
7870 struct frame *f;
7871 int r, g, b;
7872{
7873 unsigned hash = CT_HASH_RGB (r, g, b);
7874 int i = hash % CT_SIZE;
7875 struct ct_color *p;
7876
7877 for (p = ct_table[i]; p; p = p->next)
7878 if (p->r == r && p->g == g && p->b == b)
7879 break;
7880
7881 if (p == NULL)
7882 {
7883 XColor color;
7884 Colormap cmap;
7885 int rc;
7886
7887 color.red = r;
7888 color.green = g;
7889 color.blue = b;
7890
9b2956e2 7891 cmap = FRAME_X_COLORMAP (f);
d62c8769 7892 rc = x_alloc_nearest_color (f, cmap, &color);
333b20bb
GM
7893
7894 if (rc)
7895 {
7896 ++ct_colors_allocated;
7897
7898 p = (struct ct_color *) xmalloc (sizeof *p);
7899 p->r = r;
7900 p->g = g;
7901 p->b = b;
7902 p->pixel = color.pixel;
7903 p->next = ct_table[i];
7904 ct_table[i] = p;
7905 }
7906 else
7907 return FRAME_FOREGROUND_PIXEL (f);
7908 }
7909
7910 return p->pixel;
7911}
7912
7913
7914/* Look up pixel color PIXEL which is used on frame F in the color
7915 table. If not already present, allocate it. Value is PIXEL. */
7916
7917static unsigned long
7918lookup_pixel_color (f, pixel)
7919 struct frame *f;
7920 unsigned long pixel;
7921{
7922 int i = pixel % CT_SIZE;
7923 struct ct_color *p;
7924
7925 for (p = ct_table[i]; p; p = p->next)
7926 if (p->pixel == pixel)
7927 break;
7928
7929 if (p == NULL)
7930 {
7931 XColor color;
7932 Colormap cmap;
7933 int rc;
7934
9b2956e2 7935 cmap = FRAME_X_COLORMAP (f);
333b20bb 7936 color.pixel = pixel;
a31fedb7 7937 x_query_color (f, &color);
d62c8769 7938 rc = x_alloc_nearest_color (f, cmap, &color);
333b20bb
GM
7939
7940 if (rc)
7941 {
7942 ++ct_colors_allocated;
7943
7944 p = (struct ct_color *) xmalloc (sizeof *p);
7945 p->r = color.red;
7946 p->g = color.green;
7947 p->b = color.blue;
7948 p->pixel = pixel;
7949 p->next = ct_table[i];
7950 ct_table[i] = p;
7951 }
7952 else
7953 return FRAME_FOREGROUND_PIXEL (f);
7954 }
7955
7956 return p->pixel;
7957}
7958
7959
7960/* Value is a vector of all pixel colors contained in the color table,
7961 allocated via xmalloc. Set *N to the number of colors. */
7962
7963static unsigned long *
7964colors_in_color_table (n)
7965 int *n;
7966{
7967 int i, j;
7968 struct ct_color *p;
7969 unsigned long *colors;
7970
7971 if (ct_colors_allocated == 0)
7972 {
7973 *n = 0;
7974 colors = NULL;
7975 }
7976 else
7977 {
7978 colors = (unsigned long *) xmalloc (ct_colors_allocated
7979 * sizeof *colors);
7980 *n = ct_colors_allocated;
7981
7982 for (i = j = 0; i < CT_SIZE; ++i)
7983 for (p = ct_table[i]; p; p = p->next)
7984 colors[j++] = p->pixel;
7985 }
7986
7987 return colors;
7988}
7989
7990
7991\f
7992/***********************************************************************
7993 Algorithms
7994 ***********************************************************************/
7995
4a8e312c
GM
7996static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
7997static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
7998static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
7999
d2dc8167 8000/* Non-zero means draw a cross on images having `:conversion
14819cb3
GM
8001 disabled'. */
8002
8003int cross_disabled_images;
8004
4a8e312c
GM
8005/* Edge detection matrices for different edge-detection
8006 strategies. */
8007
8008static int emboss_matrix[9] = {
8009 /* x - 1 x x + 1 */
8010 2, -1, 0, /* y - 1 */
8011 -1, 0, 1, /* y */
8012 0, 1, -2 /* y + 1 */
8013};
333b20bb 8014
4a8e312c
GM
8015static int laplace_matrix[9] = {
8016 /* x - 1 x x + 1 */
8017 1, 0, 0, /* y - 1 */
8018 0, 0, 0, /* y */
8019 0, 0, -1 /* y + 1 */
8020};
333b20bb 8021
14819cb3
GM
8022/* Value is the intensity of the color whose red/green/blue values
8023 are R, G, and B. */
8024
8025#define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
8026
333b20bb 8027
4a8e312c
GM
8028/* On frame F, return an array of XColor structures describing image
8029 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
8030 non-zero means also fill the red/green/blue members of the XColor
8031 structures. Value is a pointer to the array of XColors structures,
8032 allocated with xmalloc; it must be freed by the caller. */
8033
8034static XColor *
8035x_to_xcolors (f, img, rgb_p)
333b20bb 8036 struct frame *f;
4a8e312c
GM
8037 struct image *img;
8038 int rgb_p;
333b20bb 8039{
4a8e312c
GM
8040 int x, y;
8041 XColor *colors, *p;
8042 XImage *ximg;
333b20bb 8043
4a8e312c
GM
8044 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
8045
8046 /* Get the X image IMG->pixmap. */
8047 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
8048 0, 0, img->width, img->height, ~0, ZPixmap);
333b20bb 8049
4a8e312c
GM
8050 /* Fill the `pixel' members of the XColor array. I wished there
8051 were an easy and portable way to circumvent XGetPixel. */
8052 p = colors;
8053 for (y = 0; y < img->height; ++y)
8054 {
8055 XColor *row = p;
8056
8057 for (x = 0; x < img->width; ++x, ++p)
8058 p->pixel = XGetPixel (ximg, x, y);
8059
8060 if (rgb_p)
a31fedb7 8061 x_query_colors (f, row, img->width);
4a8e312c
GM
8062 }
8063
8064 XDestroyImage (ximg);
4a8e312c 8065 return colors;
333b20bb
GM
8066}
8067
8068
4a8e312c
GM
8069/* Create IMG->pixmap from an array COLORS of XColor structures, whose
8070 RGB members are set. F is the frame on which this all happens.
8071 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
333b20bb
GM
8072
8073static void
4a8e312c 8074x_from_xcolors (f, img, colors)
333b20bb 8075 struct frame *f;
4a8e312c
GM
8076 struct image *img;
8077 XColor *colors;
333b20bb 8078{
4a8e312c
GM
8079 int x, y;
8080 XImage *oimg;
8081 Pixmap pixmap;
8082 XColor *p;
8083
4a8e312c 8084 init_color_table ();
333b20bb 8085
4a8e312c
GM
8086 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
8087 &oimg, &pixmap);
8088 p = colors;
8089 for (y = 0; y < img->height; ++y)
8090 for (x = 0; x < img->width; ++x, ++p)
8091 {
8092 unsigned long pixel;
8093 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
8094 XPutPixel (oimg, x, y, pixel);
8095 }
8096
8097 xfree (colors);
dd00328a 8098 x_clear_image_1 (f, img, 1, 0, 1);
4a8e312c
GM
8099
8100 x_put_x_image (f, oimg, pixmap, img->width, img->height);
8101 x_destroy_x_image (oimg);
8102 img->pixmap = pixmap;
8103 img->colors = colors_in_color_table (&img->ncolors);
8104 free_color_table ();
333b20bb
GM
8105}
8106
8107
4a8e312c
GM
8108/* On frame F, perform edge-detection on image IMG.
8109
8110 MATRIX is a nine-element array specifying the transformation
8111 matrix. See emboss_matrix for an example.
8112
8113 COLOR_ADJUST is a color adjustment added to each pixel of the
8114 outgoing image. */
333b20bb
GM
8115
8116static void
4a8e312c 8117x_detect_edges (f, img, matrix, color_adjust)
333b20bb
GM
8118 struct frame *f;
8119 struct image *img;
4a8e312c 8120 int matrix[9], color_adjust;
333b20bb 8121{
4a8e312c
GM
8122 XColor *colors = x_to_xcolors (f, img, 1);
8123 XColor *new, *p;
8124 int x, y, i, sum;
333b20bb 8125
4a8e312c
GM
8126 for (i = sum = 0; i < 9; ++i)
8127 sum += abs (matrix[i]);
333b20bb 8128
4a8e312c 8129#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
333b20bb 8130
4a8e312c 8131 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
333b20bb 8132
4a8e312c
GM
8133 for (y = 0; y < img->height; ++y)
8134 {
8135 p = COLOR (new, 0, y);
8136 p->red = p->green = p->blue = 0xffff/2;
8137 p = COLOR (new, img->width - 1, y);
8138 p->red = p->green = p->blue = 0xffff/2;
8139 }
8140
8141 for (x = 1; x < img->width - 1; ++x)
8142 {
8143 p = COLOR (new, x, 0);
8144 p->red = p->green = p->blue = 0xffff/2;
8145 p = COLOR (new, x, img->height - 1);
8146 p->red = p->green = p->blue = 0xffff/2;
8147 }
333b20bb 8148
4a8e312c 8149 for (y = 1; y < img->height - 1; ++y)
333b20bb 8150 {
4a8e312c
GM
8151 p = COLOR (new, 1, y);
8152
8153 for (x = 1; x < img->width - 1; ++x, ++p)
8154 {
14819cb3 8155 int r, g, b, y1, x1;
4a8e312c
GM
8156
8157 r = g = b = i = 0;
8158 for (y1 = y - 1; y1 < y + 2; ++y1)
8159 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
8160 if (matrix[i])
8161 {
8162 XColor *t = COLOR (colors, x1, y1);
8163 r += matrix[i] * t->red;
8164 g += matrix[i] * t->green;
8165 b += matrix[i] * t->blue;
8166 }
333b20bb 8167
4a8e312c
GM
8168 r = (r / sum + color_adjust) & 0xffff;
8169 g = (g / sum + color_adjust) & 0xffff;
8170 b = (b / sum + color_adjust) & 0xffff;
14819cb3 8171 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
333b20bb 8172 }
333b20bb
GM
8173 }
8174
4a8e312c
GM
8175 xfree (colors);
8176 x_from_xcolors (f, img, new);
333b20bb 8177
4a8e312c
GM
8178#undef COLOR
8179}
8180
8181
8182/* Perform the pre-defined `emboss' edge-detection on image IMG
8183 on frame F. */
8184
8185static void
8186x_emboss (f, img)
8187 struct frame *f;
8188 struct image *img;
8189{
8190 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
8191}
8192
8193
8194/* Perform the pre-defined `laplace' edge-detection on image IMG
8195 on frame F. */
8196
8197static void
8198x_laplace (f, img)
8199 struct frame *f;
8200 struct image *img;
8201{
8202 x_detect_edges (f, img, laplace_matrix, 45000);
8203}
8204
8205
8206/* Perform edge-detection on image IMG on frame F, with specified
8207 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
8208
8209 MATRIX must be either
8210
8211 - a list of at least 9 numbers in row-major form
8212 - a vector of at least 9 numbers
8213
8214 COLOR_ADJUST nil means use a default; otherwise it must be a
8215 number. */
8216
8217static void
8218x_edge_detection (f, img, matrix, color_adjust)
8219 struct frame *f;
8220 struct image *img;
8221 Lisp_Object matrix, color_adjust;
8222{
8223 int i = 0;
8224 int trans[9];
333b20bb 8225
4a8e312c
GM
8226 if (CONSP (matrix))
8227 {
8228 for (i = 0;
8229 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
8230 ++i, matrix = XCDR (matrix))
8231 trans[i] = XFLOATINT (XCAR (matrix));
8232 }
8233 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
8234 {
8235 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
8236 trans[i] = XFLOATINT (AREF (matrix, i));
8237 }
333b20bb 8238
4a8e312c
GM
8239 if (NILP (color_adjust))
8240 color_adjust = make_number (0xffff / 2);
333b20bb 8241
4a8e312c
GM
8242 if (i == 9 && NUMBERP (color_adjust))
8243 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
333b20bb
GM
8244}
8245
8246
14819cb3
GM
8247/* Transform image IMG on frame F so that it looks disabled. */
8248
8249static void
8250x_disable_image (f, img)
8251 struct frame *f;
8252 struct image *img;
8253{
8254 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
dd00328a 8255
14819cb3
GM
8256 if (dpyinfo->n_planes >= 2)
8257 {
8258 /* Color (or grayscale). Convert to gray, and equalize. Just
8259 drawing such images with a stipple can look very odd, so
8260 we're using this method instead. */
8261 XColor *colors = x_to_xcolors (f, img, 1);
8262 XColor *p, *end;
8263 const int h = 15000;
8264 const int l = 30000;
8265
8266 for (p = colors, end = colors + img->width * img->height;
8267 p < end;
8268 ++p)
8269 {
8270 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
8271 int i2 = (0xffff - h - l) * i / 0xffff + l;
8272 p->red = p->green = p->blue = i2;
8273 }
8274
8275 x_from_xcolors (f, img, colors);
8276 }
8277
8278 /* Draw a cross over the disabled image, if we must or if we
8279 should. */
8280 if (dpyinfo->n_planes < 2 || cross_disabled_images)
8281 {
8282 Display *dpy = FRAME_X_DISPLAY (f);
8283 GC gc;
8284
14819cb3
GM
8285 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
8286 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
8287 XDrawLine (dpy, img->pixmap, gc, 0, 0,
8288 img->width - 1, img->height - 1);
8289 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
8290 img->width - 1, 0);
8291 XFreeGC (dpy, gc);
8292
8293 if (img->mask)
8294 {
8295 gc = XCreateGC (dpy, img->mask, 0, NULL);
8296 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
8297 XDrawLine (dpy, img->mask, gc, 0, 0,
8298 img->width - 1, img->height - 1);
8299 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
8300 img->width - 1, 0);
8301 XFreeGC (dpy, gc);
8302 }
14819cb3
GM
8303 }
8304}
8305
8306
333b20bb
GM
8307/* Build a mask for image IMG which is used on frame F. FILE is the
8308 name of an image file, for error messages. HOW determines how to
fcf431dc
GM
8309 determine the background color of IMG. If it is a list '(R G B)',
8310 with R, G, and B being integers >= 0, take that as the color of the
8311 background. Otherwise, determine the background color of IMG
8312 heuristically. Value is non-zero if successful. */
333b20bb
GM
8313
8314static int
45158a91 8315x_build_heuristic_mask (f, img, how)
333b20bb 8316 struct frame *f;
333b20bb
GM
8317 struct image *img;
8318 Lisp_Object how;
8319{
8320 Display *dpy = FRAME_X_DISPLAY (f);
333b20bb 8321 XImage *ximg, *mask_img;
f20a3b7a 8322 int x, y, rc, use_img_background;
8ec8a5ec 8323 unsigned long bg = 0;
333b20bb 8324
4a8e312c
GM
8325 if (img->mask)
8326 {
8327 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
dd00328a 8328 img->mask = None;
f20a3b7a 8329 img->background_transparent_valid = 0;
4a8e312c 8330 }
dd00328a 8331
333b20bb 8332 /* Create an image and pixmap serving as mask. */
45158a91 8333 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
333b20bb
GM
8334 &mask_img, &img->mask);
8335 if (!rc)
28c7826c 8336 return 0;
333b20bb
GM
8337
8338 /* Get the X image of IMG->pixmap. */
8339 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
8340 ~0, ZPixmap);
8341
fcf431dc 8342 /* Determine the background color of ximg. If HOW is `(R G B)'
f20a3b7a
MB
8343 take that as color. Otherwise, use the image's background color. */
8344 use_img_background = 1;
fcf431dc
GM
8345
8346 if (CONSP (how))
8347 {
cac1daf0 8348 int rgb[3], i;
fcf431dc 8349
cac1daf0 8350 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
fcf431dc
GM
8351 {
8352 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
8353 how = XCDR (how);
8354 }
8355
8356 if (i == 3 && NILP (how))
8357 {
8358 char color_name[30];
fcf431dc 8359 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
053b3256
GM
8360 bg = x_alloc_image_color (f, img, build_string (color_name), 0);
8361 use_img_background = 0;
fcf431dc
GM
8362 }
8363 }
8364
f20a3b7a 8365 if (use_img_background)
43f7c3ea 8366 bg = four_corners_best (ximg, img->width, img->height);
333b20bb
GM
8367
8368 /* Set all bits in mask_img to 1 whose color in ximg is different
8369 from the background color bg. */
8370 for (y = 0; y < img->height; ++y)
8371 for (x = 0; x < img->width; ++x)
8372 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
8373
f20a3b7a
MB
8374 /* Fill in the background_transparent field while we have the mask handy. */
8375 image_background_transparent (img, f, mask_img);
8376
333b20bb
GM
8377 /* Put mask_img into img->mask. */
8378 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8379 x_destroy_x_image (mask_img);
8380 XDestroyImage (ximg);
8381
333b20bb
GM
8382 return 1;
8383}
8384
8385
8386\f
8387/***********************************************************************
8388 PBM (mono, gray, color)
8389 ***********************************************************************/
8390
8391static int pbm_image_p P_ ((Lisp_Object object));
8392static int pbm_load P_ ((struct frame *f, struct image *img));
63cec32f 8393static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
333b20bb
GM
8394
8395/* The symbol `pbm' identifying images of this type. */
8396
8397Lisp_Object Qpbm;
8398
8399/* Indices of image specification fields in gs_format, below. */
8400
8401enum pbm_keyword_index
8402{
8403 PBM_TYPE,
8404 PBM_FILE,
63cec32f 8405 PBM_DATA,
333b20bb
GM
8406 PBM_ASCENT,
8407 PBM_MARGIN,
8408 PBM_RELIEF,
8409 PBM_ALGORITHM,
8410 PBM_HEURISTIC_MASK,
4a8e312c 8411 PBM_MASK,
be0b1fac
GM
8412 PBM_FOREGROUND,
8413 PBM_BACKGROUND,
333b20bb
GM
8414 PBM_LAST
8415};
8416
8417/* Vector of image_keyword structures describing the format
8418 of valid user-defined image specifications. */
8419
8420static struct image_keyword pbm_format[PBM_LAST] =
8421{
8422 {":type", IMAGE_SYMBOL_VALUE, 1},
63cec32f
GM
8423 {":file", IMAGE_STRING_VALUE, 0},
8424 {":data", IMAGE_STRING_VALUE, 0},
7c7ff7f5 8425 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 8426 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 8427 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 8428 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 8429 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
be0b1fac 8430 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6f1be3b9
GM
8431 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
8432 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
8433};
8434
8435/* Structure describing the image type `pbm'. */
8436
8437static struct image_type pbm_type =
8438{
8439 &Qpbm,
8440 pbm_image_p,
8441 pbm_load,
8442 x_clear_image,
8443 NULL
8444};
8445
8446
8447/* Return non-zero if OBJECT is a valid PBM image specification. */
8448
8449static int
8450pbm_image_p (object)
8451 Lisp_Object object;
8452{
8453 struct image_keyword fmt[PBM_LAST];
8454
8455 bcopy (pbm_format, fmt, sizeof fmt);
8456
7c7ff7f5 8457 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
333b20bb 8458 return 0;
63cec32f
GM
8459
8460 /* Must specify either :data or :file. */
8461 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
333b20bb
GM
8462}
8463
8464
63cec32f
GM
8465/* Scan a decimal number from *S and return it. Advance *S while
8466 reading the number. END is the end of the string. Value is -1 at
8467 end of input. */
333b20bb
GM
8468
8469static int
63cec32f
GM
8470pbm_scan_number (s, end)
8471 unsigned char **s, *end;
333b20bb 8472{
8ec8a5ec 8473 int c = 0, val = -1;
333b20bb 8474
63cec32f 8475 while (*s < end)
333b20bb
GM
8476 {
8477 /* Skip white-space. */
63cec32f 8478 while (*s < end && (c = *(*s)++, isspace (c)))
333b20bb
GM
8479 ;
8480
8481 if (c == '#')
8482 {
8483 /* Skip comment to end of line. */
63cec32f 8484 while (*s < end && (c = *(*s)++, c != '\n'))
333b20bb
GM
8485 ;
8486 }
8487 else if (isdigit (c))
8488 {
8489 /* Read decimal number. */
8490 val = c - '0';
63cec32f 8491 while (*s < end && (c = *(*s)++, isdigit (c)))
333b20bb
GM
8492 val = 10 * val + c - '0';
8493 break;
8494 }
8495 else
8496 break;
8497 }
8498
8499 return val;
8500}
8501
8502
8503/* Load PBM image IMG for use on frame F. */
8504
8505static int
8506pbm_load (f, img)
8507 struct frame *f;
8508 struct image *img;
8509{
333b20bb 8510 int raw_p, x, y;
b6d7acec 8511 int width, height, max_color_idx = 0;
333b20bb
GM
8512 XImage *ximg;
8513 Lisp_Object file, specified_file;
8514 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
8515 struct gcpro gcpro1;
63cec32f
GM
8516 unsigned char *contents = NULL;
8517 unsigned char *end, *p;
8518 int size;
333b20bb
GM
8519
8520 specified_file = image_spec_value (img->spec, QCfile, NULL);
63cec32f 8521 file = Qnil;
333b20bb 8522 GCPRO1 (file);
333b20bb 8523
63cec32f 8524 if (STRINGP (specified_file))
333b20bb 8525 {
63cec32f
GM
8526 file = x_find_image_file (specified_file);
8527 if (!STRINGP (file))
8528 {
8529 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8530 UNGCPRO;
8531 return 0;
8532 }
333b20bb 8533
5be6c3b0 8534 contents = slurp_file (XSTRING (file)->data, &size);
63cec32f
GM
8535 if (contents == NULL)
8536 {
8537 image_error ("Error reading `%s'", file, Qnil);
8538 UNGCPRO;
8539 return 0;
8540 }
8541
8542 p = contents;
8543 end = contents + size;
8544 }
8545 else
333b20bb 8546 {
63cec32f
GM
8547 Lisp_Object data;
8548 data = image_spec_value (img->spec, QCdata, NULL);
8549 p = XSTRING (data)->data;
8550 end = p + STRING_BYTES (XSTRING (data));
333b20bb
GM
8551 }
8552
63cec32f
GM
8553 /* Check magic number. */
8554 if (end - p < 2 || *p++ != 'P')
333b20bb 8555 {
45158a91 8556 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
63cec32f
GM
8557 error:
8558 xfree (contents);
333b20bb
GM
8559 UNGCPRO;
8560 return 0;
8561 }
8562
63cec32f 8563 switch (*p++)
333b20bb
GM
8564 {
8565 case '1':
8566 raw_p = 0, type = PBM_MONO;
8567 break;
8568
8569 case '2':
8570 raw_p = 0, type = PBM_GRAY;
8571 break;
8572
8573 case '3':
8574 raw_p = 0, type = PBM_COLOR;
8575 break;
8576
8577 case '4':
8578 raw_p = 1, type = PBM_MONO;
8579 break;
8580
8581 case '5':
8582 raw_p = 1, type = PBM_GRAY;
8583 break;
8584
8585 case '6':
8586 raw_p = 1, type = PBM_COLOR;
8587 break;
8588
8589 default:
45158a91 8590 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
63cec32f 8591 goto error;
333b20bb
GM
8592 }
8593
8594 /* Read width, height, maximum color-component. Characters
8595 starting with `#' up to the end of a line are ignored. */
63cec32f
GM
8596 width = pbm_scan_number (&p, end);
8597 height = pbm_scan_number (&p, end);
333b20bb
GM
8598
8599 if (type != PBM_MONO)
8600 {
63cec32f 8601 max_color_idx = pbm_scan_number (&p, end);
333b20bb
GM
8602 if (raw_p && max_color_idx > 255)
8603 max_color_idx = 255;
8604 }
8605
63cec32f
GM
8606 if (width < 0
8607 || height < 0
333b20bb 8608 || (type != PBM_MONO && max_color_idx < 0))
63cec32f 8609 goto error;
333b20bb 8610
45158a91 8611 if (!x_create_x_image_and_pixmap (f, width, height, 0,
333b20bb 8612 &ximg, &img->pixmap))
28c7826c 8613 goto error;
333b20bb
GM
8614
8615 /* Initialize the color hash table. */
8616 init_color_table ();
8617
8618 if (type == PBM_MONO)
8619 {
8620 int c = 0, g;
be0b1fac
GM
8621 struct image_keyword fmt[PBM_LAST];
8622 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
8623 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
8624
8625 /* Parse the image specification. */
8626 bcopy (pbm_format, fmt, sizeof fmt);
8627 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
8628
8629 /* Get foreground and background colors, maybe allocate colors. */
6f1be3b9
GM
8630 if (fmt[PBM_FOREGROUND].count
8631 && STRINGP (fmt[PBM_FOREGROUND].value))
be0b1fac 8632 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
6f1be3b9
GM
8633 if (fmt[PBM_BACKGROUND].count
8634 && STRINGP (fmt[PBM_BACKGROUND].value))
f20a3b7a
MB
8635 {
8636 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
8637 img->background = bg;
8638 img->background_valid = 1;
8639 }
333b20bb
GM
8640
8641 for (y = 0; y < height; ++y)
8642 for (x = 0; x < width; ++x)
8643 {
8644 if (raw_p)
8645 {
8646 if ((x & 7) == 0)
63cec32f 8647 c = *p++;
333b20bb
GM
8648 g = c & 0x80;
8649 c <<= 1;
8650 }
8651 else
63cec32f 8652 g = pbm_scan_number (&p, end);
333b20bb 8653
be0b1fac 8654 XPutPixel (ximg, x, y, g ? fg : bg);
333b20bb
GM
8655 }
8656 }
8657 else
8658 {
8659 for (y = 0; y < height; ++y)
8660 for (x = 0; x < width; ++x)
8661 {
8662 int r, g, b;
8663
8664 if (type == PBM_GRAY)
63cec32f 8665 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
333b20bb
GM
8666 else if (raw_p)
8667 {
63cec32f
GM
8668 r = *p++;
8669 g = *p++;
8670 b = *p++;
333b20bb
GM
8671 }
8672 else
8673 {
63cec32f
GM
8674 r = pbm_scan_number (&p, end);
8675 g = pbm_scan_number (&p, end);
8676 b = pbm_scan_number (&p, end);
333b20bb
GM
8677 }
8678
8679 if (r < 0 || g < 0 || b < 0)
8680 {
333b20bb
GM
8681 xfree (ximg->data);
8682 ximg->data = NULL;
8683 XDestroyImage (ximg);
45158a91
GM
8684 image_error ("Invalid pixel value in image `%s'",
8685 img->spec, Qnil);
63cec32f 8686 goto error;
333b20bb
GM
8687 }
8688
8689 /* RGB values are now in the range 0..max_color_idx.
8690 Scale this to the range 0..0xffff supported by X. */
8691 r = (double) r * 65535 / max_color_idx;
8692 g = (double) g * 65535 / max_color_idx;
8693 b = (double) b * 65535 / max_color_idx;
8694 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8695 }
8696 }
8697
333b20bb
GM
8698 /* Store in IMG->colors the colors allocated for the image, and
8699 free the color table. */
8700 img->colors = colors_in_color_table (&img->ncolors);
8701 free_color_table ();
f20a3b7a
MB
8702
8703 /* Maybe fill in the background field while we have ximg handy. */
8704 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8705 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
8706
8707 /* Put the image into a pixmap. */
8708 x_put_x_image (f, ximg, img->pixmap, width, height);
8709 x_destroy_x_image (ximg);
333b20bb
GM
8710
8711 img->width = width;
8712 img->height = height;
8713
8714 UNGCPRO;
63cec32f 8715 xfree (contents);
333b20bb
GM
8716 return 1;
8717}
8718
8719
8720\f
8721/***********************************************************************
8722 PNG
8723 ***********************************************************************/
8724
8725#if HAVE_PNG
8726
8727#include <png.h>
8728
8729/* Function prototypes. */
8730
8731static int png_image_p P_ ((Lisp_Object object));
8732static int png_load P_ ((struct frame *f, struct image *img));
8733
8734/* The symbol `png' identifying images of this type. */
8735
8736Lisp_Object Qpng;
8737
8738/* Indices of image specification fields in png_format, below. */
8739
8740enum png_keyword_index
8741{
8742 PNG_TYPE,
63448a4d 8743 PNG_DATA,
333b20bb
GM
8744 PNG_FILE,
8745 PNG_ASCENT,
8746 PNG_MARGIN,
8747 PNG_RELIEF,
8748 PNG_ALGORITHM,
8749 PNG_HEURISTIC_MASK,
4a8e312c 8750 PNG_MASK,
f20a3b7a 8751 PNG_BACKGROUND,
333b20bb
GM
8752 PNG_LAST
8753};
8754
8755/* Vector of image_keyword structures describing the format
8756 of valid user-defined image specifications. */
8757
8758static struct image_keyword png_format[PNG_LAST] =
8759{
8760 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 8761 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 8762 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 8763 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 8764 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 8765 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 8766 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 8767 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 8768 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a 8769 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
8770};
8771
06482119 8772/* Structure describing the image type `png'. */
333b20bb
GM
8773
8774static struct image_type png_type =
8775{
8776 &Qpng,
8777 png_image_p,
8778 png_load,
8779 x_clear_image,
8780 NULL
8781};
8782
8783
8784/* Return non-zero if OBJECT is a valid PNG image specification. */
8785
8786static int
8787png_image_p (object)
8788 Lisp_Object object;
8789{
8790 struct image_keyword fmt[PNG_LAST];
8791 bcopy (png_format, fmt, sizeof fmt);
8792
7c7ff7f5 8793 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
333b20bb 8794 return 0;
63448a4d 8795
63cec32f
GM
8796 /* Must specify either the :data or :file keyword. */
8797 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
333b20bb
GM
8798}
8799
8800
8801/* Error and warning handlers installed when the PNG library
8802 is initialized. */
8803
8804static void
8805my_png_error (png_ptr, msg)
8806 png_struct *png_ptr;
8807 char *msg;
8808{
8809 xassert (png_ptr != NULL);
8810 image_error ("PNG error: %s", build_string (msg), Qnil);
8811 longjmp (png_ptr->jmpbuf, 1);
8812}
8813
8814
8815static void
8816my_png_warning (png_ptr, msg)
8817 png_struct *png_ptr;
8818 char *msg;
8819{
8820 xassert (png_ptr != NULL);
8821 image_error ("PNG warning: %s", build_string (msg), Qnil);
8822}
8823
5ad6a5fb
GM
8824/* Memory source for PNG decoding. */
8825
63448a4d
WP
8826struct png_memory_storage
8827{
5ad6a5fb
GM
8828 unsigned char *bytes; /* The data */
8829 size_t len; /* How big is it? */
8830 int index; /* Where are we? */
63448a4d
WP
8831};
8832
5ad6a5fb
GM
8833
8834/* Function set as reader function when reading PNG image from memory.
8835 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
8836 bytes from the input to DATA. */
8837
63448a4d 8838static void
5ad6a5fb
GM
8839png_read_from_memory (png_ptr, data, length)
8840 png_structp png_ptr;
8841 png_bytep data;
8842 png_size_t length;
63448a4d 8843{
5ad6a5fb
GM
8844 struct png_memory_storage *tbr
8845 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
63448a4d 8846
5ad6a5fb
GM
8847 if (length > tbr->len - tbr->index)
8848 png_error (png_ptr, "Read error");
8849
8850 bcopy (tbr->bytes + tbr->index, data, length);
8851 tbr->index = tbr->index + length;
63448a4d 8852}
333b20bb
GM
8853
8854/* Load PNG image IMG for use on frame F. Value is non-zero if
8855 successful. */
8856
8857static int
8858png_load (f, img)
8859 struct frame *f;
8860 struct image *img;
8861{
8862 Lisp_Object file, specified_file;
63448a4d 8863 Lisp_Object specified_data;
b6d7acec 8864 int x, y, i;
333b20bb
GM
8865 XImage *ximg, *mask_img = NULL;
8866 struct gcpro gcpro1;
8867 png_struct *png_ptr = NULL;
8868 png_info *info_ptr = NULL, *end_info = NULL;
8ec8a5ec 8869 FILE *volatile fp = NULL;
333b20bb 8870 png_byte sig[8];
8ec8a5ec
GM
8871 png_byte * volatile pixels = NULL;
8872 png_byte ** volatile rows = NULL;
333b20bb
GM
8873 png_uint_32 width, height;
8874 int bit_depth, color_type, interlace_type;
8875 png_byte channels;
8876 png_uint_32 row_bytes;
8877 int transparent_p;
8878 char *gamma_str;
8879 double screen_gamma, image_gamma;
8880 int intent;
63448a4d 8881 struct png_memory_storage tbr; /* Data to be read */
333b20bb
GM
8882
8883 /* Find out what file to load. */
8884 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 8885 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
8886 file = Qnil;
8887 GCPRO1 (file);
333b20bb 8888
63448a4d 8889 if (NILP (specified_data))
5ad6a5fb
GM
8890 {
8891 file = x_find_image_file (specified_file);
8892 if (!STRINGP (file))
63448a4d 8893 {
45158a91 8894 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
8895 UNGCPRO;
8896 return 0;
8897 }
333b20bb 8898
5ad6a5fb
GM
8899 /* Open the image file. */
8900 fp = fopen (XSTRING (file)->data, "rb");
8901 if (!fp)
8902 {
45158a91 8903 image_error ("Cannot open image file `%s'", file, Qnil);
5ad6a5fb
GM
8904 UNGCPRO;
8905 fclose (fp);
8906 return 0;
8907 }
63448a4d 8908
5ad6a5fb
GM
8909 /* Check PNG signature. */
8910 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
8911 || !png_check_sig (sig, sizeof sig))
8912 {
45158a91 8913 image_error ("Not a PNG file: `%s'", file, Qnil);
5ad6a5fb
GM
8914 UNGCPRO;
8915 fclose (fp);
8916 return 0;
63448a4d 8917 }
5ad6a5fb 8918 }
63448a4d 8919 else
5ad6a5fb
GM
8920 {
8921 /* Read from memory. */
8922 tbr.bytes = XSTRING (specified_data)->data;
8923 tbr.len = STRING_BYTES (XSTRING (specified_data));
8924 tbr.index = 0;
63448a4d 8925
5ad6a5fb
GM
8926 /* Check PNG signature. */
8927 if (tbr.len < sizeof sig
8928 || !png_check_sig (tbr.bytes, sizeof sig))
8929 {
45158a91 8930 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
5ad6a5fb
GM
8931 UNGCPRO;
8932 return 0;
63448a4d 8933 }
333b20bb 8934
5ad6a5fb
GM
8935 /* Need to skip past the signature. */
8936 tbr.bytes += sizeof (sig);
8937 }
8938
333b20bb
GM
8939 /* Initialize read and info structs for PNG lib. */
8940 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
8941 my_png_error, my_png_warning);
8942 if (!png_ptr)
8943 {
63448a4d 8944 if (fp) fclose (fp);
333b20bb
GM
8945 UNGCPRO;
8946 return 0;
8947 }
8948
8949 info_ptr = png_create_info_struct (png_ptr);
8950 if (!info_ptr)
8951 {
8952 png_destroy_read_struct (&png_ptr, NULL, NULL);
63448a4d 8953 if (fp) fclose (fp);
333b20bb
GM
8954 UNGCPRO;
8955 return 0;
8956 }
8957
8958 end_info = png_create_info_struct (png_ptr);
8959 if (!end_info)
8960 {
8961 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
63448a4d 8962 if (fp) fclose (fp);
333b20bb
GM
8963 UNGCPRO;
8964 return 0;
8965 }
8966
8967 /* Set error jump-back. We come back here when the PNG library
8968 detects an error. */
8969 if (setjmp (png_ptr->jmpbuf))
8970 {
8971 error:
8972 if (png_ptr)
8973 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8974 xfree (pixels);
8975 xfree (rows);
63448a4d 8976 if (fp) fclose (fp);
333b20bb
GM
8977 UNGCPRO;
8978 return 0;
8979 }
8980
8981 /* Read image info. */
63448a4d 8982 if (!NILP (specified_data))
5ad6a5fb 8983 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
63448a4d 8984 else
5ad6a5fb 8985 png_init_io (png_ptr, fp);
63448a4d 8986
333b20bb
GM
8987 png_set_sig_bytes (png_ptr, sizeof sig);
8988 png_read_info (png_ptr, info_ptr);
8989 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
8990 &interlace_type, NULL, NULL);
8991
8992 /* If image contains simply transparency data, we prefer to
8993 construct a clipping mask. */
8994 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
8995 transparent_p = 1;
8996 else
8997 transparent_p = 0;
8998
8999 /* This function is easier to write if we only have to handle
9000 one data format: RGB or RGBA with 8 bits per channel. Let's
9001 transform other formats into that format. */
9002
9003 /* Strip more than 8 bits per channel. */
9004 if (bit_depth == 16)
9005 png_set_strip_16 (png_ptr);
9006
9007 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
9008 if available. */
9009 png_set_expand (png_ptr);
9010
9011 /* Convert grayscale images to RGB. */
9012 if (color_type == PNG_COLOR_TYPE_GRAY
9013 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9014 png_set_gray_to_rgb (png_ptr);
9015
9016 /* The value 2.2 is a guess for PC monitors from PNG example.c. */
9017 gamma_str = getenv ("SCREEN_GAMMA");
9018 screen_gamma = gamma_str ? atof (gamma_str) : 2.2;
9019
9020 /* Tell the PNG lib to handle gamma correction for us. */
9021
6c1aa34d 9022#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
333b20bb
GM
9023 if (png_get_sRGB (png_ptr, info_ptr, &intent))
9024 /* There is a special chunk in the image specifying the gamma. */
9025 png_set_sRGB (png_ptr, info_ptr, intent);
6c1aa34d
GM
9026 else
9027#endif
9028 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
333b20bb
GM
9029 /* Image contains gamma information. */
9030 png_set_gamma (png_ptr, screen_gamma, image_gamma);
9031 else
9032 /* Use a default of 0.5 for the image gamma. */
9033 png_set_gamma (png_ptr, screen_gamma, 0.5);
9034
9035 /* Handle alpha channel by combining the image with a background
9036 color. Do this only if a real alpha channel is supplied. For
9037 simple transparency, we prefer a clipping mask. */
9038 if (!transparent_p)
9039 {
f20a3b7a
MB
9040 png_color_16 *image_bg;
9041 Lisp_Object specified_bg
9042 = image_spec_value (img->spec, QCbackground, NULL);
9043
f2f0a644 9044 if (STRINGP (specified_bg))
f20a3b7a
MB
9045 /* The user specified `:background', use that. */
9046 {
9047 XColor color;
f2f0a644 9048 if (x_defined_color (f, XSTRING (specified_bg)->data, &color, 0))
f20a3b7a
MB
9049 {
9050 png_color_16 user_bg;
9051
9052 bzero (&user_bg, sizeof user_bg);
9053 user_bg.red = color.red;
9054 user_bg.green = color.green;
9055 user_bg.blue = color.blue;
333b20bb 9056
f20a3b7a
MB
9057 png_set_background (png_ptr, &user_bg,
9058 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
9059 }
9060 }
9061 else if (png_get_bKGD (png_ptr, info_ptr, &image_bg))
333b20bb
GM
9062 /* Image contains a background color with which to
9063 combine the image. */
f20a3b7a 9064 png_set_background (png_ptr, image_bg,
333b20bb
GM
9065 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
9066 else
9067 {
9068 /* Image does not contain a background color with which
9069 to combine the image data via an alpha channel. Use
9070 the frame's background instead. */
9071 XColor color;
9072 Colormap cmap;
9073 png_color_16 frame_background;
9074
9b2956e2 9075 cmap = FRAME_X_COLORMAP (f);
333b20bb 9076 color.pixel = FRAME_BACKGROUND_PIXEL (f);
a31fedb7 9077 x_query_color (f, &color);
333b20bb
GM
9078
9079 bzero (&frame_background, sizeof frame_background);
9080 frame_background.red = color.red;
9081 frame_background.green = color.green;
9082 frame_background.blue = color.blue;
9083
9084 png_set_background (png_ptr, &frame_background,
9085 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
9086 }
9087 }
9088
9089 /* Update info structure. */
9090 png_read_update_info (png_ptr, info_ptr);
9091
9092 /* Get number of channels. Valid values are 1 for grayscale images
9093 and images with a palette, 2 for grayscale images with transparency
9094 information (alpha channel), 3 for RGB images, and 4 for RGB
9095 images with alpha channel, i.e. RGBA. If conversions above were
9096 sufficient we should only have 3 or 4 channels here. */
9097 channels = png_get_channels (png_ptr, info_ptr);
9098 xassert (channels == 3 || channels == 4);
9099
9100 /* Number of bytes needed for one row of the image. */
9101 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
9102
9103 /* Allocate memory for the image. */
9104 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
9105 rows = (png_byte **) xmalloc (height * sizeof *rows);
9106 for (i = 0; i < height; ++i)
9107 rows[i] = pixels + i * row_bytes;
9108
9109 /* Read the entire image. */
9110 png_read_image (png_ptr, rows);
9111 png_read_end (png_ptr, info_ptr);
5ad6a5fb
GM
9112 if (fp)
9113 {
9114 fclose (fp);
9115 fp = NULL;
9116 }
333b20bb 9117
333b20bb 9118 /* Create the X image and pixmap. */
45158a91 9119 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
333b20bb 9120 &img->pixmap))
28c7826c 9121 goto error;
333b20bb
GM
9122
9123 /* Create an image and pixmap serving as mask if the PNG image
9124 contains an alpha channel. */
9125 if (channels == 4
9126 && !transparent_p
45158a91 9127 && !x_create_x_image_and_pixmap (f, width, height, 1,
333b20bb
GM
9128 &mask_img, &img->mask))
9129 {
9130 x_destroy_x_image (ximg);
9131 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
dd00328a 9132 img->pixmap = None;
333b20bb
GM
9133 goto error;
9134 }
9135
9136 /* Fill the X image and mask from PNG data. */
9137 init_color_table ();
9138
9139 for (y = 0; y < height; ++y)
9140 {
9141 png_byte *p = rows[y];
9142
9143 for (x = 0; x < width; ++x)
9144 {
9145 unsigned r, g, b;
9146
9147 r = *p++ << 8;
9148 g = *p++ << 8;
9149 b = *p++ << 8;
9150 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
9151
9152 /* An alpha channel, aka mask channel, associates variable
9153 transparency with an image. Where other image formats
9154 support binary transparency---fully transparent or fully
9155 opaque---PNG allows up to 254 levels of partial transparency.
9156 The PNG library implements partial transparency by combining
9157 the image with a specified background color.
9158
9159 I'm not sure how to handle this here nicely: because the
9160 background on which the image is displayed may change, for
9161 real alpha channel support, it would be necessary to create
9162 a new image for each possible background.
9163
9164 What I'm doing now is that a mask is created if we have
9165 boolean transparency information. Otherwise I'm using
9166 the frame's background color to combine the image with. */
9167
9168 if (channels == 4)
9169 {
9170 if (mask_img)
9171 XPutPixel (mask_img, x, y, *p > 0);
9172 ++p;
9173 }
9174 }
9175 }
9176
f20a3b7a
MB
9177 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9178 /* Set IMG's background color from the PNG image, unless the user
9179 overrode it. */
9180 {
9181 png_color_16 *bg;
9182 if (png_get_bKGD (png_ptr, info_ptr, &bg))
9183 {
f2f0a644 9184 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
f20a3b7a
MB
9185 img->background_valid = 1;
9186 }
9187 }
9188
333b20bb
GM
9189 /* Remember colors allocated for this image. */
9190 img->colors = colors_in_color_table (&img->ncolors);
9191 free_color_table ();
9192
9193 /* Clean up. */
9194 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
9195 xfree (rows);
9196 xfree (pixels);
9197
9198 img->width = width;
9199 img->height = height;
9200
f20a3b7a
MB
9201 /* Maybe fill in the background field while we have ximg handy. */
9202 IMAGE_BACKGROUND (img, f, ximg);
9203
333b20bb
GM
9204 /* Put the image into the pixmap, then free the X image and its buffer. */
9205 x_put_x_image (f, ximg, img->pixmap, width, height);
9206 x_destroy_x_image (ximg);
9207
9208 /* Same for the mask. */
9209 if (mask_img)
9210 {
f20a3b7a
MB
9211 /* Fill in the background_transparent field while we have the mask
9212 handy. */
9213 image_background_transparent (img, f, mask_img);
9214
333b20bb
GM
9215 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
9216 x_destroy_x_image (mask_img);
9217 }
9218
333b20bb
GM
9219 UNGCPRO;
9220 return 1;
9221}
9222
9223#endif /* HAVE_PNG != 0 */
9224
9225
9226\f
9227/***********************************************************************
9228 JPEG
9229 ***********************************************************************/
9230
9231#if HAVE_JPEG
9232
ba06aba4
GM
9233/* Work around a warning about HAVE_STDLIB_H being redefined in
9234 jconfig.h. */
9235#ifdef HAVE_STDLIB_H
9236#define HAVE_STDLIB_H_1
9237#undef HAVE_STDLIB_H
9238#endif /* HAVE_STLIB_H */
9239
333b20bb
GM
9240#include <jpeglib.h>
9241#include <jerror.h>
9242#include <setjmp.h>
9243
ba06aba4
GM
9244#ifdef HAVE_STLIB_H_1
9245#define HAVE_STDLIB_H 1
9246#endif
9247
333b20bb
GM
9248static int jpeg_image_p P_ ((Lisp_Object object));
9249static int jpeg_load P_ ((struct frame *f, struct image *img));
9250
9251/* The symbol `jpeg' identifying images of this type. */
9252
9253Lisp_Object Qjpeg;
9254
9255/* Indices of image specification fields in gs_format, below. */
9256
9257enum jpeg_keyword_index
9258{
9259 JPEG_TYPE,
8e39770a 9260 JPEG_DATA,
333b20bb
GM
9261 JPEG_FILE,
9262 JPEG_ASCENT,
9263 JPEG_MARGIN,
9264 JPEG_RELIEF,
9265 JPEG_ALGORITHM,
9266 JPEG_HEURISTIC_MASK,
4a8e312c 9267 JPEG_MASK,
f20a3b7a 9268 JPEG_BACKGROUND,
333b20bb
GM
9269 JPEG_LAST
9270};
9271
9272/* Vector of image_keyword structures describing the format
9273 of valid user-defined image specifications. */
9274
9275static struct image_keyword jpeg_format[JPEG_LAST] =
9276{
9277 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9278 {":data", IMAGE_STRING_VALUE, 0},
8e39770a 9279 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9280 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9281 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9282 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9283 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9284 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9285 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9286 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9287};
9288
9289/* Structure describing the image type `jpeg'. */
9290
9291static struct image_type jpeg_type =
9292{
9293 &Qjpeg,
9294 jpeg_image_p,
9295 jpeg_load,
9296 x_clear_image,
9297 NULL
9298};
9299
9300
9301/* Return non-zero if OBJECT is a valid JPEG image specification. */
9302
9303static int
9304jpeg_image_p (object)
9305 Lisp_Object object;
9306{
9307 struct image_keyword fmt[JPEG_LAST];
9308
9309 bcopy (jpeg_format, fmt, sizeof fmt);
9310
7c7ff7f5 9311 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
333b20bb 9312 return 0;
8e39770a 9313
63cec32f
GM
9314 /* Must specify either the :data or :file keyword. */
9315 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
333b20bb
GM
9316}
9317
8e39770a 9318
333b20bb
GM
9319struct my_jpeg_error_mgr
9320{
9321 struct jpeg_error_mgr pub;
9322 jmp_buf setjmp_buffer;
9323};
9324
e3130015 9325
333b20bb
GM
9326static void
9327my_error_exit (cinfo)
9328 j_common_ptr cinfo;
9329{
9330 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
9331 longjmp (mgr->setjmp_buffer, 1);
9332}
9333
e3130015 9334
8e39770a
GM
9335/* Init source method for JPEG data source manager. Called by
9336 jpeg_read_header() before any data is actually read. See
9337 libjpeg.doc from the JPEG lib distribution. */
9338
9339static void
9340our_init_source (cinfo)
9341 j_decompress_ptr cinfo;
9342{
9343}
9344
9345
9346/* Fill input buffer method for JPEG data source manager. Called
9347 whenever more data is needed. We read the whole image in one step,
9348 so this only adds a fake end of input marker at the end. */
9349
9350static boolean
9351our_fill_input_buffer (cinfo)
9352 j_decompress_ptr cinfo;
9353{
9354 /* Insert a fake EOI marker. */
9355 struct jpeg_source_mgr *src = cinfo->src;
9356 static JOCTET buffer[2];
9357
9358 buffer[0] = (JOCTET) 0xFF;
9359 buffer[1] = (JOCTET) JPEG_EOI;
9360
9361 src->next_input_byte = buffer;
9362 src->bytes_in_buffer = 2;
9363 return TRUE;
9364}
9365
9366
9367/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
9368 is the JPEG data source manager. */
9369
9370static void
9371our_skip_input_data (cinfo, num_bytes)
9372 j_decompress_ptr cinfo;
9373 long num_bytes;
9374{
9375 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
9376
9377 if (src)
9378 {
9379 if (num_bytes > src->bytes_in_buffer)
5ad6a5fb 9380 ERREXIT (cinfo, JERR_INPUT_EOF);
8e39770a
GM
9381
9382 src->bytes_in_buffer -= num_bytes;
9383 src->next_input_byte += num_bytes;
9384 }
9385}
9386
9387
9388/* Method to terminate data source. Called by
9389 jpeg_finish_decompress() after all data has been processed. */
9390
9391static void
9392our_term_source (cinfo)
9393 j_decompress_ptr cinfo;
9394{
9395}
9396
9397
9398/* Set up the JPEG lib for reading an image from DATA which contains
9399 LEN bytes. CINFO is the decompression info structure created for
9400 reading the image. */
9401
9402static void
9403jpeg_memory_src (cinfo, data, len)
9404 j_decompress_ptr cinfo;
9405 JOCTET *data;
9406 unsigned int len;
9407{
9408 struct jpeg_source_mgr *src;
9409
9410 if (cinfo->src == NULL)
9411 {
9412 /* First time for this JPEG object? */
9413 cinfo->src = (struct jpeg_source_mgr *)
9414 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
9415 sizeof (struct jpeg_source_mgr));
9416 src = (struct jpeg_source_mgr *) cinfo->src;
9417 src->next_input_byte = data;
9418 }
9419
9420 src = (struct jpeg_source_mgr *) cinfo->src;
9421 src->init_source = our_init_source;
9422 src->fill_input_buffer = our_fill_input_buffer;
9423 src->skip_input_data = our_skip_input_data;
9424 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
9425 src->term_source = our_term_source;
9426 src->bytes_in_buffer = len;
9427 src->next_input_byte = data;
9428}
9429
5ad6a5fb 9430
333b20bb
GM
9431/* Load image IMG for use on frame F. Patterned after example.c
9432 from the JPEG lib. */
9433
9434static int
9435jpeg_load (f, img)
9436 struct frame *f;
9437 struct image *img;
9438{
9439 struct jpeg_decompress_struct cinfo;
9440 struct my_jpeg_error_mgr mgr;
9441 Lisp_Object file, specified_file;
8e39770a 9442 Lisp_Object specified_data;
8ec8a5ec 9443 FILE * volatile fp = NULL;
333b20bb
GM
9444 JSAMPARRAY buffer;
9445 int row_stride, x, y;
9446 XImage *ximg = NULL;
b6d7acec 9447 int rc;
333b20bb
GM
9448 unsigned long *colors;
9449 int width, height;
9450 struct gcpro gcpro1;
9451
9452 /* Open the JPEG file. */
9453 specified_file = image_spec_value (img->spec, QCfile, NULL);
8e39770a 9454 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
9455 file = Qnil;
9456 GCPRO1 (file);
8e39770a 9457
8e39770a 9458 if (NILP (specified_data))
333b20bb 9459 {
8e39770a 9460 file = x_find_image_file (specified_file);
8e39770a
GM
9461 if (!STRINGP (file))
9462 {
45158a91 9463 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8e39770a
GM
9464 UNGCPRO;
9465 return 0;
9466 }
333b20bb 9467
8e39770a
GM
9468 fp = fopen (XSTRING (file)->data, "r");
9469 if (fp == NULL)
9470 {
9471 image_error ("Cannot open `%s'", file, Qnil);
9472 UNGCPRO;
9473 return 0;
9474 }
333b20bb
GM
9475 }
9476
5ad6a5fb
GM
9477 /* Customize libjpeg's error handling to call my_error_exit when an
9478 error is detected. This function will perform a longjmp. */
333b20bb 9479 cinfo.err = jpeg_std_error (&mgr.pub);
14358466 9480 mgr.pub.error_exit = my_error_exit;
333b20bb
GM
9481
9482 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
9483 {
5ad6a5fb
GM
9484 if (rc == 1)
9485 {
9486 /* Called from my_error_exit. Display a JPEG error. */
9487 char buffer[JMSG_LENGTH_MAX];
9488 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
45158a91 9489 image_error ("Error reading JPEG image `%s': %s", img->spec,
5ad6a5fb
GM
9490 build_string (buffer));
9491 }
333b20bb
GM
9492
9493 /* Close the input file and destroy the JPEG object. */
5ad6a5fb 9494 if (fp)
8ec8a5ec 9495 fclose ((FILE *) fp);
333b20bb
GM
9496 jpeg_destroy_decompress (&cinfo);
9497
5ad6a5fb
GM
9498 /* If we already have an XImage, free that. */
9499 x_destroy_x_image (ximg);
333b20bb 9500
5ad6a5fb
GM
9501 /* Free pixmap and colors. */
9502 x_clear_image (f, img);
333b20bb 9503
5ad6a5fb
GM
9504 UNGCPRO;
9505 return 0;
333b20bb
GM
9506 }
9507
9508 /* Create the JPEG decompression object. Let it read from fp.
63448a4d 9509 Read the JPEG image header. */
333b20bb 9510 jpeg_create_decompress (&cinfo);
8e39770a
GM
9511
9512 if (NILP (specified_data))
8ec8a5ec 9513 jpeg_stdio_src (&cinfo, (FILE *) fp);
8e39770a
GM
9514 else
9515 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
9516 STRING_BYTES (XSTRING (specified_data)));
63448a4d 9517
333b20bb
GM
9518 jpeg_read_header (&cinfo, TRUE);
9519
9520 /* Customize decompression so that color quantization will be used.
63448a4d 9521 Start decompression. */
333b20bb
GM
9522 cinfo.quantize_colors = TRUE;
9523 jpeg_start_decompress (&cinfo);
9524 width = img->width = cinfo.output_width;
9525 height = img->height = cinfo.output_height;
9526
333b20bb 9527 /* Create X image and pixmap. */
45158a91 9528 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
28c7826c 9529 longjmp (mgr.setjmp_buffer, 2);
333b20bb
GM
9530
9531 /* Allocate colors. When color quantization is used,
5ad6a5fb
GM
9532 cinfo.actual_number_of_colors has been set with the number of
9533 colors generated, and cinfo.colormap is a two-dimensional array
9534 of color indices in the range 0..cinfo.actual_number_of_colors.
9535 No more than 255 colors will be generated. */
333b20bb 9536 {
5ad6a5fb
GM
9537 int i, ir, ig, ib;
9538
9539 if (cinfo.out_color_components > 2)
9540 ir = 0, ig = 1, ib = 2;
9541 else if (cinfo.out_color_components > 1)
9542 ir = 0, ig = 1, ib = 0;
9543 else
9544 ir = 0, ig = 0, ib = 0;
9545
9546 /* Use the color table mechanism because it handles colors that
9547 cannot be allocated nicely. Such colors will be replaced with
9548 a default color, and we don't have to care about which colors
9549 can be freed safely, and which can't. */
9550 init_color_table ();
9551 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
9552 * sizeof *colors);
333b20bb 9553
5ad6a5fb
GM
9554 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
9555 {
9556 /* Multiply RGB values with 255 because X expects RGB values
9557 in the range 0..0xffff. */
9558 int r = cinfo.colormap[ir][i] << 8;
9559 int g = cinfo.colormap[ig][i] << 8;
9560 int b = cinfo.colormap[ib][i] << 8;
9561 colors[i] = lookup_rgb_color (f, r, g, b);
9562 }
333b20bb 9563
5ad6a5fb
GM
9564 /* Remember those colors actually allocated. */
9565 img->colors = colors_in_color_table (&img->ncolors);
9566 free_color_table ();
333b20bb
GM
9567 }
9568
9569 /* Read pixels. */
9570 row_stride = width * cinfo.output_components;
9571 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
5ad6a5fb 9572 row_stride, 1);
333b20bb
GM
9573 for (y = 0; y < height; ++y)
9574 {
5ad6a5fb
GM
9575 jpeg_read_scanlines (&cinfo, buffer, 1);
9576 for (x = 0; x < cinfo.output_width; ++x)
9577 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
333b20bb
GM
9578 }
9579
9580 /* Clean up. */
9581 jpeg_finish_decompress (&cinfo);
9582 jpeg_destroy_decompress (&cinfo);
5ad6a5fb 9583 if (fp)
8ec8a5ec 9584 fclose ((FILE *) fp);
f20a3b7a
MB
9585
9586 /* Maybe fill in the background field while we have ximg handy. */
9587 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9588 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
9589
9590 /* Put the image into the pixmap. */
9591 x_put_x_image (f, ximg, img->pixmap, width, height);
9592 x_destroy_x_image (ximg);
333b20bb
GM
9593 UNGCPRO;
9594 return 1;
9595}
9596
9597#endif /* HAVE_JPEG */
9598
9599
9600\f
9601/***********************************************************************
9602 TIFF
9603 ***********************************************************************/
9604
9605#if HAVE_TIFF
9606
cf4790ad 9607#include <tiffio.h>
333b20bb
GM
9608
9609static int tiff_image_p P_ ((Lisp_Object object));
9610static int tiff_load P_ ((struct frame *f, struct image *img));
9611
9612/* The symbol `tiff' identifying images of this type. */
9613
9614Lisp_Object Qtiff;
9615
9616/* Indices of image specification fields in tiff_format, below. */
9617
9618enum tiff_keyword_index
9619{
9620 TIFF_TYPE,
63448a4d 9621 TIFF_DATA,
333b20bb
GM
9622 TIFF_FILE,
9623 TIFF_ASCENT,
9624 TIFF_MARGIN,
9625 TIFF_RELIEF,
9626 TIFF_ALGORITHM,
9627 TIFF_HEURISTIC_MASK,
4a8e312c 9628 TIFF_MASK,
f20a3b7a 9629 TIFF_BACKGROUND,
333b20bb
GM
9630 TIFF_LAST
9631};
9632
9633/* Vector of image_keyword structures describing the format
9634 of valid user-defined image specifications. */
9635
9636static struct image_keyword tiff_format[TIFF_LAST] =
9637{
9638 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9639 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 9640 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9641 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9642 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9643 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9644 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9645 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9646 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9647 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9648};
9649
9650/* Structure describing the image type `tiff'. */
9651
9652static struct image_type tiff_type =
9653{
9654 &Qtiff,
9655 tiff_image_p,
9656 tiff_load,
9657 x_clear_image,
9658 NULL
9659};
9660
9661
9662/* Return non-zero if OBJECT is a valid TIFF image specification. */
9663
9664static int
9665tiff_image_p (object)
9666 Lisp_Object object;
9667{
9668 struct image_keyword fmt[TIFF_LAST];
9669 bcopy (tiff_format, fmt, sizeof fmt);
9670
7c7ff7f5 9671 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
333b20bb 9672 return 0;
5ad6a5fb 9673
63cec32f
GM
9674 /* Must specify either the :data or :file keyword. */
9675 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
333b20bb
GM
9676}
9677
5ad6a5fb
GM
9678
9679/* Reading from a memory buffer for TIFF images Based on the PNG
9680 memory source, but we have to provide a lot of extra functions.
9681 Blah.
63448a4d
WP
9682
9683 We really only need to implement read and seek, but I am not
9684 convinced that the TIFF library is smart enough not to destroy
9685 itself if we only hand it the function pointers we need to
5ad6a5fb
GM
9686 override. */
9687
9688typedef struct
9689{
63448a4d
WP
9690 unsigned char *bytes;
9691 size_t len;
9692 int index;
5ad6a5fb
GM
9693}
9694tiff_memory_source;
63448a4d 9695
e3130015 9696
5ad6a5fb
GM
9697static size_t
9698tiff_read_from_memory (data, buf, size)
9699 thandle_t data;
9700 tdata_t buf;
9701 tsize_t size;
63448a4d 9702{
5ad6a5fb 9703 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
9704
9705 if (size > src->len - src->index)
5ad6a5fb
GM
9706 return (size_t) -1;
9707 bcopy (src->bytes + src->index, buf, size);
63448a4d
WP
9708 src->index += size;
9709 return size;
9710}
9711
e3130015 9712
5ad6a5fb
GM
9713static size_t
9714tiff_write_from_memory (data, buf, size)
9715 thandle_t data;
9716 tdata_t buf;
9717 tsize_t size;
63448a4d
WP
9718{
9719 return (size_t) -1;
9720}
9721
e3130015 9722
5ad6a5fb
GM
9723static toff_t
9724tiff_seek_in_memory (data, off, whence)
9725 thandle_t data;
9726 toff_t off;
9727 int whence;
63448a4d 9728{
5ad6a5fb 9729 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
9730 int idx;
9731
9732 switch (whence)
5ad6a5fb
GM
9733 {
9734 case SEEK_SET: /* Go from beginning of source. */
9735 idx = off;
9736 break;
9737
9738 case SEEK_END: /* Go from end of source. */
9739 idx = src->len + off;
9740 break;
9741
9742 case SEEK_CUR: /* Go from current position. */
9743 idx = src->index + off;
9744 break;
9745
9746 default: /* Invalid `whence'. */
9747 return -1;
9748 }
9749
9750 if (idx > src->len || idx < 0)
9751 return -1;
9752
63448a4d
WP
9753 src->index = idx;
9754 return src->index;
9755}
9756
e3130015 9757
5ad6a5fb
GM
9758static int
9759tiff_close_memory (data)
9760 thandle_t data;
63448a4d
WP
9761{
9762 /* NOOP */
5ad6a5fb 9763 return 0;
63448a4d
WP
9764}
9765
e3130015 9766
5ad6a5fb
GM
9767static int
9768tiff_mmap_memory (data, pbase, psize)
9769 thandle_t data;
9770 tdata_t *pbase;
9771 toff_t *psize;
63448a4d
WP
9772{
9773 /* It is already _IN_ memory. */
5ad6a5fb 9774 return 0;
63448a4d
WP
9775}
9776
e3130015 9777
5ad6a5fb
GM
9778static void
9779tiff_unmap_memory (data, base, size)
9780 thandle_t data;
9781 tdata_t base;
9782 toff_t size;
63448a4d
WP
9783{
9784 /* We don't need to do this. */
63448a4d
WP
9785}
9786
e3130015 9787
5ad6a5fb
GM
9788static toff_t
9789tiff_size_of_memory (data)
9790 thandle_t data;
63448a4d 9791{
5ad6a5fb 9792 return ((tiff_memory_source *) data)->len;
63448a4d 9793}
333b20bb 9794
e3130015 9795
c6892044
GM
9796static void
9797tiff_error_handler (title, format, ap)
9798 const char *title, *format;
9799 va_list ap;
9800{
9801 char buf[512];
9802 int len;
9803
9804 len = sprintf (buf, "TIFF error: %s ", title);
9805 vsprintf (buf + len, format, ap);
9806 add_to_log (buf, Qnil, Qnil);
9807}
9808
9809
9810static void
9811tiff_warning_handler (title, format, ap)
9812 const char *title, *format;
9813 va_list ap;
9814{
9815 char buf[512];
9816 int len;
9817
9818 len = sprintf (buf, "TIFF warning: %s ", title);
9819 vsprintf (buf + len, format, ap);
9820 add_to_log (buf, Qnil, Qnil);
9821}
9822
9823
333b20bb
GM
9824/* Load TIFF image IMG for use on frame F. Value is non-zero if
9825 successful. */
9826
9827static int
9828tiff_load (f, img)
9829 struct frame *f;
9830 struct image *img;
9831{
9832 Lisp_Object file, specified_file;
63448a4d 9833 Lisp_Object specified_data;
333b20bb
GM
9834 TIFF *tiff;
9835 int width, height, x, y;
9836 uint32 *buf;
9837 int rc;
9838 XImage *ximg;
9839 struct gcpro gcpro1;
63448a4d 9840 tiff_memory_source memsrc;
333b20bb
GM
9841
9842 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 9843 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
9844 file = Qnil;
9845 GCPRO1 (file);
63448a4d 9846
c6892044
GM
9847 TIFFSetErrorHandler (tiff_error_handler);
9848 TIFFSetWarningHandler (tiff_warning_handler);
9849
63448a4d 9850 if (NILP (specified_data))
5ad6a5fb
GM
9851 {
9852 /* Read from a file */
9853 file = x_find_image_file (specified_file);
9854 if (!STRINGP (file))
63448a4d 9855 {
45158a91 9856 image_error ("Cannot find image file `%s'", file, Qnil);
5ad6a5fb
GM
9857 UNGCPRO;
9858 return 0;
9859 }
63448a4d 9860
5ad6a5fb
GM
9861 /* Try to open the image file. */
9862 tiff = TIFFOpen (XSTRING (file)->data, "r");
9863 if (tiff == NULL)
9864 {
9865 image_error ("Cannot open `%s'", file, Qnil);
9866 UNGCPRO;
9867 return 0;
63448a4d 9868 }
5ad6a5fb 9869 }
63448a4d 9870 else
5ad6a5fb
GM
9871 {
9872 /* Memory source! */
9873 memsrc.bytes = XSTRING (specified_data)->data;
9874 memsrc.len = STRING_BYTES (XSTRING (specified_data));
9875 memsrc.index = 0;
9876
9877 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
9878 (TIFFReadWriteProc) tiff_read_from_memory,
9879 (TIFFReadWriteProc) tiff_write_from_memory,
9880 tiff_seek_in_memory,
9881 tiff_close_memory,
9882 tiff_size_of_memory,
9883 tiff_mmap_memory,
9884 tiff_unmap_memory);
9885
9886 if (!tiff)
63448a4d 9887 {
45158a91 9888 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
5ad6a5fb
GM
9889 UNGCPRO;
9890 return 0;
63448a4d 9891 }
5ad6a5fb 9892 }
333b20bb
GM
9893
9894 /* Get width and height of the image, and allocate a raster buffer
9895 of width x height 32-bit values. */
9896 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
9897 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
9898 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
9899
9900 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
9901 TIFFClose (tiff);
9902 if (!rc)
9903 {
45158a91 9904 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
333b20bb
GM
9905 xfree (buf);
9906 UNGCPRO;
9907 return 0;
9908 }
9909
333b20bb 9910 /* Create the X image and pixmap. */
45158a91 9911 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 9912 {
333b20bb
GM
9913 xfree (buf);
9914 UNGCPRO;
9915 return 0;
9916 }
9917
9918 /* Initialize the color table. */
9919 init_color_table ();
9920
9921 /* Process the pixel raster. Origin is in the lower-left corner. */
9922 for (y = 0; y < height; ++y)
9923 {
9924 uint32 *row = buf + y * width;
9925
9926 for (x = 0; x < width; ++x)
9927 {
9928 uint32 abgr = row[x];
9929 int r = TIFFGetR (abgr) << 8;
9930 int g = TIFFGetG (abgr) << 8;
9931 int b = TIFFGetB (abgr) << 8;
9932 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
9933 }
9934 }
9935
9936 /* Remember the colors allocated for the image. Free the color table. */
9937 img->colors = colors_in_color_table (&img->ncolors);
9938 free_color_table ();
f20a3b7a
MB
9939
9940 img->width = width;
9941 img->height = height;
9942
9943 /* Maybe fill in the background field while we have ximg handy. */
9944 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9945 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
9946
9947 /* Put the image into the pixmap, then free the X image and its buffer. */
9948 x_put_x_image (f, ximg, img->pixmap, width, height);
9949 x_destroy_x_image (ximg);
9950 xfree (buf);
333b20bb
GM
9951
9952 UNGCPRO;
9953 return 1;
9954}
9955
9956#endif /* HAVE_TIFF != 0 */
9957
9958
9959\f
9960/***********************************************************************
9961 GIF
9962 ***********************************************************************/
9963
9964#if HAVE_GIF
9965
9966#include <gif_lib.h>
9967
9968static int gif_image_p P_ ((Lisp_Object object));
9969static int gif_load P_ ((struct frame *f, struct image *img));
9970
9971/* The symbol `gif' identifying images of this type. */
9972
9973Lisp_Object Qgif;
9974
9975/* Indices of image specification fields in gif_format, below. */
9976
9977enum gif_keyword_index
9978{
9979 GIF_TYPE,
63448a4d 9980 GIF_DATA,
333b20bb
GM
9981 GIF_FILE,
9982 GIF_ASCENT,
9983 GIF_MARGIN,
9984 GIF_RELIEF,
9985 GIF_ALGORITHM,
9986 GIF_HEURISTIC_MASK,
4a8e312c 9987 GIF_MASK,
333b20bb 9988 GIF_IMAGE,
f20a3b7a 9989 GIF_BACKGROUND,
333b20bb
GM
9990 GIF_LAST
9991};
9992
9993/* Vector of image_keyword structures describing the format
9994 of valid user-defined image specifications. */
9995
9996static struct image_keyword gif_format[GIF_LAST] =
9997{
9998 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9999 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 10000 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 10001 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 10002 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 10003 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 10004 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 10005 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 10006 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 10007 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
f20a3b7a 10008 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
10009};
10010
10011/* Structure describing the image type `gif'. */
10012
10013static struct image_type gif_type =
10014{
10015 &Qgif,
10016 gif_image_p,
10017 gif_load,
10018 x_clear_image,
10019 NULL
10020};
10021
e3130015 10022
333b20bb
GM
10023/* Return non-zero if OBJECT is a valid GIF image specification. */
10024
10025static int
10026gif_image_p (object)
10027 Lisp_Object object;
10028{
10029 struct image_keyword fmt[GIF_LAST];
10030 bcopy (gif_format, fmt, sizeof fmt);
10031
7c7ff7f5 10032 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
333b20bb 10033 return 0;
5ad6a5fb 10034
63cec32f
GM
10035 /* Must specify either the :data or :file keyword. */
10036 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
333b20bb
GM
10037}
10038
e3130015 10039
63448a4d
WP
10040/* Reading a GIF image from memory
10041 Based on the PNG memory stuff to a certain extent. */
10042
5ad6a5fb
GM
10043typedef struct
10044{
63448a4d
WP
10045 unsigned char *bytes;
10046 size_t len;
10047 int index;
5ad6a5fb
GM
10048}
10049gif_memory_source;
63448a4d 10050
e3130015 10051
f036834a
GM
10052/* Make the current memory source available to gif_read_from_memory.
10053 It's done this way because not all versions of libungif support
10054 a UserData field in the GifFileType structure. */
10055static gif_memory_source *current_gif_memory_src;
10056
5ad6a5fb
GM
10057static int
10058gif_read_from_memory (file, buf, len)
10059 GifFileType *file;
10060 GifByteType *buf;
10061 int len;
63448a4d 10062{
f036834a 10063 gif_memory_source *src = current_gif_memory_src;
63448a4d 10064
5ad6a5fb
GM
10065 if (len > src->len - src->index)
10066 return -1;
63448a4d 10067
5ad6a5fb 10068 bcopy (src->bytes + src->index, buf, len);
63448a4d
WP
10069 src->index += len;
10070 return len;
10071}
333b20bb 10072
5ad6a5fb 10073
333b20bb
GM
10074/* Load GIF image IMG for use on frame F. Value is non-zero if
10075 successful. */
10076
10077static int
10078gif_load (f, img)
10079 struct frame *f;
10080 struct image *img;
10081{
10082 Lisp_Object file, specified_file;
63448a4d 10083 Lisp_Object specified_data;
333b20bb
GM
10084 int rc, width, height, x, y, i;
10085 XImage *ximg;
10086 ColorMapObject *gif_color_map;
10087 unsigned long pixel_colors[256];
10088 GifFileType *gif;
10089 struct gcpro gcpro1;
10090 Lisp_Object image;
10091 int ino, image_left, image_top, image_width, image_height;
63448a4d 10092 gif_memory_source memsrc;
9b784e96 10093 unsigned char *raster;
333b20bb
GM
10094
10095 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 10096 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
10097 file = Qnil;
10098 GCPRO1 (file);
63448a4d
WP
10099
10100 if (NILP (specified_data))
5ad6a5fb
GM
10101 {
10102 file = x_find_image_file (specified_file);
10103 if (!STRINGP (file))
63448a4d 10104 {
45158a91 10105 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
10106 UNGCPRO;
10107 return 0;
10108 }
333b20bb 10109
5ad6a5fb
GM
10110 /* Open the GIF file. */
10111 gif = DGifOpenFileName (XSTRING (file)->data);
10112 if (gif == NULL)
10113 {
10114 image_error ("Cannot open `%s'", file, Qnil);
10115 UNGCPRO;
10116 return 0;
63448a4d 10117 }
5ad6a5fb 10118 }
63448a4d 10119 else
5ad6a5fb
GM
10120 {
10121 /* Read from memory! */
f036834a 10122 current_gif_memory_src = &memsrc;
5ad6a5fb
GM
10123 memsrc.bytes = XSTRING (specified_data)->data;
10124 memsrc.len = STRING_BYTES (XSTRING (specified_data));
10125 memsrc.index = 0;
63448a4d 10126
5ad6a5fb
GM
10127 gif = DGifOpen(&memsrc, gif_read_from_memory);
10128 if (!gif)
10129 {
45158a91 10130 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
5ad6a5fb
GM
10131 UNGCPRO;
10132 return 0;
63448a4d 10133 }
5ad6a5fb 10134 }
333b20bb
GM
10135
10136 /* Read entire contents. */
10137 rc = DGifSlurp (gif);
10138 if (rc == GIF_ERROR)
10139 {
45158a91 10140 image_error ("Error reading `%s'", img->spec, Qnil);
333b20bb
GM
10141 DGifCloseFile (gif);
10142 UNGCPRO;
10143 return 0;
10144 }
10145
3ccff1e3 10146 image = image_spec_value (img->spec, QCindex, NULL);
333b20bb
GM
10147 ino = INTEGERP (image) ? XFASTINT (image) : 0;
10148 if (ino >= gif->ImageCount)
10149 {
45158a91
GM
10150 image_error ("Invalid image number `%s' in image `%s'",
10151 image, img->spec);
333b20bb
GM
10152 DGifCloseFile (gif);
10153 UNGCPRO;
10154 return 0;
10155 }
10156
c7f07c4c
PJ
10157 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
10158 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
333b20bb 10159
333b20bb 10160 /* Create the X image and pixmap. */
45158a91 10161 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 10162 {
333b20bb
GM
10163 DGifCloseFile (gif);
10164 UNGCPRO;
10165 return 0;
10166 }
10167
10168 /* Allocate colors. */
10169 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
10170 if (!gif_color_map)
10171 gif_color_map = gif->SColorMap;
10172 init_color_table ();
10173 bzero (pixel_colors, sizeof pixel_colors);
10174
10175 for (i = 0; i < gif_color_map->ColorCount; ++i)
10176 {
10177 int r = gif_color_map->Colors[i].Red << 8;
10178 int g = gif_color_map->Colors[i].Green << 8;
10179 int b = gif_color_map->Colors[i].Blue << 8;
10180 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
10181 }
10182
10183 img->colors = colors_in_color_table (&img->ncolors);
10184 free_color_table ();
10185
10186 /* Clear the part of the screen image that are not covered by
10187 the image from the GIF file. Full animated GIF support
10188 requires more than can be done here (see the gif89 spec,
10189 disposal methods). Let's simply assume that the part
10190 not covered by a sub-image is in the frame's background color. */
10191 image_top = gif->SavedImages[ino].ImageDesc.Top;
10192 image_left = gif->SavedImages[ino].ImageDesc.Left;
10193 image_width = gif->SavedImages[ino].ImageDesc.Width;
10194 image_height = gif->SavedImages[ino].ImageDesc.Height;
10195
10196 for (y = 0; y < image_top; ++y)
10197 for (x = 0; x < width; ++x)
10198 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10199
10200 for (y = image_top + image_height; y < height; ++y)
10201 for (x = 0; x < width; ++x)
10202 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10203
10204 for (y = image_top; y < image_top + image_height; ++y)
10205 {
10206 for (x = 0; x < image_left; ++x)
10207 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10208 for (x = image_left + image_width; x < width; ++x)
10209 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10210 }
10211
9b784e96
GM
10212 /* Read the GIF image into the X image. We use a local variable
10213 `raster' here because RasterBits below is a char *, and invites
10214 problems with bytes >= 0x80. */
10215 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
10216
333b20bb
GM
10217 if (gif->SavedImages[ino].ImageDesc.Interlace)
10218 {
10219 static int interlace_start[] = {0, 4, 2, 1};
10220 static int interlace_increment[] = {8, 8, 4, 2};
9b207e8e 10221 int pass;
06482119
GM
10222 int row = interlace_start[0];
10223
10224 pass = 0;
333b20bb 10225
06482119 10226 for (y = 0; y < image_height; y++)
333b20bb 10227 {
06482119
GM
10228 if (row >= image_height)
10229 {
10230 row = interlace_start[++pass];
10231 while (row >= image_height)
10232 row = interlace_start[++pass];
10233 }
10234
10235 for (x = 0; x < image_width; x++)
10236 {
9b784e96 10237 int i = raster[(y * image_width) + x];
06482119
GM
10238 XPutPixel (ximg, x + image_left, row + image_top,
10239 pixel_colors[i]);
10240 }
10241
10242 row += interlace_increment[pass];
333b20bb
GM
10243 }
10244 }
10245 else
10246 {
10247 for (y = 0; y < image_height; ++y)
10248 for (x = 0; x < image_width; ++x)
10249 {
9b784e96 10250 int i = raster[y * image_width + x];
333b20bb
GM
10251 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
10252 }
10253 }
10254
10255 DGifCloseFile (gif);
f20a3b7a
MB
10256
10257 /* Maybe fill in the background field while we have ximg handy. */
10258 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
10259 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
10260
10261 /* Put the image into the pixmap, then free the X image and its buffer. */
10262 x_put_x_image (f, ximg, img->pixmap, width, height);
10263 x_destroy_x_image (ximg);
333b20bb
GM
10264
10265 UNGCPRO;
10266 return 1;
10267}
10268
10269#endif /* HAVE_GIF != 0 */
10270
10271
10272\f
10273/***********************************************************************
10274 Ghostscript
10275 ***********************************************************************/
10276
10277static int gs_image_p P_ ((Lisp_Object object));
10278static int gs_load P_ ((struct frame *f, struct image *img));
10279static void gs_clear_image P_ ((struct frame *f, struct image *img));
10280
fcf431dc 10281/* The symbol `postscript' identifying images of this type. */
333b20bb 10282
fcf431dc 10283Lisp_Object Qpostscript;
333b20bb
GM
10284
10285/* Keyword symbols. */
10286
10287Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
10288
10289/* Indices of image specification fields in gs_format, below. */
10290
10291enum gs_keyword_index
10292{
10293 GS_TYPE,
10294 GS_PT_WIDTH,
10295 GS_PT_HEIGHT,
10296 GS_FILE,
10297 GS_LOADER,
10298 GS_BOUNDING_BOX,
10299 GS_ASCENT,
10300 GS_MARGIN,
10301 GS_RELIEF,
10302 GS_ALGORITHM,
10303 GS_HEURISTIC_MASK,
4a8e312c 10304 GS_MASK,
f20a3b7a 10305 GS_BACKGROUND,
333b20bb
GM
10306 GS_LAST
10307};
10308
10309/* Vector of image_keyword structures describing the format
10310 of valid user-defined image specifications. */
10311
10312static struct image_keyword gs_format[GS_LAST] =
10313{
10314 {":type", IMAGE_SYMBOL_VALUE, 1},
10315 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10316 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10317 {":file", IMAGE_STRING_VALUE, 1},
10318 {":loader", IMAGE_FUNCTION_VALUE, 0},
10319 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
7c7ff7f5 10320 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 10321 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 10322 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 10323 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 10324 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
10325 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10326 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
10327};
10328
10329/* Structure describing the image type `ghostscript'. */
10330
10331static struct image_type gs_type =
10332{
fcf431dc 10333 &Qpostscript,
333b20bb
GM
10334 gs_image_p,
10335 gs_load,
10336 gs_clear_image,
10337 NULL
10338};
10339
10340
10341/* Free X resources of Ghostscript image IMG which is used on frame F. */
10342
10343static void
10344gs_clear_image (f, img)
10345 struct frame *f;
10346 struct image *img;
10347{
10348 /* IMG->data.ptr_val may contain a recorded colormap. */
10349 xfree (img->data.ptr_val);
10350 x_clear_image (f, img);
10351}
10352
10353
10354/* Return non-zero if OBJECT is a valid Ghostscript image
10355 specification. */
10356
10357static int
10358gs_image_p (object)
10359 Lisp_Object object;
10360{
10361 struct image_keyword fmt[GS_LAST];
10362 Lisp_Object tem;
10363 int i;
10364
10365 bcopy (gs_format, fmt, sizeof fmt);
10366
7c7ff7f5 10367 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
333b20bb
GM
10368 return 0;
10369
10370 /* Bounding box must be a list or vector containing 4 integers. */
10371 tem = fmt[GS_BOUNDING_BOX].value;
10372 if (CONSP (tem))
10373 {
10374 for (i = 0; i < 4; ++i, tem = XCDR (tem))
10375 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
10376 return 0;
10377 if (!NILP (tem))
10378 return 0;
10379 }
10380 else if (VECTORP (tem))
10381 {
10382 if (XVECTOR (tem)->size != 4)
10383 return 0;
10384 for (i = 0; i < 4; ++i)
10385 if (!INTEGERP (XVECTOR (tem)->contents[i]))
10386 return 0;
10387 }
10388 else
10389 return 0;
10390
10391 return 1;
10392}
10393
10394
10395/* Load Ghostscript image IMG for use on frame F. Value is non-zero
10396 if successful. */
10397
10398static int
10399gs_load (f, img)
10400 struct frame *f;
10401 struct image *img;
10402{
10403 char buffer[100];
10404 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
10405 struct gcpro gcpro1, gcpro2;
10406 Lisp_Object frame;
10407 double in_width, in_height;
10408 Lisp_Object pixel_colors = Qnil;
10409
10410 /* Compute pixel size of pixmap needed from the given size in the
10411 image specification. Sizes in the specification are in pt. 1 pt
10412 = 1/72 in, xdpi and ydpi are stored in the frame's X display
10413 info. */
10414 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
10415 in_width = XFASTINT (pt_width) / 72.0;
10416 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
10417 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
10418 in_height = XFASTINT (pt_height) / 72.0;
10419 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
10420
10421 /* Create the pixmap. */
dd00328a 10422 xassert (img->pixmap == None);
333b20bb
GM
10423 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10424 img->width, img->height,
10425 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
333b20bb
GM
10426
10427 if (!img->pixmap)
10428 {
45158a91 10429 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
10430 return 0;
10431 }
10432
10433 /* Call the loader to fill the pixmap. It returns a process object
10434 if successful. We do not record_unwind_protect here because
10435 other places in redisplay like calling window scroll functions
10436 don't either. Let the Lisp loader use `unwind-protect' instead. */
10437 GCPRO2 (window_and_pixmap_id, pixel_colors);
10438
10439 sprintf (buffer, "%lu %lu",
10440 (unsigned long) FRAME_X_WINDOW (f),
10441 (unsigned long) img->pixmap);
10442 window_and_pixmap_id = build_string (buffer);
10443
10444 sprintf (buffer, "%lu %lu",
10445 FRAME_FOREGROUND_PIXEL (f),
10446 FRAME_BACKGROUND_PIXEL (f));
10447 pixel_colors = build_string (buffer);
10448
10449 XSETFRAME (frame, f);
10450 loader = image_spec_value (img->spec, QCloader, NULL);
10451 if (NILP (loader))
10452 loader = intern ("gs-load-image");
10453
10454 img->data.lisp_val = call6 (loader, frame, img->spec,
10455 make_number (img->width),
10456 make_number (img->height),
10457 window_and_pixmap_id,
10458 pixel_colors);
10459 UNGCPRO;
10460 return PROCESSP (img->data.lisp_val);
10461}
10462
10463
10464/* Kill the Ghostscript process that was started to fill PIXMAP on
10465 frame F. Called from XTread_socket when receiving an event
10466 telling Emacs that Ghostscript has finished drawing. */
10467
10468void
10469x_kill_gs_process (pixmap, f)
10470 Pixmap pixmap;
10471 struct frame *f;
10472{
10473 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
10474 int class, i;
10475 struct image *img;
10476
10477 /* Find the image containing PIXMAP. */
10478 for (i = 0; i < c->used; ++i)
10479 if (c->images[i]->pixmap == pixmap)
10480 break;
10481
daba7643
GM
10482 /* Should someone in between have cleared the image cache, for
10483 instance, give up. */
10484 if (i == c->used)
10485 return;
10486
333b20bb
GM
10487 /* Kill the GS process. We should have found PIXMAP in the image
10488 cache and its image should contain a process object. */
333b20bb
GM
10489 img = c->images[i];
10490 xassert (PROCESSP (img->data.lisp_val));
10491 Fkill_process (img->data.lisp_val, Qnil);
10492 img->data.lisp_val = Qnil;
10493
10494 /* On displays with a mutable colormap, figure out the colors
10495 allocated for the image by looking at the pixels of an XImage for
10496 img->pixmap. */
383d6ffc 10497 class = FRAME_X_VISUAL (f)->class;
333b20bb
GM
10498 if (class != StaticColor && class != StaticGray && class != TrueColor)
10499 {
10500 XImage *ximg;
10501
10502 BLOCK_INPUT;
10503
10504 /* Try to get an XImage for img->pixmep. */
10505 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
10506 0, 0, img->width, img->height, ~0, ZPixmap);
10507 if (ximg)
10508 {
10509 int x, y;
10510
10511 /* Initialize the color table. */
10512 init_color_table ();
10513
10514 /* For each pixel of the image, look its color up in the
10515 color table. After having done so, the color table will
10516 contain an entry for each color used by the image. */
10517 for (y = 0; y < img->height; ++y)
10518 for (x = 0; x < img->width; ++x)
10519 {
10520 unsigned long pixel = XGetPixel (ximg, x, y);
10521 lookup_pixel_color (f, pixel);
10522 }
10523
10524 /* Record colors in the image. Free color table and XImage. */
10525 img->colors = colors_in_color_table (&img->ncolors);
10526 free_color_table ();
10527 XDestroyImage (ximg);
10528
10529#if 0 /* This doesn't seem to be the case. If we free the colors
10530 here, we get a BadAccess later in x_clear_image when
10531 freeing the colors. */
10532 /* We have allocated colors once, but Ghostscript has also
10533 allocated colors on behalf of us. So, to get the
10534 reference counts right, free them once. */
10535 if (img->ncolors)
462d5d40 10536 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
10537#endif
10538 }
10539 else
10540 image_error ("Cannot get X image of `%s'; colors will not be freed",
45158a91 10541 img->spec, Qnil);
333b20bb
GM
10542
10543 UNBLOCK_INPUT;
10544 }
ad18ffb1
GM
10545
10546 /* Now that we have the pixmap, compute mask and transform the
10547 image if requested. */
10548 BLOCK_INPUT;
10549 postprocess_image (f, img);
10550 UNBLOCK_INPUT;
333b20bb
GM
10551}
10552
10553
10554\f
10555/***********************************************************************
10556 Window properties
10557 ***********************************************************************/
10558
10559DEFUN ("x-change-window-property", Fx_change_window_property,
10560 Sx_change_window_property, 2, 3, 0,
7ee72033 10561 doc: /* Change window property PROP to VALUE on the X window of FRAME.
c061c855 10562PROP and VALUE must be strings. FRAME nil or omitted means use the
7ee72033
MB
10563selected frame. Value is VALUE. */)
10564 (prop, value, frame)
333b20bb
GM
10565 Lisp_Object frame, prop, value;
10566{
10567 struct frame *f = check_x_frame (frame);
10568 Atom prop_atom;
10569
b7826503
PJ
10570 CHECK_STRING (prop);
10571 CHECK_STRING (value);
333b20bb
GM
10572
10573 BLOCK_INPUT;
10574 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10575 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10576 prop_atom, XA_STRING, 8, PropModeReplace,
10577 XSTRING (value)->data, XSTRING (value)->size);
10578
10579 /* Make sure the property is set when we return. */
10580 XFlush (FRAME_X_DISPLAY (f));
10581 UNBLOCK_INPUT;
10582
10583 return value;
10584}
10585
10586
10587DEFUN ("x-delete-window-property", Fx_delete_window_property,
10588 Sx_delete_window_property, 1, 2, 0,
7ee72033
MB
10589 doc: /* Remove window property PROP from X window of FRAME.
10590FRAME nil or omitted means use the selected frame. Value is PROP. */)
10591 (prop, frame)
333b20bb
GM
10592 Lisp_Object prop, frame;
10593{
10594 struct frame *f = check_x_frame (frame);
10595 Atom prop_atom;
10596
b7826503 10597 CHECK_STRING (prop);
333b20bb
GM
10598 BLOCK_INPUT;
10599 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10600 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
10601
10602 /* Make sure the property is removed when we return. */
10603 XFlush (FRAME_X_DISPLAY (f));
10604 UNBLOCK_INPUT;
10605
10606 return prop;
10607}
10608
10609
10610DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
10611 1, 2, 0,
7ee72033 10612 doc: /* Value is the value of window property PROP on FRAME.
c061c855
GM
10613If FRAME is nil or omitted, use the selected frame. Value is nil
10614if FRAME hasn't a property with name PROP or if PROP has no string
7ee72033
MB
10615value. */)
10616 (prop, frame)
333b20bb
GM
10617 Lisp_Object prop, frame;
10618{
10619 struct frame *f = check_x_frame (frame);
10620 Atom prop_atom;
10621 int rc;
10622 Lisp_Object prop_value = Qnil;
10623 char *tmp_data = NULL;
10624 Atom actual_type;
10625 int actual_format;
10626 unsigned long actual_size, bytes_remaining;
10627
b7826503 10628 CHECK_STRING (prop);
333b20bb
GM
10629 BLOCK_INPUT;
10630 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10631 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10632 prop_atom, 0, 0, False, XA_STRING,
10633 &actual_type, &actual_format, &actual_size,
10634 &bytes_remaining, (unsigned char **) &tmp_data);
10635 if (rc == Success)
10636 {
10637 int size = bytes_remaining;
10638
10639 XFree (tmp_data);
10640 tmp_data = NULL;
10641
10642 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10643 prop_atom, 0, bytes_remaining,
10644 False, XA_STRING,
10645 &actual_type, &actual_format,
10646 &actual_size, &bytes_remaining,
10647 (unsigned char **) &tmp_data);
4c8c7926 10648 if (rc == Success && tmp_data)
333b20bb
GM
10649 prop_value = make_string (tmp_data, size);
10650
10651 XFree (tmp_data);
10652 }
10653
10654 UNBLOCK_INPUT;
10655 return prop_value;
10656}
10657
10658
10659\f
10660/***********************************************************************
10661 Busy cursor
10662 ***********************************************************************/
10663
4ae9a85e 10664/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 10665 an hourglass cursor on all frames. */
333b20bb 10666
0af913d7 10667static struct atimer *hourglass_atimer;
333b20bb 10668
0af913d7 10669/* Non-zero means an hourglass cursor is currently shown. */
333b20bb 10670
0af913d7 10671static int hourglass_shown_p;
333b20bb 10672
0af913d7 10673/* Number of seconds to wait before displaying an hourglass cursor. */
333b20bb 10674
0af913d7 10675static Lisp_Object Vhourglass_delay;
333b20bb 10676
0af913d7 10677/* Default number of seconds to wait before displaying an hourglass
4ae9a85e
GM
10678 cursor. */
10679
0af913d7 10680#define DEFAULT_HOURGLASS_DELAY 1
4ae9a85e
GM
10681
10682/* Function prototypes. */
10683
0af913d7
GM
10684static void show_hourglass P_ ((struct atimer *));
10685static void hide_hourglass P_ ((void));
4ae9a85e
GM
10686
10687
0af913d7 10688/* Cancel a currently active hourglass timer, and start a new one. */
4ae9a85e
GM
10689
10690void
0af913d7 10691start_hourglass ()
333b20bb 10692{
4ae9a85e 10693 EMACS_TIME delay;
3caa99d3 10694 int secs, usecs = 0;
4ae9a85e 10695
0af913d7 10696 cancel_hourglass ();
4ae9a85e 10697
0af913d7
GM
10698 if (INTEGERP (Vhourglass_delay)
10699 && XINT (Vhourglass_delay) > 0)
10700 secs = XFASTINT (Vhourglass_delay);
10701 else if (FLOATP (Vhourglass_delay)
10702 && XFLOAT_DATA (Vhourglass_delay) > 0)
3caa99d3
GM
10703 {
10704 Lisp_Object tem;
0af913d7 10705 tem = Ftruncate (Vhourglass_delay, Qnil);
3caa99d3 10706 secs = XFASTINT (tem);
0af913d7 10707 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
3caa99d3 10708 }
4ae9a85e 10709 else
0af913d7 10710 secs = DEFAULT_HOURGLASS_DELAY;
4ae9a85e 10711
3caa99d3 10712 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
10713 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
10714 show_hourglass, NULL);
4ae9a85e
GM
10715}
10716
10717
0af913d7 10718/* Cancel the hourglass cursor timer if active, hide a busy cursor if
4ae9a85e
GM
10719 shown. */
10720
10721void
0af913d7 10722cancel_hourglass ()
4ae9a85e 10723{
0af913d7 10724 if (hourglass_atimer)
99f01f62 10725 {
0af913d7
GM
10726 cancel_atimer (hourglass_atimer);
10727 hourglass_atimer = NULL;
99f01f62
GM
10728 }
10729
0af913d7
GM
10730 if (hourglass_shown_p)
10731 hide_hourglass ();
4ae9a85e
GM
10732}
10733
10734
0af913d7
GM
10735/* Timer function of hourglass_atimer. TIMER is equal to
10736 hourglass_atimer.
4ae9a85e 10737
0af913d7
GM
10738 Display an hourglass pointer on all frames by mapping the frames'
10739 hourglass_window. Set the hourglass_p flag in the frames'
10740 output_data.x structure to indicate that an hourglass cursor is
10741 shown on the frames. */
4ae9a85e
GM
10742
10743static void
0af913d7 10744show_hourglass (timer)
4ae9a85e
GM
10745 struct atimer *timer;
10746{
10747 /* The timer implementation will cancel this timer automatically
0af913d7 10748 after this function has run. Set hourglass_atimer to null
4ae9a85e 10749 so that we know the timer doesn't have to be canceled. */
0af913d7 10750 hourglass_atimer = NULL;
4ae9a85e 10751
0af913d7 10752 if (!hourglass_shown_p)
333b20bb
GM
10753 {
10754 Lisp_Object rest, frame;
4ae9a85e
GM
10755
10756 BLOCK_INPUT;
10757
333b20bb 10758 FOR_EACH_FRAME (rest, frame)
5f7a1890
GM
10759 {
10760 struct frame *f = XFRAME (frame);
10761
10762 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
10763 {
10764 Display *dpy = FRAME_X_DISPLAY (f);
10765
10766#ifdef USE_X_TOOLKIT
10767 if (f->output_data.x->widget)
10768#else
10769 if (FRAME_OUTER_WINDOW (f))
10770#endif
10771 {
0af913d7 10772 f->output_data.x->hourglass_p = 1;
4ae9a85e 10773
0af913d7 10774 if (!f->output_data.x->hourglass_window)
5f7a1890
GM
10775 {
10776 unsigned long mask = CWCursor;
10777 XSetWindowAttributes attrs;
4ae9a85e 10778
0af913d7 10779 attrs.cursor = f->output_data.x->hourglass_cursor;
4ae9a85e 10780
0af913d7 10781 f->output_data.x->hourglass_window
5f7a1890
GM
10782 = XCreateWindow (dpy, FRAME_OUTER_WINDOW (f),
10783 0, 0, 32000, 32000, 0, 0,
10784 InputOnly,
10785 CopyFromParent,
10786 mask, &attrs);
10787 }
4ae9a85e 10788
0af913d7 10789 XMapRaised (dpy, f->output_data.x->hourglass_window);
5f7a1890
GM
10790 XFlush (dpy);
10791 }
10792 }
10793 }
333b20bb 10794
0af913d7 10795 hourglass_shown_p = 1;
4ae9a85e
GM
10796 UNBLOCK_INPUT;
10797 }
333b20bb
GM
10798}
10799
10800
0af913d7
GM
10801/* Hide the hourglass pointer on all frames, if it is currently
10802 shown. */
333b20bb 10803
4ae9a85e 10804static void
0af913d7 10805hide_hourglass ()
4ae9a85e 10806{
0af913d7 10807 if (hourglass_shown_p)
333b20bb 10808 {
4ae9a85e
GM
10809 Lisp_Object rest, frame;
10810
10811 BLOCK_INPUT;
10812 FOR_EACH_FRAME (rest, frame)
333b20bb 10813 {
4ae9a85e
GM
10814 struct frame *f = XFRAME (frame);
10815
10816 if (FRAME_X_P (f)
10817 /* Watch out for newly created frames. */
0af913d7 10818 && f->output_data.x->hourglass_window)
4ae9a85e 10819 {
0af913d7
GM
10820 XUnmapWindow (FRAME_X_DISPLAY (f),
10821 f->output_data.x->hourglass_window);
10822 /* Sync here because XTread_socket looks at the
10823 hourglass_p flag that is reset to zero below. */
4ae9a85e 10824 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 10825 f->output_data.x->hourglass_p = 0;
4ae9a85e 10826 }
333b20bb 10827 }
333b20bb 10828
0af913d7 10829 hourglass_shown_p = 0;
4ae9a85e
GM
10830 UNBLOCK_INPUT;
10831 }
333b20bb
GM
10832}
10833
10834
10835\f
10836/***********************************************************************
10837 Tool tips
10838 ***********************************************************************/
10839
10840static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
275841bf 10841 Lisp_Object, Lisp_Object));
06d62053 10842static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
ab452f99 10843 Lisp_Object, int, int, int *, int *));
333b20bb 10844
44b5a125 10845/* The frame of a currently visible tooltip. */
333b20bb 10846
44b5a125 10847Lisp_Object tip_frame;
333b20bb
GM
10848
10849/* If non-nil, a timer started that hides the last tooltip when it
10850 fires. */
10851
10852Lisp_Object tip_timer;
10853Window tip_window;
10854
06d62053
GM
10855/* If non-nil, a vector of 3 elements containing the last args
10856 with which x-show-tip was called. See there. */
10857
10858Lisp_Object last_show_tip_args;
10859
d63931a2
GM
10860/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
10861
10862Lisp_Object Vx_max_tooltip_size;
10863
eaf1eea9
GM
10864
10865static Lisp_Object
10866unwind_create_tip_frame (frame)
10867 Lisp_Object frame;
10868{
c844a81a
GM
10869 Lisp_Object deleted;
10870
10871 deleted = unwind_create_frame (frame);
10872 if (EQ (deleted, Qt))
10873 {
10874 tip_window = None;
10875 tip_frame = Qnil;
10876 }
10877
10878 return deleted;
eaf1eea9
GM
10879}
10880
10881
333b20bb 10882/* Create a frame for a tooltip on the display described by DPYINFO.
275841bf
GM
10883 PARMS is a list of frame parameters. TEXT is the string to
10884 display in the tip frame. Value is the frame.
eaf1eea9
GM
10885
10886 Note that functions called here, esp. x_default_parameter can
10887 signal errors, for instance when a specified color name is
10888 undefined. We have to make sure that we're in a consistent state
10889 when this happens. */
333b20bb
GM
10890
10891static Lisp_Object
275841bf 10892x_create_tip_frame (dpyinfo, parms, text)
333b20bb 10893 struct x_display_info *dpyinfo;
275841bf 10894 Lisp_Object parms, text;
333b20bb
GM
10895{
10896 struct frame *f;
10897 Lisp_Object frame, tem;
10898 Lisp_Object name;
333b20bb
GM
10899 long window_prompting = 0;
10900 int width, height;
eaf1eea9 10901 int count = BINDING_STACK_SIZE ();
b6d7acec 10902 struct gcpro gcpro1, gcpro2, gcpro3;
333b20bb 10903 struct kboard *kb;
06d62053 10904 int face_change_count_before = face_change_count;
275841bf
GM
10905 Lisp_Object buffer;
10906 struct buffer *old_buffer;
333b20bb
GM
10907
10908 check_x ();
10909
10910 /* Use this general default value to start with until we know if
10911 this frame has a specified name. */
10912 Vx_resource_name = Vinvocation_name;
10913
10914#ifdef MULTI_KBOARD
10915 kb = dpyinfo->kboard;
10916#else
10917 kb = &the_only_kboard;
10918#endif
10919
10920 /* Get the name of the frame to use for resource lookup. */
10921 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
10922 if (!STRINGP (name)
10923 && !EQ (name, Qunbound)
10924 && !NILP (name))
10925 error ("Invalid frame name--not a string or nil");
10926 Vx_resource_name = name;
10927
10928 frame = Qnil;
10929 GCPRO3 (parms, name, frame);
44b5a125 10930 f = make_frame (1);
333b20bb 10931 XSETFRAME (frame, f);
275841bf
GM
10932
10933 buffer = Fget_buffer_create (build_string (" *tip*"));
10934 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
10935 old_buffer = current_buffer;
10936 set_buffer_internal_1 (XBUFFER (buffer));
d63931a2 10937 current_buffer->truncate_lines = Qnil;
275841bf
GM
10938 Ferase_buffer ();
10939 Finsert (1, &text);
10940 set_buffer_internal_1 (old_buffer);
10941
333b20bb 10942 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
8a1a7743 10943 record_unwind_protect (unwind_create_tip_frame, frame);
333b20bb 10944
eaf1eea9
GM
10945 /* By setting the output method, we're essentially saying that
10946 the frame is live, as per FRAME_LIVE_P. If we get a signal
10947 from this point on, x_destroy_window might screw up reference
10948 counts etc. */
333b20bb
GM
10949 f->output_method = output_x_window;
10950 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
10951 bzero (f->output_data.x, sizeof (struct x_output));
10952 f->output_data.x->icon_bitmap = -1;
10953 f->output_data.x->fontset = -1;
61d461a8
GM
10954 f->output_data.x->scroll_bar_foreground_pixel = -1;
10955 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
10956#ifdef USE_TOOLKIT_SCROLL_BARS
10957 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
10958 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
10959#endif /* USE_TOOLKIT_SCROLL_BARS */
333b20bb
GM
10960 f->icon_name = Qnil;
10961 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 10962#if GLYPH_DEBUG
eaf1eea9
GM
10963 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
10964 dpyinfo_refcount = dpyinfo->reference_count;
10965#endif /* GLYPH_DEBUG */
333b20bb
GM
10966#ifdef MULTI_KBOARD
10967 FRAME_KBOARD (f) = kb;
10968#endif
10969 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10970 f->output_data.x->explicit_parent = 0;
10971
61d461a8
GM
10972 /* These colors will be set anyway later, but it's important
10973 to get the color reference counts right, so initialize them! */
10974 {
10975 Lisp_Object black;
10976 struct gcpro gcpro1;
10977
10978 black = build_string ("black");
10979 GCPRO1 (black);
10980 f->output_data.x->foreground_pixel
10981 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10982 f->output_data.x->background_pixel
10983 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10984 f->output_data.x->cursor_pixel
10985 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10986 f->output_data.x->cursor_foreground_pixel
10987 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10988 f->output_data.x->border_pixel
10989 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10990 f->output_data.x->mouse_pixel
10991 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10992 UNGCPRO;
10993 }
10994
333b20bb
GM
10995 /* Set the name; the functions to which we pass f expect the name to
10996 be set. */
10997 if (EQ (name, Qunbound) || NILP (name))
10998 {
10999 f->name = build_string (dpyinfo->x_id_name);
11000 f->explicit_name = 0;
11001 }
11002 else
11003 {
11004 f->name = name;
11005 f->explicit_name = 1;
11006 /* use the frame's title when getting resources for this frame. */
11007 specbind (Qx_resource_name, name);
11008 }
11009
eaf1eea9
GM
11010 /* Extract the window parameters from the supplied values that are
11011 needed to determine window geometry. */
333b20bb
GM
11012 {
11013 Lisp_Object font;
11014
11015 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
11016
11017 BLOCK_INPUT;
11018 /* First, try whatever font the caller has specified. */
11019 if (STRINGP (font))
11020 {
11021 tem = Fquery_fontset (font, Qnil);
11022 if (STRINGP (tem))
11023 font = x_new_fontset (f, XSTRING (tem)->data);
11024 else
11025 font = x_new_font (f, XSTRING (font)->data);
11026 }
11027
11028 /* Try out a font which we hope has bold and italic variations. */
11029 if (!STRINGP (font))
11030 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
11031 if (!STRINGP (font))
11032 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11033 if (! STRINGP (font))
11034 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11035 if (! STRINGP (font))
11036 /* This was formerly the first thing tried, but it finds too many fonts
11037 and takes too long. */
11038 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
11039 /* If those didn't work, look for something which will at least work. */
11040 if (! STRINGP (font))
11041 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
11042 UNBLOCK_INPUT;
11043 if (! STRINGP (font))
11044 font = build_string ("fixed");
11045
11046 x_default_parameter (f, parms, Qfont, font,
11047 "font", "Font", RES_TYPE_STRING);
11048 }
11049
11050 x_default_parameter (f, parms, Qborder_width, make_number (2),
11051 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
11052
11053 /* This defaults to 2 in order to match xterm. We recognize either
11054 internalBorderWidth or internalBorder (which is what xterm calls
11055 it). */
11056 if (NILP (Fassq (Qinternal_border_width, parms)))
11057 {
11058 Lisp_Object value;
11059
11060 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
11061 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
11062 if (! EQ (value, Qunbound))
11063 parms = Fcons (Fcons (Qinternal_border_width, value),
11064 parms);
11065 }
11066
11067 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
11068 "internalBorderWidth", "internalBorderWidth",
11069 RES_TYPE_NUMBER);
11070
11071 /* Also do the stuff which must be set before the window exists. */
11072 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
11073 "foreground", "Foreground", RES_TYPE_STRING);
11074 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
11075 "background", "Background", RES_TYPE_STRING);
11076 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
11077 "pointerColor", "Foreground", RES_TYPE_STRING);
11078 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
11079 "cursorColor", "Foreground", RES_TYPE_STRING);
11080 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
11081 "borderColor", "BorderColor", RES_TYPE_STRING);
11082
11083 /* Init faces before x_default_parameter is called for scroll-bar
11084 parameters because that function calls x_set_scroll_bar_width,
11085 which calls change_frame_size, which calls Fset_window_buffer,
11086 which runs hooks, which call Fvertical_motion. At the end, we
11087 end up in init_iterator with a null face cache, which should not
11088 happen. */
11089 init_frame_faces (f);
11090
11091 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
11092 window_prompting = x_figure_window_size (f, parms);
11093
11094 if (window_prompting & XNegative)
11095 {
11096 if (window_prompting & YNegative)
11097 f->output_data.x->win_gravity = SouthEastGravity;
11098 else
11099 f->output_data.x->win_gravity = NorthEastGravity;
11100 }
11101 else
11102 {
11103 if (window_prompting & YNegative)
11104 f->output_data.x->win_gravity = SouthWestGravity;
11105 else
11106 f->output_data.x->win_gravity = NorthWestGravity;
11107 }
11108
11109 f->output_data.x->size_hint_flags = window_prompting;
11110 {
11111 XSetWindowAttributes attrs;
11112 unsigned long mask;
11113
11114 BLOCK_INPUT;
c51d2b5e
GM
11115 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
11116 if (DoesSaveUnders (dpyinfo->screen))
11117 mask |= CWSaveUnder;
11118
9b2956e2
GM
11119 /* Window managers look at the override-redirect flag to determine
11120 whether or net to give windows a decoration (Xlib spec, chapter
333b20bb
GM
11121 3.2.8). */
11122 attrs.override_redirect = True;
11123 attrs.save_under = True;
11124 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
11125 /* Arrange for getting MapNotify and UnmapNotify events. */
11126 attrs.event_mask = StructureNotifyMask;
11127 tip_window
11128 = FRAME_X_WINDOW (f)
11129 = XCreateWindow (FRAME_X_DISPLAY (f),
11130 FRAME_X_DISPLAY_INFO (f)->root_window,
11131 /* x, y, width, height */
11132 0, 0, 1, 1,
11133 /* Border. */
11134 1,
11135 CopyFromParent, InputOutput, CopyFromParent,
11136 mask, &attrs);
11137 UNBLOCK_INPUT;
11138 }
11139
11140 x_make_gc (f);
11141
333b20bb
GM
11142 x_default_parameter (f, parms, Qauto_raise, Qnil,
11143 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11144 x_default_parameter (f, parms, Qauto_lower, Qnil,
11145 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11146 x_default_parameter (f, parms, Qcursor_type, Qbox,
11147 "cursorType", "CursorType", RES_TYPE_SYMBOL);
11148
11149 /* Dimensions, especially f->height, must be done via change_frame_size.
11150 Change will not be effected unless different from the current
11151 f->height. */
11152 width = f->width;
11153 height = f->height;
11154 f->height = 0;
11155 SET_FRAME_WIDTH (f, 0);
8938a4fb 11156 change_frame_size (f, height, width, 1, 0, 0);
333b20bb 11157
035d5114 11158 /* Set up faces after all frame parameters are known. This call
6801a572
GM
11159 also merges in face attributes specified for new frames.
11160
11161 Frame parameters may be changed if .Xdefaults contains
11162 specifications for the default font. For example, if there is an
11163 `Emacs.default.attributeBackground: pink', the `background-color'
11164 attribute of the frame get's set, which let's the internal border
11165 of the tooltip frame appear in pink. Prevent this. */
11166 {
11167 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
11168
11169 /* Set tip_frame here, so that */
11170 tip_frame = frame;
11171 call1 (Qface_set_after_frame_default, frame);
11172
11173 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
11174 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
11175 Qnil));
11176 }
035d5114 11177
333b20bb
GM
11178 f->no_split = 1;
11179
11180 UNGCPRO;
11181
11182 /* It is now ok to make the frame official even if we get an error
11183 below. And the frame needs to be on Vframe_list or making it
11184 visible won't work. */
11185 Vframe_list = Fcons (frame, Vframe_list);
11186
11187 /* Now that the frame is official, it counts as a reference to
11188 its display. */
11189 FRAME_X_DISPLAY_INFO (f)->reference_count++;
11190
06d62053
GM
11191 /* Setting attributes of faces of the tooltip frame from resources
11192 and similar will increment face_change_count, which leads to the
11193 clearing of all current matrices. Since this isn't necessary
11194 here, avoid it by resetting face_change_count to the value it
11195 had before we created the tip frame. */
11196 face_change_count = face_change_count_before;
11197
eaf1eea9 11198 /* Discard the unwind_protect. */
333b20bb
GM
11199 return unbind_to (count, frame);
11200}
11201
11202
06d62053
GM
11203/* Compute where to display tip frame F. PARMS is the list of frame
11204 parameters for F. DX and DY are specified offsets from the current
ab452f99
GM
11205 location of the mouse. WIDTH and HEIGHT are the width and height
11206 of the tooltip. Return coordinates relative to the root window of
11207 the display in *ROOT_X, and *ROOT_Y. */
06d62053
GM
11208
11209static void
ab452f99 11210compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
06d62053
GM
11211 struct frame *f;
11212 Lisp_Object parms, dx, dy;
ab452f99 11213 int width, height;
06d62053
GM
11214 int *root_x, *root_y;
11215{
11216 Lisp_Object left, top;
11217 int win_x, win_y;
11218 Window root, child;
11219 unsigned pmask;
11220
11221 /* User-specified position? */
11222 left = Fcdr (Fassq (Qleft, parms));
11223 top = Fcdr (Fassq (Qtop, parms));
11224
11225 /* Move the tooltip window where the mouse pointer is. Resize and
11226 show it. */
570d22b0 11227 if (!INTEGERP (left) || !INTEGERP (top))
ab452f99
GM
11228 {
11229 BLOCK_INPUT;
11230 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
11231 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
11232 UNBLOCK_INPUT;
11233 }
06d62053 11234
06d62053
GM
11235 if (INTEGERP (top))
11236 *root_y = XINT (top);
ab452f99
GM
11237 else if (*root_y + XINT (dy) - height < 0)
11238 *root_y -= XINT (dy);
11239 else
11240 {
11241 *root_y -= height;
11242 *root_y += XINT (dy);
11243 }
11244
11245 if (INTEGERP (left))
11246 *root_x = XINT (left);
d682d3df
RS
11247 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
11248 /* It fits to the right of the pointer. */
11249 *root_x += XINT (dx);
11250 else if (width + XINT (dx) <= *root_x)
11251 /* It fits to the left of the pointer. */
ab452f99
GM
11252 *root_x -= width + XINT (dx);
11253 else
d682d3df
RS
11254 /* Put it left-justified on the screen--it ought to fit that way. */
11255 *root_x = 0;
06d62053
GM
11256}
11257
11258
0634ce98 11259DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7ee72033 11260 doc: /* Show STRING in a "tooltip" window on frame FRAME.
c061c855
GM
11261A tooltip window is a small X window displaying a string.
11262
11263FRAME nil or omitted means use the selected frame.
11264
11265PARMS is an optional list of frame parameters which can be used to
11266change the tooltip's appearance.
11267
11268Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
11269means use the default timeout of 5 seconds.
11270
11271If the list of frame parameters PARAMS contains a `left' parameters,
11272the tooltip is displayed at that x-position. Otherwise it is
11273displayed at the mouse position, with offset DX added (default is 5 if
11274DX isn't specified). Likewise for the y-position; if a `top' frame
11275parameter is specified, it determines the y-position of the tooltip
11276window, otherwise it is displayed at the mouse position, with offset
11277DY added (default is -10).
11278
11279A tooltip's maximum size is specified by `x-max-tooltip-size'.
7ee72033
MB
11280Text larger than the specified size is clipped. */)
11281 (string, frame, parms, timeout, dx, dy)
0634ce98 11282 Lisp_Object string, frame, parms, timeout, dx, dy;
333b20bb
GM
11283{
11284 struct frame *f;
11285 struct window *w;
06d62053 11286 int root_x, root_y;
333b20bb
GM
11287 struct buffer *old_buffer;
11288 struct text_pos pos;
11289 int i, width, height;
393f2d14 11290 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
333b20bb 11291 int old_windows_or_buffers_changed = windows_or_buffers_changed;
06d62053 11292 int count = BINDING_STACK_SIZE ();
333b20bb
GM
11293
11294 specbind (Qinhibit_redisplay, Qt);
11295
393f2d14 11296 GCPRO4 (string, parms, frame, timeout);
333b20bb 11297
b7826503 11298 CHECK_STRING (string);
333b20bb
GM
11299 f = check_x_frame (frame);
11300 if (NILP (timeout))
11301 timeout = make_number (5);
11302 else
b7826503 11303 CHECK_NATNUM (timeout);
0634ce98
GM
11304
11305 if (NILP (dx))
11306 dx = make_number (5);
11307 else
b7826503 11308 CHECK_NUMBER (dx);
0634ce98
GM
11309
11310 if (NILP (dy))
12c67a7f 11311 dy = make_number (-10);
0634ce98 11312 else
b7826503 11313 CHECK_NUMBER (dy);
333b20bb 11314
06d62053
GM
11315 if (NILP (last_show_tip_args))
11316 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
11317
11318 if (!NILP (tip_frame))
11319 {
11320 Lisp_Object last_string = AREF (last_show_tip_args, 0);
11321 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
11322 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
11323
11324 if (EQ (frame, last_frame)
11325 && !NILP (Fequal (last_string, string))
11326 && !NILP (Fequal (last_parms, parms)))
11327 {
11328 struct frame *f = XFRAME (tip_frame);
11329
11330 /* Only DX and DY have changed. */
11331 if (!NILP (tip_timer))
ae782866
GM
11332 {
11333 Lisp_Object timer = tip_timer;
11334 tip_timer = Qnil;
11335 call1 (Qcancel_timer, timer);
11336 }
06d62053
GM
11337
11338 BLOCK_INPUT;
ab452f99
GM
11339 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
11340 PIXEL_HEIGHT (f), &root_x, &root_y);
06d62053 11341 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 11342 root_x, root_y);
06d62053
GM
11343 UNBLOCK_INPUT;
11344 goto start_timer;
11345 }
11346 }
11347
333b20bb
GM
11348 /* Hide a previous tip, if any. */
11349 Fx_hide_tip ();
11350
06d62053
GM
11351 ASET (last_show_tip_args, 0, string);
11352 ASET (last_show_tip_args, 1, frame);
11353 ASET (last_show_tip_args, 2, parms);
11354
333b20bb
GM
11355 /* Add default values to frame parameters. */
11356 if (NILP (Fassq (Qname, parms)))
11357 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
11358 if (NILP (Fassq (Qinternal_border_width, parms)))
11359 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
11360 if (NILP (Fassq (Qborder_width, parms)))
11361 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
11362 if (NILP (Fassq (Qborder_color, parms)))
11363 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
11364 if (NILP (Fassq (Qbackground_color, parms)))
11365 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
11366 parms);
11367
11368 /* Create a frame for the tooltip, and record it in the global
11369 variable tip_frame. */
275841bf 11370 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
44b5a125 11371 f = XFRAME (frame);
333b20bb 11372
d63931a2 11373 /* Set up the frame's root window. */
333b20bb
GM
11374 w = XWINDOW (FRAME_ROOT_WINDOW (f));
11375 w->left = w->top = make_number (0);
d63931a2
GM
11376
11377 if (CONSP (Vx_max_tooltip_size)
11378 && INTEGERP (XCAR (Vx_max_tooltip_size))
11379 && XINT (XCAR (Vx_max_tooltip_size)) > 0
11380 && INTEGERP (XCDR (Vx_max_tooltip_size))
11381 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
11382 {
11383 w->width = XCAR (Vx_max_tooltip_size);
11384 w->height = XCDR (Vx_max_tooltip_size);
11385 }
11386 else
11387 {
11388 w->width = make_number (80);
11389 w->height = make_number (40);
11390 }
11391
11392 f->window_width = XINT (w->width);
333b20bb
GM
11393 adjust_glyphs (f);
11394 w->pseudo_window_p = 1;
11395
11396 /* Display the tooltip text in a temporary buffer. */
333b20bb 11397 old_buffer = current_buffer;
275841bf 11398 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
d63931a2 11399 current_buffer->truncate_lines = Qnil;
333b20bb
GM
11400 clear_glyph_matrix (w->desired_matrix);
11401 clear_glyph_matrix (w->current_matrix);
11402 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
11403 try_window (FRAME_ROOT_WINDOW (f), pos);
11404
11405 /* Compute width and height of the tooltip. */
11406 width = height = 0;
11407 for (i = 0; i < w->desired_matrix->nrows; ++i)
11408 {
11409 struct glyph_row *row = &w->desired_matrix->rows[i];
11410 struct glyph *last;
11411 int row_width;
11412
11413 /* Stop at the first empty row at the end. */
11414 if (!row->enabled_p || !row->displays_text_p)
11415 break;
11416
d7bf0342
GM
11417 /* Let the row go over the full width of the frame. */
11418 row->full_width_p = 1;
333b20bb 11419
e3130015 11420 /* There's a glyph at the end of rows that is used to place
333b20bb
GM
11421 the cursor there. Don't include the width of this glyph. */
11422 if (row->used[TEXT_AREA])
11423 {
11424 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
11425 row_width = row->pixel_width - last->pixel_width;
11426 }
11427 else
11428 row_width = row->pixel_width;
11429
11430 height += row->height;
11431 width = max (width, row_width);
11432 }
11433
11434 /* Add the frame's internal border to the width and height the X
11435 window should have. */
11436 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11437 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11438
11439 /* Move the tooltip window where the mouse pointer is. Resize and
11440 show it. */
ab452f99 11441 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
0634ce98 11442
0634ce98 11443 BLOCK_INPUT;
333b20bb 11444 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 11445 root_x, root_y, width, height);
333b20bb
GM
11446 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11447 UNBLOCK_INPUT;
06d62053 11448
333b20bb
GM
11449 /* Draw into the window. */
11450 w->must_be_updated_p = 1;
11451 update_single_window (w, 1);
11452
11453 /* Restore original current buffer. */
11454 set_buffer_internal_1 (old_buffer);
11455 windows_or_buffers_changed = old_windows_or_buffers_changed;
11456
06d62053 11457 start_timer:
333b20bb
GM
11458 /* Let the tip disappear after timeout seconds. */
11459 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
11460 intern ("x-hide-tip"));
a744a2ec
DL
11461
11462 UNGCPRO;
333b20bb
GM
11463 return unbind_to (count, Qnil);
11464}
11465
11466
11467DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7ee72033
MB
11468 doc: /* Hide the current tooltip window, if there is any.
11469Value is t if tooltip was open, nil otherwise. */)
11470 ()
333b20bb 11471{
44b5a125 11472 int count;
c0006262
GM
11473 Lisp_Object deleted, frame, timer;
11474 struct gcpro gcpro1, gcpro2;
44b5a125
GM
11475
11476 /* Return quickly if nothing to do. */
c0006262 11477 if (NILP (tip_timer) && NILP (tip_frame))
44b5a125 11478 return Qnil;
333b20bb 11479
c0006262
GM
11480 frame = tip_frame;
11481 timer = tip_timer;
11482 GCPRO2 (frame, timer);
11483 tip_frame = tip_timer = deleted = Qnil;
11484
44b5a125 11485 count = BINDING_STACK_SIZE ();
333b20bb 11486 specbind (Qinhibit_redisplay, Qt);
44b5a125 11487 specbind (Qinhibit_quit, Qt);
333b20bb 11488
c0006262 11489 if (!NILP (timer))
ae782866 11490 call1 (Qcancel_timer, timer);
333b20bb 11491
c0006262 11492 if (FRAMEP (frame))
333b20bb 11493 {
44b5a125
GM
11494 Fdelete_frame (frame, Qnil);
11495 deleted = Qt;
f6c44811
GM
11496
11497#ifdef USE_LUCID
11498 /* Bloodcurdling hack alert: The Lucid menu bar widget's
11499 redisplay procedure is not called when a tip frame over menu
11500 items is unmapped. Redisplay the menu manually... */
11501 {
11502 struct frame *f = SELECTED_FRAME ();
11503 Widget w = f->output_data.x->menubar_widget;
11504 extern void xlwmenu_redisplay P_ ((Widget));
9180dc8c 11505
f6c44811 11506 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
dbc64aa7 11507 && w != NULL)
f6c44811
GM
11508 {
11509 BLOCK_INPUT;
11510 xlwmenu_redisplay (w);
11511 UNBLOCK_INPUT;
11512 }
11513 }
11514#endif /* USE_LUCID */
333b20bb
GM
11515 }
11516
c0006262 11517 UNGCPRO;
44b5a125 11518 return unbind_to (count, deleted);
333b20bb
GM
11519}
11520
11521
11522\f
11523/***********************************************************************
11524 File selection dialog
11525 ***********************************************************************/
11526
11527#ifdef USE_MOTIF
11528
11529/* Callback for "OK" and "Cancel" on file selection dialog. */
11530
11531static void
11532file_dialog_cb (widget, client_data, call_data)
11533 Widget widget;
11534 XtPointer call_data, client_data;
11535{
11536 int *result = (int *) client_data;
11537 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
11538 *result = cb->reason;
11539}
11540
11541
a779d213
GM
11542/* Callback for unmapping a file selection dialog. This is used to
11543 capture the case where a dialog is closed via a window manager's
11544 closer button, for example. Using a XmNdestroyCallback didn't work
11545 in this case. */
11546
11547static void
11548file_dialog_unmap_cb (widget, client_data, call_data)
11549 Widget widget;
11550 XtPointer call_data, client_data;
11551{
11552 int *result = (int *) client_data;
11553 *result = XmCR_CANCEL;
11554}
11555
11556
333b20bb 11557DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
7ee72033 11558 doc: /* Read file name, prompting with PROMPT in directory DIR.
c061c855
GM
11559Use a file selection dialog.
11560Select DEFAULT-FILENAME in the dialog's file selection box, if
11561specified. Don't let the user enter a file name in the file
7ee72033
MB
11562selection dialog's entry field, if MUSTMATCH is non-nil. */)
11563 (prompt, dir, default_filename, mustmatch)
333b20bb
GM
11564 Lisp_Object prompt, dir, default_filename, mustmatch;
11565{
11566 int result;
0fe92f72 11567 struct frame *f = SELECTED_FRAME ();
333b20bb
GM
11568 Lisp_Object file = Qnil;
11569 Widget dialog, text, list, help;
11570 Arg al[10];
11571 int ac = 0;
11572 extern XtAppContext Xt_app_con;
333b20bb 11573 XmString dir_xmstring, pattern_xmstring;
333b20bb
GM
11574 int count = specpdl_ptr - specpdl;
11575 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
11576
11577 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
11578 CHECK_STRING (prompt);
11579 CHECK_STRING (dir);
333b20bb
GM
11580
11581 /* Prevent redisplay. */
11582 specbind (Qinhibit_redisplay, Qt);
11583
11584 BLOCK_INPUT;
11585
11586 /* Create the dialog with PROMPT as title, using DIR as initial
11587 directory and using "*" as pattern. */
11588 dir = Fexpand_file_name (dir, Qnil);
11589 dir_xmstring = XmStringCreateLocalized (XSTRING (dir)->data);
11590 pattern_xmstring = XmStringCreateLocalized ("*");
11591
11592 XtSetArg (al[ac], XmNtitle, XSTRING (prompt)->data); ++ac;
11593 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
11594 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
11595 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
11596 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
11597 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
11598 "fsb", al, ac);
11599 XmStringFree (dir_xmstring);
11600 XmStringFree (pattern_xmstring);
11601
11602 /* Add callbacks for OK and Cancel. */
11603 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
11604 (XtPointer) &result);
11605 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
11606 (XtPointer) &result);
a779d213
GM
11607 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
11608 (XtPointer) &result);
333b20bb
GM
11609
11610 /* Disable the help button since we can't display help. */
11611 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
11612 XtSetSensitive (help, False);
11613
11614 /* Mark OK button as default. */
11615 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
11616 XmNshowAsDefault, True, NULL);
11617
11618 /* If MUSTMATCH is non-nil, disable the file entry field of the
11619 dialog, so that the user must select a file from the files list
11620 box. We can't remove it because we wouldn't have a way to get at
11621 the result file name, then. */
11622 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
11623 if (!NILP (mustmatch))
11624 {
11625 Widget label;
11626 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
11627 XtSetSensitive (text, False);
11628 XtSetSensitive (label, False);
11629 }
11630
11631 /* Manage the dialog, so that list boxes get filled. */
11632 XtManageChild (dialog);
11633
11634 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
11635 must include the path for this to work. */
11636 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
11637 if (STRINGP (default_filename))
11638 {
11639 XmString default_xmstring;
11640 int item_pos;
11641
11642 default_xmstring
11643 = XmStringCreateLocalized (XSTRING (default_filename)->data);
11644
11645 if (!XmListItemExists (list, default_xmstring))
11646 {
11647 /* Add a new item if DEFAULT_FILENAME is not in the list. */
11648 XmListAddItem (list, default_xmstring, 0);
11649 item_pos = 0;
11650 }
11651 else
11652 item_pos = XmListItemPos (list, default_xmstring);
11653 XmStringFree (default_xmstring);
11654
11655 /* Select the item and scroll it into view. */
11656 XmListSelectPos (list, item_pos, True);
11657 XmListSetPos (list, item_pos);
11658 }
11659
563b384d
GM
11660 /* Process events until the user presses Cancel or OK. Block
11661 and unblock input here so that we get a chance of processing
11662 expose events. */
11663 UNBLOCK_INPUT;
03100098 11664 result = 0;
a779d213 11665 while (result == 0)
563b384d
GM
11666 {
11667 BLOCK_INPUT;
11668 XtAppProcessEvent (Xt_app_con, XtIMAll);
11669 UNBLOCK_INPUT;
11670 }
11671 BLOCK_INPUT;
03100098 11672
333b20bb
GM
11673 /* Get the result. */
11674 if (result == XmCR_OK)
11675 {
11676 XmString text;
11677 String data;
11678
d1670063 11679 XtVaGetValues (dialog, XmNtextString, &text, NULL);
333b20bb
GM
11680 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
11681 XmStringFree (text);
11682 file = build_string (data);
11683 XtFree (data);
11684 }
11685 else
11686 file = Qnil;
11687
11688 /* Clean up. */
11689 XtUnmanageChild (dialog);
11690 XtDestroyWidget (dialog);
11691 UNBLOCK_INPUT;
11692 UNGCPRO;
11693
11694 /* Make "Cancel" equivalent to C-g. */
11695 if (NILP (file))
11696 Fsignal (Qquit, Qnil);
11697
11698 return unbind_to (count, file);
11699}
11700
11701#endif /* USE_MOTIF */
11702
333b20bb
GM
11703
11704\f
82bab41c
GM
11705/***********************************************************************
11706 Keyboard
11707 ***********************************************************************/
11708
11709#ifdef HAVE_XKBGETKEYBOARD
11710#include <X11/XKBlib.h>
11711#include <X11/keysym.h>
11712#endif
11713
11714DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
11715 Sx_backspace_delete_keys_p, 0, 1, 0,
7ee72033 11716 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
c061c855
GM
11717FRAME nil means use the selected frame.
11718Value is t if we know that both keys are present, and are mapped to the
7ee72033
MB
11719usual X keysyms. */)
11720 (frame)
82bab41c
GM
11721 Lisp_Object frame;
11722{
11723#ifdef HAVE_XKBGETKEYBOARD
11724 XkbDescPtr kb;
11725 struct frame *f = check_x_frame (frame);
11726 Display *dpy = FRAME_X_DISPLAY (f);
11727 Lisp_Object have_keys;
46f6a258 11728 int major, minor, op, event, error;
82bab41c
GM
11729
11730 BLOCK_INPUT;
46f6a258
GM
11731
11732 /* Check library version in case we're dynamically linked. */
11733 major = XkbMajorVersion;
11734 minor = XkbMinorVersion;
11735 if (!XkbLibraryVersion (&major, &minor))
c1efd260
GM
11736 {
11737 UNBLOCK_INPUT;
11738 return Qnil;
11739 }
46f6a258
GM
11740
11741 /* Check that the server supports XKB. */
11742 major = XkbMajorVersion;
11743 minor = XkbMinorVersion;
11744 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
c1efd260
GM
11745 {
11746 UNBLOCK_INPUT;
11747 return Qnil;
11748 }
46f6a258
GM
11749
11750 have_keys = Qnil;
c1efd260 11751 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
82bab41c
GM
11752 if (kb)
11753 {
11754 int delete_keycode = 0, backspace_keycode = 0, i;
c1efd260
GM
11755
11756 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
82bab41c 11757 {
c1efd260
GM
11758 for (i = kb->min_key_code;
11759 (i < kb->max_key_code
11760 && (delete_keycode == 0 || backspace_keycode == 0));
11761 ++i)
11762 {
d63931a2
GM
11763 /* The XKB symbolic key names can be seen most easily in
11764 the PS file generated by `xkbprint -label name
11765 $DISPLAY'. */
c1efd260
GM
11766 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
11767 delete_keycode = i;
11768 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
11769 backspace_keycode = i;
11770 }
11771
11772 XkbFreeNames (kb, 0, True);
82bab41c
GM
11773 }
11774
c1efd260 11775 XkbFreeClientMap (kb, 0, True);
82bab41c
GM
11776
11777 if (delete_keycode
11778 && backspace_keycode
11779 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
11780 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
11781 have_keys = Qt;
11782 }
11783 UNBLOCK_INPUT;
11784 return have_keys;
11785#else /* not HAVE_XKBGETKEYBOARD */
11786 return Qnil;
11787#endif /* not HAVE_XKBGETKEYBOARD */
11788}
11789
11790
11791\f
333b20bb
GM
11792/***********************************************************************
11793 Initialization
11794 ***********************************************************************/
11795
11796void
11797syms_of_xfns ()
11798{
11799 /* This is zero if not using X windows. */
11800 x_in_use = 0;
11801
11802 /* The section below is built by the lisp expression at the top of the file,
11803 just above where these variables are declared. */
11804 /*&&& init symbols here &&&*/
11805 Qauto_raise = intern ("auto-raise");
11806 staticpro (&Qauto_raise);
11807 Qauto_lower = intern ("auto-lower");
11808 staticpro (&Qauto_lower);
11809 Qbar = intern ("bar");
dbc4e1c1 11810 staticpro (&Qbar);
f9942c9e
JB
11811 Qborder_color = intern ("border-color");
11812 staticpro (&Qborder_color);
11813 Qborder_width = intern ("border-width");
11814 staticpro (&Qborder_width);
dbc4e1c1
JB
11815 Qbox = intern ("box");
11816 staticpro (&Qbox);
f9942c9e
JB
11817 Qcursor_color = intern ("cursor-color");
11818 staticpro (&Qcursor_color);
dbc4e1c1
JB
11819 Qcursor_type = intern ("cursor-type");
11820 staticpro (&Qcursor_type);
f9942c9e
JB
11821 Qgeometry = intern ("geometry");
11822 staticpro (&Qgeometry);
f9942c9e
JB
11823 Qicon_left = intern ("icon-left");
11824 staticpro (&Qicon_left);
11825 Qicon_top = intern ("icon-top");
11826 staticpro (&Qicon_top);
11827 Qicon_type = intern ("icon-type");
11828 staticpro (&Qicon_type);
80534dd6
KH
11829 Qicon_name = intern ("icon-name");
11830 staticpro (&Qicon_name);
f9942c9e
JB
11831 Qinternal_border_width = intern ("internal-border-width");
11832 staticpro (&Qinternal_border_width);
11833 Qleft = intern ("left");
11834 staticpro (&Qleft);
1ab3d87e
RS
11835 Qright = intern ("right");
11836 staticpro (&Qright);
f9942c9e
JB
11837 Qmouse_color = intern ("mouse-color");
11838 staticpro (&Qmouse_color);
baaed68e
JB
11839 Qnone = intern ("none");
11840 staticpro (&Qnone);
f9942c9e
JB
11841 Qparent_id = intern ("parent-id");
11842 staticpro (&Qparent_id);
4701395c
KH
11843 Qscroll_bar_width = intern ("scroll-bar-width");
11844 staticpro (&Qscroll_bar_width);
8af1d7ca
JB
11845 Qsuppress_icon = intern ("suppress-icon");
11846 staticpro (&Qsuppress_icon);
01f1ba30 11847 Qundefined_color = intern ("undefined-color");
f9942c9e 11848 staticpro (&Qundefined_color);
a3c87d4e
JB
11849 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
11850 staticpro (&Qvertical_scroll_bars);
49795535
JB
11851 Qvisibility = intern ("visibility");
11852 staticpro (&Qvisibility);
f9942c9e
JB
11853 Qwindow_id = intern ("window-id");
11854 staticpro (&Qwindow_id);
2cbebefb
RS
11855 Qouter_window_id = intern ("outer-window-id");
11856 staticpro (&Qouter_window_id);
f9942c9e
JB
11857 Qx_frame_parameter = intern ("x-frame-parameter");
11858 staticpro (&Qx_frame_parameter);
9ef48a9d
RS
11859 Qx_resource_name = intern ("x-resource-name");
11860 staticpro (&Qx_resource_name);
4fe1de12
RS
11861 Quser_position = intern ("user-position");
11862 staticpro (&Quser_position);
11863 Quser_size = intern ("user-size");
11864 staticpro (&Quser_size);
333b20bb
GM
11865 Qscroll_bar_foreground = intern ("scroll-bar-foreground");
11866 staticpro (&Qscroll_bar_foreground);
11867 Qscroll_bar_background = intern ("scroll-bar-background");
11868 staticpro (&Qscroll_bar_background);
d62c8769
GM
11869 Qscreen_gamma = intern ("screen-gamma");
11870 staticpro (&Qscreen_gamma);
563b67aa
GM
11871 Qline_spacing = intern ("line-spacing");
11872 staticpro (&Qline_spacing);
7c7ff7f5
GM
11873 Qcenter = intern ("center");
11874 staticpro (&Qcenter);
96db09e4
KH
11875 Qcompound_text = intern ("compound-text");
11876 staticpro (&Qcompound_text);
ae782866
GM
11877 Qcancel_timer = intern ("cancel-timer");
11878 staticpro (&Qcancel_timer);
ea0a1f53
GM
11879 Qwait_for_wm = intern ("wait-for-wm");
11880 staticpro (&Qwait_for_wm);
49d41073
EZ
11881 Qfullscreen = intern ("fullscreen");
11882 staticpro (&Qfullscreen);
11883 Qfullwidth = intern ("fullwidth");
11884 staticpro (&Qfullwidth);
11885 Qfullheight = intern ("fullheight");
11886 staticpro (&Qfullheight);
11887 Qfullboth = intern ("fullboth");
11888 staticpro (&Qfullboth);
f9942c9e
JB
11889 /* This is the end of symbol initialization. */
11890
58cad5ed
KH
11891 /* Text property `display' should be nonsticky by default. */
11892 Vtext_property_default_nonsticky
11893 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
11894
11895
333b20bb
GM
11896 Qlaplace = intern ("laplace");
11897 staticpro (&Qlaplace);
4a8e312c
GM
11898 Qemboss = intern ("emboss");
11899 staticpro (&Qemboss);
11900 Qedge_detection = intern ("edge-detection");
11901 staticpro (&Qedge_detection);
11902 Qheuristic = intern ("heuristic");
11903 staticpro (&Qheuristic);
11904 QCmatrix = intern (":matrix");
11905 staticpro (&QCmatrix);
11906 QCcolor_adjustment = intern (":color-adjustment");
11907 staticpro (&QCcolor_adjustment);
11908 QCmask = intern (":mask");
11909 staticpro (&QCmask);
11910
a367641f
RS
11911 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
11912 staticpro (&Qface_set_after_frame_default);
11913
01f1ba30
JB
11914 Fput (Qundefined_color, Qerror_conditions,
11915 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
11916 Fput (Qundefined_color, Qerror_message,
11917 build_string ("Undefined color"));
11918
f9942c9e
JB
11919 init_x_parm_symbols ();
11920
7ee72033
MB
11921 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
11922 doc: /* Non-nil means always draw a cross over disabled images.
c061c855
GM
11923Disabled images are those having an `:conversion disabled' property.
11924A cross is always drawn on black & white displays. */);
14819cb3
GM
11925 cross_disabled_images = 0;
11926
7ee72033
MB
11927 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
11928 doc: /* List of directories to search for bitmap files for X. */);
e241c09b 11929 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
f1c7b5a6 11930
7ee72033
MB
11931 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
11932 doc: /* The shape of the pointer when over text.
c061c855
GM
11933Changing the value does not affect existing frames
11934unless you set the mouse color. */);
01f1ba30
JB
11935 Vx_pointer_shape = Qnil;
11936
7ee72033
MB
11937 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
11938 doc: /* The name Emacs uses to look up X resources.
c061c855
GM
11939`x-get-resource' uses this as the first component of the instance name
11940when requesting resource values.
11941Emacs initially sets `x-resource-name' to the name under which Emacs
11942was invoked, or to the value specified with the `-name' or `-rn'
11943switches, if present.
11944
11945It may be useful to bind this variable locally around a call
11946to `x-get-resource'. See also the variable `x-resource-class'. */);
d387c960 11947 Vx_resource_name = Qnil;
ac63d3d6 11948
7ee72033
MB
11949 DEFVAR_LISP ("x-resource-class", &Vx_resource_class,
11950 doc: /* The class Emacs uses to look up X resources.
c061c855
GM
11951`x-get-resource' uses this as the first component of the instance class
11952when requesting resource values.
11953
11954Emacs initially sets `x-resource-class' to "Emacs".
11955
11956Setting this variable permanently is not a reasonable thing to do,
11957but binding this variable locally around a call to `x-get-resource'
11958is a reasonable practice. See also the variable `x-resource-name'. */);
498e9ac3
RS
11959 Vx_resource_class = build_string (EMACS_CLASS);
11960
ca0ecbf5 11961#if 0 /* This doesn't really do anything. */
7ee72033
MB
11962 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
11963 doc: /* The shape of the pointer when not over text.
c061c855
GM
11964This variable takes effect when you create a new frame
11965or when you set the mouse color. */);
af01ef26 11966#endif
01f1ba30
JB
11967 Vx_nontext_pointer_shape = Qnil;
11968
7ee72033
MB
11969 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
11970 doc: /* The shape of the pointer when Emacs is busy.
c061c855
GM
11971This variable takes effect when you create a new frame
11972or when you set the mouse color. */);
0af913d7 11973 Vx_hourglass_pointer_shape = Qnil;
333b20bb 11974
7ee72033
MB
11975 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
11976 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 11977 display_hourglass_p = 1;
333b20bb 11978
7ee72033
MB
11979 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
11980 doc: /* *Seconds to wait before displaying an hourglass pointer.
c061c855 11981Value must be an integer or float. */);
0af913d7 11982 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
4ae9a85e 11983
ca0ecbf5 11984#if 0 /* This doesn't really do anything. */
7ee72033
MB
11985 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
11986 doc: /* The shape of the pointer when over the mode line.
c061c855
GM
11987This variable takes effect when you create a new frame
11988or when you set the mouse color. */);
af01ef26 11989#endif
01f1ba30
JB
11990 Vx_mode_pointer_shape = Qnil;
11991
d3b06468 11992 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7ee72033
MB
11993 &Vx_sensitive_text_pointer_shape,
11994 doc: /* The shape of the pointer when over mouse-sensitive text.
c061c855
GM
11995This variable takes effect when you create a new frame
11996or when you set the mouse color. */);
ca0ecbf5 11997 Vx_sensitive_text_pointer_shape = Qnil;
95f80c78 11998
8fb4ec9c 11999 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7ee72033
MB
12000 &Vx_window_horizontal_drag_shape,
12001 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
c061c855
GM
12002This variable takes effect when you create a new frame
12003or when you set the mouse color. */);
8fb4ec9c
GM
12004 Vx_window_horizontal_drag_shape = Qnil;
12005
7ee72033
MB
12006 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
12007 doc: /* A string indicating the foreground color of the cursor box. */);
01f1ba30
JB
12008 Vx_cursor_fore_pixel = Qnil;
12009
7ee72033
MB
12010 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
12011 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
c061c855 12012Text larger than this is clipped. */);
d63931a2
GM
12013 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
12014
7ee72033
MB
12015 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
12016 doc: /* Non-nil if no X window manager is in use.
c061c855
GM
12017Emacs doesn't try to figure this out; this is always nil
12018unless you set it to something else. */);
2d38195d
RS
12019 /* We don't have any way to find this out, so set it to nil
12020 and maybe the user would like to set it to t. */
12021 Vx_no_window_manager = Qnil;
1d3dac41 12022
942ea06d 12023 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7ee72033
MB
12024 &Vx_pixel_size_width_font_regexp,
12025 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
c061c855
GM
12026
12027Since Emacs gets width of a font matching with this regexp from
12028PIXEL_SIZE field of the name, font finding mechanism gets faster for
12029such a font. This is especially effective for such large fonts as
12030Chinese, Japanese, and Korean. */);
942ea06d
KH
12031 Vx_pixel_size_width_font_regexp = Qnil;
12032
7ee72033
MB
12033 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
12034 doc: /* Time after which cached images are removed from the cache.
c061c855
GM
12035When an image has not been displayed this many seconds, remove it
12036from the image cache. Value must be an integer or nil with nil
12037meaning don't clear the cache. */);
fcf431dc 12038 Vimage_cache_eviction_delay = make_number (30 * 60);
333b20bb 12039
1d3dac41 12040#ifdef USE_X_TOOLKIT
6f3f6a8d 12041 Fprovide (intern ("x-toolkit"), Qnil);
5b827abb 12042#ifdef USE_MOTIF
6f3f6a8d 12043 Fprovide (intern ("motif"), Qnil);
fc2cdd9a 12044
7ee72033
MB
12045 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
12046 doc: /* Version info for LessTif/Motif. */);
fc2cdd9a
GM
12047 Vmotif_version_string = build_string (XmVERSION_STRING);
12048#endif /* USE_MOTIF */
12049#endif /* USE_X_TOOLKIT */
01f1ba30 12050
01f1ba30 12051 defsubr (&Sx_get_resource);
333b20bb
GM
12052
12053 /* X window properties. */
12054 defsubr (&Sx_change_window_property);
12055 defsubr (&Sx_delete_window_property);
12056 defsubr (&Sx_window_property);
12057
2d764c78 12058 defsubr (&Sxw_display_color_p);
d0c9d219 12059 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
12060 defsubr (&Sxw_color_defined_p);
12061 defsubr (&Sxw_color_values);
9d317b2c 12062 defsubr (&Sx_server_max_request_size);
41beb8fc
RS
12063 defsubr (&Sx_server_vendor);
12064 defsubr (&Sx_server_version);
12065 defsubr (&Sx_display_pixel_width);
12066 defsubr (&Sx_display_pixel_height);
12067 defsubr (&Sx_display_mm_width);
12068 defsubr (&Sx_display_mm_height);
12069 defsubr (&Sx_display_screens);
12070 defsubr (&Sx_display_planes);
12071 defsubr (&Sx_display_color_cells);
12072 defsubr (&Sx_display_visual_class);
12073 defsubr (&Sx_display_backing_store);
12074 defsubr (&Sx_display_save_under);
8af1d7ca 12075 defsubr (&Sx_parse_geometry);
f676886a 12076 defsubr (&Sx_create_frame);
01f1ba30 12077 defsubr (&Sx_open_connection);
08a90d6a
RS
12078 defsubr (&Sx_close_connection);
12079 defsubr (&Sx_display_list);
01f1ba30 12080 defsubr (&Sx_synchronize);
3decc1e7 12081 defsubr (&Sx_focus_frame);
82bab41c
GM
12082 defsubr (&Sx_backspace_delete_keys_p);
12083
942ea06d
KH
12084 /* Setting callback functions for fontset handler. */
12085 get_font_info_func = x_get_font_info;
333b20bb
GM
12086
12087#if 0 /* This function pointer doesn't seem to be used anywhere.
12088 And the pointer assigned has the wrong type, anyway. */
942ea06d 12089 list_fonts_func = x_list_fonts;
333b20bb
GM
12090#endif
12091
942ea06d 12092 load_font_func = x_load_font;
bc1958c4 12093 find_ccl_program_func = x_find_ccl_program;
942ea06d
KH
12094 query_font_func = x_query_font;
12095 set_frame_fontset_func = x_set_font;
5f652afc 12096 get_font_repertory_func = x_get_font_repertory;
942ea06d 12097 check_window_system_func = check_x;
333b20bb
GM
12098
12099 /* Images. */
12100 Qxbm = intern ("xbm");
12101 staticpro (&Qxbm);
12102 QCtype = intern (":type");
12103 staticpro (&QCtype);
d2dc8167
GM
12104 QCconversion = intern (":conversion");
12105 staticpro (&QCconversion);
333b20bb
GM
12106 QCheuristic_mask = intern (":heuristic-mask");
12107 staticpro (&QCheuristic_mask);
12108 QCcolor_symbols = intern (":color-symbols");
12109 staticpro (&QCcolor_symbols);
333b20bb
GM
12110 QCascent = intern (":ascent");
12111 staticpro (&QCascent);
12112 QCmargin = intern (":margin");
12113 staticpro (&QCmargin);
12114 QCrelief = intern (":relief");
12115 staticpro (&QCrelief);
fcf431dc
GM
12116 Qpostscript = intern ("postscript");
12117 staticpro (&Qpostscript);
333b20bb
GM
12118 QCloader = intern (":loader");
12119 staticpro (&QCloader);
12120 QCbounding_box = intern (":bounding-box");
12121 staticpro (&QCbounding_box);
12122 QCpt_width = intern (":pt-width");
12123 staticpro (&QCpt_width);
12124 QCpt_height = intern (":pt-height");
12125 staticpro (&QCpt_height);
3ccff1e3
GM
12126 QCindex = intern (":index");
12127 staticpro (&QCindex);
333b20bb
GM
12128 Qpbm = intern ("pbm");
12129 staticpro (&Qpbm);
12130
12131#if HAVE_XPM
12132 Qxpm = intern ("xpm");
12133 staticpro (&Qxpm);
12134#endif
12135
12136#if HAVE_JPEG
12137 Qjpeg = intern ("jpeg");
12138 staticpro (&Qjpeg);
12139#endif
12140
12141#if HAVE_TIFF
12142 Qtiff = intern ("tiff");
12143 staticpro (&Qtiff);
12144#endif
12145
12146#if HAVE_GIF
12147 Qgif = intern ("gif");
12148 staticpro (&Qgif);
12149#endif
12150
12151#if HAVE_PNG
12152 Qpng = intern ("png");
12153 staticpro (&Qpng);
12154#endif
12155
12156 defsubr (&Sclear_image_cache);
42677916 12157 defsubr (&Simage_size);
b243755a 12158 defsubr (&Simage_mask_p);
333b20bb 12159
0af913d7
GM
12160 hourglass_atimer = NULL;
12161 hourglass_shown_p = 0;
333b20bb
GM
12162
12163 defsubr (&Sx_show_tip);
12164 defsubr (&Sx_hide_tip);
333b20bb 12165 tip_timer = Qnil;
44b5a125
GM
12166 staticpro (&tip_timer);
12167 tip_frame = Qnil;
12168 staticpro (&tip_frame);
333b20bb 12169
06d62053
GM
12170 last_show_tip_args = Qnil;
12171 staticpro (&last_show_tip_args);
12172
333b20bb
GM
12173#ifdef USE_MOTIF
12174 defsubr (&Sx_file_dialog);
12175#endif
12176}
12177
12178
12179void
12180init_xfns ()
12181{
12182 image_types = NULL;
12183 Vimage_types = Qnil;
12184
12185 define_image_type (&xbm_type);
12186 define_image_type (&gs_type);
12187 define_image_type (&pbm_type);
12188
12189#if HAVE_XPM
12190 define_image_type (&xpm_type);
12191#endif
12192
12193#if HAVE_JPEG
12194 define_image_type (&jpeg_type);
12195#endif
12196
12197#if HAVE_TIFF
12198 define_image_type (&tiff_type);
12199#endif
12200
12201#if HAVE_GIF
12202 define_image_type (&gif_type);
12203#endif
12204
12205#if HAVE_PNG
12206 define_image_type (&png_type);
12207#endif
01f1ba30
JB
12208}
12209
12210#endif /* HAVE_X_WINDOWS */