[Message Prev][Message Next][Thread Prev][Thread Next][Message Index][Thread Index]

Re: 16 bit patch #5.




Attahed are patch 5 and 6, in case you haven't got them yet.

Regards,
Fredrik Jönsson
diff -u rdesktop-1.0.0/Makefile rdesktop-1.0.0.new/Makefile
--- rdesktop-1.0.0/Makefile	Sat Sep 16 14:51:29 2000
+++ rdesktop-1.0.0.new/Makefile	Thu Oct 19 17:11:52 2000
@@ -5,13 +5,15 @@
 ##############################################
 
 # Uncomment to enable debugging
-# DEBUG = -g -DRDP_DEBUG
+DEBUG = -g #-DRDP_DEBUG
 
 CC     = gcc
-CFLAGS = -O2 -Wall $(DEBUG) -I/usr/X11R6/include
-LIBS   = -L/usr/X11R6/lib -lX11
+CFLAGS = -Wall $(DEBUG) -I/usr/X11R6/include
+LIBS   = -L/usr/X11R6/lib -lX11 -lefence
 
-RDPOBJ = rdesktop.o tcp.o iso.o mcs.o secure.o licence.o rdp.o orders.o bitmap.o cache.o xwin.o
+SRCS = rdesktop.c tcp.c iso.c mcs.c secure.c licence.c rdp.c orders.c bitmap.c cache.c xwin.c
+
+RDPOBJ = $(SRCS:.c=.o)
 CRYPTOBJ = crypto/rc4_enc.o crypto/rc4_skey.o crypto/md5_dgst.o crypto/sha1dgst.o crypto/arith.o
 
 rdesktop: $(RDPOBJ) $(CRYPTOBJ)
@@ -21,10 +23,15 @@
 	cproto -DMAKE_PROTO -o proto.h *.c
 
 clean:
-	rm -f *.o crypto/*.o *~
+	rm -f *.o crypto/*.o *~ dep
 
 .SUFFIXES:
 .SUFFIXES: .c .o
 
 .c.o:
 	$(CC) $(CFLAGS) -o $@ -c $<
+
+dep: $(SRCS)
+	$(CC) $(CFLAGS) -MM $(SRCS) > dep
+	 
+-include dep
diff -u rdesktop-1.0.0/cache.c rdesktop-1.0.0.new/cache.c
--- rdesktop-1.0.0/cache.c	Mon Oct 16 09:37:51 2000
+++ rdesktop-1.0.0.new/cache.c	Thu Oct 19 15:47:12 2000
@@ -152,12 +152,12 @@
 
 
 /* DESKTOP CACHE */
-static uint8 deskcache[0x38400];
+static uint8 deskcache[0x38400*4];
 
 /* Retrieve desktop data from the cache */
-uint8 *cache_get_desktop(uint32 offset, int cx, int cy)
+uint8 *cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel)
 {
-	int length = cx * cy;
+	int length = cx * cy * bytes_per_pixel;
 
 	if ((offset + length) <= sizeof(deskcache))
 	{
@@ -169,12 +169,13 @@
 }
 
 /* Store desktop data in the cache */
-void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, uint8 *data)
+void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 *data)
 {
-	int length = cx * cy;
+	int length = cx * cy * bytes_per_pixel;
 
 	if ((offset + length) <= sizeof(deskcache))
 	{
+	        cx *= bytes_per_pixel;
 		while (cy--)
 		{
 			memcpy(&deskcache[offset], data, cx);
@@ -187,4 +188,3 @@
 		ERROR("put desktop %d:%d\n", offset, length);
 	}
 }
-
diff -u rdesktop-1.0.0/orders.c rdesktop-1.0.0.new/orders.c
--- rdesktop-1.0.0/orders.c	Mon Oct 16 09:37:52 2000
+++ rdesktop-1.0.0.new/orders.c	Thu Oct 19 17:06:52 2000
@@ -634,7 +634,8 @@
 	HBITMAP bitmap;
 	uint16 cache_idx, bufsize;
 	uint8 cache_id, width, height, bpp;
-	uint8 *data;
+	uint8 *data, *d2;
+	int y;
 
 	in_uint8(s, cache_id);
 	in_uint8s(s, 1); /* pad */
@@ -647,8 +648,13 @@
 
 	DEBUG("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n",
 	      width, height, cache_id, cache_idx);
+	d2 = xmalloc(width*height);
+	for(y=0; y < height; y++) {
+	  memcpy(&d2[(height-y-1)*width], &data[y*width], width);
+	}
 
-	bitmap = ui_create_bitmap(width, height, data);
+	bitmap = ui_create_bitmap(width, height, d2);
+	xfree(d2);
 	cache_put_bitmap(cache_id, cache_idx, bitmap);
 }
 
diff -u rdesktop-1.0.0/proto.h rdesktop-1.0.0.new/proto.h
--- rdesktop-1.0.0/proto.h	Mon Oct 16 10:44:47 2000
+++ rdesktop-1.0.0.new/proto.h	Thu Oct 19 18:13:06 2000
@@ -1,32 +1,45 @@
-/* rdesktop.c */
-int main(int argc, char *argv[]);
-void generate_random(uint8 *random);
-void *xmalloc(int size);
-void *xrealloc(void *oldmem, int size);
-void xfree(void *mem);
-void hexdump(unsigned char *p, unsigned int len);
-
-/* tcp.c */
-STREAM tcp_init(int maxlen);
-void tcp_send(STREAM s);
-STREAM tcp_recv(int length);
-BOOL tcp_connect(char *server);
-void tcp_disconnect(void);
-
+/* bitmap.c */
+BOOL bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size);
+/* cache.c */
+HBITMAP cache_get_bitmap(uint8 cache_id, uint16 cache_idx);
+void cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap);
+FONTGLYPH *cache_get_font(uint8 font, uint16 character);
+void cache_put_font(uint8 font, uint16 character, uint16 offset, uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap);
+DATABLOB *cache_get_text(uint8 cache_id);
+void cache_put_text(uint8 cache_id, void *data, int length);
+uint8 *cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel);
+void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 *data);
 /* iso.c */
 STREAM iso_init(int length);
 void iso_send(STREAM s);
 STREAM iso_recv(void);
 BOOL iso_connect(char *server);
 void iso_disconnect(void);
