xfns.c 190 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* Functions for the X window system.
Glenn Morris's avatar
Glenn Morris committed
2

3
Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Jim Blandy's avatar
Jim Blandy committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Jim Blandy's avatar
Jim Blandy committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19

Richard M. Stallman's avatar
Richard M. Stallman committed
20
#include <config.h>
21
#include <stdio.h>
22
#include <math.h>
23 24
#include <unistd.h>

Jim Blandy's avatar
Jim Blandy committed
25 26
#include "lisp.h"
#include "xterm.h"
Paul Eggert's avatar
Paul Eggert committed
27
#include "menu.h"
Jim Blandy's avatar
Jim Blandy committed
28
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
29
#include "window.h"
30
#include "character.h"
Jim Blandy's avatar
Jim Blandy committed
31
#include "buffer.h"
Kenichi Handa's avatar
Kenichi Handa committed
32
#include "intervals.h"
Jim Blandy's avatar
Jim Blandy committed
33
#include "dispextern.h"
Jim Blandy's avatar
Jim Blandy committed
34
#include "keyboard.h"
35
#include "blockinput.h"
36
#include <epaths.h>
Karl Heuer's avatar
Karl Heuer committed
37
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
38
#include "coding.h"
Karl Heuer's avatar
Karl Heuer committed
39
#include "fontset.h"
40 41
#include "systime.h"
#include "termhooks.h"
42
#include "atimer.h"
43
#include "termchar.h"
Kenichi Handa's avatar
Kenichi Handa committed
44 45
#include "font.h"

46 47
#include <sys/types.h>
#include <sys/stat.h>
Jim Blandy's avatar
Jim Blandy committed
48

49
#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work.  */
50 51
#include "bitmaps/gray.xbm"
#else
Jim Blandy's avatar
Jim Blandy committed
52
#include <X11/bitmaps/gray>
53
#endif
Jim Blandy's avatar
Jim Blandy committed
54

55 56
#include "xsettings.h"

57 58 59 60 61 62 63
#ifdef HAVE_XRANDR
#include <X11/extensions/Xrandr.h>
#endif
#ifdef HAVE_XINERAMA
#include <X11/extensions/Xinerama.h>
#endif

Jan Djärv's avatar
Jan Djärv committed
64 65 66 67
#ifdef USE_GTK
#include "gtkutil.h"
#endif

68 69 70
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>

71
#ifndef USE_MOTIF
Chong Yidong's avatar
Chong Yidong committed
72 73
#ifdef HAVE_XAW3D
#include <X11/Xaw3d/Paned.h>
Chong Yidong's avatar
Chong Yidong committed
74
#include <X11/Xaw3d/Label.h>
Chong Yidong's avatar
Chong Yidong committed
75
#else /* !HAVE_XAW3D */
76 77
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Label.h>
Chong Yidong's avatar
Chong Yidong committed
78
#endif /* HAVE_XAW3D */
79
#endif /* USE_MOTIF */
80 81 82 83 84

#ifdef USG
#undef USG	/* ####KLUDGE for Solaris 2.2 and up */
#include <X11/Xos.h>
#define USG
85 86
#ifdef USG /* Pacify gcc -Wunused-macros.  */
#endif
87 88 89 90 91 92 93 94
#else
#include <X11/Xos.h>
#endif

#include "widget.h"

#include "../lwlib/lwlib.h"

95 96 97 98
#ifdef USE_MOTIF
#include <Xm/Xm.h>
#include <Xm/DialogS.h>
#include <Xm/FileSB.h>
Jan D's avatar
Jan D committed
99 100
#include <Xm/List.h>
#include <Xm/TextF.h>
101 102
#endif

103 104 105 106
#ifdef USE_LUCID
#include "../lwlib/xlwmenu.h"
#endif

Juanma Barranquero's avatar
Juanma Barranquero committed
107
#if !defined (NO_EDITRES)
108
#define HACK_EDITRES
109
extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
Dan Nicolaescu's avatar
Dan Nicolaescu committed
110
#endif /* not defined NO_EDITRES */
111

