xterm.c 395 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
/* X Communication module for terminals which understand the X protocol.
Glenn Morris's avatar
Glenn Morris committed
2

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 1989, 1993-2019 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 <https://www.gnu.org/licenses/>.  */
Jim Blandy's avatar
Jim Blandy committed
19

20
/* New display code by Gerd Moellmann <gerd@gnu.org>.  */
21 22
/* Xt features made by Fred Pierresteguy.  */

23
#include <config.h>
24
#include <stdio.h>
Paul Eggert's avatar
Paul Eggert committed
25
#include <stdlib.h>
26
#include <math.h>
27

Jim Blandy's avatar
Jim Blandy committed
28
#include "lisp.h"
29
#include "blockinput.h"
30

Jim Blandy's avatar
Jim Blandy committed
31 32 33
/* This may include sys/types.h, and that somehow loses
   if this is not done before the other system files.  */
#include "xterm.h"
34
#include <X11/cursorfont.h>
Jim Blandy's avatar
Jim Blandy committed
35

36 37 38 39 40
/* If we have Xfixes extension, use it for pointer blanking.  */
#ifdef HAVE_XFIXES
#include <X11/extensions/Xfixes.h>
#endif

41 42 43 44
#ifdef HAVE_XDBE
#include <X11/extensions/Xdbe.h>
#endif

Jim Blandy's avatar
Jim Blandy committed
45 46 47 48
/* Load sys/types.h if not already loaded.
   In some systems loading it twice is suicidal.  */
#ifndef makedev
#include <sys/types.h>
49
#endif /* makedev */
Jim Blandy's avatar
Jim Blandy committed
50 51 52

#include <sys/ioctl.h>

Jim Blandy's avatar
Jim Blandy committed
53
#include "systime.h"
Jim Blandy's avatar
Jim Blandy committed
54 55 56 57

#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
Kenichi Handa's avatar
Kenichi Handa committed
58
#include "character.h"
Kenichi Handa's avatar
Kenichi Handa committed
59
#include "coding.h"
60
#include "composite.h"
61
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
62
#include "dispextern.h"
Paul Eggert's avatar
Paul Eggert committed
63
#include "xwidget.h"
64
#include "fontset.h"
Jim Blandy's avatar
Jim Blandy committed
65 66 67
#include "termhooks.h"
#include "termopts.h"
#include "termchar.h"
68
#include "emacs-icon.h"
Jim Blandy's avatar
Jim Blandy committed
69
#include "buffer.h"
70
#include "window.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
71
#include "keyboard.h"
Gerd Moellmann's avatar
Gerd Moellmann committed
72
#include "atimer.h"
73
#include "font.h"
74
#include "xsettings.h"
75
#include "sysselect.h"
76
#include "menu.h"
Daniel Colascione's avatar
Daniel Colascione committed
77
#include "pdumper.h"
Jim Blandy's avatar
Jim Blandy committed
78

79 80 81 82
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>
#endif

83 84
#include <unistd.h>

Jan Djärv's avatar
Jan Djärv committed
85 86
#ifdef USE_GTK
#include "gtkutil.h"
Jan Djärv's avatar
Jan Djärv committed
87 88 89
#ifdef HAVE_GTK3
#include <X11/Xproto.h>
#endif
Jan Djärv's avatar
Jan Djärv committed
90 91
#endif

92
#if defined (USE_LUCID) || defined (USE_MOTIF)
93
#include "../lwlib/xlwmenu.h"
Jan Djärv's avatar
Jan Djärv committed
94
#endif
95

Jan Djärv's avatar
Jan Djärv committed
96
#ifdef USE_X_TOOLKIT
97 98 99 100 101

/* Include toolkit specific headers for the scroll bar widget.  */

#ifdef USE_TOOLKIT_SCROLL_BARS
#if defined USE_MOTIF
102
#include <Xm/Xm.h>		/* For LESSTIF_VERSION */
103
#include <Xm/ScrollBar.h>
104 105 106
#else /* !USE_MOTIF i.e. use Xaw */

#ifdef HAVE_XAW3D
107 108
#include <X11/Xaw3d/Simple.h>
#include <X11/Xaw3d/Scrollbar.h>
109
#include <X11/Xaw3d/ThreeD.h>
110 111 112 113 114 115 116 117
#else /* !HAVE_XAW3D */
#include <X11/Xaw/Simple.h>
#include <X11/Xaw/Scrollbar.h>
#endif /* !HAVE_XAW3D */
#ifndef XtNpickTop
#define XtNpickTop "pickTop"
#endif /* !XtNpickTop */
#endif /* !USE_MOTIF */
118 119
#endif /* USE_TOOLKIT_SCROLL_BARS */

120 121
#endif /* USE_X_TOOLKIT */

122
#ifdef USE_X_TOOLKIT
Karl Heuer's avatar
Karl Heuer committed
123
#include "widget.h"
124 125 126 127 128
#ifndef XtNinitialState
#define XtNinitialState "initialState"
#endif
#endif

129 130
#include "bitmaps/gray.xbm"

131 132 133 134
#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

Dave Love's avatar
Dave Love committed
135
/* Default to using XIM if available.  */
136
#ifdef USE_XIM
137
bool use_xim = true;
138
#else
139
bool use_xim = false;  /* configure --without-xim */
140 141
#endif

142 143 144
/* Non-zero means that a HELP_EVENT has been generated since Emacs
   start.  */

145
static bool any_help_event_p;
146 147 148 149

/* This is a chain of structures for all the X displays currently in
   use.  */

150
struct x_display_info *x_display_list;
Jim Blandy's avatar
Jim Blandy committed
151

152
#ifdef USE_X_TOOLKIT
153

154 155
/* The application context for Xt use.  */
XtAppContext Xt_app_con;
156
static String Xt_default_resources[] = {0};
157

Gerd Moellmann's avatar
Gerd Moellmann committed
158
/* Non-zero means user is interacting with a toolkit scroll bar.  */
159
static bool toolkit_scroll_bar_interaction;
Gerd Moellmann's avatar
Gerd Moellmann committed
160

161
#endif /* USE_X_TOOLKIT */
Jim Blandy's avatar
Jim Blandy committed
162

Kenichi Handa's avatar
Kenichi Handa committed
163 164 165 166
/* Non-zero timeout value means ignore next mouse click if it arrives
   before that timeout elapses (i.e. as part of the same sequence of
   events resulting from clicking on a frame to select it).  */

167
static Time ignore_next_mouse_click_timeout;
Kenichi Handa's avatar
Kenichi Handa committed
168

169
/* Used locally within XTread_socket.  */
170

