Rune - Fix thread issue
[rune.git] / ext_x11 / text.c
1 /*
2  * TEXT.C
3  */
4
5 #include "defs.h"
6
7 static
8 XFontStruct *
9 getFontInfo(int32_t fontid)
10 {
11         XFontStruct *fs;
12         pthread_mutex_t *mtx;
13
14         if ((fs = hashLookup(fontid, HASH_FONTINFO, &mtx)) == NULL) {
15                 fs = XQueryFont(SaveDisplay, fontid);
16                 if (fs)
17                         hashEnter(fs, HASH_FONTINFO, fontid);
18         }
19         threadMutexUnlock(mtx);
20
21         return(fs);
22 }
23
24 void
25 RuneSysCall_xDrawString(void *args, int *res)
26 {
27         struct {
28                 int32_t xid;
29                 int32_t gcid;
30                 int32_t x;
31                 int32_t y;
32                 PointerStor buf;
33                 int32_t len;
34         } *rhs;
35         pthread_mutex_t *mtx;
36
37         threadMutexLock(&XMtx);
38         rhs = args;
39         *res = XDrawString(SaveDisplay, rhs->xid, 
40                            hashLookup(rhs->gcid, HASH_GC, &mtx),
41                            rhs->x, rhs->y,
42                            rhs->buf.s_Addr, rhs->len);
43         threadMutexUnlock(mtx);
44         threadMutexUnlock(&XMtx);
45 }
46
47 void
48 RuneSysCall_xDrawImageString(void *args, int *res)
49 {
50         struct {
51                 int32_t xid;
52                 int32_t gcid;
53                 int32_t x;
54                 int32_t y;
55                 PointerStor buf;
56                 int32_t len;
57         } *rhs;
58         pthread_mutex_t *mtx;
59
60         threadMutexLock(&XMtx);
61         rhs = args;
62         *res = XDrawImageString(SaveDisplay, rhs->xid, 
63                                 hashLookup(rhs->gcid, HASH_GC, &mtx),
64                                 rhs->x, rhs->y,
65                                 rhs->buf.s_Addr, rhs->len);
66         threadMutexUnlock(mtx);
67         threadMutexUnlock(&XMtx);
68 }
69
70 void
71 RuneSysCall_xTextWidth(void *args, int *res)
72 {
73         struct {
74                 int32_t fontid;
75                 PointerStor buf;
76                 int32_t len;
77         } *rhs;
78         XFontStruct *fs;
79
80         threadMutexLock(&XMtx);
81         rhs = args;
82         fs = getFontInfo(rhs->fontid);
83         dassert(fs != NULL);
84         *res = XTextWidth(fs, rhs->buf.s_Addr, rhs->len);
85         threadMutexUnlock(&XMtx);
86 }
87
88 void
89 RuneSysCall_xTextHeight(void *args, int *res)
90 {
91         struct {
92                 int32_t fontid;
93         } *rhs;
94         XFontStruct *fs;
95
96         threadMutexLock(&XMtx);
97         rhs = args;
98         fs = getFontInfo(rhs->fontid);
99         dassert(fs != NULL);
100         *res = fs->ascent + fs->descent;
101         threadMutexUnlock(&XMtx);
102 }
103
104 void
105 RuneSysCall_xTextDescent(void *args, int *res)
106 {
107         struct {
108                 int32_t fontid;
109         } *rhs;
110         XFontStruct *fs;
111
112         threadMutexLock(&XMtx);
113         rhs = args;
114         fs = getFontInfo(rhs->fontid);
115         dassert(fs != NULL);
116         *res = fs->descent;
117         threadMutexUnlock(&XMtx);
118 }
119
120 void
121 RuneSysCall_xLoadFont(void *args, int *res)
122 {
123         struct {
124                 PointerStor buf;
125         } *rhs;
126
127         if (SaveDisplay == NULL)
128                 initXDisplay();
129         threadMutexLock(&XMtx);
130         rhs = args;
131         *res = XLoadFont(SaveDisplay, rhs->buf.s_Addr);
132         threadMutexUnlock(&XMtx);
133 }
134
135 void
136 RuneSysCall_xUnloadFont(void *args, int *res)
137 {
138         struct {
139                 int32_t fontid;
140         } *rhs;
141         XFontStruct *fs;
142
143         threadMutexLock(&XMtx);
144         rhs = args;
145         if ((fs = hashRemove(rhs->fontid, HASH_FONTINFO)) != NULL)
146                 XFreeFontInfo(NULL, fs, 1);
147         *res = XUnloadFont(SaveDisplay, rhs->fontid);
148         threadMutexUnlock(&XMtx);
149 }