xsettings.c 30.7 KB
Newer Older
1 2
/* Functions for handling font and other changes dynamically.

Paul Eggert's avatar
Paul Eggert committed
3
Copyright (C) 2009-2019 Free Software Foundation, Inc.
4 5 6 7 8

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
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.
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/>.  */
19

Dan Nicolaescu's avatar
Dan Nicolaescu committed
20
#include <config.h>
21 22

#include <float.h>
23
#include <limits.h>
24
#include <fcntl.h>
25 26 27

#include <byteswap.h>

28 29 30 31
#include "lisp.h"
#include "xterm.h"
#include "xsettings.h"
#include "frame.h"
32
#include "keyboard.h"
33 34
#include "blockinput.h"
#include "termhooks.h"
Daniel Colascione's avatar
Daniel Colascione committed
35
#include "pdumper.h"
36 37 38

#include <X11/Xproto.h>

39
#ifdef HAVE_GSETTINGS
Jan Djärv's avatar
Jan Djärv committed
40 41
#include <glib-object.h>
#include <gio/gio.h>
42 43
#endif

44 45 46
#ifdef HAVE_GCONF
#include <gconf/gconf-client.h>
#endif
47

48 49 50 51
#if defined USE_CAIRO || defined HAVE_XFT
#ifdef USE_CAIRO
#include <fontconfig/fontconfig.h>
#else  /* HAVE_XFT */
52 53
#include <X11/Xft/Xft.h>
#endif
54
#endif
55 56

static char *current_mono_font;
57
static char *current_font;
58
static struct x_display_info *first_dpyinfo;
59
static Lisp_Object current_tool_bar_style;
60

61
/* Store a config changed event in to the event queue.  */
62

63
static void
64
store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
65 66 67 68 69 70 71 72 73
{
  struct input_event event;
  EVENT_INIT (event);
  event.kind = CONFIG_CHANGED_EVENT;
  event.frame_or_window = display_name;
  event.arg = arg;
  kbd_buffer_store_event (&event);
}

74 75
/* Return true if DPYINFO is still valid.  */
static bool
76 77
dpyinfo_valid (struct x_display_info *dpyinfo)
{
78
  bool found = false;
79 80 81 82 83 84 85 86 87 88 89
  if (dpyinfo != NULL)
    {
      struct x_display_info *d;
      for (d = x_display_list; !found && d; d = d->next)
        found = d == dpyinfo && d->display == dpyinfo->display;
    }
  return found;
}

/* Store a monospace font change event if the monospaced font changed.  */

90
#if (defined USE_CAIRO || defined HAVE_XFT) && (defined HAVE_GSETTINGS || defined HAVE_GCONF)
91
static void
92
store_monospaced_changed (const char *newfont)
93
{
94 95 96
  if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
    return; /* No change. */

97
  dupstring (&current_mono_font, newfont);
98

99
  if (dpyinfo_valid (first_dpyinfo) && use_system_font)
100
    {
101 102
      store_config_changed_event (Qmonospace_font_name,
                                  XCAR (first_dpyinfo->name_list_element));
103 104
    }
}
105
#endif
106

107
/* Store a font name change event if the font name changed.  */
108

109
#if defined USE_CAIRO || defined HAVE_XFT
110 111 112 113 114 115
static void
store_font_name_changed (const char *newfont)
{
  if (current_font != NULL && strcmp (newfont, current_font) == 0)
    return; /* No change. */

116
  dupstring (&current_font, newfont);
117 118 119 120 121 122 123

  if (dpyinfo_valid (first_dpyinfo))
    {
      store_config_changed_event (Qfont_name,
                                  XCAR (first_dpyinfo->name_list_element));
    }
}
124
#endif /* USE_CAIRO || HAVE_XFT */
125

Paul Eggert's avatar
Paul Eggert committed
126
/* Map TOOL_BAR_STYLE from a string to its corresponding Lisp value.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
   Return Qnil if TOOL_BAR_STYLE is not known.  */

static Lisp_Object
map_tool_bar_style (const char *tool_bar_style)
{
  Lisp_Object style = Qnil;
  if (tool_bar_style)
    {
      if (strcmp (tool_bar_style, "both") == 0)
        style = Qboth;
      else if (strcmp (tool_bar_style, "both-horiz") == 0)
        style = Qboth_horiz;
      else if (strcmp (tool_bar_style, "icons") == 0)
        style = Qimage;
      else if (strcmp (tool_bar_style, "text") == 0)
        style = Qtext;
    }

  return style;
}

