Advertisement
Guest User

Untitled

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