-
+/* licence.c */
+void licence_generate_keys(uint8 *client_key, uint8 *server_key, uint8 *client_rsa);
+void licence_process(STREAM s);
 /* mcs.c */
 STREAM mcs_init(int length);
 void mcs_send(STREAM s);
 STREAM mcs_recv(void);
 BOOL mcs_connect(char *server, STREAM mcs_data);
 void mcs_disconnect(void);
-
+/* orders.c */
+void process_orders(STREAM s);
+void reset_order_state(void);
+/* rdesktop.c */
+int main(int argc, char *argv[]);
+void generate_random(uint8 *random);
+void *xmalloc(int size);
+void *xrealloc(int size);
+void xfree(void *mem);
+void hexdump(unsigned char *p, unsigned int len);
+/* rdp.c */
+void rdp_out_unistr(STREAM s, char *string, int len);
+void rdp_send_input(uint32 time, uint16 message_type, uint16 device_flags, uint16 param1, uint16 param2);
+void rdp_main_loop(void);
+BOOL rdp_connect(char *server, uint32 flags, char *domain, char *password, char *command, char *directory);
+void rdp_disconnect(void);
 /* secure.c */
 void sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt);
 void sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2);
@@ -37,35 +50,12 @@
 STREAM sec_recv(void);
 BOOL sec_connect(char *server);
 void sec_disconnect(void);
-
-/* licence.c */
-void licence_generate_keys(uint8 *client_key, uint8 *server_key, uint8 *client_rsa);
-void licence_process(STREAM s);
-
-/* rdp.c */
-void rdp_out_unistr(STREAM s, char *string, int len);
-void rdp_send_input(uint32 time, uint16 message_type, uint16 device_flags, uint16 param1, uint16 param2);
-void rdp_main_loop(void);
-BOOL rdp_connect(char *server, uint32 flags, char *domain, char *password, char *command, char *directory);
-void rdp_disconnect(void);
-
-/* orders.c */
-void process_orders(STREAM s);
-void reset_order_state(void);
-
-/* bitmap.c */
-BOOL bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size);
-
-/* cache.c */
-HBITMAP cache_get_bitmap(uint8 cache_id, uint16 cache_idx);
-void cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap);
-FONTGLYPH *cache_get_font(uint8 font, uint16 character);
-void cache_put_font(uint8 font, uint16 character, uint16 offset, uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap);
-DATABLOB *cache_get_text(uint8 cache_id);
-void cache_put_text(uint8 cache_id, void *data, int length);
-uint8 *cache_get_desktop(uint32 offset, int cx, int cy);
-void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, uint8 *data);
-
+/* tcp.c */
+STREAM tcp_init(int maxlen);
+void tcp_send(STREAM s);
+STREAM tcp_recv(int length);
+BOOL tcp_connect(char *server);
+void tcp_disconnect(void);
 /* xwin.c */
 BOOL ui_create_window(char *title);
 void ui_destroy_window(void);
diff -u rdesktop-1.0.0/rdesktop.c rdesktop-1.0.0.new/rdesktop.c
--- rdesktop-1.0.0/rdesktop.c	Mon Oct 16 10:44:47 2000
+++ rdesktop-1.0.0.new/rdesktop.c	Thu Oct 19 17:34:54 2000
@@ -27,7 +27,7 @@
 #include <sys/times.h>	/* times */
 #include "rdesktop.h"
 
-char username[16];
+char username[64];
 char hostname[16];
 int width = 800;
 int height = 600;
diff -u rdesktop-1.0.0/rdesktop.h rdesktop-1.0.0.new/rdesktop.h
--- rdesktop-1.0.0/rdesktop.h	Mon Oct 16 10:44:48 2000
+++ rdesktop-1.0.0.new/rdesktop.h	Thu Oct 19 17:25:12 2000
@@ -23,6 +23,8 @@
 
 #define VERSION "1.0.0"
 
+#define BYTESPERPIXEL 2
+
 #define STATUS(args...) fprintf(stderr, args);
 #define ERROR(args...)  fprintf(stderr, "ERROR: "args);
 #define WARN(args...)   fprintf(stderr, "WARNING: "args);
diff -u rdesktop-1.0.0/xwin.c rdesktop-1.0.0.new/xwin.c
--- rdesktop-1.0.0/xwin.c	Mon Oct 16 10:44:48 2000
+++ rdesktop-1.0.0.new/xwin.c	Thu Oct 19 18:11:39 2000
@@ -19,6 +19,7 @@
 */
 
 #include <X11/Xlib.h>
+#include <X11/Xutil.h>
 #include <time.h>
 #include "rdesktop.h"
 
@@ -32,12 +33,13 @@
 static Visual *visual;
 static XIM IM;
 
+static uint32 *colmap;
+
 BOOL ui_create_window(char *title)
 {
-	Screen *screen;
 	XSetWindowAttributes attribs;
+	XClassHint *classhints;
 	unsigned long input_mask;
-	int i;
 
 	display = XOpenDisplay(NULL);
 	if (display == NULL)
@@ -46,28 +48,28 @@
 		return False;
 	}
 
-	/* Check the screen supports 8-bit depth. */
-	screen = DefaultScreenOfDisplay(display);
-	for (i = 0; i < screen->ndepths; i++)
-		if (screen->depths[i].depth == 8)
-			break;
-
-	if (i >= screen->ndepths)
-	{
-		ERROR("8-bit depth required (in this version).\n");
-		XCloseDisplay(display);
-		return False;
-	}
-
 	visual = DefaultVisual(display, DefaultScreen(display));
 
