xsettings.c 27.1 KB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1 2
/* Functions for handling font and other changes dynamically.

3
Copyright (C) 2009-2011  Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

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
along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */

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

#include <float.h>
23
#include <limits.h>
24 25 26 27 28 29
#include <setjmp.h>
#include <fcntl.h>
#include "lisp.h"
#include "xterm.h"
#include "xsettings.h"
#include "frame.h"
30
#include "keyboard.h"
31 32 33 34 35 36
#include "blockinput.h"
#include "termhooks.h"
#include "termopts.h"

#include <X11/Xproto.h>

37 38 39 40
#define HAVE_GSETTINGS
#ifdef HAVE_GSETTINGS
#include <glib.h>
#else
41 42 43
#ifdef HAVE_GCONF
#include <gconf/gconf-client.h>
#endif
44 45
#endif

46 47 48 49 50
#ifdef HAVE_XFT
#include <X11/Xft/Xft.h>
#endif

static char *current_mono_font;
51
static char *current_font;
52
static struct x_display_info *first_dpyinfo;
53 54 55
static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render,
  Qtool_bar_style;
static Lisp_Object current_tool_bar_style;
56 57

static void
58
store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
59 60 61 62 63 64 65 66 67
{
  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);
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
static void
store_monospaced_changed (void)
{
  if (first_dpyinfo != NULL)
    {
      /* Check if display still open */
      struct x_display_info *dpyinfo;
      int found = 0;
      for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
        found = dpyinfo == first_dpyinfo;

      if (found && use_system_font)
        store_config_changed_event (Qmonospace_font_name,
                                    XCAR (first_dpyinfo->name_list_element));
    }
}


#ifdef HAVE_GSETTINGS

#define EMACS_TYPE_SETTINGS                  (emacs_settings_get_type ())
#define EMACS_SETTINGS(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), EMACS_TYPE_SETTINGS, EmacsSettings))
#define EMACS_IS_SETTINGS(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EMACS_TYPE_SETTINGS))
#define EMACS_SETTINGS_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST ((klass), EMACS_TYPE_SETTINGS, EmacsSettingsClass))
#define EMACS_IS_SETTINGS_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE ((klass), EMACS_TYPE_SETTINGS))
#define EMACS_SETTINGS_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), EMACS_TYPE_SETTINGS, EmacsSettingsClass))

typedef struct _EmacsSettings        EmacsSettings;
typedef struct _EmacsSettingsClass   EmacsSettingsClass;

struct _EmacsSettings
{
  GObject parent_instance;
};

struct _EmacsSettingsClass
{
  GObjectClass parent_class;
};

/* will create emacs_settings_get_type and set emacs_settings_parent_class */
G_DEFINE_TYPE (EmacsSettings, emacs_settings, G_TYPE_OBJECT);

static GObject *
emacs_settings_constructor (GType gtype,
                            guint n_properties,
                            GObjectConstructParam *properties)
{
  GObject *obj;

  /* Always chain up to the parent constructor */
  obj = G_OBJECT_CLASS (emacs_settings_parent_class)
    ->constructor (gtype, n_properties, properties);
  
  /* update the object state depending on constructor properties */

  return obj;
}

enum { PROP_MONO = 1, PROP_FONT };

static void
emacs_settings_get_property (GObject    *object,
                             guint       property_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  switch (property_id) 
    {
    case PROP_MONO:
      g_value_set_string (value, current_mono_font);
      break;
    case PROP_FONT:
      g_value_set_string (value, current_font);
      break;
    }
}

static void
emacs_settings_set_property (GObject      *object,
                             guint         property_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  const char *newfont;
  switch (property_id) 
    {
    case PROP_MONO:
      xfree (current_mono_font);
      newfont = g_value_get_string (value);
      if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
        return; /* No change. */

      current_mono_font = xstrdup (newfont);
      store_monospaced_changed ();
      break;

    case PROP_FONT:
      xfree (current_font);
      current_font = xstrdup (g_value_get_string (value));
      break;
    }
}

