Xwindowのプログラミング

いまさらXwindowの勉強をするのは既に古いかもしれません。 まあ、何でも勉強ということで。

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

int main(){
  Window Win;
  Display *Disp;
  int gScreen;
  XEvent Ev;
  
// base GC
  GC Gc;

  Disp = XOpenDisplay( NULL );  /* Xサーバに接続する */
  gScreen = DefaultScreen( Disp );  /* スクリーン設定 */
  
  Win = XCreateSimpleWindow(   /* ウィンドウを開く */
                                Disp,                      /* Xサーバ */
                                RootWindow(Disp, gScreen),   /* 親ウィンドウ */
                                0, 0,                     /* 表示時の左上隅の座標 x, y */
                                 200,  100, /* ウィンドウの幅と高さ */ 
                                1, BlackPixel(Disp, gScreen),/* ボーダーの幅と色 */
                                WhitePixel(Disp, gScreen)    /* ウィンドウの背景色 */
                                );
  XSetStandardProperties( Disp, Win, "Sample", "Sample",
                         None, NULL, 0, NULL);

  /* MAP NOTIFYING */
  XSelectInput( Disp, Win, StructureNotifyMask );
  
  XMapWindow( Disp, Win );  /* ウィンドウのマッピング */
  
  Gc = DefaultGC( Disp, gScreen ); /* グラフィックコンテキストの設定 */
  XSetForeground( Disp, Gc, BlackPixel(Disp, gScreen) ); /* 描画色設定 */
  XSetBackground( Disp, Gc, WhitePixel(Disp, gScreen));

  while(1){
     XNextEvent( Disp, &Ev);
     if( Ev.type == MapNotify ) break;
  }

  XFlush( Disp );

  printf("Hit Enter key to end...");
  getchar(); getchar();
  XFreeGC( Disp, Gc );
  XDestroyWindow( Disp, Win );
  return 0;
}
このソースをコンパイルするときは、
gcc sample.c -o sample -L/usr/X11R6/lib -I/usr/X11R6/include -lX11 
としてください。小さなウインドウが一つ現れましたか?リターンキーを2度押すと 終了します。