/* Store a tool bar style change event if the tool bar style changed.  */

static void
store_tool_bar_style_changed (const char *newstyle,
                              struct x_display_info *dpyinfo)
{
  Lisp_Object style = map_tool_bar_style (newstyle);
  if (EQ (current_tool_bar_style, style))
    return; /* No change. */

  current_tool_bar_style = style;
  if (dpyinfo_valid (dpyinfo))
    store_config_changed_event (Qtool_bar_style,
                                XCAR (dpyinfo->name_list_element));
}
163

164
#if defined USE_CAIRO || defined HAVE_XFT
165
#define XSETTINGS_FONT_NAME       "Gtk/FontName"
166
#endif
167
#define XSETTINGS_TOOL_BAR_STYLE  "Gtk/ToolbarStyle"
168

169 170 171 172 173 174 175 176
enum {
  SEEN_AA         = 0x01,
  SEEN_HINTING    = 0x02,
  SEEN_RGBA       = 0x04,
  SEEN_LCDFILTER  = 0x08,
  SEEN_HINTSTYLE  = 0x10,
  SEEN_DPI        = 0x20,
  SEEN_FONT       = 0x40,
Paul Eggert's avatar
Paul Eggert committed
177
  SEEN_TB_STYLE   = 0x80
178
};
179
struct xsettings
180
{
181
#if defined USE_CAIRO || defined HAVE_XFT
182 183 184 185 186
  FcBool aa, hinting;
  int rgba, lcdfilter, hintstyle;
  double dpi;

  char *font;
187 188
#endif

189 190 191 192 193
  char *tb_style;

  unsigned seen;
};

194
#ifdef HAVE_GSETTINGS
195 196 197
#define GSETTINGS_SCHEMA         "org.gnome.desktop.interface"
#define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"

198
#if defined USE_CAIRO || defined HAVE_XFT
199 200 201 202 203 204 205 206 207 208
#define GSETTINGS_MONO_FONT  "monospace-font-name"
#define GSETTINGS_FONT_NAME  "font-name"
#endif


/* The single GSettings instance, or NULL if not connected to GSettings.  */

static GSettings *gsettings_client;

/* Callback called when something changed in GSettings.  */
209

210
static void
211 212 213
something_changed_gsettingsCB (GSettings *settings,
                               gchar *key,
                               gpointer user_data)
214 215
{
  GVariant *val;
216 217

  if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
218
    {
219 220
      val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
      if (val)
221
        {
222 223 224 225 226 227 228 229 230
          g_variant_ref_sink (val);
          if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
            {
              const gchar *newstyle = g_variant_get_string (val, NULL);
              store_tool_bar_style_changed (newstyle, first_dpyinfo);
            }
          g_variant_unref (val);
        }
    }
231
#if defined USE_CAIRO || defined HAVE_XFT
232 233 234 235 236 237 238 239 240 241 242 243
  else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
    {
      val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
      if (val)
        {
          g_variant_ref_sink (val);
          if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
            {
              const gchar *newfont = g_variant_get_string (val, NULL);
              store_monospaced_changed (newfont);
            }
          g_variant_unref (val);
244 245
        }
    }
246 247 248 249 250 251 252 253 254 255 256 257 258 259
  else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
    {
      val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
      if (val)
        {
          g_variant_ref_sink (val);
          if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
            {
              const gchar *newfont = g_variant_get_string (val, NULL);
              store_font_name_changed (newfont);
            }
          g_variant_unref (val);
        }
    }
260
#endif /* USE_CAIRO || HAVE_XFT */
261 262
}

263
#endif /* HAVE_GSETTINGS */
264

265 266
#ifdef HAVE_GCONF
#define GCONF_TOOL_BAR_STYLE "/desktop/gnome/interface/toolbar_style"
267
#if defined USE_CAIRO || defined HAVE_XFT
268 269
#define GCONF_MONO_FONT  "/desktop/gnome/interface/monospace_font_name"
#define GCONF_FONT_NAME  "/desktop/gnome/interface/font_name"
270
#endif
271

272 273 274 275 276
/* The single GConf instance, or NULL if not connected to GConf.  */

static GConfClient *gconf_client;

/* Callback called when something changed in GConf that we care about.  */
277 278

static void
279 280 281 282
something_changed_gconfCB (GConfClient *client,
                           guint cnxn_id,
                           GConfEntry *entry,
                           gpointer user_data)