+	/* visual->class == TrueColor ? */
+
 	attribs.background_pixel = BlackPixel(display, DefaultScreen(display));
 	attribs.backing_store = Always;
 	wnd = XCreateWindow(display, DefaultRootWindow(display),
-			0, 0, width, height, 0, 8, InputOutput, visual,
+			0, 0, width, height, 0, CopyFromParent,
+			InputOutput, CopyFromParent,
 			CWBackingStore | CWBackPixel, &attribs);
 
 	XStoreName(display, wnd, title);
+	 
+        classhints = XAllocClassHint();
+        if (classhints != NULL)
+        {
+            classhints->res_name = "rdesktop";
+            classhints->res_class = "rdesktop";
+            XSetClassHint(display,wnd,classhints);
+            XFree(classhints);
+        }
+ 
 	XMapWindow(display, wnd);
 
 	input_mask  = KeyPressMask | KeyReleaseMask;
@@ -99,6 +101,18 @@
 
 	switch (key)
 	{
+		case 0x6a: /* insert */
+			return 0x54; /* XXX Wrong code */
+		case 0x6b: /* delete */
+			return 0x53;
+		case 0x61: /* home */
+			return 0x47;
+		case 0x67: /* end */
+			return 0x4f;
+		case 0x63: /* page up */
+			return 0x49;
+		case 0x69: /* page down */
+			return 0x51;
 		case 0x62: /* left arrow */
 			return 0x48;
 		case 0x64: /* up arrow */
@@ -139,7 +153,7 @@
 	if (display == NULL)
 		return;
 
-	while (XCheckWindowEvent(display, wnd, 0xffffffff, &event))
+	while (XCheckWindowEvent(display, wnd, KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask|PointerMotionHintMask|Button1MotionMask|Button2MotionMask|Button3MotionMask|Button4MotionMask|Button5MotionMask|ButtonMotionMask, &event))
 	{
 		ev_time = time(NULL);
 
@@ -201,18 +215,59 @@
 	XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);
 }
 
+#define Ctrans(col) (colmap[col])
+
+static uint8 *
+translate(int width, int height, uint8 *data)
+{
+  uint32 i;
+  uint8 *d2 = xmalloc(width*height*BYTESPERPIXEL);
+  uint8 *d3 = d2;
+
+  if(ImageByteOrder(display) == MSBFirst) {
+    for(i=width*height; i; i--) {
+      uint32 pix = Ctrans(*data++);
+#if BYTESPERPIXEL > 1
+#if BYTESPERPIXEL > 2
+      *d3++ = pix>>24;
+      *d3++ = pix>>16;
+#endif
+      *d3++ = pix>>8;
+#endif
+      *d3++ = pix;
+    }
+  }else {
+    for(i=width*height; i; i--) {
+      uint32 pix = Ctrans(*data++);
+      *d3++ = pix;
+#if BYTESPERPIXEL > 1
+      *d3++ = pix>>8;
+#if BYTESPERPIXEL > 2
+      *d3++ = pix>>16;
+      *d3++ = pix>>24;
+#endif
+#endif
+    }
+  }
+  return d2;
+}
+
 HBITMAP ui_create_bitmap(int width, int height, uint8 *data)
 {
 	XImage *image;
 	Pixmap bitmap;
+	uint8 *d2;
+
+	d2 = translate(width, height, data);
 
-	bitmap = XCreatePixmap(display, wnd, width, height, 8);
+	bitmap = XCreatePixmap(display, wnd, width, height, DefaultDepth(display, DefaultScreen(display)));
+
+	image = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, d2, width, height, BitmapPad(display), 0);
 
-	image = XCreateImage(display, visual, 8, ZPixmap, 0,
-				data, width, height, 8, width);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
 	XFree(image);
+	xfree(d2);
 	
 	return (HBITMAP)bitmap;
 }
@@ -221,12 +276,15 @@
 			int width, int height, uint8 *data)
 {
 	XImage *image;
+	uint8 *d2 = translate(width, height, data);
 
-	image = XCreateImage(display, visual, 8, ZPixmap, 0,
-				data, width, height, 8, width);
+	image = XCreateImage(display, visual, DefaultDepth(display,
+	                      DefaultScreen(display)), ZPixmap, 0, d2, width,
+	                      height, BitmapPad(display), 0);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
-	XFree(image); 
+	XFree(image);
+	xfree(d2);
 }
 
 void ui_destroy_bitmap(HBITMAP bmp)
@@ -238,16 +296,13 @@
 {
 	XImage *image;
 	Pixmap bitmap;
-	int scanline;
 	GC gc;
 
-	scanline = (width + 7) / 8;
-
 	bitmap = XCreatePixmap(display, wnd, width, height, 1);
 	gc = XCreateGC(display, bitmap, 0, NULL);
 
 	image = XCreateImage(display, visual, 1, ZPixmap, 0,
-				data, width, height, 8, scanline);
+				data, width, height, 8, 0);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
 	XFree(image);
@@ -263,39 +318,35 @@
 
 HCOLOURMAP ui_create_colourmap(COLOURMAP *colours)
 {
-	COLOURENTRY *entry;
-	XColor *xcolours, *xentry;
-	Colormap map;
-	int i, ncolours = colours->ncolours;
-
-	xcolours = xmalloc(sizeof(XColor) * ncolours);
-	for (i = 0; i < ncolours; i++)
-	{
-		entry = &colours->colours[i];
-		xentry = &xcolours[i];
-
-		xentry->pixel = i;
-		xentry->red = entry->red << 8;
-		xentry->blue = entry->blue << 8;
-		xentry->green = entry->green << 8;
-		xentry->flags = DoRed | DoBlue | DoGreen;
-	}
-
-	map = XCreateColormap(display, wnd, visual, AllocAll);
-	XStoreColors(display, map, xcolours, ncolours);
-
-	xfree(xcolours);
-	return (HCOLOURMAP)map;
+  COLOURENTRY *entry;
+  int i, ncolours = colours->ncolours;
+  uint32 *nc = xmalloc(sizeof(*colmap) * ncolours);
+
+  for (i = 0; i < ncolours; i++) {
+    XColor xc;
+    entry = &colours->colours[i];
+
+    xc.red = entry->red << 8;
+    xc.green = entry->green << 8;
+    xc.blue = entry->blue << 8;
+
+    XAllocColor(display, DefaultColormap(display, DefaultScreen(display)), &xc);
+    /* XXX Check return value */
+    nc[i] = xc.pixel;
+  }
+  return nc;
 }
 
 void ui_destroy_colourmap(HCOLOURMAP map)
 {
-	XFreeColormap(display, (Colormap)map);
+  xfree(map);
 }
 
 void ui_set_colourmap(HCOLOURMAP map)
 {
-	XSetWindowColormap(display, wnd, (Colormap)map);
+  /* XXX, change values of all pixels on the screen if the new colmap
+   * doesn't have the same values as the old one? */
+  colmap = map;
 }
 
 void ui_set_clip(int x, int y, int cx, int cy)
