--- sourceforge.net/trunk/rdesktop/xwin.c 2004/02/07 18:47:06 603 +++ sourceforge.net/trunk/rdesktop/xwin.c 2006/01/24 12:40:24 1042 @@ -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 @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -29,12 +30,17 @@ 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; extern BOOL g_hide_decorations; extern char g_title[]; -extern int g_server_bpp; +/* Color depth of the RDP session. + As of RDP 5.1, it may be 8, 15, 16 or 24. */ +extern int g_server_depth; extern int g_win_button_size; Display *g_display; @@ -42,10 +48,20 @@ static int g_x_socket; static Screen *g_screen; Window g_wnd; +extern uint32 g_embed_wnd; BOOL g_enable_compose = False; +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; +/* Color depth of the X11 visual of our window (e.g. 24 for True Color R8G8B visual). + This may be 32 for R8G8B8 visuals, and then the rest of the bits are undefined + as far as we're concerned. */ static int g_depth; +/* Bits-per-Pixel of the pixmaps we'll be using to draw on our window. + This may be larger than g_depth, in which case some of the bits would + be kept solely for alignment (e.g. 32bpp pixmaps on a 24bpp visual). */ static int g_bpp; static XIM g_IM; static XIC g_IC; @@ -55,6 +71,13 @@ static Atom g_protocol_atom, g_kill_atom; static BOOL g_focused; static BOOL g_mouse_in_wnd; +/* Indicates the visual is has 15, 16 or 24 depth + and the same color channel masks as its RDP equivalent. */ +static BOOL g_compatible_depth; +/* Indicates whether RDP's bitmaps and our XImages have the same + binary format. If so, we can avoid an expensive translation. + If this is True, so is g_compatible_depth. */ +static BOOL g_no_translate_image = False; /* endianness */ static BOOL g_host_be; @@ -63,13 +86,14 @@ static int g_red_shift_l, g_blue_shift_l, g_green_shift_l; /* software backing store */ -static BOOL g_ownbackstore; +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; @@ -82,11 +106,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; @@ -111,12 +135,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 : g_colmap[col] ) +#define TRANSLATE(col) ( g_server_depth != 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)); @@ -164,427 +212,646 @@ (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_colour(PixelColour pc) -{ - return (((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 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); \ +} \ + +#define SPLITCOLOUR24(colour, rv) \ +{ \ + rv.blue = (colour & 0xff0000) >> 16; \ + rv.green = (colour & 0x00ff00) >> 8; \ + rv.red = (colour & 0x0000ff); \ } +#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 & 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) + switch (g_server_depth) { case 15: - pc = split_colour15(colour); + SPLITCOLOUR15(colour, pc); break; case 16: - pc = split_colour16(colour); + SPLITCOLOUR16(colour, pc); break; case 24: - pc = split_colour24(colour); + SPLITCOLOUR24(colour, pc); + break; + default: + /* Avoid warning */ + pc.red = 0; + pc.green = 0; + pc.blue = 0; break; } - return make_colour(pc); + 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, uint8 * out, uint8 * end) +translate8to16(const uint8 * data, uint8 * out, uint8 * end) { uint16 value; - while (out < end) + if (g_compatible_depth) { - value = (uint16) g_colmap[*(data++)]; - - if (g_xserver_be) + /* *INDENT-OFF* */ + REPEAT2 + ( + *((uint16 *) out) = g_colmap[*(data++)]; + out += 2; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) { - *(out++) = value >> 8; - *(out++) = value; + value = (uint16) g_colmap[*(data++)]; + BOUT16(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; + 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_compatible_depth) { - value = g_colmap[*(data++)]; - - if (g_xserver_be) + while (out < end) { - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + value = g_colmap[*(data++)]; + BOUT24(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + value = g_colmap[*(data++)]; + LOUT24(out, value); } } } static void -translate8to32(uint8 * data, uint8 * out, uint8 * end) +translate8to32(const uint8 * data, uint8 * out, uint8 * end) { uint32 value; - while (out < end) + if (g_compatible_depth) { - value = g_colmap[*(data++)]; - - if (g_xserver_be) + /* *INDENT-OFF* */ + REPEAT4 + ( + *((uint32 *) out) = g_colmap[*(data++)]; + out += 4; + ) + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) { - *(out++) = value >> 24; - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + value = g_colmap[*(data++)]; + BOUT32(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; - *(out++) = value >> 24; + 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_colour(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_compatible_depth) { - pixel = *(data++); - - if (g_host_be) - { - BSWAP16(pixel); - } - - value = make_colour(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_compatible_depth) { - pixel = *(data++); - - if (g_host_be) - { - BSWAP16(pixel); - } - - value = make_colour(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, uint8 * out, uint8 * end) +translate16to16(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); + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT16(out, value); + } } - - value = make_colour(split_colour16(pixel)); - - if (g_xserver_be) + else { - *(out++) = value >> 8; - *(out++) = value; + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT16(out, value); + } + } + } + else + { + if (g_host_be) + { + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + LOUT16(out, value); + } } else { - *(out++) = value; - *(out++) = value >> 8; + 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_compatible_depth) + { + /* *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); + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); + } + } + else + { + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); + } } - - value = make_colour(split_colour16(pixel)); - - if (g_xserver_be) + } + else + { + if (g_host_be) { - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + 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_compatible_depth) + { + /* *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); + while (out < end) + { + pixel = *(data++); + BSWAP16(pixel); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT32(out, value); + } } - - value = make_colour(split_colour16(pixel)); - - if (g_xserver_be) + else { - *(out++) = value >> 24; - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + while (out < end) + { + pixel = *(data++); + SPLITCOLOUR16(pixel, pc); + value = MAKECOLOUR(pc); + BOUT32(out, value); + } + } + } + else + { + if (g_host_be) + { + 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_colour(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) { uint32 pixel; uint32 value; + PixelColour pc; - while (out < end) + if (g_xserver_be) { - pixel = *(data++) << 16; - pixel |= *(data++) << 8; - pixel |= *(data++); - - value = make_colour(split_colour24(pixel)); - - if (g_xserver_be) + while (out < end) { - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); + BOUT24(out, value); } - else + } + else + { + while (out < end) { - *(out++) = value; - *(out++) = value >> 8; - *(out++) = value >> 16; + 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) { uint32 pixel; uint32 value; + PixelColour pc; - while (out < end) + if (g_compatible_depth) { - pixel = *(data++) << 16; - pixel |= *(data++) << 8; - pixel |= *(data++); - - value = make_colour(split_colour24(pixel)); - - if (g_xserver_be) + /* *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 beyond buffer. */ + *((uint32 *) out) = *((uint16 *) data) + (*((uint8 *) data + 2) << 16); + out += 4; + data += 3; + ) +#endif + /* *INDENT-ON* */ + } + else if (g_xserver_be) + { + while (out < end) { - *(out++) = value >> 24; - *(out++) = value >> 16; - *(out++) = value >> 8; - *(out++) = value; + pixel = *(data++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(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++) << 16; + pixel |= *(data++) << 8; + pixel |= *(data++); + SPLITCOLOUR24(pixel, pc); + value = MAKECOLOUR(pc); + LOUT32(out, value); } } } @@ -592,11 +859,31 @@ 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 RDP depth and X Visual depths match, + and arch(endian) matches, no need to translate: + just return data. + Note: select_visual should've already ensured g_no_translate + is only set for compatible depths, but the RDP depth might've + changed during connection negotiations. + */ + if (g_no_translate_image) + { + if ((g_depth == 15 && g_server_depth == 15) || + (g_depth == 16 && g_server_depth == 16) || + (g_depth == 24 && g_server_depth == 24)) + return data; + } + + size = width * height * (g_bpp / 8); + out = (uint8 *) xmalloc(size); + end = out + size; - switch (g_server_bpp) + switch (g_server_depth) { case 24: switch (g_bpp) @@ -694,109 +981,262 @@ *shift_r = 8 - ffs(mask & ~(mask >> 1)); } -BOOL -ui_init(void) +/* Given a mask of a colour channel (e.g. XVisualInfo.red_mask), + calculates the bits-per-pixel of this channel (a.k.a. colour weight). + */ +static unsigned +calculate_mask_weight(uint32 mask) +{ + unsigned weight = 0; + do + { + weight += (mask & 1); + } + while (mask >>= 1); + return weight; +} + +static BOOL +select_visual() { - XVisualInfo vi; XPixmapFormatValues *pfm; - uint16 test; - int i, screen_num, nvisuals; + int pixmap_formats_count, visuals_count; XVisualInfo *vmatches = NULL; XVisualInfo template; - Bool TrueColorVisual = False; + int i; + unsigned red_weight, blue_weight, green_weight; - g_display = XOpenDisplay(NULL); - if (g_display == NULL) + red_weight = blue_weight = green_weight = 0; + + pfm = XListPixmapFormats(g_display, &pixmap_formats_count); + if (pfm == NULL) { - error("Failed to open display: %s\n", XDisplayName(NULL)); + error("Unable to get list of pixmap formats from display.\n"); + XCloseDisplay(g_display); return False; } - screen_num = DefaultScreen(g_display); - g_x_socket = ConnectionNumber(g_display); - g_screen = ScreenOfDisplay(g_display, screen_num); - g_depth = DefaultDepthOfScreen(g_screen); - - /* Search for best TrueColor depth */ + /* Search for best TrueColor visual */ template.class = TrueColor; - vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &nvisuals); + vmatches = XGetVisualInfo(g_display, VisualClassMask, &template, &visuals_count); + g_visual = NULL; + g_no_translate_image = False; + g_compatible_depth = False; + if (vmatches != NULL) + { + for (i = 0; i < visuals_count; ++i) + { + XVisualInfo *visual_info = &vmatches[i]; + + /* Try to find a no-translation visual that'll + allow us to use RDP bitmaps directly as ZPixmaps. */ + if (!g_xserver_be && (((visual_info->depth == 15) && + /* R5G5B5 */ + (visual_info->red_mask == 0x7c00) && + (visual_info->green_mask == 0x3e0) && + (visual_info->blue_mask == 0x1f)) || + ((visual_info->depth == 16) && + /* R5G6B5 */ + (visual_info->red_mask == 0xf800) && + (visual_info->green_mask == 0x7e0) && + (visual_info->blue_mask == 0x1f)) || + ((visual_info->depth == 24) && + /* R8G8B8 */ + (visual_info->red_mask == 0xff0000) && + (visual_info->green_mask == 0xff00) && + (visual_info->blue_mask == 0xff)))) + { + g_visual = visual_info->visual; + g_depth = visual_info->depth; + g_compatible_depth = True; + g_no_translate_image = (visual_info->depth == g_server_depth); + if (g_no_translate_image) + /* We found the best visual */ + break; + } + else + { + g_compatible_depth = False; + } - nvisuals--; - while (nvisuals >= 0) - { - if ((vmatches + nvisuals)->depth > g_depth) - { - g_depth = (vmatches + nvisuals)->depth; + if (visual_info->depth > 24) + { + /* Avoid 32-bit visuals and likes like the plague. + They're either untested or proven to work bad + (e.g. nvidia's Composite 32-bit visual). + Most implementation offer a 24-bit visual anyway. */ + continue; + } + + /* Only care for visuals, for whose BPPs (not depths!) + we have a translateXtoY function. */ + BOOL can_translate_to_bpp = False; + int j; + for (j = 0; j < pixmap_formats_count; ++j) + { + if (pfm[j].depth == visual_info->depth) + { + if ((pfm[j].bits_per_pixel == 16) || + (pfm[j].bits_per_pixel == 24) || + (pfm[j].bits_per_pixel == 32)) + { + can_translate_to_bpp = True; + } + break; + } + } + + /* Prefer formats which have the most colour depth. + We're being truly aristocratic here, minding each + weight on its own. */ + if (can_translate_to_bpp) + { + unsigned vis_red_weight = + calculate_mask_weight(visual_info->red_mask); + unsigned vis_green_weight = + calculate_mask_weight(visual_info->green_mask); + unsigned vis_blue_weight = + calculate_mask_weight(visual_info->blue_mask); + if ((vis_red_weight >= red_weight) + && (vis_green_weight >= green_weight) + && (vis_blue_weight >= blue_weight)) + { + red_weight = vis_red_weight; + green_weight = vis_green_weight; + blue_weight = vis_blue_weight; + g_visual = visual_info->visual; + g_depth = visual_info->depth; + } + } } - nvisuals--; - TrueColorVisual = True; + XFree(vmatches); } - if ((g_server_bpp == 8) && ((!TrueColorVisual) || (g_depth <= 8))) + if (g_visual != NULL) { - /* 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; - } + g_owncolmap = False; + calculate_shifts(g_visual->red_mask, &g_red_shift_r, &g_red_shift_l); + calculate_shifts(g_visual->green_mask, &g_green_shift_r, &g_green_shift_l); + calculate_shifts(g_visual->blue_mask, &g_blue_shift_r, &g_blue_shift_l); } 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"); + template.class = PseudoColor; + template.depth = 8; + template.colormap_size = 256; + vmatches = + XGetVisualInfo(g_display, + VisualClassMask | VisualDepthMask | VisualColormapSizeMask, + &template, &visuals_count); + if (vmatches == NULL) + { + error("No usable TrueColor or PseudoColor visuals on this display.\n"); + XCloseDisplay(g_display); + XFree(pfm); 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); + /* we use a colourmap, so the default visual should do */ + g_owncolmap = True; + g_visual = vmatches[0].visual; + g_depth = vmatches[0].depth; } - pfm = XListPixmapFormats(g_display, &i); - if (pfm != NULL) + g_bpp = 0; + for (i = 0; i < pixmap_formats_count; ++i) { - /* Use maximum bpp for this depth - this is generally - desirable, e.g. 24 bits->32 bits. */ - while (i--) + XPixmapFormatValues *pf = &pfm[i]; + if (pf->depth == g_depth) { - if ((pfm[i].depth == g_depth) && (pfm[i].bits_per_pixel > g_bpp)) + g_bpp = pf->bits_per_pixel; + + if (g_no_translate_image) { - g_bpp = pfm[i].bits_per_pixel; + switch (g_server_depth) + { + case 15: + case 16: + if (g_bpp != 16) + g_no_translate_image = False; + break; + case 24: + /* Yes, this will force image translation + on most modern servers which use 32 bits + for R8G8B8. */ + if (g_bpp != 24) + g_no_translate_image = False; + break; + default: + g_no_translate_image = False; + break; + } } + + /* Pixmap formats list is a depth-to-bpp mapping -- + there's just a single entry for every depth, + so we can safely break here */ + break; } - XFree(pfm); } + XFree(pfm); + pfm = NULL; + return True; +} + +BOOL +ui_init(void) +{ + int screen_num; - if (g_bpp < 8) + g_display = XOpenDisplay(NULL); + if (g_display == NULL) { - error("Less than 8 bpp not currently supported.\n"); - XCloseDisplay(g_display); + error("Failed to open display: %s\n", XDisplayName(NULL)); + return False; + } + + { + uint16 endianess_test = 1; + g_host_be = !(BOOL) (*(uint8 *) (&endianess_test)); + } + + g_xserver_be = (ImageByteOrder(g_display) == MSBFirst); + screen_num = DefaultScreen(g_display); + g_x_socket = ConnectionNumber(g_display); + g_screen = ScreenOfDisplay(g_display, screen_num); + g_depth = DefaultDepthOfScreen(g_screen); + + if (!select_visual()) return False; + + if (g_no_translate_image) + { + DEBUG(("Performance optimization possible: avoiding image translation (colour depth conversion).\n")); + } + + if (g_server_depth > g_bpp) + { + warning("Remote desktop colour depth %d higher than display colour depth %d.\n", + g_server_depth, g_bpp); } + DEBUG(("RDP depth: %d, display depth: %d, display bpp: %d, X server BE: %d, host BE: %d\n", + g_server_depth, g_depth, g_bpp, g_xserver_be, g_host_be)); + if (!g_owncolmap) { 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"); + warning("Display colour depth is %d bit: you may want to use -C for a private colourmap.\n", g_depth); } - 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); + } /* * Determine desktop size @@ -809,6 +1249,8 @@ 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; } @@ -816,6 +1258,7 @@ { /* Fetch geometry from _NET_WORKAREA */ uint32 x, y, cx, cy; + g_using_full_workarea = True; if (get_current_workarea(&x, &y, &cx, &cy) == 0) { @@ -842,7 +1285,7 @@ xclip_init(); - DEBUG_RDP5(("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_depth, g_bpp, g_depth)); return True; } @@ -870,7 +1313,8 @@ ui_create_window(void) { uint8 null_pointer_mask[1] = { 0x80 }; - uint8 null_pointer_data[4] = { 0x00, 0x00, 0x00, 0x00 }; + uint8 null_pointer_data[24] = { 0x00 }; + XSetWindowAttributes attribs; XClassHint *classhints; XSizeHints *sizehints; @@ -881,20 +1325,29 @@ 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, g_depth, InputOutput, g_visual, - CWBackPixel | CWBackingStore | CWOverrideRedirect | - CWColormap | CWBorderPixel, &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); @@ -921,14 +1374,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; @@ -958,6 +1418,7 @@ XMaskEvent(g_display, VisibilityChangeMask, &xevent); } while (xevent.type != VisibilityNotify); + g_Unobscured = xevent.xvisibility.state == VisibilityUnobscured; g_focused = False; g_mouse_in_wnd = False; @@ -975,6 +1436,39 @@ } 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) @@ -1008,20 +1502,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; + int events = 0; - while (XPending(g_display) > 0) + while ((XPending(g_display) > 0) && events++ < 20) { XNextEvent(g_display, &xevent); @@ -1031,10 +1596,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) @@ -1066,24 +1632,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: @@ -1091,85 +1648,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: @@ -1262,6 +1757,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 */ @@ -1272,13 +1773,14 @@ int ui_select(int rdp_socket) { - int n = (rdp_socket > g_x_socket) ? rdp_socket : g_x_socket; + 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 */ @@ -1312,12 +1814,9 @@ error("select: %s\n", strerror(errno)); case 0: - /* TODO: if tv.tv_sec just times out - * we will segfault. - * FIXME: - */ - //s_timeout = True; - //rdpdr_check_fds(&rfds, &wfds, (BOOL) True); + /* Abort serial read calls */ + if (s_timeout) + rdpdr_check_fds(&rfds, &wfds, (BOOL) True); continue; } @@ -1347,7 +1846,7 @@ uint8 *tdata; int bitmap_pad; - if (g_server_bpp == 8) + if (g_server_depth == 8) { bitmap_pad = 8; } @@ -1364,10 +1863,10 @@ image = XCreateImage(g_display, g_visual, g_depth, ZPixmap, 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; } @@ -1379,7 +1878,7 @@ uint8 *tdata; int bitmap_pad; - if (g_server_bpp == 8) + if (g_server_depth == 8) { bitmap_pad = 8; } @@ -1406,7 +1905,7 @@ } XFree(image); - if (!g_owncolmap) + if (tdata != data) xfree(tdata); } @@ -1422,12 +1921,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); @@ -1435,10 +1934,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; } @@ -1730,7 +2228,7 @@ { case 0: /* Solid */ SET_FOREGROUND(fgcolour); - FILL_RECTANGLE(x, y, cx, cy); + FILL_RECTANGLE_BACKSTORE(x, y, cx, cy); break; case 2: /* Hatch */ @@ -1741,7 +2239,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); @@ -1751,15 +2249,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); @@ -1770,6 +2265,9 @@ } RESET_FUNCTION(opcode); + + if (g_ownbackstore) + XCopyArea(g_display, g_backstore, g_wnd, g_gc, x, y, cx, cy, x, y); } void @@ -1778,9 +2276,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); } @@ -1851,6 +2365,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, @@ -1906,17 +2565,25 @@ } 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, 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); @@ -1936,25 +2603,40 @@ 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]; @@ -2044,3 +2726,14 @@ XFree(image); } + +/* these do nothing here but are used in uiports */ +void +ui_begin_update(void) +{ +} + +void +ui_end_update(void) +{ +}