112 113
/* Unique id counter for widgets created by the Lucid Widget Library.  */

114 115
extern LWLIB_ID widget_id_tick;

116 117 118 119
#ifdef USE_MOTIF

#endif /* USE_MOTIF */

120 121
#endif /* USE_X_TOOLKIT */

Kenichi Handa's avatar
Kenichi Handa committed
122 123 124 125
#ifdef USE_GTK

#endif /* USE_GTK */

Richard M. Stallman's avatar
Richard M. Stallman committed
126 127
#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))

128 129 130
static Lisp_Object Qsuppress_icon;
static Lisp_Object Qundefined_color;
static Lisp_Object Qcompound_text, Qcancel_timer;
131
Lisp_Object Qfont_param;
Jim Blandy's avatar
Jim Blandy committed
132

133
#ifdef GLYPH_DEBUG
134 135
static ptrdiff_t image_cache_refcount;
static int dpyinfo_refcount;
136 137
#endif

138 139
static struct x_display_info *x_display_info_for_name (Lisp_Object);

140
/* Let the user specify an X display with a Lisp object.
141
   OBJECT may be nil, a frame or a terminal object.
142 143 144
   nil stands for the selected frame--or, if that is not an X frame,
   the first X display on the list.  */

145
struct x_display_info *
146
check_x_display_info (Lisp_Object object)
147
{
148
  struct x_display_info *dpyinfo = NULL;
149

150
  if (NILP (object))
151
    {
Gerd Moellmann's avatar
Gerd Moellmann committed
152
      struct frame *sf = XFRAME (selected_frame);
153

Gerd Moellmann's avatar
Gerd Moellmann committed
154
      if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
155
	dpyinfo = FRAME_DISPLAY_INFO (sf);
156
      else if (x_display_list != 0)
157
	dpyinfo = x_display_list;
158 159 160
      else
	error ("X windows are not in use or not initialized");
    }
161
  else if (TERMINALP (object))
162
    {
163
      struct terminal *t = get_terminal (object, 1);
164

165
      if (t->type != output_x_window)
166
        error ("Terminal %d is not an X display", t->id);
167

168
      dpyinfo = t->display_info.x;
169
    }
170 171
  else if (STRINGP (object))
    dpyinfo = x_display_info_for_name (object);
172 173
  else
    {
Dmitry Antipov's avatar
Dmitry Antipov committed
174
      struct frame *f = decode_window_system_frame (object);
175
      dpyinfo = FRAME_DISPLAY_INFO (f);
176
    }
177 178

  return dpyinfo;
179
}
180

181
/* Store the screen positions of frame F into XPTR and YPTR.
182 183 184 185
   These are the positions of the containing window manager window,
   not Emacs's own window.  */

void
Dmitry Antipov's avatar
Dmitry Antipov committed
186
x_real_positions (struct frame *f, int *xptr, int *yptr)
187
{
188
  int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
189 190 191
  int real_x = 0, real_y = 0;
  int had_errors = 0;
  Window win = f->output_data.x->parent_desc;
192 193
  Atom actual_type;
  unsigned long actual_size, bytes_remaining;
194
  int rc, actual_format;
195
  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
196 197 198 199
  long max_len = 400;
  Display *dpy = FRAME_X_DISPLAY (f);
  unsigned char *tmp_data = NULL;
  Atom target_type = XA_CARDINAL;
200

201
  block_input ();
202

203
  x_catch_errors (dpy);
204

205
  if (win == dpyinfo->root_window)
206 207 208 209 210 211 212
    win = FRAME_OUTER_WINDOW (f);

  /* This loop traverses up the containment tree until we hit the root
     window.  Window managers may intersect many windows between our window
     and the root window.  The window we find just before the root window
     should be the outer WM window. */
  for (;;)
213
    {
214 215 216
      Window wm_window, rootw;
      Window *tmp_children;
      unsigned int tmp_nchildren;
217
      int success;
218

219 220
      success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
			    &wm_window, &tmp_children, &tmp_nchildren);
221

222
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
223

224 225 226 227
      /* Don't free tmp_children if XQueryTree failed.  */
      if (! success)
	break;

228
      XFree (tmp_children);
229

230 231
      if (wm_window == rootw || had_errors)
        break;
232

233 234
      win = wm_window;
    }
