1 /* Functions for handling font and other changes dynamically.
3 Copyright (C) 2009-2011 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/>. */
26 #include "xsettings.h"
29 #include "blockinput.h"
30 #include "termhooks.h"
33 #include <X11/Xproto.h>
36 #include <gconf/gconf-client.h>
39 #include <X11/Xft/Xft.h>
42 static char *current_mono_font
;
43 static char *current_font
;
44 static struct x_display_info
*first_dpyinfo
;
45 static Lisp_Object Qmonospace_font_name
, Qfont_name
, Qfont_render
,
47 static Lisp_Object current_tool_bar_style
;
50 static GConfClient
*gconf_client
;
55 store_config_changed_event (Lisp_Object arg
, Lisp_Object display_name
)
57 struct input_event event
;
59 event
.kind
= CONFIG_CHANGED_EVENT
;
60 event
.frame_or_window
= display_name
;
62 kbd_buffer_store_event (&event
);
65 #define XSETTINGS_FONT_NAME "Gtk/FontName"
66 #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle"
72 SEEN_LCDFILTER
= 0x08,
73 SEEN_HINTSTYLE
= 0x10,
82 int rgba
, lcdfilter
, hintstyle
;
94 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
95 #define SYSTEM_FONT "/desktop/gnome/interface/font_name"
97 /* Callback called when something changed in GConf that we care about,
98 that is SYSTEM_MONO_FONT. */
101 something_changedCB (GConfClient
*client
,
106 GConfValue
*v
= gconf_entry_get_value (entry
);
109 if (v
->type
== GCONF_VALUE_STRING
)
111 const char *value
= gconf_value_get_string (v
);
112 if (current_mono_font
!= NULL
&& strcmp (value
, current_mono_font
) == 0)
113 return; /* No change. */
115 xfree (current_mono_font
);
116 current_mono_font
= xstrdup (value
);
120 if (first_dpyinfo
!= NULL
)
122 /* Check if display still open */
123 struct x_display_info
*dpyinfo
;
125 for (dpyinfo
= x_display_list
; !found
&& dpyinfo
; dpyinfo
= dpyinfo
->next
)
126 found
= dpyinfo
== first_dpyinfo
;
128 if (found
&& use_system_font
)
129 store_config_changed_event (Qmonospace_font_name
,
130 XCAR (first_dpyinfo
->name_list_element
));
133 #endif /* HAVE_GCONF */
137 /* Older fontconfig versions don't have FC_LCD_*. */
139 #define FC_LCD_NONE 0
141 #ifndef FC_LCD_DEFAULT
142 #define FC_LCD_DEFAULT 1
144 #ifndef FC_LCD_FILTER
145 #define FC_LCD_FILTER "lcdfilter"
148 #endif /* HAVE_XFT */
150 /* Find the window that contains the XSETTINGS property values. */
153 get_prop_window (struct x_display_info
*dpyinfo
)
155 Display
*dpy
= dpyinfo
->display
;
158 dpyinfo
->xsettings_window
= XGetSelectionOwner (dpy
,
159 dpyinfo
->Xatom_xsettings_sel
);
160 if (dpyinfo
->xsettings_window
!= None
)
161 /* Select events so we can detect if window is deleted or if settings
163 XSelectInput (dpy
, dpyinfo
->xsettings_window
,
164 PropertyChangeMask
|StructureNotifyMask
);
169 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
170 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
171 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
172 #define PAD(nr) (((nr) + 3) & ~3)
174 /* Parse xsettings and extract those that deal with Xft.
175 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
176 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
178 Layout of prop. First is a header:
181 ------------------------------------
187 Then N_SETTINGS records, with header:
190 ------------------------------------
191 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
193 2 CARD16 n == name-length
195 p unused, p=pad_to_even_4(n)
196 4 CARD32 last-change-serial
198 and then the value, For string:
201 ------------------------------------
202 4 CARD32 n = value-length
204 p unused, p=pad_to_even_4(n)
209 ------------------------------------
215 ------------------------------------
221 Returns non-zero if some Xft settings was seen, zero otherwise.
225 parse_settings (unsigned char *prop
,
226 long unsigned int bytes
,
227 struct xsettings
*settings
)
229 Lisp_Object byteorder
= Fbyteorder ();
230 int my_bo
= XFASTINT (byteorder
) == 'B' ? MSBFirst
: LSBFirst
;
231 int that_bo
= prop
[0];
233 int bytes_parsed
= 0;
234 int settings_seen
= 0;
237 /* First 4 bytes is a serial number, skip that. */
239 if (bytes
< 12) return BadLength
;
240 memcpy (&n_settings
, prop
+8, 4);
241 if (my_bo
!= that_bo
) n_settings
= SWAP32 (n_settings
);
244 memset (settings
, 0, sizeof (*settings
));
246 while (bytes_parsed
+4 < bytes
&& settings_seen
< 7
249 int type
= prop
[bytes_parsed
++];
251 CARD32 vlen
, ival
= 0;
252 char name
[128]; /* The names we are looking for are not this long. */
253 char sval
[128]; /* The values we are looking for are not this long. */
259 ++bytes_parsed
; /* Padding */
261 memcpy (&nlen
, prop
+bytes_parsed
, 2);
263 if (my_bo
!= that_bo
) nlen
= SWAP16 (nlen
);
264 if (bytes_parsed
+nlen
> bytes
) return BadLength
;
265 to_cpy
= nlen
> 127 ? 127 : nlen
;
266 memcpy (name
, prop
+bytes_parsed
, to_cpy
);
269 bytes_parsed
+= nlen
;
270 bytes_parsed
= PAD (bytes_parsed
);
272 bytes_parsed
+= 4; /* Skip serial for this value */
273 if (bytes_parsed
> bytes
) return BadLength
;
277 (nlen
> 6 && strncmp (name
, "Xft/", 4) == 0)
280 (strcmp (XSETTINGS_FONT_NAME
, name
) == 0)
281 || (strcmp (XSETTINGS_TOOL_BAR_STYLE
, name
) == 0);
285 case 0: /* Integer */
286 if (bytes_parsed
+4 > bytes
) return BadLength
;
289 memcpy (&ival
, prop
+bytes_parsed
, 4);
290 if (my_bo
!= that_bo
) ival
= SWAP32 (ival
);
296 if (bytes_parsed
+4 > bytes
) return BadLength
;
297 memcpy (&vlen
, prop
+bytes_parsed
, 4);
299 if (my_bo
!= that_bo
) vlen
= SWAP32 (vlen
);
302 to_cpy
= vlen
> 127 ? 127 : vlen
;
303 memcpy (sval
, prop
+bytes_parsed
, to_cpy
);
306 bytes_parsed
+= vlen
;
307 bytes_parsed
= PAD (bytes_parsed
);
310 case 2: /* RGB value */
311 /* No need to parse this */
312 if (bytes_parsed
+8 > bytes
) return BadLength
;
313 bytes_parsed
+= 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
316 default: /* Parse Error */
323 if (strcmp (name
, XSETTINGS_FONT_NAME
) == 0)
325 settings
->font
= xstrdup (sval
);
326 settings
->seen
|= SEEN_FONT
;
328 else if (strcmp (name
, XSETTINGS_TOOL_BAR_STYLE
) == 0)
330 settings
->tb_style
= xstrdup (sval
);
331 settings
->seen
|= SEEN_TB_STYLE
;
334 else if (strcmp (name
, "Xft/Antialias") == 0)
336 settings
->seen
|= SEEN_AA
;
337 settings
->aa
= ival
!= 0;
339 else if (strcmp (name
, "Xft/Hinting") == 0)
341 settings
->seen
|= SEEN_HINTING
;
342 settings
->hinting
= ival
!= 0;
344 # ifdef FC_HINT_STYLE
345 else if (strcmp (name
, "Xft/HintStyle") == 0)
347 settings
->seen
|= SEEN_HINTSTYLE
;
348 if (strcmp (sval
, "hintnone") == 0)
349 settings
->hintstyle
= FC_HINT_NONE
;
350 else if (strcmp (sval
, "hintslight") == 0)
351 settings
->hintstyle
= FC_HINT_SLIGHT
;
352 else if (strcmp (sval
, "hintmedium") == 0)
353 settings
->hintstyle
= FC_HINT_MEDIUM
;
354 else if (strcmp (sval
, "hintfull") == 0)
355 settings
->hintstyle
= FC_HINT_FULL
;
357 settings
->seen
&= ~SEEN_HINTSTYLE
;
360 else if (strcmp (name
, "Xft/RGBA") == 0)
362 settings
->seen
|= SEEN_RGBA
;
363 if (strcmp (sval
, "none") == 0)
364 settings
->rgba
= FC_RGBA_NONE
;
365 else if (strcmp (sval
, "rgb") == 0)
366 settings
->rgba
= FC_RGBA_RGB
;
367 else if (strcmp (sval
, "bgr") == 0)
368 settings
->rgba
= FC_RGBA_BGR
;
369 else if (strcmp (sval
, "vrgb") == 0)
370 settings
->rgba
= FC_RGBA_VRGB
;
371 else if (strcmp (sval
, "vbgr") == 0)
372 settings
->rgba
= FC_RGBA_VBGR
;
374 settings
->seen
&= ~SEEN_RGBA
;
376 else if (strcmp (name
, "Xft/DPI") == 0)
378 settings
->seen
|= SEEN_DPI
;
379 settings
->dpi
= (double)ival
/1024.0;
381 else if (strcmp (name
, "Xft/lcdfilter") == 0)
383 settings
->seen
|= SEEN_LCDFILTER
;
384 if (strcmp (sval
, "none") == 0)
385 settings
->lcdfilter
= FC_LCD_NONE
;
386 else if (strcmp (sval
, "lcddefault") == 0)
387 settings
->lcdfilter
= FC_LCD_DEFAULT
;
389 settings
->seen
&= ~SEEN_LCDFILTER
;
391 #endif /* HAVE_XFT */
395 return settings_seen
;
399 read_settings (struct x_display_info
*dpyinfo
, struct xsettings
*settings
)
403 unsigned long nitems
, bytes_after
;
404 unsigned char *prop
= NULL
;
405 Display
*dpy
= dpyinfo
->display
;
408 x_catch_errors (dpy
);
409 rc
= XGetWindowProperty (dpy
,
410 dpyinfo
->xsettings_window
,
411 dpyinfo
->Xatom_xsettings_prop
,
412 0, LONG_MAX
, False
, AnyPropertyType
,
413 &act_type
, &act_form
, &nitems
, &bytes_after
,
416 if (rc
== Success
&& prop
!= NULL
&& act_form
== 8 && nitems
> 0
417 && act_type
== dpyinfo
->Xatom_xsettings_prop
)
418 rc
= parse_settings (prop
, nitems
, settings
);
429 apply_xft_settings (struct x_display_info
*dpyinfo
,
431 struct xsettings
*settings
)
435 struct xsettings oldsettings
;
439 memset (&oldsettings
, 0, sizeof (oldsettings
));
441 pat
= FcPatternCreate ();
442 XftDefaultSubstitute (dpyinfo
->display
,
443 XScreenNumberOfScreen (dpyinfo
->screen
),
445 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
446 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
447 # ifdef FC_HINT_STYLE
448 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
450 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
451 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
452 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
454 if ((settings
->seen
& SEEN_AA
) != 0 && oldsettings
.aa
!= settings
->aa
)
456 FcPatternDel (pat
, FC_ANTIALIAS
);
457 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
->aa
);
459 oldsettings
.aa
= settings
->aa
;
461 sprintf (buf
, "Antialias: %d", oldsettings
.aa
);
463 if ((settings
->seen
& SEEN_HINTING
) != 0
464 && oldsettings
.hinting
!= settings
->hinting
)
466 FcPatternDel (pat
, FC_HINTING
);
467 FcPatternAddBool (pat
, FC_HINTING
, settings
->hinting
);
469 oldsettings
.hinting
= settings
->hinting
;
471 if (strlen (buf
) > 0) strcat (buf
, ", ");
472 sprintf (buf
+strlen (buf
), "Hinting: %d", oldsettings
.hinting
);
473 if ((settings
->seen
& SEEN_RGBA
) != 0 && oldsettings
.rgba
!= settings
->rgba
)
475 FcPatternDel (pat
, FC_RGBA
);
476 FcPatternAddInteger (pat
, FC_RGBA
, settings
->rgba
);
477 oldsettings
.rgba
= settings
->rgba
;
480 if (strlen (buf
) > 0) strcat (buf
, ", ");
481 sprintf (buf
+strlen (buf
), "RGBA: %d", oldsettings
.rgba
);
483 /* Older fontconfig versions don't have FC_LCD_FILTER. */
484 if ((settings
->seen
& SEEN_LCDFILTER
) != 0
485 && oldsettings
.lcdfilter
!= settings
->lcdfilter
)
487 FcPatternDel (pat
, FC_LCD_FILTER
);
488 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
->lcdfilter
);
490 oldsettings
.lcdfilter
= settings
->lcdfilter
;
492 if (strlen (buf
) > 0) strcat (buf
, ", ");
493 sprintf (buf
+strlen (buf
), "LCDFilter: %d", oldsettings
.lcdfilter
);
495 # ifdef FC_HINT_STYLE
496 if ((settings
->seen
& SEEN_HINTSTYLE
) != 0
497 && oldsettings
.hintstyle
!= settings
->hintstyle
)
499 FcPatternDel (pat
, FC_HINT_STYLE
);
500 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
->hintstyle
);
502 oldsettings
.hintstyle
= settings
->hintstyle
;
505 if (strlen (buf
) > 0) strcat (buf
, ", ");
506 sprintf (buf
+strlen (buf
), "Hintstyle: %d", oldsettings
.hintstyle
);
508 if ((settings
->seen
& SEEN_DPI
) != 0 && oldsettings
.dpi
!= settings
->dpi
509 && settings
->dpi
> 0)
511 Lisp_Object frame
, tail
;
513 FcPatternDel (pat
, FC_DPI
);
514 FcPatternAddDouble (pat
, FC_DPI
, settings
->dpi
);
516 oldsettings
.dpi
= settings
->dpi
;
518 /* Change the DPI on this display and all frames on the display. */
519 dpyinfo
->resy
= dpyinfo
->resx
= settings
->dpi
;
520 FOR_EACH_FRAME (tail
, frame
)
521 if (FRAME_X_P (XFRAME (frame
))
522 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
523 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
->dpi
;
526 if (strlen (buf
) > 0) strcat (buf
, ", ");
527 sprintf (buf
+strlen (buf
), "DPI: %lf", oldsettings
.dpi
);
531 XftDefaultSet (dpyinfo
->display
, pat
);
533 store_config_changed_event (Qfont_render
,
534 XCAR (dpyinfo
->name_list_element
));
535 Vxft_settings
= make_string (buf
, strlen (buf
));
538 FcPatternDestroy (pat
);
539 #endif /* HAVE_XFT */
543 read_and_apply_settings (struct x_display_info
*dpyinfo
, int send_event_p
)
545 struct xsettings settings
;
546 Lisp_Object dpyname
= XCAR (dpyinfo
->name_list_element
);
548 if (!read_settings (dpyinfo
, &settings
))
551 apply_xft_settings (dpyinfo
, True
, &settings
);
552 if (settings
.seen
& SEEN_TB_STYLE
)
554 Lisp_Object style
= Qnil
;
555 if (strcmp (settings
.tb_style
, "both") == 0)
557 else if (strcmp (settings
.tb_style
, "both-horiz") == 0)
559 else if (strcmp (settings
.tb_style
, "icons") == 0)
561 else if (strcmp (settings
.tb_style
, "text") == 0)
563 if (!NILP (style
) && !EQ (style
, current_tool_bar_style
))
565 current_tool_bar_style
= style
;
567 store_config_changed_event (Qtool_bar_style
, dpyname
);
569 free (settings
.tb_style
);
572 if (settings
.seen
& SEEN_FONT
)
574 if (!current_font
|| strcmp (current_font
, settings
.font
) != 0)
577 current_font
= settings
.font
;
579 store_config_changed_event (Qfont_name
, dpyname
);
582 free (settings
.font
);
587 xft_settings_event (struct x_display_info
*dpyinfo
, XEvent
*event
)
589 int check_window_p
= 0;
590 int apply_settings
= 0;
595 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
600 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
601 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
602 && event
->xclient
.window
== dpyinfo
->root_window
)
607 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
608 && event
->xproperty
.state
== PropertyNewValue
609 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
617 dpyinfo
->xsettings_window
= None
;
618 get_prop_window (dpyinfo
);
619 if (dpyinfo
->xsettings_window
!= None
)
624 read_and_apply_settings (dpyinfo
, True
);
631 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
634 #ifdef HAVE_G_TYPE_INIT
637 gconf_client
= gconf_client_get_default ();
638 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
641 current_mono_font
= xstrdup (s
);
644 s
= gconf_client_get_string (gconf_client
, SYSTEM_FONT
, NULL
);
647 current_font
= xstrdup (s
);
650 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
651 gconf_client_add_dir (gconf_client
,
653 GCONF_CLIENT_PRELOAD_ONELEVEL
,
655 gconf_client_notify_add (gconf_client
,
659 #endif /* HAVE_GCONF && HAVE_XFT */
663 init_xsettings (struct x_display_info
*dpyinfo
)
665 Display
*dpy
= dpyinfo
->display
;
669 /* Select events so we can detect client messages sent when selection
671 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
673 get_prop_window (dpyinfo
);
674 if (dpyinfo
->xsettings_window
!= None
)
675 read_and_apply_settings (dpyinfo
, False
);
681 xsettings_initialize (struct x_display_info
*dpyinfo
)
683 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
685 init_xsettings (dpyinfo
);
689 xsettings_get_system_font (void)
691 return current_mono_font
;
695 xsettings_get_system_normal_font (void)
700 DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font
,
701 Sfont_get_system_normal_font
,
703 doc
: /* Get the system default application font. */)
707 ? make_string (current_font
, strlen (current_font
))
711 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
713 doc
: /* Get the system default fixed width font. */)
716 return current_mono_font
717 ? make_string (current_mono_font
, strlen (current_mono_font
))
721 DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style
, Stool_bar_get_system_style
,
723 doc
: /* Get the system tool bar style.
724 If no system tool bar style is known, return `tool-bar-style' if set to a
725 known style. Otherwise return image. */)
728 if (EQ (Vtool_bar_style
, Qimage
)
729 || EQ (Vtool_bar_style
, Qtext
)
730 || EQ (Vtool_bar_style
, Qboth
)
731 || EQ (Vtool_bar_style
, Qboth_horiz
)
732 || EQ (Vtool_bar_style
, Qtext_image_horiz
))
733 return Vtool_bar_style
;
734 if (!NILP (current_tool_bar_style
))
735 return current_tool_bar_style
;
740 syms_of_xsettings (void)
742 current_mono_font
= NULL
;
744 first_dpyinfo
= NULL
;
749 Qmonospace_font_name
= intern_c_string ("monospace-font-name");
750 staticpro (&Qmonospace_font_name
);
751 Qfont_name
= intern_c_string ("font-name");
752 staticpro (&Qfont_name
);
753 Qfont_render
= intern_c_string ("font-render");
754 staticpro (&Qfont_render
);
755 defsubr (&Sfont_get_system_font
);
756 defsubr (&Sfont_get_system_normal_font
);
758 DEFVAR_BOOL ("font-use-system-font", use_system_font
,
759 doc
: /* *Non-nil means to apply the system defined font dynamically.
760 When this is non-nil and the system defined fixed width font changes, we
761 update frames dynamically.
762 If this variable is nil, Emacs ignores system font changes. */);
765 DEFVAR_LISP ("xft-settings", Vxft_settings
,
766 doc
: /* Font settings applied to Xft. */);
767 Vxft_settings
= make_string ("", 0);
770 Fprovide (intern_c_string ("font-render-setting"), Qnil
);
772 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
776 current_tool_bar_style
= Qnil
;
777 Qtool_bar_style
= intern_c_string ("tool-bar-style");
778 staticpro (&Qtool_bar_style
);
779 defsubr (&Stool_bar_get_system_style
);
781 Fprovide (intern_c_string ("dynamic-setting"), Qnil
);