comparison src/xsettings.c @ 106085:cd4cbab8bb21

Handle system default font and changing font parameters. * xterm.h (struct x_display_info): Add atoms and Window for xsettings. * xterm.c (handle_one_xevent): Call xft_settings_event for ClientMessage, PropertyNotify and DestroyNotify. (x_term_init): If we have XFT, get DPI from Xft.dpi. Call xsettings_initialize. * xftfont.c (xftfont_fix_match): New function. (xftfont_open): Call XftDefaultSubstitute before XftFontMatch. Call xftfont_fix_match after XftFontMatch. * xfont.c (xfont_driver): Initialize all members. * xfns.c (x_default_font_parameter): Try font from Ffont_get_system_font. Do not get font from x_default_parameter if we got one from Ffont_get_system_font. (Fx_select_font): Get the defaut font name from :name of FRAME_FONT (f). * w32font.c (w32font_driver): Initialize all members. * termhooks.h (enum event_kind): CONFIG_CHANGED_EVENT is new. * lisp.h: Declare syms_of_xsettings. * keyboard.c (kbd_buffer_get_event, make_lispy_event): Handle CONFIG_CHANGED_EVENT. * ftfont.c (ftfont_filter_properties): New function. * frame.c (x_set_font): Remove unused variable lval. * font.h (struct font_driver): filter_properties is new. * font.c (font_put_extra): Don't return if val is nil, it means boolean option is off. (font_parse_fcname): Collect all extra properties in extra_props and call filter_properties for all drivers with extra_props and font as parameter. (font_open_entity): Do not use cache, it does not pick up new fontconfig settings like hinting. (font_load_for_lface): If spec had a name in it, store it in entity. * emacs.c (main): Call syms_of_xsettings * config.in: HAVE_GCONF is new. * Makefile.in (GCONF_CFLAGS, GCONF_LIBS): New variables for HAVE_GCONF. xsettings.o is new. * menu-bar.el: Put "Use system font" in Option-menu. * loadup.el: If feature system-font-setting or font-render-setting is there, load font-setting. * Makefile.in (ELCFILES): font-settings.el is new. * font-setting.el: New file. * NEWS: Mention dynamic font changes (font-use-system-font). * configure.in: New option: --with(out)-gconf. Set HAVE_GCONF if we find gconf.
author Jan Djärv <jan.h.d@swipnet.se>
date Tue, 17 Nov 2009 08:21:23 +0000
parents
children b40edfe3e412
comparison
equal deleted inserted replaced
106084:f03048d6d95a 106085:cd4cbab8bb21
1 /* Functions for handle font changes dynamically.
2 Copyright (C) 2009
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 "blockinput.h"
28 #include "termhooks.h"
29 #include "termopts.h"
30
31 #include <X11/Xproto.h>
32
33 #ifdef HAVE_GCONF
34 #include <gconf/gconf-client.h>
35 #endif
36 #ifdef HAVE_XFT
37 #include <X11/Xft/Xft.h>
38 #endif
39
40 static char *current_mono_font;
41 static struct x_display_info *first_dpyinfo;
42 static Lisp_Object Qfont_name, Qfont_render;
43
44 #ifdef HAVE_GCONF
45 static GConfClient *gconf_client;
46 #endif
47
48
49 static void
50 store_font_changed_event (arg, display_name)
51 Lisp_Object arg;
52 Lisp_Object display_name;
53 {
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);
60 }
61
62 #ifdef HAVE_GCONF
63
64 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
65
66 /* Callback called when something changed in GConf that we care about,
67 that is SYSTEM_MONO_FONT. */
68
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;
75 {
76 GConfValue *v = gconf_entry_get_value (entry);
77
78 if (!v) return;
79 if (v->type == GCONF_VALUE_STRING)
80 {
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.
85
86 xfree (current_mono_font);
87 current_mono_font = xstrdup (value);
88 }
89
90
91 if (first_dpyinfo != NULL)
92 {
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;
98
99 if (found)
100 store_font_changed_event (Qfont_name,
101 XCAR (first_dpyinfo->name_list_element));
102 }
103 }
104 #endif /* HAVE_GCONF */
105
106 #ifdef HAVE_XFT
107
108 /* Find the window that contains the XSETTINGS property values. */
109
110 static void
111 get_prop_window (dpyinfo)
112 struct x_display_info *dpyinfo;
113 {
114 Display *dpy = dpyinfo->display;
115
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);
124
125 XUngrabServer (dpy);
126 }
127
128 struct xsettings
129 {
130 FcBool aa, hinting;
131 int rgba, lcdfilter, hintstyle;
132 double dpi;
133 };
134
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)
139
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.
143
144 Layout of prop. First is a header:
145
146 bytes type what
147 ------------------------------------
148 1 CARD8 byte-order
149 3 unused
150 4 CARD32 SERIAL
151 4 CARD32 N_SETTINGS
152
153 Then N_SETTINGS records, with header:
154
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
163
164 and then the value, For string:
165
166 bytes type what
167 ------------------------------------
168 4 CARD32 n = value-length
169 n STRING8 value
170 p unused, p=pad_to_even_4(n)
171
172 For integer:
173
174 bytes type what
175 ------------------------------------
176 4 INT32 value
177
178 For RGB color:
179
180 bytes type what
181 ------------------------------------
182 2 CARD16 red
183 2 CARD16 blue
184 2 CARD16 green
185 2 CARD16 alpha
186
187 */
188
189 static int
190 parse_xft_settings (prop, bytes, settings)
191 unsigned char *prop;
192 unsigned long bytes;
193 struct xsettings *settings;
194 {
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;
202
203 /* First 4 bytes is a serial number, skip that. */
204
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;
209
210 memset (settings, 0, sizeof (*settings));
211
212 while (bytes_parsed+4 < bytes && settings_seen < 6
213 && i < n_settings)
214 {
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;
222
223 sval[0] = '\0';
224 ++i;
225 ++bytes_parsed; /* Padding */
226
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';
234
235 bytes_parsed += nlen;
236 bytes_parsed = PAD (bytes_parsed);
237
238 bytes_parsed += 4; /* Skip serial for this value */
239 if (bytes_parsed > bytes) return BadLength;
240
241 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
242
243 switch (type)
244 {
245 case 0: /* Integer */
246 if (bytes_parsed+4 > bytes) return BadLength;
247 if (is_xft)
248 {
249 memcpy (&ival, prop+bytes_parsed, 4);
250 if (my_bo != that_bo) ival = SWAP32 (ival);
251 }
252 bytes_parsed += 4;
253 break;
254
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)
261 {
262 to_cpy = vlen > 127 ? 127 : vlen;
263 memcpy (sval, prop+bytes_parsed, to_cpy);
264 sval[to_cpy] = '\0';
265 }
266 bytes_parsed += vlen;
267 bytes_parsed = PAD (bytes_parsed);
268 break;
269
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;
275
276 default: /* Parse Error */
277 return BadValue;
278 }
279
280 if (is_xft)
281 {
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)
288 {
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;
297 }
298 else if (strcmp (name, "Xft/RGBA") == 0)
299 {
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;
310 }
311 else if (strcmp (name, "Xft/DPI") == 0)
312 settings->dpi = (double)ival/1024.0;
313 else if (strcmp (name, "Xft/lcdfilter") == 0)
314 {
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;
319 }
320 }
321 }
322
323 return Success;
324 }
325
326 static int
327 read_xft_settings (dpyinfo, settings)
328 struct x_display_info *dpyinfo;
329 struct xsettings *settings;
330 {
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;
338
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);
346
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);
350
351 XFree (prop);
352
353 x_uncatch_errors ();
354
355 return rc == Success;
356 }
357
358 static void
359 apply_xft_settings (dpyinfo, send_event_p)
360 struct x_display_info *dpyinfo;
361 int send_event_p;
362 {
363 FcPattern *pat;
364 struct xsettings settings, oldsettings;
365 int changed = 0;
366
367 if (!read_xft_settings (dpyinfo, &settings))
368 return;
369
370 memset (&oldsettings, 0, sizeof (oldsettings));
371
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);
382
383 if (oldsettings.aa != settings.aa)
384 {
385 FcPatternDel (pat, FC_ANTIALIAS);
386 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
387 ++changed;
388 }
389 if (oldsettings.hinting != settings.hinting)
390 {
391 FcPatternDel (pat, FC_HINTING);
392 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
393 ++changed;
394 }
395 if (oldsettings.rgba != settings.rgba)
396 {
397 FcPatternDel (pat, FC_RGBA);
398 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
399 ++changed;
400 }
401 if (oldsettings.lcdfilter != settings.lcdfilter)
402 {
403 FcPatternDel (pat, FC_LCD_FILTER);
404 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
405 ++changed;
406 }
407 if (oldsettings.hintstyle != settings.hintstyle)
408 {
409 FcPatternDel (pat, FC_HINT_STYLE);
410 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
411 ++changed;
412 }
413 if (oldsettings.dpi != settings.dpi)
414 {
415 Lisp_Object frame, tail;
416
417 FcPatternDel (pat, FC_DPI);
418 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
419 ++changed;
420
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;
427 }
428
429 if (changed)
430 {
431 XftDefaultSet (dpyinfo->display, pat);
432 if (send_event_p)
433 store_font_changed_event (Qfont_render,
434 XCAR (dpyinfo->name_list_element));
435 }
436 else
437 FcPatternDestroy (pat);
438 }
439
440 #endif /* HAVE_XFT */
441
442 void
443 xft_settings_event (dpyinfo, event)
444 struct x_display_info *dpyinfo;
445 XEvent *event;
446 {
447 #ifdef HAVE_XFT
448 int check_window_p = 0;
449
450 switch (event->type)
451 {
452 case DestroyNotify:
453 if (dpyinfo->xsettings_window == event->xany.window)
454 check_window_p = 1;
455 break;
456
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;
463
464 case PropertyNotify:
465 if (event->xproperty.window == dpyinfo->xsettings_window
466 && event->xproperty.state == PropertyNewValue
467 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
468 {
469 apply_xft_settings (dpyinfo, True);
470 }
471 break;
472 }
473
474 if (check_window_p)
475 {
476 dpyinfo->xsettings_window = None;
477 get_prop_window (dpyinfo);
478 if (dpyinfo->xsettings_window != None)
479 apply_xft_settings (dpyinfo, True);
480 }
481 #endif /* HAVE_XFT */
482 }
483
484
485 static void
486 init_gconf ()
487 {
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];
496
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;
509
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)
514 {
515 current_mono_font = xstrdup (s);
516 g_free (s);
517 }
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);
532
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])
536 {
537 #ifdef F_SETOWN
538 fcntl (i, F_SETOWN, getpid ());
539 #endif /* ! defined (F_SETOWN) */
540
541 #ifdef SIGIO
542 if (interrupt_input)
543 init_sigio (i);
544 #endif /* ! defined (SIGIO) */
545 }
546 #endif /* HAVE_GCONF */
547 }
548
549 static void
550 init_xfd_settings (dpyinfo)
551 struct x_display_info *dpyinfo;
552 {
553 #ifdef HAVE_XFT
554 char sel[64];
555 Display *dpy = dpyinfo->display;
556
557 BLOCK_INPUT;
558
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);
565
566 /* Select events so we can detect client messages sent when selection
567 owner changes. */
568 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
569
570 get_prop_window (dpyinfo);
571 if (dpyinfo->xsettings_window != None)
572 apply_xft_settings (dpyinfo, False);
573
574 UNBLOCK_INPUT;
575
576 #else /* ! HAVE_XFT */
577
578 dpyinfo->Xatom_xsettings_sel = None;
579 dpyinfo->Xatom_xsettings_prop = None;
580 dpyinfo->Xatom_xsettings_mgr = None;
581 dpyinfo->xsettings_window = None;
582
583 #endif /* ! HAVE_XFT */
584 }
585
586 void
587 xsettings_initialize (dpyinfo)
588 struct x_display_info *dpyinfo;
589 {
590 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
591 init_gconf ();
592 init_xfd_settings (dpyinfo);
593 }
594
595
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. */)
599 ()
600 {
601 return current_mono_font
602 ? make_string (current_mono_font, strlen (current_mono_font))
603 : Qnil;
604 }
605
606 void
607 syms_of_xsettings ()
608 {
609 current_mono_font = NULL;
610 first_dpyinfo = NULL;
611 #ifdef HAVE_GCONF
612 gconf_client = NULL;
613 #endif
614
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);
620
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
627 }