Appendix ( ÉÕÏ¿ )

¡û ¤Ï¤¸¤á¤Ë

¤³¤³¤Þ¤Ç¤Î LECTURE ¤Ç¾Ò²ð¤·¤Ê¤«¤Ã¤¿ÏÃÂê¤Ë¤Ä¤¤¤Æ¡¢¤¤¤¯¤Ä¤«¾Ò²ð¤·¤Æ¤¤¤Þ¤¹¡£¤Á¤ç¤Ã¤È¤·¤¿¥á¥âÂå¤ï¤ê¤È¤·¤ÆÆɤó¤Ç¤ß¤Æ²¼¤µ¤¤¡£

¡û select() ¤Ë¤è¤ë¿½ÅÆþÎϽèÍý

Ê£¿ô¤Î¥½¥±¥Ã¥È¤ä¥Ç¥Ð¥¤¥¹¤«¤é¤ÎÆþÎϤòÊÂÎó¤Ë°·¤¤¤¿¤¤¾ì¹ç¡¢Unix ¤Ç¤Ï select() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤¬¤è¤¯»È¤ï¤ì¤Þ¤¹¡£select() ¥·¥¹¥Æ¥à¥³¡¼¥ë¤ò»È¤Ã¤Æ³Æ¥Õ¥¡¥¤¥ëµ­½Ò»Ò¤ò¥Á¥§¥Ã¥¯¤·¡¢Æɤ߹þ¤ß²Äǽ¤ÈȽÃǤµ¤ì¤¿¥Õ¥¡¥¤¥ëµ­½Ò»Ò¤Ë¤Ä¤¤¤Æ½ç¼¡Æɤ߹þ¤ß½èÍý¤ò¹Ô¤¨¤Ð¡¢Ê£¿ô¤ÎÆþÎϤòÊÂÎó¤Ë½èÍý¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

¤³¤ì¤Ï Xlib ¤ò»È¤Ã¤¿ GUI ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó¤Ë¤ª¤¤¤Æ¤âƱÍͤǡ¢X ¥µ¡¼¥Ð¤È¤ÎÄÌ¿®¤Ë»ÈÍѤ·¤Æ¤¤¤ë¥Õ¥¡¥¤¥ëµ­½Ò»Ò ( ¥½¥±¥Ã¥È )¤ò¼èÆÀ¤·¤¿¾å¤ÇÄ̾ï¤Î select() ½èÍý¤ÈƱÍͤ˽èÍý¤¹¤ë¤³¤È¤Ç¼Â¸½¤Ç¤­¤Þ¤¹¡£

X ¥µ¡¼¥Ð¤È¤ÎÄÌ¿®¤ËÍѤ¤¤Æ¤¤¤ë¥Õ¥¡¥¤¥ëµ­½Ò»Ò¤ò¼èÆÀ¤¹¤ë¤Ë¤Ï¡¢ConnectionNumber() ¥Ç¥£¥¹¥×¥ì¥¤¥Þ¥¯¥í¤ò»È¤¤¤Þ¤¹¡£

int fd; /* ¥Õ¥¡¥¤¥ëµ­½Ò»Ò */

fd = ConnectionNumber( dpy );

¥Õ¥¡¥¤¥ëµ­½Ò»Ò¤ò¼èÆÀ¤·¤¿¸å¤ÏÄ̾ï¤Î select() ½èÍý¤ò¹Ô¤¤¡¢ X ¥µ¡¼¥Ð¤«¤é¤ÎÆþÎϤ¬¤¢¤Ã¤¿¾ì¹ç¤Ë¤Ï X¤Î¥¤¥Ù¥ó¥È½èÍý¤ò¹Ô¤¤¡¢¤½¤Î¾¤ÎÆþÎÏ ( ¥Í¥Ã¥È¥ï¡¼¥¯±Û¤·¤Î¥¯¥é¥¤¥¢¥ó¥È¤«¤é¤Î¥¢¥¯¥»¥¹¤Ê¤É ) ¤¬¤¢¤Ã¤¿¾ì¹ç¤Ï¤½¤ì¤é¤ÎÆþÎϼõ¿®½èÍý¤ò¹Ô¤¤¤Þ¤¹¡£

--- ¥³¡¼¥ÉÎã ---

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int    fd_x; /* X ¥µ¡¼¥Ð¤È¤ÎÄÌ¿® */
int    fd_o; /* ¤½¤Î¾¤ÎÆþÎÏ */
fd_set set;  /* select() ¤Ç´Æ»ë¤¹¤ë¥Õ¥¡¥¤¥ëµ­½Ò»Ò¤òÀßÄê */

