Bump version to 23.1.90.
[emacs.git] / src / xsettings.c
blob9c4749f4036a9fd1037efa1527210558bc7f546e
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;
43 static int use_system_font;
45 #ifdef HAVE_GCONF
46 static GConfClient *gconf_client;
47 #endif
50 static void
51 store_font_changed_event (arg, display_name)
52 Lisp_Object arg;
53 Lisp_Object display_name;
55 struct input_event event;
56 EVENT_INIT (event);
57 event.kind = CONFIG_CHANGED_EVENT;
58 event.frame_or_window = display_name;
59 event.arg = arg;
60 kbd_buffer_store_event (&event);
63 #ifdef HAVE_GCONF
65 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
67 /* Callback called when something changed in GConf that we care about,
68 that is SYSTEM_MONO_FONT. */
70 static void
71 something_changedCB (client, cnxn_id, entry, user_data)
72 GConfClient *client;
73 guint cnxn_id;
74 GConfEntry *entry;
75 gpointer user_data;
77 GConfValue *v = gconf_entry_get_value (entry);
79 if (!v) return;
80 if (v->type == GCONF_VALUE_STRING)
82 const char *value = gconf_value_get_string (v);
83 int i;
84 if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0)
85 return; /* No change. */
87 xfree (current_mono_font);
88 current_mono_font = xstrdup (value);
92 if (first_dpyinfo != NULL)
94 /* Check if display still open */
95 struct x_display_info *dpyinfo;
96 int found = 0;
97 for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
98 found = dpyinfo == first_dpyinfo;
100 if (found && use_system_font)
101 store_font_changed_event (Qfont_name,
102 XCAR (first_dpyinfo->name_list_element));
105 #endif /* HAVE_GCONF */
107 #ifdef HAVE_XFT
109 /* Older fontconfig versions don't have FC_LCD_*. */
110 #ifndef FC_LCD_NONE
111 #define FC_LCD_NONE 0
112 #endif
113 #ifndef FC_LCD_DEFAULT
114 #define FC_LCD_DEFAULT 1
115 #endif
116 #ifndef FC_LCD_FILTER
117 #define FC_LCD_FILTER "lcdfilter"
118 #endif
120 /* Find the window that contains the XSETTINGS property values. */
122 static void
123 get_prop_window (dpyinfo)
124 struct x_display_info *dpyinfo;
126 Display *dpy = dpyinfo->display;
128 XGrabServer (dpy);
129 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
130 dpyinfo->Xatom_xsettings_sel);
131 if (dpyinfo->xsettings_window != None)
132 /* Select events so we can detect if window is deleted or if settings
133 are changed. */
134 XSelectInput (dpy, dpyinfo->xsettings_window,
135 PropertyChangeMask|StructureNotifyMask);
137 XUngrabServer (dpy);
140 struct xsettings
142 FcBool aa, hinting;
143 int rgba, lcdfilter, hintstyle;
144 double dpi;
147 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
148 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
149 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
150 #define PAD(nr) (((nr) + 3) & ~3)
152 /* Parse xsettings and extract those that deal with Xft.
153 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
154 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
156 Layout of prop. First is a header:
158 bytes type what
159 ------------------------------------
160 1 CARD8 byte-order
161 3 unused
162 4 CARD32 SERIAL
163 4 CARD32 N_SETTINGS
165 Then N_SETTINGS records, with header:
167 bytes type what
168 ------------------------------------
169 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
170 1 unused
171 2 CARD16 n == name-length
172 n STRING8 name
173 p unused, p=pad_to_even_4(n)
174 4 CARD32 last-change-serial
176 and then the value, For string:
178 bytes type what
179 ------------------------------------
180 4 CARD32 n = value-length
181 n STRING8 value
182 p unused, p=pad_to_even_4(n)
184 For integer:
186 bytes type what
187 ------------------------------------
188 4 INT32 value
190 For RGB color:
192 bytes type what
193 ------------------------------------
194 2 CARD16 red
195 2 CARD16 blue
196 2 CARD16 green
197 2 CARD16 alpha
201 static int
202 parse_xft_settings (prop, bytes, settings)
203 unsigned char *prop;
204 unsigned long bytes;
205 struct xsettings *settings;
207 Lisp_Object byteorder = Fbyteorder ();
208 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
209 int that_bo = prop[0];
210 CARD32 n_settings;
211 int bytes_parsed = 0;
212 int settings_seen = 0;
213 int i = 0;
215 /* First 4 bytes is a serial number, skip that. */
217 if (bytes < 12) return BadLength;
218 memcpy (&n_settings, prop+8, 4);
219 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
220 bytes_parsed = 12;
222 memset (settings, 0, sizeof (*settings));
224 while (bytes_parsed+4 < bytes && settings_seen < 6
225 && i < n_settings)
227 int type = prop[bytes_parsed++];
228 CARD16 nlen;
229 CARD32 vlen, ival = 0;
230 char name[128]; /* The names we are looking for are not this long. */
231 char sval[128]; /* The values we are looking for are not this long. */
232 int is_xft;
233 int to_cpy;
235 sval[0] = '\0';
236 ++i;
237 ++bytes_parsed; /* Padding */
239 memcpy (&nlen, prop+bytes_parsed, 2);
240 bytes_parsed += 2;
241 if (my_bo != that_bo) nlen = SWAP16 (nlen);
242 if (bytes_parsed+nlen > bytes) return BadLength;
243 to_cpy = nlen > 127 ? 127 : nlen;
244 memcpy (name, prop+bytes_parsed, to_cpy);
245 name[to_cpy] = '\0';
247 bytes_parsed += nlen;
248 bytes_parsed = PAD (bytes_parsed);
250 bytes_parsed += 4; /* Skip serial for this value */
251 if (bytes_parsed > bytes) return BadLength;
253 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
255 switch (type)
257 case 0: /* Integer */
258 if (bytes_parsed+4 > bytes) return BadLength;
259 if (is_xft)
261 memcpy (&ival, prop+bytes_parsed, 4);
262 if (my_bo != that_bo) ival = SWAP32 (ival);
264 bytes_parsed += 4;
265 break;
267 case 1: /* String */
268 if (bytes_parsed+4 > bytes) return BadLength;
269 memcpy (&vlen, prop+bytes_parsed, 4);
270 bytes_parsed += 4;
271 if (my_bo != that_bo) vlen = SWAP32 (vlen);
272 if (is_xft)
274 to_cpy = vlen > 127 ? 127 : vlen;
275 memcpy (sval, prop+bytes_parsed, to_cpy);
276 sval[to_cpy] = '\0';
278 bytes_parsed += vlen;
279 bytes_parsed = PAD (bytes_parsed);
280 break;
282 case 2: /* RGB value */
283 /* No need to parse this */
284 if (bytes_parsed+8 > bytes) return BadLength;
285 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
286 break;
288 default: /* Parse Error */
289 return BadValue;
292 if (is_xft)
294 ++settings_seen;
295 if (strcmp (name, "Xft/Antialias") == 0)
296 settings->aa = ival != 0;
297 else if (strcmp (name, "Xft/Hinting") == 0)
298 settings->hinting = ival != 0;
299 else if (strcmp (name, "Xft/HintStyle") == 0)
301 if (strcmp (sval, "hintnone") == 0)
302 settings->hintstyle = FC_HINT_NONE;
303 else if (strcmp (sval, "hintslight") == 0)
304 settings->hintstyle = FC_HINT_SLIGHT;
305 else if (strcmp (sval, "hintmedium") == 0)
306 settings->hintstyle = FC_HINT_MEDIUM;
307 else if (strcmp (sval, "hintfull") == 0)
308 settings->hintstyle = FC_HINT_FULL;
310 else if (strcmp (name, "Xft/RGBA") == 0)
312 if (strcmp (sval, "none") == 0)
313 settings->rgba = FC_RGBA_NONE;
314 else if (strcmp (sval, "rgb") == 0)
315 settings->rgba = FC_RGBA_RGB;
316 else if (strcmp (sval, "bgr") == 0)
317 settings->rgba = FC_RGBA_BGR;
318 else if (strcmp (sval, "vrgb") == 0)
319 settings->rgba = FC_RGBA_VRGB;
320 else if (strcmp (sval, "vbgr") == 0)
321 settings->rgba = FC_RGBA_VBGR;
323 else if (strcmp (name, "Xft/DPI") == 0)
324 settings->dpi = (double)ival/1024.0;
325 else if (strcmp (name, "Xft/lcdfilter") == 0)
327 if (strcmp (sval, "none") == 0)
328 settings->lcdfilter = FC_LCD_NONE;
329 else if (strcmp (sval, "lcddefault") == 0)
330 settings->lcdfilter = FC_LCD_DEFAULT;
335 return Success;
338 static int
339 read_xft_settings (dpyinfo, settings)
340 struct x_display_info *dpyinfo;
341 struct xsettings *settings;
343 long long_len;
344 Atom act_type;
345 int act_form;
346 unsigned long nitems, bytes_after;
347 unsigned char *prop = NULL;
348 Display *dpy = dpyinfo->display;
349 int rc;
351 x_catch_errors (dpy);
352 rc = XGetWindowProperty (dpy,
353 dpyinfo->xsettings_window,
354 dpyinfo->Xatom_xsettings_prop,
355 0, LONG_MAX, False, AnyPropertyType,
356 &act_type, &act_form, &nitems, &bytes_after,
357 &prop);
359 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
360 && act_type == dpyinfo->Xatom_xsettings_prop)
361 rc = parse_xft_settings (prop, nitems, settings);
363 XFree (prop);
365 x_uncatch_errors ();
367 return rc == Success;
371 static void
372 apply_xft_settings (dpyinfo, send_event_p)
373 struct x_display_info *dpyinfo;
374 int send_event_p;
376 FcPattern *pat;
377 struct xsettings settings, oldsettings;
378 int changed = 0;
380 if (!read_xft_settings (dpyinfo, &settings))
381 return;
383 memset (&oldsettings, 0, sizeof (oldsettings));
385 pat = FcPatternCreate ();
386 XftDefaultSubstitute (dpyinfo->display,
387 XScreenNumberOfScreen (dpyinfo->screen),
388 pat);
389 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
390 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
391 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
392 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
393 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
394 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
396 if (oldsettings.aa != settings.aa)
398 FcPatternDel (pat, FC_ANTIALIAS);
399 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
400 ++changed;
402 if (oldsettings.hinting != settings.hinting)
404 FcPatternDel (pat, FC_HINTING);
405 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
406 ++changed;
408 if (oldsettings.rgba != settings.rgba)
410 FcPatternDel (pat, FC_RGBA);
411 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
412 ++changed;
414 /* Older fontconfig versions don't have FC_LCD_FILTER. */
415 if (oldsettings.lcdfilter != settings.lcdfilter)
417 FcPatternDel (pat, FC_LCD_FILTER);
418 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
419 ++changed;
421 if (oldsettings.hintstyle != settings.hintstyle)
423 FcPatternDel (pat, FC_HINT_STYLE);
424 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
425 ++changed;
427 if (oldsettings.dpi != settings.dpi)
429 Lisp_Object frame, tail;
431 FcPatternDel (pat, FC_DPI);
432 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
433 ++changed;
435 /* Change the DPI on this display and all frames on the display. */
436 dpyinfo->resy = dpyinfo->resx = settings.dpi;
437 FOR_EACH_FRAME (tail, frame)
438 if (FRAME_X_P (XFRAME (frame))
439 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
440 XFRAME (frame)->resy = XFRAME (frame)->resx = settings.dpi;
443 if (changed)
445 XftDefaultSet (dpyinfo->display, pat);
446 if (send_event_p)
447 store_font_changed_event (Qfont_render,
448 XCAR (dpyinfo->name_list_element));
450 else
451 FcPatternDestroy (pat);
454 #endif /* HAVE_XFT */
456 void
457 xft_settings_event (dpyinfo, event)
458 struct x_display_info *dpyinfo;
459 XEvent *event;
461 #ifdef HAVE_XFT
462 int check_window_p = 0;
464 switch (event->type)
466 case DestroyNotify:
467 if (dpyinfo->xsettings_window == event->xany.window)
468 check_window_p = 1;
469 break;
471 case ClientMessage:
472 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
473 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
474 && event->xclient.window == dpyinfo->root_window)
475 check_window_p = 1;
476 break;
478 case PropertyNotify:
479 if (event->xproperty.window == dpyinfo->xsettings_window
480 && event->xproperty.state == PropertyNewValue
481 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
483 apply_xft_settings (dpyinfo, True);
485 break;
488 if (check_window_p)
490 dpyinfo->xsettings_window = None;
491 get_prop_window (dpyinfo);
492 if (dpyinfo->xsettings_window != None)
493 apply_xft_settings (dpyinfo, True);
495 #endif /* HAVE_XFT */
499 static void
500 init_gconf ()
502 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
503 int i;
504 char *s;
506 g_type_init ();
507 gconf_client = gconf_client_get_default ();
508 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
509 if (s)
511 current_mono_font = xstrdup (s);
512 g_free (s);
514 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
515 gconf_client_add_dir (gconf_client,
516 SYSTEM_MONO_FONT,
517 GCONF_CLIENT_PRELOAD_ONELEVEL,
518 NULL);
519 gconf_client_notify_add (gconf_client,
520 SYSTEM_MONO_FONT,
521 something_changedCB,
522 NULL, NULL, NULL);
523 #endif /* HAVE_GCONF && HAVE_XFT */
526 static void
527 init_xfd_settings (dpyinfo)
528 struct x_display_info *dpyinfo;
530 #ifdef HAVE_XFT
531 char sel[64];
532 Display *dpy = dpyinfo->display;
534 BLOCK_INPUT;
536 sprintf (sel, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
537 dpyinfo->Xatom_xsettings_sel = XInternAtom (dpy, sel, False);
538 dpyinfo->Xatom_xsettings_prop = XInternAtom (dpy,
539 "_XSETTINGS_SETTINGS",
540 False);
541 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
543 /* Select events so we can detect client messages sent when selection
544 owner changes. */
545 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
547 get_prop_window (dpyinfo);
548 if (dpyinfo->xsettings_window != None)
549 apply_xft_settings (dpyinfo, False);
551 UNBLOCK_INPUT;
553 #else /* ! HAVE_XFT */
555 dpyinfo->Xatom_xsettings_sel = None;
556 dpyinfo->Xatom_xsettings_prop = None;
557 dpyinfo->Xatom_xsettings_mgr = None;
558 dpyinfo->xsettings_window = None;
560 #endif /* ! HAVE_XFT */
563 void
564 xsettings_initialize (dpyinfo)
565 struct x_display_info *dpyinfo;
567 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
568 init_gconf ();
569 init_xfd_settings (dpyinfo);
572 const char *
573 xsettings_get_system_font ()
575 return current_mono_font;
578 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
579 0, 0, 0,
580 doc: /* Get the system default monospaced font. */)
583 return current_mono_font && use_system_font
584 ? make_string (current_mono_font, strlen (current_mono_font))
585 : Qnil;
588 void
589 syms_of_xsettings ()
591 current_mono_font = NULL;
592 first_dpyinfo = NULL;
593 #ifdef HAVE_GCONF
594 gconf_client = NULL;
595 #endif
597 Qfont_name = intern_c_string ("font-name");
598 staticpro (&Qfont_name);
599 Qfont_render = intern_c_string ("font-render");
600 staticpro (&Qfont_render);
601 defsubr (&Sfont_get_system_font);
603 DEFVAR_BOOL ("font-use-system-font", &use_system_font,
604 doc: /* *Non-nil means to use the system defined font. */);
605 use_system_font = 0;
607 #ifdef HAVE_XFT
608 Fprovide (intern_c_string ("font-render-setting"), Qnil);
609 #ifdef HAVE_GCONF
610 Fprovide (intern_c_string ("system-font-setting"), Qnil);
611 #endif
612 #endif
615 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
616 (do not change this comment) */