emacs-lisp/package.el (package--read-pkg-desc): Fix tar-desc reference.
[emacs.git] / src / xsettings.c
blob028487b91ee1a10305aab89f60dfbf8a9b01db59
1 /* Functions for handling font and other changes dynamically.
3 Copyright (C) 2009-2015 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
10 (at 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 <http://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)
542 settings->seen |= SEEN_DPI;
543 settings->dpi = (double)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 && oldsettings.dpi != settings->dpi
671 && settings->dpi > 0)
673 FcPatternDel (pat, FC_DPI);
674 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
675 changed = true;
676 oldsettings.dpi = settings->dpi;
678 /* Changing the DPI on this display affects all frames on it.
679 Check FRAME_RES_X and FRAME_RES_Y in frame.h to see how. */
680 dpyinfo->resy = dpyinfo->resx = settings->dpi;
683 if (changed)
685 static char const format[] =
686 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
687 "Hintstyle: %d, DPI: %f";
688 enum
690 d_formats = 5,
691 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
692 lf_formats = 1,
693 max_f_integer_digits = DBL_MAX_10_EXP + 1,
694 f_precision = 6,
695 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
696 - sizeof "%f")
698 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
700 XftDefaultSet (dpyinfo->display, pat);
701 store_config_changed_event (Qfont_render,
702 XCAR (dpyinfo->name_list_element));
703 Vxft_settings
704 = make_formatted_string (buf, format,
705 oldsettings.aa, oldsettings.hinting,
706 oldsettings.rgba, oldsettings.lcdfilter,
707 oldsettings.hintstyle, oldsettings.dpi);
710 else
711 FcPatternDestroy (pat);
712 #endif /* HAVE_XFT */
715 /* Read XSettings from the display for DPYINFO.
716 If SEND_EVENT_P store a Lisp event settings that changed. */
718 static void
719 read_and_apply_settings (struct x_display_info *dpyinfo, bool send_event_p)
721 struct xsettings settings;
723 if (!read_settings (dpyinfo, &settings))
724 return;
726 apply_xft_settings (dpyinfo, &settings);
727 if (settings.seen & SEEN_TB_STYLE)
729 if (send_event_p)
730 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
731 else
732 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
733 xfree (settings.tb_style);
735 #ifdef HAVE_XFT
736 if (settings.seen & SEEN_FONT)
738 if (send_event_p)
739 store_font_name_changed (settings.font);
740 else
741 dupstring (&current_font, settings.font);
742 xfree (settings.font);
744 #endif
747 /* Check if EVENT for the display in DPYINFO is XSettings related. */
749 void
750 xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
752 bool check_window_p = false, apply_settings_p = false;
754 switch (event->type)
756 case DestroyNotify:
757 if (dpyinfo->xsettings_window == event->xany.window)
758 check_window_p = true;
759 break;
761 case ClientMessage:
762 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
763 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
764 && event->xclient.window == dpyinfo->root_window)
765 check_window_p = true;
766 break;
768 case PropertyNotify:
769 if (event->xproperty.window == dpyinfo->xsettings_window
770 && event->xproperty.state == PropertyNewValue
771 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
772 apply_settings_p = true;
773 break;
777 if (check_window_p)
779 dpyinfo->xsettings_window = None;
780 get_prop_window (dpyinfo);
781 if (dpyinfo->xsettings_window != None)
782 apply_settings_p = true;
785 if (apply_settings_p)
786 read_and_apply_settings (dpyinfo, true);
789 /* Initialize GSettings and read startup values. */
791 static void
792 init_gsettings (void)
794 #ifdef HAVE_GSETTINGS
795 GVariant *val;
796 bool schema_found = false;
798 #if ! GLIB_CHECK_VERSION (2, 36, 0)
799 g_type_init ();
800 #endif
802 #if GLIB_CHECK_VERSION (2, 32, 0)
804 GSettingsSchema *sc = g_settings_schema_source_lookup
805 (g_settings_schema_source_get_default (),
806 GSETTINGS_SCHEMA,
807 true);
808 schema_found = sc != NULL;
809 if (sc) g_settings_schema_unref (sc);
811 #else
813 const gchar *const *schemas = g_settings_list_schemas ();
814 if (schemas == NULL) return;
815 while (! schema_found && *schemas != NULL)
816 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
818 #endif
819 if (!schema_found) return;
821 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
822 if (!gsettings_client) return;
823 g_object_ref_sink (G_OBJECT (gsettings_client));
824 g_signal_connect (G_OBJECT (gsettings_client), "changed",
825 G_CALLBACK (something_changed_gsettingsCB), NULL);
827 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
828 if (val)
830 g_variant_ref_sink (val);
831 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
832 current_tool_bar_style
833 = map_tool_bar_style (g_variant_get_string (val, NULL));
834 g_variant_unref (val);
837 #ifdef HAVE_XFT
838 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
839 if (val)
841 g_variant_ref_sink (val);
842 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
843 dupstring (&current_mono_font, g_variant_get_string (val, NULL));
844 g_variant_unref (val);
847 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
848 if (val)
850 g_variant_ref_sink (val);
851 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
852 dupstring (&current_font, g_variant_get_string (val, NULL));
853 g_variant_unref (val);
855 #endif /* HAVE_XFT */
857 #endif /* HAVE_GSETTINGS */
860 /* Init GConf and read startup values. */
862 static void
863 init_gconf (void)
865 #if defined (HAVE_GCONF)
866 char *s;
868 #if ! GLIB_CHECK_VERSION (2, 36, 0)
869 g_type_init ();
870 #endif
872 gconf_client = gconf_client_get_default ();
873 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
874 gconf_client_add_dir (gconf_client,
875 GCONF_TOOL_BAR_STYLE,
876 GCONF_CLIENT_PRELOAD_ONELEVEL,
877 NULL);
878 gconf_client_notify_add (gconf_client,
879 GCONF_TOOL_BAR_STYLE,
880 something_changed_gconfCB,
881 NULL, NULL, NULL);
883 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
884 if (s)
886 current_tool_bar_style = map_tool_bar_style (s);
887 g_free (s);
890 #ifdef HAVE_XFT
891 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
892 if (s)
894 dupstring (&current_mono_font, s);
895 g_free (s);
897 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
898 if (s)
900 dupstring (&current_font, s);
901 g_free (s);
903 gconf_client_add_dir (gconf_client,
904 GCONF_MONO_FONT,
905 GCONF_CLIENT_PRELOAD_ONELEVEL,
906 NULL);
907 gconf_client_notify_add (gconf_client,
908 GCONF_MONO_FONT,
909 something_changed_gconfCB,
910 NULL, NULL, NULL);
911 gconf_client_add_dir (gconf_client,
912 GCONF_FONT_NAME,
913 GCONF_CLIENT_PRELOAD_ONELEVEL,
914 NULL);
915 gconf_client_notify_add (gconf_client,
916 GCONF_FONT_NAME,
917 something_changed_gconfCB,
918 NULL, NULL, NULL);
919 #endif /* HAVE_XFT */
920 #endif /* HAVE_GCONF */
923 /* Init Xsettings and read startup values. */
925 static void
926 init_xsettings (struct x_display_info *dpyinfo)
928 Display *dpy = dpyinfo->display;
930 block_input ();
932 /* Select events so we can detect client messages sent when selection
933 owner changes. */
934 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
936 get_prop_window (dpyinfo);
937 if (dpyinfo->xsettings_window != None)
938 read_and_apply_settings (dpyinfo, false);
940 unblock_input ();
943 void
944 xsettings_initialize (struct x_display_info *dpyinfo)
946 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
947 init_gconf ();
948 init_xsettings (dpyinfo);
949 init_gsettings ();
952 /* Return the system monospaced font.
953 May be NULL if not known. */
955 const char *
956 xsettings_get_system_font (void)
958 return current_mono_font;
961 #ifdef USE_LUCID
962 /* Return the system font.
963 May be NULL if not known. */
965 const char *
966 xsettings_get_system_normal_font (void)
968 return current_font;
970 #endif
972 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
973 Sfont_get_system_normal_font,
974 0, 0, 0,
975 doc: /* Get the system default application font. */)
976 (void)
978 return current_font ? build_string (current_font) : Qnil;
981 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
982 0, 0, 0,
983 doc: /* Get the system default fixed width font. */)
984 (void)
986 return current_mono_font ? build_string (current_mono_font) : Qnil;
989 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
990 Stool_bar_get_system_style, 0, 0, 0,
991 doc: /* Get the system tool bar style.
992 If no system tool bar style is known, return `tool-bar-style' if set to a
993 known style. Otherwise return image. */)
994 (void)
996 if (EQ (Vtool_bar_style, Qimage)
997 || EQ (Vtool_bar_style, Qtext)
998 || EQ (Vtool_bar_style, Qboth)
999 || EQ (Vtool_bar_style, Qboth_horiz)
1000 || EQ (Vtool_bar_style, Qtext_image_horiz))
1001 return Vtool_bar_style;
1002 if (!NILP (current_tool_bar_style))
1003 return current_tool_bar_style;
1004 return Qimage;
1007 void
1008 syms_of_xsettings (void)
1010 current_mono_font = NULL;
1011 current_font = NULL;
1012 first_dpyinfo = NULL;
1013 #ifdef HAVE_GSETTINGS
1014 gsettings_client = NULL;
1015 #endif
1016 #ifdef HAVE_GCONF
1017 gconf_client = NULL;
1018 #endif
1020 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1021 DEFSYM (Qfont_name, "font-name");
1022 DEFSYM (Qfont_render, "font-render");
1023 defsubr (&Sfont_get_system_font);
1024 defsubr (&Sfont_get_system_normal_font);
1026 DEFVAR_BOOL ("font-use-system-font", use_system_font,
1027 doc: /* Non-nil means to apply the system defined font dynamically.
1028 When this is non-nil and the system defined fixed width font changes, we
1029 update frames dynamically.
1030 If this variable is nil, Emacs ignores system font changes. */);
1031 use_system_font = false;
1033 DEFVAR_LISP ("xft-settings", Vxft_settings,
1034 doc: /* Font settings applied to Xft. */);
1035 Vxft_settings = empty_unibyte_string;
1037 #ifdef HAVE_XFT
1038 Fprovide (intern_c_string ("font-render-setting"), Qnil);
1039 #if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
1040 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1041 #endif
1042 #endif
1044 current_tool_bar_style = Qnil;
1045 DEFSYM (Qtool_bar_style, "tool-bar-style");
1046 defsubr (&Stool_bar_get_system_style);
1048 Fprovide (intern_c_string ("dynamic-setting"), Qnil);