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
;
45 static Lisp_Object Vxft_settings
;
49 static GConfClient
*gconf_client
;
54 store_font_changed_event (arg
, display_name
)
56 Lisp_Object display_name
;
58 struct input_event event
;
60 event
.kind
= CONFIG_CHANGED_EVENT
;
61 event
.frame_or_window
= display_name
;
63 kbd_buffer_store_event (&event
);
68 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
70 /* Callback called when something changed in GConf that we care about,
71 that is SYSTEM_MONO_FONT. */
74 something_changedCB (client
, cnxn_id
, entry
, user_data
)
80 GConfValue
*v
= gconf_entry_get_value (entry
);
83 if (v
->type
== GCONF_VALUE_STRING
)
85 const char *value
= gconf_value_get_string (v
);
87 if (current_mono_font
!= NULL
&& strcmp (value
, current_mono_font
) == 0)
88 return; /* No change. */
90 xfree (current_mono_font
);
91 current_mono_font
= xstrdup (value
);
95 if (first_dpyinfo
!= NULL
)
97 /* Check if display still open */
98 struct x_display_info
*dpyinfo
;
100 for (dpyinfo
= x_display_list
; !found
&& dpyinfo
; dpyinfo
= dpyinfo
->next
)
101 found
= dpyinfo
== first_dpyinfo
;
103 if (found
&& use_system_font
)
104 store_font_changed_event (Qfont_name
,
105 XCAR (first_dpyinfo
->name_list_element
));
108 #endif /* HAVE_GCONF */
112 /* Older fontconfig versions don't have FC_LCD_*. */
114 #define FC_LCD_NONE 0
116 #ifndef FC_LCD_DEFAULT
117 #define FC_LCD_DEFAULT 1
119 #ifndef FC_LCD_FILTER
120 #define FC_LCD_FILTER "lcdfilter"
123 /* Find the window that contains the XSETTINGS property values. */
126 get_prop_window (dpyinfo
)
127 struct x_display_info
*dpyinfo
;
129 Display
*dpy
= dpyinfo
->display
;
132 dpyinfo
->xsettings_window
= XGetSelectionOwner (dpy
,
133 dpyinfo
->Xatom_xsettings_sel
);
134 if (dpyinfo
->xsettings_window
!= None
)
135 /* Select events so we can detect if window is deleted or if settings
137 XSelectInput (dpy
, dpyinfo
->xsettings_window
,
138 PropertyChangeMask
|StructureNotifyMask
);
147 SEEN_LCDFILTER
= 0x08,
148 SEEN_HINTSTYLE
= 0x10,
154 int rgba
, lcdfilter
, hintstyle
;
160 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
161 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
162 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
163 #define PAD(nr) (((nr) + 3) & ~3)
165 /* Parse xsettings and extract those that deal with Xft.
166 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
167 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
169 Layout of prop. First is a header:
172 ------------------------------------
178 Then N_SETTINGS records, with header:
181 ------------------------------------
182 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
184 2 CARD16 n == name-length
186 p unused, p=pad_to_even_4(n)
187 4 CARD32 last-change-serial
189 and then the value, For string:
192 ------------------------------------
193 4 CARD32 n = value-length
195 p unused, p=pad_to_even_4(n)
200 ------------------------------------
206 ------------------------------------
212 Returns non-zero if some Xft settings was seen, zero otherwise.
216 parse_xft_settings (prop
, bytes
, settings
)
219 struct xsettings
*settings
;
221 Lisp_Object byteorder
= Fbyteorder ();
222 int my_bo
= XFASTINT (byteorder
) == 'B' ? MSBFirst
: LSBFirst
;
223 int that_bo
= prop
[0];
225 int bytes_parsed
= 0;
226 int settings_seen
= 0;
229 /* First 4 bytes is a serial number, skip that. */
231 if (bytes
< 12) return BadLength
;
232 memcpy (&n_settings
, prop
+8, 4);
233 if (my_bo
!= that_bo
) n_settings
= SWAP32 (n_settings
);
236 memset (settings
, 0, sizeof (*settings
));
238 while (bytes_parsed
+4 < bytes
&& settings_seen
< 6
241 int type
= prop
[bytes_parsed
++];
243 CARD32 vlen
, ival
= 0;
244 char name
[128]; /* The names we are looking for are not this long. */
245 char sval
[128]; /* The values we are looking for are not this long. */
251 ++bytes_parsed
; /* Padding */
253 memcpy (&nlen
, prop
+bytes_parsed
, 2);
255 if (my_bo
!= that_bo
) nlen
= SWAP16 (nlen
);
256 if (bytes_parsed
+nlen
> bytes
) return BadLength
;
257 to_cpy
= nlen
> 127 ? 127 : nlen
;
258 memcpy (name
, prop
+bytes_parsed
, to_cpy
);
261 bytes_parsed
+= nlen
;
262 bytes_parsed
= PAD (bytes_parsed
);
264 bytes_parsed
+= 4; /* Skip serial for this value */
265 if (bytes_parsed
> bytes
) return BadLength
;
267 is_xft
= nlen
> 6 && strncmp (name
, "Xft/", 4) == 0;
271 case 0: /* Integer */
272 if (bytes_parsed
+4 > bytes
) return BadLength
;
275 memcpy (&ival
, prop
+bytes_parsed
, 4);
276 if (my_bo
!= that_bo
) ival
= SWAP32 (ival
);
282 if (bytes_parsed
+4 > bytes
) return BadLength
;
283 memcpy (&vlen
, prop
+bytes_parsed
, 4);
285 if (my_bo
!= that_bo
) vlen
= SWAP32 (vlen
);
288 to_cpy
= vlen
> 127 ? 127 : vlen
;
289 memcpy (sval
, prop
+bytes_parsed
, to_cpy
);
292 bytes_parsed
+= vlen
;
293 bytes_parsed
= PAD (bytes_parsed
);
296 case 2: /* RGB value */
297 /* No need to parse this */
298 if (bytes_parsed
+8 > bytes
) return BadLength
;
299 bytes_parsed
+= 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
302 default: /* Parse Error */
309 if (strcmp (name
, "Xft/Antialias") == 0)
311 settings
->seen
|= SEEN_AA
;
312 settings
->aa
= ival
!= 0;
314 else if (strcmp (name
, "Xft/Hinting") == 0)
316 settings
->seen
|= SEEN_HINTING
;
317 settings
->hinting
= ival
!= 0;
319 else if (strcmp (name
, "Xft/HintStyle") == 0)
321 settings
->seen
|= SEEN_HINTSTYLE
;
322 if (strcmp (sval
, "hintnone") == 0)
323 settings
->hintstyle
= FC_HINT_NONE
;
324 else if (strcmp (sval
, "hintslight") == 0)
325 settings
->hintstyle
= FC_HINT_SLIGHT
;
326 else if (strcmp (sval
, "hintmedium") == 0)
327 settings
->hintstyle
= FC_HINT_MEDIUM
;
328 else if (strcmp (sval
, "hintfull") == 0)
329 settings
->hintstyle
= FC_HINT_FULL
;
331 settings
->seen
&= ~SEEN_HINTSTYLE
;
333 else if (strcmp (name
, "Xft/RGBA") == 0)
335 settings
->seen
|= SEEN_RGBA
;
336 if (strcmp (sval
, "none") == 0)
337 settings
->rgba
= FC_RGBA_NONE
;
338 else if (strcmp (sval
, "rgb") == 0)
339 settings
->rgba
= FC_RGBA_RGB
;
340 else if (strcmp (sval
, "bgr") == 0)
341 settings
->rgba
= FC_RGBA_BGR
;
342 else if (strcmp (sval
, "vrgb") == 0)
343 settings
->rgba
= FC_RGBA_VRGB
;
344 else if (strcmp (sval
, "vbgr") == 0)
345 settings
->rgba
= FC_RGBA_VBGR
;
347 settings
->seen
&= ~SEEN_RGBA
;
349 else if (strcmp (name
, "Xft/DPI") == 0)
351 settings
->seen
|= SEEN_DPI
;
352 settings
->dpi
= (double)ival
/1024.0;
354 else if (strcmp (name
, "Xft/lcdfilter") == 0)
356 settings
->seen
|= SEEN_LCDFILTER
;
357 if (strcmp (sval
, "none") == 0)
358 settings
->lcdfilter
= FC_LCD_NONE
;
359 else if (strcmp (sval
, "lcddefault") == 0)
360 settings
->lcdfilter
= FC_LCD_DEFAULT
;
362 settings
->seen
&= ~SEEN_LCDFILTER
;
367 return settings_seen
;
371 read_xft_settings (dpyinfo
, settings
)
372 struct x_display_info
*dpyinfo
;
373 struct xsettings
*settings
;
378 unsigned long nitems
, bytes_after
;
379 unsigned char *prop
= NULL
;
380 Display
*dpy
= dpyinfo
->display
;
383 x_catch_errors (dpy
);
384 rc
= XGetWindowProperty (dpy
,
385 dpyinfo
->xsettings_window
,
386 dpyinfo
->Xatom_xsettings_prop
,
387 0, LONG_MAX
, False
, AnyPropertyType
,
388 &act_type
, &act_form
, &nitems
, &bytes_after
,
391 if (rc
== Success
&& prop
!= NULL
&& act_form
== 8 && nitems
> 0
392 && act_type
== dpyinfo
->Xatom_xsettings_prop
)
393 rc
= parse_xft_settings (prop
, nitems
, settings
);
404 apply_xft_settings (dpyinfo
, send_event_p
)
405 struct x_display_info
*dpyinfo
;
409 struct xsettings settings
, oldsettings
;
413 if (!read_xft_settings (dpyinfo
, &settings
))
416 memset (&oldsettings
, 0, sizeof (oldsettings
));
418 pat
= FcPatternCreate ();
419 XftDefaultSubstitute (dpyinfo
->display
,
420 XScreenNumberOfScreen (dpyinfo
->screen
),
422 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
423 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
424 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
425 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
426 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
427 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
429 if ((settings
.seen
& SEEN_AA
) != 0 && oldsettings
.aa
!= settings
.aa
)
431 FcPatternDel (pat
, FC_ANTIALIAS
);
432 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
434 oldsettings
.aa
= settings
.aa
;
436 sprintf (buf
, "Antialias: %d", oldsettings
.aa
);
438 if ((settings
.seen
& SEEN_HINTING
) != 0
439 && oldsettings
.hinting
!= settings
.hinting
)
441 FcPatternDel (pat
, FC_HINTING
);
442 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
444 oldsettings
.hinting
= settings
.hinting
;
446 if (strlen (buf
) > 0) strcat (buf
, ", ");
447 sprintf (buf
+strlen (buf
), "Hinting: %d", oldsettings
.hinting
);
448 if ((settings
.seen
& SEEN_RGBA
) != 0 && oldsettings
.rgba
!= settings
.rgba
)
450 FcPatternDel (pat
, FC_RGBA
);
451 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
452 oldsettings
.rgba
= settings
.rgba
;
455 if (strlen (buf
) > 0) strcat (buf
, ", ");
456 sprintf (buf
+strlen (buf
), "RGBA: %d", oldsettings
.rgba
);
458 /* Older fontconfig versions don't have FC_LCD_FILTER. */
459 if ((settings
.seen
& SEEN_LCDFILTER
) != 0
460 && oldsettings
.lcdfilter
!= settings
.lcdfilter
)
462 FcPatternDel (pat
, FC_LCD_FILTER
);
463 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
465 oldsettings
.lcdfilter
= settings
.lcdfilter
;
467 if (strlen (buf
) > 0) strcat (buf
, ", ");
468 sprintf (buf
+strlen (buf
), "LCDFilter: %d", oldsettings
.lcdfilter
);
470 if ((settings
.seen
& SEEN_HINTSTYLE
) != 0
471 && oldsettings
.hintstyle
!= settings
.hintstyle
)
473 FcPatternDel (pat
, FC_HINT_STYLE
);
474 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
476 oldsettings
.hintstyle
= settings
.hintstyle
;
478 if (strlen (buf
) > 0) strcat (buf
, ", ");
479 sprintf (buf
+strlen (buf
), "Hintstyle: %d", oldsettings
.hintstyle
);
481 if ((settings
.seen
& SEEN_DPI
) != 0 && oldsettings
.dpi
!= settings
.dpi
484 Lisp_Object frame
, tail
;
486 FcPatternDel (pat
, FC_DPI
);
487 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
489 oldsettings
.dpi
= settings
.dpi
;
491 /* Change the DPI on this display and all frames on the display. */
492 dpyinfo
->resy
= dpyinfo
->resx
= settings
.dpi
;
493 FOR_EACH_FRAME (tail
, frame
)
494 if (FRAME_X_P (XFRAME (frame
))
495 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
496 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
.dpi
;
499 if (strlen (buf
) > 0) strcat (buf
, ", ");
500 sprintf (buf
+strlen (buf
), "DPI: %lf", oldsettings
.dpi
);
504 XftDefaultSet (dpyinfo
->display
, pat
);
506 store_font_changed_event (Qfont_render
,
507 XCAR (dpyinfo
->name_list_element
));
508 Vxft_settings
= make_string (buf
, strlen (buf
));
511 FcPatternDestroy (pat
);
514 #endif /* HAVE_XFT */
517 xft_settings_event (dpyinfo
, event
)
518 struct x_display_info
*dpyinfo
;
522 int check_window_p
= 0;
527 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
532 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
533 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
534 && event
->xclient
.window
== dpyinfo
->root_window
)
539 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
540 && event
->xproperty
.state
== PropertyNewValue
541 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
543 apply_xft_settings (dpyinfo
, True
);
550 dpyinfo
->xsettings_window
= None
;
551 get_prop_window (dpyinfo
);
552 if (dpyinfo
->xsettings_window
!= None
)
553 apply_xft_settings (dpyinfo
, True
);
555 #endif /* HAVE_XFT */
562 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
567 gconf_client
= gconf_client_get_default ();
568 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
571 current_mono_font
= xstrdup (s
);
574 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
575 gconf_client_add_dir (gconf_client
,
577 GCONF_CLIENT_PRELOAD_ONELEVEL
,
579 gconf_client_notify_add (gconf_client
,
583 #endif /* HAVE_GCONF && HAVE_XFT */
587 init_xfd_settings (dpyinfo
)
588 struct x_display_info
*dpyinfo
;
592 Display
*dpy
= dpyinfo
->display
;
596 sprintf (sel
, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo
->screen
));
597 dpyinfo
->Xatom_xsettings_sel
= XInternAtom (dpy
, sel
, False
);
598 dpyinfo
->Xatom_xsettings_prop
= XInternAtom (dpy
,
599 "_XSETTINGS_SETTINGS",
601 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
603 /* Select events so we can detect client messages sent when selection
605 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
607 get_prop_window (dpyinfo
);
608 if (dpyinfo
->xsettings_window
!= None
)
609 apply_xft_settings (dpyinfo
, False
);
613 #else /* ! HAVE_XFT */
615 dpyinfo
->Xatom_xsettings_sel
= None
;
616 dpyinfo
->Xatom_xsettings_prop
= None
;
617 dpyinfo
->Xatom_xsettings_mgr
= None
;
618 dpyinfo
->xsettings_window
= None
;
620 #endif /* ! HAVE_XFT */
624 xsettings_initialize (dpyinfo
)
625 struct x_display_info
*dpyinfo
;
627 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
629 init_xfd_settings (dpyinfo
);
633 xsettings_get_system_font ()
635 return current_mono_font
;
638 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
640 doc
: /* Get the system default monospaced font. */)
643 return current_mono_font
&& use_system_font
644 ? make_string (current_mono_font
, strlen (current_mono_font
))
651 current_mono_font
= NULL
;
652 first_dpyinfo
= NULL
;
657 Qfont_name
= intern_c_string ("font-name");
658 staticpro (&Qfont_name
);
659 Qfont_render
= intern_c_string ("font-render");
660 staticpro (&Qfont_render
);
661 defsubr (&Sfont_get_system_font
);
663 DEFVAR_BOOL ("font-use-system-font", &use_system_font
,
664 doc
: /* *Non-nil means to use the system defined font. */);
667 DEFVAR_LISP ("xft-settings", &Vxft_settings
,
668 doc
: /* Font settings applied to Xft. */);
669 Vxft_settings
= make_string ("", 0);
672 Fprovide (intern_c_string ("font-render-setting"), Qnil
);
674 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
679 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
680 (do not change this comment) */