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"
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
);
144 int rgba
, lcdfilter
, hintstyle
;
148 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
149 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
150 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
151 #define PAD(nr) (((nr) + 3) & ~3)
153 /* Parse xsettings and extract those that deal with Xft.
154 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
155 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
157 Layout of prop. First is a header:
160 ------------------------------------
166 Then N_SETTINGS records, with header:
169 ------------------------------------
170 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
172 2 CARD16 n == name-length
174 p unused, p=pad_to_even_4(n)
175 4 CARD32 last-change-serial
177 and then the value, For string:
180 ------------------------------------
181 4 CARD32 n = value-length
183 p unused, p=pad_to_even_4(n)
188 ------------------------------------
194 ------------------------------------
203 parse_xft_settings (prop
, bytes
, settings
)
206 struct xsettings
*settings
;
208 Lisp_Object byteorder
= Fbyteorder ();
209 int my_bo
= XFASTINT (byteorder
) == 'B' ? MSBFirst
: LSBFirst
;
210 int that_bo
= prop
[0];
212 int bytes_parsed
= 0;
213 int settings_seen
= 0;
216 /* First 4 bytes is a serial number, skip that. */
218 if (bytes
< 12) return BadLength
;
219 memcpy (&n_settings
, prop
+8, 4);
220 if (my_bo
!= that_bo
) n_settings
= SWAP32 (n_settings
);
223 memset (settings
, 0, sizeof (*settings
));
225 while (bytes_parsed
+4 < bytes
&& settings_seen
< 6
228 int type
= prop
[bytes_parsed
++];
230 CARD32 vlen
, ival
= 0;
231 char name
[128]; /* The names we are looking for are not this long. */
232 char sval
[128]; /* The values we are looking for are not this long. */
238 ++bytes_parsed
; /* Padding */
240 memcpy (&nlen
, prop
+bytes_parsed
, 2);
242 if (my_bo
!= that_bo
) nlen
= SWAP16 (nlen
);
243 if (bytes_parsed
+nlen
> bytes
) return BadLength
;
244 to_cpy
= nlen
> 127 ? 127 : nlen
;
245 memcpy (name
, prop
+bytes_parsed
, to_cpy
);
248 bytes_parsed
+= nlen
;
249 bytes_parsed
= PAD (bytes_parsed
);
251 bytes_parsed
+= 4; /* Skip serial for this value */
252 if (bytes_parsed
> bytes
) return BadLength
;
254 is_xft
= nlen
> 6 && strncmp (name
, "Xft/", 4) == 0;
258 case 0: /* Integer */
259 if (bytes_parsed
+4 > bytes
) return BadLength
;
262 memcpy (&ival
, prop
+bytes_parsed
, 4);
263 if (my_bo
!= that_bo
) ival
= SWAP32 (ival
);
269 if (bytes_parsed
+4 > bytes
) return BadLength
;
270 memcpy (&vlen
, prop
+bytes_parsed
, 4);
272 if (my_bo
!= that_bo
) vlen
= SWAP32 (vlen
);
275 to_cpy
= vlen
> 127 ? 127 : vlen
;
276 memcpy (sval
, prop
+bytes_parsed
, to_cpy
);
279 bytes_parsed
+= vlen
;
280 bytes_parsed
= PAD (bytes_parsed
);
283 case 2: /* RGB value */
284 /* No need to parse this */
285 if (bytes_parsed
+8 > bytes
) return BadLength
;
286 bytes_parsed
+= 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
289 default: /* Parse Error */
296 if (strcmp (name
, "Xft/Antialias") == 0)
297 settings
->aa
= ival
!= 0;
298 else if (strcmp (name
, "Xft/Hinting") == 0)
299 settings
->hinting
= ival
!= 0;
300 else if (strcmp (name
, "Xft/HintStyle") == 0)
302 if (strcmp (sval
, "hintnone") == 0)
303 settings
->hintstyle
= FC_HINT_NONE
;
304 else if (strcmp (sval
, "hintslight") == 0)
305 settings
->hintstyle
= FC_HINT_SLIGHT
;
306 else if (strcmp (sval
, "hintmedium") == 0)
307 settings
->hintstyle
= FC_HINT_MEDIUM
;
308 else if (strcmp (sval
, "hintfull") == 0)
309 settings
->hintstyle
= FC_HINT_FULL
;
311 else if (strcmp (name
, "Xft/RGBA") == 0)
313 if (strcmp (sval
, "none") == 0)
314 settings
->rgba
= FC_RGBA_NONE
;
315 else if (strcmp (sval
, "rgb") == 0)
316 settings
->rgba
= FC_RGBA_RGB
;
317 else if (strcmp (sval
, "bgr") == 0)
318 settings
->rgba
= FC_RGBA_BGR
;
319 else if (strcmp (sval
, "vrgb") == 0)
320 settings
->rgba
= FC_RGBA_VRGB
;
321 else if (strcmp (sval
, "vbgr") == 0)
322 settings
->rgba
= FC_RGBA_VBGR
;
324 else if (strcmp (name
, "Xft/DPI") == 0)
325 settings
->dpi
= (double)ival
/1024.0;
326 else if (strcmp (name
, "Xft/lcdfilter") == 0)
328 if (strcmp (sval
, "none") == 0)
329 settings
->lcdfilter
= FC_LCD_NONE
;
330 else if (strcmp (sval
, "lcddefault") == 0)
331 settings
->lcdfilter
= FC_LCD_DEFAULT
;
340 read_xft_settings (dpyinfo
, settings
)
341 struct x_display_info
*dpyinfo
;
342 struct xsettings
*settings
;
347 unsigned long nitems
, bytes_after
;
348 unsigned char *prop
= NULL
;
349 Display
*dpy
= dpyinfo
->display
;
352 x_catch_errors (dpy
);
353 rc
= XGetWindowProperty (dpy
,
354 dpyinfo
->xsettings_window
,
355 dpyinfo
->Xatom_xsettings_prop
,
356 0, LONG_MAX
, False
, AnyPropertyType
,
357 &act_type
, &act_form
, &nitems
, &bytes_after
,
360 if (rc
== Success
&& prop
!= NULL
&& act_form
== 8 && nitems
> 0
361 && act_type
== dpyinfo
->Xatom_xsettings_prop
)
362 rc
= parse_xft_settings (prop
, nitems
, settings
);
368 return rc
== Success
;
373 apply_xft_settings (dpyinfo
, send_event_p
)
374 struct x_display_info
*dpyinfo
;
378 struct xsettings settings
, oldsettings
;
381 if (!read_xft_settings (dpyinfo
, &settings
))
384 memset (&oldsettings
, 0, sizeof (oldsettings
));
386 pat
= FcPatternCreate ();
387 XftDefaultSubstitute (dpyinfo
->display
,
388 XScreenNumberOfScreen (dpyinfo
->screen
),
390 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
391 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
392 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
393 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
394 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
395 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
397 if (oldsettings
.aa
!= settings
.aa
)
399 FcPatternDel (pat
, FC_ANTIALIAS
);
400 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
403 if (oldsettings
.hinting
!= settings
.hinting
)
405 FcPatternDel (pat
, FC_HINTING
);
406 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
409 if (oldsettings
.rgba
!= settings
.rgba
)
411 FcPatternDel (pat
, FC_RGBA
);
412 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
415 /* Older fontconfig versions don't have FC_LCD_FILTER. */
416 if (oldsettings
.lcdfilter
!= settings
.lcdfilter
)
418 FcPatternDel (pat
, FC_LCD_FILTER
);
419 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
422 if (oldsettings
.hintstyle
!= settings
.hintstyle
)
424 FcPatternDel (pat
, FC_HINT_STYLE
);
425 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
428 if (oldsettings
.dpi
!= settings
.dpi
)
430 Lisp_Object frame
, tail
;
432 FcPatternDel (pat
, FC_DPI
);
433 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
436 /* Change the DPI on this display and all frames on the display. */
437 dpyinfo
->resy
= dpyinfo
->resx
= settings
.dpi
;
438 FOR_EACH_FRAME (tail
, frame
)
439 if (FRAME_X_P (XFRAME (frame
))
440 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
441 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
.dpi
;
446 XftDefaultSet (dpyinfo
->display
, pat
);
448 store_font_changed_event (Qfont_render
,
449 XCAR (dpyinfo
->name_list_element
));
452 FcPatternDestroy (pat
);
455 #endif /* HAVE_XFT */
458 xft_settings_event (dpyinfo
, event
)
459 struct x_display_info
*dpyinfo
;
463 int check_window_p
= 0;
468 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
473 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
474 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
475 && event
->xclient
.window
== dpyinfo
->root_window
)
480 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
481 && event
->xproperty
.state
== PropertyNewValue
482 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
484 apply_xft_settings (dpyinfo
, True
);
491 dpyinfo
->xsettings_window
= None
;
492 get_prop_window (dpyinfo
);
493 if (dpyinfo
->xsettings_window
!= None
)
494 apply_xft_settings (dpyinfo
, True
);
496 #endif /* HAVE_XFT */
503 #if defined (HAVE_GCONF) && defined (HAVE_XFT)
508 gconf_client
= gconf_client_get_default ();
509 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
512 current_mono_font
= xstrdup (s
);
515 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
516 gconf_client_add_dir (gconf_client
,
518 GCONF_CLIENT_PRELOAD_ONELEVEL
,
520 gconf_client_notify_add (gconf_client
,
524 #endif /* HAVE_GCONF && HAVE_XFT */
528 init_xfd_settings (dpyinfo
)
529 struct x_display_info
*dpyinfo
;
533 Display
*dpy
= dpyinfo
->display
;
537 sprintf (sel
, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo
->screen
));
538 dpyinfo
->Xatom_xsettings_sel
= XInternAtom (dpy
, sel
, False
);
539 dpyinfo
->Xatom_xsettings_prop
= XInternAtom (dpy
,
540 "_XSETTINGS_SETTINGS",
542 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
544 /* Select events so we can detect client messages sent when selection
546 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
548 get_prop_window (dpyinfo
);
549 if (dpyinfo
->xsettings_window
!= None
)
550 apply_xft_settings (dpyinfo
, False
);
554 #else /* ! HAVE_XFT */
556 dpyinfo
->Xatom_xsettings_sel
= None
;
557 dpyinfo
->Xatom_xsettings_prop
= None
;
558 dpyinfo
->Xatom_xsettings_mgr
= None
;
559 dpyinfo
->xsettings_window
= None
;
561 #endif /* ! HAVE_XFT */
565 xsettings_initialize (dpyinfo
)
566 struct x_display_info
*dpyinfo
;
568 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
570 init_xfd_settings (dpyinfo
);
574 xsettings_get_system_font ()
576 return current_mono_font
;
579 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
581 doc
: /* Get the system default monospaced font. */)
584 return current_mono_font
&& use_system_font
585 ? make_string (current_mono_font
, strlen (current_mono_font
))
592 current_mono_font
= NULL
;
593 first_dpyinfo
= NULL
;
598 Qfont_name
= intern_c_string ("font-name");
599 staticpro (&Qfont_name
);
600 Qfont_render
= intern_c_string ("font-render");
601 staticpro (&Qfont_render
);
602 defsubr (&Sfont_get_system_font
);
604 DEFVAR_BOOL ("font-use-system-font", &use_system_font
,
605 doc
: /* *Non-nil means to use the system defined font. */);
609 Fprovide (intern_c_string ("font-render-setting"), Qnil
);
611 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
616 /* arch-tag: 541716ed-2e6b-42e1-8212-3197e01ea61d
617 (do not change this comment) */