Release 20021031.
[wine/multimedia.git] / tools / make_X11wrappers
blob8c6691b88d69a59b722acfe801b89a22eb3e4716
1 #!/usr/bin/perl -w
3 # Create threads safe wrappers around X11 calls.
5 # Copyright 1998 Kristian Nielsen.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # FIXME: This does not do full C prototype parsing, but relies on
22 # knowledge on how the X11 include files are formatted. It will
23 # probably need to be modified for new include files. It also fails
24 # for certain prototypes (notably those with function pointer
25 # arguments or results), so these must be added manually. And it
26 # relies on a fixed location of X11 includes (/usr/X11R6/include/).
28 # This program expects to be run from Wine's main directory.
30 $X11_include_dir = "/usr/X11/include";
31 $outdir = "dlls/x11drv";
32 $wantfile = "$outdir/X11_calls";
33 @dolist = ("Xlib");
35 # First read list of wanted function names.
37 open(WANT, $wantfile) || die "open";
38 while(<WANT>) {
39 next if /^\s*\#/; # Skip comment lines.
40 next if /^\s*$/; # Skip empty lines.
41 if(/^\s*([a-zA-Z0-9_]+)\s*$/) {
42 $want{$1} = 1;
43 } else {
44 die "syntax error in file '$wantfile', in line '$_'";
47 close(WANT);
49 foreach $name (@dolist) {
51 $ucname = uc $name;
52 $lcname = lc $name;
54 $outfile = "/ts_$lcname";
55 open(OUTC, ">$outdir/$outfile.c") || die "open";
56 open(OUTH, ">$outdir/$outfile.h") || die "open";
58 $x11_incl = "";
59 $extensions_dir = "";
60 $pre_file = "#ifdef HAVE_X11_XLIB_H\n";
61 $post_file = "#endif /* defined(HAVE_X11_XLIB_H) */\n";
62 $inc_name = $name;
64 print OUTH <<END;
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 * DO NOT EDIT!
72 #ifndef __WINE_TS_$ucname\_H
73 #define __WINE_TS_$ucname\_H
75 #ifndef __WINE_CONFIG_H
76 # error You must include config.h to use this header
77 #endif
79 $pre_file
80 $x11_incl#include <X11/$extensions_dir$inc_name.h>
82 extern void wine_tsx11_lock(void);
83 extern void wine_tsx11_unlock(void);
85 END
87 print OUTC <<END;
89 * Thread safe wrappers around $name calls.
90 * This file was generated automatically by tools/make_X11wrappers
91 * DO NOT EDIT!
94 #include "config.h"
96 $pre_file
97 $x11_incl#include <X11/$extensions_dir$inc_name.h>
99 #include "ts_$lcname.h"
103 open(IN,
104 "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | " .
105 "gcc -L$X11_include_dir -DNeedFunctionPrototypes -E - | " .
106 "grep -v '^[ \t]*\$)' |"
107 ) || die "open";
109 PROTO: while(<IN>) {
110 if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
111 $result_type = $1;
112 $fn_name = $2;
113 $result_type = "int" if $result_type =~ /^\s*$/;
114 @args = ();
115 while(<IN>) {
116 last if m'\)\s*;';
117 # Give up on vararg functions and function pointer args.
118 if(m'\.\.\.|\(\*\)') {
119 undef $fn_name;
120 last;
122 if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
123 $args[$#args+1] = $1;
124 if ($1 =~ /char\s*\[/) { # small hack for XQueryKeymap
125 $args[$#args] = "char*";
129 # Skip if vararg, function pointer arg, or not needed.
130 next unless $fn_name;
131 next unless $want{$fn_name} && $want{$fn_name} == 1;
133 # Special case for no arguments (which is specified as "void").
134 if($#args == 0 && $args[0] eq "void") {
135 @args = ();
137 $proto = "";
138 $formals = "";
139 $actuals = "";
140 for($i = 0; $i <= $#args; $i++) {
141 $comma = $i < $#args ? ", " : "";
142 $proto .= "$args[$i]$comma";
143 $formals .= "$args[$i] a$i$comma";
144 $actuals .= "a$i$comma";
146 $proto = $formals = "void" if $#args == -1;
147 output_fn($fn_name, $result_type, $proto, $formals, $actuals);
151 print OUTH <<END;
153 $post_file
154 #endif /* __WINE_TS_$ucname\_H */
156 print OUTC "\n", $post_file;
159 foreach $i (keys %want) {
160 if($want{$i} == 1) {
161 print "Unresolved: $i\n";
166 sub output_fn {
167 # Example call:
168 # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
171 my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;
173 return raw_output_fn($fn_name,
174 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
175 "$result_type TS$fn_name($protos)",
176 "$result_type TS$fn_name($formals)",
177 $actuals);
180 sub output_fn_short {
181 # Example call:
182 # output_fn_sort("Bool", "XDGAQueryExtension", "Display *", "int *", "int *");
184 my ($result_type, $fn_name, @args) = @_;
186 my ($i, $proto, $formals, $actuals) = (0,
187 "$result_type TS$fn_name(",
188 "$result_type TS$fn_name(",
189 "");
190 while ($val = shift @args) {
191 $proto = $proto . $val;
192 $formals = $formals . $val . " a$i";
193 $actuals = $actuals . " a$i";
194 $i++;
195 if (@args) {
196 $proto = $proto . ", ";
197 $formals = $formals . ", ";
198 $actuals = $actuals . ", ";
201 $proto = $proto . ")";
202 $formals = $formals . ")";
205 raw_output_fn($fn_name,
206 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
207 $proto,
208 $formals,
209 $actuals);
212 sub raw_output_fn {
213 # Example call:
214 # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
217 my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;
219 return undef unless $want{$fn_name} && $want{$fn_name} == 1;
221 print OUTC "\n$defdecl\n";
222 print OUTH "extern $protodecl;\n";
223 # print OUTH "#define $fn_name TS$fn_name\n";
224 print OUTC "{\n";
225 print OUTC " $resultdecl;\n" if $resultdecl;
226 print OUTC " wine_tsx11_lock();\n";
227 print OUTC " ";
228 print OUTC "r = " if $resultdecl;
229 print OUTC "$fn_name($actuals);\n";
230 print OUTC " wine_tsx11_unlock();\n";
231 print OUTC " return r;\n" if $resultdecl;
232 print OUTC "}\n";
233 $want{$fn_name} = 2;
234 return 1;