171
static int x_noop_count;
Jim Blandy's avatar
Jim Blandy committed
172

173 174 175 176 177
#ifdef USE_GTK
/* The name of the Emacs icon file.  */
static Lisp_Object xg_default_icon_file;
#endif

178 179 180
/* Some functions take this as char *, not const char *.  */
static char emacs_class[] = EMACS_CLASS;

Andreas Schwab's avatar
Andreas Schwab committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
enum xembed_info
  {
    XEMBED_MAPPED = 1 << 0
  };

enum xembed_message
  {
    XEMBED_EMBEDDED_NOTIFY        = 0,
    XEMBED_WINDOW_ACTIVATE        = 1,
    XEMBED_WINDOW_DEACTIVATE      = 2,
    XEMBED_REQUEST_FOCUS          = 3,
    XEMBED_FOCUS_IN               = 4,
    XEMBED_FOCUS_OUT              = 5,
    XEMBED_FOCUS_NEXT             = 6,
    XEMBED_FOCUS_PREV             = 7,

    XEMBED_MODALITY_ON            = 10,
    XEMBED_MODALITY_OFF           = 11,
    XEMBED_REGISTER_ACCELERATOR   = 12,
    XEMBED_UNREGISTER_ACCELERATOR = 13,
    XEMBED_ACTIVATE_ACCELERATOR   = 14
  };
203

Paul Eggert's avatar
Paul Eggert committed
204
static void x_free_cr_resources (struct frame *);
205
static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
206 207
static void x_raise_frame (struct frame *);
static void x_lower_frame (struct frame *);
208 209 210 211
static int x_io_error_quitter (Display *);
static struct terminal *x_create_terminal (struct x_display_info *);
static void x_frame_rehighlight (struct x_display_info *);

Joakim Verona's avatar
Joakim Verona committed
212 213
static void x_clip_to_row (struct window *, struct glyph_row *,
			   enum glyph_row_area, GC);
214
static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
215 216 217
static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
                                        enum scroll_bar_part *,
                                        Lisp_Object *, Lisp_Object *,
218
                                        Time *);
219 220 221 222
static void x_horizontal_scroll_bar_report_motion (struct frame **, Lisp_Object *,
						   enum scroll_bar_part *,
						   Lisp_Object *, Lisp_Object *,
						   Time *);
Paul Eggert's avatar
Paul Eggert committed
223
static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
224 225
static void x_check_fullscreen (struct frame *);
static void x_check_expected_move (struct frame *, int, int);
Paul Eggert's avatar
Paul Eggert committed
226
static void x_sync_with_move (struct frame *, int, int, bool);
227
static int handle_one_xevent (struct x_display_info *,
228
			      const XEvent *, int *,
229
			      struct input_event *);
230
#if ! (defined USE_X_TOOLKIT || defined USE_MOTIF) && defined USE_GTK
231 232 233
static int x_dispatch_event (XEvent *, Display *);
#endif
static void x_wm_set_window_state (struct frame *, int);
234
static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
235
static void x_initialize (void);
Jan Djärv's avatar
Jan Djärv committed
236

Paul Eggert's avatar
Paul Eggert committed
237
static bool get_current_wm_state (struct frame *, Window, int *, bool *);
238

Joakim Verona's avatar
Joakim Verona committed
239
/* Flush display of frame F.  */
240 241

static void
242
x_flush (struct frame *f)
243
{
Joakim Verona's avatar
Joakim Verona committed
244
  eassert (f && FRAME_X_P (f));
245 246 247 248 249
  /* Don't call XFlush when it is not safe to redisplay; the X
     connection may be broken.  */
  if (!NILP (Vinhibit_redisplay))
    return;

250
  block_input ();
Joakim Verona's avatar
Joakim Verona committed
251
  XFlush (FRAME_X_DISPLAY (f));
252
  unblock_input ();
253 254
}

Jim Blandy's avatar
Jim Blandy committed
255

256 257 258 259 260 261 262
/* Remove calls to XFlush by defining XFlush to an empty replacement.
   Calls to XFlush should be unnecessary because the X output buffer
   is flushed automatically as needed by calls to XPending,
   XNextEvent, or XWindowEvent according to the XFlush man page.
   XTread_socket calls XPending.  Removing XFlush improves
   performance.  */

Juanma Barranquero's avatar
Juanma Barranquero committed
263
#define XFlush(DISPLAY)	(void) 0
264

265

266 267 268 269
/***********************************************************************
			      Debugging
 ***********************************************************************/

Paul Eggert's avatar
Paul Eggert committed
270
#if false
271 272 273

/* This is a function useful for recording debugging information about
   the sequence of occurrences in this file.  */
274

Juanma Barranquero's avatar
Juanma Barranquero committed
275
struct record
276 277 278 279 280 281 282 283 284
{
  char *locus;
  int type;
};

struct record event_record[100];

int event_record_index;

Andreas Schwab's avatar
Andreas Schwab committed
285 286
void
record_event (char *locus, int type)
287
{
288
  if (event_record_index == ARRAYELTS (event_record))
289 290 291 292 293 294 295
    event_record_index = 0;

  event_record[event_record_index].locus = locus;
  event_record[event_record_index].type = type;
  event_record_index++;
}

Paul Eggert's avatar
Paul Eggert committed
296
#endif
297

Jan D's avatar
Jan D committed
298 299 300 301 302 303
#ifdef USE_CAIRO

#define FRAME_CR_CONTEXT(f)	((f)->output_data.x->cr_context)
#define FRAME_CR_SURFACE(f)	((f)->output_data.x->cr_surface)

static struct x_gc_ext_data *
304
x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
Jan D's avatar
Jan D committed
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
{
  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
  XEDataObject object;
  XExtData **head, *ext_data;

  object.gc = gc;
  head = XEHeadOfExtensionList (object);
  ext_data = XFindOnExtensionList (head, dpyinfo->ext_codes->extension);
  if (ext_data == NULL)
    {
      if (!create_if_not_found_p)
	return NULL;
      else
	{
	  ext_data = xzalloc (sizeof (*ext_data));
	  ext_data->number = dpyinfo->ext_codes->extension;
	  ext_data->private_data = xzalloc (sizeof (struct x_gc_ext_data));
	  XAddToExtensionList (head, ext_data);
	}
    }
  return (struct x_gc_ext_data *) ext_data->private_data;
}

static void
329
x_extension_initialize (struct x_display_info *dpyinfo)
Jan D's avatar
Jan D committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
  XExtCodes *ext_codes = XAddExtension (dpyinfo->display);

  dpyinfo->ext_codes = ext_codes;
}