@@ -369,15 +420,15 @@
 	switch (brush->style)
 	{
 		case 0: /* Solid */
-			XSetForeground(dpy, gc, fgcolour);
+			XSetForeground(dpy, gc, Ctrans(fgcolour));
 			XFillRectangle(dpy, wnd, gc, x, y, cx, cy);
 			break;
 
 		case 3: /* Pattern */
 			fill = (Pixmap)ui_create_glyph(8, 8, brush->pattern);
 
-			XSetForeground(dpy, gc, fgcolour);
-			XSetBackground(dpy, gc, bgcolour);
+			XSetForeground(dpy, gc, Ctrans(fgcolour));
+			XSetBackground(dpy, gc, Ctrans(bgcolour));
 			XSetFillStyle(dpy, gc, FillOpaqueStippled);
 			XSetStipple(dpy, gc, fill);
 
@@ -425,19 +476,22 @@
 		case 0x69: /* PDSxxn */
 			ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
 			ui_patblt(ROP2_NXOR, x, y, cx, cy,
-					brush, bgcolour, fgcolour);
+					brush, bgcolour,
+					       fgcolour);
 			break;
 
 		case 0xb8: /* PSDPxax */
 			ui_patblt(ROP2_XOR, x, y, cx, cy,
-					brush, bgcolour, fgcolour);
+					brush, bgcolour,
+					       fgcolour);
 			ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
 			ui_patblt(ROP2_XOR, x, y, cx, cy,
-					brush, bgcolour, fgcolour);
+					brush, bgcolour,
+					       fgcolour);
 			break;
 
 		default:
-			NOTIMP("triblt 0x%x\n", opcode);
+			NOTIMP("triblt 1x%x\n", opcode);
 			ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
 	}
 }
@@ -448,7 +502,7 @@
 {
 	xwin_set_function(opcode);
 
-	XSetForeground(display, gc, pen->colour);
+	XSetForeground(display, gc, Ctrans(pen->colour));
 	XDrawLine(display, wnd, gc, startx, starty, endx, endy);
 }
 
@@ -458,7 +512,7 @@
 {
 	xwin_set_function(ROP2_COPY);
 
-	XSetForeground(display, gc, colour);
+	XSetForeground(display, gc, Ctrans(colour));
 	XFillRectangle(display, wnd, gc, x, y, cx, cy);
 }
 
@@ -470,7 +524,7 @@
 
 	xwin_set_function(ROP2_COPY);
 
-	XSetForeground(display, gc, fgcolour);
+	XSetForeground(display, gc, Ctrans(fgcolour));
 
 	switch (mixmode)
 	{
@@ -484,7 +538,7 @@
 			break;
 
 		case MIX_OPAQUE:
-			XSetBackground(display, gc, bgcolour);
+			XSetBackground(display, gc, Ctrans(bgcolour));
 			XCopyPlane(display, pixmap, wnd, gc,
 					srcx, srcy, cx, cy, x, y, 1);
 			break;
@@ -537,10 +591,12 @@
 {
 	XImage *image;
 
-	image = XGetImage(display, wnd, x, y, cx, cy, 0xffffffff, ZPixmap);
-	cache_put_desktop(offset, cx, cy, image->bytes_per_line, image->data);
-	XFree(image->data);
-	XFree(image);
+	offset *= BYTESPERPIXEL;
+
+
+	image = XGetImage(display, wnd, x, y, cx, cy, AllPlanes, ZPixmap);
+	cache_put_desktop(offset, cx, cy, image->bytes_per_line, image->bytes_per_line/cx, image->data);
+	XDestroyImage(image);
 }
 
 void ui_desktop_restore(uint32 offset, int x, int y, int cx, int cy)
@@ -548,12 +604,15 @@
 	XImage *image;
 	uint8 *data;
 
-	data = cache_get_desktop(offset, cx, cy);
+	offset *= BYTESPERPIXEL;
+
+	data = cache_get_desktop(offset, cx, cy, BYTESPERPIXEL);
 	if (data == NULL)
 		return;
 
-	image = XCreateImage(display, visual, 8, ZPixmap, 0,
-				data, cx, cy, 32, cx);
+	image = XCreateImage(display, visual,
+	                     DefaultDepth(display, DefaultScreen(display)),
+			     ZPixmap, 0, data, cx, cy, BitmapPad(display), cx*BYTESPERPIXEL);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
 	XFree(image);
diff -u rdesktop-1.0.0/cache.c rdesktop-1.0.0.new/cache.c
--- rdesktop-1.0.0/cache.c	Mon Oct 16 09:37:51 2000
+++ rdesktop-1.0.0.new/cache.c	Thu Oct 19 15:47:12 2000
@@ -152,12 +152,12 @@
 
 
 /* DESKTOP CACHE */
-static uint8 deskcache[0x38400];
+static uint8 deskcache[0x38400*4];
 
 /* Retrieve desktop data from the cache */
-uint8 *cache_get_desktop(uint32 offset, int cx, int cy)
+uint8 *cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel)
 {
-	int length = cx * cy;
+	int length = cx * cy * bytes_per_pixel;
 
 	if ((offset + length) <= sizeof(deskcache))
 	{
@@ -169,12 +169,13 @@
 }
 
 /* Store desktop data in the cache */
-void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, uint8 *data)
+void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 *data)
 {
-	int length = cx * cy;
+	int length = cx * cy * bytes_per_pixel;
 
 	if ((offset + length) <= sizeof(deskcache))
 	{
+	        cx *= bytes_per_pixel;
 		while (cy--)
 		{
 			memcpy(&deskcache[offset], data, cx);
@@ -187,4 +188,3 @@
 		ERROR("put desktop %d:%d\n", offset, length);
 	}
 }
-
diff -u rdesktop-1.0.0/orders.c rdesktop-1.0.0.new/orders.c
--- rdesktop-1.0.0/orders.c	Mon Oct 16 09:37:52 2000
+++ rdesktop-1.0.0.new/orders.c	Thu Oct 19 18:28:52 2000
@@ -634,7 +634,8 @@
 	HBITMAP bitmap;
 	uint16 cache_idx, bufsize;
 	uint8 cache_id, width, height, bpp;
-	uint8 *data;
+	uint8 *data, *d2;
+	int y;
 
 	in_uint8(s, cache_id);
 	in_uint8s(s, 1); /* pad */
@@ -647,8 +648,13 @@
 
 	DEBUG("RAW_BMPCACHE(cx=%d,cy=%d,id=%d,idx=%d)\n",
 	      width, height, cache_id, cache_idx);
+	d2 = xmalloc(width*height);
+	for(y=0; y < height; y++) {
+	  memcpy(&d2[(height-y-1)*width], &data[y*width], width);
+	}
 
-	bitmap = ui_create_bitmap(width, height, data);
+	bitmap = ui_create_bitmap(width, height, d2);
+	xfree(d2);
 	cache_put_bitmap(cache_id, cache_idx, bitmap);
 }
 