235

236 237
  if (! had_errors)
    {
238
      unsigned int ign;
239
      Window child, rootw;
240

241 242 243 244 245 246 247 248 249 250 251 252 253 254
      /* Get the real coordinates for the WM window upper left corner */
      XGetGeometry (FRAME_X_DISPLAY (f), win,
                    &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);

      /* Translate real coordinates to coordinates relative to our
         window.  For our window, the upper left corner is 0, 0.
         Since the upper left corner of the WM window is outside
         our window, win_x and win_y will be negative:

         ------------------          ---> x
         |      title                |
         | -----------------         v y
         | |  our window
      */
255
      XTranslateCoordinates (FRAME_X_DISPLAY (f),
256

257
			     /* From-window, to-window.  */
258
			     FRAME_DISPLAY_INFO (f)->root_window,
259
                             FRAME_X_WINDOW (f),
260

261
			     /* From-position, to-position.  */
262
                             real_x, real_y, &win_x, &win_y,
263

264 265
			     /* Child of win.  */
			     &child);
266

267
      if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
268
	{
269 270
          outer_x = win_x;
          outer_y = win_y;
271
	}
272 273 274
      else
        {
          XTranslateCoordinates (FRAME_X_DISPLAY (f),
275

276
                                 /* From-window, to-window.  */
277
                                 FRAME_DISPLAY_INFO (f)->root_window,
278
                                 FRAME_OUTER_WINDOW (f),
279

280 281
                                 /* From-position, to-position.  */
                                 real_x, real_y, &outer_x, &outer_y,
282

283 284
                                 /* Child of win.  */
                                 &child);
285
	}
286

287 288
      had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
    }
289

290 291 292 293 294 295 296 297 298 299 300 301

  if (dpyinfo->root_window == f->output_data.x->parent_desc)
    {
      /* Try _NET_FRAME_EXTENTS if our parent is the root window.  */
      rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
                               0, max_len, False, target_type,
                               &actual_type, &actual_format, &actual_size,
                               &bytes_remaining, &tmp_data);

      if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
          && actual_size == 4 && actual_format == 32)
        {
302
          unsigned int ign;
303
          Window rootw;
304
          long *fe = (long *)tmp_data;
305 306 307 308 309 310 311 312 313 314 315 316

          XGetGeometry (FRAME_X_DISPLAY (f), win,
                        &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
          outer_x = -fe[0];
          outer_y = -fe[2];
          real_x -= fe[0];
          real_y -= fe[2];
        }
    }

  if (tmp_data) XFree (tmp_data);

317
  x_uncatch_errors ();
318

319
  unblock_input ();
320 321

  if (had_errors) return;
322

323 324 325 326 327
  f->x_pixels_diff = -win_x;
  f->y_pixels_diff = -win_y;

  FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
  FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
328 329 330

  *xptr = real_x;
  *yptr = real_y;
331 332
}

Jim Blandy's avatar
Jim Blandy committed
333

334

335 336 337 338

/* Gamma-correct COLOR on frame F.  */

void
339
gamma_correct (struct frame *f, XColor *color)
340 341 342 343 344 345 346 347 348 349
{
  if (f->gamma)
    {
      color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
      color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
      color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
    }
}


350
/* Decide if color named COLOR_NAME is valid for use on frame F.  If
351 352
   so, return the RGB values in COLOR.  If ALLOC_P,
   allocate the color.  Value is false if COLOR_NAME is invalid, or
353
   no color could be allocated.  */
354

355
bool
356
x_defined_color (struct frame *f, const char *color_name,
357
		 XColor *color, bool alloc_p)