283 284
{
  GConfValue *v = gconf_entry_get_value (entry);
285
  const char *key = gconf_entry_get_key (entry);
286

287 288 289 290 291 292
  if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
  if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
    {
      const char *value = gconf_value_get_string (v);
      store_tool_bar_style_changed (value, first_dpyinfo);
    }
293
#if defined USE_CAIRO || defined HAVE_XFT
294
  else if (strcmp (key, GCONF_MONO_FONT) == 0)
295 296
    {
      const char *value = gconf_value_get_string (v);
297
      store_monospaced_changed (value);
298
    }
299 300 301 302 303
  else if (strcmp (key, GCONF_FONT_NAME) == 0)
    {
      const char *value = gconf_value_get_string (v);
      store_font_name_changed (value);
    }
304
#endif /* USE_CAIRO || HAVE_XFT */
305
}
306

307 308
#endif /* HAVE_GCONF */

309
#if defined USE_CAIRO || defined HAVE_XFT
310

311 312 313 314 315 316 317 318 319 320 321
/* Older fontconfig versions don't have FC_LCD_*.  */
#ifndef FC_LCD_NONE
#define FC_LCD_NONE 0
#endif
#ifndef FC_LCD_DEFAULT
#define FC_LCD_DEFAULT 1
#endif
#ifndef FC_LCD_FILTER
#define FC_LCD_FILTER "lcdfilter"
#endif

322
#endif /* USE_CAIRO || HAVE_XFT */
323

324 325 326
/* Find the window that contains the XSETTINGS property values.  */

static void
327
get_prop_window (struct x_display_info *dpyinfo)
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
{
  Display *dpy = dpyinfo->display;

  XGrabServer (dpy);
  dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
                                                  dpyinfo->Xatom_xsettings_sel);
  if (dpyinfo->xsettings_window != None)
    /* Select events so we can detect if window is deleted or if settings
       are changed.  */
    XSelectInput (dpy, dpyinfo->xsettings_window,
                  PropertyChangeMask|StructureNotifyMask);

  XUngrabServer (dpy);
}

#define PAD(nr)    (((nr) + 3) & ~3)

/* Parse xsettings and extract those that deal with Xft.
   See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
   and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.

   Layout of prop.  First is a header:

   bytes   type     what
   ------------------------------------
   1      CARD8    byte-order
   3               unused
   4      CARD32   SERIAL
   4      CARD32   N_SETTINGS

   Then N_SETTINGS records, with header:

   bytes   type          what
   ------------------------------------
   1      SETTING_TYPE  type (0 = integer, 1 = string, 2 RGB color).
   1                    unused
   2      CARD16        n == name-length
   n      STRING8       name
   p                    unused, p=pad_to_even_4(n)
   4      CARD32        last-change-serial

   and then the value, For string:
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
   bytes   type          what
   ------------------------------------
   4      CARD32        n = value-length
   n      STRING8       value
   p                    unused, p=pad_to_even_4(n)

   For integer:

   bytes   type          what
   ------------------------------------
   4      INT32         value

   For RGB color:

   bytes   type          what
   ------------------------------------
   2      CARD16        red
   2      CARD16        blue
   2      CARD16        green
   2      CARD16        alpha

392
   Returns non-zero if some Xft settings was seen, zero otherwise.
393 394 395
*/

static int
396
parse_settings (unsigned char *prop,
397
                unsigned long bytes,
398
                struct xsettings *settings)
