* lisp.h (eassert): Assume that COND is true when optimizing.
[bpt/emacs.git] / src / xsettings.c
CommitLineData
4ff155dd
GM
1/* Functions for handling font and other changes dynamically.
2
ab422c4d 3Copyright (C) 2009-2013 Free Software Foundation, Inc.
637fa988
JD
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
08a494a3 20#include <config.h>
aaafe47a
PE
21
22#include <float.h>
210af043 23#include <limits.h>
637fa988
JD
24#include <fcntl.h>
25#include "lisp.h"
26#include "xterm.h"
27#include "xsettings.h"
28#include "frame.h"
b87dd913 29#include "keyboard.h"
637fa988
JD
30#include "blockinput.h"
31#include "termhooks.h"
637fa988
JD
32
33#include <X11/Xproto.h>
34
9851bfc5 35#ifdef HAVE_GSETTINGS
51bb811f
JD
36#include <glib-object.h>
37#include <gio/gio.h>
869795d6
JD
38#endif
39
637fa988
JD
40#ifdef HAVE_GCONF
41#include <gconf/gconf-client.h>
42#endif
9851bfc5 43
637fa988
JD
44#ifdef HAVE_XFT
45#include <X11/Xft/Xft.h>
46#endif
47
48static char *current_mono_font;
99852628 49static char *current_font;
637fa988 50static struct x_display_info *first_dpyinfo;
f904c0f9
JD
51static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render,
52 Qtool_bar_style;
f904c0f9 53static Lisp_Object current_tool_bar_style;
637fa988 54
869795d6
JD
55/* Store an config changed event in to the event queue. */
56
637fa988 57static void
971de7fb 58store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
637fa988
JD
59{
60 struct input_event event;
61 EVENT_INIT (event);
62 event.kind = CONFIG_CHANGED_EVENT;
63 event.frame_or_window = display_name;
64 event.arg = arg;
65 kbd_buffer_store_event (&event);
66}
67
869795d6
JD
68/* Return non-zero if DPYINFO is still valid. */
69static int
70dpyinfo_valid (struct x_display_info *dpyinfo)
71{
72 int found = 0;
73 if (dpyinfo != NULL)
74 {
75 struct x_display_info *d;
76 for (d = x_display_list; !found && d; d = d->next)
77 found = d == dpyinfo && d->display == dpyinfo->display;
78 }
79 return found;
80}
81
82/* Store a monospace font change event if the monospaced font changed. */
83
490011a6 84#if defined HAVE_XFT && (defined HAVE_GSETTINGS || defined HAVE_GCONF)
9851bfc5 85static void
0949d2b6 86store_monospaced_changed (const char *newfont)
9851bfc5 87{
0949d2b6
JD
88 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
89 return; /* No change. */
90
91 xfree (current_mono_font);
92 current_mono_font = xstrdup (newfont);
93
869795d6 94 if (dpyinfo_valid (first_dpyinfo) && use_system_font)
9851bfc5 95 {
869795d6
JD
96 store_config_changed_event (Qmonospace_font_name,
97 XCAR (first_dpyinfo->name_list_element));
9851bfc5
JD
98 }
99}
490011a6 100#endif
9851bfc5 101
869795d6 102/* Store a font name change event if the font name changed. */
9851bfc5 103
490011a6 104#ifdef HAVE_XFT
869795d6
JD
105static void
106store_font_name_changed (const char *newfont)
107{
108 if (current_font != NULL && strcmp (newfont, current_font) == 0)
109 return; /* No change. */
110
111 xfree (current_font);
112 current_font = xstrdup (newfont);
113
114 if (dpyinfo_valid (first_dpyinfo))
115 {
116 store_config_changed_event (Qfont_name,
117 XCAR (first_dpyinfo->name_list_element));
118 }
119}
120#endif /* HAVE_XFT */
121
da6062e6 122/* Map TOOL_BAR_STYLE from a string to its corresponding Lisp value.
869795d6
JD
123 Return Qnil if TOOL_BAR_STYLE is not known. */
124
125static Lisp_Object
126map_tool_bar_style (const char *tool_bar_style)
127{
128 Lisp_Object style = Qnil;
129 if (tool_bar_style)
130 {
131 if (strcmp (tool_bar_style, "both") == 0)
132 style = Qboth;
133 else if (strcmp (tool_bar_style, "both-horiz") == 0)
134 style = Qboth_horiz;
135 else if (strcmp (tool_bar_style, "icons") == 0)
136 style = Qimage;
137 else if (strcmp (tool_bar_style, "text") == 0)
138 style = Qtext;
139 }
140
141 return style;
142}
143
144/* Store a tool bar style change event if the tool bar style changed. */
145
146static void
147store_tool_bar_style_changed (const char *newstyle,
148 struct x_display_info *dpyinfo)
149{
150 Lisp_Object style = map_tool_bar_style (newstyle);
151 if (EQ (current_tool_bar_style, style))
152 return; /* No change. */
153
154 current_tool_bar_style = style;
155 if (dpyinfo_valid (dpyinfo))
156 store_config_changed_event (Qtool_bar_style,
157 XCAR (dpyinfo->name_list_element));
158}
9851bfc5 159
2014308a 160#ifdef HAVE_XFT
f904c0f9 161#define XSETTINGS_FONT_NAME "Gtk/FontName"
2014308a 162#endif
f904c0f9 163#define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
0269ef77 164
f904c0f9
JD
165enum {
166 SEEN_AA = 0x01,
167 SEEN_HINTING = 0x02,
168 SEEN_RGBA = 0x04,
169 SEEN_LCDFILTER = 0x08,
170 SEEN_HINTSTYLE = 0x10,
171 SEEN_DPI = 0x20,
172 SEEN_FONT = 0x40,
29abe551 173 SEEN_TB_STYLE = 0x80
f904c0f9 174};
af59aa6e 175struct xsettings
f904c0f9
JD
176{
177#ifdef HAVE_XFT
178 FcBool aa, hinting;
179 int rgba, lcdfilter, hintstyle;
180 double dpi;
f904c0f9
JD
181
182 char *font;
869795d6
JD
183#endif
184
f904c0f9
JD
185 char *tb_style;
186
187 unsigned seen;
188};
189
9851bfc5 190#ifdef HAVE_GSETTINGS
869795d6
JD
191#define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
192#define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"
193
194#ifdef HAVE_XFT
195#define GSETTINGS_MONO_FONT "monospace-font-name"
196#define GSETTINGS_FONT_NAME "font-name"
197#endif
198
199
200/* The single GSettings instance, or NULL if not connected to GSettings. */
201
202static GSettings *gsettings_client;
203
204/* Callback called when something changed in GSettings. */
31a01b90 205
0949d2b6 206static void
869795d6
JD
207something_changed_gsettingsCB (GSettings *settings,
208 gchar *key,
209 gpointer user_data)
0949d2b6
JD
210{
211 GVariant *val;
869795d6
JD
212
213 if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
0949d2b6 214 {
869795d6
JD
215 val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
216 if (val)
0949d2b6 217 {
869795d6
JD
218 g_variant_ref_sink (val);
219 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
220 {
221 const gchar *newstyle = g_variant_get_string (val, NULL);
222 store_tool_bar_style_changed (newstyle, first_dpyinfo);
223 }
224 g_variant_unref (val);
225 }
226 }
227#ifdef HAVE_XFT
228 else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
229 {
230 val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
231 if (val)
232 {
233 g_variant_ref_sink (val);
234 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
235 {
236 const gchar *newfont = g_variant_get_string (val, NULL);
237 store_monospaced_changed (newfont);
238 }
239 g_variant_unref (val);
0949d2b6 240 }
0949d2b6 241 }
869795d6
JD
242 else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
243 {
244 val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
245 if (val)
246 {
247 g_variant_ref_sink (val);
248 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
249 {
250 const gchar *newfont = g_variant_get_string (val, NULL);
251 store_font_name_changed (newfont);
252 }
253 g_variant_unref (val);
254 }
255 }
490011a6 256#endif /* HAVE_XFT */
0949d2b6
JD
257}
258
869795d6 259#endif /* HAVE_GSETTINGS */
2e13213d 260
869795d6
JD
261#ifdef HAVE_GCONF
262#define GCONF_TOOL_BAR_STYLE "/desktop/gnome/interface/toolbar_style"
2e13213d 263#ifdef HAVE_XFT
869795d6
JD
264#define GCONF_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
265#define GCONF_FONT_NAME "/desktop/gnome/interface/font_name"
2e13213d 266#endif
31a01b90 267
869795d6
JD
268/* The single GConf instance, or NULL if not connected to GConf. */
269
270static GConfClient *gconf_client;
271
272/* Callback called when something changed in GConf that we care about. */
637fa988
JD
273
274static void
869795d6
JD
275something_changed_gconfCB (GConfClient *client,
276 guint cnxn_id,
277 GConfEntry *entry,
278 gpointer user_data)
637fa988
JD
279{
280 GConfValue *v = gconf_entry_get_value (entry);
869795d6 281 const char *key = gconf_entry_get_key (entry);
af59aa6e 282
869795d6
JD
283 if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
284 if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
285 {
286 const char *value = gconf_value_get_string (v);
287 store_tool_bar_style_changed (value, first_dpyinfo);
288 }
289#ifdef HAVE_XFT
290 else if (strcmp (key, GCONF_MONO_FONT) == 0)
637fa988
JD
291 {
292 const char *value = gconf_value_get_string (v);
0949d2b6 293 store_monospaced_changed (value);
637fa988 294 }
869795d6
JD
295 else if (strcmp (key, GCONF_FONT_NAME) == 0)
296 {
297 const char *value = gconf_value_get_string (v);
298 store_font_name_changed (value);
299 }
300#endif /* HAVE_XFT */
637fa988 301}
9851bfc5 302
637fa988
JD
303#endif /* HAVE_GCONF */
304
305#ifdef HAVE_XFT
306
21050de1
JD
307/* Older fontconfig versions don't have FC_LCD_*. */
308#ifndef FC_LCD_NONE
309#define FC_LCD_NONE 0
310#endif
311#ifndef FC_LCD_DEFAULT
312#define FC_LCD_DEFAULT 1
313#endif
314#ifndef FC_LCD_FILTER
315#define FC_LCD_FILTER "lcdfilter"
316#endif
317
f904c0f9
JD
318#endif /* HAVE_XFT */
319
637fa988
JD
320/* Find the window that contains the XSETTINGS property values. */
321
322static void
971de7fb 323get_prop_window (struct x_display_info *dpyinfo)
637fa988
JD
324{
325 Display *dpy = dpyinfo->display;
326
327 XGrabServer (dpy);
328 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
329 dpyinfo->Xatom_xsettings_sel);
330 if (dpyinfo->xsettings_window != None)
331 /* Select events so we can detect if window is deleted or if settings
332 are changed. */
333 XSelectInput (dpy, dpyinfo->xsettings_window,
334 PropertyChangeMask|StructureNotifyMask);
335
336 XUngrabServer (dpy);
337}
338
637fa988
JD
339#define PAD(nr) (((nr) + 3) & ~3)
340
341/* Parse xsettings and extract those that deal with Xft.
342 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
343 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
344
345 Layout of prop. First is a header:
346
347 bytes type what
348 ------------------------------------
349 1 CARD8 byte-order
350 3 unused
351 4 CARD32 SERIAL
352 4 CARD32 N_SETTINGS
353
354 Then N_SETTINGS records, with header:
355
356 bytes type what
357 ------------------------------------
358 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
359 1 unused
360 2 CARD16 n == name-length
361 n STRING8 name
362 p unused, p=pad_to_even_4(n)
363 4 CARD32 last-change-serial
364
365 and then the value, For string:
af59aa6e 366
637fa988
JD
367 bytes type what
368 ------------------------------------
369 4 CARD32 n = value-length
370 n STRING8 value
371 p unused, p=pad_to_even_4(n)
372
373 For integer:
374
375 bytes type what
376 ------------------------------------
377 4 INT32 value
378
379 For RGB color:
380
381 bytes type what
382 ------------------------------------
383 2 CARD16 red
384 2 CARD16 blue
385 2 CARD16 green
386 2 CARD16 alpha
387
3c055b77 388 Returns non-zero if some Xft settings was seen, zero otherwise.
637fa988
JD
389*/
390
391static int
e4c8d29a
J
392parse_settings (unsigned char *prop,
393 long unsigned int bytes,
394 struct xsettings *settings)
637fa988
JD
395{
396 Lisp_Object byteorder = Fbyteorder ();
397 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
398 int that_bo = prop[0];
399 CARD32 n_settings;
400 int bytes_parsed = 0;
401 int settings_seen = 0;
402 int i = 0;
403
404 /* First 4 bytes is a serial number, skip that. */
405
406 if (bytes < 12) return BadLength;
407 memcpy (&n_settings, prop+8, 4);
3e0b94e7 408 if (my_bo != that_bo) n_settings = swap32 (n_settings);
637fa988
JD
409 bytes_parsed = 12;
410
411 memset (settings, 0, sizeof (*settings));
412
99852628 413 while (bytes_parsed+4 < bytes && settings_seen < 7
637fa988
JD
414 && i < n_settings)
415 {
416 int type = prop[bytes_parsed++];
417 CARD16 nlen;
418 CARD32 vlen, ival = 0;
419 char name[128]; /* The names we are looking for are not this long. */
420 char sval[128]; /* The values we are looking for are not this long. */
99852628 421 int want_this;
637fa988
JD
422 int to_cpy;
423
424 sval[0] = '\0';
425 ++i;
426 ++bytes_parsed; /* Padding */
427
428 memcpy (&nlen, prop+bytes_parsed, 2);
429 bytes_parsed += 2;
3e0b94e7 430 if (my_bo != that_bo) nlen = swap16 (nlen);
637fa988
JD
431 if (bytes_parsed+nlen > bytes) return BadLength;
432 to_cpy = nlen > 127 ? 127 : nlen;
433 memcpy (name, prop+bytes_parsed, to_cpy);
434 name[to_cpy] = '\0';
435
436 bytes_parsed += nlen;
437 bytes_parsed = PAD (bytes_parsed);
438
439 bytes_parsed += 4; /* Skip serial for this value */
440 if (bytes_parsed > bytes) return BadLength;
441
490011a6 442 want_this =
f904c0f9
JD
443#ifdef HAVE_XFT
444 (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
869795d6 445 || strcmp (XSETTINGS_FONT_NAME, name) == 0
f904c0f9
JD
446 ||
447#endif
869795d6 448 strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
490011a6 449
af59aa6e 450 switch (type)
637fa988
JD
451 {
452 case 0: /* Integer */
453 if (bytes_parsed+4 > bytes) return BadLength;
99852628 454 if (want_this)
637fa988
JD
455 {
456 memcpy (&ival, prop+bytes_parsed, 4);
3e0b94e7 457 if (my_bo != that_bo) ival = swap32 (ival);
637fa988
JD
458 }
459 bytes_parsed += 4;
460 break;
461
462 case 1: /* String */
463 if (bytes_parsed+4 > bytes) return BadLength;
464 memcpy (&vlen, prop+bytes_parsed, 4);
465 bytes_parsed += 4;
3e0b94e7 466 if (my_bo != that_bo) vlen = swap32 (vlen);
99852628 467 if (want_this)
637fa988
JD
468 {
469 to_cpy = vlen > 127 ? 127 : vlen;
470 memcpy (sval, prop+bytes_parsed, to_cpy);
471 sval[to_cpy] = '\0';
472 }
473 bytes_parsed += vlen;
474 bytes_parsed = PAD (bytes_parsed);
475 break;
476
477 case 2: /* RGB value */
478 /* No need to parse this */
479 if (bytes_parsed+8 > bytes) return BadLength;
af59aa6e 480 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
637fa988
JD
481 break;
482
483 default: /* Parse Error */
484 return BadValue;
485 }
486
af59aa6e 487 if (want_this)
637fa988
JD
488 {
489 ++settings_seen;
869795d6 490 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
f904c0f9
JD
491 {
492 settings->tb_style = xstrdup (sval);
493 settings->seen |= SEEN_TB_STYLE;
494 }
495#ifdef HAVE_XFT
869795d6
JD
496 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
497 {
498 settings->font = xstrdup (sval);
499 settings->seen |= SEEN_FONT;
500 }
f904c0f9 501 else if (strcmp (name, "Xft/Antialias") == 0)
3c055b77
JD
502 {
503 settings->seen |= SEEN_AA;
504 settings->aa = ival != 0;
505 }
637fa988 506 else if (strcmp (name, "Xft/Hinting") == 0)
3c055b77
JD
507 {
508 settings->seen |= SEEN_HINTING;
509 settings->hinting = ival != 0;
510 }
af59aa6e 511# ifdef FC_HINT_STYLE
637fa988
JD
512 else if (strcmp (name, "Xft/HintStyle") == 0)
513 {
3c055b77 514 settings->seen |= SEEN_HINTSTYLE;
637fa988
JD
515 if (strcmp (sval, "hintnone") == 0)
516 settings->hintstyle = FC_HINT_NONE;
517 else if (strcmp (sval, "hintslight") == 0)
518 settings->hintstyle = FC_HINT_SLIGHT;
519 else if (strcmp (sval, "hintmedium") == 0)
520 settings->hintstyle = FC_HINT_MEDIUM;
521 else if (strcmp (sval, "hintfull") == 0)
522 settings->hintstyle = FC_HINT_FULL;
3c055b77
JD
523 else
524 settings->seen &= ~SEEN_HINTSTYLE;
637fa988 525 }
af59aa6e 526# endif
637fa988
JD
527 else if (strcmp (name, "Xft/RGBA") == 0)
528 {
3c055b77 529 settings->seen |= SEEN_RGBA;
637fa988
JD
530 if (strcmp (sval, "none") == 0)
531 settings->rgba = FC_RGBA_NONE;
532 else if (strcmp (sval, "rgb") == 0)
533 settings->rgba = FC_RGBA_RGB;
534 else if (strcmp (sval, "bgr") == 0)
535 settings->rgba = FC_RGBA_BGR;
536 else if (strcmp (sval, "vrgb") == 0)
537 settings->rgba = FC_RGBA_VRGB;
538 else if (strcmp (sval, "vbgr") == 0)
539 settings->rgba = FC_RGBA_VBGR;
3c055b77
JD
540 else
541 settings->seen &= ~SEEN_RGBA;
637fa988
JD
542 }
543 else if (strcmp (name, "Xft/DPI") == 0)
3c055b77
JD
544 {
545 settings->seen |= SEEN_DPI;
546 settings->dpi = (double)ival/1024.0;
547 }
637fa988
JD
548 else if (strcmp (name, "Xft/lcdfilter") == 0)
549 {
3c055b77 550 settings->seen |= SEEN_LCDFILTER;
637fa988
JD
551 if (strcmp (sval, "none") == 0)
552 settings->lcdfilter = FC_LCD_NONE;
553 else if (strcmp (sval, "lcddefault") == 0)
554 settings->lcdfilter = FC_LCD_DEFAULT;
3c055b77
JD
555 else
556 settings->seen &= ~SEEN_LCDFILTER;
637fa988 557 }
f904c0f9 558#endif /* HAVE_XFT */
637fa988
JD
559 }
560 }
561
3c055b77 562 return settings_seen;
637fa988
JD
563}
564
869795d6
JD
565/* Read settings from the XSettings property window on display for DPYINFO.
566 Store settings read in SETTINGS.
567 Return non-zero if successful, zero if not. */
568
637fa988 569static int
971de7fb 570read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
637fa988 571{
637fa988
JD
572 Atom act_type;
573 int act_form;
574 unsigned long nitems, bytes_after;
575 unsigned char *prop = NULL;
576 Display *dpy = dpyinfo->display;
577 int rc;
578
579 x_catch_errors (dpy);
580 rc = XGetWindowProperty (dpy,
581 dpyinfo->xsettings_window,
582 dpyinfo->Xatom_xsettings_prop,
583 0, LONG_MAX, False, AnyPropertyType,
584 &act_type, &act_form, &nitems, &bytes_after,
585 &prop);
586
587 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
588 && act_type == dpyinfo->Xatom_xsettings_prop)
f904c0f9 589 rc = parse_settings (prop, nitems, settings);
637fa988
JD
590
591 XFree (prop);
592
593 x_uncatch_errors ();
594
3c055b77 595 return rc != 0;
637fa988
JD
596}
597
869795d6
JD
598/* Apply Xft settings in SETTINGS to the Xft library.
599 If SEND_EVENT_P is non-zero store a Lisp event that Xft settings changed. */
581e51e8 600
637fa988 601static void
e4c8d29a
J
602apply_xft_settings (struct x_display_info *dpyinfo,
603 int send_event_p,
604 struct xsettings *settings)
637fa988 605{
f904c0f9 606#ifdef HAVE_XFT
637fa988 607 FcPattern *pat;
f904c0f9 608 struct xsettings oldsettings;
637fa988
JD
609 int changed = 0;
610
637fa988 611 memset (&oldsettings, 0, sizeof (oldsettings));
637fa988
JD
612 pat = FcPatternCreate ();
613 XftDefaultSubstitute (dpyinfo->display,
614 XScreenNumberOfScreen (dpyinfo->screen),
615 pat);
616 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
617 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
869795d6 618#ifdef FC_HINT_STYLE
637fa988 619 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
869795d6 620#endif
637fa988
JD
621 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
622 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
623 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
624
f904c0f9 625 if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
637fa988
JD
626 {
627 FcPatternDel (pat, FC_ANTIALIAS);
f904c0f9 628 FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
637fa988 629 ++changed;
f904c0f9 630 oldsettings.aa = settings->aa;
637fa988 631 }
67477f30 632
f904c0f9
JD
633 if ((settings->seen & SEEN_HINTING) != 0
634 && oldsettings.hinting != settings->hinting)
637fa988
JD
635 {
636 FcPatternDel (pat, FC_HINTING);
f904c0f9 637 FcPatternAddBool (pat, FC_HINTING, settings->hinting);
637fa988 638 ++changed;
f904c0f9 639 oldsettings.hinting = settings->hinting;
637fa988 640 }
f904c0f9 641 if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
637fa988
JD
642 {
643 FcPatternDel (pat, FC_RGBA);
f904c0f9
JD
644 FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
645 oldsettings.rgba = settings->rgba;
637fa988
JD
646 ++changed;
647 }
67477f30 648
a6eb20d8 649 /* Older fontconfig versions don't have FC_LCD_FILTER. */
f904c0f9
JD
650 if ((settings->seen & SEEN_LCDFILTER) != 0
651 && oldsettings.lcdfilter != settings->lcdfilter)
637fa988
JD
652 {
653 FcPatternDel (pat, FC_LCD_FILTER);
f904c0f9 654 FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
637fa988 655 ++changed;
f904c0f9 656 oldsettings.lcdfilter = settings->lcdfilter;
637fa988 657 }
67477f30 658
869795d6 659#ifdef FC_HINT_STYLE
f904c0f9
JD
660 if ((settings->seen & SEEN_HINTSTYLE) != 0
661 && oldsettings.hintstyle != settings->hintstyle)
637fa988
JD
662 {
663 FcPatternDel (pat, FC_HINT_STYLE);
f904c0f9 664 FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
637fa988 665 ++changed;
f904c0f9 666 oldsettings.hintstyle = settings->hintstyle;
637fa988 667 }
869795d6 668#endif
67477f30 669
f904c0f9
JD
670 if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi
671 && settings->dpi > 0)
637fa988 672 {
637fa988 673 FcPatternDel (pat, FC_DPI);
f904c0f9 674 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
637fa988 675 ++changed;
f904c0f9 676 oldsettings.dpi = settings->dpi;
af59aa6e 677
42143acd
DA
678 /* Changing the DPI on this display affects all frames on it.
679 Check FRAME_RES_X and FRAME_RES_Y in frame.h to see how. */
f904c0f9 680 dpyinfo->resy = dpyinfo->resx = settings->dpi;
637fa988
JD
681 }
682
683 if (changed)
684 {
aaafe47a
PE
685 static char const format[] =
686 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
29abe551 687 "Hintstyle: %d, DPI: %f";
aaafe47a
PE
688 enum
689 {
690 d_formats = 5,
691 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
692 lf_formats = 1,
693 max_f_integer_digits = DBL_MAX_10_EXP + 1,
694 f_precision = 6,
695 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
29abe551 696 - sizeof "%f")
aaafe47a
PE
697 };
698 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
699
637fa988
JD
700 XftDefaultSet (dpyinfo->display, pat);
701 if (send_event_p)
f904c0f9
JD
702 store_config_changed_event (Qfont_render,
703 XCAR (dpyinfo->name_list_element));
0328b6de 704 Vxft_settings
a8290ec3
DA
705 = make_formatted_string (buf, format,
706 oldsettings.aa, oldsettings.hinting,
707 oldsettings.rgba, oldsettings.lcdfilter,
708 oldsettings.hintstyle, oldsettings.dpi);
0328b6de 709
637fa988
JD
710 }
711 else
712 FcPatternDestroy (pat);
f904c0f9 713#endif /* HAVE_XFT */
637fa988
JD
714}
715
869795d6
JD
716/* Read XSettings from the display for DPYINFO.
717 If SEND_EVENT_P is non-zero store a Lisp event settings that changed. */
718
f904c0f9 719static void
971de7fb 720read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
f904c0f9
JD
721{
722 struct xsettings settings;
f904c0f9
JD
723
724 if (!read_settings (dpyinfo, &settings))
725 return;
726
727 apply_xft_settings (dpyinfo, True, &settings);
728 if (settings.seen & SEEN_TB_STYLE)
729 {
869795d6
JD
730 if (send_event_p)
731 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
732 else
733 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
baad03f0 734 xfree (settings.tb_style);
f904c0f9 735 }
869795d6 736#ifdef HAVE_XFT
f904c0f9
JD
737 if (settings.seen & SEEN_FONT)
738 {
869795d6
JD
739 if (send_event_p)
740 store_font_name_changed (settings.font);
741 else
f904c0f9 742 {
baad03f0 743 xfree (current_font);
869795d6 744 current_font = xstrdup (settings.font);
f904c0f9 745 }
869795d6 746 xfree (settings.font);
f904c0f9 747 }
869795d6 748#endif
f904c0f9 749}
637fa988 750
869795d6
JD
751/* Check if EVENT for the display in DPYINFO is XSettings related. */
752
637fa988 753void
b7ad2f74 754xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
637fa988 755{
d7e6881a 756 bool check_window_p = 0, apply_settings_p = 0;
637fa988
JD
757
758 switch (event->type)
759 {
760 case DestroyNotify:
761 if (dpyinfo->xsettings_window == event->xany.window)
762 check_window_p = 1;
763 break;
764
765 case ClientMessage:
766 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
767 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
768 && event->xclient.window == dpyinfo->root_window)
769 check_window_p = 1;
770 break;
771
772 case PropertyNotify:
773 if (event->xproperty.window == dpyinfo->xsettings_window
774 && event->xproperty.state == PropertyNewValue
775 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
d7e6881a 776 apply_settings_p = 1;
637fa988
JD
777 break;
778 }
779
f904c0f9 780
637fa988
JD
781 if (check_window_p)
782 {
783 dpyinfo->xsettings_window = None;
784 get_prop_window (dpyinfo);
785 if (dpyinfo->xsettings_window != None)
d7e6881a 786 apply_settings_p = 1;
637fa988 787 }
f904c0f9 788
d7e6881a 789 if (apply_settings_p)
f904c0f9 790 read_and_apply_settings (dpyinfo, True);
637fa988
JD
791}
792
869795d6 793/* Initialize GSettings and read startup values. */
637fa988 794
9851bfc5
JD
795static void
796init_gsettings (void)
797{
798#ifdef HAVE_GSETTINGS
799 GVariant *val;
0949d2b6
JD
800 const gchar *const *schemas;
801 int schema_found = 0;
802
3f386383 803#if ! GLIB_CHECK_VERSION (2, 36, 0)
9851bfc5
JD
804 g_type_init ();
805#endif
806
5e617bc2 807 schemas = g_settings_list_schemas ();
0949d2b6
JD
808 if (schemas == NULL) return;
809 while (! schema_found && *schemas != NULL)
810 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
811 if (!schema_found) return;
812
9851bfc5
JD
813 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
814 if (!gsettings_client) return;
815 g_object_ref_sink (G_OBJECT (gsettings_client));
869795d6
JD
816 g_signal_connect (G_OBJECT (gsettings_client), "changed",
817 G_CALLBACK (something_changed_gsettingsCB), NULL);
818
819 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
820 if (val)
821 {
822 g_variant_ref_sink (val);
823 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
824 current_tool_bar_style
825 = map_tool_bar_style (g_variant_get_string (val, NULL));
826 g_variant_unref (val);
827 }
9851bfc5 828
869795d6
JD
829#ifdef HAVE_XFT
830 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
2e13213d 831 if (val)
9851bfc5
JD
832 {
833 g_variant_ref_sink (val);
834 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
835 current_mono_font = xstrdup (g_variant_get_string (val, NULL));
836 g_variant_unref (val);
837 }
838
869795d6
JD
839 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
840 if (val)
841 {
842 g_variant_ref_sink (val);
843 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
844 current_font = xstrdup (g_variant_get_string (val, NULL));
845 g_variant_unref (val);
846 }
847#endif /* HAVE_XFT */
848
9851bfc5
JD
849#endif /* HAVE_GSETTINGS */
850}
851
869795d6 852/* Init GConf and read startup values. */
9851bfc5 853
637fa988 854static void
971de7fb 855init_gconf (void)
637fa988 856{
869795d6 857#if defined (HAVE_GCONF)
637fa988 858 char *s;
637fa988 859
3f386383 860#if ! GLIB_CHECK_VERSION (2, 36, 0)
637fa988 861 g_type_init ();
1e600395 862#endif
869795d6 863
637fa988 864 gconf_client = gconf_client_get_default ();
869795d6
JD
865 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
866 gconf_client_add_dir (gconf_client,
867 GCONF_TOOL_BAR_STYLE,
868 GCONF_CLIENT_PRELOAD_ONELEVEL,
869 NULL);
870 gconf_client_notify_add (gconf_client,
871 GCONF_TOOL_BAR_STYLE,
872 something_changed_gconfCB,
873 NULL, NULL, NULL);
874
875 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
876 if (s)
877 {
878 current_tool_bar_style = map_tool_bar_style (s);
879 g_free (s);
880 }
881
882#ifdef HAVE_XFT
883 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
637fa988
JD
884 if (s)
885 {
886 current_mono_font = xstrdup (s);
887 g_free (s);
888 }
869795d6 889 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
99852628
JD
890 if (s)
891 {
892 current_font = xstrdup (s);
893 g_free (s);
894 }
637fa988 895 gconf_client_add_dir (gconf_client,
869795d6 896 GCONF_MONO_FONT,
637fa988
JD
897 GCONF_CLIENT_PRELOAD_ONELEVEL,
898 NULL);
899 gconf_client_notify_add (gconf_client,
869795d6
JD
900 GCONF_MONO_FONT,
901 something_changed_gconfCB,
637fa988 902 NULL, NULL, NULL);
869795d6
JD
903 gconf_client_add_dir (gconf_client,
904 GCONF_FONT_NAME,
905 GCONF_CLIENT_PRELOAD_ONELEVEL,
906 NULL);
907 gconf_client_notify_add (gconf_client,
908 GCONF_FONT_NAME,
909 something_changed_gconfCB,
910 NULL, NULL, NULL);
911#endif /* HAVE_XFT */
912#endif /* HAVE_GCONF */
637fa988
JD
913}
914
869795d6
JD
915/* Init Xsettings and read startup values. */
916
637fa988 917static void
971de7fb 918init_xsettings (struct x_display_info *dpyinfo)
637fa988 919{
637fa988
JD
920 Display *dpy = dpyinfo->display;
921
4d7e6e51 922 block_input ();
637fa988 923
637fa988
JD
924 /* Select events so we can detect client messages sent when selection
925 owner changes. */
926 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
927
928 get_prop_window (dpyinfo);
929 if (dpyinfo->xsettings_window != None)
f904c0f9 930 read_and_apply_settings (dpyinfo, False);
637fa988 931
4d7e6e51 932 unblock_input ();
637fa988
JD
933}
934
935void
971de7fb 936xsettings_initialize (struct x_display_info *dpyinfo)
637fa988
JD
937{
938 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
939 init_gconf ();
f904c0f9 940 init_xsettings (dpyinfo);
869795d6 941 init_gsettings ();
637fa988
JD
942}
943
869795d6
JD
944/* Return the system monospaced font.
945 May be NULL if not known. */
946
0d1d0d26 947const char *
971de7fb 948xsettings_get_system_font (void)
0d1d0d26
JD
949{
950 return current_mono_font;
951}
637fa988 952
e87b6180 953#ifdef USE_LUCID
869795d6
JD
954/* Return the system font.
955 May be NULL if not known. */
956
99852628 957const char *
971de7fb 958xsettings_get_system_normal_font (void)
99852628
JD
959{
960 return current_font;
961}
e87b6180 962#endif
99852628
JD
963
964DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
965 Sfont_get_system_normal_font,
966 0, 0, 0,
bf935339 967 doc: /* Get the system default application font. */)
5842a27b 968 (void)
99852628 969{
2674ddc8 970 return current_font ? build_string (current_font) : Qnil;
99852628
JD
971}
972
637fa988
JD
973DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
974 0, 0, 0,
bf935339 975 doc: /* Get the system default fixed width font. */)
5842a27b 976 (void)
637fa988 977{
2674ddc8 978 return current_mono_font ? build_string (current_mono_font) : Qnil;
637fa988
JD
979}
980
a7ca3326 981DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
16a97296 982 Stool_bar_get_system_style, 0, 0, 0,
f904c0f9 983 doc: /* Get the system tool bar style.
4721152c 984If no system tool bar style is known, return `tool-bar-style' if set to a
f904c0f9 985known style. Otherwise return image. */)
5842a27b 986 (void)
f904c0f9
JD
987{
988 if (EQ (Vtool_bar_style, Qimage)
989 || EQ (Vtool_bar_style, Qtext)
990 || EQ (Vtool_bar_style, Qboth)
8a52f00a
JD
991 || EQ (Vtool_bar_style, Qboth_horiz)
992 || EQ (Vtool_bar_style, Qtext_image_horiz))
f904c0f9
JD
993 return Vtool_bar_style;
994 if (!NILP (current_tool_bar_style))
995 return current_tool_bar_style;
996 return Qimage;
997}
998
637fa988 999void
971de7fb 1000syms_of_xsettings (void)
637fa988
JD
1001{
1002 current_mono_font = NULL;
99852628 1003 current_font = NULL;
637fa988 1004 first_dpyinfo = NULL;
9851bfc5
JD
1005#ifdef HAVE_GSETTINGS
1006 gsettings_client = NULL;
869795d6 1007#endif
637fa988
JD
1008#ifdef HAVE_GCONF
1009 gconf_client = NULL;
1010#endif
1011
cd3520a4
JB
1012 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1013 DEFSYM (Qfont_name, "font-name");
1014 DEFSYM (Qfont_render, "font-render");
637fa988 1015 defsubr (&Sfont_get_system_font);
99852628 1016 defsubr (&Sfont_get_system_normal_font);
637fa988 1017
29208e82 1018 DEFVAR_BOOL ("font-use-system-font", use_system_font,
fb7ada5f 1019 doc: /* Non-nil means to apply the system defined font dynamically.
bf935339
J
1020When this is non-nil and the system defined fixed width font changes, we
1021update frames dynamically.
1022If this variable is nil, Emacs ignores system font changes. */);
dfb3c4c6
JD
1023 use_system_font = 0;
1024
29208e82 1025 DEFVAR_LISP ("xft-settings", Vxft_settings,
67477f30 1026 doc: /* Font settings applied to Xft. */);
4a7edc24 1027 Vxft_settings = empty_unibyte_string;
67477f30 1028
0d1d0d26
JD
1029#ifdef HAVE_XFT
1030 Fprovide (intern_c_string ("font-render-setting"), Qnil);
9851bfc5 1031#if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
637fa988
JD
1032 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1033#endif
637fa988 1034#endif
f904c0f9
JD
1035
1036 current_tool_bar_style = Qnil;
cd3520a4 1037 DEFSYM (Qtool_bar_style, "tool-bar-style");
f904c0f9
JD
1038 defsubr (&Stool_bar_get_system_style);
1039
1040 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
637fa988 1041}