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