static void
x_cr_destroy_surface (struct frame *f)
{
  if (FRAME_CR_SURFACE (f))
    {
      cairo_t *cr = FRAME_CR_CONTEXT (f);
      cairo_surface_destroy (FRAME_CR_SURFACE (f));
      FRAME_CR_SURFACE (f) = 0;
      if (cr) cairo_destroy (cr);
      FRAME_CR_CONTEXT (f) = NULL;
    }
}

cairo_t *
350
x_begin_cr_clip (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
351 352 353 354 355 356 357 358
{
  cairo_t *cr = FRAME_CR_CONTEXT (f);

  if (!cr)
    {

      if (! FRAME_CR_SURFACE (f))
        {
359 360 361 362 363
	  int scale = 1;
#ifdef USE_GTK
	  scale = xg_get_scale (f);
#endif

364 365
	  FRAME_CR_SURFACE (f) =
	    cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
366 367
					scale * FRAME_PIXEL_WIDTH (f),
					scale * FRAME_PIXEL_HEIGHT (f));
368 369
	}
      cr = cairo_create (FRAME_CR_SURFACE (f));
Jan D's avatar
Jan D committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      FRAME_CR_CONTEXT (f) = cr;
    }
  cairo_save (cr);

  if (gc)
    {
      struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);

      if (gc_ext && gc_ext->n_clip_rects)
	{
	  int i;

	  for (i = 0; i < gc_ext->n_clip_rects; i++)
	    cairo_rectangle (cr, gc_ext->clip_rects[i].x,
			     gc_ext->clip_rects[i].y,
			     gc_ext->clip_rects[i].width,
			     gc_ext->clip_rects[i].height);
	  cairo_clip (cr);
	}
    }

  return cr;
}

void
395
x_end_cr_clip (struct frame *f)
Jan D's avatar
Jan D committed
396 397 398 399 400
{
  cairo_restore (FRAME_CR_CONTEXT (f));
}

void
401
x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
402 403 404 405 406 407 408 409 410 411 412 413
{
  XGCValues xgcv;
  XColor color;

  XGetGCValues (FRAME_X_DISPLAY (f), gc, GCForeground, &xgcv);
  color.pixel = xgcv.foreground;
  x_query_color (f, &color);
  cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
			color.green / 65535.0, color.blue / 65535.0);
}

