]> code.delx.au - gnu-emacs/blob - src/xsettings.c
Assume freestanding C89 headers, string.h, stdlib.h.
[gnu-emacs] / src / xsettings.c
1 /* Functions for handling font and other changes dynamically.
2
3 Copyright (C) 2009-2011 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 <setjmp.h>
25 #include <fcntl.h>
26 #include "lisp.h"
27 #include "xterm.h"
28 #include "xsettings.h"
29 #include "frame.h"
30 #include "keyboard.h"
31 #include "blockinput.h"
32 #include "termhooks.h"
33 #include "termopts.h"
34
35 #include <X11/Xproto.h>
36
37 #ifdef HAVE_GSETTINGS
38 #include <glib-object.h>
39 #include <gio/gio.h>
40 #endif
41
42 #ifdef HAVE_GCONF
43 #include <gconf/gconf-client.h>
44 #endif
45
46 #ifdef HAVE_XFT
47 #include <X11/Xft/Xft.h>
48 #endif
49
50 static char *current_mono_font;
51 static char *current_font;
52 static struct x_display_info *first_dpyinfo;
53 static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render,
54 Qtool_bar_style;
55 static Lisp_Object current_tool_bar_style;
56
57 /* Store an config changed event in to the event queue. */
58
59 static void
60 store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
61 {
62 struct input_event event;
63 EVENT_INIT (event);
64 event.kind = CONFIG_CHANGED_EVENT;
65 event.frame_or_window = display_name;
66 event.arg = arg;
67 kbd_buffer_store_event (&event);
68 }
69
70 /* Return non-zero if DPYINFO is still valid. */
71 static int
72 dpyinfo_valid (struct x_display_info *dpyinfo)
73 {
74 int found = 0;
75 if (dpyinfo != NULL)
76 {
77 struct x_display_info *d;
78 for (d = x_display_list; !found && d; d = d->next)
79 found = d == dpyinfo && d->display == dpyinfo->display;
80 }
81 return found;
82 }
83
84 /* Store a monospace font change event if the monospaced font changed. */
85
86 #ifdef HAVE_XFT
87 static void
88 store_monospaced_changed (const char *newfont)
89 {
90 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
91 return; /* No change. */
92
93 xfree (current_mono_font);
94 current_mono_font = xstrdup (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
103 /* Store a font name change event if the font name changed. */
104
105 static void
106 store_font_name_changed (const char *newfont)
107 {
108 if (current_font != NULL && strcmp (newfont, current_font) == 0)
109 return; /* No change. */
110
111 xfree (current_font);
112 current_font = xstrdup (newfont);
113
114 if (dpyinfo_valid (first_dpyinfo))
115 {
116 store_config_changed_event (Qfont_name,
117 XCAR (first_dpyinfo->name_list_element));
118 }
119 }
120 #endif /* HAVE_XFT */
121
122 /* Map TOOL_BAR_STYLE from a string to its correspinding Lisp value.
123 Return Qnil if TOOL_BAR_STYLE is not known. */
124
125 static Lisp_Object
126 map_tool_bar_style (const char *tool_bar_style)
127 {
128 Lisp_Object style = Qnil;
129 if (tool_bar_style)
130 {
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;
139 }
140
141 return style;
142 }
143
144 /* Store a tool bar style change event if the tool bar style changed. */
145
146 static void
147 store_tool_bar_style_changed (const char *newstyle,
148 struct x_display_info *dpyinfo)
149 {
150 Lisp_Object style = map_tool_bar_style (newstyle);
151 if (EQ (current_tool_bar_style, style))
152 return; /* No change. */
153
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));
158 }
159
160
161 #define XSETTINGS_FONT_NAME "Gtk/FontName"
162 #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
163
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,
173 };
174 struct xsettings
175 {
176 #ifdef HAVE_XFT
177 FcBool aa, hinting;
178 int rgba, lcdfilter, hintstyle;
179 double dpi;
180
181 char *font;
182 #endif
183
184 char *tb_style;
185
186 unsigned seen;
187 };
188
189 #ifdef HAVE_GSETTINGS
190 #define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
191 #define GSETTINGS_TOOL_BAR_STYLE "toolbar-style"
192
193 #ifdef HAVE_XFT
194 #define GSETTINGS_MONO_FONT "monospace-font-name"
195 #define GSETTINGS_FONT_NAME "font-name"
196 #endif
197
198
199 /* The single GSettings instance, or NULL if not connected to GSettings. */
200
201 static GSettings *gsettings_client;
202
203 /* Callback called when something changed in GSettings. */
204
205 static void
206 something_changed_gsettingsCB (GSettings *settings,
207 gchar *key,
208 gpointer user_data)
209 {
210 GVariant *val;
211
212 if (strcmp (key, GSETTINGS_TOOL_BAR_STYLE) == 0)
213 {
214 val = g_settings_get_value (settings, GSETTINGS_TOOL_BAR_STYLE);
215 if (val)
216 {
217 g_variant_ref_sink (val);
218 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
219 {
220 const gchar *newstyle = g_variant_get_string (val, NULL);
221 store_tool_bar_style_changed (newstyle, first_dpyinfo);
222 }
223 g_variant_unref (val);
224 }
225 }
226 #ifdef HAVE_XFT
227 else if (strcmp (key, GSETTINGS_MONO_FONT) == 0)
228 {
229 val = g_settings_get_value (settings, GSETTINGS_MONO_FONT);
230 if (val)
231 {
232 g_variant_ref_sink (val);
233 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
234 {
235 const gchar *newfont = g_variant_get_string (val, NULL);
236 store_monospaced_changed (newfont);
237 }
238 g_variant_unref (val);
239 }
240 }
241 else if (strcmp (key, GSETTINGS_FONT_NAME) == 0)
242 {
243 val = g_settings_get_value (settings, GSETTINGS_FONT_NAME);
244 if (val)
245 {
246 g_variant_ref_sink (val);
247 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
248 {
249 const gchar *newfont = g_variant_get_string (val, NULL);
250 store_font_name_changed (newfont);
251 }
252 g_variant_unref (val);
253 }
254 }
255 #endif /* HAVE_XFT */
256 }
257
258 #endif /* HAVE_GSETTINGS */
259
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
266
267 /* The single GConf instance, or NULL if not connected to GConf. */
268
269 static GConfClient *gconf_client;
270
271 /* Callback called when something changed in GConf that we care about. */
272
273 static void
274 something_changed_gconfCB (GConfClient *client,
275 guint cnxn_id,
276 GConfEntry *entry,
277 gpointer user_data)
278 {
279 GConfValue *v = gconf_entry_get_value (entry);
280 const char *key = gconf_entry_get_key (entry);
281
282 if (!v || v->type != GCONF_VALUE_STRING || ! key) return;
283 if (strcmp (key, GCONF_TOOL_BAR_STYLE) == 0)
284 {
285 const char *value = gconf_value_get_string (v);
286 store_tool_bar_style_changed (value, first_dpyinfo);
287 }
288 #ifdef HAVE_XFT
289 else if (strcmp (key, GCONF_MONO_FONT) == 0)
290 {
291 const char *value = gconf_value_get_string (v);
292 store_monospaced_changed (value);
293 }
294 else if (strcmp (key, GCONF_FONT_NAME) == 0)
295 {
296 const char *value = gconf_value_get_string (v);
297 store_font_name_changed (value);
298 }
299 #endif /* HAVE_XFT */
300 }
301
302 #endif /* HAVE_GCONF */
303
304 #ifdef HAVE_XFT
305
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
316
317 #endif /* HAVE_XFT */
318
319 /* Find the window that contains the XSETTINGS property values. */
320
321 static void
322 get_prop_window (struct x_display_info *dpyinfo)
323 {
324 Display *dpy = dpyinfo->display;
325
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);
334
335 XUngrabServer (dpy);
336 }
337
338 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
339 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
340 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
341 #define PAD(nr) (((nr) + 3) & ~3)
342
343 /* Parse xsettings and extract those that deal with Xft.
344 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
345 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
346
347 Layout of prop. First is a header:
348
349 bytes type what
350 ------------------------------------
351 1 CARD8 byte-order
352 3 unused
353 4 CARD32 SERIAL
354 4 CARD32 N_SETTINGS
355
356 Then N_SETTINGS records, with header:
357
358 bytes type what
359 ------------------------------------
360 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
361 1 unused
362 2 CARD16 n == name-length
363 n STRING8 name
364 p unused, p=pad_to_even_4(n)
365 4 CARD32 last-change-serial
366
367 and then the value, For string:
368
369 bytes type what
370 ------------------------------------
371 4 CARD32 n = value-length
372 n STRING8 value
373 p unused, p=pad_to_even_4(n)
374
375 For integer:
376
377 bytes type what
378 ------------------------------------
379 4 INT32 value
380
381 For RGB color:
382
383 bytes type what
384 ------------------------------------
385 2 CARD16 red
386 2 CARD16 blue
387 2 CARD16 green
388 2 CARD16 alpha
389
390 Returns non-zero if some Xft settings was seen, zero otherwise.
391 */
392
393 static int
394 parse_settings (unsigned char *prop,
395 long unsigned int bytes,
396 struct xsettings *settings)
397 {
398 Lisp_Object byteorder = Fbyteorder ();
399 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
400 int that_bo = prop[0];
401 CARD32 n_settings;
402 int bytes_parsed = 0;
403 int settings_seen = 0;
404 int i = 0;
405
406 /* First 4 bytes is a serial number, skip that. */
407
408 if (bytes < 12) return BadLength;
409 memcpy (&n_settings, prop+8, 4);
410 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
411 bytes_parsed = 12;
412
413 memset (settings, 0, sizeof (*settings));
414
415 while (bytes_parsed+4 < bytes && settings_seen < 7
416 && i < n_settings)
417 {
418 int type = prop[bytes_parsed++];
419 CARD16 nlen;
420 CARD32 vlen, ival = 0;
421 char name[128]; /* The names we are looking for are not this long. */
422 char sval[128]; /* The values we are looking for are not this long. */
423 int want_this;
424 int to_cpy;
425
426 sval[0] = '\0';
427 ++i;
428 ++bytes_parsed; /* Padding */
429
430 memcpy (&nlen, prop+bytes_parsed, 2);
431 bytes_parsed += 2;
432 if (my_bo != that_bo) nlen = SWAP16 (nlen);
433 if (bytes_parsed+nlen > bytes) return BadLength;
434 to_cpy = nlen > 127 ? 127 : nlen;
435 memcpy (name, prop+bytes_parsed, to_cpy);
436 name[to_cpy] = '\0';
437
438 bytes_parsed += nlen;
439 bytes_parsed = PAD (bytes_parsed);
440
441 bytes_parsed += 4; /* Skip serial for this value */
442 if (bytes_parsed > bytes) return BadLength;
443
444 want_this =
445 #ifdef HAVE_XFT
446 (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
447 || strcmp (XSETTINGS_FONT_NAME, name) == 0
448 ||
449 #endif
450 strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0;
451
452 switch (type)
453 {
454 case 0: /* Integer */
455 if (bytes_parsed+4 > bytes) return BadLength;
456 if (want_this)
457 {
458 memcpy (&ival, prop+bytes_parsed, 4);
459 if (my_bo != that_bo) ival = SWAP32 (ival);
460 }
461 bytes_parsed += 4;
462 break;
463
464 case 1: /* String */
465 if (bytes_parsed+4 > bytes) return BadLength;
466 memcpy (&vlen, prop+bytes_parsed, 4);
467 bytes_parsed += 4;
468 if (my_bo != that_bo) vlen = SWAP32 (vlen);
469 if (want_this)
470 {
471 to_cpy = vlen > 127 ? 127 : vlen;
472 memcpy (sval, prop+bytes_parsed, to_cpy);
473 sval[to_cpy] = '\0';
474 }
475 bytes_parsed += vlen;
476 bytes_parsed = PAD (bytes_parsed);
477 break;
478
479 case 2: /* RGB value */
480 /* No need to parse this */
481 if (bytes_parsed+8 > bytes) return BadLength;
482 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
483 break;
484
485 default: /* Parse Error */
486 return BadValue;
487 }
488
489 if (want_this)
490 {
491 ++settings_seen;
492 if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
493 {
494 settings->tb_style = xstrdup (sval);
495 settings->seen |= SEEN_TB_STYLE;
496 }
497 #ifdef HAVE_XFT
498 else if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
499 {
500 settings->font = xstrdup (sval);
501 settings->seen |= SEEN_FONT;
502 }
503 else if (strcmp (name, "Xft/Antialias") == 0)
504 {
505 settings->seen |= SEEN_AA;
506 settings->aa = ival != 0;
507 }
508 else if (strcmp (name, "Xft/Hinting") == 0)
509 {
510 settings->seen |= SEEN_HINTING;
511 settings->hinting = ival != 0;
512 }
513 # ifdef FC_HINT_STYLE
514 else if (strcmp (name, "Xft/HintStyle") == 0)
515 {
516 settings->seen |= SEEN_HINTSTYLE;
517 if (strcmp (sval, "hintnone") == 0)
518 settings->hintstyle = FC_HINT_NONE;
519 else if (strcmp (sval, "hintslight") == 0)
520 settings->hintstyle = FC_HINT_SLIGHT;
521 else if (strcmp (sval, "hintmedium") == 0)
522 settings->hintstyle = FC_HINT_MEDIUM;
523 else if (strcmp (sval, "hintfull") == 0)
524 settings->hintstyle = FC_HINT_FULL;
525 else
526 settings->seen &= ~SEEN_HINTSTYLE;
527 }
528 # endif
529 else if (strcmp (name, "Xft/RGBA") == 0)
530 {
531 settings->seen |= SEEN_RGBA;
532 if (strcmp (sval, "none") == 0)
533 settings->rgba = FC_RGBA_NONE;
534 else if (strcmp (sval, "rgb") == 0)
535 settings->rgba = FC_RGBA_RGB;
536 else if (strcmp (sval, "bgr") == 0)
537 settings->rgba = FC_RGBA_BGR;
538 else if (strcmp (sval, "vrgb") == 0)
539 settings->rgba = FC_RGBA_VRGB;
540 else if (strcmp (sval, "vbgr") == 0)
541 settings->rgba = FC_RGBA_VBGR;
542 else
543 settings->seen &= ~SEEN_RGBA;
544 }
545 else if (strcmp (name, "Xft/DPI") == 0)
546 {
547 settings->seen |= SEEN_DPI;
548 settings->dpi = (double)ival/1024.0;
549 }
550 else if (strcmp (name, "Xft/lcdfilter") == 0)
551 {
552 settings->seen |= SEEN_LCDFILTER;
553 if (strcmp (sval, "none") == 0)
554 settings->lcdfilter = FC_LCD_NONE;
555 else if (strcmp (sval, "lcddefault") == 0)
556 settings->lcdfilter = FC_LCD_DEFAULT;
557 else
558 settings->seen &= ~SEEN_LCDFILTER;
559 }
560 #endif /* HAVE_XFT */
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 non-zero if successful, zero if not. */
570
571 static int
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
581 x_catch_errors (dpy);
582 rc = XGetWindowProperty (dpy,
583 dpyinfo->xsettings_window,
584 dpyinfo->Xatom_xsettings_prop,
585 0, LONG_MAX, False, AnyPropertyType,
586 &act_type, &act_form, &nitems, &bytes_after,
587 &prop);
588
589 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
590 && act_type == dpyinfo->Xatom_xsettings_prop)
591 rc = parse_settings (prop, nitems, settings);
592
593 XFree (prop);
594
595 x_uncatch_errors ();
596
597 return rc != 0;
598 }
599
600 /* Apply Xft settings in SETTINGS to the Xft library.
601 If SEND_EVENT_P is non-zero store a Lisp event that Xft settings changed. */
602
603 static void
604 apply_xft_settings (struct x_display_info *dpyinfo,
605 int send_event_p,
606 struct xsettings *settings)
607 {
608 #ifdef HAVE_XFT
609 FcPattern *pat;
610 struct xsettings oldsettings;
611 int changed = 0;
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;
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;
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;
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;
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;
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 Lisp_Object frame, tail;
676
677 FcPatternDel (pat, FC_DPI);
678 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
679 ++changed;
680 oldsettings.dpi = settings->dpi;
681
682 /* Change the DPI on this display and all frames on the display. */
683 dpyinfo->resy = dpyinfo->resx = settings->dpi;
684 FOR_EACH_FRAME (tail, frame)
685 if (FRAME_X_P (XFRAME (frame))
686 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
687 XFRAME (frame)->resy = XFRAME (frame)->resx = settings->dpi;
688 }
689
690 if (changed)
691 {
692 static char const format[] =
693 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
694 "Hintstyle: %d, DPI: %lf";
695 enum
696 {
697 d_formats = 5,
698 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
699 lf_formats = 1,
700 max_f_integer_digits = DBL_MAX_10_EXP + 1,
701 f_precision = 6,
702 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
703 - sizeof "%lf")
704 };
705 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
706
707 XftDefaultSet (dpyinfo->display, pat);
708 if (send_event_p)
709 store_config_changed_event (Qfont_render,
710 XCAR (dpyinfo->name_list_element));
711 sprintf (buf, format, oldsettings.aa, oldsettings.hinting,
712 oldsettings.rgba, oldsettings.lcdfilter,
713 oldsettings.hintstyle, oldsettings.dpi);
714 Vxft_settings = build_string (buf);
715 }
716 else
717 FcPatternDestroy (pat);
718 #endif /* HAVE_XFT */
719 }
720
721 /* Read XSettings from the display for DPYINFO.
722 If SEND_EVENT_P is non-zero store a Lisp event settings that changed. */
723
724 static void
725 read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
726 {
727 struct xsettings settings;
728
729 if (!read_settings (dpyinfo, &settings))
730 return;
731
732 apply_xft_settings (dpyinfo, True, &settings);
733 if (settings.seen & SEEN_TB_STYLE)
734 {
735 if (send_event_p)
736 store_tool_bar_style_changed (settings.tb_style, dpyinfo);
737 else
738 current_tool_bar_style = map_tool_bar_style (settings.tb_style);
739 xfree (settings.tb_style);
740 }
741 #ifdef HAVE_XFT
742 if (settings.seen & SEEN_FONT)
743 {
744 if (send_event_p)
745 store_font_name_changed (settings.font);
746 else
747 {
748 xfree (current_font);
749 current_font = xstrdup (settings.font);
750 }
751 xfree (settings.font);
752 }
753 #endif
754 }
755
756 /* Check if EVENT for the display in DPYINFO is XSettings related. */
757
758 void
759 xft_settings_event (struct x_display_info *dpyinfo, XEvent *event)
760 {
761 int check_window_p = 0;
762 int apply_settings = 0;
763
764 switch (event->type)
765 {
766 case DestroyNotify:
767 if (dpyinfo->xsettings_window == event->xany.window)
768 check_window_p = 1;
769 break;
770
771 case ClientMessage:
772 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
773 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
774 && event->xclient.window == dpyinfo->root_window)
775 check_window_p = 1;
776 break;
777
778 case PropertyNotify:
779 if (event->xproperty.window == dpyinfo->xsettings_window
780 && event->xproperty.state == PropertyNewValue
781 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
782 apply_settings = 1;
783 break;
784 }
785
786
787 if (check_window_p)
788 {
789 dpyinfo->xsettings_window = None;
790 get_prop_window (dpyinfo);
791 if (dpyinfo->xsettings_window != None)
792 apply_settings = 1;
793 }
794
795 if (apply_settings)
796 read_and_apply_settings (dpyinfo, True);
797 }
798
799 /* Initialize GSettings and read startup values. */
800
801 static void
802 init_gsettings (void)
803 {
804 #ifdef HAVE_GSETTINGS
805 GVariant *val;
806 const gchar *const *schemas;
807 int schema_found = 0;
808
809 #ifdef HAVE_G_TYPE_INIT
810 g_type_init ();
811 #endif
812
813 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 if (!schema_found) return;
818
819 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
820 if (!gsettings_client) return;
821 g_object_ref_sink (G_OBJECT (gsettings_client));
822 g_signal_connect (G_OBJECT (gsettings_client), "changed",
823 G_CALLBACK (something_changed_gsettingsCB), NULL);
824
825 val = g_settings_get_value (gsettings_client, GSETTINGS_TOOL_BAR_STYLE);
826 if (val)
827 {
828 g_variant_ref_sink (val);
829 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
830 current_tool_bar_style
831 = map_tool_bar_style (g_variant_get_string (val, NULL));
832 g_variant_unref (val);
833 }
834
835 #ifdef HAVE_XFT
836 val = g_settings_get_value (gsettings_client, GSETTINGS_MONO_FONT);
837 if (val)
838 {
839 g_variant_ref_sink (val);
840 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
841 current_mono_font = xstrdup (g_variant_get_string (val, NULL));
842 g_variant_unref (val);
843 }
844
845 val = g_settings_get_value (gsettings_client, GSETTINGS_FONT_NAME);
846 if (val)
847 {
848 g_variant_ref_sink (val);
849 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
850 current_font = xstrdup (g_variant_get_string (val, NULL));
851 g_variant_unref (val);
852 }
853 #endif /* HAVE_XFT */
854
855 #endif /* HAVE_GSETTINGS */
856 }
857
858 /* Init GConf and read startup values. */
859
860 static void
861 init_gconf (void)
862 {
863 #if defined (HAVE_GCONF)
864 char *s;
865
866 #ifdef HAVE_G_TYPE_INIT
867 g_type_init ();
868 #endif
869
870 gconf_client = gconf_client_get_default ();
871 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
872 gconf_client_add_dir (gconf_client,
873 GCONF_TOOL_BAR_STYLE,
874 GCONF_CLIENT_PRELOAD_ONELEVEL,
875 NULL);
876 gconf_client_notify_add (gconf_client,
877 GCONF_TOOL_BAR_STYLE,
878 something_changed_gconfCB,
879 NULL, NULL, NULL);
880
881 s = gconf_client_get_string (gconf_client, GCONF_TOOL_BAR_STYLE, NULL);
882 if (s)
883 {
884 current_tool_bar_style = map_tool_bar_style (s);
885 g_free (s);
886 }
887
888 #ifdef HAVE_XFT
889 s = gconf_client_get_string (gconf_client, GCONF_MONO_FONT, NULL);
890 if (s)
891 {
892 current_mono_font = xstrdup (s);
893 g_free (s);
894 }
895 s = gconf_client_get_string (gconf_client, GCONF_FONT_NAME, NULL);
896 if (s)
897 {
898 current_font = xstrdup (s);
899 g_free (s);
900 }
901 gconf_client_add_dir (gconf_client,
902 GCONF_MONO_FONT,
903 GCONF_CLIENT_PRELOAD_ONELEVEL,
904 NULL);
905 gconf_client_notify_add (gconf_client,
906 GCONF_MONO_FONT,
907 something_changed_gconfCB,
908 NULL, NULL, NULL);
909 gconf_client_add_dir (gconf_client,
910 GCONF_FONT_NAME,
911 GCONF_CLIENT_PRELOAD_ONELEVEL,
912 NULL);
913 gconf_client_notify_add (gconf_client,
914 GCONF_FONT_NAME,
915 something_changed_gconfCB,
916 NULL, NULL, NULL);
917 #endif /* HAVE_XFT */
918 #endif /* HAVE_GCONF */
919 }
920
921 /* Init Xsettings and read startup values. */
922
923 static void
924 init_xsettings (struct x_display_info *dpyinfo)
925 {
926 Display *dpy = dpyinfo->display;
927
928 BLOCK_INPUT;
929
930 /* Select events so we can detect client messages sent when selection
931 owner changes. */
932 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
933
934 get_prop_window (dpyinfo);
935 if (dpyinfo->xsettings_window != None)
936 read_and_apply_settings (dpyinfo, False);
937
938 UNBLOCK_INPUT;
939 }
940
941 void
942 xsettings_initialize (struct x_display_info *dpyinfo)
943 {
944 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
945 init_gconf ();
946 init_xsettings (dpyinfo);
947 init_gsettings ();
948 }
949
950 /* Return the system monospaced font.
951 May be NULL if not known. */
952
953 const char *
954 xsettings_get_system_font (void)
955 {
956 return current_mono_font;
957 }
958
959 #ifdef USE_LUCID
960 /* Return the system font.
961 May be NULL if not known. */
962
963 const char *
964 xsettings_get_system_normal_font (void)
965 {
966 return current_font;
967 }
968 #endif
969
970 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
971 Sfont_get_system_normal_font,
972 0, 0, 0,
973 doc: /* Get the system default application font. */)
974 (void)
975 {
976 return current_font ? build_string (current_font) : Qnil;
977 }
978
979 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
980 0, 0, 0,
981 doc: /* Get the system default fixed width font. */)
982 (void)
983 {
984 return current_mono_font ? build_string (current_mono_font) : Qnil;
985 }
986
987 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
988 Stool_bar_get_system_style, 0, 0, 0,
989 doc: /* Get the system tool bar style.
990 If no system tool bar style is known, return `tool-bar-style' if set to a
991 known style. Otherwise return image. */)
992 (void)
993 {
994 if (EQ (Vtool_bar_style, Qimage)
995 || EQ (Vtool_bar_style, Qtext)
996 || EQ (Vtool_bar_style, Qboth)
997 || EQ (Vtool_bar_style, Qboth_horiz)
998 || EQ (Vtool_bar_style, Qtext_image_horiz))
999 return Vtool_bar_style;
1000 if (!NILP (current_tool_bar_style))
1001 return current_tool_bar_style;
1002 return Qimage;
1003 }
1004
1005 void
1006 syms_of_xsettings (void)
1007 {
1008 current_mono_font = NULL;
1009 current_font = NULL;
1010 first_dpyinfo = NULL;
1011 #ifdef HAVE_GSETTINGS
1012 gsettings_client = NULL;
1013 #endif
1014 #ifdef HAVE_GCONF
1015 gconf_client = NULL;
1016 #endif
1017
1018 DEFSYM (Qmonospace_font_name, "monospace-font-name");
1019 DEFSYM (Qfont_name, "font-name");
1020 DEFSYM (Qfont_render, "font-render");
1021 defsubr (&Sfont_get_system_font);
1022 defsubr (&Sfont_get_system_normal_font);
1023
1024 DEFVAR_BOOL ("font-use-system-font", use_system_font,
1025 doc: /* *Non-nil means to apply the system defined font dynamically.
1026 When this is non-nil and the system defined fixed width font changes, we
1027 update frames dynamically.
1028 If this variable is nil, Emacs ignores system font changes. */);
1029 use_system_font = 0;
1030
1031 DEFVAR_LISP ("xft-settings", Vxft_settings,
1032 doc: /* Font settings applied to Xft. */);
1033 Vxft_settings = make_string ("", 0);
1034
1035 #ifdef HAVE_XFT
1036 Fprovide (intern_c_string ("font-render-setting"), Qnil);
1037 #if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
1038 Fprovide (intern_c_string ("system-font-setting"), Qnil);
1039 #endif
1040 #endif
1041
1042 current_tool_bar_style = Qnil;
1043 DEFSYM (Qtool_bar_style, "tool-bar-style");
1044 defsubr (&Stool_bar_get_system_style);
1045
1046 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
1047 }