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