399
{
400 401
  int int1 = 1;
  int my_bo = *(char *) &int1 == 1 ? LSBFirst : MSBFirst;
402 403 404 405 406 407 408 409
  int that_bo = prop[0];
  CARD32 n_settings;
  int bytes_parsed = 0;
  int settings_seen = 0;
  int i = 0;

  /* First 4 bytes is a serial number, skip that.  */

410
  if (bytes < 12) return settings_seen;
411
  memcpy (&n_settings, prop+8, 4);
412
  if (my_bo != that_bo) n_settings = bswap_32 (n_settings);
413 414 415 416
  bytes_parsed = 12;

  memset (settings, 0, sizeof (*settings));

417
  while (bytes_parsed+4 < bytes && settings_seen < 7
418 419 420 421 422 423 424
         && i < n_settings)
    {
      int type = prop[bytes_parsed++];
      CARD16 nlen;
      CARD32 vlen, ival = 0;
      char name[128]; /* The names we are looking for are not this long.  */
      char sval[128]; /* The values we are looking for are not this long.  */
425
      bool want_this;
426 427 428 429 430 431 432 433
      int to_cpy;

      sval[0] = '\0';
      ++i;
      ++bytes_parsed; /* Padding */

      memcpy (&nlen, prop+bytes_parsed, 2);
      bytes_parsed += 2;
434
      if (my_bo != that_bo) nlen = bswap_16 (nlen);
435 436
      if (bytes_parsed + nlen > bytes) return settings_seen;
      to_cpy = min (nlen, sizeof name - 1);
437 438 439 440 441 442 443
      memcpy (name, prop+bytes_parsed, to_cpy);
      name[to_cpy] = '\0';

      bytes_parsed += nlen;
      bytes_parsed = PAD (bytes_parsed);

      bytes_parsed += 4; /* Skip serial for this value */
444
      if (bytes_parsed > bytes) return settings_seen;
445

446
      want_this = strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
447
#if defined USE_CAIRO || defined HAVE_XFT
448 449 450
      if ((nlen > 6 && memcmp (name, "Xft/", 4) == 0)
	  || strcmp (XSETTINGS_FONT_NAME, name) == 0)
	want_this = true;
451
#endif
452

453
      switch (type)
454 455
        {
        case 0: /* Integer */
456
          if (bytes_parsed + 4 > bytes) return settings_seen;
457
          if (want_this)
458 459
            {
              memcpy (&ival, prop+bytes_parsed, 4);
460
              if (my_bo != that_bo) ival = bswap_32 (ival);
461 462 463 464 465
            }
          bytes_parsed += 4;
          break;

        case 1: /* String */
466
          if (bytes_parsed + 4 > bytes) return settings_seen;
467 468
          memcpy (&vlen, prop+bytes_parsed, 4);
          bytes_parsed += 4;
469
          if (my_bo != that_bo) vlen = bswap_32 (vlen);
470
          if (want_this)
471
            {
472
              to_cpy = min (vlen, sizeof sval - 1);
473 474 475 476 477 478 479 480 481
              memcpy (sval, prop+bytes_parsed, to_cpy);
              sval[to_cpy] = '\0';
            }
          bytes_parsed += vlen;
          bytes_parsed = PAD (bytes_parsed);
          break;

        case 2: /* RGB value */
          /* No need to parse this */
482
          if (bytes_parsed + 8 > bytes) return settings_seen;
483
          bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each.  */
484 485 486
          break;

        default: /* Parse Error */
487
          return settings_seen;
488 489
        }

490
      if (want_this)
491
        {
492
          if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
493
            {
494
              dupstring (&settings->tb_style, sval);
495 496
              settings->seen |= SEEN_TB_STYLE;
            }
497
#if defined USE_CAIRO || defined HAVE_XFT
498 499
          else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
            {
500
              dupstring (&settings->font, sval);
501 502
              settings->seen |= SEEN_FONT;
            }
503
          else if (strcmp (name, "Xft/Antialias") == 0)
504 505 506 507
            {
              settings->seen |= SEEN_AA;
              settings->aa = ival != 0;
            }
508
          else if (strcmp (name, "Xft/Hinting") == 0)
509 510 511 512
            {
              settings->seen |= SEEN_HINTING;
              settings->hinting = ival != 0;
            }
513
# ifdef FC_HINT_STYLE
514 515
          else if (strcmp (name, "Xft/HintStyle") == 0)
            {
516
              settings->seen |= SEEN_HINTSTYLE;
517 518 519 520 521 522 523 524
              if (strcmp (sval, "hintnone") == 0)
                settings->hintstyle = FC_HINT_NONE;
              else if (strcmp (sval, "hintslight") == 0)
                settings->hintstyle = FC_HINT_SLIGHT;
              else if (strcmp (sval, "hintmedium") == 0)
                settings->hintstyle = FC_HINT_MEDIUM;
              else if (strcmp (sval, "hintfull") == 0)
                settings->hintstyle = FC_HINT_FULL;
525 526
              else
                settings->seen &= ~SEEN_HINTSTYLE;
527
            }
528
# endif
529 530
          else if (strcmp (name, "Xft/RGBA") == 0)
            {
531
              settings->seen |= SEEN_RGBA;
532 533 534 535 536 537 538 539 540 541
              if (strcmp (sval, "none") == 0)
                settings->rgba = FC_RGBA_NONE;
              else if (strcmp (sval, "rgb") == 0)
                settings->rgba = FC_RGBA_RGB;
              else if (strcmp (sval, "bgr") == 0)
                settings->rgba = FC_RGBA_BGR;
              else if (strcmp (sval, "vrgb") == 0)
                settings->rgba = FC_RGBA_VRGB;
              else if (strcmp (sval, "vbgr") == 0)
                settings->rgba = FC_RGBA_VBGR;
542 543
              else
                settings->seen &= ~SEEN_RGBA;
544
            }
545
          else if (strcmp (name, "Xft/DPI") == 0 && ival != (CARD32) -1)
546 547
            {
              settings->seen |= SEEN_DPI;
548
              settings->dpi = ival / 1024.0;
549
            }
550 551
          else if (strcmp (name, "Xft/lcdfilter") == 0)
            {
552
              settings->seen |= SEEN_LCDFILTER;
553 554 555 556
              if (strcmp (sval, "none") == 0)
                settings->lcdfilter = FC_LCD_NONE;
              else if (strcmp (sval, "lcddefault") == 0)
                settings->lcdfilter = FC_LCD_DEFAULT;
557 558
              else
                settings->seen &= ~SEEN_LCDFILTER;
559
            }
560
#endif /* USE_CAIRO || HAVE_XFT */
561 562 563
	  else
	    want_this = false;
	  settings_seen += want_this;
564 565 566
        }
    }

567
  return settings_seen;
568 569
}

