Advertisement
Guest User

Untitled

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