void
414
x_set_cr_source_with_gc_background (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428
{
  XGCValues xgcv;
  XColor color;

  XGetGCValues (FRAME_X_DISPLAY (f), gc, GCBackground, &xgcv);
  color.pixel = xgcv.background;
  x_query_color (f, &color);
  cairo_set_source_rgb (FRAME_CR_CONTEXT (f), color.red / 65535.0,
			color.green / 65535.0, color.blue / 65535.0);
}

/* Fringe bitmaps.  */

static int max_fringe_bmp = 0;
429
static cairo_surface_t **fringe_bmp = 0;
Jan D's avatar
Jan D committed
430 431

static void
432
x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
Jan D's avatar
Jan D committed
433 434 435 436 437 438 439 440 441
{
  int i, stride;
  cairo_surface_t *surface;
  unsigned char *data;

  if (which >= max_fringe_bmp)
    {
      i = max_fringe_bmp;
      max_fringe_bmp = which + 20;
442
      fringe_bmp = xrealloc (fringe_bmp, max_fringe_bmp * sizeof (*fringe_bmp));
Jan D's avatar
Jan D committed
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
      while (i < max_fringe_bmp)
	fringe_bmp[i++] = 0;
    }

  block_input ();

  surface = cairo_image_surface_create (CAIRO_FORMAT_A1, wd, h);
  stride = cairo_image_surface_get_stride (surface);
  data = cairo_image_surface_get_data (surface);

  for (i = 0; i < h; i++)
    {
      *((unsigned short *) data) = bits[i];
      data += stride;
    }

459
  cairo_surface_mark_dirty (surface);
Jan D's avatar
Jan D committed
460 461 462

  unblock_input ();

463
  fringe_bmp[which] = surface;
Jan D's avatar
Jan D committed
464 465 466
}

static void
467
x_cr_destroy_fringe_bitmap (int which)
Jan D's avatar
Jan D committed
468 469 470 471 472 473 474
{
  if (which >= max_fringe_bmp)
    return;

  if (fringe_bmp[which])
    {
      block_input ();
475
      cairo_surface_destroy (fringe_bmp[which]);
Jan D's avatar
Jan D committed
476 477 478 479 480 481
      unblock_input ();
    }
  fringe_bmp[which] = 0;
}

static void
482
x_cr_draw_image (struct frame *f, GC gc, cairo_surface_t *image,
483
		 int image_width, int image_height,
484 485
		 int src_x, int src_y, int width, int height,
		 int dest_x, int dest_y, bool overlay_p)
Jan D's avatar
Jan D committed
486
{
487
  cairo_t *cr = x_begin_cr_clip (f, gc);
Jan D's avatar
Jan D committed
488 489 490 491 492 493 494 495 496

  if (overlay_p)
    cairo_rectangle (cr, dest_x, dest_y, width, height);
  else
    {
      x_set_cr_source_with_gc_background (f, gc);
      cairo_rectangle (cr, dest_x, dest_y, width, height);
      cairo_fill_preserve (cr);
    }
497 498 499 500 501 502 503 504 505 506 507 508 509 510

  int orig_image_width = cairo_image_surface_get_width (image);
  if (image_width == 0) image_width = orig_image_width;
  int orig_image_height = cairo_image_surface_get_height (image);
  if (image_height == 0) image_height = orig_image_height;

  cairo_pattern_t *pattern = cairo_pattern_create_for_surface (image);
  cairo_matrix_t matrix;
  cairo_matrix_init_scale (&matrix, orig_image_width / (double) image_width,
			   orig_image_height / (double) image_height);
  cairo_matrix_translate (&matrix, src_x - dest_x, src_y - dest_y);
  cairo_pattern_set_matrix (pattern, &matrix);

  cairo_format_t format = cairo_image_surface_get_format (image);
Jan D's avatar
Jan D committed
511 512
  if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
    {
513
      cairo_set_source (cr, pattern);
Jan D's avatar
Jan D committed
514 515 516 517 518
      cairo_fill (cr);
    }
  else
    {
      x_set_cr_source_with_gc_foreground (f, gc);
519
      cairo_clip (cr);
520
      cairo_mask (cr, pattern);
Jan D's avatar
Jan D committed
521
    }
522 523
  cairo_pattern_destroy (pattern);

Jan D's avatar
Jan D committed
524 525 526 527
  x_end_cr_clip (f);
}

void
528
x_cr_draw_frame (cairo_t *cr, struct frame *f)
Jan D's avatar
Jan D committed
529 530 531 532 533 534
{
  int width, height;

  width = FRAME_PIXEL_WIDTH (f);
  height = FRAME_PIXEL_HEIGHT (f);

535
  x_free_cr_resources (f);
Jan D's avatar
Jan D committed
536 537 538 539 540 541 542
  FRAME_CR_CONTEXT (f) = cr;
  x_clear_area (f, 0, 0, width, height);
  expose_frame (f, 0, 0, width, height);
  FRAME_CR_CONTEXT (f) = NULL;
}

static cairo_status_t
543 544
x_cr_accumulate_data (void *closure, const unsigned char *data,
		      unsigned int length)
Jan D's avatar
Jan D committed
545 546 547
{
  Lisp_Object *acc = (Lisp_Object *) closure;

548
  *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
Jan D's avatar
Jan D committed
549 550 551 552 553

  return CAIRO_STATUS_SUCCESS;
}

static void
Ari Roponen's avatar
Ari Roponen committed
554
x_cr_destroy (void *cr)
Jan D's avatar
Jan D committed
555 556 557 558 559 560 561
{
  block_input ();
  cairo_destroy (cr);
  unblock_input ();
}

Lisp_Object
562
x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
Jan D's avatar
Jan D committed
563 564 565 566 567 568
{
  struct frame *f;
  cairo_surface_t *surface;
  cairo_t *cr;
  int width, height;
  void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
569
  Lisp_Object acc = Qnil;
570
  ptrdiff_t count = SPECPDL_INDEX ();
Jan D's avatar
Jan D committed
571

572 573
  specbind (Qredisplay_dont_pause, Qt);
  redisplay_preserve_echo_area (31);
Jan D's avatar
Jan D committed
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

  f = XFRAME (XCAR (frames));
  frames = XCDR (frames);
  width = FRAME_PIXEL_WIDTH (f);
  height = FRAME_PIXEL_HEIGHT (f);

  block_input ();
#ifdef CAIRO_HAS_PDF_SURFACE
  if (surface_type == CAIRO_SURFACE_TYPE_PDF)
    {
      surface = cairo_pdf_surface_create_for_stream (x_cr_accumulate_data, &acc,
						     width, height);
      surface_set_size_func = cairo_pdf_surface_set_size;
    }
  else
#endif
#ifdef CAIRO_HAS_PNG_FUNCTIONS
  if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
  else
#endif
#ifdef CAIRO_HAS_PS_SURFACE
  if (surface_type == CAIRO_SURFACE_TYPE_PS)
    {
      surface = cairo_ps_surface_create_for_stream (x_cr_accumulate_data, &acc,
						    width, height);
      surface_set_size_func = cairo_ps_surface_set_size;
    }
  else
#endif
#ifdef CAIRO_HAS_SVG_SURFACE
  if (surface_type == CAIRO_SURFACE_TYPE_SVG)
    surface = cairo_svg_surface_create_for_stream (x_cr_accumulate_data, &acc,
						   width, height);
  else
#endif
    abort ();

  cr = cairo_create (surface);
  cairo_surface_destroy (surface);
Ari Roponen's avatar
Ari Roponen committed
614
  record_unwind_protect_ptr (x_cr_destroy, cr);
Jan D's avatar
Jan D committed
615 616 617

  while (1)
    {
618
      x_free_cr_resources (f);
Jan D's avatar
Jan D committed
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
      FRAME_CR_CONTEXT (f) = cr;
      x_clear_area (f, 0, 0, width, height);
      expose_frame (f, 0, 0, width, height);
      FRAME_CR_CONTEXT (f) = NULL;

      if (NILP (frames))
	break;

      cairo_surface_show_page (surface);
      f = XFRAME (XCAR (frames));
      frames = XCDR (frames);
      width = FRAME_PIXEL_WIDTH (f);
      height = FRAME_PIXEL_HEIGHT (f);
      if (surface_set_size_func)
	(*surface_set_size_func) (surface, width, height);
634

Jan D's avatar
Jan D committed
635
      unblock_input ();
Paul Eggert's avatar
Paul Eggert committed
636
      maybe_quit ();
637
      block_input ();
Jan D's avatar
Jan D committed
638 639 640 641 642 643 644 645 646
    }

#ifdef CAIRO_HAS_PNG_FUNCTIONS
  if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
    {
      cairo_surface_flush (surface);
      cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
    }
#endif
647 648
  unblock_input ();

Jan D's avatar
Jan D committed
649 650
  unbind_to (count, Qnil);

651
  return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
Jan D's avatar
Jan D committed
652 653 654 655 656
}

#endif	/* USE_CAIRO */

static void
657
x_free_cr_resources (struct frame *f)
Jan D's avatar
Jan D committed
658 659 660 661 662 663 664
{
#ifdef USE_CAIRO
  if (f == NULL)
    {
      Lisp_Object rest, frame;
      FOR_EACH_FRAME (rest, frame)
	if (FRAME_X_P (XFRAME (frame)))
665
	  x_free_cr_resources (XFRAME (frame));
Jan D's avatar
Jan D committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
    }
  else
    {
      cairo_t *cr = FRAME_CR_CONTEXT (f);

      if (cr)
	{
	  cairo_surface_t *surface = cairo_get_target (cr);

	  if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XLIB)
	    {
	      cairo_destroy (cr);
	      FRAME_CR_CONTEXT (f) = NULL;
	    }
	}
    }
#endif
}

static void
686
x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
Jan D's avatar
Jan D committed
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
{
  XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, rectangles, n, Unsorted);
#ifdef USE_CAIRO
  eassert (n >= 0 && n <= MAX_CLIP_RECTS);

  {
    struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 1);

    gc_ext->n_clip_rects = n;
    memcpy (gc_ext->clip_rects, rectangles, sizeof (XRectangle) * n);
  }
#endif
}

static void
702
x_reset_clip_rectangles (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
703 704 705 706 707 708 709 710 711 712 713 714
{
  XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
#ifdef USE_CAIRO
  {
    struct x_gc_ext_data *gc_ext = x_gc_get_ext_data (f, gc, 0);

    if (gc_ext)
      gc_ext->n_clip_rects = 0;
  }
#endif
}

715
static void
716
x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
Jan D's avatar
Jan D committed
717 718 719 720 721 722 723 724 725 726
{
#ifdef USE_CAIRO
  cairo_t *cr;

  cr = x_begin_cr_clip (f, gc);
  x_set_cr_source_with_gc_foreground (f, gc);
  cairo_rectangle (cr, x, y, width, height);
  cairo_fill (cr);
  x_end_cr_clip (f);
#else
727
  XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
Jan D's avatar
Jan D committed
728 729 730 731 732
		  gc, x, y, width, height);
#endif
}

