Commit | Line | Data |
---|---|---|
4ff155dd GM |
1 | /* Functions for handling font and other changes dynamically. |
2 | ||
3 | Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc. | |
637fa988 JD |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. */ | |
19 | ||
08a494a3 | 20 | #include <config.h> |
210af043 | 21 | #include <limits.h> |
637fa988 JD |
22 | #include <setjmp.h> |
23 | #include <fcntl.h> | |
24 | #include "lisp.h" | |
25 | #include "xterm.h" | |
26 | #include "xsettings.h" | |
27 | #include "frame.h" | |
b87dd913 | 28 | #include "keyboard.h" |
637fa988 JD |
29 | #include "blockinput.h" |
30 | #include "termhooks.h" | |
31 | #include "termopts.h" | |
32 | ||
33 | #include <X11/Xproto.h> | |
34 | ||
35 | #ifdef HAVE_GCONF | |
36 | #include <gconf/gconf-client.h> | |
37 | #endif | |
38 | #ifdef HAVE_XFT | |
39 | #include <X11/Xft/Xft.h> | |
40 | #endif | |
41 | ||
42 | static char *current_mono_font; | |
99852628 | 43 | static char *current_font; |
637fa988 | 44 | static struct x_display_info *first_dpyinfo; |
f904c0f9 JD |
45 | static Lisp_Object Qmonospace_font_name, Qfont_name, Qfont_render, |
46 | Qtool_bar_style; | |
dfb3c4c6 | 47 | static int use_system_font; |
67477f30 | 48 | static Lisp_Object Vxft_settings; |
f904c0f9 | 49 | static Lisp_Object current_tool_bar_style; |
637fa988 JD |
50 | |
51 | #ifdef HAVE_GCONF | |
52 | static GConfClient *gconf_client; | |
53 | #endif | |
54 | ||
55 | ||
56 | static void | |
971de7fb | 57 | store_config_changed_event (Lisp_Object arg, Lisp_Object display_name) |
637fa988 JD |
58 | { |
59 | struct input_event event; | |
60 | EVENT_INIT (event); | |
61 | event.kind = CONFIG_CHANGED_EVENT; | |
62 | event.frame_or_window = display_name; | |
63 | event.arg = arg; | |
64 | kbd_buffer_store_event (&event); | |
65 | } | |
66 | ||
f904c0f9 JD |
67 | #define XSETTINGS_FONT_NAME "Gtk/FontName" |
68 | #define XSETTINGS_TOOL_BAR_STYLE "Gtk/ToolbarStyle" | |
0269ef77 | 69 | |
f904c0f9 JD |
70 | enum { |
71 | SEEN_AA = 0x01, | |
72 | SEEN_HINTING = 0x02, | |
73 | SEEN_RGBA = 0x04, | |
74 | SEEN_LCDFILTER = 0x08, | |
75 | SEEN_HINTSTYLE = 0x10, | |
76 | SEEN_DPI = 0x20, | |
77 | SEEN_FONT = 0x40, | |
78 | SEEN_TB_STYLE = 0x80, | |
79 | }; | |
80 | struct xsettings | |
81 | { | |
82 | #ifdef HAVE_XFT | |
83 | FcBool aa, hinting; | |
84 | int rgba, lcdfilter, hintstyle; | |
85 | double dpi; | |
86 | #endif | |
87 | ||
88 | char *font; | |
89 | char *tb_style; | |
90 | ||
91 | unsigned seen; | |
92 | }; | |
93 | ||
31a01b90 JD |
94 | #ifdef HAVE_GCONF |
95 | ||
96 | #define SYSTEM_MONO_FONT "/desktop/gnome/interface/monospace_font_name" | |
97 | #define SYSTEM_FONT "/desktop/gnome/interface/font_name" | |
98 | ||
637fa988 JD |
99 | /* Callback called when something changed in GConf that we care about, |
100 | that is SYSTEM_MONO_FONT. */ | |
101 | ||
102 | static void | |
e4c8d29a J |
103 | something_changedCB (GConfClient *client, |
104 | guint cnxn_id, | |
105 | GConfEntry *entry, | |
106 | gpointer user_data) | |
637fa988 JD |
107 | { |
108 | GConfValue *v = gconf_entry_get_value (entry); | |
109 | ||
110 | if (!v) return; | |
111 | if (v->type == GCONF_VALUE_STRING) | |
112 | { | |
113 | const char *value = gconf_value_get_string (v); | |
637fa988 | 114 | if (current_mono_font != NULL && strcmp (value, current_mono_font) == 0) |
872870b2 | 115 | return; /* No change. */ |
637fa988 JD |
116 | |
117 | xfree (current_mono_font); | |
118 | current_mono_font = xstrdup (value); | |
119 | } | |
120 | ||
121 | ||
122 | if (first_dpyinfo != NULL) | |
123 | { | |
124 | /* Check if display still open */ | |
125 | struct x_display_info *dpyinfo; | |
126 | int found = 0; | |
127 | for (dpyinfo = x_display_list; !found && dpyinfo; dpyinfo = dpyinfo->next) | |
128 | found = dpyinfo == first_dpyinfo; | |
129 | ||
dfb3c4c6 | 130 | if (found && use_system_font) |
f904c0f9 JD |
131 | store_config_changed_event (Qmonospace_font_name, |
132 | XCAR (first_dpyinfo->name_list_element)); | |
637fa988 JD |
133 | } |
134 | } | |
135 | #endif /* HAVE_GCONF */ | |
136 | ||
137 | #ifdef HAVE_XFT | |
138 | ||
21050de1 JD |
139 | /* Older fontconfig versions don't have FC_LCD_*. */ |
140 | #ifndef FC_LCD_NONE | |
141 | #define FC_LCD_NONE 0 | |
142 | #endif | |
143 | #ifndef FC_LCD_DEFAULT | |
144 | #define FC_LCD_DEFAULT 1 | |
145 | #endif | |
146 | #ifndef FC_LCD_FILTER | |
147 | #define FC_LCD_FILTER "lcdfilter" | |
148 | #endif | |
149 | ||
f904c0f9 JD |
150 | #endif /* HAVE_XFT */ |
151 | ||
637fa988 JD |
152 | /* Find the window that contains the XSETTINGS property values. */ |
153 | ||
154 | static void | |
971de7fb | 155 | get_prop_window (struct x_display_info *dpyinfo) |
637fa988 JD |
156 | { |
157 | Display *dpy = dpyinfo->display; | |
158 | ||
159 | XGrabServer (dpy); | |
160 | dpyinfo->xsettings_window = XGetSelectionOwner (dpy, | |
161 | dpyinfo->Xatom_xsettings_sel); | |
162 | if (dpyinfo->xsettings_window != None) | |
163 | /* Select events so we can detect if window is deleted or if settings | |
164 | are changed. */ | |
165 | XSelectInput (dpy, dpyinfo->xsettings_window, | |
166 | PropertyChangeMask|StructureNotifyMask); | |
167 | ||
168 | XUngrabServer (dpy); | |
169 | } | |
170 | ||
637fa988 JD |
171 | #define SWAP32(nr) (((nr) << 24) | (((nr) << 8) & 0xff0000) \ |
172 | | (((nr) >> 8) & 0xff00) | ((nr) >> 24)) | |
173 | #define SWAP16(nr) (((nr) << 8) | ((nr) >> 8)) | |
174 | #define PAD(nr) (((nr) + 3) & ~3) | |
175 | ||
176 | /* Parse xsettings and extract those that deal with Xft. | |
177 | See http://freedesktop.org/wiki/Specifications/XSettingsRegistry | |
178 | and http://standards.freedesktop.org/xsettings-spec/xsettings-spec-0.5.html. | |
179 | ||
180 | Layout of prop. First is a header: | |
181 | ||
182 | bytes type what | |
183 | ------------------------------------ | |
184 | 1 CARD8 byte-order | |
185 | 3 unused | |
186 | 4 CARD32 SERIAL | |
187 | 4 CARD32 N_SETTINGS | |
188 | ||
189 | Then N_SETTINGS records, with header: | |
190 | ||
191 | bytes type what | |
192 | ------------------------------------ | |
193 | 1 SETTING_TYPE type (0 = integer, 1 = string, 2 RGB color). | |
194 | 1 unused | |
195 | 2 CARD16 n == name-length | |
196 | n STRING8 name | |
197 | p unused, p=pad_to_even_4(n) | |
198 | 4 CARD32 last-change-serial | |
199 | ||
200 | and then the value, For string: | |
201 | ||
202 | bytes type what | |
203 | ------------------------------------ | |
204 | 4 CARD32 n = value-length | |
205 | n STRING8 value | |
206 | p unused, p=pad_to_even_4(n) | |
207 | ||
208 | For integer: | |
209 | ||
210 | bytes type what | |
211 | ------------------------------------ | |
212 | 4 INT32 value | |
213 | ||
214 | For RGB color: | |
215 | ||
216 | bytes type what | |
217 | ------------------------------------ | |
218 | 2 CARD16 red | |
219 | 2 CARD16 blue | |
220 | 2 CARD16 green | |
221 | 2 CARD16 alpha | |
222 | ||
3c055b77 | 223 | Returns non-zero if some Xft settings was seen, zero otherwise. |
637fa988 JD |
224 | */ |
225 | ||
226 | static int | |
e4c8d29a J |
227 | parse_settings (unsigned char *prop, |
228 | long unsigned int bytes, | |
229 | struct xsettings *settings) | |
637fa988 JD |
230 | { |
231 | Lisp_Object byteorder = Fbyteorder (); | |
232 | int my_bo = XFASTINT (byteorder) == 'B' ? MSBFirst : LSBFirst; | |
233 | int that_bo = prop[0]; | |
234 | CARD32 n_settings; | |
235 | int bytes_parsed = 0; | |
236 | int settings_seen = 0; | |
237 | int i = 0; | |
238 | ||
239 | /* First 4 bytes is a serial number, skip that. */ | |
240 | ||
241 | if (bytes < 12) return BadLength; | |
242 | memcpy (&n_settings, prop+8, 4); | |
243 | if (my_bo != that_bo) n_settings = SWAP32 (n_settings); | |
244 | bytes_parsed = 12; | |
245 | ||
246 | memset (settings, 0, sizeof (*settings)); | |
247 | ||
99852628 | 248 | while (bytes_parsed+4 < bytes && settings_seen < 7 |
637fa988 JD |
249 | && i < n_settings) |
250 | { | |
251 | int type = prop[bytes_parsed++]; | |
252 | CARD16 nlen; | |
253 | CARD32 vlen, ival = 0; | |
254 | char name[128]; /* The names we are looking for are not this long. */ | |
255 | char sval[128]; /* The values we are looking for are not this long. */ | |
99852628 | 256 | int want_this; |
637fa988 JD |
257 | int to_cpy; |
258 | ||
259 | sval[0] = '\0'; | |
260 | ++i; | |
261 | ++bytes_parsed; /* Padding */ | |
262 | ||
263 | memcpy (&nlen, prop+bytes_parsed, 2); | |
264 | bytes_parsed += 2; | |
265 | if (my_bo != that_bo) nlen = SWAP16 (nlen); | |
266 | if (bytes_parsed+nlen > bytes) return BadLength; | |
267 | to_cpy = nlen > 127 ? 127 : nlen; | |
268 | memcpy (name, prop+bytes_parsed, to_cpy); | |
269 | name[to_cpy] = '\0'; | |
270 | ||
271 | bytes_parsed += nlen; | |
272 | bytes_parsed = PAD (bytes_parsed); | |
273 | ||
274 | bytes_parsed += 4; /* Skip serial for this value */ | |
275 | if (bytes_parsed > bytes) return BadLength; | |
276 | ||
f904c0f9 JD |
277 | want_this = |
278 | #ifdef HAVE_XFT | |
279 | (nlen > 6 && strncmp (name, "Xft/", 4) == 0) | |
280 | || | |
281 | #endif | |
282 | (strcmp (XSETTINGS_FONT_NAME, name) == 0) | |
283 | || (strcmp (XSETTINGS_TOOL_BAR_STYLE, name) == 0); | |
637fa988 JD |
284 | |
285 | switch (type) | |
286 | { | |
287 | case 0: /* Integer */ | |
288 | if (bytes_parsed+4 > bytes) return BadLength; | |
99852628 | 289 | if (want_this) |
637fa988 JD |
290 | { |
291 | memcpy (&ival, prop+bytes_parsed, 4); | |
292 | if (my_bo != that_bo) ival = SWAP32 (ival); | |
293 | } | |
294 | bytes_parsed += 4; | |
295 | break; | |
296 | ||
297 | case 1: /* String */ | |
298 | if (bytes_parsed+4 > bytes) return BadLength; | |
299 | memcpy (&vlen, prop+bytes_parsed, 4); | |
300 | bytes_parsed += 4; | |
301 | if (my_bo != that_bo) vlen = SWAP32 (vlen); | |
99852628 | 302 | if (want_this) |
637fa988 JD |
303 | { |
304 | to_cpy = vlen > 127 ? 127 : vlen; | |
305 | memcpy (sval, prop+bytes_parsed, to_cpy); | |
306 | sval[to_cpy] = '\0'; | |
307 | } | |
308 | bytes_parsed += vlen; | |
309 | bytes_parsed = PAD (bytes_parsed); | |
310 | break; | |
311 | ||
312 | case 2: /* RGB value */ | |
313 | /* No need to parse this */ | |
314 | if (bytes_parsed+8 > bytes) return BadLength; | |
315 | bytes_parsed += 8; /* 4 values (r, b, g, alpha), 2 bytes each. */ | |
316 | break; | |
317 | ||
318 | default: /* Parse Error */ | |
319 | return BadValue; | |
320 | } | |
321 | ||
99852628 | 322 | if (want_this) |
637fa988 JD |
323 | { |
324 | ++settings_seen; | |
f904c0f9 JD |
325 | if (strcmp (name, XSETTINGS_FONT_NAME) == 0) |
326 | { | |
327 | settings->font = xstrdup (sval); | |
328 | settings->seen |= SEEN_FONT; | |
329 | } | |
330 | else if (strcmp (name, XSETTINGS_TOOL_BAR_STYLE) == 0) | |
331 | { | |
332 | settings->tb_style = xstrdup (sval); | |
333 | settings->seen |= SEEN_TB_STYLE; | |
334 | } | |
335 | #ifdef HAVE_XFT | |
336 | else if (strcmp (name, "Xft/Antialias") == 0) | |
3c055b77 JD |
337 | { |
338 | settings->seen |= SEEN_AA; | |
339 | settings->aa = ival != 0; | |
340 | } | |
637fa988 | 341 | else if (strcmp (name, "Xft/Hinting") == 0) |
3c055b77 JD |
342 | { |
343 | settings->seen |= SEEN_HINTING; | |
344 | settings->hinting = ival != 0; | |
345 | } | |
637fa988 JD |
346 | else if (strcmp (name, "Xft/HintStyle") == 0) |
347 | { | |
3c055b77 | 348 | settings->seen |= SEEN_HINTSTYLE; |
637fa988 JD |
349 | if (strcmp (sval, "hintnone") == 0) |
350 | settings->hintstyle = FC_HINT_NONE; | |
351 | else if (strcmp (sval, "hintslight") == 0) | |
352 | settings->hintstyle = FC_HINT_SLIGHT; | |
353 | else if (strcmp (sval, "hintmedium") == 0) | |
354 | settings->hintstyle = FC_HINT_MEDIUM; | |
355 | else if (strcmp (sval, "hintfull") == 0) | |
356 | settings->hintstyle = FC_HINT_FULL; | |
3c055b77 JD |
357 | else |
358 | settings->seen &= ~SEEN_HINTSTYLE; | |
637fa988 JD |
359 | } |
360 | else if (strcmp (name, "Xft/RGBA") == 0) | |
361 | { | |
3c055b77 | 362 | settings->seen |= SEEN_RGBA; |
637fa988 JD |
363 | if (strcmp (sval, "none") == 0) |
364 | settings->rgba = FC_RGBA_NONE; | |
365 | else if (strcmp (sval, "rgb") == 0) | |
366 | settings->rgba = FC_RGBA_RGB; | |
367 | else if (strcmp (sval, "bgr") == 0) | |
368 | settings->rgba = FC_RGBA_BGR; | |
369 | else if (strcmp (sval, "vrgb") == 0) | |
370 | settings->rgba = FC_RGBA_VRGB; | |
371 | else if (strcmp (sval, "vbgr") == 0) | |
372 | settings->rgba = FC_RGBA_VBGR; | |
3c055b77 JD |
373 | else |
374 | settings->seen &= ~SEEN_RGBA; | |
637fa988 JD |
375 | } |
376 | else if (strcmp (name, "Xft/DPI") == 0) | |
3c055b77 JD |
377 | { |
378 | settings->seen |= SEEN_DPI; | |
379 | settings->dpi = (double)ival/1024.0; | |
380 | } | |
637fa988 JD |
381 | else if (strcmp (name, "Xft/lcdfilter") == 0) |
382 | { | |
3c055b77 | 383 | settings->seen |= SEEN_LCDFILTER; |
637fa988 JD |
384 | if (strcmp (sval, "none") == 0) |
385 | settings->lcdfilter = FC_LCD_NONE; | |
386 | else if (strcmp (sval, "lcddefault") == 0) | |
387 | settings->lcdfilter = FC_LCD_DEFAULT; | |
3c055b77 JD |
388 | else |
389 | settings->seen &= ~SEEN_LCDFILTER; | |
637fa988 | 390 | } |
f904c0f9 | 391 | #endif /* HAVE_XFT */ |
637fa988 JD |
392 | } |
393 | } | |
394 | ||
3c055b77 | 395 | return settings_seen; |
637fa988 JD |
396 | } |
397 | ||
398 | static int | |
971de7fb | 399 | read_settings (struct x_display_info *dpyinfo, struct xsettings *settings) |
637fa988 | 400 | { |
637fa988 JD |
401 | Atom act_type; |
402 | int act_form; | |
403 | unsigned long nitems, bytes_after; | |
404 | unsigned char *prop = NULL; | |
405 | Display *dpy = dpyinfo->display; | |
406 | int rc; | |
407 | ||
408 | x_catch_errors (dpy); | |
409 | rc = XGetWindowProperty (dpy, | |
410 | dpyinfo->xsettings_window, | |
411 | dpyinfo->Xatom_xsettings_prop, | |
412 | 0, LONG_MAX, False, AnyPropertyType, | |
413 | &act_type, &act_form, &nitems, &bytes_after, | |
414 | &prop); | |
415 | ||
416 | if (rc == Success && prop != NULL && act_form == 8 && nitems > 0 | |
417 | && act_type == dpyinfo->Xatom_xsettings_prop) | |
f904c0f9 | 418 | rc = parse_settings (prop, nitems, settings); |
637fa988 JD |
419 | |
420 | XFree (prop); | |
421 | ||
422 | x_uncatch_errors (); | |
423 | ||
3c055b77 | 424 | return rc != 0; |
637fa988 JD |
425 | } |
426 | ||
581e51e8 | 427 | |
637fa988 | 428 | static void |
e4c8d29a J |
429 | apply_xft_settings (struct x_display_info *dpyinfo, |
430 | int send_event_p, | |
431 | struct xsettings *settings) | |
637fa988 | 432 | { |
f904c0f9 | 433 | #ifdef HAVE_XFT |
637fa988 | 434 | FcPattern *pat; |
f904c0f9 | 435 | struct xsettings oldsettings; |
637fa988 | 436 | int changed = 0; |
67477f30 | 437 | char buf[256]; |
637fa988 | 438 | |
637fa988 | 439 | memset (&oldsettings, 0, sizeof (oldsettings)); |
67477f30 | 440 | buf[0] = '\0'; |
637fa988 JD |
441 | pat = FcPatternCreate (); |
442 | XftDefaultSubstitute (dpyinfo->display, | |
443 | XScreenNumberOfScreen (dpyinfo->screen), | |
444 | pat); | |
445 | FcPatternGetBool (pat, FC_ANTIALIAS, 0, &oldsettings.aa); | |
446 | FcPatternGetBool (pat, FC_HINTING, 0, &oldsettings.hinting); | |
447 | FcPatternGetInteger (pat, FC_HINT_STYLE, 0, &oldsettings.hintstyle); | |
448 | FcPatternGetInteger (pat, FC_LCD_FILTER, 0, &oldsettings.lcdfilter); | |
449 | FcPatternGetInteger (pat, FC_RGBA, 0, &oldsettings.rgba); | |
450 | FcPatternGetDouble (pat, FC_DPI, 0, &oldsettings.dpi); | |
451 | ||
f904c0f9 | 452 | if ((settings->seen & SEEN_AA) != 0 && oldsettings.aa != settings->aa) |
637fa988 JD |
453 | { |
454 | FcPatternDel (pat, FC_ANTIALIAS); | |
f904c0f9 | 455 | FcPatternAddBool (pat, FC_ANTIALIAS, settings->aa); |
637fa988 | 456 | ++changed; |
f904c0f9 | 457 | oldsettings.aa = settings->aa; |
637fa988 | 458 | } |
67477f30 JD |
459 | sprintf (buf, "Antialias: %d", oldsettings.aa); |
460 | ||
f904c0f9 JD |
461 | if ((settings->seen & SEEN_HINTING) != 0 |
462 | && oldsettings.hinting != settings->hinting) | |
637fa988 JD |
463 | { |
464 | FcPatternDel (pat, FC_HINTING); | |
f904c0f9 | 465 | FcPatternAddBool (pat, FC_HINTING, settings->hinting); |
637fa988 | 466 | ++changed; |
f904c0f9 | 467 | oldsettings.hinting = settings->hinting; |
637fa988 | 468 | } |
67477f30 JD |
469 | if (strlen (buf) > 0) strcat (buf, ", "); |
470 | sprintf (buf+strlen (buf), "Hinting: %d", oldsettings.hinting); | |
f904c0f9 | 471 | if ((settings->seen & SEEN_RGBA) != 0 && oldsettings.rgba != settings->rgba) |
637fa988 JD |
472 | { |
473 | FcPatternDel (pat, FC_RGBA); | |
f904c0f9 JD |
474 | FcPatternAddInteger (pat, FC_RGBA, settings->rgba); |
475 | oldsettings.rgba = settings->rgba; | |
637fa988 JD |
476 | ++changed; |
477 | } | |
67477f30 JD |
478 | if (strlen (buf) > 0) strcat (buf, ", "); |
479 | sprintf (buf+strlen (buf), "RGBA: %d", oldsettings.rgba); | |
480 | ||
a6eb20d8 | 481 | /* Older fontconfig versions don't have FC_LCD_FILTER. */ |
f904c0f9 JD |
482 | if ((settings->seen & SEEN_LCDFILTER) != 0 |
483 | && oldsettings.lcdfilter != settings->lcdfilter) | |
637fa988 JD |
484 | { |
485 | FcPatternDel (pat, FC_LCD_FILTER); | |
f904c0f9 | 486 | FcPatternAddInteger (pat, FC_LCD_FILTER, settings->lcdfilter); |
637fa988 | 487 | ++changed; |
f904c0f9 | 488 | oldsettings.lcdfilter = settings->lcdfilter; |
637fa988 | 489 | } |
67477f30 JD |
490 | if (strlen (buf) > 0) strcat (buf, ", "); |
491 | sprintf (buf+strlen (buf), "LCDFilter: %d", oldsettings.lcdfilter); | |
492 | ||
f904c0f9 JD |
493 | if ((settings->seen & SEEN_HINTSTYLE) != 0 |
494 | && oldsettings.hintstyle != settings->hintstyle) | |
637fa988 JD |
495 | { |
496 | FcPatternDel (pat, FC_HINT_STYLE); | |
f904c0f9 | 497 | FcPatternAddInteger (pat, FC_HINT_STYLE, settings->hintstyle); |
637fa988 | 498 | ++changed; |
f904c0f9 | 499 | oldsettings.hintstyle = settings->hintstyle; |
637fa988 | 500 | } |
67477f30 JD |
501 | if (strlen (buf) > 0) strcat (buf, ", "); |
502 | sprintf (buf+strlen (buf), "Hintstyle: %d", oldsettings.hintstyle); | |
503 | ||
f904c0f9 JD |
504 | if ((settings->seen & SEEN_DPI) != 0 && oldsettings.dpi != settings->dpi |
505 | && settings->dpi > 0) | |
637fa988 JD |
506 | { |
507 | Lisp_Object frame, tail; | |
508 | ||
509 | FcPatternDel (pat, FC_DPI); | |
f904c0f9 | 510 | FcPatternAddDouble (pat, FC_DPI, settings->dpi); |
637fa988 | 511 | ++changed; |
f904c0f9 | 512 | oldsettings.dpi = settings->dpi; |
67477f30 | 513 | |
637fa988 | 514 | /* Change the DPI on this display and all frames on the display. */ |
f904c0f9 | 515 | dpyinfo->resy = dpyinfo->resx = settings->dpi; |
637fa988 JD |
516 | FOR_EACH_FRAME (tail, frame) |
517 | if (FRAME_X_P (XFRAME (frame)) | |
518 | && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo) | |
f904c0f9 | 519 | XFRAME (frame)->resy = XFRAME (frame)->resx = settings->dpi; |
637fa988 JD |
520 | } |
521 | ||
67477f30 JD |
522 | if (strlen (buf) > 0) strcat (buf, ", "); |
523 | sprintf (buf+strlen (buf), "DPI: %lf", oldsettings.dpi); | |
524 | ||
637fa988 JD |
525 | if (changed) |
526 | { | |
527 | XftDefaultSet (dpyinfo->display, pat); | |
528 | if (send_event_p) | |
f904c0f9 JD |
529 | store_config_changed_event (Qfont_render, |
530 | XCAR (dpyinfo->name_list_element)); | |
67477f30 | 531 | Vxft_settings = make_string (buf, strlen (buf)); |
637fa988 JD |
532 | } |
533 | else | |
534 | FcPatternDestroy (pat); | |
f904c0f9 | 535 | #endif /* HAVE_XFT */ |
637fa988 JD |
536 | } |
537 | ||
f904c0f9 | 538 | static void |
971de7fb | 539 | read_and_apply_settings (struct x_display_info *dpyinfo, int send_event_p) |
f904c0f9 JD |
540 | { |
541 | struct xsettings settings; | |
542 | Lisp_Object dpyname = XCAR (dpyinfo->name_list_element); | |
543 | ||
544 | if (!read_settings (dpyinfo, &settings)) | |
545 | return; | |
546 | ||
547 | apply_xft_settings (dpyinfo, True, &settings); | |
548 | if (settings.seen & SEEN_TB_STYLE) | |
549 | { | |
550 | Lisp_Object style = Qnil; | |
551 | if (strcmp (settings.tb_style, "both") == 0) | |
552 | style = Qboth; | |
553 | else if (strcmp (settings.tb_style, "both-horiz") == 0) | |
554 | style = Qboth_horiz; | |
555 | else if (strcmp (settings.tb_style, "icons") == 0) | |
556 | style = Qimage; | |
557 | else if (strcmp (settings.tb_style, "text") == 0) | |
558 | style = Qtext; | |
559 | if (!NILP (style) && !EQ (style, current_tool_bar_style)) | |
560 | { | |
561 | current_tool_bar_style = style; | |
562 | if (send_event_p) | |
563 | store_config_changed_event (Qtool_bar_style, dpyname); | |
564 | } | |
565 | free (settings.tb_style); | |
566 | } | |
567 | ||
568 | if (settings.seen & SEEN_FONT) | |
569 | { | |
a0d0b563 | 570 | if (!current_font || strcmp (current_font, settings.font) != 0) |
f904c0f9 JD |
571 | { |
572 | free (current_font); | |
573 | current_font = settings.font; | |
574 | if (send_event_p) | |
575 | store_config_changed_event (Qfont_name, dpyname); | |
576 | } | |
577 | else | |
578 | free (settings.font); | |
579 | } | |
580 | } | |
637fa988 JD |
581 | |
582 | void | |
971de7fb | 583 | xft_settings_event (struct x_display_info *dpyinfo, XEvent *event) |
637fa988 | 584 | { |
637fa988 | 585 | int check_window_p = 0; |
f904c0f9 | 586 | int apply_settings = 0; |
637fa988 JD |
587 | |
588 | switch (event->type) | |
589 | { | |
590 | case DestroyNotify: | |
591 | if (dpyinfo->xsettings_window == event->xany.window) | |
592 | check_window_p = 1; | |
593 | break; | |
594 | ||
595 | case ClientMessage: | |
596 | if (event->xclient.message_type == dpyinfo->Xatom_xsettings_mgr | |
597 | && event->xclient.data.l[1] == dpyinfo->Xatom_xsettings_sel | |
598 | && event->xclient.window == dpyinfo->root_window) | |
599 | check_window_p = 1; | |
600 | break; | |
601 | ||
602 | case PropertyNotify: | |
603 | if (event->xproperty.window == dpyinfo->xsettings_window | |
604 | && event->xproperty.state == PropertyNewValue | |
605 | && event->xproperty.atom == dpyinfo->Xatom_xsettings_prop) | |
f904c0f9 | 606 | apply_settings = 1; |
637fa988 JD |
607 | break; |
608 | } | |
609 | ||
f904c0f9 | 610 | |
637fa988 JD |
611 | if (check_window_p) |
612 | { | |
613 | dpyinfo->xsettings_window = None; | |
614 | get_prop_window (dpyinfo); | |
615 | if (dpyinfo->xsettings_window != None) | |
f904c0f9 | 616 | apply_settings = 1; |
637fa988 | 617 | } |
f904c0f9 JD |
618 | |
619 | if (apply_settings) | |
620 | read_and_apply_settings (dpyinfo, True); | |
637fa988 JD |
621 | } |
622 | ||
623 | ||
624 | static void | |
971de7fb | 625 | init_gconf (void) |
637fa988 | 626 | { |
0d1d0d26 | 627 | #if defined (HAVE_GCONF) && defined (HAVE_XFT) |
637fa988 | 628 | char *s; |
637fa988 | 629 | |
1e600395 | 630 | #ifdef HAVE_G_TYPE_INIT |
637fa988 | 631 | g_type_init (); |
1e600395 | 632 | #endif |
637fa988 JD |
633 | gconf_client = gconf_client_get_default (); |
634 | s = gconf_client_get_string (gconf_client, SYSTEM_MONO_FONT, NULL); | |
635 | if (s) | |
636 | { | |
637 | current_mono_font = xstrdup (s); | |
638 | g_free (s); | |
639 | } | |
99852628 JD |
640 | s = gconf_client_get_string (gconf_client, SYSTEM_FONT, NULL); |
641 | if (s) | |
642 | { | |
643 | current_font = xstrdup (s); | |
644 | g_free (s); | |
645 | } | |
637fa988 JD |
646 | gconf_client_set_error_handling (gconf_client, GCONF_CLIENT_HANDLE_NONE); |
647 | gconf_client_add_dir (gconf_client, | |
648 | SYSTEM_MONO_FONT, | |
649 | GCONF_CLIENT_PRELOAD_ONELEVEL, | |
650 | NULL); | |
651 | gconf_client_notify_add (gconf_client, | |
652 | SYSTEM_MONO_FONT, | |
653 | something_changedCB, | |
654 | NULL, NULL, NULL); | |
0d1d0d26 | 655 | #endif /* HAVE_GCONF && HAVE_XFT */ |
637fa988 JD |
656 | } |
657 | ||
658 | static void | |
971de7fb | 659 | init_xsettings (struct x_display_info *dpyinfo) |
637fa988 | 660 | { |
637fa988 JD |
661 | Display *dpy = dpyinfo->display; |
662 | ||
663 | BLOCK_INPUT; | |
664 | ||
637fa988 JD |
665 | /* Select events so we can detect client messages sent when selection |
666 | owner changes. */ | |
667 | XSelectInput (dpy, dpyinfo->root_window, StructureNotifyMask); | |
668 | ||
669 | get_prop_window (dpyinfo); | |
670 | if (dpyinfo->xsettings_window != None) | |
f904c0f9 | 671 | read_and_apply_settings (dpyinfo, False); |
637fa988 JD |
672 | |
673 | UNBLOCK_INPUT; | |
637fa988 JD |
674 | } |
675 | ||
676 | void | |
971de7fb | 677 | xsettings_initialize (struct x_display_info *dpyinfo) |
637fa988 JD |
678 | { |
679 | if (first_dpyinfo == NULL) first_dpyinfo = dpyinfo; | |
680 | init_gconf (); | |
f904c0f9 | 681 | init_xsettings (dpyinfo); |
637fa988 JD |
682 | } |
683 | ||
0d1d0d26 | 684 | const char * |
971de7fb | 685 | xsettings_get_system_font (void) |
0d1d0d26 JD |
686 | { |
687 | return current_mono_font; | |
688 | } | |
637fa988 | 689 | |
99852628 | 690 | const char * |
971de7fb | 691 | xsettings_get_system_normal_font (void) |
99852628 JD |
692 | { |
693 | return current_font; | |
694 | } | |
695 | ||
696 | DEFUN ("font-get-system-normal-font", Ffont_get_system_normal_font, | |
697 | Sfont_get_system_normal_font, | |
698 | 0, 0, 0, | |
bf935339 | 699 | doc: /* Get the system default application font. */) |
5842a27b | 700 | (void) |
99852628 | 701 | { |
bf935339 | 702 | return current_font |
99852628 JD |
703 | ? make_string (current_font, strlen (current_font)) |
704 | : Qnil; | |
705 | } | |
706 | ||
637fa988 JD |
707 | DEFUN ("font-get-system-font", Ffont_get_system_font, Sfont_get_system_font, |
708 | 0, 0, 0, | |
bf935339 | 709 | doc: /* Get the system default fixed width font. */) |
5842a27b | 710 | (void) |
637fa988 | 711 | { |
bf935339 | 712 | return current_mono_font |
637fa988 JD |
713 | ? make_string (current_mono_font, strlen (current_mono_font)) |
714 | : Qnil; | |
715 | } | |
716 | ||
f904c0f9 JD |
717 | DEFUN ("tool-bar-get-system-style", Ftool_bar_get_system_style, Stool_bar_get_system_style, |
718 | 0, 0, 0, | |
719 | doc: /* Get the system tool bar style. | |
4721152c | 720 | If no system tool bar style is known, return `tool-bar-style' if set to a |
f904c0f9 | 721 | known style. Otherwise return image. */) |
5842a27b | 722 | (void) |
f904c0f9 JD |
723 | { |
724 | if (EQ (Vtool_bar_style, Qimage) | |
725 | || EQ (Vtool_bar_style, Qtext) | |
726 | || EQ (Vtool_bar_style, Qboth) | |
8a52f00a JD |
727 | || EQ (Vtool_bar_style, Qboth_horiz) |
728 | || EQ (Vtool_bar_style, Qtext_image_horiz)) | |
f904c0f9 JD |
729 | return Vtool_bar_style; |
730 | if (!NILP (current_tool_bar_style)) | |
731 | return current_tool_bar_style; | |
732 | return Qimage; | |
733 | } | |
734 | ||
637fa988 | 735 | void |
971de7fb | 736 | syms_of_xsettings (void) |
637fa988 JD |
737 | { |
738 | current_mono_font = NULL; | |
99852628 | 739 | current_font = NULL; |
637fa988 JD |
740 | first_dpyinfo = NULL; |
741 | #ifdef HAVE_GCONF | |
742 | gconf_client = NULL; | |
743 | #endif | |
744 | ||
f904c0f9 JD |
745 | Qmonospace_font_name = intern_c_string ("monospace-font-name"); |
746 | staticpro (&Qmonospace_font_name); | |
637fa988 JD |
747 | Qfont_name = intern_c_string ("font-name"); |
748 | staticpro (&Qfont_name); | |
749 | Qfont_render = intern_c_string ("font-render"); | |
750 | staticpro (&Qfont_render); | |
751 | defsubr (&Sfont_get_system_font); | |
99852628 | 752 | defsubr (&Sfont_get_system_normal_font); |
637fa988 | 753 | |
dfb3c4c6 | 754 | DEFVAR_BOOL ("font-use-system-font", &use_system_font, |
bf935339 J |
755 | doc: /* *Non-nil means to apply the system defined font dynamically. |
756 | When this is non-nil and the system defined fixed width font changes, we | |
757 | update frames dynamically. | |
758 | If this variable is nil, Emacs ignores system font changes. */); | |
dfb3c4c6 JD |
759 | use_system_font = 0; |
760 | ||
67477f30 JD |
761 | DEFVAR_LISP ("xft-settings", &Vxft_settings, |
762 | doc: /* Font settings applied to Xft. */); | |
763 | Vxft_settings = make_string ("", 0); | |
764 | ||
0d1d0d26 JD |
765 | #ifdef HAVE_XFT |
766 | Fprovide (intern_c_string ("font-render-setting"), Qnil); | |
637fa988 JD |
767 | #ifdef HAVE_GCONF |
768 | Fprovide (intern_c_string ("system-font-setting"), Qnil); | |
769 | #endif | |
637fa988 | 770 | #endif |
f904c0f9 JD |
771 | |
772 | current_tool_bar_style = Qnil; | |
773 | Qtool_bar_style = intern_c_string ("tool-bar-style"); | |
774 | staticpro (&Qtool_bar_style); | |
775 | defsubr (&Stool_bar_get_system_style); | |
776 | ||
777 | Fprovide (intern_c_string ("dynamic-setting"), Qnil); | |
637fa988 | 778 | } |
964f5b2b | 779 |