#!/usr/bin/perl
#
# This script has been (or is hereby) released into the public domain by
# its author, Karl J. Runge <runge@karlrunge.com>. This applies worldwide.
# 
# In case this is not legally possible: Karl J. Runge grants anyone the
# right to use this work for any purpose, without any conditions, unless
# such conditions are required by law.

while (<>) {
	if (/^#include.*"rfb.h"/) {
		print <<END;
#include <rfb/rfb.h>
#define Bool rfbBool
#define CARD32 uint32_t
#define CARD16 uint16_t
#define CARD8 uint8_t
#define xalloc malloc
#define xrealloc realloc
#define rfbTightNoZlib 0x0A
#define tightSubsampLevel correMaxWidth

#if LIBVNCSERVER_HAVE_LIBPTHREAD && LIBVNCSERVER_HAVE_TLS
#define TLS __thread
#else
#define TLS
#endif

END
		next;
	}
	foreach $func (qw(FindBestSolidArea ExtendSolidArea CheckSolidTile CheckSolidTile##bpp CheckSolidTile8 CheckSolidTile16 CheckSolidTile32 Pack24)) {
		if (/static.*\b\Q$func\E\b/ && !exists $did_static{$func}) {
			$_ =~ s/\b\Q$func\E\b(\s*)\(/$func$1(rfbClientPtr cl, /;
			$did_static{$func} = 1;
		} elsif (/\b\Q$func\E\b\s*\(/) {
			$_ =~ s/\b\Q$func\E\b(\s*)\(/$func$1(cl, /;
		}
	}

#	if (/^\s*subsampLevel\s*=\s*cl/) {
#		$_ = "//$_";
#		print "subsampLevel = 0;\n";
#	}

#	$_ =~ s/cl->tightQualityLevel;/cl->tightQualityLevel * 10;/;

	if (/^static\s+(Bool|int|CARD32|PALETTE|char|unsigned|tjhandle)\s+[^()]*;\s*$/) {
		$_ =~ s/^static/static TLS /;
	}

	$_ =~ s/rfbScreen.pfbMemory/cl->scaledScreen->frameBuffer/g;
	$_ =~ s/rfbScreen.paddedWidthInBytes/cl->scaledScreen->paddedWidthInBytes/g;
	$_ =~ s/rfbScreen.bitsPerPixel/cl->scaledScreen->bitsPerPixel/g;
	$_ =~ s/rfbServerFormat/cl->screen->serverFormat/g;

	if (/^(FindBestSolidArea|ExtendSolidArea|static void Pack24|CheckSolidTile)\(cl/) {
		$_ .= "rfbClientPtr cl;\n";
	}
	if (/^(CheckSolidTile##bpp)\(cl/) {
		$_ .= "rfbClientPtr cl; \\\n";
	}
	$_ =~ s/\bublen\b/cl->ublen/;
	$_ =~ s/\bupdateBuf\b/cl->updateBuf/;

	if (/cl->(rfbRectanglesSent|rfbBytesSent)/) {
		$_ = "//$_";
	}
	print;
}

print <<END;

void rfbTightCleanup(rfbScreenInfoPtr screen) {
}

END