#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>

uint16_t x2dos[256][4][2]={

//0x00

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{0x08,0x0e,0x08,0x0e,0x7f,0x0e,0x00,0x0e},//BS
{0x09,0x0f,0x15,0x15,0x00,0x94,0x00,0xa5},//TAB
{0x0a,0x1c,0x0a,0x1c,0x0d,0x1c,0x00,0x1c},//LF
{   0,   0,   0,   0,   0,   0,   0,   0},//CLEAR
{   0,   0,   0,   0,   0,   0,   0,   0},
{0x0d,0x1c,0x0d,0x1c,0x0a,0x1c,0x00,0x1c},//RET
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x10

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},//Pause
{   0,   0,   0,   0,   0,   0,   0,   0},//Scroll_Lock
{   0,   0,   0,   0,   0,   0,   0,   0},//SysRq
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{0x1b,0x01,0x1b,0x01,0x1b,0x01,0x00,0x01},//ESC
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x20

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x30

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x40

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x50:cursor block

{0xe0,0x47,0xe0,0x47,0xe0,0x77,0xe0,0x97},//Home
{0xe0,0x4b,0xe0,0x4b,0xe0,0x73,0xe0,0x9b},//Left
{0xe0,0x48,0xe0,0x48,0xe0,0x8d,0xe0,0x98},//Up
{0xe0,0x4d,0xe0,0x4d,0xe0,0x74,0xe0,0x9d},//Right
{0xe0,0x50,0xe0,0x50,0xe0,0x91,0xe0,0xa0},//Down
{0xe0,0x49,0xe0,0x49,0xe0,0x84,0xe0,0x99},//PgUp
{0xe0,0x51,0xe0,0x51,0xe0,0x76,0xe0,0xa1},//PgDn
{0xe0,0x4f,0xe0,0x4f,0xe0,0x75,0xe0,0x9f},//EOL

{0xe0,0x47,0xe0,0x47,0xe0,0x77,0xe0,0x97},//BOL
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x60

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{0xe0,0x52,0xe0,0x52,0xe0,0x92,   0,0xa2},//INS
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{0x1b,0x01,0x1b,0x01,0x1b,0x01,0x00,0x01},//ESC==Cancel
{   0,   0,   0,   0,   0,   0,   0,   0},//
{0x1b,0x01,0x1b,0x01,0x1b,0x01,0x00,0x01},//ESC==Break
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x70

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

//0x80: Num-Block

{0x20,0x57,0x20,0x57,0x20,0x57,0x20,0x57},//SPACE
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//

{   0,   0,   0,   0,   0,   0,   0,   0},//
{0x09,0x0f,0x15,0x15,0x00,0x94,0x00,0xa5},//TAB
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{0x0d,0xe0,0x0d,0xe0,0x0a,0xe0,   0,0xa6},//ENTER
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//

//0x90

{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,0x3b,   0,0x54,   0,0x5e,   0,0x68},//KP_F1
{   0,0x3b,   0,0x55,   0,0x5f,   0,0x69},//
{   0,0x3d,   0,0x56,   0,0x60,   0,0x6a},//
{   0,0x3b,   0,0x57,   0,0x61,   0,0x6b},//KP_F4
{   0,0x47,0x37,0x47,   0,0x77,   0,   0},//KP_HOME
{   0,0x4b,0x34,0x4b,   0,0x73,   0,   0},//KP_LEFT
{   0,0x48,0x38,0x48,   0,0x8d,   0,   0},//KP_UP

{   0,0x4d,0x36,0x4d,   0,0x74,   0,   0},//KP_RIGHT
{   0,0x50,0x32,0x50,   0,0x91,   0,   0},//KP_DOWN
{   0,0x49,0x39,0x49,   0,0x84,   0,   0},//KP_PgUp
{   0,0x51,0x33,0x51,   0,0x76,   0,   0},//KP_PgDn
{   0,0x4f,0x31,0x4f,   0,0x91,   0,   0},//KP_END
{   0,0x47,0x37,0x47,   0,0x77,   0,   0},//KP_HOME==BEGIN
{   0,0x52,0x31,0x52,   0,0x92,   0,   0},//KP_INS
{   0,0x53,0x2e,0x53,   0,0x93,   0,   0},//KP_DEL

//0xa0

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{0x2a,0xe0,0x2a,0xe0,   0,0x96,   0,   0},//KP_*
{0x2b,0xe0,0x2b,0xe0,   0,0x90,   0,0x4e},//KP_+
{0x2e,0xe0,0x2d,0xe0,   0,0x8e,   0,0x4a},//KP_-
{0x2d,0x53,   0,0x53,   0,0x93,   0,   0},//KP_,
{0x2c,0x53,   0,0x53,   0,0x93,   0,   0},//KP_.
{0x2f,0xe0,0x2f,0xe0,   0,0x95,   0,0xa4},//KP_/

//0xb0

{0x31,0x4f,0x31,0x4f,   0,0x75,   0,   0},//KP_1
{0x32,0x50,0x32,0x50,   0,0x91,   0,   0},//KP_2
{0x33,0x51,0x33,0x51,   0,0x76,   0,   0},//KP_3
{0x34,0x4b,0x34,0x4b,   0,0x73,   0,   0},//KP_4
{0x35,0x4c,0x35,0x4c,   0,0x8f,   0,   0},//KP_5
{0x36,0x4d,0x36,0x4d,   0,0x74,   0,   0},//KP_6
{0x37,0x47,0x37,0x47,   0,0x77,   0,   0},//KP_7
{0x38,0x48,0x38,0x48,   0,0x8d,   0,   0},//KP_8