diff -u rdesktop-1.0.0/proto.h rdesktop-1.0.0.new/proto.h
--- rdesktop-1.0.0/proto.h	Mon Oct 16 10:44:47 2000
+++ rdesktop-1.0.0.new/proto.h	Thu Oct 19 19:56:32 2000
@@ -1,32 +1,45 @@
-/* rdesktop.c */
-int main(int argc, char *argv[]);
-void generate_random(uint8 *random);
-void *xmalloc(int size);
-void *xrealloc(void *oldmem, int size);
-void xfree(void *mem);
-void hexdump(unsigned char *p, unsigned int len);
-
-/* tcp.c */
-STREAM tcp_init(int maxlen);
-void tcp_send(STREAM s);
-STREAM tcp_recv(int length);
-BOOL tcp_connect(char *server);
-void tcp_disconnect(void);
-
+/* bitmap.c */
+BOOL bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size);
+/* cache.c */
+HBITMAP cache_get_bitmap(uint8 cache_id, uint16 cache_idx);
+void cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap);
+FONTGLYPH *cache_get_font(uint8 font, uint16 character);
+void cache_put_font(uint8 font, uint16 character, uint16 offset, uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap);
+DATABLOB *cache_get_text(uint8 cache_id);
+void cache_put_text(uint8 cache_id, void *data, int length);
+uint8 *cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel);
+void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, uint8 *data);
 /* iso.c */
 STREAM iso_init(int length);
 void iso_send(STREAM s);
 STREAM iso_recv(void);
 BOOL iso_connect(char *server);
 void iso_disconnect(void);
-
+/* licence.c */
+void licence_generate_keys(uint8 *client_key, uint8 *server_key, uint8 *client_rsa);
+void licence_process(STREAM s);
 /* mcs.c */
 STREAM mcs_init(int length);
 void mcs_send(STREAM s);
 STREAM mcs_recv(void);
 BOOL mcs_connect(char *server, STREAM mcs_data);
 void mcs_disconnect(void);
-
+/* orders.c */
+void process_orders(STREAM s);
+void reset_order_state(void);
+/* rdesktop.c */
+int main(int argc, char *argv[]);
+void generate_random(uint8 *random);
+void *xmalloc(int size);
+void *xrealloc(void *oldmem, int size);
+void xfree(void *mem);
+void hexdump(unsigned char *p, unsigned int len);
+/* rdp.c */
+void rdp_out_unistr(STREAM s, char *string, int len);
+void rdp_send_input(uint32 time, uint16 message_type, uint16 device_flags, uint16 param1, uint16 param2);
+void rdp_main_loop(void);
+BOOL rdp_connect(char *server, uint32 flags, char *domain, char *password, char *command, char *directory);
+void rdp_disconnect(void);
 /* secure.c */
 void sec_hash_48(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2, uint8 salt);
 void sec_hash_16(uint8 *out, uint8 *in, uint8 *salt1, uint8 *salt2);
@@ -37,35 +50,12 @@
 STREAM sec_recv(void);
 BOOL sec_connect(char *server);
 void sec_disconnect(void);
-
-/* licence.c */
-void licence_generate_keys(uint8 *client_key, uint8 *server_key, uint8 *client_rsa);
-void licence_process(STREAM s);
-
-/* rdp.c */
-void rdp_out_unistr(STREAM s, char *string, int len);
-void rdp_send_input(uint32 time, uint16 message_type, uint16 device_flags, uint16 param1, uint16 param2);
-void rdp_main_loop(void);
-BOOL rdp_connect(char *server, uint32 flags, char *domain, char *password, char *command, char *directory);
-void rdp_disconnect(void);
-
-/* orders.c */
-void process_orders(STREAM s);
-void reset_order_state(void);
-
-/* bitmap.c */
-BOOL bitmap_decompress(unsigned char *output, int width, int height, unsigned char *input, int size);
-
-/* cache.c */
-HBITMAP cache_get_bitmap(uint8 cache_id, uint16 cache_idx);
-void cache_put_bitmap(uint8 cache_id, uint16 cache_idx, HBITMAP bitmap);
-FONTGLYPH *cache_get_font(uint8 font, uint16 character);
-void cache_put_font(uint8 font, uint16 character, uint16 offset, uint16 baseline, uint16 width, uint16 height, HGLYPH pixmap);
-DATABLOB *cache_get_text(uint8 cache_id);
-void cache_put_text(uint8 cache_id, void *data, int length);
-uint8 *cache_get_desktop(uint32 offset, int cx, int cy);
-void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, uint8 *data);
-
+/* tcp.c */
+STREAM tcp_init(int maxlen);
+void tcp_send(STREAM s);
+STREAM tcp_recv(int length);
+BOOL tcp_connect(char *server);
+void tcp_disconnect(void);
 /* xwin.c */
 BOOL ui_create_window(char *title);
 void ui_destroy_window(void);