static void
733
x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
Jan D's avatar
Jan D committed
734 735 736 737 738 739 740 741 742 743 744
{
#ifdef USE_CAIRO
  cairo_t *cr;

  cr = x_begin_cr_clip (f, gc);
  x_set_cr_source_with_gc_foreground (f, gc);
  cairo_rectangle (cr, x + 0.5, y + 0.5, width, height);
  cairo_set_line_width (cr, 1);
  cairo_stroke (cr);
  x_end_cr_clip (f);
#else
745
  XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
Jan D's avatar
Jan D committed
746 747 748 749 750
		  gc, x, y, width, height);
#endif
}

static void
751
x_clear_window (struct frame *f)
Jan D's avatar
Jan D committed
752 753 754 755 756 757 758 759 760
{
#ifdef USE_CAIRO
  cairo_t *cr;

  cr = x_begin_cr_clip (f, NULL);
  x_set_cr_source_with_gc_background (f, f->output_data.x->normal_gc);
  cairo_paint (cr);
  x_end_cr_clip (f);
#else
761 762 763 764
  if (FRAME_X_DOUBLE_BUFFERED_P (f))
    x_clear_area (f, 0, 0, FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f));
  else
    XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
Jan D's avatar
Jan D committed
765 766 767 768 769
#endif
}

#ifdef USE_CAIRO
static void
770
x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
771
			     int width, int height, int top_p)
Jan D's avatar
Jan D committed
772 773 774 775 776 777 778 779 780 781 782 783
{
  cairo_t *cr;

  cr = x_begin_cr_clip (f, gc);
  x_set_cr_source_with_gc_foreground (f, gc);
  cairo_move_to (cr, top_p ? x : x + height, y);
  cairo_line_to (cr, x, y + height);
  cairo_line_to (cr, top_p ? x + width - height : x + width, y + height);
  cairo_line_to (cr, x + width, y);
  cairo_fill (cr);
  x_end_cr_clip (f);
}
784

785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
enum corners
  {
    CORNER_BOTTOM_RIGHT,	/* 0 -> pi/2 */
    CORNER_BOTTOM_LEFT,		/* pi/2 -> pi */
    CORNER_TOP_LEFT,		/* pi -> 3pi/2 */
    CORNER_TOP_RIGHT,		/* 3pi/2 -> 2pi */
    CORNER_LAST
  };

static void
x_erase_corners_for_relief (struct frame *f, GC gc, int x, int y,
			    int width, int height,
			    double radius, double margin, int corners)
{
  cairo_t *cr;
  int i;

  cr = x_begin_cr_clip (f, gc);
  x_set_cr_source_with_gc_background (f, gc);
  for (i = 0; i < CORNER_LAST; i++)
    if (corners & (1 << i))
      {
	double xm, ym, xc, yc;

	if (i == CORNER_TOP_LEFT || i == CORNER_BOTTOM_LEFT)
	  xm = x - margin, xc = xm + radius;
	else
	  xm = x + width + margin, xc = xm - radius;
	if (i == CORNER_TOP_LEFT || i == CORNER_TOP_RIGHT)
	  ym = y - margin, yc = ym + radius;
	else
	  ym = y + height + margin, yc = ym - radius;

	cairo_move_to (cr, xm, ym);
	cairo_arc (cr, xc, yc, radius, i * M_PI_2, (i + 1) * M_PI_2);
      }
  cairo_clip (cr);
  cairo_rectangle (cr, x, y, width, height);
  cairo_fill (cr);
  x_end_cr_clip (f);
}

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
static void
x_draw_horizontal_wave (struct frame *f, GC gc, int x, int y,
			int width, int height, int wave_length)
{
  cairo_t *cr;
  double dx = wave_length, dy = height - 1;
  int xoffset, n;

  cr = x_begin_cr_clip (f, gc);
  x_set_cr_source_with_gc_foreground (f, gc);
  cairo_rectangle (cr, x, y, width, height);
  cairo_clip (cr);

  if (x >= 0)
    {
      xoffset = x % (wave_length * 2);
      if (xoffset == 0)
	xoffset = wave_length * 2;
    }
  else
    xoffset = x % (wave_length * 2) + wave_length * 2;
  n = (width + xoffset) / wave_length + 1;
  if (xoffset > wave_length)
    {
      xoffset -= wave_length;
      --n;
      y += height - 1;
      dy = -dy;
    }

  cairo_move_to (cr, x - xoffset + 0.5, y + 0.5);
  while (--n >= 0)
    {
      cairo_rel_line_to (cr, dx, dy);
      dy = -dy;
    }
  cairo_set_line_width (cr, 1);
  cairo_stroke (cr);
  x_end_cr_clip (f);
}
Jan D's avatar
Jan D committed
867
#endif
868

869

870 871 872
/* Return the struct x_display_info corresponding to DPY.  */

struct x_display_info *
873
x_display_info_for_display (Display *dpy)
874 875 876 877 878 879
{
  struct x_display_info *dpyinfo;

  for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
    if (dpyinfo->display == dpy)
      return dpyinfo;
880

881 882
  return 0;
}
883

884 885 886 887 888 889 890
static Window
x_find_topmost_parent (struct frame *f)
{
  struct x_output *x = f->output_data.x;
  Window win = None, wi = x->parent_desc;
  Display *dpy = FRAME_X_DISPLAY (f);

891
  while (wi != FRAME_DISPLAY_INFO (f)->root_window)
892 893 894 895 896 897
    {
      Window root;
      Window *children;
      unsigned int nchildren;

      win = wi;
898 899 900 901
      if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
	XFree (children);
      else
	break;
902 903 904 905 906
    }

  return win;
}

907 908 909
#define OPAQUE  0xffffffff