/* X ¥µ¡¼¥Ð¤È¤ÎÄÌ¿®ÍÑ¥½¥±¥Ã¥È¤ò¼èÆÀ */
fd_x = ConnectionNumber( dpy );

while( 1 ) {
  /* ´Æ»ëÂÐ¾Ý¤Ë fd_x ¤È fd_o ¤òÀßÄê */
  FD_ZERO( &set );
  FD_SET( fd_x, &set );
  FD_SET( fd_o, &set );

  /* fd_x > fd_o ¤È²¾Äꤹ¤ë¡£°ú¿ô¤Ï select() ¤Î¥Þ¥Ë¥å¥¢¥ë»²¾È¤Î¤³¤È */
  select( fd_x + 1, &set, NULL, NULL, NULL ); /* 

  if ( FD_ISSET( fd_x, &set ) ) {

    /* X ¥µ¡¼¥Ð¤«¤é¤ÎÆþÎÏÍ­¤ê */
    while( XPending( dpy ) ) {
       XNextEvent( dpy, &event );

       /* Ä̾ï¤Î¥¤¥Ù¥ó¥È½èÍý */
    }
  }

  if ( FD_ISSET( fd_o, &set ) ) {
    /* ¥Í¥Ã¥È¥ï¡¼¥¯¥¯¥é¥¤¥¢¥ó¥È¤«¤é¤ÎÆþÎÏÍ­¤ê */

  }
}

¡û ¥ê¥½¡¼¥¹¥Õ¥¡¥¤¥ë( $HOME/.Xdefaults ) ¤Î´Êñ¤Ê»ÈÍÑË¡

¥ê¥½¡¼¥¹¥Õ¥¡¥¤¥ë( $HOME/.Xdefaults )¤Ëµ­½Ò¤µ¤ì¤¿¥¯¥é¥¤¥¢¥ó¥ÈÍѤΥ꥽¡¼¥¹Ãͤò´Êñ¤ËÆɤ߹þ¤à¤¿¤á¤Ë¡¢XGetDefault() ¤È¤¤¤¦´Ø¿ô¤¬ÍÑ°Õ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

char* XGetDefault( Display* dpy, char* program, char* option );

program : ¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾
option  : ¥ê¥½¡¼¥¹Ì¾

program ¤Ë¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾ ( ¥¯¥é¥¤¥¢¥ó¥È̾¡¢Ä̾ï¤Ï¥³¥Þ¥ó¥É̾ argv[0] ¤Ç¤è¤¤ ) ¤ò»ØÄꤷ¤Þ¤¹¡£option ¤ÏÃͤòÆɤ߹þ¤ß¤¿¤¤¥ê¥½¡¼¥¹Ì¾¤Ç¤¹¡£¤³¤Î´Ø¿ô¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç¡¢$HOME/.Xdefaults ¥Õ¥¡¥¤¥ë¤Ë "¥¢¥×¥ê¥±¡¼¥·¥ç¥ó̾"*"¥ê¥½¡¼¥¹Ì¾" : ¥ê¥½¡¼¥¹ÃÍ ¤Î·Á¼°¤Çµ­½Ò¤µ¤ì¤Æ¤¤¤ë¥ê¥½¡¼¥¹¤Î¥ê¥½¡¼¥¹Ãͤ¬Ê¸»úÎó¤ÇÊ֤äƤ­¤Þ¤¹¡£Ãͤ¬»ØÄꤵ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤Ï NULL ¤¬ÊÖ¤µ¤ì¤Þ¤¹¡£¤Ê¤ª¡¢¥ê¥½¡¼¥¹¤Î´ÉÍý¤Ë xrdb ¤ò»È¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ï¥ê¥½¡¼¥¹¥Ç¡¼¥¿¥Ù¡¼¥¹¤Î¥ê¥½¡¼¥¹¤òÆɤߤˤ¤¤­¤Þ¤¹¡£