static void
emacs_settings_class_init (EmacsSettingsClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->constructor = emacs_settings_constructor;
  gobject_class->set_property = emacs_settings_set_property;
  gobject_class->get_property = emacs_settings_get_property;

  g_object_class_install_property
    (gobject_class,
     PROP_MONO,
     g_param_spec_string ("monospace-font",
                          "Monospace-font",
                          "System monospace font",
                          "",
                          G_PARAM_READWRITE));
  g_object_class_install_property
    (gobject_class,
     PROP_FONT,
     g_param_spec_string ("font",
                          "Font",
                          "System font",
                          "",
                          G_PARAM_READWRITE));

}

static void
emacs_settings_init (EmacsSettings *self)
{
}


static GSettings *gsettings_client;
static EmacsSettings *gsettings_obj;

#else
#ifdef HAVE_GCONF
static GConfClient *gconf_client;
#endif
#endif


221 222
#define XSETTINGS_FONT_NAME       "Gtk/FontName"
#define XSETTINGS_TOOL_BAR_STYLE  "Gtk/ToolbarStyle"
223

224 225 226 227 228 229 230 231 232 233
enum {
  SEEN_AA         = 0x01,
  SEEN_HINTING    = 0x02,
  SEEN_RGBA       = 0x04,
  SEEN_LCDFILTER  = 0x08,
  SEEN_HINTSTYLE  = 0x10,
  SEEN_DPI        = 0x20,
  SEEN_FONT       = 0x40,
  SEEN_TB_STYLE   = 0x80,
};
234
struct xsettings
235 236 237 238 239 240 241 242 243 244 245 246 247
{
#ifdef HAVE_XFT
  FcBool aa, hinting;
  int rgba, lcdfilter, hintstyle;
  double dpi;
#endif

  char *font;
  char *tb_style;

  unsigned seen;
};

248 249 250 251
#ifdef HAVE_GSETTINGS
#define GSETTINGS_SCHEMA  "org.gnome.desktop.interface"
#define SYSTEM_MONO_FONT  "monospace-font-name"
#define SYSTEM_FONT       "font-name"
252

253 254 255 256
#else
#ifdef HAVE_GCONF
#define SYSTEM_MONO_FONT  "/desktop/gnome/interface/monospace_font_name"
#define SYSTEM_FONT       "/desktop/gnome/interface/font_name"
257

258 259 260 261
/* Callback called when something changed in GConf that we care about,
   that is SYSTEM_MONO_FONT.  */

static void
262 263 264 265
something_changedCB (GConfClient *client,
                     guint cnxn_id,
                     GConfEntry *entry,
                     gpointer user_data)
266 267
{
  GConfValue *v = gconf_entry_get_value (entry);
268

269 270 271 272 273
  if (!v) return;
  if (v->type == GCONF_VALUE_STRING)
    {
      const char *value = gconf_value_get_string (v);
      if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0)
274
        return; /* No change. */
275 276 277

      xfree (current_mono_font);
      current_mono_font = xstrdup (value);
278
      store_monospaced_changed ();
279 280
    }
}
281

282
#endif /* HAVE_GCONF */
283
#endif /* ! HAVE_GSETTINGS */
284 285 286

#ifdef HAVE_XFT

287 288 289 290 291 292 293 294 295 296 297
/* 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

298 299
#endif /* HAVE_XFT */

300 301 302
/* Find the window that contains the XSETTINGS property values.  */

static void
303
get_prop_window (struct x_display_info *dpyinfo)
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
{
  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 SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000)     \
                    | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
#define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
#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:
349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
   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

371
   Returns non-zero if some Xft settings was seen, zero otherwise.
372 373 374
*/

