Rune - Fix thread issue
[rune.git] / ext_x11 / main.c
1 /*
2  * MAIN.C
3  */
4
5 #include "defs.h"
6
7 Display *SaveDisplay;
8 pthread_mutex_t XMtx;
9
10 /*
11  * These types are active when dynamically loaded into rune for code
12  * generation or interpretation.  However, when loaded as a shared library
13  * in a generated Rune executable, these types should already exist and
14  * be properly initialized in the Rune binary.
15  *
16  * We use a weak reference to allow for both cases.
17  */
18 Type _PointType = { .ty_Flags = 0 };
19 Type _RectType = { .ty_Flags = 0 };
20 Type _EventType = { .ty_Flags = 0 };
21 Type _EventPtrType = { .ty_Flags = 0 };
22
23 __weak_reference(_PointType, PointType);
24 __weak_reference(_RectType, RectType);
25 __weak_reference(_EventType, EventType);
26 __weak_reference(_EventPtrType, EventPtrType);
27
28 #define GENINTERNALCALL(name)                           \
29                                                         \
30 static gendata_t *                                      \
31 gen_ ## name(genctx_p ct, Exp *exp)                     \
32 {                                                       \
33         return GenInternalCallWrapper(ct, exp, "RuneSysCall_" #name); \
34 }
35
36 GENINTERNALCALL(xCreateWindow)
37 GENINTERNALCALL(xMoveResizeWindow)
38 GENINTERNALCALL(xCreateGC)
39 GENINTERNALCALL(xDestroyWindow)
40 GENINTERNALCALL(xMapWindow)
41 GENINTERNALCALL(xUnmapWindow)
42 GENINTERNALCALL(xClearWindow)
43 GENINTERNALCALL(xClearArea)
44 GENINTERNALCALL(xMoveWindow)
45 GENINTERNALCALL(xResizeWindow)
46 GENINTERNALCALL(xFreeGC)
47 GENINTERNALCALL(xFlush)
48 GENINTERNALCALL(xSync)
49 GENINTERNALCALL(xEvent)
50 GENINTERNALCALL(xDrawPoint)
51 GENINTERNALCALL(xDrawLine)
52 GENINTERNALCALL(xDrawRect)
53 GENINTERNALCALL(xFillRect)
54 GENINTERNALCALL(xAllocColor)
55 GENINTERNALCALL(xFreeColor)
56 GENINTERNALCALL(xSetForeground)
57 GENINTERNALCALL(xSetBackground)
58 GENINTERNALCALL(xDrawString)
59 GENINTERNALCALL(xDrawImageString)
60 GENINTERNALCALL(xTextWidth)
61 GENINTERNALCALL(xTextHeight)
62 GENINTERNALCALL(xTextDescent)
63 GENINTERNALCALL(xLoadFont)
64 GENINTERNALCALL(xUnloadFont)
65
66 static Syscall X11OpAry[] = {
67         { .sc_Name = "xCreateWindow",
68           .sc_RunFunc = (sys_run_t)RuneSysCall_xCreateWindow,
69           .sc_GenFunc = gen_xCreateWindow,
70           .sc_Id = NULL
71         },
72         { .sc_Name = "xMoveResizeWindow",
73           .sc_RunFunc = (sys_run_t)RuneSysCall_xMoveResizeWindow,
74           .sc_GenFunc = gen_xMoveResizeWindow,
75           .sc_Id = NULL
76         },
77         { .sc_Name = "xCreateGC",
78           .sc_RunFunc = (sys_run_t)RuneSysCall_xCreateGC,
79           .sc_GenFunc = gen_xCreateGC,
80           .sc_Id = NULL
81         },
82         { .sc_Name = "xDestroyWindow",
83           .sc_RunFunc = (sys_run_t)RuneSysCall_xDestroyWindow,
84           .sc_GenFunc = gen_xDestroyWindow,
85           .sc_Id = NULL
86         },
87         { .sc_Name = "xMapWindow",
88           .sc_RunFunc = (sys_run_t)RuneSysCall_xMapWindow,
89           .sc_GenFunc = gen_xMapWindow,
90           .sc_Id = NULL
91         },
92         { .sc_Name = "xUnmapWindow",
93           .sc_RunFunc = (sys_run_t)RuneSysCall_xUnmapWindow,
94           .sc_GenFunc = gen_xUnmapWindow,
95           .sc_Id = NULL
96         },
97         { .sc_Name = "xClearWindow",
98           .sc_RunFunc = (sys_run_t)RuneSysCall_xClearWindow,
99           .sc_GenFunc = gen_xClearWindow,
100           .sc_Id = NULL
101         },
102         { .sc_Name = "xClearArea",
103           .sc_RunFunc = (sys_run_t)RuneSysCall_xClearArea,
104           .sc_GenFunc = gen_xClearArea,
105           .sc_Id = NULL
106         },
107         { .sc_Name = "xMoveWindow",
108           .sc_RunFunc = (sys_run_t)RuneSysCall_xMoveWindow,
109           .sc_GenFunc = gen_xMoveWindow,
110           .sc_Id = NULL
111         },
112         { .sc_Name = "xResizeWindow",
113           .sc_RunFunc = (sys_run_t)RuneSysCall_xResizeWindow,
114           .sc_GenFunc = gen_xResizeWindow,
115           .sc_Id = NULL
116         },
117         { .sc_Name = "xFreeGC",
118           .sc_RunFunc = (sys_run_t)RuneSysCall_xFreeGC,
119           .sc_GenFunc = gen_xFreeGC,
120           .sc_Id = NULL
121         },
122         { .sc_Name = "xFlush",  
123           .sc_RunFunc = (sys_run_t)RuneSysCall_xFlush,
124           .sc_GenFunc = gen_xFlush,
125           .sc_Id = NULL
126         },
127         { .sc_Name = "xSync",   
128           .sc_RunFunc = (sys_run_t)RuneSysCall_xSync,
129           .sc_GenFunc = gen_xSync,
130           .sc_Id = NULL
131         },
132         { .sc_Name = "xEvent",  
133           .sc_RunFunc = (sys_run_t)RuneSysCall_xEvent,
134           .sc_GenFunc = gen_xEvent,
135           .sc_Id = NULL
136         },
137         { .sc_Name = "xDrawPoint",
138           .sc_RunFunc = (sys_run_t)RuneSysCall_xDrawPoint,
139           .sc_GenFunc = gen_xDrawPoint,
140           .sc_Id = NULL
141         },
142         { .sc_Name = "xDrawLine",
143           .sc_RunFunc = (sys_run_t)RuneSysCall_xDrawLine,
144           .sc_GenFunc = gen_xDrawLine,
145           .sc_Id = NULL
146         },
147         { .sc_Name = "xDrawRect",
148           .sc_RunFunc = (sys_run_t)RuneSysCall_xDrawRect,
149           .sc_GenFunc = gen_xDrawRect,
150           .sc_Id = NULL
151         },
152         { .sc_Name = "xFillRect",
153           .sc_RunFunc = (sys_run_t)RuneSysCall_xFillRect,
154           .sc_GenFunc = gen_xFillRect,
155           .sc_Id = NULL
156         },
157         { .sc_Name = "xAllocColor",
158           .sc_RunFunc = (sys_run_t)RuneSysCall_xAllocColor,
159           .sc_GenFunc = gen_xAllocColor,
160           .sc_Id = NULL
161         },
162         { .sc_Name = "xFreeColor",
163           .sc_RunFunc = (sys_run_t)RuneSysCall_xFreeColor,
164           .sc_GenFunc = gen_xFreeColor,
165           .sc_Id = NULL
166         },
167         { .sc_Name = "xSetForeground",
168           .sc_RunFunc = (sys_run_t)RuneSysCall_xSetForeground,
169           .sc_GenFunc = gen_xSetForeground,
170           .sc_Id = NULL
171         },
172         { .sc_Name = "xSetBackground",
173           .sc_RunFunc = (sys_run_t)RuneSysCall_xSetBackground,
174           .sc_GenFunc = gen_xSetBackground,
175           .sc_Id = NULL
176         },
177         { .sc_Name = "xDrawString",
178           .sc_RunFunc = (sys_run_t)RuneSysCall_xDrawString,
179           .sc_GenFunc = gen_xDrawString,
180           .sc_Id = NULL
181         },
182         { .sc_Name = "xDrawImageString",
183           .sc_RunFunc = (sys_run_t)RuneSysCall_xDrawImageString,
184           .sc_GenFunc = gen_xDrawImageString,
185           .sc_Id = NULL
186         },
187         { .sc_Name = "xTextWidth",
188           .sc_RunFunc = (sys_run_t)RuneSysCall_xTextWidth,
189           .sc_GenFunc = gen_xTextWidth,
190           .sc_Id = NULL
191         },
192         { .sc_Name = "xTextHeight",
193           .sc_RunFunc = (sys_run_t)RuneSysCall_xTextHeight,
194           .sc_GenFunc = gen_xTextHeight,
195           .sc_Id = NULL
196         },
197         { .sc_Name = "xTextDescent",
198           .sc_RunFunc = (sys_run_t)RuneSysCall_xTextDescent,
199           .sc_GenFunc = gen_xTextDescent,
200           .sc_Id = NULL
201         },
202         { .sc_Name = "xLoadFont",
203           .sc_RunFunc = (sys_run_t)RuneSysCall_xLoadFont,
204           .sc_GenFunc = gen_xLoadFont,
205           .sc_Id = NULL
206         },
207         { .sc_Name = "xUnloadFont",
208           .sc_RunFunc = (sys_run_t)RuneSysCall_xUnloadFont,
209           .sc_GenFunc = gen_xUnloadFont,
210           .sc_Id = NULL
211         }
212 };
213
214 void resolveClasses(void);      /* generation glue */
215 void resolveStorage(void);      /* generation glue */
216 void init(void);                /* link glue */
217
218 void
219 init(void)
220 {
221         size_t i;
222
223         /*
224          * This code is only active when libgen is linked in for
225          * compiling or intepretation.  It is inactive when a Rune
226          * executable is directly run.
227          */
228         if (LibGenActive) {
229                 initType(&EventType, NULL, TY_UNRESOLVED);
230                 initType(&PointType, NULL, TY_UNRESOLVED);
231                 initType(&RectType, NULL, TY_UNRESOLVED);
232                 initPtrType(&EventPtrType, &EventType, 0);
233
234                 for (i = 0; i < arysize(X11OpAry); ++i) {
235                         SyscallAdd(&X11OpAry[i]);
236                 }
237                 InternalRegisterType("__internal_gfx_Point",
238                                      "PointType", &PointType);
239                 InternalRegisterType("__internal_gfx_Rect",
240                                      "RectType", &RectType);
241                 InternalRegisterType("__internal_gfx_Event",
242                                      "EventType", &EventType);
243                 InternalRegisterType("__internal_gfx_EventPtr",
244                                      "EventPtrType", &EventPtrType);
245                 runeaddlib("-lx11");
246                 runeaddlib("-lXext");
247                 runeaddlib("-lX11");
248         }
249 }
250
251 void
252 initXDisplay(void)
253 {
254         if (SaveDisplay)
255                 return;
256         threadMutexLock(&XMtx);
257         if (SaveDisplay == NULL) {
258                 XInitThreads();
259                 SaveDisplay = XOpenDisplay(NULL);
260         }
261         threadMutexUnlock(&XMtx);
262         if (SaveDisplay == NULL)
263                 fatal("Unable to open display");
264 }
265
266 void
267 resolveClasses(void)
268 {
269 }
270
271 void
272 resolveStorage(void)
273 {
274 }