{0x39,0x49,0x39,0x49,   0,0x84,   0,   0},//KP_9
{0x30,0x52,0x30,0x52,   0,0x92,   0,   0},//KP_0
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,   0,   0,   0,   0,   0,   0,   0},//
{   0,0x3b,   0,0x54,   0,0x5e,   0,0x68},//F1
{   0,0x3c,   0,0x55,   0,0x5f,   0,0x69},

//0xc0

{   0,0x3d,   0,0x56,   0,0x60,   0,0x6a},//F3
{   0,0x3b,   0,0x57,   0,0x61,   0,0x6b},
{   0,0x3f,   0,0x58,   0,0x62,   0,0x6c},
{   0,0x40,   0,0x59,   0,0x63,   0,0x6d},
{   0,0x41,   0,0x5a,   0,0x64,   0,0x6e},
{   0,0x42,   0,0x5b,   0,0x65,   0,0x6f},
{   0,0x43,   0,0x5c,   0,0x66,   0,0x70},
{   0,0x44,   0,0x5d,   0,0x67,   0,0x71},//F10

{   0,0x85,   0,0x87,   0,0x89,   0,0x8b},//F11
{   0,0x86,   0,0x88,   0,0x8a,   0,0x8c},//F12
{   0,   0,   0,   0,   0,   0,   0,   0},//F13
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},//F18

//0xd0

{   0,   0,   0,   0,   0,   0,   0,   0},//F19
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},//F26

{   0,   0,   0,   0,   0,   0,   0,   0},//F27
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},//F34

//0xe0

{   0,   0,   0,   0,   0,   0,   0,   0},//F35
{   0,   0,   0,   0,   0,   0,   0,   0},//Shift
{   0,   0,   0,   0,   0,   0,   0,   0},//R
{   0,   0,   0,   0,   0,   0,   0,   0},//Ctrl
{   0,   0,   0,   0,   0,   0,   0,   0},//R
{   0,   0,   0,   0,   0,   0,   0,   0},//CAPSLOCK
{   0,   0,   0,   0,   0,   0,   0,   0},//SHIFTLOCK
{   0,   0,   0,   0,   0,   0,   0,   0},//Meta

{   0,   0,   0,   0,   0,   0,   0,   0},//R
{   0,   0,   0,   0,   0,   0,   0,   0},//Alt
{   0,   0,   0,   0,   0,   0,   0,   0},//R
{   0,   0,   0,   0,   0,   0,   0,   0},//Super
{   0,   0,   0,   0,   0,   0,   0,   0},//R
{   0,   0,   0,   0,   0,   0,   0,   0},//Hyper
{   0,   0,   0,   0,   0,   0,   0,   0},//R
{   0,   0,   0,   0,   0,   0,   0,   0},//

//0xf0

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},
{   0,   0,   0,   0,   0,   0,   0,   0},

{0xe0,0x53,0xe0,0x53,0xe0,0x93,   0,0xa3}};//DEL

uint8_t * Xfont_8x8;
uint8_t * Xfont_8x14;
uint8_t * Xfont_8x16;

struct Font_t {
	uint8_t *d;
	uint32_t size;
};

struct rect {
	uint32_t x1, y1, x2, y2;
};

struct XData {
	XImage *image;
	struct Font_t font;
	union {
		uint32_t *x32;
		uint16_t *x16;
		uint8_t *x8;
	} bg;
	uint32_t bg_size;
	uint32_t bytes_per_line;
	uint32_t depth;
	uint32_t kbsym;
	uint32_t kbstate;
	uint32_t kbkeycode;
	uint32_t kbscan;
	uint32_t mx;
	uint32_t my;
	uint32_t mb;
	struct rect update;
	uint8_t active;
	Display *dis;
	Visual *visual;
	int screen;
	int black;
	int white;
	Window win;
	Window root;
	Colormap colormap;
	GC gc;
	XVisualInfo vi;
	XComposeStatus compose;
	XEvent e;
	Bool IsEvent;
	KeySym keysym;
} X;

static uint8_t const EGA[16][3] = {
	0x00,0x00,000, 0x00,0x00,0xaa, 0x00,0xaa,0x00, 0x00,0xaa,0xaa,
	0xaa,0x00,000, 0xaa,0x00,0xaa, 0xaa,0x55,0x00, 0xaa,0xaa,0xaa,
	0x55,0x55,055, 0x55,0x55,0xff, 0x55,0xff,0x55, 0x55,0xff,0xff,
	0xff,0x55,055, 0xff,0x55,0xff, 0xff,0xff,0x55, 0xff,0xff,0xff
};

char hex[16]="0123456789abcdef";

static XColor Xpal[256];
static uint32_t XWpal[256/32];


void Xupdate(uint32_t x, uint32_t y)
{
	if (x < X.update.x1)
		X.update.x1 = x;
	if (y < X.update.y1)
		X.update.y1 = y;
	if (x > X.update.x2)
		X.update.x2 = x;
	if (y > X.update.y2)
		X.update.y2 = y;
}

