xterm.c 372 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-2015 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

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>
25 26 27
#ifdef USE_CAIRO
#include <math.h>
#endif
28

Jim Blandy's avatar
Jim Blandy committed
29
#include "lisp.h"
30
#include "blockinput.h"
31 32
#include "syssignal.h"

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

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

43 44 45 46 47
/* Using Xft implies that XRender is available.  */
#ifdef HAVE_XFT
#include <X11/extensions/Xrender.h>
#endif

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

#include <sys/ioctl.h>

Jim Blandy's avatar
Jim Blandy committed
56
#include "systime.h"
Jim Blandy's avatar
Jim Blandy committed
57 58 59 60

#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
61 62
/* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed.  */
/* #include <sys/param.h>  */
Jim Blandy's avatar
Jim Blandy committed
63

64
#include "charset.h"
Kenichi Handa's avatar
Kenichi Handa committed
65
#include "character.h"
Kenichi Handa's avatar
Kenichi Handa committed
66
#include "coding.h"
67
#include "frame.h"
Jim Blandy's avatar
Jim Blandy committed
68
#include "dispextern.h"
69
#include "fontset.h"
Jim Blandy's avatar
Jim Blandy committed
70 71 72
#include "termhooks.h"
#include "termopts.h"
#include "termchar.h"
73
#include "emacs-icon.h"
Jim Blandy's avatar
Jim Blandy committed
74 75
#include "disptab.h"
#include "buffer.h"
76
#include "window.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
77
#include "keyboard.h"
Karl Heuer's avatar
Karl Heuer committed
78
#include "intervals.h"
Andreas Schwab's avatar
Andreas Schwab committed
79
#include "process.h"
Gerd Moellmann's avatar
Gerd Moellmann committed
80
#include "atimer.h"
Stefan Monnier's avatar
Stefan Monnier committed
81
#include "keymap.h"
82
#include "font.h"
83
#include "xsettings.h"
84
#include "xgselect.h"
85
#include "sysselect.h"
86
#include "menu.h"
Jim Blandy's avatar
Jim Blandy committed
87

88 89 90 91
#ifdef USE_X_TOOLKIT
#include <X11/Shell.h>
#endif

92 93
#include <unistd.h>

Jan Djärv's avatar
Jan Djärv committed
94 95
#ifdef USE_GTK
#include "gtkutil.h"
Jan Djärv's avatar
Jan Djärv committed
96 97 98
#ifdef HAVE_GTK3
#include <X11/Xproto.h>
#endif
Jan Djärv's avatar
Jan Djärv committed
99 100
#endif

101
#if defined (USE_LUCID) || defined (USE_MOTIF)
102
#include "../lwlib/xlwmenu.h"
Jan Djärv's avatar
Jan Djärv committed
103
#endif
104

Jan Djärv's avatar
Jan Djärv committed
105
#ifdef USE_X_TOOLKIT
Juanma Barranquero's avatar
Juanma Barranquero committed
106
#if !defined (NO_EDITRES)
107
#define HACK_EDITRES
108
extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
109
#endif /* not NO_EDITRES */
110 111 112 113 114

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

#ifdef USE_TOOLKIT_SCROLL_BARS
#if defined USE_MOTIF
115
#include <Xm/Xm.h>		/* For LESSTIF_VERSION */
116
#include <Xm/ScrollBar.h>
117 118 119
#else /* !USE_MOTIF i.e. use Xaw */

#ifdef HAVE_XAW3D
120 121
#include <X11/Xaw3d/Simple.h>
#include <X11/Xaw3d/Scrollbar.h>
122
#include <X11/Xaw3d/ThreeD.h>
123 124 125 126 127 128 129 130
#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 */
131 132
#endif /* USE_TOOLKIT_SCROLL_BARS */

133 134
#endif /* USE_X_TOOLKIT */

135
#ifdef USE_X_TOOLKIT
Karl Heuer's avatar
Karl Heuer committed
136
#include "widget.h"
137 138 139 140 141
#ifndef XtNinitialState
#define XtNinitialState "initialState"
#endif
#endif

142 143
#include "bitmaps/gray.xbm"

144 145 146 147
#ifdef HAVE_XKB
#include <X11/XKBlib.h>
#endif

Dave Love's avatar
Dave Love committed
148
/* Default to using XIM if available.  */
149
#ifdef USE_XIM
150
bool use_xim = true;
151
#else
152
bool use_xim = false;  /* configure --without-xim */
153 154
#endif