Jim Blandy's avatar
Jim Blandy committed
358
{
359
  bool success_p = 0;
360 361
  Display *dpy = FRAME_X_DISPLAY (f);
  Colormap cmap = FRAME_X_COLORMAP (f);
Jim Blandy's avatar
Jim Blandy committed
362

363
  block_input ();
364 365 366 367
#ifdef USE_GTK
  success_p = xg_check_special_colors (f, color_name, color);
#endif
  if (!success_p)
368
    success_p = XParseColor (dpy, cmap, color_name, color) != 0;
369 370
  if (success_p && alloc_p)
    success_p = x_alloc_nearest_color (f, cmap, color);
371
  unblock_input ();
Jim Blandy's avatar
Jim Blandy committed
372

373
  return success_p;
Jim Blandy's avatar
Jim Blandy committed
374 375
}

376 377 378 379

/* Return the pixel color value for color COLOR_NAME on frame F.  If F
   is a monochrome frame, return MONO_COLOR regardless of what ARG says.
   Signal an error if color can't be allocated.  */
Jim Blandy's avatar
Jim Blandy committed
380

381
static int
Dmitry Antipov's avatar
Dmitry Antipov committed
382
x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
Jim Blandy's avatar
Jim Blandy committed
383
{
384
  XColor cdef;
Jim Blandy's avatar
Jim Blandy committed
385

386
  CHECK_STRING (color_name);
Jim Blandy's avatar
Jim Blandy committed
387

388 389 390
#if 0 /* Don't do this.  It's wrong when we're not using the default
	 colormap, it makes freeing difficult, and it's probably not
	 an important optimization.  */
391
  if (strcmp (SDATA (color_name), "black") == 0)
392
    return BLACK_PIX_DEFAULT (f);
393
  else if (strcmp (SDATA (color_name), "white") == 0)
394
    return WHITE_PIX_DEFAULT (f);
395
#endif
Jim Blandy's avatar
Jim Blandy committed
396

397
  /* Return MONO_COLOR for monochrome frames.  */
398
  if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
399
    return mono_color;
Jim Blandy's avatar
Jim Blandy committed
400

401
  /* x_defined_color is responsible for coping with failures
402
     by looking for a near-miss.  */
403
  if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
404 405
    return cdef.pixel;

406
  signal_error ("Undefined color", color_name);
Jim Blandy's avatar
Jim Blandy committed
407
}
408 409


Jim Blandy's avatar
Jim Blandy committed
410

Gerd Moellmann's avatar
Gerd Moellmann committed
411 412 413 414 415
/* Change the `wait-for-wm' frame parameter of frame F.  OLD_VALUE is
   the previous value of that parameter, NEW_VALUE is the new value.
   See also the comment of wait_for_wm in struct x_output.  */

static void
416
x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
Gerd Moellmann's avatar
Gerd Moellmann committed
417 418 419 420
{
  f->output_data.x->wait_for_wm = !NILP (new_value);
}

421 422 423 424 425 426 427 428 429 430 431
static void
x_set_tool_bar_position (struct frame *f,
                         Lisp_Object new_value,
                         Lisp_Object old_value)
{
  if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
      && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
    return;
  if (EQ (new_value, old_value)) return;

#ifdef USE_GTK
432 433
  xg_change_toolbar_position (f, new_value);
  fset_tool_bar_position (f, new_value);
434 435 436
#endif
}

437 438
#ifdef USE_GTK

439 440
/* Set icon from FILE for frame F.  By using GTK functions the icon
   may be any format that GdkPixbuf knows about, i.e. not just bitmaps.  */
441 442

