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
;
372 apply_xft_settings (dpyinfo
, send_event_p
)
373 struct x_display_info
*dpyinfo
;
377 struct xsettings settings
, oldsettings
;
380 if (!read_xft_settings (dpyinfo
, &settings
))
383 memset (&oldsettings
, 0, sizeof (oldsettings
));
385 pat
= FcPatternCreate ();
386 XftDefaultSubstitute (dpyinfo
->display
,
387 XScreenNumberOfScreen (dpyinfo
->screen
),
389 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
390 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
391 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
392 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
393 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
394 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
396 if (oldsettings
.aa
!= settings
.aa
)
398 FcPatternDel (pat
, FC_ANTIALIAS
);
399 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
402 if (oldsettings
.hinting
!= settings
.hinting
)
404 FcPatternDel (pat
, FC_HINTING
);
405 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
408 if (oldsettings
.rgba
!= settings
.rgba
)
410 FcPatternDel (pat
, FC_RGBA
);
411 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
414 /* Older fontconfig versions don't have FC_LCD_FILTER. */
415 if (oldsettings
.lcdfilter
!= settings
.lcdfilter
)
417 FcPatternDel (pat
, FC_LCD_FILTER
);
418 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
421 if (oldsettings
.hintstyle
!= settings
.hintstyle
)
423 FcPatternDel (pat
, FC_HINT_STYLE
);
424 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
427 if (oldsettings
.dpi
!= settings
.dpi
)
429 Lisp_Object frame
, tail
;
431 FcPatternDel (pat
, FC_DPI
);
432 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
435 /* Change the DPI on this display and all frames on the display. */
436 dpyinfo
->resy
= dpyinfo
->resx
= settings
.dpi
;
437 FOR_EACH_FRAME (tail
, frame
)
438 if (FRAME_X_P (XFRAME (frame
))
439 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
440 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
.dpi
;
445 XftDefaultSet (dpyinfo
->display
, pat
);
447 store_font_changed_event (Qfont_render
,
448 XCAR (dpyinfo
->name_list_element
));
451 FcPatternDestroy (pat
);
454 #endif /* HAVE_XFT */
457 xft_settings_event (dpyinfo
, event
)
458 struct x_display_info
*dpyinfo
;
462 int check_window_p
= 0;
467 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
472 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
473 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
474 && event
->xclient
.window
== dpyinfo
->root_window
)
479 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
480 && event
->xproperty
.state
== PropertyNewValue
481 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
483 apply_xft_settings (dpyinfo
, True
);
490 dpyinfo
->xsettings_window
= None
;
491 get_prop_window (dpyinfo
);
492 if (dpyinfo
->xsettings_window
!= None
)
493 apply_xft_settings (dpyinfo
, True
);
495 #endif /* HAVE_XFT */
502 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
507 gconf_client
= gconf_client_get_default ();
508 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
511 current_mono_font
= xstrdup (s
);
514 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
515 gconf_client_add_dir (gconf_client
,
517 GCONF_CLIENT_PRELOAD_ONELEVEL
,
519 gconf_client_notify_add (gconf_client
,
523 #endif /* HAVE_GCONF && HAVE_XFT */
527 init_xfd_settings (dpyinfo
)
528 struct x_display_info
*dpyinfo
;
532 Display
*dpy
= dpyinfo
->display
;
536 sprintf (sel
, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo
->screen
));
537 dpyinfo
->Xatom_xsettings_sel
= XInternAtom (dpy
, sel
, False
);
538 dpyinfo
->Xatom_xsettings_prop
= XInternAtom (dpy
,
539 "_XSETTINGS_SETTINGS",
541 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
543 /* Select events so we can detect client messages sent when selection
545 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
547 get_prop_window (dpyinfo
);
548 if (dpyinfo
->xsettings_window
!= None
)
549 apply_xft_settings (dpyinfo
, False
);
553 #else /* ! HAVE_XFT */
555 dpyinfo
->Xatom_xsettings_sel
= None
;
556 dpyinfo
->Xatom_xsettings_prop
= None
;
557 dpyinfo
->Xatom_xsettings_mgr
= None
;
558 dpyinfo
->xsettings_window
= None
;
560 #endif /* ! HAVE_XFT */
564 xsettings_initialize (dpyinfo
)
565 struct x_display_info
*dpyinfo
;
567 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
569 init_xfd_settings (dpyinfo
);
573 xsettings_get_system_font ()
575 return current_mono_font
;
578 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
580 doc
: /* Get the system default monospaced font. */)
583 return current_mono_font
&& use_system_font
584 ? make_string (current_mono_font
, strlen (current_mono_font
))
591 current_mono_font
= NULL
;
592 first_dpyinfo
= NULL
;
597 Qfont_name
= intern_c_string ("font-name");
598 staticpro (&Qfont_name
);
599 Qfont_render
= intern_c_string ("font-render");
600 staticpro (&Qfont_render
);
601 defsubr (&Sfont_get_system_font
);
603 DEFVAR_BOOL ("font-use-system-font", &use_system_font
,
604 doc
: /* *Non-nil means to use the system defined font. */);
608 Fprovide (intern_c_string ("font-render-setting"), Qnil
);
610 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
615 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
616 (do not change this comment) */