155 156 157
/* Non-zero means that a HELP_EVENT has been generated since Emacs
   start.  */

158
static bool any_help_event_p;
159 160 161 162

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

163
struct x_display_info *x_display_list;
Jim Blandy's avatar
Jim Blandy committed
164

165
#ifdef USE_X_TOOLKIT
166

167 168
/* The application context for Xt use.  */
XtAppContext Xt_app_con;
169
static String Xt_default_resources[] = {0};
170

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

174
#endif /* USE_X_TOOLKIT */
Jim Blandy's avatar
Jim Blandy committed
175

Kenichi Handa's avatar
Kenichi Handa committed
176 177 178 179
/* 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).  */

180
static Time ignore_next_mouse_click_timeout;
Kenichi Handa's avatar
Kenichi Handa committed
181

182
/* Used locally within XTread_socket.  */
183

184
static int x_noop_count;
Jim Blandy's avatar
Jim Blandy committed
185

186 187 188 189 190
#ifdef USE_GTK
/* The name of the Emacs icon file.  */
static Lisp_Object xg_default_icon_file;
#endif

191 192 193
/* Some functions take this as char *, not const char *.  */
static char emacs_class[] = EMACS_CLASS;

Andreas Schwab's avatar
Andreas Schwab committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
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
  };
216

Paul Eggert's avatar
Paul Eggert committed
217
static void x_free_cr_resources (struct frame *);
218
static bool x_alloc_nearest_color_1 (Display *, Colormap, XColor *);
219 220
static void x_raise_frame (struct frame *);
static void x_lower_frame (struct frame *);
221 222 223 224
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
225 226
static void x_clip_to_row (struct window *, struct glyph_row *,
			   enum glyph_row_area, GC);
227
static struct scroll_bar *x_window_to_scroll_bar (Display *, Window, int);
228 229 230
static void x_scroll_bar_report_motion (struct frame **, Lisp_Object *,
                                        enum scroll_bar_part *,
                                        Lisp_Object *, Lisp_Object *,
231
                                        Time *);
232 233 234 235
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
236
static bool x_handle_net_wm_state (struct frame *, const XPropertyEvent *);
237 238
static void x_check_fullscreen (struct frame *);
static void x_check_expected_move (struct frame *, int, int);
Paul Eggert's avatar
Paul Eggert committed
239
static void x_sync_with_move (struct frame *, int, int, bool);
240
static int handle_one_xevent (struct x_display_info *,
241
			      const XEvent *, int *,
242
			      struct input_event *);
243
#if ! (defined USE_X_TOOLKIT || defined USE_MOTIF)
244 245 246
static int x_dispatch_event (XEvent *, Display *);
#endif
static void x_wm_set_window_state (struct frame *, int);
247
static void x_wm_set_icon_pixmap (struct frame *, ptrdiff_t);
248
static void x_initialize (void);
Jan Djärv's avatar
Jan Djärv committed
249

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

Joakim Verona's avatar
Joakim Verona committed
252
/* Flush display of frame F.  */
253 254

static void
255
x_flush (struct frame *f)
256
{
Joakim Verona's avatar
Joakim Verona committed
257
  eassert (f && FRAME_X_P (f));
258 259 260 261 262
  /* Don't call XFlush when it is not safe to redisplay; the X
     connection may be broken.  */
  if (!NILP (Vinhibit_redisplay))
    return;

263
  block_input ();
Joakim Verona's avatar
Joakim Verona committed
264
  XFlush (FRAME_X_DISPLAY (f));
265
  unblock_input ();
266 267
}

Jim Blandy's avatar
Jim Blandy committed
268

269 270 271 272 273 274 275
/* 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
276
#define XFlush(DISPLAY)	(void) 0
277

278

279 280 281 282
/***********************************************************************
			      Debugging
 ***********************************************************************/

Paul Eggert's avatar
Paul Eggert committed
283
#if false
284 285 286

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

Juanma Barranquero's avatar
Juanma Barranquero committed
288
struct record
289 290 291 292 293 294 295 296 297
{
  char *locus;
  int type;
};

struct record event_record[100];

int event_record_index;

