configure.in: Don't check for RSVG or GConf unless X11 is used.
[emacs.git] / src / xsettings.c
blob946cfe7b8990f08d676f974a4e38b70964ff2a89
1 /* Functions for handle font changes dynamically.
2 Copyright (C) 2009
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include <setjmp.h>
22 #include <fcntl.h>
23 #include "lisp.h"
24 #include "xterm.h"
25 #include "xsettings.h"
26 #include "frame.h"
27 #include "keyboard.h"
28 #include "blockinput.h"
29 #include "termhooks.h"
30 #include "termopts.h"
32 #include <X11/Xproto.h>
34 #ifdef HAVE_GCONF
35 #include <gconf/gconf-client.h>
36 #endif
37 #ifdef HAVE_XFT
38 #include <X11/Xft/Xft.h>
39 #endif
41 static char *current_mono_font;
42 static struct x_display_info *first_dpyinfo;
43 static Lisp_Object Qfont_name, Qfont_render;
44 static int use_system_font;
46 #ifdef HAVE_GCONF
47 static GConfClient *gconf_client;
48 #endif
51 static void
52 store_font_changed_event (arg, display_name)
53 Lisp_Object arg;
54 Lisp_Object display_name;
56 struct input_event event;
57 EVENT_INIT (event);
58 event.kind = CONFIG_CHANGED_EVENT;
59 event.frame_or_window = display_name;
60 event.arg = arg;
61 kbd_buffer_store_event (&event);
64 #ifdef HAVE_GCONF
66 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
68 /* Callback called when something changed in GConf that we care about,
69 that is SYSTEM_MONO_FONT. */
71 static void
72 something_changedCB (client, cnxn_id, entry, user_data)
73 GConfClient *client;
74 guint cnxn_id;
75 GConfEntry *entry;
76 gpointer user_data;
78 GConfValue *v = gconf_entry_get_value (entry);
80 if (!v) return;
81 if (v->type == GCONF_VALUE_STRING)
83 const char *value = gconf_value_get_string (v);
84 int i;
85 if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0)
86 return; /* No change. */
88 xfree (current_mono_font);
89 current_mono_font = xstrdup (value);
93 if (first_dpyinfo != NULL)
95 /* Check if display still open */
96 struct x_display_info *dpyinfo;
97 int found = 0;
98 for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
99 found = dpyinfo == first_dpyinfo;
101 if (found && use_system_font)
102 store_font_changed_event (Qfont_name,
103 XCAR (first_dpyinfo->name_list_element));
106 #endif /* HAVE_GCONF */
108 #ifdef HAVE_XFT
110 /* Older fontconfig versions don't have FC_LCD_*. */
111 #ifndef FC_LCD_NONE
112 #define FC_LCD_NONE 0
113 #endif
114 #ifndef FC_LCD_DEFAULT
115 #define FC_LCD_DEFAULT 1
116 #endif
117 #ifndef FC_LCD_FILTER
118 #define FC_LCD_FILTER "lcdfilter"
119 #endif
121 /* Find the window that contains the XSETTINGS property values. */
123 static void
124 get_prop_window (dpyinfo)
125 struct x_display_info *dpyinfo;
127 Display *dpy = dpyinfo->display;
129 XGrabServer (dpy);
130 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
131 dpyinfo->Xatom_xsettings_sel);
132 if (dpyinfo->xsettings_window != None)
133 /* Select events so we can detect if window is deleted or if settings
134 are changed. */
135 XSelectInput (dpy, dpyinfo->xsettings_window,
136 PropertyChangeMask|StructureNotifyMask);
138 XUngrabServer (dpy);
141 struct xsettings
143 FcBool aa, hinting;
144 int rgba, lcdfilter, hintstyle;
145 double dpi;
148 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
149 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
150 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
151 #define PAD(nr) (((nr) + 3) & ~3)
153 /* Parse xsettings and extract those that deal with Xft.
154 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
155 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
157 Layout of prop. First is a header:
159 bytes type what
160 ------------------------------------
161 1 CARD8 byte-order
162 3 unused
163 4 CARD32 SERIAL
164 4 CARD32 N_SETTINGS
166 Then N_SETTINGS records, with header:
168 bytes type what
169 ------------------------------------
170 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
171 1 unused
172 2 CARD16 n == name-length
173 n STRING8 name
174 p unused, p=pad_to_even_4(n)
175 4 CARD32 last-change-serial
177 and then the value, For string:
179 bytes type what
180 ------------------------------------
181 4 CARD32 n = value-length
182 n STRING8 value
183 p unused, p=pad_to_even_4(n)
185 For integer:
187 bytes type what
188 ------------------------------------
189 4 INT32 value
191 For RGB color:
193 bytes type what
194 ------------------------------------
195 2 CARD16 red
196 2 CARD16 blue
197 2 CARD16 green
198 2 CARD16 alpha
202 static int
203 parse_xft_settings (prop, bytes, settings)
204 unsigned char *prop;
205 unsigned long bytes;
206 struct xsettings *settings;
208 Lisp_Object byteorder = Fbyteorder ();
209 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
210 int that_bo = prop[0];
211 CARD32 n_settings;
212 int bytes_parsed = 0;
213 int settings_seen = 0;
214 int i = 0;
216 /* First 4 bytes is a serial number, skip that. */
218 if (bytes < 12) return BadLength;
219 memcpy (&n_settings, prop+8, 4);
220 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
221 bytes_parsed = 12;
223 memset (settings, 0, sizeof (*settings));
225 while (bytes_parsed+4 < bytes && settings_seen < 6
226 && i < n_settings)
228 int type = prop[bytes_parsed++];
229 CARD16 nlen;
230 CARD32 vlen, ival = 0;
231 char name[128]; /* The names we are looking for are not this long. */
232 char sval[128]; /* The values we are looking for are not this long. */
233 int is_xft;
234 int to_cpy;
236 sval[0] = '\0';
237 ++i;
238 ++bytes_parsed; /* Padding */
240 memcpy (&nlen, prop+bytes_parsed, 2);
241 bytes_parsed += 2;
242 if (my_bo != that_bo) nlen = SWAP16 (nlen);
243 if (bytes_parsed+nlen > bytes) return BadLength;
244 to_cpy = nlen > 127 ? 127 : nlen;
245 memcpy (name, prop+bytes_parsed, to_cpy);
246 name[to_cpy] = '\0';
248 bytes_parsed += nlen;
249 bytes_parsed = PAD (bytes_parsed);
251 bytes_parsed += 4; /* Skip serial for this value */
252 if (bytes_parsed > bytes) return BadLength;
254 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
256 switch (type)
258 case 0: /* Integer */
259 if (bytes_parsed+4 > bytes) return BadLength;
260 if (is_xft)
262 memcpy (&ival, prop+bytes_parsed, 4);
263 if (my_bo != that_bo) ival = SWAP32 (ival);
265 bytes_parsed += 4;
266 break;
268 case 1: /* String */
269 if (bytes_parsed+4 > bytes) return BadLength;
270 memcpy (&vlen, prop+bytes_parsed, 4);
271 bytes_parsed += 4;
272 if (my_bo != that_bo) vlen = SWAP32 (vlen);
273 if (is_xft)
275 to_cpy = vlen > 127 ? 127 : vlen;
276 memcpy (sval, prop+bytes_parsed, to_cpy);
277 sval[to_cpy] = '\0';
279 bytes_parsed += vlen;
280 bytes_parsed = PAD (bytes_parsed);
281 break;
283 case 2: /* RGB value */
284 /* No need to parse this */
285 if (bytes_parsed+8 > bytes) return BadLength;
286 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
287 break;
289 default: /* Parse Error */
290 return BadValue;
293 if (is_xft)
295 ++settings_seen;
296 if (strcmp (name, "Xft/Antialias") == 0)
297 settings->aa = ival != 0;
298 else if (strcmp (name, "Xft/Hinting") == 0)
299 settings->hinting = ival != 0;
300 else if (strcmp (name, "Xft/HintStyle") == 0)
302 if (strcmp (sval, "hintnone") == 0)
303 settings->hintstyle = FC_HINT_NONE;
304 else if (strcmp (sval, "hintslight") == 0)
305 settings->hintstyle = FC_HINT_SLIGHT;
306 else if (strcmp (sval, "hintmedium") == 0)
307 settings->hintstyle = FC_HINT_MEDIUM;
308 else if (strcmp (sval, "hintfull") == 0)
309 settings->hintstyle = FC_HINT_FULL;
311 else if (strcmp (name, "Xft/RGBA") == 0)
313 if (strcmp (sval, "none") == 0)
314 settings->rgba = FC_RGBA_NONE;
315 else if (strcmp (sval, "rgb") == 0)
316 settings->rgba = FC_RGBA_RGB;
317 else if (strcmp (sval, "bgr") == 0)
318 settings->rgba = FC_RGBA_BGR;
319 else if (strcmp (sval, "vrgb") == 0)
320 settings->rgba = FC_RGBA_VRGB;
321 else if (strcmp (sval, "vbgr") == 0)
322 settings->rgba = FC_RGBA_VBGR;
324 else if (strcmp (name, "Xft/DPI") == 0)
325 settings->dpi = (double)ival/1024.0;
326 else if (strcmp (name, "Xft/lcdfilter") == 0)
328 if (strcmp (sval, "none") == 0)
329 settings->lcdfilter = FC_LCD_NONE;
330 else if (strcmp (sval, "lcddefault") == 0)
331 settings->lcdfilter = FC_LCD_DEFAULT;
336 return Success;
339 static int
340 read_xft_settings (dpyinfo, settings)
341 struct x_display_info *dpyinfo;
342 struct xsettings *settings;
344 long long_len;
345 Atom act_type;
346 int act_form;
347 unsigned long nitems, bytes_after;
348 unsigned char *prop = NULL;
349 Display *dpy = dpyinfo->display;
350 int rc;
352 x_catch_errors (dpy);
353 rc = XGetWindowProperty (dpy,
354 dpyinfo->xsettings_window,
355 dpyinfo->Xatom_xsettings_prop,
356 0, LONG_MAX, False, AnyPropertyType,
357 &act_type, &act_form, &nitems, &bytes_after,
358 &prop);
360 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
361 && act_type == dpyinfo->Xatom_xsettings_prop)
362 rc = parse_xft_settings (prop, nitems, settings);
364 XFree (prop);
366 x_uncatch_errors ();
368 return rc == Success;
372 static void
373 apply_xft_settings (dpyinfo, send_event_p)
374 struct x_display_info *dpyinfo;
375 int send_event_p;
377 FcPattern *pat;
378 struct xsettings settings, oldsettings;
379 int changed = 0;
381 if (!read_xft_settings (dpyinfo, &settings))
382 return;
384 memset (&oldsettings, 0, sizeof (oldsettings));
386 pat = FcPatternCreate ();
387 XftDefaultSubstitute (dpyinfo->display,
388 XScreenNumberOfScreen (dpyinfo->screen),
389 pat);
390 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
391 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
392 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
393 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
394 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
395 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
397 if (oldsettings.aa != settings.aa)
399 FcPatternDel (pat, FC_ANTIALIAS);
400 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
401 ++changed;
403 if (oldsettings.hinting != settings.hinting)
405 FcPatternDel (pat, FC_HINTING);
406 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
407 ++changed;
409 if (oldsettings.rgba != settings.rgba)
411 FcPatternDel (pat, FC_RGBA);
412 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
413 ++changed;
415 /* Older fontconfig versions don't have FC_LCD_FILTER. */
416 if (oldsettings.lcdfilter != settings.lcdfilter)
418 FcPatternDel (pat, FC_LCD_FILTER);
419 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
420 ++changed;
422 if (oldsettings.hintstyle != settings.hintstyle)
424 FcPatternDel (pat, FC_HINT_STYLE);
425 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
426 ++changed;
428 if (oldsettings.dpi != settings.dpi)
430 Lisp_Object frame, tail;
432 FcPatternDel (pat, FC_DPI);
433 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
434 ++changed;
436 /* Change the DPI on this display and all frames on the display. */
437 dpyinfo->resy = dpyinfo->resx = settings.dpi;
438 FOR_EACH_FRAME (tail, frame)
439 if (FRAME_X_P (XFRAME (frame))
440 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
441 XFRAME (frame)->resy = XFRAME (frame)->resx = settings.dpi;
444 if (changed)
446 XftDefaultSet (dpyinfo->display, pat);
447 if (send_event_p)
448 store_font_changed_event (Qfont_render,
449 XCAR (dpyinfo->name_list_element));
451 else
452 FcPatternDestroy (pat);
455 #endif /* HAVE_XFT */
457 void
458 xft_settings_event (dpyinfo, event)
459 struct x_display_info *dpyinfo;
460 XEvent *event;
462 #ifdef HAVE_XFT
463 int check_window_p = 0;
465 switch (event->type)
467 case DestroyNotify:
468 if (dpyinfo->xsettings_window == event->xany.window)
469 check_window_p = 1;
470 break;
472 case ClientMessage:
473 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
474 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
475 && event->xclient.window == dpyinfo->root_window)
476 check_window_p = 1;
477 break;
479 case PropertyNotify:
480 if (event->xproperty.window == dpyinfo->xsettings_window
481 && event->xproperty.state == PropertyNewValue
482 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
484 apply_xft_settings (dpyinfo, True);
486 break;
489 if (check_window_p)
491 dpyinfo->xsettings_window = None;
492 get_prop_window (dpyinfo);
493 if (dpyinfo->xsettings_window != None)
494 apply_xft_settings (dpyinfo, True);
496 #endif /* HAVE_XFT */
500 static void
501 init_gconf ()
503 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
504 int i;
505 char *s;
507 g_type_init ();
508 gconf_client = gconf_client_get_default ();
509 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
510 if (s)
512 current_mono_font = xstrdup (s);
513 g_free (s);
515 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
516 gconf_client_add_dir (gconf_client,
517 SYSTEM_MONO_FONT,
518 GCONF_CLIENT_PRELOAD_ONELEVEL,
519 NULL);
520 gconf_client_notify_add (gconf_client,
521 SYSTEM_MONO_FONT,
522 something_changedCB,
523 NULL, NULL, NULL);
524 #endif /* HAVE_GCONF && HAVE_XFT */
527 static void
528 init_xfd_settings (dpyinfo)
529 struct x_display_info *dpyinfo;
531 #ifdef HAVE_XFT
532 char sel[64];
533 Display *dpy = dpyinfo->display;
535 BLOCK_INPUT;
537 sprintf (sel, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
538 dpyinfo->Xatom_xsettings_sel = XInternAtom (dpy, sel, False);
539 dpyinfo->Xatom_xsettings_prop = XInternAtom (dpy,
540 "_XSETTINGS_SETTINGS",
541 False);
542 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
544 /* Select events so we can detect client messages sent when selection
545 owner changes. */
546 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
548 get_prop_window (dpyinfo);
549 if (dpyinfo->xsettings_window != None)
550 apply_xft_settings (dpyinfo, False);
552 UNBLOCK_INPUT;
554 #else /* ! HAVE_XFT */
556 dpyinfo->Xatom_xsettings_sel = None;
557 dpyinfo->Xatom_xsettings_prop = None;
558 dpyinfo->Xatom_xsettings_mgr = None;
559 dpyinfo->xsettings_window = None;
561 #endif /* ! HAVE_XFT */
564 void
565 xsettings_initialize (dpyinfo)
566 struct x_display_info *dpyinfo;
568 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
569 init_gconf ();
570 init_xfd_settings (dpyinfo);
573 const char *
574 xsettings_get_system_font ()
576 return current_mono_font;
579 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
580 0, 0, 0,
581 doc: /* Get the system default monospaced font. */)
584 return current_mono_font && use_system_font
585 ? make_string (current_mono_font, strlen (current_mono_font))
586 : Qnil;
589 void
590 syms_of_xsettings ()
592 current_mono_font = NULL;
593 first_dpyinfo = NULL;
594 #ifdef HAVE_GCONF
595 gconf_client = NULL;
596 #endif
598 Qfont_name = intern_c_string ("font-name");
599 staticpro (&Qfont_name);
600 Qfont_render = intern_c_string ("font-render");
601 staticpro (&Qfont_render);
602 defsubr (&Sfont_get_system_font);
604 DEFVAR_BOOL ("font-use-system-font", &use_system_font,
605 doc: /* *Non-nil means to use the system defined font. */);
606 use_system_font = 0;
608 #ifdef HAVE_XFT
609 Fprovide (intern_c_string ("font-render-setting"), Qnil);
610 #ifdef HAVE_GCONF
611 Fprovide (intern_c_string ("system-font-setting"), Qnil);
612 #endif
613 #endif
616 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
617 (do not change this comment) */