570 571
/* Read settings from the XSettings property window on display for DPYINFO.
   Store settings read in SETTINGS.
572
   Return true iff successful.  */
573

574
static bool
575
read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
576 577 578 579 580 581 582
{
  Atom act_type;
  int act_form;
  unsigned long nitems, bytes_after;
  unsigned char *prop = NULL;
  Display *dpy = dpyinfo->display;
  int rc;
583
  bool got_settings = false;
584 585 586 587 588 589 590 591 592 593 594

  x_catch_errors (dpy);
  rc = XGetWindowProperty (dpy,
                           dpyinfo->xsettings_window,
                           dpyinfo->Xatom_xsettings_prop,
                           0, LONG_MAX, False, AnyPropertyType,
                           &act_type, &act_form, &nitems, &bytes_after,
                           &prop);

  if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
      && act_type == dpyinfo->Xatom_xsettings_prop)
595
    got_settings = parse_settings (prop, nitems, settings) != 0;
596 597 598 599 600

  XFree (prop);

  x_uncatch_errors ();

601
  return got_settings;
602 603
}

604
/* Apply Xft settings in SETTINGS to the Xft library.
605
   Store a Lisp event that Xft settings changed.  */
606

607
static void
608 609
apply_xft_settings (struct x_display_info *dpyinfo,
                    struct xsettings *settings)
610
{
611
#if defined USE_CAIRO || defined HAVE_XFT
612
  FcPattern *pat;
613
  struct xsettings oldsettings;
614
  bool changed = false;
615 616 617

  memset (&oldsettings, 0, sizeof (oldsettings));
  pat = FcPatternCreate ();
618
#ifdef HAVE_XFT
619 620 621
  XftDefaultSubstitute (dpyinfo->display,
                        XScreenNumberOfScreen (dpyinfo->screen),
                        pat);
622
#endif
623 624
  FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
  FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
625
#ifdef FC_HINT_STYLE
626
  FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
627
#endif
628 629 630 631
  FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
  FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
  FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);

632
  if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
633 634
    {
      FcPatternDel (pat, FC_ANTIALIAS);
635
      FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
636
      changed = true;
637
      oldsettings.aa = settings->aa;
638
    }
639

640 641
  if ((settings->seen & SEEN_HINTING) != 0
      && oldsettings.hinting != settings->hinting)
642 643
    {
      FcPatternDel (pat, FC_HINTING);
644
      FcPatternAddBool (pat, FC_HINTING, settings->hinting);
645
      changed = true;
646
      oldsettings.hinting = settings->hinting;
647
    }
648
  if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
649 650
    {
      FcPatternDel (pat, FC_RGBA);
651 652
      FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
      oldsettings.rgba = settings->rgba;
653
      changed = true;
654
    }
655

Jan Djärv's avatar
Jan Djärv committed
656
  /* Older fontconfig versions don't have FC_LCD_FILTER. */
657 658
  if ((settings->seen & SEEN_LCDFILTER) != 0
      && oldsettings.lcdfilter != settings->lcdfilter)
659 660
    {
      FcPatternDel (pat, FC_LCD_FILTER);
661
      FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
662
      changed = true;
663
      oldsettings.lcdfilter = settings->lcdfilter;
664
    }
665

666
#ifdef FC_HINT_STYLE
667 668
  if ((settings->seen & SEEN_HINTSTYLE) != 0
      && oldsettings.hintstyle != settings->hintstyle)
