]> code.delx.au - gnu-emacs/blob - src/xsettings.c
Merge from mainline.
[gnu-emacs] / src / xsettings.c
1 /* Functions for handle font changes dynamically.
2 Copyright (C) 2009, 2010
3 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 #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"
31
32 #include <X11/Xproto.h>
33
34 #ifdef HAVE_GCONF
35 #include <gconf/gconf-client.h>
36 #endif
37 #ifdef HAVE_XFT
38 #include <X11/Xft/Xft.h>
39 #endif
40
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
46 #ifdef HAVE_GCONF
47 static GConfClient *gconf_client;
48 #endif
49
50
51 static void
52 store_font_changed_event (arg, display_name)
53 Lisp_Object arg;
54 Lisp_Object display_name;
55 {
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);
62 }
63
64 #ifdef HAVE_GCONF
65
66 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
67
68 /* Callback called when something changed in GConf that we care about,
69 that is SYSTEM_MONO_FONT. */
70
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;
77 {
78 GConfValue *v = gconf_entry_get_value (entry);
79
80 if (!v) return;
81 if (v->type == GCONF_VALUE_STRING)
82 {
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. */
87
88 xfree (current_mono_font);
89 current_mono_font = xstrdup (value);
90 }
91
92
93 if (first_dpyinfo != NULL)
94 {
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;
100
101 if (found && use_system_font)
102 store_font_changed_event (Qfont_name,
103 XCAR (first_dpyinfo->name_list_element));
104 }
105 }
106 #endif /* HAVE_GCONF */
107
108 #ifdef HAVE_XFT
109
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
120
121 /* Find the window that contains the XSETTINGS property values. */
122
123 static void
124 get_prop_window (dpyinfo)
125 struct x_display_info *dpyinfo;
126 {
127 Display *dpy = dpyinfo->display;
128
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);
137
138 XUngrabServer (dpy);
139 }
140
141 enum {
142 SEEN_AA = 0x01,
143 SEEN_HINTING = 0x02,
144 SEEN_RGBA = 0x04,
145 SEEN_LCDFILTER = 0x08,
146 SEEN_HINTSTYLE = 0x10,
147 SEEN_DPI = 0x20,
148 };
149 struct xsettings
150 {
151 FcBool aa, hinting;
152 int rgba, lcdfilter, hintstyle;
153 double dpi;
154
155 unsigned seen;
156 };
157
158 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
159 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
160 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
161 #define PAD(nr) (((nr) + 3) & ~3)
162
163 /* Parse xsettings and extract those that deal with Xft.
164 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
165 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
166
167 Layout of prop. First is a header:
168
169 bytes type what
170 ------------------------------------
171 1 CARD8 byte-order
172 3 unused
173 4 CARD32 SERIAL
174 4 CARD32 N_SETTINGS
175
176 Then N_SETTINGS records, with header:
177
178 bytes type what
179 ------------------------------------
180 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
181 1 unused
182 2 CARD16 n == name-length
183 n STRING8 name
184 p unused, p=pad_to_even_4(n)
185 4 CARD32 last-change-serial
186
187 and then the value, For string:
188
189 bytes type what
190 ------------------------------------
191 4 CARD32 n = value-length
192 n STRING8 value
193 p unused, p=pad_to_even_4(n)
194
195 For integer:
196
197 bytes type what
198 ------------------------------------
199 4 INT32 value
200
201 For RGB color:
202
203 bytes type what
204 ------------------------------------
205 2 CARD16 red
206 2 CARD16 blue
207 2 CARD16 green
208 2 CARD16 alpha
209
210 Returns non-zero if some Xft settings was seen, zero otherwise.
211 */
212
213 static int
214 parse_xft_settings (prop, bytes, settings)
215 unsigned char *prop;
216 unsigned long bytes;
217 struct xsettings *settings;
218 {
219 Lisp_Object byteorder = Fbyteorder ();
220 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
221 int that_bo = prop[0];
222 CARD32 n_settings;
223 int bytes_parsed = 0;
224 int settings_seen = 0;
225 int i = 0;
226
227 /* First 4 bytes is a serial number, skip that. */
228
229 if (bytes < 12) return BadLength;
230 memcpy (&n_settings, prop+8, 4);
231 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
232 bytes_parsed = 12;
233
234 memset (settings, 0, sizeof (*settings));
235
236 while (bytes_parsed+4 < bytes && settings_seen < 6
237 && i < n_settings)
238 {
239 int type = prop[bytes_parsed++];
240 CARD16 nlen;
241 CARD32 vlen, ival = 0;
242 char name[128]; /* The names we are looking for are not this long. */
243 char sval[128]; /* The values we are looking for are not this long. */
244 int is_xft;
245 int to_cpy;
246
247 sval[0] = '\0';
248 ++i;
249 ++bytes_parsed; /* Padding */
250
251 memcpy (&nlen, prop+bytes_parsed, 2);
252 bytes_parsed += 2;
253 if (my_bo != that_bo) nlen = SWAP16 (nlen);
254 if (bytes_parsed+nlen > bytes) return BadLength;
255 to_cpy = nlen > 127 ? 127 : nlen;
256 memcpy (name, prop+bytes_parsed, to_cpy);
257 name[to_cpy] = '\0';
258
259 bytes_parsed += nlen;
260 bytes_parsed = PAD (bytes_parsed);
261
262 bytes_parsed += 4; /* Skip serial for this value */
263 if (bytes_parsed > bytes) return BadLength;
264
265 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
266
267 switch (type)
268 {
269 case 0: /* Integer */
270 if (bytes_parsed+4 > bytes) return BadLength;
271 if (is_xft)
272 {
273 memcpy (&ival, prop+bytes_parsed, 4);
274 if (my_bo != that_bo) ival = SWAP32 (ival);
275 }
276 bytes_parsed += 4;
277 break;
278
279 case 1: /* String */
280 if (bytes_parsed+4 > bytes) return BadLength;
281 memcpy (&vlen, prop+bytes_parsed, 4);
282 bytes_parsed += 4;
283 if (my_bo != that_bo) vlen = SWAP32 (vlen);
284 if (is_xft)
285 {
286 to_cpy = vlen > 127 ? 127 : vlen;
287 memcpy (sval, prop+bytes_parsed, to_cpy);
288 sval[to_cpy] = '\0';
289 }
290 bytes_parsed += vlen;
291 bytes_parsed = PAD (bytes_parsed);
292 break;
293
294 case 2: /* RGB value */
295 /* No need to parse this */
296 if (bytes_parsed+8 > bytes) return BadLength;
297 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
298 break;
299
300 default: /* Parse Error */
301 return BadValue;
302 }
303
304 if (is_xft)
305 {
306 ++settings_seen;
307 if (strcmp (name, "Xft/Antialias") == 0)
308 {
309 settings->seen |= SEEN_AA;
310 settings->aa = ival != 0;
311 }
312 else if (strcmp (name, "Xft/Hinting") == 0)
313 {
314 settings->seen |= SEEN_HINTING;
315 settings->hinting = ival != 0;
316 }
317 else if (strcmp (name, "Xft/HintStyle") == 0)
318 {
319 settings->seen |= SEEN_HINTSTYLE;
320 if (strcmp (sval, "hintnone") == 0)
321 settings->hintstyle = FC_HINT_NONE;
322 else if (strcmp (sval, "hintslight") == 0)
323 settings->hintstyle = FC_HINT_SLIGHT;
324 else if (strcmp (sval, "hintmedium") == 0)
325 settings->hintstyle = FC_HINT_MEDIUM;
326 else if (strcmp (sval, "hintfull") == 0)
327 settings->hintstyle = FC_HINT_FULL;
328 else
329 settings->seen &= ~SEEN_HINTSTYLE;
330 }
331 else if (strcmp (name, "Xft/RGBA") == 0)
332 {
333 settings->seen |= SEEN_RGBA;
334 if (strcmp (sval, "none") == 0)
335 settings->rgba = FC_RGBA_NONE;
336 else if (strcmp (sval, "rgb") == 0)
337 settings->rgba = FC_RGBA_RGB;
338 else if (strcmp (sval, "bgr") == 0)
339 settings->rgba = FC_RGBA_BGR;
340 else if (strcmp (sval, "vrgb") == 0)
341 settings->rgba = FC_RGBA_VRGB;
342 else if (strcmp (sval, "vbgr") == 0)
343 settings->rgba = FC_RGBA_VBGR;
344 else
345 settings->seen &= ~SEEN_RGBA;
346 }
347 else if (strcmp (name, "Xft/DPI") == 0)
348 {
349 settings->seen |= SEEN_DPI;
350 settings->dpi = (double)ival/1024.0;
351 }
352 else if (strcmp (name, "Xft/lcdfilter") == 0)
353 {
354 settings->seen |= SEEN_LCDFILTER;
355 if (strcmp (sval, "none") == 0)
356 settings->lcdfilter = FC_LCD_NONE;
357 else if (strcmp (sval, "lcddefault") == 0)
358 settings->lcdfilter = FC_LCD_DEFAULT;
359 else
360 settings->seen &= ~SEEN_LCDFILTER;
361 }
362 }
363 }
364
365 return settings_seen;
366 }
367
368 static int
369 read_xft_settings (dpyinfo, settings)
370 struct x_display_info *dpyinfo;
371 struct xsettings *settings;
372 {
373 long long_len;
374 Atom act_type;
375 int act_form;
376 unsigned long nitems, bytes_after;
377 unsigned char *prop = NULL;
378 Display *dpy = dpyinfo->display;
379 int rc;
380
381 x_catch_errors (dpy);
382 rc = XGetWindowProperty (dpy,
383 dpyinfo->xsettings_window,
384 dpyinfo->Xatom_xsettings_prop,
385 0, LONG_MAX, False, AnyPropertyType,
386 &act_type, &act_form, &nitems, &bytes_after,
387 &prop);
388
389 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
390 && act_type == dpyinfo->Xatom_xsettings_prop)
391 rc = parse_xft_settings (prop, nitems, settings);
392
393 XFree (prop);
394
395 x_uncatch_errors ();
396
397 return rc != 0;
398 }
399
400
401 static void
402 apply_xft_settings (dpyinfo, send_event_p)
403 struct x_display_info *dpyinfo;
404 int send_event_p;
405 {
406 FcPattern *pat;
407 struct xsettings settings, oldsettings;
408 int changed = 0;
409
410 if (!read_xft_settings (dpyinfo, &settings))
411 return;
412
413 memset (&oldsettings, 0, sizeof (oldsettings));
414
415 pat = FcPatternCreate ();
416 XftDefaultSubstitute (dpyinfo->display,
417 XScreenNumberOfScreen (dpyinfo->screen),
418 pat);
419 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
420 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
421 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
422 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
423 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
424 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
425
426 if ((settings.seen & SEEN_AA) != 0 && oldsettings.aa != settings.aa)
427 {
428 FcPatternDel (pat, FC_ANTIALIAS);
429 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
430 ++changed;
431 }
432 if ((settings.seen & SEEN_HINTING) != 0
433 && oldsettings.hinting != settings.hinting)
434 {
435 FcPatternDel (pat, FC_HINTING);
436 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
437 ++changed;
438 }
439 if ((settings.seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings.rgba)
440 {
441 FcPatternDel (pat, FC_RGBA);
442 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
443 ++changed;
444 }
445 /* Older fontconfig versions don't have FC_LCD_FILTER. */
446 if ((settings.seen & SEEN_LCDFILTER) != 0
447 && oldsettings.lcdfilter != settings.lcdfilter)
448 {
449 FcPatternDel (pat, FC_LCD_FILTER);
450 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
451 ++changed;
452 }
453 if ((settings.seen & SEEN_HINTSTYLE) != 0
454 && oldsettings.hintstyle != settings.hintstyle)
455 {
456 FcPatternDel (pat, FC_HINT_STYLE);
457 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
458 ++changed;
459 }
460 if ((settings.seen & SEEN_DPI) != 0 && oldsettings.dpi != settings.dpi
461 && settings.dpi > 0)
462 {
463 Lisp_Object frame, tail;
464
465 FcPatternDel (pat, FC_DPI);
466 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
467 ++changed;
468
469 /* Change the DPI on this display and all frames on the display. */
470 dpyinfo->resy = dpyinfo->resx = settings.dpi;
471 FOR_EACH_FRAME (tail, frame)
472 if (FRAME_X_P (XFRAME (frame))
473 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
474 XFRAME (frame)->resy = XFRAME (frame)->resx = settings.dpi;
475 }
476
477 if (changed)
478 {
479 XftDefaultSet (dpyinfo->display, pat);
480 if (send_event_p)
481 store_font_changed_event (Qfont_render,
482 XCAR (dpyinfo->name_list_element));
483 }
484 else
485 FcPatternDestroy (pat);
486 }
487
488 #endif /* HAVE_XFT */
489
490 void
491 xft_settings_event (dpyinfo, event)
492 struct x_display_info *dpyinfo;
493 XEvent *event;
494 {
495 #ifdef HAVE_XFT
496 int check_window_p = 0;
497
498 switch (event->type)
499 {
500 case DestroyNotify:
501 if (dpyinfo->xsettings_window == event->xany.window)
502 check_window_p = 1;
503 break;
504
505 case ClientMessage:
506 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
507 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
508 && event->xclient.window == dpyinfo->root_window)
509 check_window_p = 1;
510 break;
511
512 case PropertyNotify:
513 if (event->xproperty.window == dpyinfo->xsettings_window
514 && event->xproperty.state == PropertyNewValue
515 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
516 {
517 apply_xft_settings (dpyinfo, True);
518 }
519 break;
520 }
521
522 if (check_window_p)
523 {
524 dpyinfo->xsettings_window = None;
525 get_prop_window (dpyinfo);
526 if (dpyinfo->xsettings_window != None)
527 apply_xft_settings (dpyinfo, True);
528 }
529 #endif /* HAVE_XFT */
530 }
531
532
533 static void
534 init_gconf ()
535 {
536 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
537 int i;
538 char *s;
539
540 g_type_init ();
541 gconf_client = gconf_client_get_default ();
542 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
543 if (s)
544 {
545 current_mono_font = xstrdup (s);
546 g_free (s);
547 }
548 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
549 gconf_client_add_dir (gconf_client,
550 SYSTEM_MONO_FONT,
551 GCONF_CLIENT_PRELOAD_ONELEVEL,
552 NULL);
553 gconf_client_notify_add (gconf_client,
554 SYSTEM_MONO_FONT,
555 something_changedCB,
556 NULL, NULL, NULL);
557 #endif /* HAVE_GCONF && HAVE_XFT */
558 }
559
560 static void
561 init_xfd_settings (dpyinfo)
562 struct x_display_info *dpyinfo;
563 {
564 #ifdef HAVE_XFT
565 char sel[64];
566 Display *dpy = dpyinfo->display;
567
568 BLOCK_INPUT;
569
570 sprintf (sel, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
571 dpyinfo->Xatom_xsettings_sel = XInternAtom (dpy, sel, False);
572 dpyinfo->Xatom_xsettings_prop = XInternAtom (dpy,
573 "_XSETTINGS_SETTINGS",
574 False);
575 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
576
577 /* Select events so we can detect client messages sent when selection
578 owner changes. */
579 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
580
581 get_prop_window (dpyinfo);
582 if (dpyinfo->xsettings_window != None)
583 apply_xft_settings (dpyinfo, False);
584
585 UNBLOCK_INPUT;
586
587 #else /* ! HAVE_XFT */
588
589 dpyinfo->Xatom_xsettings_sel = None;
590 dpyinfo->Xatom_xsettings_prop = None;
591 dpyinfo->Xatom_xsettings_mgr = None;
592 dpyinfo->xsettings_window = None;
593
594 #endif /* ! HAVE_XFT */
595 }
596
597 void
598 xsettings_initialize (dpyinfo)
599 struct x_display_info *dpyinfo;
600 {
601 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
602 init_gconf ();
603 init_xfd_settings (dpyinfo);
604 }
605
606 const char *
607 xsettings_get_system_font ()
608 {
609 return current_mono_font;
610 }
611
612 DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
613 0, 0, 0,
614 doc: /* Get the system default monospaced font. */)
615 ()
616 {
617 return current_mono_font && use_system_font
618 ? make_string (current_mono_font, strlen (current_mono_font))
619 : Qnil;
620 }
621
622 void
623 syms_of_xsettings ()
624 {
625 current_mono_font = NULL;
626 first_dpyinfo = NULL;
627 #ifdef HAVE_GCONF
628 gconf_client = NULL;
629 #endif
630
631 Qfont_name = intern_c_string ("font-name");
632 staticpro (&Qfont_name);
633 Qfont_render = intern_c_string ("font-render");
634 staticpro (&Qfont_render);
635 defsubr (&Sfont_get_system_font);
636
637 DEFVAR_BOOL ("font-use-system-font", &use_system_font,
638 doc: /* *Non-nil means to use the system defined font. */);
639 use_system_font = 0;
640
641 #ifdef HAVE_XFT
642 Fprovide (intern_c_string ("font-render-setting"), Qnil);
643 #ifdef HAVE_GCONF
644 Fprovide (intern_c_string ("system-font-setting"), Qnil);
645 #endif
646 #endif
647 }
648
649 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
650 (do not change this comment) */