#!/usr/bin/perl -w

# Create threads safe wrappers around X11 calls.
#
# Copyright 1998 Kristian Nielsen.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# FIXME: This does not do full C prototype parsing, but relies on
# knowledge on how the X11 include files are formatted. It will
# probably need to be modified for new include files. It also fails
# for certain prototypes (notably those with function pointer
# arguments or results), so these must be added manually. And it
# relies on a fixed location of X11 includes (/usr/X11R6/include/).
#
# This program expects to be run from Wine's main directory.

$X11_include_dir = "/usr/X11/include";
$outdir = "tsx11";
$wantfile = "$outdir/X11_calls";
@dolist = ("Xlib", "Xresource", "Xutil", "XShm", "xf86dga", "xf86dga2", "xf86vmode", "shape", "xvideo");

# First read list of wanted function names.

open(WANT, $wantfile) || die "open";
while(<WANT>) {
    next if /^\s*\#/;		# Skip comment lines.
    next if /^\s*$/;		# Skip empty lines.
    if(/^\s*([a-zA-Z0-9_]+)\s*$/) {
	$want{$1} = 1;
    } else {
	die "syntax error in file '$wantfile', in line '$_'";
    }
}
close(WANT);

foreach $name (@dolist) {

    $ucname = uc $name;
    $lcname = lc $name;

    $outfile = "/ts_$lcname";
    open(OUTC, ">$outdir/$outfile.c") || die "open";
    open(OUTH, ">include/$outfile.h") || die "open";

    $x11_incl = "";
    $extensions_dir = "";
    $pre_file = "#ifdef HAVE_X11_XLIB_H\n";
    $post_file = "#endif /* defined(HAVE_X11_XLIB_H) */\n";
    $inc_name = $name;
    if($name eq "Xutil" || $name eq "Xresource" || $name eq "XShm") {
	$x11_incl = "#include <X11/Xlib.h>\n";
	# For Xutil, we need X11/Xresource.h for XUniqueContext().
	$x11_incl .= "#include <X11/Xresource.h>\n" if $name eq "Xutil";
    }
    if($name eq "xf86dga")  {
    	$x11_incl = "#include <X11/Xlib.h>\n";
	$extensions_dir = "extensions/";
	$pre_file = "#ifdef HAVE_LIBXXF86DGA\n";
	$post_file = "#endif /* defined(HAVE_LIBXXF86DGA) */\n";
    }
    if($name eq "xf86dga2")  {
    	$x11_incl = "#include <X11/Xlib.h>\n";
	$extensions_dir = "extensions/";
	$pre_file = "#ifdef HAVE_LIBXXF86DGA2\n";
	$post_file = "#endif /* defined(HAVE_LIBXXF86DGA2) */\n";
	$inc_name = "xf86dga";
    }
    if($name eq "XShm") {
	$extensions_dir = "extensions/";
	$pre_file = "#ifdef HAVE_LIBXXSHM\n";
	$post_file = "#endif /* defined(HAVE_LIBXXSHM) */\n";
    }
    if($name eq "xf86vmode") {
     	$x11_incl = "#include <X11/Xlib.h>\n";
	$extensions_dir = "extensions/";
	$pre_file = "#include \"windef.h\"\n#ifdef HAVE_LIBXXF86VM\n#define XMD_H\n#include \"basetsd.h\"\n";
	$post_file = "#endif /* defined(HAVE_LIBXXF86VM) */\n";
    }
    if($name eq "shape") {
        $extensions_dir = "extensions/";
        $pre_file = "#ifdef HAVE_LIBXSHAPE\n#include <X11/IntrinsicP.h>\n";
        $post_file = "#endif /* defined(HAVE_LIBXSHAPE) */\n";
        $inc_name = "shape";
    }
    if($name eq "xvideo")  {
    	$x11_incl = "#include <X11/Xlib.h>\n#include <X11/extensions/Xv.h>\n#include <X11/extensions/XShm.h>\n";
	$extensions_dir = "extensions/";
	$pre_file = "#ifdef HAVE_XVIDEO\n";
	$post_file = "#endif /* defined(HAVE_XVIDEO) */\n";
	$inc_name = "Xvlib";
    }


    print OUTH <<END;
/*
 * Thread safe wrappers around $name calls.
 * Always include this file instead of <X11/$name.h>.
 * This file was generated automatically by tools/make_X11wrappers
 * DO NOT EDIT!
 */

#ifndef __WINE_TS_$ucname\_H
#define __WINE_TS_$ucname\_H

#ifndef __WINE_CONFIG_H
# error You must include config.h to use this header
#endif

$pre_file
$x11_incl#include <X11/$extensions_dir$inc_name.h>

extern void (*wine_tsx11_lock)(void);
extern void (*wine_tsx11_unlock)(void);

END

    print OUTC <<END;
/*
 * Thread safe wrappers around $name calls.
 * This file was generated automatically by tools/make_X11wrappers
 * DO NOT EDIT!
 */

#include "config.h"

$pre_file
$x11_incl#include <X11/$extensions_dir$inc_name.h>

#include "ts_$lcname.h"

END

    if ($name eq "XShm") {
	output_fn("XShmQueryExtension", "Bool",
		  "Display *", "Display *a0", "a0");
	output_fn("XShmQueryVersion", "Bool",
		  "Display *, int *, int *, Bool *",
		  "Display *a0, int *a1, int *a2, Bool *a3", "a0, a1, a2, a3");
	output_fn("XShmPixmapFormat", "int",
		  "Display *", "Display *a0", "a0");
	output_fn("XShmAttach", Status,
		  "Display *, XShmSegmentInfo *",
		  "Display *a0, XShmSegmentInfo *a1", "a0, a1");
	output_fn("XShmDetach", Status,
		  "Display *, XShmSegmentInfo *",
		  "Display *a0, XShmSegmentInfo *a1", "a0, a1");
	output_fn("XShmPutImage", Status,
		  "Display *, Drawable, GC, XImage *, int, int, int, int, unsigned int, unsigned int, Bool",
		  "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");
	output_fn("XShmGetImage", Status,
		  "Display *, Drawable, XImage *, int, int, unsigned long",
		  "Display *a0, Drawable a1, XImage *a2, int a3, int a4, unsigned long a5",
		  "a0, a1, a2, a3, a4, a5");
	output_fn("XShmCreateImage", "XImage *",
		  "Display *, Visual *, unsigned int, int, char *, XShmSegmentInfo *, unsigned int, unsigned int",
		  "Display *a0, Visual *a1, unsigned int a2, int a3, char *a4, XShmSegmentInfo *a5, unsigned int a6, unsigned int a7",
		  "a0, a1, a2, a3, a4, a5, a6, a7");
	output_fn("XShmCreatePixmap", "Pixmap",
		  "Display *, Drawable, char *, XShmSegmentInfo *, unsigned int, unsigned int, unsigned int",
		  "Display *a0, Drawable a1, char *a2, XShmSegmentInfo *a3, unsigned int a4, unsigned int a5, unsigned int a6",
		  "a0, a1, a2, a3, a4, a5, a6");
    } elsif($name eq "xf86dga") {
    	output_fn("XF86DGAQueryVersion",Bool,
		"Display*,int*,int*",
		"Display*a0,int*a1,int*a2",
		"a0,a1,a2"
	);
    	output_fn("XF86DGAQueryExtension",Bool,
		"Display*,int*,int*",
		"Display*a0,int*a1,int*a2",
		"a0,a1,a2"
	);
	output_fn("XF86DGAGetVideo",Status,
		"Display*,int,char**,int*,int*,int*",
		"Display*a0,int a1,char**a2,int*a3,int*a4,int*a5",
		"a0,a1,a2,a3,a4,a5"
	);
	output_fn("XF86DGADirectVideo",Status,
		"Display*,int,int",
		"Display*a0,int a1,int a2",
		"a0,a1,a2"
	);
	output_fn("XF86DGAGetViewPortSize",Status,
		"Display*,int,int*,int*",
		"Display*a0,int a1,int *a2,int *a3",
		"a0,a1,a2,a3"
	);
	output_fn("XF86DGASetViewPort",Status,
		"Display*,int,int,int",
		"Display*a0,int a1,int a2,int a3",
		"a0,a1,a2,a3"
	);
	output_fn("XF86DGAInstallColormap",Status,
		"Display*,int,Colormap",
		"Display*a0,int a1,Colormap a2",
		"a0,a1,a2"
	);
	output_fn("XF86DGAQueryDirectVideo",Status,
		"Display*,int,int*",
		"Display*a0,int a1,int *a2",
		"a0,a1,a2"
	);
	output_fn("XF86DGAViewPortChanged",Status,
		"Display*,int,int",
		"Display*a0,int a1,int a2",
		"a0,a1,a2"
	);
    } elsif($name eq "xf86dga2") {
    	output_fn_short("Bool", "XDGAQueryVersion", "Display*" ,"int*","int*");
    	output_fn_short("Bool", "XDGAQueryExtension", "Display*" ,"int*","int*");
    	output_fn_short("XDGAMode*", "XDGAQueryModes", "Display*" ,"int", "int*");
    	output_fn_short("XDGADevice*", "XDGASetMode", "Display*" ,"int","int");
    	output_fn_short("Bool", "XDGAOpenFramebuffer", "Display*" ,"int");
    	output_fn_short("void", "XDGACloseFramebuffer", "Display*" ,"int");
    	output_fn_short("void", "XDGASetViewport", "Display*" ,"int", "int", "int", "int");
    	output_fn_short("void", "XDGAInstallColormap", "Display*" , "int", "Colormap");
	output_fn_short("Colormap", "XDGACreateColormap", "Display*" ,"int", "XDGADevice*", "int");
    	output_fn_short("void", "XDGASelectInput", "Display*" ,"int", "long");
    	output_fn_short("void", "XDGAFillRectangle", "Display*" ,"int", "int", "int", "unsigned int", "unsigned int", "unsigned long");
   	output_fn_short("void", "XDGACopyArea", "Display*" ,"int", "int", "int", "unsigned int", "unsigned int", "int", "int");
   	output_fn_short("void", "XDGACopyTransparentArea", "Display*" ,"int", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned long");
    	output_fn_short("int", "XDGAGetViewportStatus", "Display*" ,"int");
    	output_fn_short("void", "XDGASync", "Display*" ,"int");
    	output_fn_short("Bool", "XDGASetClientVersion", "Display*");
    	output_fn_short("void", "XDGAChangePixmapMode", "Display*" ,"int", "int*", "int*", "int");
	output_fn_short("void", "XDGAKeyEventToXKeyEvent", "XDGAKeyEvent*" ,"XKeyEvent*");
    } elsif ($name eq "xvideo") {
	output_fn_short("int", "XvQueryExtension", "Display*", "unsigned int*", "unsigned int*", "unsigned int*", "unsigned int*", "unsigned int*");
	output_fn_short("int", "XvQueryAdaptors", "Display*", "Window", "unsigned int*", "XvAdaptorInfo**");
	output_fn_short("int", "XvQueryEncodings", "Display*", "XvPortID", "unsigned int*", "XvEncodingInfo**");
	output_fn_short("int", "XvPutVideo", "Display*", "XvPortID", "Drawable", "GC", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned int", "unsigned int");
	output_fn_short("int", "XvPutStill", "Display*", "XvPortID", "Drawable", "GC", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned int", "unsigned int");
	output_fn_short("int", "XvGetVideo", "Display*", "XvPortID", "Drawable", "GC", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned int", "unsigned int");
	output_fn_short("int", "XvGetStill", "Display*", "XvPortID", "Drawable", "GC", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned int", "unsigned int");
	output_fn_short("int", "XvStopVideo", "Display*", "XvPortID", "Drawable");
	output_fn_short("int", "XvGrabPort", "Display*", "XvPortID", "Time");
	output_fn_short("int", "XvUngrabPort", "Display*", "XvPortID", "Time");
	output_fn_short("int", "XvSelectVideoNotify", "Display*", "Drawable", "Bool");
	output_fn_short("int", "XvSelectPortNotify", "Display*", "XvPortID", "Bool");
	output_fn_short("int", "XvSetPortAttribute", "Display*", "XvPortID", "Atom", "int");
	output_fn_short("int", "XvGetPortAttribute", "Display*", "XvPortID", "Atom", "int*");
	output_fn_short("int", "XvQueryBestSize", "Display*", "XvPortID", "Bool", "unsigned int", "unsigned int", "unsigned int", "unsigned int", "unsigned int*", "unsigned int*");
	output_fn_short("XvAttribute*", "XvQueryPortAttributes", "Display*", "XvPortID", "int*");
	output_fn_short("void", "XvFreeAdaptorInfo", "XvAdaptorInfo*");
	output_fn_short("void", "XvFreeEncodingInfo", "XvEncodingInfo*");
	output_fn_short("XvImageFormatValues *", "XvListImageFormats", "Display*", "XvPortID", "int*");
	output_fn_short("XvImage *", "XvCreateImage", "Display*", "XvPortID", "int", "char*", "int", "int");
	output_fn_short("int", "XvPutImage", "Display*", "XvPortID", "Drawable", "GC", "XvImage*", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned int", "unsigned int");
	output_fn_short("int", "XvShmPutImage", "Display*", "XvPortID", "Drawable", "GC", "XvImage*", "int", "int", "unsigned int", "unsigned int", "int", "int", "unsigned int", "unsigned int", "Bool");
output_fn_short("XvImage *", "XvShmCreateImage", "Display*", "XvPortID", "int", "char*", "int", "int", "XShmSegmentInfo*");
    } elsif($name eq "xf86vmode") {
    	output_fn("XF86VidModeQueryVersion",Bool,
		"Display*,int*,int*",
		"Display*a0,int*a1,int*a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeQueryExtension",Bool,
		"Display*,int*,int*",
		"Display*a0,int*a1,int*a2",
		"a0,a2,a2"
	);
	output_fn("XF86VidModeGetModeLine",Bool,
		"Display*,int,int*,XF86VidModeModeLine*",
		"Display*a0,int a1,int*a2,XF86VidModeModeLine*a3",
		"a0,a1,a2,a3"
	);
	output_fn("XF86VidModeGetAllModeLines",Bool,
		"Display*,int,int*,XF86VidModeModeInfo***",
		"Display*a0,int a1,int*a2,XF86VidModeModeInfo***a3",
		"a0,a1,a2,a3"
	);
	output_fn("XF86VidModeAddModeLine",Bool,
		"Display*,int,XF86VidModeModeInfo*,XF86VidModeModeInfo*",
		"Display*a0,int a1,XF86VidModeModeInfo*a2,XF86VidModeModeInfo*a3",
		"a0,a1,a2,a3"
	);
	output_fn("XF86VidModeDeleteModeLine",Bool,
		"Display*,int,XF86VidModeModeInfo*",
		"Display*a0,int a1,XF86VidModeModeInfo*a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeModModeLine",Bool,
		"Display*,int,XF86VidModeModeLine*",
		"Display*a0,int a1,XF86VidModeModeLine*a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeValidateModeLine",Status,
		"Display*,int,XF86VidModeModeInfo*",
		"Display*a0,int a1,XF86VidModeModeInfo*a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeSwitchMode",Bool,
		"Display*,int,int",
		"Display*a0,int a1,int a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeSwitchToMode",Bool,
		"Display*,int,XF86VidModeModeInfo*",
		"Display*a0,int a1,XF86VidModeModeInfo*a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeLockModeSwitch",Bool,
		"Display*,int,int",
		"Display*a0,int a1,int a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeGetMonitor",Bool,
		"Display*,int,XF86VidModeMonitor*",
		"Display*a0,int a1,XF86VidModeMonitor*a2",
		"a0,a1,a2"
	);
	output_fn("XF86VidModeGetViewPort",Bool,
		"Display*,int,int*,int*",
		"Display*a0,int a1,int*a2,int*a3",
		"a0,a1,a2,a3"
	);
	output_fn("XF86VidModeSetViewPort",Bool,
		"Display*,int,int,int",
		"Display*a0,int a1,int a2,int a3",
		"a0,a1,a2,a3"
	);

    } else {
	open(IN,
	     "echo \"$x11_incl#include <X11/$extensions_dir$name.h>\" | " .
	     "gcc -L$X11_include_dir -DNeedFunctionPrototypes -E - | " .
	     "grep -v '^[ \t]*\$)' |"
	     ) || die "open";

      PROTO: while(<IN>) {
	  if(m'extern\s+([^()]*)\b([a-zA-Z0-9_]+)\s*\(') {
	      $result_type = $1;
	      $fn_name = $2;
	      $result_type = "int" if $result_type =~ /^\s*$/;
	      @args = ();
	      while(<IN>) {
		  last if m'\)\s*;';
		  # Give up on vararg functions and function pointer args.
		  if(m'\.\.\.|\(\*\)') {
		      undef $fn_name;
		      last;
		  }
		  if(m'\s*([^,]*[^, \t])\s*(,?\n)') {
		      $args[$#args+1] = $1;
		      if ($1 =~ /char\s*\[/) { # small hack for XQueryKeymap
		      	$args[$#args] = "char*";
		      }
		  }
	      }
	      # Skip if vararg, function pointer arg, or not needed.
	      next unless $fn_name;
	      next unless $want{$fn_name} && $want{$fn_name} == 1;

	      # Special case for no arguments (which is specified as "void").
	      if($#args == 0 && $args[0] eq "void") {
		  @args = ();
	      }
	      $proto = "";
	      $formals = "";
	      $actuals = "";
	      for($i = 0; $i <= $#args; $i++) {
		  $comma = $i < $#args ? ", " : "";
		  $proto .= "$args[$i]$comma";
		  $formals .= "$args[$i] a$i$comma";
		  $actuals .= "a$i$comma";
	      }
	      $proto = $formals = "void" if $#args == -1;
	      output_fn($fn_name, $result_type, $proto, $formals, $actuals);
	  }
      }
    }

    if($name eq "Xlib") {
	raw_output_fn("XSynchronize", "int (*r)(Display *)",
		  "int (*TSXSynchronize(Display *, Bool))(Display *)",
		  "int (*TSXSynchronize(Display *a0, Bool a1))(Display *)",
		  "a0, a1");
    } elsif($name eq "Xutil") {
	output_fn("XDestroyImage", "int",
		  "struct _XImage *", "struct _XImage *a0", "a0");
	output_fn("XGetPixel", "unsigned long",
		  "struct _XImage *, int, int",
		  "struct _XImage *a0, int a1, int a2",
		  "a0, a1, a2");
	output_fn("XPutPixel", "int",
		  "struct _XImage *, int, int, unsigned long",
		  "struct _XImage *a0, int a1, int a2, unsigned long a3",
		  "a0, a1, a2, a3");
	output_fn("XSubImage", "struct _XImage *",
		  "struct _XImage *, int, int, unsigned int, unsigned int",
		  "struct _XImage *a0, int a1, int a2, unsigned int a3, unsigned int a4",
		  "a0, a1, a2, a3, a4");
	output_fn("XAddPixel", "int",
		  "struct _XImage *, long",
		  "struct _XImage *a0, long a1", "a0, a1");
	output_fn("XUniqueContext", "XContext", "void", "void", "");
	output_fn("XDeleteContext", "int",
		  "Display*,XID,XContext",
		  "Display*a0,XID a1,XContext a2",
		  "a0,a1,a2");
    }

    print OUTH <<END;

$post_file
#endif /* __WINE_TS_$ucname\_H */
END
    print OUTC <<END;

$post_file
END



}

foreach $i (keys %want) {
    if($want{$i} == 1) {
	print "Unresolved: $i\n";
    }
}


sub output_fn {
    # Example call:
    # output_fn("main", "int", "int, char **", "int a0, char **a1", "a0, a1")
    #

    my ($fn_name, $result_type, $protos, $formals, $actuals) = @_;

    return raw_output_fn($fn_name,
			 $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
			 "$result_type TS$fn_name($protos)",
			 "$result_type TS$fn_name($formals)",
			 $actuals);
}

sub output_fn_short {
    # Example call:
    # output_fn_sort("Bool", "XDGAQueryExtension", "Display *", "int *", "int *");
    #
    my ($result_type, $fn_name, @args) = @_;

    my ($i, $proto, $formals, $actuals) = (0,
					   "$result_type TS$fn_name(",
					   "$result_type TS$fn_name(",
					   "");
    while ($val = shift @args) {
	$proto = $proto . $val;
	$formals = $formals . $val . " a$i";
	$actuals = $actuals . " a$i";
	$i++;
	if (@args) {
	    $proto = $proto . ", ";
	    $formals = $formals . ", ";
	    $actuals = $actuals . ", ";
	}
    }
    $proto = $proto . ")";
    $formals = $formals . ")";


    raw_output_fn($fn_name,
		  $result_type =~ /^\s*void\s*$/ ? "" : "$result_type r",
		  $proto,
		  $formals,
		  $actuals);
}

sub raw_output_fn {
    # Example call:
    # output_fn("main", "int r", "int main(int, char **)", "int main(int a0, char **a1)", "a0, a1")
    #

    my ($fn_name, $resultdecl, $protodecl, $defdecl, $actuals) = @_;

    return undef unless $want{$fn_name} && $want{$fn_name} == 1;

    print OUTC "\n$defdecl\n";
    print OUTH "extern $protodecl;\n";
#    print OUTH "#define $fn_name TS$fn_name\n";
    print OUTC "{\n";
    print OUTC "  $resultdecl;\n" if $resultdecl;
    print OUTC "  wine_tsx11_lock();\n";
    print OUTC "  ";
    print OUTC "r = " if $resultdecl;
    print OUTC "$fn_name($actuals);\n";
    print OUTC "  wine_tsx11_unlock();\n";
    print OUTC "  return r;\n" if $resultdecl;
    print OUTC "}\n";
    $want{$fn_name} = 2;
    return 1;
}