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;
313 else if (strcmp (name
, "Xft/lcdfilter") == 0)
315 if (strcmp (sval
, "none") == 0)
316 settings
->lcdfilter
= FC_LCD_NONE
;
317 else if (strcmp (sval
, "lcddefault") == 0)
318 settings
->lcdfilter
= FC_LCD_DEFAULT
;
327 read_xft_settings (dpyinfo
, settings
)
328 struct x_display_info
*dpyinfo
;
329 struct xsettings
*settings
;
334 unsigned long nitems
, bytes_after
;
335 unsigned char *prop
= NULL
;
336 Display
*dpy
= dpyinfo
->display
;
339 x_catch_errors (dpy
);
340 rc
= XGetWindowProperty (dpy
,
341 dpyinfo
->xsettings_window
,
342 dpyinfo
->Xatom_xsettings_prop
,
343 0, LONG_MAX
, False
, AnyPropertyType
,
344 &act_type
, &act_form
, &nitems
, &bytes_after
,
347 if (rc
== Success
&& prop
!= NULL
&& act_form
== 8 && nitems
> 0
348 && act_type
== dpyinfo
->Xatom_xsettings_prop
)
349 rc
= parse_xft_settings (prop
, nitems
, settings
);
355 return rc
== Success
;
359 apply_xft_settings (dpyinfo
, send_event_p
)
360 struct x_display_info
*dpyinfo
;
364 struct xsettings settings
, oldsettings
;
367 if (!read_xft_settings (dpyinfo
, &settings
))
370 memset (&oldsettings
, 0, sizeof (oldsettings
));
372 pat
= FcPatternCreate ();
373 XftDefaultSubstitute (dpyinfo
->display
,
374 XScreenNumberOfScreen (dpyinfo
->screen
),
376 FcPatternGetBool (pat
, FC_ANTIALIAS
, 0, &oldsettings
.aa
);
377 FcPatternGetBool (pat
, FC_HINTING
, 0, &oldsettings
.hinting
);
378 FcPatternGetInteger (pat
, FC_HINT_STYLE
, 0, &oldsettings
.hintstyle
);
379 FcPatternGetInteger (pat
, FC_LCD_FILTER
, 0, &oldsettings
.lcdfilter
);
380 FcPatternGetInteger (pat
, FC_RGBA
, 0, &oldsettings
.rgba
);
381 FcPatternGetDouble (pat
, FC_DPI
, 0, &oldsettings
.dpi
);
383 if (oldsettings
.aa
!= settings
.aa
)
385 FcPatternDel (pat
, FC_ANTIALIAS
);
386 FcPatternAddBool (pat
, FC_ANTIALIAS
, settings
.aa
);
389 if (oldsettings
.hinting
!= settings
.hinting
)
391 FcPatternDel (pat
, FC_HINTING
);
392 FcPatternAddBool (pat
, FC_HINTING
, settings
.hinting
);
395 if (oldsettings
.rgba
!= settings
.rgba
)
397 FcPatternDel (pat
, FC_RGBA
);
398 FcPatternAddInteger (pat
, FC_RGBA
, settings
.rgba
);
401 if (oldsettings
.lcdfilter
!= settings
.lcdfilter
)
403 FcPatternDel (pat
, FC_LCD_FILTER
);
404 FcPatternAddInteger (pat
, FC_LCD_FILTER
, settings
.lcdfilter
);
407 if (oldsettings
.hintstyle
!= settings
.hintstyle
)
409 FcPatternDel (pat
, FC_HINT_STYLE
);
410 FcPatternAddInteger (pat
, FC_HINT_STYLE
, settings
.hintstyle
);
413 if (oldsettings
.dpi
!= settings
.dpi
)
415 Lisp_Object frame
, tail
;
417 FcPatternDel (pat
, FC_DPI
);
418 FcPatternAddDouble (pat
, FC_DPI
, settings
.dpi
);
421 /* Change the DPI on this display and all frames on the display. */
422 dpyinfo
->resy
= dpyinfo
->resx
= settings
.dpi
;
423 FOR_EACH_FRAME (tail
, frame
)
424 if (FRAME_X_P (XFRAME (frame
))
425 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
426 XFRAME (frame
)->resy
= XFRAME (frame
)->resx
= settings
.dpi
;
431 XftDefaultSet (dpyinfo
->display
, pat
);
433 store_font_changed_event (Qfont_render
,
434 XCAR (dpyinfo
->name_list_element
));
437 FcPatternDestroy (pat
);
440 #endif /* HAVE_XFT */
443 xft_settings_event (dpyinfo
, event
)
444 struct x_display_info
*dpyinfo
;
448 int check_window_p
= 0;
453 if (dpyinfo
->xsettings_window
== event
->xany
.window
)
458 if (event
->xclient
.message_type
== dpyinfo
->Xatom_xsettings_mgr
459 && event
->xclient
.data
.l
[1] == dpyinfo
->Xatom_xsettings_sel
460 && event
->xclient
.window
== dpyinfo
->root_window
)
465 if (event
->xproperty
.window
== dpyinfo
->xsettings_window
466 && event
->xproperty
.state
== PropertyNewValue
467 && event
->xproperty
.atom
== dpyinfo
->Xatom_xsettings_prop
)
469 apply_xft_settings (dpyinfo
, True
);
476 dpyinfo
->xsettings_window
= None
;
477 get_prop_window (dpyinfo
);
478 if (dpyinfo
->xsettings_window
!= None
)
479 apply_xft_settings (dpyinfo
, True
);
481 #endif /* HAVE_XFT */
491 /* Should be enough, this is called at startup */
493 int fd_before
[N_FDS
], fd_before1
[N_FDS
];
497 /* To find out which filedecriptors GConf uses, check before and after.
498 If we do not do this, GConf changes will only happen when Emacs gets
500 memset (fd_before
, 0, sizeof (fd_before
));
501 n_fds
= g_main_context_query (g_main_context_default (),
506 for (i
= 0; i
< n_fds
; ++i
)
507 if (gfds
[i
].fd
< N_FDS
&& gfds
[i
].fd
> 0 && gfds
[i
].events
> 0)
508 fd_before
[gfds
[i
].fd
] = 1;
511 gconf_client
= gconf_client_get_default ();
512 s
= gconf_client_get_string (gconf_client
, SYSTEM_MONO_FONT
, NULL
);
515 current_mono_font
= xstrdup (s
);
518 gconf_client_set_error_handling (gconf_client
, GCONF_CLIENT_HANDLE_NONE
);
519 gconf_client_add_dir (gconf_client
,
521 GCONF_CLIENT_PRELOAD_ONELEVEL
,
523 gconf_client_notify_add (gconf_client
,
527 n_fds
= g_main_context_query (g_main_context_default (),
533 for (i
= 0; i
< n_fds
; ++i
)
534 if (gfds
[i
].fd
< N_FDS
&& gfds
[i
].fd
> 0 && gfds
[i
].events
> 0
535 && !fd_before
[gfds
[i
].fd
])
538 fcntl (i
, F_SETOWN
, getpid ());
539 #endif /* ! defined (F_SETOWN) */
544 #endif /* ! defined (SIGIO) */
546 #endif /* HAVE_GCONF */
550 init_xfd_settings (dpyinfo
)
551 struct x_display_info
*dpyinfo
;
555 Display
*dpy
= dpyinfo
->display
;
559 sprintf (sel
, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo
->screen
));
560 dpyinfo
->Xatom_xsettings_sel
= XInternAtom (dpy
, sel
, False
);
561 dpyinfo
->Xatom_xsettings_prop
= XInternAtom (dpy
,
562 "_XSETTINGS_SETTINGS",
564 dpyinfo
->Xatom_xsettings_mgr
= XInternAtom (dpy
, "MANAGER", False
);
566 /* Select events so we can detect client messages sent when selection
568 XSelectInput (dpy
, dpyinfo
->root_window
, StructureNotifyMask
);
570 get_prop_window (dpyinfo
);
571 if (dpyinfo
->xsettings_window
!= None
)
572 apply_xft_settings (dpyinfo
, False
);
576 #else /* ! HAVE_XFT */
578 dpyinfo
->Xatom_xsettings_sel
= None
;
579 dpyinfo
->Xatom_xsettings_prop
= None
;
580 dpyinfo
->Xatom_xsettings_mgr
= None
;
581 dpyinfo
->xsettings_window
= None
;
583 #endif /* ! HAVE_XFT */
587 xsettings_initialize (dpyinfo
)
588 struct x_display_info
*dpyinfo
;
590 if (first_dpyinfo
== NULL
) first_dpyinfo
= dpyinfo
;
592 init_xfd_settings (dpyinfo
);
596 DEFUN ("font-get-system-font", Ffont_get_system_font
, Sfont_get_system_font
,
598 doc
: /* Get the system default monospaced font. */)
601 return current_mono_font
602 ? make_string (current_mono_font
, strlen (current_mono_font
))
609 current_mono_font
= NULL
;
610 first_dpyinfo
= NULL
;
615 Qfont_name
= intern_c_string ("font-name");
616 staticpro (&Qfont_name
);
617 Qfont_render
= intern_c_string ("font-render");
618 staticpro (&Qfont_render
);
619 defsubr (&Sfont_get_system_font
);
622 Fprovide (intern_c_string ("system-font-setting"), Qnil
);
625 Fprovide (intern_c_string ("font-render-setting"), Qnil
);