Andreas Schwab's avatar
Andreas Schwab committed
298 299
void
record_event (char *locus, int type)
300
{
301
  if (event_record_index == ARRAYELTS (event_record))
302 303 304 305 306 307 308
    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
309
#endif
310

Jan D's avatar
Jan D committed
311 312 313 314 315 316
#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 *
317
x_gc_get_ext_data (struct frame *f, GC gc, int create_if_not_found_p)
Jan D's avatar
Jan D committed
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
{
  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
342
x_extension_initialize (struct x_display_info *dpyinfo)
Jan D's avatar
Jan D committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
{
  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 *
363
x_begin_cr_clip (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
364 365 366 367 368 369 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 395 396 397 398 399 400 401 402 403 404 405 406 407
{
  cairo_t *cr = FRAME_CR_CONTEXT (f);

  if (!cr)
    {

      if (! FRAME_CR_SURFACE (f))
        {
          cairo_surface_t *surface;
          surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
                                               FRAME_X_WINDOW (f),
                                               FRAME_DISPLAY_INFO (f)->visual,
                                               FRAME_PIXEL_WIDTH (f),
                                               FRAME_PIXEL_HEIGHT (f));
          cr = cairo_create (surface);
          cairo_surface_destroy (surface);
        }
      else
        cr = cairo_create (FRAME_CR_SURFACE (f));
      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
408
x_end_cr_clip (struct frame *f)
Jan D's avatar
Jan D committed
409 410 411 412 413
{
  cairo_restore (FRAME_CR_CONTEXT (f));
}

void
414
x_set_cr_source_with_gc_foreground (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
415 416 417 418 419 420 421 422 423 424 425 426
{
  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
427
x_set_cr_source_with_gc_background (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
  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;
static cairo_pattern_t **fringe_bmp = 0;

static void
445
x_cr_define_fringe_bitmap (int which, unsigned short *bits, int h, int wd)
Jan D's avatar
Jan D committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
{
  int i, stride;
  cairo_surface_t *surface;
  unsigned char *data;
  cairo_pattern_t *pattern;

  if (which >= max_fringe_bmp)
    {
      i = max_fringe_bmp;
      max_fringe_bmp = which + 20;
      fringe_bmp = (cairo_pattern_t **) xrealloc (fringe_bmp, max_fringe_bmp * sizeof (cairo_pattern_t *));
      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;
    }

473
  cairo_surface_mark_dirty (surface);
Jan D's avatar
Jan D committed
474 475 476 477 478 479 480 481 482
  pattern = cairo_pattern_create_for_surface (surface);
  cairo_surface_destroy (surface);

  unblock_input ();

  fringe_bmp[which] = pattern;
}

static void
483
x_cr_destroy_fringe_bitmap (int which)
Jan D's avatar
Jan D committed
484 485 486 487 488 489 490 491 492 493 494 495 496 497
{
  if (which >= max_fringe_bmp)
    return;

  if (fringe_bmp[which])
    {
      block_input ();
      cairo_pattern_destroy (fringe_bmp[which]);
      unblock_input ();
    }
  fringe_bmp[which] = 0;
}

static void
498 499 500
x_cr_draw_image (struct frame *f, GC gc, cairo_pattern_t *image,
		 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
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
{
  cairo_t *cr;
  cairo_matrix_t matrix;
  cairo_surface_t *surface;
  cairo_format_t format;

  cr = x_begin_cr_clip (f, gc);
  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);
    }
  cairo_clip (cr);
  cairo_matrix_init_translate (&matrix, src_x - dest_x, src_y - dest_y);
  cairo_pattern_set_matrix (image, &matrix);
  cairo_pattern_get_surface (image, &surface);
  format = cairo_image_surface_get_format (surface);
  if (format != CAIRO_FORMAT_A8 && format != CAIRO_FORMAT_A1)
    {
      cairo_set_source (cr, image);
      cairo_fill (cr);
    }
  else
    {
      x_set_cr_source_with_gc_foreground (f, gc);
      cairo_mask (cr, image);
    }
  x_end_cr_clip (f);
}

void
535
x_cr_draw_frame (cairo_t *cr, struct frame *f)
Jan D's avatar
Jan D committed
536 537 538 539 540 541
{
  int width, height;

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

542
  x_free_cr_resources (f);
Jan D's avatar
Jan D committed
543 544 545 546 547 548 549
  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
550 551
x_cr_accumulate_data (void *closure, const unsigned char *data,
		      unsigned int length)
Jan D's avatar
Jan D committed
552 553 554
{
  Lisp_Object *acc = (Lisp_Object *) closure;

555
  *acc = Fcons (make_unibyte_string ((char const *) data, length), *acc);
Jan D's avatar
Jan D committed
556 557 558 559 560

  return CAIRO_STATUS_SUCCESS;
}

static void
561
x_cr_destroy (Lisp_Object arg)
Jan D's avatar
Jan D committed
562 563 564 565 566 567 568 569 570
{
  cairo_t *cr = (cairo_t *) XSAVE_POINTER (arg, 0);

  block_input ();
  cairo_destroy (cr);
  unblock_input ();
}

Lisp_Object
571
x_cr_export_frames (Lisp_Object frames, cairo_surface_type_t surface_type)
Jan D's avatar
Jan D committed
572 573 574 575 576 577
{
  struct frame *f;
  cairo_surface_t *surface;
  cairo_t *cr;
  int width, height;
  void (*surface_set_size_func) (cairo_surface_t *, double, double) = NULL;
578
  Lisp_Object acc = Qnil;
Jan D's avatar
Jan D committed
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 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
  int count = SPECPDL_INDEX ();

  Fredisplay (Qt);

  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);
  record_unwind_protect (x_cr_destroy, make_save_ptr (cr));
  unblock_input ();

  while (1)
    {
      QUIT;

      block_input ();
630
      x_free_cr_resources (f);
Jan D's avatar
Jan D committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
      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;
      unblock_input ();

      if (NILP (frames))
	break;

      block_input ();
      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);
      unblock_input ();
    }

#ifdef CAIRO_HAS_PNG_FUNCTIONS
  if (surface_type == CAIRO_SURFACE_TYPE_IMAGE)
    {
      block_input ();
      cairo_surface_flush (surface);
      cairo_surface_write_to_png_stream (surface, x_cr_accumulate_data, &acc);
      unblock_input ();
    }
#endif
  unbind_to (count, Qnil);

662
  return CALLN (Fapply, intern ("concat"), Fnreverse (acc));
Jan D's avatar
Jan D committed
663 664 665 666 667
}

#endif	/* USE_CAIRO */

static void
668
x_free_cr_resources (struct frame *f)
Jan D's avatar
Jan D committed
669 670 671 672 673 674 675
{
#ifdef USE_CAIRO
  if (f == NULL)
    {
      Lisp_Object rest, frame;
      FOR_EACH_FRAME (rest, frame)
	if (FRAME_X_P (XFRAME (frame)))
676
	  x_free_cr_resources (XFRAME (frame));
Jan D's avatar
Jan D committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    }
  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
697
x_set_clip_rectangles (struct frame *f, GC gc, XRectangle *rectangles, int n)
Jan D's avatar
Jan D committed
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
{
  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
713
x_reset_clip_rectangles (struct frame *f, GC gc)
Jan D's avatar
Jan D committed
714 715 716 717 718 719 720 721 722 723 724 725 726
{
  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
}

static void
727
x_fill_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
Jan D's avatar
Jan D committed
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
{
#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
  XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
		  gc, x, y, width, height);
#endif
}

static void
744
x_draw_rectangle (struct frame *f, GC gc, int x, int y, int width, int height)
Jan D's avatar
Jan D committed
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
{
#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
  XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
		  gc, x, y, width, height);
#endif
}

static void
762
x_clear_window (struct frame *f)
Jan D's avatar
Jan D committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
{
#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
  XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
#endif
}

#ifdef USE_CAIRO
static void
778
x_fill_trapezoid_for_relief (struct frame *f, GC gc, int x, int y,
779
			     int width, int height, int top_p)
Jan D's avatar
Jan D committed
780 781 782 783 784 785 786 787 788 789 790 791
{
  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);
}
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 827 828 829 830 831 832 833 834
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);
}

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 867 868 869 870 871 872 873 874
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
875
#endif
876

877

878 879 880
/* Return the struct x_display_info corresponding to DPY.  */

struct x_display_info *
881
x_display_info_for_display (Display *dpy)
882 883 884 885 886 887
{
  struct x_display_info *dpyinfo;

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

889 890
  return 0;
}
891

892 893 894 895 896 897 898
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);

899
  while (wi != FRAME_DISPLAY_INFO (f)->root_window)
900 901 902 903 904 905
    {
      Window root;
      Window *children;
      unsigned int nchildren;

      win = wi;
906 907 908 909
      if (XQueryTree (dpy, win, &root, &wi, &children, &nchildren))
	XFree (children);
      else
	break;
910 911 912 913 914
    }

  return win;
}

915 916 917
#define OPAQUE  0xffffffff

void
918
x_set_frame_alpha (struct frame *f)
919
{
920
  struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
921 922
  Display *dpy = FRAME_X_DISPLAY (f);
  Window win = FRAME_OUTER_WINDOW (f);
923 924
  double alpha = 1.0;
  double alpha_min = 1.0;
925
  unsigned long opac;
926
  Window parent;
927

928 929 930 931 932 933 934 935 936 937
  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);
  else if (INTEGERP (Vframe_alpha_lower_limit))
    alpha_min = (XINT (Vframe_alpha_lower_limit)) / 100.0;

938 939 940
  if (alpha < 0.0)
    return;
  else if (alpha > 1.0)
941 942 943 944
    alpha = 1.0;
  else if (0.0 <= alpha && alpha < alpha_min && alpha_min <= 1.0)
    alpha = alpha_min;

945
  opac = alpha * OPAQUE;
946

947 948 949 950 951 952 953 954 955 956 957
  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.  */

  parent = x_find_topmost_parent (f);
  if (parent != None)
    XChangeProperty (dpy, parent, dpyinfo->Xatom_net_wm_window_opacity,
                     XA_CARDINAL, 32, PropModeReplace,
958
                     (unsigned char *) &opac, 1);
959

960 961 962 963
  /* return unless necessary */
  {
    unsigned char *data;
    Atom actual;
964
    int rc, format;
965 966
    unsigned long n, left;

967
    rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
968
			     0, 1, False, XA_CARDINAL,
969 970
			     &actual, &format, &n, &left,
			     &data);
971 972

    if (rc == Success && actual != None)
973
      {
974
        unsigned long value = *(unsigned long *)data;
975
	XFree (data);
976
	if (value == opac)
977 978 979 980 981
	  {
	    x_uncatch_errors ();
	    return;
	  }
      }
982 983
  }

