Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. /* ヘッダファイルのインクルード */
  2. #include <stdio.h> /* C標準入出力 */
  3. #include <string.h> /* C文字列処理 */
  4. #include <unistd.h> /* UNIX標準 */
  5. #include <X11/Xlib.h> /* Xlib */
  6. #include <X11/Xutil.h> /* Xユーティリティ */
  7. #include <X11/Xatom.h> /* アトム */
  8.  
  9. /* main関数 */
  10. int main(int argc, char **argv){
  11.  
  12. /* 変数の宣言と初期化. */
  13. Display *d; /* Display構造体へのポインタd. */
  14. Window wr; /* ウィンドウ生成の結果を表す値wr.(Window == XID == unsigned long) */
  15. int result; /* マップの結果result. */
  16. unsigned long white; /* 白のRGB値white. */
  17. XEvent event; /* XEvent構造体(共用体)のevent. */
  18. int i; /* ループ用変数i. */
  19. XTextProperty text_property; /* テキストプロパティtext_property. */
  20. char title[] = "ABCDE"; /* タイトルtitleを"ABCDE"で初期化. */
  21. Atom atom_wm_delete_window; /* WM_DELETE_WINDOWのアトムatom_wm_delete_window. */
  22. Atom atom_wm_protocols; /* WM_PROTOCOLSのアトムatom_wm_protocols. */
  23.  
  24. /* Xサーバとの接続. */
  25. d = XOpenDisplay(NULL); /* XOpenDisplayでXサーバに接続し, 戻り値のアドレスをdに格納. */
  26.  
  27. /* dを出力. */
  28. printf("d = %08x\n", d); /* dの値を16進数で出力. */
  29.  
  30. /* 白のRGB値を取得. */
  31. white = XWhitePixel(d, 0); /* XWhitePixelでスクリーン0における白のRGB値を取得し, whiteに格納. */
  32.  
  33. /* ウィンドウの生成. */
  34. wr = XCreateSimpleWindow(d, DefaultRootWindow(d), 100, 100, 640, 480, 1, white, white); /* XCreateSimpleWindowでウィンドウ生成し, 結果はwrに格納.(DefaultRootWindowでルートウィンドウを取得.) */
  35.  
  36. /* ウィンドウ生成の結果を出力. */
  37. printf("wr = %08x\n", wr); /* wrを出力. */
  38.  
  39. /* ウィンドウのマッピング(表示要求) */
  40. result = XMapWindow(d, wr); /* XMapWindowでマッピング. */
  41.  
  42. /* マッピング結果を出力. */
  43. printf("result = %d\n", result); /* resultの値を出力. */
  44.  
  45. /* テキストプロパティのセット. */
  46. text_property.value = (unsigned char *)title; /* タイトル文字列を指定. */
  47. text_property.encoding = XA_STRING; /* XA_STRINGを指定. */
  48. text_property.format = 8; /* 半角英数の場合8ビットなので8を指定. */
  49. text_property.nitems = strlen(title); /* titleの文字数を指定. */
  50. XSetWMProperties(d, wr, &text_property, NULL, argv, argc, NULL, NULL, NULL); /* XSetWMPropertiesでウィンドウマネージャにtext_propertyをセット. */
  51.  
  52. /* WM_PROTOCOLSにWM_DELETE_WINDOWをセット. */
  53. atom_wm_delete_window = XInternAtom(d, "WM_DELETE_WINDOW", False); /* XInternAtomで"WM_DELETE_WINDOW"のアトムを取得. */
  54. XSetWMProtocols(d, wr, &atom_wm_delete_window, 1); /* XSetWMProtocolsでatom_wm_delete_windowをセット. */
  55.  
  56. /* WM_PROTOCOLSのアトムを取得. */
  57. atom_wm_protocols = XInternAtom(d, "WM_PROTOCOLS", False); /* XInternAtomでWM_PROTOCOLSのアトムを取得. */
  58.  
  59. /* イベントマスクのセット. */
  60. XSelectInput(d, wr, ButtonPressMask | ButtonReleaseMask | StructureNotifyMask); /* XSelectInputでButtonPressMask, ButtonReleaseMask(マウスボタンを離された時のマスク.), StructureNotifyMask(通知マスク)をセット. */
  61.  
  62. /* 表示要求イベントをフラッシュ. */
  63. XFlush(d); /* XFlushでフラッシュ. */
  64.  
  65. /* iの初期化. */
  66. i = 0; /* iを0にしておく. */
  67.  
  68. /* イベントループ. */
  69. while (1){
  70.  
  71. /* イベントの取得. */
  72. XNextEvent(d, &event); /* XNextEventでeventを取得. */
  73.  
  74. /* イベントタイプごとに処理. */
  75. switch (event.type){ /* event.typeの値で分岐. */
  76.  
  77. /* ButtonPress */
  78. case ButtonPress: /* マウスボタンが押された時. */
  79.  
  80. /* ButtonPressブロック. */
  81. {
  82.  
  83. /* マウス位置の出力. */
  84. printf("(%d, %d)\n", event.xbutton.x, event.xbutton.y); /* event.xbutton.xとevent.xbutton.yを出力. */
  85. i++; /* iをインクリメント. */
  86. if (i == 10){ /* iが10の時. */
  87.  
  88. /* Xサーバとの接続を終了する. */
  89. XCloseDisplay(d); /* XCloseDisplayで切断する. */
  90.  
  91. /* プログラムの終了 */
  92. return 0; /* 0を返して正常終了. */
  93.  
  94. }
  95.  
  96. }
  97.  
  98. /* break. */
  99. break; /* breakで終わる. */
  100.  
  101. /* ButtonRelease */
  102. case ButtonRelease: /* マウスボタンが離された時. */
  103.  
  104. /* ButtonReleaseブロック. */
  105. {
  106.  
  107. /* "ButtonRelease!!". */
  108. printf("ButtonRelease!!\n"); /* "ButtonRelease!!"と出力. */
  109.  
  110. }
  111.  
  112. /* break. */
  113. break; /* breakで終わる. */
  114.  
  115. /* ClientMessage */
  116. case ClientMessage: /* クライアントメッセージ */
  117.  
  118. /* ClientMessageブロック. */
  119. {
  120.  
  121. /* WM_PROTOCOLSの場合. */
  122. if (event.xclient.message_type == atom_wm_protocols){ /* atom_wm_protocolsなら. */
  123.  
  124. /* "WM_PROTOCOLS!" */
  125. printf("WM_PROTOCOLS!\n"); /* "WM_PROTOCOLS!"と出力. */
  126.  
  127. /* WM_DELETE_WINDOWなら終了. */
  128. if (event.xclient.data.l[0] == atom_wm_delete_window){ /* atom_wm_delete_windowなら. */
  129.  
  130. /* "WM_DELETE_WINDOW!!" */
  131. printf("WM_DELETE_WINDOW!!\n"); /* "WM_DELETE_WINDOW!!"と出力. */
  132.  
  133. /* ウィンドウを破棄. */
  134. XDestroyWindow(d, wr); /* XDestroyWindowでウィンドウを破棄. */
  135.  
  136. }
  137.  
  138. }
  139.  
  140. }
  141.  
  142. /* break. */
  143. break; /* breakで終わる. */
  144.  
  145. /* DestroyNotify */
  146. case DestroyNotify: /* ウィンドウ破棄通知. */
  147.  
  148. /* DestroyNotifyブロック. */
  149. {
  150.  
  151. /* "DestroyNotify!" */
  152. printf("DestroyNotify!\n"); /* "DestroyNotify!"と出力. */
  153.  
  154. /* Xサーバとの接続を終了する. */
  155. XCloseDisplay(d); /* XCloseDisplayで切断する. */
  156.  
  157. /* プログラムの終了 */
  158. return 0; /* 0を返して正常終了. */
  159.  
  160. }
  161.  
  162. /* break. */
  163. break; /* breakで終わる. */
  164.  
  165. /* default */
  166. default: /* それ以外. */
  167.  
  168. /* break. */
  169. break; /* breakで終わる. */
  170.  
  171. }
  172.  
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement