typedef unsigned long COLORREF;
typedef unsigned char   __u8;
typedef unsigned short  __u16;
typedef unsigned long   __u32;
typedef int semaphore_t;

#define PaletteMask             0x3C6
#define PaletteRegisterWrite    0x3C8
#define PaletteData             0x3C9

#define GetRValue(rgb)   ((__u8) (rgb))
#define GetGValue(rgb)   ((__u8) (((__u16) (rgb)) >> 8))
#define GetBValue(rgb)   ((__u8) ((rgb) >> 16))
#define RGB(r, g ,b)  ((__u32) (((__u8) (r) |  ((__u16) (g) << 8)) | (((__u32) (__u8) (b)) << 16)))

#define set_color(c) if(c!=active_color){outportw(0x3ce,c<<8);inportb(0x3da);active_color=c;}
#define sort(x,y) {if((x)>(y)) swap((x),(y));}
#define swap(x,y) {unsigned long __t__;__t__=(x);(x)=(y);(y)=__t__;}
#define absolute(x) (((x)<0)?(-(x)):(x))
#define vga_clip(x,y)   if(x<0) \
                                x=0; \
                        if(x>vidmode.maxx-1) \
                                x=vidmode.maxx-1; \
                        if(y<0) \
                                y=0; \
                        if(y>vidmode.maxy-1) \
                                y=vidmode.maxy-1;

struct video_mode {
  semaphore_t iolock;
  __u8 mode;
  __u16 maxx;
  __u16 maxy;
  __u8  bpp;
  void (* VGAInitMode)();
  void (* SetColor)(__u32 nr, COLORREF color);
  COLORREF (* GetDrvColor) (COLORREF color);
  void (* PutPixel)(__u32 x, __u32 y, COLORREF color);
  COLORREF (* GetPixel)(__u32 x, __u32 y);
  void (* SolidLine)(__u32 xp, __u32 yp,__u32 xk,__u32 yk, COLORREF color);
  void (* FillRect)(__u32 xp, __u32 yp,__u32 xk,__u32 yk, COLORREF color);
  void (* CopyScreenToBuf) (__u32 xp, __u32 yp,__u32 xk,__u32 yk, __u8 *buf);
  void (* CopyBufToScreen) (__u32 xp, __u32 yp,__u32 xk,__u32 yk, __u8 *buf);
  };

typedef struct _vga_mode {
 unsigned char attribute[21];
 unsigned char crtc[24];
 unsigned char graphics[9];
 unsigned char misc;
 unsigned char sequencer[5];
} vga_mode;

extern struct video_mode vidmode;

void init_mode_0x11(void);
void drvVGA13SetColor(__u32 nr, COLORREF color);
COLORREF  drvVGetDrvColor (COLORREF color);
static void vga_putpixel(__u32 x,__u32 y,COLORREF color);
static void vga_putline(__u32 x1,__u32 y1,__u32 x2,__u32 y2,COLORREF color);
static COLORREF vga_getpixel(__u32 x,__u32 y);
void defVGAFillRect(__u32 xp, __u32 yp, __u32 xk, __u32 yk, COLORREF color);
static void vga_setregs(vga_mode* mode);