Release 980201
[wine/multimedia.git] / tools / make_X11wrappers
blob8dffbb3a954d91688173420ba4c0837f78a46cfb
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
77 * Copyright 1998 Kristian Nielsen
80 $x11_incl#include <X11/$extensions_dir$name.h>
81 #include "tsx11defs.h"
82 #include "stddebug.h"
83 #include "debug.h"
84 END
86 if($name eq "xpm") { # Handle as special case.
87 output_fn("XpmCreatePixmapFromData", "int",
88 "Display *, Drawable, char **, Pixmap *, Pixmap *, XpmAttributes *",
89 "Display *a0, Drawable a1, char **a2, Pixmap *a3, Pixmap *a4, XpmAttributes *a5",
90 "a0, a1, a2, a3, a4, a5");
91 output_fn("XpmAttributesSize", "int", "void", "void", "");
92 } elsif($name eq "XShm") {
93 output_fn("XShmQueryExtension", "Bool",
94 "Display *", "Display *a0", "a0");
95 output_fn("XShmPixmapFormat", "int",
96 "Display *", "Display *a0", "a0");
97 output_fn("XShmDetach", Status,
98 "Display *, XShmSegmentInfo *",
99 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
100 output_fn("XShmAttach", Status,
101 "Display *, XShmSegmentInfo *",
102 "Display *a0, XShmSegmentInfo *a1", "a0, a1");
103 } else {
104 open(IN, "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | gcc -L$X11_include_dir -E - | grep -v '^[ \t]*\$'|") || die "open";
106 PROTO: while(<IN>) {
107 if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
108 $result_type = $1;
109 $fn_name = $2;
110 $result_type = "int" if $result_type =~ /^\s*$/;
111 @args = ();
112 while(<IN>) {
113 last if m'\)\s*;';
114 # Give up on vararg functions and function pointer args.
115 if(m'\.\.\.|\(\*\)') {
116 undef $fn_name;
117 last;
119 if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
120 $args[$#args+1] = $1;
123 # Skip if vararg, function pointer arg, or not needed.
124 next unless $fn_name;
125 next unless $want{$fn_name} && $want{$fn_name} == 1;
127 # Special case for no arguments (which is specified as "void").
128 if($#args == 0 && $args[0] eq "void") {
129 @args = ();
131 $proto = "";
132 $formals = "";
133 $actuals = "";
134 for($i = 0; $i <= $#args; $i++) {
135 $comma = $i < $#args ? ", " : "";
136 $proto .= "$args[$i]$comma";
137 $formals .= "$args[$i] a$i$comma";
138 $actuals .= "a$i$comma";
140 $proto = $formals = "void" if $#args == -1;
141 output_fn($fn_name, $result_type, $proto, $formals, $actuals);
146 if($name eq "Xlib") {
147 raw_output_fn("XSynchronize", "int (*r)(Display *)",
148 "int (*TSXSynchronize(Display *, Bool))(Display *)",
149 "int (*TSXSynchronize(Display *a0, Bool a1))(Display *)",
150 "a0, a1");
151 print OUTC "\nextern void _XInitImageFuncPtrs(XImage *);\n";
152 output_fn("_XInitImageFuncPtrs", "void", "XImage *", "XImage *a0", "a0");
153 } elsif($name eq "Xutil") {
154 output_fn("XDestroyImage", "int",
155 "struct _XImage *", "struct _XImage *a0", "a0");
156 output_fn("XGetPixel", "unsigned long",
157 "struct _XImage *, int, int",
158 "struct _XImage *a0, int a1, int a2",
159 "a0, a1, a2");
160 output_fn("XPutPixel", "int",
161 "struct _XImage *, int, int, unsigned long",
162 "struct _XImage *a0, int a1, int a2, unsigned long a3",
163 "a0, a1, a2, a3");
164 output_fn("XSubImage", "struct _XImage *",
165 "struct _XImage *, int, int, unsigned int, unsigned int",
166 "struct _XImage *a0, int a1, int a2, unsigned int a3, unsigned int a4",
167 "a0, a1, a2, a3, a4");
168 output_fn("XAddPixel", "int",
169 "struct _XImage *, long",
170 "struct _XImage *a0, long a1", "a0, a1");
171 output_fn("XUniqueContext", "XContext", "void", "void", "");
174 print OUTH <<END;
176 #endif /* __WINE_TS$ucname\_H */
181 foreach $i (keys %want) {
182 if($want{$i} == 1) {
183 print "Unresolved: $i\n";
188 sub output_fn {
189 # Example call:
190 # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
193 my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;
195 return raw_output_fn($fn_name,
196 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
197 "$result_type TS$fn_name($protos)",
198 "$result_type TS$fn_name($formals)",
199 $actuals);
202 sub raw_output_fn {
203 # Example call:
204 # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
207 my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;
209 return undef unless $want{$fn_name} && $want{$fn_name} == 1;
211 print OUTC "\n$defdecl\n";
212 print OUTH "extern $protodecl;\n";
213 # print OUTH "#define $fn_name TS$fn_name\n";
214 print OUTC "{\n";
215 print OUTC " $resultdecl;\n" if $resultdecl;
216 print OUTC " dprintf_x11(stddeb, \"Call $fn_name\\n\");\n";
217 print OUTC " X11_LOCK();\n";
218 print OUTC " ";
219 print OUTC "r = " if $resultdecl;
220 print OUTC "$fn_name($actuals);\n";
221 print OUTC " X11_UNLOCK();\n";
222 print OUTC " dprintf_x11(stddeb, \"Ret $fn_name\\n\");\n";
223 print OUTC " return r;\n" if $resultdecl;
224 print OUTC "}\n";
225 $want{$fn_name} = 2;
226 return 1;