void Xupdate2(uint32_t x, uint32_t y, uint32_t x2, uint32_t y2)
{
	if (x2 > X.update.x2)
		X.update.x2 = x2;
	if (y2 > X.update.y2)
		X.update.y2 = y2;
	if (x < X.update.x1)
		X.update.x1 = x;
	if (y < X.update.y1)
		X.update.y1 = y;
}

static uint32_t mousemode=0;

void Xdone()
{
	if (X.image) {
		XDestroyImage(X.image);
		X.image = NULL;
	}
//  if(X.colormap){
//      XFree(X.colormap);
//      X.colormap=NULL;
//  }
	if (X.gc) {
		XFreeGC(X.dis, X.gc);
		X.gc = NULL;
	}
//  if(X.visual){
//      XFree(X.visual);
//      X.visual=NULL;
//  }
	if (X.dis) {
		XCloseDisplay(X.dis);
		X.dis = NULL;
	}
	X.active=0;
};

void XSetColorBlock(uint16_t first, uint16_t num, uint8_t * c)
{
	int i,j,k,d,e;
	int f=first;
	char C[8]="#######";
	if(!(X.vi.class & 1)) //static color-map)
	for(i=0;i<num;i++) {
		C[1]=hex[*c>>4];
		C[2]=hex[*(c++)&15];
		C[3]=hex[*c>>4];
		C[4]=hex[*(c++)&15];
		C[5]=hex[*c>>4];
		C[6]=hex[*(c++)&15];
		XParseColor(X.dis, X.colormap, C, &Xpal[f]);
		XAllocColor(X.dis, X.colormap, &Xpal[f++]);
	}
	else for(i=0;i<num;i++) {
		int used=0;
		e=0xff;
		for(k=0;k<8;k++){
			C[1]=hex[(d=*c++&e)>>4];
			C[2]=hex[d&15];
			C[3]=hex[(d=*c++&e)>>4];
			C[4]=hex[d&15];
			C[5]=hex[(d=*c++&e)>>4];
			C[6]=hex[d&15];
			if(XWpal[f/32]&(1<<f%32)) {
				for(j=0;j<256;j++)
					if((j!=i)
					&& XWpal[j/32]&(1<<j%32)
					&& Xpal[j].pixel==Xpal[f].pixel)
					used=1;
				if(!used) {
					XFreeColors(X.dis,X.colormap,&Xpal[f].pixel,1,0);
					XWpal[f/32]&=~(1<<f%32);
				}
			}
			XParseColor(X.dis, X.colormap, C, &Xpal[f]);		
			if(XAllocColor(X.dis, X.colormap, &Xpal[f])){
				XWpal[f/32]|=(1<<f%32);
				break;
			} else c-=3;
			e<<=1;
			f++;
		};
	}
};

int XinitC(uint32_t x, uint32_t y)
{
	int i;

	if(!X.dis) {
		if (!(X.dis = XOpenDisplay(NULL)))
			return 0;
		X.screen = DefaultScreen(X.dis);
		X.root = DefaultRootWindow(X.dis);
		X.visual = DefaultVisual(X.dis, X.screen);
		X.depth = DefaultDepth(X.dis, X.screen);
		X.black = BlackPixel(X.dis, X.screen);
		X.white = WhitePixel(X.dis, X.screen);
		X.colormap = DefaultColormap(X.dis, 0);

		i=5;
		while (!XMatchVisualInfo(X.dis, X.screen, X.depth,
			i--, &X.vi) && (X.vi.visual != X.visual));

		memset(XWpal,0,sizeof(XWpal));
		XSetColorBlock(0,16,EGA);
	} else {
		if (X.image) {
			XDestroyImage(X.image);
			X.image = NULL;
		}
		if (X.gc) {
			XFreeGC(X.dis, X.gc);
			X.gc = NULL;
		}
		XDestroyWindow(X.dis,X.win);
		for(i=0;i<256;i++) if(XWpal[i/32]&1<<(i%32)) {
			XFreeColors(X.dis,X.colormap,&Xpal[i].pixel,1,0);
		}
		XSetColorBlock(0,16,EGA);
	}

	X.win =
		XCreateSimpleWindow(X.dis, X.root, 0, 0, x, y,
							0, X.black, X.black);
	X.gc = XCreateGC(X.dis, X.win, 0, 0);

	if (!(X.image =
		  XCreateImage(X.dis, X.visual, X.depth, ZPixmap, 0, NULL, x, y,
					   32, 0))
		|| !(X.image->data = X.bg.x8 =
			 malloc((X.bg_size = X.image->bytes_per_line * y)))
		|| (!XInitImage(X.image))) {
		Xdone();
		return 0;
	};
	memset(X.bg.x8, 0, X.bg_size);
	X.bytes_per_line = X.image->bytes_per_line;
	X.IsEvent=0;

	X.update.x1 = x - 1;
	X.update.y1 = y - 1;
	X.update.x2 = 0;
	X.update.y2 = 0;

	XMapWindow(X.dis, X.win);
	XSelectInput(X.dis, X.win, ExposureMask | KeyPressMask);

	return (X.active=1);
};

void Xplotline8_32(uint16_t ox, uint16_t xx,
                   uint16_t oy, uint8_t * c)
{
	uint32_t * dest=&X.bg.x32[oy * X.bytes_per_line >> 2 + ox];
	Xupdate2(ox,oy,ox+xx-1,oy);
	for(;xx>0;xx--) *(dest++)=	Xpal[*c++].pixel;
};

