1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
|
/**************************************************************************/
/* */
/* Copyright (c) 2001, 2011 NoMachine, http://www.nomachine.com/. */
/* */
/* NXAGENT, NX protocol compression and NX extensions to this software */
/* are copyright of NoMachine. Redistribution and use of the present */
/* software is allowed according to terms specified in the file LICENSE */
/* which comes in the source distribution. */
/* */
/* Check http://www.nomachine.com/licensing.html for applicability. */
/* */
/* NX and NoMachine are trademarks of Medialogic S.p.A. */
/* */
/* All rights reserved. */
/* */
/**************************************************************************/
/*
Copyright 1993 by Davor Matic
Permission to use, copy, modify, distribute, and sell this software
and its documentation for any purpose is hereby granted without fee,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation. Davor Matic makes no representations about
the suitability of this software for any purpose. It is provided "as
is" without express or implied warranty.
*/
#include "scrnintstr.h"
#include "dixstruct.h"
#include <X11/fonts/font.h>
#include <X11/fonts/fontstruct.h>
#include "misc.h"
#include "miscstruct.h"
#include "opaque.h"
#include "Agent.h"
#include "Display.h"
#include "Font.h"
#include "Error.h"
#include <stdio.h>
#include <sys/stat.h>
#include "resource.h"
#include "Reconnect.h"
#include "Args.h"
#include <nx/NXlib.h>
#include <nx/NXalert.h>
#include <string.h>
#include <stdlib.h>
#define PANIC
#define WARNING
#undef TEST
#undef DEBUG
#define NXAGENT_DEFAULT_FONT_DIR "/usr/X11R6/lib/X11/fonts"
#define NXAGENT_ALTERNATE_FONT_DIR "/usr/share/X11/fonts"
#define NXAGENT_ALTERNATE_FONT_DIR_2 "/usr/share/fonts/X11"
#define NXAGENT_ALTERNATE_FONT_DIR_3 "/usr/share/fonts"
#define NXAGENT_ALTERNATE_FONT_DIR_4 "/usr/NX/share/fonts"
#define NXAGENT_DEFAULT_FONT_PATH \
"/usr/X11R6/lib/X11/fonts/misc/,/usr/X11R6/lib/X11/fonts/Speedo/,\
/usr/X11R6/lib/X11/fonts/Type1/,/usr/X11R6/lib/X11/fonts/75dpi/,\
/usr/X11R6/lib/X11/fonts/100dpi/,/usr/X11R6/lib/X11/fonts/TTF/,\
/usr/NX/share/fonts/base"
#define NXAGENT_ALTERNATE_FONT_PATH \
"/usr/share/X11/fonts/misc/,/usr/share/X11/fonts/Speedo/,\
/usr/share/X11/fonts/Type1/,/usr/share/X11/fonts/75dpi/,\
/usr/share/X11/fonts/100dpi/,/usr/share/X11/fonts/TTF/,\
/usr/NX/share/fonts/base"
#define NXAGENT_ALTERNATE_FONT_PATH_2 \
"/usr/share/fonts/X11/misc/,/usr/share/fonts/X11/Speedo/,\
/usr/share/fonts/X11/Type1/,/usr/share/fonts/X11/75dpi/,\
/usr/share/fonts/X11/100dpi/,/usr/share/fonts/X11/TTF/,\
/usr/NX/share/fonts/base"
#define NXAGENT_ALTERNATE_FONT_PATH_3 \
"/usr/share/fonts/misc/,/usr/share/fonts/Speedo/,\
/usr/share/fonts/Type1/,/usr/share/fonts/75dpi/,\
/usr/share/fonts/100dpi/,/usr/share/fonts/TTF/,\
/usr/NX/share/fonts/base"
#define NXAGENT_ALTERNATE_FONT_PATH_4 \
"/usr/NX/share/fonts/base"
#undef NXAGENT_FONTCACHE_DEBUG
#undef NXAGENT_RECONNECT_FONT_DEBUG
#undef NXAGENT_FONTMATCH_DEBUG
#define FIELDS 14
static int reconnectFlexibility;
static void nxagentCleanCacheAfterReconnect(void);
static void nxagentFontReconnect(FontPtr, XID, void *);
static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontPtr pFont);
static XFontStruct *nxagentLoadQueryFont(register Display *dpy , char *fontName , FontPtr pFont);
int nxagentFreeFont(XFontStruct *fs);
static Bool nxagentGetFontServerPath(char * fontServerPath);
static char * nxagentMakeScalableFontName(const char *fontName, int scalableResolution);
RESTYPE RT_NX_FONT;
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
static void printFontCacheDump(char*);
#endif
typedef struct _nxagentFontRec
{
char *name;
int status;
} nxagentFontRec, *nxagentFontRecPtr;
typedef struct _nxagentFontList
{
nxagentFontRecPtr *list;
int length;
int listSize;
} nxagentFontList, *nxagentFontListPtr;
nxagentFontList nxagentRemoteFontList = {NULL, (int)0, (int)0};
int nxagentFontPrivateIndex;
typedef struct _nxCacheFontEntry
{
Atom atom;
XFontStruct *font_struct;
char *name;
} nxCacheFontEntryRec, *nxCacheFontEntryRecPtr;
static struct _nxagentFontCache
{
nxCacheFontEntryRecPtr *entry;
int index;
int size;
} nxagentFontCache = { NULL, (int) 0, (int) 0 };
#define CACHE_ENTRY_PTR (nxagentFontCache.entry)
#define CACHE_INDEX (nxagentFontCache.index)
#define CACHE_SIZE (nxagentFontCache.size)
#define CACHE_ENTRY(A) (CACHE_ENTRY_PTR[A])
#define CACHE_FSTRUCT(A) (CACHE_ENTRY(A) -> font_struct)
#define CACHE_NAME(A) (CACHE_ENTRY(A) -> name)
#define CACHE_ATOM(A) (CACHE_ENTRY(A) -> atom)
static struct _nxagentFailedToReconnectFonts
{
FontPtr *font;
XID *id;
int size;
int index;
} nxagentFailedToReconnectFonts = {NULL, NULL, 0, 0};
/*
* This is used if nxagentFullGeneration is true
* in CloseDisplay().
*/
void nxagentFreeFontCache(void)
{
int i;
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: Freeing nxagent font cache\n");
#endif
if (CACHE_INDEX == 0)
return;
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: Freeing nxagent font cache, there are [%d] entries.\n", CACHE_INDEX);
#endif
for (i = 0; i < CACHE_INDEX; i++)
{
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: Freeing nxagent font cache entry [%d] entry pointer is [%p], name [%s]\n",
i, CACHE_ENTRY(i), CACHE_NAME(i));
#endif
if (CACHE_FSTRUCT(i))
{
nxagentFreeFont(CACHE_FSTRUCT(i));
}
free(CACHE_NAME(i));
free(CACHE_ENTRY(i));
}
free(CACHE_ENTRY_PTR);
CACHE_ENTRY_PTR = NULL;
CACHE_INDEX = 0;
CACHE_SIZE = 0;
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: nxagent font cache fully freed\n");
#endif
return;
}
void nxagentListRemoteFonts(const char *searchPattern, const int maxNames)
{
int i, q, p;
char **xList;
int xLen = 0;
const char *patterns[] = {"*", "-*-*-*-*-*-*-*-*-*-*-*-*-*-*"};
int patternsQt = 2;
if (NXDisplayError(nxagentDisplay) == 1)
{
return;
}
/*
* Avoid querying again the remote
* fonts.
*/
if (nxagentRemoteFontList.length > 0)
{
return;
}
/*
* We can't retrieve the full remote font
* list with a single query, because the
* number of dashes in the pattern acts as
* a rule to select how to search for the
* font names, so the pattern '*' is useful
* to retrive the font aliases, while the
* other one will select the 'real' fonts.
*/
for (p = 0; p < patternsQt; p++)
{
xList = XListFonts(nxagentDisplay, patterns[p], maxNames, &xLen);
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "nxagentListRemoteFonts: NXagent remote list [%s] has %d elements.\n", patterns[p], xLen);
#endif
/*
* Add the ListFont request pattern to the list with
* the last requested maxnames.
*/
nxagentListRemoteAddName(searchPattern, maxNames);
for (i = 0; i < xLen; i++)
{
q = 1;
nxagentListRemoteAddName(xList[i], q);
}
XFreeFontNames(xList);
}
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "nxagentListRemoteFonts: Printing remote font list.\n");
for (i = 0; i < nxagentRemoteFontList.length; i++)
{
fprintf(stderr, "Font# %d, \"%s\"\n", i, nxagentRemoteFontList.list[i]->name);
}
fprintf(stderr, "nxagentListRemoteFonts: End of list\n");
#endif
}
void nxagentListRemoteAddName(const char *name, int status)
{
int pos;
if (nxagentFontFind(name, &pos))
{
if (nxagentRemoteFontList.list[pos]->status < status)
{
nxagentRemoteFontList.list[pos]->status = status;
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "Font: Font# %d, [%s] change status to %s\n",
pos, nxagentRemoteFontList.list[pos]->name,nxagentRemoteFontList.list[pos]->status?"OK":"deleted");
#endif
}
return;
}
if (nxagentRemoteFontList.length == nxagentRemoteFontList.listSize)
{
nxagentRemoteFontList.list = realloc(nxagentRemoteFontList.list, sizeof(nxagentFontRecPtr)
* (nxagentRemoteFontList.listSize + 1000));
if (nxagentRemoteFontList.list == NULL)
{
FatalError("Font: remote list memory re-allocation failed!.\n");
}
nxagentRemoteFontList.listSize += 1000;
}
if (pos < nxagentRemoteFontList.length)
{
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "Font: Going to move list from %p to %p len = %d!.\n",
&nxagentRemoteFontList.list[pos], &nxagentRemoteFontList.list[pos+1],
(nxagentRemoteFontList.length - pos) * sizeof(nxagentFontRecPtr));
#endif
memmove(&nxagentRemoteFontList.list[pos+1],
&nxagentRemoteFontList.list[pos],
(nxagentRemoteFontList.length - pos) * sizeof(nxagentFontRecPtr));
}
if ((nxagentRemoteFontList.list[pos] = malloc(sizeof(nxagentFontRec))))
{
nxagentRemoteFontList.list[pos]->name = malloc(strlen(name) +1);
if (nxagentRemoteFontList.list[pos]->name == NULL)
{
fprintf(stderr, "Font: remote list name memory allocation failed!.\n");
return;
}
}
else
{
fprintf(stderr, "Font: remote list record memory allocation failed!.\n");
return;
}
strcpy(nxagentRemoteFontList.list[pos]->name,name);
nxagentRemoteFontList.list[pos]->status = status;
nxagentRemoteFontList.length++;
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "Font: remote font list added [%s] in position [%d] as %s !.\n",
name, pos, status ? "OK" : "deleted");
fprintf(stderr, "Font: remote font list total len is [%d] Size is [%d] !.\n",
nxagentRemoteFontList.length, nxagentRemoteFontList.listSize);
#endif
}
static void nxagentFreeRemoteFontList(nxagentFontList *listRec)
{
int l;
for (l = 0; l < listRec -> length; l++)
{
if (listRec -> list[l])
{
free(listRec -> list[l] -> name);
listRec -> list[l] -> name = NULL;
free(listRec -> list[l]);
listRec -> list[l] = NULL;
}
}
listRec -> length = listRec -> listSize = 0;
free(listRec -> list);
listRec -> list = NULL;
return;
}
Bool nxagentFontFind(const char *name, int *pos)
{
int low,high,res,iter,lpos;
if (!nxagentRemoteFontList.length)
{
*pos=0;
return False;
}
low = 0;
high = nxagentRemoteFontList.length - 1;
iter = 0;
res = 1;
lpos = nxagentRemoteFontList.length;
while (low <= high)
{
*pos = (high + low)/2;
iter ++;
res = strcasecmp(nxagentRemoteFontList.list[*pos]->name,name);
if (res > 0)
{
high = *pos - 1;
lpos = *pos;
continue;
}
else if (res < 0)
{
low = *pos + 1;
lpos = low;
continue;
}
break;
}
*pos = (res == 0)?*pos:lpos;
#ifdef NXAGENT_FONTMATCH_DEBUG
if (res == 0)
fprintf(stderr, "Font: font found in %d iterations in pos = %d\n", iter, *pos);
else
fprintf(stderr, "Font: not font found in %d iterations insertion pos is = %d\n", iter, *pos);
#endif
return (res == 0);
}
Bool nxagentFontLookUp(const char *name)
{
int i;
int result;
char *scalable;
if (name != NULL && strlen(name) == 0)
{
return 0;
}
result = nxagentFontFind(name, &i);
scalable = NULL;
/*
* Let's try with the scalable font description.
*/
if (result == 0)
{
scalable = nxagentMakeScalableFontName(name, 0);
if (scalable != NULL)
{
result = nxagentFontFind(scalable, &i);
free(scalable);
}
}
/*
* Let's try again after replacing zero to xdpi and ydpi in the pattern.
*/
if (result == 0)
{
scalable = nxagentMakeScalableFontName(name, 1);
if (scalable != NULL)
{
result = nxagentFontFind(scalable, &i);
free(scalable);
}
}
if (result == 0)
{
return 0;
}
else
{
return (nxagentRemoteFontList.list[i]->status > 0);
}
}
Bool nxagentRealizeFont(ScreenPtr pScreen, FontPtr pFont)
{
void * priv;
Atom name_atom, value_atom;
int nprops;
FontPropPtr props;
int i;
char *name;
char *origName = (char*) pScreen;
FontSetPrivate(pFont, nxagentFontPrivateIndex, NULL);
if (requestingClient && XpClientIsPrintClient(requestingClient, NULL))
return True;
name_atom = MakeAtom("FONT", 4, True);
value_atom = 0L;
nprops = pFont->info.nprops;
props = pFont->info.props;
for (i = 0; i < nprops; i++)
if ((Atom)props[i].name == name_atom) {
value_atom = props[i].value;
break;
}
if (!value_atom) return False;
name = (char *)NameForAtom(value_atom);
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: nxagentRealizeFont, realizing font: %s\n", validateString(name));
fprintf(stderr, " atom: %ld\n", value_atom);
fprintf(stderr, "Font: Cache dump:\n");
for (i = 0; i < CACHE_INDEX; i++)
{
fprintf(stderr, "nxagentFontCache.entry[%d]->name: %s font_struct at %p\n",
i, CACHE_NAME(i), CACHE_FSTRUCT(i));
}
#endif
if (!name) return False;
if ((strcasecmp(origName, name) != 0) && !strchr(origName,'*'))
{
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "Font: Changing font name to realize from [%s] to [%s]\n",
validateString(name), origName);
#endif
name = origName;
}
priv = (void *)malloc(sizeof(nxagentPrivFont));
FontSetPrivate(pFont, nxagentFontPrivateIndex, priv);
nxagentFontPriv(pFont) -> mirrorID = 0;
for (i = 0; i < nxagentFontCache.index; i++)
{
/* if (value_atom == CACHE_ATOM(i))*/
if (strcasecmp(CACHE_NAME(i), name) == 0)
{
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: nxagentFontCache hit [%s] = [%s]!\n", CACHE_NAME(i), validateString(name));
#endif
break;
}
}
if (i < CACHE_INDEX)
{
nxagentFontPriv(pFont)->font_struct = CACHE_FSTRUCT(i);
strcpy(nxagentFontPriv(pFont)->fontName, name);
}
else
{
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: nxagentFontCache fail.\n");
#endif
if (CACHE_INDEX == CACHE_SIZE)
{
CACHE_ENTRY_PTR = realloc(CACHE_ENTRY_PTR, sizeof(nxCacheFontEntryRecPtr) * (CACHE_SIZE + 100));
if (CACHE_ENTRY_PTR == NULL)
{
FatalError("Font: Cache list memory re-allocation failed.\n");
}
CACHE_SIZE += 100;
}
CACHE_ENTRY(CACHE_INDEX) = malloc(sizeof(nxCacheFontEntryRec));
if (CACHE_ENTRY(CACHE_INDEX) == NULL)
{
return False;
}
CACHE_NAME(CACHE_INDEX) = malloc(strlen(name) + 1);
if (CACHE_NAME(CACHE_INDEX) == NULL)
{
return False;
}
#ifdef NXAGENT_FONTMATCH_DEBUG
fprintf(stderr, "Font: Going to realize font [%s],[%s] on real X server.\n", validateString(name), origName);
#endif
if (nxagentRemoteFontList.length == 0 && (NXDisplayError(nxagentDisplay) == 0))
{
nxagentListRemoteFonts("*", nxagentMaxFontNames);
}
nxagentFontPriv(pFont)->font_struct = nxagentLoadQueryFont(nxagentDisplay, name, pFont);
strcpy(nxagentFontPriv(pFont)->fontName, name);
if (nxagentFontPriv(pFont)->font_struct != NULL)
{
CACHE_ATOM(i) = value_atom;
strcpy(CACHE_NAME(i), name);
CACHE_FSTRUCT(i) = nxagentFontPriv(pFont)->font_struct;
CACHE_INDEX++;
nxagentFontPriv(pFont) -> mirrorID = FakeClientID(serverClient -> index);
AddResource(nxagentFontPriv(pFont) -> mirrorID, RT_NX_FONT, pFont);
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "Font: nxagentFontCache adds font [%s] in pos. [%d].\n",
validateString(name), CACHE_INDEX - 1);
#endif
}
}
#ifdef NXAGENT_FONTMATCH_DEBUG
if (nxagentFontPriv(pFont)->font_struct == NULL)
{
if (nxagentFontLookUp(name) == False)
{
fprintf(stderr, "Font: nxagentRealizeFont failed with font Font=%s, not in our remote list\n",
validateString(name));
}
else
{
fprintf(stderr, "Font: nxagentRealizeFont failed with font Font=%s but the font is in our remote list\n",
validateString(name));
}
}
else
fprintf(stderr, "Font: nxagentRealizeFont OK realizing font Font=%s\n",
validateString(name));
#endif
return (nxagentFontPriv(pFont)->font_struct != NULL);
}
Bool nxagentUnrealizeFont(ScreenPtr pScreen, FontPtr pFont)
{
if (nxagentFontPriv(pFont))
{
if (NXDisplayError(nxagentDisplay) == 0)
{
if (nxagentFontStruct(pFont))
{
int i;
for (i = 0; i < CACHE_INDEX; i++)
{
if (CACHE_FSTRUCT(i) == nxagentFontStruct(pFont))
{
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "nxagentUnrealizeFont: Not freeing the font in cache.\n");
#endif
break;
}
}
if (i == CACHE_INDEX)
{
/*
* This font is not in the cache.
*/
#ifdef NXAGENT_FONTCACHE_DEBUG
fprintf(stderr, "nxagentUnrealizeFont: Freeing font not found in cache '%d'\n",
CACHE_ATOM(i));
#endif
XFreeFont(nxagentDisplay, nxagentFontStruct(pFont));
}
}
}
if (nxagentFontPriv(pFont) -> mirrorID)
FreeResource(nxagentFontPriv(pFont) -> mirrorID, RT_NONE);
free(nxagentFontPriv(pFont));
FontSetPrivate(pFont, nxagentFontPrivateIndex, NULL);
}
return True;
}
int nxagentDestroyNewFontResourceType(void * p, XID id)
{
#ifdef TEST
fprintf(stderr, "nxagentDestroyNewFontResourceType: Destroying mirror id [%ld] for font at [%p].\n",
nxagentFontPriv((FontPtr) p) -> mirrorID, (void *) p);
#endif
/*
FIXME: It happens that this resource had been already
destroied. We should verify if the same font is
assigned both to the server client and another
client. We had a crash when freeing server client
resources.
*/
if (nxagentFontPriv((FontPtr) p) != NULL)
{
nxagentFontPriv((FontPtr) p) -> mirrorID = None;
}
return 1;
}
static XFontStruct *nxagentLoadBestQueryFont(Display* dpy, char *fontName, FontPtr pFont)
{
XFontStruct *fontStruct;
char *substFontBuf;
/* X Logical Font Description Conventions
* require 14 fields in the font names.
*
*/
char *searchFields[FIELDS+1];
char *fontNameFields[FIELDS+1];
int i;
int j;
int numSearchFields = 0;
int numFontFields = 0;
int weight = 0;
int tempWeight = 1;
int fieldOrder[14] = { 4, /* Slant */
11, /* Spacing */
12, /* Width info */
13, /* Charset */
14, /* Language */
7, /* Height */
6, /* Add-style */
3, /* Weight */
2, /* Name */
1, /* Foundry */
9, /* DPI_x */
5, /* Set-width */
8, /* Point size */
10 /* DPI_y */
};
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentLoadBestQueryFont: Searching font '%s' .\n", fontName);
#endif
substFontBuf = (char *) malloc(sizeof(char) * 512);
numFontFields = nxagentSplitString(fontName, fontNameFields, FIELDS + 1, "-");
memcpy(substFontBuf, "fixed\0", strlen("fixed") + 1);
if (numFontFields <= FIELDS)
{
#ifdef WARNING
if (nxagentVerbose == 1)
{
fprintf(stderr, "nxagentLoadBestQueryFont: WARNING! Font name in non standard format. \n");
}
#endif
}
else
{
for (i = 1 ; i < nxagentRemoteFontList.length ; i++)
{
numSearchFields = nxagentSplitString(nxagentRemoteFontList.list[i]->name, searchFields, FIELDS+1, "-");
/* The following code attemps to find an accurate approximation
* of the missing font. The current candidate and the missing font are
* compared on the 14 fields of the X Logical Font Description Convention.
* The selection is performed by the analysis of the matching fields,
* shifting left the value of the Weight variable on the right matches
* and shifting right the value of the Weight on the wrong ones;
* due a probability of overmuch right shifting, the starting weight is set
* to a high value. At the end of matching the selected font is the one
* with the bigger final Weight. The shift operation has been used instead
* of other operation for a performance issue.
* In some check the shift is performed by more than one position, because
* of the relevance of the field; for example a correct slant or a matching
* charset is more relevant than the size.
*/
if (numSearchFields > FIELDS)
{
tempWeight = 0;
for (j = 0; j < FIELDS; j++)
{
if (strcasecmp(searchFields[fieldOrder[j]], fontNameFields[fieldOrder[j]]) == 0 ||
strcmp(searchFields[fieldOrder[j]], "") == 0 ||
strcmp(fontNameFields[fieldOrder[j]], "") != 0 ||
strcmp(searchFields[fieldOrder[j]], "*") == 0 ||
strcmp(fontNameFields[fieldOrder[j]], "*") == 0)
{
tempWeight ++;
}
tempWeight <<= 1;
}
}
if (tempWeight > weight)
{
/* Found more accurate font */
weight = tempWeight;
memcpy(substFontBuf, nxagentRemoteFontList.list[i]->name, strlen(nxagentRemoteFontList.list[i]->name));
substFontBuf[strlen(nxagentRemoteFontList.list[i]->name)] = '\0';
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentLoadBestQueryFont: Weight '%d' of more accurate font '%s' .\n", weight, substFontBuf);
#endif
}
for (j = 0; j < numSearchFields; j++)
{
if (searchFields[j] != NULL)
{
free(searchFields[j]);
}
}
}
}
#ifdef WARNING
if (nxagentVerbose == 1)
{
fprintf(stderr, "nxagentLoadBestQueryFont: WARNING! Failed to load font '%s'. Replacing with '%s'.\n",
fontName, substFontBuf);
}
#endif
fontStruct = nxagentLoadQueryFont(dpy, substFontBuf, pFont);
free (substFontBuf);
for (j = 0; j < numFontFields; j++)
{
if (fontNameFields[j] != NULL)
{
free(fontNameFields[j]);
}
}
return fontStruct;
}
static void nxagentFontDisconnect(FontPtr pFont, XID param1, void * param2)
{
nxagentPrivFont *privFont;
Bool *pBool = (Bool*)param2;
int i;
if (pFont == NULL || !*pBool)
return;
privFont = nxagentFontPriv(pFont);
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFontDisconnect: pFont %p, XID %lx\n",
(void *) pFont, privFont -> font_struct ? nxagentFont(pFont) : 0);
#endif
for (i = 0; i < CACHE_INDEX; i++)
{
if (strcasecmp(CACHE_NAME(i), privFont -> fontName) == 0)
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFontDisconnect: font %s found in cache at position %d\n",
privFont -> fontName, i);
#endif
privFont -> font_struct = NULL;
return;
}
}
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFontDisconnect: WARNING font %s not found in cache freeing it now\n",
privFont -> fontName);
#endif
if (privFont -> font_struct)
{
XFreeFont(nxagentDisplay, privFont -> font_struct);
privFont -> font_struct = NULL;
}
}
static void nxagentCollectFailedFont(FontPtr fpt, XID id)
{
if (nxagentFailedToReconnectFonts.font == NULL)
{
nxagentFailedToReconnectFonts.size = 8;
nxagentFailedToReconnectFonts.font = malloc(nxagentFailedToReconnectFonts.size *
sizeof(FontPtr));
nxagentFailedToReconnectFonts.id = malloc(nxagentFailedToReconnectFonts.size * sizeof(XID));
if (nxagentFailedToReconnectFonts.font == NULL || nxagentFailedToReconnectFonts.id == NULL)
{
FatalError("Font: font not reconnected memory allocation failed!.\n");
}
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentCollectFailedFont: allocated [%d] bytes.\n",
8 * (sizeof(FontPtr)+ sizeof(XID)));
#endif
}
else if (nxagentFailedToReconnectFonts.index == nxagentFailedToReconnectFonts.size - 1)
{
nxagentFailedToReconnectFonts.size *= 2;
nxagentFailedToReconnectFonts.font = realloc(nxagentFailedToReconnectFonts.font,
nxagentFailedToReconnectFonts.size *
sizeof(FontPtr));
nxagentFailedToReconnectFonts.id = realloc(nxagentFailedToReconnectFonts.id,
nxagentFailedToReconnectFonts.size *
sizeof(XID));
if (nxagentFailedToReconnectFonts.font == NULL || nxagentFailedToReconnectFonts.id == NULL)
{
FatalError("Font: font not reconnected memory re-allocation failed!.\n");
}
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr,"nxagentCollectFailedFont: reallocated memory.\n ");
#endif
}
nxagentFailedToReconnectFonts.font[nxagentFailedToReconnectFonts.index] = fpt;
nxagentFailedToReconnectFonts.id[nxagentFailedToReconnectFonts.index] = id;
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentCollectFailedFont: font not reconnected at [%p], "
"put in nxagentFailedToReconnectFonts.font[%d] = [%p], with XID = [%lu].\n",
(void*) fpt, nxagentFailedToReconnectFonts.index,
(void *)nxagentFailedToReconnectFonts.font[nxagentFailedToReconnectFonts.index],
nxagentFailedToReconnectFonts.id[nxagentFailedToReconnectFonts.index]);
#endif
nxagentFailedToReconnectFonts.index++;
}
static void nxagentFontReconnect(FontPtr pFont, XID param1, void * param2)
{
int i;
nxagentPrivFont *privFont;
Bool *pBool = (Bool*)param2;
if (pFont == NULL)
return;
privFont = nxagentFontPriv(pFont);
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFontReconnect: pFont %p - XID %lx - name %s\n",
(void*) pFont, (privFont -> font_struct) ? nxagentFont(pFont) : 0,
privFont -> fontName);
#endif
for (i = 0; i < CACHE_INDEX; i++)
{
if (strcasecmp(CACHE_NAME(i), privFont -> fontName) == 0)
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "\tfound in cache");
#endif
if (!CACHE_FSTRUCT(i))
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, " --- font struct not valid\n");
#endif
break;
}
nxagentFontStruct(pFont) = CACHE_FSTRUCT(i);
return;
}
}
if (i == CACHE_INDEX)
{
FatalError("nxagentFontReconnect: font not found in cache.");
}
privFont -> font_struct = nxagentLoadQueryFont(nxagentDisplay, privFont -> fontName, pFont);
if ((privFont -> font_struct == NULL) && reconnectFlexibility)
{
privFont -> font_struct = nxagentLoadBestQueryFont(nxagentDisplay, privFont -> fontName, pFont);
}
if (privFont->font_struct != NULL)
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "\tXID %lx\n", privFont -> font_struct -> fid);
#endif
CACHE_FSTRUCT(i) = privFont -> font_struct;
}
else
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFontReconnect: failed\n");
#endif
nxagentCollectFailedFont(pFont, param1);
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFontReconnect: reconnection of font [%s] failed.\n",
privFont -> fontName);
#endif
nxagentSetReconnectError(FAILED_RESUME_FONTS_ALERT,
"Couldn't restore the font '%s'", privFont -> fontName);
*pBool = False;
}
return;
}
static void nxagentFreeCacheBeforeReconnect(void)
{
int i;
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
printFontCacheDump("nxagentFreeCacheBeforeReconnect");
#endif
for (i = 0; i < CACHE_INDEX; i++)
{
if (CACHE_FSTRUCT(i))
{
nxagentFreeFont(CACHE_FSTRUCT(i));
CACHE_FSTRUCT(i) = NULL;
}
}
}
static void nxagentCleanCacheAfterReconnect(void)
{
int i, j;
int real_size = CACHE_INDEX;
nxCacheFontEntryRecPtr swapEntryPtr;
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
printFontCacheDump("nxagentCleanCacheAfterReconnect");
#endif
for (i = 0; i < CACHE_INDEX; i++)
{
if(CACHE_FSTRUCT(i) == NULL)
{
XFree(CACHE_NAME(i));
real_size--;
}
}
for (i = 0; i < real_size; i++)
{
/* Find - first bad occurrence if exist. */
while ((i < real_size) && CACHE_FSTRUCT(i)) i++;
/* Really nothing more to do. */
if (i == real_size)
break;
/*
* Find - first good occurrence (moving backward from right end) entry in
* order to replace the bad one.
*/
for (j = CACHE_INDEX - 1; CACHE_FSTRUCT(j) == NULL; j--);
/*
* Now we can swap the two entry
* and reduce the Cache index
*/
swapEntryPtr = CACHE_ENTRY(i);
CACHE_ENTRY(i) = CACHE_ENTRY(j);
CACHE_ENTRY(j) = swapEntryPtr;
}
CACHE_INDEX = real_size;
}
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
static void printFontCacheDump(char* msg)
{
int i;
fprintf(stderr, "%s - begin -\n", msg);
for (i = 0; i < CACHE_INDEX; i++)
{
if (CACHE_FSTRUCT(i))
{
fprintf(stderr, "\tXID %lx - %s\n", CACHE_FSTRUCT(i) -> fid, CACHE_NAME(i));
}
else
{
fprintf(stderr, "\tdestroyed - %s\n", CACHE_NAME(i));
}
}
fprintf(stderr, "%s - end -\n", msg);
}
#endif
Bool nxagentReconnectAllFonts(void *p0)
{
int cid;
Bool fontSuccess = True;
reconnectFlexibility = *((int *) p0);
#if defined(NXAGENT_RECONNECT_DEBUG) || defined(NXAGENT_RECONNECT_FONT_DEBUG)
fprintf(stderr, "nxagentReconnectAllFonts\n");
#endif
/*
* The resource type RT_NX_FONT is created on the
* server client only, so we can avoid to loop
* through all the clients.
*/
FindClientResourcesByType(clients[serverClient -> index], RT_NX_FONT,
(FindResType) nxagentFontReconnect, &fontSuccess);
for (cid = 0; cid < MAXCLIENTS; cid++)
{
if (clients[cid])
{
FindClientResourcesByType(clients[cid], RT_FONT,
(FindResType) nxagentFontReconnect, &fontSuccess);
}
}
if (fontSuccess)
{
nxagentCleanCacheAfterReconnect();
}
return fontSuccess;
}
static void nxagentFailedFontReconnect(FontPtr pFont, XID param1, void * param2)
{
int i;
nxagentPrivFont *privFont;
Bool *pBool = (Bool*)param2;
if (pFont == NULL)
return;
privFont = nxagentFontPriv(pFont);
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFailedFontReconnect: pFont %p - XID %lx - name %s\n",
(void*) pFont, (privFont -> font_struct) ? nxagentFont(pFont) : 0,
privFont -> fontName);
#endif
for (i = 0; i < CACHE_INDEX; i++)
{
if (strcasecmp(CACHE_NAME(i), privFont -> fontName) == 0)
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "\tfound in cache");
#endif
if (!CACHE_FSTRUCT(i))
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, " --- font struct not valid\n");
#endif
break;
}
nxagentFontStruct(pFont) = CACHE_FSTRUCT(i);
return;
}
}
if (i == CACHE_INDEX)
{
FatalError("nxagentFailedFontReconnect: font not found in cache.");
}
privFont -> font_struct = nxagentLoadQueryFont(nxagentDisplay, privFont -> fontName, pFont);
if (privFont -> font_struct == NULL)
{
privFont -> font_struct = nxagentLoadBestQueryFont(nxagentDisplay, privFont -> fontName, pFont);
}
if (privFont->font_struct != NULL)
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "\tXID %lx\n", privFont -> font_struct -> fid);
#endif
CACHE_FSTRUCT(i) = privFont -> font_struct;
}
else
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFailedFontReconnect: failed\n");
#endif
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentFailedFontReconnect: reconnection of font [%s] failed.\n",
privFont -> fontName);
#endif
nxagentSetReconnectError(FAILED_RESUME_FONTS_ALERT,
"Couldn't restore the font '%s'", privFont -> fontName);
*pBool = False;
}
return;
}
static void nxagentFreeFailedToReconnectFonts()
{
if (nxagentFailedToReconnectFonts.font != NULL)
{
free(nxagentFailedToReconnectFonts.font);
nxagentFailedToReconnectFonts.font = NULL;
}
if (nxagentFailedToReconnectFonts.id != NULL)
{
free(nxagentFailedToReconnectFonts.id);
nxagentFailedToReconnectFonts.id = NULL;
}
nxagentFailedToReconnectFonts.size = 0;
nxagentFailedToReconnectFonts.index = 0;
}
Bool nxagentReconnectFailedFonts(void *p0)
{
int i;
int attempt = 1;
const int maxAttempt = 5;
char **fontPaths, **localFontPaths, **newFontPaths;
char fontServerPath[256] = "";
int nPaths = 0;
Bool repeat = True;
Bool fontSuccess = True;
reconnectFlexibility = *((int *) p0);
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentReconnectFailedFonts: \n");
#endif
if (nxagentGetFontServerPath(fontServerPath) == False)
{
#ifdef WARNING
fprintf(stderr, "nxagentReconnectFailedFonts: WARNING! "
"Font server tunneling not retrieved.\n");
#endif
}
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentReconnectFailedFonts: font server path [%s]\n", fontServerPath);
#endif
fontPaths = XGetFontPath(nxagentDisplay, &nPaths);
if ((newFontPaths = malloc((nPaths + 1) * sizeof(char *))) == NULL)
{
FatalError("nxagentReconnectFailedFonts: malloc failed.");
}
memcpy(newFontPaths, fontPaths, nPaths * sizeof(char*));
localFontPaths = newFontPaths;
localFontPaths += nPaths;
*localFontPaths = fontServerPath;
while(repeat)
{
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentReconnectFailedFonts: attempt [%d].\n", attempt);
#endif
repeat = False;
XSetFontPath(nxagentDisplay, newFontPaths, nPaths + 1);
nxagentFreeRemoteFontList(&nxagentRemoteFontList);
nxagentListRemoteFonts("*", nxagentMaxFontNames);
for(i = 0; i < nxagentFailedToReconnectFonts.index; i++)
{
fontSuccess = True;
if(nxagentFailedToReconnectFonts.font[i])
{
nxagentFailedFontReconnect(nxagentFailedToReconnectFonts.font[i],
nxagentFailedToReconnectFonts.id[i],
&fontSuccess);
if (fontSuccess)
{
nxagentFailedToReconnectFonts.font[i] = NULL;
}
else
{
repeat = True;
}
}
}
attempt++;
if (attempt > maxAttempt)
{
nxagentFreeFailedToReconnectFonts();
XSetFontPath(nxagentDisplay, fontPaths, nPaths);
nxagentFreeRemoteFontList(&nxagentRemoteFontList);
nxagentListRemoteFonts("*", nxagentMaxFontNames);
XFreeFontPath(fontPaths);
free(newFontPaths);
return False;
}
}
nxagentFreeFailedToReconnectFonts();
XSetFontPath(nxagentDisplay, fontPaths, nPaths);
XFreeFontPath(fontPaths);
free(newFontPaths);
nxagentCleanCacheAfterReconnect();
return True;
}
Bool nxagentDisconnectAllFonts()
{
int cid;
Bool fontSuccess = True;
#if defined(NXAGENT_RECONNECT_DEBUG) || defined(NXAGENT_RECONNECT_FONT_DEBUG)
fprintf(stderr, "nxagentDisconnectAllFonts\n");
#endif
nxagentFreeRemoteFontList(&nxagentRemoteFontList);
nxagentFreeCacheBeforeReconnect();
/*
* The resource type RT_NX_FONT is created on the
* server client only, so we can avoid to loop
* through all the clients.
*/
FindClientResourcesByType(clients[serverClient -> index], RT_NX_FONT,
(FindResType) nxagentFontDisconnect, &fontSuccess);
for(cid = 0; cid < MAXCLIENTS; cid++)
{
if( clients[cid] && fontSuccess )
{
FindClientResourcesByType(clients[cid], RT_FONT,
(FindResType) nxagentFontDisconnect, &fontSuccess);
}
}
return True;
}
static Bool nxagentGetFontServerPath(char * fontServerPath)
{
char path[256];
if (NXGetFontParameters(nxagentDisplay, 256, path) == True)
{
if (*path != '\0')
{
strncpy(fontServerPath, path + 1, *path);
*(fontServerPath + *path) = '\0';
#ifdef TEST
fprintf(stderr, "nxagentGetFontServerPath: Got path [%s].\n",
fontServerPath);
#endif
}
else
{
#ifdef TEST
fprintf(stderr, "nxagentGetFontServerPath: WARNING! Font server tunneling not enabled.\n");
#endif
return False;
}
}
else
{
#ifdef TEST
fprintf(stderr, "nxagentGetFontServerPath: WARNING! Failed to get path for font server tunneling.\n");
#endif
return False;
}
return True;
}
void nxagentVerifyDefaultFontPath(void)
{
struct stat dirStat;
static char *fontPath;
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Going to search for one or more valid font paths.\n");
#endif
fontPath = malloc(strlen(defaultFontPath) + 1);
if (fontPath == NULL)
{
#ifdef WARNING
fprintf(stderr, "nxagentVerifyDefaultFontPath: WARNING! Unable to allocate memory for a new font path. "
"Using the default font path [%s].\n", validateString(defaultFontPath));
#endif
return;
}
/*
* Set the default font path as the first choice.
*/
strcpy(fontPath, defaultFontPath);
if (stat(NXAGENT_DEFAULT_FONT_DIR, &dirStat) == 0 &&
S_ISDIR(dirStat.st_mode) != 0)
{
/*
* Let's use the old "/usr/X11R6/lib/X11/fonts" style.
*/
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Assuming fonts in directory [%s].\n",
validateString(NXAGENT_DEFAULT_FONT_DIR));
#endif
if (*fontPath != '\0')
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_DEFAULT_FONT_PATH) + 2);
strcat(fontPath, ",");
}
else
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_DEFAULT_FONT_PATH) + 1);
}
strcat(fontPath, NXAGENT_DEFAULT_FONT_PATH);
}
if (stat(NXAGENT_ALTERNATE_FONT_DIR, &dirStat) == 0 &&
S_ISDIR(dirStat.st_mode) != 0)
{
/*
* Let's use the new "/usr/share/X11/fonts" path.
*/
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Assuming fonts in directory [%s].\n",
validateString(NXAGENT_ALTERNATE_FONT_DIR));
#endif
if (*fontPath != '\0')
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH) + 2);
strcat(fontPath, ",");
}
else
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH) + 1);
}
strcat(fontPath, NXAGENT_ALTERNATE_FONT_PATH);
}
if (stat(NXAGENT_ALTERNATE_FONT_DIR_2, &dirStat) == 0 &&
S_ISDIR(dirStat.st_mode) != 0)
{
/*
* Let's use the "/usr/share/fonts/X11" path.
*/
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Assuming fonts in directory [%s].\n",
validateString(NXAGENT_ALTERNATE_FONT_DIR_2));
#endif
if (*fontPath != '\0')
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH_2) + 2);
strcat(fontPath, ",");
}
else
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH_2) + 1);
}
strcat(fontPath, NXAGENT_ALTERNATE_FONT_PATH_2);
}
if (stat(NXAGENT_ALTERNATE_FONT_DIR_3, &dirStat) == 0 &&
S_ISDIR(dirStat.st_mode) != 0)
{
/*
* Let's use the "/usr/share/fonts" path.
*/
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Assuming fonts in directory [%s].\n",
validateString(NXAGENT_ALTERNATE_FONT_DIR_3));
#endif
if (*fontPath != '\0')
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH_3) + 2);
strcat(fontPath, ",");
}
else
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH_3) + 1);
}
strcat(fontPath, NXAGENT_ALTERNATE_FONT_PATH_3);
}
if (stat(NXAGENT_ALTERNATE_FONT_DIR_4, &dirStat) == 0 &&
S_ISDIR(dirStat.st_mode) != 0)
{
/*
* Let's use the "/usr/NX/share/fonts" path.
*/
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Assuming fonts in directory [%s].\n",
validateString(NXAGENT_ALTERNATE_FONT_DIR_4));
#endif
if (*fontPath != '\0')
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH_4) + 2);
strcat(fontPath, ",");
}
else
{
fontPath = realloc(fontPath, strlen(fontPath) + strlen(NXAGENT_ALTERNATE_FONT_PATH_4) + 1);
}
strcat(fontPath, NXAGENT_ALTERNATE_FONT_PATH_4);
}
if (*fontPath == '\0')
{
#ifdef WARNING
fprintf(stderr, "nxagentVerifyDefaultFontPath: WARNING! Can't find a valid font directory.\n");
fprintf(stderr, "nxagentVerifyDefaultFontPath: WARNING! Using font path [%s].\n",
validateString(defaultFontPath));
#endif
}
else
{
defaultFontPath = fontPath;
#ifdef TEST
fprintf(stderr, "nxagentVerifyDefaultFontPath: Using font path [%s].\n",
validateString(defaultFontPath));
#endif
}
return;
}
XFontStruct* nxagentLoadQueryFont(register Display *dpy, char *name, FontPtr pFont)
{
XFontStruct* fs;
xCharInfo *xcip;
fs = (XFontStruct *) malloc (sizeof (XFontStruct));
if (fs == NULL)
{
#ifdef WARNING
fprintf(stderr, "nxagentLoadQueryFont: WARNING! Failed allocation of XFontStruct.\n");
#endif
return (XFontStruct *)NULL;
}
#ifdef NXAGENT_RECONNECT_FONT_DEBUG
fprintf(stderr, "nxagentLoadQueryFont: Looking for font '%s'.\n", name);
#endif
if (nxagentFontLookUp(name) == 0)
{
#ifdef DEBUG
fprintf(stderr, "nxagentLoadQueryFont: WARNING! Font not found '%s'.\n", name);
#endif
free(fs);
return (XFontStruct *) NULL;
}
fs -> ext_data = NULL; /* Hook for extension to hang data.*/
fs -> fid = XLoadFont(dpy, name); /* Font id for this font. */
fs -> direction = pFont->info.drawDirection; /* Hint about the direction font is painted. */
fs -> min_char_or_byte2 = pFont->info.firstCol; /* First character. */
fs -> max_char_or_byte2 = pFont->info.lastCol; /* Last character. */
fs -> min_byte1 = pFont->info.firstRow; /* First row that exists. */
fs -> max_byte1 = pFont->info.lastRow; /* Last row that exists. */
fs -> all_chars_exist = pFont->info.allExist; /* Flag if all characters have nonzero size. */
fs -> default_char = pFont->info.defaultCh; /* Char to print for undefined character. */
fs -> n_properties = pFont->info.nprops; /* How many properties there are. */
/*
* If no properties defined for the font, then it is bad
* font, but shouldn't try to read nothing.
*/
if (fs -> n_properties > 0)
{
register long nbytes;
nbytes = pFont -> info.nprops * sizeof(XFontProp);
fs -> properties = (XFontProp *) malloc((unsigned) nbytes);
if (fs -> properties == NULL)
{
#ifdef WARNING
fprintf(stderr, "nxagentLoadQueryFont: WARNING! Failed allocation of XFontProp.");
#endif
free((char *) fs);
return (XFontStruct *) NULL;
}
memmove(fs -> properties, pFont -> info.props, nbytes);
}
xcip = (xCharInfo *) &pFont -> info.ink_minbounds;
fs -> min_bounds.lbearing = cvtINT16toShort(xcip -> leftSideBearing);
fs -> min_bounds.rbearing = cvtINT16toShort(xcip -> rightSideBearing);
fs -> min_bounds.width = cvtINT16toShort(xcip -> characterWidth);
fs -> min_bounds.ascent = cvtINT16toShort(xcip -> ascent);
fs -> min_bounds.descent = cvtINT16toShort(xcip -> descent);
fs -> min_bounds.attributes = xcip -> attributes;
xcip = (xCharInfo *) &pFont -> info.ink_maxbounds;
fs -> max_bounds.lbearing = cvtINT16toShort(xcip -> leftSideBearing);
fs -> max_bounds.rbearing = cvtINT16toShort(xcip -> rightSideBearing);
fs -> max_bounds.width = cvtINT16toShort(xcip -> characterWidth);
fs -> max_bounds.ascent = cvtINT16toShort(xcip -> ascent);
fs -> max_bounds.descent = cvtINT16toShort(xcip -> descent);
fs -> max_bounds.attributes = xcip -> attributes;
fs -> per_char = NULL; /* First_char to last_char information. */
fs -> ascent = pFont->info.fontAscent; /* Logical extent above baseline for spacing. */
fs -> descent = pFont->info.fontDescent; /* Logical decent below baseline for spacing. */
return fs;
}
int nxagentFreeFont(XFontStruct *fs)
{
if (fs -> per_char)
{
#ifdef USE_XF86BIGFONT
_XF86BigfontFreeFontMetrics(fs);
#else
free ((char *) fs->per_char);
#endif
}
if (fs -> properties)
{
free (fs->properties);
}
XFree(fs);
return 1;
}
int nxagentSplitString(char *string, char *fields[], int nfields, char *sep)
{
int seplen;
int fieldlen;
int last;
int len;
int i;
char *current;
char *next;
seplen = strlen(sep);
len = strlen(string);
current = string;
i = 0;
last = 0;
for (;;)
{
next = NULL;
if (current < string + len)
{
next = strstr(current, sep);
}
if (next == NULL)
{
next = string + len;
last = 1;
}
fieldlen = next - current;
if (i < nfields)
{
fields[i] = (char *) malloc(fieldlen + 1);
strncpy(fields[i], current, fieldlen);
*(fields[i] + fieldlen) = 0;
}
else
{
fields[i] = NULL;
}
current = next + seplen;
i++;
if (last == 1)
{
break;
}
}
return i;
}
char *nxagentMakeScalableFontName(const char *fontName, int scalableResolution)
{
char *scalableFontName;
const char *s;
int len;
int field;
len = strlen(fontName) + 1;
scalableFontName = malloc(len);
if (scalableFontName == NULL)
{
#ifdef PANIC
fprintf(stderr, "nxagentMakeScalableFontName: PANIC! malloc() failed.\n");
#endif
return NULL;
}
scalableFontName[0] = 0;
if (*fontName != '-')
{
goto MakeScalableFontNameError;
}
s = fontName;
field = 0;
while (s != NULL)
{
s = strchr(s + 1, '-');
if (s != NULL)
{
if (field == 6 || field == 7 || field == 11)
{
/*
* PIXEL_SIZE || POINT_SIZE || AVERAGE_WIDTH
*/
strcat(scalableFontName, "-0");
}
else if (scalableResolution == 1 && (field == 8 || field == 9))
{
/*
* RESOLUTION_X || RESOLUTION_Y
*/
strcat(scalableFontName, "-0");
}
else
{
strncat(scalableFontName, fontName, s - fontName);
}
fontName = s;
}
else
{
strcat(scalableFontName, fontName);
}
field++;
}
if (field != 14)
{
goto MakeScalableFontNameError;
}
return scalableFontName;
MakeScalableFontNameError:
free(scalableFontName);
#ifdef DEBUG
fprintf(stderr, "nxagentMakeScalableFontName: Invalid font name.\n");
#endif
return NULL;
}
|