diff -u rdesktop-1.0.0/rdesktop.c rdesktop-1.0.0.new/rdesktop.c
--- rdesktop-1.0.0/rdesktop.c	Mon Oct 16 10:44:47 2000
+++ rdesktop-1.0.0.new/rdesktop.c	Thu Oct 19 19:55:55 2000
@@ -27,7 +27,7 @@
 #include <sys/times.h>	/* times */
 #include "rdesktop.h"
 
-char username[16];
+char username[64];
 char hostname[16];
 int width = 800;
 int height = 600;
@@ -207,6 +207,43 @@
 	r[7] = st.st_ctime;
 }
 
+#ifdef DEBUG_LEAK
+
+/* malloc; exit if out of memory */
+void *xmalloc2(int l, char *s, int size)
+{
+	void *mem = malloc(size);
+	if (mem == NULL)
+	{
+		ERROR("xmalloc %d\n", size);
+		exit(1);
+	}
+	printf("A:%i:%s:%08X:%04i\n", l, s, mem, size);
+	return mem;
+}
+
+/* realloc; exit if out of memory */
+void *xrealloc2(int l, char *s, void *oldmem, int size)
+{
+	void *mem = realloc(oldmem, size);
+	if (mem == NULL)
+	{
+		ERROR("xrealloc %d\n", size);
+		exit(1);
+	}
+	printf("R:%i:%s:%08X:%08X:%04i\n", l, s, oldmem, mem, size);
+	return mem;
+}
+
+/* free */
+void xfree2(int l, char *s, void *mem)
+{
+	printf("F:%i:%s:%08X\n", l, s, mem);
+	free(mem);
+}
+
+#else
+
 /* malloc; exit if out of memory */
 void *xmalloc(int size)
 {
@@ -236,6 +273,8 @@
 {
 	free(mem);
 }
+
+#endif
 
 /* Produce a hex dump */
 void hexdump(unsigned char *p, unsigned int len)
diff -u rdesktop-1.0.0/rdesktop.h rdesktop-1.0.0.new/rdesktop.h
--- rdesktop-1.0.0/rdesktop.h	Mon Oct 16 10:44:48 2000
+++ rdesktop-1.0.0.new/rdesktop.h	Thu Oct 19 19:56:19 2000
@@ -23,12 +23,15 @@
 
 #define VERSION "1.0.0"
 
+#define BYTESPERPIXEL 2
+
 #define STATUS(args...) fprintf(stderr, args);
 #define ERROR(args...)  fprintf(stderr, "ERROR: "args);
 #define WARN(args...)   fprintf(stderr, "WARNING: "args);
 #define NOTIMP(args...) fprintf(stderr, "NOTIMP: "args);
 
-#ifdef RDP_DEBUG
+#if defined(DEBUG) || defined(RDP_DEBUG) 
+#undef DEBUG
 #define DEBUG(args...)  fprintf(stderr, args);
 #else
 #define DEBUG(args...)
@@ -40,4 +43,10 @@
 
 #ifndef MAKE_PROTO
 #include "proto.h"
+#endif
+
+#ifdef DEBUG_LEAK
+#define xmalloc(size) xmalloc2(__LINE__, ##__FILE__, size)
+#define xrealloc(mem, size) xrealloc2(__LINE__, ##__FILE__, mem, size)
+#define xfree(mem) xfree2(__LINE__, ##__FILE__, mem)
 #endif
diff -u rdesktop-1.0.0/rdp.c rdesktop-1.0.0.new/rdp.c
--- rdesktop-1.0.0/rdp.c	Mon Oct 16 10:44:48 2000
+++ rdesktop-1.0.0.new/rdp.c	Thu Oct 19 19:48:55 2000
@@ -501,8 +501,15 @@
 
 		if (!compress)
 		{
-			in_uint8p(s, data, bufsize);
-			ui_paint_bitmap(left, top, cx, cy, width, height, data);
+		        int y;
+		        rawdata = xmalloc(width * height);
+			for(y=0; y<height; y++) {
+			  in_uint8a(s, &rawdata[(height-y-1)*width], width);
+			}
+
+			ui_paint_bitmap(left, top, cx, cy, width, height,
+			                rawdata);
+		        xfree(rawdata);
 			return;
 		}
 
diff -u rdesktop-1.0.0/xwin.c rdesktop-1.0.0.new/xwin.c
--- rdesktop-1.0.0/xwin.c	Mon Oct 16 10:44:48 2000
+++ rdesktop-1.0.0.new/xwin.c	Thu Oct 19 18:11:39 2000
@@ -19,6 +19,7 @@
 */
 
 #include <X11/Xlib.h>
+#include <X11/Xutil.h>
 #include <time.h>
 #include "rdesktop.h"
 
@@ -32,12 +33,13 @@
 static Visual *visual;
 static XIM IM;
 
+static uint32 *colmap;
+
 BOOL ui_create_window(char *title)
 {
-	Screen *screen;
 	XSetWindowAttributes attribs;
+	XClassHint *classhints;
 	unsigned long input_mask;
-	int i;
 
 	display = XOpenDisplay(NULL);
 	if (display == NULL)
@@ -46,28 +48,28 @@
 		return False;
 	}
 
-	/* Check the screen supports 8-bit depth. */
-	screen = DefaultScreenOfDisplay(display);
-	for (i = 0; i < screen->ndepths; i++)
-		if (screen->depths[i].depth == 8)
-			break;
-
-	if (i >= screen->ndepths)
-	{
-		ERROR("8-bit depth required (in this version).\n");
-		XCloseDisplay(display);
-		return False;
-	}
-
 	visual = DefaultVisual(display, DefaultScreen(display));
 
+	/* visual->class == TrueColor ? */
+
 	attribs.background_pixel = BlackPixel(display, DefaultScreen(display));
 	attribs.backing_store = Always;
 	wnd = XCreateWindow(display, DefaultRootWindow(display),
-			0, 0, width, height, 0, 8, InputOutput, visual,
+			0, 0, width, height, 0, CopyFromParent,
+			InputOutput, CopyFromParent,
 			CWBackingStore | CWBackPixel, &attribs);
 
 	XStoreName(display, wnd, title);
+	 
+        classhints = XAllocClassHint();
+        if (classhints != NULL)
+        {
+            classhints->res_name = "rdesktop";
+            classhints->res_class = "rdesktop";
+            XSetClassHint(display,wnd,classhints);
+            XFree(classhints);
+        }
+ 
 	XMapWindow(display, wnd);
 
 	input_mask  = KeyPressMask | KeyReleaseMask;
@@ -99,6 +101,18 @@
 
 	switch (key)
 	{
+		case 0x6a: /* insert */
+			return 0x54; /* XXX Wrong code */
+		case 0x6b: /* delete */
+			return 0x53;
+		case 0x61: /* home */
+			return 0x47;
+		case 0x67: /* end */
+			return 0x4f;
+		case 0x63: /* page up */
+			return 0x49;
+		case 0x69: /* page down */
+			return 0x51;
 		case 0x62: /* left arrow */
 			return 0x48;
 		case 0x64: /* up arrow */
@@ -139,7 +153,7 @@
 	if (display == NULL)
 		return;
 
-	while (XCheckWindowEvent(display, wnd, 0xffffffff, &event))
+	while (XCheckWindowEvent(display, wnd, KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask|PointerMotionHintMask|Button1MotionMask|Button2MotionMask|Button3MotionMask|Button4MotionMask|Button5MotionMask|ButtonMotionMask, &event))
 	{
 		ev_time = time(NULL);
 
@@ -201,18 +215,59 @@
 	XWarpPointer(display, wnd, wnd, 0, 0, 0, 0, x, y);
 }
 
+#define Ctrans(col) (colmap[col])
+
+static uint8 *
+translate(int width, int height, uint8 *data)
+{
+  uint32 i;
+  uint8 *d2 = xmalloc(width*height*BYTESPERPIXEL);
+  uint8 *d3 = d2;
+
+  if(ImageByteOrder(display) == MSBFirst) {
+    for(i=width*height; i; i--) {
+      uint32 pix = Ctrans(*data++);
+#if BYTESPERPIXEL > 1
+#if BYTESPERPIXEL > 2
+      *d3++ = pix>>24;
+      *d3++ = pix>>16;
+#endif
+      *d3++ = pix>>8;
+#endif
+      *d3++ = pix;
+    }
+  }else {
+    for(i=width*height; i; i--) {
+      uint32 pix = Ctrans(*data++);
+      *d3++ = pix;
+#if BYTESPERPIXEL > 1
+      *d3++ = pix>>8;
+#if BYTESPERPIXEL > 2
+      *d3++ = pix>>16;
+      *d3++ = pix>>24;
+#endif
+#endif
+    }
+  }
+  return d2;
+}
+
 HBITMAP ui_create_bitmap(int width, int height, uint8 *data)
 {
 	XImage *image;
 	Pixmap bitmap;
+	uint8 *d2;
+
+	d2 = translate(width, height, data);
 
-	bitmap = XCreatePixmap(display, wnd, width, height, 8);
+	bitmap = XCreatePixmap(display, wnd, width, height, DefaultDepth(display, DefaultScreen(display)));
+
+	image = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, d2, width, height, BitmapPad(display), 0);
 
-	image = XCreateImage(display, visual, 8, ZPixmap, 0,
-				data, width, height, 8, width);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
 	XFree(image);
+	xfree(d2);
 	
 	return (HBITMAP)bitmap;
 }
@@ -221,12 +276,15 @@
 			int width, int height, uint8 *data)
 {
 	XImage *image;
+	uint8 *d2 = translate(width, height, data);
 
-	image = XCreateImage(display, visual, 8, ZPixmap, 0,
-				data, width, height, 8, width);
+	image = XCreateImage(display, visual, DefaultDepth(display,
+	                      DefaultScreen(display)), ZPixmap, 0, d2, width,
+	                      height, BitmapPad(display), 0);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
-	XFree(image); 
+	XFree(image);
+	xfree(d2);
 }
 
 void ui_destroy_bitmap(HBITMAP bmp)
