/************************************************************************************
libsj
Copyright (c) 2010, Segway-Japan, Ltd.
All rights reserved.

Contact us if you use this software for sell.
If you use this software not for sell, you can use this software under BSD lisence.
See the files LICENSE.TXT and LICENSE-BSD.TXT for more details.                     
************************************************************************************/

#include "string.h"
#include "x11win.h"


void x11win::drawlock()
{
    XLockDisplay(mydisplay);
}

void x11win::drawunlock()
{
    XUnlockDisplay(mydisplay);
}

void x11win::drawimage( char* image_buf, int x, int y, int width, int height )
{
#if 0
    XImage* im = XGetImage(dis, win, 0, 0, 16, 16, 0xFF, ZPixmap);
    printf("bpp %d\n", im->bits_per_pixel);
    printf("depth %d\n", im->depth);
    printf("bytes_per_line %d\n", im->bytes_per_line);
    printf("bitmap_pad %d\n", im->bitmap_pad);
    printf("bitmap_unit %d\n", im->bitmap_unit);
#endif

  XImage image;

  image.format           = ZPixmap;
  image.data             = image_buf;
  image.width            = width;
  image.height           = height;
  image.xoffset          = 0; 
  image.byte_order       = MSBFirst;
  image.bitmap_bit_order = MSBFirst;
  image.bits_per_pixel   = 32;
  image.bytes_per_line   = width*4;
  image.bitmap_unit      = 32;
  image.bitmap_pad       = 32;
  image.depth            = 24;

  XPutImage( mydisplay, mywindow, mygc, &image, 0, 0, x, y, width, height );
}

void x11win::drawimage2( char* image_buf, int sw, int sh, int sx, int sy, int x, int y, int w, int h)
{
  XImage image;

  image.format           = ZPixmap;
  image.data             = image_buf;
  image.width            = sw;
  image.height           = sh;
  image.xoffset          = 0; 
  image.byte_order       = MSBFirst;
  image.bitmap_bit_order = MSBFirst;
  image.bits_per_pixel   = 32;
  image.bytes_per_line   = sw*4;
  image.bitmap_unit      = 32;
  image.bitmap_pad       = 32;
  image.depth            = 24;

  XPutImage( mydisplay, mywindow, mygc, &image, sx, sy, x, y, w, h );
}


void x11win::drawarc(int x, int y, int w, int h)
{
    XDrawArc(mydisplay, mywindow, mygc, x, y, w, h, 0, 360*64);
}

void x11win::drawrect(int x, int y, int w, int h)
{
    XDrawRectangle(mydisplay, mywindow, mygc, x, y, w, h);
}

void x11win::drawrectfill(int x, int y, int w, int h)
{
  XSetForeground(mydisplay, mygc, 0xFFFFFFFF);
  XFillRectangle(mydisplay, mywindow, mygc, x, y, w, h);
  XSetForeground(mydisplay, mygc, 0xFF000000);
}

void x11win::drawline(int x1, int y1, int x2, int y2)
{
    XDrawLine(mydisplay, mywindow, mygc, x1, y1, x2, y2);
}

void x11win::drawstring(int x, int y, char* str)
{
  XDrawString(mydisplay, mywindow, mygc, x, y, str, strlen(str));
}

void x11win::drawfrush()
{
    XFlush(mydisplay);
}






void x11win::start(int argc, char** argv, int x, int y, int w, int h, char* name)
{
    XInitThreads();

    // Ｘサーバに接続する	 
    mydisplay = XOpenDisplay(""); 
    myscreen = DefaultScreen( mydisplay ); 
    // Ｘサーバで使用されている基本色を得る 
    mybackground = WhitePixel( mydisplay, myscreen ); 
    myforeground = BlackPixel( mydisplay, myscreen ); 
    // ウィンドウの位置、大きさを指定 
    myhint.x = x; 
    myhint.y = y; 
    myhint.width = w;
    myhint.height = h; 
    myhint.flags = PPosition | PSize; 
    // ウィンドウを生成 
    mywindow = XCreateSimpleWindow ( mydisplay, 
				     DefaultRootWindow( mydisplay ), 
				     myhint.x, myhint.y, myhint.width, myhint.height, 
				     5, myforeground, mybackground); 

    // 生成したウィンドウの情報をシステムに知らせる 
    XSetStandardProperties( mydisplay, mywindow, name, name, None, argv, argc, &myhint ); 
    // グラフィックを描くための準備	 
    mygc = XCreateGC( mydisplay, mywindow, 0, 0); 
    XSetBackground( mydisplay, mygc, mybackground ); 
    XSetForeground( mydisplay, mygc, myforeground ); 
    // 受け取るイベントの選択 
    XSelectInput( mydisplay, mywindow, ButtonPressMask | ExposureMask ); 
    // ウィンドウを表示 
    XMapRaised( mydisplay, mywindow ); 
}

void x11win::end()
{
    XFreeGC(mydisplay, mygc);		// コンテキスト解放 
    XDestroyWindow(mydisplay, mywindow);	// ウィンドウの破棄 
    XCloseDisplay(mydisplay);		// 接続の解除 
}

void x11win::eventloop()
{
  // イベントの取り出し 
  XNextEvent ( mydisplay, &myevent ); 
  switch ( myevent.type ){ 
    // 再描画要求 
  case Expose: 
    break; 
    // マウスのボタンが押された 
  case ButtonPress: 
    break; 
  }	 
}
