1 /* Functions for handle font changes dynamically.
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"
27 #include "blockinput.h"
28 #include "termhooks.h"
31 #include <X11/Xproto.h>
34 #include <gconf/gconf-client.h>
37 #include <X11/Xft/Xft.h>
40 static char *current_mono_font
;
41 static struct x_display_info
*first_dpyinfo
;
42 static Lisp_Object Qfont_name
, Qfont_render
;
43 static int use_system_font
;
46 static GConfClient
*gconf_client
;
51 store_font_changed_event (arg
, display_name
)
53 Lisp_Object display_name
;
55 struct input_event event
;
57 event
.kind
= CONFIG_CHANGED_EVENT
;
58 event
.frame_or_window
= display_name
;
60 kbd_buffer_store_event (&event
);
65 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
67 /* Callback called when something changed in GConf that we care about,
68 that is SYSTEM_MONO_FONT. */
71 something_changedCB (client
, cnxn_id
, entry
, user_data
)
77 GConfValue
*v
= gconf_entry_get_value (entry
);
80 if (v
->type
== GCONF_VALUE_STRING
)
82 const char *value
= gconf_value_get_string (v
);
84 if (current_mono_font
!= NULL
&& strcmp (value
, current_mono_font
) == 0)
85 return; /* No change. */
87 xfree (current_mono_font
);
88 current_mono_font
= xstrdup (value
);
92 if (first_dpyinfo
!= NULL
)
94 /* Check if display still open */
95 struct x_display_info
*dpyinfo
;
97 for (dpyinfo
= x_display_list
; !found
&& dpyinfo
; dpyinfo
= dpyinfo
->next
)
98 found
= dpyinfo
== first_dpyinfo
;
100 if (found
&& use_system_font
)
101 store_font_changed_event (Qfont_name
,
102 XCAR (first_dpyinfo
->name_list_element
));
105 #endif /* HAVE_GCONF */
109 /* Older fontconfig versions don't have FC_LCD_*. */
111 #define FC_LCD_NONE 0
113 #ifndef FC_LCD_DEFAULT
114 #define FC_LCD_DEFAULT 1
116 #ifndef FC_LCD_FILTER
117 #define FC_LCD_FILTER "lcdfilter"
120 /* Find the window that contains the XSETTINGS property values. */
123 get_prop_window (dpyinfo
)
124 struct x_display_info
*dpyinfo
;
126 Display
*dpy
= dpyinfo
->display
;
129 dpyinfo
->xsettings_window
= XGetSelectionOwner (dpy
,
130 dpyinfo
->Xatom_xsettings_sel
);
131 if (dpyinfo
->xsettings_window
!= None
)
132 /* Select events so we can detect if window is deleted or if settings
134 XSelectInput (dpy
, dpyinfo
->xsettings_window
,
135 PropertyChangeMask
|StructureNotifyMask
);
143 int rgba
, lcdfilter
, hintstyle
;
147 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
148 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
149 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
150 #define PAD(nr) (((nr) + 3) & ~3)
152 /* Parse xsettings and extract those that deal with Xft.
153 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
154 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
156 Layout of prop. First is a header:
159 ------------------------------------
165 Then N_SETTINGS records, with header:
168 ------------------------------------
169 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
171 2 CARD16 n == name-length
173 p unused, p=pad_to_even_4(n)
174 4 CARD32 last-change-serial
176 and then the value, For string:
179 ------------------------------------
180 4 CARD32 n = value-length
182 p unused, p=pad_to_even_4(n)
187 ------------------------------------
193 ------------------------------------
202 parse_xft_settings (prop
, bytes
, settings
)
205 struct xsettings
*settings
;
207 Lisp_Object byteorder
= Fbyteorder ();
208 int my_bo
= XFASTINT (byteorder
) == 'B' ? MSBFirst
: LSBFirst
;
209 int that_bo
= prop
[0];
211 int bytes_parsed
= 0;
212 int settings_seen
= 0;
215 /* First 4 bytes is a serial number, skip that. */
217 if (bytes
< 12) return BadLength
;
218 memcpy (&n_settings
, prop
+8, 4);
219 if (my_bo
!= that_bo
) n_settings
= SWAP32 (n_settings
);
222 memset (settings
, 0, sizeof (*settings
));
224 while (bytes_parsed
+4 < bytes
&& settings_seen
< 6
227 int type
= prop
[bytes_parsed
++];
229 CARD32 vlen
, ival
= 0;
230 char name
[128]; /* The names we are looking for are not this long. */
231 char sval
[128]; /* The values we are looking for are not this long. */
237 ++bytes_parsed
; /* Padding */
239 memcpy (&nlen
, prop
+bytes_parsed
, 2);
241 if (my_bo
!= that_bo
) nlen
= SWAP16 (nlen
);
242 if (bytes_parsed
+nlen
> bytes
) return BadLength
;
243 to_cpy
= nlen
> 127 ? 127 : nlen
;
244 memcpy (name
, prop
+bytes_parsed
, to_cpy
);
247 bytes_parsed
+= nlen
;
248 bytes_parsed
= PAD (bytes_parsed
);
250 bytes_parsed
+= 4; /* Skip serial for this value */
251 if (bytes_parsed
> bytes
) return BadLength
;
253 is_xft
= nlen
> 6 && strncmp (name
, "Xft/", 4) == 0;
257 case 0: /* Integer */
258 if (bytes_parsed
+4 > bytes
) return BadLength
;
261 memcpy (&ival
, prop
+bytes_parsed
, 4);
262 if (my_bo
!= that_bo
) ival
= SWAP32 (ival
);
268 if (bytes_parsed
+4 > bytes
) return BadLength
;
269 memcpy (&vlen
, prop
+bytes_parsed
, 4);
271 if (my_bo
!= that_bo
) vlen
= SWAP32 (vlen
);
274 to_cpy
= vlen
> 127 ? 127 : vlen
;
275 memcpy (sval
, prop
+bytes_parsed
, to_cpy
);
278 bytes_parsed
+= vlen
;
279 bytes_parsed
= PAD (bytes_parsed
);
282 case 2: /* RGB value */
283 /* No need to parse this */
284 if (bytes_parsed
+8 > bytes
) return BadLength
;
285 bytes_parsed
+= 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
288 default: /* Parse Error */
295 if (strcmp (name
, "Xft/Antialias") == 0)
296 settings
->aa
= ival
!= 0;
297 else if (strcmp (name
, "Xft/Hinting") == 0)
298 settings
->hinting
= ival
!= 0;
299 else if (strcmp (name
, "Xft/HintStyle") == 0)
301 if (strcmp (sval
, "hintnone") == 0)
302 settings
->hintstyle
= FC_HINT_NONE
;
303 else if (strcmp (sval
, "hintslight") == 0)
304 settings
->hintstyle
= FC_HINT_SLIGHT
;
305 else if (strcmp (sval
, "hintmedium") == 0)
306 settings
->hintstyle
= FC_HINT_MEDIUM
;
307 else if (strcmp (sval
, "hintfull") == 0)
308 settings
->hintstyle
= FC_HINT_FULL
;
310 else if (strcmp (name
, "Xft/RGBA") == 0)
312 if (strcmp (sval
, "none") == 0)
313 settings
->rgba
= FC_RGBA_NONE
;
314 else if (strcmp (sval
, "rgb") == 0)
315 settings
->rgba
= FC_RGBA_RGB
;
316 else if (strcmp (sval
, "bgr") == 0)
317 settings
->rgba
= FC_RGBA_BGR
;
318 else if (strcmp (sval
, "vrgb") == 0)
319 settings
->rgba
= FC_RGBA_VRGB
;
320 else if (strcmp (sval
, "vbgr") == 0)
321 settings
->rgba
= FC_RGBA_VBGR
;
323 else if (strcmp (name
, "Xft/DPI") == 0)
324 settings
->dpi
= (double)ival
/1024.0;
325 else if (strcmp (name
, "Xft/lcdfilter") == 0)
327 if (strcmp (sval
, "none") == 0)
328 settings
->lcdfilter
= FC_LCD_NONE
;
329 else if (strcmp (sval
, "lcddefault") == 0)
330 settings
->lcdfilter
= FC_LCD_DEFAULT
;
339 read_xft_settings (dpyinfo
, settings
)
340 struct x_display_info
*dpyinfo
;
341 struct xsettings
*settings
;
346 unsigned long nitems
, bytes_after
;
347 unsigned char *prop
= NULL
;
348 Display
*dpy
= dpyinfo
->display
;
351 x_catch_errors (dpy
);
352 rc
= XGetWindowProperty (dpy
,
353 dpyinfo
->xsettings_window
,
354 dpyinfo
->Xatom_xsettings_prop
,
355 0, LONG_MAX
, False
, AnyPropertyType
,
356 &act_type
, &act_form
, &nitems
, &bytes_after
,
359 if (rc
== Success
&& prop
!= NULL
&& act_form
== 8 && nitems
> 0
360 && act_type
== dpyinfo
->Xatom_xsettings_prop
)
361 rc
= parse_xft_settings (prop
, nitems
, settings
);
367 return rc
== Success
;
371 set_default_xft_settings (dpyinfo
)
372 struct x_display_info
*dpyinfo
;
375 pat
= FcPatternCreate ();
376 XftDefaultSubstitute (dpyinfo
->display
,
377 XScreenNumberOfScreen (dpyinfo
->screen
),
380 FcPatternDel (pat
, FC_ANTIALIAS
);
381 FcPatternAddBool (pat
, FC_ANTIALIAS
, FcTrue
);
382 FcPatternDel (pat
, FC_HINTING
);
383 FcPatternAddBool (pat
, FC_HINTING
, FcTrue
);
384 FcPatternDel (pat
, FC_HINT_STYLE
);
385 FcPatternAddInteger (pat
, FC_HINT_STYLE
,FC_HINT_MEDIUM
);
386 XftDefaultSet (dpyinfo
->display
, pat
);
390 apply_xft_settings (dpyinfo
, send_event_p
)
391 struct x_display_info
*dpyinfo
;
395 struct xsettings settings
, oldsettings
;
398 if (!read_xft_settings (dpyinfo
, &settings
))
401 memset (&oldsettings
, 0, sizeof (oldsettings
));
403 pat
= FcPatternCreate ();
404 XftDefaultSubstitute (dpyinfo
->display
,
405 XScreenNumberOfScreen (dpyinfo
->screen
),
407 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
408 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
409 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
410 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
411 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
412 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
414 if (oldsettings
.aa
!= settings
.aa
)
416 FcPatternDel (pat
, FC_ANTIALIAS
);
417 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
420 if (oldsettings
.hinting
!= settings
.hinting
)
422 FcPatternDel (pat
, FC_HINTING
);
423 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
426 if (oldsettings
.rgba
!= settings
.rgba
)
428 FcPatternDel (pat
, FC_RGBA
);
429 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
432 /* Older fontconfig versions don't have FC_LCD_FILTER. */
433 if (oldsettings
.lcdfilter
!= settings
.lcdfilter
)
435 FcPatternDel (pat
, FC_LCD_FILTER
);
436 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
439 if (oldsettings
.hintstyle
!= settings
.hintstyle
)
441 FcPatternDel (pat
, FC_HINT_STYLE
);
442 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
445 if (oldsettings
.dpi
!= settings
.dpi
)
447 Lisp_Object frame
, tail
;
449 FcPatternDel (pat
, FC_DPI
);
450 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
453 /* Change the DPI on this display and all frames on the display. */
454 dpyinfo
->resy
= dpyinfo
->resx
= settings
.dpi
;
455 FOR_EACH_FRAME (tail
, frame
)
456 if (FRAME_X_P (XFRAME (frame
))
457 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
458 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
.dpi
;
463 XftDefaultSet (dpyinfo
->display
, pat
);
465 store_font_changed_event (Qfont_render
,
466 XCAR (dpyinfo
->name_list_element
));
469 FcPatternDestroy (pat
);
472 #endif /* HAVE_XFT */
475 xft_settings_event (dpyinfo
, event
)
476 struct x_display_info
*dpyinfo
;
480 int check_window_p
= 0;
485 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
490 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
491 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
492 && event
->xclient
.window
== dpyinfo
->root_window
)
497 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
498 && event
->xproperty
.state
== PropertyNewValue
499 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
501 apply_xft_settings (dpyinfo
, True
);
508 dpyinfo
->xsettings_window
= None
;
509 get_prop_window (dpyinfo
);
510 if (dpyinfo
->xsettings_window
!= None
)
511 apply_xft_settings (dpyinfo
, True
);
513 #endif /* HAVE_XFT */
520 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
525 gconf_client
= gconf_client_get_default ();
526 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
529 current_mono_font
= xstrdup (s
);
532 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
533 gconf_client_add_dir (gconf_client
,
535 GCONF_CLIENT_PRELOAD_ONELEVEL
,
537 gconf_client_notify_add (gconf_client
,
541 #endif /* HAVE_GCONF && HAVE_XFT */
545 init_xfd_settings (dpyinfo
)
546 struct x_display_info
*dpyinfo
;
550 Display
*dpy
= dpyinfo
->display
;
554 sprintf (sel
, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo
->screen
));
555 dpyinfo
->Xatom_xsettings_sel
= XInternAtom (dpy
, sel
, False
);
556 dpyinfo
->Xatom_xsettings_prop
= XInternAtom (dpy
,
557 "_XSETTINGS_SETTINGS",
559 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
561 /* Select events so we can detect client messages sent when selection
563 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
565 get_prop_window (dpyinfo
);
566 if (dpyinfo
->xsettings_window
!= None
)
567 apply_xft_settings (dpyinfo
, False
);
569 set_default_xft_settings (dpyinfo
);
573 #else /* ! HAVE_XFT */
575 dpyinfo
->Xatom_xsettings_sel
= None
;
576 dpyinfo
->Xatom_xsettings_prop
= None
;
577 dpyinfo
->Xatom_xsettings_mgr
= None
;
578 dpyinfo
->xsettings_window
= None
;
580 #endif /* ! HAVE_XFT */
584 xsettings_initialize (dpyinfo
)
585 struct x_display_info
*dpyinfo
;
587 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
589 init_xfd_settings (dpyinfo
);
593 xsettings_get_system_font ()
595 return current_mono_font
;
598 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
600 doc
: /* Get the system default monospaced font. */)
603 return current_mono_font
&& use_system_font
604 ? make_string (current_mono_font
, strlen (current_mono_font
))
611 current_mono_font
= NULL
;
612 first_dpyinfo
= NULL
;
617 Qfont_name
= intern_c_string ("font-name");
618 staticpro (&Qfont_name
);
619 Qfont_render
= intern_c_string ("font-render");
620 staticpro (&Qfont_render
);
621 defsubr (&Sfont_get_system_font
);
623 DEFVAR_BOOL ("font-use-system-font", &use_system_font
,
624 doc
: /* *Non-nil means to use the system defined font. */);
628 Fprovide (intern_c_string ("font-render-setting"), Qnil
);
630 Fprovide (intern_c_string ("system-font-setting"), Qnil
);