void Xplotline2_8_32(uint16_t ox, uint16_t xx,
                   uint16_t oy, uint8_t * c)
{
	uint32_t * dest=&X.bg.x32[oy * X.bytes_per_line >> 2 + ox*2];
	Xupdate2(ox,oy,ox+xx-1,oy);
	for(;xx>0;xx--) {
		*(dest++)=	Xpal[*c].pixel;
		*(dest++)=	Xpal[*c++].pixel;
	}
};

void Xsetmouse(uint32_t mouse)
{
	mousemode=mouse;
	XSelectInput(X.dis, X.win, ExposureMask | KeyPressMask
				 | (mouse?ButtonPressMask|ButtonReleaseMask:0)
				 | (mouse>1?PointerMotionMask:0));
}

void Xwriteca4_32(uint8_t c, uint8_t a, uint_fast16_t x, uint_fast16_t y)
{
	int i, j;
	uint32_t A = Xpal[a & 0xf].pixel;
	uint32_t B = Xpal[a >>4].pixel;
	int size = X.font.size;
	Xupdate2(x, y, x + 7, y + X.font.size-1);
	for (i = 0; i < X.font.size; i++) {
		uint_fast8_t b = X.font.d[size * c + i];
		uint_fast16_t Y = ((y + i) * X.bytes_per_line >> 2) + x;
		for (j = 0; j < 8; j++) {
			if (b & 0x80)
				X.bg.x32[Y] = A;
			else
				X.bg.x32[Y] = B;
			Y++;
			b <<= 1;
		}
	}
}

void Xwritecan7_32(uint8_t c, uint8_t a, uint32_t n, uint32_t x, uint32_t y)
{
	int i, j, k;
	uint32_t A = Xpal[a & 0x7f].pixel;
	int size = X.font.size;
	Xupdate2(x, y, x + 7 + 8*n, y + X.font.size-1);
	for (i = 0; i < X.font.size; i++) {
		uint_fast8_t B = X.font.d[size * c + i];
		for(k=0;k<n;k++) {
			uint_fast8_t b = B;
			uint_fast16_t Y = ((y + i) * X.bytes_per_line >> 2) + x;
			if(a&0x80) for (j = 0; j < 8; j++) {
				if (b & 0x80)
					X.bg.x32[Y] ^= A;
				Y++;
				b <<= 1;
			} else for (j = 0; j < 8; j++) {
				if (b & 0x80)
					X.bg.x32[Y] = A;
				Y++;
				b <<= 1;
			}
		}
	}
}

void Xwritesa4_32(int32_t l, uint8_t const *s, uint8_t a,
			   uint_fast16_t x, uint_fast16_t y)
{
	int i, j, k;
	uint8_t t[l];
	uint32_t A = Xpal[a & 0xf].pixel;
	uint32_t B = Xpal[a >> 4].pixel;
	if (!l)
		return;
	Xupdate2(x, y, x + l * 8 - 1, y + X.font.size-1);
	for (i = 0; i < X.font.size; i++) {
		uint_fast16_t Y = ((y + i) * X.bytes_per_line >> 2) + x;
		for (k = 0; k < l; k++)
			t[k] = X.font.d[X.font.size * s[k] + i];
		for (k = 0; k < l; k++) {
			uint_fast8_t c = t[k];
			for (j = 0; j < 8; j++) {
				if (c & 0x80)
					X.bg.x32[Y++] = A;
				else
					X.bg.x32[Y++] = B;
				c <<= 1;
			}
		}
	}
}

void Xhline8_32(int32_t x, int32_t x2, int32_t y, int8_t a)
{
	uint_fast16_t Y = (y * X.bytes_per_line >> 2) + x;
	uint32_t A = Xpal[a].pixel;
	Xupdate2(x,y,x2,y);
	for(x2=x2-x+Y;Y<=x2;Y++) X.bg.x32[Y] = A;
}

void Xfrect8_32(int32_t x1, int32_t y1,
                int32_t x2, int32_t y2, int8_t a)
{
	uint32_t A = Xpal[a].pixel;
	Xupdate2(x1,y1,x2,y2);
	x2-=x1;
	for(;y1<=y2;y1++){
		uint_fast16_t Y = (y1 * X.bytes_per_line >> 2) + x1;
		int x3;
		for(x3=x2+Y;Y<=x3;Y++) X.bg.x32[Y] = A;
	}
}

void Xvline8_32(int32_t x, int32_t y, int32_t y2, int8_t a, int8_t logic)
{
	uint_fast16_t inc=X.bytes_per_line >> 2;
	uint_fast16_t Y = (y * inc) + x;
	uint32_t A = Xpal[a].pixel;
	Xupdate2(x,y,x,y2);
	for(;y<=y2;y++) {
		X.bg.x32[Y] = A;
		Y+=inc;
	}
}

void Xplot8_32(uint32_t x, uint32_t y, uint8_t a)
{
	uint32_t A = Xpal[a].pixel;
	uint_fast16_t Y = ((y) * X.bytes_per_line >> 2) + x;
	Xupdate(x, y);
	X.bg.x32[Y] = A;
}

