]> code.delx.au - gnu-emacs/blob - src/xsettings.c
Update copyright year to 2015
[gnu-emacs] / src / xsettings.c
1 /* Functions for handling font and other changes dynamically.
2
3 Copyright (C) 2009-2015 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 settings_seen;
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 settings_seen;
433 to_cpy = min (nlen, sizeof name - 1);
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 settings_seen;
442
443 want_this = strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
444 #ifdef HAVE_XFT
445 if ((nlen > 6 && memcmp (name, "Xft/", 4) == 0)
446 || strcmp (XSETTINGS_FONT_NAME, name) == 0)
447 want_this = true;
448 #endif
449
450 switch (type)
451 {
452 case 0: /* Integer */
453 if (bytes_parsed + 4 > bytes) return settings_seen;
454 if (want_this)
455 {
456 memcpy (&ival, prop+bytes_parsed, 4);
457 if (my_bo != that_bo) ival = bswap_32 (ival);
458 }
459 bytes_parsed += 4;
460 break;
461
462 case 1: /* String */
463 if (bytes_parsed + 4 > bytes) return settings_seen;
464 memcpy (&vlen, prop+bytes_parsed, 4);
465 bytes_parsed += 4;
466 if (my_bo != that_bo) vlen = bswap_32 (vlen);
467 if (want_this)
468 {
469 to_cpy = min (vlen, sizeof sval - 1);
470 memcpy (sval, prop+bytes_parsed, to_cpy);
471 sval[to_cpy] = '\0';
472 }
473 bytes_parsed += vlen;
474 bytes_parsed = PAD (bytes_parsed);
475 break;
476
477 case 2: /* RGB value */
478 /* No need to parse this */
479 if (bytes_parsed + 8 > bytes) return settings_seen;
480 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
481 break;
482
483 default: /* Parse Error */
484 return settings_seen;
485 }
486
487 if (want_this)
488 {
489 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
490 {
491 dupstring (&settings->tb_style, sval);
492 settings->seen |= SEEN_TB_STYLE;
493 }
494 #ifdef HAVE_XFT
495 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
496 {
497 dupstring (&settings->font, sval);
498 settings->seen |= SEEN_FONT;
499 }
500 else if (strcmp (name, "Xft/Antialias") == 0)
501 {
502 settings->seen |= SEEN_AA;
503 settings->aa = ival != 0;
504 }
505 else if (strcmp (name, "Xft/Hinting") == 0)
506 {
507 settings->seen |= SEEN_HINTING;
508 settings->hinting = ival != 0;
509 }
510 # ifdef FC_HINT_STYLE
511 else if (strcmp (name, "Xft/HintStyle") == 0)
512 {
513 settings->seen |= SEEN_HINTSTYLE;
514 if (strcmp (sval, "hintnone") == 0)
515 settings->hintstyle = FC_HINT_NONE;
516 else if (strcmp (sval, "hintslight") == 0)
517 settings->hintstyle = FC_HINT_SLIGHT;
518 else if (strcmp (sval, "hintmedium") == 0)
519 settings->hintstyle = FC_HINT_MEDIUM;
520 else if (strcmp (sval, "hintfull") == 0)
521 settings->hintstyle = FC_HINT_FULL;
522 else
523 settings->seen &= ~SEEN_HINTSTYLE;
524 }
525 # endif
526 else if (strcmp (name, "Xft/RGBA") == 0)
527 {
528 settings->seen |= SEEN_RGBA;
529 if (strcmp (sval, "none") == 0)
530 settings->rgba = FC_RGBA_NONE;
531 else if (strcmp (sval, "rgb") == 0)
532 settings->rgba = FC_RGBA_RGB;
533 else if (strcmp (sval, "bgr") == 0)
534 settings->rgba = FC_RGBA_BGR;
535 else if (strcmp (sval, "vrgb") == 0)
536 settings->rgba = FC_RGBA_VRGB;
537 else if (strcmp (sval, "vbgr") == 0)
538 settings->rgba = FC_RGBA_VBGR;
539 else
540 settings->seen &= ~SEEN_RGBA;
541 }
542 else if (strcmp (name, "Xft/DPI") == 0)
543 {
544 settings->seen |= SEEN_DPI;
545 settings->dpi = (double)ival/1024.0;
546 }
547 else if (strcmp (name, "Xft/lcdfilter") == 0)
548 {
549 settings->seen |= SEEN_LCDFILTER;
550 if (strcmp (sval, "none") == 0)
551 settings->lcdfilter = FC_LCD_NONE;
552 else if (strcmp (sval, "lcddefault") == 0)
553 settings->lcdfilter = FC_LCD_DEFAULT;
554 else
555 settings->seen &= ~SEEN_LCDFILTER;
556 }
557 #endif /* HAVE_XFT */
558 else
559 want_this = false;
560 settings_seen += want_this;
561 }
562 }
563
564 return settings_seen;
565 }
566
567 /* Read settings from the XSettings property window on display for DPYINFO.
568 Store settings read in SETTINGS.
569 Return true iff successful. */
570
571 static bool
572 read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
573 {
574 Atom act_type;
575 int act_form;
576 unsigned long nitems, bytes_after;
577 unsigned char *prop = NULL;
578 Display *dpy = dpyinfo->display;
579 int rc;
580 bool got_settings = false;
581
582 x_catch_errors (dpy);
583 rc = XGetWindowProperty (dpy,
584 dpyinfo->xsettings_window,
585 dpyinfo->Xatom_xsettings_prop,
586 0, LONG_MAX, False, AnyPropertyType,
587 &act_type, &act_form, &nitems, &bytes_after,
588 &prop);
589
590 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
591 && act_type == dpyinfo->Xatom_xsettings_prop)
592 got_settings = parse_settings (prop, nitems, settings) != 0;
593
594 XFree (prop);
595
596 x_uncatch_errors ();
597
598 return got_settings;
599 }
600
601 /* Apply Xft settings in SETTINGS to the Xft library.
602 Store a Lisp event that Xft settings changed. */
603
604 static void
605 apply_xft_settings (struct x_display_info *dpyinfo,
606 struct xsettings *settings)
607 {
608 #ifdef HAVE_XFT
609 FcPattern *pat;
610 struct xsettings oldsettings;
611 bool changed = false;
612
613 memset (&oldsettings, 0, sizeof (oldsettings));
614 pat = FcPatternCreate ();
615 XftDefaultSubstitute (dpyinfo->display,
616 XScreenNumberOfScreen (dpyinfo->screen),
617 pat);
618 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
619 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
620 #ifdef FC_HINT_STYLE
621 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
622 #endif
623 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
624 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
625 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
626
627 if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
628 {
629 FcPatternDel (pat, FC_ANTIALIAS);
630 FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
631 changed = true;
632 oldsettings.aa = settings->aa;
633 }
634
635 if ((settings->seen & SEEN_HINTING) != 0
636 && oldsettings.hinting != settings->hinting)
637 {
638 FcPatternDel (pat, FC_HINTING);
639 FcPatternAddBool (pat, FC_HINTING, settings->hinting);
640 changed = true;
641 oldsettings.hinting = settings->hinting;
642 }
643 if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
644 {
645 FcPatternDel (pat, FC_RGBA);
646 FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
647 oldsettings.rgba = settings->rgba;
648 changed = true;
649 }
650
651 /* Older fontconfig versions don't have FC_LCD_FILTER. */
652 if ((settings->seen & SEEN_LCDFILTER) != 0
653 && oldsettings.lcdfilter != settings->lcdfilter)
654 {
655 FcPatternDel (pat, FC_LCD_FILTER);
656 FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
657 changed = true;
658 oldsettings.lcdfilter = settings->lcdfilter;
659 }
660
661 #ifdef FC_HINT_STYLE
662 if ((settings->seen & SEEN_HINTSTYLE) != 0
663 && oldsettings.hintstyle != settings->hintstyle)
664 {
665 FcPatternDel (pat, FC_HINT_STYLE);
666 FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
667 changed = true;
668 oldsettings.hintstyle = settings->hintstyle;
669 }
670 #endif
671
672 if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi
673 && settings->dpi > 0)
674 {
675 FcPatternDel (pat, FC_DPI);
676 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
677 changed = true;
678 oldsettings.dpi = settings->dpi;
679
680 /* Changing the DPI on this display affects all frames on it.
681 Check FRAME_RES_X and FRAME_RES_Y in frame.h to see how. */
682 dpyinfo->resy = dpyinfo->resx = settings->dpi;
683 }
684
685 if (changed)
686 {
687 static char const format[] =
688 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
689 "Hintstyle: %d, DPI: %f";
690 enum
691 {
692 d_formats = 5,
693 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
694 lf_formats = 1,
695 max_f_integer_digits = DBL_MAX_10_EXP + 1,
696 f_precision = 6,
697 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
698 - sizeof "%f")
699 };
700 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
701
702 XftDefaultSet (dpyinfo->display, pat);
703 store_config_changed_event (Qfont_render,
704 XCAR (dpyinfo->name_list_element));
705 Vxft_settings
706 = make_formatted_string (buf, format,
707 oldsettings.aa, oldsettings.hinting,
708 oldsettings.rgba, oldsettings.lcdfilter,
709 oldsettings.hintstyle, oldsettings.dpi);
710
711 }
712 else
713 FcPatternDestroy (pat);
714 #endif /* HAVE_XFT */
715 }
716
717 /* Read XSettings from the display for DPYINFO.
718 If SEND_EVENT_P store a Lisp event settings that changed. */
719
720 static void
721 read_and_apply_settings (struct x_display_info *dpyinfo, bool send_event_p)
722 {
723 struct xsettings settings;
724
725 if (!read_settings (dpyinfo, &settings))
726 return;
727
728 apply_xft_settings (dpyinfo, &settings);
729 if (settings.seen & SEEN_TB_STYLE)
730 {
731 if (send_event_p)
732 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
733 else
734 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
735 xfree (settings.tb_style);
736 }
737 #ifdef HAVE_XFT
738 if (settings.seen & SEEN_FONT)
739 {
740 if (send_event_p)
741 store_font_name_changed (settings.font);
742 else
743 dupstring (&current_font, settings.font);
744 xfree (settings.font);
745 }
746 #endif
747 }
748
749 /* Check if EVENT for the display in DPYINFO is XSettings related. */
750
751 void
752 xft_settings_event (struct x_display_info *dpyinfo, const XEvent *event)
753 {
754 bool check_window_p = false, apply_settings_p = false;
755
756 switch (event->type)
757 {
758 case DestroyNotify:
759 if (dpyinfo->xsettings_window == event->xany.window)
760 check_window_p = true;
761 break;
762
763 case ClientMessage:
764 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
765 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
766 && event->xclient.window == dpyinfo->root_window)
767 check_window_p = true;
768 break;
769
770 case PropertyNotify:
771 if (event->xproperty.window == dpyinfo->xsettings_window
772 && event->xproperty.state == PropertyNewValue
773 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
774 apply_settings_p = true;
775 break;
776 }
777
778
779 if (check_window_p)
780 {
781 dpyinfo->xsettings_window = None;
782 get_prop_window (dpyinfo);
783 if (dpyinfo->xsettings_window != None)
784 apply_settings_p = true;
785 }
786
787 if (apply_settings_p)
788 read_and_apply_settings (dpyinfo, true);
789 }
790
791 /* Initialize GSettings and read startup values. */
792
793 static void
794 init_gsettings (void)
795 {
796 #ifdef HAVE_GSETTINGS
797 GVariant *val;
798 bool schema_found = false;
799
800 #if ! GLIB_CHECK_VERSION (2, 36, 0)
801 g_type_init ();
802 #endif
803
804 #if GLIB_CHECK_VERSION (2, 32, 0)
805 {
806 GSettingsSchema *sc = g_settings_schema_source_lookup
807 (g_settings_schema_source_get_default (),
808 GSETTINGS_SCHEMA,
809 TRUE);
810 schema_found = sc != NULL;
811 if (sc) g_settings_schema_unref (sc);
812 }
813 #else
814 {
815 const gchar *const *schemas = g_settings_list_schemas ();
816 if (schemas == NULL) return;
817 while (! schema_found && *schemas != NULL)
818 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
819 }
820 #endif
821 if (!schema_found) return;
822
823 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
824 if (!gsettings_client) return;
825 g_object_ref_sink (G_OBJECT (gsettings_client));
826 g_signal_connect (G_OBJECT (gsettings_client), "changed",
827 G_CALLBACK (something_changed_gsettingsCB), NULL);
828
829 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
830 if (val)
831 {
832 g_variant_ref_sink (val);
833 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
834 current_tool_bar_style
835 = map_tool_bar_style (g_variant_get_string (val, NULL));
836 g_variant_unref (val);
837 }
838
839 #ifdef HAVE_XFT
840 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
841 if (val)
842 {
843 g_variant_ref_sink (val);
844 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
845 dupstring (&current_mono_font, g_variant_get_string (val, NULL));
846 g_variant_unref (val);
847 }
848
849 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
850 if (val)
851 {
852 g_variant_ref_sink (val);
853 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
854 dupstring (&current_font, g_variant_get_string (val, NULL));
855 g_variant_unref (val);
856 }
857 #endif /* HAVE_XFT */
858
859 #endif /* HAVE_GSETTINGS */
860 }
861
862 /* Init GConf and read startup values. */
863
864 static void
865 init_gconf (void)
866 {
867 #if defined (HAVE_GCONF)
868 char *s;
869
870 #if ! GLIB_CHECK_VERSION (2, 36, 0)
871 g_type_init ();
872 #endif
873
874 gconf_client = gconf_client_get_default ();
875 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
876 gconf_client_add_dir (gconf_client,
877 GCONF_TOOL_BAR_STYLE,
878 GCONF_CLIENT_PRELOAD_ONELEVEL,
879 NULL);
880 gconf_client_notify_add (gconf_client,
881 GCONF_TOOL_BAR_STYLE,
882 something_changed_gconfCB,
883 NULL, NULL, NULL);
884
885 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
886 if (s)
887 {
888 current_tool_bar_style = map_tool_bar_style (s);
889 g_free (s);
890 }
891
892 #ifdef HAVE_XFT
893 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
894 if (s)
895 {
896 dupstring (&current_mono_font, s);
897 g_free (s);
898 }
899 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
900 if (s)
901 {
902 dupstring (&current_font, s);
903 g_free (s);
904 }
905 gconf_client_add_dir (gconf_client,
906 GCONF_MONO_FONT,
907 GCONF_CLIENT_PRELOAD_ONELEVEL,
908 NULL);
909 gconf_client_notify_add (gconf_client,
910 GCONF_MONO_FONT,
911 something_changed_gconfCB,
912 NULL, NULL, NULL);
913 gconf_client_add_dir (gconf_client,
914 GCONF_FONT_NAME,
915 GCONF_CLIENT_PRELOAD_ONELEVEL,
916 NULL);
917 gconf_client_notify_add (gconf_client,
918 GCONF_FONT_NAME,
919 something_changed_gconfCB,
920 NULL, NULL, NULL);
921 #endif /* HAVE_XFT */
922 #endif /* HAVE_GCONF */
923 }
924
925 /* Init Xsettings and read startup values. */
926
927 static void
928 init_xsettings (struct x_display_info *dpyinfo)
929 {
930 Display *dpy = dpyinfo->display;
931
932 block_input ();
933
934 /* Select events so we can detect client messages sent when selection
935 owner changes. */
936 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
937
938 get_prop_window (dpyinfo);
939 if (dpyinfo->xsettings_window != None)
940 read_and_apply_settings (dpyinfo, false);
941
942 unblock_input ();
943 }
944
945 void
946 xsettings_initialize (struct x_display_info *dpyinfo)
947 {
948 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
949 init_gconf ();
950 init_xsettings (dpyinfo);
951 init_gsettings ();
952 }
953
954 /* Return the system monospaced font.
955 May be NULL if not known. */
956
957 const char *
958 xsettings_get_system_font (void)
959 {
960 return current_mono_font;
961 }
962
963 #ifdef USE_LUCID
964 /* Return the system font.
965 May be NULL if not known. */
966
967 const char *
968 xsettings_get_system_normal_font (void)
969 {
970 return current_font;
971 }
972 #endif
973
974 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
975 Sfont_get_system_normal_font,
976 0, 0, 0,
977 doc: /* Get the system default application font. */)
978 (void)
979 {
980 return current_font ? build_string (current_font) : Qnil;
981 }
982
983 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
984 0, 0, 0,
985 doc: /* Get the system default fixed width font. */)
986 (void)
987 {
988 return current_mono_font ? build_string (current_mono_font) : Qnil;
989 }
990
991 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
992 Stool_bar_get_system_style, 0, 0, 0,
993 doc: /* Get the system tool bar style.
994 If no system tool bar style is known, return `tool-bar-style' if set to a
995 known style. Otherwise return image. */)
996 (void)
997 {
998 if (EQ (Vtool_bar_style, Qimage)
999 || EQ (Vtool_bar_style, Qtext)
1000 || EQ (Vtool_bar_style, Qboth)
1001 || EQ (Vtool_bar_style, Qboth_horiz)
1002 || EQ (Vtool_bar_style, Qtext_image_horiz))
1003 return Vtool_bar_style;
1004 if (!NILP (current_tool_bar_style))
1005 return current_tool_bar_style;
1006 return Qimage;
1007 }
1008
1009 void
1010 syms_of_xsettings (void)
1011 {
1012 current_mono_font = NULL;
1013 current_font = NULL;
1014 first_dpyinfo = NULL;
1015 #ifdef HAVE_GSETTINGS
1016 gsettings_client = NULL;
1017 #endif
1018 #ifdef HAVE_GCONF
1019 gconf_client = NULL;
1020 #endif
1021
1022 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1023 DEFSYM (Qfont_name, "font-name");
1024 DEFSYM (Qfont_render, "font-render");
1025 defsubr (&Sfont_get_system_font);
1026 defsubr (&Sfont_get_system_normal_font);
1027
1028 DEFVAR_BOOL ("font-use-system-font", use_system_font,
1029 doc: /* Non-nil means to apply the system defined font dynamically.
1030 When this is non-nil and the system defined fixed width font changes, we
1031 update frames dynamically.
1032 If this variable is nil, Emacs ignores system font changes. */);
1033 use_system_font = false;
1034
1035 DEFVAR_LISP ("xft-settings", Vxft_settings,
1036 doc: /* Font settings applied to Xft. */);
1037 Vxft_settings = empty_unibyte_string;
1038
1039 #ifdef HAVE_XFT
1040 Fprovide (intern_c_string ("font-render-setting"), Qnil);
1041 #if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
1042 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1043 #endif
1044 #endif
1045
1046 current_tool_bar_style = Qnil;
1047 DEFSYM (Qtool_bar_style, "tool-bar-style");
1048 defsubr (&Stool_bar_get_system_style);
1049
1050 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
1051 }