2 * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * Kevin E. Martin <kem@redhat.com>
35 * This file provides support for fonts. */
37 #ifdef HAVE_DMX_CONFIG_H
38 #include <dmx-config.h>
41 #define DMX_FONTPATH_DEBUG 0
48 #include <X11/fonts/fontstruct.h>
50 #include "dixstruct.h"
52 static int (*dmxSaveProcVector[256])(ClientPtr);
53 static int dmxFontLastError;
55 static int dmxFontErrorHandler(Display *dpy, XErrorEvent *ev)
57 dmxFontLastError = ev->error_code;
62 static char **dmxGetFontPath(int *npaths)
65 unsigned char *c, *paths;
69 GetFontPath(serverClient, npaths, &len, &paths);
71 newfp = malloc(*npaths + len);
72 c = (unsigned char *)newfp;
73 fp = malloc(*npaths * sizeof(*fp));
75 memmove(newfp, paths+1, *npaths + len - 1);
77 for (i = 0; i < *npaths; i++) {
84 #if DMX_FONTPATH_DEBUG
85 for (i = 0; i < *npaths; i++)
86 dmxLog(dmxDebug, "FontPath[%d] = %s\n", i, fp[i]);
92 static void dmxFreeFontPath(char **fp)
98 static Bool dmxCheckFontPathElement(DMXScreenInfo *dmxScreen, char *fp)
100 int (*oldErrorHandler)(Display *, XErrorEvent *);
102 if (!dmxScreen->beDisplay)
105 dmxFontLastError = 0;
106 oldErrorHandler = XSetErrorHandler(dmxFontErrorHandler);
107 XSetFontPath(dmxScreen->beDisplay, &fp, 1);
108 dmxSync(dmxScreen, TRUE); /* Must complete before removing handler */
109 XSetErrorHandler(oldErrorHandler);
111 return dmxFontLastError == 0;
114 static int dmxSetFontPath(DMXScreenInfo *dmxScreen)
116 int (*oldErrorHandler)(Display *, XErrorEvent *);
118 int result = Success;
121 if (!dmxScreen->beDisplay)
124 fp = dmxGetFontPath(&npaths);
125 if (!fp) return BadAlloc;
127 dmxFontLastError = 0;
128 oldErrorHandler = XSetErrorHandler(dmxFontErrorHandler);
129 XSetFontPath(dmxScreen->beDisplay, fp, npaths);
130 dmxSync(dmxScreen, TRUE); /* Must complete before removing handler */
131 XSetErrorHandler(oldErrorHandler);
133 if (dmxFontLastError) {
134 result = dmxFontLastError;
135 /* We could set *error here to the offending path, but it is
136 * ignored, so we don't bother figuring out which path is bad.
137 * If we do add this support in the future, we'll need to add
138 * error to the function's argument list.
147 static int dmxCheckFontPath(DMXScreenInfo *dmxScreen, int *error)
151 int result = Success;
153 if (!dmxScreen->beDisplay)
156 /* Save old font path */
157 oldFontPath = XGetFontPath(dmxScreen->beDisplay, &nOldPaths);
159 result = dmxSetFontPath(dmxScreen);
161 /* Restore old font path */
162 XSetFontPath(dmxScreen->beDisplay, oldFontPath, nOldPaths);
163 XFreeFontPath(oldFontPath);
164 dmxSync(dmxScreen, FALSE);
169 static int dmxProcSetFontPath(ClientPtr client)
172 unsigned long nbytes, total, n;
175 unsigned char *oldFontPath, *tmpFontPath;
178 REQUEST(xSetFontPathReq);
180 REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
182 nbytes = (client->req_len << 2) - sizeof(xSetFontPathReq);
184 ptr = (unsigned char *)&stuff[1];
185 nfonts = stuff->nFonts;
187 while (--nfonts >= 0) {
188 if ((total == 0) || (total < (n = (*ptr + 1))))
196 GetFontPath(serverClient, &nOldPaths, &lenOldPaths, &tmpFontPath);
197 oldFontPath = malloc(nOldPaths + lenOldPaths);
198 memmove(oldFontPath, tmpFontPath, nOldPaths + lenOldPaths);
200 result = SetFontPath(client, stuff->nFonts, (unsigned char *)&stuff[1]);
203 for (i = 0; i < dmxNumScreens; i++)
204 if ((result = dmxCheckFontPath(&dmxScreens[i], &error)))
208 /* Restore old fontpath in the DMX server */
209 SetFontPath(client, nOldPaths, oldFontPath);
210 client->errorValue = error;
218 /** Initialize font support. In addition to the screen function call
219 * pointers, DMX also hooks in at the ProcVector[] level. Here the old
220 * ProcVector function pointers are saved and the new ProcVector
221 * function pointers are initialized. */
222 void dmxInitFonts(void)
226 for (i = 0; i < 256; i++)
227 dmxSaveProcVector[i] = ProcVector[i];
229 ProcVector[X_SetFontPath] = dmxProcSetFontPath;
232 /** Reset font support by restoring the original ProcVector function
234 void dmxResetFonts(void)
238 for (i = 0; i < 256; i++)
239 ProcVector[i] = dmxSaveProcVector[i];
242 /** Load the font, \a pFont, on the back-end server associated with \a
243 * pScreen. When a font is loaded, the font path on back-end server is
244 * first initialized to that specified on the command line with the
245 * -fontpath options, and then the font is loaded. */
246 Bool dmxBELoadFont(ScreenPtr pScreen, FontPtr pFont)
248 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
249 dmxFontPrivPtr pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex);
251 char **oldFontPath = NULL;
253 Atom name_atom, value_atom;
256 /* Make sure we have a font private struct to work with */
260 /* Don't load a font over top of itself */
261 if (pFontPriv->font[pScreen->myNum]) {
262 return TRUE; /* Already loaded font */
265 /* Save old font path */
266 oldFontPath = XGetFontPath(dmxScreen->beDisplay, &nOldPaths);
268 /* Set the font path for the font about to be loaded on the back-end */
269 if (dmxSetFontPath(dmxScreen)) {
274 /* This could fail only when first starting the X server and
275 * loading the default font. If it fails here, then the default
276 * font path is invalid, no default font path will be set, the
277 * DMX server will fail to load the default font, and it will
278 * exit with an error unless we remove the offending font paths
279 * with the -ignorebadfontpaths command line option.
282 fp = dmxGetFontPath(&npaths);
285 "No default font path set.\n");
287 "Please see the Xdmx man page for information on how to\n");
289 "initialize the DMX server's default font path.\n");
290 XFreeFontPath(oldFontPath);
295 dmxLog(dmxWarning, "No default font path is set.\n");
297 goodfps = malloc(npaths * sizeof(*goodfps));
300 "The DMX server failed to set the following font paths on "
301 "screen #%d:\n", pScreen->myNum);
303 for (i = 0; i < npaths; i++)
304 if (!(goodfps[i] = dmxCheckFontPathElement(dmxScreen, fp[i])))
305 dmxLog(dmxError, " %s\n", fp[i]);
307 if (dmxIgnoreBadFontPaths) {
314 "These font paths will not be used because the "
315 "\"-ignorebadfontpaths\"\n");
319 for (i = 0; i < npaths; i++)
321 len += strlen(fp[i]) + 1;
326 /* No valid font paths were found */
328 "After removing the font paths above, no valid font "
331 "available. Please check that the font paths set on "
334 "line or in the configuration file via the "
335 "\"-fontpath\" option\n");
337 "are valid on all back-end servers. See the Xdmx man "
340 "more information on font paths.\n");
342 XFreeFontPath(oldFontPath);
347 newfp = malloc(len * sizeof(*newfp));
348 for (i = 0; i < npaths; i++) {
350 int n = strlen(fp[i]);
352 strncpy(&newfp[j], fp[i], n);
357 if (SetFontPath(serverClient, newnpaths, (unsigned char *)newfp)) {
358 /* Note that this should never happen since all of the
359 * FPEs were previously valid. */
360 dmxLog(dmxError, "Cannot reset the default font path.\n");
362 } else if (dmxFontPath) {
364 "Please remove these font paths from the command line "
367 "configuration file, or set the \"-ignorebadfontpaths\" "
370 "ignore them. For more information on these options, see "
376 "Please specify the font paths that are available on all "
379 "servers with the \"-fontpath\" option, or use the "
380 "\"-ignorebadfontpaths\"\n");
382 "to ignore bad defaults. For more information on "
383 "these and other\n");
385 "font-path-related options, see the Xdmx man page.\n");
388 if (!dmxIgnoreBadFontPaths ||
389 (dmxIgnoreBadFontPaths && dmxSetFontPath(dmxScreen))) {
390 /* We still have errors so return with error */
392 XFreeFontPath(oldFontPath);
398 /* Find requested font on back-end server */
399 name_atom = MakeAtom("FONT", 4, TRUE);
402 for (i = 0; i < pFont->info.nprops; i++) {
403 if ((Atom)pFont->info.props[i].name == name_atom) {
404 value_atom = pFont->info.props[i].value;
408 if (!value_atom) return FALSE;
410 name = NameForAtom(value_atom);
411 if (!name) return FALSE;
413 pFontPriv->font[pScreen->myNum] =
414 XLoadQueryFont(dmxScreen->beDisplay, name);
416 /* Restore old font path */
417 XSetFontPath(dmxScreen->beDisplay, oldFontPath, nOldPaths);
418 XFreeFontPath(oldFontPath);
419 dmxSync(dmxScreen, FALSE);
421 if (!pFontPriv->font[pScreen->myNum]) return FALSE;
426 /** Realize the font, \a pFont, on the back-end server associated with
428 Bool dmxRealizeFont(ScreenPtr pScreen, FontPtr pFont)
430 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
431 dmxFontPrivPtr pFontPriv;
433 if (!(pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex))) {
434 FontSetPrivate(pFont, dmxFontPrivateIndex, NULL);
435 pFontPriv = malloc(sizeof(dmxFontPrivRec));
436 if (!pFontPriv) return FALSE;
437 pFontPriv->font = NULL;
438 MAXSCREENSALLOC(pFontPriv->font);
439 if (!pFontPriv->font) {
443 pFontPriv->refcnt = 0;
446 FontSetPrivate(pFont, dmxFontPrivateIndex, (pointer)pFontPriv);
448 if (dmxScreen->beDisplay) {
449 if (!dmxBELoadFont(pScreen, pFont))
454 pFontPriv->font[pScreen->myNum] = NULL;
460 /** Free \a pFont on the back-end associated with \a pScreen. */
461 Bool dmxBEFreeFont(ScreenPtr pScreen, FontPtr pFont)
463 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
464 dmxFontPrivPtr pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex);
466 if (pFontPriv && pFontPriv->font[pScreen->myNum]) {
467 XFreeFont(dmxScreen->beDisplay, pFontPriv->font[pScreen->myNum]);
468 pFontPriv->font[pScreen->myNum] = NULL;
475 /** Unrealize the font, \a pFont, on the back-end server associated with
477 Bool dmxUnrealizeFont(ScreenPtr pScreen, FontPtr pFont)
479 DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
480 dmxFontPrivPtr pFontPriv;
482 if ((pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex))) {
483 /* In case the font failed to load properly */
484 if (!pFontPriv->refcnt) {
485 MAXSCREENSFREE(pFontPriv->font);
487 FontSetPrivate(pFont, dmxFontPrivateIndex, NULL);
488 } else if (pFontPriv->font[pScreen->myNum]) {
489 if (dmxScreen->beDisplay)
490 dmxBEFreeFont(pScreen, pFont);
492 /* The code below is non-obvious, so here's an explanation...
494 * When creating the default GC, the server opens up the
495 * default font once for each screen, which in turn calls
496 * the RealizeFont function pointer once for each screen.
497 * During this process both dix's font refcnt and DMX's font
498 * refcnt are incremented once for each screen.
500 * Later, when shutting down the X server, dix shuts down
501 * each screen in reverse order. During this shutdown
502 * procedure, each screen's default GC is freed and then
503 * that screen is closed by calling the CloseScreen function
504 * pointer. screenInfo.numScreens is then decremented after
505 * closing each screen. This procedure means that the dix's
506 * font refcnt for the font used by the default GC's is
507 * decremented once for each screen # greater than 0.
508 * However, since dix's refcnt for the default font is not
509 * yet 0 for each screen greater than 0, no call to the
510 * UnrealizeFont function pointer is made for those screens.
511 * Then, when screen 0 is being closed, dix's font refcnt
512 * for the default GC's font is finally 0 and the font is
513 * unrealized. However, since screenInfo.numScreens has
514 * been decremented already down to 1, only one call to
515 * UnrealizeFont is made (for screen 0). Thus, even though
516 * RealizeFont was called once for each screen,
517 * UnrealizeFont is only called for screen 0.
519 * This is a bug in dix.
521 * To avoid the memory leak of pFontPriv for each server
522 * generation, we can also free pFontPriv if the refcnt is
523 * not yet 0 but the # of screens is 1 -- i.e., the case
524 * described in the dix bug above. This is only a temporary
525 * workaround until the bug in dix is solved.
527 * The other problem is that the font structure allocated by
528 * XLoadQueryFont() above is not freed for screens > 0.
529 * This problem cannot be worked around here since the back-
530 * end displays for screens > 0 have already been closed by
531 * the time this code is called from dix.
533 * When the bug in dix described above is fixed, then we can
534 * remove the "|| screenInfo.numScreens == 1" code below and
535 * the memory leaks will be eliminated.
537 if (--pFontPriv->refcnt == 0
539 /* Remove this code when the dix bug is fixed */
540 || screenInfo.numScreens == 1
543 MAXSCREENSFREE(pFontPriv->font);
545 FontSetPrivate(pFont, dmxFontPrivateIndex, NULL);