xref: /OK3568_Linux_fs/external/xserver/hw/dmx/dmxfont.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2001-2004 Red Hat Inc., Durham, North Carolina.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining
7*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
8*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun  * without limitation on the rights to use, copy, modify, merge,
10*4882a593Smuzhiyun  * publish, distribute, sublicense, and/or sell copies of the Software,
11*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so,
12*4882a593Smuzhiyun  * subject to the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial
16*4882a593Smuzhiyun  * portions of the Software.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21*4882a593Smuzhiyun  * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25*4882a593Smuzhiyun  * SOFTWARE.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Authors:
30*4882a593Smuzhiyun  *   Kevin E. Martin <kem@redhat.com>
31*4882a593Smuzhiyun  *
32*4882a593Smuzhiyun  */
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /** \file
35*4882a593Smuzhiyun  * This file provides support for fonts. */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #ifdef HAVE_DMX_CONFIG_H
38*4882a593Smuzhiyun #include <dmx-config.h>
39*4882a593Smuzhiyun #endif
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun #define DMX_FONTPATH_DEBUG 0
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun #include "dmx.h"
44*4882a593Smuzhiyun #include "dmxsync.h"
45*4882a593Smuzhiyun #include "dmxfont.h"
46*4882a593Smuzhiyun #include "dmxlog.h"
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun #include <X11/fonts/fontstruct.h>
49*4882a593Smuzhiyun #include <X11/fonts/libxfont2.h>
50*4882a593Smuzhiyun #include "dixfont.h"
51*4882a593Smuzhiyun #include "dixstruct.h"
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun static int (*dmxSaveProcVector[256]) (ClientPtr);
54*4882a593Smuzhiyun static int dmxFontLastError;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun static int
dmxFontErrorHandler(Display * dpy,XErrorEvent * ev)57*4882a593Smuzhiyun dmxFontErrorHandler(Display * dpy, XErrorEvent * ev)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun     dmxFontLastError = ev->error_code;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun     return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static char **
dmxGetFontPath(int * npaths)65*4882a593Smuzhiyun dmxGetFontPath(int *npaths)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun     char **fp;
68*4882a593Smuzhiyun     unsigned char *c, *paths;
69*4882a593Smuzhiyun     char *newfp;
70*4882a593Smuzhiyun     int len, l, i;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun     GetFontPath(serverClient, npaths, &len, &paths);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun     newfp = malloc(*npaths + len);
75*4882a593Smuzhiyun     c = (unsigned char *) newfp;
76*4882a593Smuzhiyun     fp = xallocarray(*npaths, sizeof(*fp));
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun     memmove(newfp, paths + 1, *npaths + len - 1);
79*4882a593Smuzhiyun     l = *paths;
80*4882a593Smuzhiyun     for (i = 0; i < *npaths; i++) {
81*4882a593Smuzhiyun         fp[i] = (char *) c;
82*4882a593Smuzhiyun         c += l;
83*4882a593Smuzhiyun         l = *c;
84*4882a593Smuzhiyun         *c++ = '\0';
85*4882a593Smuzhiyun     }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #if DMX_FONTPATH_DEBUG
88*4882a593Smuzhiyun     for (i = 0; i < *npaths; i++)
89*4882a593Smuzhiyun         dmxLog(dmxDebug, "FontPath[%d] = %s\n", i, fp[i]);
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun     return fp;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun static void
dmxFreeFontPath(char ** fp)96*4882a593Smuzhiyun dmxFreeFontPath(char **fp)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun     free(fp[0]);
99*4882a593Smuzhiyun     free(fp);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun static Bool
dmxCheckFontPathElement(DMXScreenInfo * dmxScreen,char * fp)103*4882a593Smuzhiyun dmxCheckFontPathElement(DMXScreenInfo * dmxScreen, char *fp)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun     int (*oldErrorHandler) (Display *, XErrorEvent *);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun     if (!dmxScreen->beDisplay)
108*4882a593Smuzhiyun         return TRUE;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun     dmxFontLastError = 0;
111*4882a593Smuzhiyun     oldErrorHandler = XSetErrorHandler(dmxFontErrorHandler);
112*4882a593Smuzhiyun     XSetFontPath(dmxScreen->beDisplay, &fp, 1);
113*4882a593Smuzhiyun     dmxSync(dmxScreen, TRUE);   /* Must complete before removing handler */
114*4882a593Smuzhiyun     XSetErrorHandler(oldErrorHandler);
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun     return dmxFontLastError == 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun static int
dmxSetFontPath(DMXScreenInfo * dmxScreen)120*4882a593Smuzhiyun dmxSetFontPath(DMXScreenInfo * dmxScreen)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun     int (*oldErrorHandler) (Display *, XErrorEvent *);
123*4882a593Smuzhiyun     char **fp;
124*4882a593Smuzhiyun     int result = Success;
125*4882a593Smuzhiyun     int npaths;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun     if (!dmxScreen->beDisplay)
128*4882a593Smuzhiyun         return result;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun     fp = dmxGetFontPath(&npaths);
131*4882a593Smuzhiyun     if (!fp)
132*4882a593Smuzhiyun         return BadAlloc;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun     dmxFontLastError = 0;
135*4882a593Smuzhiyun     oldErrorHandler = XSetErrorHandler(dmxFontErrorHandler);
136*4882a593Smuzhiyun     XSetFontPath(dmxScreen->beDisplay, fp, npaths);
137*4882a593Smuzhiyun     dmxSync(dmxScreen, TRUE);   /* Must complete before removing handler */
138*4882a593Smuzhiyun     XSetErrorHandler(oldErrorHandler);
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun     if (dmxFontLastError) {
141*4882a593Smuzhiyun         result = dmxFontLastError;
142*4882a593Smuzhiyun         /* We could set *error here to the offending path, but it is
143*4882a593Smuzhiyun          * ignored, so we don't bother figuring out which path is bad.
144*4882a593Smuzhiyun          * If we do add this support in the future, we'll need to add
145*4882a593Smuzhiyun          * error to the function's argument list.
146*4882a593Smuzhiyun          */
147*4882a593Smuzhiyun     }
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun     dmxFreeFontPath(fp);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun     return result;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun static int
dmxCheckFontPath(DMXScreenInfo * dmxScreen,int * error)155*4882a593Smuzhiyun dmxCheckFontPath(DMXScreenInfo * dmxScreen, int *error)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun     char **oldFontPath;
158*4882a593Smuzhiyun     int nOldPaths;
159*4882a593Smuzhiyun     int result = Success;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun     if (!dmxScreen->beDisplay)
162*4882a593Smuzhiyun         return result;
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun     /* Save old font path */
165*4882a593Smuzhiyun     oldFontPath = XGetFontPath(dmxScreen->beDisplay, &nOldPaths);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun     result = dmxSetFontPath(dmxScreen);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun     /* Restore old font path */
170*4882a593Smuzhiyun     XSetFontPath(dmxScreen->beDisplay, oldFontPath, nOldPaths);
171*4882a593Smuzhiyun     XFreeFontPath(oldFontPath);
172*4882a593Smuzhiyun     dmxSync(dmxScreen, FALSE);
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun     return result;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun static int
dmxProcSetFontPath(ClientPtr client)178*4882a593Smuzhiyun dmxProcSetFontPath(ClientPtr client)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun     unsigned char *ptr;
181*4882a593Smuzhiyun     unsigned long nbytes, total, n;
182*4882a593Smuzhiyun     long nfonts;
183*4882a593Smuzhiyun     int i, result;
184*4882a593Smuzhiyun     unsigned char *oldFontPath, *tmpFontPath;
185*4882a593Smuzhiyun     int nOldPaths;
186*4882a593Smuzhiyun     int lenOldPaths;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun     REQUEST(xSetFontPathReq);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun     REQUEST_AT_LEAST_SIZE(xSetFontPathReq);
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun     nbytes = (client->req_len << 2) - sizeof(xSetFontPathReq);
193*4882a593Smuzhiyun     total = nbytes;
194*4882a593Smuzhiyun     ptr = (unsigned char *) &stuff[1];
195*4882a593Smuzhiyun     nfonts = stuff->nFonts;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun     while (--nfonts >= 0) {
198*4882a593Smuzhiyun         if ((total == 0) || (total < (n = (*ptr + 1))))
199*4882a593Smuzhiyun             return BadLength;
200*4882a593Smuzhiyun         total -= n;
201*4882a593Smuzhiyun         ptr += n;
202*4882a593Smuzhiyun     }
203*4882a593Smuzhiyun     if (total >= 4)
204*4882a593Smuzhiyun         return BadLength;
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun     GetFontPath(serverClient, &nOldPaths, &lenOldPaths, &tmpFontPath);
207*4882a593Smuzhiyun     oldFontPath = malloc(nOldPaths + lenOldPaths);
208*4882a593Smuzhiyun     memmove(oldFontPath, tmpFontPath, nOldPaths + lenOldPaths);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun     result = SetFontPath(client, stuff->nFonts, (unsigned char *) &stuff[1]);
211*4882a593Smuzhiyun     if (!result) {
212*4882a593Smuzhiyun         int error = 0;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun         for (i = 0; i < dmxNumScreens; i++)
215*4882a593Smuzhiyun             if ((result = dmxCheckFontPath(&dmxScreens[i], &error)))
216*4882a593Smuzhiyun                 break;
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun         if (result) {
219*4882a593Smuzhiyun             /* Restore old fontpath in the DMX server */
220*4882a593Smuzhiyun             SetFontPath(client, nOldPaths, oldFontPath);
221*4882a593Smuzhiyun             client->errorValue = error;
222*4882a593Smuzhiyun         }
223*4882a593Smuzhiyun     }
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun     free(oldFontPath);
226*4882a593Smuzhiyun     return result;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun /** Initialize font support.  In addition to the screen function call
230*4882a593Smuzhiyun  *  pointers, DMX also hooks in at the ProcVector[] level.  Here the old
231*4882a593Smuzhiyun  *  ProcVector function pointers are saved and the new ProcVector
232*4882a593Smuzhiyun  *  function pointers are initialized. */
233*4882a593Smuzhiyun void
dmxInitFonts(void)234*4882a593Smuzhiyun dmxInitFonts(void)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun     int i;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun     for (i = 0; i < 256; i++)
239*4882a593Smuzhiyun         dmxSaveProcVector[i] = ProcVector[i];
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun     ProcVector[X_SetFontPath] = dmxProcSetFontPath;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /** Reset font support by restoring the original ProcVector function
245*4882a593Smuzhiyun  *  pointers. */
246*4882a593Smuzhiyun void
dmxResetFonts(void)247*4882a593Smuzhiyun dmxResetFonts(void)
248*4882a593Smuzhiyun {
249*4882a593Smuzhiyun     int i;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun     for (i = 0; i < 256; i++)
252*4882a593Smuzhiyun         ProcVector[i] = dmxSaveProcVector[i];
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /** Load the font, \a pFont, on the back-end server associated with \a
256*4882a593Smuzhiyun  *  pScreen.  When a font is loaded, the font path on back-end server is
257*4882a593Smuzhiyun  *  first initialized to that specified on the command line with the
258*4882a593Smuzhiyun  *  -fontpath options, and then the font is loaded. */
259*4882a593Smuzhiyun Bool
dmxBELoadFont(ScreenPtr pScreen,FontPtr pFont)260*4882a593Smuzhiyun dmxBELoadFont(ScreenPtr pScreen, FontPtr pFont)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
263*4882a593Smuzhiyun     dmxFontPrivPtr pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex);
264*4882a593Smuzhiyun     const char *name;
265*4882a593Smuzhiyun     char **oldFontPath = NULL;
266*4882a593Smuzhiyun     int nOldPaths;
267*4882a593Smuzhiyun     Atom name_atom, value_atom;
268*4882a593Smuzhiyun     int i;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun     /* Make sure we have a font private struct to work with */
271*4882a593Smuzhiyun     if (!pFontPriv)
272*4882a593Smuzhiyun         return FALSE;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun     /* Don't load a font over top of itself */
275*4882a593Smuzhiyun     if (pFontPriv->font[pScreen->myNum]) {
276*4882a593Smuzhiyun         return TRUE;            /* Already loaded font */
277*4882a593Smuzhiyun     }
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun     /* Save old font path */
280*4882a593Smuzhiyun     oldFontPath = XGetFontPath(dmxScreen->beDisplay, &nOldPaths);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun     /* Set the font path for the font about to be loaded on the back-end */
283*4882a593Smuzhiyun     if (dmxSetFontPath(dmxScreen)) {
284*4882a593Smuzhiyun         char **fp;
285*4882a593Smuzhiyun         int npaths;
286*4882a593Smuzhiyun         Bool *goodfps;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun         /* This could fail only when first starting the X server and
289*4882a593Smuzhiyun          * loading the default font.  If it fails here, then the default
290*4882a593Smuzhiyun          * font path is invalid, no default font path will be set, the
291*4882a593Smuzhiyun          * DMX server will fail to load the default font, and it will
292*4882a593Smuzhiyun          * exit with an error unless we remove the offending font paths
293*4882a593Smuzhiyun          * with the -ignorebadfontpaths command line option.
294*4882a593Smuzhiyun          */
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun         fp = dmxGetFontPath(&npaths);
297*4882a593Smuzhiyun         if (!fp) {
298*4882a593Smuzhiyun             dmxLog(dmxError, "No default font path set.\n");
299*4882a593Smuzhiyun             dmxLog(dmxError,
300*4882a593Smuzhiyun                    "Please see the Xdmx man page for information on how to\n");
301*4882a593Smuzhiyun             dmxLog(dmxError,
302*4882a593Smuzhiyun                    "initialize the DMX server's default font path.\n");
303*4882a593Smuzhiyun             XFreeFontPath(oldFontPath);
304*4882a593Smuzhiyun             return FALSE;
305*4882a593Smuzhiyun         }
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun         if (!dmxFontPath)
308*4882a593Smuzhiyun             dmxLog(dmxWarning, "No default font path is set.\n");
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun         goodfps = xallocarray(npaths, sizeof(*goodfps));
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun         dmxLog(dmxError,
313*4882a593Smuzhiyun                "The DMX server failed to set the following font paths on "
314*4882a593Smuzhiyun                "screen #%d:\n", pScreen->myNum);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun         for (i = 0; i < npaths; i++)
317*4882a593Smuzhiyun             if (!(goodfps[i] = dmxCheckFontPathElement(dmxScreen, fp[i])))
318*4882a593Smuzhiyun                 dmxLog(dmxError, "    %s\n", fp[i]);
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun         if (dmxIgnoreBadFontPaths) {
321*4882a593Smuzhiyun             char *newfp;
322*4882a593Smuzhiyun             int newnpaths = 0;
323*4882a593Smuzhiyun             int len = 0;
324*4882a593Smuzhiyun             int j = 0;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun             dmxLog(dmxError,
327*4882a593Smuzhiyun                    "These font paths will not be used because the "
328*4882a593Smuzhiyun                    "\"-ignorebadfontpaths\"\n");
329*4882a593Smuzhiyun             dmxLog(dmxError, "option is set.\n");
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun             for (i = 0; i < npaths; i++)
332*4882a593Smuzhiyun                 if (goodfps[i]) {
333*4882a593Smuzhiyun                     len += strlen(fp[i]) + 1;
334*4882a593Smuzhiyun                     newnpaths++;
335*4882a593Smuzhiyun                 }
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun             if (!newnpaths) {
338*4882a593Smuzhiyun                 /* No valid font paths were found */
339*4882a593Smuzhiyun                 dmxLog(dmxError,
340*4882a593Smuzhiyun                        "After removing the font paths above, no valid font "
341*4882a593Smuzhiyun                        "paths were\n");
342*4882a593Smuzhiyun                 dmxLog(dmxError,
343*4882a593Smuzhiyun                        "available.  Please check that the font paths set on "
344*4882a593Smuzhiyun                        "the command\n");
345*4882a593Smuzhiyun                 dmxLog(dmxError,
346*4882a593Smuzhiyun                        "line or in the configuration file via the "
347*4882a593Smuzhiyun                        "\"-fontpath\" option\n");
348*4882a593Smuzhiyun                 dmxLog(dmxError,
349*4882a593Smuzhiyun                        "are valid on all back-end servers.  See the Xdmx man "
350*4882a593Smuzhiyun                        "page for\n");
351*4882a593Smuzhiyun                 dmxLog(dmxError, "more information on font paths.\n");
352*4882a593Smuzhiyun                 dmxFreeFontPath(fp);
353*4882a593Smuzhiyun                 XFreeFontPath(oldFontPath);
354*4882a593Smuzhiyun                 free(goodfps);
355*4882a593Smuzhiyun                 return FALSE;
356*4882a593Smuzhiyun             }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun             newfp = xallocarray(len, sizeof(*newfp));
359*4882a593Smuzhiyun             for (i = 0; i < npaths; i++) {
360*4882a593Smuzhiyun                 if (goodfps[i]) {
361*4882a593Smuzhiyun                     int n = strlen(fp[i]);
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun                     newfp[j++] = n;
364*4882a593Smuzhiyun                     strncpy(&newfp[j], fp[i], n);
365*4882a593Smuzhiyun                     j += n;
366*4882a593Smuzhiyun                 }
367*4882a593Smuzhiyun             }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun             if (SetFontPath(serverClient, newnpaths, (unsigned char *) newfp)) {
370*4882a593Smuzhiyun                 /* Note that this should never happen since all of the
371*4882a593Smuzhiyun                  * FPEs were previously valid. */
372*4882a593Smuzhiyun                 dmxLog(dmxError, "Cannot reset the default font path.\n");
373*4882a593Smuzhiyun             }
374*4882a593Smuzhiyun         }
375*4882a593Smuzhiyun         else if (dmxFontPath) {
376*4882a593Smuzhiyun             dmxLog(dmxError,
377*4882a593Smuzhiyun                    "Please remove these font paths from the command line "
378*4882a593Smuzhiyun                    "or\n");
379*4882a593Smuzhiyun             dmxLog(dmxError,
380*4882a593Smuzhiyun                    "configuration file, or set the \"-ignorebadfontpaths\" "
381*4882a593Smuzhiyun                    "option to\n");
382*4882a593Smuzhiyun             dmxLog(dmxError,
383*4882a593Smuzhiyun                    "ignore them.  For more information on these options, see "
384*4882a593Smuzhiyun                    "the\n");
385*4882a593Smuzhiyun             dmxLog(dmxError, "Xdmx man page.\n");
386*4882a593Smuzhiyun         }
387*4882a593Smuzhiyun         else {
388*4882a593Smuzhiyun             dmxLog(dmxError,
389*4882a593Smuzhiyun                    "Please specify the font paths that are available on all "
390*4882a593Smuzhiyun                    "back-end\n");
391*4882a593Smuzhiyun             dmxLog(dmxError,
392*4882a593Smuzhiyun                    "servers with the \"-fontpath\" option, or use the "
393*4882a593Smuzhiyun                    "\"-ignorebadfontpaths\"\n");
394*4882a593Smuzhiyun             dmxLog(dmxError,
395*4882a593Smuzhiyun                    "to ignore bad defaults.  For more information on "
396*4882a593Smuzhiyun                    "these and other\n");
397*4882a593Smuzhiyun             dmxLog(dmxError,
398*4882a593Smuzhiyun                    "font-path-related options, see the Xdmx man page.\n");
399*4882a593Smuzhiyun         }
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun         free(goodfps);
402*4882a593Smuzhiyun         if (!dmxIgnoreBadFontPaths ||
403*4882a593Smuzhiyun             (dmxIgnoreBadFontPaths && dmxSetFontPath(dmxScreen))) {
404*4882a593Smuzhiyun             /* We still have errors so return with error */
405*4882a593Smuzhiyun             dmxFreeFontPath(fp);
406*4882a593Smuzhiyun             XFreeFontPath(oldFontPath);
407*4882a593Smuzhiyun             return FALSE;
408*4882a593Smuzhiyun         }
409*4882a593Smuzhiyun     }
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun     /* Find requested font on back-end server */
412*4882a593Smuzhiyun     name_atom = MakeAtom("FONT", 4, TRUE);
413*4882a593Smuzhiyun     value_atom = 0L;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun     for (i = 0; i < pFont->info.nprops; i++) {
416*4882a593Smuzhiyun         if ((Atom) pFont->info.props[i].name == name_atom) {
417*4882a593Smuzhiyun             value_atom = pFont->info.props[i].value;
418*4882a593Smuzhiyun             break;
419*4882a593Smuzhiyun         }
420*4882a593Smuzhiyun     }
421*4882a593Smuzhiyun     if (!value_atom)
422*4882a593Smuzhiyun         return FALSE;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun     name = NameForAtom(value_atom);
425*4882a593Smuzhiyun     if (!name)
426*4882a593Smuzhiyun         return FALSE;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun     pFontPriv->font[pScreen->myNum] =
429*4882a593Smuzhiyun         XLoadQueryFont(dmxScreen->beDisplay, name);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun     /* Restore old font path */
432*4882a593Smuzhiyun     XSetFontPath(dmxScreen->beDisplay, oldFontPath, nOldPaths);
433*4882a593Smuzhiyun     XFreeFontPath(oldFontPath);
434*4882a593Smuzhiyun     dmxSync(dmxScreen, FALSE);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun     if (!pFontPriv->font[pScreen->myNum])
437*4882a593Smuzhiyun         return FALSE;
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun     return TRUE;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun /** Realize the font, \a pFont, on the back-end server associated with
443*4882a593Smuzhiyun  *  \a pScreen. */
444*4882a593Smuzhiyun Bool
dmxRealizeFont(ScreenPtr pScreen,FontPtr pFont)445*4882a593Smuzhiyun dmxRealizeFont(ScreenPtr pScreen, FontPtr pFont)
446*4882a593Smuzhiyun {
447*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
448*4882a593Smuzhiyun     dmxFontPrivPtr pFontPriv;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun     if (!(pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex))) {
451*4882a593Smuzhiyun         xfont2_font_set_private(pFont, dmxFontPrivateIndex, NULL);
452*4882a593Smuzhiyun         pFontPriv = malloc(sizeof(dmxFontPrivRec));
453*4882a593Smuzhiyun         if (!pFontPriv)
454*4882a593Smuzhiyun             return FALSE;
455*4882a593Smuzhiyun         pFontPriv->font = NULL;
456*4882a593Smuzhiyun         MAXSCREENSALLOC(pFontPriv->font);
457*4882a593Smuzhiyun         if (!pFontPriv->font) {
458*4882a593Smuzhiyun             free(pFontPriv);
459*4882a593Smuzhiyun             return FALSE;
460*4882a593Smuzhiyun         }
461*4882a593Smuzhiyun         pFontPriv->refcnt = 0;
462*4882a593Smuzhiyun     }
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun     xfont2_font_set_private(pFont, dmxFontPrivateIndex, (void *) pFontPriv);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun     if (dmxScreen->beDisplay) {
467*4882a593Smuzhiyun         if (!dmxBELoadFont(pScreen, pFont))
468*4882a593Smuzhiyun             return FALSE;
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun         pFontPriv->refcnt++;
471*4882a593Smuzhiyun     }
472*4882a593Smuzhiyun     else {
473*4882a593Smuzhiyun         pFontPriv->font[pScreen->myNum] = NULL;
474*4882a593Smuzhiyun     }
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun     return TRUE;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun /** Free \a pFont on the back-end associated with \a pScreen. */
480*4882a593Smuzhiyun Bool
dmxBEFreeFont(ScreenPtr pScreen,FontPtr pFont)481*4882a593Smuzhiyun dmxBEFreeFont(ScreenPtr pScreen, FontPtr pFont)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
484*4882a593Smuzhiyun     dmxFontPrivPtr pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun     if (pFontPriv && pFontPriv->font[pScreen->myNum]) {
487*4882a593Smuzhiyun         XFreeFont(dmxScreen->beDisplay, pFontPriv->font[pScreen->myNum]);
488*4882a593Smuzhiyun         pFontPriv->font[pScreen->myNum] = NULL;
489*4882a593Smuzhiyun         return TRUE;
490*4882a593Smuzhiyun     }
491*4882a593Smuzhiyun 
492*4882a593Smuzhiyun     return FALSE;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /** Unrealize the font, \a pFont, on the back-end server associated with
496*4882a593Smuzhiyun  *  \a pScreen. */
497*4882a593Smuzhiyun Bool
dmxUnrealizeFont(ScreenPtr pScreen,FontPtr pFont)498*4882a593Smuzhiyun dmxUnrealizeFont(ScreenPtr pScreen, FontPtr pFont)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
501*4882a593Smuzhiyun     dmxFontPrivPtr pFontPriv;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun     if ((pFontPriv = FontGetPrivate(pFont, dmxFontPrivateIndex))) {
504*4882a593Smuzhiyun         /* In case the font failed to load properly */
505*4882a593Smuzhiyun         if (!pFontPriv->refcnt) {
506*4882a593Smuzhiyun             MAXSCREENSFREE(pFontPriv->font);
507*4882a593Smuzhiyun             free(pFontPriv);
508*4882a593Smuzhiyun             xfont2_font_set_private(pFont, dmxFontPrivateIndex, NULL);
509*4882a593Smuzhiyun         }
510*4882a593Smuzhiyun         else if (pFontPriv->font[pScreen->myNum]) {
511*4882a593Smuzhiyun             if (dmxScreen->beDisplay)
512*4882a593Smuzhiyun                 dmxBEFreeFont(pScreen, pFont);
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun             /* The code below is non-obvious, so here's an explanation...
515*4882a593Smuzhiyun              *
516*4882a593Smuzhiyun              * When creating the default GC, the server opens up the
517*4882a593Smuzhiyun              * default font once for each screen, which in turn calls
518*4882a593Smuzhiyun              * the RealizeFont function pointer once for each screen.
519*4882a593Smuzhiyun              * During this process both dix's font refcnt and DMX's font
520*4882a593Smuzhiyun              * refcnt are incremented once for each screen.
521*4882a593Smuzhiyun              *
522*4882a593Smuzhiyun              * Later, when shutting down the X server, dix shuts down
523*4882a593Smuzhiyun              * each screen in reverse order.  During this shutdown
524*4882a593Smuzhiyun              * procedure, each screen's default GC is freed and then
525*4882a593Smuzhiyun              * that screen is closed by calling the CloseScreen function
526*4882a593Smuzhiyun              * pointer.  screenInfo.numScreens is then decremented after
527*4882a593Smuzhiyun              * closing each screen.  This procedure means that the dix's
528*4882a593Smuzhiyun              * font refcnt for the font used by the default GC's is
529*4882a593Smuzhiyun              * decremented once for each screen # greater than 0.
530*4882a593Smuzhiyun              * However, since dix's refcnt for the default font is not
531*4882a593Smuzhiyun              * yet 0 for each screen greater than 0, no call to the
532*4882a593Smuzhiyun              * UnrealizeFont function pointer is made for those screens.
533*4882a593Smuzhiyun              * Then, when screen 0 is being closed, dix's font refcnt
534*4882a593Smuzhiyun              * for the default GC's font is finally 0 and the font is
535*4882a593Smuzhiyun              * unrealized.  However, since screenInfo.numScreens has
536*4882a593Smuzhiyun              * been decremented already down to 1, only one call to
537*4882a593Smuzhiyun              * UnrealizeFont is made (for screen 0).  Thus, even though
538*4882a593Smuzhiyun              * RealizeFont was called once for each screen,
539*4882a593Smuzhiyun              * UnrealizeFont is only called for screen 0.
540*4882a593Smuzhiyun              *
541*4882a593Smuzhiyun              * This is a bug in dix.
542*4882a593Smuzhiyun              *
543*4882a593Smuzhiyun              * To avoid the memory leak of pFontPriv for each server
544*4882a593Smuzhiyun              * generation, we can also free pFontPriv if the refcnt is
545*4882a593Smuzhiyun              * not yet 0 but the # of screens is 1 -- i.e., the case
546*4882a593Smuzhiyun              * described in the dix bug above.  This is only a temporary
547*4882a593Smuzhiyun              * workaround until the bug in dix is solved.
548*4882a593Smuzhiyun              *
549*4882a593Smuzhiyun              * The other problem is that the font structure allocated by
550*4882a593Smuzhiyun              * XLoadQueryFont() above is not freed for screens > 0.
551*4882a593Smuzhiyun              * This problem cannot be worked around here since the back-
552*4882a593Smuzhiyun              * end displays for screens > 0 have already been closed by
553*4882a593Smuzhiyun              * the time this code is called from dix.
554*4882a593Smuzhiyun              *
555*4882a593Smuzhiyun              * When the bug in dix described above is fixed, then we can
556*4882a593Smuzhiyun              * remove the "|| screenInfo.numScreens == 1" code below and
557*4882a593Smuzhiyun              * the memory leaks will be eliminated.
558*4882a593Smuzhiyun              */
559*4882a593Smuzhiyun             if (--pFontPriv->refcnt == 0
560*4882a593Smuzhiyun #if 1
561*4882a593Smuzhiyun                 /* Remove this code when the dix bug is fixed */
562*4882a593Smuzhiyun                 || screenInfo.numScreens == 1
563*4882a593Smuzhiyun #endif
564*4882a593Smuzhiyun                 ) {
565*4882a593Smuzhiyun                 MAXSCREENSFREE(pFontPriv->font);
566*4882a593Smuzhiyun                 free(pFontPriv);
567*4882a593Smuzhiyun                 xfont2_font_set_private(pFont, dmxFontPrivateIndex, NULL);
568*4882a593Smuzhiyun             }
569*4882a593Smuzhiyun         }
570*4882a593Smuzhiyun     }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun     return TRUE;
573*4882a593Smuzhiyun }
574