static int
375 376 377
parse_settings (unsigned char *prop,
                long unsigned int bytes,
                struct xsettings *settings)
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
{
  Lisp_Object byteorder = Fbyteorder ();
  int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
  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.  */

  if (bytes < 12) return BadLength;
  memcpy (&n_settings, prop+8, 4);
  if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
  bytes_parsed = 12;

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

396
  while (bytes_parsed+4 < bytes && settings_seen < 7
397 398 399 400 401 402 403
         && 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.  */
404
      int want_this;
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
      int to_cpy;

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

      memcpy (&nlen, prop+bytes_parsed, 2);
      bytes_parsed += 2;
      if (my_bo != that_bo) nlen = SWAP16 (nlen);
      if (bytes_parsed+nlen > bytes) return BadLength;
      to_cpy = nlen > 127 ? 127 : nlen;
      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 */
      if (bytes_parsed > bytes) return BadLength;

425 426 427 428 429 430 431
      want_this =
#ifdef HAVE_XFT
        (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
        ||
#endif
        (strcmp (XSETTINGS_FONT_NAME, name) == 0)
        || (strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0);
432

433
      switch (type)
434 435 436
        {
        case 0: /* Integer */
          if (bytes_parsed+4 > bytes) return BadLength;
437
          if (want_this)
438 439 440 441 442 443 444 445 446 447 448 449
            {
              memcpy (&ival, prop+bytes_parsed, 4);
              if (my_bo != that_bo) ival = SWAP32 (ival);
            }
          bytes_parsed += 4;
          break;

        case 1: /* String */
          if (bytes_parsed+4 > bytes) return BadLength;
          memcpy (&vlen, prop+bytes_parsed, 4);
          bytes_parsed += 4;
          if (my_bo != that_bo) vlen = SWAP32 (vlen);
450
          if (want_this)
451 452 453 454 455 456 457 458 459 460 461 462
            {
              to_cpy = vlen > 127 ? 127 : vlen;
              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 */
          if (bytes_parsed+8 > bytes) return BadLength;
463
          bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each.  */
464 465 466 467 468 469
          break;

        default: /* Parse Error */
          return BadValue;
        }

470
      if (want_this)
471 472
        {
          ++settings_seen;
473 474 475 476 477 478 479 480 481 482 483 484
          if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
            {
              settings->font = xstrdup (sval);
              settings->seen |= SEEN_FONT;
            }
          else if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
            {
              settings->tb_style = xstrdup (sval);
              settings->seen |= SEEN_TB_STYLE;
            }
#ifdef HAVE_XFT
          else if (strcmp (name, "Xft/Antialias") == 0)
485 486 487 488
            {
              settings->seen |= SEEN_AA;
              settings->aa = ival != 0;
            }
489
          else if (strcmp (name, "Xft/Hinting") == 0)
490 491 492 493
            {
              settings->seen |= SEEN_HINTING;
              settings->hinting = ival != 0;
            }
494
# ifdef FC_HINT_STYLE
495 496
          else if (strcmp (name, "Xft/HintStyle") == 0)
            {
497
              settings->seen |= SEEN_HINTSTYLE;
498 499 500 501 502 503 504 505
              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;
506 507
              else
                settings->seen &= ~SEEN_HINTSTYLE;
508
            }
509
# endif
510 511
          else if (strcmp (name, "Xft/RGBA") == 0)
            {
512
              settings->seen |= SEEN_RGBA;
513 514 515 516 517 518 519 520 521 522
              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;
523 524
              else
                settings->seen &= ~SEEN_RGBA;
525 526
            }
          else if (strcmp (name, "Xft/DPI") == 0)
527 528 529 530
            {
              settings->seen |= SEEN_DPI;
              settings->dpi = (double)ival/1024.0;
            }
531 532
          else if (strcmp (name, "Xft/lcdfilter") == 0)
            {
533
              settings->seen |= SEEN_LCDFILTER;
534 535 536 537
              if (strcmp (sval, "none") == 0)
                settings->lcdfilter = FC_LCD_NONE;
              else if (strcmp (sval, "lcddefault") == 0)
                settings->lcdfilter = FC_LCD_DEFAULT;
538 539
              else
                settings->seen &= ~SEEN_LCDFILTER;
540
            }
541
#endif /* HAVE_XFT */
542 543 544
        }
    }

545
  return settings_seen;
546 547 548
}

static int
549
read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
{
  Atom act_type;
  int act_form;
  unsigned long nitems, bytes_after;
  unsigned char *prop = NULL;
  Display *dpy = dpyinfo->display;
  int rc;

  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)
568
    rc = parse_settings (prop, nitems, settings);
569 570 571 572 573

  XFree (prop);

  x_uncatch_errors ();

574
  return rc != 0;
575 576
}

577

578
static void
579 580 581
apply_xft_settings (struct x_display_info *dpyinfo,
                    int send_event_p,
                    struct xsettings *settings)
582
{
583
#ifdef HAVE_XFT
584
  FcPattern *pat;
585
  struct xsettings oldsettings;
586 587 588 589 590 591 592 593 594
  int changed = 0;

  memset (&oldsettings, 0, sizeof (oldsettings));
  pat = FcPatternCreate ();
  XftDefaultSubstitute (dpyinfo->display,
                        XScreenNumberOfScreen (dpyinfo->screen),
                        pat);
  FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
  FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
595
# ifdef FC_HINT_STYLE
596
  FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
597
# endif
598 599 600 601
  FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
  FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
  FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);

602
  if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
603 604
    {
      FcPatternDel (pat, FC_ANTIALIAS);
605
      FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
606
      ++changed;
607
      oldsettings.aa = settings->aa;
608
    }
609

610 611
  if ((settings->seen & SEEN_HINTING) != 0
      && oldsettings.hinting != settings->hinting)
612 613
    {
      FcPatternDel (pat, FC_HINTING);
614
      FcPatternAddBool (pat, FC_HINTING, settings->hinting);
615
      ++changed;
616
      oldsettings.hinting = settings->hinting;
617
    }
618
  if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
619 620
    {
      FcPatternDel (pat, FC_RGBA);
621 622
      FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
      oldsettings.rgba = settings->rgba;
623 624
      ++changed;
    }
625

Jan Djärv's avatar
Jan Djärv committed
626
  /* Older fontconfig versions don't have FC_LCD_FILTER. */
627 628
  if ((settings->seen & SEEN_LCDFILTER) != 0
      && oldsettings.lcdfilter != settings->lcdfilter)
629 630
    {
      FcPatternDel (pat, FC_LCD_FILTER);
631
      FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
632
      ++changed;
633
      oldsettings.lcdfilter = settings->lcdfilter;
634
    }
635

636
# ifdef FC_HINT_STYLE
637 638
  if ((settings->seen & SEEN_HINTSTYLE) != 0
      && oldsettings.hintstyle != settings->hintstyle)
639 640
    {
      FcPatternDel (pat, FC_HINT_STYLE);
641
      FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
642
      ++changed;
643
      oldsettings.hintstyle = settings->hintstyle;
644
    }
645
# endif
646

647 648
  if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi
      && settings->dpi > 0)