@@ -238,16 +296,13 @@
 {
 	XImage *image;
 	Pixmap bitmap;
-	int scanline;
 	GC gc;
 
-	scanline = (width + 7) / 8;
-
 	bitmap = XCreatePixmap(display, wnd, width, height, 1);
 	gc = XCreateGC(display, bitmap, 0, NULL);
 
 	image = XCreateImage(display, visual, 1, ZPixmap, 0,
-				data, width, height, 8, scanline);
+				data, width, height, 8, 0);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, bitmap, gc, image, 0, 0, 0, 0, width, height);
 	XFree(image);
@@ -263,39 +318,35 @@
 
 HCOLOURMAP ui_create_colourmap(COLOURMAP *colours)
 {
-	COLOURENTRY *entry;
-	XColor *xcolours, *xentry;
-	Colormap map;
-	int i, ncolours = colours->ncolours;
-
-	xcolours = xmalloc(sizeof(XColor) * ncolours);
-	for (i = 0; i < ncolours; i++)
-	{
-		entry = &colours->colours[i];
-		xentry = &xcolours[i];
-
-		xentry->pixel = i;
-		xentry->red = entry->red << 8;
-		xentry->blue = entry->blue << 8;
-		xentry->green = entry->green << 8;
-		xentry->flags = DoRed | DoBlue | DoGreen;
-	}
-
-	map = XCreateColormap(display, wnd, visual, AllocAll);
-	XStoreColors(display, map, xcolours, ncolours);
-
-	xfree(xcolours);
-	return (HCOLOURMAP)map;
+  COLOURENTRY *entry;
+  int i, ncolours = colours->ncolours;
+  uint32 *nc = xmalloc(sizeof(*colmap) * ncolours);
+
+  for (i = 0; i < ncolours; i++) {
+    XColor xc;
+    entry = &colours->colours[i];
+
+    xc.red = entry->red << 8;
+    xc.green = entry->green << 8;
+    xc.blue = entry->blue << 8;
+
+    XAllocColor(display, DefaultColormap(display, DefaultScreen(display)), &xc);
+    /* XXX Check return value */
+    nc[i] = xc.pixel;
+  }
+  return nc;
 }
 
 void ui_destroy_colourmap(HCOLOURMAP map)
 {
-	XFreeColormap(display, (Colormap)map);
+  xfree(map);
 }
 
 void ui_set_colourmap(HCOLOURMAP map)
 {
-	XSetWindowColormap(display, wnd, (Colormap)map);
+  /* XXX, change values of all pixels on the screen if the new colmap
+   * doesn't have the same values as the old one? */
+  colmap = map;
 }
 
 void ui_set_clip(int x, int y, int cx, int cy)