void
910
x_set_frame_alpha (struct frame *f)
911
{
912
  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
913 914
  Display *dpy = FRAME_X_DISPLAY (f);
  Window win = FRAME_OUTER_WINDOW (f);
915 916
  double alpha = 1.0;
  double alpha_min = 1.0;
917
  unsigned long opac;
918
  Window parent;
919

920 921 922 923 924 925 926
  if (dpyinfo->x_highlight_frame == f)
    alpha = f->alpha[0];
  else
    alpha = f->alpha[1];

  if (FLOATP (Vframe_alpha_lower_limit))
    alpha_min = XFLOAT_DATA (Vframe_alpha_lower_limit);
927
  else if (FIXNUMP (Vframe_alpha_lower_limit))
Tom Tromey's avatar
Tom Tromey committed
928
    alpha_min = (XFIXNUM (Vframe_alpha_lower_limit)) / 100.0;
929

930 931 932
  if (alpha < 0.0)
    return;
  else if (alpha > 1.0)
933 934 935 936
    alpha = 1.0;
  else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
    alpha = alpha_min;

937
  opac = alpha * OPAQUE;
938

939 940 941 942 943 944 945
  x_catch_errors (dpy);

  /* If there is a parent from the window manager, put the property there
     also, to work around broken window managers that fail to do that.
     Do this unconditionally as this function is called on reparent when
     alpha has not changed on the frame.  */

946 947 948 949 950 951 952 953
  if (!FRAME_PARENT_FRAME (f))
    {
      parent = x_find_topmost_parent (f);
      if (parent != None)
	XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
			 XA_CARDINAL, 32, PropModeReplace,
			 (unsigned char *) &opac, 1);
    }
954

955 956 957 958
  /* return unless necessary */
  {
    unsigned char *data;
    Atom actual;
959
    int rc, format;
960 961
    unsigned long n, left;

962
    rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
963
			     0, 1, False, XA_CARDINAL,
964 965
			     &actual, &format, &n, &left,
			     &data);
966 967

    if (rc == Success && actual != None)
968
      {
969
        unsigned long value = *(unsigned long *)data;
970
	XFree (data);
971
	if (value == opac)
972 973 974 975 976
	  {
	    x_uncatch_errors ();
	    return;
	  }
      }
977 978
  }

979
  XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
980
		   XA_CARDINAL, 32, PropModeReplace,
981
		   (unsigned char *) &opac, 1);
982
  x_uncatch_errors ();
983
}
984 985 986 987

/***********************************************************************
		    Starting and ending an update
 ***********************************************************************/
Juanma Barranquero's avatar
Juanma Barranquero committed
988

989 990 991 992 993
/* Start an update of frame F.  This function is installed as a hook
   for update_begin, i.e. it is called when update_begin is called.
   This function is called prior to calls to x_update_window_begin for
   each window being updated.  Currently, there is nothing to do here
   because all interesting stuff is done on a window basis.  */
Jim Blandy's avatar
Jim Blandy committed
994

Andreas Schwab's avatar
Andreas Schwab committed
995
static void
996
x_update_begin (struct frame *f)
997
{
Jan D's avatar
Jan D committed
998
#ifdef USE_CAIRO
999
  if (FRAME_TOOLTIP_P (f) && !FRAME_VISIBLE_P (f))
1000 1001
    return;

Jan D's avatar
Jan D committed
1002 1003 1004 1005 1006 1007 1008
  if (! FRAME_CR_SURFACE (f))
    {
      int width, height;
#ifdef USE_GTK
      if (FRAME_GTK_WIDGET (f))
        {
          GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
1009 1010
	  int scale = xg_get_scale (f);
	  width = scale * gdk_window_get_width (w);
1011
	  height = scale * gdk_window_get_height (w);
Jan D's avatar
Jan D committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
        }
      else
#endif
        {
          width = FRAME_PIXEL_WIDTH (f);
          height = FRAME_PIXEL_HEIGHT (f);
          if (! FRAME_EXTERNAL_TOOL_BAR (f))
            height += FRAME_TOOL_BAR_HEIGHT (f);
          if (! FRAME_EXTERNAL_MENU_BAR (f))
            height += FRAME_MENU_BAR_HEIGHT (f);
        }
Jim Blandy's avatar
Jim Blandy committed
1023

Jan D's avatar
Jan D committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
      if (width > 0 && height > 0)
        {
          block_input();
          FRAME_CR_SURFACE (f) = cairo_image_surface_create
            (CAIRO_FORMAT_ARGB32, width, height);
          unblock_input();
        }
    }
#endif /* USE_CAIRO */
}
Jim Blandy's avatar
Jim Blandy committed
1034

Joakim Verona's avatar
Joakim Verona committed
1035
/* Start update of window W.  */
Jim Blandy's avatar
Jim Blandy committed
1036

1037
static void
1038
x_update_window_begin (struct window *w)
1039 1040
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
1041
  Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
Juanma Barranquero's avatar
Juanma Barranquero committed
1042

Joakim Verona's avatar
Joakim Verona committed
1043
  w->output_cursor = w->cursor;
1044

1045
  block_input ();
1046

1047
  if (f == hlinfo->mouse_face_mouse_frame)
1048
    {
1049
      /* Don't do highlighting for mouse motion during the update.  */
Paul Eggert's avatar
Paul Eggert committed
1050
      hlinfo->mouse_face_defer = true;
1051

1052 1053
      /* If F needs to be redrawn, simply forget about any prior mouse
	 highlighting.  */
1054
      if (FRAME_GARBAGED_P (f))
1055
	hlinfo->mouse_face_window = Qnil;
1056
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
1057

1058
  unblock_input ();
Jim Blandy's avatar
Jim Blandy committed
1059 1060
}

1061

1062
/* Draw a vertical window border from (x,y0) to (x,y1)  */
1063

Andreas Schwab's avatar
Andreas Schwab committed
1064
static void
1065
x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1066
{
1067
  struct frame *f = XFRAME (WINDOW_FRAME (w));
1068 1069
  struct face *face;

1070
  face = FACE_FROM_ID_OR_NULL (f, VERTICAL_BORDER_FACE_ID);
1071 1072 1073
  if (face)
    XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
		    face->foreground);
1074

Jan D's avatar
Jan D committed
1075 1076 1077
#ifdef USE_CAIRO
  x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
#else
1078
  XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
1079
	     f->output_data.x->normal_gc, x, y0, x, y1);
Jan D's avatar
Jan D committed
1080
#endif
1081
}
Juanma Barranquero's avatar
Juanma Barranquero committed
1082

1083 1084 1085 1086 1087 1088
/* Draw a window divider from (x0,y0) to (x1,y1)  */

static void
x_draw_window_divider (struct window *w, int x0, int x1, int y0, int y1)
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
1089
  struct face *face = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FACE_ID);
1090
  struct face *face_first
1091
    = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
1092
  struct face *face_last
