; Typo fixes, mostly repeated words
[emacs.git] / src / xsettings.c
blobe35c61651cbc6588ad0dca9478b21e7a689d4ee9
1 /* Functions for handling font and other changes dynamically.
3 Copyright (C) 2009-2017 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or (at
10 your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
20 #include <config.h>
22 #include <float.h>
23 #include <limits.h>
24 #include <fcntl.h>
26 #include <byteswap.h>
28 #include "lisp.h"
29 #include "xterm.h"
30 #include "xsettings.h"
31 #include "frame.h"
32 #include "keyboard.h"
33 #include "blockinput.h"
34 #include "termhooks.h"
36 #include <X11/Xproto.h>
38 #ifdef HAVE_GSETTINGS
39 #include <glib-object.h>
40 #include <gio/gio.h>
41 #endif
43 #ifdef HAVE_GCONF
44 #include <gconf/gconf-client.h>
45 #endif
47 #ifdef HAVE_XFT
48 #include <X11/Xft/Xft.h>
49 #endif
51 static char *current_mono_font;
52 static char *current_font;
53 static struct x_display_info *first_dpyinfo;
54 static Lisp_Object current_tool_bar_style;
56 /* Store an config changed event in to the event queue. */
58 static void
59 store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
61 struct input_event event;
62 EVENT_INIT (event);
63 event.kind = CONFIG_CHANGED_EVENT;
64 event.frame_or_window = display_name;
65 event.arg = arg;
66 kbd_buffer_store_event (&event);
69 /* Return true if DPYINFO is still valid. */
70 static bool
71 dpyinfo_valid (struct x_display_info *dpyinfo)
73 bool found = false;
74 if (dpyinfo != NULL)
76 struct x_display_info *d;
77 for (d = x_display_list; !found && d; d = d->next)
78 found = d == dpyinfo && d->display == dpyinfo->display;
80 return found;
83 /* Store a monospace font change event if the monospaced font changed. */
85 #if defined HAVE_XFT && (defined HAVE_GSETTINGS || defined HAVE_GCONF)
86 static void
87 store_monospaced_changed (const char *newfont)
89 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
90 return; /* No change. */
92 dupstring (&current_mono_font, newfont);
94 if (dpyinfo_valid (first_dpyinfo) && use_system_font)
96 store_config_changed_event (Qmonospace_font_name,
97 XCAR (first_dpyinfo->name_list_element));
100 #endif
102 /* Store a font name change event if the font name changed. */
104 #ifdef HAVE_XFT
105 static void
106 store_font_name_changed (const char *newfont)
108 if (current_font != NULL && strcmp (newfont, current_font) == 0)
109 return; /* No change. */
111 dupstring (&current_font, newfont);
113 if (dpyinfo_valid (first_dpyinfo))
115 store_config_changed_event (Qfont_name,
116 XCAR (first_dpyinfo->name_list_element));
119 #endif /* HAVE_XFT */
121 /* Map TOOL_BAR_STYLE from a string to its corresponding Lisp value.
122 Return Qnil if TOOL_BAR_STYLE is not known. */
124 static Lisp_Object
125 map_tool_bar_style (const char *tool_bar_style)
127 Lisp_Object style = Qnil;
128 if (tool_bar_style)
130 if (strcmp (tool_bar_style, "both") == 0)
131 style = Qboth;
132 else if (strcmp (tool_bar_style, "both-horiz") == 0)
133 style = Qboth_horiz;
134 else if (strcmp (tool_bar_style, "icons") == 0)
135 style = Qimage;
136 else if (strcmp (tool_bar_style, "text") == 0)
137 style = Qtext;
140 return style;
143 /* Store a tool bar style change event if the tool bar style changed. */
145 static void
146 store_tool_bar_style_changed (const char *newstyle,
147 struct x_display_info *dpyinfo)
149 Lisp_Object style = map_tool_bar_style (newstyle);
150 if (EQ (current_tool_bar_style, style))
151 return; /* No change. */
153 current_tool_bar_style = style;
154 if (dpyinfo_valid (dpyinfo))
155 store_config_changed_event (Qtool_bar_style,
156 XCAR (dpyinfo->name_list_element));
159 #ifdef HAVE_XFT
160 #define XSETTINGS_FONT_NAME "Gtk/FontName"
161 #endif
162 #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
164 enum {
165 SEEN_AA = 0x01,
166 SEEN_HINTING = 0x02,
167 SEEN_RGBA = 0x04,
168 SEEN_LCDFILTER = 0x08,
169 SEEN_HINTSTYLE = 0x10,
170 SEEN_DPI = 0x20,
171 SEEN_FONT = 0x40,
172 SEEN_TB_STYLE = 0x80
174 struct xsettings
176 #ifdef HAVE_XFT
177 FcBool aa, hinting;
178 int rgba, lcdfilter, hintstyle;
179 double dpi;
181 char *font;
182 #endif
184 char *tb_style;
186 unsigned seen;
189 #ifdef HAVE_GSETTINGS
190 #define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
191 #define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"
193 #ifdef HAVE_XFT
194 #define GSETTINGS_MONO_FONT "monospace-font-name"
195 #define GSETTINGS_FONT_NAME "font-name"
196 #endif
199 /* The single GSettings instance, or NULL if not connected to GSettings. */
201 static GSettings *gsettings_client;
203 /* Callback called when something changed in GSettings. */
205 static void
206 something_changed_gsettingsCB (GSettings *settings,
207 gchar *key,
208 gpointer user_data)
210 GVariant *val;
212 if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
214 val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
215 if (val)
217 g_variant_ref_sink (val);
218 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
220 const gchar *newstyle = g_variant_get_string (val, NULL);
221 store_tool_bar_style_changed (newstyle, first_dpyinfo);
223 g_variant_unref (val);
226 #ifdef HAVE_XFT
227 else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
229 val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
230 if (val)
232 g_variant_ref_sink (val);
233 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
235 const gchar *newfont = g_variant_get_string (val, NULL);
236 store_monospaced_changed (newfont);
238 g_variant_unref (val);
241 else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
243 val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
244 if (val)
246 g_variant_ref_sink (val);
247 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
249 const gchar *newfont = g_variant_get_string (val, NULL);
250 store_font_name_changed (newfont);
252 g_variant_unref (val);
255 #endif /* HAVE_XFT */
258 #endif /* HAVE_GSETTINGS */
260 #ifdef HAVE_GCONF
261 #define GCONF_TOOL_BAR_STYLE "/desktop/gnome/interface/toolbar_style"
262 #ifdef HAVE_XFT
263 #define GCONF_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
264 #define GCONF_FONT_NAME "/desktop/gnome/interface/font_name"
265 #endif
267 /* The single GConf instance, or NULL if not connected to GConf. */
269 static GConfClient *gconf_client;
271 /* Callback called when something changed in GConf that we care about. */
273 static void
274 something_changed_gconfCB (GConfClient *client,
275 guint cnxn_id,
276 GConfEntry *entry,
277 gpointer user_data)
279 GConfValue *v = gconf_entry_get_value (entry);
280 const char *key = gconf_entry_get_key (entry);
282 if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
283 if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
285 const char *value = gconf_value_get_string (v);
286 store_tool_bar_style_changed (value, first_dpyinfo);
288 #ifdef HAVE_XFT
289 else if (strcmp (key, GCONF_MONO_FONT) == 0)
291 const char *value = gconf_value_get_string (v);
292 store_monospaced_changed (value);
294 else if (strcmp (key, GCONF_FONT_NAME) == 0)
296 const char *value = gconf_value_get_string (v);
297 store_font_name_changed (value);
299 #endif /* HAVE_XFT */
302 #endif /* HAVE_GCONF */
304 #ifdef HAVE_XFT
306 /* Older fontconfig versions don't have FC_LCD_*. */
307 #ifndef FC_LCD_NONE
308 #define FC_LCD_NONE 0
309 #endif
310 #ifndef FC_LCD_DEFAULT
311 #define FC_LCD_DEFAULT 1
312 #endif
313 #ifndef FC_LCD_FILTER
314 #define FC_LCD_FILTER "lcdfilter"
315 #endif
317 #endif /* HAVE_XFT */
319 /* Find the window that contains the XSETTINGS property values. */
321 static void
322 get_prop_window (struct x_display_info *dpyinfo)
324 Display *dpy = dpyinfo->display;
326 XGrabServer (dpy);
327 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
328 dpyinfo->Xatom_xsettings_sel);
329 if (dpyinfo->xsettings_window != None)
330 /* Select events so we can detect if window is deleted or if settings
331 are changed. */
332 XSelectInput (dpy, dpyinfo->xsettings_window,
333 PropertyChangeMask|StructureNotifyMask);
335 XUngrabServer (dpy);
338 #define PAD(nr) (((nr) + 3) & ~3)
340 /* Parse xsettings and extract those that deal with Xft.
341 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
342 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
344 Layout of prop. First is a header:
346 bytes type what
347 ------------------------------------
348 1 CARD8 byte-order
349 3 unused
350 4 CARD32 SERIAL
351 4 CARD32 N_SETTINGS
353 Then N_SETTINGS records, with header:
355 bytes type what
356 ------------------------------------
357 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
358 1 unused
359 2 CARD16 n == name-length
360 n STRING8 name
361 p unused, p=pad_to_even_4(n)
362 4 CARD32 last-change-serial
364 and then the value, For string:
366 bytes type what
367 ------------------------------------
368 4 CARD32 n = value-length
369 n STRING8 value
370 p unused, p=pad_to_even_4(n)
372 For integer:
374 bytes type what
375 ------------------------------------
376 4 INT32 value
378 For RGB color:
380 bytes type what
381 ------------------------------------
382 2 CARD16 red
383 2 CARD16 blue
384 2 CARD16 green
385 2 CARD16 alpha
387 Returns non-zero if some Xft settings was seen, zero otherwise.
390 static int
391 parse_settings (unsigned char *prop,
392 unsigned long bytes,
393 struct xsettings *settings)
395 Lisp_Object byteorder = Fbyteorder ();
396 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
397 int that_bo = prop[0];
398 CARD32 n_settings;
399 int bytes_parsed = 0;
400 int settings_seen = 0;
401 int i = 0;
403 /* First 4 bytes is a serial number, skip that. */
405 if (bytes < 12) return settings_seen;
406 memcpy (&n_settings, prop+8, 4);
407 if (my_bo != that_bo) n_settings = bswap_32 (n_settings);
408 bytes_parsed = 12;
410 memset (settings, 0, sizeof (*settings));
412 while (bytes_parsed+4 < bytes && settings_seen < 7
413 && i < n_settings)
415 int type = prop[bytes_parsed++];
416 CARD16 nlen;
417 CARD32 vlen, ival = 0;
418 char name[128]; /* The names we are looking for are not this long. */
419 char sval[128]; /* The values we are looking for are not this long. */
420 bool want_this;
421 int to_cpy;
423 sval[0] = '\0';
424 ++i;
425 ++bytes_parsed; /* Padding */
427 memcpy (&nlen, prop+bytes_parsed, 2);
428 bytes_parsed += 2;
429 if (my_bo != that_bo) nlen = bswap_16 (nlen);
430 if (bytes_parsed + nlen > bytes) return settings_seen;
431 to_cpy = min (nlen, sizeof name - 1);
432 memcpy (name, prop+bytes_parsed, to_cpy);
433 name[to_cpy] = '\0';
435 bytes_parsed += nlen;
436 bytes_parsed = PAD (bytes_parsed);
438 bytes_parsed += 4; /* Skip serial for this value */
439 if (bytes_parsed > bytes) return settings_seen;
441 want_this = strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
442 #ifdef HAVE_XFT
443 if ((nlen > 6 && memcmp (name, "Xft/", 4) == 0)
444 || strcmp (XSETTINGS_FONT_NAME, name) == 0)
445 want_this = true;
446 #endif
448 switch (type)
450 case 0: /* Integer */
451 if (bytes_parsed + 4 > bytes) return settings_seen;
452 if (want_this)
454 memcpy (&ival, prop+bytes_parsed, 4);
455 if (my_bo != that_bo) ival = bswap_32 (ival);
457 bytes_parsed += 4;
458 break;
460 case 1: /* String */
461 if (bytes_parsed + 4 > bytes) return settings_seen;
462 memcpy (&vlen, prop+bytes_parsed, 4);
463 bytes_parsed += 4;
464 if (my_bo != that_bo) vlen = bswap_32 (vlen);
465 if (want_this)
467 to_cpy = min (vlen, sizeof sval - 1);
468 memcpy (sval, prop+bytes_parsed, to_cpy);
469 sval[to_cpy] = '\0';
471 bytes_parsed += vlen;
472 bytes_parsed = PAD (bytes_parsed);
473 break;
475 case 2: /* RGB value */
476 /* No need to parse this */
477 if (bytes_parsed + 8 > bytes) return settings_seen;
478 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
479 break;
481 default: /* Parse Error */
482 return settings_seen;
485 if (want_this)
487 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
489 dupstring (&settings->tb_style, sval);
490 settings->seen |= SEEN_TB_STYLE;
492 #ifdef HAVE_XFT
493 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
495 dupstring (&settings->font, sval);
496 settings->seen |= SEEN_FONT;
498 else if (strcmp (name, "Xft/Antialias") == 0)
500 settings->seen |= SEEN_AA;
501 settings->aa = ival != 0;
503 else if (strcmp (name, "Xft/Hinting") == 0)
505 settings->seen |= SEEN_HINTING;
506 settings->hinting = ival != 0;
508 # ifdef FC_HINT_STYLE
509 else if (strcmp (name, "Xft/HintStyle") == 0)
511 settings->seen |= SEEN_HINTSTYLE;
512 if (strcmp (sval, "hintnone") == 0)
513 settings->hintstyle = FC_HINT_NONE;
514 else if (strcmp (sval, "hintslight") == 0)
515 settings->hintstyle = FC_HINT_SLIGHT;
516 else if (strcmp (sval, "hintmedium") == 0)
517 settings->hintstyle = FC_HINT_MEDIUM;
518 else if (strcmp (sval, "hintfull") == 0)
519 settings->hintstyle = FC_HINT_FULL;
520 else
521 settings->seen &= ~SEEN_HINTSTYLE;
523 # endif
524 else if (strcmp (name, "Xft/RGBA") == 0)
526 settings->seen |= SEEN_RGBA;
527 if (strcmp (sval, "none") == 0)
528 settings->rgba = FC_RGBA_NONE;
529 else if (strcmp (sval, "rgb") == 0)
530 settings->rgba = FC_RGBA_RGB;
531 else if (strcmp (sval, "bgr") == 0)
532 settings->rgba = FC_RGBA_BGR;
533 else if (strcmp (sval, "vrgb") == 0)
534 settings->rgba = FC_RGBA_VRGB;
535 else if (strcmp (sval, "vbgr") == 0)
536 settings->rgba = FC_RGBA_VBGR;
537 else
538 settings->seen &= ~SEEN_RGBA;
540 else if (strcmp (name, "Xft/DPI") == 0 && ival != (CARD32) -1)
542 settings->seen |= SEEN_DPI;
543 settings->dpi = ival / 1024.0;
545 else if (strcmp (name, "Xft/lcdfilter") == 0)
547 settings->seen |= SEEN_LCDFILTER;
548 if (strcmp (sval, "none") == 0)
549 settings->lcdfilter = FC_LCD_NONE;
550 else if (strcmp (sval, "lcddefault") == 0)
551 settings->lcdfilter = FC_LCD_DEFAULT;
552 else
553 settings->seen &= ~SEEN_LCDFILTER;
555 #endif /* HAVE_XFT */
556 else
557 want_this = false;
558 settings_seen += want_this;
562 return settings_seen;
565 /* Read settings from the XSettings property window on display for DPYINFO.
566 Store settings read in SETTINGS.
567 Return true iff successful. */
569 static bool
570 read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
572 Atom act_type;
573 int act_form;
574 unsigned long nitems, bytes_after;
575 unsigned char *prop = NULL;
576 Display *dpy = dpyinfo->display;
577 int rc;
578 bool got_settings = false;
580 x_catch_errors (dpy);
581 rc = XGetWindowProperty (dpy,
582 dpyinfo->xsettings_window,
583 dpyinfo->Xatom_xsettings_prop,
584 0, LONG_MAX, False, AnyPropertyType,
585 &act_type, &act_form, &nitems, &bytes_after,
586 &prop);
588 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
589 && act_type == dpyinfo->Xatom_xsettings_prop)
590 got_settings = parse_settings (prop, nitems, settings) != 0;
592 XFree (prop);
594 x_uncatch_errors ();
596 return got_settings;
599 /* Apply Xft settings in SETTINGS to the Xft library.
600 Store a Lisp event that Xft settings changed. */
602 static void
603 apply_xft_settings (struct x_display_info *dpyinfo,
604 struct xsettings *settings)
606 #ifdef HAVE_XFT
607 FcPattern *pat;
608 struct xsettings oldsettings;
609 bool changed = false;
611 memset (&oldsettings, 0, sizeof (oldsettings));
612 pat = FcPatternCreate ();
613 XftDefaultSubstitute (dpyinfo->display,
614 XScreenNumberOfScreen (dpyinfo->screen),
615 pat);
616 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
617 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
618 #ifdef FC_HINT_STYLE
619 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
620 #endif
621 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
622 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
623 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
625 if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
627 FcPatternDel (pat, FC_ANTIALIAS);
628 FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
629 changed = true;
630 oldsettings.aa = settings->aa;
633 if ((settings->seen & SEEN_HINTING) != 0
634 && oldsettings.hinting != settings->hinting)
636 FcPatternDel (pat, FC_HINTING);
637 FcPatternAddBool (pat, FC_HINTING, settings->hinting);
638 changed = true;
639 oldsettings.hinting = settings->hinting;
641 if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
643 FcPatternDel (pat, FC_RGBA);
644 FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
645 oldsettings.rgba = settings->rgba;
646 changed = true;
649 /* Older fontconfig versions don't have FC_LCD_FILTER. */
650 if ((settings->seen & SEEN_LCDFILTER) != 0
651 && oldsettings.lcdfilter != settings->lcdfilter)
653 FcPatternDel (pat, FC_LCD_FILTER);
654 FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
655 changed = true;
656 oldsettings.lcdfilter = settings->lcdfilter;
659 #ifdef FC_HINT_STYLE
660 if ((settings->seen & SEEN_HINTSTYLE) != 0
661 && oldsettings.hintstyle != settings->hintstyle)
663 FcPatternDel (pat, FC_HINT_STYLE);
664 FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
665 changed = true;
666 oldsettings.hintstyle = settings->hintstyle;
668 #endif
670 if ((settings->seen & SEEN_DPI) != 0
671 && settings->dpi > 0
672 /* The following conjunct avoids setting `changed' to true when
673 old and new dpi settings do not differ "substantially".
674 Otherwise, the dynamic-setting Elisp code may process all sorts
675 of unrelated settings that override users' font customizations,
676 among others. Compare:
678 https://lists.gnu.org/archive/html/emacs-devel/2016-05/msg00557.html
679 https://lists.gnu.org/archive/html/bug-gnu-emacs/2016-12/msg00820.html
681 As soon as the dynamic-settings code has been tested and
682 verified, this Emacs 25.2 workaround should be removed. */
683 && ((oldsettings.dpi >= settings->dpi
684 && (oldsettings.dpi - settings->dpi) > 2)
685 || ((settings->dpi > oldsettings.dpi)
686 && (settings->dpi - oldsettings.dpi) > 2)))
688 FcPatternDel (pat, FC_DPI);
689 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
690 changed = true;
691 oldsettings.dpi = settings->dpi;
693 /* Changing the DPI on this display affects all frames on it.
694 Check FRAME_RES_X and FRAME_RES_Y in frame.h to see how. */
695 dpyinfo->resy = dpyinfo->resx = settings->dpi;
698 if (changed)
700 static char const format[] =
701 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
702 "Hintstyle: %d, DPI: %f";
703 enum
705 d_formats = 5,
706 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
707 lf_formats = 1,
708 max_f_integer_digits = DBL_MAX_10_EXP + 1,
709 f_precision = 6,
710 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
711 - sizeof "%f")
713 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
715 XftDefaultSet (dpyinfo->display, pat);
716 store_config_changed_event (Qfont_render,
717 XCAR (dpyinfo->name_list_element));
718 Vxft_settings
719 = make_formatted_string (buf, format,
720 oldsettings.aa, oldsettings.hinting,
721 oldsettings.rgba, oldsettings.lcdfilter,
722 oldsettings.hintstyle, oldsettings.dpi);
725 else
726 FcPatternDestroy (pat);
727 #endif /* HAVE_XFT */
730 /* Read XSettings from the display for DPYINFO.
731 If SEND_EVENT_P store a Lisp event settings that changed. */
733 static void
734 read_and_apply_settings (struct x_display_info *dpyinfo, bool send_event_p)
736 struct xsettings settings;
738 if (!read_settings (dpyinfo, &settings))
739 return;
741 apply_xft_settings (dpyinfo, &settings);
742 if (settings.seen & SEEN_TB_STYLE)
744 if (send_event_p)
745 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
746 else
747 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
748 xfree (settings.tb_style);
750 #ifdef HAVE_XFT
751 if (settings.seen & SEEN_FONT)
753 if (send_event_p)
754 store_font_name_changed (settings.font);
755 else
756 dupstring (&current_font, settings.font);
757 xfree (settings.font);
759 #endif
762 /* Check if EVENT for the display in DPYINFO is XSettings related. */
764 void
765 xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
767 bool check_window_p = false, apply_settings_p = false;
769 switch (event->type)
771 case DestroyNotify:
772 if (dpyinfo->xsettings_window == event->xany.window)
773 check_window_p = true;
774 break;
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 = true;
781 break;
783 case PropertyNotify:
784 if (event->xproperty.window == dpyinfo->xsettings_window
785 && event->xproperty.state == PropertyNewValue
786 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
787 apply_settings_p = true;
788 break;
792 if (check_window_p)
794 dpyinfo->xsettings_window = None;
795 get_prop_window (dpyinfo);
796 if (dpyinfo->xsettings_window != None)
797 apply_settings_p = true;
800 if (apply_settings_p)
801 read_and_apply_settings (dpyinfo, true);
804 /* Initialize GSettings and read startup values. */
806 static void
807 init_gsettings (void)
809 #ifdef HAVE_GSETTINGS
810 GVariant *val;
811 bool schema_found = false;
813 #if ! GLIB_CHECK_VERSION (2, 36, 0)
814 g_type_init ();
815 #endif
817 #if GLIB_CHECK_VERSION (2, 32, 0)
819 GSettingsSchema *sc = g_settings_schema_source_lookup
820 (g_settings_schema_source_get_default (),
821 GSETTINGS_SCHEMA,
822 true);
823 schema_found = sc != NULL;
824 if (sc) g_settings_schema_unref (sc);
826 #else
828 const gchar *const *schemas = g_settings_list_schemas ();
829 if (schemas == NULL) return;
830 while (! schema_found && *schemas != NULL)
831 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
833 #endif
834 if (!schema_found) return;
836 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
837 if (!gsettings_client) return;
838 g_object_ref_sink (G_OBJECT (gsettings_client));
839 g_signal_connect (G_OBJECT (gsettings_client), "changed",
840 G_CALLBACK (something_changed_gsettingsCB), NULL);
842 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
843 if (val)
845 g_variant_ref_sink (val);
846 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
847 current_tool_bar_style
848 = map_tool_bar_style (g_variant_get_string (val, NULL));
849 g_variant_unref (val);
852 #ifdef HAVE_XFT
853 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
854 if (val)
856 g_variant_ref_sink (val);
857 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
858 dupstring (&current_mono_font, g_variant_get_string (val, NULL));
859 g_variant_unref (val);
862 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
863 if (val)
865 g_variant_ref_sink (val);
866 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
867 dupstring (&current_font, g_variant_get_string (val, NULL));
868 g_variant_unref (val);
870 #endif /* HAVE_XFT */
872 #endif /* HAVE_GSETTINGS */
875 /* Init GConf and read startup values. */
877 static void
878 init_gconf (void)
880 #if defined (HAVE_GCONF)
881 char *s;
883 #if ! GLIB_CHECK_VERSION (2, 36, 0)
884 g_type_init ();
885 #endif
887 gconf_client = gconf_client_get_default ();
888 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
889 gconf_client_add_dir (gconf_client,
890 GCONF_TOOL_BAR_STYLE,
891 GCONF_CLIENT_PRELOAD_ONELEVEL,
892 NULL);
893 gconf_client_notify_add (gconf_client,
894 GCONF_TOOL_BAR_STYLE,
895 something_changed_gconfCB,
896 NULL, NULL, NULL);
898 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
899 if (s)
901 current_tool_bar_style = map_tool_bar_style (s);
902 g_free (s);
905 #ifdef HAVE_XFT
906 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
907 if (s)
909 dupstring (&current_mono_font, s);
910 g_free (s);
912 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
913 if (s)
915 dupstring (&current_font, s);
916 g_free (s);
918 gconf_client_add_dir (gconf_client,
919 GCONF_MONO_FONT,
920 GCONF_CLIENT_PRELOAD_ONELEVEL,
921 NULL);
922 gconf_client_notify_add (gconf_client,
923 GCONF_MONO_FONT,
924 something_changed_gconfCB,
925 NULL, NULL, NULL);
926 gconf_client_add_dir (gconf_client,
927 GCONF_FONT_NAME,
928 GCONF_CLIENT_PRELOAD_ONELEVEL,
929 NULL);
930 gconf_client_notify_add (gconf_client,
931 GCONF_FONT_NAME,
932 something_changed_gconfCB,
933 NULL, NULL, NULL);
934 #endif /* HAVE_XFT */
935 #endif /* HAVE_GCONF */
938 /* Init Xsettings and read startup values. */
940 static void
941 init_xsettings (struct x_display_info *dpyinfo)
943 Display *dpy = dpyinfo->display;
945 block_input ();
947 /* Select events so we can detect client messages sent when selection
948 owner changes. */
949 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
951 get_prop_window (dpyinfo);
952 if (dpyinfo->xsettings_window != None)
953 read_and_apply_settings (dpyinfo, false);
955 unblock_input ();
958 void
959 xsettings_initialize (struct x_display_info *dpyinfo)
961 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
962 init_gconf ();
963 init_xsettings (dpyinfo);
964 init_gsettings ();
967 /* Return the system monospaced font.
968 May be NULL if not known. */
970 const char *
971 xsettings_get_system_font (void)
973 return current_mono_font;
976 #ifdef USE_LUCID
977 /* Return the system font.
978 May be NULL if not known. */
980 const char *
981 xsettings_get_system_normal_font (void)
983 return current_font;
985 #endif
987 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
988 Sfont_get_system_normal_font,
989 0, 0, 0,
990 doc: /* Get the system default application font. */)
991 (void)
993 return current_font ? build_string (current_font) : Qnil;
996 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
997 0, 0, 0,
998 doc: /* Get the system default fixed width font. */)
999 (void)
1001 return current_mono_font ? build_string (current_mono_font) : Qnil;
1004 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
1005 Stool_bar_get_system_style, 0, 0, 0,
1006 doc: /* Get the system tool bar style.
1007 If no system tool bar style is known, return `tool-bar-style' if set to a
1008 known style. Otherwise return image. */)
1009 (void)
1011 if (EQ (Vtool_bar_style, Qimage)
1012 || EQ (Vtool_bar_style, Qtext)
1013 || EQ (Vtool_bar_style, Qboth)
1014 || EQ (Vtool_bar_style, Qboth_horiz)
1015 || EQ (Vtool_bar_style, Qtext_image_horiz))
1016 return Vtool_bar_style;
1017 if (!NILP (current_tool_bar_style))
1018 return current_tool_bar_style;
1019 return Qimage;
1022 void
1023 syms_of_xsettings (void)
1025 current_mono_font = NULL;
1026 current_font = NULL;
1027 first_dpyinfo = NULL;
1028 #ifdef HAVE_GSETTINGS
1029 gsettings_client = NULL;
1030 #endif
1031 #ifdef HAVE_GCONF
1032 gconf_client = NULL;
1033 #endif
1035 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1036 DEFSYM (Qfont_name, "font-name");
1037 DEFSYM (Qfont_render, "font-render");
1038 defsubr (&Sfont_get_system_font);
1039 defsubr (&Sfont_get_system_normal_font);
1041 DEFVAR_BOOL ("font-use-system-font", use_system_font,
1042 doc: /* Non-nil means to apply the system defined font dynamically.
1043 When this is non-nil and the system defined fixed width font changes, we
1044 update frames dynamically.
1045 If this variable is nil, Emacs ignores system font changes. */);
1046 use_system_font = false;
1048 DEFVAR_LISP ("xft-settings", Vxft_settings,
1049 doc: /* Font settings applied to Xft. */);
1050 Vxft_settings = empty_unibyte_string;
1052 #ifdef HAVE_XFT
1053 Fprovide (intern_c_string ("font-render-setting"), Qnil);
1054 #if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
1055 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1056 #endif
1057 #endif
1059 current_tool_bar_style = Qnil;
1060 DEFSYM (Qtool_bar_style, "tool-bar-style");
1061 defsubr (&Stool_bar_get_system_style);
1063 Fprovide (intern_c_string ("dynamic-setting"), Qnil);