int
Dmitry Antipov's avatar
Dmitry Antipov committed
443
xg_set_icon (struct frame *f, Lisp_Object file)
444
{
445 446
  int result = 0;
  Lisp_Object found;
447

448 449 450 451 452 453
  found = x_find_image_file (file);

  if (! NILP (found))
    {
      GdkPixbuf *pixbuf;
      GError *err = NULL;
454
      char *filename = SSDATA (found);
455
      block_input ();
456 457 458 459 460 461 462 463 464 465 466 467 468 469

      pixbuf = gdk_pixbuf_new_from_file (filename, &err);

      if (pixbuf)
	{
	  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
			       pixbuf);
	  g_object_unref (pixbuf);

	  result = 1;
	}
      else
	g_error_free (err);

470
      unblock_input ();
471 472 473
    }

  return result;
474
}
475 476

int
Dmitry Antipov's avatar
Dmitry Antipov committed
477
xg_set_icon_from_xpm_data (struct frame *f, const char **data)
478
{
479
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
480 481

  if (!pixbuf)
482
    return 0;
483

484
  gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
485 486 487
  g_object_unref (pixbuf);
  return 1;
}
488 489
#endif /* USE_GTK */

Gerd Moellmann's avatar
Gerd Moellmann committed
490

Jim Blandy's avatar
Jim Blandy committed
491
/* Functions called only from `x_set_frame_param'
Jim Blandy's avatar
Jim Blandy committed
492 493
   to set individual parameters.

494
   If FRAME_X_WINDOW (f) is 0,
Jim Blandy's avatar
Jim Blandy committed
495
   the frame is being created and its X-window does not exist yet.
Jim Blandy's avatar
Jim Blandy committed
496 497 498
   In that case, just record the parameter's new value
   in the standard place; do not attempt to change the window.  */

499
static void
500
x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
501
{
502 503
  struct x_output *x = f->output_data.x;
  unsigned long fg, old_fg;
504

505
  fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
506 507
  old_fg = FRAME_FOREGROUND_PIXEL (f);
  FRAME_FOREGROUND_PIXEL (f) = fg;
508

509
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
510
    {
511
      Display *dpy = FRAME_X_DISPLAY (f);
512

513
      block_input ();
514 515
      XSetForeground (dpy, x->normal_gc, fg);
      XSetBackground (dpy, x->reverse_gc, fg);
516

517 518 519 520 521 522
      if (x->cursor_pixel == old_fg)
	{
	  unload_color (f, x->cursor_pixel);
	  x->cursor_pixel = x_copy_color (f, fg);
	  XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
	}
523

524
      unblock_input ();
525

526
      update_face_from_frame_parameter (f, Qforeground_color, arg);
527

528
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
529
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
530
    }
531

532
  unload_color (f, old_fg);
Jim Blandy's avatar
Jim Blandy committed
533 534
}

535
static void
536
x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
537
{
538 539
  struct x_output *x = f->output_data.x;
  unsigned long bg;
Jim Blandy's avatar
Jim Blandy committed
540

541
  bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
542 543
  unload_color (f, FRAME_BACKGROUND_PIXEL (f));
  FRAME_BACKGROUND_PIXEL (f) = bg;
544

545
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
546
    {
547
      Display *dpy = FRAME_X_DISPLAY (f);
548

549
      block_input ();
550 551 552 553 554
      XSetBackground (dpy, x->normal_gc, bg);
      XSetForeground (dpy, x->reverse_gc, bg);
      XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
      XSetForeground (dpy, x->cursor_gc, bg);

Jan Djärv's avatar
Jan Djärv committed
555 556 557 558
#ifdef USE_GTK
      xg_set_background_color (f, bg);
#endif

559 560 561 562 563 564 565 566
#ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
				   toolkit scroll bars.  */
      {
	Lisp_Object bar;
	for (bar = FRAME_SCROLL_BARS (f);
	     !NILP (bar);
	     bar = XSCROLL_BAR (bar)->next)
	  {
567
	    Window window = XSCROLL_BAR (bar)->x_window;
568 569 570 571
	    XSetWindowBackground (dpy, window, bg);
	  }
      }
#endif /* USE_TOOLKIT_SCROLL_BARS */
Jim Blandy's avatar
Jim Blandy committed
572

573
      unblock_input ();
574
      update_face_from_frame_parameter (f, Qbackground_color, arg);
575

576
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
577
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
578 579 580
    }
}

