--- sourceforge.net/trunk/rdesktop/xwin.c 2003/10/15 14:01:32 499 +++ sourceforge.net/trunk/rdesktop/xwin.c 2006/01/24 12:24:40 1040 @@ -1,7 +1,7 @@ /* -*- c-basic-offset: 8 -*- rdesktop: A Remote Desktop Protocol client. User interface services - X Window System - Copyright (C) Matthew Chapman 1999-2002 + Copyright (C) Matthew Chapman 1999-2005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -20,13 +20,19 @@ #include #include +#include +#include #include #include +#include #include "rdesktop.h" #include "xproto.h" extern int g_width; extern int g_height; +extern int g_xpos; +extern int g_ypos; +extern int g_pos; extern BOOL g_sendmotion; extern BOOL g_fullscreen; extern BOOL g_grab_keyboard; @@ -40,8 +46,12 @@ static int g_x_socket; static Screen *g_screen; Window g_wnd; +extern uint32 g_embed_wnd; BOOL g_enable_compose = False; -static GC g_gc; +BOOL g_Unobscured; /* used for screenblt */ +static GC g_gc = NULL; +static GC g_create_bitmap_gc = NULL; +static GC g_create_glyph_gc = NULL; static Visual *g_visual; static int g_depth; static int g_bpp; @@ -49,26 +59,32 @@ static XIC g_IC; static XModifierKeymap *g_mod_map; static Cursor g_current_cursor; +static HCURSOR g_null_cursor = NULL; static Atom g_protocol_atom, g_kill_atom; static BOOL g_focused; static BOOL g_mouse_in_wnd; +static BOOL g_arch_match = False; /* set to True if RGB XServer and little endian */ /* endianness */ static BOOL g_host_be; static BOOL g_xserver_be; +static int g_red_shift_r, g_blue_shift_r, g_green_shift_r; +static int g_red_shift_l, g_blue_shift_l, g_green_shift_l; /* software backing store */ -static BOOL g_ownbackstore; -static Pixmap g_backstore; +extern BOOL g_ownbackstore; +static Pixmap g_backstore = 0; /* Moving in single app mode */ static BOOL g_moving_wnd; static int g_move_x_offset = 0; static int g_move_y_offset = 0; +static BOOL g_using_full_workarea = False; #ifdef WITH_RDPSND extern int g_dsp_fd; extern BOOL g_dsp_busy; +extern BOOL g_rdpsnd; #endif /* MWM decorations */ @@ -76,11 +92,11 @@ #define PROP_MOTIF_WM_HINTS_ELEMENTS 5 typedef struct { - uint32 flags; - uint32 functions; - uint32 decorations; - sint32 inputMode; - uint32 status; + unsigned long flags; + unsigned long functions; + unsigned long decorations; + long inputMode; + unsigned long status; } PropMotifWmHints; @@ -105,12 +121,36 @@ XFillRectangle(g_display, g_ownbackstore ? g_backstore : g_wnd, g_gc, x, y, cx, cy); \ } +#define FILL_POLYGON(p,np)\ +{ \ + XFillPolygon(g_display, g_wnd, g_gc, p, np, Complex, CoordModePrevious); \ + if (g_ownbackstore) \ + XFillPolygon(g_display, g_backstore, g_gc, p, np, Complex, CoordModePrevious); \ +} + +#define DRAW_ELLIPSE(x,y,cx,cy,m)\ +{ \ + switch (m) \ + { \ + case 0: /* Outline */ \ + XDrawArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \ + if (g_ownbackstore) \ + XDrawArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \ + break; \ + case 1: /* Filled */ \ + XFillArc(g_display, g_wnd, g_gc, x, y, cx, cy, 0, 360*64); \ + if (g_ownbackstore) \ + XFillArc(g_display, g_backstore, g_gc, x, y, cx, cy, 0, 360*64); \ + break; \ + } \ +} + /* colour maps */ -BOOL g_owncolmap = False; +extern BOOL g_owncolmap; static Colormap g_xcolmap; static uint32 *g_colmap = NULL; -#define TRANSLATE(col) ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : translate_colour(g_colmap[col]) ) +#define TRANSLATE(col) ( g_server_bpp != 8 ? translate_colour(col) : g_owncolmap ? col : g_colmap[col] ) #define SET_FOREGROUND(col) XSetForeground(g_display, g_gc, TRANSLATE(col)); #define SET_BACKGROUND(col) XSetBackground(g_display, g_gc, TRANSLATE(col)); @@ -158,408 +198,640 @@ (unsigned char *) &motif_hints, PROP_MOTIF_WM_HINTS_ELEMENTS); } -static PixelColour -split_colour15(uint32 colour) -{ - PixelColour rv; - rv.red = (colour & 0x7c00) >> 10; - rv.red = (rv.red * 0xff) / 0x1f; - rv.green = (colour & 0x03e0) >> 5; - rv.green = (rv.green * 0xff) / 0x1f; - rv.blue = (colour & 0x1f); - rv.blue = (rv.blue * 0xff) / 0x1f; - return rv; -} - -static PixelColour -split_colour16(uint32 colour) -{ - PixelColour rv; - rv.red = (colour & 0xf800) >> 11; - rv.red = (rv.red * 0xff) / 0x1f; - rv.green = (colour & 0x07e0) >> 5; - rv.green = (rv.green * 0xff) / 0x3f; - rv.blue = (colour & 0x001f); - rv.blue = (rv.blue * 0xff) / 0x1f; - return rv; -} - -static PixelColour -split_colour24(uint32 colour) -{ - PixelColour rv; - rv.blue = (colour & 0xff0000) >> 16; - rv.green = (colour & 0xff00) >> 8; - rv.red = (colour & 0xff); - return rv; +#define SPLITCOLOUR15(colour, rv) \ +{ \ + rv.red = ((colour >> 7) & 0xf8) | ((colour >> 12) & 0x7); \ + rv.green = ((colour >> 2) & 0xf8) | ((colour >> 8) & 0x7); \ + rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \ } -static uint32 -make_colour16(PixelColour pc) -{ - pc.red = (pc.red * 0x1f) / 0xff; - pc.green = (pc.green * 0x3f) / 0xff; - pc.blue = (pc.blue * 0x1f) / 0xff; - return (pc.red << 11) | (pc.green << 5) | pc.blue; -} +#define SPLITCOLOUR16(colour, rv) \ +{ \ + rv.red = ((colour >> 8) & 0xf8) | ((colour >> 13) & 0x7); \ + rv.green = ((colour >> 3) & 0xfc) | ((colour >> 9) & 0x3); \ + rv.blue = ((colour << 3) & 0xf8) | ((colour >> 2) & 0x7); \ +} \ -static uint32 -make_colour24(PixelColour pc) -{ - if (g_xserver_be) - { - return pc.red | (pc.green << 8) | (pc.blue << 16); - } - else - { - return (pc.red << 16) | (pc.green << 8) | pc.blue; - } +#define SPLITCOLOUR24(colour, rv) \ +{ \ + rv.blue = (colour & 0xff0000) >> 16; \ + rv.green = (colour & 0x00ff00) >> 8; \ + rv.red = (colour & 0x0000ff); \ } -static uint32 -make_colour32(PixelColour pc) -{ - if (g_xserver_be) - { - return pc.red | (pc.green << 8) | (pc.blue << 16); - } - else - { - return (pc.red << 16) | (pc.green << 8) | pc.blue; - } -} +#define MAKECOLOUR(pc) \ + ((pc.red >> g_red_shift_r) << g_red_shift_l) \ + | ((pc.green >> g_green_shift_r) << g_green_shift_l) \ + | ((pc.blue >> g_blue_shift_r) << g_blue_shift_l) \ #define BSWAP16(x) { x = (((x & 0xff) << 8) | (x >> 8)); } -#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | ((x >> 8) & 0xff00)); } +#define BSWAP24(x) { x = (((x & 0xff) << 16) | (x >> 16) | (x & 0xff00)); } #define BSWAP32(x) { x = (((x & 0xff00ff) << 8) | ((x >> 8) & 0xff00ff)); \ x = (x << 16) | (x >> 16); } +#define BOUT16(o, x) { *(o++) = x >> 8; *(o++) = x; } +#define BOUT24(o, x) { *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; } +#define BOUT32(o, x) { *(o++) = x >> 24; *(o++) = x >> 16; *(o++) = x >> 8; *(o++) = x; } +#define LOUT16(o, x) { *(o++) = x; *(o++) = x >> 8; } +#define LOUT24(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; } +#define LOUT32(o, x) { *(o++) = x; *(o++) = x >> 8; *(o++) = x >> 16; *(o++) = x >> 24; } + static uint32 translate_colour(uint32 colour) { + PixelColour pc; switch (g_server_bpp) { case 15: - switch (g_bpp) - { - case 16: - colour = make_colour16(split_colour15(colour)); - break; - case 24: - colour = make_colour24(split_colour15(colour)); - break; - case 32: - colour = make_colour32(split_colour15(colour)); - break; - } + SPLITCOLOUR15(colour, pc); break; case 16: - switch (g_bpp) - { - case 16: - break; - case 24: - colour = make_colour24(split_colour16(colour)); - break; - case 32: - colour = make_colour32(split_colour16(colour)); - break; - } + SPLITCOLOUR16(colour, pc); break; case 24: - switch (g_bpp) - { - case 16: - colour = make_colour16(split_colour24(colour)); - break; - case 24: - break; - case 32: - colour = make_colour32(split_colour24(colour)); - break; - } + SPLITCOLOUR24(colour, pc); break; } - return colour; + return MAKECOLOUR(pc); +} + +/* indent is confused by UNROLL8 */ +/* *INDENT-OFF* */ + +/* repeat and unroll, similar to bitmap.c */ +/* potentialy any of the following translate */ +/* functions can use repeat but just doing */ +/* the most common ones */ + +#define UNROLL8(stm) { stm stm stm stm stm stm stm stm } +/* 2 byte output repeat */ +#define REPEAT2(stm) \ +{ \ + while (out <= end - 8 * 2) \ + UNROLL8(stm) \ + while (out < end) \ + { stm } \ +} +/* 3 byte output repeat */ +#define REPEAT3(stm) \ +{ \ + while (out <= end - 8 * 3) \ + UNROLL8(stm) \ + while (out < end) \ + { stm } \ +} +/* 4 byte output repeat */ +#define REPEAT4(stm) \ +{ \ + while (out <= end - 8 * 4) \ + UNROLL8(stm) \ + while (out < end) \ + { stm } \ } +/* *INDENT-ON* */ static void -translate8to8(uint8 * data, uint8 * out, uint8 * end) +translate8to8(const uint8 * data, uint8 * out, uint8 * end) { while (out < end) *(out++) = (uint8) g_colmap[*(data++)]; } static void -translate8to16(uint8 * data, uint16 * out, uint16 * end) +translate8to16(const uint8 * data, uint8 * out, uint8 * end) { - while (out < end) - *(out++) = (uint16) g_colmap[*(data++)]; + uint16 value; + + if (g_arch_match) + { + /* *INDENT-OFF* */ + REPEAT2 + ( + *((uint16 *) out) = g_colmap[*(data++)]; + out += 2; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) + { + value = (uint16) g_colmap[*(data++)]; + BOUT16(out, value); + } + } + else + { + while (out < end) + { + value = (uint16) g_colmap[*(data++)]; + LOUT16(out, value); + } + } } /* little endian - conversion happens when colourmap is built */ static void -translate8to24(uint8 * data, uint8 * out, uint8 * end) +translate8to24(const uint8 * data, uint8 * out, uint8 * end) { uint32 value; - while (out < end) + if (g_xserver_be) { - value = g_colmap[*(data++)]; - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + while (out < end) + { + value = g_colmap[*(data++)]; + BOUT24(out, value); + } + } + else + { + while (out < end) + { + value = g_colmap[*(data++)]; + LOUT24(out, value); + } } } static void -translate8to32(uint8 * data, uint32 * out, uint32 * end) +translate8to32(const uint8 * data, uint8 * out, uint8 * end) { - while (out < end) - *(out++) = g_colmap[*(data++)]; -} + uint32 value; -/* todo the remaining translate function might need some big endian check ?? */ + if (g_arch_match) + { + /* *INDENT-OFF* */ + REPEAT4 + ( + *((uint32 *) out) = g_colmap[*(data++)]; + out += 4; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) + { + value = g_colmap[*(data++)]; + BOUT32(out, value); + } + } + else + { + while (out < end) + { + value = g_colmap[*(data++)]; + LOUT32(out, value); + } + } +} static void -translate15to16(uint16 * data, uint8 * out, uint8 * end) +translate15to16(const uint16 * data, uint8 * out, uint8 * end) { uint16 pixel; uint16 value; + PixelColour pc; - while (out < end) + if (g_xserver_be) { - pixel = *(data++); - - if (g_host_be) - { - BSWAP16(pixel)} - - value = make_colour16(split_colour15(pixel)); - - if (g_xserver_be) + while (out < end) { - *(out++) = value >> 8; - *(out++) = value; + pixel = *(data++); + if (g_host_be) + { + BSWAP16(pixel); + } + SPLITCOLOUR15(pixel, pc); + value = MAKECOLOUR(pc); + BOUT16(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; + pixel = *(data++); + if (g_host_be) + { + BSWAP16(pixel); + } + SPLITCOLOUR15(pixel, pc); + value = MAKECOLOUR(pc); + LOUT16(out, value); } } } static void -translate15to24(uint16 * data, uint8 * out, uint8 * end) +translate15to24(const uint16 * data, uint8 * out, uint8 * end) { uint32 value; uint16 pixel; + PixelColour pc; - while (out < end) + if (g_arch_match) { - pixel = *(data++); - - if (g_host_be) - { - BSWAP16(pixel)} - - value = make_colour24(split_colour15(pixel)); - if (g_xserver_be) + /* *INDENT-OFF* */ + REPEAT3 + ( + pixel = *(data++); + SPLITCOLOUR15(pixel, pc); + *(out++) = pc.blue; + *(out++) = pc.green; + *(out++) = pc.red; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) { - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + pixel = *(data++); + if (g_host_be) + { + BSWAP16(pixel); + } + SPLITCOLOUR15(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + pixel = *(data++); + if (g_host_be) + { + BSWAP16(pixel); + } + SPLITCOLOUR15(pixel, pc); + value = MAKECOLOUR(pc); + LOUT24(out, value); } } } static void -translate15to32(uint16 * data, uint8 * out, uint8 * end) +translate15to32(const uint16 * data, uint8 * out, uint8 * end) { uint16 pixel; uint32 value; + PixelColour pc; - while (out < end) + if (g_arch_match) { - pixel = *(data++); - - if (g_host_be) - { - BSWAP16(pixel); - } - - value = make_colour32(split_colour15(pixel)); - - if (g_xserver_be) + /* *INDENT-OFF* */ + REPEAT4 + ( + pixel = *(data++); + SPLITCOLOUR15(pixel, pc); + *(out++) = pc.blue; + *(out++) = pc.green; + *(out++) = pc.red; + *(out++) = 0; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) { - *(out++) = value >> 24; - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + pixel = *(data++); + if (g_host_be) + { + BSWAP16(pixel); + } + SPLITCOLOUR15(pixel, pc); + value = MAKECOLOUR(pc); + BOUT32(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; - *(out++) = value >> 24; + pixel = *(data++); + if (g_host_be) + { + BSWAP16(pixel); + } + SPLITCOLOUR15(pixel, pc); + value = MAKECOLOUR(pc); + LOUT32(out, value); } } } static void -translate16to16(uint16 * data, uint16 * out, uint16 * end) +translate16to16(const uint16 * data, uint8 * out, uint8 * end) { + uint16 pixel; uint16 value; + PixelColour pc; if (g_xserver_be) { - while (out < end) + if (g_host_be) + { + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT16(out, value); + } + } + else { - value = *data; - BSWAP16(value); - *out = value; - data++; - out++; + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT16(out, value); + } } - } else { - while (out < end) + if (g_host_be) { - *out = *data; - out++; - data++; + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT16(out, value); + } + } + else + { + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT16(out, value); + } } } } - static void -translate16to24(uint16 * data, uint8 * out, uint8 * end) +translate16to24(const uint16 * data, uint8 * out, uint8 * end) { uint32 value; uint16 pixel; + PixelColour pc; - while (out < end) + if (g_arch_match) + { + /* *INDENT-OFF* */ + REPEAT3 + ( + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + *(out++) = pc.blue; + *(out++) = pc.green; + *(out++) = pc.red; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) { - pixel = *(data++); - if (g_host_be) { - BSWAP16(pixel)} - - value = make_colour24(split_colour16(pixel)); - - if (g_xserver_be) + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); + } + } + else { - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); + } + } + } + else + { + if (g_host_be) + { + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT24(out, value); + } } else { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT24(out, value); + } } } } static void -translate16to32(uint16 * data, uint8 * out, uint8 * end) +translate16to32(const uint16 * data, uint8 * out, uint8 * end) { uint16 pixel; uint32 value; + PixelColour pc; - while (out < end) + if (g_arch_match) + { + /* *INDENT-OFF* */ + REPEAT4 + ( + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + *(out++) = pc.blue; + *(out++) = pc.green; + *(out++) = pc.red; + *(out++) = 0; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) { - pixel = *(data++); - if (g_host_be) { - BSWAP16(pixel)} - - value = make_colour32(split_colour16(pixel)); - - if (g_xserver_be) + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT32(out, value); + } + } + else + { + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT32(out, value); + } + } + } + else + { + if (g_host_be) { - *(out++) = value >> 24; - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT32(out, value); + } } else { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; - *(out++) = value >> 24; + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT32(out, value); + } } } } static void -translate24to16(uint8 * data, uint8 * out, uint8 * end) +translate24to16(const uint8 * data, uint8 * out, uint8 * end) { uint32 pixel = 0; uint16 value; + PixelColour pc; + while (out < end) { pixel = *(data++) << 16; pixel |= *(data++) << 8; pixel |= *(data++); - - value = (uint16) make_colour16(split_colour24(pixel)); - + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); if (g_xserver_be) { - *(out++) = value >> 8; - *(out++) = value; + BOUT16(out, value); } else { - *(out++) = value; - *(out++) = value >> 8; + LOUT16(out, value); } } } static void -translate24to24(uint8 * data, uint8 * out, uint8 * end) +translate24to24(const uint8 * data, uint8 * out, uint8 * end) { - while (out < end) + uint32 pixel; + uint32 value; + PixelColour pc; + + if (g_xserver_be) + { + while (out < end) + { + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); + } + } + else { - *(out++) = (*(data++)); + while (out < end) + { + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); + LOUT24(out, value); + } } } static void -translate24to32(uint8 * data, uint8 * out, uint8 * end) +translate24to32(const uint8 * data, uint8 * out, uint8 * end) { - while (out < end) + uint32 pixel; + uint32 value; + PixelColour pc; + + if (g_arch_match) { - if (g_xserver_be) - { - *(out++) = 0x00; + /* *INDENT-OFF* */ +#ifdef NEED_ALIGN + REPEAT4 + ( *(out++) = *(data++); *(out++) = *(data++); *(out++) = *(data++); + *(out++) = 0; + ) +#else + REPEAT4 + ( + /* Only read 3 bytes. Reading 4 bytes means reading beyound buffer. */ + *((uint32 *) out) = *((uint16 *) data) + (*((uint8 *) data + 2) << 16); + out += 4; + data += 3; + ) +#endif + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) + { + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); + BOUT32(out, value); } - else + } + else + { + while (out < end) { - *(out++) = *(data++); - *(out++) = *(data++); - *(out++) = *(data++); - *(out++) = 0x00; + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); + LOUT32(out, value); } } } @@ -567,9 +839,26 @@ static uint8 * translate_image(int width, int height, uint8 * data) { - int size = width * height * g_bpp / 8; - uint8 *out = (uint8 *) xmalloc(size); - uint8 *end = out + size; + int size; + uint8 *out; + uint8 *end; + + /* if server and xserver bpp match, */ + /* and arch(endian) matches, no need to translate */ + /* just return data */ + if (g_arch_match) + { + if (g_depth == 15 && g_server_bpp == 15) + return data; + if (g_depth == 16 && g_server_bpp == 16) + return data; + if (g_depth == 24 && g_bpp == 24 && g_server_bpp == 24) + return data; + } + + size = width * height * (g_bpp / 8); + out = (uint8 *) xmalloc(size); + end = out + size; switch (g_server_bpp) { @@ -597,8 +886,7 @@ translate16to24((uint16 *) data, out, end); break; case 16: - translate16to16((uint16 *) data, (uint16 *) out, - (uint16 *) end); + translate16to16((uint16 *) data, out, end); break; } break; @@ -623,13 +911,13 @@ translate8to8(data, out, end); break; case 16: - translate8to16(data, (uint16 *) out, (uint16 *) end); + translate8to16(data, out, end); break; case 24: translate8to24(data, out, end); break; case 32: - translate8to32(data, (uint32 *) out, (uint32 *) end); + translate8to32(data, out, end); break; } break; @@ -662,12 +950,24 @@ return (state & keysymMask) ? True : False; } +static void +calculate_shifts(uint32 mask, int *shift_r, int *shift_l) +{ + *shift_l = ffs(mask) - 1; + mask >>= *shift_l; + *shift_r = 8 - ffs(mask & ~(mask >> 1)); +} + BOOL ui_init(void) { + XVisualInfo vi; XPixmapFormatValues *pfm; uint16 test; - int i; + int i, screen_num, nvisuals; + XVisualInfo *vmatches = NULL; + XVisualInfo template; + Bool TrueColorVisual = False; g_display = XOpenDisplay(NULL); if (g_display == NULL) @@ -676,11 +976,74 @@ return False; } + screen_num = DefaultScreen(g_display); g_x_socket = ConnectionNumber(g_display); - g_screen = DefaultScreenOfDisplay(g_display); - g_visual = DefaultVisualOfScreen(g_screen); + g_screen = ScreenOfDisplay(g_display, screen_num); g_depth = DefaultDepthOfScreen(g_screen); + /* Search for best TrueColor depth */ + template.class = TrueColor; + vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals); + + nvisuals--; + while (nvisuals >= 0) + { + if ((vmatches + nvisuals)->depth > g_depth) + { + g_depth = (vmatches + nvisuals)->depth; + } + nvisuals--; + TrueColorVisual = True; + } + + test = 1; + g_host_be = !(BOOL) (*(uint8 *) (&test)); + g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); + + if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8))) + { + /* we use a colourmap, so the default visual should do */ + g_visual = DefaultVisualOfScreen(g_screen); + g_depth = DefaultDepthOfScreen(g_screen); + + /* Do not allocate colours on a TrueColor visual */ + if (g_visual->class == TrueColor) + { + g_owncolmap = False; + } + } + else + { + /* need a truecolour visual */ + if (!XMatchVisualInfo(g_display, screen_num, g_depth, TrueColor, &vi)) + { + error("The display does not support true colour - high colour support unavailable.\n"); + return False; + } + + g_visual = vi.visual; + g_owncolmap = False; + calculate_shifts(vi.red_mask, &g_red_shift_r, &g_red_shift_l); + calculate_shifts(vi.blue_mask, &g_blue_shift_r, &g_blue_shift_l); + calculate_shifts(vi.green_mask, &g_green_shift_r, &g_green_shift_l); + + /* if RGB video and everything is little endian */ + if ((vi.red_mask > vi.green_mask && vi.green_mask > vi.blue_mask) && + !g_xserver_be && !g_host_be) + { + if (g_depth <= 16 || (g_red_shift_l == 16 && g_green_shift_l == 8 && + g_blue_shift_l == 0)) + { + g_arch_match = True; + } + } + + if (g_arch_match) + { + DEBUG(("Architectures match, enabling little endian optimisations.\n")); + } + } + pfm = XListPixmapFormats(g_display, &i); if (pfm != NULL) { @@ -703,26 +1066,42 @@ return False; } - if (g_owncolmap != True) + if (!g_owncolmap) { - g_xcolmap = DefaultColormapOfScreen(g_screen); + g_xcolmap = + XCreateColormap(g_display, RootWindowOfScreen(g_screen), g_visual, + AllocNone); if (g_depth <= 8) warning("Screen depth is 8 bits or lower: you may want to use -C for a private colourmap\n"); } - g_gc = XCreateGC(g_display, RootWindowOfScreen(g_screen), 0, NULL); - - if (DoesBackingStore(g_screen) != Always) + if ((!g_ownbackstore) && (DoesBackingStore(g_screen) != Always)) + { + warning("External BackingStore not available, using internal\n"); g_ownbackstore = True; + } - test = 1; - g_host_be = !(BOOL) (*(uint8 *) (&test)); - g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); - - if ((g_width == 0) || (g_height == 0)) + /* + * Determine desktop size + */ + if (g_fullscreen) + { + g_width = WidthOfScreen(g_screen); + g_height = HeightOfScreen(g_screen); + } + else if (g_width < 0) + { + /* Percent of screen */ + if (-g_width >= 100) + g_using_full_workarea = True; + g_height = HeightOfScreen(g_screen) * (-g_width) / 100; + g_width = WidthOfScreen(g_screen) * (-g_width) / 100; + } + else if (g_width == 0) { /* Fetch geometry from _NET_WORKAREA */ uint32 x, y, cx, cy; + g_using_full_workarea = True; if (get_current_workarea(&x, &y, &cx, &cy) == 0) { @@ -737,26 +1116,9 @@ } } - if (g_fullscreen) - { - g_width = WidthOfScreen(g_screen); - g_height = HeightOfScreen(g_screen); - } - /* make sure width is a multiple of 4 */ g_width = (g_width + 3) & ~3; - if (g_ownbackstore) - { - g_backstore = - XCreatePixmap(g_display, RootWindowOfScreen(g_screen), g_width, g_height, - g_depth); - - /* clear to prevent rubbish being exposed at startup */ - XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen)); - XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height); - } - g_mod_map = XGetModifierMapping(g_display); xkeymap_init(); @@ -766,8 +1128,7 @@ xclip_init(); - /* todo take this out when high colour is done */ - printf("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth); + DEBUG_RDP5(("server bpp %d client bpp %d depth %d\n", g_server_bpp, g_bpp, g_depth)); return True; } @@ -778,6 +1139,9 @@ if (g_IM != NULL) XCloseIM(g_IM); + if (g_null_cursor != NULL) + ui_destroy_cursor(g_null_cursor); + XFreeModifiermap(g_mod_map); if (g_ownbackstore) @@ -791,6 +1155,9 @@ BOOL ui_create_window(void) { + uint8 null_pointer_mask[1] = { 0x80 }; + uint8 null_pointer_data[24] = { 0x00 }; + XSetWindowAttributes attribs; XClassHint *classhints; XSizeHints *sizehints; @@ -801,13 +1168,37 @@ wndwidth = g_fullscreen ? WidthOfScreen(g_screen) : g_width; wndheight = g_fullscreen ? HeightOfScreen(g_screen) : g_height; + /* Handle -x-y portion of geometry string */ + if (g_xpos < 0 || (g_xpos == 0 && (g_pos & 2))) + g_xpos = WidthOfScreen(g_screen) + g_xpos - g_width; + if (g_ypos < 0 || (g_ypos == 0 && (g_pos & 4))) + g_ypos = HeightOfScreen(g_screen) + g_ypos - g_height; + attribs.background_pixel = BlackPixelOfScreen(g_screen); + attribs.border_pixel = WhitePixelOfScreen(g_screen); attribs.backing_store = g_ownbackstore ? NotUseful : Always; attribs.override_redirect = g_fullscreen; + attribs.colormap = g_xcolmap; - g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), 0, 0, wndwidth, wndheight, - 0, CopyFromParent, InputOutput, CopyFromParent, - CWBackPixel | CWBackingStore | CWOverrideRedirect, &attribs); + g_wnd = XCreateWindow(g_display, RootWindowOfScreen(g_screen), g_xpos, g_ypos, wndwidth, + wndheight, 0, g_depth, InputOutput, g_visual, + CWBackPixel | CWBackingStore | CWOverrideRedirect | CWColormap | + CWBorderPixel, &attribs); + + if (g_gc == NULL) + g_gc = XCreateGC(g_display, g_wnd, 0, NULL); + + if (g_create_bitmap_gc == NULL) + g_create_bitmap_gc = XCreateGC(g_display, g_wnd, 0, NULL); + + if ((g_ownbackstore) && (g_backstore == 0)) + { + g_backstore = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth); + + /* clear to prevent rubbish being exposed at startup */ + XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen)); + XFillRectangle(g_display, g_backstore, g_gc, 0, 0, g_width, g_height); + } XStoreName(g_display, g_wnd, g_title); @@ -826,14 +1217,21 @@ if (sizehints) { sizehints->flags = PMinSize | PMaxSize; + if (g_pos) + sizehints->flags |= PPosition; sizehints->min_width = sizehints->max_width = g_width; sizehints->min_height = sizehints->max_height = g_height; XSetWMNormalHints(g_display, g_wnd, sizehints); XFree(sizehints); } + if (g_embed_wnd) + { + XReparentWindow(g_display, g_wnd, (Window) g_embed_wnd, 0, 0); + } + input_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | - VisibilityChangeMask | FocusChangeMask; + VisibilityChangeMask | FocusChangeMask | StructureNotifyMask; if (g_sendmotion) input_mask |= PointerMotionMask; @@ -863,6 +1261,7 @@ XMaskEvent(g_display, VisibilityChangeMask, &xevent); } while (xevent.type != VisibilityNotify); + g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; g_focused = False; g_mouse_in_wnd = False; @@ -872,10 +1271,47 @@ g_kill_atom = XInternAtom(g_display, "WM_DELETE_WINDOW", True); XSetWMProtocols(g_display, g_wnd, &g_kill_atom, 1); + /* create invisible 1x1 cursor to be used as null cursor */ + if (g_null_cursor == NULL) + g_null_cursor = ui_create_cursor(0, 0, 1, 1, null_pointer_mask, null_pointer_data); + return True; } void +ui_resize_window() +{ + XSizeHints *sizehints; + Pixmap bs; + + sizehints = XAllocSizeHints(); + if (sizehints) + { + sizehints->flags = PMinSize | PMaxSize; + sizehints->min_width = sizehints->max_width = g_width; + sizehints->min_height = sizehints->max_height = g_height; + XSetWMNormalHints(g_display, g_wnd, sizehints); + XFree(sizehints); + } + + if (!(g_fullscreen || g_embed_wnd)) + { + XResizeWindow(g_display, g_wnd, g_width, g_height); + } + + /* create new backstore pixmap */ + if (g_backstore != 0) + { + bs = XCreatePixmap(g_display, g_wnd, g_width, g_height, g_depth); + XSetForeground(g_display, g_gc, BlackPixelOfScreen(g_screen)); + XFillRectangle(g_display, bs, g_gc, 0, 0, g_width, g_height); + XCopyArea(g_display, g_backstore, bs, g_gc, 0, 0, g_width, g_height, 0, 0); + XFreePixmap(g_display, g_backstore); + g_backstore = bs; + } +} + +void ui_destroy_window(void) { if (g_IC != NULL) @@ -909,23 +1345,91 @@ } } -/* Process all events in Xlib queue +static void +handle_button_event(XEvent xevent, BOOL down) +{ + uint16 button, flags = 0; + g_last_gesturetime = xevent.xbutton.time; + button = xkeymap_translate_button(xevent.xbutton.button); + if (button == 0) + return; + + if (down) + flags = MOUSE_FLAG_DOWN; + + /* Stop moving window when button is released, regardless of cursor position */ + if (g_moving_wnd && (xevent.type == ButtonRelease)) + g_moving_wnd = False; + + /* If win_button_size is nonzero, enable single app mode */ + if (xevent.xbutton.y < g_win_button_size) + { + /* Check from right to left: */ + if (xevent.xbutton.x >= g_width - g_win_button_size) + { + /* The close button, continue */ + ; + } + else if (xevent.xbutton.x >= g_width - g_win_button_size * 2) + { + /* The maximize/restore button. Do not send to + server. It might be a good idea to change the + cursor or give some other visible indication + that rdesktop inhibited this click */ + if (xevent.type == ButtonPress) + return; + } + else if (xevent.xbutton.x >= g_width - g_win_button_size * 3) + { + /* The minimize button. Iconify window. */ + if (xevent.type == ButtonRelease) + { + /* Release the mouse button outside the minimize button, to prevent the + actual minimazation to happen */ + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, button, 1, 1); + XIconifyWindow(g_display, g_wnd, DefaultScreen(g_display)); + return; + } + } + else if (xevent.xbutton.x <= g_win_button_size) + { + /* The system menu. Ignore. */ + if (xevent.type == ButtonPress) + return; + } + else + { + /* The title bar. */ + if (xevent.type == ButtonPress) + { + if (!g_fullscreen && g_hide_decorations && !g_using_full_workarea) + { + g_moving_wnd = True; + g_move_x_offset = xevent.xbutton.x; + g_move_y_offset = xevent.xbutton.y; + } + return; + } + } + } + + rdp_send_input(time(NULL), RDP_INPUT_MOUSE, + flags | button, xevent.xbutton.x, xevent.xbutton.y); +} + +/* Process events in Xlib queue Returns 0 after user quit, 1 otherwise */ static int xwin_process_events(void) { XEvent xevent; KeySym keysym; - uint16 button, flags; uint32 ev_time; - key_translation tr; char str[256]; Status status; - unsigned int state; - Window wdummy; - int dummy; + int events = 0; - while (XPending(g_display) > 0) + while ((XPending(g_display) > 0) && events++ < 20) { XNextEvent(g_display, &xevent); @@ -935,10 +1439,11 @@ continue; } - flags = 0; - switch (xevent.type) { + case VisibilityNotify: + g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; + break; case ClientMessage: /* the window manager told us to quit */ if ((xevent.xclient.message_type == g_protocol_atom) @@ -970,24 +1475,15 @@ str, sizeof(str), &keysym, NULL); } - DEBUG_KBD(("KeyPress for (keysym 0x%lx, %s)\n", keysym, + DEBUG_KBD(("KeyPress for keysym (0x%lx, %s)\n", keysym, get_ksname(keysym))); ev_time = time(NULL); if (handle_special_keys(keysym, xevent.xkey.state, ev_time, True)) break; - tr = xkeymap_translate_key(keysym, - xevent.xkey.keycode, xevent.xkey.state); - - if (tr.scancode == 0) - break; - - save_remote_modifiers(tr.scancode); - ensure_remote_modifiers(ev_time, tr); - rdp_send_scancode(ev_time, RDP_KEYPRESS, tr.scancode); - restore_remote_modifiers(ev_time, tr.scancode); - + xkeymap_send_keys(keysym, xevent.xkey.keycode, xevent.xkey.state, + ev_time, True, 0); break; case KeyRelease: @@ -995,85 +1491,23 @@ XLookupString((XKeyEvent *) & xevent, str, sizeof(str), &keysym, NULL); - DEBUG_KBD(("\nKeyRelease for (keysym 0x%lx, %s)\n", keysym, + DEBUG_KBD(("\nKeyRelease for keysym (0x%lx, %s)\n", keysym, get_ksname(keysym))); ev_time = time(NULL); if (handle_special_keys(keysym, xevent.xkey.state, ev_time, False)) break; - tr = xkeymap_translate_key(keysym, - xevent.xkey.keycode, xevent.xkey.state); - - if (tr.scancode == 0) - break; - - rdp_send_scancode(ev_time, RDP_KEYRELEASE, tr.scancode); + xkeymap_send_keys(keysym, xevent.xkey.keycode, xevent.xkey.state, + ev_time, False, 0); break; case ButtonPress: - flags = MOUSE_FLAG_DOWN; - /* fall through */ + handle_button_event(xevent, True); + break; case ButtonRelease: - g_last_gesturetime = xevent.xbutton.time; - button = xkeymap_translate_button(xevent.xbutton.button); - if (button == 0) - break; - - /* If win_button_size is nonzero, enable single app mode */ - if (xevent.xbutton.y < g_win_button_size) - { - /* Stop moving window when button is released, regardless of cursor position */ - if (g_moving_wnd && (xevent.type == ButtonRelease)) - g_moving_wnd = False; - - /* Check from right to left: */ - - if (xevent.xbutton.x >= g_width - g_win_button_size) - { - /* The close button, continue */ - ; - } - else if (xevent.xbutton.x >= - g_width - g_win_button_size * 2) - { - /* The maximize/restore button. Do not send to - server. It might be a good idea to change the - cursor or give some other visible indication - that rdesktop inhibited this click */ - break; - } - else if (xevent.xbutton.x >= - g_width - g_win_button_size * 3) - { - /* The minimize button. Iconify window. */ - XIconifyWindow(g_display, g_wnd, - DefaultScreen(g_display)); - break; - } - else if (xevent.xbutton.x <= g_win_button_size) - { - /* The system menu. Ignore. */ - break; - } - else - { - /* The title bar. */ - if ((xevent.type == ButtonPress) && !g_fullscreen - && g_hide_decorations) - { - g_moving_wnd = True; - g_move_x_offset = xevent.xbutton.x; - g_move_y_offset = xevent.xbutton.y; - } - break; - - } - } - - rdp_send_input(time(NULL), RDP_INPUT_MOUSE, - flags | button, xevent.xbutton.x, xevent.xbutton.y); + handle_button_event(xevent, False); break; case MotionNotify: @@ -1096,9 +1530,7 @@ if (xevent.xfocus.mode == NotifyGrab) break; g_focused = True; - XQueryPointer(g_display, g_wnd, &wdummy, &wdummy, &dummy, &dummy, - &dummy, &dummy, &state); - reset_modifier_keys(state); + reset_modifier_keys(); if (g_grab_keyboard && g_mouse_in_wnd) XGrabKeyboard(g_display, g_wnd, True, GrabModeAsync, GrabModeAsync, CurrentTime); @@ -1168,6 +1600,12 @@ case PropertyNotify: xclip_handle_PropertyNotify(&xevent.xproperty); break; + case MapNotify: + rdp_send_client_window_status(1); + break; + case UnmapNotify: + rdp_send_client_window_status(0); + break; } } /* Keep going */ @@ -1178,11 +1616,14 @@ int ui_select(int rdp_socket) { - int n = (rdp_socket > g_x_socket) ? rdp_socket + 1 : g_x_socket + 1; + int n; fd_set rfds, wfds; + struct timeval tv; + BOOL s_timeout = False; while (True) { + n = (rdp_socket > g_x_socket) ? rdp_socket : g_x_socket; /* Process any events already waiting */ if (!xwin_process_events()) /* User quit */ @@ -1198,19 +1639,32 @@ if (g_dsp_busy) { FD_SET(g_dsp_fd, &wfds); - n = (g_dsp_fd + 1 > n) ? g_dsp_fd + 1 : n; + n = (g_dsp_fd > n) ? g_dsp_fd : n; } #endif + /* default timeout */ + tv.tv_sec = 60; + tv.tv_usec = 0; + + /* add redirection handles */ + rdpdr_add_fds(&n, &rfds, &wfds, &tv, &s_timeout); - switch (select(n, &rfds, &wfds, NULL, NULL)) + n++; + + switch (select(n, &rfds, &wfds, NULL, &tv)) { case -1: error("select: %s\n", strerror(errno)); case 0: + /* Abort serial read calls */ + if (s_timeout) + rdpdr_check_fds(&rfds, &wfds, (BOOL) True); continue; } + rdpdr_check_fds(&rfds, &wfds, (BOOL) False); + if (FD_ISSET(rdp_socket, &rfds)) return 1; @@ -1233,16 +1687,29 @@ XImage *image; Pixmap bitmap; uint8 *tdata; + int bitmap_pad; + + if (g_server_bpp == 8) + { + bitmap_pad = 8; + } + else + { + bitmap_pad = g_bpp; + + if (g_bpp == 24) + bitmap_pad = 32; + } tdata = (g_owncolmap ? data : translate_image(width, height, data)); bitmap = XCreatePixmap(g_display, g_wnd, width, height, g_depth); image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0, - (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0); + (char *) tdata, width, height, bitmap_pad, 0); - XPutImage(g_display, bitmap, g_gc, image, 0, 0, 0, 0, width, height); + XPutImage(g_display, bitmap, g_create_bitmap_gc, image, 0, 0, 0, 0, width, height); XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); return (HBITMAP) bitmap; } @@ -1252,9 +1719,23 @@ { XImage *image; uint8 *tdata; + int bitmap_pad; + + if (g_server_bpp == 8) + { + bitmap_pad = 8; + } + else + { + bitmap_pad = g_bpp; + + if (g_bpp == 24) + bitmap_pad = 32; + } + tdata = (g_owncolmap ? data : translate_image(width, height, data)); image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 0, - (char *) tdata, width, height, g_server_bpp == 8 ? 8 : g_bpp, 0); + (char *) tdata, width, height, bitmap_pad, 0); if (g_ownbackstore) { @@ -1267,7 +1748,7 @@ } XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); } @@ -1283,12 +1764,12 @@ XImage *image; Pixmap bitmap; int scanline; - GC gc; scanline = (width + 7) / 8; bitmap = XCreatePixmap(g_display, g_wnd, width, height, 1); - gc = XCreateGC(g_display, bitmap, 0, NULL); + if (g_create_glyph_gc == 0) + g_create_glyph_gc = XCreateGC(g_display, bitmap, 0, NULL); image = XCreateImage(g_display, g_visual, 1, ZPixmap, 0, (char *) data, width, height, 8, scanline); @@ -1296,10 +1777,9 @@ image->bitmap_bit_order = MSBFirst; XInitImage(image); - XPutImage(g_display, bitmap, gc, image, 0, 0, 0, 0, width, height); + XPutImage(g_display, bitmap, g_create_glyph_gc, image, 0, 0, 0, 0, width, height); XFree(image); - XFreeGC(g_display, gc); return (HGLYPH) bitmap; } @@ -1393,6 +1873,12 @@ XFreeCursor(g_display, (Cursor) cursor); } +void +ui_set_null_cursor(void) +{ + ui_set_cursor(g_null_cursor); +} + #define MAKE_XCOLOR(xc,c) \ (xc)->red = ((c)->red << 8) | (c)->red; \ (xc)->green = ((c)->green << 8) | (c)->green; \ @@ -1474,9 +1960,7 @@ } - - /* byte swap here to make translate_image faster */ - map[i] = translate_colour(colour); + map[i] = colour; } return map; } @@ -1587,7 +2071,7 @@ { case 0: /* Solid */ SET_FOREGROUND(fgcolour); - FILL_RECTANGLE(x, y, cx, cy); + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); break; case 2: /* Hatch */ @@ -1598,7 +2082,7 @@ XSetFillStyle(g_display, g_gc, FillOpaqueStippled); XSetStipple(g_display, g_gc, fill); XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); - FILL_RECTANGLE(x, y, cx, cy); + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); XSetFillStyle(g_display, g_gc, FillSolid); XSetTSOrigin(g_display, g_gc, 0, 0); ui_destroy_glyph((HGLYPH) fill); @@ -1608,15 +2092,12 @@ for (i = 0; i != 8; i++) ipattern[7 - i] = brush->pattern[i]; fill = (Pixmap) ui_create_glyph(8, 8, ipattern); - SET_FOREGROUND(bgcolour); SET_BACKGROUND(fgcolour); XSetFillStyle(g_display, g_gc, FillOpaqueStippled); XSetStipple(g_display, g_gc, fill); XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); - - FILL_RECTANGLE(x, y, cx, cy); - + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); XSetFillStyle(g_display, g_gc, FillSolid); XSetTSOrigin(g_display, g_gc, 0, 0); ui_destroy_glyph((HGLYPH) fill); @@ -1627,6 +2108,9 @@ } RESET_FUNCTION(opcode); + + if (g_ownbackstore) + XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y); } void @@ -1635,9 +2119,25 @@ /* src */ int srcx, int srcy) { SET_FUNCTION(opcode); - XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); if (g_ownbackstore) - XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, y); + { + if (g_Unobscured) + { + XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, + y); + } + else + { + XCopyArea(g_display, g_backstore, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + XCopyArea(g_display, g_backstore, g_backstore, g_gc, srcx, srcy, cx, cy, x, + y); + } + } + else + { + XCopyArea(g_display, g_wnd, g_wnd, g_gc, srcx, srcy, cx, cy, x, y); + } RESET_FUNCTION(opcode); } @@ -1708,6 +2208,151 @@ FILL_RECTANGLE(x, y, cx, cy); } +void +ui_polygon(uint8 opcode, + /* mode */ uint8 fillmode, + /* dest */ POINT * point, int npoints, + /* brush */ BRUSH * brush, int bgcolour, int fgcolour) +{ + uint8 style, i, ipattern[8]; + Pixmap fill; + + SET_FUNCTION(opcode); + + switch (fillmode) + { + case ALTERNATE: + XSetFillRule(g_display, g_gc, EvenOddRule); + break; + case WINDING: + XSetFillRule(g_display, g_gc, WindingRule); + break; + default: + unimpl("fill mode %d\n", fillmode); + } + + if (brush) + style = brush->style; + else + style = 0; + + switch (style) + { + case 0: /* Solid */ + SET_FOREGROUND(fgcolour); + FILL_POLYGON((XPoint *) point, npoints); + break; + + case 2: /* Hatch */ + fill = (Pixmap) ui_create_glyph(8, 8, + hatch_patterns + brush->pattern[0] * 8); + SET_FOREGROUND(fgcolour); + SET_BACKGROUND(bgcolour); + XSetFillStyle(g_display, g_gc, FillOpaqueStippled); + XSetStipple(g_display, g_gc, fill); + XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); + FILL_POLYGON((XPoint *) point, npoints); + XSetFillStyle(g_display, g_gc, FillSolid); + XSetTSOrigin(g_display, g_gc, 0, 0); + ui_destroy_glyph((HGLYPH) fill); + break; + + case 3: /* Pattern */ + for (i = 0; i != 8; i++) + ipattern[7 - i] = brush->pattern[i]; + fill = (Pixmap) ui_create_glyph(8, 8, ipattern); + SET_FOREGROUND(bgcolour); + SET_BACKGROUND(fgcolour); + XSetFillStyle(g_display, g_gc, FillOpaqueStippled); + XSetStipple(g_display, g_gc, fill); + XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); + FILL_POLYGON((XPoint *) point, npoints); + XSetFillStyle(g_display, g_gc, FillSolid); + XSetTSOrigin(g_display, g_gc, 0, 0); + ui_destroy_glyph((HGLYPH) fill); + break; + + default: + unimpl("brush %d\n", brush->style); + } + + RESET_FUNCTION(opcode); +} + +void +ui_polyline(uint8 opcode, + /* dest */ POINT * points, int npoints, + /* pen */ PEN * pen) +{ + /* TODO: set join style */ + SET_FUNCTION(opcode); + SET_FOREGROUND(pen->colour); + XDrawLines(g_display, g_wnd, g_gc, (XPoint *) points, npoints, CoordModePrevious); + if (g_ownbackstore) + XDrawLines(g_display, g_backstore, g_gc, (XPoint *) points, npoints, + CoordModePrevious); + RESET_FUNCTION(opcode); +} + +void +ui_ellipse(uint8 opcode, + /* mode */ uint8 fillmode, + /* dest */ int x, int y, int cx, int cy, + /* brush */ BRUSH * brush, int bgcolour, int fgcolour) +{ + uint8 style, i, ipattern[8]; + Pixmap fill; + + SET_FUNCTION(opcode); + + if (brush) + style = brush->style; + else + style = 0; + + switch (style) + { + case 0: /* Solid */ + SET_FOREGROUND(fgcolour); + DRAW_ELLIPSE(x, y, cx, cy, fillmode); + break; + + case 2: /* Hatch */ + fill = (Pixmap) ui_create_glyph(8, 8, + hatch_patterns + brush->pattern[0] * 8); + SET_FOREGROUND(fgcolour); + SET_BACKGROUND(bgcolour); + XSetFillStyle(g_display, g_gc, FillOpaqueStippled); + XSetStipple(g_display, g_gc, fill); + XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); + DRAW_ELLIPSE(x, y, cx, cy, fillmode); + XSetFillStyle(g_display, g_gc, FillSolid); + XSetTSOrigin(g_display, g_gc, 0, 0); + ui_destroy_glyph((HGLYPH) fill); + break; + + case 3: /* Pattern */ + for (i = 0; i != 8; i++) + ipattern[7 - i] = brush->pattern[i]; + fill = (Pixmap) ui_create_glyph(8, 8, ipattern); + SET_FOREGROUND(bgcolour); + SET_BACKGROUND(fgcolour); + XSetFillStyle(g_display, g_gc, FillOpaqueStippled); + XSetStipple(g_display, g_gc, fill); + XSetTSOrigin(g_display, g_gc, brush->xorigin, brush->yorigin); + DRAW_ELLIPSE(x, y, cx, cy, fillmode); + XSetFillStyle(g_display, g_gc, FillSolid); + XSetTSOrigin(g_display, g_gc, 0, 0); + ui_destroy_glyph((HGLYPH) fill); + break; + + default: + unimpl("brush %d\n", brush->style); + } + + RESET_FUNCTION(opcode); +} + /* warning, this function only draws on wnd or backstore, not both */ void ui_draw_glyph(int mixmode, @@ -1732,47 +2377,56 @@ {\ glyph = cache_get_font (font, ttext[idx]);\ if (!(flags & TEXT2_IMPLICIT_X))\ + {\ + xyoffset = ttext[++idx];\ + if ((xyoffset & 0x80))\ {\ - xyoffset = ttext[++idx];\ - if ((xyoffset & 0x80))\ - {\ - if (flags & TEXT2_VERTICAL) \ - y += ttext[idx+1] | (ttext[idx+2] << 8);\ - else\ - x += ttext[idx+1] | (ttext[idx+2] << 8);\ - idx += 2;\ - }\ + if (flags & TEXT2_VERTICAL)\ + y += ttext[idx+1] | (ttext[idx+2] << 8);\ else\ - {\ - if (flags & TEXT2_VERTICAL) \ - y += xyoffset;\ - else\ - x += xyoffset;\ - }\ + x += ttext[idx+1] | (ttext[idx+2] << 8);\ + idx += 2;\ }\ - if (glyph != NULL)\ + else\ {\ - ui_draw_glyph (mixmode, x + glyph->offset,\ - y + glyph->baseline,\ - glyph->width, glyph->height,\ - glyph->pixmap, 0, 0, bgcolour, fgcolour);\ - if (flags & TEXT2_IMPLICIT_X)\ - x += glyph->width;\ + if (flags & TEXT2_VERTICAL)\ + y += xyoffset;\ + else\ + x += xyoffset;\ }\ + }\ + if (glyph != NULL)\ + {\ + x1 = x + glyph->offset;\ + y1 = y + glyph->baseline;\ + XSetStipple(g_display, g_gc, (Pixmap) glyph->pixmap);\ + XSetTSOrigin(g_display, g_gc, x1, y1);\ + FILL_RECTANGLE_BACKSTORE(x1, y1, glyph->width, glyph->height);\ + if (flags & TEXT2_IMPLICIT_X)\ + x += glyph->width;\ + }\ } void -ui_draw_text(uint8 font, uint8 flags, int mixmode, int x, int y, +ui_draw_text(uint8 font, uint8 flags, uint8 opcode, int mixmode, int x, int y, int clipx, int clipy, int clipcx, int clipcy, - int boxx, int boxy, int boxcx, int boxcy, int bgcolour, - int fgcolour, uint8 * text, uint8 length) + int boxx, int boxy, int boxcx, int boxcy, BRUSH * brush, + int bgcolour, int fgcolour, uint8 * text, uint8 length) { + /* TODO: use brush appropriately */ + FONTGLYPH *glyph; - int i, j, xyoffset; + int i, j, xyoffset, x1, y1; DATABLOB *entry; SET_FOREGROUND(bgcolour); + /* Sometimes, the boxcx value is something really large, like + 32691. This makes XCopyArea fail with Xvnc. The code below + is a quick fix. */ + if (boxx + boxcx > g_width) + boxcx = g_width - boxx; + if (boxcx > 1) { FILL_RECTANGLE_BACKSTORE(boxx, boxy, boxcx, boxcy); @@ -1782,31 +2436,50 @@ FILL_RECTANGLE_BACKSTORE(clipx, clipy, clipcx, clipcy); } + SET_FOREGROUND(fgcolour); + SET_BACKGROUND(bgcolour); + XSetFillStyle(g_display, g_gc, FillStippled); + /* Paint text, character by character */ for (i = 0; i < length;) { switch (text[i]) { case 0xff: - if (i + 2 < length) - cache_put_text(text[i + 1], text, text[i + 2]); - else + /* At least two bytes needs to follow */ + if (i + 3 > length) { - error("this shouldn't be happening\n"); - exit(1); + warning("Skipping short 0xff command:"); + for (j = 0; j < length; j++) + fprintf(stderr, "%02x ", text[j]); + fprintf(stderr, "\n"); + i = length = 0; + break; } + cache_put_text(text[i + 1], text, text[i + 2]); + i += 3; + length -= i; /* this will move pointer from start to first character after FF command */ - length -= i + 3; - text = &(text[i + 3]); + text = &(text[i]); i = 0; break; case 0xfe: + /* At least one byte needs to follow */ + if (i + 2 > length) + { + warning("Skipping short 0xfe command:"); + for (j = 0; j < length; j++) + fprintf(stderr, "%02x ", text[j]); + fprintf(stderr, "\n"); + i = length = 0; + break; + } entry = cache_get_text(text[i + 1]); - if (entry != NULL) + if (entry->data != NULL) { - if ((((uint8 *) (entry->data))[1] == - 0) && (!(flags & TEXT2_IMPLICIT_X))) + if ((((uint8 *) (entry->data))[1] == 0) + && (!(flags & TEXT2_IMPLICIT_X)) && (i + 2 < length)) { if (flags & TEXT2_VERTICAL) y += text[i + 2]; @@ -1832,6 +2505,9 @@ break; } } + + XSetFillStyle(g_display, g_gc, FillSolid); + if (g_ownbackstore) { if (boxcx > 1) @@ -1893,3 +2569,14 @@ XFree(image); } + +/* these do nothing here but are used in uiports */ +void +ui_begin_update(void) +{ +} + +void +ui_end_update(void) +{ +}