669 670
    {
      FcPatternDel (pat, FC_HINT_STYLE);
671
      FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
672
      changed = true;
673
      oldsettings.hintstyle = settings->hintstyle;
674
    }
675
#endif
676

677 678 679 680 681 682 683 684
  if ((settings->seen & SEEN_DPI) != 0
      && settings->dpi > 0
      /* The following conjunct avoids setting `changed' to true when
	 old and new dpi settings do not differ "substantially".
	 Otherwise, the dynamic-setting Elisp code may process all sorts
	 of unrelated settings that override users' font customizations,
	 among others.  Compare:

685 686
	 https://lists.gnu.org/r/emacs-devel/2016-05/msg00557.html
	 https://lists.gnu.org/r/bug-gnu-emacs/2016-12/msg00820.html
687 688 689 690 691 692 693

	 As soon as the dynamic-settings code has been tested and
	 verified, this Emacs 25.2 workaround should be removed.  */
      && ((oldsettings.dpi >= settings->dpi
	   && (oldsettings.dpi - settings->dpi) > 2)
	  || ((settings->dpi > oldsettings.dpi)
	      && (settings->dpi - oldsettings.dpi) > 2)))
694 695
    {
      FcPatternDel (pat, FC_DPI);
696
      FcPatternAddDouble (pat, FC_DPI, settings->dpi);
697
      changed = true;
698
      oldsettings.dpi = settings->dpi;
699

700 701
      /* Changing the DPI on this display affects all frames on it.
	 Check FRAME_RES_X and FRAME_RES_Y in frame.h to see how.  */
702
      dpyinfo->resy = dpyinfo->resx = settings->dpi;
703 704 705 706
    }

  if (changed)
    {
707 708
      static char const format[] =
	"Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
Paul Eggert's avatar
Paul Eggert committed
709
	"Hintstyle: %d, DPI: %f";
710 711 712 713 714 715 716 717
      enum
      {
	d_formats = 5,
	d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
	lf_formats = 1,
	max_f_integer_digits = DBL_MAX_10_EXP + 1,
	f_precision = 6,
	lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
Paul Eggert's avatar
Paul Eggert committed
718
		     - sizeof "%f")
719 720 721
      };
      char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];

722
#ifdef HAVE_XFT
723
      XftDefaultSet (dpyinfo->display, pat);
724
#endif
725 726
      store_config_changed_event (Qfont_render,
				  XCAR (dpyinfo->name_list_element));
727
      Vxft_settings
728 729 730 731
	= make_formatted_string (buf, format,
				 oldsettings.aa, oldsettings.hinting,
				 oldsettings.rgba, oldsettings.lcdfilter,
				 oldsettings.hintstyle, oldsettings.dpi);
732

733 734 735
    }
  else
    FcPatternDestroy (pat);
736
#endif /* USE_CAIRO || HAVE_XFT */
737 738
}

739
/* Read XSettings from the display for DPYINFO.
740
   If SEND_EVENT_P store a Lisp event settings that changed.  */
741

742
static void
743
read_and_apply_settings (struct x_display_info *dpyinfo, bool send_event_p)
744 745 746 747 748 749
{
  struct xsettings settings;

  if (!read_settings (dpyinfo, &settings))
    return;

750
  apply_xft_settings (dpyinfo, &settings);
751 752
  if (settings.seen & SEEN_TB_STYLE)
    {
753 754 755 756
      if (send_event_p)
        store_tool_bar_style_changed (settings.tb_style, dpyinfo);
      else
        current_tool_bar_style = map_tool_bar_style (settings.tb_style);
757
      xfree (settings.tb_style);
758
    }
759
#if defined USE_CAIRO || defined HAVE_XFT
760 761
  if (settings.seen & SEEN_FONT)
    {
762 763 764
      if (send_event_p)
        store_font_name_changed (settings.font);
      else
765
	dupstring (&current_font, settings.font);
766
      xfree (settings.font);
767
    }
768
#endif
769
}
770

771 772
/* Check if EVENT for the display in DPYINFO is XSettings related.  */