581
static void
582
x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
583
{
584 585
  struct x_output *x = f->output_data.x;
  Display *dpy = FRAME_X_DISPLAY (f);
586
  Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
587
  Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
588
  unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
Karoly Lorentey's avatar
Karoly Lorentey committed
589
  unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
590

591
  /* Don't let pointers be invisible.  */
592
  if (mask_color == pixel)
593 594
    {
      x_free_colors (f, &pixel, 1);
Karoly Lorentey's avatar
Karoly Lorentey committed
595
      pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
596
    }
597

598 599
  unload_color (f, x->mouse_pixel);
  x->mouse_pixel = pixel;
Jim Blandy's avatar
Jim Blandy committed
600

601
  block_input ();
602

603
  /* It's not okay to crash if the user selects a screwy cursor.  */
604
  x_catch_errors (dpy);
605

606
  if (!NILP (Vx_pointer_shape))
Jim Blandy's avatar
Jim Blandy committed
607
    {
608
      CHECK_NUMBER (Vx_pointer_shape);
609
      cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
Jim Blandy's avatar
Jim Blandy committed
610 611
    }
  else
612 613
    cursor = XCreateFontCursor (dpy, XC_xterm);
  x_check_errors (dpy, "bad text pointer cursor: %s");
Jim Blandy's avatar
Jim Blandy committed
614

615
  if (!NILP (Vx_nontext_pointer_shape))
Jim Blandy's avatar
Jim Blandy committed
616
    {
617
      CHECK_NUMBER (Vx_nontext_pointer_shape);
618 619
      nontext_cursor
	= XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
Jim Blandy's avatar
Jim Blandy committed
620 621
    }
  else
622 623
    nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
  x_check_errors (dpy, "bad nontext pointer cursor: %s");
Jim Blandy's avatar
Jim Blandy committed
624

625
  if (!NILP (Vx_hourglass_pointer_shape))
626
    {
627
      CHECK_NUMBER (Vx_hourglass_pointer_shape);
628 629
      hourglass_cursor
	= XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
630 631
    }
  else
632 633
    hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
  x_check_errors (dpy, "bad hourglass pointer cursor: %s");
634

635
  if (!NILP (Vx_mode_pointer_shape))
Jim Blandy's avatar
Jim Blandy committed
636
    {
637
      CHECK_NUMBER (Vx_mode_pointer_shape);
638
      mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
Jim Blandy's avatar
Jim Blandy committed
639 640
    }
  else
641 642
    mode_cursor = XCreateFontCursor (dpy, XC_xterm);
  x_check_errors (dpy, "bad modeline pointer cursor: %s");
643

644
  if (!NILP (Vx_sensitive_text_pointer_shape))
645
    {
646
      CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
647
      hand_cursor
648
	= XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
649 650
    }
  else
651
    hand_cursor = XCreateFontCursor (dpy, XC_hand2);
Jim Blandy's avatar
Jim Blandy committed
652

653 654
  if (!NILP (Vx_window_horizontal_drag_shape))
    {
Paul Eggert's avatar
Paul Eggert committed
655
      CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
656
      horizontal_drag_cursor
657
	= XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
658 659 660
    }
  else
    horizontal_drag_cursor
661
      = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
662

663 664 665 666 667 668 669 670 671 672
  if (!NILP (Vx_window_vertical_drag_shape))
    {
      CHECK_NUMBER (Vx_window_vertical_drag_shape);
      vertical_drag_cursor
	= XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
    }
  else
    vertical_drag_cursor
      = XCreateFontCursor (dpy, XC_sb_v_double_arrow);

673
  /* Check and report errors with the above calls.  */
674
  x_check_errors (dpy, "can't set cursor shape: %s");
675
  x_uncatch_errors ();
676

Jim Blandy's avatar
Jim Blandy committed
677 678 679
  {
    XColor fore_color, back_color;

680
    fore_color.pixel = x->mouse_pixel;
681
    x_query_color (f, &fore_color);
Jim Blandy's avatar
Jim Blandy committed
682
    back_color.pixel = mask_color;
683
    x_query_color (f, &back_color);
684

685 686 687
    XRecolorCursor (dpy, cursor, &fore_color, &back_color);
    XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
    XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
688
    XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
689 690
    XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
    XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
691
    XRecolorCursor (dpy, vertical_drag_cursor, &fore_color, &back_color);
Jim Blandy's avatar
Jim Blandy committed
692 693
  }

694
  if (FRAME_X_WINDOW (f) != 0)
695 696
    XDefineCursor (dpy, FRAME_X_WINDOW (f),
                   f->output_data.x->current_cursor = cursor);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716

  if (cursor != x->text_cursor
      && x->text_cursor != 0)
    XFreeCursor (dpy, x->text_cursor);
  x->text_cursor = cursor;

  if (nontext_cursor != x->nontext_cursor
      && x->nontext_cursor != 0)
    XFreeCursor (dpy, x->nontext_cursor);
  x->nontext_cursor = nontext_cursor;

  if (hourglass_cursor != x->hourglass_cursor
      && x->hourglass_cursor != 0)
    XFreeCursor (dpy, x->hourglass_cursor);
  x->hourglass_cursor = hourglass_cursor;

  if (mode_cursor != x->modeline_cursor
      && x->modeline_cursor != 0)
    XFreeCursor (dpy, f->output_data.x->modeline_cursor);
  x->modeline_cursor = mode_cursor;
717

718 719 720 721
  if (hand_cursor != x->hand_cursor
      && x->hand_cursor != 0)
    XFreeCursor (dpy, x->hand_cursor);
  x->hand_cursor = hand_cursor;
Jim Blandy's avatar
Jim Blandy committed
722

723 724 725 726
  if (horizontal_drag_cursor != x->horizontal_drag_cursor
      && x->horizontal_drag_cursor != 0)
    XFreeCursor (dpy, x->horizontal_drag_cursor);
  x->horizontal_drag_cursor = horizontal_drag_cursor;
727

728 729 730 731 732
  if (vertical_drag_cursor != x->vertical_drag_cursor
      && x->vertical_drag_cursor != 0)
    XFreeCursor (dpy, x->vertical_drag_cursor);
  x->vertical_drag_cursor = vertical_drag_cursor;

733
  XFlush (dpy);
734
  unblock_input ();
735 736

  update_face_from_frame_parameter (f, Qmouse_color, arg);
Jim Blandy's avatar
Jim Blandy committed
737 738
}