1093
    = FACE_FROM_ID_OR_NULL (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
1094 1095 1096 1097 1098 1099 1100 1101 1102
  unsigned long color = face ? face->foreground : FRAME_FOREGROUND_PIXEL (f);
  unsigned long color_first = (face_first
			       ? face_first->foreground
			       : FRAME_FOREGROUND_PIXEL (f));
  unsigned long color_last = (face_last
			      ? face_last->foreground
			      : FRAME_FOREGROUND_PIXEL (f));
  Display *display = FRAME_X_DISPLAY (f);

1103 1104 1105
  if ((y1 - y0 > x1 - x0) && (x1 - x0 >= 3))
    /* A vertical divider, at least three pixels wide: Draw first and
       last pixels differently.  */
1106 1107
    {
      XSetForeground (display, f->output_data.x->normal_gc, color_first);
1108 1109
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0, 1, y1 - y0);
1110
      XSetForeground (display, f->output_data.x->normal_gc, color);
1111 1112
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1113
      XSetForeground (display, f->output_data.x->normal_gc, color_last);
1114 1115
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x1 - 1, y0, 1, y1 - y0);
1116
    }
1117 1118 1119
  else if ((x1 - x0 > y1 - y0) && (y1 - y0 >= 3))
    /* A horizontal divider, at least three pixels high: Draw first and
       last pixels differently.  */
1120 1121
    {
      XSetForeground (display, f->output_data.x->normal_gc, color_first);
1122 1123
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0, x1 - x0, 1);
1124
      XSetForeground (display, f->output_data.x->normal_gc, color);
1125 1126
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1127
      XSetForeground (display, f->output_data.x->normal_gc, color_last);
1128 1129
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y1 - 1, x1 - x0, 1);
1130 1131 1132
    }
  else
    {
1133 1134
    /* In any other case do not draw the first and last pixels
       differently.  */
1135
      XSetForeground (display, f->output_data.x->normal_gc, color);
1136 1137
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0, x1 - x0, y1 - y0);
1138
    }
1139 1140
}

1141
/* End update of window W.
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

   Draw vertical borders between horizontally adjacent windows, and
   display W's cursor if CURSOR_ON_P is non-zero.

   MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
   glyphs in mouse-face were overwritten.  In that case we have to
   make sure that the mouse-highlight is properly redrawn.

   W may be a menu bar pseudo-window in case we don't have X toolkit
   support.  Such windows don't have a cursor, so don't display it
   here.  */
1153 1154

static void
1155 1156
x_update_window_end (struct window *w, bool cursor_on_p,
		     bool mouse_face_overwritten_p)
1157 1158 1159
{
  if (!w->pseudo_window_p)
    {
1160
      block_input ();
1161

1162
      if (cursor_on_p)
Paul Eggert's avatar
Paul Eggert committed
1163
	display_and_set_cursor (w, true,
Joakim Verona's avatar
Joakim Verona committed
1164 1165
				w->output_cursor.hpos, w->output_cursor.vpos,
				w->output_cursor.x, w->output_cursor.y);
Juanma Barranquero's avatar
Juanma Barranquero committed
1166

Paul Eggert's avatar
Paul Eggert committed
1167
      if (draw_window_fringes (w, true))
1168 1169 1170 1171 1172 1173
	{
	  if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
	    x_draw_right_divider (w);
	  else
	    x_draw_vertical_border (w);
	}
Kenichi Handa's avatar
Kenichi Handa committed
1174

1175
      unblock_input ();
1176
    }
Juanma Barranquero's avatar
Juanma Barranquero committed
1177

1178 1179 1180
  /* If a row with mouse-face was overwritten, arrange for
     XTframe_up_to_date to redisplay the mouse highlight.  */
  if (mouse_face_overwritten_p)
1181 1182 1183 1184 1185 1186 1187
    {
      Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));

      hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
      hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
      hlinfo->mouse_face_window = Qnil;
    }
1188
}
Jim Blandy's avatar
Jim Blandy committed
1189

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
/* Show the frame back buffer.  If frame is double-buffered,
   atomically publish to the user's screen graphics updates made since
   the last call to show_back_buffer.  */
static void
show_back_buffer (struct frame *f)
{
  block_input ();
  if (FRAME_X_DOUBLE_BUFFERED_P (f))
    {
#ifdef HAVE_XDBE
      XdbeSwapInfo swap_info;
      memset (&swap_info, 0, sizeof (swap_info));
      swap_info.swap_window = FRAME_X_WINDOW (f);
      swap_info.swap_action = XdbeCopied;
      XdbeSwapBuffers (FRAME_X_DISPLAY (f), &swap_info, 1);
#else
      eassert (!"should have back-buffer only with XDBE");
#endif
    }
  FRAME_X_NEED_BUFFER_FLIP (f) = false;
  unblock_input ();
}

/* Updates back buffer and flushes changes to display.  Called from
   minibuf read code.  Note that we display the back buffer even if
   buffer flipping is blocked.  */
static void
x_flip_and_flush (struct frame *f)
{
  block_input ();
  if (FRAME_X_NEED_BUFFER_FLIP (f))
      show_back_buffer (f);
  x_flush (f);
  unblock_input ();
}
Jim Blandy's avatar
Jim Blandy committed
1225

1226 1227 1228 1229
/* End update of frame F.  This function is installed as a hook in
   update_end.  */

static void
1230
x_update_end (struct frame *f)
1231 1232
{
  /* Mouse highlight may be displayed again.  */
Paul Eggert's avatar
Paul Eggert committed
1233
  MOUSE_HL_INFO (f)->mouse_face_defer = false;
1234

Jan D's avatar
Jan D committed
1235 1236 1237
#ifdef USE_CAIRO
  if (FRAME_CR_SURFACE (f))
    {
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
      cairo_t *cr;
      cairo_surface_t *surface;
      int width, height;

      block_input ();
      width = FRAME_PIXEL_WIDTH (f);
      height = FRAME_PIXEL_HEIGHT (f);
      if (! FRAME_EXTERNAL_TOOL_BAR (f))
	height += FRAME_TOOL_BAR_HEIGHT (f);
      if (! FRAME_EXTERNAL_MENU_BAR (f))
	height += FRAME_MENU_BAR_HEIGHT (f);
      surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
					   FRAME_X_DRAWABLE (f),
					   FRAME_DISPLAY_INFO (f)->visual,
					   width,
					   height);
      cr = cairo_create (surface);
      cairo_surface_destroy (surface);
Jan D's avatar
Jan D committed
1256 1257 1258 1259 1260 1261

      cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
      cairo_paint (cr);
      cairo_destroy (cr);
      unblock_input ();
    }
1262
#endif
Jan D's avatar
Jan D committed
1263

1264
#ifndef XFlush
1265
  block_input ();
1266
  XFlush (FRAME_X_DISPLAY (f));
1267
  unblock_input ();
1268
#endif
Jim Blandy's avatar
Jim Blandy committed
1269
}
1270

1271 1272
/* This function is called from various places in xdisp.c
   whenever a complete update has been performed.  */
1273

