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/>. */
25 #include "xsettings.h"
28 #include "blockinput.h"
29 #include "termhooks.h"
32 #include <X11/Xproto.h>
35 #include <gconf/gconf-client.h>
38 #include <X11/Xft/Xft.h>
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
;
47 static GConfClient
*gconf_client
;
52 store_font_changed_event (arg
, display_name
)
54 Lisp_Object display_name
;
56 struct input_event event
;
58 event
.kind
= CONFIG_CHANGED_EVENT
;
59 event
.frame_or_window
= display_name
;
61 kbd_buffer_store_event (&event
);
66 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
68 /* Callback called when something changed in GConf that we care about,
69 that is SYSTEM_MONO_FONT. */
72 something_changedCB (client
, cnxn_id
, entry
, user_data
)
78 GConfValue
*v
= gconf_entry_get_value (entry
);
81 if (v
->type
== GCONF_VALUE_STRING
)
83 const char *value
= gconf_value_get_string (v
);
85 if (current_mono_font
!= NULL
&& strcmp (value
, current_mono_font
) == 0)
86 return; /* No change. */
88 xfree (current_mono_font
);
89 current_mono_font
= xstrdup (value
);
93 if (first_dpyinfo
!= NULL
)
95 /* Check if display still open */
96 struct x_display_info
*dpyinfo
;
98 for (dpyinfo
= x_display_list
; !found
&& dpyinfo
; dpyinfo
= dpyinfo
->next
)
99 found
= dpyinfo
== first_dpyinfo
;
101 if (found
&& use_system_font
)
102 store_font_changed_event (Qfont_name
,
103 XCAR (first_dpyinfo
->name_list_element
));
106 #endif /* HAVE_GCONF */
110 /* Older fontconfig versions don't have FC_LCD_*. */
112 #define FC_LCD_NONE 0
114 #ifndef FC_LCD_DEFAULT
115 #define FC_LCD_DEFAULT 1
117 #ifndef FC_LCD_FILTER
118 #define FC_LCD_FILTER "lcdfilter"
121 /* Find the window that contains the XSETTINGS property values. */
124 get_prop_window (dpyinfo
)
125 struct x_display_info
*dpyinfo
;
127 Display
*dpy
= dpyinfo
->display
;
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
135 XSelectInput (dpy
, dpyinfo
->xsettings_window
,
136 PropertyChangeMask
|StructureNotifyMask
);
145 SEEN_LCDFILTER
= 0x08,
146 SEEN_HINTSTYLE
= 0x10,
152 int rgba
, lcdfilter
, hintstyle
;
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)
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.
167 Layout of prop. First is a header:
170 ------------------------------------
176 Then N_SETTINGS records, with header:
179 ------------------------------------
180 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
182 2 CARD16 n == name-length
184 p unused, p=pad_to_even_4(n)
185 4 CARD32 last-change-serial
187 and then the value, For string:
190 ------------------------------------
191 4 CARD32 n = value-length
193 p unused, p=pad_to_even_4(n)
198 ------------------------------------
204 ------------------------------------
210 Returns non-zero if some Xft settings was seen, zero otherwise.
214 parse_xft_settings (prop
, bytes
, settings
)
217 struct xsettings
*settings
;
219 Lisp_Object byteorder
= Fbyteorder ();
220 int my_bo
= XFASTINT (byteorder
) == 'B' ? MSBFirst
: LSBFirst
;
221 int that_bo
= prop
[0];
223 int bytes_parsed
= 0;
224 int settings_seen
= 0;
227 /* First 4 bytes is a serial number, skip that. */
229 if (bytes
< 12) return BadLength
;
230 memcpy (&n_settings
, prop
+8, 4);
231 if (my_bo
!= that_bo
) n_settings
= SWAP32 (n_settings
);
234 memset (settings
, 0, sizeof (*settings
));
236 while (bytes_parsed
+4 < bytes
&& settings_seen
< 6
239 int type
= prop
[bytes_parsed
++];
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. */
249 ++bytes_parsed
; /* Padding */
251 memcpy (&nlen
, prop
+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
);
259 bytes_parsed
+= nlen
;
260 bytes_parsed
= PAD (bytes_parsed
);
262 bytes_parsed
+= 4; /* Skip serial for this value */
263 if (bytes_parsed
> bytes
) return BadLength
;
265 is_xft
= nlen
> 6 && strncmp (name
, "Xft/", 4) == 0;
269 case 0: /* Integer */
270 if (bytes_parsed
+4 > bytes
) return BadLength
;
273 memcpy (&ival
, prop
+bytes_parsed
, 4);
274 if (my_bo
!= that_bo
) ival
= SWAP32 (ival
);
280 if (bytes_parsed
+4 > bytes
) return BadLength
;
281 memcpy (&vlen
, prop
+bytes_parsed
, 4);
283 if (my_bo
!= that_bo
) vlen
= SWAP32 (vlen
);
286 to_cpy
= vlen
> 127 ? 127 : vlen
;
287 memcpy (sval
, prop
+bytes_parsed
, to_cpy
);
290 bytes_parsed
+= vlen
;
291 bytes_parsed
= PAD (bytes_parsed
);
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. */
300 default: /* Parse Error */
307 if (strcmp (name
, "Xft/Antialias") == 0)
309 settings
->seen
|= SEEN_AA
;
310 settings
->aa
= ival
!= 0;
312 else if (strcmp (name
, "Xft/Hinting") == 0)
314 settings
->seen
|= SEEN_HINTING
;
315 settings
->hinting
= ival
!= 0;
317 else if (strcmp (name
, "Xft/HintStyle") == 0)
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
;
329 settings
->seen
&= ~SEEN_HINTSTYLE
;
331 else if (strcmp (name
, "Xft/RGBA") == 0)
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
;
345 settings
->seen
&= ~SEEN_RGBA
;
347 else if (strcmp (name
, "Xft/DPI") == 0)
349 settings
->seen
|= SEEN_DPI
;
350 settings
->dpi
= (double)ival
/1024.0;
352 else if (strcmp (name
, "Xft/lcdfilter") == 0)
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
;
360 settings
->seen
&= ~SEEN_LCDFILTER
;
365 return settings_seen
;
369 read_xft_settings (dpyinfo
, settings
)
370 struct x_display_info
*dpyinfo
;
371 struct xsettings
*settings
;
376 unsigned long nitems
, bytes_after
;
377 unsigned char *prop
= NULL
;
378 Display
*dpy
= dpyinfo
->display
;
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
,
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
);
402 apply_xft_settings (dpyinfo
, send_event_p
)
403 struct x_display_info
*dpyinfo
;
407 struct xsettings settings
, oldsettings
;
410 if (!read_xft_settings (dpyinfo
, &settings
))
413 memset (&oldsettings
, 0, sizeof (oldsettings
));
415 pat
= FcPatternCreate ();
416 XftDefaultSubstitute (dpyinfo
->display
,
417 XScreenNumberOfScreen (dpyinfo
->screen
),
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
);
426 if ((settings
.seen
& SEEN_AA
) != 0 && oldsettings
.aa
!= settings
.aa
)
428 FcPatternDel (pat
, FC_ANTIALIAS
);
429 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
432 if ((settings
.seen
& SEEN_HINTING
) != 0
433 && oldsettings
.hinting
!= settings
.hinting
)
435 FcPatternDel (pat
, FC_HINTING
);
436 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
439 if ((settings
.seen
& SEEN_RGBA
) != 0 && oldsettings
.rgba
!= settings
.rgba
)
441 FcPatternDel (pat
, FC_RGBA
);
442 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
445 /* Older fontconfig versions don't have FC_LCD_FILTER. */
446 if ((settings
.seen
& SEEN_LCDFILTER
) != 0
447 && oldsettings
.lcdfilter
!= settings
.lcdfilter
)
449 FcPatternDel (pat
, FC_LCD_FILTER
);
450 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
453 if ((settings
.seen
& SEEN_HINTSTYLE
) != 0
454 && oldsettings
.hintstyle
!= settings
.hintstyle
)
456 FcPatternDel (pat
, FC_HINT_STYLE
);
457 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
460 if ((settings
.seen
& SEEN_DPI
) != 0 && oldsettings
.dpi
!= settings
.dpi
463 Lisp_Object frame
, tail
;
465 FcPatternDel (pat
, FC_DPI
);
466 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
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
;
479 XftDefaultSet (dpyinfo
->display
, pat
);
481 store_font_changed_event (Qfont_render
,
482 XCAR (dpyinfo
->name_list_element
));
485 FcPatternDestroy (pat
);
488 #endif /* HAVE_XFT */
491 xft_settings_event (dpyinfo
, event
)
492 struct x_display_info
*dpyinfo
;
496 int check_window_p
= 0;
501 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
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
)
513 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
514 && event
->xproperty
.state
== PropertyNewValue
515 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
517 apply_xft_settings (dpyinfo
, True
);
524 dpyinfo
->xsettings_window
= None
;
525 get_prop_window (dpyinfo
);
526 if (dpyinfo
->xsettings_window
!= None
)
527 apply_xft_settings (dpyinfo
, True
);
529 #endif /* HAVE_XFT */
536 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
541 gconf_client
= gconf_client_get_default ();
542 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
545 current_mono_font
= xstrdup (s
);
548 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
549 gconf_client_add_dir (gconf_client
,
551 GCONF_CLIENT_PRELOAD_ONELEVEL
,
553 gconf_client_notify_add (gconf_client
,
557 #endif /* HAVE_GCONF && HAVE_XFT */
561 init_xfd_settings (dpyinfo
)
562 struct x_display_info
*dpyinfo
;
566 Display
*dpy
= dpyinfo
->display
;
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",
575 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
577 /* Select events so we can detect client messages sent when selection
579 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
581 get_prop_window (dpyinfo
);
582 if (dpyinfo
->xsettings_window
!= None
)
583 apply_xft_settings (dpyinfo
, False
);
587 #else /* ! HAVE_XFT */
589 dpyinfo
->Xatom_xsettings_sel
= None
;
590 dpyinfo
->Xatom_xsettings_prop
= None
;
591 dpyinfo
->Xatom_xsettings_mgr
= None
;
592 dpyinfo
->xsettings_window
= None
;
594 #endif /* ! HAVE_XFT */
598 xsettings_initialize (dpyinfo
)
599 struct x_display_info
*dpyinfo
;
601 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
603 init_xfd_settings (dpyinfo
);
607 xsettings_get_system_font ()
609 return current_mono_font
;
612 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
614 doc
: /* Get the system default monospaced font. */)
617 return current_mono_font
&& use_system_font
618 ? make_string (current_mono_font
, strlen (current_mono_font
))
625 current_mono_font
= NULL
;
626 first_dpyinfo
= NULL
;
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
);
637 DEFVAR_BOOL ("font-use-system-font", &use_system_font
,
638 doc
: /* *Non-nil means to use the system defined font. */);
642 Fprovide (intern_c_string ("font-render-setting"), Qnil
);
644 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
649 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
650 (do not change this comment) */