Abey George (of Macadamian/Corel)
[wine/multimedia.git] / tools / make_X11wrappers
blobc443ff3dd8bcbbf02c9bb9e83b5ab07ff09548e0
1 #!/usr/bin/perl -w
3 # Create threads safe wrappers around X11 calls.
5 # Copyright 1998 Kristian Nielsen.
8 # FIXME: This does not do full C prototype parsing, but relies on
9 # knowledge on how the X11 include files are formatted. It will
10 # probably need to be modified for new include files. It also fails
11 # for certain prototypes (notably those with function pointer
12 # arguments or results), so these must be added manually. And it
13 # relies on a fixed location of X11 includes (/usr/X11R6/include/).
15 # This program expects to be run from Wine's main directory.
17 $X11_include_dir = "/usr/X11/include";
18 $outdir = "tsx11";
19 $wantfile = "$outdir/X11_calls";
20 @dolist = ("Xlib", "Xresource", "Xutil", "xpm", "XShm", "xf86dga", "xf86dga2", "xf86vmode");
22 # First read list of wanted function names.
24 open(WANT, $wantfile) || die "open";
25 while(<WANT>) {
26 next if /^\s*\#/; # Skip comment lines.
27 next if /^\s*$/; # Skip empty lines.
28 if(/^\s*([a-zA-Z0-9_]+)\s*$/) {
29 $want{$1} = 1;
30 } else {
31 die "syntax error in file '$wantfile', in line '$_'";
34 close(WANT);
36 foreach $name (@dolist) {
38 $ucname = uc $name;
39 $lcname = lc $name;
41 $outfile = "/ts_$lcname";
42 open(OUTC, ">$outdir/$outfile.c") || die "open";
43 open(OUTH, ">include/$outfile.h") || die "open";
45 $x11_incl = "";
46 $extensions_dir = "";
47 $pre_file = "";
48 $post_file = "";
49 $inc_name = $name;
50 if($name eq "Xutil" || $name eq "Xresource" || $name eq "XShm") {
51 $x11_incl = "#include <X11/Xlib.h>\n";
52 # For Xutil, we need X11/Xresource.h for XUniqueContext().
53 $x11_incl .= "#include <X11/Xresource.h>\n" if $name eq "Xutil";
55 if($name eq "xf86dga") {
56 $x11_incl = "#include <X11/Xlib.h>\n";
57 $extensions_dir = "extensions/";
58 $pre_file = "#ifdef HAVE_LIBXXF86DGA\n";
59 $post_file = "#endif /* defined(HAVE_LIBXXF86DGA) */\n";
61 if($name eq "xf86dga2") {
62 $x11_incl = "#include <X11/Xlib.h>\n";
63 $extensions_dir = "extensions/";
64 $pre_file = "#ifdef HAVE_LIBXXF86DGA2\n";
65 $post_file = "#endif /* defined(HAVE_LIBXXF86DGA2) */\n";
66 $inc_name = "xf86dga";
68 if($name eq "XShm") {
69 $extensions_dir = "extensions/";
70 $pre_file = "#ifdef HAVE_LIBXXSHM\n";
71 $post_file = "#endif /* defined(HAVE_LIBXXSHM) */\n";
73 if($name eq "xpm") {
74 $pre_file = "#ifdef HAVE_LIBXXPM\n";
75 $post_file = "#endif /* defined(HAVE_LIBXXPM) */\n";
77 if($name eq "xf86vmode") {
78 $x11_incl = "#include <X11/Xlib.h>\n";
79 $extensions_dir = "extensions/";
80 $pre_file = "#include \"windef.h\"\n#ifdef HAVE_LIBXXF86VM\n#define XMD_H\n#include \"basetsd.h\"\n";
81 $post_file = "#endif /* defined(HAVE_LIBXXF86VM) */\n";
84 print OUTH <<END;
86 * Thread safe wrappers around $name calls.
87 * Always include this file instead of <X11/$name.h>.
88 * This file was generated automatically by tools/make_X11wrappers
90 * Copyright 1998 Kristian Nielsen
93 #ifndef __WINE_TS_$ucname\_H
94 #define __WINE_TS_$ucname\_H
96 #include "config.h"
98 $pre_file
99 $x11_incl#include <X11/$extensions_dir$inc_name.h>
103 print OUTC <<END;
105 * Thread safe wrappers around $name calls.
106 * This file was generated automatically by tools/make_X11wrappers
107 * DO NOT EDIT!
110 #include "config.h"
112 $pre_file
113 $x11_incl#include <X11/$extensions_dir$inc_name.h>
115 #include "debugtools.h"
116 #include "ts_$lcname.h"
117 #include "x11drv.h"
119 DEFAULT_DEBUG_CHANNEL(x11)
122 if($name eq "xpm") { # Handle as special case.
123 output_fn("XpmCreatePixmapFromData", "int",
124 "Display *, Drawable, char **, Pixmap *, Pixmap *, XpmAttributes *",
125 "Display *a0, Drawable a1, char **a2, Pixmap *a3, Pixmap *a4, XpmAttributes *a5",
126 "a0, a1, a2, a3, a4, a5");
127 output_fn("XpmAttributesSize", "int", "void", "void", "");
128 } elsif($name eq "XShm") {
129 output_fn("XShmQueryExtension", "Bool",
130 "Display *", "Display *a0", "a0");
131 output_fn("XShmQueryVersion", "Bool",
132 "Display *, int *, int *, Bool *",
133 "Display *a0, int *a1, int *a2, Bool *a3", "a0, a1, a2, a3");
134 output_fn("XShmPixmapFormat", "int",
135 "Display *", "Display *a0", "a0");
136 output_fn("XShmAttach", Status,
137 "Display *, XShmSegmentInfo *",
138 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
139 output_fn("XShmDetach", Status,
140 "Display *, XShmSegmentInfo *",
141 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
142 output_fn("XShmPutImage", Status,
143 "Display *, Drawable, GC, XImage *, int, int, int, int, unsigned int, unsigned int, Bool",
144 "Display *a0, Drawable a1, GC a2, XImage *a3, int a4, int a5, int a6, int a7, unsigned int a8, unsigned int a9, Bool a10", "a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10");
145 output_fn("XShmGetImage", Status,
146 "Display *, Drawable, XImage *, int, int, unsigned long",
147 "Display *a0, Drawable a1, XImage *a2, int a3, int a4, unsigned long a5",
148 "a0, a1, a2, a3, a4, a5");
149 output_fn("XShmCreateImage", "XImage *",
150 "Display *, Visual *, unsigned int, int, char *, XShmSegmentInfo *, unsigned int, unsigned int",
151 "Display *a0, Visual *a1, unsigned int a2, int a3, char *a4, XShmSegmentInfo *a5, unsigned int a6, unsigned int a7",
152 "a0, a1, a2, a3, a4, a5, a6, a7");
153 output_fn("XShmCreatePixmap", "Pixmap",
154 "Display *, Drawable, char *, XShmSegmentInfo *, unsigned int, unsigned int, unsigned int",
155 "Display *a0, Drawable a1, char *a2, XShmSegmentInfo *a3, unsigned int a4, unsigned int a5, unsigned int a6",
156 "a0, a1, a2, a3, a4, a5, a6");
157 } elsif($name eq "xf86dga") {
158 output_fn("XF86DGAQueryVersion",Bool,
159 "Display*,int*,int*",
160 "Display*a0,int*a1,int*a2",
161 "a0,a1,a2"
163 output_fn("XF86DGAQueryExtension",Bool,
164 "Display*,int*,int*",
165 "Display*a0,int*a1,int*a2",
166 "a0,a1,a2"
168 output_fn("XF86DGAGetVideo",Status,
169 "Display*,int,char**,int*,int*,int*",
170 "Display*a0,int a1,char**a2,int*a3,int*a4,int*a5",
171 "a0,a1,a2,a3,a4,a5"
173 output_fn("XF86DGADirectVideo",Status,
174 "Display*,int,int",
175 "Display*a0,int a1,int a2",
176 "a0,a1,a2"
178 output_fn("XF86DGAGetViewPortSize",Status,
179 "Display*,int,int*,int*",
180 "Display*a0,int a1,int *a2,int *a3",
181 "a0,a1,a2,a3"
183 output_fn("XF86DGASetViewPort",Status,
184 "Display*,int,int,int",
185 "Display*a0,int a1,int a2,int a3",
186 "a0,a1,a2,a3"
188 output_fn("XF86DGAInstallColormap",Status,
189 "Display*,int,Colormap",
190 "Display*a0,int a1,Colormap a2",
191 "a0,a1,a2"
193 output_fn("XF86DGAQueryDirectVideo",Status,
194 "Display*,int,int*",
195 "Display*a0,int a1,int *a2",
196 "a0,a1,a2"
198 output_fn("XF86DGAViewPortChanged",Status,
199 "Display*,int,int",
200 "Display*a0,int a1,int a2",
201 "a0,a1,a2"
203 } elsif($name eq "xf86dga2") {
204 output_fn_short("Bool", "XDGAQueryVersion", "Display*" ,"int*","int*");
205 output_fn_short("Bool", "XDGAQueryExtension", "Display*" ,"int*","int*");
206 output_fn_short("XDGAMode*", "XDGAQueryModes", "Display*" ,"int", "int*");
207 output_fn_short("XDGADevice*", "XDGASetMode", "Display*" ,"int","int");
208 output_fn_short("Bool", "XDGAOpenFramebuffer", "Display*" ,"int");
209 output_fn_short("void", "XDGACloseFramebuffer", "Display*" ,"int");
210 output_fn_short("void", "XDGASetViewport", "Display*" ,"int", "int", "int", "int");
211 output_fn_short("void", "XDGAInstallColormap", "Display*" , "int", "Colormap");
212 output_fn_short("Colormap", "XDGACreateColormap", "Display*" ,"int", "XDGADevice*", "int");
213 output_fn_short("void", "XDGASelectInput", "Display*" ,"int", "long");
214 output_fn_short("void", "XDGAFillRectangle", "Display*" ,"int", "int", "int", "unsigned int", "unsigned int", "unsigned long");
215 output_fn_short("void", "XDGACopyArea", "Display*" ,"int", "int", "int", "unsigned int", "unsigned int", "int", "int");
216 output_fn_short("void", "XDGACopyTransparentArea", "Display*" ,"int", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned long");
217 output_fn_short("int", "XDGAGetViewportStatus", "Display*" ,"int");
218 output_fn_short("void", "XDGASync", "Display*" ,"int");
219 output_fn_short("Bool", "XDGASetClientVersion", "Display*");
220 output_fn_short("void", "XDGAChangePixmapMode", "Display*" ,"int", "int*", "int*", "int");
221 output_fn_short("void", "XDGAKeyEventToXKeyEvent", "XDGAKeyEvent*" ,"XKeyEvent*");
222 } elsif($name eq "xf86vmode") {
223 output_fn("XF86VidModeQueryVersion",Bool,
224 "Display*,int*,int*",
225 "Display*a0,int*a1,int*a2",
226 "a0,a1,a2"
228 output_fn("XF86VidModeQueryExtension",Bool,
229 "Display*,int*,int*",
230 "Display*a0,int*a1,int*a2",
231 "a0,a2,a2"
233 output_fn("XF86VidModeGetModeLine",Bool,
234 "Display*,int,int*,XF86VidModeModeLine*",
235 "Display*a0,int a1,int*a2,XF86VidModeModeLine*a3",
236 "a0,a1,a2,a3"
238 output_fn("XF86VidModeGetAllModeLines",Bool,
239 "Display*,int,int*,XF86VidModeModeInfo***",
240 "Display*a0,int a1,int*a2,XF86VidModeModeInfo***a3",
241 "a0,a1,a2,a3"
243 output_fn("XF86VidModeAddModeLine",Bool,
244 "Display*,int,XF86VidModeModeInfo*,XF86VidModeModeInfo*",
245 "Display*a0,int a1,XF86VidModeModeInfo*a2,XF86VidModeModeInfo*a3",
246 "a0,a1,a2,a3"
248 output_fn("XF86VidModeDeleteModeLine",Bool,
249 "Display*,int,XF86VidModeModeInfo*",
250 "Display*a0,int a1,XF86VidModeModeInfo*a2",
251 "a0,a1,a2"
253 output_fn("XF86VidModeModModeLine",Bool,
254 "Display*,int,XF86VidModeModeLine*",
255 "Display*a0,int a1,XF86VidModeModeLine*a2",
256 "a0,a1,a2"
258 output_fn("XF86VidModeValidateModeLine",Status,
259 "Display*,int,XF86VidModeModeInfo*",
260 "Display*a0,int a1,XF86VidModeModeInfo*a2",
261 "a0,a1,a2"
263 output_fn("XF86VidModeSwitchMode",Bool,
264 "Display*,int,int",
265 "Display*a0,int a1,int a2",
266 "a0,a1,a2"
268 output_fn("XF86VidModeSwitchToMode",Bool,
269 "Display*,int,XF86VidModeModeInfo*",
270 "Display*a0,int a1,XF86VidModeModeInfo*a2",
271 "a0,a1,a2"
273 output_fn("XF86VidModeLockModeSwitch",Bool,
274 "Display*,int,int",
275 "Display*a0,int a1,int a2",
276 "a0,a1,a2"
278 output_fn("XF86VidModeGetMonitor",Bool,
279 "Display*,int,XF86VidModeMonitor*",
280 "Display*a0,int a1,XF86VidModeMonitor*a2",
281 "a0,a1,a2"
283 output_fn("XF86VidModeGetViewPort",Bool,
284 "Display*,int,int*,int*",
285 "Display*a0,int a1,int*a2,int*a3",
286 "a0,a1,a2,a3"
288 output_fn("XF86VidModeSetViewPort",Bool,
289 "Display*,int,int,int",
290 "Display*a0,int a1,int a2,int a3",
291 "a0,a1,a2,a3"
293 } else {
294 open(IN,
295 "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | " .
296 "gcc -L$X11_include_dir -DNeedFunctionPrototypes -E - | " .
297 "grep -v '^[ \t]*\$)' |"
298 ) || die "open";
300 PROTO: while(<IN>) {
301 if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
302 $result_type = $1;
303 $fn_name = $2;
304 $result_type = "int" if $result_type =~ /^\s*$/;
305 @args = ();
306 while(<IN>) {
307 last if m'\)\s*;';
308 # Give up on vararg functions and function pointer args.
309 if(m'\.\.\.|\(\*\)') {
310 undef $fn_name;
311 last;
313 if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
314 $args[$#args+1] = $1;
315 if ($1 =~ /char\s*\[/) { # small hack for XQueryKeymap
316 $args[$#args] = "char*";
320 # Skip if vararg, function pointer arg, or not needed.
321 next unless $fn_name;
322 next unless $want{$fn_name} && $want{$fn_name} == 1;
324 # Special case for no arguments (which is specified as "void").
325 if($#args == 0 && $args[0] eq "void") {
326 @args = ();
328 $proto = "";
329 $formals = "";
330 $actuals = "";
331 for($i = 0; $i <= $#args; $i++) {
332 $comma = $i < $#args ? ", " : "";
333 $proto .= "$args[$i]$comma";
334 $formals .= "$args[$i] a$i$comma";
335 $actuals .= "a$i$comma";
337 $proto = $formals = "void" if $#args == -1;
338 output_fn($fn_name, $result_type, $proto, $formals, $actuals);
343 if($name eq "Xlib") {
344 raw_output_fn("XSynchronize", "int (*r)(Display *)",
345 "int (*TSXSynchronize(Display *, Bool))(Display *)",
346 "int (*TSXSynchronize(Display *a0, Bool a1))(Display *)",
347 "a0, a1");
348 print OUTC "\nextern void _XInitImageFuncPtrs(XImage *);\n";
349 output_fn("_XInitImageFuncPtrs", "void", "XImage *", "XImage *a0", "a0");
350 } elsif($name eq "Xutil") {
351 output_fn("XDestroyImage", "int",
352 "struct _XImage *", "struct _XImage *a0", "a0");
353 output_fn("XGetPixel", "unsigned long",
354 "struct _XImage *, int, int",
355 "struct _XImage *a0, int a1, int a2",
356 "a0, a1, a2");
357 output_fn("XPutPixel", "int",
358 "struct _XImage *, int, int, unsigned long",
359 "struct _XImage *a0, int a1, int a2, unsigned long a3",
360 "a0, a1, a2, a3");
361 output_fn("XSubImage", "struct _XImage *",
362 "struct _XImage *, int, int, unsigned int, unsigned int",
363 "struct _XImage *a0, int a1, int a2, unsigned int a3, unsigned int a4",
364 "a0, a1, a2, a3, a4");
365 output_fn("XAddPixel", "int",
366 "struct _XImage *, long",
367 "struct _XImage *a0, long a1", "a0, a1");
368 output_fn("XUniqueContext", "XContext", "void", "void", "");
371 print OUTH <<END;
373 $post_file
374 #endif /* __WINE_TS_$ucname\_H */
376 print OUTC <<END;
378 $post_file
385 foreach $i (keys %want) {
386 if($want{$i} == 1) {
387 print "Unresolved: $i\n";
392 sub output_fn {
393 # Example call:
394 # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
397 my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;
399 return raw_output_fn($fn_name,
400 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
401 "$result_type TS$fn_name($protos)",
402 "$result_type TS$fn_name($formals)",
403 $actuals);
406 sub output_fn_short {
407 # Example call:
408 # output_fn_sort("Bool", "XDGAQueryExtension", "Display *", "int *", "int *");
410 my ($result_type, $fn_name, @args) = @_;
412 my ($i, $proto, $formals, $actuals) = (0,
413 "$result_type TS$fn_name(",
414 "$result_type TS$fn_name(",
415 "");
416 while ($val = shift @args) {
417 $proto = $proto . $val;
418 $formals = $formals . $val . " a$i";
419 $actuals = $actuals . " a$i";
420 $i++;
421 if (@args) {
422 $proto = $proto . ", ";
423 $formals = $formals . ", ";
424 $actuals = $actuals . ", ";
427 $proto = $proto . ")";
428 $formals = $formals . ")";
431 raw_output_fn($fn_name,
432 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
433 $proto,
434 $formals,
435 $actuals);
438 sub raw_output_fn {
439 # Example call:
440 # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
443 my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;
445 return undef unless $want{$fn_name} && $want{$fn_name} == 1;
447 print OUTC "\n$defdecl\n";
448 print OUTH "extern $protodecl;\n";
449 # print OUTH "#define $fn_name TS$fn_name\n";
450 print OUTC "{\n";
451 print OUTC " $resultdecl;\n" if $resultdecl;
452 print OUTC " TRACE(\"Call $fn_name\\n\");\n";
453 print OUTC " EnterCriticalSection( &X11DRV_CritSection );\n";
454 print OUTC " ";
455 print OUTC "r = " if $resultdecl;
456 print OUTC "$fn_name($actuals);\n";
457 print OUTC " LeaveCriticalSection( &X11DRV_CritSection );\n";
458 print OUTC " TRACE(\"Ret $fn_name\\n\");\n";
459 print OUTC " return r;\n" if $resultdecl;
460 print OUTC "}\n";
461 $want{$fn_name} = 2;
462 return 1;