]> code.delx.au - gnu-emacs/blob - src/xsettings.c
Merge from origin/emacs-24
[gnu-emacs] / src / xsettings.c
1 /* Functions for handling font and other changes dynamically.
2
3 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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/>. */
19
20 #include <config.h>
21
22 #include <float.h>
23 #include <limits.h>
24 #include <fcntl.h>
25
26 #include <byteswap.h>
27
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"
35
36 #include <X11/Xproto.h>
37
38 #ifdef HAVE_GSETTINGS
39 #include <glib-object.h>
40 #include <gio/gio.h>
41 #endif
42
43 #ifdef HAVE_GCONF
44 #include <gconf/gconf-client.h>
45 #endif
46
47 #ifdef HAVE_XFT
48 #include <X11/Xft/Xft.h>
49 #endif
50
51 static char *current_mono_font;
52 static char *current_font;
53 static struct x_display_info *first_dpyinfo;
54 static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render,
55 Qtool_bar_style;
56 static Lisp_Object current_tool_bar_style;
57
58 /* Store an config changed event in to the event queue. */
59
60 static void
61 store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
62 {
63 struct input_event event;
64 EVENT_INIT (event);
65 event.kind = CONFIG_CHANGED_EVENT;
66 event.frame_or_window = display_name;
67 event.arg = arg;
68 kbd_buffer_store_event (&event);
69 }
70
71 /* Return true if DPYINFO is still valid. */
72 static bool
73 dpyinfo_valid (struct x_display_info *dpyinfo)
74 {
75 bool found = false;
76 if (dpyinfo != NULL)
77 {
78 struct x_display_info *d;
79 for (d = x_display_list; !found && d; d = d->next)
80 found = d == dpyinfo && d->display == dpyinfo->display;
81 }
82 return found;
83 }
84
85 /* Store a monospace font change event if the monospaced font changed. */
86
87 #if defined HAVE_XFT && (defined HAVE_GSETTINGS || defined HAVE_GCONF)
88 static void
89 store_monospaced_changed (const char *newfont)
90 {
91 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
92 return; /* No change. */
93
94 dupstring (&current_mono_font, newfont);
95
96 if (dpyinfo_valid (first_dpyinfo) && use_system_font)
97 {
98 store_config_changed_event (Qmonospace_font_name,
99 XCAR (first_dpyinfo->name_list_element));
100 }
101 }
102 #endif
103
104 /* Store a font name change event if the font name changed. */
105
106 #ifdef HAVE_XFT
107 static void
108 store_font_name_changed (const char *newfont)
109 {
110 if (current_font != NULL && strcmp (newfont, current_font) == 0)
111 return; /* No change. */
112
113 dupstring (&current_font, newfont);
114
115 if (dpyinfo_valid (first_dpyinfo))
116 {
117 store_config_changed_event (Qfont_name,
118 XCAR (first_dpyinfo->name_list_element));
119 }
120 }
121 #endif /* HAVE_XFT */
122
123 /* Map TOOL_BAR_STYLE from a string to its corresponding Lisp value.
124 Return Qnil if TOOL_BAR_STYLE is not known. */
125
126 static Lisp_Object
127 map_tool_bar_style (const char *tool_bar_style)
128 {
129 Lisp_Object style = Qnil;
130 if (tool_bar_style)
131 {
132 if (strcmp (tool_bar_style, "both") == 0)
133 style = Qboth;
134 else if (strcmp (tool_bar_style, "both-horiz") == 0)
135 style = Qboth_horiz;
136 else if (strcmp (tool_bar_style, "icons") == 0)
137 style = Qimage;
138 else if (strcmp (tool_bar_style, "text") == 0)
139 style = Qtext;
140 }
141
142 return style;
143 }
144
145 /* Store a tool bar style change event if the tool bar style changed. */
146
147 static void
148 store_tool_bar_style_changed (const char *newstyle,
149 struct x_display_info *dpyinfo)
150 {
151 Lisp_Object style = map_tool_bar_style (newstyle);
152 if (EQ (current_tool_bar_style, style))
153 return; /* No change. */
154
155 current_tool_bar_style = style;
156 if (dpyinfo_valid (dpyinfo))
157 store_config_changed_event (Qtool_bar_style,
158 XCAR (dpyinfo->name_list_element));
159 }
160
161 #ifdef HAVE_XFT
162 #define XSETTINGS_FONT_NAME "Gtk/FontName"
163 #endif
164 #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
165
166 enum {
167 SEEN_AA = 0x01,
168 SEEN_HINTING = 0x02,
169 SEEN_RGBA = 0x04,
170 SEEN_LCDFILTER = 0x08,
171 SEEN_HINTSTYLE = 0x10,
172 SEEN_DPI = 0x20,
173 SEEN_FONT = 0x40,
174 SEEN_TB_STYLE = 0x80
175 };
176 struct xsettings
177 {
178 #ifdef HAVE_XFT
179 FcBool aa, hinting;
180 int rgba, lcdfilter, hintstyle;
181 double dpi;
182
183 char *font;
184 #endif
185
186 char *tb_style;
187
188 unsigned seen;
189 };
190
191 #ifdef HAVE_GSETTINGS
192 #define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
193 #define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"
194
195 #ifdef HAVE_XFT
196 #define GSETTINGS_MONO_FONT "monospace-font-name"
197 #define GSETTINGS_FONT_NAME "font-name"
198 #endif
199
200
201 /* The single GSettings instance, or NULL if not connected to GSettings. */
202
203 static GSettings *gsettings_client;
204
205 /* Callback called when something changed in GSettings. */
206
207 static void
208 something_changed_gsettingsCB (GSettings *settings,
209 gchar *key,
210 gpointer user_data)
211 {
212 GVariant *val;
213
214 if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
215 {
216 val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
217 if (val)
218 {
219 g_variant_ref_sink (val);
220 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
221 {
222 const gchar *newstyle = g_variant_get_string (val, NULL);
223 store_tool_bar_style_changed (newstyle, first_dpyinfo);
224 }
225 g_variant_unref (val);
226 }
227 }
228 #ifdef HAVE_XFT
229 else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
230 {
231 val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
232 if (val)
233 {
234 g_variant_ref_sink (val);
235 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
236 {
237 const gchar *newfont = g_variant_get_string (val, NULL);
238 store_monospaced_changed (newfont);
239 }
240 g_variant_unref (val);
241 }
242 }
243 else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
244 {
245 val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
246 if (val)
247 {
248 g_variant_ref_sink (val);
249 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
250 {
251 const gchar *newfont = g_variant_get_string (val, NULL);
252 store_font_name_changed (newfont);
253 }
254 g_variant_unref (val);
255 }
256 }
257 #endif /* HAVE_XFT */
258 }
259
260 #endif /* HAVE_GSETTINGS */
261
262 #ifdef HAVE_GCONF
263 #define GCONF_TOOL_BAR_STYLE "/desktop/gnome/interface/toolbar_style"
264 #ifdef HAVE_XFT
265 #define GCONF_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
266 #define GCONF_FONT_NAME "/desktop/gnome/interface/font_name"
267 #endif
268
269 /* The single GConf instance, or NULL if not connected to GConf. */
270
271 static GConfClient *gconf_client;
272
273 /* Callback called when something changed in GConf that we care about. */
274
275 static void
276 something_changed_gconfCB (GConfClient *client,
277 guint cnxn_id,
278 GConfEntry *entry,
279 gpointer user_data)
280 {
281 GConfValue *v = gconf_entry_get_value (entry);
282 const char *key = gconf_entry_get_key (entry);
283
284 if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
285 if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
286 {
287 const char *value = gconf_value_get_string (v);
288 store_tool_bar_style_changed (value, first_dpyinfo);
289 }
290 #ifdef HAVE_XFT
291 else if (strcmp (key, GCONF_MONO_FONT) == 0)
292 {
293 const char *value = gconf_value_get_string (v);
294 store_monospaced_changed (value);
295 }
296 else if (strcmp (key, GCONF_FONT_NAME) == 0)
297 {
298 const char *value = gconf_value_get_string (v);
299 store_font_name_changed (value);
300 }
301 #endif /* HAVE_XFT */
302 }
303
304 #endif /* HAVE_GCONF */
305
306 #ifdef HAVE_XFT
307
308 /* Older fontconfig versions don't have FC_LCD_*. */
309 #ifndef FC_LCD_NONE
310 #define FC_LCD_NONE 0
311 #endif
312 #ifndef FC_LCD_DEFAULT
313 #define FC_LCD_DEFAULT 1
314 #endif
315 #ifndef FC_LCD_FILTER
316 #define FC_LCD_FILTER "lcdfilter"
317 #endif
318
319 #endif /* HAVE_XFT */
320
321 /* Find the window that contains the XSETTINGS property values. */
322
323 static void
324 get_prop_window (struct x_display_info *dpyinfo)
325 {
326 Display *dpy = dpyinfo->display;
327
328 XGrabServer (dpy);
329 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
330 dpyinfo->Xatom_xsettings_sel);
331 if (dpyinfo->xsettings_window != None)
332 /* Select events so we can detect if window is deleted or if settings
333 are changed. */
334 XSelectInput (dpy, dpyinfo->xsettings_window,
335 PropertyChangeMask|StructureNotifyMask);
336
337 XUngrabServer (dpy);
338 }
339
340 #define PAD(nr) (((nr) + 3) & ~3)
341
342 /* Parse xsettings and extract those that deal with Xft.
343 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
344 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
345
346 Layout of prop. First is a header:
347
348 bytes type what
349 ------------------------------------
350 1 CARD8 byte-order
351 3 unused
352 4 CARD32 SERIAL
353 4 CARD32 N_SETTINGS
354
355 Then N_SETTINGS records, with header:
356
357 bytes type what
358 ------------------------------------
359 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
360 1 unused
361 2 CARD16 n == name-length
362 n STRING8 name
363 p unused, p=pad_to_even_4(n)
364 4 CARD32 last-change-serial
365
366 and then the value, For string:
367
368 bytes type what
369 ------------------------------------
370 4 CARD32 n = value-length
371 n STRING8 value
372 p unused, p=pad_to_even_4(n)
373
374 For integer:
375
376 bytes type what
377 ------------------------------------
378 4 INT32 value
379
380 For RGB color:
381
382 bytes type what
383 ------------------------------------
384 2 CARD16 red
385 2 CARD16 blue
386 2 CARD16 green
387 2 CARD16 alpha
388
389 Returns non-zero if some Xft settings was seen, zero otherwise.
390 */
391
392 static int
393 parse_settings (unsigned char *prop,
394 unsigned long bytes,
395 struct xsettings *settings)
396 {
397 Lisp_Object byteorder = Fbyteorder ();
398 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
399 int that_bo = prop[0];
400 CARD32 n_settings;
401 int bytes_parsed = 0;
402 int settings_seen = 0;
403 int i = 0;
404
405 /* First 4 bytes is a serial number, skip that. */
406
407 if (bytes < 12) return BadLength;
408 memcpy (&n_settings, prop+8, 4);
409 if (my_bo != that_bo) n_settings = bswap_32 (n_settings);
410 bytes_parsed = 12;
411
412 memset (settings, 0, sizeof (*settings));
413
414 while (bytes_parsed+4 < bytes && settings_seen < 7
415 && i < n_settings)
416 {
417 int type = prop[bytes_parsed++];
418 CARD16 nlen;
419 CARD32 vlen, ival = 0;
420 char name[128]; /* The names we are looking for are not this long. */
421 char sval[128]; /* The values we are looking for are not this long. */
422 bool want_this;
423 int to_cpy;
424
425 sval[0] = '\0';
426 ++i;
427 ++bytes_parsed; /* Padding */
428
429 memcpy (&nlen, prop+bytes_parsed, 2);
430 bytes_parsed += 2;
431 if (my_bo != that_bo) nlen = bswap_16 (nlen);
432 if (bytes_parsed+nlen > bytes) return BadLength;
433 to_cpy = nlen > 127 ? 127 : nlen;
434 memcpy (name, prop+bytes_parsed, to_cpy);
435 name[to_cpy] = '\0';
436
437 bytes_parsed += nlen;
438 bytes_parsed = PAD (bytes_parsed);
439
440 bytes_parsed += 4; /* Skip serial for this value */
441 if (bytes_parsed > bytes) return BadLength;
442
443 want_this =
444 #ifdef HAVE_XFT
445 (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
446 || strcmp (XSETTINGS_FONT_NAME, name) == 0
447 ||
448 #endif
449 strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
450
451 switch (type)
452 {
453 case 0: /* Integer */
454 if (bytes_parsed+4 > bytes) return BadLength;
455 if (want_this)
456 {
457 memcpy (&ival, prop+bytes_parsed, 4);
458 if (my_bo != that_bo) ival = bswap_32 (ival);
459 }
460 bytes_parsed += 4;
461 break;
462
463 case 1: /* String */
464 if (bytes_parsed+4 > bytes) return BadLength;
465 memcpy (&vlen, prop+bytes_parsed, 4);
466 bytes_parsed += 4;
467 if (my_bo != that_bo) vlen = bswap_32 (vlen);
468 if (want_this)
469 {
470 to_cpy = vlen > 127 ? 127 : vlen;
471 memcpy (sval, prop+bytes_parsed, to_cpy);
472 sval[to_cpy] = '\0';
473 }
474 bytes_parsed += vlen;
475 bytes_parsed = PAD (bytes_parsed);
476 break;
477
478 case 2: /* RGB value */
479 /* No need to parse this */
480 if (bytes_parsed+8 > bytes) return BadLength;
481 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
482 break;
483
484 default: /* Parse Error */
485 return BadValue;
486 }
487
488 if (want_this)
489 {
490 ++settings_seen;
491 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
492 {
493 dupstring (&settings->tb_style, sval);
494 settings->seen |= SEEN_TB_STYLE;
495 }
496 #ifdef HAVE_XFT
497 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
498 {
499 dupstring (&settings->font, sval);
500 settings->seen |= SEEN_FONT;
501 }
502 else if (strcmp (name, "Xft/Antialias") == 0)
503 {
504 settings->seen |= SEEN_AA;
505 settings->aa = ival != 0;
506 }
507 else if (strcmp (name, "Xft/Hinting") == 0)
508 {
509 settings->seen |= SEEN_HINTING;
510 settings->hinting = ival != 0;
511 }
512 # ifdef FC_HINT_STYLE
513 else if (strcmp (name, "Xft/HintStyle") == 0)
514 {
515 settings->seen |= SEEN_HINTSTYLE;
516 if (strcmp (sval, "hintnone") == 0)
517 settings->hintstyle = FC_HINT_NONE;
518 else if (strcmp (sval, "hintslight") == 0)
519 settings->hintstyle = FC_HINT_SLIGHT;
520 else if (strcmp (sval, "hintmedium") == 0)
521 settings->hintstyle = FC_HINT_MEDIUM;
522 else if (strcmp (sval, "hintfull") == 0)
523 settings->hintstyle = FC_HINT_FULL;
524 else
525 settings->seen &= ~SEEN_HINTSTYLE;
526 }
527 # endif
528 else if (strcmp (name, "Xft/RGBA") == 0)
529 {
530 settings->seen |= SEEN_RGBA;
531 if (strcmp (sval, "none") == 0)
532 settings->rgba = FC_RGBA_NONE;
533 else if (strcmp (sval, "rgb") == 0)
534 settings->rgba = FC_RGBA_RGB;
535 else if (strcmp (sval, "bgr") == 0)
536 settings->rgba = FC_RGBA_BGR;
537 else if (strcmp (sval, "vrgb") == 0)
538 settings->rgba = FC_RGBA_VRGB;
539 else if (strcmp (sval, "vbgr") == 0)
540 settings->rgba = FC_RGBA_VBGR;
541 else
542 settings->seen &= ~SEEN_RGBA;
543 }
544 else if (strcmp (name, "Xft/DPI") == 0)
545 {
546 settings->seen |= SEEN_DPI;
547 settings->dpi = (double)ival/1024.0;
548 }
549 else if (strcmp (name, "Xft/lcdfilter") == 0)
550 {
551 settings->seen |= SEEN_LCDFILTER;
552 if (strcmp (sval, "none") == 0)
553 settings->lcdfilter = FC_LCD_NONE;
554 else if (strcmp (sval, "lcddefault") == 0)
555 settings->lcdfilter = FC_LCD_DEFAULT;
556 else
557 settings->seen &= ~SEEN_LCDFILTER;
558 }
559 #endif /* HAVE_XFT */
560 }
561 }
562
563 return settings_seen;
564 }
565
566 /* Read settings from the XSettings property window on display for DPYINFO.
567 Store settings read in SETTINGS.
568 Return true iff successful. */
569
570 static bool
571 read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
572 {
573 Atom act_type;
574 int act_form;
575 unsigned long nitems, bytes_after;
576 unsigned char *prop = NULL;
577 Display *dpy = dpyinfo->display;
578 int rc;
579
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);
587
588 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
589 && act_type == dpyinfo->Xatom_xsettings_prop)
590 rc = parse_settings (prop, nitems, settings);
591
592 XFree (prop);
593
594 x_uncatch_errors ();
595
596 return rc != 0;
597 }
598
599 /* Apply Xft settings in SETTINGS to the Xft library.
600 Store a Lisp event that Xft settings changed. */
601
602 static void
603 apply_xft_settings (struct x_display_info *dpyinfo,
604 struct xsettings *settings)
605 {
606 #ifdef HAVE_XFT
607 FcPattern *pat;
608 struct xsettings oldsettings;
609 bool changed = false;
610
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);
624
625 if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
626 {
627 FcPatternDel (pat, FC_ANTIALIAS);
628 FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
629 changed = true;
630 oldsettings.aa = settings->aa;
631 }
632
633 if ((settings->seen & SEEN_HINTING) != 0
634 && oldsettings.hinting != settings->hinting)
635 {
636 FcPatternDel (pat, FC_HINTING);
637 FcPatternAddBool (pat, FC_HINTING, settings->hinting);
638 changed = true;
639 oldsettings.hinting = settings->hinting;
640 }
641 if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
642 {
643 FcPatternDel (pat, FC_RGBA);
644 FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
645 oldsettings.rgba = settings->rgba;
646 changed = true;
647 }
648
649 /* Older fontconfig versions don't have FC_LCD_FILTER. */
650 if ((settings->seen & SEEN_LCDFILTER) != 0
651 && oldsettings.lcdfilter != settings->lcdfilter)
652 {
653 FcPatternDel (pat, FC_LCD_FILTER);
654 FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
655 changed = true;
656 oldsettings.lcdfilter = settings->lcdfilter;
657 }
658
659 #ifdef FC_HINT_STYLE
660 if ((settings->seen & SEEN_HINTSTYLE) != 0
661 && oldsettings.hintstyle != settings->hintstyle)
662 {
663 FcPatternDel (pat, FC_HINT_STYLE);
664 FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
665 changed = true;
666 oldsettings.hintstyle = settings->hintstyle;
667 }
668 #endif
669
670 if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi
671 && settings->dpi > 0)
672 {
673 FcPatternDel (pat, FC_DPI);
674 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
675 changed = true;
676 oldsettings.dpi = settings->dpi;
677
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;
681 }
682
683 if (changed)
684 {
685 static char const format[] =
686 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
687 "Hintstyle: %d, DPI: %f";
688 enum
689 {
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")
697 };
698 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
699
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);
708
709 }
710 else
711 FcPatternDestroy (pat);
712 #endif /* HAVE_XFT */
713 }
714
715 /* Read XSettings from the display for DPYINFO.
716 If SEND_EVENT_P store a Lisp event settings that changed. */
717
718 static void
719 read_and_apply_settings (struct x_display_info *dpyinfo, bool send_event_p)
720 {
721 struct xsettings settings;
722
723 if (!read_settings (dpyinfo, &settings))
724 return;
725
726 apply_xft_settings (dpyinfo, &settings);
727 if (settings.seen & SEEN_TB_STYLE)
728 {
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);
734 }
735 #ifdef HAVE_XFT
736 if (settings.seen & SEEN_FONT)
737 {
738 if (send_event_p)
739 store_font_name_changed (settings.font);
740 else
741 dupstring (&current_font, settings.font);
742 xfree (settings.font);
743 }
744 #endif
745 }
746
747 /* Check if EVENT for the display in DPYINFO is XSettings related. */
748
749 void
750 xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
751 {
752 bool check_window_p = false, apply_settings_p = false;
753
754 switch (event->type)
755 {
756 case DestroyNotify:
757 if (dpyinfo->xsettings_window == event->xany.window)
758 check_window_p = true;
759 break;
760
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;
767
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;
774 }
775
776
777 if (check_window_p)
778 {
779 dpyinfo->xsettings_window = None;
780 get_prop_window (dpyinfo);
781 if (dpyinfo->xsettings_window != None)
782 apply_settings_p = true;
783 }
784
785 if (apply_settings_p)
786 read_and_apply_settings (dpyinfo, true);
787 }
788
789 /* Initialize GSettings and read startup values. */
790
791 static void
792 init_gsettings (void)
793 {
794 #ifdef HAVE_GSETTINGS
795 GVariant *val;
796 bool schema_found = false;
797
798 #if ! GLIB_CHECK_VERSION (2, 36, 0)
799 g_type_init ();
800 #endif
801
802 #if GLIB_CHECK_VERSION (2, 32, 0)
803 {
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);
810 }
811 #else
812 {
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;
817 }
818 #endif
819 if (!schema_found) return;
820
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);
826
827 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
828 if (val)
829 {
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);
835 }
836
837 #ifdef HAVE_XFT
838 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
839 if (val)
840 {
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);
845 }
846
847 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
848 if (val)
849 {
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);
854 }
855 #endif /* HAVE_XFT */
856
857 #endif /* HAVE_GSETTINGS */
858 }
859
860 /* Init GConf and read startup values. */
861
862 static void
863 init_gconf (void)
864 {
865 #if defined (HAVE_GCONF)
866 char *s;
867
868 #if ! GLIB_CHECK_VERSION (2, 36, 0)
869 g_type_init ();
870 #endif
871
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);
882
883 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
884 if (s)
885 {
886 current_tool_bar_style = map_tool_bar_style (s);
887 g_free (s);
888 }
889
890 #ifdef HAVE_XFT
891 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
892 if (s)
893 {
894 dupstring (&current_mono_font, s);
895 g_free (s);
896 }
897 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
898 if (s)
899 {
900 dupstring (&current_font, s);
901 g_free (s);
902 }
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 */
921 }
922
923 /* Init Xsettings and read startup values. */
924
925 static void
926 init_xsettings (struct x_display_info *dpyinfo)
927 {
928 Display *dpy = dpyinfo->display;
929
930 block_input ();
931
932 /* Select events so we can detect client messages sent when selection
933 owner changes. */
934 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
935
936 get_prop_window (dpyinfo);
937 if (dpyinfo->xsettings_window != None)
938 read_and_apply_settings (dpyinfo, false);
939
940 unblock_input ();
941 }
942
943 void
944 xsettings_initialize (struct x_display_info *dpyinfo)
945 {
946 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
947 init_gconf ();
948 init_xsettings (dpyinfo);
949 init_gsettings ();
950 }
951
952 /* Return the system monospaced font.
953 May be NULL if not known. */
954
955 const char *
956 xsettings_get_system_font (void)
957 {
958 return current_mono_font;
959 }
960
961 #ifdef USE_LUCID
962 /* Return the system font.
963 May be NULL if not known. */
964
965 const char *
966 xsettings_get_system_normal_font (void)
967 {
968 return current_font;
969 }
970 #endif
971
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)
977 {
978 return current_font ? build_string (current_font) : Qnil;
979 }
980
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)
985 {
986 return current_mono_font ? build_string (current_mono_font) : Qnil;
987 }
988
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)
995 {
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;
1005 }
1006
1007 void
1008 syms_of_xsettings (void)
1009 {
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
1019
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);
1025
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;
1032
1033 DEFVAR_LISP ("xft-settings", Vxft_settings,
1034 doc: /* Font settings applied to Xft. */);
1035 Vxft_settings = empty_unibyte_string;
1036
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
1043
1044 current_tool_bar_style = Qnil;
1045 DEFSYM (Qtool_bar_style, "tool-bar-style");
1046 defsubr (&Stool_bar_get_system_style);
1047
1048 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
1049 }