739
static void
740
x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
741
{
742
  unsigned long fore_pixel, pixel;
743
  bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
744
  struct x_output *x = f->output_data.x;
Jim Blandy's avatar
Jim Blandy committed
745

746 747 748 749 750 751
  if (!NILP (Vx_cursor_fore_pixel))
    {
      fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
				   WHITE_PIX_DEFAULT (f));
      fore_pixel_allocated_p = 1;
    }
Jim Blandy's avatar
Jim Blandy committed
752
  else
Karoly Lorentey's avatar
Karoly Lorentey committed
753
    fore_pixel = FRAME_BACKGROUND_PIXEL (f);
754

755
  pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
756
  pixel_allocated_p = 1;
757

758
  /* Make sure that the cursor color differs from the background color.  */
Karoly Lorentey's avatar
Karoly Lorentey committed
759
  if (pixel == FRAME_BACKGROUND_PIXEL (f))
Jim Blandy's avatar
Jim Blandy committed
760
    {
761 762 763 764 765
      if (pixel_allocated_p)
	{
	  x_free_colors (f, &pixel, 1);
	  pixel_allocated_p = 0;
	}
766

767
      pixel = x->mouse_pixel;
768
      if (pixel == fore_pixel)
769 770 771 772 773 774
	{
	  if (fore_pixel_allocated_p)
	    {
	      x_free_colors (f, &fore_pixel, 1);
	      fore_pixel_allocated_p = 0;
	    }
Karoly Lorentey's avatar
Karoly Lorentey committed
775
	  fore_pixel = FRAME_BACKGROUND_PIXEL (f);
776
	}
Jim Blandy's avatar
Jim Blandy committed
777
    }
