Rewrite globals to allow indirection in C.
[emacs.git] / src / xsettings.c
blob945007db2f06229fd24d2e379f173c5fd36f9a3e
1 /* Functions for handle font changes dynamically.
2 Copyright (C) 2009, 2010
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;
45 static Lisp_Object Vxft_settings;
48 #ifdef HAVE_GCONF
49 static GConfClient *gconf_client;
50 #endif
53 static void
54 store_font_changed_event (arg, display_name)
55 Lisp_Object arg;
56 Lisp_Object display_name;
58 struct input_event event;
59 EVENT_INIT (event);
60 event.kind = CONFIG_CHANGED_EVENT;
61 event.frame_or_window = display_name;
62 event.arg = arg;
63 kbd_buffer_store_event (&event);
66 #ifdef HAVE_GCONF
68 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
70 /* Callback called when something changed in GConf that we care about,
71 that is SYSTEM_MONO_FONT. */
73 static void
74 something_changedCB (client, cnxn_id, entry, user_data)
75 GConfClient *client;
76 guint cnxn_id;
77 GConfEntry *entry;
78 gpointer user_data;
80 GConfValue *v = gconf_entry_get_value (entry);
82 if (!v) return;
83 if (v->type == GCONF_VALUE_STRING)
85 const char *value = gconf_value_get_string (v);
86 int i;
87 if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0)
88 return; /* No change. */
90 xfree (current_mono_font);
91 current_mono_font = xstrdup (value);
95 if (first_dpyinfo != NULL)
97 /* Check if display still open */
98 struct x_display_info *dpyinfo;
99 int found = 0;
100 for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
101 found = dpyinfo == first_dpyinfo;
103 if (found && use_system_font)
104 store_font_changed_event (Qfont_name,
105 XCAR (first_dpyinfo->name_list_element));
108 #endif /* HAVE_GCONF */
110 #ifdef HAVE_XFT
112 /* Older fontconfig versions don't have FC_LCD_*. */
113 #ifndef FC_LCD_NONE
114 #define FC_LCD_NONE 0
115 #endif
116 #ifndef FC_LCD_DEFAULT
117 #define FC_LCD_DEFAULT 1
118 #endif
119 #ifndef FC_LCD_FILTER
120 #define FC_LCD_FILTER "lcdfilter"
121 #endif
123 /* Find the window that contains the XSETTINGS property values. */
125 static void
126 get_prop_window (dpyinfo)
127 struct x_display_info *dpyinfo;
129 Display *dpy = dpyinfo->display;
131 XGrabServer (dpy);
132 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
133 dpyinfo->Xatom_xsettings_sel);
134 if (dpyinfo->xsettings_window != None)
135 /* Select events so we can detect if window is deleted or if settings
136 are changed. */
137 XSelectInput (dpy, dpyinfo->xsettings_window,
138 PropertyChangeMask|StructureNotifyMask);
140 XUngrabServer (dpy);
143 enum {
144 SEEN_AA = 0x01,
145 SEEN_HINTING = 0x02,
146 SEEN_RGBA = 0x04,
147 SEEN_LCDFILTER = 0x08,
148 SEEN_HINTSTYLE = 0x10,
149 SEEN_DPI = 0x20,
151 struct xsettings
153 FcBool aa, hinting;
154 int rgba, lcdfilter, hintstyle;
155 double dpi;
157 unsigned seen;
160 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
161 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
162 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
163 #define PAD(nr) (((nr) + 3) & ~3)
165 /* Parse xsettings and extract those that deal with Xft.
166 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
167 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
169 Layout of prop. First is a header:
171 bytes type what
172 ------------------------------------
173 1 CARD8 byte-order
174 3 unused
175 4 CARD32 SERIAL
176 4 CARD32 N_SETTINGS
178 Then N_SETTINGS records, with header:
180 bytes type what
181 ------------------------------------
182 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
183 1 unused
184 2 CARD16 n == name-length
185 n STRING8 name
186 p unused, p=pad_to_even_4(n)
187 4 CARD32 last-change-serial
189 and then the value, For string:
191 bytes type what
192 ------------------------------------
193 4 CARD32 n = value-length
194 n STRING8 value
195 p unused, p=pad_to_even_4(n)
197 For integer:
199 bytes type what
200 ------------------------------------
201 4 INT32 value
203 For RGB color:
205 bytes type what
206 ------------------------------------
207 2 CARD16 red
208 2 CARD16 blue
209 2 CARD16 green
210 2 CARD16 alpha
212 Returns non-zero if some Xft settings was seen, zero otherwise.
215 static int
216 parse_xft_settings (prop, bytes, settings)
217 unsigned char *prop;
218 unsigned long bytes;
219 struct xsettings *settings;
221 Lisp_Object byteorder = Fbyteorder ();
222 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
223 int that_bo = prop[0];
224 CARD32 n_settings;
225 int bytes_parsed = 0;
226 int settings_seen = 0;
227 int i = 0;
229 /* First 4 bytes is a serial number, skip that. */
231 if (bytes < 12) return BadLength;
232 memcpy (&n_settings, prop+8, 4);
233 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
234 bytes_parsed = 12;
236 memset (settings, 0, sizeof (*settings));
238 while (bytes_parsed+4 < bytes && settings_seen < 6
239 && i < n_settings)
241 int type = prop[bytes_parsed++];
242 CARD16 nlen;
243 CARD32 vlen, ival = 0;
244 char name[128]; /* The names we are looking for are not this long. */
245 char sval[128]; /* The values we are looking for are not this long. */
246 int is_xft;
247 int to_cpy;
249 sval[0] = '\0';
250 ++i;
251 ++bytes_parsed; /* Padding */
253 memcpy (&nlen, prop+bytes_parsed, 2);
254 bytes_parsed += 2;
255 if (my_bo != that_bo) nlen = SWAP16 (nlen);
256 if (bytes_parsed+nlen > bytes) return BadLength;
257 to_cpy = nlen > 127 ? 127 : nlen;
258 memcpy (name, prop+bytes_parsed, to_cpy);
259 name[to_cpy] = '\0';
261 bytes_parsed += nlen;
262 bytes_parsed = PAD (bytes_parsed);
264 bytes_parsed += 4; /* Skip serial for this value */
265 if (bytes_parsed > bytes) return BadLength;
267 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
269 switch (type)
271 case 0: /* Integer */
272 if (bytes_parsed+4 > bytes) return BadLength;
273 if (is_xft)
275 memcpy (&ival, prop+bytes_parsed, 4);
276 if (my_bo != that_bo) ival = SWAP32 (ival);
278 bytes_parsed += 4;
279 break;
281 case 1: /* String */
282 if (bytes_parsed+4 > bytes) return BadLength;
283 memcpy (&vlen, prop+bytes_parsed, 4);
284 bytes_parsed += 4;
285 if (my_bo != that_bo) vlen = SWAP32 (vlen);
286 if (is_xft)
288 to_cpy = vlen > 127 ? 127 : vlen;
289 memcpy (sval, prop+bytes_parsed, to_cpy);
290 sval[to_cpy] = '\0';
292 bytes_parsed += vlen;
293 bytes_parsed = PAD (bytes_parsed);
294 break;
296 case 2: /* RGB value */
297 /* No need to parse this */
298 if (bytes_parsed+8 > bytes) return BadLength;
299 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
300 break;
302 default: /* Parse Error */
303 return BadValue;
306 if (is_xft)
308 ++settings_seen;
309 if (strcmp (name, "Xft/Antialias") == 0)
311 settings->seen |= SEEN_AA;
312 settings->aa = ival != 0;
314 else if (strcmp (name, "Xft/Hinting") == 0)
316 settings->seen |= SEEN_HINTING;
317 settings->hinting = ival != 0;
319 else if (strcmp (name, "Xft/HintStyle") == 0)
321 settings->seen |= SEEN_HINTSTYLE;
322 if (strcmp (sval, "hintnone") == 0)
323 settings->hintstyle = FC_HINT_NONE;
324 else if (strcmp (sval, "hintslight") == 0)
325 settings->hintstyle = FC_HINT_SLIGHT;
326 else if (strcmp (sval, "hintmedium") == 0)
327 settings->hintstyle = FC_HINT_MEDIUM;
328 else if (strcmp (sval, "hintfull") == 0)
329 settings->hintstyle = FC_HINT_FULL;
330 else
331 settings->seen &= ~SEEN_HINTSTYLE;
333 else if (strcmp (name, "Xft/RGBA") == 0)
335 settings->seen |= SEEN_RGBA;
336 if (strcmp (sval, "none") == 0)
337 settings->rgba = FC_RGBA_NONE;
338 else if (strcmp (sval, "rgb") == 0)
339 settings->rgba = FC_RGBA_RGB;
340 else if (strcmp (sval, "bgr") == 0)
341 settings->rgba = FC_RGBA_BGR;
342 else if (strcmp (sval, "vrgb") == 0)
343 settings->rgba = FC_RGBA_VRGB;
344 else if (strcmp (sval, "vbgr") == 0)
345 settings->rgba = FC_RGBA_VBGR;
346 else
347 settings->seen &= ~SEEN_RGBA;
349 else if (strcmp (name, "Xft/DPI") == 0)
351 settings->seen |= SEEN_DPI;
352 settings->dpi = (double)ival/1024.0;
354 else if (strcmp (name, "Xft/lcdfilter") == 0)
356 settings->seen |= SEEN_LCDFILTER;
357 if (strcmp (sval, "none") == 0)
358 settings->lcdfilter = FC_LCD_NONE;
359 else if (strcmp (sval, "lcddefault") == 0)
360 settings->lcdfilter = FC_LCD_DEFAULT;
361 else
362 settings->seen &= ~SEEN_LCDFILTER;
367 return settings_seen;
370 static int
371 read_xft_settings (dpyinfo, settings)
372 struct x_display_info *dpyinfo;
373 struct xsettings *settings;
375 long long_len;
376 Atom act_type;
377 int act_form;
378 unsigned long nitems, bytes_after;
379 unsigned char *prop = NULL;
380 Display *dpy = dpyinfo->display;
381 int rc;
383 x_catch_errors (dpy);
384 rc = XGetWindowProperty (dpy,
385 dpyinfo->xsettings_window,
386 dpyinfo->Xatom_xsettings_prop,
387 0, LONG_MAX, False, AnyPropertyType,
388 &act_type, &act_form, &nitems, &bytes_after,
389 &prop);
391 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
392 && act_type == dpyinfo->Xatom_xsettings_prop)
393 rc = parse_xft_settings (prop, nitems, settings);
395 XFree (prop);
397 x_uncatch_errors ();
399 return rc != 0;
403 static void
404 apply_xft_settings (dpyinfo, send_event_p)
405 struct x_display_info *dpyinfo;
406 int send_event_p;
408 FcPattern *pat;
409 struct xsettings settings, oldsettings;
410 int changed = 0;
411 char buf[256];
413 if (!read_xft_settings (dpyinfo, &settings))
414 return;
416 memset (&oldsettings, 0, sizeof (oldsettings));
417 buf[0] = '\0';
418 pat = FcPatternCreate ();
419 XftDefaultSubstitute (dpyinfo->display,
420 XScreenNumberOfScreen (dpyinfo->screen),
421 pat);
422 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
423 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
424 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
425 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
426 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
427 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
429 if ((settings.seen & SEEN_AA) != 0 && oldsettings.aa != settings.aa)
431 FcPatternDel (pat, FC_ANTIALIAS);
432 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
433 ++changed;
434 oldsettings.aa = settings.aa;
436 sprintf (buf, "Antialias: %d", oldsettings.aa);
438 if ((settings.seen & SEEN_HINTING) != 0
439 && oldsettings.hinting != settings.hinting)
441 FcPatternDel (pat, FC_HINTING);
442 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
443 ++changed;
444 oldsettings.hinting = settings.hinting;
446 if (strlen (buf) > 0) strcat (buf, ", ");
447 sprintf (buf+strlen (buf), "Hinting: %d", oldsettings.hinting);
448 if ((settings.seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings.rgba)
450 FcPatternDel (pat, FC_RGBA);
451 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
452 oldsettings.rgba = settings.rgba;
453 ++changed;
455 if (strlen (buf) > 0) strcat (buf, ", ");
456 sprintf (buf+strlen (buf), "RGBA: %d", oldsettings.rgba);
458 /* Older fontconfig versions don't have FC_LCD_FILTER. */
459 if ((settings.seen & SEEN_LCDFILTER) != 0
460 && oldsettings.lcdfilter != settings.lcdfilter)
462 FcPatternDel (pat, FC_LCD_FILTER);
463 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
464 ++changed;
465 oldsettings.lcdfilter = settings.lcdfilter;
467 if (strlen (buf) > 0) strcat (buf, ", ");
468 sprintf (buf+strlen (buf), "LCDFilter: %d", oldsettings.lcdfilter);
470 if ((settings.seen & SEEN_HINTSTYLE) != 0
471 && oldsettings.hintstyle != settings.hintstyle)
473 FcPatternDel (pat, FC_HINT_STYLE);
474 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
475 ++changed;
476 oldsettings.hintstyle = settings.hintstyle;
478 if (strlen (buf) > 0) strcat (buf, ", ");
479 sprintf (buf+strlen (buf), "Hintstyle: %d", oldsettings.hintstyle);
481 if ((settings.seen & SEEN_DPI) != 0 && oldsettings.dpi != settings.dpi
482 && settings.dpi > 0)
484 Lisp_Object frame, tail;
486 FcPatternDel (pat, FC_DPI);
487 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
488 ++changed;
489 oldsettings.dpi = settings.dpi;
491 /* Change the DPI on this display and all frames on the display. */
492 dpyinfo->resy = dpyinfo->resx = settings.dpi;
493 FOR_EACH_FRAME (tail, frame)
494 if (FRAME_X_P (XFRAME (frame))
495 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
496 XFRAME (frame)->resy = XFRAME (frame)->resx = settings.dpi;
499 if (strlen (buf) > 0) strcat (buf, ", ");
500 sprintf (buf+strlen (buf), "DPI: %lf", oldsettings.dpi);
502 if (changed)
504 XftDefaultSet (dpyinfo->display, pat);
505 if (send_event_p)
506 store_font_changed_event (Qfont_render,
507 XCAR (dpyinfo->name_list_element));
508 Vxft_settings = make_string (buf, strlen (buf));
510 else
511 FcPatternDestroy (pat);
514 #endif /* HAVE_XFT */
516 void
517 xft_settings_event (dpyinfo, event)
518 struct x_display_info *dpyinfo;
519 XEvent *event;
521 #ifdef HAVE_XFT
522 int check_window_p = 0;
524 switch (event->type)
526 case DestroyNotify:
527 if (dpyinfo->xsettings_window == event->xany.window)
528 check_window_p = 1;
529 break;
531 case ClientMessage:
532 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
533 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
534 && event->xclient.window == dpyinfo->root_window)
535 check_window_p = 1;
536 break;
538 case PropertyNotify:
539 if (event->xproperty.window == dpyinfo->xsettings_window
540 && event->xproperty.state == PropertyNewValue
541 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
543 apply_xft_settings (dpyinfo, True);
545 break;
548 if (check_window_p)
550 dpyinfo->xsettings_window = None;
551 get_prop_window (dpyinfo);
552 if (dpyinfo->xsettings_window != None)
553 apply_xft_settings (dpyinfo, True);
555 #endif /* HAVE_XFT */
559 static void
560 init_gconf ()
562 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
563 int i;
564 char *s;
566 g_type_init ();
567 gconf_client = gconf_client_get_default ();
568 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
569 if (s)
571 current_mono_font = xstrdup (s);
572 g_free (s);
574 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
575 gconf_client_add_dir (gconf_client,
576 SYSTEM_MONO_FONT,
577 GCONF_CLIENT_PRELOAD_ONELEVEL,
578 NULL);
579 gconf_client_notify_add (gconf_client,
580 SYSTEM_MONO_FONT,
581 something_changedCB,
582 NULL, NULL, NULL);
583 #endif /* HAVE_GCONF && HAVE_XFT */
586 static void
587 init_xfd_settings (dpyinfo)
588 struct x_display_info *dpyinfo;
590 #ifdef HAVE_XFT
591 char sel[64];
592 Display *dpy = dpyinfo->display;
594 BLOCK_INPUT;
596 sprintf (sel, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
597 dpyinfo->Xatom_xsettings_sel = XInternAtom (dpy, sel, False);
598 dpyinfo->Xatom_xsettings_prop = XInternAtom (dpy,
599 "_XSETTINGS_SETTINGS",
600 False);
601 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
603 /* Select events so we can detect client messages sent when selection
604 owner changes. */
605 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
607 get_prop_window (dpyinfo);
608 if (dpyinfo->xsettings_window != None)
609 apply_xft_settings (dpyinfo, False);
611 UNBLOCK_INPUT;
613 #else /* ! HAVE_XFT */
615 dpyinfo->Xatom_xsettings_sel = None;
616 dpyinfo->Xatom_xsettings_prop = None;
617 dpyinfo->Xatom_xsettings_mgr = None;
618 dpyinfo->xsettings_window = None;
620 #endif /* ! HAVE_XFT */
623 void
624 xsettings_initialize (dpyinfo)
625 struct x_display_info *dpyinfo;
627 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
628 init_gconf ();
629 init_xfd_settings (dpyinfo);
632 const char *
633 xsettings_get_system_font ()
635 return current_mono_font;
638 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
639 0, 0, 0,
640 doc: /* Get the system default monospaced font. */)
643 return current_mono_font && use_system_font
644 ? make_string (current_mono_font, strlen (current_mono_font))
645 : Qnil;
648 void
649 syms_of_xsettings ()
651 current_mono_font = NULL;
652 first_dpyinfo = NULL;
653 #ifdef HAVE_GCONF
654 gconf_client = NULL;
655 #endif
657 Qfont_name = intern_c_string ("font-name");
658 staticpro (&Qfont_name);
659 Qfont_render = intern_c_string ("font-render");
660 staticpro (&Qfont_render);
661 defsubr (&Sfont_get_system_font);
663 DEFVAR_BOOL ("font-use-system-font", &use_system_font,
664 doc: /* *Non-nil means to use the system defined font. */);
665 use_system_font = 0;
667 DEFVAR_LISP ("xft-settings", &Vxft_settings,
668 doc: /* Font settings applied to Xft. */);
669 Vxft_settings = make_string ("", 0);
671 #ifdef HAVE_XFT
672 Fprovide (intern_c_string ("font-render-setting"), Qnil);
673 #ifdef HAVE_GCONF
674 Fprovide (intern_c_string ("system-font-setting"), Qnil);
675 #endif
676 #endif
679 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
680 (do not change this comment) */