Andreas Schwab's avatar
Andreas Schwab committed
1274
static void
1275
XTframe_up_to_date (struct frame *f)
1276
{
1277 1278 1279 1280 1281 1282
  eassert (FRAME_X_P (f));
  block_input ();
  FRAME_MOUSE_UPDATE (f);
  if (!buffer_flipping_blocked_p () && FRAME_X_NEED_BUFFER_FLIP (f))
    show_back_buffer (f);
  unblock_input ();
1283
}
1284

1285 1286 1287 1288 1289 1290
static void
XTbuffer_flipping_unblocked_hook (struct frame *f)
{
  if (FRAME_X_NEED_BUFFER_FLIP (f))
    show_back_buffer (f);
}
1291

1292 1293 1294 1295 1296 1297
/**
 * x_clear_under_internal_border:
 *
 * Clear area of frame F's internal border.  If the internal border face
 * of F has been specified (is not null), fill the area with that face.
 */
1298 1299 1300 1301 1302 1303 1304 1305
void
x_clear_under_internal_border (struct frame *f)
{
  if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0)
    {
      int border = FRAME_INTERNAL_BORDER_WIDTH (f);
      int width = FRAME_PIXEL_WIDTH (f);
      int height = FRAME_PIXEL_HEIGHT (f);
1306 1307 1308
#ifdef USE_GTK
      int margin = 0;
#else
1309
      int margin = FRAME_TOP_MARGIN_HEIGHT (f);
1310
#endif
1311 1312 1313 1314 1315
      int face_id =
	!NILP (Vface_remapping_alist)
	? lookup_basic_face (NULL, f, INTERNAL_BORDER_FACE_ID)
	: INTERNAL_BORDER_FACE_ID;
      struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1316 1317

      block_input ();
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339

      if (face)
	{
	  unsigned long color = face->background;
	  Display *display = FRAME_X_DISPLAY (f);
	  GC gc = f->output_data.x->normal_gc;

	  XSetForeground (display, gc, color);
	  x_fill_rectangle (f, gc, 0, margin, width, border);
	  x_fill_rectangle (f, gc, 0, 0, border, height);
	  x_fill_rectangle (f, gc, width - border, 0, border, height);
	  x_fill_rectangle (f, gc, 0, height - border, width, border);
	  XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
	}
      else
	{
	  x_clear_area (f, 0, 0, border, height);
	  x_clear_area (f, 0, margin, width, border);
	  x_clear_area (f, width - border, 0, border, height);
	  x_clear_area (f, 0, height - border, width, border);
	}

1340 1341 1342 1343
      unblock_input ();
    }
}

1344
/* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
Kim F. Storm's avatar
Kim F. Storm committed
1345
   arrow bitmaps, or clear the fringes if no bitmaps are required
1346
   before DESIRED_ROW is made current.  This function is called from
1347 1348 1349 1350
   update_window_line only if it is known that there are differences
   between bitmaps to be drawn between current row and DESIRED_ROW.  */

static void
1351
x_after_update_window_line (struct window *w, struct glyph_row *desired_row)
1352
{
1353
  eassert (w);
Juanma Barranquero's avatar
Juanma Barranquero committed
1354

1355
  if (!desired_row->mode_line_p && !w->pseudo_window_p)
Paul Eggert's avatar
Paul Eggert committed
1356
    desired_row->redraw_fringe_bitmaps_p = true;
Juanma Barranquero's avatar
Juanma Barranquero committed
1357

1358
#ifdef USE_X_TOOLKIT
1359 1360
  /* When a window has disappeared, make sure that no rest of
     full-width rows stays visible in the internal border.  Could
1361
     check here if updated window is the leftmost/rightmost window,
1362 1363 1364
     but I guess it's not worth doing since vertically split windows
     are almost never used, internal border is rarely set, and the
     overhead is very small.  */
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
  {
    struct frame *f;
    int width, height;

    if (windows_or_buffers_changed
	&& desired_row->full_width_p
	&& (f = XFRAME (w->frame),
	    width = FRAME_INTERNAL_BORDER_WIDTH (f),
	    width != 0)
	&& (height = desired_row->visible_height,
	    height > 0))
      {
	int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
1378 1379 1380 1381 1382
	int face_id =
	  !NILP (Vface_remapping_alist)
	  ? lookup_basic_face (NULL, f, INTERNAL_BORDER_FACE_ID)
	  : INTERNAL_BORDER_FACE_ID;
	struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
1383

1384
	block_input ();
1385 1386 1387 1388
	if (face)
	  {
	    unsigned long color = face->background;
	    Display *display = FRAME_X_DISPLAY (f);
1389
	    GC gc = f->output_data.x->normal_gc;
1390

1391 1392 1393 1394 1395
	    XSetForeground (display, gc, color);
	    x_fill_rectangle (f, gc, 0, y, width, height);
	    x_fill_rectangle (f, gc, FRAME_PIXEL_WIDTH (f) - width, y,
			      width, height);
	    XSetForeground (display, gc, FRAME_FOREGROUND_PIXEL (f));
1396 1397 1398 1399 1400 1401
	  }
	else
	  {
	    x_clear_area (f, 0, y, width, height);
	    x_clear_area (f, FRAME_PIXEL_WIDTH (f) - width, y, width, height);
	  }
1402 1403 1404 1405
	unblock_input ();
      }
  }
#endif
1406 1407 1408
}

static void
1409
x_draw_fringe_bitmap (struct window *w, struct glyph_row *row, struct draw_fringe_bitmap_params *p)
1410 1411 1412 1413
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  Display *display = FRAME_X_DISPLAY (f);
  GC gc = f->output_data.x->normal_gc;
Kim F. Storm's avatar
Kim F. Storm committed
1414
  struct face *face = p->face;
1415 1416

  /* Must clip because of partially visible lines.  */
Joakim Verona's avatar
Joakim Verona committed
1417
  x_clip_to_row (w, row, ANY_AREA, gc);
1418

1419
  if (p->bx >= 0 && !p->overlay_p)
1420 1421 1422 1423 1424 1425 1426 1427 1428
    {
      /* In case the same realized face is used for fringes and
	 for something displayed in the text (e.g. face `region' on
	 mono-displays, the fill style may have been changed to
	 FillSolid in x_draw_glyph_string_background.  */
      if (face->stipple)
	XSetFillStyle (display, face->gc, FillOpaqueStippled);
      else
	XSetForeground (display, face->gc, face->background);
Juanma Barranquero's avatar
Juanma Barranquero committed
1429

Jan D's avatar
Jan D committed
1430
      x_fill_rectangle (f, face->gc, p->bx, p->by, p->nx, p->ny);