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
;
45 static GConfClient
*gconf_client
;
50 store_font_changed_event (arg
, display_name
)
52 Lisp_Object display_name
;
54 struct input_event event
;
56 event
.kind
= CONFIG_CHANGED_EVENT
;
57 event
.frame_or_window
= display_name
;
59 kbd_buffer_store_event (&event
);
64 #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
66 /* Callback called when something changed in GConf that we care about,
67 that is SYSTEM_MONO_FONT. */
70 something_changedCB (client
, cnxn_id
, entry
, user_data
)
76 GConfValue
*v
= gconf_entry_get_value (entry
);
79 if (v
->type
== GCONF_VALUE_STRING
)
81 const char *value
= gconf_value_get_string (v
);
83 if (current_mono_font
!= NULL
&& strcmp (value
, current_mono_font
) == 0)
86 xfree (current_mono_font
);
87 current_mono_font
= xstrdup (value
);
91 if (first_dpyinfo
!= NULL
)
93 /* Check if display still open */
94 struct x_display_info
*dpyinfo
;
96 for (dpyinfo
= x_display_list
; !found
&& dpyinfo
; dpyinfo
= dpyinfo
->next
)
97 found
= dpyinfo
== first_dpyinfo
;
100 store_font_changed_event (Qfont_name
,
101 XCAR (first_dpyinfo
->name_list_element
));
104 #endif /* HAVE_GCONF */
108 /* Find the window that contains the XSETTINGS property values. */
111 get_prop_window (dpyinfo
)
112 struct x_display_info
*dpyinfo
;
114 Display
*dpy
= dpyinfo
->display
;
117 dpyinfo
->xsettings_window
= XGetSelectionOwner (dpy
,
118 dpyinfo
->Xatom_xsettings_sel
);
119 if (dpyinfo
->xsettings_window
!= None
)
120 /* Select events so we can detect if window is deleted or if settings
122 XSelectInput (dpy
, dpyinfo
->xsettings_window
,
123 PropertyChangeMask
|StructureNotifyMask
);
131 int rgba
, lcdfilter
, hintstyle
;
135 #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
136 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
137 #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
138 #define PAD(nr) (((nr) + 3) & ~3)
140 /* Parse xsettings and extract those that deal with Xft.
141 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
142 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
144 Layout of prop. First is a header:
147 ------------------------------------
153 Then N_SETTINGS records, with header:
156 ------------------------------------
157 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
159 2 CARD16 n == name-length
161 p unused, p=pad_to_even_4(n)
162 4 CARD32 last-change-serial
164 and then the value, For string:
167 ------------------------------------
168 4 CARD32 n = value-length
170 p unused, p=pad_to_even_4(n)
175 ------------------------------------
181 ------------------------------------
190 parse_xft_settings (prop
, bytes
, settings
)
193 struct xsettings
*settings
;
195 Lisp_Object byteorder
= Fbyteorder ();
196 int my_bo
= XFASTINT (byteorder
) == 'B' ? MSBFirst
: LSBFirst
;
197 int that_bo
= prop
[0];
199 int bytes_parsed
= 0;
200 int settings_seen
= 0;
203 /* First 4 bytes is a serial number, skip that. */
205 if (bytes
< 12) return BadLength
;
206 memcpy (&n_settings
, prop
+8, 4);
207 if (my_bo
!= that_bo
) n_settings
= SWAP32 (n_settings
);
210 memset (settings
, 0, sizeof (*settings
));
212 while (bytes_parsed
+4 < bytes
&& settings_seen
< 6
215 int type
= prop
[bytes_parsed
++];
217 CARD32 vlen
, ival
= 0;
218 char name
[128]; /* The names we are looking for are not this long. */
219 char sval
[128]; /* The values we are looking for are not this long. */
225 ++bytes_parsed
; /* Padding */
227 memcpy (&nlen
, prop
+bytes_parsed
, 2);
229 if (my_bo
!= that_bo
) nlen
= SWAP16 (nlen
);
230 if (bytes_parsed
+nlen
> bytes
) return BadLength
;
231 to_cpy
= nlen
> 127 ? 127 : nlen
;
232 memcpy (name
, prop
+bytes_parsed
, to_cpy
);
235 bytes_parsed
+= nlen
;
236 bytes_parsed
= PAD (bytes_parsed
);
238 bytes_parsed
+= 4; /* Skip serial for this value */
239 if (bytes_parsed
> bytes
) return BadLength
;
241 is_xft
= nlen
> 6 && strncmp (name
, "Xft/", 4) == 0;
245 case 0: /* Integer */
246 if (bytes_parsed
+4 > bytes
) return BadLength
;
249 memcpy (&ival
, prop
+bytes_parsed
, 4);
250 if (my_bo
!= that_bo
) ival
= SWAP32 (ival
);
256 if (bytes_parsed
+4 > bytes
) return BadLength
;
257 memcpy (&vlen
, prop
+bytes_parsed
, 4);
259 if (my_bo
!= that_bo
) vlen
= SWAP32 (vlen
);
262 to_cpy
= vlen
> 127 ? 127 : vlen
;
263 memcpy (sval
, prop
+bytes_parsed
, to_cpy
);
266 bytes_parsed
+= vlen
;
267 bytes_parsed
= PAD (bytes_parsed
);
270 case 2: /* RGB value */
271 /* No need to parse this */
272 if (bytes_parsed
+8 > bytes
) return BadLength
;
273 bytes_parsed
+= 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
276 default: /* Parse Error */
283 if (strcmp (name
, "Xft/Antialias") == 0)
284 settings
->aa
= ival
!= 0;
285 else if (strcmp (name
, "Xft/Hinting") == 0)
286 settings
->hinting
= ival
!= 0;
287 else if (strcmp (name
, "Xft/HintStyle") == 0)
289 if (strcmp (sval
, "hintnone") == 0)
290 settings
->hintstyle
= FC_HINT_NONE
;
291 else if (strcmp (sval
, "hintslight") == 0)
292 settings
->hintstyle
= FC_HINT_SLIGHT
;
293 else if (strcmp (sval
, "hintmedium") == 0)
294 settings
->hintstyle
= FC_HINT_MEDIUM
;
295 else if (strcmp (sval
, "hintfull") == 0)
296 settings
->hintstyle
= FC_HINT_FULL
;
298 else if (strcmp (name
, "Xft/RGBA") == 0)
300 if (strcmp (sval
, "none") == 0)
301 settings
->rgba
= FC_RGBA_NONE
;
302 else if (strcmp (sval
, "rgb") == 0)
303 settings
->rgba
= FC_RGBA_RGB
;
304 else if (strcmp (sval
, "bgr") == 0)
305 settings
->rgba
= FC_RGBA_BGR
;
306 else if (strcmp (sval
, "vrgb") == 0)
307 settings
->rgba
= FC_RGBA_VRGB
;
308 else if (strcmp (sval
, "vbgr") == 0)
309 settings
->rgba
= FC_RGBA_VBGR
;
311 else if (strcmp (name
, "Xft/DPI") == 0)
312 settings
->dpi
= (double)ival
/1024.0;
314 /* Older fontconfig versions don't have FC_LCD_FILTER. */
315 else if (strcmp (name
, "Xft/lcdfilter") == 0)
317 if (strcmp (sval
, "none") == 0)
318 settings
->lcdfilter
= FC_LCD_NONE
;
319 else if (strcmp (sval
, "lcddefault") == 0)
320 settings
->lcdfilter
= FC_LCD_DEFAULT
;
330 read_xft_settings (dpyinfo
, settings
)
331 struct x_display_info
*dpyinfo
;
332 struct xsettings
*settings
;
337 unsigned long nitems
, bytes_after
;
338 unsigned char *prop
= NULL
;
339 Display
*dpy
= dpyinfo
->display
;
342 x_catch_errors (dpy
);
343 rc
= XGetWindowProperty (dpy
,
344 dpyinfo
->xsettings_window
,
345 dpyinfo
->Xatom_xsettings_prop
,
346 0, LONG_MAX
, False
, AnyPropertyType
,
347 &act_type
, &act_form
, &nitems
, &bytes_after
,
350 if (rc
== Success
&& prop
!= NULL
&& act_form
== 8 && nitems
> 0
351 && act_type
== dpyinfo
->Xatom_xsettings_prop
)
352 rc
= parse_xft_settings (prop
, nitems
, settings
);
358 return rc
== Success
;
362 apply_xft_settings (dpyinfo
, send_event_p
)
363 struct x_display_info
*dpyinfo
;
367 struct xsettings settings
, oldsettings
;
370 if (!read_xft_settings (dpyinfo
, &settings
))
373 memset (&oldsettings
, 0, sizeof (oldsettings
));
375 pat
= FcPatternCreate ();
376 XftDefaultSubstitute (dpyinfo
->display
,
377 XScreenNumberOfScreen (dpyinfo
->screen
),
379 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
380 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
381 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
383 /* Older fontconfig versions don't have FC_LCD_FILTER. */
384 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
386 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
387 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
389 if (oldsettings
.aa
!= settings
.aa
)
391 FcPatternDel (pat
, FC_ANTIALIAS
);
392 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
395 if (oldsettings
.hinting
!= settings
.hinting
)
397 FcPatternDel (pat
, FC_HINTING
);
398 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
401 if (oldsettings
.rgba
!= settings
.rgba
)
403 FcPatternDel (pat
, FC_RGBA
);
404 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
408 /* Older fontconfig versions don't have FC_LCD_FILTER. */
409 if (oldsettings
.lcdfilter
!= settings
.lcdfilter
)
411 FcPatternDel (pat
, FC_LCD_FILTER
);
412 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
416 if (oldsettings
.hintstyle
!= settings
.hintstyle
)
418 FcPatternDel (pat
, FC_HINT_STYLE
);
419 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
422 if (oldsettings
.dpi
!= settings
.dpi
)
424 Lisp_Object frame
, tail
;
426 FcPatternDel (pat
, FC_DPI
);
427 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
430 /* Change the DPI on this display and all frames on the display. */
431 dpyinfo
->resy
= dpyinfo
->resx
= settings
.dpi
;
432 FOR_EACH_FRAME (tail
, frame
)
433 if (FRAME_X_P (XFRAME (frame
))
434 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
435 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
.dpi
;
440 XftDefaultSet (dpyinfo
->display
, pat
);
442 store_font_changed_event (Qfont_render
,
443 XCAR (dpyinfo
->name_list_element
));
446 FcPatternDestroy (pat
);
449 #endif /* HAVE_XFT */
452 xft_settings_event (dpyinfo
, event
)
453 struct x_display_info
*dpyinfo
;
457 int check_window_p
= 0;
462 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
467 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
468 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
469 && event
->xclient
.window
== dpyinfo
->root_window
)
474 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
475 && event
->xproperty
.state
== PropertyNewValue
476 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
478 apply_xft_settings (dpyinfo
, True
);
485 dpyinfo
->xsettings_window
= None
;
486 get_prop_window (dpyinfo
);
487 if (dpyinfo
->xsettings_window
!= None
)
488 apply_xft_settings (dpyinfo
, True
);
490 #endif /* HAVE_XFT */
500 /* Should be enough, this is called at startup */
502 int fd_before
[N_FDS
], fd_before1
[N_FDS
];
506 /* To find out which filedecriptors GConf uses, check before and after.
507 If we do not do this, GConf changes will only happen when Emacs gets
509 memset (fd_before
, 0, sizeof (fd_before
));
510 n_fds
= g_main_context_query (g_main_context_default (),
515 for (i
= 0; i
< n_fds
; ++i
)
516 if (gfds
[i
].fd
< N_FDS
&& gfds
[i
].fd
> 0 && gfds
[i
].events
> 0)
517 fd_before
[gfds
[i
].fd
] = 1;
520 gconf_client
= gconf_client_get_default ();
521 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
524 current_mono_font
= xstrdup (s
);
527 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
528 gconf_client_add_dir (gconf_client
,
530 GCONF_CLIENT_PRELOAD_ONELEVEL
,
532 gconf_client_notify_add (gconf_client
,
536 n_fds
= g_main_context_query (g_main_context_default (),
542 for (i
= 0; i
< n_fds
; ++i
)
543 if (gfds
[i
].fd
< N_FDS
&& gfds
[i
].fd
> 0 && gfds
[i
].events
> 0
544 && !fd_before
[gfds
[i
].fd
])
547 fcntl (i
, F_SETOWN
, getpid ());
548 #endif /* ! defined (F_SETOWN) */
553 #endif /* ! defined (SIGIO) */
555 #endif /* HAVE_GCONF */
559 init_xfd_settings (dpyinfo
)
560 struct x_display_info
*dpyinfo
;
564 Display
*dpy
= dpyinfo
->display
;
568 sprintf (sel
, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo
->screen
));
569 dpyinfo
->Xatom_xsettings_sel
= XInternAtom (dpy
, sel
, False
);
570 dpyinfo
->Xatom_xsettings_prop
= XInternAtom (dpy
,
571 "_XSETTINGS_SETTINGS",
573 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
575 /* Select events so we can detect client messages sent when selection
577 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
579 get_prop_window (dpyinfo
);
580 if (dpyinfo
->xsettings_window
!= None
)
581 apply_xft_settings (dpyinfo
, False
);
585 #else /* ! HAVE_XFT */
587 dpyinfo
->Xatom_xsettings_sel
= None
;
588 dpyinfo
->Xatom_xsettings_prop
= None
;
589 dpyinfo
->Xatom_xsettings_mgr
= None
;
590 dpyinfo
->xsettings_window
= None
;
592 #endif /* ! HAVE_XFT */
596 xsettings_initialize (dpyinfo
)
597 struct x_display_info
*dpyinfo
;
599 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
601 init_xfd_settings (dpyinfo
);
605 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
607 doc
: /* Get the system default monospaced font. */)
610 return current_mono_font
611 ? make_string (current_mono_font
, strlen (current_mono_font
))
618 current_mono_font
= NULL
;
619 first_dpyinfo
= NULL
;
624 Qfont_name
= intern_c_string ("font-name");
625 staticpro (&Qfont_name
);
626 Qfont_render
= intern_c_string ("font-render");
627 staticpro (&Qfont_render
);
628 defsubr (&Sfont_get_system_font
);
631 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
634 Fprovide (intern_c_string ("font-render-setting"), Qnil
);