Merge from emacs-24; up to 2012-12-23T02:41:17Z!rgm@gnu.org
[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,
173 SEEN_TB_STYLE = 0x80,
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 SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
340 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
341#define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
342#define PAD(nr) (((nr) + 3) & ~3)
343
344/* Parse xsettings and extract those that deal with Xft.
345 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
346 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
347
348 Layout of prop. First is a header:
349
350 bytes type what
351 ------------------------------------
352 1 CARD8 byte-order
353 3 unused
354 4 CARD32 SERIAL
355 4 CARD32 N_SETTINGS
356
357 Then N_SETTINGS records, with header:
358
359 bytes type what
360 ------------------------------------
361 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
362 1 unused
363 2 CARD16 n == name-length
364 n STRING8 name
365 p unused, p=pad_to_even_4(n)
366 4 CARD32 last-change-serial
367
368 and then the value, For string:
af59aa6e 369
637fa988
JD
370 bytes type what
371 ------------------------------------
372 4 CARD32 n = value-length
373 n STRING8 value
374 p unused, p=pad_to_even_4(n)
375
376 For integer:
377
378 bytes type what
379 ------------------------------------
380 4 INT32 value
381
382 For RGB color:
383
384 bytes type what
385 ------------------------------------
386 2 CARD16 red
387 2 CARD16 blue
388 2 CARD16 green
389 2 CARD16 alpha
390
3c055b77 391 Returns non-zero if some Xft settings was seen, zero otherwise.
637fa988
JD
392*/
393
394static int
e4c8d29a
J
395parse_settings (unsigned char *prop,
396 long unsigned int bytes,
397 struct xsettings *settings)
637fa988
JD
398{
399 Lisp_Object byteorder = Fbyteorder ();
400 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
401 int that_bo = prop[0];
402 CARD32 n_settings;
403 int bytes_parsed = 0;
404 int settings_seen = 0;
405 int i = 0;
406
407 /* First 4 bytes is a serial number, skip that. */
408
409 if (bytes < 12) return BadLength;
410 memcpy (&n_settings, prop+8, 4);
411 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
412 bytes_parsed = 12;
413
414 memset (settings, 0, sizeof (*settings));
415
99852628 416 while (bytes_parsed+4 < bytes && settings_seen < 7
637fa988
JD
417 && i < n_settings)
418 {
419 int type = prop[bytes_parsed++];
420 CARD16 nlen;
421 CARD32 vlen, ival = 0;
422 char name[128]; /* The names we are looking for are not this long. */
423 char sval[128]; /* The values we are looking for are not this long. */
99852628 424 int want_this;
637fa988
JD
425 int to_cpy;
426
427 sval[0] = '\0';
428 ++i;
429 ++bytes_parsed; /* Padding */
430
431 memcpy (&nlen, prop+bytes_parsed, 2);
432 bytes_parsed += 2;
433 if (my_bo != that_bo) nlen = SWAP16 (nlen);
434 if (bytes_parsed+nlen > bytes) return BadLength;
435 to_cpy = nlen > 127 ? 127 : nlen;
436 memcpy (name, prop+bytes_parsed, to_cpy);
437 name[to_cpy] = '\0';
438
439 bytes_parsed += nlen;
440 bytes_parsed = PAD (bytes_parsed);
441
442 bytes_parsed += 4; /* Skip serial for this value */
443 if (bytes_parsed > bytes) return BadLength;
444
490011a6 445 want_this =
f904c0f9
JD
446#ifdef HAVE_XFT
447 (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
869795d6 448 || strcmp (XSETTINGS_FONT_NAME, name) == 0
f904c0f9
JD
449 ||
450#endif
869795d6 451 strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
490011a6 452
af59aa6e 453 switch (type)
637fa988
JD
454 {
455 case 0: /* Integer */
456 if (bytes_parsed+4 > bytes) return BadLength;
99852628 457 if (want_this)
637fa988
JD
458 {
459 memcpy (&ival, prop+bytes_parsed, 4);
460 if (my_bo != that_bo) ival = SWAP32 (ival);
461 }
462 bytes_parsed += 4;
463 break;
464
465 case 1: /* String */
466 if (bytes_parsed+4 > bytes) return BadLength;
467 memcpy (&vlen, prop+bytes_parsed, 4);
468 bytes_parsed += 4;
469 if (my_bo != that_bo) vlen = SWAP32 (vlen);
99852628 470 if (want_this)
637fa988
JD
471 {
472 to_cpy = vlen > 127 ? 127 : vlen;
473 memcpy (sval, prop+bytes_parsed, to_cpy);
474 sval[to_cpy] = '\0';
475 }
476 bytes_parsed += vlen;
477 bytes_parsed = PAD (bytes_parsed);
478 break;
479
480 case 2: /* RGB value */
481 /* No need to parse this */
482 if (bytes_parsed+8 > bytes) return BadLength;
af59aa6e 483 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
637fa988
JD
484 break;
485
486 default: /* Parse Error */
487 return BadValue;
488 }
489
af59aa6e 490 if (want_this)
637fa988
JD
491 {
492 ++settings_seen;
869795d6 493 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
f904c0f9
JD
494 {
495 settings->tb_style = xstrdup (sval);
496 settings->seen |= SEEN_TB_STYLE;
497 }
498#ifdef HAVE_XFT
869795d6
JD
499 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
500 {
501 settings->font = xstrdup (sval);
502 settings->seen |= SEEN_FONT;
503 }
f904c0f9 504 else if (strcmp (name, "Xft/Antialias") == 0)
3c055b77
JD
505 {
506 settings->seen |= SEEN_AA;
507 settings->aa = ival != 0;
508 }
637fa988 509 else if (strcmp (name, "Xft/Hinting") == 0)
3c055b77
JD
510 {
511 settings->seen |= SEEN_HINTING;
512 settings->hinting = ival != 0;
513 }
af59aa6e 514# ifdef FC_HINT_STYLE
637fa988
JD
515 else if (strcmp (name, "Xft/HintStyle") == 0)
516 {
3c055b77 517 settings->seen |= SEEN_HINTSTYLE;
637fa988
JD
518 if (strcmp (sval, "hintnone") == 0)
519 settings->hintstyle = FC_HINT_NONE;
520 else if (strcmp (sval, "hintslight") == 0)
521 settings->hintstyle = FC_HINT_SLIGHT;
522 else if (strcmp (sval, "hintmedium") == 0)
523 settings->hintstyle = FC_HINT_MEDIUM;
524 else if (strcmp (sval, "hintfull") == 0)
525 settings->hintstyle = FC_HINT_FULL;
3c055b77
JD
526 else
527 settings->seen &= ~SEEN_HINTSTYLE;
637fa988 528 }
af59aa6e 529# endif
637fa988
JD
530 else if (strcmp (name, "Xft/RGBA") == 0)
531 {
3c055b77 532 settings->seen |= SEEN_RGBA;
637fa988
JD
533 if (strcmp (sval, "none") == 0)
534 settings->rgba = FC_RGBA_NONE;
535 else if (strcmp (sval, "rgb") == 0)
536 settings->rgba = FC_RGBA_RGB;
537 else if (strcmp (sval, "bgr") == 0)
538 settings->rgba = FC_RGBA_BGR;
539 else if (strcmp (sval, "vrgb") == 0)
540 settings->rgba = FC_RGBA_VRGB;
541 else if (strcmp (sval, "vbgr") == 0)
542 settings->rgba = FC_RGBA_VBGR;
3c055b77
JD
543 else
544 settings->seen &= ~SEEN_RGBA;
637fa988
JD
545 }
546 else if (strcmp (name, "Xft/DPI") == 0)
3c055b77
JD
547 {
548 settings->seen |= SEEN_DPI;
549 settings->dpi = (double)ival/1024.0;
550 }
637fa988
JD
551 else if (strcmp (name, "Xft/lcdfilter") == 0)
552 {
3c055b77 553 settings->seen |= SEEN_LCDFILTER;
637fa988
JD
554 if (strcmp (sval, "none") == 0)
555 settings->lcdfilter = FC_LCD_NONE;
556 else if (strcmp (sval, "lcddefault") == 0)
557 settings->lcdfilter = FC_LCD_DEFAULT;
3c055b77
JD
558 else
559 settings->seen &= ~SEEN_LCDFILTER;
637fa988 560 }
f904c0f9 561#endif /* HAVE_XFT */
637fa988
JD
562 }
563 }
564
3c055b77 565 return settings_seen;
637fa988
JD
566}
567
869795d6
JD
568/* Read settings from the XSettings property window on display for DPYINFO.
569 Store settings read in SETTINGS.
570 Return non-zero if successful, zero if not. */
571
637fa988 572static int
971de7fb 573read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
637fa988 574{
637fa988
JD
575 Atom act_type;
576 int act_form;
577 unsigned long nitems, bytes_after;
578 unsigned char *prop = NULL;
579 Display *dpy = dpyinfo->display;
580 int rc;
581
582 x_catch_errors (dpy);
583 rc = XGetWindowProperty (dpy,
584 dpyinfo->xsettings_window,
585 dpyinfo->Xatom_xsettings_prop,
586 0, LONG_MAX, False, AnyPropertyType,
587 &act_type, &act_form, &nitems, &bytes_after,
588 &prop);
589
590 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
591 && act_type == dpyinfo->Xatom_xsettings_prop)
f904c0f9 592 rc = parse_settings (prop, nitems, settings);
637fa988
JD
593
594 XFree (prop);
595
596 x_uncatch_errors ();
597
3c055b77 598 return rc != 0;
637fa988
JD
599}
600
869795d6
JD
601/* Apply Xft settings in SETTINGS to the Xft library.
602 If SEND_EVENT_P is non-zero store a Lisp event that Xft settings changed. */
581e51e8 603
637fa988 604static void
e4c8d29a
J
605apply_xft_settings (struct x_display_info *dpyinfo,
606 int send_event_p,
607 struct xsettings *settings)
637fa988 608{
f904c0f9 609#ifdef HAVE_XFT
637fa988 610 FcPattern *pat;
f904c0f9 611 struct xsettings oldsettings;
637fa988
JD
612 int changed = 0;
613
637fa988 614 memset (&oldsettings, 0, sizeof (oldsettings));
637fa988
JD
615 pat = FcPatternCreate ();
616 XftDefaultSubstitute (dpyinfo->display,
617 XScreenNumberOfScreen (dpyinfo->screen),
618 pat);
619 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
620 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
869795d6 621#ifdef FC_HINT_STYLE
637fa988 622 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
869795d6 623#endif
637fa988
JD
624 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
625 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
626 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
627
f904c0f9 628 if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
637fa988
JD
629 {
630 FcPatternDel (pat, FC_ANTIALIAS);
f904c0f9 631 FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
637fa988 632 ++changed;
f904c0f9 633 oldsettings.aa = settings->aa;
637fa988 634 }
67477f30 635
f904c0f9
JD
636 if ((settings->seen & SEEN_HINTING) != 0
637 && oldsettings.hinting != settings->hinting)
637fa988
JD
638 {
639 FcPatternDel (pat, FC_HINTING);
f904c0f9 640 FcPatternAddBool (pat, FC_HINTING, settings->hinting);
637fa988 641 ++changed;
f904c0f9 642 oldsettings.hinting = settings->hinting;
637fa988 643 }
f904c0f9 644 if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
637fa988
JD
645 {
646 FcPatternDel (pat, FC_RGBA);
f904c0f9
JD
647 FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
648 oldsettings.rgba = settings->rgba;
637fa988
JD
649 ++changed;
650 }
67477f30 651
a6eb20d8 652 /* Older fontconfig versions don't have FC_LCD_FILTER. */
f904c0f9
JD
653 if ((settings->seen & SEEN_LCDFILTER) != 0
654 && oldsettings.lcdfilter != settings->lcdfilter)
637fa988
JD
655 {
656 FcPatternDel (pat, FC_LCD_FILTER);
f904c0f9 657 FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
637fa988 658 ++changed;
f904c0f9 659 oldsettings.lcdfilter = settings->lcdfilter;
637fa988 660 }
67477f30 661
869795d6 662#ifdef FC_HINT_STYLE
f904c0f9
JD
663 if ((settings->seen & SEEN_HINTSTYLE) != 0
664 && oldsettings.hintstyle != settings->hintstyle)
637fa988
JD
665 {
666 FcPatternDel (pat, FC_HINT_STYLE);
f904c0f9 667 FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
637fa988 668 ++changed;
f904c0f9 669 oldsettings.hintstyle = settings->hintstyle;
637fa988 670 }
869795d6 671#endif
67477f30 672
f904c0f9
JD
673 if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi
674 && settings->dpi > 0)
637fa988
JD
675 {
676 Lisp_Object frame, tail;
677
678 FcPatternDel (pat, FC_DPI);
f904c0f9 679 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
637fa988 680 ++changed;
f904c0f9 681 oldsettings.dpi = settings->dpi;
af59aa6e 682
637fa988 683 /* Change the DPI on this display and all frames on the display. */
f904c0f9 684 dpyinfo->resy = dpyinfo->resx = settings->dpi;
637fa988
JD
685 FOR_EACH_FRAME (tail, frame)
686 if (FRAME_X_P (XFRAME (frame))
687 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
f904c0f9 688 XFRAME (frame)->resy = XFRAME (frame)->resx = settings->dpi;
637fa988
JD
689 }
690
691 if (changed)
692 {
aaafe47a
PE
693 static char const format[] =
694 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
695 "Hintstyle: %d, DPI: %lf";
696 enum
697 {
698 d_formats = 5,
699 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
700 lf_formats = 1,
701 max_f_integer_digits = DBL_MAX_10_EXP + 1,
702 f_precision = 6,
703 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
704 - sizeof "%lf")
705 };
706 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
707
637fa988
JD
708 XftDefaultSet (dpyinfo->display, pat);
709 if (send_event_p)
f904c0f9
JD
710 store_config_changed_event (Qfont_render,
711 XCAR (dpyinfo->name_list_element));
0328b6de 712 Vxft_settings
a8290ec3
DA
713 = make_formatted_string (buf, format,
714 oldsettings.aa, oldsettings.hinting,
715 oldsettings.rgba, oldsettings.lcdfilter,
716 oldsettings.hintstyle, oldsettings.dpi);
0328b6de 717
637fa988
JD
718 }
719 else
720 FcPatternDestroy (pat);
f904c0f9 721#endif /* HAVE_XFT */
637fa988
JD
722}
723
869795d6
JD
724/* Read XSettings from the display for DPYINFO.
725 If SEND_EVENT_P is non-zero store a Lisp event settings that changed. */
726
f904c0f9 727static void
971de7fb 728read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
f904c0f9
JD
729{
730 struct xsettings settings;
f904c0f9
JD
731
732 if (!read_settings (dpyinfo, &settings))
733 return;
734
735 apply_xft_settings (dpyinfo, True, &settings);
736 if (settings.seen & SEEN_TB_STYLE)
737 {
869795d6
JD
738 if (send_event_p)
739 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
740 else
741 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
baad03f0 742 xfree (settings.tb_style);
f904c0f9 743 }
869795d6 744#ifdef HAVE_XFT
f904c0f9
JD
745 if (settings.seen & SEEN_FONT)
746 {
869795d6
JD
747 if (send_event_p)
748 store_font_name_changed (settings.font);
749 else
f904c0f9 750 {
baad03f0 751 xfree (current_font);
869795d6 752 current_font = xstrdup (settings.font);
f904c0f9 753 }
869795d6 754 xfree (settings.font);
f904c0f9 755 }
869795d6 756#endif
f904c0f9 757}
637fa988 758
869795d6
JD
759/* Check if EVENT for the display in DPYINFO is XSettings related. */
760
637fa988 761void
971de7fb 762xft_settings_event (struct x_display_info *dpyinfo, XEvent *event)
637fa988 763{
637fa988 764 int check_window_p = 0;
f904c0f9 765 int apply_settings = 0;
637fa988
JD
766
767 switch (event->type)
768 {
769 case DestroyNotify:
770 if (dpyinfo->xsettings_window == event->xany.window)
771 check_window_p = 1;
772 break;
773
774 case ClientMessage:
775 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
776 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
777 && event->xclient.window == dpyinfo->root_window)
778 check_window_p = 1;
779 break;
780
781 case PropertyNotify:
782 if (event->xproperty.window == dpyinfo->xsettings_window
783 && event->xproperty.state == PropertyNewValue
784 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
f904c0f9 785 apply_settings = 1;
637fa988
JD
786 break;
787 }
788
f904c0f9 789
637fa988
JD
790 if (check_window_p)
791 {
792 dpyinfo->xsettings_window = None;
793 get_prop_window (dpyinfo);
794 if (dpyinfo->xsettings_window != None)
f904c0f9 795 apply_settings = 1;
637fa988 796 }
f904c0f9
JD
797
798 if (apply_settings)
799 read_and_apply_settings (dpyinfo, True);
637fa988
JD
800}
801
869795d6 802/* Initialize GSettings and read startup values. */
637fa988 803
9851bfc5
JD
804static void
805init_gsettings (void)
806{
807#ifdef HAVE_GSETTINGS
808 GVariant *val;
0949d2b6
JD
809 const gchar *const *schemas;
810 int schema_found = 0;
811
9851bfc5
JD
812#ifdef HAVE_G_TYPE_INIT
813 g_type_init ();
814#endif
815
5e617bc2 816 schemas = g_settings_list_schemas ();
0949d2b6
JD
817 if (schemas == NULL) return;
818 while (! schema_found && *schemas != NULL)
819 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
820 if (!schema_found) return;
821
9851bfc5
JD
822 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
823 if (!gsettings_client) return;
824 g_object_ref_sink (G_OBJECT (gsettings_client));
869795d6
JD
825 g_signal_connect (G_OBJECT (gsettings_client), "changed",
826 G_CALLBACK (something_changed_gsettingsCB), NULL);
827
828 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
829 if (val)
830 {
831 g_variant_ref_sink (val);
832 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
833 current_tool_bar_style
834 = map_tool_bar_style (g_variant_get_string (val, NULL));
835 g_variant_unref (val);
836 }
9851bfc5 837
869795d6
JD
838#ifdef HAVE_XFT
839 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
2e13213d 840 if (val)
9851bfc5
JD
841 {
842 g_variant_ref_sink (val);
843 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
844 current_mono_font = xstrdup (g_variant_get_string (val, NULL));
845 g_variant_unref (val);
846 }
847
869795d6
JD
848 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
849 if (val)
850 {
851 g_variant_ref_sink (val);
852 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
853 current_font = xstrdup (g_variant_get_string (val, NULL));
854 g_variant_unref (val);
855 }
856#endif /* HAVE_XFT */
857
9851bfc5
JD
858#endif /* HAVE_GSETTINGS */
859}
860
869795d6 861/* Init GConf and read startup values. */
9851bfc5 862
637fa988 863static void
971de7fb 864init_gconf (void)
637fa988 865{
869795d6 866#if defined (HAVE_GCONF)
637fa988 867 char *s;
637fa988 868
1e600395 869#ifdef HAVE_G_TYPE_INIT
637fa988 870 g_type_init ();
1e600395 871#endif
869795d6 872
637fa988 873 gconf_client = gconf_client_get_default ();
869795d6
JD
874 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
875 gconf_client_add_dir (gconf_client,
876 GCONF_TOOL_BAR_STYLE,
877 GCONF_CLIENT_PRELOAD_ONELEVEL,
878 NULL);
879 gconf_client_notify_add (gconf_client,
880 GCONF_TOOL_BAR_STYLE,
881 something_changed_gconfCB,
882 NULL, NULL, NULL);
883
884 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
885 if (s)
886 {
887 current_tool_bar_style = map_tool_bar_style (s);
888 g_free (s);
889 }
890
891#ifdef HAVE_XFT
892 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
637fa988
JD
893 if (s)
894 {
895 current_mono_font = xstrdup (s);
896 g_free (s);
897 }
869795d6 898 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
99852628
JD
899 if (s)
900 {
901 current_font = xstrdup (s);
902 g_free (s);
903 }
637fa988 904 gconf_client_add_dir (gconf_client,
869795d6 905 GCONF_MONO_FONT,
637fa988
JD
906 GCONF_CLIENT_PRELOAD_ONELEVEL,
907 NULL);
908 gconf_client_notify_add (gconf_client,
869795d6
JD
909 GCONF_MONO_FONT,
910 something_changed_gconfCB,
637fa988 911 NULL, NULL, NULL);
869795d6
JD
912 gconf_client_add_dir (gconf_client,
913 GCONF_FONT_NAME,
914 GCONF_CLIENT_PRELOAD_ONELEVEL,
915 NULL);
916 gconf_client_notify_add (gconf_client,
917 GCONF_FONT_NAME,
918 something_changed_gconfCB,
919 NULL, NULL, NULL);
920#endif /* HAVE_XFT */
921#endif /* HAVE_GCONF */
637fa988
JD
922}
923
869795d6
JD
924/* Init Xsettings and read startup values. */
925
637fa988 926static void
971de7fb 927init_xsettings (struct x_display_info *dpyinfo)
637fa988 928{
637fa988
JD
929 Display *dpy = dpyinfo->display;
930
4d7e6e51 931 block_input ();
637fa988 932
637fa988
JD
933 /* Select events so we can detect client messages sent when selection
934 owner changes. */
935 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
936
937 get_prop_window (dpyinfo);
938 if (dpyinfo->xsettings_window != None)
f904c0f9 939 read_and_apply_settings (dpyinfo, False);
637fa988 940
4d7e6e51 941 unblock_input ();
637fa988
JD
942}
943
944void
971de7fb 945xsettings_initialize (struct x_display_info *dpyinfo)
637fa988
JD
946{
947 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
948 init_gconf ();
f904c0f9 949 init_xsettings (dpyinfo);
869795d6 950 init_gsettings ();
637fa988
JD
951}
952
869795d6
JD
953/* Return the system monospaced font.
954 May be NULL if not known. */
955
0d1d0d26 956const char *
971de7fb 957xsettings_get_system_font (void)
0d1d0d26
JD
958{
959 return current_mono_font;
960}
637fa988 961
e87b6180 962#ifdef USE_LUCID
869795d6
JD
963/* Return the system font.
964 May be NULL if not known. */
965
99852628 966const char *
971de7fb 967xsettings_get_system_normal_font (void)
99852628
JD
968{
969 return current_font;
970}
e87b6180 971#endif
99852628
JD
972
973DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
974 Sfont_get_system_normal_font,
975 0, 0, 0,
bf935339 976 doc: /* Get the system default application font. */)
5842a27b 977 (void)
99852628 978{
2674ddc8 979 return current_font ? build_string (current_font) : Qnil;
99852628
JD
980}
981
637fa988
JD
982DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
983 0, 0, 0,
bf935339 984 doc: /* Get the system default fixed width font. */)
5842a27b 985 (void)
637fa988 986{
2674ddc8 987 return current_mono_font ? build_string (current_mono_font) : Qnil;
637fa988
JD
988}
989
a7ca3326 990DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
16a97296 991 Stool_bar_get_system_style, 0, 0, 0,
f904c0f9 992 doc: /* Get the system tool bar style.
4721152c 993If no system tool bar style is known, return `tool-bar-style' if set to a
f904c0f9 994known style. Otherwise return image. */)
5842a27b 995 (void)
f904c0f9
JD
996{
997 if (EQ (Vtool_bar_style, Qimage)
998 || EQ (Vtool_bar_style, Qtext)
999 || EQ (Vtool_bar_style, Qboth)
8a52f00a
JD
1000 || EQ (Vtool_bar_style, Qboth_horiz)
1001 || EQ (Vtool_bar_style, Qtext_image_horiz))
f904c0f9
JD
1002 return Vtool_bar_style;
1003 if (!NILP (current_tool_bar_style))
1004 return current_tool_bar_style;
1005 return Qimage;
1006}
1007
637fa988 1008void
971de7fb 1009syms_of_xsettings (void)
637fa988
JD
1010{
1011 current_mono_font = NULL;
99852628 1012 current_font = NULL;
637fa988 1013 first_dpyinfo = NULL;
9851bfc5
JD
1014#ifdef HAVE_GSETTINGS
1015 gsettings_client = NULL;
869795d6 1016#endif
637fa988
JD
1017#ifdef HAVE_GCONF
1018 gconf_client = NULL;
1019#endif
1020
cd3520a4
JB
1021 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1022 DEFSYM (Qfont_name, "font-name");
1023 DEFSYM (Qfont_render, "font-render");
637fa988 1024 defsubr (&Sfont_get_system_font);
99852628 1025 defsubr (&Sfont_get_system_normal_font);
637fa988 1026
29208e82 1027 DEFVAR_BOOL ("font-use-system-font", use_system_font,
fb7ada5f 1028 doc: /* Non-nil means to apply the system defined font dynamically.
bf935339
J
1029When this is non-nil and the system defined fixed width font changes, we
1030update frames dynamically.
1031If this variable is nil, Emacs ignores system font changes. */);
dfb3c4c6
JD
1032 use_system_font = 0;
1033
29208e82 1034 DEFVAR_LISP ("xft-settings", Vxft_settings,
67477f30 1035 doc: /* Font settings applied to Xft. */);
4a7edc24 1036 Vxft_settings = empty_unibyte_string;
67477f30 1037
0d1d0d26
JD
1038#ifdef HAVE_XFT
1039 Fprovide (intern_c_string ("font-render-setting"), Qnil);
9851bfc5 1040#if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
637fa988
JD
1041 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1042#endif
637fa988 1043#endif
f904c0f9
JD
1044
1045 current_tool_bar_style = Qnil;
cd3520a4 1046 DEFSYM (Qtool_bar_style, "tool-bar-style");
f904c0f9
JD
1047 defsubr (&Stool_bar_get_system_style);
1048
1049 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
637fa988 1050}