]> code.delx.au - gnu-emacs/blob - src/xsettings.c
* mh-letter.el (mh-letter-mode-map, mh-letter-complete)
[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 #else
41 #ifdef HAVE_GCONF
42 #include <gconf/gconf-client.h>
43 #endif
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 static void
58 store_config_changed_event (Lisp_Object arg, Lisp_Object display_name)
59 {
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);
66 }
67
68 static void
69 store_monospaced_changed (const char *newfont)
70 {
71 if (current_mono_font != NULL && strcmp (newfont, current_mono_font) == 0)
72 return; /* No change. */
73
74 xfree (current_mono_font);
75 current_mono_font = xstrdup (newfont);
76
77 if (first_dpyinfo != NULL && use_system_font)
78 {
79 /* Check if display still open */
80 struct x_display_info *dpyinfo;
81 int found = 0;
82 for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
83 found = dpyinfo == first_dpyinfo;
84
85 if (found)
86 store_config_changed_event (Qmonospace_font_name,
87 XCAR (first_dpyinfo->name_list_element));
88 }
89 }
90
91
92 #ifdef HAVE_GSETTINGS
93 static GSettings *gsettings_client;
94 #else
95 #ifdef HAVE_GCONF
96 static GConfClient *gconf_client;
97 #endif
98 #endif
99
100
101 #define XSETTINGS_FONT_NAME "Gtk/FontName"
102 #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
103
104 enum {
105 SEEN_AA = 0x01,
106 SEEN_HINTING = 0x02,
107 SEEN_RGBA = 0x04,
108 SEEN_LCDFILTER = 0x08,
109 SEEN_HINTSTYLE = 0x10,
110 SEEN_DPI = 0x20,
111 SEEN_FONT = 0x40,
112 SEEN_TB_STYLE = 0x80,
113 };
114 struct xsettings
115 {
116 #ifdef HAVE_XFT
117 FcBool aa, hinting;
118 int rgba, lcdfilter, hintstyle;
119 double dpi;
120 #endif
121
122 char *font;
123 char *tb_style;
124
125 unsigned seen;
126 };
127
128 #ifdef HAVE_GSETTINGS
129 #define GSETTINGS_SCHEMA "org.gnome.desktop.interface"
130 #define SYSTEM_MONO_FONT "monospace-font-name"
131
132 static void
133 something_changedCB (GSettings *settings,
134 gchar *key,
135 gpointer user_data)
136 {
137 GVariant *val;
138 if (strcmp (key, SYSTEM_MONO_FONT) != 0) return;
139 val = g_settings_get_value (settings, SYSTEM_MONO_FONT);
140 if (val)
141 {
142 g_variant_ref_sink (val);
143 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
144 {
145 const gchar *newfont = g_variant_get_string (val, NULL);
146 store_monospaced_changed (newfont);
147 }
148 g_variant_unref (val);
149 }
150 }
151
152 #else
153 #ifdef HAVE_GCONF
154 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
155
156 #ifdef HAVE_XFT
157 # define SYSTEM_FONT "/desktop/gnome/interface/font_name"
158 #endif
159
160 /* Callback called when something changed in GConf that we care about,
161 that is SYSTEM_MONO_FONT. */
162
163 static void
164 something_changedCB (GConfClient *client,
165 guint cnxn_id,
166 GConfEntry *entry,
167 gpointer user_data)
168 {
169 GConfValue *v = gconf_entry_get_value (entry);
170
171 if (!v) return;
172 if (v->type == GCONF_VALUE_STRING)
173 {
174 const char *value = gconf_value_get_string (v);
175 store_monospaced_changed (value);
176 }
177 }
178
179 #endif /* HAVE_GCONF */
180 #endif /* ! HAVE_GSETTINGS */
181
182 #ifdef HAVE_XFT
183
184 /* Older fontconfig versions don't have FC_LCD_*. */
185 #ifndef FC_LCD_NONE
186 #define FC_LCD_NONE 0
187 #endif
188 #ifndef FC_LCD_DEFAULT
189 #define FC_LCD_DEFAULT 1
190 #endif
191 #ifndef FC_LCD_FILTER
192 #define FC_LCD_FILTER "lcdfilter"
193 #endif
194
195 #endif /* HAVE_XFT */
196
197 /* Find the window that contains the XSETTINGS property values. */
198
199 static void
200 get_prop_window (struct x_display_info *dpyinfo)
201 {
202 Display *dpy = dpyinfo->display;
203
204 XGrabServer (dpy);
205 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
206 dpyinfo->Xatom_xsettings_sel);
207 if (dpyinfo->xsettings_window != None)
208 /* Select events so we can detect if window is deleted or if settings
209 are changed. */
210 XSelectInput (dpy, dpyinfo->xsettings_window,
211 PropertyChangeMask|StructureNotifyMask);
212
213 XUngrabServer (dpy);
214 }
215
216 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
217 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
218 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
219 #define PAD(nr) (((nr) + 3) & ~3)
220
221 /* Parse xsettings and extract those that deal with Xft.
222 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
223 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
224
225 Layout of prop. First is a header:
226
227 bytes type what
228 ------------------------------------
229 1 CARD8 byte-order
230 3 unused
231 4 CARD32 SERIAL
232 4 CARD32 N_SETTINGS
233
234 Then N_SETTINGS records, with header:
235
236 bytes type what
237 ------------------------------------
238 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
239 1 unused
240 2 CARD16 n == name-length
241 n STRING8 name
242 p unused, p=pad_to_even_4(n)
243 4 CARD32 last-change-serial
244
245 and then the value, For string:
246
247 bytes type what
248 ------------------------------------
249 4 CARD32 n = value-length
250 n STRING8 value
251 p unused, p=pad_to_even_4(n)
252
253 For integer:
254
255 bytes type what
256 ------------------------------------
257 4 INT32 value
258
259 For RGB color:
260
261 bytes type what
262 ------------------------------------
263 2 CARD16 red
264 2 CARD16 blue
265 2 CARD16 green
266 2 CARD16 alpha
267
268 Returns non-zero if some Xft settings was seen, zero otherwise.
269 */
270
271 static int
272 parse_settings (unsigned char *prop,
273 long unsigned int bytes,
274 struct xsettings *settings)
275 {
276 Lisp_Object byteorder = Fbyteorder ();
277 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
278 int that_bo = prop[0];
279 CARD32 n_settings;
280 int bytes_parsed = 0;
281 int settings_seen = 0;
282 int i = 0;
283
284 /* First 4 bytes is a serial number, skip that. */
285
286 if (bytes < 12) return BadLength;
287 memcpy (&n_settings, prop+8, 4);
288 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
289 bytes_parsed = 12;
290
291 memset (settings, 0, sizeof (*settings));
292
293 while (bytes_parsed+4 < bytes && settings_seen < 7
294 && i < n_settings)
295 {
296 int type = prop[bytes_parsed++];
297 CARD16 nlen;
298 CARD32 vlen, ival = 0;
299 char name[128]; /* The names we are looking for are not this long. */
300 char sval[128]; /* The values we are looking for are not this long. */
301 int want_this;
302 int to_cpy;
303
304 sval[0] = '\0';
305 ++i;
306 ++bytes_parsed; /* Padding */
307
308 memcpy (&nlen, prop+bytes_parsed, 2);
309 bytes_parsed += 2;
310 if (my_bo != that_bo) nlen = SWAP16 (nlen);
311 if (bytes_parsed+nlen > bytes) return BadLength;
312 to_cpy = nlen > 127 ? 127 : nlen;
313 memcpy (name, prop+bytes_parsed, to_cpy);
314 name[to_cpy] = '\0';
315
316 bytes_parsed += nlen;
317 bytes_parsed = PAD (bytes_parsed);
318
319 bytes_parsed += 4; /* Skip serial for this value */
320 if (bytes_parsed > bytes) return BadLength;
321
322 want_this =
323 #ifdef HAVE_XFT
324 (nlen > 6 && strncmp (name, "Xft/", 4) == 0)
325 ||
326 #endif
327 (strcmp (XSETTINGS_FONT_NAME, name) == 0)
328 || (strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0);
329
330 switch (type)
331 {
332 case 0: /* Integer */
333 if (bytes_parsed+4 > bytes) return BadLength;
334 if (want_this)
335 {
336 memcpy (&ival, prop+bytes_parsed, 4);
337 if (my_bo != that_bo) ival = SWAP32 (ival);
338 }
339 bytes_parsed += 4;
340 break;
341
342 case 1: /* String */
343 if (bytes_parsed+4 > bytes) return BadLength;
344 memcpy (&vlen, prop+bytes_parsed, 4);
345 bytes_parsed += 4;
346 if (my_bo != that_bo) vlen = SWAP32 (vlen);
347 if (want_this)
348 {
349 to_cpy = vlen > 127 ? 127 : vlen;
350 memcpy (sval, prop+bytes_parsed, to_cpy);
351 sval[to_cpy] = '\0';
352 }
353 bytes_parsed += vlen;
354 bytes_parsed = PAD (bytes_parsed);
355 break;
356
357 case 2: /* RGB value */
358 /* No need to parse this */
359 if (bytes_parsed+8 > bytes) return BadLength;
360 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
361 break;
362
363 default: /* Parse Error */
364 return BadValue;
365 }
366
367 if (want_this)
368 {
369 ++settings_seen;
370 if (strcmp (name, XSETTINGS_FONT_NAME) == 0)
371 {
372 settings->font = xstrdup (sval);
373 settings->seen |= SEEN_FONT;
374 }
375 else if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0)
376 {
377 settings->tb_style = xstrdup (sval);
378 settings->seen |= SEEN_TB_STYLE;
379 }
380 #ifdef HAVE_XFT
381 else if (strcmp (name, "Xft/Antialias") == 0)
382 {
383 settings->seen |= SEEN_AA;
384 settings->aa = ival != 0;
385 }
386 else if (strcmp (name, "Xft/Hinting") == 0)
387 {
388 settings->seen |= SEEN_HINTING;
389 settings->hinting = ival != 0;
390 }
391 # ifdef FC_HINT_STYLE
392 else if (strcmp (name, "Xft/HintStyle") == 0)
393 {
394 settings->seen |= SEEN_HINTSTYLE;
395 if (strcmp (sval, "hintnone") == 0)
396 settings->hintstyle = FC_HINT_NONE;
397 else if (strcmp (sval, "hintslight") == 0)
398 settings->hintstyle = FC_HINT_SLIGHT;
399 else if (strcmp (sval, "hintmedium") == 0)
400 settings->hintstyle = FC_HINT_MEDIUM;
401 else if (strcmp (sval, "hintfull") == 0)
402 settings->hintstyle = FC_HINT_FULL;
403 else
404 settings->seen &= ~SEEN_HINTSTYLE;
405 }
406 # endif
407 else if (strcmp (name, "Xft/RGBA") == 0)
408 {
409 settings->seen |= SEEN_RGBA;
410 if (strcmp (sval, "none") == 0)
411 settings->rgba = FC_RGBA_NONE;
412 else if (strcmp (sval, "rgb") == 0)
413 settings->rgba = FC_RGBA_RGB;
414 else if (strcmp (sval, "bgr") == 0)
415 settings->rgba = FC_RGBA_BGR;
416 else if (strcmp (sval, "vrgb") == 0)
417 settings->rgba = FC_RGBA_VRGB;
418 else if (strcmp (sval, "vbgr") == 0)
419 settings->rgba = FC_RGBA_VBGR;
420 else
421 settings->seen &= ~SEEN_RGBA;
422 }
423 else if (strcmp (name, "Xft/DPI") == 0)
424 {
425 settings->seen |= SEEN_DPI;
426 settings->dpi = (double)ival/1024.0;
427 }
428 else if (strcmp (name, "Xft/lcdfilter") == 0)
429 {
430 settings->seen |= SEEN_LCDFILTER;
431 if (strcmp (sval, "none") == 0)
432 settings->lcdfilter = FC_LCD_NONE;
433 else if (strcmp (sval, "lcddefault") == 0)
434 settings->lcdfilter = FC_LCD_DEFAULT;
435 else
436 settings->seen &= ~SEEN_LCDFILTER;
437 }
438 #endif /* HAVE_XFT */
439 }
440 }
441
442 return settings_seen;
443 }
444
445 static int
446 read_settings (struct x_display_info *dpyinfo, struct xsettings *settings)
447 {
448 Atom act_type;
449 int act_form;
450 unsigned long nitems, bytes_after;
451 unsigned char *prop = NULL;
452 Display *dpy = dpyinfo->display;
453 int rc;
454
455 x_catch_errors (dpy);
456 rc = XGetWindowProperty (dpy,
457 dpyinfo->xsettings_window,
458 dpyinfo->Xatom_xsettings_prop,
459 0, LONG_MAX, False, AnyPropertyType,
460 &act_type, &act_form, &nitems, &bytes_after,
461 &prop);
462
463 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
464 && act_type == dpyinfo->Xatom_xsettings_prop)
465 rc = parse_settings (prop, nitems, settings);
466
467 XFree (prop);
468
469 x_uncatch_errors ();
470
471 return rc != 0;
472 }
473
474
475 static void
476 apply_xft_settings (struct x_display_info *dpyinfo,
477 int send_event_p,
478 struct xsettings *settings)
479 {
480 #ifdef HAVE_XFT
481 FcPattern *pat;
482 struct xsettings oldsettings;
483 int changed = 0;
484
485 memset (&oldsettings, 0, sizeof (oldsettings));
486 pat = FcPatternCreate ();
487 XftDefaultSubstitute (dpyinfo->display,
488 XScreenNumberOfScreen (dpyinfo->screen),
489 pat);
490 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
491 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
492 # ifdef FC_HINT_STYLE
493 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
494 # endif
495 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
496 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
497 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
498
499 if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa)
500 {
501 FcPatternDel (pat, FC_ANTIALIAS);
502 FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa);
503 ++changed;
504 oldsettings.aa = settings->aa;
505 }
506
507 if ((settings->seen & SEEN_HINTING) != 0
508 && oldsettings.hinting != settings->hinting)
509 {
510 FcPatternDel (pat, FC_HINTING);
511 FcPatternAddBool (pat, FC_HINTING, settings->hinting);
512 ++changed;
513 oldsettings.hinting = settings->hinting;
514 }
515 if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba)
516 {
517 FcPatternDel (pat, FC_RGBA);
518 FcPatternAddInteger (pat, FC_RGBA, settings->rgba);
519 oldsettings.rgba = settings->rgba;
520 ++changed;
521 }
522
523 /* Older fontconfig versions don't have FC_LCD_FILTER. */
524 if ((settings->seen & SEEN_LCDFILTER) != 0
525 && oldsettings.lcdfilter != settings->lcdfilter)
526 {
527 FcPatternDel (pat, FC_LCD_FILTER);
528 FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter);
529 ++changed;
530 oldsettings.lcdfilter = settings->lcdfilter;
531 }
532
533 # ifdef FC_HINT_STYLE
534 if ((settings->seen & SEEN_HINTSTYLE) != 0
535 && oldsettings.hintstyle != settings->hintstyle)
536 {
537 FcPatternDel (pat, FC_HINT_STYLE);
538 FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle);
539 ++changed;
540 oldsettings.hintstyle = settings->hintstyle;
541 }
542 # endif
543
544 if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi
545 && settings->dpi > 0)
546 {
547 Lisp_Object frame, tail;
548
549 FcPatternDel (pat, FC_DPI);
550 FcPatternAddDouble (pat, FC_DPI, settings->dpi);
551 ++changed;
552 oldsettings.dpi = settings->dpi;
553
554 /* Change the DPI on this display and all frames on the display. */
555 dpyinfo->resy = dpyinfo->resx = settings->dpi;
556 FOR_EACH_FRAME (tail, frame)
557 if (FRAME_X_P (XFRAME (frame))
558 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
559 XFRAME (frame)->resy = XFRAME (frame)->resx = settings->dpi;
560 }
561
562 if (changed)
563 {
564 static char const format[] =
565 "Antialias: %d, Hinting: %d, RGBA: %d, LCDFilter: %d, "
566 "Hintstyle: %d, DPI: %lf";
567 enum
568 {
569 d_formats = 5,
570 d_growth = INT_BUFSIZE_BOUND (int) - sizeof "%d",
571 lf_formats = 1,
572 max_f_integer_digits = DBL_MAX_10_EXP + 1,
573 f_precision = 6,
574 lf_growth = (sizeof "-." + max_f_integer_digits + f_precision
575 - sizeof "%lf")
576 };
577 char buf[sizeof format + d_formats * d_growth + lf_formats * lf_growth];
578
579 XftDefaultSet (dpyinfo->display, pat);
580 if (send_event_p)
581 store_config_changed_event (Qfont_render,
582 XCAR (dpyinfo->name_list_element));
583 sprintf (buf, format, oldsettings.aa, oldsettings.hinting,
584 oldsettings.rgba, oldsettings.lcdfilter,
585 oldsettings.hintstyle, oldsettings.dpi);
586 Vxft_settings = build_string (buf);
587 }
588 else
589 FcPatternDestroy (pat);
590 #endif /* HAVE_XFT */
591 }
592
593 static void
594 read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p)
595 {
596 struct xsettings settings;
597 Lisp_Object dpyname = XCAR (dpyinfo->name_list_element);
598
599 if (!read_settings (dpyinfo, &settings))
600 return;
601
602 apply_xft_settings (dpyinfo, True, &settings);
603 if (settings.seen & SEEN_TB_STYLE)
604 {
605 Lisp_Object style = Qnil;
606 if (strcmp (settings.tb_style, "both") == 0)
607 style = Qboth;
608 else if (strcmp (settings.tb_style, "both-horiz") == 0)
609 style = Qboth_horiz;
610 else if (strcmp (settings.tb_style, "icons") == 0)
611 style = Qimage;
612 else if (strcmp (settings.tb_style, "text") == 0)
613 style = Qtext;
614 if (!NILP (style) && !EQ (style, current_tool_bar_style))
615 {
616 current_tool_bar_style = style;
617 if (send_event_p)
618 store_config_changed_event (Qtool_bar_style, dpyname);
619 }
620 xfree (settings.tb_style);
621 }
622
623 if (settings.seen & SEEN_FONT)
624 {
625 if (!current_font || strcmp (current_font, settings.font) != 0)
626 {
627 xfree (current_font);
628 current_font = settings.font;
629 if (send_event_p)
630 store_config_changed_event (Qfont_name, dpyname);
631 }
632 else
633 xfree (settings.font);
634 }
635 }
636
637 void
638 xft_settings_event (struct x_display_info *dpyinfo, XEvent *event)
639 {
640 int check_window_p = 0;
641 int apply_settings = 0;
642
643 switch (event->type)
644 {
645 case DestroyNotify:
646 if (dpyinfo->xsettings_window == event->xany.window)
647 check_window_p = 1;
648 break;
649
650 case ClientMessage:
651 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
652 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
653 && event->xclient.window == dpyinfo->root_window)
654 check_window_p = 1;
655 break;
656
657 case PropertyNotify:
658 if (event->xproperty.window == dpyinfo->xsettings_window
659 && event->xproperty.state == PropertyNewValue
660 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
661 apply_settings = 1;
662 break;
663 }
664
665
666 if (check_window_p)
667 {
668 dpyinfo->xsettings_window = None;
669 get_prop_window (dpyinfo);
670 if (dpyinfo->xsettings_window != None)
671 apply_settings = 1;
672 }
673
674 if (apply_settings)
675 read_and_apply_settings (dpyinfo, True);
676 }
677
678
679 static void
680 init_gsettings (void)
681 {
682 #ifdef HAVE_GSETTINGS
683 GVariant *val;
684 const gchar *const *schemas;
685 int schema_found = 0;
686
687 #ifdef HAVE_G_TYPE_INIT
688 g_type_init ();
689 #endif
690
691 schemas = g_settings_list_schemas();
692 if (schemas == NULL) return;
693 while (! schema_found && *schemas != NULL)
694 schema_found = strcmp (*schemas++, GSETTINGS_SCHEMA) == 0;
695 if (!schema_found) return;
696
697 gsettings_client = g_settings_new (GSETTINGS_SCHEMA);
698 if (!gsettings_client) return;
699 g_object_ref_sink (G_OBJECT (gsettings_client));
700
701 val = g_settings_get_value (gsettings_client, SYSTEM_MONO_FONT);
702 if (val)
703 {
704 g_variant_ref_sink (val);
705 if (g_variant_is_of_type (val, G_VARIANT_TYPE_STRING))
706 current_mono_font = xstrdup (g_variant_get_string (val, NULL));
707 g_variant_unref (val);
708 }
709
710 g_signal_connect (G_OBJECT (gsettings_client), "changed",
711 G_CALLBACK (something_changedCB), NULL);
712 #endif /* HAVE_GSETTINGS */
713 }
714
715
716 static void
717 init_gconf (void)
718 {
719 #if defined (HAVE_GCONF) && defined (HAVE_XFT) && ! defined (HAVE_GSETTINGS)
720 char *s;
721
722 #ifdef HAVE_G_TYPE_INIT
723 g_type_init ();
724 #endif
725 gconf_client = gconf_client_get_default ();
726 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
727 if (s)
728 {
729 current_mono_font = xstrdup (s);
730 g_free (s);
731 }
732 s = gconf_client_get_string (gconf_client, SYSTEM_FONT, NULL);
733 if (s)
734 {
735 current_font = xstrdup (s);
736 g_free (s);
737 }
738 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
739 gconf_client_add_dir (gconf_client,
740 SYSTEM_MONO_FONT,
741 GCONF_CLIENT_PRELOAD_ONELEVEL,
742 NULL);
743 gconf_client_notify_add (gconf_client,
744 SYSTEM_MONO_FONT,
745 something_changedCB,
746 NULL, NULL, NULL);
747 #endif /* HAVE_GCONF && HAVE_XFT && ! HAVE_GSETTINGS */
748 }
749
750 static void
751 init_xsettings (struct x_display_info *dpyinfo)
752 {
753 Display *dpy = dpyinfo->display;
754
755 BLOCK_INPUT;
756
757 /* Select events so we can detect client messages sent when selection
758 owner changes. */
759 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
760
761 get_prop_window (dpyinfo);
762 if (dpyinfo->xsettings_window != None)
763 read_and_apply_settings (dpyinfo, False);
764
765 UNBLOCK_INPUT;
766 }
767
768 void
769 xsettings_initialize (struct x_display_info *dpyinfo)
770 {
771 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
772 init_gsettings ();
773 init_gconf ();
774 init_xsettings (dpyinfo);
775 }
776
777 const char *
778 xsettings_get_system_font (void)
779 {
780 return current_mono_font;
781 }
782
783 #ifdef USE_LUCID
784 const char *
785 xsettings_get_system_normal_font (void)
786 {
787 return current_font;
788 }
789 #endif
790
791 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font,
792 Sfont_get_system_normal_font,
793 0, 0, 0,
794 doc: /* Get the system default application font. */)
795 (void)
796 {
797 return current_font ? build_string (current_font) : Qnil;
798 }
799
800 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
801 0, 0, 0,
802 doc: /* Get the system default fixed width font. */)
803 (void)
804 {
805 return current_mono_font ? build_string (current_mono_font) : Qnil;
806 }
807
808 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style,
809 Stool_bar_get_system_style, 0, 0, 0,
810 doc: /* Get the system tool bar style.
811 If no system tool bar style is known, return `tool-bar-style' if set to a
812 known style. Otherwise return image. */)
813 (void)
814 {
815 if (EQ (Vtool_bar_style, Qimage)
816 || EQ (Vtool_bar_style, Qtext)
817 || EQ (Vtool_bar_style, Qboth)
818 || EQ (Vtool_bar_style, Qboth_horiz)
819 || EQ (Vtool_bar_style, Qtext_image_horiz))
820 return Vtool_bar_style;
821 if (!NILP (current_tool_bar_style))
822 return current_tool_bar_style;
823 return Qimage;
824 }
825
826 void
827 syms_of_xsettings (void)
828 {
829 current_mono_font = NULL;
830 current_font = NULL;
831 first_dpyinfo = NULL;
832 #ifdef HAVE_GSETTINGS
833 gsettings_client = NULL;
834 #else
835 #ifdef HAVE_GCONF
836 gconf_client = NULL;
837 #endif
838 #endif
839
840 DEFSYM (Qmonospace_font_name, "monospace-font-name");
841 DEFSYM (Qfont_name, "font-name");
842 DEFSYM (Qfont_render, "font-render");
843 defsubr (&Sfont_get_system_font);
844 defsubr (&Sfont_get_system_normal_font);
845
846 DEFVAR_BOOL ("font-use-system-font", use_system_font,
847 doc: /* *Non-nil means to apply the system defined font dynamically.
848 When this is non-nil and the system defined fixed width font changes, we
849 update frames dynamically.
850 If this variable is nil, Emacs ignores system font changes. */);
851 use_system_font = 0;
852
853 DEFVAR_LISP ("xft-settings", Vxft_settings,
854 doc: /* Font settings applied to Xft. */);
855 Vxft_settings = make_string ("", 0);
856
857 #ifdef HAVE_XFT
858 Fprovide (intern_c_string ("font-render-setting"), Qnil);
859 #if defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
860 Fprovide (intern_c_string ("system-font-setting"), Qnil);
861 #endif
862 #endif
863
864 current_tool_bar_style = Qnil;
865 DEFSYM (Qtool_bar_style, "tool-bar-style");
866 defsubr (&Stool_bar_get_system_style);
867
868 Fprovide (intern_c_string ("dynamic-setting"), Qnil);
869 }