Better implementation of GetShortPathNameA/W.
[wine/multimedia.git] / tools / make_X11wrappers
blob477c694e7085aa255064c05e10e119233b8f6b26
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/X11R6/include";
18 $outdir = "tsx11";
19 $wantfile = "$outdir/X11_calls";
20 @dolist = ("Xlib", "Xresource", "Xutil", "xpm", "XShm", "xf86dga", "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 if($name eq "Xutil" || $name eq "Xresource" || $name eq "XShm") {
50 $x11_incl = "#include <X11/Xlib.h>\n";
51 # For Xutil, we need X11/Xresource.h for XUniqueContext().
52 $x11_incl .= "#include <X11/Xresource.h>\n" if $name eq "Xutil";
54 if($name eq "xf86dga") {
55 $x11_incl = "#include <X11/Xlib.h>\n";
56 $extensions_dir = "extensions/";
57 $pre_file = "#ifdef HAVE_LIBXXF86DGA\n";
58 $post_file = "#endif /* defined(HAVE_LIBXXF86DGA) */\n";
60 if($name eq "XShm") {
61 $extensions_dir = "extensions/";
62 $pre_file = "#ifdef HAVE_LIBXXSHM\n";
63 $post_file = "#endif /* defined(HAVE_LIBXXSHM) */\n";
65 if($name eq "xpm") {
66 $pre_file = "#ifdef HAVE_LIBXXPM\n";
67 $post_file = "#endif /* defined(HAVE_LIBXXPM) */\n";
69 if($name eq "xf86vmode") {
70 $x11_incl = "#include <X11/Xlib.h>\n";
71 $extensions_dir = "extensions/";
72 $pre_file = "#include \"wintypes.h\"\n#ifdef HAVE_LIBXXF86VM\n#define XMD_H\n";
73 $post_file = "#endif /* defined(HAVE_LIBXXF86VM) */\n";
76 print OUTH <<END;
78 * Thread safe wrappers around $name calls.
79 * Always include this file instead of <X11/$name.h>.
80 * This file was generated automatically by tools/make_X11wrappers
82 * Copyright 1998 Kristian Nielsen
85 #ifndef __WINE_TS$ucname\_H
86 #define __WINE_TS$ucname\_H
88 #include "config.h"
90 #ifndef X_DISPLAY_MISSING
92 $pre_file
93 $x11_incl#include <X11/$extensions_dir$name.h>
95 END
97 print OUTC <<END;
99 * Thread safe wrappers around $name calls.
100 * This file was generated automatically by tools/make_X11wrappers
101 * DO NOT EDIT!
104 #include "config.h"
106 #ifndef X_DISPLAY_MISSING
108 $pre_file
109 $x11_incl#include <X11/$extensions_dir$name.h>
110 #include "debug.h"
111 #include "x11drv.h"
114 if($name eq "xpm") { # Handle as special case.
115 output_fn("XpmCreatePixmapFromData", "int",
116 "Display *, Drawable, char **, Pixmap *, Pixmap *, XpmAttributes *",
117 "Display *a0, Drawable a1, char **a2, Pixmap *a3, Pixmap *a4, XpmAttributes *a5",
118 "a0, a1, a2, a3, a4, a5");
119 output_fn("XpmAttributesSize", "int", "void", "void", "");
120 } elsif($name eq "XShm") {
121 output_fn("XShmQueryExtension", "Bool",
122 "Display *", "Display *a0", "a0");
123 output_fn("XShmQueryVersion", "Bool",
124 "Display *, int *, int *, Bool *",
125 "Display *a0, int *a1, int *a2, Bool *a3", "a0, a1, a2, a3");
126 output_fn("XShmPixmapFormat", "int",
127 "Display *", "Display *a0", "a0");
128 output_fn("XShmAttach", Status,
129 "Display *, XShmSegmentInfo *",
130 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
131 output_fn("XShmDetach", Status,
132 "Display *, XShmSegmentInfo *",
133 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
134 output_fn("XShmPutImage", Status,
135 "Display *, Drawable, GC, XImage *, int, int, int, int, unsigned int, unsigned int, Bool",
136 "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");
137 output_fn("XShmGetImage", Status,
138 "Display *, Drawable, XImage *, int, int, unsigned long",
139 "Display *a0, Drawable a1, XImage *a2, int a3, int a4, unsigned long a5",
140 "a0, a1, a2, a3, a4, a5");
141 output_fn("XShmCreateImage", "XImage *",
142 "Display *, Visual *, unsigned int, int, char *, XShmSegmentInfo *, unsigned int, unsigned int",
143 "Display *a0, Visual *a1, unsigned int a2, int a3, char *a4, XShmSegmentInfo *a5, unsigned int a6, unsigned int a7",
144 "a0, a1, a2, a3, a4, a5, a6, a7");
145 output_fn("XShmCreatePixmap", "Pixmap",
146 "Display *, Drawable, char *, XShmSegmentInfo *, unsigned int, unsigned int, unsigned int",
147 "Display *a0, Drawable a1, char *a2, XShmSegmentInfo *a3, unsigned int a4, unsigned int a5, unsigned int a6",
148 "a0, a1, a2, a3, a4, a5, a6");
149 } elsif($name eq "xf86dga") {
150 output_fn("XF86DGAQueryVersion",Bool,
151 "Display*,int*,int*",
152 "Display*a0,int*a1,int*a2",
153 "a0,a1,a2"
155 output_fn("XF86DGAQueryExtension",Bool,
156 "Display*,int*,int*",
157 "Display*a0,int*a1,int*a2",
158 "a0,a1,a2"
160 output_fn("XF86DGAGetVideo",Status,
161 "Display*,int,char**,int*,int*,int*",
162 "Display*a0,int a1,char**a2,int*a3,int*a4,int*a5",
163 "a0,a1,a2,a3,a4,a5"
165 output_fn("XF86DGADirectVideo",Status,
166 "Display*,int,int",
167 "Display*a0,int a1,int a2",
168 "a0,a1,a2"
170 output_fn("XF86DGAGetViewPortSize",Status,
171 "Display*,int,int*,int*",
172 "Display*a0,int a1,int *a2,int *a3",
173 "a0,a1,a2,a3"
175 output_fn("XF86DGASetViewPort",Status,
176 "Display*,int,int,int",
177 "Display*a0,int a1,int a2,int a3",
178 "a0,a1,a2,a3"
180 output_fn("XF86DGAInstallColormap",Status,
181 "Display*,int,Colormap",
182 "Display*a0,int a1,Colormap a2",
183 "a0,a1,a2"
185 output_fn("XF86DGAQueryDirectVideo",Status,
186 "Display*,int,int*",
187 "Display*a0,int a1,int *a2",
188 "a0,a1,a2"
190 output_fn("XF86DGAViewPortChanged",Status,
191 "Display*,int,int",
192 "Display*a0,int a1,int a2",
193 "a0,a1,a2"
195 } elsif($name eq "xf86vmode") {
196 output_fn("XF86VidModeQueryVersion",Bool,
197 "Display*,int*,int*",
198 "Display*a0,int*a1,int*a2",
199 "a0,a1,a2"
201 output_fn("XF86VidModeQueryExtension",Bool,
202 "Display*,int*,int*",
203 "Display*a0,int*a1,int*a2",
204 "a0,a2,a2"
206 output_fn("XF86VidModeGetModeLine",Bool,
207 "Display*,int,int*,XF86VidModeModeLine*",
208 "Display*a0,int a1,int*a2,XF86VidModeModeLine*a3",
209 "a0,a1,a2,a3"
211 output_fn("XF86VidModeGetAllModeLines",Bool,
212 "Display*,int,int*,XF86VidModeModeInfo***",
213 "Display*a0,int a1,int*a2,XF86VidModeModeInfo***a3",
214 "a0,a1,a2,a3"
216 output_fn("XF86VidModeAddModeLine",Bool,
217 "Display*,int,XF86VidModeModeInfo*,XF86VidModeModeInfo*",
218 "Display*a0,int a1,XF86VidModeModeInfo*a2,XF86VidModeModeInfo*a3",
219 "a0,a1,a2,a3"
221 output_fn("XF86VidModeDeleteModeLine",Bool,
222 "Display*,int,XF86VidModeModeInfo*",
223 "Display*a0,int a1,XF86VidModeModeInfo*a2",
224 "a0,a1,a2"
226 output_fn("XF86VidModeModModeLine",Bool,
227 "Display*,int,XF86VidModeModeLine*",
228 "Display*a0,int a1,XF86VidModeModeLine*a2",
229 "a0,a1,a2"
231 output_fn("XF86VidModeValidateModeLine",Status,
232 "Display*,int,XF86VidModeModeInfo*",
233 "Display*a0,int a1,XF86VidModeModeInfo*a2",
234 "a0,a1,a2"
236 output_fn("XF86VidModeSwitchMode",Bool,
237 "Display*,int,int",
238 "Display*a0,int a1,int a2",
239 "a0,a1,a2"
241 output_fn("XF86VidModeSwitchToMode",Bool,
242 "Display*,int,XF86VidModeModeInfo*",
243 "Display*a0,int a1,XF86VidModeModeInfo*a2",
244 "a0,a1,a2"
246 output_fn("XF86VidModeLockModeSwitch",Bool,
247 "Display*,int,int",
248 "Display*a0,int a1,int a2",
249 "a0,a1,a2"
251 output_fn("XF86VidModeGetMonitor",Bool,
252 "Display*,int,XF86VidModeMonitor*",
253 "Display*a0,int a1,XF86VidModeMonitor*a2",
254 "a0,a1,a2"
256 output_fn("XF86VidModeGetViewPort",Bool,
257 "Display*,int,int*,int*",
258 "Display*a0,int a1,int*a2,int*a3",
259 "a0,a1,a2,a3"
261 output_fn("XF86VidModeSetViewPort",Bool,
262 "Display*,int,int,int",
263 "Display*a0,int a1,int a2,int a3",
264 "a0,a1,a2,a3"
266 } else {
267 open(IN, "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | gcc -L$X11_include_dir -E - | grep -v '^[ \t]*\$'|") || die "open";
269 PROTO: while(<IN>) {
270 if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
271 $result_type = $1;
272 $fn_name = $2;
273 $result_type = "int" if $result_type =~ /^\s*$/;
274 @args = ();
275 while(<IN>) {
276 last if m'\)\s*;';
277 # Give up on vararg functions and function pointer args.
278 if(m'\.\.\.|\(\*\)') {
279 undef $fn_name;
280 last;
282 if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
283 $args[$#args+1] = $1;
284 if ($1 =~ /char\s*\[/) { # small hack for XQueryKeymap
285 $args[$#args] = "char*";
289 # Skip if vararg, function pointer arg, or not needed.
290 next unless $fn_name;
291 next unless $want{$fn_name} && $want{$fn_name} == 1;
293 # Special case for no arguments (which is specified as "void").
294 if($#args == 0 && $args[0] eq "void") {
295 @args = ();
297 $proto = "";
298 $formals = "";
299 $actuals = "";
300 for($i = 0; $i <= $#args; $i++) {
301 $comma = $i < $#args ? ", " : "";
302 $proto .= "$args[$i]$comma";
303 $formals .= "$args[$i] a$i$comma";
304 $actuals .= "a$i$comma";
306 $proto = $formals = "void" if $#args == -1;
307 output_fn($fn_name, $result_type, $proto, $formals, $actuals);
312 if($name eq "Xlib") {
313 raw_output_fn("XSynchronize", "int (*r)(Display *)",
314 "int (*TSXSynchronize(Display *, Bool))(Display *)",
315 "int (*TSXSynchronize(Display *a0, Bool a1))(Display *)",
316 "a0, a1");
317 print OUTC "\nextern void _XInitImageFuncPtrs(XImage *);\n";
318 output_fn("_XInitImageFuncPtrs", "void", "XImage *", "XImage *a0", "a0");
319 } elsif($name eq "Xutil") {
320 output_fn("XDestroyImage", "int",
321 "struct _XImage *", "struct _XImage *a0", "a0");
322 output_fn("XGetPixel", "unsigned long",
323 "struct _XImage *, int, int",
324 "struct _XImage *a0, int a1, int a2",
325 "a0, a1, a2");
326 output_fn("XPutPixel", "int",
327 "struct _XImage *, int, int, unsigned long",
328 "struct _XImage *a0, int a1, int a2, unsigned long a3",
329 "a0, a1, a2, a3");
330 output_fn("XSubImage", "struct _XImage *",
331 "struct _XImage *, int, int, unsigned int, unsigned int",
332 "struct _XImage *a0, int a1, int a2, unsigned int a3, unsigned int a4",
333 "a0, a1, a2, a3, a4");
334 output_fn("XAddPixel", "int",
335 "struct _XImage *, long",
336 "struct _XImage *a0, long a1", "a0, a1");
337 output_fn("XUniqueContext", "XContext", "void", "void", "");
340 print OUTH <<END;
342 $post_file
343 #endif /* !defined(X_DISPLAY_MISSING) */
345 #endif /* __WINE_TS$ucname\_H */
347 print OUTC <<END;
349 $post_file
350 #endif /* !defined(X_DISPLAY_MISSING) */
357 foreach $i (keys %want) {
358 if($want{$i} == 1) {
359 print "Unresolved: $i\n";
364 sub output_fn {
365 # Example call:
366 # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
369 my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;
371 return raw_output_fn($fn_name,
372 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
373 "$result_type TS$fn_name($protos)",
374 "$result_type TS$fn_name($formals)",
375 $actuals);
378 sub raw_output_fn {
379 # Example call:
380 # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
383 my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;
385 return undef unless $want{$fn_name} && $want{$fn_name} == 1;
387 print OUTC "\n$defdecl\n";
388 print OUTH "extern $protodecl;\n";
389 # print OUTH "#define $fn_name TS$fn_name\n";
390 print OUTC "{\n";
391 print OUTC " $resultdecl;\n" if $resultdecl;
392 print OUTC " TRACE(x11, \"Call $fn_name\\n\");\n";
393 print OUTC " EnterCriticalSection( &X11DRV_CritSection );\n";
394 print OUTC " ";
395 print OUTC "r = " if $resultdecl;
396 print OUTC "$fn_name($actuals);\n";
397 print OUTC " LeaveCriticalSection( &X11DRV_CritSection );\n";
398 print OUTC " TRACE(x11, \"Ret $fn_name\\n\");\n";
399 print OUTC " return r;\n" if $resultdecl;
400 print OUTC "}\n";
401 $want{$fn_name} = 2;
402 return 1;