984
  XChangeProperty (dpy, win, dpyinfo->Xatom_net_wm_window_opacity,
985
		   XA_CARDINAL, 32, PropModeReplace,
986
		   (unsigned char *) &opac, 1);
987
  x_uncatch_errors ();
988
}
989 990 991 992

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

994 995 996 997 998
/* 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
999

Andreas Schwab's avatar
Andreas Schwab committed
1000
static void
1001
x_update_begin (struct frame *f)
1002
{
Jan D's avatar
Jan D committed
1003
#ifdef USE_CAIRO
1004 1005 1006 1007
  if (! NILP (tip_frame) && XFRAME (tip_frame) == f
      && ! FRAME_VISIBLE_P (f))
    return;

Jan D's avatar
Jan D committed
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
  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));
          width = gdk_window_get_width (w);
          height = gdk_window_get_height (w);
        }
      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
1028

Jan D's avatar
Jan D committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
      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
1039

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

1042
static void
1043
x_update_window_begin (struct window *w)
1044 1045
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
1046
  Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
Juanma Barranquero's avatar
Juanma Barranquero committed
1047

Joakim Verona's avatar
Joakim Verona committed
1048
  w->output_cursor = w->cursor;
1049

1050
  block_input ();
1051

1052
  if (f == hlinfo->mouse_face_mouse_frame)
1053
    {
1054
      /* Don't do highlighting for mouse motion during the update.  */
