(x_default_font_parameter): Remove dead assignment.
[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
21050de1
JD
108/* Older fontconfig versions don't have FC_LCD_*. */
109#ifndef FC_LCD_NONE
110#define FC_LCD_NONE 0
111#endif
112#ifndef FC_LCD_DEFAULT
113#define FC_LCD_DEFAULT 1
114#endif
115#ifndef FC_LCD_FILTER
116#define FC_LCD_FILTER "lcdfilter"
117#endif
118
637fa988
JD
119/* Find the window that contains the XSETTINGS property values. */
120
121static void
122get_prop_window (dpyinfo)
123 struct x_display_info *dpyinfo;
124{
125 Display *dpy = dpyinfo->display;
126
127 XGrabServer (dpy);
128 dpyinfo->xsettings_window = XGetSelectionOwner (dpy,
129 dpyinfo->Xatom_xsettings_sel);
130 if (dpyinfo->xsettings_window != None)
131 /* Select events so we can detect if window is deleted or if settings
132 are changed. */
133 XSelectInput (dpy, dpyinfo->xsettings_window,
134 PropertyChangeMask|StructureNotifyMask);
135
136 XUngrabServer (dpy);
137}
138
139struct xsettings
140{
141 FcBool aa, hinting;
142 int rgba, lcdfilter, hintstyle;
143 double dpi;
144};
145
146#define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \
147 | (((nr) >> 8) & 0xff00) | ((nr) >> 24))
148#define SWAP16(nr) (((nr) << 8) | ((nr) >> 8))
149#define PAD(nr) (((nr) + 3) & ~3)
150
151/* Parse xsettings and extract those that deal with Xft.
152 See http://freedesktop.org/wiki/Specifications/XSettingsRegistry
153 and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html.
154
155 Layout of prop. First is a header:
156
157 bytes type what
158 ------------------------------------
159 1 CARD8 byte-order
160 3 unused
161 4 CARD32 SERIAL
162 4 CARD32 N_SETTINGS
163
164 Then N_SETTINGS records, with header:
165
166 bytes type what
167 ------------------------------------
168 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color).
169 1 unused
170 2 CARD16 n == name-length
171 n STRING8 name
172 p unused, p=pad_to_even_4(n)
173 4 CARD32 last-change-serial
174
175 and then the value, For string:
176
177 bytes type what
178 ------------------------------------
179 4 CARD32 n = value-length
180 n STRING8 value
181 p unused, p=pad_to_even_4(n)
182
183 For integer:
184
185 bytes type what
186 ------------------------------------
187 4 INT32 value
188
189 For RGB color:
190
191 bytes type what
192 ------------------------------------
193 2 CARD16 red
194 2 CARD16 blue
195 2 CARD16 green
196 2 CARD16 alpha
197
198*/
199
200static int
201parse_xft_settings (prop, bytes, settings)
202 unsigned char *prop;
203 unsigned long bytes;
204 struct xsettings *settings;
205{
206 Lisp_Object byteorder = Fbyteorder ();
207 int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst;
208 int that_bo = prop[0];
209 CARD32 n_settings;
210 int bytes_parsed = 0;
211 int settings_seen = 0;
212 int i = 0;
213
214 /* First 4 bytes is a serial number, skip that. */
215
216 if (bytes < 12) return BadLength;
217 memcpy (&n_settings, prop+8, 4);
218 if (my_bo != that_bo) n_settings = SWAP32 (n_settings);
219 bytes_parsed = 12;
220
221 memset (settings, 0, sizeof (*settings));
222
223 while (bytes_parsed+4 < bytes && settings_seen < 6
224 && i < n_settings)
225 {
226 int type = prop[bytes_parsed++];
227 CARD16 nlen;
228 CARD32 vlen, ival = 0;
229 char name[128]; /* The names we are looking for are not this long. */
230 char sval[128]; /* The values we are looking for are not this long. */
231 int is_xft;
232 int to_cpy;
233
234 sval[0] = '\0';
235 ++i;
236 ++bytes_parsed; /* Padding */
237
238 memcpy (&nlen, prop+bytes_parsed, 2);
239 bytes_parsed += 2;
240 if (my_bo != that_bo) nlen = SWAP16 (nlen);
241 if (bytes_parsed+nlen > bytes) return BadLength;
242 to_cpy = nlen > 127 ? 127 : nlen;
243 memcpy (name, prop+bytes_parsed, to_cpy);
244 name[to_cpy] = '\0';
245
246 bytes_parsed += nlen;
247 bytes_parsed = PAD (bytes_parsed);
248
249 bytes_parsed += 4; /* Skip serial for this value */
250 if (bytes_parsed > bytes) return BadLength;
251
252 is_xft = nlen > 6 && strncmp (name, "Xft/", 4) == 0;
253
254 switch (type)
255 {
256 case 0: /* Integer */
257 if (bytes_parsed+4 > bytes) return BadLength;
258 if (is_xft)
259 {
260 memcpy (&ival, prop+bytes_parsed, 4);
261 if (my_bo != that_bo) ival = SWAP32 (ival);
262 }
263 bytes_parsed += 4;
264 break;
265
266 case 1: /* String */
267 if (bytes_parsed+4 > bytes) return BadLength;
268 memcpy (&vlen, prop+bytes_parsed, 4);
269 bytes_parsed += 4;
270 if (my_bo != that_bo) vlen = SWAP32 (vlen);
271 if (is_xft)
272 {
273 to_cpy = vlen > 127 ? 127 : vlen;
274 memcpy (sval, prop+bytes_parsed, to_cpy);
275 sval[to_cpy] = '\0';
276 }
277 bytes_parsed += vlen;
278 bytes_parsed = PAD (bytes_parsed);
279 break;
280
281 case 2: /* RGB value */
282 /* No need to parse this */
283 if (bytes_parsed+8 > bytes) return BadLength;
284 bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */
285 break;
286
287 default: /* Parse Error */
288 return BadValue;
289 }
290
291 if (is_xft)
292 {
293 ++settings_seen;
294 if (strcmp (name, "Xft/Antialias") == 0)
295 settings->aa = ival != 0;
296 else if (strcmp (name, "Xft/Hinting") == 0)
297 settings->hinting = ival != 0;
298 else if (strcmp (name, "Xft/HintStyle") == 0)
299 {
300 if (strcmp (sval, "hintnone") == 0)
301 settings->hintstyle = FC_HINT_NONE;
302 else if (strcmp (sval, "hintslight") == 0)
303 settings->hintstyle = FC_HINT_SLIGHT;
304 else if (strcmp (sval, "hintmedium") == 0)
305 settings->hintstyle = FC_HINT_MEDIUM;
306 else if (strcmp (sval, "hintfull") == 0)
307 settings->hintstyle = FC_HINT_FULL;
308 }
309 else if (strcmp (name, "Xft/RGBA") == 0)
310 {
311 if (strcmp (sval, "none") == 0)
312 settings->rgba = FC_RGBA_NONE;
313 else if (strcmp (sval, "rgb") == 0)
314 settings->rgba = FC_RGBA_RGB;
315 else if (strcmp (sval, "bgr") == 0)
316 settings->rgba = FC_RGBA_BGR;
317 else if (strcmp (sval, "vrgb") == 0)
318 settings->rgba = FC_RGBA_VRGB;
319 else if (strcmp (sval, "vbgr") == 0)
320 settings->rgba = FC_RGBA_VBGR;
321 }
322 else if (strcmp (name, "Xft/DPI") == 0)
323 settings->dpi = (double)ival/1024.0;
324 else if (strcmp (name, "Xft/lcdfilter") == 0)
325 {
326 if (strcmp (sval, "none") == 0)
327 settings->lcdfilter = FC_LCD_NONE;
328 else if (strcmp (sval, "lcddefault") == 0)
329 settings->lcdfilter = FC_LCD_DEFAULT;
330 }
331 }
332 }
333
334 return Success;
335}
336
337static int
338read_xft_settings (dpyinfo, settings)
339 struct x_display_info *dpyinfo;
340 struct xsettings *settings;
341{
342 long long_len;
343 Atom act_type;
344 int act_form;
345 unsigned long nitems, bytes_after;
346 unsigned char *prop = NULL;
347 Display *dpy = dpyinfo->display;
348 int rc;
349
350 x_catch_errors (dpy);
351 rc = XGetWindowProperty (dpy,
352 dpyinfo->xsettings_window,
353 dpyinfo->Xatom_xsettings_prop,
354 0, LONG_MAX, False, AnyPropertyType,
355 &act_type, &act_form, &nitems, &bytes_after,
356 &prop);
357
358 if (rc == Success && prop != NULL && act_form == 8 && nitems > 0
359 && act_type == dpyinfo->Xatom_xsettings_prop)
360 rc = parse_xft_settings (prop, nitems, settings);
361
362 XFree (prop);
363
364 x_uncatch_errors ();
365
366 return rc == Success;
367}
368
369static void
370apply_xft_settings (dpyinfo, send_event_p)
371 struct x_display_info *dpyinfo;
372 int send_event_p;
373{
374 FcPattern *pat;
375 struct xsettings settings, oldsettings;
376 int changed = 0;
377
378 if (!read_xft_settings (dpyinfo, &settings))
379 return;
380
381 memset (&oldsettings, 0, sizeof (oldsettings));
382
383 pat = FcPatternCreate ();
384 XftDefaultSubstitute (dpyinfo->display,
385 XScreenNumberOfScreen (dpyinfo->screen),
386 pat);
387 FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa);
388 FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting);
389 FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle);
390 FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter);
391 FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba);
392 FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi);
393
394 if (oldsettings.aa != settings.aa)
395 {
396 FcPatternDel (pat, FC_ANTIALIAS);
397 FcPatternAddBool (pat, FC_ANTIALIAS, settings.aa);
398 ++changed;
399 }
400 if (oldsettings.hinting != settings.hinting)
401 {
402 FcPatternDel (pat, FC_HINTING);
403 FcPatternAddBool (pat, FC_HINTING, settings.hinting);
404 ++changed;
405 }
406 if (oldsettings.rgba != settings.rgba)
407 {
408 FcPatternDel (pat, FC_RGBA);
409 FcPatternAddInteger (pat, FC_RGBA, settings.rgba);
410 ++changed;
411 }
a6eb20d8 412 /* Older fontconfig versions don't have FC_LCD_FILTER. */
637fa988
JD
413 if (oldsettings.lcdfilter != settings.lcdfilter)
414 {
415 FcPatternDel (pat, FC_LCD_FILTER);
416 FcPatternAddInteger (pat, FC_LCD_FILTER, settings.lcdfilter);
417 ++changed;
418 }
419 if (oldsettings.hintstyle != settings.hintstyle)
420 {
421 FcPatternDel (pat, FC_HINT_STYLE);
422 FcPatternAddInteger (pat, FC_HINT_STYLE, settings.hintstyle);
423 ++changed;
424 }
425 if (oldsettings.dpi != settings.dpi)
426 {
427 Lisp_Object frame, tail;
428
429 FcPatternDel (pat, FC_DPI);
430 FcPatternAddDouble (pat, FC_DPI, settings.dpi);
431 ++changed;
432
433 /* Change the DPI on this display and all frames on the display. */
434 dpyinfo->resy = dpyinfo->resx = settings.dpi;
435 FOR_EACH_FRAME (tail, frame)
436 if (FRAME_X_P (XFRAME (frame))
437 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
438 XFRAME (frame)->resy = XFRAME (frame)->resx = settings.dpi;
439 }
440
441 if (changed)
442 {
443 XftDefaultSet (dpyinfo->display, pat);
444 if (send_event_p)
445 store_font_changed_event (Qfont_render,
446 XCAR (dpyinfo->name_list_element));
447 }
448 else
449 FcPatternDestroy (pat);
450}
451
452#endif /* HAVE_XFT */
453
454void
455xft_settings_event (dpyinfo, event)
456 struct x_display_info *dpyinfo;
457 XEvent *event;
458{
459#ifdef HAVE_XFT
460 int check_window_p = 0;
461
462 switch (event->type)
463 {
464 case DestroyNotify:
465 if (dpyinfo->xsettings_window == event->xany.window)
466 check_window_p = 1;
467 break;
468
469 case ClientMessage:
470 if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr
471 && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel
472 && event->xclient.window == dpyinfo->root_window)
473 check_window_p = 1;
474 break;
475
476 case PropertyNotify:
477 if (event->xproperty.window == dpyinfo->xsettings_window
478 && event->xproperty.state == PropertyNewValue
479 && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop)
480 {
481 apply_xft_settings (dpyinfo, True);
482 }
483 break;
484 }
485
486 if (check_window_p)
487 {
488 dpyinfo->xsettings_window = None;
489 get_prop_window (dpyinfo);
490 if (dpyinfo->xsettings_window != None)
491 apply_xft_settings (dpyinfo, True);
492 }
493#endif /* HAVE_XFT */
494}
495
496
497static void
498init_gconf ()
499{
500#ifdef HAVE_GCONF
501 int i;
502 char *s;
503 /* Should be enough, this is called at startup */
504#define N_FDS 1024
505 int fd_before[N_FDS], fd_before1[N_FDS];
506 int dummy, n_fds;
507 GPollFD gfds[N_FDS];
508
509 /* To find out which filedecriptors GConf uses, check before and after.
510 If we do not do this, GConf changes will only happen when Emacs gets
511 an X event. */
512 memset (fd_before, 0, sizeof (fd_before));
513 n_fds = g_main_context_query (g_main_context_default (),
514 G_PRIORITY_LOW,
515 &dummy,
516 gfds,
517 N_FDS);
518 for (i = 0; i < n_fds; ++i)
519 if (gfds[i].fd < N_FDS && gfds[i].fd > 0 && gfds[i].events > 0)
520 fd_before[gfds[i].fd] = 1;
521
522 g_type_init ();
523 gconf_client = gconf_client_get_default ();
524 s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL);
525 if (s)
526 {
527 current_mono_font = xstrdup (s);
528 g_free (s);
529 }
530 gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE);
531 gconf_client_add_dir (gconf_client,
532 SYSTEM_MONO_FONT,
533 GCONF_CLIENT_PRELOAD_ONELEVEL,
534 NULL);
535 gconf_client_notify_add (gconf_client,
536 SYSTEM_MONO_FONT,
537 something_changedCB,
538 NULL, NULL, NULL);
539 n_fds = g_main_context_query (g_main_context_default (),
540 G_PRIORITY_LOW,
541 &dummy,
542 gfds,
543 N_FDS);
544
545 for (i = 0; i < n_fds; ++i)
546 if (gfds[i].fd < N_FDS && gfds[i].fd > 0 && gfds[i].events > 0
547 && !fd_before[gfds[i].fd])
548 {
549#ifdef F_SETOWN
550 fcntl (i, F_SETOWN, getpid ());
551#endif /* ! defined (F_SETOWN) */
552
553#ifdef SIGIO
554 if (interrupt_input)
555 init_sigio (i);
556#endif /* ! defined (SIGIO) */
557 }
558#endif /* HAVE_GCONF */
559}
560
561static void
562init_xfd_settings (dpyinfo)
563 struct x_display_info *dpyinfo;
564{
565#ifdef HAVE_XFT
566 char sel[64];
567 Display *dpy = dpyinfo->display;
568
569 BLOCK_INPUT;
570
571 sprintf (sel, "_XSETTINGS_S%d", XScreenNumberOfScreen (dpyinfo->screen));
572 dpyinfo->Xatom_xsettings_sel = XInternAtom (dpy, sel, False);
573 dpyinfo->Xatom_xsettings_prop = XInternAtom (dpy,
574 "_XSETTINGS_SETTINGS",
575 False);
576 dpyinfo->Xatom_xsettings_mgr = XInternAtom (dpy, "MANAGER", False);
577
578 /* Select events so we can detect client messages sent when selection
579 owner changes. */
580 XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask);
581
582 get_prop_window (dpyinfo);
583 if (dpyinfo->xsettings_window != None)
584 apply_xft_settings (dpyinfo, False);
585
586 UNBLOCK_INPUT;
587
588#else /* ! HAVE_XFT */
589
590 dpyinfo->Xatom_xsettings_sel = None;
591 dpyinfo->Xatom_xsettings_prop = None;
592 dpyinfo->Xatom_xsettings_mgr = None;
593 dpyinfo->xsettings_window = None;
594
595#endif /* ! HAVE_XFT */
596}
597
598void
599xsettings_initialize (dpyinfo)
600 struct x_display_info *dpyinfo;
601{
602 if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo;
603 init_gconf ();
604 init_xfd_settings (dpyinfo);
605}
606
607
608DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font,
609 0, 0, 0,
610 doc: /* Get the system default monospaced font. */)
611 ()
612{
613 return current_mono_font
614 ? make_string (current_mono_font, strlen (current_mono_font))
615 : Qnil;
616}
617
618void
619syms_of_xsettings ()
620{
621 current_mono_font = NULL;
622 first_dpyinfo = NULL;
623#ifdef HAVE_GCONF
624 gconf_client = NULL;
625#endif
626
627 Qfont_name = intern_c_string ("font-name");
628 staticpro (&Qfont_name);
629 Qfont_render = intern_c_string ("font-render");
630 staticpro (&Qfont_render);
631 defsubr (&Sfont_get_system_font);
632
633#ifdef HAVE_GCONF
634 Fprovide (intern_c_string ("system-font-setting"), Qnil);
635#endif
636#ifdef HAVE_XFT
637 Fprovide (intern_c_string ("font-render-setting"), Qnil);
638#endif
639}