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