Paul Eggert's avatar
Paul Eggert committed
1055
      hlinfo->mouse_face_defer = true;
1056

1057 1058
      /* If F needs to be redrawn, simply forget about any prior mouse
	 highlighting.  */
1059
      if (FRAME_GARBAGED_P (f))
1060
	hlinfo->mouse_face_window = Qnil;
1061
    }
Richard M. Stallman's avatar
Richard M. Stallman committed
1062

1063
  unblock_input ();
Jim Blandy's avatar
Jim Blandy committed
1064 1065
}

1066

1067
/* Draw a vertical window border from (x,y0) to (x,y1)  */
1068

Andreas Schwab's avatar
Andreas Schwab committed
1069
static void
1070
x_draw_vertical_window_border (struct window *w, int x, int y0, int y1)
1071
{
1072
  struct frame *f = XFRAME (WINDOW_FRAME (w));
1073 1074 1075 1076 1077 1078
  struct face *face;

  face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
  if (face)
    XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
		    face->foreground);
1079

Jan D's avatar
Jan D committed
1080 1081 1082
#ifdef USE_CAIRO
  x_fill_rectangle (f, f->output_data.x->normal_gc, x, y0, 1, y1 - y0);
#else
1083 1084
  XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
	     f->output_data.x->normal_gc, x, y0, x, y1);
