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