void Xdelay(uint_fast32_t ms)
{
	XEvent e;

	while (ms > 200) {
		while (XCheckMaskEvent(X.dis, ExposureMask, &e)) {
			Xupdate2(e.xexpose.x, e.xexpose.y,
					 e.xexpose.x + e.xexpose.width - 1,
					 e.xexpose.y + e.xexpose.height - 1);
		}
		if (X.update.x1 < X.update.x2) {
			XPutImage(X.dis, X.win, X.gc, X.image, X.update.x1,
					  X.update.y1, X.update.x1, X.update.y1,
					  X.update.x2 - X.update.x1 + 1,
					  X.update.y2 - X.update.y1 + 1);

			X.update.x1 = X.image->width - 1;
			X.update.y1 = X.image->height - 1;
			X.update.x2 = 0;
			X.update.y2 = 0;
		}

		XFlush(X.dis);
		usleep(200000);
		ms -= 200;
	}
	usleep(ms * 1000);
}


uint8_t Xkeypressed()
{
	return X.IsEvent || (X.IsEvent=XCheckMaskEvent(X.dis, -1, &X.e ));
}

uint8_t Xreadkey()
{
	char c[16];
	int l;
	
	if(X.kbsym) {
		int i=X.kbsym&0xffffff;
		X.kbsym=0;
		return i;
	}

	while (1){
		if(X.IsEvent&&(X.e.type==Expose)) {
			Xupdate2(X.e.xexpose.x, X.e.xexpose.y,
			X.e.xexpose.x + X.e.xexpose.width - 1,
			X.e.xexpose.y + X.e.xexpose.height - 1);
			X.IsEvent=0;
		}
		while (XCheckMaskEvent(X.dis, ExposureMask, &X.e)) {
			Xupdate2(X.e.xexpose.x,  X.e.xexpose.y,
					 X.e.xexpose.x + X.e.xexpose.width - 1,
					 X.e.xexpose.y + X.e.xexpose.height - 1);
		}
		if (!Xkeypressed() && (X.update.x1 < X.update.x2)) {
			XPutImage(X.dis, X.win, X.gc, X.image, X.update.x1,
					  X.update.y1, X.update.x1, X.update.y1,
					  X.update.x2 - X.update.x1 + 1,
					  X.update.y2 - X.update.y1 + 1);

			X.update.x1 = X.image->width - 1;
			X.update.y1 = X.image->height - 1;
			X.update.x2 = 0;
			X.update.y2 = 0;
		}

		if(!X.IsEvent)XNextEvent(X.dis, &X.e);
		X.IsEvent=0;
		switch(X.e.type) {
		case Expose:
			Xupdate2(X.e.xexpose.x,  X.e.xexpose.y,
					 X.e.xexpose.x + X.e.xexpose.width - 1,
					 X.e.xexpose.y + X.e.xexpose.height - 1);
			break;
		case KeyPress:
			l=XLookupString(&X.e.xkey, c,16, &X.keysym, &X.compose);
			X.kbstate=X.e.xkey.state;
			X.kbkeycode=X.e.xkey.keycode;
			printf("K %x  %d %x %c  %x %x\n",X.keysym,
			    l,c[0],c[0], X.kbstate,X.kbkeycode);
			/*fixme*/
			if((X.keysym&~0xff)==0xff00) {
				int i=X.kbstate&1?1:X.kbstate&4?2:X.kbstate&0x8?3:0;
				printf("k: #%x #%x\n",x2dos[X.keysym-0xff00][i][0],
	                                  x2dos[X.keysym-0xff00][i][1]);
				if(!(X.kbsym=x2dos[X.keysym-0xff00][i][1])) continue;
				i=x2dos[X.keysym-0xff00][i][0];
				return i==0xe0?0:i;
			}
			if(1==l) return c[0];
			return 0;
		case ButtonPress:
			printf("M %d %d %x\n",	X.e.xbutton.x,X.e.xbutton.y,X.e.xbutton.button);
			return 0;
			break;
		case ButtonRelease:
			printf("m %d %d %x\n",	X.e.xbutton.x,X.e.xbutton.y,X.e.xbutton.button);
			return 254;
			break;
		case MotionNotify:
//			printf("O %d %d %x\n",	X.e.xmotion.x,X.e.xmotion.y,X.e.xmotion.state);
			X.mx=X.e.xmotion.x;
			X.my=X.e.xmotion.y;
			X.mb=X.e.xmotion.state;
			X.kbsym=0x1000000;
			return 0;
			break;
		}
	}
}

