* alloc.c (valgrind_p): Use bool for boolean.
[emacs.git] / src / xsettings.c
blob8fe82fec74b5d31a85c0c2b26432cba63ecd3757
1 /* Functions for handling font and other changes dynamically.
3 Copyright (C) 2009-2013 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>
25 #include "lisp.h"
26 #include "xterm.h"
27 #include "xsettings.h"
28 #include "frame.h"
29 #include "keyboard.h"
30 #include "blockinput.h"
31 #include "termhooks.h"
33 #include <X11/Xproto.h>
35 #ifdef HAVE_GSETTINGS
36 #include <glib-object.h>
37 #include <gio/gio.h>
38 #endif
40 #ifdef HAVE_GCONF
41 #include <gconf/gconf-client.h>
42 #endif
44 #ifdef HAVE_XFT
45 #include <X11/Xft/Xft.h>
46 #endif
48 static char *current_mono_font;
49 static char *current_font;
50 static struct x_display_info *first_dpyinfo;
51 static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render,
52 Qtool_bar_style;
53 static Lisp_Object current_tool_bar_style;
55 /* Store an config changed event in to the event queue. */
57 static void
58 store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
60 struct input_event event;
61 EVENT_INIT (event);
62 event.kind = CONFIG_CHANGED_EVENT;
63 event.frame_or_window = display_name;
64 event.arg = arg;
65 kbd_buffer_store_event (&event);
68 /* Return non-zero if DPYINFO is still valid. */
69 static int
70 dpyinfo_valid (struct x_display_info *dpyinfo)
72 int found = 0;
73 if (dpyinfo != NULL)
75 struct x_display_info *d;
76 for (d = x_display_list; !found && d; d = d->next)
77 found = d == dpyinfo && d->display == dpyinfo->display;
79 return found;
82 /* Store a monospace font change event if the monospaced font changed. */
84 #if defined HAVE_XFT && (defined HAVE_GSETTINGS || defined HAVE_GCONF)
85 static void
86 store_monospaced_changed (const char *newfont)
88 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
89 return; /* No change. */
91 xfree (current_mono_font);
92 current_mono_font = xstrdup (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 xfree (current_font);
112 current_font = xstrdup (newfont);
114 if (dpyinfo_valid (first_dpyinfo))
116 store_config_changed_event (Qfont_name,
117 XCAR (first_dpyinfo->name_list_element));
120 #endif /* HAVE_XFT */
122 /* Map TOOL_BAR_STYLE from a string to its corresponding Lisp value.
123 Return Qnil if TOOL_BAR_STYLE is not known. */
125 static Lisp_Object
126 map_tool_bar_style (const char *tool_bar_style)
128 Lisp_Object style = Qnil;
129 if (tool_bar_style)
131 if (strcmp (tool_bar_style, "both") == 0)
132 style = Qboth;
133 else if (strcmp (tool_bar_style, "both-horiz") == 0)
134 style = Qboth_horiz;
135 else if (strcmp (tool_bar_style, "icons") == 0)
136 style = Qimage;
137 else if (strcmp (tool_bar_style, "text") == 0)
138 style = Qtext;
141 return style;
144 /* Store a tool bar style change event if the tool bar style changed. */
146 static void
147 store_tool_bar_style_changed (const char *newstyle,
148 struct x_display_info *dpyinfo)
150 Lisp_Object style = map_tool_bar_style (newstyle);
151 if (EQ (current_tool_bar_style, style))
152 return; /* No change. */
154 current_tool_bar_style = style;
155 if (dpyinfo_valid (dpyinfo))
156 store_config_changed_event (Qtool_bar_style,
157 XCAR (dpyinfo->name_list_element));
160 #ifdef HAVE_XFT
161 #define XSETTINGS_FONT_NAME "Gtk/FontName"
162 #endif
163 #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
165 enum {
166 SEEN_AA = 0x01,
167 SEEN_HINTING = 0x02,
168 SEEN_RGBA = 0x04,
169 SEEN_LCDFILTER = 0x08,
170 SEEN_HINTSTYLE = 0x10,
171 SEEN_DPI = 0x20,
172 SEEN_FONT = 0x40,
173 SEEN_TB_STYLE = 0x80
175 struct xsettings
177 #ifdef HAVE_XFT
178 FcBool aa, hinting;
179 int rgba, lcdfilter, hintstyle;
180 double dpi;
182 char *font;
183 #endif
185 char *tb_style;
187 unsigned seen;
190 #ifdef HAVE_GSETTINGS
191 #define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
192 #define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"
194 #ifdef HAVE_XFT
195 #define GSETTINGS_MONO_FONT "monospace-font-name"
196 #define GSETTINGS_FONT_NAME "font-name"
197 #endif
200 /* The single GSettings instance, or NULL if not connected to GSettings. */
202 static GSettings *gsettings_client;
204 /* Callback called when something changed in GSettings. */
206 static void
207 something_changed_gsettingsCB (GSettings *settings,
208 gchar *key,
209 gpointer user_data)
211 GVariant *val;
213 if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
215 val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
216 if (val)
218 g_variant_ref_sink (val);
219 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
221 const gchar *newstyle = g_variant_get_string (val, NULL);
222 store_tool_bar_style_changed (newstyle, first_dpyinfo);
224 g_variant_unref (val);
227 #ifdef HAVE_XFT
228 else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
230 val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
231 if (val)
233 g_variant_ref_sink (val);
234 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
236 const gchar *newfont = g_variant_get_string (val, NULL);
237 store_monospaced_changed (newfont);
239 g_variant_unref (val);
242 else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
244 val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
245 if (val)
247 g_variant_ref_sink (val);
248 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
250 const gchar *newfont = g_variant_get_string (val, NULL);
251 store_font_name_changed (newfont);
253 g_variant_unref (val);
256 #endif /* HAVE_XFT */
259 #endif /* HAVE_GSETTINGS */
261 #ifdef HAVE_GCONF
262 #define GCONF_TOOL_BAR_STYLE "/desktop/gnome/interface/toolbar_style"
263 #ifdef HAVE_XFT
264 #define GCONF_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
265 #define GCONF_FONT_NAME "/desktop/gnome/interface/font_name"
266 #endif
268 /* The single GConf instance, or NULL if not connected to GConf. */
270 static GConfClient *gconf_client;
272 /* Callback called when something changed in GConf that we care about. */
274 static void
275 something_changed_gconfCB (GConfClient *client,
276 guint cnxn_id,
277 GConfEntry *entry,
278 gpointer user_data)
280 GConfValue *v = gconf_entry_get_value (entry);
281 const char *key = gconf_entry_get_key (entry);
283 if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
284 if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
286 const char *value = gconf_value_get_string (v);
287 store_tool_bar_style_changed (value, first_dpyinfo);
289 #ifdef HAVE_XFT
290 else if (strcmp (key, GCONF_MONO_FONT) == 0)
292 const char *value = gconf_value_get_string (v);
293 store_monospaced_changed (value);
295 else if (strcmp (key, GCONF_FONT_NAME) == 0)
297 const char *value = gconf_value_get_string (v);
298 store_font_name_changed (value);
300 #endif /* HAVE_XFT */
303 #endif /* HAVE_GCONF */
305 #ifdef HAVE_XFT
307 /* Older fontconfig versions don't have FC_LCD_*. */
308 #ifndef FC_LCD_NONE
309 #define FC_LCD_NONE 0
310 #endif
311 #ifndef FC_LCD_DEFAULT
312 #define FC_LCD_DEFAULT 1
313 #endif
314 #ifndef FC_LCD_FILTER
315 #define FC_LCD_FILTER "lcdfilter"
316 #endif
318 #endif /* HAVE_XFT */
320 /* Find the window that contains the XSETTINGS property values. */
322 static void
323 get_prop_window (struct x_display_info *dpyinfo)
325 Display *dpy = dpyinfo->display;
327 XGrabServer (dpy);
328 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
329 dpyinfo->Xatom_xsettings_sel);
330 if (dpyinfo->xsettings_window != None)
331 /* Select events so we can detect if window is deleted or if settings
332 are changed. */
333 XSelectInput (dpy, dpyinfo->xsettings_window,
334 PropertyChangeMask|StructureNotifyMask);
336 XUngrabServer (dpy);
339 #define PAD(nr) (((nr) + 3) & ~3)
341 /* Parse xsettings and extract those that deal with Xft.
342 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
343 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
345 Layout of prop. First is a header:
347 bytes type what
348 ------------------------------------
349 1 CARD8 byte-order
350 3 unused
351 4 CARD32 SERIAL
352 4 CARD32 N_SETTINGS
354 Then N_SETTINGS records, with header:
356 bytes type what
357 ------------------------------------
358 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
359 1 unused
360 2 CARD16 n == name-length
361 n STRING8 name
362 p unused, p=pad_to_even_4(n)
363 4 CARD32 last-change-serial
365 and then the value, For string:
367 bytes type what
368 ------------------------------------
369 4 CARD32 n = value-length
370 n STRING8 value
371 p unused, p=pad_to_even_4(n)
373 For integer:
375 bytes type what
376 ------------------------------------
377 4 INT32 value
379 For RGB color:
381 bytes type what
382 ------------------------------------
383 2 CARD16 red
384 2 CARD16 blue
385 2 CARD16 green
386 2 CARD16 alpha
388 Returns non-zero if some Xft settings was seen, zero otherwise.
391 static int
392 parse_settings (unsigned char *prop,
393 long unsigned int bytes,
394 struct xsettings *settings)
396 Lisp_Object byteorder = Fbyteorder ();
397 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
398 int that_bo = prop[0];
399 CARD32 n_settings;
400 int bytes_parsed = 0;
401 int settings_seen = 0;
402 int i = 0;
404 /* First 4 bytes is a serial number, skip that. */
406 if (bytes < 12) return BadLength;
407 memcpy (&n_settings, prop+8, 4);
408 if (my_bo != that_bo) n_settings = swap32 (n_settings);
409 bytes_parsed = 12;
411 memset (settings, 0, sizeof (*settings));
413 while (bytes_parsed+4 < bytes && settings_seen < 7
414 && i < n_settings)
416 int type = prop[bytes_parsed++];
417 CARD16 nlen;
418 CARD32 vlen, ival = 0;
419 char name[128]; /* The names we are looking for are not this long. */
420 char sval[128]; /* The values we are looking for are not this long. */
421 int want_this;
422 int to_cpy;
424 sval[0] = '\0';
425 ++i;
426 ++bytes_parsed; /* Padding */
428 memcpy (&nlen, prop+bytes_parsed, 2);
429 bytes_parsed += 2;
430 if (my_bo != that_bo) nlen = swap16 (nlen);
431 if (bytes_parsed+nlen > bytes) return BadLength;
432 to_cpy = nlen > 127 ? 127 : nlen;
433 memcpy (name, prop+bytes_parsed, to_cpy);
434 name[to_cpy] = '\0';
436 bytes_parsed += nlen;
437 bytes_parsed = PAD (bytes_parsed);
439 bytes_parsed += 4; /* Skip serial for this value */
440 if (bytes_parsed > bytes) return BadLength;
442 want_this =
443 #ifdef HAVE_XFT
444 (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
445 || strcmp (XSETTINGS_FONT_NAME, name) == 0
447 #endif
448 strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
450 switch (type)
452 case 0: /* Integer */
453 if (bytes_parsed+4 > bytes) return BadLength;
454 if (want_this)
456 memcpy (&ival, prop+bytes_parsed, 4);
457 if (my_bo != that_bo) ival = swap32 (ival);
459 bytes_parsed += 4;
460 break;
462 case 1: /* String */
463 if (bytes_parsed+4 > bytes) return BadLength;
464 memcpy (&vlen, prop+bytes_parsed, 4);
465 bytes_parsed += 4;
466 if (my_bo != that_bo) vlen = swap32 (vlen);
467 if (want_this)
469 to_cpy = vlen > 127 ? 127 : vlen;
470 memcpy (sval, prop+bytes_parsed, to_cpy);
471 sval[to_cpy] = '\0';
473 bytes_parsed += vlen;
474 bytes_parsed = PAD (bytes_parsed);
475 break;
477 case 2: /* RGB value */
478 /* No need to parse this */
479 if (bytes_parsed+8 > bytes) return BadLength;
480 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
481 break;
483 default: /* Parse Error */
484 return BadValue;
487 if (want_this)
489 ++settings_seen;
490 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
492 settings->tb_style = xstrdup (sval);
493 settings->seen |= SEEN_TB_STYLE;
495 #ifdef HAVE_XFT
496 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
498 settings->font = xstrdup (sval);
499 settings->seen |= SEEN_FONT;
501 else if (strcmp (name, "Xft/Antialias") == 0)
503 settings->seen |= SEEN_AA;
504 settings->aa = ival != 0;
506 else if (strcmp (name, "Xft/Hinting") == 0)
508 settings->seen |= SEEN_HINTING;
509 settings->hinting = ival != 0;
511 # ifdef FC_HINT_STYLE
512 else if (strcmp (name, "Xft/HintStyle") == 0)
514 settings->seen |= SEEN_HINTSTYLE;
515 if (strcmp (sval, "hintnone") == 0)
516 settings->hintstyle = FC_HINT_NONE;
517 else if (strcmp (sval, "hintslight") == 0)
518 settings->hintstyle = FC_HINT_SLIGHT;
519 else if (strcmp (sval, "hintmedium") == 0)
520 settings->hintstyle = FC_HINT_MEDIUM;
521 else if (strcmp (sval, "hintfull") == 0)
522 settings->hintstyle = FC_HINT_FULL;
523 else
524 settings->seen &= ~SEEN_HINTSTYLE;
526 # endif
527 else if (strcmp (name, "Xft/RGBA") == 0)
529 settings->seen |= SEEN_RGBA;
530 if (strcmp (sval, "none") == 0)
531 settings->rgba = FC_RGBA_NONE;
532 else if (strcmp (sval, "rgb") == 0)
533 settings->rgba = FC_RGBA_RGB;
534 else if (strcmp (sval, "bgr") == 0)
535 settings->rgba = FC_RGBA_BGR;
536 else if (strcmp (sval, "vrgb") == 0)
537 settings->rgba = FC_RGBA_VRGB;
538 else if (strcmp (sval, "vbgr") == 0)
539 settings->rgba = FC_RGBA_VBGR;
540 else
541 settings->seen &= ~SEEN_RGBA;
543 else if (strcmp (name, "Xft/DPI") == 0)
545 settings->seen |= SEEN_DPI;
546 settings->dpi = (double)ival/1024.0;
548 else if (strcmp (name, "Xft/lcdfilter") == 0)
550 settings->seen |= SEEN_LCDFILTER;
551 if (strcmp (sval, "none") == 0)
552 settings->lcdfilter = FC_LCD_NONE;
553 else if (strcmp (sval, "lcddefault") == 0)
554 settings->lcdfilter = FC_LCD_DEFAULT;
555 else
556 settings->seen &= ~SEEN_LCDFILTER;
558 #endif /* HAVE_XFT */
562 return settings_seen;
565 /* Read settings from the XSettings property window on display for DPYINFO.
566 Store settings read in SETTINGS.
567 Return non-zero if successful, zero if not. */
569 static int
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;
579 x_catch_errors (dpy);
580 rc = XGetWindowProperty (dpy,
581 dpyinfo->xsettings_window,
582 dpyinfo->Xatom_xsettings_prop,
583 0, LONG_MAX, False, AnyPropertyType,
584 &act_type, &act_form, &nitems, &bytes_after,
585 &prop);
587 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
588 && act_type == dpyinfo->Xatom_xsettings_prop)
589 rc = parse_settings (prop, nitems, settings);
591 XFree (prop);
593 x_uncatch_errors ();
595 return rc != 0;
598 /* Apply Xft settings in SETTINGS to the Xft library.
599 If SEND_EVENT_P is non-zero store a Lisp event that Xft settings changed. */
601 static void
602 apply_xft_settings (struct x_display_info *dpyinfo,
603 int send_event_p,
604 struct xsettings *settings)
606 #ifdef HAVE_XFT
607 FcPattern *pat;
608 struct xsettings oldsettings;
609 int changed = 0;
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;
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;
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;
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;
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;
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;
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 if (send_event_p)
702 store_config_changed_event (Qfont_render,
703 XCAR (dpyinfo->name_list_element));
704 Vxft_settings
705 = make_formatted_string (buf, format,
706 oldsettings.aa, oldsettings.hinting,
707 oldsettings.rgba, oldsettings.lcdfilter,
708 oldsettings.hintstyle, oldsettings.dpi);
711 else
712 FcPatternDestroy (pat);
713 #endif /* HAVE_XFT */
716 /* Read XSettings from the display for DPYINFO.
717 If SEND_EVENT_P is non-zero store a Lisp event settings that changed. */
719 static void
720 read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
722 struct xsettings settings;
724 if (!read_settings (dpyinfo, &settings))
725 return;
727 apply_xft_settings (dpyinfo, True, &settings);
728 if (settings.seen & SEEN_TB_STYLE)
730 if (send_event_p)
731 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
732 else
733 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
734 xfree (settings.tb_style);
736 #ifdef HAVE_XFT
737 if (settings.seen & SEEN_FONT)
739 if (send_event_p)
740 store_font_name_changed (settings.font);
741 else
743 xfree (current_font);
744 current_font = xstrdup (settings.font);
746 xfree (settings.font);
748 #endif
751 /* Check if EVENT for the display in DPYINFO is XSettings related. */
753 void
754 xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
756 bool check_window_p = 0, apply_settings_p = 0;
758 switch (event->type)
760 case DestroyNotify:
761 if (dpyinfo->xsettings_window == event->xany.window)
762 check_window_p = 1;
763 break;
765 case ClientMessage:
766 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
767 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
768 && event->xclient.window == dpyinfo->root_window)
769 check_window_p = 1;
770 break;
772 case PropertyNotify:
773 if (event->xproperty.window == dpyinfo->xsettings_window
774 && event->xproperty.state == PropertyNewValue
775 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
776 apply_settings_p = 1;
777 break;
781 if (check_window_p)
783 dpyinfo->xsettings_window = None;
784 get_prop_window (dpyinfo);
785 if (dpyinfo->xsettings_window != None)
786 apply_settings_p = 1;
789 if (apply_settings_p)
790 read_and_apply_settings (dpyinfo, True);
793 /* Initialize GSettings and read startup values. */
795 static void
796 init_gsettings (void)
798 #ifdef HAVE_GSETTINGS
799 GVariant *val;
800 const gchar *const *schemas;
801 int schema_found = 0;
803 #if ! GLIB_CHECK_VERSION (2, 36, 0)
804 g_type_init ();
805 #endif
807 schemas = g_settings_list_schemas ();
808 if (schemas == NULL) return;
809 while (! schema_found && *schemas != NULL)
810 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
811 if (!schema_found) return;
813 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
814 if (!gsettings_client) return;
815 g_object_ref_sink (G_OBJECT (gsettings_client));
816 g_signal_connect (G_OBJECT (gsettings_client), "changed",
817 G_CALLBACK (something_changed_gsettingsCB), NULL);
819 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
820 if (val)
822 g_variant_ref_sink (val);
823 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
824 current_tool_bar_style
825 = map_tool_bar_style (g_variant_get_string (val, NULL));
826 g_variant_unref (val);
829 #ifdef HAVE_XFT
830 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
831 if (val)
833 g_variant_ref_sink (val);
834 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
835 current_mono_font = xstrdup (g_variant_get_string (val, NULL));
836 g_variant_unref (val);
839 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
840 if (val)
842 g_variant_ref_sink (val);
843 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
844 current_font = xstrdup (g_variant_get_string (val, NULL));
845 g_variant_unref (val);
847 #endif /* HAVE_XFT */
849 #endif /* HAVE_GSETTINGS */
852 /* Init GConf and read startup values. */
854 static void
855 init_gconf (void)
857 #if defined (HAVE_GCONF)
858 char *s;
860 #if ! GLIB_CHECK_VERSION (2, 36, 0)
861 g_type_init ();
862 #endif
864 gconf_client = gconf_client_get_default ();
865 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
866 gconf_client_add_dir (gconf_client,
867 GCONF_TOOL_BAR_STYLE,
868 GCONF_CLIENT_PRELOAD_ONELEVEL,
869 NULL);
870 gconf_client_notify_add (gconf_client,
871 GCONF_TOOL_BAR_STYLE,
872 something_changed_gconfCB,
873 NULL, NULL, NULL);
875 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
876 if (s)
878 current_tool_bar_style = map_tool_bar_style (s);
879 g_free (s);
882 #ifdef HAVE_XFT
883 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
884 if (s)
886 current_mono_font = xstrdup (s);
887 g_free (s);
889 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
890 if (s)
892 current_font = xstrdup (s);
893 g_free (s);
895 gconf_client_add_dir (gconf_client,
896 GCONF_MONO_FONT,
897 GCONF_CLIENT_PRELOAD_ONELEVEL,
898 NULL);
899 gconf_client_notify_add (gconf_client,
900 GCONF_MONO_FONT,
901 something_changed_gconfCB,
902 NULL, NULL, NULL);
903 gconf_client_add_dir (gconf_client,
904 GCONF_FONT_NAME,
905 GCONF_CLIENT_PRELOAD_ONELEVEL,
906 NULL);
907 gconf_client_notify_add (gconf_client,
908 GCONF_FONT_NAME,
909 something_changed_gconfCB,
910 NULL, NULL, NULL);
911 #endif /* HAVE_XFT */
912 #endif /* HAVE_GCONF */
915 /* Init Xsettings and read startup values. */
917 static void
918 init_xsettings (struct x_display_info *dpyinfo)
920 Display *dpy = dpyinfo->display;
922 block_input ();
924 /* Select events so we can detect client messages sent when selection
925 owner changes. */
926 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
928 get_prop_window (dpyinfo);
929 if (dpyinfo->xsettings_window != None)
930 read_and_apply_settings (dpyinfo, False);
932 unblock_input ();
935 void
936 xsettings_initialize (struct x_display_info *dpyinfo)
938 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
939 init_gconf ();
940 init_xsettings (dpyinfo);
941 init_gsettings ();
944 /* Return the system monospaced font.
945 May be NULL if not known. */
947 const char *
948 xsettings_get_system_font (void)
950 return current_mono_font;
953 #ifdef USE_LUCID
954 /* Return the system font.
955 May be NULL if not known. */
957 const char *
958 xsettings_get_system_normal_font (void)
960 return current_font;
962 #endif
964 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
965 Sfont_get_system_normal_font,
966 0, 0, 0,
967 doc: /* Get the system default application font. */)
968 (void)
970 return current_font ? build_string (current_font) : Qnil;
973 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
974 0, 0, 0,
975 doc: /* Get the system default fixed width font. */)
976 (void)
978 return current_mono_font ? build_string (current_mono_font) : Qnil;
981 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
982 Stool_bar_get_system_style, 0, 0, 0,
983 doc: /* Get the system tool bar style.
984 If no system tool bar style is known, return `tool-bar-style' if set to a
985 known style. Otherwise return image. */)
986 (void)
988 if (EQ (Vtool_bar_style, Qimage)
989 || EQ (Vtool_bar_style, Qtext)
990 || EQ (Vtool_bar_style, Qboth)
991 || EQ (Vtool_bar_style, Qboth_horiz)
992 || EQ (Vtool_bar_style, Qtext_image_horiz))
993 return Vtool_bar_style;
994 if (!NILP (current_tool_bar_style))
995 return current_tool_bar_style;
996 return Qimage;
999 void
1000 syms_of_xsettings (void)
1002 current_mono_font = NULL;
1003 current_font = NULL;
1004 first_dpyinfo = NULL;
1005 #ifdef HAVE_GSETTINGS
1006 gsettings_client = NULL;
1007 #endif
1008 #ifdef HAVE_GCONF
1009 gconf_client = NULL;
1010 #endif
1012 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1013 DEFSYM (Qfont_name, "font-name");
1014 DEFSYM (Qfont_render, "font-render");
1015 defsubr (&Sfont_get_system_font);
1016 defsubr (&Sfont_get_system_normal_font);
1018 DEFVAR_BOOL ("font-use-system-font", use_system_font,
1019 doc: /* Non-nil means to apply the system defined font dynamically.
1020 When this is non-nil and the system defined fixed width font changes, we
1021 update frames dynamically.
1022 If this variable is nil, Emacs ignores system font changes. */);
1023 use_system_font = 0;
1025 DEFVAR_LISP ("xft-settings", Vxft_settings,
1026 doc: /* Font settings applied to Xft. */);
1027 Vxft_settings = empty_unibyte_string;
1029 #ifdef HAVE_XFT
1030 Fprovide (intern_c_string ("font-render-setting"), Qnil);
1031 #if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
1032 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1033 #endif
1034 #endif
1036 current_tool_bar_style = Qnil;
1037 DEFSYM (Qtool_bar_style, "tool-bar-style");
1038 defsubr (&Stool_bar_get_system_style);
1040 Fprovide (intern_c_string ("dynamic-setting"), Qnil);