(x_window, x_window): Use use_xim.
[bpt/emacs.git] / src / xfns.c
CommitLineData
01f1ba30 1/* Functions for the X window system.
c8533ac4 2 Copyright (C) 1989, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 01, 02
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))
75f8dd6a 1998 error ("The default fontset can't be used for a frame font");
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
af1b7625 3434/* Supported XIM styles, ordered by preference. */
5a7df7d7
GM
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;
af1b7625
DL
3890 if (use_xim)
3891 create_frame_xic (f);
5a7df7d7 3892#endif
64d16748 3893
7556890b
RS
3894 f->output_data.x->wm_hints.input = True;
3895 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 3896 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3897 &f->output_data.x->wm_hints);
b8228beb 3898
c4ec904f 3899 hack_wm_protocols (f, shell_widget);
9ef48a9d 3900
6c32dd68
PR
3901#ifdef HACK_EDITRES
3902 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
3903#endif
3904
9ef48a9d 3905 /* Do a stupid property change to force the server to generate a
333b20bb 3906 PropertyNotify event so that the event_stream server timestamp will
9ef48a9d
RS
3907 be initialized to something relevant to the time we created the window.
3908 */
6c32dd68 3909 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
b9dc4443
RS
3910 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3911 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
3912 (unsigned char*) NULL, 0);
3913
5a7df7d7 3914 /* Make all the standard events reach the Emacs frame. */
31ac8d8c 3915 attributes.event_mask = STANDARD_EVENT_SET;
5a7df7d7
GM
3916
3917#ifdef HAVE_X_I18N
3918 if (FRAME_XIC (f))
3919 {
3920 /* XIM server might require some X events. */
3921 unsigned long fevent = NoEventMask;
3922 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3923 attributes.event_mask |= fevent;
3924 }
3925#endif /* HAVE_X_I18N */
3926
31ac8d8c
FP
3927 attribute_mask = CWEventMask;
3928 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
3929 attribute_mask, &attributes);
3930
6c32dd68 3931 XtMapWidget (frame_widget);
9ef48a9d 3932
8fc2766b
RS
3933 /* x_set_name normally ignores requests to set the name if the
3934 requested name is the same as the current name. This is the one
3935 place where that assumption isn't correct; f->name is set, but
3936 the X server hasn't been told. */
3937 {
3938 Lisp_Object name;
3939 int explicit = f->explicit_name;
3940
3941 f->explicit_name = 0;
3942 name = f->name;
3943 f->name = Qnil;
3944 x_set_name (f, name, explicit);
3945 }
3946
b9dc4443 3947 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3948 f->output_data.x->text_cursor);
8fc2766b
RS
3949
3950 UNBLOCK_INPUT;
3951
495fa05e
GM
3952 /* This is a no-op, except under Motif. Make sure main areas are
3953 set to something reasonable, in case we get an error later. */
3954 lw_set_main_areas (pane_widget, 0, frame_widget);
8fc2766b
RS
3955}
3956
9ef48a9d
RS
3957#else /* not USE_X_TOOLKIT */
3958
8fc2766b
RS
3959/* Create and set up the X window for frame F. */
3960
201d8c78 3961void
8fc2766b
RS
3962x_window (f)
3963 struct frame *f;
3964
3965{
3966 XClassHint class_hints;
3967 XSetWindowAttributes attributes;
3968 unsigned long attribute_mask;
3969
7556890b
RS
3970 attributes.background_pixel = f->output_data.x->background_pixel;
3971 attributes.border_pixel = f->output_data.x->border_pixel;
01f1ba30
JB
3972 attributes.bit_gravity = StaticGravity;
3973 attributes.backing_store = NotUseful;
3974 attributes.save_under = True;
3975 attributes.event_mask = STANDARD_EVENT_SET;
9b2956e2
GM
3976 attributes.colormap = FRAME_X_COLORMAP (f);
3977 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
3978 | CWColormap);
01f1ba30
JB
3979
3980 BLOCK_INPUT;
fe24a618 3981 FRAME_X_WINDOW (f)
b9dc4443 3982 = XCreateWindow (FRAME_X_DISPLAY (f),
7556890b
RS
3983 f->output_data.x->parent_desc,
3984 f->output_data.x->left_pos,
3985 f->output_data.x->top_pos,
f676886a 3986 PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
7556890b 3987 f->output_data.x->border_width,
01f1ba30
JB
3988 CopyFromParent, /* depth */
3989 InputOutput, /* class */
383d6ffc 3990 FRAME_X_VISUAL (f),
01f1ba30 3991 attribute_mask, &attributes);
5a7df7d7
GM
3992
3993#ifdef HAVE_X_I18N
af1b7625 3994 if use_xim
5a7df7d7 3995 {
af1b7625
DL
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 }
5a7df7d7
GM
4007 }
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
4fc0b72e
DL
9236# undef HAVE_STDLIB_H
9237# include <jpeglib.h>
c8533ac4 9238# undef HAVE_STDLIB_H
4fc0b72e
DL
9239# define HAVE_STDLIB_H 1
9240#else
9241# include <jpeglib.h>
4fc0b72e 9242#endif /* HAVE_STDLIB_H */
c8533ac4 9243#include <jerror.h>
ba06aba4 9244
333b20bb
GM
9245#include <setjmp.h>
9246
9247static int jpeg_image_p P_ ((Lisp_Object object));
9248static int jpeg_load P_ ((struct frame *f, struct image *img));
9249
9250/* The symbol `jpeg' identifying images of this type. */
9251
9252Lisp_Object Qjpeg;
9253
9254/* Indices of image specification fields in gs_format, below. */
9255
9256enum jpeg_keyword_index
9257{
9258 JPEG_TYPE,
8e39770a 9259 JPEG_DATA,
333b20bb
GM
9260 JPEG_FILE,
9261 JPEG_ASCENT,
9262 JPEG_MARGIN,
9263 JPEG_RELIEF,
9264 JPEG_ALGORITHM,
9265 JPEG_HEURISTIC_MASK,
4a8e312c 9266 JPEG_MASK,
f20a3b7a 9267 JPEG_BACKGROUND,
333b20bb
GM
9268 JPEG_LAST
9269};
9270
9271/* Vector of image_keyword structures describing the format
9272 of valid user-defined image specifications. */
9273
9274static struct image_keyword jpeg_format[JPEG_LAST] =
9275{
9276 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9277 {":data", IMAGE_STRING_VALUE, 0},
8e39770a 9278 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9279 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9280 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9281 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9282 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9283 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9284 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9285 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9286};
9287
9288/* Structure describing the image type `jpeg'. */
9289
9290static struct image_type jpeg_type =
9291{
9292 &Qjpeg,
9293 jpeg_image_p,
9294 jpeg_load,
9295 x_clear_image,
9296 NULL
9297};
9298
9299
9300/* Return non-zero if OBJECT is a valid JPEG image specification. */
9301
9302static int
9303jpeg_image_p (object)
9304 Lisp_Object object;
9305{
9306 struct image_keyword fmt[JPEG_LAST];
9307
9308 bcopy (jpeg_format, fmt, sizeof fmt);
9309
7c7ff7f5 9310 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
333b20bb 9311 return 0;
8e39770a 9312
63cec32f
GM
9313 /* Must specify either the :data or :file keyword. */
9314 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
333b20bb
GM
9315}
9316
8e39770a 9317
333b20bb
GM
9318struct my_jpeg_error_mgr
9319{
9320 struct jpeg_error_mgr pub;
9321 jmp_buf setjmp_buffer;
9322};
9323
e3130015 9324
333b20bb
GM
9325static void
9326my_error_exit (cinfo)
9327 j_common_ptr cinfo;
9328{
9329 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
9330 longjmp (mgr->setjmp_buffer, 1);
9331}
9332
e3130015 9333
8e39770a
GM
9334/* Init source method for JPEG data source manager. Called by
9335 jpeg_read_header() before any data is actually read. See
9336 libjpeg.doc from the JPEG lib distribution. */
9337
9338static void
9339our_init_source (cinfo)
9340 j_decompress_ptr cinfo;
9341{
9342}
9343
9344
9345/* Fill input buffer method for JPEG data source manager. Called
9346 whenever more data is needed. We read the whole image in one step,
9347 so this only adds a fake end of input marker at the end. */
9348
9349static boolean
9350our_fill_input_buffer (cinfo)
9351 j_decompress_ptr cinfo;
9352{
9353 /* Insert a fake EOI marker. */
9354 struct jpeg_source_mgr *src = cinfo->src;
9355 static JOCTET buffer[2];
9356
9357 buffer[0] = (JOCTET) 0xFF;
9358 buffer[1] = (JOCTET) JPEG_EOI;
9359
9360 src->next_input_byte = buffer;
9361 src->bytes_in_buffer = 2;
9362 return TRUE;
9363}
9364
9365
9366/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
9367 is the JPEG data source manager. */
9368
9369static void
9370our_skip_input_data (cinfo, num_bytes)
9371 j_decompress_ptr cinfo;
9372 long num_bytes;
9373{
9374 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
9375
9376 if (src)
9377 {
9378 if (num_bytes > src->bytes_in_buffer)
5ad6a5fb 9379 ERREXIT (cinfo, JERR_INPUT_EOF);
8e39770a
GM
9380
9381 src->bytes_in_buffer -= num_bytes;
9382 src->next_input_byte += num_bytes;
9383 }
9384}
9385
9386
9387/* Method to terminate data source. Called by
9388 jpeg_finish_decompress() after all data has been processed. */
9389
9390static void
9391our_term_source (cinfo)
9392 j_decompress_ptr cinfo;
9393{
9394}
9395
9396
9397/* Set up the JPEG lib for reading an image from DATA which contains
9398 LEN bytes. CINFO is the decompression info structure created for
9399 reading the image. */
9400
9401static void
9402jpeg_memory_src (cinfo, data, len)
9403 j_decompress_ptr cinfo;
9404 JOCTET *data;
9405 unsigned int len;
9406{
9407 struct jpeg_source_mgr *src;
9408
9409 if (cinfo->src == NULL)
9410 {
9411 /* First time for this JPEG object? */
9412 cinfo->src = (struct jpeg_source_mgr *)
9413 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
9414 sizeof (struct jpeg_source_mgr));
9415 src = (struct jpeg_source_mgr *) cinfo->src;
9416 src->next_input_byte = data;
9417 }
9418
9419 src = (struct jpeg_source_mgr *) cinfo->src;
9420 src->init_source = our_init_source;
9421 src->fill_input_buffer = our_fill_input_buffer;
9422 src->skip_input_data = our_skip_input_data;
9423 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
9424 src->term_source = our_term_source;
9425 src->bytes_in_buffer = len;
9426 src->next_input_byte = data;
9427}
9428
5ad6a5fb 9429
333b20bb
GM
9430/* Load image IMG for use on frame F. Patterned after example.c
9431 from the JPEG lib. */
9432
9433static int
9434jpeg_load (f, img)
9435 struct frame *f;
9436 struct image *img;
9437{
9438 struct jpeg_decompress_struct cinfo;
9439 struct my_jpeg_error_mgr mgr;
9440 Lisp_Object file, specified_file;
8e39770a 9441 Lisp_Object specified_data;
8ec8a5ec 9442 FILE * volatile fp = NULL;
333b20bb
GM
9443 JSAMPARRAY buffer;
9444 int row_stride, x, y;
9445 XImage *ximg = NULL;
b6d7acec 9446 int rc;
333b20bb
GM
9447 unsigned long *colors;
9448 int width, height;
9449 struct gcpro gcpro1;
9450
9451 /* Open the JPEG file. */
9452 specified_file = image_spec_value (img->spec, QCfile, NULL);
8e39770a 9453 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
9454 file = Qnil;
9455 GCPRO1 (file);
8e39770a 9456
8e39770a 9457 if (NILP (specified_data))
333b20bb 9458 {
8e39770a 9459 file = x_find_image_file (specified_file);
8e39770a
GM
9460 if (!STRINGP (file))
9461 {
45158a91 9462 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8e39770a
GM
9463 UNGCPRO;
9464 return 0;
9465 }
333b20bb 9466
8e39770a
GM
9467 fp = fopen (XSTRING (file)->data, "r");
9468 if (fp == NULL)
9469 {
9470 image_error ("Cannot open `%s'", file, Qnil);
9471 UNGCPRO;
9472 return 0;
9473 }
333b20bb
GM
9474 }
9475
5ad6a5fb
GM
9476 /* Customize libjpeg's error handling to call my_error_exit when an
9477 error is detected. This function will perform a longjmp. */
333b20bb 9478 cinfo.err = jpeg_std_error (&mgr.pub);
14358466 9479 mgr.pub.error_exit = my_error_exit;
333b20bb
GM
9480
9481 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
9482 {
5ad6a5fb
GM
9483 if (rc == 1)
9484 {
9485 /* Called from my_error_exit. Display a JPEG error. */
9486 char buffer[JMSG_LENGTH_MAX];
9487 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
45158a91 9488 image_error ("Error reading JPEG image `%s': %s", img->spec,
5ad6a5fb
GM
9489 build_string (buffer));
9490 }
333b20bb
GM
9491
9492 /* Close the input file and destroy the JPEG object. */
5ad6a5fb 9493 if (fp)
8ec8a5ec 9494 fclose ((FILE *) fp);
333b20bb
GM
9495 jpeg_destroy_decompress (&cinfo);
9496
5ad6a5fb
GM
9497 /* If we already have an XImage, free that. */
9498 x_destroy_x_image (ximg);
333b20bb 9499
5ad6a5fb
GM
9500 /* Free pixmap and colors. */
9501 x_clear_image (f, img);
333b20bb 9502
5ad6a5fb
GM
9503 UNGCPRO;
9504 return 0;
333b20bb
GM
9505 }
9506
9507 /* Create the JPEG decompression object. Let it read from fp.
63448a4d 9508 Read the JPEG image header. */
333b20bb 9509 jpeg_create_decompress (&cinfo);
8e39770a
GM
9510
9511 if (NILP (specified_data))
8ec8a5ec 9512 jpeg_stdio_src (&cinfo, (FILE *) fp);
8e39770a
GM
9513 else
9514 jpeg_memory_src (&cinfo, XSTRING (specified_data)->data,
9515 STRING_BYTES (XSTRING (specified_data)));
63448a4d 9516
333b20bb
GM
9517 jpeg_read_header (&cinfo, TRUE);
9518
9519 /* Customize decompression so that color quantization will be used.
63448a4d 9520 Start decompression. */
333b20bb
GM
9521 cinfo.quantize_colors = TRUE;
9522 jpeg_start_decompress (&cinfo);
9523 width = img->width = cinfo.output_width;
9524 height = img->height = cinfo.output_height;
9525
333b20bb 9526 /* Create X image and pixmap. */
45158a91 9527 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
28c7826c 9528 longjmp (mgr.setjmp_buffer, 2);
333b20bb
GM
9529
9530 /* Allocate colors. When color quantization is used,
5ad6a5fb
GM
9531 cinfo.actual_number_of_colors has been set with the number of
9532 colors generated, and cinfo.colormap is a two-dimensional array
9533 of color indices in the range 0..cinfo.actual_number_of_colors.
9534 No more than 255 colors will be generated. */
333b20bb 9535 {
5ad6a5fb
GM
9536 int i, ir, ig, ib;
9537
9538 if (cinfo.out_color_components > 2)
9539 ir = 0, ig = 1, ib = 2;
9540 else if (cinfo.out_color_components > 1)
9541 ir = 0, ig = 1, ib = 0;
9542 else
9543 ir = 0, ig = 0, ib = 0;
9544
9545 /* Use the color table mechanism because it handles colors that
9546 cannot be allocated nicely. Such colors will be replaced with
9547 a default color, and we don't have to care about which colors
9548 can be freed safely, and which can't. */
9549 init_color_table ();
9550 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
9551 * sizeof *colors);
333b20bb 9552
5ad6a5fb
GM
9553 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
9554 {
9555 /* Multiply RGB values with 255 because X expects RGB values
9556 in the range 0..0xffff. */
9557 int r = cinfo.colormap[ir][i] << 8;
9558 int g = cinfo.colormap[ig][i] << 8;
9559 int b = cinfo.colormap[ib][i] << 8;
9560 colors[i] = lookup_rgb_color (f, r, g, b);
9561 }
333b20bb 9562
5ad6a5fb
GM
9563 /* Remember those colors actually allocated. */
9564 img->colors = colors_in_color_table (&img->ncolors);
9565 free_color_table ();
333b20bb
GM
9566 }
9567
9568 /* Read pixels. */
9569 row_stride = width * cinfo.output_components;
9570 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
5ad6a5fb 9571 row_stride, 1);
333b20bb
GM
9572 for (y = 0; y < height; ++y)
9573 {
5ad6a5fb
GM
9574 jpeg_read_scanlines (&cinfo, buffer, 1);
9575 for (x = 0; x < cinfo.output_width; ++x)
9576 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
333b20bb
GM
9577 }
9578
9579 /* Clean up. */
9580 jpeg_finish_decompress (&cinfo);
9581 jpeg_destroy_decompress (&cinfo);
5ad6a5fb 9582 if (fp)
8ec8a5ec 9583 fclose ((FILE *) fp);
f20a3b7a
MB
9584
9585 /* Maybe fill in the background field while we have ximg handy. */
9586 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9587 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
9588
9589 /* Put the image into the pixmap. */
9590 x_put_x_image (f, ximg, img->pixmap, width, height);
9591 x_destroy_x_image (ximg);
333b20bb
GM
9592 UNGCPRO;
9593 return 1;
9594}
9595
9596#endif /* HAVE_JPEG */
9597
9598
9599\f
9600/***********************************************************************
9601 TIFF
9602 ***********************************************************************/
9603
9604#if HAVE_TIFF
9605
cf4790ad 9606#include <tiffio.h>
333b20bb
GM
9607
9608static int tiff_image_p P_ ((Lisp_Object object));
9609static int tiff_load P_ ((struct frame *f, struct image *img));
9610
9611/* The symbol `tiff' identifying images of this type. */
9612
9613Lisp_Object Qtiff;
9614
9615/* Indices of image specification fields in tiff_format, below. */
9616
9617enum tiff_keyword_index
9618{
9619 TIFF_TYPE,
63448a4d 9620 TIFF_DATA,
333b20bb
GM
9621 TIFF_FILE,
9622 TIFF_ASCENT,
9623 TIFF_MARGIN,
9624 TIFF_RELIEF,
9625 TIFF_ALGORITHM,
9626 TIFF_HEURISTIC_MASK,
4a8e312c 9627 TIFF_MASK,
f20a3b7a 9628 TIFF_BACKGROUND,
333b20bb
GM
9629 TIFF_LAST
9630};
9631
9632/* Vector of image_keyword structures describing the format
9633 of valid user-defined image specifications. */
9634
9635static struct image_keyword tiff_format[TIFF_LAST] =
9636{
9637 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9638 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 9639 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9640 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9641 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9642 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9643 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9644 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9645 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9646 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9647};
9648
9649/* Structure describing the image type `tiff'. */
9650
9651static struct image_type tiff_type =
9652{
9653 &Qtiff,
9654 tiff_image_p,
9655 tiff_load,
9656 x_clear_image,
9657 NULL
9658};
9659
9660
9661/* Return non-zero if OBJECT is a valid TIFF image specification. */
9662
9663static int
9664tiff_image_p (object)
9665 Lisp_Object object;
9666{
9667 struct image_keyword fmt[TIFF_LAST];
9668 bcopy (tiff_format, fmt, sizeof fmt);
9669
7c7ff7f5 9670 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
333b20bb 9671 return 0;
5ad6a5fb 9672
63cec32f
GM
9673 /* Must specify either the :data or :file keyword. */
9674 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
333b20bb
GM
9675}
9676
5ad6a5fb
GM
9677
9678/* Reading from a memory buffer for TIFF images Based on the PNG
9679 memory source, but we have to provide a lot of extra functions.
9680 Blah.
63448a4d
WP
9681
9682 We really only need to implement read and seek, but I am not
9683 convinced that the TIFF library is smart enough not to destroy
9684 itself if we only hand it the function pointers we need to
5ad6a5fb
GM
9685 override. */
9686
9687typedef struct
9688{
63448a4d
WP
9689 unsigned char *bytes;
9690 size_t len;
9691 int index;
5ad6a5fb
GM
9692}
9693tiff_memory_source;
63448a4d 9694
e3130015 9695
5ad6a5fb
GM
9696static size_t
9697tiff_read_from_memory (data, buf, size)
9698 thandle_t data;
9699 tdata_t buf;
9700 tsize_t size;
63448a4d 9701{
5ad6a5fb 9702 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
9703
9704 if (size > src->len - src->index)
5ad6a5fb
GM
9705 return (size_t) -1;
9706 bcopy (src->bytes + src->index, buf, size);
63448a4d
WP
9707 src->index += size;
9708 return size;
9709}
9710
e3130015 9711
5ad6a5fb
GM
9712static size_t
9713tiff_write_from_memory (data, buf, size)
9714 thandle_t data;
9715 tdata_t buf;
9716 tsize_t size;
63448a4d
WP
9717{
9718 return (size_t) -1;
9719}
9720
e3130015 9721
5ad6a5fb
GM
9722static toff_t
9723tiff_seek_in_memory (data, off, whence)
9724 thandle_t data;
9725 toff_t off;
9726 int whence;
63448a4d 9727{
5ad6a5fb 9728 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
9729 int idx;
9730
9731 switch (whence)
5ad6a5fb
GM
9732 {
9733 case SEEK_SET: /* Go from beginning of source. */
9734 idx = off;
9735 break;
9736
9737 case SEEK_END: /* Go from end of source. */
9738 idx = src->len + off;
9739 break;
9740
9741 case SEEK_CUR: /* Go from current position. */
9742 idx = src->index + off;
9743 break;
9744
9745 default: /* Invalid `whence'. */
9746 return -1;
9747 }
9748
9749 if (idx > src->len || idx < 0)
9750 return -1;
9751
63448a4d
WP
9752 src->index = idx;
9753 return src->index;
9754}
9755
e3130015 9756
5ad6a5fb
GM
9757static int
9758tiff_close_memory (data)
9759 thandle_t data;
63448a4d
WP
9760{
9761 /* NOOP */
5ad6a5fb 9762 return 0;
63448a4d
WP
9763}
9764
e3130015 9765
5ad6a5fb
GM
9766static int
9767tiff_mmap_memory (data, pbase, psize)
9768 thandle_t data;
9769 tdata_t *pbase;
9770 toff_t *psize;
63448a4d
WP
9771{
9772 /* It is already _IN_ memory. */
5ad6a5fb 9773 return 0;
63448a4d
WP
9774}
9775
e3130015 9776
5ad6a5fb
GM
9777static void
9778tiff_unmap_memory (data, base, size)
9779 thandle_t data;
9780 tdata_t base;
9781 toff_t size;
63448a4d
WP
9782{
9783 /* We don't need to do this. */
63448a4d
WP
9784}
9785
e3130015 9786
5ad6a5fb
GM
9787static toff_t
9788tiff_size_of_memory (data)
9789 thandle_t data;
63448a4d 9790{
5ad6a5fb 9791 return ((tiff_memory_source *) data)->len;
63448a4d 9792}
333b20bb 9793
e3130015 9794
c6892044
GM
9795static void
9796tiff_error_handler (title, format, ap)
9797 const char *title, *format;
9798 va_list ap;
9799{
9800 char buf[512];
9801 int len;
9802
9803 len = sprintf (buf, "TIFF error: %s ", title);
9804 vsprintf (buf + len, format, ap);
9805 add_to_log (buf, Qnil, Qnil);
9806}
9807
9808
9809static void
9810tiff_warning_handler (title, format, ap)
9811 const char *title, *format;
9812 va_list ap;
9813{
9814 char buf[512];
9815 int len;
9816
9817 len = sprintf (buf, "TIFF warning: %s ", title);
9818 vsprintf (buf + len, format, ap);
9819 add_to_log (buf, Qnil, Qnil);
9820}
9821
9822
333b20bb
GM
9823/* Load TIFF image IMG for use on frame F. Value is non-zero if
9824 successful. */
9825
9826static int
9827tiff_load (f, img)
9828 struct frame *f;
9829 struct image *img;
9830{
9831 Lisp_Object file, specified_file;
63448a4d 9832 Lisp_Object specified_data;
333b20bb
GM
9833 TIFF *tiff;
9834 int width, height, x, y;
9835 uint32 *buf;
9836 int rc;
9837 XImage *ximg;
9838 struct gcpro gcpro1;
63448a4d 9839 tiff_memory_source memsrc;
333b20bb
GM
9840
9841 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 9842 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
9843 file = Qnil;
9844 GCPRO1 (file);
63448a4d 9845
c6892044
GM
9846 TIFFSetErrorHandler (tiff_error_handler);
9847 TIFFSetWarningHandler (tiff_warning_handler);
9848
63448a4d 9849 if (NILP (specified_data))
5ad6a5fb
GM
9850 {
9851 /* Read from a file */
9852 file = x_find_image_file (specified_file);
9853 if (!STRINGP (file))
63448a4d 9854 {
45158a91 9855 image_error ("Cannot find image file `%s'", file, Qnil);
5ad6a5fb
GM
9856 UNGCPRO;
9857 return 0;
9858 }
63448a4d 9859
5ad6a5fb
GM
9860 /* Try to open the image file. */
9861 tiff = TIFFOpen (XSTRING (file)->data, "r");
9862 if (tiff == NULL)
9863 {
9864 image_error ("Cannot open `%s'", file, Qnil);
9865 UNGCPRO;
9866 return 0;
63448a4d 9867 }
5ad6a5fb 9868 }
63448a4d 9869 else
5ad6a5fb
GM
9870 {
9871 /* Memory source! */
9872 memsrc.bytes = XSTRING (specified_data)->data;
9873 memsrc.len = STRING_BYTES (XSTRING (specified_data));
9874 memsrc.index = 0;
9875
9876 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
9877 (TIFFReadWriteProc) tiff_read_from_memory,
9878 (TIFFReadWriteProc) tiff_write_from_memory,
9879 tiff_seek_in_memory,
9880 tiff_close_memory,
9881 tiff_size_of_memory,
9882 tiff_mmap_memory,
9883 tiff_unmap_memory);
9884
9885 if (!tiff)
63448a4d 9886 {
45158a91 9887 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
5ad6a5fb
GM
9888 UNGCPRO;
9889 return 0;
63448a4d 9890 }
5ad6a5fb 9891 }
333b20bb
GM
9892
9893 /* Get width and height of the image, and allocate a raster buffer
9894 of width x height 32-bit values. */
9895 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
9896 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
9897 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
9898
9899 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
9900 TIFFClose (tiff);
9901 if (!rc)
9902 {
45158a91 9903 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
333b20bb
GM
9904 xfree (buf);
9905 UNGCPRO;
9906 return 0;
9907 }
9908
333b20bb 9909 /* Create the X image and pixmap. */
45158a91 9910 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 9911 {
333b20bb
GM
9912 xfree (buf);
9913 UNGCPRO;
9914 return 0;
9915 }
9916
9917 /* Initialize the color table. */
9918 init_color_table ();
9919
9920 /* Process the pixel raster. Origin is in the lower-left corner. */
9921 for (y = 0; y < height; ++y)
9922 {
9923 uint32 *row = buf + y * width;
9924
9925 for (x = 0; x < width; ++x)
9926 {
9927 uint32 abgr = row[x];
9928 int r = TIFFGetR (abgr) << 8;
9929 int g = TIFFGetG (abgr) << 8;
9930 int b = TIFFGetB (abgr) << 8;
9931 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
9932 }
9933 }
9934
9935 /* Remember the colors allocated for the image. Free the color table. */
9936 img->colors = colors_in_color_table (&img->ncolors);
9937 free_color_table ();
f20a3b7a
MB
9938
9939 img->width = width;
9940 img->height = height;
9941
9942 /* Maybe fill in the background field while we have ximg handy. */
9943 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9944 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
9945
9946 /* Put the image into the pixmap, then free the X image and its buffer. */
9947 x_put_x_image (f, ximg, img->pixmap, width, height);
9948 x_destroy_x_image (ximg);
9949 xfree (buf);
333b20bb
GM
9950
9951 UNGCPRO;
9952 return 1;
9953}
9954
9955#endif /* HAVE_TIFF != 0 */
9956
9957
9958\f
9959/***********************************************************************
9960 GIF
9961 ***********************************************************************/
9962
9963#if HAVE_GIF
9964
9965#include <gif_lib.h>
9966
9967static int gif_image_p P_ ((Lisp_Object object));
9968static int gif_load P_ ((struct frame *f, struct image *img));
9969
9970/* The symbol `gif' identifying images of this type. */
9971
9972Lisp_Object Qgif;
9973
9974/* Indices of image specification fields in gif_format, below. */
9975
9976enum gif_keyword_index
9977{
9978 GIF_TYPE,
63448a4d 9979 GIF_DATA,
333b20bb
GM
9980 GIF_FILE,
9981 GIF_ASCENT,
9982 GIF_MARGIN,
9983 GIF_RELIEF,
9984 GIF_ALGORITHM,
9985 GIF_HEURISTIC_MASK,
4a8e312c 9986 GIF_MASK,
333b20bb 9987 GIF_IMAGE,
f20a3b7a 9988 GIF_BACKGROUND,
333b20bb
GM
9989 GIF_LAST
9990};
9991
9992/* Vector of image_keyword structures describing the format
9993 of valid user-defined image specifications. */
9994
9995static struct image_keyword gif_format[GIF_LAST] =
9996{
9997 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9998 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 9999 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 10000 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 10001 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 10002 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 10003 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 10004 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 10005 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 10006 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
f20a3b7a 10007 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
10008};
10009
10010/* Structure describing the image type `gif'. */
10011
10012static struct image_type gif_type =
10013{
10014 &Qgif,
10015 gif_image_p,
10016 gif_load,
10017 x_clear_image,
10018 NULL
10019};
10020
e3130015 10021
333b20bb
GM
10022/* Return non-zero if OBJECT is a valid GIF image specification. */
10023
10024static int
10025gif_image_p (object)
10026 Lisp_Object object;
10027{
10028 struct image_keyword fmt[GIF_LAST];
10029 bcopy (gif_format, fmt, sizeof fmt);
10030
7c7ff7f5 10031 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
333b20bb 10032 return 0;
5ad6a5fb 10033
63cec32f
GM
10034 /* Must specify either the :data or :file keyword. */
10035 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
333b20bb
GM
10036}
10037
e3130015 10038
63448a4d
WP
10039/* Reading a GIF image from memory
10040 Based on the PNG memory stuff to a certain extent. */
10041
5ad6a5fb
GM
10042typedef struct
10043{
63448a4d
WP
10044 unsigned char *bytes;
10045 size_t len;
10046 int index;
5ad6a5fb
GM
10047}
10048gif_memory_source;
63448a4d 10049
e3130015 10050
f036834a
GM
10051/* Make the current memory source available to gif_read_from_memory.
10052 It's done this way because not all versions of libungif support
10053 a UserData field in the GifFileType structure. */
10054static gif_memory_source *current_gif_memory_src;
10055
5ad6a5fb
GM
10056static int
10057gif_read_from_memory (file, buf, len)
10058 GifFileType *file;
10059 GifByteType *buf;
10060 int len;
63448a4d 10061{
f036834a 10062 gif_memory_source *src = current_gif_memory_src;
63448a4d 10063
5ad6a5fb
GM
10064 if (len > src->len - src->index)
10065 return -1;
63448a4d 10066
5ad6a5fb 10067 bcopy (src->bytes + src->index, buf, len);
63448a4d
WP
10068 src->index += len;
10069 return len;
10070}
333b20bb 10071
5ad6a5fb 10072
333b20bb
GM
10073/* Load GIF image IMG for use on frame F. Value is non-zero if
10074 successful. */
10075
10076static int
10077gif_load (f, img)
10078 struct frame *f;
10079 struct image *img;
10080{
10081 Lisp_Object file, specified_file;
63448a4d 10082 Lisp_Object specified_data;
333b20bb
GM
10083 int rc, width, height, x, y, i;
10084 XImage *ximg;
10085 ColorMapObject *gif_color_map;
10086 unsigned long pixel_colors[256];
10087 GifFileType *gif;
10088 struct gcpro gcpro1;
10089 Lisp_Object image;
10090 int ino, image_left, image_top, image_width, image_height;
63448a4d 10091 gif_memory_source memsrc;
9b784e96 10092 unsigned char *raster;
333b20bb
GM
10093
10094 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 10095 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
10096 file = Qnil;
10097 GCPRO1 (file);
63448a4d
WP
10098
10099 if (NILP (specified_data))
5ad6a5fb
GM
10100 {
10101 file = x_find_image_file (specified_file);
10102 if (!STRINGP (file))
63448a4d 10103 {
45158a91 10104 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
10105 UNGCPRO;
10106 return 0;
10107 }
333b20bb 10108
5ad6a5fb
GM
10109 /* Open the GIF file. */
10110 gif = DGifOpenFileName (XSTRING (file)->data);
10111 if (gif == NULL)
10112 {
10113 image_error ("Cannot open `%s'", file, Qnil);
10114 UNGCPRO;
10115 return 0;
63448a4d 10116 }
5ad6a5fb 10117 }
63448a4d 10118 else
5ad6a5fb
GM
10119 {
10120 /* Read from memory! */
f036834a 10121 current_gif_memory_src = &memsrc;
5ad6a5fb
GM
10122 memsrc.bytes = XSTRING (specified_data)->data;
10123 memsrc.len = STRING_BYTES (XSTRING (specified_data));
10124 memsrc.index = 0;
63448a4d 10125
5ad6a5fb
GM
10126 gif = DGifOpen(&memsrc, gif_read_from_memory);
10127 if (!gif)
10128 {
45158a91 10129 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
5ad6a5fb
GM
10130 UNGCPRO;
10131 return 0;
63448a4d 10132 }
5ad6a5fb 10133 }
333b20bb
GM
10134
10135 /* Read entire contents. */
10136 rc = DGifSlurp (gif);
10137 if (rc == GIF_ERROR)
10138 {
45158a91 10139 image_error ("Error reading `%s'", img->spec, Qnil);
333b20bb
GM
10140 DGifCloseFile (gif);
10141 UNGCPRO;
10142 return 0;
10143 }
10144
3ccff1e3 10145 image = image_spec_value (img->spec, QCindex, NULL);
333b20bb
GM
10146 ino = INTEGERP (image) ? XFASTINT (image) : 0;
10147 if (ino >= gif->ImageCount)
10148 {
45158a91
GM
10149 image_error ("Invalid image number `%s' in image `%s'",
10150 image, img->spec);
333b20bb
GM
10151 DGifCloseFile (gif);
10152 UNGCPRO;
10153 return 0;
10154 }
10155
c7f07c4c
PJ
10156 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
10157 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
333b20bb 10158
333b20bb 10159 /* Create the X image and pixmap. */
45158a91 10160 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 10161 {
333b20bb
GM
10162 DGifCloseFile (gif);
10163 UNGCPRO;
10164 return 0;
10165 }
10166
10167 /* Allocate colors. */
10168 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
10169 if (!gif_color_map)
10170 gif_color_map = gif->SColorMap;
10171 init_color_table ();
10172 bzero (pixel_colors, sizeof pixel_colors);
10173
10174 for (i = 0; i < gif_color_map->ColorCount; ++i)
10175 {
10176 int r = gif_color_map->Colors[i].Red << 8;
10177 int g = gif_color_map->Colors[i].Green << 8;
10178 int b = gif_color_map->Colors[i].Blue << 8;
10179 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
10180 }
10181
10182 img->colors = colors_in_color_table (&img->ncolors);
10183 free_color_table ();
10184
10185 /* Clear the part of the screen image that are not covered by
10186 the image from the GIF file. Full animated GIF support
10187 requires more than can be done here (see the gif89 spec,
10188 disposal methods). Let's simply assume that the part
10189 not covered by a sub-image is in the frame's background color. */
10190 image_top = gif->SavedImages[ino].ImageDesc.Top;
10191 image_left = gif->SavedImages[ino].ImageDesc.Left;
10192 image_width = gif->SavedImages[ino].ImageDesc.Width;
10193 image_height = gif->SavedImages[ino].ImageDesc.Height;
10194
10195 for (y = 0; y < image_top; ++y)
10196 for (x = 0; x < width; ++x)
10197 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10198
10199 for (y = image_top + image_height; y < height; ++y)
10200 for (x = 0; x < width; ++x)
10201 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10202
10203 for (y = image_top; y < image_top + image_height; ++y)
10204 {
10205 for (x = 0; x < image_left; ++x)
10206 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10207 for (x = image_left + image_width; x < width; ++x)
10208 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10209 }
10210
9b784e96
GM
10211 /* Read the GIF image into the X image. We use a local variable
10212 `raster' here because RasterBits below is a char *, and invites
10213 problems with bytes >= 0x80. */
10214 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
10215
333b20bb
GM
10216 if (gif->SavedImages[ino].ImageDesc.Interlace)
10217 {
10218 static int interlace_start[] = {0, 4, 2, 1};
10219 static int interlace_increment[] = {8, 8, 4, 2};
9b207e8e 10220 int pass;
06482119
GM
10221 int row = interlace_start[0];
10222
10223 pass = 0;
333b20bb 10224
06482119 10225 for (y = 0; y < image_height; y++)
333b20bb 10226 {
06482119
GM
10227 if (row >= image_height)
10228 {
10229 row = interlace_start[++pass];
10230 while (row >= image_height)
10231 row = interlace_start[++pass];
10232 }
10233
10234 for (x = 0; x < image_width; x++)
10235 {
9b784e96 10236 int i = raster[(y * image_width) + x];
06482119
GM
10237 XPutPixel (ximg, x + image_left, row + image_top,
10238 pixel_colors[i]);
10239 }
10240
10241 row += interlace_increment[pass];
333b20bb
GM
10242 }
10243 }
10244 else
10245 {
10246 for (y = 0; y < image_height; ++y)
10247 for (x = 0; x < image_width; ++x)
10248 {
9b784e96 10249 int i = raster[y * image_width + x];
333b20bb
GM
10250 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
10251 }
10252 }
10253
10254 DGifCloseFile (gif);
f20a3b7a
MB
10255
10256 /* Maybe fill in the background field while we have ximg handy. */
10257 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
10258 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
10259
10260 /* Put the image into the pixmap, then free the X image and its buffer. */
10261 x_put_x_image (f, ximg, img->pixmap, width, height);
10262 x_destroy_x_image (ximg);
333b20bb
GM
10263
10264 UNGCPRO;
10265 return 1;
10266}
10267
10268#endif /* HAVE_GIF != 0 */
10269
10270
10271\f
10272/***********************************************************************
10273 Ghostscript
10274 ***********************************************************************/
10275
10276static int gs_image_p P_ ((Lisp_Object object));
10277static int gs_load P_ ((struct frame *f, struct image *img));
10278static void gs_clear_image P_ ((struct frame *f, struct image *img));
10279
fcf431dc 10280/* The symbol `postscript' identifying images of this type. */
333b20bb 10281
fcf431dc 10282Lisp_Object Qpostscript;
333b20bb
GM
10283
10284/* Keyword symbols. */
10285
10286Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
10287
10288/* Indices of image specification fields in gs_format, below. */
10289
10290enum gs_keyword_index
10291{
10292 GS_TYPE,
10293 GS_PT_WIDTH,
10294 GS_PT_HEIGHT,
10295 GS_FILE,
10296 GS_LOADER,
10297 GS_BOUNDING_BOX,
10298 GS_ASCENT,
10299 GS_MARGIN,
10300 GS_RELIEF,
10301 GS_ALGORITHM,
10302 GS_HEURISTIC_MASK,
4a8e312c 10303 GS_MASK,
f20a3b7a 10304 GS_BACKGROUND,
333b20bb
GM
10305 GS_LAST
10306};
10307
10308/* Vector of image_keyword structures describing the format
10309 of valid user-defined image specifications. */
10310
10311static struct image_keyword gs_format[GS_LAST] =
10312{
10313 {":type", IMAGE_SYMBOL_VALUE, 1},
10314 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10315 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10316 {":file", IMAGE_STRING_VALUE, 1},
10317 {":loader", IMAGE_FUNCTION_VALUE, 0},
10318 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
7c7ff7f5 10319 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 10320 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 10321 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 10322 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 10323 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
10324 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10325 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
10326};
10327
10328/* Structure describing the image type `ghostscript'. */
10329
10330static struct image_type gs_type =
10331{
fcf431dc 10332 &Qpostscript,
333b20bb
GM
10333 gs_image_p,
10334 gs_load,
10335 gs_clear_image,
10336 NULL
10337};
10338
10339
10340/* Free X resources of Ghostscript image IMG which is used on frame F. */
10341
10342static void
10343gs_clear_image (f, img)
10344 struct frame *f;
10345 struct image *img;
10346{
10347 /* IMG->data.ptr_val may contain a recorded colormap. */
10348 xfree (img->data.ptr_val);
10349 x_clear_image (f, img);
10350}
10351
10352
10353/* Return non-zero if OBJECT is a valid Ghostscript image
10354 specification. */
10355
10356static int
10357gs_image_p (object)
10358 Lisp_Object object;
10359{
10360 struct image_keyword fmt[GS_LAST];
10361 Lisp_Object tem;
10362 int i;
10363
10364 bcopy (gs_format, fmt, sizeof fmt);
10365
7c7ff7f5 10366 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
333b20bb
GM
10367 return 0;
10368
10369 /* Bounding box must be a list or vector containing 4 integers. */
10370 tem = fmt[GS_BOUNDING_BOX].value;
10371 if (CONSP (tem))
10372 {
10373 for (i = 0; i < 4; ++i, tem = XCDR (tem))
10374 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
10375 return 0;
10376 if (!NILP (tem))
10377 return 0;
10378 }
10379 else if (VECTORP (tem))
10380 {
10381 if (XVECTOR (tem)->size != 4)
10382 return 0;
10383 for (i = 0; i < 4; ++i)
10384 if (!INTEGERP (XVECTOR (tem)->contents[i]))
10385 return 0;
10386 }
10387 else
10388 return 0;
10389
10390 return 1;
10391}
10392
10393
10394/* Load Ghostscript image IMG for use on frame F. Value is non-zero
10395 if successful. */
10396
10397static int
10398gs_load (f, img)
10399 struct frame *f;
10400 struct image *img;
10401{
10402 char buffer[100];
10403 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
10404 struct gcpro gcpro1, gcpro2;
10405 Lisp_Object frame;
10406 double in_width, in_height;
10407 Lisp_Object pixel_colors = Qnil;
10408
10409 /* Compute pixel size of pixmap needed from the given size in the
10410 image specification. Sizes in the specification are in pt. 1 pt
10411 = 1/72 in, xdpi and ydpi are stored in the frame's X display
10412 info. */
10413 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
10414 in_width = XFASTINT (pt_width) / 72.0;
10415 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
10416 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
10417 in_height = XFASTINT (pt_height) / 72.0;
10418 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
10419
10420 /* Create the pixmap. */
dd00328a 10421 xassert (img->pixmap == None);
333b20bb
GM
10422 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10423 img->width, img->height,
10424 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
333b20bb
GM
10425
10426 if (!img->pixmap)
10427 {
45158a91 10428 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
10429 return 0;
10430 }
10431
10432 /* Call the loader to fill the pixmap. It returns a process object
10433 if successful. We do not record_unwind_protect here because
10434 other places in redisplay like calling window scroll functions
10435 don't either. Let the Lisp loader use `unwind-protect' instead. */
10436 GCPRO2 (window_and_pixmap_id, pixel_colors);
10437
10438 sprintf (buffer, "%lu %lu",
10439 (unsigned long) FRAME_X_WINDOW (f),
10440 (unsigned long) img->pixmap);
10441 window_and_pixmap_id = build_string (buffer);
10442
10443 sprintf (buffer, "%lu %lu",
10444 FRAME_FOREGROUND_PIXEL (f),
10445 FRAME_BACKGROUND_PIXEL (f));
10446 pixel_colors = build_string (buffer);
10447
10448 XSETFRAME (frame, f);
10449 loader = image_spec_value (img->spec, QCloader, NULL);
10450 if (NILP (loader))
10451 loader = intern ("gs-load-image");
10452
10453 img->data.lisp_val = call6 (loader, frame, img->spec,
10454 make_number (img->width),
10455 make_number (img->height),
10456 window_and_pixmap_id,
10457 pixel_colors);
10458 UNGCPRO;
10459 return PROCESSP (img->data.lisp_val);
10460}
10461
10462
10463/* Kill the Ghostscript process that was started to fill PIXMAP on
10464 frame F. Called from XTread_socket when receiving an event
10465 telling Emacs that Ghostscript has finished drawing. */
10466
10467void
10468x_kill_gs_process (pixmap, f)
10469 Pixmap pixmap;
10470 struct frame *f;
10471{
10472 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
10473 int class, i;
10474 struct image *img;
10475
10476 /* Find the image containing PIXMAP. */
10477 for (i = 0; i < c->used; ++i)
10478 if (c->images[i]->pixmap == pixmap)
10479 break;
10480
daba7643
GM
10481 /* Should someone in between have cleared the image cache, for
10482 instance, give up. */
10483 if (i == c->used)
10484 return;
10485
333b20bb
GM
10486 /* Kill the GS process. We should have found PIXMAP in the image
10487 cache and its image should contain a process object. */
333b20bb
GM
10488 img = c->images[i];
10489 xassert (PROCESSP (img->data.lisp_val));
10490 Fkill_process (img->data.lisp_val, Qnil);
10491 img->data.lisp_val = Qnil;
10492
10493 /* On displays with a mutable colormap, figure out the colors
10494 allocated for the image by looking at the pixels of an XImage for
10495 img->pixmap. */
383d6ffc 10496 class = FRAME_X_VISUAL (f)->class;
333b20bb
GM
10497 if (class != StaticColor && class != StaticGray && class != TrueColor)
10498 {
10499 XImage *ximg;
10500
10501 BLOCK_INPUT;
10502
10503 /* Try to get an XImage for img->pixmep. */
10504 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
10505 0, 0, img->width, img->height, ~0, ZPixmap);
10506 if (ximg)
10507 {
10508 int x, y;
10509
10510 /* Initialize the color table. */
10511 init_color_table ();
10512
10513 /* For each pixel of the image, look its color up in the
10514 color table. After having done so, the color table will
10515 contain an entry for each color used by the image. */
10516 for (y = 0; y < img->height; ++y)
10517 for (x = 0; x < img->width; ++x)
10518 {
10519 unsigned long pixel = XGetPixel (ximg, x, y);
10520 lookup_pixel_color (f, pixel);
10521 }
10522
10523 /* Record colors in the image. Free color table and XImage. */
10524 img->colors = colors_in_color_table (&img->ncolors);
10525 free_color_table ();
10526 XDestroyImage (ximg);
10527
10528#if 0 /* This doesn't seem to be the case. If we free the colors
10529 here, we get a BadAccess later in x_clear_image when
10530 freeing the colors. */
10531 /* We have allocated colors once, but Ghostscript has also
10532 allocated colors on behalf of us. So, to get the
10533 reference counts right, free them once. */
10534 if (img->ncolors)
462d5d40 10535 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
10536#endif
10537 }
10538 else
10539 image_error ("Cannot get X image of `%s'; colors will not be freed",
45158a91 10540 img->spec, Qnil);
333b20bb
GM
10541
10542 UNBLOCK_INPUT;
10543 }
ad18ffb1
GM
10544
10545 /* Now that we have the pixmap, compute mask and transform the
10546 image if requested. */
10547 BLOCK_INPUT;
10548 postprocess_image (f, img);
10549 UNBLOCK_INPUT;
333b20bb
GM
10550}
10551
10552
10553\f
10554/***********************************************************************
10555 Window properties
10556 ***********************************************************************/
10557
10558DEFUN ("x-change-window-property", Fx_change_window_property,
10559 Sx_change_window_property, 2, 3, 0,
7ee72033 10560 doc: /* Change window property PROP to VALUE on the X window of FRAME.
c061c855 10561PROP and VALUE must be strings. FRAME nil or omitted means use the
7ee72033
MB
10562selected frame. Value is VALUE. */)
10563 (prop, value, frame)
333b20bb
GM
10564 Lisp_Object frame, prop, value;
10565{
10566 struct frame *f = check_x_frame (frame);
10567 Atom prop_atom;
10568
b7826503
PJ
10569 CHECK_STRING (prop);
10570 CHECK_STRING (value);
333b20bb
GM
10571
10572 BLOCK_INPUT;
10573 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10574 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10575 prop_atom, XA_STRING, 8, PropModeReplace,
10576 XSTRING (value)->data, XSTRING (value)->size);
10577
10578 /* Make sure the property is set when we return. */
10579 XFlush (FRAME_X_DISPLAY (f));
10580 UNBLOCK_INPUT;
10581
10582 return value;
10583}
10584
10585
10586DEFUN ("x-delete-window-property", Fx_delete_window_property,
10587 Sx_delete_window_property, 1, 2, 0,
7ee72033
MB
10588 doc: /* Remove window property PROP from X window of FRAME.
10589FRAME nil or omitted means use the selected frame. Value is PROP. */)
10590 (prop, frame)
333b20bb
GM
10591 Lisp_Object prop, frame;
10592{
10593 struct frame *f = check_x_frame (frame);
10594 Atom prop_atom;
10595
b7826503 10596 CHECK_STRING (prop);
333b20bb
GM
10597 BLOCK_INPUT;
10598 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10599 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
10600
10601 /* Make sure the property is removed when we return. */
10602 XFlush (FRAME_X_DISPLAY (f));
10603 UNBLOCK_INPUT;
10604
10605 return prop;
10606}
10607
10608
10609DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
10610 1, 2, 0,
7ee72033 10611 doc: /* Value is the value of window property PROP on FRAME.
c061c855
GM
10612If FRAME is nil or omitted, use the selected frame. Value is nil
10613if FRAME hasn't a property with name PROP or if PROP has no string
7ee72033
MB
10614value. */)
10615 (prop, frame)
333b20bb
GM
10616 Lisp_Object prop, frame;
10617{
10618 struct frame *f = check_x_frame (frame);
10619 Atom prop_atom;
10620 int rc;
10621 Lisp_Object prop_value = Qnil;
10622 char *tmp_data = NULL;
10623 Atom actual_type;
10624 int actual_format;
10625 unsigned long actual_size, bytes_remaining;
10626
b7826503 10627 CHECK_STRING (prop);
333b20bb
GM
10628 BLOCK_INPUT;
10629 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), XSTRING (prop)->data, False);
10630 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10631 prop_atom, 0, 0, False, XA_STRING,
10632 &actual_type, &actual_format, &actual_size,
10633 &bytes_remaining, (unsigned char **) &tmp_data);
10634 if (rc == Success)
10635 {
10636 int size = bytes_remaining;
10637
10638 XFree (tmp_data);
10639 tmp_data = NULL;
10640
10641 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10642 prop_atom, 0, bytes_remaining,
10643 False, XA_STRING,
10644 &actual_type, &actual_format,
10645 &actual_size, &bytes_remaining,
10646 (unsigned char **) &tmp_data);
4c8c7926 10647 if (rc == Success && tmp_data)
333b20bb
GM
10648 prop_value = make_string (tmp_data, size);
10649
10650 XFree (tmp_data);
10651 }
10652
10653 UNBLOCK_INPUT;
10654 return prop_value;
10655}
10656
10657
10658\f
10659/***********************************************************************
10660 Busy cursor
10661 ***********************************************************************/
10662
4ae9a85e 10663/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 10664 an hourglass cursor on all frames. */
333b20bb 10665
0af913d7 10666static struct atimer *hourglass_atimer;
333b20bb 10667
0af913d7 10668/* Non-zero means an hourglass cursor is currently shown. */
333b20bb 10669
0af913d7 10670static int hourglass_shown_p;
333b20bb 10671
0af913d7 10672/* Number of seconds to wait before displaying an hourglass cursor. */
333b20bb 10673
0af913d7 10674static Lisp_Object Vhourglass_delay;
333b20bb 10675
0af913d7 10676/* Default number of seconds to wait before displaying an hourglass
4ae9a85e
GM
10677 cursor. */
10678
0af913d7 10679#define DEFAULT_HOURGLASS_DELAY 1
4ae9a85e
GM
10680
10681/* Function prototypes. */
10682
0af913d7
GM
10683static void show_hourglass P_ ((struct atimer *));
10684static void hide_hourglass P_ ((void));
4ae9a85e
GM
10685
10686
0af913d7 10687/* Cancel a currently active hourglass timer, and start a new one. */
4ae9a85e
GM
10688
10689void
0af913d7 10690start_hourglass ()
333b20bb 10691{
4ae9a85e 10692 EMACS_TIME delay;
3caa99d3 10693 int secs, usecs = 0;
4ae9a85e 10694
0af913d7 10695 cancel_hourglass ();
4ae9a85e 10696
0af913d7
GM
10697 if (INTEGERP (Vhourglass_delay)
10698 && XINT (Vhourglass_delay) > 0)
10699 secs = XFASTINT (Vhourglass_delay);
10700 else if (FLOATP (Vhourglass_delay)
10701 && XFLOAT_DATA (Vhourglass_delay) > 0)
3caa99d3
GM
10702 {
10703 Lisp_Object tem;
0af913d7 10704 tem = Ftruncate (Vhourglass_delay, Qnil);
3caa99d3 10705 secs = XFASTINT (tem);
0af913d7 10706 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
3caa99d3 10707 }
4ae9a85e 10708 else
0af913d7 10709 secs = DEFAULT_HOURGLASS_DELAY;
4ae9a85e 10710
3caa99d3 10711 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
10712 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
10713 show_hourglass, NULL);
4ae9a85e
GM
10714}
10715
10716
0af913d7 10717/* Cancel the hourglass cursor timer if active, hide a busy cursor if
4ae9a85e
GM
10718 shown. */
10719
10720void
0af913d7 10721cancel_hourglass ()
4ae9a85e 10722{
0af913d7 10723 if (hourglass_atimer)
99f01f62 10724 {
0af913d7
GM
10725 cancel_atimer (hourglass_atimer);
10726 hourglass_atimer = NULL;
99f01f62
GM
10727 }
10728
0af913d7
GM
10729 if (hourglass_shown_p)
10730 hide_hourglass ();
4ae9a85e
GM
10731}
10732
10733
0af913d7
GM
10734/* Timer function of hourglass_atimer. TIMER is equal to
10735 hourglass_atimer.
4ae9a85e 10736
0af913d7
GM
10737 Display an hourglass pointer on all frames by mapping the frames'
10738 hourglass_window. Set the hourglass_p flag in the frames'
10739 output_data.x structure to indicate that an hourglass cursor is
10740 shown on the frames. */
4ae9a85e
GM
10741
10742static void
0af913d7 10743show_hourglass (timer)
4ae9a85e
GM
10744 struct atimer *timer;
10745{
10746 /* The timer implementation will cancel this timer automatically
0af913d7 10747 after this function has run. Set hourglass_atimer to null
4ae9a85e 10748 so that we know the timer doesn't have to be canceled. */
0af913d7 10749 hourglass_atimer = NULL;
4ae9a85e 10750
0af913d7 10751 if (!hourglass_shown_p)
333b20bb
GM
10752 {
10753 Lisp_Object rest, frame;
4ae9a85e
GM
10754
10755 BLOCK_INPUT;
10756
333b20bb 10757 FOR_EACH_FRAME (rest, frame)
5f7a1890
GM
10758 {
10759 struct frame *f = XFRAME (frame);
10760
10761 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
10762 {
10763 Display *dpy = FRAME_X_DISPLAY (f);
10764
10765#ifdef USE_X_TOOLKIT
10766 if (f->output_data.x->widget)
10767#else
10768 if (FRAME_OUTER_WINDOW (f))
10769#endif
10770 {
0af913d7 10771 f->output_data.x->hourglass_p = 1;
4ae9a85e 10772
0af913d7 10773 if (!f->output_data.x->hourglass_window)
5f7a1890
GM
10774 {
10775 unsigned long mask = CWCursor;
10776 XSetWindowAttributes attrs;
4ae9a85e 10777
0af913d7 10778 attrs.cursor = f->output_data.x->hourglass_cursor;
4ae9a85e 10779
0af913d7 10780 f->output_data.x->hourglass_window
5f7a1890
GM
10781 = XCreateWindow (dpy, FRAME_OUTER_WINDOW (f),
10782 0, 0, 32000, 32000, 0, 0,
10783 InputOnly,
10784 CopyFromParent,
10785 mask, &attrs);
10786 }
4ae9a85e 10787
0af913d7 10788 XMapRaised (dpy, f->output_data.x->hourglass_window);
5f7a1890
GM
10789 XFlush (dpy);
10790 }
10791 }
10792 }
333b20bb 10793
0af913d7 10794 hourglass_shown_p = 1;
4ae9a85e
GM
10795 UNBLOCK_INPUT;
10796 }
333b20bb
GM
10797}
10798
10799
0af913d7
GM
10800/* Hide the hourglass pointer on all frames, if it is currently
10801 shown. */
333b20bb 10802
4ae9a85e 10803static void
0af913d7 10804hide_hourglass ()
4ae9a85e 10805{
0af913d7 10806 if (hourglass_shown_p)
333b20bb 10807 {
4ae9a85e
GM
10808 Lisp_Object rest, frame;
10809
10810 BLOCK_INPUT;
10811 FOR_EACH_FRAME (rest, frame)
333b20bb 10812 {
4ae9a85e
GM
10813 struct frame *f = XFRAME (frame);
10814
10815 if (FRAME_X_P (f)
10816 /* Watch out for newly created frames. */
0af913d7 10817 && f->output_data.x->hourglass_window)
4ae9a85e 10818 {
0af913d7
GM
10819 XUnmapWindow (FRAME_X_DISPLAY (f),
10820 f->output_data.x->hourglass_window);
10821 /* Sync here because XTread_socket looks at the
10822 hourglass_p flag that is reset to zero below. */
4ae9a85e 10823 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 10824 f->output_data.x->hourglass_p = 0;
4ae9a85e 10825 }
333b20bb 10826 }
333b20bb 10827
0af913d7 10828 hourglass_shown_p = 0;
4ae9a85e
GM
10829 UNBLOCK_INPUT;
10830 }
333b20bb
GM
10831}
10832
10833
10834\f
10835/***********************************************************************
10836 Tool tips
10837 ***********************************************************************/
10838
10839static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
275841bf 10840 Lisp_Object, Lisp_Object));
06d62053 10841static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
ab452f99 10842 Lisp_Object, int, int, int *, int *));
333b20bb 10843
44b5a125 10844/* The frame of a currently visible tooltip. */
333b20bb 10845
44b5a125 10846Lisp_Object tip_frame;
333b20bb
GM
10847
10848/* If non-nil, a timer started that hides the last tooltip when it
10849 fires. */
10850
10851Lisp_Object tip_timer;
10852Window tip_window;
10853
06d62053
GM
10854/* If non-nil, a vector of 3 elements containing the last args
10855 with which x-show-tip was called. See there. */
10856
10857Lisp_Object last_show_tip_args;
10858
d63931a2
GM
10859/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
10860
10861Lisp_Object Vx_max_tooltip_size;
10862
eaf1eea9
GM
10863
10864static Lisp_Object
10865unwind_create_tip_frame (frame)
10866 Lisp_Object frame;
10867{
c844a81a
GM
10868 Lisp_Object deleted;
10869
10870 deleted = unwind_create_frame (frame);
10871 if (EQ (deleted, Qt))
10872 {
10873 tip_window = None;
10874 tip_frame = Qnil;
10875 }
10876
10877 return deleted;
eaf1eea9
GM
10878}
10879
10880
333b20bb 10881/* Create a frame for a tooltip on the display described by DPYINFO.
275841bf
GM
10882 PARMS is a list of frame parameters. TEXT is the string to
10883 display in the tip frame. Value is the frame.
eaf1eea9
GM
10884
10885 Note that functions called here, esp. x_default_parameter can
10886 signal errors, for instance when a specified color name is
10887 undefined. We have to make sure that we're in a consistent state
10888 when this happens. */
333b20bb
GM
10889
10890static Lisp_Object
275841bf 10891x_create_tip_frame (dpyinfo, parms, text)
333b20bb 10892 struct x_display_info *dpyinfo;
275841bf 10893 Lisp_Object parms, text;
333b20bb
GM
10894{
10895 struct frame *f;
10896 Lisp_Object frame, tem;
10897 Lisp_Object name;
333b20bb
GM
10898 long window_prompting = 0;
10899 int width, height;
eaf1eea9 10900 int count = BINDING_STACK_SIZE ();
b6d7acec 10901 struct gcpro gcpro1, gcpro2, gcpro3;
333b20bb 10902 struct kboard *kb;
06d62053 10903 int face_change_count_before = face_change_count;
275841bf
GM
10904 Lisp_Object buffer;
10905 struct buffer *old_buffer;
333b20bb
GM
10906
10907 check_x ();
10908
10909 /* Use this general default value to start with until we know if
10910 this frame has a specified name. */
10911 Vx_resource_name = Vinvocation_name;
10912
10913#ifdef MULTI_KBOARD
10914 kb = dpyinfo->kboard;
10915#else
10916 kb = &the_only_kboard;
10917#endif
10918
10919 /* Get the name of the frame to use for resource lookup. */
10920 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
10921 if (!STRINGP (name)
10922 && !EQ (name, Qunbound)
10923 && !NILP (name))
10924 error ("Invalid frame name--not a string or nil");
10925 Vx_resource_name = name;
10926
10927 frame = Qnil;
10928 GCPRO3 (parms, name, frame);
44b5a125 10929 f = make_frame (1);
333b20bb 10930 XSETFRAME (frame, f);
275841bf
GM
10931
10932 buffer = Fget_buffer_create (build_string (" *tip*"));
10933 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
10934 old_buffer = current_buffer;
10935 set_buffer_internal_1 (XBUFFER (buffer));
d63931a2 10936 current_buffer->truncate_lines = Qnil;
275841bf
GM
10937 Ferase_buffer ();
10938 Finsert (1, &text);
10939 set_buffer_internal_1 (old_buffer);
10940
333b20bb 10941 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
8a1a7743 10942 record_unwind_protect (unwind_create_tip_frame, frame);
333b20bb 10943
eaf1eea9
GM
10944 /* By setting the output method, we're essentially saying that
10945 the frame is live, as per FRAME_LIVE_P. If we get a signal
10946 from this point on, x_destroy_window might screw up reference
10947 counts etc. */
333b20bb
GM
10948 f->output_method = output_x_window;
10949 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
10950 bzero (f->output_data.x, sizeof (struct x_output));
10951 f->output_data.x->icon_bitmap = -1;
10952 f->output_data.x->fontset = -1;
61d461a8
GM
10953 f->output_data.x->scroll_bar_foreground_pixel = -1;
10954 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
10955#ifdef USE_TOOLKIT_SCROLL_BARS
10956 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
10957 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
10958#endif /* USE_TOOLKIT_SCROLL_BARS */
333b20bb
GM
10959 f->icon_name = Qnil;
10960 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 10961#if GLYPH_DEBUG
eaf1eea9
GM
10962 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
10963 dpyinfo_refcount = dpyinfo->reference_count;
10964#endif /* GLYPH_DEBUG */
333b20bb
GM
10965#ifdef MULTI_KBOARD
10966 FRAME_KBOARD (f) = kb;
10967#endif
10968 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10969 f->output_data.x->explicit_parent = 0;
10970
61d461a8
GM
10971 /* These colors will be set anyway later, but it's important
10972 to get the color reference counts right, so initialize them! */
10973 {
10974 Lisp_Object black;
10975 struct gcpro gcpro1;
10976
10977 black = build_string ("black");
10978 GCPRO1 (black);
10979 f->output_data.x->foreground_pixel
10980 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10981 f->output_data.x->background_pixel
10982 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10983 f->output_data.x->cursor_pixel
10984 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10985 f->output_data.x->cursor_foreground_pixel
10986 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10987 f->output_data.x->border_pixel
10988 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10989 f->output_data.x->mouse_pixel
10990 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10991 UNGCPRO;
10992 }
10993
333b20bb
GM
10994 /* Set the name; the functions to which we pass f expect the name to
10995 be set. */
10996 if (EQ (name, Qunbound) || NILP (name))
10997 {
10998 f->name = build_string (dpyinfo->x_id_name);
10999 f->explicit_name = 0;
11000 }
11001 else
11002 {
11003 f->name = name;
11004 f->explicit_name = 1;
11005 /* use the frame's title when getting resources for this frame. */
11006 specbind (Qx_resource_name, name);
11007 }
11008
eaf1eea9
GM
11009 /* Extract the window parameters from the supplied values that are
11010 needed to determine window geometry. */
333b20bb
GM
11011 {
11012 Lisp_Object font;
11013
11014 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
11015
11016 BLOCK_INPUT;
11017 /* First, try whatever font the caller has specified. */
11018 if (STRINGP (font))
11019 {
11020 tem = Fquery_fontset (font, Qnil);
11021 if (STRINGP (tem))
11022 font = x_new_fontset (f, XSTRING (tem)->data);
11023 else
11024 font = x_new_font (f, XSTRING (font)->data);
11025 }
11026
11027 /* Try out a font which we hope has bold and italic variations. */
11028 if (!STRINGP (font))
11029 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
11030 if (!STRINGP (font))
11031 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11032 if (! STRINGP (font))
11033 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11034 if (! STRINGP (font))
11035 /* This was formerly the first thing tried, but it finds too many fonts
11036 and takes too long. */
11037 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
11038 /* If those didn't work, look for something which will at least work. */
11039 if (! STRINGP (font))
11040 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
11041 UNBLOCK_INPUT;
11042 if (! STRINGP (font))
11043 font = build_string ("fixed");
11044
11045 x_default_parameter (f, parms, Qfont, font,
11046 "font", "Font", RES_TYPE_STRING);
11047 }
11048
11049 x_default_parameter (f, parms, Qborder_width, make_number (2),
11050 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
11051
11052 /* This defaults to 2 in order to match xterm. We recognize either
11053 internalBorderWidth or internalBorder (which is what xterm calls
11054 it). */
11055 if (NILP (Fassq (Qinternal_border_width, parms)))
11056 {
11057 Lisp_Object value;
11058
11059 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
11060 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
11061 if (! EQ (value, Qunbound))
11062 parms = Fcons (Fcons (Qinternal_border_width, value),
11063 parms);
11064 }
11065
11066 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
11067 "internalBorderWidth", "internalBorderWidth",
11068 RES_TYPE_NUMBER);
11069
11070 /* Also do the stuff which must be set before the window exists. */
11071 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
11072 "foreground", "Foreground", RES_TYPE_STRING);
11073 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
11074 "background", "Background", RES_TYPE_STRING);
11075 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
11076 "pointerColor", "Foreground", RES_TYPE_STRING);
11077 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
11078 "cursorColor", "Foreground", RES_TYPE_STRING);
11079 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
11080 "borderColor", "BorderColor", RES_TYPE_STRING);
11081
11082 /* Init faces before x_default_parameter is called for scroll-bar
11083 parameters because that function calls x_set_scroll_bar_width,
11084 which calls change_frame_size, which calls Fset_window_buffer,
11085 which runs hooks, which call Fvertical_motion. At the end, we
11086 end up in init_iterator with a null face cache, which should not
11087 happen. */
11088 init_frame_faces (f);
11089
11090 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
11091 window_prompting = x_figure_window_size (f, parms);
11092
11093 if (window_prompting & XNegative)
11094 {
11095 if (window_prompting & YNegative)
11096 f->output_data.x->win_gravity = SouthEastGravity;
11097 else
11098 f->output_data.x->win_gravity = NorthEastGravity;
11099 }
11100 else
11101 {
11102 if (window_prompting & YNegative)
11103 f->output_data.x->win_gravity = SouthWestGravity;
11104 else
11105 f->output_data.x->win_gravity = NorthWestGravity;
11106 }
11107
11108 f->output_data.x->size_hint_flags = window_prompting;
11109 {
11110 XSetWindowAttributes attrs;
11111 unsigned long mask;
11112
11113 BLOCK_INPUT;
c51d2b5e
GM
11114 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
11115 if (DoesSaveUnders (dpyinfo->screen))
11116 mask |= CWSaveUnder;
11117
9b2956e2
GM
11118 /* Window managers look at the override-redirect flag to determine
11119 whether or net to give windows a decoration (Xlib spec, chapter
333b20bb
GM
11120 3.2.8). */
11121 attrs.override_redirect = True;
11122 attrs.save_under = True;
11123 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
11124 /* Arrange for getting MapNotify and UnmapNotify events. */
11125 attrs.event_mask = StructureNotifyMask;
11126 tip_window
11127 = FRAME_X_WINDOW (f)
11128 = XCreateWindow (FRAME_X_DISPLAY (f),
11129 FRAME_X_DISPLAY_INFO (f)->root_window,
11130 /* x, y, width, height */
11131 0, 0, 1, 1,
11132 /* Border. */
11133 1,
11134 CopyFromParent, InputOutput, CopyFromParent,
11135 mask, &attrs);
11136 UNBLOCK_INPUT;
11137 }
11138
11139 x_make_gc (f);
11140
333b20bb
GM
11141 x_default_parameter (f, parms, Qauto_raise, Qnil,
11142 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11143 x_default_parameter (f, parms, Qauto_lower, Qnil,
11144 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11145 x_default_parameter (f, parms, Qcursor_type, Qbox,
11146 "cursorType", "CursorType", RES_TYPE_SYMBOL);
11147
11148 /* Dimensions, especially f->height, must be done via change_frame_size.
11149 Change will not be effected unless different from the current
11150 f->height. */
11151 width = f->width;
11152 height = f->height;
11153 f->height = 0;
11154 SET_FRAME_WIDTH (f, 0);
8938a4fb 11155 change_frame_size (f, height, width, 1, 0, 0);
333b20bb 11156
035d5114 11157 /* Set up faces after all frame parameters are known. This call
6801a572
GM
11158 also merges in face attributes specified for new frames.
11159
11160 Frame parameters may be changed if .Xdefaults contains
11161 specifications for the default font. For example, if there is an
11162 `Emacs.default.attributeBackground: pink', the `background-color'
11163 attribute of the frame get's set, which let's the internal border
11164 of the tooltip frame appear in pink. Prevent this. */
11165 {
11166 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
11167
11168 /* Set tip_frame here, so that */
11169 tip_frame = frame;
11170 call1 (Qface_set_after_frame_default, frame);
11171
11172 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
11173 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
11174 Qnil));
11175 }
035d5114 11176
333b20bb
GM
11177 f->no_split = 1;
11178
11179 UNGCPRO;
11180
11181 /* It is now ok to make the frame official even if we get an error
11182 below. And the frame needs to be on Vframe_list or making it
11183 visible won't work. */
11184 Vframe_list = Fcons (frame, Vframe_list);
11185
11186 /* Now that the frame is official, it counts as a reference to
11187 its display. */
11188 FRAME_X_DISPLAY_INFO (f)->reference_count++;
11189
06d62053
GM
11190 /* Setting attributes of faces of the tooltip frame from resources
11191 and similar will increment face_change_count, which leads to the
11192 clearing of all current matrices. Since this isn't necessary
11193 here, avoid it by resetting face_change_count to the value it
11194 had before we created the tip frame. */
11195 face_change_count = face_change_count_before;
11196
eaf1eea9 11197 /* Discard the unwind_protect. */
333b20bb
GM
11198 return unbind_to (count, frame);
11199}
11200
11201
06d62053
GM
11202/* Compute where to display tip frame F. PARMS is the list of frame
11203 parameters for F. DX and DY are specified offsets from the current
ab452f99
GM
11204 location of the mouse. WIDTH and HEIGHT are the width and height
11205 of the tooltip. Return coordinates relative to the root window of
11206 the display in *ROOT_X, and *ROOT_Y. */
06d62053
GM
11207
11208static void
ab452f99 11209compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
06d62053
GM
11210 struct frame *f;
11211 Lisp_Object parms, dx, dy;
ab452f99 11212 int width, height;
06d62053
GM
11213 int *root_x, *root_y;
11214{
11215 Lisp_Object left, top;
11216 int win_x, win_y;
11217 Window root, child;
11218 unsigned pmask;
11219
11220 /* User-specified position? */
11221 left = Fcdr (Fassq (Qleft, parms));
11222 top = Fcdr (Fassq (Qtop, parms));
11223
11224 /* Move the tooltip window where the mouse pointer is. Resize and
11225 show it. */
570d22b0 11226 if (!INTEGERP (left) || !INTEGERP (top))
ab452f99
GM
11227 {
11228 BLOCK_INPUT;
11229 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
11230 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
11231 UNBLOCK_INPUT;
11232 }
06d62053 11233
06d62053
GM
11234 if (INTEGERP (top))
11235 *root_y = XINT (top);
ab452f99
GM
11236 else if (*root_y + XINT (dy) - height < 0)
11237 *root_y -= XINT (dy);
11238 else
11239 {
11240 *root_y -= height;
11241 *root_y += XINT (dy);
11242 }
11243
11244 if (INTEGERP (left))
11245 *root_x = XINT (left);
d682d3df
RS
11246 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
11247 /* It fits to the right of the pointer. */
11248 *root_x += XINT (dx);
11249 else if (width + XINT (dx) <= *root_x)
11250 /* It fits to the left of the pointer. */
ab452f99
GM
11251 *root_x -= width + XINT (dx);
11252 else
d682d3df
RS
11253 /* Put it left-justified on the screen--it ought to fit that way. */
11254 *root_x = 0;
06d62053
GM
11255}
11256
11257
0634ce98 11258DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7ee72033 11259 doc: /* Show STRING in a "tooltip" window on frame FRAME.
c061c855
GM
11260A tooltip window is a small X window displaying a string.
11261
11262FRAME nil or omitted means use the selected frame.
11263
11264PARMS is an optional list of frame parameters which can be used to
11265change the tooltip's appearance.
11266
11267Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
11268means use the default timeout of 5 seconds.
11269
11270If the list of frame parameters PARAMS contains a `left' parameters,
11271the tooltip is displayed at that x-position. Otherwise it is
11272displayed at the mouse position, with offset DX added (default is 5 if
11273DX isn't specified). Likewise for the y-position; if a `top' frame
11274parameter is specified, it determines the y-position of the tooltip
11275window, otherwise it is displayed at the mouse position, with offset
11276DY added (default is -10).
11277
11278A tooltip's maximum size is specified by `x-max-tooltip-size'.
7ee72033
MB
11279Text larger than the specified size is clipped. */)
11280 (string, frame, parms, timeout, dx, dy)
0634ce98 11281 Lisp_Object string, frame, parms, timeout, dx, dy;
333b20bb
GM
11282{
11283 struct frame *f;
11284 struct window *w;
06d62053 11285 int root_x, root_y;
333b20bb
GM
11286 struct buffer *old_buffer;
11287 struct text_pos pos;
11288 int i, width, height;
393f2d14 11289 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
333b20bb 11290 int old_windows_or_buffers_changed = windows_or_buffers_changed;
06d62053 11291 int count = BINDING_STACK_SIZE ();
333b20bb
GM
11292
11293 specbind (Qinhibit_redisplay, Qt);
11294
393f2d14 11295 GCPRO4 (string, parms, frame, timeout);
333b20bb 11296
b7826503 11297 CHECK_STRING (string);
333b20bb
GM
11298 f = check_x_frame (frame);
11299 if (NILP (timeout))
11300 timeout = make_number (5);
11301 else
b7826503 11302 CHECK_NATNUM (timeout);
0634ce98
GM
11303
11304 if (NILP (dx))
11305 dx = make_number (5);
11306 else
b7826503 11307 CHECK_NUMBER (dx);
0634ce98
GM
11308
11309 if (NILP (dy))
12c67a7f 11310 dy = make_number (-10);
0634ce98 11311 else
b7826503 11312 CHECK_NUMBER (dy);
333b20bb 11313
06d62053
GM
11314 if (NILP (last_show_tip_args))
11315 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
11316
11317 if (!NILP (tip_frame))
11318 {
11319 Lisp_Object last_string = AREF (last_show_tip_args, 0);
11320 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
11321 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
11322
11323 if (EQ (frame, last_frame)
11324 && !NILP (Fequal (last_string, string))
11325 && !NILP (Fequal (last_parms, parms)))
11326 {
11327 struct frame *f = XFRAME (tip_frame);
11328
11329 /* Only DX and DY have changed. */
11330 if (!NILP (tip_timer))
ae782866
GM
11331 {
11332 Lisp_Object timer = tip_timer;
11333 tip_timer = Qnil;
11334 call1 (Qcancel_timer, timer);
11335 }
06d62053
GM
11336
11337 BLOCK_INPUT;
ab452f99
GM
11338 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
11339 PIXEL_HEIGHT (f), &root_x, &root_y);
06d62053 11340 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 11341 root_x, root_y);
06d62053
GM
11342 UNBLOCK_INPUT;
11343 goto start_timer;
11344 }
11345 }
11346
333b20bb
GM
11347 /* Hide a previous tip, if any. */
11348 Fx_hide_tip ();
11349
06d62053
GM
11350 ASET (last_show_tip_args, 0, string);
11351 ASET (last_show_tip_args, 1, frame);
11352 ASET (last_show_tip_args, 2, parms);
11353
333b20bb
GM
11354 /* Add default values to frame parameters. */
11355 if (NILP (Fassq (Qname, parms)))
11356 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
11357 if (NILP (Fassq (Qinternal_border_width, parms)))
11358 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
11359 if (NILP (Fassq (Qborder_width, parms)))
11360 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
11361 if (NILP (Fassq (Qborder_color, parms)))
11362 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
11363 if (NILP (Fassq (Qbackground_color, parms)))
11364 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
11365 parms);
11366
11367 /* Create a frame for the tooltip, and record it in the global
11368 variable tip_frame. */
275841bf 11369 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
44b5a125 11370 f = XFRAME (frame);
333b20bb 11371
d63931a2 11372 /* Set up the frame's root window. */
333b20bb
GM
11373 w = XWINDOW (FRAME_ROOT_WINDOW (f));
11374 w->left = w->top = make_number (0);
d63931a2
GM
11375
11376 if (CONSP (Vx_max_tooltip_size)
11377 && INTEGERP (XCAR (Vx_max_tooltip_size))
11378 && XINT (XCAR (Vx_max_tooltip_size)) > 0
11379 && INTEGERP (XCDR (Vx_max_tooltip_size))
11380 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
11381 {
11382 w->width = XCAR (Vx_max_tooltip_size);
11383 w->height = XCDR (Vx_max_tooltip_size);
11384 }
11385 else
11386 {
11387 w->width = make_number (80);
11388 w->height = make_number (40);
11389 }
11390
11391 f->window_width = XINT (w->width);
333b20bb
GM
11392 adjust_glyphs (f);
11393 w->pseudo_window_p = 1;
11394
11395 /* Display the tooltip text in a temporary buffer. */
333b20bb 11396 old_buffer = current_buffer;
275841bf 11397 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
d63931a2 11398 current_buffer->truncate_lines = Qnil;
333b20bb
GM
11399 clear_glyph_matrix (w->desired_matrix);
11400 clear_glyph_matrix (w->current_matrix);
11401 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
11402 try_window (FRAME_ROOT_WINDOW (f), pos);
11403
11404 /* Compute width and height of the tooltip. */
11405 width = height = 0;
11406 for (i = 0; i < w->desired_matrix->nrows; ++i)
11407 {
11408 struct glyph_row *row = &w->desired_matrix->rows[i];
11409 struct glyph *last;
11410 int row_width;
11411
11412 /* Stop at the first empty row at the end. */
11413 if (!row->enabled_p || !row->displays_text_p)
11414 break;
11415
d7bf0342
GM
11416 /* Let the row go over the full width of the frame. */
11417 row->full_width_p = 1;
333b20bb 11418
e3130015 11419 /* There's a glyph at the end of rows that is used to place
333b20bb
GM
11420 the cursor there. Don't include the width of this glyph. */
11421 if (row->used[TEXT_AREA])
11422 {
11423 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
11424 row_width = row->pixel_width - last->pixel_width;
11425 }
11426 else
11427 row_width = row->pixel_width;
11428
11429 height += row->height;
11430 width = max (width, row_width);
11431 }
11432
11433 /* Add the frame's internal border to the width and height the X
11434 window should have. */
11435 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11436 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11437
11438 /* Move the tooltip window where the mouse pointer is. Resize and
11439 show it. */
ab452f99 11440 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
0634ce98 11441
0634ce98 11442 BLOCK_INPUT;
333b20bb 11443 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 11444 root_x, root_y, width, height);
333b20bb
GM
11445 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11446 UNBLOCK_INPUT;
06d62053 11447
333b20bb
GM
11448 /* Draw into the window. */
11449 w->must_be_updated_p = 1;
11450 update_single_window (w, 1);
11451
11452 /* Restore original current buffer. */
11453 set_buffer_internal_1 (old_buffer);
11454 windows_or_buffers_changed = old_windows_or_buffers_changed;
11455
06d62053 11456 start_timer:
333b20bb
GM
11457 /* Let the tip disappear after timeout seconds. */
11458 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
11459 intern ("x-hide-tip"));
a744a2ec
DL
11460
11461 UNGCPRO;
333b20bb
GM
11462 return unbind_to (count, Qnil);
11463}
11464
11465
11466DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7ee72033
MB
11467 doc: /* Hide the current tooltip window, if there is any.
11468Value is t if tooltip was open, nil otherwise. */)
11469 ()
333b20bb 11470{
44b5a125 11471 int count;
c0006262
GM
11472 Lisp_Object deleted, frame, timer;
11473 struct gcpro gcpro1, gcpro2;
44b5a125
GM
11474
11475 /* Return quickly if nothing to do. */
c0006262 11476 if (NILP (tip_timer) && NILP (tip_frame))
44b5a125 11477 return Qnil;
333b20bb 11478
c0006262
GM
11479 frame = tip_frame;
11480 timer = tip_timer;
11481 GCPRO2 (frame, timer);
11482 tip_frame = tip_timer = deleted = Qnil;
11483
44b5a125 11484 count = BINDING_STACK_SIZE ();
333b20bb 11485 specbind (Qinhibit_redisplay, Qt);
44b5a125 11486 specbind (Qinhibit_quit, Qt);
333b20bb 11487
c0006262 11488 if (!NILP (timer))
ae782866 11489 call1 (Qcancel_timer, timer);
333b20bb 11490
c0006262 11491 if (FRAMEP (frame))
333b20bb 11492 {
44b5a125
GM
11493 Fdelete_frame (frame, Qnil);
11494 deleted = Qt;
f6c44811
GM
11495
11496#ifdef USE_LUCID
11497 /* Bloodcurdling hack alert: The Lucid menu bar widget's
11498 redisplay procedure is not called when a tip frame over menu
11499 items is unmapped. Redisplay the menu manually... */
11500 {
11501 struct frame *f = SELECTED_FRAME ();
11502 Widget w = f->output_data.x->menubar_widget;
11503 extern void xlwmenu_redisplay P_ ((Widget));
9180dc8c 11504
f6c44811 11505 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
dbc64aa7 11506 && w != NULL)
f6c44811
GM
11507 {
11508 BLOCK_INPUT;
11509 xlwmenu_redisplay (w);
11510 UNBLOCK_INPUT;
11511 }
11512 }
11513#endif /* USE_LUCID */
333b20bb
GM
11514 }
11515
c0006262 11516 UNGCPRO;
44b5a125 11517 return unbind_to (count, deleted);
333b20bb
GM
11518}
11519
11520
11521\f
11522/***********************************************************************
11523 File selection dialog
11524 ***********************************************************************/
11525
11526#ifdef USE_MOTIF
11527
11528/* Callback for "OK" and "Cancel" on file selection dialog. */
11529
11530static void
11531file_dialog_cb (widget, client_data, call_data)
11532 Widget widget;
11533 XtPointer call_data, client_data;
11534{
11535 int *result = (int *) client_data;
11536 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
11537 *result = cb->reason;
11538}
11539
11540
a779d213
GM
11541/* Callback for unmapping a file selection dialog. This is used to
11542 capture the case where a dialog is closed via a window manager's
11543 closer button, for example. Using a XmNdestroyCallback didn't work
11544 in this case. */
11545
11546static void
11547file_dialog_unmap_cb (widget, client_data, call_data)
11548 Widget widget;
11549 XtPointer call_data, client_data;
11550{
11551 int *result = (int *) client_data;
11552 *result = XmCR_CANCEL;
11553}
11554
11555
333b20bb 11556DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
7ee72033 11557 doc: /* Read file name, prompting with PROMPT in directory DIR.
c061c855
GM
11558Use a file selection dialog.
11559Select DEFAULT-FILENAME in the dialog's file selection box, if
11560specified. Don't let the user enter a file name in the file
7ee72033
MB
11561selection dialog's entry field, if MUSTMATCH is non-nil. */)
11562 (prompt, dir, default_filename, mustmatch)
333b20bb
GM
11563 Lisp_Object prompt, dir, default_filename, mustmatch;
11564{
11565 int result;
0fe92f72 11566 struct frame *f = SELECTED_FRAME ();
333b20bb
GM
11567 Lisp_Object file = Qnil;
11568 Widget dialog, text, list, help;
11569 Arg al[10];
11570 int ac = 0;
11571 extern XtAppContext Xt_app_con;
333b20bb 11572 XmString dir_xmstring, pattern_xmstring;
333b20bb
GM
11573 int count = specpdl_ptr - specpdl;
11574 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
11575
11576 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
11577 CHECK_STRING (prompt);
11578 CHECK_STRING (dir);
333b20bb
GM
11579
11580 /* Prevent redisplay. */
11581 specbind (Qinhibit_redisplay, Qt);
11582
11583 BLOCK_INPUT;
11584
11585 /* Create the dialog with PROMPT as title, using DIR as initial
11586 directory and using "*" as pattern. */
11587 dir = Fexpand_file_name (dir, Qnil);
11588 dir_xmstring = XmStringCreateLocalized (XSTRING (dir)->data);
11589 pattern_xmstring = XmStringCreateLocalized ("*");
11590
11591 XtSetArg (al[ac], XmNtitle, XSTRING (prompt)->data); ++ac;
11592 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
11593 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
11594 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
11595 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
11596 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
11597 "fsb", al, ac);
11598 XmStringFree (dir_xmstring);
11599 XmStringFree (pattern_xmstring);
11600
11601 /* Add callbacks for OK and Cancel. */
11602 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
11603 (XtPointer) &result);
11604 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
11605 (XtPointer) &result);
a779d213
GM
11606 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
11607 (XtPointer) &result);
333b20bb
GM
11608
11609 /* Disable the help button since we can't display help. */
11610 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
11611 XtSetSensitive (help, False);
11612
11613 /* Mark OK button as default. */
11614 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
11615 XmNshowAsDefault, True, NULL);
11616
11617 /* If MUSTMATCH is non-nil, disable the file entry field of the
11618 dialog, so that the user must select a file from the files list
11619 box. We can't remove it because we wouldn't have a way to get at
11620 the result file name, then. */
11621 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
11622 if (!NILP (mustmatch))
11623 {
11624 Widget label;
11625 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
11626 XtSetSensitive (text, False);
11627 XtSetSensitive (label, False);
11628 }
11629
11630 /* Manage the dialog, so that list boxes get filled. */
11631 XtManageChild (dialog);
11632
11633 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
11634 must include the path for this to work. */
11635 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
11636 if (STRINGP (default_filename))
11637 {
11638 XmString default_xmstring;
11639 int item_pos;
11640
11641 default_xmstring
11642 = XmStringCreateLocalized (XSTRING (default_filename)->data);
11643
11644 if (!XmListItemExists (list, default_xmstring))
11645 {
11646 /* Add a new item if DEFAULT_FILENAME is not in the list. */
11647 XmListAddItem (list, default_xmstring, 0);
11648 item_pos = 0;
11649 }
11650 else
11651 item_pos = XmListItemPos (list, default_xmstring);
11652 XmStringFree (default_xmstring);
11653
11654 /* Select the item and scroll it into view. */
11655 XmListSelectPos (list, item_pos, True);
11656 XmListSetPos (list, item_pos);
11657 }
11658
563b384d
GM
11659 /* Process events until the user presses Cancel or OK. Block
11660 and unblock input here so that we get a chance of processing
11661 expose events. */
11662 UNBLOCK_INPUT;
03100098 11663 result = 0;
a779d213 11664 while (result == 0)
563b384d
GM
11665 {
11666 BLOCK_INPUT;
11667 XtAppProcessEvent (Xt_app_con, XtIMAll);
11668 UNBLOCK_INPUT;
11669 }
11670 BLOCK_INPUT;
03100098 11671
333b20bb
GM
11672 /* Get the result. */
11673 if (result == XmCR_OK)
11674 {
11675 XmString text;
11676 String data;
11677
d1670063 11678 XtVaGetValues (dialog, XmNtextString, &text, NULL);
333b20bb
GM
11679 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
11680 XmStringFree (text);
11681 file = build_string (data);
11682 XtFree (data);
11683 }
11684 else
11685 file = Qnil;
11686
11687 /* Clean up. */
11688 XtUnmanageChild (dialog);
11689 XtDestroyWidget (dialog);
11690 UNBLOCK_INPUT;
11691 UNGCPRO;
11692
11693 /* Make "Cancel" equivalent to C-g. */
11694 if (NILP (file))
11695 Fsignal (Qquit, Qnil);
11696
11697 return unbind_to (count, file);
11698}
11699
11700#endif /* USE_MOTIF */
11701
333b20bb
GM
11702
11703\f
82bab41c
GM
11704/***********************************************************************
11705 Keyboard
11706 ***********************************************************************/
11707
11708#ifdef HAVE_XKBGETKEYBOARD
11709#include <X11/XKBlib.h>
11710#include <X11/keysym.h>
11711#endif
11712
11713DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
11714 Sx_backspace_delete_keys_p, 0, 1, 0,
7ee72033 11715 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
c061c855
GM
11716FRAME nil means use the selected frame.
11717Value is t if we know that both keys are present, and are mapped to the
7ee72033
MB
11718usual X keysyms. */)
11719 (frame)
82bab41c
GM
11720 Lisp_Object frame;
11721{
11722#ifdef HAVE_XKBGETKEYBOARD
11723 XkbDescPtr kb;
11724 struct frame *f = check_x_frame (frame);
11725 Display *dpy = FRAME_X_DISPLAY (f);
11726 Lisp_Object have_keys;
46f6a258 11727 int major, minor, op, event, error;
82bab41c
GM
11728
11729 BLOCK_INPUT;
46f6a258
GM
11730
11731 /* Check library version in case we're dynamically linked. */
11732 major = XkbMajorVersion;
11733 minor = XkbMinorVersion;
11734 if (!XkbLibraryVersion (&major, &minor))
c1efd260
GM
11735 {
11736 UNBLOCK_INPUT;
11737 return Qnil;
11738 }
46f6a258
GM
11739
11740 /* Check that the server supports XKB. */
11741 major = XkbMajorVersion;
11742 minor = XkbMinorVersion;
11743 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
c1efd260
GM
11744 {
11745 UNBLOCK_INPUT;
11746 return Qnil;
11747 }
46f6a258
GM
11748
11749 have_keys = Qnil;
c1efd260 11750 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
82bab41c
GM
11751 if (kb)
11752 {
11753 int delete_keycode = 0, backspace_keycode = 0, i;
c1efd260
GM
11754
11755 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
82bab41c 11756 {
c1efd260
GM
11757 for (i = kb->min_key_code;
11758 (i < kb->max_key_code
11759 && (delete_keycode == 0 || backspace_keycode == 0));
11760 ++i)
11761 {
d63931a2
GM
11762 /* The XKB symbolic key names can be seen most easily in
11763 the PS file generated by `xkbprint -label name
11764 $DISPLAY'. */
c1efd260
GM
11765 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
11766 delete_keycode = i;
11767 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
11768 backspace_keycode = i;
11769 }
11770
11771 XkbFreeNames (kb, 0, True);
82bab41c
GM
11772 }
11773
c1efd260 11774 XkbFreeClientMap (kb, 0, True);
82bab41c
GM
11775
11776 if (delete_keycode
11777 && backspace_keycode
11778 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
11779 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
11780 have_keys = Qt;
11781 }
11782 UNBLOCK_INPUT;
11783 return have_keys;
11784#else /* not HAVE_XKBGETKEYBOARD */
11785 return Qnil;
11786#endif /* not HAVE_XKBGETKEYBOARD */
11787}
11788
11789
11790\f
333b20bb
GM
11791/***********************************************************************
11792 Initialization
11793 ***********************************************************************/
11794
11795void
11796syms_of_xfns ()
11797{
11798 /* This is zero if not using X windows. */
11799 x_in_use = 0;
11800
11801 /* The section below is built by the lisp expression at the top of the file,
11802 just above where these variables are declared. */
11803 /*&&& init symbols here &&&*/
11804 Qauto_raise = intern ("auto-raise");
11805 staticpro (&Qauto_raise);
11806 Qauto_lower = intern ("auto-lower");
11807 staticpro (&Qauto_lower);
11808 Qbar = intern ("bar");
dbc4e1c1 11809 staticpro (&Qbar);
f9942c9e
JB
11810 Qborder_color = intern ("border-color");
11811 staticpro (&Qborder_color);
11812 Qborder_width = intern ("border-width");
11813 staticpro (&Qborder_width);
dbc4e1c1
JB
11814 Qbox = intern ("box");
11815 staticpro (&Qbox);
f9942c9e
JB
11816 Qcursor_color = intern ("cursor-color");
11817 staticpro (&Qcursor_color);
dbc4e1c1
JB
11818 Qcursor_type = intern ("cursor-type");
11819 staticpro (&Qcursor_type);
f9942c9e
JB
11820 Qgeometry = intern ("geometry");
11821 staticpro (&Qgeometry);
f9942c9e
JB
11822 Qicon_left = intern ("icon-left");
11823 staticpro (&Qicon_left);
11824 Qicon_top = intern ("icon-top");
11825 staticpro (&Qicon_top);
11826 Qicon_type = intern ("icon-type");
11827 staticpro (&Qicon_type);
80534dd6
KH
11828 Qicon_name = intern ("icon-name");
11829 staticpro (&Qicon_name);
f9942c9e
JB
11830 Qinternal_border_width = intern ("internal-border-width");
11831 staticpro (&Qinternal_border_width);
11832 Qleft = intern ("left");
11833 staticpro (&Qleft);
1ab3d87e
RS
11834 Qright = intern ("right");
11835 staticpro (&Qright);
f9942c9e
JB
11836 Qmouse_color = intern ("mouse-color");
11837 staticpro (&Qmouse_color);
baaed68e
JB
11838 Qnone = intern ("none");
11839 staticpro (&Qnone);
f9942c9e
JB
11840 Qparent_id = intern ("parent-id");
11841 staticpro (&Qparent_id);
4701395c
KH
11842 Qscroll_bar_width = intern ("scroll-bar-width");
11843 staticpro (&Qscroll_bar_width);
8af1d7ca
JB
11844 Qsuppress_icon = intern ("suppress-icon");
11845 staticpro (&Qsuppress_icon);
01f1ba30 11846 Qundefined_color = intern ("undefined-color");
f9942c9e 11847 staticpro (&Qundefined_color);
a3c87d4e
JB
11848 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
11849 staticpro (&Qvertical_scroll_bars);
49795535
JB
11850 Qvisibility = intern ("visibility");
11851 staticpro (&Qvisibility);
f9942c9e
JB
11852 Qwindow_id = intern ("window-id");
11853 staticpro (&Qwindow_id);
2cbebefb
RS
11854 Qouter_window_id = intern ("outer-window-id");
11855 staticpro (&Qouter_window_id);
f9942c9e
JB
11856 Qx_frame_parameter = intern ("x-frame-parameter");
11857 staticpro (&Qx_frame_parameter);
9ef48a9d
RS
11858 Qx_resource_name = intern ("x-resource-name");
11859 staticpro (&Qx_resource_name);
4fe1de12
RS
11860 Quser_position = intern ("user-position");
11861 staticpro (&Quser_position);
11862 Quser_size = intern ("user-size");
11863 staticpro (&Quser_size);
333b20bb
GM
11864 Qscroll_bar_foreground = intern ("scroll-bar-foreground");
11865 staticpro (&Qscroll_bar_foreground);
11866 Qscroll_bar_background = intern ("scroll-bar-background");
11867 staticpro (&Qscroll_bar_background);
d62c8769
GM
11868 Qscreen_gamma = intern ("screen-gamma");
11869 staticpro (&Qscreen_gamma);
563b67aa
GM
11870 Qline_spacing = intern ("line-spacing");
11871 staticpro (&Qline_spacing);
7c7ff7f5
GM
11872 Qcenter = intern ("center");
11873 staticpro (&Qcenter);
96db09e4
KH
11874 Qcompound_text = intern ("compound-text");
11875 staticpro (&Qcompound_text);
ae782866
GM
11876 Qcancel_timer = intern ("cancel-timer");
11877 staticpro (&Qcancel_timer);
ea0a1f53
GM
11878 Qwait_for_wm = intern ("wait-for-wm");
11879 staticpro (&Qwait_for_wm);
49d41073
EZ
11880 Qfullscreen = intern ("fullscreen");
11881 staticpro (&Qfullscreen);
11882 Qfullwidth = intern ("fullwidth");
11883 staticpro (&Qfullwidth);
11884 Qfullheight = intern ("fullheight");
11885 staticpro (&Qfullheight);
11886 Qfullboth = intern ("fullboth");
11887 staticpro (&Qfullboth);
f9942c9e
JB
11888 /* This is the end of symbol initialization. */
11889
58cad5ed
KH
11890 /* Text property `display' should be nonsticky by default. */
11891 Vtext_property_default_nonsticky
11892 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
11893
11894
333b20bb
GM
11895 Qlaplace = intern ("laplace");
11896 staticpro (&Qlaplace);
4a8e312c
GM
11897 Qemboss = intern ("emboss");
11898 staticpro (&Qemboss);
11899 Qedge_detection = intern ("edge-detection");
11900 staticpro (&Qedge_detection);
11901 Qheuristic = intern ("heuristic");
11902 staticpro (&Qheuristic);
11903 QCmatrix = intern (":matrix");
11904 staticpro (&QCmatrix);
11905 QCcolor_adjustment = intern (":color-adjustment");
11906 staticpro (&QCcolor_adjustment);
11907 QCmask = intern (":mask");
11908 staticpro (&QCmask);
11909
a367641f
RS
11910 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
11911 staticpro (&Qface_set_after_frame_default);
11912
01f1ba30
JB
11913 Fput (Qundefined_color, Qerror_conditions,
11914 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
11915 Fput (Qundefined_color, Qerror_message,
11916 build_string ("Undefined color"));
11917
f9942c9e
JB
11918 init_x_parm_symbols ();
11919
7ee72033
MB
11920 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
11921 doc: /* Non-nil means always draw a cross over disabled images.
c061c855
GM
11922Disabled images are those having an `:conversion disabled' property.
11923A cross is always drawn on black & white displays. */);
14819cb3
GM
11924 cross_disabled_images = 0;
11925
7ee72033
MB
11926 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
11927 doc: /* List of directories to search for bitmap files for X. */);
e241c09b 11928 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
f1c7b5a6 11929
7ee72033
MB
11930 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
11931 doc: /* The shape of the pointer when over text.
c061c855
GM
11932Changing the value does not affect existing frames
11933unless you set the mouse color. */);
01f1ba30
JB
11934 Vx_pointer_shape = Qnil;
11935
7ee72033
MB
11936 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
11937 doc: /* The name Emacs uses to look up X resources.
c061c855
GM
11938`x-get-resource' uses this as the first component of the instance name
11939when requesting resource values.
11940Emacs initially sets `x-resource-name' to the name under which Emacs
11941was invoked, or to the value specified with the `-name' or `-rn'
11942switches, if present.
11943
11944It may be useful to bind this variable locally around a call
11945to `x-get-resource'. See also the variable `x-resource-class'. */);
d387c960 11946 Vx_resource_name = Qnil;
ac63d3d6 11947
7ee72033
MB
11948 DEFVAR_LISP ("x-resource-class", &Vx_resource_class,
11949 doc: /* The class Emacs uses to look up X resources.
c061c855
GM
11950`x-get-resource' uses this as the first component of the instance class
11951when requesting resource values.
11952
11953Emacs initially sets `x-resource-class' to "Emacs".
11954
11955Setting this variable permanently is not a reasonable thing to do,
11956but binding this variable locally around a call to `x-get-resource'
11957is a reasonable practice. See also the variable `x-resource-name'. */);
498e9ac3
RS
11958 Vx_resource_class = build_string (EMACS_CLASS);
11959
ca0ecbf5 11960#if 0 /* This doesn't really do anything. */
7ee72033
MB
11961 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
11962 doc: /* The shape of the pointer when not over text.
c061c855
GM
11963This variable takes effect when you create a new frame
11964or when you set the mouse color. */);
af01ef26 11965#endif
01f1ba30
JB
11966 Vx_nontext_pointer_shape = Qnil;
11967
7ee72033
MB
11968 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
11969 doc: /* The shape of the pointer when Emacs is busy.
c061c855
GM
11970This variable takes effect when you create a new frame
11971or when you set the mouse color. */);
0af913d7 11972 Vx_hourglass_pointer_shape = Qnil;
333b20bb 11973
7ee72033
MB
11974 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
11975 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 11976 display_hourglass_p = 1;
333b20bb 11977
7ee72033
MB
11978 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
11979 doc: /* *Seconds to wait before displaying an hourglass pointer.
c061c855 11980Value must be an integer or float. */);
0af913d7 11981 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
4ae9a85e 11982
ca0ecbf5 11983#if 0 /* This doesn't really do anything. */
7ee72033
MB
11984 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
11985 doc: /* The shape of the pointer when over the mode line.
c061c855
GM
11986This variable takes effect when you create a new frame
11987or when you set the mouse color. */);
af01ef26 11988#endif
01f1ba30
JB
11989 Vx_mode_pointer_shape = Qnil;
11990
d3b06468 11991 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7ee72033
MB
11992 &Vx_sensitive_text_pointer_shape,
11993 doc: /* The shape of the pointer when over mouse-sensitive text.
c061c855
GM
11994This variable takes effect when you create a new frame
11995or when you set the mouse color. */);
ca0ecbf5 11996 Vx_sensitive_text_pointer_shape = Qnil;
95f80c78 11997
8fb4ec9c 11998 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7ee72033
MB
11999 &Vx_window_horizontal_drag_shape,
12000 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
c061c855
GM
12001This variable takes effect when you create a new frame
12002or when you set the mouse color. */);
8fb4ec9c
GM
12003 Vx_window_horizontal_drag_shape = Qnil;
12004
7ee72033
MB
12005 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
12006 doc: /* A string indicating the foreground color of the cursor box. */);
01f1ba30
JB
12007 Vx_cursor_fore_pixel = Qnil;
12008
7ee72033
MB
12009 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
12010 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
c061c855 12011Text larger than this is clipped. */);
d63931a2
GM
12012 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
12013
7ee72033
MB
12014 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
12015 doc: /* Non-nil if no X window manager is in use.
c061c855
GM
12016Emacs doesn't try to figure this out; this is always nil
12017unless you set it to something else. */);
2d38195d
RS
12018 /* We don't have any way to find this out, so set it to nil
12019 and maybe the user would like to set it to t. */
12020 Vx_no_window_manager = Qnil;
1d3dac41 12021
942ea06d 12022 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7ee72033
MB
12023 &Vx_pixel_size_width_font_regexp,
12024 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
c061c855
GM
12025
12026Since Emacs gets width of a font matching with this regexp from
12027PIXEL_SIZE field of the name, font finding mechanism gets faster for
12028such a font. This is especially effective for such large fonts as
12029Chinese, Japanese, and Korean. */);
942ea06d
KH
12030 Vx_pixel_size_width_font_regexp = Qnil;
12031
7ee72033
MB
12032 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
12033 doc: /* Time after which cached images are removed from the cache.
c061c855
GM
12034When an image has not been displayed this many seconds, remove it
12035from the image cache. Value must be an integer or nil with nil
12036meaning don't clear the cache. */);
fcf431dc 12037 Vimage_cache_eviction_delay = make_number (30 * 60);
333b20bb 12038
1d3dac41 12039#ifdef USE_X_TOOLKIT
6f3f6a8d 12040 Fprovide (intern ("x-toolkit"), Qnil);
5b827abb 12041#ifdef USE_MOTIF
6f3f6a8d 12042 Fprovide (intern ("motif"), Qnil);
fc2cdd9a 12043
7ee72033
MB
12044 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
12045 doc: /* Version info for LessTif/Motif. */);
fc2cdd9a
GM
12046 Vmotif_version_string = build_string (XmVERSION_STRING);
12047#endif /* USE_MOTIF */
12048#endif /* USE_X_TOOLKIT */
01f1ba30 12049
01f1ba30 12050 defsubr (&Sx_get_resource);
333b20bb
GM
12051
12052 /* X window properties. */
12053 defsubr (&Sx_change_window_property);
12054 defsubr (&Sx_delete_window_property);
12055 defsubr (&Sx_window_property);
12056
2d764c78 12057 defsubr (&Sxw_display_color_p);
d0c9d219 12058 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
12059 defsubr (&Sxw_color_defined_p);
12060 defsubr (&Sxw_color_values);
9d317b2c 12061 defsubr (&Sx_server_max_request_size);
41beb8fc
RS
12062 defsubr (&Sx_server_vendor);
12063 defsubr (&Sx_server_version);
12064 defsubr (&Sx_display_pixel_width);
12065 defsubr (&Sx_display_pixel_height);
12066 defsubr (&Sx_display_mm_width);
12067 defsubr (&Sx_display_mm_height);
12068 defsubr (&Sx_display_screens);
12069 defsubr (&Sx_display_planes);
12070 defsubr (&Sx_display_color_cells);
12071 defsubr (&Sx_display_visual_class);
12072 defsubr (&Sx_display_backing_store);
12073 defsubr (&Sx_display_save_under);
8af1d7ca 12074 defsubr (&Sx_parse_geometry);
f676886a 12075 defsubr (&Sx_create_frame);
01f1ba30 12076 defsubr (&Sx_open_connection);
08a90d6a
RS
12077 defsubr (&Sx_close_connection);
12078 defsubr (&Sx_display_list);
01f1ba30 12079 defsubr (&Sx_synchronize);
3decc1e7 12080 defsubr (&Sx_focus_frame);
82bab41c
GM
12081 defsubr (&Sx_backspace_delete_keys_p);
12082
942ea06d
KH
12083 /* Setting callback functions for fontset handler. */
12084 get_font_info_func = x_get_font_info;
333b20bb
GM
12085
12086#if 0 /* This function pointer doesn't seem to be used anywhere.
12087 And the pointer assigned has the wrong type, anyway. */
942ea06d 12088 list_fonts_func = x_list_fonts;
333b20bb
GM
12089#endif
12090
942ea06d 12091 load_font_func = x_load_font;
bc1958c4 12092 find_ccl_program_func = x_find_ccl_program;
942ea06d
KH
12093 query_font_func = x_query_font;
12094 set_frame_fontset_func = x_set_font;
5f652afc 12095 get_font_repertory_func = x_get_font_repertory;
942ea06d 12096 check_window_system_func = check_x;
333b20bb
GM
12097
12098 /* Images. */
12099 Qxbm = intern ("xbm");
12100 staticpro (&Qxbm);
12101 QCtype = intern (":type");
12102 staticpro (&QCtype);
d2dc8167
GM
12103 QCconversion = intern (":conversion");
12104 staticpro (&QCconversion);
333b20bb
GM
12105 QCheuristic_mask = intern (":heuristic-mask");
12106 staticpro (&QCheuristic_mask);
12107 QCcolor_symbols = intern (":color-symbols");
12108 staticpro (&QCcolor_symbols);
333b20bb
GM
12109 QCascent = intern (":ascent");
12110 staticpro (&QCascent);
12111 QCmargin = intern (":margin");
12112 staticpro (&QCmargin);
12113 QCrelief = intern (":relief");
12114 staticpro (&QCrelief);
fcf431dc
GM
12115 Qpostscript = intern ("postscript");
12116 staticpro (&Qpostscript);
333b20bb
GM
12117 QCloader = intern (":loader");
12118 staticpro (&QCloader);
12119 QCbounding_box = intern (":bounding-box");
12120 staticpro (&QCbounding_box);
12121 QCpt_width = intern (":pt-width");
12122 staticpro (&QCpt_width);
12123 QCpt_height = intern (":pt-height");
12124 staticpro (&QCpt_height);
3ccff1e3
GM
12125 QCindex = intern (":index");
12126 staticpro (&QCindex);
333b20bb
GM
12127 Qpbm = intern ("pbm");
12128 staticpro (&Qpbm);
12129
12130#if HAVE_XPM
12131 Qxpm = intern ("xpm");
12132 staticpro (&Qxpm);
12133#endif
12134
12135#if HAVE_JPEG
12136 Qjpeg = intern ("jpeg");
12137 staticpro (&Qjpeg);
12138#endif
12139
12140#if HAVE_TIFF
12141 Qtiff = intern ("tiff");
12142 staticpro (&Qtiff);
12143#endif
12144
12145#if HAVE_GIF
12146 Qgif = intern ("gif");
12147 staticpro (&Qgif);
12148#endif
12149
12150#if HAVE_PNG
12151 Qpng = intern ("png");
12152 staticpro (&Qpng);
12153#endif
12154
12155 defsubr (&Sclear_image_cache);
42677916 12156 defsubr (&Simage_size);
b243755a 12157 defsubr (&Simage_mask_p);
333b20bb 12158
0af913d7
GM
12159 hourglass_atimer = NULL;
12160 hourglass_shown_p = 0;
333b20bb
GM
12161
12162 defsubr (&Sx_show_tip);
12163 defsubr (&Sx_hide_tip);
333b20bb 12164 tip_timer = Qnil;
44b5a125
GM
12165 staticpro (&tip_timer);
12166 tip_frame = Qnil;
12167 staticpro (&tip_frame);
333b20bb 12168
06d62053
GM
12169 last_show_tip_args = Qnil;
12170 staticpro (&last_show_tip_args);
12171
333b20bb
GM
12172#ifdef USE_MOTIF
12173 defsubr (&Sx_file_dialog);
12174#endif
12175}
12176
12177
12178void
12179init_xfns ()
12180{
12181 image_types = NULL;
12182 Vimage_types = Qnil;
12183
12184 define_image_type (&xbm_type);
12185 define_image_type (&gs_type);
12186 define_image_type (&pbm_type);
12187
12188#if HAVE_XPM
12189 define_image_type (&xpm_type);
12190#endif
12191
12192#if HAVE_JPEG
12193 define_image_type (&jpeg_type);
12194#endif
12195
12196#if HAVE_TIFF
12197 define_image_type (&tiff_type);
12198#endif
12199
12200#if HAVE_GIF
12201 define_image_type (&gif_type);
12202#endif
12203
12204#if HAVE_PNG
12205 define_image_type (&png_type);
12206#endif
01f1ba30
JB
12207}
12208
12209#endif /* HAVE_X_WINDOWS */