* net/tramp.el (tramp-advice-file-expand-wildcards): Simplify.
[bpt/emacs.git] / src / xsettings.c
CommitLineData
637fa988
JD
1/* Functions for handle font changes dynamically.
2 Copyright (C) 2009
3 Free Software Foundation, Inc.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include <setjmp.h>
22#include <fcntl.h>
23#include "lisp.h"
24#include "xterm.h"
25#include "xsettings.h"
26#include "frame.h"
27#include "blockinput.h"
28#include "termhooks.h"
29#include "termopts.h"
30
31#include <X11/Xproto.h>
32
33#ifdef HAVE_GCONF
34#include <gconf/gconf-client.h>
35#endif
36#ifdef HAVE_XFT
37#include <X11/Xft/Xft.h>
38#endif
39
40static char *current_mono_font;
41static struct x_display_info *first_dpyinfo;
42static Lisp_Object Qfont_name, Qfont_render;
43
44#ifdef HAVE_GCONF
45static GConfClient *gconf_client;
46#endif
47
48
49static void
50store_font_changed_event (arg, display_name)
51 Lisp_Object arg;
52 Lisp_Object display_name;
53{
54 struct input_event event;
55 EVENT_INIT (event);
56 event.kind = CONFIG_CHANGED_EVENT;
57 event.frame_or_window = display_name;
58 event.arg = arg;
59 kbd_buffer_store_event (&event);
60}
61
62#ifdef HAVE_GCONF
63
64#define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name"
65
66/* Callback called when something changed in GConf that we care about,
67 that is SYSTEM_MONO_FONT. */
68
69static void
70something_changedCB (client, cnxn_id, entry, user_data)
71 GConfClient *client;
72 guint cnxn_id;
73 GConfEntry *entry;
74 gpointer user_data;
75{
76 GConfValue *v = gconf_entry_get_value (entry);
77
78 if (!v) return;
79 if (v->type == GCONF_VALUE_STRING)
80 {
81 const char *value = gconf_value_get_string (v);
82 int i;
83 if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0)
84 return; // No change.
85
86 xfree (current_mono_font);
87 current_mono_font = xstrdup (value);
88 }
89
90
91 if (first_dpyinfo != NULL)
92 {
93 /* Check if display still open */
94 struct x_display_info *dpyinfo;
95 int found = 0;
96 for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next)
97 found = dpyinfo == first_dpyinfo;
98
99 if (found)
100 store_font_changed_event (Qfont_name,
101 XCAR (first_dpyinfo->name_list_element));
102 }
103}
104#endif /* HAVE_GCONF */
105
106#ifdef HAVE_XFT
107
108/* Find the window that contains the XSETTINGS property values. */
109
110static void
111get_prop_window (dpyinfo)
112 struct x_display_info *dpyinfo;
113{
114 Display *dpy = dpyinfo->display;
115
116 XGrabServer (dpy);
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
121 are changed. */
122 XSelectInput (dpy, dpyinfo->xsettings_window,
123 PropertyChangeMask|StructureNotifyMask);
124
125 XUngrabServer (dpy);
126}
127
128struct xsettings
129{
130 FcBool aa, hinting;
131 int rgba, lcdfilter, hintstyle;
132 double dpi;
133};
134
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)
139
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.
143
144 Layout of prop. First is a header:
145
146 bytes type what
147 ------------------------------------
148 1 CARD8 byte-order
149 3 unused
150 4 CARD32 SERIAL
151 4 CARD32 N_SETTINGS
152
153 Then N_SETTINGS records, with header:
154
155 bytes type what
156 ------------------------------------
157 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
158 1 unused
159 2 CARD16 n == name-length
160 n STRING8 name
161 p unused, p=pad_to_even_4(n)
162 4 CARD32 last-change-serial
163
164 and then the value, For string:
165
166 bytes type what
167 ------------------------------------
168 4 CARD32 n = value-length
169 n STRING8 value
170 p unused, p=pad_to_even_4(n)
171
172 For integer:
173
174 bytes type what
175 ------------------------------------
176 4 INT32 value
177
178 For RGB color:
179
180 bytes type what
181 ------------------------------------
182 2 CARD16 red
183 2 CARD16 blue
184 2 CARD16 green
185 2 CARD16 alpha
186
187*/
188
189static int
190parse_xft_settings (prop, bytes, settings)
191 unsigned char *prop;
192 unsigned long bytes;
193 struct xsettings *settings;
194{
195 Lisp_Object byteorder = Fbyteorder ();
196 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
197 int that_bo = prop[0];
198 CARD32 n_settings;
199 int bytes_parsed = 0;
200 int settings_seen = 0;
201 int i = 0;
202
203 /* First 4 bytes is a serial number, skip that. */
204
205 if (bytes < 12) return BadLength;
206 memcpy (&n_settings, prop+8, 4);
207 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
208 bytes_parsed = 12;
209
210 memset (settings, 0, sizeof (*settings));
211
212 while (bytes_parsed+4 < bytes && settings_seen < 6
213 && i < n_settings)
214 {
215 int type = prop[bytes_parsed++];
216 CARD16 nlen;
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. */
220 int is_xft;
221 int to_cpy;
222
223 sval[0] = '\0';
224 ++i;
225 ++bytes_parsed; /* Padding */
226
227 memcpy (&nlen, prop+bytes_parsed, 2);
228 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);
233 name[to_cpy] = '\0';
234
235 bytes_parsed += nlen;
236 bytes_parsed = PAD (bytes_parsed);
237
238 bytes_parsed += 4; /* Skip serial for this value */
239 if (bytes_parsed > bytes) return BadLength;
240
241 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
242
243 switch (type)
244 {
245 case 0: /* Integer */
246 if (bytes_parsed+4 > bytes) return BadLength;
247 if (is_xft)
248 {
249 memcpy (&ival, prop+bytes_parsed, 4);
250 if (my_bo != that_bo) ival = SWAP32 (ival);
251 }
252 bytes_parsed += 4;
253 break;
254
255 case 1: /* String */
256 if (bytes_parsed+4 > bytes) return BadLength;
257 memcpy (&vlen, prop+bytes_parsed, 4);
258 bytes_parsed += 4;
259 if (my_bo != that_bo) vlen = SWAP32 (vlen);
260 if (is_xft)
261 {
262 to_cpy = vlen > 127 ? 127 : vlen;
263 memcpy (sval, prop+bytes_parsed, to_cpy);
264 sval[to_cpy] = '\0';
265 }
266 bytes_parsed += vlen;
267 bytes_parsed = PAD (bytes_parsed);
268 break;
269
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. */
274 break;
275
276 default: /* Parse Error */
277 return BadValue;
278 }
279
280 if (is_xft)
281 {
282 ++settings_seen;
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)
288 {
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;
297 }
298 else if (strcmp (name, "Xft/RGBA") == 0)
299 {
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;
310 }
311 else if (strcmp (name, "Xft/DPI") == 0)
312 settings->dpi = (double)ival/1024.0;
a6eb20d8
JD
313#ifdef FC_LCD_FILTER
314 /* Older fontconfig versions don't have FC_LCD_FILTER. */
637fa988
JD
315 else if (strcmp (name, "Xft/lcdfilter") == 0)
316 {
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;
321 }
a6eb20d8 322#endif
637fa988
JD
323 }
324 }
325
326 return Success;
327}
328
329static int
330read_xft_settings (dpyinfo, settings)
331 struct x_display_info *dpyinfo;
332 struct xsettings *settings;
333{
334 long long_len;
335 Atom act_type;
336 int act_form;
337 unsigned long nitems, bytes_after;
338 unsigned char *prop = NULL;
339 Display *dpy = dpyinfo->display;
340 int rc;
341
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,
348 &prop);
349
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);
353
354 XFree (prop);
355
356 x_uncatch_errors ();
357
358 return rc == Success;
359}
360
361static void
362apply_xft_settings (dpyinfo, send_event_p)
363 struct x_display_info *dpyinfo;
364 int send_event_p;
365{
366 FcPattern *pat;
367 struct xsettings settings, oldsettings;
368 int changed = 0;
369
370 if (!read_xft_settings (dpyinfo, &settings))
371 return;
372
373 memset (&oldsettings, 0, sizeof (oldsettings));
374
375 pat = FcPatternCreate ();
376 XftDefaultSubstitute (dpyinfo->display,
377 XScreenNumberOfScreen (dpyinfo->screen),
378 pat);
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);
a6eb20d8
JD
382#ifdef FC_LCD_FILTER
383 /* Older fontconfig versions don't have FC_LCD_FILTER. */
637fa988 384 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
a6eb20d8 385#endif
637fa988
JD
386 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
387 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
388
389 if (oldsettings.aa != settings.aa)
390 {
391 FcPatternDel (pat, FC_ANTIALIAS);
392 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
393 ++changed;
394 }
395 if (oldsettings.hinting != settings.hinting)
396 {
397 FcPatternDel (pat, FC_HINTING);
398 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
399 ++changed;
400 }
401 if (oldsettings.rgba != settings.rgba)
402 {
403 FcPatternDel (pat, FC_RGBA);
404 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
405 ++changed;
406 }
a6eb20d8
JD
407#ifdef FC_LCD_FILTER
408 /* Older fontconfig versions don't have FC_LCD_FILTER. */
637fa988
JD
409 if (oldsettings.lcdfilter != settings.lcdfilter)
410 {
411 FcPatternDel (pat, FC_LCD_FILTER);
412 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
413 ++changed;
414 }
a6eb20d8 415#endif
637fa988
JD
416 if (oldsettings.hintstyle != settings.hintstyle)
417 {
418 FcPatternDel (pat, FC_HINT_STYLE);
419 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
420 ++changed;
421 }
422 if (oldsettings.dpi != settings.dpi)
423 {
424 Lisp_Object frame, tail;
425
426 FcPatternDel (pat, FC_DPI);
427 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
428 ++changed;
429
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;
436 }
437
438 if (changed)
439 {
440 XftDefaultSet (dpyinfo->display, pat);
441 if (send_event_p)
442 store_font_changed_event (Qfont_render,
443 XCAR (dpyinfo->name_list_element));
444 }
445 else
446 FcPatternDestroy (pat);
447}
448
449#endif /* HAVE_XFT */
450
451void
452xft_settings_event (dpyinfo, event)
453 struct x_display_info *dpyinfo;
454 XEvent *event;
455{
456#ifdef HAVE_XFT
457 int check_window_p = 0;
458
459 switch (event->type)
460 {
461 case DestroyNotify:
462 if (dpyinfo->xsettings_window == event->xany.window)
463 check_window_p = 1;
464 break;
465
466 case ClientMessage:
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)
470 check_window_p = 1;
471 break;
472
473 case PropertyNotify:
474 if (event->xproperty.window == dpyinfo->xsettings_window
475 && event->xproperty.state == PropertyNewValue
476 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
477 {
478 apply_xft_settings (dpyinfo, True);
479 }
480 break;
481 }
482
483 if (check_window_p)
484 {
485 dpyinfo->xsettings_window = None;
486 get_prop_window (dpyinfo);
487 if (dpyinfo->xsettings_window != None)
488 apply_xft_settings (dpyinfo, True);
489 }
490#endif /* HAVE_XFT */
491}
492
493
494static void
495init_gconf ()
496{
497#ifdef HAVE_GCONF
498 int i;
499 char *s;
500 /* Should be enough, this is called at startup */
501#define N_FDS 1024
502 int fd_before[N_FDS], fd_before1[N_FDS];
503 int dummy, n_fds;
504 GPollFD gfds[N_FDS];
505
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
508 an X event. */
509 memset (fd_before, 0, sizeof (fd_before));
510 n_fds = g_main_context_query (g_main_context_default (),
511 G_PRIORITY_LOW,
512 &dummy,
513 gfds,
514 N_FDS);
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;
518
519 g_type_init ();
520 gconf_client = gconf_client_get_default ();
521 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
522 if (s)
523 {
524 current_mono_font = xstrdup (s);
525 g_free (s);
526 }
527 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
528 gconf_client_add_dir (gconf_client,
529 SYSTEM_MONO_FONT,
530 GCONF_CLIENT_PRELOAD_ONELEVEL,
531 NULL);
532 gconf_client_notify_add (gconf_client,
533 SYSTEM_MONO_FONT,
534 something_changedCB,
535 NULL, NULL, NULL);
536 n_fds = g_main_context_query (g_main_context_default (),
537 G_PRIORITY_LOW,
538 &dummy,
539 gfds,
540 N_FDS);
541
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])
545 {
546#ifdef F_SETOWN
547 fcntl (i, F_SETOWN, getpid ());
548#endif /* ! defined (F_SETOWN) */
549
550#ifdef SIGIO
551 if (interrupt_input)
552 init_sigio (i);
553#endif /* ! defined (SIGIO) */
554 }
555#endif /* HAVE_GCONF */
556}
557
558static void
559init_xfd_settings (dpyinfo)
560 struct x_display_info *dpyinfo;
561{
562#ifdef HAVE_XFT
563 char sel[64];
564 Display *dpy = dpyinfo->display;
565
566 BLOCK_INPUT;
567
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",
572 False);
573 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
574
575 /* Select events so we can detect client messages sent when selection
576 owner changes. */
577 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
578
579 get_prop_window (dpyinfo);
580 if (dpyinfo->xsettings_window != None)
581 apply_xft_settings (dpyinfo, False);
582
583 UNBLOCK_INPUT;
584
585#else /* ! HAVE_XFT */
586
587 dpyinfo->Xatom_xsettings_sel = None;
588 dpyinfo->Xatom_xsettings_prop = None;
589 dpyinfo->Xatom_xsettings_mgr = None;
590 dpyinfo->xsettings_window = None;
591
592#endif /* ! HAVE_XFT */
593}
594
595void
596xsettings_initialize (dpyinfo)
597 struct x_display_info *dpyinfo;
598{
599 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
600 init_gconf ();
601 init_xfd_settings (dpyinfo);
602}
603
604
605DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
606 0, 0, 0,
607 doc: /* Get the system default monospaced font. */)
608 ()
609{
610 return current_mono_font
611 ? make_string (current_mono_font, strlen (current_mono_font))
612 : Qnil;
613}
614
615void
616syms_of_xsettings ()
617{
618 current_mono_font = NULL;
619 first_dpyinfo = NULL;
620#ifdef HAVE_GCONF
621 gconf_client = NULL;
622#endif
623
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);
629
630#ifdef HAVE_GCONF
631 Fprovide (intern_c_string ("system-font-setting"), Qnil);
632#endif
633#ifdef HAVE_XFT
634 Fprovide (intern_c_string ("font-render-setting"), Qnil);
635#endif
636}