/*
{ext_data = 0x0,
visualid = 36,
class = 4,
red_mask = 16711680,
green_mask = 65280,
blue_mask = 255,
bits_per_rgb = 8,
map_entries = 256}

{
width = 640,
height = 480,
xoffset = 0,
format = 2,
data = 0x804ad40 "",
byte_order = 0,
bitmap_unit = 32,
bitmap_bit_order = 0,
bitmap_pad = 32,
depth = 24,
bytes_per_line = 2560,
bits_per_pixel = 32,
red_mask = 16711680,
green_mask = 65280,
blue_mask = 255,
obdata = 0x0,
f = {create_image = 0x8048810 <XCreateImage>,
	destroy_image = 0x40106bb8 <_XDestroyImage>,
	get_pixel = 0x40106eb4 <_XGetPixel32>,
	put_pixel = 0x401074f8 <_XPutPixel32>,
	sub_image = 0x40107758 <_XSubImage>,
	add_pixel = 0x401079e8 <_XAddPixel>}}

/***********************************************************


CURSORBLOCK:
~~~~~~~~~~~~

  TAB	09/0f#	15/15#	00/94*  00/a5*

  ^°    5e/29#	f8/29#  -----	00/29
  1!	31/02#	21/02#	-----	00/78
  2"²	32/03#	22/03#  00/03	00/79
  3§	33/04#	15/04#	-----	00/7a
  4$	34/05#	24/05#	-----	00/7b
  5%	35/06#	25/06#	-----	00/7c
  6&	36/07#	26/07#  1e/07#	00/7d
  7/{	37/08#	2f/08#	-----	00/08 oder 00/7e
  8([	38/09#	28/09#	-----	00/7f
  9)]	39/0a#	29/0a#	-----	00/80
  0=}	30/0b#	3d/0b#  -----	00/81
  ß?\	e1/0c#  3f/0c#	1f/0c#  00/82
  '`	27/0d#	60/0d#  -----	00/83

  qQ@	71/10#	51/10#	11/10#	00/10
  wW	77/11#	57/11#	17/11#  00/11
  eE	64/12#	44/12#	05/12#  00/12
  rR	72/13#	52/13#	12/13#  00/13
  tT	74/14#	54/14#	14/14#  00/14
  zZ	7a/2c#	5a/2c#	1a/2c#  00/2c
  uU	75/16#	55/16#	15/16#  00/16
  iI	69/17#	49/17#	09/17#  00/17
  oO	6f/18#	4f/18#	0f/18#  00/18
  pP	70/19#	50/19#	10/19#  00/19
  üÜ	81/1a#	9a/1a#	1b/1a#  00/1a
  +*~	2b/1b#	2a/1b#	1d/1b#	00/1b
  <ŽÙ	0d/1c#	0d/1c#	0a/1c#	00/1c*

  aA    61/1e#	41/1e#	01/1e#	00/1e
  sS	73/1f#	53/1f#	13/1f#	00/1f
  dD	64/20#	44/20#	04/20#	00/20
  fF	66/21#	46/21#	06/21#	00/21
  gG	67/22#	47/22#	07/22#	00/22
  hH	68/23#	48/23#	08/23#	00/23
  jJ	6a/24#	4a/24#	0a/24#	00/24
  kK	6b/25#	4b/25#	0b/25#	00/25
  lL	6c/26#	4c/26#	0c/26#	00/26
  öÖ	94/27#	99/27#	-----	00/27
  äÄ	84/28#	8e/28#	-----	00/28
  #'	23/29#	27/29#	1c/29#	00/29

  <>|	3c/56#	3e/56#	-----	-----
  yY	79/15#	59/15#	19/15#	00/15
  xX	78/2d#	58/2d#	18/2d#	00/2d
  cC	63/2e#	43/2e#	03/2e#	00/2e
  vV	76/2f#	56/2f#	16/2f#	00/2f
  bB	62/30#	42/30#	02/30#	00/30
  nN	6e/31#	4e/31#	0e/31#	00/31
  mM	6d/32#	4d/32#	0d/32#	00/32
  ,;	2c/33#	3b/33#	-----	00/33
  .:	2e/34#	3a/34#	-----	00/34
  -_	2d/35#	5f/35#	-----	00/35

 SPACE	20/57#	20/57#	20/57#	20/57#

/* International & multi-key character composition * /



/*
 * Auxilliary Functions; note the duplicate definitions for left and right
 * function keys;  Sun keyboards and a few other manufactures have such
 * function key groups on the left and/or right sides of the keyboard.
 * We've not found a keyboard with more than 35 function keys total.
 * /

/*
 * ISO 9995 Function and Modifier Keys
 * Byte 3 = 0xFE
 * /

#define	XK_ISO_Left_Tab					0xFE20
#define	XK_ISO_Move_Line_Up				0xFE21
#define	XK_ISO_Move_Line_Down				0xFE22
#define	XK_ISO_Partial_Line_Up				0xFE23
#define	XK_ISO_Partial_Line_Down			0xFE24
#define	XK_ISO_Partial_Space_Left			0xFE25
#define	XK_ISO_Partial_Space_Right			0xFE26
#define	XK_ISO_Set_Margin_Left				0xFE27
#define	XK_ISO_Set_Margin_Right				0xFE28
#define	XK_ISO_Release_Margin_Left			0xFE29
#define	XK_ISO_Release_Margin_Right			0xFE2A
#define	XK_ISO_Release_Both_Margins			0xFE2B
#define	XK_ISO_Fast_Cursor_Left				0xFE2C
#define	XK_ISO_Fast_Cursor_Right			0xFE2D
#define	XK_ISO_Fast_Cursor_Up				0xFE2E
#define	XK_ISO_Fast_Cursor_Down				0xFE2F
#define	XK_ISO_Continuous_Underline			0xFE30
#define	XK_ISO_Discontinuous_Underline			0xFE31
#define	XK_ISO_Emphasize				0xFE32
#define	XK_ISO_Center_Object				0xFE33
#define	XK_ISO_Enter					0xFE34

#define	XK_dead_grave					0xFE50
#define	XK_dead_acute					0xFE51
#define	XK_dead_circumflex				0xFE52
#define	XK_dead_tilde					0xFE53
#define	XK_dead_macron					0xFE54
#define	XK_dead_breve					0xFE55
#define	XK_dead_abovedot				0xFE56
#define	XK_dead_diaeresis				0xFE57
#define	XK_dead_abovering				0xFE58
#define	XK_dead_doubleacute				0xFE59
#define	XK_dead_caron					0xFE5A
#define	XK_dead_cedilla					0xFE5B
#define	XK_dead_ogonek					0xFE5C
#define	XK_dead_iota					0xFE5D
#define	XK_dead_voiced_sound				0xFE5E
#define	XK_dead_semivoiced_sound			0xFE5F
#define	XK_dead_belowdot				0xFE60
#define XK_dead_hook					0xFE61
#define XK_dead_horn					0xFE62

#define	XK_First_Virtual_Screen				0xFED0
#define	XK_Prev_Virtual_Screen				0xFED1
#define	XK_Next_Virtual_Screen				0xFED2
#define	XK_Last_Virtual_Screen				0xFED4
#define	XK_Terminate_Server				0xFED5

#define	XK_AccessX_Enable				0xFE70
#define	XK_AccessX_Feedback_Enable			0xFE71
#define	XK_RepeatKeys_Enable				0xFE72
#define	XK_SlowKeys_Enable				0xFE73
#define	XK_BounceKeys_Enable				0xFE74
#define	XK_StickyKeys_Enable				0xFE75
#define	XK_MouseKeys_Enable				0xFE76
#define	XK_MouseKeys_Accel_Enable			0xFE77
#define	XK_Overlay1_Enable				0xFE78
#define	XK_Overlay2_Enable				0xFE79
#define	XK_AudibleBell_Enable				0xFE7A

#define	XK_Pointer_Left					0xFEE0
#define	XK_Pointer_Right				0xFEE1
#define	XK_Pointer_Up					0xFEE2
#define	XK_Pointer_Down					0xFEE3
#define	XK_Pointer_UpLeft				0xFEE4
#define	XK_Pointer_UpRight				0xFEE5
#define	XK_Pointer_DownLeft				0xFEE6
#define	XK_Pointer_DownRight				0xFEE7
#define	XK_Pointer_Button_Dflt				0xFEE8
#define	XK_Pointer_Button1				0xFEE9
#define	XK_Pointer_Button2				0xFEEA
#define	XK_Pointer_Button3				0xFEEB
#define	XK_Pointer_Button4				0xFEEC
#define	XK_Pointer_Button5				0xFEED
#define	XK_Pointer_DblClick_Dflt			0xFEEE
#define	XK_Pointer_DblClick1				0xFEEF
#define	XK_Pointer_DblClick2				0xFEF0
#define	XK_Pointer_DblClick3				0xFEF1
#define	XK_Pointer_DblClick4				0xFEF2
#define	XK_Pointer_DblClick5				0xFEF3
#define	XK_Pointer_Drag_Dflt				0xFEF4
#define	XK_Pointer_Drag1				0xFEF5
#define	XK_Pointer_Drag2				0xFEF6
#define	XK_Pointer_Drag3				0xFEF7
#define	XK_Pointer_Drag4				0xFEF8
#define	XK_Pointer_Drag5				0xFEFD

#define	XK_Pointer_EnableKeys				0xFEF9
#define	XK_Pointer_Accelerate				0xFEFA
#define	XK_Pointer_DfltBtnNext				0xFEFB
#define	XK_Pointer_DfltBtnPrev				0xFEFC


/*
 *  Latin 1
 *  Byte 3 = 0
 * /
#define XK_space               0x020
#define XK_exclam              0x021
#define XK_quotedbl            0x022
#define XK_numbersign          0x023
#define XK_dollar              0x024
#define XK_percent             0x025
#define XK_ampersand           0x026
#define XK_apostrophe          0x027
#define XK_quoteright          0x027	/* deprecated * /
#define XK_parenleft           0x028
#define XK_parenright          0x029
#define XK_asterisk            0x02a
#define XK_plus                0x02b
#define XK_comma               0x02c
#define XK_minus               0x02d
#define XK_period              0x02e
#define XK_slash               0x02f
#define XK_0                   0x030
#define XK_1                   0x031
#define XK_2                   0x032
#define XK_3                   0x033
#define XK_4                   0x034
#define XK_5                   0x035
#define XK_6                   0x036
#define XK_7                   0x037
#define XK_8                   0x038
#define XK_9                   0x039
#define XK_colon               0x03a
#define XK_semicolon           0x03b
#define XK_less                0x03c
#define XK_equal               0x03d
#define XK_greater             0x03e
#define XK_question            0x03f
#define XK_at                  0x040
#define XK_A                   0x041
#define XK_B                   0x042
#define XK_C                   0x043
#define XK_D                   0x044
#define XK_E                   0x045
#define XK_F                   0x046
#define XK_G                   0x047
#define XK_H                   0x048
#define XK_I                   0x049
#define XK_J                   0x04a
#define XK_K                   0x04b
#define XK_L                   0x04c
#define XK_M                   0x04d
#define XK_N                   0x04e
#define XK_O                   0x04f
#define XK_P                   0x050
#define XK_Q                   0x051
#define XK_R                   0x052
#define XK_S                   0x053
#define XK_T                   0x054
#define XK_U                   0x055
#define XK_V                   0x056
#define XK_W                   0x057
#define XK_X                   0x058
#define XK_Y                   0x059
#define XK_Z                   0x05a
#define XK_bracketleft         0x05b
#define XK_backslash           0x05c
#define XK_bracketright        0x05d
#define XK_asciicircum         0x05e
#define XK_underscore          0x05f
#define XK_grave               0x060
#define XK_quoteleft           0x060	/* deprecated * /
#define XK_a                   0x061
#define XK_b                   0x062
#define XK_c                   0x063
#define XK_d                   0x064
#define XK_e                   0x065
#define XK_f                   0x066
#define XK_g                   0x067
#define XK_h                   0x068
#define XK_i                   0x069
#define XK_j                   0x06a
#define XK_k                   0x06b
#define XK_l                   0x06c
#define XK_m                   0x06d
#define XK_n                   0x06e
#define XK_o                   0x06f
#define XK_p                   0x070
#define XK_q                   0x071
#define XK_r                   0x072
#define XK_s                   0x073
#define XK_t                   0x074
#define XK_u                   0x075
#define XK_v                   0x076
#define XK_w                   0x077
#define XK_x                   0x078
#define XK_y                   0x079
#define XK_z                   0x07a
#define XK_braceleft           0x07b
#define XK_bar                 0x07c
#define XK_braceright          0x07d
#define XK_asciitilde          0x07e

#define XK_nobreakspace        0x0a0
#define XK_exclamdown          0x0a1
#define XK_cent        	       0x0a2
#define XK_sterling            0x0a3
#define XK_currency            0x0a4
#define XK_yen                 0x0a5
#define XK_brokenbar           0x0a6
#define XK_section             0x0a7
#define XK_diaeresis           0x0a8
#define XK_copyright           0x0a9
#define XK_ordfeminine         0x0aa
#define XK_guillemotleft       0x0ab	/* left angle quotation mark * /
#define XK_notsign             0x0ac
#define XK_hyphen              0x0ad
#define XK_registered          0x0ae
#define XK_macron              0x0af
#define XK_degree              0x0b0
#define XK_plusminus           0x0b1
#define XK_twosuperior         0x0b2
#define XK_threesuperior       0x0b3
#define XK_acute               0x0b4
#define XK_mu                  0x0b5
#define XK_paragraph           0x0b6
#define XK_periodcentered      0x0b7
#define XK_cedilla             0x0b8
#define XK_onesuperior         0x0b9
#define XK_masculine           0x0ba
#define XK_guillemotright      0x0bb	/* right angle quotation mark * /
#define XK_onequarter          0x0bc
#define XK_onehalf             0x0bd
#define XK_threequarters       0x0be
#define XK_questiondown        0x0bf
#define XK_Agrave              0x0c0
#define XK_Aacute              0x0c1
#define XK_Acircumflex         0x0c2
#define XK_Atilde              0x0c3
#define XK_Adiaeresis          0x0c4
#define XK_Aring               0x0c5
#define XK_AE                  0x0c6
#define XK_Ccedilla            0x0c7
#define XK_Egrave              0x0c8
#define XK_Eacute              0x0c9
#define XK_Ecircumflex         0x0ca
#define XK_Ediaeresis          0x0cb
#define XK_Igrave              0x0cc
#define XK_Iacute              0x0cd
#define XK_Icircumflex         0x0ce
#define XK_Idiaeresis          0x0cf
#define XK_ETH                 0x0d0
#define XK_Ntilde              0x0d1
#define XK_Ograve              0x0d2
#define XK_Oacute              0x0d3
#define XK_Ocircumflex         0x0d4
#define XK_Otilde              0x0d5
#define XK_Odiaeresis          0x0d6
#define XK_multiply            0x0d7
#define XK_Ooblique            0x0d8
#define XK_Oslash              XK_Ooblique
#define XK_Ugrave              0x0d9
#define XK_Uacute              0x0da
#define XK_Ucircumflex         0x0db
#define XK_Udiaeresis          0x0dc
#define XK_Yacute              0x0dd
#define XK_THORN               0x0de
#define XK_ssharp              0x0df
#define XK_agrave              0x0e0
#define XK_aacute              0x0e1
#define XK_acircumflex         0x0e2
#define XK_atilde              0x0e3
#define XK_adiaeresis          0x0e4
#define XK_aring               0x0e5
#define XK_ae                  0x0e6
#define XK_ccedilla            0x0e7
#define XK_egrave              0x0e8
#define XK_eacute              0x0e9
#define XK_ecircumflex         0x0ea
#define XK_ediaeresis          0x0eb
#define XK_igrave              0x0ec
#define XK_iacute              0x0ed
#define XK_icircumflex         0x0ee
#define XK_idiaeresis          0x0ef
#define XK_eth                 0x0f0
#define XK_ntilde              0x0f1
#define XK_ograve              0x0f2
#define XK_oacute              0x0f3
#define XK_ocircumflex         0x0f4
#define XK_otilde              0x0f5
#define XK_odiaeresis          0x0f6
#define XK_division            0x0f7
#define XK_oslash              0x0f8
#define XK_ugrave              0x0f9
#define XK_uacute              0x0fa
#define XK_ucircumflex         0x0fb
#define XK_udiaeresis          0x0fc
#define XK_yacute              0x0fd
#define XK_thorn               0x0fe
#define XK_ydiaeresis          0x0ff

*/