773
void
774
xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
775
{
776
  bool check_window_p = false, apply_settings_p = false;
777 778 779 780 781

  switch (event->type)
    {
    case DestroyNotify:
      if (dpyinfo->xsettings_window == event->xany.window)
782
        check_window_p = true;
783 784 785 786 787 788
      break;

    case ClientMessage:
      if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
          && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
          && event->xclient.window == dpyinfo->root_window)
789
        check_window_p = true;
790 791 792 793 794 795
      break;

    case PropertyNotify:
      if (event->xproperty.window == dpyinfo->xsettings_window
          && event->xproperty.state == PropertyNewValue
          && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
796
        apply_settings_p = true;
797 798 799
      break;
    }

800

801 802 803 804 805
  if (check_window_p)
    {
      dpyinfo->xsettings_window = None;
      get_prop_window (dpyinfo);
      if (dpyinfo->xsettings_window != None)
806
        apply_settings_p = true;
807
    }
808

809
  if (apply_settings_p)
810
    read_and_apply_settings (dpyinfo, true);
811 812
}

813
/* Initialize GSettings and read startup values.  */
814

815 816 817 818 819
static void
init_gsettings (void)
{
#ifdef HAVE_GSETTINGS
  GVariant *val;
820
  bool schema_found = false;
821

822
#if ! GLIB_CHECK_VERSION (2, 36, 0)
823 824 825
  g_type_init ();
#endif

826 827 828 829 830
#if GLIB_CHECK_VERSION (2, 32, 0)
  {
    GSettingsSchema *sc = g_settings_schema_source_lookup
      (g_settings_schema_source_get_default (),
       GSETTINGS_SCHEMA,
831
       true);
832 833 834 835 836 837 838 839 840 841 842
    schema_found = sc != NULL;
    if (sc) g_settings_schema_unref (sc);
  }
#else
  {
    const gchar *const *schemas = g_settings_list_schemas ();
    if (schemas == NULL) return;
    while (! schema_found && *schemas != NULL)
      schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
  }
#endif
843 844
  if (!schema_found) return;

845 846 847
  gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
  if (!gsettings_client) return;
  g_object_ref_sink (G_OBJECT (gsettings_client));
848 849 850 851 852 853 854 855 856 857 858 859
  g_signal_connect (G_OBJECT (gsettings_client), "changed",
                    G_CALLBACK (something_changed_gsettingsCB), NULL);

  val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
  if (val)
    {
      g_variant_ref_sink (val);
      if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
        current_tool_bar_style
          = map_tool_bar_style (g_variant_get_string (val, NULL));
      g_variant_unref (val);
    }
860

861
#if defined USE_CAIRO || defined HAVE_XFT
862
  val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
863
  if (val)
864 865 866
    {
      g_variant_ref_sink (val);
      if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
867
	dupstring (&current_mono_font, g_variant_get_string (val, NULL));
868 869 870
      g_variant_unref (val);
    }

871 872 873 874 875
  val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
  if (val)
    {
      g_variant_ref_sink (val);
      if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
876
        dupstring (&current_font, g_variant_get_string (val, NULL));
877 878
      g_variant_unref (val);
    }
879
#endif /* USE_CAIRO || HAVE_XFT */
880

881 882 883
#endif /* HAVE_GSETTINGS */
}

884
/* Init GConf and read startup values.  */
885

886
static void
887
init_gconf (void)
888
{
889
#if defined (HAVE_GCONF)
890 891
  char *s;

892
#if ! GLIB_CHECK_VERSION (2, 36, 0)
893
  g_type_init ();
894
#endif
895

896
  gconf_client = gconf_client_get_default ();
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
  gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
  gconf_client_add_dir (gconf_client,
                        GCONF_TOOL_BAR_STYLE,
                        GCONF_CLIENT_PRELOAD_ONELEVEL,
                        NULL);
  gconf_client_notify_add (gconf_client,
                           GCONF_TOOL_BAR_STYLE,
                           something_changed_gconfCB,
                           NULL, NULL, NULL);

  s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
  if (s)
    {
      current_tool_bar_style = map_tool_bar_style (s);
      g_free (s);
    }

914
#if defined USE_CAIRO || defined HAVE_XFT
915
  s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
916 917
  if (s)
    {
918
      dupstring (&current_mono_font, s);
919 920
      g_free (s);
    }
921
  s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
922 923
  if (s)
    {
924
      dupstring (&current_font, s);
925 926
      g_free (s);
    }
927
  gconf_client_add_dir (gconf_client,
928
                        GCONF_MONO_FONT,
929 930 931
                        GCONF_CLIENT_PRELOAD_ONELEVEL,
                        NULL);
  gconf_client_notify_add (gconf_client,
932 933
                           GCONF_MONO_FONT,
                           something_changed_gconfCB,
934
                           NULL, NULL, NULL);
935 936 937 938 939 940 941 942
  gconf_client_add_dir (gconf_client,
                        GCONF_FONT_NAME,
                        GCONF_CLIENT_PRELOAD_ONELEVEL,
                        NULL);
  gconf_client_notify_add (gconf_client,
                           GCONF_FONT_NAME,
                           something_changed_gconfCB,
                           NULL, NULL, NULL);
943
#endif /* USE_CAIRO || HAVE_XFT */
944
#endif /* HAVE_GCONF */
945 946
}