@@ -369,15 +420,15 @@
 	switch (brush->style)
 	{
 		case 0: /* Solid */
-			XSetForeground(dpy, gc, fgcolour);
+			XSetForeground(dpy, gc, Ctrans(fgcolour));
 			XFillRectangle(dpy, wnd, gc, x, y, cx, cy);
 			break;
 
 		case 3: /* Pattern */
 			fill = (Pixmap)ui_create_glyph(8, 8, brush->pattern);
 
-			XSetForeground(dpy, gc, fgcolour);
-			XSetBackground(dpy, gc, bgcolour);
+			XSetForeground(dpy, gc, Ctrans(fgcolour));
+			XSetBackground(dpy, gc, Ctrans(bgcolour));
 			XSetFillStyle(dpy, gc, FillOpaqueStippled);
 			XSetStipple(dpy, gc, fill);
 
@@ -425,19 +476,22 @@
 		case 0x69: /* PDSxxn */
 			ui_memblt(ROP2_XOR, x, y, cx, cy, src, srcx, srcy);
 			ui_patblt(ROP2_NXOR, x, y, cx, cy,
-					brush, bgcolour, fgcolour);
+					brush, bgcolour,
+					       fgcolour);
 			break;
 
 		case 0xb8: /* PSDPxax */
 			ui_patblt(ROP2_XOR, x, y, cx, cy,
-					brush, bgcolour, fgcolour);
+					brush, bgcolour,
+					       fgcolour);
 			ui_memblt(ROP2_AND, x, y, cx, cy, src, srcx, srcy);
 			ui_patblt(ROP2_XOR, x, y, cx, cy,
-					brush, bgcolour, fgcolour);
+					brush, bgcolour,
+					       fgcolour);
 			break;
 
 		default:
-			NOTIMP("triblt 0x%x\n", opcode);
+			NOTIMP("triblt 1x%x\n", opcode);
 			ui_memblt(ROP2_COPY, x, y, cx, cy, src, srcx, srcy);
 	}
 }
@@ -448,7 +502,7 @@
 {
 	xwin_set_function(opcode);
 
-	XSetForeground(display, gc, pen->colour);
+	XSetForeground(display, gc, Ctrans(pen->colour));
 	XDrawLine(display, wnd, gc, startx, starty, endx, endy);
 }
 
@@ -458,7 +512,7 @@
 {
 	xwin_set_function(ROP2_COPY);
 
-	XSetForeground(display, gc, colour);
+	XSetForeground(display, gc, Ctrans(colour));
 	XFillRectangle(display, wnd, gc, x, y, cx, cy);
 }
 
@@ -470,7 +524,7 @@
 
 	xwin_set_function(ROP2_COPY);
 
-	XSetForeground(display, gc, fgcolour);
+	XSetForeground(display, gc, Ctrans(fgcolour));
 
 	switch (mixmode)
 	{
@@ -484,7 +538,7 @@
 			break;
 
 		case MIX_OPAQUE:
-			XSetBackground(display, gc, bgcolour);
+			XSetBackground(display, gc, Ctrans(bgcolour));
 			XCopyPlane(display, pixmap, wnd, gc,
 					srcx, srcy, cx, cy, x, y, 1);
 			break;
@@ -537,10 +591,12 @@
 {
 	XImage *image;
 
-	image = XGetImage(display, wnd, x, y, cx, cy, 0xffffffff, ZPixmap);
-	cache_put_desktop(offset, cx, cy, image->bytes_per_line, image->data);
-	XFree(image->data);
-	XFree(image);
+	offset *= BYTESPERPIXEL;
+
+
+	image = XGetImage(display, wnd, x, y, cx, cy, AllPlanes, ZPixmap);
+	cache_put_desktop(offset, cx, cy, image->bytes_per_line, image->bytes_per_line/cx, image->data);
+	XDestroyImage(image);
 }
 
 void ui_desktop_restore(uint32 offset, int x, int y, int cx, int cy)
@@ -548,12 +604,15 @@
 	XImage *image;
 	uint8 *data;
 
-	data = cache_get_desktop(offset, cx, cy);
+	offset *= BYTESPERPIXEL;
+
+	data = cache_get_desktop(offset, cx, cy, BYTESPERPIXEL);
 	if (data == NULL)
 		return;
 
-	image = XCreateImage(display, visual, 8, ZPixmap, 0,
-				data, cx, cy, 32, cx);
+	image = XCreateImage(display, visual,
+	                     DefaultDepth(display, DefaultScreen(display)),
+			     ZPixmap, 0, data, cx, cy, BitmapPad(display), cx*BYTESPERPIXEL);
 	XSetFunction(display, gc, GXcopy);
 	XPutImage(display, wnd, gc, image, 0, 0, x, y, cx, cy);
 	XFree(image);