Release 980215
[wine/multimedia.git] / tools / make_X11wrappers
blob0f2db0b0bef1dedf9f206b3736cdc4c0c43c3247
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");
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 if($name eq "Xutil" || $name eq "Xresource" || $name eq "XShm") {
48 $x11_incl = "#include <X11/Xlib.h>\n";
49 # For Xutil, we need X11/Xresource.h for XUniqueContext().
50 $x11_incl .= "#include <X11/Xresource.h>\n" if $name eq "Xutil";
52 if($name eq "XShm") {
53 $extensions_dir = "extensions/";
56 print OUTH <<END;
58 * Thread safe wrappers around $name calls.
59 * Always include this file instead of <X11/$name.h>.
60 * This file was generated automatically by tools/make_X11wrappers
62 * Copyright 1998 Kristian Nielsen
65 #ifndef __WINE_TS$ucname\_H
66 #define __WINE_TS$ucname\_H
68 $x11_incl#include <X11/$extensions_dir$name.h>
70 END
72 print OUTC <<END;
74 * Thread safe wrappers around $name calls.
75 * This file was generated automatically by tools/make_X11wrappers
76 * DO NOT EDIT!
79 $x11_incl#include <X11/$extensions_dir$name.h>
80 #include "x11drv.h"
81 #include "stddebug.h"
82 #include "debug.h"
83 END
85 if($name eq "xpm") { # Handle as special case.
86 output_fn("XpmCreatePixmapFromData", "int",
87 "Display *, Drawable, char **, Pixmap *, Pixmap *, XpmAttributes *",
88 "Display *a0, Drawable a1, char **a2, Pixmap *a3, Pixmap *a4, XpmAttributes *a5",
89 "a0, a1, a2, a3, a4, a5");
90 output_fn("XpmAttributesSize", "int", "void", "void", "");
91 } elsif($name eq "XShm") {
92 output_fn("XShmQueryExtension", "Bool",
93 "Display *", "Display *a0", "a0");
94 output_fn("XShmPixmapFormat", "int",
95 "Display *", "Display *a0", "a0");
96 output_fn("XShmDetach", Status,
97 "Display *, XShmSegmentInfo *",
98 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
99 output_fn("XShmAttach", Status,
100 "Display *, XShmSegmentInfo *",
101 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
102 } else {
103 open(IN, "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | gcc -L$X11_include_dir -E - | grep -v '^[ \t]*\$'|") || die "open";
105 PROTO: while(<IN>) {
106 if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
107 $result_type = $1;
108 $fn_name = $2;
109 $result_type = "int" if $result_type =~ /^\s*$/;
110 @args = ();
111 while(<IN>) {
112 last if m'\)\s*;';
113 # Give up on vararg functions and function pointer args.
114 if(m'\.\.\.|\(\*\)') {
115 undef $fn_name;
116 last;
118 if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
119 $args[$#args+1] = $1;
122 # Skip if vararg, function pointer arg, or not needed.
123 next unless $fn_name;
124 next unless $want{$fn_name} && $want{$fn_name} == 1;
126 # Special case for no arguments (which is specified as "void").
127 if($#args == 0 && $args[0] eq "void") {
128 @args = ();
130 $proto = "";
131 $formals = "";
132 $actuals = "";
133 for($i = 0; $i <= $#args; $i++) {
134 $comma = $i < $#args ? ", " : "";
135 $proto .= "$args[$i]$comma";
136 $formals .= "$args[$i] a$i$comma";
137 $actuals .= "a$i$comma";
139 $proto = $formals = "void" if $#args == -1;
140 output_fn($fn_name, $result_type, $proto, $formals, $actuals);
145 if($name eq "Xlib") {
146 raw_output_fn("XSynchronize", "int (*r)(Display *)",
147 "int (*TSXSynchronize(Display *, Bool))(Display *)",
148 "int (*TSXSynchronize(Display *a0, Bool a1))(Display *)",
149 "a0, a1");
150 print OUTC "\nextern void _XInitImageFuncPtrs(XImage *);\n";
151 output_fn("_XInitImageFuncPtrs", "void", "XImage *", "XImage *a0", "a0");
152 } elsif($name eq "Xutil") {
153 output_fn("XDestroyImage", "int",
154 "struct _XImage *", "struct _XImage *a0", "a0");
155 output_fn("XGetPixel", "unsigned long",
156 "struct _XImage *, int, int",
157 "struct _XImage *a0, int a1, int a2",
158 "a0, a1, a2");
159 output_fn("XPutPixel", "int",
160 "struct _XImage *, int, int, unsigned long",
161 "struct _XImage *a0, int a1, int a2, unsigned long a3",
162 "a0, a1, a2, a3");
163 output_fn("XSubImage", "struct _XImage *",
164 "struct _XImage *, int, int, unsigned int, unsigned int",
165 "struct _XImage *a0, int a1, int a2, unsigned int a3, unsigned int a4",
166 "a0, a1, a2, a3, a4");
167 output_fn("XAddPixel", "int",
168 "struct _XImage *, long",
169 "struct _XImage *a0, long a1", "a0, a1");
170 output_fn("XUniqueContext", "XContext", "void", "void", "");
173 print OUTH <<END;
175 #endif /* __WINE_TS$ucname\_H */
180 foreach $i (keys %want) {
181 if($want{$i} == 1) {
182 print "Unresolved: $i\n";
187 sub output_fn {
188 # Example call:
189 # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
192 my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;
194 return raw_output_fn($fn_name,
195 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
196 "$result_type TS$fn_name($protos)",
197 "$result_type TS$fn_name($formals)",
198 $actuals);
201 sub raw_output_fn {
202 # Example call:
203 # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
206 my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;
208 return undef unless $want{$fn_name} && $want{$fn_name} == 1;
210 print OUTC "\n$defdecl\n";
211 print OUTH "extern $protodecl;\n";
212 # print OUTH "#define $fn_name TS$fn_name\n";
213 print OUTC "{\n";
214 print OUTC " $resultdecl;\n" if $resultdecl;
215 print OUTC " dprintf_x11(stddeb, \"Call $fn_name\\n\");\n";
216 print OUTC " EnterCriticalSection( &X11DRV_CritSection );\n";
217 print OUTC " ";
218 print OUTC "r = " if $resultdecl;
219 print OUTC "$fn_name($actuals);\n";
220 print OUTC " LeaveCriticalSection( &X11DRV_CritSection );\n";
221 print OUTC " dprintf_x11(stddeb, \"Ret $fn_name\\n\");\n";
222 print OUTC " return r;\n" if $resultdecl;
223 print OUTC "}\n";
224 $want{$fn_name} = 2;
225 return 1;