947 948
/* Init Xsettings and read startup values.  */

949
static void
950
init_xsettings (struct x_display_info *dpyinfo)
951 952 953
{
  Display *dpy = dpyinfo->display;

954
  block_input ();
955 956 957 958 959 960 961

  /* Select events so we can detect client messages sent when selection
     owner changes.  */
  XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);

  get_prop_window (dpyinfo);
  if (dpyinfo->xsettings_window != None)
962
    read_and_apply_settings (dpyinfo, false);
963

964
  unblock_input ();
965 966 967
}

void
968
xsettings_initialize (struct x_display_info *dpyinfo)
969 970 971
{
  if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
  init_gconf ();
972
  init_xsettings (dpyinfo);
973
  init_gsettings ();
974 975
}

976 977 978
/* Return the system monospaced font.
   May be NULL if not known.  */

979
const char *
980
xsettings_get_system_font (void)
981 982 983
{
  return current_mono_font;
}
984

985
#ifdef USE_LUCID
986 987 988
/* Return the system font.
   May be NULL if not known.  */

989
const char *
990
xsettings_get_system_normal_font (void)
991 992 993
{
  return current_font;
}
994
#endif
995 996 997 998

DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
       Sfont_get_system_normal_font,
       0, 0, 0,
999
       doc: /* Get the system default application font. */)
1000
  (void)
1001
{
1002
  return current_font ? build_string (current_font) : Qnil;
1003 1004
}

1005 1006
DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
       0, 0, 0,
1007
       doc: /* Get the system default fixed width font. */)
1008
  (void)
1009
{
1010
  return current_mono_font ? build_string (current_mono_font) : Qnil;
1011 1012
}

Paul Eggert's avatar
Paul Eggert committed
1013
DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
1014
       Stool_bar_get_system_style, 0, 0, 0,
1015
       doc: /* Get the system tool bar style.
1016
If no system tool bar style is known, return `tool-bar-style' if set to a
1017
known style.  Otherwise return image.  */)
1018
  (void)
1019 1020 1021 1022
{
  if (EQ (Vtool_bar_style, Qimage)
      || EQ (Vtool_bar_style, Qtext)
      || EQ (Vtool_bar_style, Qboth)
1023 1024
      || EQ (Vtool_bar_style, Qboth_horiz)
      || EQ (Vtool_bar_style, Qtext_image_horiz))
1025 1026 1027 1028 1029 1030
    return Vtool_bar_style;
  if (!NILP (current_tool_bar_style))
    return current_tool_bar_style;
  return Qimage;
}

1031
void
1032
syms_of_xsettings (void)
1033 1034
{
  current_mono_font = NULL;
Daniel Colascione's avatar
Daniel Colascione committed
1035
  PDUMPER_IGNORE (current_mono_font);
1036
  current_font = NULL;
Daniel Colascione's avatar
Daniel Colascione committed
1037
  PDUMPER_IGNORE (current_font);
1038
  first_dpyinfo = NULL;
Daniel Colascione's avatar
Daniel Colascione committed
1039
  PDUMPER_IGNORE (first_dpyinfo);
1040 1041
#ifdef HAVE_GSETTINGS
  gsettings_client = NULL;
Daniel Colascione's avatar
Daniel Colascione committed
1042
  PDUMPER_IGNORE (gsettings_client);
1043
#endif
1044 1045
#ifdef HAVE_GCONF
  gconf_client = NULL;
Daniel Colascione's avatar
Daniel Colascione committed
1046
  PDUMPER_IGNORE (gconf_client);
1047 1048
#endif

1049 1050 1051
  DEFSYM (Qmonospace_font_name, "monospace-font-name");
  DEFSYM (Qfont_name, "font-name");
  DEFSYM (Qfont_render, "font-render");
1052
  defsubr (&Sfont_get_system_font);
1053
  defsubr (&Sfont_get_system_normal_font);
1054

1055
  DEFVAR_BOOL ("font-use-system-font", use_system_font,
1056
    doc: /* Non-nil means to apply the system defined font dynamically.
1057 1058 1059
When this is non-nil and the system defined fixed width font changes, we
update frames dynamically.
If this variable is nil, Emacs ignores system font changes.  */);
1060
  use_system_font = false;
1061