Handle system default font and changing font parameters.
[emacs.git] / src / xsettings.c
blob84e05a1898adf6bb4ae439a6d4d556b6b8104a51
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 "blockinput.h"
28 #include "termhooks.h"
29 #include "termopts.h"
31 #include <X11/Xproto.h>
33 #ifdef HAVE_GCONF
34 #include <gconf/gconf-client.h>
35 #endif
36 #ifdef HAVE_XFT
37 #include <X11/Xft/Xft.h>
38 #endif
40 static char *current_mono_font;
41 static struct x_display_info *first_dpyinfo;
42 static Lisp_Object Qfont_name, Qfont_render;
44 #ifdef HAVE_GCONF
45 static GConfClient *gconf_client;
46 #endif
49 static void
50 store_font_changed_event (arg, display_name)
51 Lisp_Object arg;
52 Lisp_Object display_name;
54 struct input_event event;
55 EVENT_INIT (event);
56 event.kind = CONFIG_CHANGED_EVENT;
57 event.frame_or_window = display_name;
58 event.arg = arg;
59 kbd_buffer_store_event (&event);
62 #ifdef HAVE_GCONF
64 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
66 /* Callback called when something changed in GConf that we care about,
67 that is SYSTEM_MONO_FONT. */
69 static void
70 something_changedCB (client, cnxn_id, entry, user_data)
71 GConfClient *client;
72 guint cnxn_id;
73 GConfEntry *entry;
74 gpointer user_data;
76 GConfValue *v = gconf_entry_get_value (entry);
78 if (!v) return;
79 if (v->type == GCONF_VALUE_STRING)
81 const char *value = gconf_value_get_string (v);
82 int i;
83 if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0)
84 return; // No change.
86 xfree (current_mono_font);
87 current_mono_font = xstrdup (value);
91 if (first_dpyinfo != NULL)
93 /* Check if display still open */
94 struct x_display_info *dpyinfo;
95 int found = 0;
96 for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
97 found = dpyinfo == first_dpyinfo;
99 if (found)
100 store_font_changed_event (Qfont_name,
101 XCAR (first_dpyinfo->name_list_element));
104 #endif /* HAVE_GCONF */
106 #ifdef HAVE_XFT
108 /* Find the window that contains the XSETTINGS property values. */
110 static void
111 get_prop_window (dpyinfo)
112 struct x_display_info *dpyinfo;
114 Display *dpy = dpyinfo->display;
116 XGrabServer (dpy);
117 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
118 dpyinfo->Xatom_xsettings_sel);
119 if (dpyinfo->xsettings_window != None)
120 /* Select events so we can detect if window is deleted or if settings
121 are changed. */
122 XSelectInput (dpy, dpyinfo->xsettings_window,
123 PropertyChangeMask|StructureNotifyMask);
125 XUngrabServer (dpy);
128 struct xsettings
130 FcBool aa, hinting;
131 int rgba, lcdfilter, hintstyle;
132 double dpi;
135 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
136 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
137 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
138 #define PAD(nr) (((nr) + 3) & ~3)
140 /* Parse xsettings and extract those that deal with Xft.
141 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
142 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
144 Layout of prop. First is a header:
146 bytes type what
147 ------------------------------------
148 1 CARD8 byte-order
149 3 unused
150 4 CARD32 SERIAL
151 4 CARD32 N_SETTINGS
153 Then N_SETTINGS records, with header:
155 bytes type what
156 ------------------------------------
157 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
158 1 unused
159 2 CARD16 n == name-length
160 n STRING8 name
161 p unused, p=pad_to_even_4(n)
162 4 CARD32 last-change-serial
164 and then the value, For string:
166 bytes type what
167 ------------------------------------
168 4 CARD32 n = value-length
169 n STRING8 value
170 p unused, p=pad_to_even_4(n)
172 For integer:
174 bytes type what
175 ------------------------------------
176 4 INT32 value
178 For RGB color:
180 bytes type what
181 ------------------------------------
182 2 CARD16 red
183 2 CARD16 blue
184 2 CARD16 green
185 2 CARD16 alpha
189 static int
190 parse_xft_settings (prop, bytes, settings)
191 unsigned char *prop;
192 unsigned long bytes;
193 struct xsettings *settings;
195 Lisp_Object byteorder = Fbyteorder ();
196 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
197 int that_bo = prop[0];
198 CARD32 n_settings;
199 int bytes_parsed = 0;
200 int settings_seen = 0;
201 int i = 0;
203 /* First 4 bytes is a serial number, skip that. */
205 if (bytes < 12) return BadLength;
206 memcpy (&n_settings, prop+8, 4);
207 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
208 bytes_parsed = 12;
210 memset (settings, 0, sizeof (*settings));
212 while (bytes_parsed+4 < bytes && settings_seen < 6
213 && i < n_settings)
215 int type = prop[bytes_parsed++];
216 CARD16 nlen;
217 CARD32 vlen, ival = 0;
218 char name[128]; /* The names we are looking for are not this long. */
219 char sval[128]; /* The values we are looking for are not this long. */
220 int is_xft;
221 int to_cpy;
223 sval[0] = '\0';
224 ++i;
225 ++bytes_parsed; /* Padding */
227 memcpy (&nlen, prop+bytes_parsed, 2);
228 bytes_parsed += 2;
229 if (my_bo != that_bo) nlen = SWAP16 (nlen);
230 if (bytes_parsed+nlen > bytes) return BadLength;
231 to_cpy = nlen > 127 ? 127 : nlen;
232 memcpy (name, prop+bytes_parsed, to_cpy);
233 name[to_cpy] = '\0';
235 bytes_parsed += nlen;
236 bytes_parsed = PAD (bytes_parsed);
238 bytes_parsed += 4; /* Skip serial for this value */
239 if (bytes_parsed > bytes) return BadLength;
241 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
243 switch (type)
245 case 0: /* Integer */
246 if (bytes_parsed+4 > bytes) return BadLength;
247 if (is_xft)
249 memcpy (&ival, prop+bytes_parsed, 4);
250 if (my_bo != that_bo) ival = SWAP32 (ival);
252 bytes_parsed += 4;
253 break;
255 case 1: /* String */
256 if (bytes_parsed+4 > bytes) return BadLength;
257 memcpy (&vlen, prop+bytes_parsed, 4);
258 bytes_parsed += 4;
259 if (my_bo != that_bo) vlen = SWAP32 (vlen);
260 if (is_xft)
262 to_cpy = vlen > 127 ? 127 : vlen;
263 memcpy (sval, prop+bytes_parsed, to_cpy);
264 sval[to_cpy] = '\0';
266 bytes_parsed += vlen;
267 bytes_parsed = PAD (bytes_parsed);
268 break;
270 case 2: /* RGB value */
271 /* No need to parse this */
272 if (bytes_parsed+8 > bytes) return BadLength;
273 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
274 break;
276 default: /* Parse Error */
277 return BadValue;
280 if (is_xft)
282 ++settings_seen;
283 if (strcmp (name, "Xft/Antialias") == 0)
284 settings->aa = ival != 0;
285 else if (strcmp (name, "Xft/Hinting") == 0)
286 settings->hinting = ival != 0;
287 else if (strcmp (name, "Xft/HintStyle") == 0)
289 if (strcmp (sval, "hintnone") == 0)
290 settings->hintstyle = FC_HINT_NONE;
291 else if (strcmp (sval, "hintslight") == 0)
292 settings->hintstyle = FC_HINT_SLIGHT;
293 else if (strcmp (sval, "hintmedium") == 0)
294 settings->hintstyle = FC_HINT_MEDIUM;
295 else if (strcmp (sval, "hintfull") == 0)
296 settings->hintstyle = FC_HINT_FULL;
298 else if (strcmp (name, "Xft/RGBA") == 0)
300 if (strcmp (sval, "none") == 0)
301 settings->rgba = FC_RGBA_NONE;
302 else if (strcmp (sval, "rgb") == 0)
303 settings->rgba = FC_RGBA_RGB;
304 else if (strcmp (sval, "bgr") == 0)
305 settings->rgba = FC_RGBA_BGR;
306 else if (strcmp (sval, "vrgb") == 0)
307 settings->rgba = FC_RGBA_VRGB;
308 else if (strcmp (sval, "vbgr") == 0)
309 settings->rgba = FC_RGBA_VBGR;
311 else if (strcmp (name, "Xft/DPI") == 0)
312 settings->dpi = (double)ival/1024.0;
313 else if (strcmp (name, "Xft/lcdfilter") == 0)
315 if (strcmp (sval, "none") == 0)
316 settings->lcdfilter = FC_LCD_NONE;
317 else if (strcmp (sval, "lcddefault") == 0)
318 settings->lcdfilter = FC_LCD_DEFAULT;
323 return Success;
326 static int
327 read_xft_settings (dpyinfo, settings)
328 struct x_display_info *dpyinfo;
329 struct xsettings *settings;
331 long long_len;
332 Atom act_type;
333 int act_form;
334 unsigned long nitems, bytes_after;
335 unsigned char *prop = NULL;
336 Display *dpy = dpyinfo->display;
337 int rc;
339 x_catch_errors (dpy);
340 rc = XGetWindowProperty (dpy,
341 dpyinfo->xsettings_window,
342 dpyinfo->Xatom_xsettings_prop,
343 0, LONG_MAX, False, AnyPropertyType,
344 &act_type, &act_form, &nitems, &bytes_after,
345 &prop);
347 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
348 && act_type == dpyinfo->Xatom_xsettings_prop)
349 rc = parse_xft_settings (prop, nitems, settings);
351 XFree (prop);
353 x_uncatch_errors ();
355 return rc == Success;
358 static void
359 apply_xft_settings (dpyinfo, send_event_p)
360 struct x_display_info *dpyinfo;
361 int send_event_p;
363 FcPattern *pat;
364 struct xsettings settings, oldsettings;
365 int changed = 0;
367 if (!read_xft_settings (dpyinfo, &settings))
368 return;
370 memset (&oldsettings, 0, sizeof (oldsettings));
372 pat = FcPatternCreate ();
373 XftDefaultSubstitute (dpyinfo->display,
374 XScreenNumberOfScreen (dpyinfo->screen),
375 pat);
376 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
377 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
378 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
379 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
380 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
381 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
383 if (oldsettings.aa != settings.aa)
385 FcPatternDel (pat, FC_ANTIALIAS);
386 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
387 ++changed;
389 if (oldsettings.hinting != settings.hinting)
391 FcPatternDel (pat, FC_HINTING);
392 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
393 ++changed;
395 if (oldsettings.rgba != settings.rgba)
397 FcPatternDel (pat, FC_RGBA);
398 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
399 ++changed;
401 if (oldsettings.lcdfilter != settings.lcdfilter)
403 FcPatternDel (pat, FC_LCD_FILTER);
404 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
405 ++changed;
407 if (oldsettings.hintstyle != settings.hintstyle)
409 FcPatternDel (pat, FC_HINT_STYLE);
410 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
411 ++changed;
413 if (oldsettings.dpi != settings.dpi)
415 Lisp_Object frame, tail;
417 FcPatternDel (pat, FC_DPI);
418 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
419 ++changed;
421 /* Change the DPI on this display and all frames on the display. */
422 dpyinfo->resy = dpyinfo->resx = settings.dpi;
423 FOR_EACH_FRAME (tail, frame)
424 if (FRAME_X_P (XFRAME (frame))
425 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
426 XFRAME (frame)->resy = XFRAME (frame)->resx = settings.dpi;
429 if (changed)
431 XftDefaultSet (dpyinfo->display, pat);
432 if (send_event_p)
433 store_font_changed_event (Qfont_render,
434 XCAR (dpyinfo->name_list_element));
436 else
437 FcPatternDestroy (pat);
440 #endif /* HAVE_XFT */
442 void
443 xft_settings_event (dpyinfo, event)
444 struct x_display_info *dpyinfo;
445 XEvent *event;
447 #ifdef HAVE_XFT
448 int check_window_p = 0;
450 switch (event->type)
452 case DestroyNotify:
453 if (dpyinfo->xsettings_window == event->xany.window)
454 check_window_p = 1;
455 break;
457 case ClientMessage:
458 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
459 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
460 && event->xclient.window == dpyinfo->root_window)
461 check_window_p = 1;
462 break;
464 case PropertyNotify:
465 if (event->xproperty.window == dpyinfo->xsettings_window
466 && event->xproperty.state == PropertyNewValue
467 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
469 apply_xft_settings (dpyinfo, True);
471 break;
474 if (check_window_p)
476 dpyinfo->xsettings_window = None;
477 get_prop_window (dpyinfo);
478 if (dpyinfo->xsettings_window != None)
479 apply_xft_settings (dpyinfo, True);
481 #endif /* HAVE_XFT */
485 static void
486 init_gconf ()
488 #ifdef HAVE_GCONF
489 int i;
490 char *s;
491 /* Should be enough, this is called at startup */
492 #define N_FDS 1024
493 int fd_before[N_FDS], fd_before1[N_FDS];
494 int dummy, n_fds;
495 GPollFD gfds[N_FDS];
497 /* To find out which filedecriptors GConf uses, check before and after.
498 If we do not do this, GConf changes will only happen when Emacs gets
499 an X event. */
500 memset (fd_before, 0, sizeof (fd_before));
501 n_fds = g_main_context_query (g_main_context_default (),
502 G_PRIORITY_LOW,
503 &dummy,
504 gfds,
505 N_FDS);
506 for (i = 0; i < n_fds; ++i)
507 if (gfds[i].fd < N_FDS && gfds[i].fd > 0 && gfds[i].events > 0)
508 fd_before[gfds[i].fd] = 1;
510 g_type_init ();
511 gconf_client = gconf_client_get_default ();
512 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
513 if (s)
515 current_mono_font = xstrdup (s);
516 g_free (s);
518 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
519 gconf_client_add_dir (gconf_client,
520 SYSTEM_MONO_FONT,
521 GCONF_CLIENT_PRELOAD_ONELEVEL,
522 NULL);
523 gconf_client_notify_add (gconf_client,
524 SYSTEM_MONO_FONT,
525 something_changedCB,
526 NULL, NULL, NULL);
527 n_fds = g_main_context_query (g_main_context_default (),
528 G_PRIORITY_LOW,
529 &dummy,
530 gfds,
531 N_FDS);
533 for (i = 0; i < n_fds; ++i)
534 if (gfds[i].fd < N_FDS && gfds[i].fd > 0 && gfds[i].events > 0
535 && !fd_before[gfds[i].fd])
537 #ifdef F_SETOWN
538 fcntl (i, F_SETOWN, getpid ());
539 #endif /* ! defined (F_SETOWN) */
541 #ifdef SIGIO
542 if (interrupt_input)
543 init_sigio (i);
544 #endif /* ! defined (SIGIO) */
546 #endif /* HAVE_GCONF */
549 static void
550 init_xfd_settings (dpyinfo)
551 struct x_display_info *dpyinfo;
553 #ifdef HAVE_XFT
554 char sel[64];
555 Display *dpy = dpyinfo->display;
557 BLOCK_INPUT;
559 sprintf (sel, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
560 dpyinfo->Xatom_xsettings_sel = XInternAtom (dpy, sel, False);
561 dpyinfo->Xatom_xsettings_prop = XInternAtom (dpy,
562 "_XSETTINGS_SETTINGS",
563 False);
564 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
566 /* Select events so we can detect client messages sent when selection
567 owner changes. */
568 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
570 get_prop_window (dpyinfo);
571 if (dpyinfo->xsettings_window != None)
572 apply_xft_settings (dpyinfo, False);
574 UNBLOCK_INPUT;
576 #else /* ! HAVE_XFT */
578 dpyinfo->Xatom_xsettings_sel = None;
579 dpyinfo->Xatom_xsettings_prop = None;
580 dpyinfo->Xatom_xsettings_mgr = None;
581 dpyinfo->xsettings_window = None;
583 #endif /* ! HAVE_XFT */
586 void
587 xsettings_initialize (dpyinfo)
588 struct x_display_info *dpyinfo;
590 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
591 init_gconf ();
592 init_xfd_settings (dpyinfo);
596 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
597 0, 0, 0,
598 doc: /* Get the system default monospaced font. */)
601 return current_mono_font
602 ? make_string (current_mono_font, strlen (current_mono_font))
603 : Qnil;
606 void
607 syms_of_xsettings ()
609 current_mono_font = NULL;
610 first_dpyinfo = NULL;
611 #ifdef HAVE_GCONF
612 gconf_client = NULL;
613 #endif
615 Qfont_name = intern_c_string ("font-name");
616 staticpro (&Qfont_name);
617 Qfont_render = intern_c_string ("font-render");
618 staticpro (&Qfont_render);
619 defsubr (&Sfont_get_system_font);
621 #ifdef HAVE_GCONF
622 Fprovide (intern_c_string ("system-font-setting"), Qnil);
623 #endif
624 #ifdef HAVE_XFT
625 Fprovide (intern_c_string ("font-render-setting"), Qnil);
626 #endif