649 650 651 652
    {
      Lisp_Object frame, tail;

      FcPatternDel (pat, FC_DPI);
653
      FcPatternAddDouble (pat, FC_DPI, settings->dpi);
654
      ++changed;
655
      oldsettings.dpi = settings->dpi;
656

657
      /* Change the DPI on this display and all frames on the display.  */
658
      dpyinfo->resy = dpyinfo->resx = settings->dpi;
659 660 661
      FOR_EACH_FRAME (tail, frame)
        if (FRAME_X_P (XFRAME (frame))
            && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
662
          XFRAME (frame)->resy = XFRAME (frame)->resx = settings->dpi;
663 664 665 666
    }

  if (changed)
    {
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
      static char const format[] =
	"Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
	"Hintstyle: %d, DPI: %lf";
      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
		     - sizeof "%lf")
      };
      char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];

682 683
      XftDefaultSet (dpyinfo->display, pat);
      if (send_event_p)
684 685
        store_config_changed_event (Qfont_render,
                                    XCAR (dpyinfo->name_list_element));
686 687 688 689
      sprintf (buf, format, oldsettings.aa, oldsettings.hinting,
	       oldsettings.rgba, oldsettings.lcdfilter,
	       oldsettings.hintstyle, oldsettings.dpi);
      Vxft_settings = build_string (buf);
690 691 692
    }
  else
    FcPatternDestroy (pat);
693
#endif /* HAVE_XFT */
694 695
}