778

779
  unload_color (f, x->cursor_foreground_pixel);
780 781
  if (!fore_pixel_allocated_p)
    fore_pixel = x_copy_color (f, fore_pixel);
782
  x->cursor_foreground_pixel = fore_pixel;
Jim Blandy's avatar
Jim Blandy committed
783

784
  unload_color (f, x->cursor_pixel);
785 786
  if (!pixel_allocated_p)
    pixel = x_copy_color (f, pixel);
787
  x->cursor_pixel = pixel;
788

789
  if (FRAME_X_WINDOW (f) != 0)
Jim Blandy's avatar
Jim Blandy committed
790
    {
791
      block_input ();
792 793
      XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
      XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
794
      unblock_input ();
Jim Blandy's avatar
Jim Blandy committed
795

796
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
797
	{
798 799
	  x_update_cursor (f, 0);
	  x_update_cursor (f, 1);
Jim Blandy's avatar
Jim Blandy committed
800 801
	}
    }
802 803

  update_face_from_frame_parameter (f, Qcursor_color, arg);
Jim Blandy's avatar
Jim Blandy committed
804
}
805

Jim Blandy's avatar
Jim Blandy committed
806
/* Set the border-color of frame F to pixel value PIX.
Jim Blandy's avatar
Jim Blandy committed
807
   Note that this does not fully take effect if done before
Jim Blandy's avatar
Jim Blandy committed
808
   F has an x-window.  */
Jim Blandy's avatar
Jim Blandy committed
809

810
static void
811
x_set_border_pixel (struct frame *f, int pix)
Jim Blandy's avatar
Jim Blandy committed
812
{
813
  unload_color (f, f->output_data.x->border_pixel);
814
  f->output_data.x->border_pixel = pix;
Jim Blandy's avatar
Jim Blandy committed
815

816
  if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
Jim Blandy's avatar
Jim Blandy committed
817
    {
818
      block_input ();
Paul Eggert's avatar
Paul Eggert committed
819
      XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
820
      unblock_input ();
Jim Blandy's avatar
Jim Blandy committed
821

822
      if (FRAME_VISIBLE_P (f))
Jim Blandy's avatar
Jim Blandy committed
823
        redraw_frame (f);
Jim Blandy's avatar
Jim Blandy committed
824 825 826
    }
}

827 828 829 830 831 832 833 834 835
/* Set the border-color of frame F to value described by ARG.
   ARG can be a string naming a color.
   The border-color is used for the border that is drawn by the X server.
   Note that this does not fully take effect if done before
   F has an x-window; it must be redone when the window is created.

   Note: this is done in two routines because of the way X10 works.

   Note: under X11, this is normally the province of the window manager,
836
   and so emacs's border colors may be overridden.  */
837

838
static void
839
x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
840 841 842 843 844 845 846 847
{
  int pix;

  CHECK_STRING (arg);
  pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
  x_set_border_pixel (f, pix);
  update_face_from_frame_parameter (f, Qborder_color, arg);
}
848 849


850
static void
Dmitry Antipov's avatar
Dmitry Antipov committed
851
x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
852
{
Kim F. Storm's avatar
Kim F. Storm committed
853
  set_frame_cursor_types (f, arg);
Jim Blandy's avatar
Jim Blandy committed
854
}
855

856
static void
857
x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
Jim Blandy's avatar
Jim Blandy committed
858 859 860
{
  int result;

861 862 863 864 865 866
  if (STRINGP (arg))
    {
      if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
	return;
    }
  else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
Jim Blandy's avatar
Jim Blandy committed
867 868