Advertisement
Guest User

Untitled

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