696
static void
697
read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
{
  struct xsettings settings;
  Lisp_Object dpyname = XCAR (dpyinfo->name_list_element);

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

  apply_xft_settings (dpyinfo, True, &settings);
  if (settings.seen & SEEN_TB_STYLE)
    {
      Lisp_Object style = Qnil;
      if (strcmp (settings.tb_style, "both") == 0)
        style = Qboth;
      else if (strcmp (settings.tb_style, "both-horiz") == 0)
        style = Qboth_horiz;
      else if (strcmp (settings.tb_style, "icons") == 0)
        style = Qimage;
      else if (strcmp (settings.tb_style, "text") == 0)
        style = Qtext;
      if (!NILP (style) && !EQ (style, current_tool_bar_style))
        {
          current_tool_bar_style = style;
          if (send_event_p)
            store_config_changed_event (Qtool_bar_style, dpyname);
        }
723
      xfree (settings.tb_style);
724 725 726 727
    }

  if (settings.seen & SEEN_FONT)
    {
728
      if (!current_font || strcmp (current_font, settings.font) != 0)
729
        {
730
          xfree (current_font);
731 732 733 734 735
          current_font = settings.font;
          if (send_event_p)
            store_config_changed_event (Qfont_name, dpyname);
        }
      else
736
        xfree (settings.font);
737 738
    }
}
739 740

void
741
xft_settings_event (struct x_display_info *dpyinfo, XEvent *event)
742 743
{
  int check_window_p = 0;
744
  int apply_settings = 0;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763

  switch (event->type)
    {
    case DestroyNotify:
      if (dpyinfo->xsettings_window == event->xany.window)
        check_window_p = 1;
      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)
        check_window_p = 1;
      break;

    case PropertyNotify:
      if (event->xproperty.window == dpyinfo->xsettings_window
          && event->xproperty.state == PropertyNewValue
          && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
764
        apply_settings = 1;
765 766 767
      break;
    }

768

769 770 771 772 773
  if (check_window_p)
    {
      dpyinfo->xsettings_window = None;
      get_prop_window (dpyinfo);
      if (dpyinfo->xsettings_window != None)
774
        apply_settings = 1;
775
    }
776 777 778

  if (apply_settings)
    read_and_apply_settings (dpyinfo, True);
779 780 781
}


782 783 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
static void
init_gsettings (void)
{
#ifdef HAVE_GSETTINGS
  GVariant *val;
#ifdef HAVE_G_TYPE_INIT
  g_type_init ();
#endif

  gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
  if (!gsettings_client) return;
  g_object_ref_sink (G_OBJECT (gsettings_client));

  gsettings_obj = g_object_new (EMACS_TYPE_SETTINGS, NULL);
  if (!gsettings_obj) 
    {
      g_object_unref (G_OBJECT (gsettings_client));
      return;
    }
  g_object_ref_sink (G_OBJECT (gsettings_obj));

  val = g_settings_get_value (gsettings_client, SYSTEM_MONO_FONT);
  if (val) 
    {
      g_variant_ref_sink (val);
      if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
        current_mono_font = xstrdup (g_variant_get_string (val, NULL));
      g_variant_unref (val);
    }

  g_settings_bind (gsettings_client, SYSTEM_MONO_FONT, gsettings_obj,
                   "monospace-font", G_SETTINGS_BIND_GET);
  g_settings_bind (gsettings_client, SYSTEM_FONT, gsettings_obj,
                   "font", G_SETTINGS_BIND_GET);
#endif /* HAVE_GSETTINGS */
}


820
static void
821
init_gconf (void)
822
{
823
#if defined (HAVE_GCONF) && defined (HAVE_XFT) && ! defined (HAVE_GSETTINGS)
824 825
  char *s;

826
#ifdef HAVE_G_TYPE_INIT
827
  g_type_init ();
828
#endif
829 830 831 832 833 834 835
  gconf_client = gconf_client_get_default ();
  s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
  if (s)
    {
      current_mono_font = xstrdup (s);
      g_free (s);
    }
836 837 838 839 840 841
  s = gconf_client_get_string (gconf_client, SYSTEM_FONT, NULL);
  if (s)
    {
      current_font = xstrdup (s);
      g_free (s);
    }
842 843 844 845 846 847 848 849 850
  gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
  gconf_client_add_dir (gconf_client,
                        SYSTEM_MONO_FONT,
                        GCONF_CLIENT_PRELOAD_ONELEVEL,
                        NULL);
  gconf_client_notify_add (gconf_client,
                           SYSTEM_MONO_FONT,
                           something_changedCB,
                           NULL, NULL, NULL);
851
#endif /* HAVE_GCONF && HAVE_XFT && ! HAVE_GSETTINGS */
852 853 854
}