¤Á¤Ê¤ß¤Ë¡¢¤³¤Î´Ø¿ô¤Ï X ¥¦¥£¥ó¥É¥¦¥·¥¹¥Æ¥à¤Î¥ê¥ê¡¼¥¹ 5 °Ê¹ß¤Ç¤Ï¿ä¾©¤µ¤ì¤Æ¤¤¤Ê¤¤¤è¤¦¤Ç¤¹ ( ¥ê¥½¡¼¥¹¥Õ¥¡¥¤¥ë¤ä¥×¥í¥Ñ¥Æ¥£¤òÁ´¤ÆÆɤߤ³¤à¤ï¤±¤Ç¤Ï¤Ê¤¤¤«¤é )¡£¤¬¡¢xrdb ¤ò»È¤ï¤º $HOME/.Xdefaults Æâ¤Ç¤Á¤ç¤³¤Á¤ç¤³¤Ã¤È¥ê¥½¡¼¥¹¤ò°·¤¤¤¿¤¤»þ¤Ë¤Ï½¼Ê¬»È¤¨¤ë¤Î¤Ç¤Ï¤Ê¤¤¤«¤È»×¤Ã¤Æ¤¤¤Þ¤¹¡£Àµ¼°¤Ê¥ê¥½¡¼¥¹´ÉÍýÍѤˤϡ¢Äã¥ê¥½¡¼¥¹¥Þ¥Í¡¼¥¸¥ã´Ø¿ô¤È¸Æ¤Ð¤ì¤ë´Ø¿ô·²¤ò»ÈÍѤ·¤Æ¤¯¤À¤µ¤¤ ( ËͤϤޤÀÊÙ¶¯¤·¤Æ¤Þ¤»¤ó¡£¡£¡£)¡£

¡û ¥Ó¥Ã¥È¥Þ¥Ã¥×¥Ç¡¼¥¿¤«¤é¥Ô¥¯¥¹¥Þ¥Ã¥×¤òºîÀ®

XCreatePixmapFromBitmapData() ¤ò»È¤¦¤È¡¢bitmap ¥¯¥é¥¤¥¢¥ó¥È¤Ê¤É¤ÇºîÀ®¤·¤¿ X ¥Ó¥Ã¥È¥Þ¥Ã¥×¥Ç¡¼¥¿¤«¤é¥Ô¥¯¥¹¥Þ¥Ã¥×¤òºîÀ®¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£

Pixmap XCreatePixmapFromBitmapData( Display*      dpy, 
                                    Drawable      d,
                                    char*         data, 
                                    unsigned int  width, 
                                    unsigned int  height, 
                                    unsigned long fg, 
                                    unsigned long bg, 
                                    unsigned int  depth 
                                  );

data          : ¥Ó¥Ã¥È¥Þ¥Ã¥×¥Ç¡¼¥¿
width, height : data¤ÎÉý¤È¹â¤µ
fg            : ¥Õ¥©¥¢¥°¥é¥¦¥ó¥É¥«¥é¡¼
bg            : ¥Ð¥Ã¥¯¥°¥é¥¦¥ó¥É¥«¥é¡¼
depth         : À¸À®¤¹¤ë¥Ô¥¯¥¹¥Þ¥Ã¥×¤Î¿¼¤µ

data ¤Ç»ØÄꤷ¤¿ X ¥Ó¥Ã¥È¥Þ¥Ã¥×¥Ç¡¼¥¿¤«¤é¡¢Éý width¡¢¹â¤µ height¡¢¿¼¤µ depth ¤Î¥Ô¥¯¥¹¥Þ¥Ã¥×¤òÀ¸À®¤·¤Þ¤¹¡£À¸À®¤µ¤ì¤¿¥Ô¥¯¥¹¥Þ¥Ã¥×¤Ï¥Ó¥Ã¥È¥Þ¥Ã¥×¤Î 1 ¤ÎÉôʬ¤¬ fg¡¢0 ¤ÎÉôʬ¤¬ bg ¤ÇÅɤé¤ì¤Þ¤¹¡£

¤Ê¤ª¥Ó¥Ã¥È¥Þ¥Ã¥×¥Ç¡¼¥¿¤«¤é¿¼¤µ 1 ¤Î¥Ô¥¯¥¹¥Þ¥Ã¥×¡¢¤¹¤Ê¤ï¤Á¥Ó¥Ã¥È¥Þ¥Ã¥×¤òÀ¸À®¤¹¤ë´Ø¿ô¤È¤·¤Æ¡¢XCreateBitmapFromData() ¤¬¤¢¤ê¤Þ¤¹¡£

Pixmap XCreateBitmapFromData( Display*     dpy, 
                              Drawable     d, 
                              char*        data, 
                              unsigned int width, 
                              unsigned int height 
                            );