Removed gets message.
[wine] / tools / make_X11wrappers
1 #!/usr/bin/perl -w
2
3 # Create threads safe wrappers around X11 calls.
4 #
5 # Copyright 1998 Kristian Nielsen.
6 #
7
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/).
14 #
15 # This program expects to be run from Wine's main directory.
16
17 $X11_include_dir = "/usr/X11R6/include";
18 $outdir = "tsx11";
19 $wantfile = "$outdir/X11_calls";
20 @dolist = ("Xlib", "Xresource", "Xutil", "xpm", "XShm", "xf86dga");
21
22 # First read list of wanted function names.
23
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 '$_'";
32     }
33 }
34 close(WANT);
35
36 foreach $name (@dolist) {
37
38     $ucname = uc $name;
39     $lcname = lc $name;
40
41     $outfile = "/ts_$lcname";
42     open(OUTC, ">$outdir/$outfile.c") || die "open";
43     open(OUTH, ">include/$outfile.h") || die "open";
44
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";
53     }
54     if($name eq "xf86dga")  {
55         $x11_incl = "#include <X11/Xlib.h>\n";
56         $extensions_dir = "extensions/";
57         $pre_file = "#include \"config.h\"\n#ifdef HAVE_LIBXXF86DGA\n";
58         $post_file = "#endif";
59     }
60     if($name eq "XShm") {
61         $extensions_dir = "extensions/";
62     }
63
64     print OUTH <<END;
65 /*
66  * Thread safe wrappers around $name calls.
67  * Always include this file instead of <X11/$name.h>.
68  * This file was generated automatically by tools/make_X11wrappers
69  *
70  * Copyright 1998 Kristian Nielsen
71  */
72
73 #ifndef __WINE_TS$ucname\_H
74 #define __WINE_TS$ucname\_H
75
76 $x11_incl#include <X11/$extensions_dir$name.h>
77
78 END
79
80     print OUTC <<END;
81 /*
82  * Thread safe wrappers around $name calls.
83  * This file was generated automatically by tools/make_X11wrappers
84  * DO NOT EDIT!
85  */
86 $pre_file
87 $x11_incl#include <X11/$extensions_dir$name.h>
88 #include "x11drv.h"
89 #include "debug.h"
90 END
91
92     if($name eq "xpm") {        # Handle as special case.
93         output_fn("XpmCreatePixmapFromData", "int",
94                   "Display *, Drawable, char **, Pixmap *, Pixmap *, XpmAttributes *",
95                   "Display *a0, Drawable a1, char **a2, Pixmap *a3, Pixmap *a4, XpmAttributes *a5",
96                   "a0, a1, a2, a3, a4, a5");
97         output_fn("XpmAttributesSize", "int", "void", "void", "");
98     } elsif($name eq "XShm") {
99         output_fn("XShmQueryExtension", "Bool",
100                   "Display *", "Display *a0", "a0");
101         output_fn("XShmPixmapFormat", "int",
102                   "Display *", "Display *a0", "a0");
103         output_fn("XShmDetach", Status,
104                   "Display *, XShmSegmentInfo *",
105                   "Display *a0, XShmSegmentInfo *a1", "a0, a1");
106         output_fn("XShmAttach", Status,
107                   "Display *, XShmSegmentInfo *",
108                   "Display *a0, XShmSegmentInfo *a1", "a0, a1");
109         output_fn("XShmPutImage", Status,
110                   "Display *, Drawable, GC, XImage *, int, int, int, int, unsigned int, unsigned int, Bool",
111                   "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");
112     } elsif($name eq "xf86dga") {
113         output_fn("XF86DGAQueryVersion",Bool,
114                 "Display*,int*,int*",
115                 "Display*a0,int*a1,int*a2",
116                 "a0,a1,a2"
117         );
118         output_fn("XF86DGAQueryExtension",Bool,
119                 "Display*,int*,int*",
120                 "Display*a0,int*a1,int*a2",
121                 "a0,a1,a2"
122         );
123         output_fn("XF86DGAGetVideo",Status,
124                 "Display*,int,char**,int*,int*,int*",
125                 "Display*a0,int a1,char**a2,int*a3,int*a4,int*a5",
126                 "a0,a1,a2,a3,a4,a5"
127         );
128         output_fn("XF86DGADirectVideo",Status,
129                 "Display*,int,int",
130                 "Display*a0,int a1,int a2",
131                 "a0,a1,a2"
132         );
133         output_fn("XF86DGAGetViewPortSize",Status,
134                 "Display*,int,int*,int*",
135                 "Display*a0,int a1,int *a2,int *a3",
136                 "a0,a1,a2,a3"
137         );
138         output_fn("XF86DGASetViewPort",Status,
139                 "Display*,int,int,int",
140                 "Display*a0,int a1,int a2,int a3",
141                 "a0,a1,a2,a3"
142         );
143         output_fn("XF86DGAInstallColormap",Status,
144                 "Display*,int,Colormap",
145                 "Display*a0,int a1,Colormap a2",
146                 "a0,a1,a2"
147         );
148         output_fn("XF86DGAQueryDirectVideo",Status,
149                 "Display*,int,int*",
150                 "Display*a0,int a1,int *a2",
151                 "a0,a1,a2"
152         );
153         output_fn("XF86DGAViewPortChanged",Status,
154                 "Display*,int,int",
155                 "Display*a0,int a1,int a2",
156                 "a0,a1,a2"
157         );
158     } else {
159         open(IN, "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | gcc -L$X11_include_dir -E - | grep -v '^[ \t]*\$'|") || die "open";
160
161       PROTO: while(<IN>) {
162           if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
163               $result_type = $1;
164               $fn_name = $2;
165               $result_type = "int" if $result_type =~ /^\s*$/;
166               @args = ();
167               while(<IN>) {
168                   last if m'\)\s*;';
169                   # Give up on vararg functions and function pointer args.
170                   if(m'\.\.\.|\(\*\)') {
171                       undef $fn_name;
172                       last;
173                   }
174                   if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
175                       $args[$#args+1] = $1;
176                       if ($1 =~ /char\s*\[/) { # small hack for XQueryKeymap
177                         $args[$#args] = "char*";
178                       }
179                   }
180               }
181               # Skip if vararg, function pointer arg, or not needed.
182               next unless $fn_name;
183               next unless $want{$fn_name} && $want{$fn_name} == 1;
184
185               # Special case for no arguments (which is specified as "void").
186               if($#args == 0 && $args[0] eq "void") {
187                   @args = ();
188               }
189               $proto = "";
190               $formals = "";
191               $actuals = "";
192               for($i = 0; $i <= $#args; $i++) {
193                   $comma = $i < $#args ? ", " : "";
194                   $proto .= "$args[$i]$comma";
195                   $formals .= "$args[$i] a$i$comma";
196                   $actuals .= "a$i$comma";
197               }
198               $proto = $formals = "void" if $#args == -1;
199               output_fn($fn_name, $result_type, $proto, $formals, $actuals);
200           }
201       }
202     }
203
204     if($name eq "Xlib") {
205         raw_output_fn("XSynchronize", "int (*r)(Display *)",
206                   "int (*TSXSynchronize(Display *, Bool))(Display *)",
207                   "int (*TSXSynchronize(Display *a0, Bool a1))(Display *)",
208                   "a0, a1");
209         print OUTC "\nextern void _XInitImageFuncPtrs(XImage *);\n";
210         output_fn("_XInitImageFuncPtrs", "void", "XImage *", "XImage *a0", "a0");
211     } elsif($name eq "Xutil") {
212         output_fn("XDestroyImage", "int",
213                   "struct _XImage *", "struct _XImage *a0", "a0");
214         output_fn("XGetPixel", "unsigned long",
215                   "struct _XImage *, int, int",
216                   "struct _XImage *a0, int a1, int a2",
217                   "a0, a1, a2");
218         output_fn("XPutPixel", "int",
219                   "struct _XImage *, int, int, unsigned long",
220                   "struct _XImage *a0, int a1, int a2, unsigned long a3",
221                   "a0, a1, a2, a3");
222         output_fn("XSubImage", "struct _XImage *",
223                   "struct _XImage *, int, int, unsigned int, unsigned int",
224                   "struct _XImage *a0, int a1, int a2, unsigned int a3, unsigned int a4",
225                   "a0, a1, a2, a3, a4");
226         output_fn("XAddPixel", "int",
227                   "struct _XImage *, long",
228                   "struct _XImage *a0, long a1", "a0, a1");
229         output_fn("XUniqueContext", "XContext", "void", "void", "");
230     }
231
232     print OUTH <<END;
233
234 #endif /* __WINE_TS$ucname\_H */
235 END
236     print OUTC <<END;
237 $post_file
238 END
239
240
241
242 }
243
244 foreach $i (keys %want) {
245     if($want{$i} == 1) {
246         print "Unresolved: $i\n";
247     }
248 }
249
250
251 sub output_fn {
252     # Example call:
253     # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
254     #
255
256     my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;
257
258     return raw_output_fn($fn_name,
259                          $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
260                          "$result_type TS$fn_name($protos)",
261                          "$result_type TS$fn_name($formals)",
262                          $actuals);
263 }
264
265 sub raw_output_fn {
266     # Example call:
267     # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
268     #
269
270     my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;
271
272     return undef unless $want{$fn_name} && $want{$fn_name} == 1;
273
274     print OUTC "\n$defdecl\n";
275     print OUTH "extern $protodecl;\n";
276 #    print OUTH "#define $fn_name TS$fn_name\n";
277     print OUTC "{\n";
278     print OUTC "  $resultdecl;\n" if $resultdecl;
279     print OUTC "  TRACE(x11, \"Call $fn_name\\n\");\n";
280     print OUTC "  EnterCriticalSection( &X11DRV_CritSection );\n";
281     print OUTC "  ";
282     print OUTC "r = " if $resultdecl;
283     print OUTC "$fn_name($actuals);\n";
284     print OUTC "  LeaveCriticalSection( &X11DRV_CritSection );\n";
285     print OUTC "  TRACE(x11, \"Ret $fn_name\\n\");\n";
286     print OUTC "  return r;\n" if $resultdecl;
287     print OUTC "}\n";
288     $want{$fn_name} = 2;
289     return 1;
290 }