static void
855
init_xsettings (struct x_display_info *dpyinfo)
856 857 858 859 860 861 862 863 864 865 866
{
  Display *dpy = dpyinfo->display;

  BLOCK_INPUT;

  /* 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)
867
    read_and_apply_settings (dpyinfo, False);
868 869 870 871 872

  UNBLOCK_INPUT;
}

void
873
xsettings_initialize (struct x_display_info *dpyinfo)
874 875
{
  if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
876
  init_gsettings ();
877
  init_gconf ();
878
  init_xsettings (dpyinfo);
879 880
}

881
const char *
882
xsettings_get_system_font (void)
883 884 885
{
  return current_mono_font;
}
886

887
#ifdef USE_LUCID
888
const char *
889
xsettings_get_system_normal_font (void)
890 891 892
{
  return current_font;
}
893
#endif
894 895 896 897

DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
       Sfont_get_system_normal_font,
       0, 0, 0,
898
       doc: /* Get the system default application font. */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
899
  (void)
900
{
901
  return current_font ? build_string (current_font) : Qnil;
902 903
}

904 905
DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
       0, 0, 0,
906
       doc: /* Get the system default fixed width font. */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
907
  (void)
908
{
909
  return current_mono_font ? build_string (current_mono_font) : Qnil;
910 911
}

Paul Eggert's avatar
Paul Eggert committed
912
DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
913
       Stool_bar_get_system_style, 0, 0, 0,
914
       doc: /* Get the system tool bar style.
915
If no system tool bar style is known, return `tool-bar-style' if set to a
916
known style.  Otherwise return image.  */)
Dan Nicolaescu's avatar
Dan Nicolaescu committed
917
  (void)
918 919 920 921
{
  if (EQ (Vtool_bar_style, Qimage)
      || EQ (Vtool_bar_style, Qtext)
      || EQ (Vtool_bar_style, Qboth)
922 923
      || EQ (Vtool_bar_style, Qboth_horiz)
      || EQ (Vtool_bar_style, Qtext_image_horiz))
924 925 926 927 928 929
    return Vtool_bar_style;
  if (!NILP (current_tool_bar_style))
    return current_tool_bar_style;
  return Qimage;
}

930
void
931
syms_of_xsettings (void)
932 933
{
  current_mono_font = NULL;
934
  current_font = NULL;
935
  first_dpyinfo = NULL;
936 937 938 939
#ifdef HAVE_GSETTINGS
  gsettings_client = NULL;
  gsettings_obj = NULL;
#else
940 941
#ifdef HAVE_GCONF
  gconf_client = NULL;
942
#endif
943 944
#endif

945 946 947
  DEFSYM (Qmonospace_font_name, "monospace-font-name");
  DEFSYM (Qfont_name, "font-name");
  DEFSYM (Qfont_render, "font-render");
948
  defsubr (&Sfont_get_system_font);
949
  defsubr (&Sfont_get_system_normal_font);
950

951
  DEFVAR_BOOL ("font-use-system-font", use_system_font,
952 953 954 955
    doc: /* *Non-nil means to apply the system defined font dynamically.
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.  */);
956 957
  use_system_font = 0;

958
  DEFVAR_LISP ("xft-settings", Vxft_settings,
959 960 961
               doc: /* Font settings applied to Xft.  */);
  Vxft_settings = make_string ("", 0);

962 963
#ifdef HAVE_XFT
  Fprovide (intern_c_string ("font-render-setting"), Qnil);
964
#if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
965 966 967
  Fprovide (intern_c_string ("system-font-setting"), Qnil);
#endif
#endif
968 969

  current_tool_bar_style = Qnil;
970
  DEFSYM (Qtool_bar_style, "tool-bar-style");
971 972 973
  defsubr (&Stool_bar_get_system_style);

  Fprovide (intern_c_string ("dynamic-setting"), Qnil);
974
}