Jan D's avatar
Jan D committed
1085
#endif
1086
}
Juanma Barranquero's avatar
Juanma Barranquero committed
1087

1088 1089 1090 1091 1092 1093
/* 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));
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
  struct face *face = FACE_FROM_ID (f, WINDOW_DIVIDER_FACE_ID);
  struct face *face_first = FACE_FROM_ID (f, WINDOW_DIVIDER_FIRST_PIXEL_FACE_ID);
  struct face *face_last = FACE_FROM_ID (f, WINDOW_DIVIDER_LAST_PIXEL_FACE_ID);
  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);

  if (y1 - y0 > x1 - x0 && x1 - x0 > 2)
    /* Vertical.  */
    {
      XSetForeground (display, f->output_data.x->normal_gc, color_first);
1110 1111
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0, 1, y1 - y0);
1112
      XSetForeground (display, f->output_data.x->normal_gc, color);
1113 1114
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0 + 1, y0, x1 - x0 - 2, y1 - y0);
1115
      XSetForeground (display, f->output_data.x->normal_gc, color_last);
1116 1117
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x1 - 1, y0, 1, y1 - y0);
1118 1119 1120 1121 1122
    }
  else if (x1 - x0 > y1 - y0 && y1 - y0 > 3)
    /* Horizontal.  */
    {
      XSetForeground (display, f->output_data.x->normal_gc, color_first);
1123 1124
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0, x1 - x0, 1);
1125
      XSetForeground (display, f->output_data.x->normal_gc, color);
1126 1127
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0 + 1, x1 - x0, y1 - y0 - 2);
1128
      XSetForeground (display, f->output_data.x->normal_gc, color_last);
1129 1130
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y1 - 1, x1 - x0, 1);
1131 1132 1133 1134
    }
  else
    {
      XSetForeground (display, f->output_data.x->normal_gc, color);
1135 1136
      x_fill_rectangle (f, f->output_data.x->normal_gc,
			x0, y0, x1 - x0, y1 - y0);
1137
    }
1138 1139
}

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

   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.  */
1152 1153

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

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

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

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

1177 1178 1179
  /* If a row with mouse-face was overwritten, arrange for
     XTframe_up_to_date to redisplay the mouse highlight.  */
  if (mouse_face_overwritten_p)
1180 1181 1182 1183 1184 1185 1186
    {
      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;
    }
1187
}
Jim Blandy's avatar
Jim Blandy committed
1188 1189


1190 1191 1192 1193
/* End update of frame F.  This function is installed as a hook in
   update_end.  */

static void
1194
x_update_end (struct frame *f)
1195 1196
{
  /* Mouse highlight may be displayed again.  */
Paul Eggert's avatar
Paul Eggert committed
1197
  MOUSE_HL_INFO (f)->mouse_face_defer = false;
1198

Jan D's avatar
Jan D committed
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 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
#ifdef USE_CAIRO
  if (FRAME_CR_SURFACE (f))
    {
      cairo_t *cr = 0;
      block_input();
#if defined (USE_GTK) && defined (HAVE_GTK3)
      if (FRAME_GTK_WIDGET (f))
        {
          GdkWindow *w = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
          cr = gdk_cairo_create (w);
        }
      else
#endif
        {
          cairo_surface_t *surface;
          int width = FRAME_PIXEL_WIDTH (f);
          int 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_WINDOW (f),
                                               FRAME_DISPLAY_INFO (f)->visual,
                                               width,
                                               height);
          cr = cairo_create (surface);
          cairo_surface_destroy (surface);
        }

      cairo_set_source_surface (cr, FRAME_CR_SURFACE (f), 0, 0);
      cairo_paint (cr);
      cairo_destroy (cr);
      unblock_input ();
    }
#endif /* USE_CAIRO */

1236
#ifndef XFlush
1237
  block_input ();
1238
  XFlush (FRAME_X_DISPLAY (f));
1239
  unblock_input ();
1240
#endif