* lisp/term/xterm.el (xterm--version-handler): Adapt to xterm-280's output.
[bpt/emacs.git] / src / xsettings.c
CommitLineData
4ff155dd
GM
1/* Functions for handling font and other changes dynamically.
2
ba318903 3Copyright (C) 2009-2014 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 24#include <fcntl.h>
4eed3157
PE
25
26#include <byteswap.h>
27
637fa988
JD
28#include "lisp.h"
29#include "xterm.h"
30#include "xsettings.h"
31#include "frame.h"
b87dd913 32#include "keyboard.h"
637fa988
JD
33#include "blockinput.h"
34#include "termhooks.h"
637fa988
JD
35
36#include <X11/Xproto.h>
37
9851bfc5 38#ifdef HAVE_GSETTINGS
51bb811f
JD
39#include <glib-object.h>
40#include <gio/gio.h>
869795d6
JD
41#endif
42
637fa988
JD
43#ifdef HAVE_GCONF
44#include <gconf/gconf-client.h>
45#endif
9851bfc5 46
637fa988
JD
47#ifdef HAVE_XFT
48#include <X11/Xft/Xft.h>
49#endif
50
51static char *current_mono_font;
99852628 52static char *current_font;
637fa988 53static struct x_display_info *first_dpyinfo;
f904c0f9
JD
54static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render,
55 Qtool_bar_style;
f904c0f9 56static Lisp_Object current_tool_bar_style;
637fa988 57
869795d6
JD
58/* Store an config changed event in to the event queue. */
59
637fa988 60static void
971de7fb 61store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
637fa988
JD
62{
63 struct input_event event;
64 EVENT_INIT (event);
65 event.kind = CONFIG_CHANGED_EVENT;
66 event.frame_or_window = display_name;
67 event.arg = arg;
68 kbd_buffer_store_event (&event);
69}
70
869795d6
JD
71/* Return non-zero if DPYINFO is still valid. */
72static int
73dpyinfo_valid (struct x_display_info *dpyinfo)
74{
75 int found = 0;
76 if (dpyinfo != NULL)
77 {
78 struct x_display_info *d;
79 for (d = x_display_list; !found && d; d = d->next)
80 found = d == dpyinfo && d->display == dpyinfo->display;
81 }
82 return found;
83}
84
85/* Store a monospace font change event if the monospaced font changed. */
86
490011a6 87#if defined HAVE_XFT && (defined HAVE_GSETTINGS || defined HAVE_GCONF)
9851bfc5 88static void
0949d2b6 89store_monospaced_changed (const char *newfont)
9851bfc5 90{
0949d2b6
JD
91 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
92 return; /* No change. */
93
94 xfree (current_mono_font);
95 current_mono_font = xstrdup (newfont);
96
869795d6 97 if (dpyinfo_valid (first_dpyinfo) && use_system_font)
9851bfc5 98 {
869795d6
JD
99 store_config_changed_event (Qmonospace_font_name,
100 XCAR (first_dpyinfo->name_list_element));
9851bfc5
JD
101 }
102}
490011a6 103#endif
9851bfc5 104
869795d6 105/* Store a font name change event if the font name changed. */
9851bfc5 106
490011a6 107#ifdef HAVE_XFT
869795d6
JD
108static void
109store_font_name_changed (const char *newfont)
110{
111 if (current_font != NULL && strcmp (newfont, current_font) == 0)
112 return; /* No change. */
113
114 xfree (current_font);
115 current_font = xstrdup (newfont);
116
117 if (dpyinfo_valid (first_dpyinfo))
118 {
119 store_config_changed_event (Qfont_name,
120 XCAR (first_dpyinfo->name_list_element));
121 }
122}
123#endif /* HAVE_XFT */
124
da6062e6 125/* Map TOOL_BAR_STYLE from a string to its corresponding Lisp value.
869795d6
JD
126 Return Qnil if TOOL_BAR_STYLE is not known. */
127
128static Lisp_Object
129map_tool_bar_style (const char *tool_bar_style)
130{
131 Lisp_Object style = Qnil;
132 if (tool_bar_style)
133 {
134 if (strcmp (tool_bar_style, "both") == 0)
135 style = Qboth;
136 else if (strcmp (tool_bar_style, "both-horiz") == 0)
137 style = Qboth_horiz;
138 else if (strcmp (tool_bar_style, "icons") == 0)
139 style = Qimage;
140 else if (strcmp (tool_bar_style, "text") == 0)
141 style = Qtext;
142 }
143
144 return style;
145}
146
147/* Store a tool bar style change event if the tool bar style changed. */
148
149static void
150store_tool_bar_style_changed (const char *newstyle,
151 struct x_display_info *dpyinfo)
152{
153 Lisp_Object style = map_tool_bar_style (newstyle);
154 if (EQ (current_tool_bar_style, style))
155 return; /* No change. */
156
157 current_tool_bar_style = style;
158 if (dpyinfo_valid (dpyinfo))
159 store_config_changed_event (Qtool_bar_style,
160 XCAR (dpyinfo->name_list_element));
161}
9851bfc5 162
2014308a 163#ifdef HAVE_XFT
f904c0f9 164#define XSETTINGS_FONT_NAME "Gtk/FontName"
2014308a 165#endif
f904c0f9 166#define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
0269ef77 167
f904c0f9
JD
168enum {
169 SEEN_AA = 0x01,
170 SEEN_HINTING = 0x02,
171 SEEN_RGBA = 0x04,
172 SEEN_LCDFILTER = 0x08,
173 SEEN_HINTSTYLE = 0x10,
174 SEEN_DPI = 0x20,
175 SEEN_FONT = 0x40,
29abe551 176 SEEN_TB_STYLE = 0x80
f904c0f9 177};
af59aa6e 178struct xsettings
f904c0f9
JD
179{
180#ifdef HAVE_XFT
181 FcBool aa, hinting;
182 int rgba, lcdfilter, hintstyle;
183 double dpi;
f904c0f9
JD
184
185 char *font;
869795d6
JD
186#endif
187
f904c0f9
JD
188 char *tb_style;
189
190 unsigned seen;
191};
192
9851bfc5 193#ifdef HAVE_GSETTINGS
869795d6
JD
194#define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
195#define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"
196
197#ifdef HAVE_XFT
198#define GSETTINGS_MONO_FONT "monospace-font-name"
199#define GSETTINGS_FONT_NAME "font-name"
200#endif
201
202
203/* The single GSettings instance, or NULL if not connected to GSettings. */
204
205static GSettings *gsettings_client;
206
207/* Callback called when something changed in GSettings. */
31a01b90 208
0949d2b6 209static void
869795d6
JD
210something_changed_gsettingsCB (GSettings *settings,
211 gchar *key,
212 gpointer user_data)
0949d2b6
JD
213{
214 GVariant *val;
869795d6
JD
215
216 if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
0949d2b6 217 {
869795d6
JD
218 val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
219 if (val)
0949d2b6 220 {
869795d6
JD
221 g_variant_ref_sink (val);
222 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
223 {
224 const gchar *newstyle = g_variant_get_string (val, NULL);
225 store_tool_bar_style_changed (newstyle, first_dpyinfo);
226 }
227 g_variant_unref (val);
228 }
229 }
230#ifdef HAVE_XFT
231 else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
232 {
233 val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
234 if (val)
235 {
236 g_variant_ref_sink (val);
237 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
238 {
239 const gchar *newfont = g_variant_get_string (val, NULL);
240 store_monospaced_changed (newfont);
241 }
242 g_variant_unref (val);
0949d2b6 243 }
0949d2b6 244 }
869795d6
JD
245 else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
246 {
247 val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
248 if (val)
249 {
250 g_variant_ref_sink (val);
251 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
252 {
253 const gchar *newfont = g_variant_get_string (val, NULL);
254 store_font_name_changed (newfont);
255 }
256 g_variant_unref (val);
257 }
258 }
490011a6 259#endif /* HAVE_XFT */
0949d2b6
JD
260}
261
869795d6 262#endif /* HAVE_GSETTINGS */
2e13213d 263
869795d6
JD
264#ifdef HAVE_GCONF
265#define GCONF_TOOL_BAR_STYLE "/desktop/gnome/interface/toolbar_style"
2e13213d 266#ifdef HAVE_XFT
869795d6
JD
267#define GCONF_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
268#define GCONF_FONT_NAME "/desktop/gnome/interface/font_name"
2e13213d 269#endif
31a01b90 270
869795d6
JD
271/* The single GConf instance, or NULL if not connected to GConf. */
272
273static GConfClient *gconf_client;
274
275/* Callback called when something changed in GConf that we care about. */
637fa988
JD
276
277static void
869795d6
JD
278something_changed_gconfCB (GConfClient *client,
279 guint cnxn_id,
280 GConfEntry *entry,
281 gpointer user_data)
637fa988
JD
282{
283 GConfValue *v = gconf_entry_get_value (entry);
869795d6 284 const char *key = gconf_entry_get_key (entry);
af59aa6e 285
869795d6
JD
286 if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
287 if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
288 {
289 const char *value = gconf_value_get_string (v);
290 store_tool_bar_style_changed (value, first_dpyinfo);
291 }
292#ifdef HAVE_XFT
293 else if (strcmp (key, GCONF_MONO_FONT) == 0)
637fa988
JD
294 {
295 const char *value = gconf_value_get_string (v);
0949d2b6 296 store_monospaced_changed (value);
637fa988 297 }
869795d6
JD
298 else if (strcmp (key, GCONF_FONT_NAME) == 0)
299 {
300 const char *value = gconf_value_get_string (v);
301 store_font_name_changed (value);
302 }
303#endif /* HAVE_XFT */
637fa988 304}
9851bfc5 305
637fa988
JD
306#endif /* HAVE_GCONF */
307
308#ifdef HAVE_XFT
309
21050de1
JD
310/* Older fontconfig versions don't have FC_LCD_*. */
311#ifndef FC_LCD_NONE
312#define FC_LCD_NONE 0
313#endif
314#ifndef FC_LCD_DEFAULT
315#define FC_LCD_DEFAULT 1
316#endif
317#ifndef FC_LCD_FILTER
318#define FC_LCD_FILTER "lcdfilter"
319#endif
320
f904c0f9
JD
321#endif /* HAVE_XFT */
322
637fa988
JD
323/* Find the window that contains the XSETTINGS property values. */
324
325static void
971de7fb 326get_prop_window (struct x_display_info *dpyinfo)
637fa988
JD
327{
328 Display *dpy = dpyinfo->display;
329
330 XGrabServer (dpy);
331 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
332 dpyinfo->Xatom_xsettings_sel);
333 if (dpyinfo->xsettings_window != None)
334 /* Select events so we can detect if window is deleted or if settings
335 are changed. */
336 XSelectInput (dpy, dpyinfo->xsettings_window,
337 PropertyChangeMask|StructureNotifyMask);
338
339 XUngrabServer (dpy);
340}
341
637fa988
JD
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 395parse_settings (unsigned char *prop,
2d9783e0 396 unsigned long bytes,
e4c8d29a 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);
4eed3157 411 if (my_bo != that_bo) n_settings = bswap_32 (n_settings);
637fa988
JD
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;
4eed3157 433 if (my_bo != that_bo) nlen = bswap_16 (nlen);
637fa988
JD
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);
4eed3157 460 if (my_bo != that_bo) ival = bswap_32 (ival);
637fa988
JD
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;
4eed3157 469 if (my_bo != that_bo) vlen = bswap_32 (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 675 {
637fa988 676 FcPatternDel (pat, FC_DPI);
f904c0f9 677 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
637fa988 678 ++changed;
f904c0f9 679 oldsettings.dpi = settings->dpi;
af59aa6e 680
42143acd
DA
681 /* Changing the DPI on this display affects all frames on it.
682 Check FRAME_RES_X and FRAME_RES_Y in frame.h to see how. */
f904c0f9 683 dpyinfo->resy = dpyinfo->resx = settings->dpi;
637fa988
JD
684 }
685
686 if (changed)
687 {
aaafe47a
PE
688 static char const format[] =
689 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
29abe551 690 "Hintstyle: %d, DPI: %f";
aaafe47a
PE
691 enum
692 {
693 d_formats = 5,
694 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
695 lf_formats = 1,
696 max_f_integer_digits = DBL_MAX_10_EXP + 1,
697 f_precision = 6,
698 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
29abe551 699 - sizeof "%f")
aaafe47a
PE
700 };
701 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
702
637fa988
JD
703 XftDefaultSet (dpyinfo->display, pat);
704 if (send_event_p)
f904c0f9
JD
705 store_config_changed_event (Qfont_render,
706 XCAR (dpyinfo->name_list_element));
0328b6de 707 Vxft_settings
a8290ec3
DA
708 = make_formatted_string (buf, format,
709 oldsettings.aa, oldsettings.hinting,
710 oldsettings.rgba, oldsettings.lcdfilter,
711 oldsettings.hintstyle, oldsettings.dpi);
0328b6de 712
637fa988
JD
713 }
714 else
715 FcPatternDestroy (pat);
f904c0f9 716#endif /* HAVE_XFT */
637fa988
JD
717}
718
869795d6
JD
719/* Read XSettings from the display for DPYINFO.
720 If SEND_EVENT_P is non-zero store a Lisp event settings that changed. */
721
f904c0f9 722static void
971de7fb 723read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
f904c0f9
JD
724{
725 struct xsettings settings;
f904c0f9
JD
726
727 if (!read_settings (dpyinfo, &settings))
728 return;
729
730 apply_xft_settings (dpyinfo, True, &settings);
731 if (settings.seen & SEEN_TB_STYLE)
732 {
869795d6
JD
733 if (send_event_p)
734 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
735 else
736 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
baad03f0 737 xfree (settings.tb_style);
f904c0f9 738 }
869795d6 739#ifdef HAVE_XFT
f904c0f9
JD
740 if (settings.seen & SEEN_FONT)
741 {
869795d6
JD
742 if (send_event_p)
743 store_font_name_changed (settings.font);
744 else
f904c0f9 745 {
baad03f0 746 xfree (current_font);
869795d6 747 current_font = xstrdup (settings.font);
f904c0f9 748 }
869795d6 749 xfree (settings.font);
f904c0f9 750 }
869795d6 751#endif
f904c0f9 752}
637fa988 753
869795d6
JD
754/* Check if EVENT for the display in DPYINFO is XSettings related. */
755
637fa988 756void
b7ad2f74 757xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
637fa988 758{
d7e6881a 759 bool check_window_p = 0, apply_settings_p = 0;
637fa988
JD
760
761 switch (event->type)
762 {
763 case DestroyNotify:
764 if (dpyinfo->xsettings_window == event->xany.window)
765 check_window_p = 1;
766 break;
767
768 case ClientMessage:
769 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
770 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
771 && event->xclient.window == dpyinfo->root_window)
772 check_window_p = 1;
773 break;
774
775 case PropertyNotify:
776 if (event->xproperty.window == dpyinfo->xsettings_window
777 && event->xproperty.state == PropertyNewValue
778 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
d7e6881a 779 apply_settings_p = 1;
637fa988
JD
780 break;
781 }
782
f904c0f9 783
637fa988
JD
784 if (check_window_p)
785 {
786 dpyinfo->xsettings_window = None;
787 get_prop_window (dpyinfo);
788 if (dpyinfo->xsettings_window != None)
d7e6881a 789 apply_settings_p = 1;
637fa988 790 }
f904c0f9 791
d7e6881a 792 if (apply_settings_p)
f904c0f9 793 read_and_apply_settings (dpyinfo, True);
637fa988
JD
794}
795
869795d6 796/* Initialize GSettings and read startup values. */
637fa988 797
9851bfc5
JD
798static void
799init_gsettings (void)
800{
801#ifdef HAVE_GSETTINGS
802 GVariant *val;
0949d2b6
JD
803 const gchar *const *schemas;
804 int schema_found = 0;
805
3f386383 806#if ! GLIB_CHECK_VERSION (2, 36, 0)
9851bfc5
JD
807 g_type_init ();
808#endif
809
5e617bc2 810 schemas = g_settings_list_schemas ();
0949d2b6
JD
811 if (schemas == NULL) return;
812 while (! schema_found && *schemas != NULL)
813 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
814 if (!schema_found) return;
815
9851bfc5
JD
816 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
817 if (!gsettings_client) return;
818 g_object_ref_sink (G_OBJECT (gsettings_client));
869795d6
JD
819 g_signal_connect (G_OBJECT (gsettings_client), "changed",
820 G_CALLBACK (something_changed_gsettingsCB), NULL);
821
822 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
823 if (val)
824 {
825 g_variant_ref_sink (val);
826 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
827 current_tool_bar_style
828 = map_tool_bar_style (g_variant_get_string (val, NULL));
829 g_variant_unref (val);
830 }
9851bfc5 831
869795d6
JD
832#ifdef HAVE_XFT
833 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
2e13213d 834 if (val)
9851bfc5
JD
835 {
836 g_variant_ref_sink (val);
837 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
838 current_mono_font = xstrdup (g_variant_get_string (val, NULL));
839 g_variant_unref (val);
840 }
841
869795d6
JD
842 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
843 if (val)
844 {
845 g_variant_ref_sink (val);
846 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
847 current_font = xstrdup (g_variant_get_string (val, NULL));
848 g_variant_unref (val);
849 }
850#endif /* HAVE_XFT */
851
9851bfc5
JD
852#endif /* HAVE_GSETTINGS */
853}
854
869795d6 855/* Init GConf and read startup values. */
9851bfc5 856
637fa988 857static void
971de7fb 858init_gconf (void)
637fa988 859{
869795d6 860#if defined (HAVE_GCONF)
637fa988 861 char *s;
637fa988 862
3f386383 863#if ! GLIB_CHECK_VERSION (2, 36, 0)
637fa988 864 g_type_init ();
1e600395 865#endif
869795d6 866
637fa988 867 gconf_client = gconf_client_get_default ();
869795d6
JD
868 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
869 gconf_client_add_dir (gconf_client,
870 GCONF_TOOL_BAR_STYLE,
871 GCONF_CLIENT_PRELOAD_ONELEVEL,
872 NULL);
873 gconf_client_notify_add (gconf_client,
874 GCONF_TOOL_BAR_STYLE,
875 something_changed_gconfCB,
876 NULL, NULL, NULL);
877
878 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
879 if (s)
880 {
881 current_tool_bar_style = map_tool_bar_style (s);
882 g_free (s);
883 }
884
885#ifdef HAVE_XFT
886 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
637fa988
JD
887 if (s)
888 {
889 current_mono_font = xstrdup (s);
890 g_free (s);
891 }
869795d6 892 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
99852628
JD
893 if (s)
894 {
895 current_font = xstrdup (s);
896 g_free (s);
897 }
637fa988 898 gconf_client_add_dir (gconf_client,
869795d6 899 GCONF_MONO_FONT,
637fa988
JD
900 GCONF_CLIENT_PRELOAD_ONELEVEL,
901 NULL);
902 gconf_client_notify_add (gconf_client,
869795d6
JD
903 GCONF_MONO_FONT,
904 something_changed_gconfCB,
637fa988 905 NULL, NULL, NULL);
869795d6
JD
906 gconf_client_add_dir (gconf_client,
907 GCONF_FONT_NAME,
908 GCONF_CLIENT_PRELOAD_ONELEVEL,
909 NULL);
910 gconf_client_notify_add (gconf_client,
911 GCONF_FONT_NAME,
912 something_changed_gconfCB,
913 NULL, NULL, NULL);
914#endif /* HAVE_XFT */
915#endif /* HAVE_GCONF */
637fa988
JD
916}
917
869795d6
JD
918/* Init Xsettings and read startup values. */
919
637fa988 920static void
971de7fb 921init_xsettings (struct x_display_info *dpyinfo)
637fa988 922{
637fa988
JD
923 Display *dpy = dpyinfo->display;
924
4d7e6e51 925 block_input ();
637fa988 926
637fa988
JD
927 /* Select events so we can detect client messages sent when selection
928 owner changes. */
929 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
930
931 get_prop_window (dpyinfo);
932 if (dpyinfo->xsettings_window != None)
f904c0f9 933 read_and_apply_settings (dpyinfo, False);
637fa988 934
4d7e6e51 935 unblock_input ();
637fa988
JD
936}
937
938void
971de7fb 939xsettings_initialize (struct x_display_info *dpyinfo)
637fa988
JD
940{
941 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
942 init_gconf ();
f904c0f9 943 init_xsettings (dpyinfo);
869795d6 944 init_gsettings ();
637fa988
JD
945}
946
869795d6
JD
947/* Return the system monospaced font.
948 May be NULL if not known. */
949
0d1d0d26 950const char *
971de7fb 951xsettings_get_system_font (void)
0d1d0d26
JD
952{
953 return current_mono_font;
954}
637fa988 955
e87b6180 956#ifdef USE_LUCID
869795d6
JD
957/* Return the system font.
958 May be NULL if not known. */
959
99852628 960const char *
971de7fb 961xsettings_get_system_normal_font (void)
99852628
JD
962{
963 return current_font;
964}
e87b6180 965#endif
99852628
JD
966
967DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
968 Sfont_get_system_normal_font,
969 0, 0, 0,
bf935339 970 doc: /* Get the system default application font. */)
5842a27b 971 (void)
99852628 972{
2674ddc8 973 return current_font ? build_string (current_font) : Qnil;
99852628
JD
974}
975
637fa988
JD
976DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
977 0, 0, 0,
bf935339 978 doc: /* Get the system default fixed width font. */)
5842a27b 979 (void)
637fa988 980{
2674ddc8 981 return current_mono_font ? build_string (current_mono_font) : Qnil;
637fa988
JD
982}
983
a7ca3326 984DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
16a97296 985 Stool_bar_get_system_style, 0, 0, 0,
f904c0f9 986 doc: /* Get the system tool bar style.
4721152c 987If no system tool bar style is known, return `tool-bar-style' if set to a
f904c0f9 988known style. Otherwise return image. */)
5842a27b 989 (void)
f904c0f9
JD
990{
991 if (EQ (Vtool_bar_style, Qimage)
992 || EQ (Vtool_bar_style, Qtext)
993 || EQ (Vtool_bar_style, Qboth)
8a52f00a
JD
994 || EQ (Vtool_bar_style, Qboth_horiz)
995 || EQ (Vtool_bar_style, Qtext_image_horiz))
f904c0f9
JD
996 return Vtool_bar_style;
997 if (!NILP (current_tool_bar_style))
998 return current_tool_bar_style;
999 return Qimage;
1000}
1001
637fa988 1002void
971de7fb 1003syms_of_xsettings (void)
637fa988
JD
1004{
1005 current_mono_font = NULL;
99852628 1006 current_font = NULL;
637fa988 1007 first_dpyinfo = NULL;
9851bfc5
JD
1008#ifdef HAVE_GSETTINGS
1009 gsettings_client = NULL;
869795d6 1010#endif
637fa988
JD
1011#ifdef HAVE_GCONF
1012 gconf_client = NULL;
1013#endif
1014
cd3520a4
JB
1015 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1016 DEFSYM (Qfont_name, "font-name");
1017 DEFSYM (Qfont_render, "font-render");
637fa988 1018 defsubr (&Sfont_get_system_font);
99852628 1019 defsubr (&Sfont_get_system_normal_font);
637fa988 1020
29208e82 1021 DEFVAR_BOOL ("font-use-system-font", use_system_font,
fb7ada5f 1022 doc: /* Non-nil means to apply the system defined font dynamically.
bf935339
J
1023When this is non-nil and the system defined fixed width font changes, we
1024update frames dynamically.
1025If this variable is nil, Emacs ignores system font changes. */);
dfb3c4c6
JD
1026 use_system_font = 0;
1027
29208e82 1028 DEFVAR_LISP ("xft-settings", Vxft_settings,
67477f30 1029 doc: /* Font settings applied to Xft. */);
4a7edc24 1030 Vxft_settings = empty_unibyte_string;
67477f30 1031
0d1d0d26
JD
1032#ifdef HAVE_XFT
1033 Fprovide (intern_c_string ("font-render-setting"), Qnil);
9851bfc5 1034#if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
637fa988
JD
1035 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1036#endif
637fa988 1037#endif
f904c0f9
JD
1038
1039 current_tool_bar_style = Qnil;
cd3520a4 1040 DEFSYM (Qtool_bar_style, "tool-bar-style");
f904c0f9
JD
1041 defsubr (&Stool_bar_get_system_style);
1042
1043 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
637fa988 1044}