www.mtkn.jp

Manuscripts for my personal webpage.
git clone https://git.mtkn.jp/www.mtkn.jp
Log | Files | Refs | README

commit 49c9c1d7b4a34b50977c08587d20cf910d31f35e
parent 522775b9915efc5243b8b541b718e30c3d7f4908
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Wed, 21 Dec 2022 17:34:22 +0900

add video

Diffstat:
Mdata/weblog | 6++++++
Aman/computer/videos/example1.webm | 0
Aman/computer/xlib_playground1.html | 154+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dman/draft/xlib_playground.html | 150-------------------------------------------------------------------------------
Apub/computer/videos/example1.webm | 0
Apub/computer/xlib_playground1.html | 167+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpub/draft/xlib_playground.html | 163-------------------------------------------------------------------------------
Mpub/rss.xml | 165++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mpub/sitemap.xml | 1+
9 files changed, 491 insertions(+), 315 deletions(-)

diff --git a/data/weblog b/data/weblog @@ -111,3 +111,9 @@ 1669129200 /books/978-4-06-288451-8.html 1669129200 /index.html 1669129200 /books/978-4-06-288451-8.html +1671548400 /computer/xlib_playground1.html +1671548400 /computer/xlib_playground1.html +1671548400 /computer/xlib_playground1.html +1671548400 /computer/xlib_playground1.html +1671548400 /computer/xlib_playground1.html +1671548400 /computer/xlib_playground1.html diff --git a/man/computer/videos/example1.webm b/man/computer/videos/example1.webm Binary files differ. diff --git a/man/computer/xlib_playground1.html b/man/computer/xlib_playground1.html @@ -0,0 +1,154 @@ +<h1>Xlibで遊んでみる</h1> +<time>2022-12-16</time> + +<h2>はじめに</h2> +<p>X11でGUIのプログラミングをしてみようと思い、してみた。\ +X11用の低レベルのライブラリはXlibとxcbの二つがあるようだ。\ +x.orgのウェブページを見てみると、Xlibは古く、xcbに置きかわりつつあるという。\ +そのため、新しくなにかを作る場合はxcbを使うようにとのことである。\ +ところがこのxcbはドキュメンテーションに乏しく、\ +X11を触るのが初めての人間にはなにをどうすればいいのかほとんど分からなかった。\ +知らない関数や構造体やらがでてきても(殆ど全部知らないものだが)、\ +その関数なり構造体なりの説明がどこにも見当たらない。\ +manページもない。あるのはdoxygenなるものでソースコードのコメントから\ +自動生成したいい加減なものだけで、使いものにならない。</p> +<p>とりあえずX11のことを少しは理解してからでないと初められそうもないと思い、\ +もう少しましな情報があるXlibから始めることにした。\ +</p> +<p>言語はC言語である。ソースコードは\ +<a href="https://git.mtkn.jp/xlib_playground/log.html">ここ</a>にある。 + +<h2>初期設定</h2> +<p>ディスプレイを開き、ウィンドウを作成する。\ +変数はとりあえずグローバルに宣言することにした。\ +<code>main</code>関数はできるだけ小さくして実際の処理は\ +それぞれの関数にさせてみる:</p> +<pre><code> +#include &lt;stdio.h&gt; +#include &lt;stdlib.h&gt; +#include &lt;X11/Xlib.h&gt; + +/* macros */ +#define INIT_WIDTH 800 +#define INIT_HEIGHT 600 + +/* variables */ +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; + +void +setup(void) +{ + // Open a display. + if ((display = XOpenDisplay(NULL)) == NULL){ + fprintf(stderr, "ERROR: could not open display\n"); + exit(1); + } + // Create a window. + window = XCreateSimpleWindow( + display, + XDefaultRootWindow(display), + 0, 0, + win_width, win_height, + 0, 0, + 0); + XStoreName(display, window, "UNKO"); + + // Setup a graphical context. + gc = XCreateGC(display, window, 0, NULL); + XSetForeground(display, gc, 0xFFFFFF); + + // Kill the application when the window is destroyed. + wm_delete_window = XInternAtom(display, + "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &wm_delete_window, 1); + + // Setup which input to process. + XSelectInput(display, window, + ExposureMask|KeyPressMask|KeyReleaseMask); + + // Actually draw the window. + XMapWindow(display, window); +} + +void +clean_up(void) +{ + XCloseDisplay(display); +} +</code></pre> + +<p>適当な四角形のものを表示し、その位置を時間の関数として動かしてみる。\ +</p> +<pre><code>#include &lt;time.h&gt; + +int +main(void) +{ + int px, py; + int quit; + struct timespec ts; + XEvent event; + + setup(); + quit = 0; + + while (!quit){ + while(XPending(display) > 0){ + XNextEvent(display, &event); + switch (event.type){ + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)){ + case 'q': + quit = 1; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + quit = 1; + } + } break; + default: + break; + } + } + clock_gettime(CLOCK_MONOTONIC, &ts); + px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XFillRectangle(display, window, gc, + px, py, // position + 100, 100); // width and height + + ts.tv_sec = 0; + ts.tv_nsec = 10 * 1000 * 1000; + nanosleep(&ts, NULL); + } + + cleanup(); + return 0; +} +</code></pre> + +<p>ここまでのコードはgitリポジトリの\ +<a href="https://git.mtkn.jp/xlib_playground/file/example/test.c.html">example/test.c</a>\ +にある。</p> +<p>完成品:</p> +<video controls> + <source src="videos/example1.webm" type="video/webm"> +</video> + +<h2>参考</h2> +<ul> +<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> +<li><a href="https://www.youtube.com/watch?v=764fnfEb1_c">X11 App in C with Xlib(youtube video by tsoding)</a></li> +</ul> diff --git a/man/draft/xlib_playground.html b/man/draft/xlib_playground.html @@ -1,150 +0,0 @@ -<h1>Xlibで遊んでみる</h1> -<time>2022-12-16</time> - -<h2>はじめに</h2> -<p>X11でGUIのプログラミングをしてみようと思い、してみた。\ -X11用の低レベルのライブラリはXlibとxcbの二つがあるようだ。\ -x.orgのウェブページを見てみると、Xlibは古く、xcbに置きかわりつつあるという。\ -そのため、新しくなにかを作る場合はxcbを使うようにとのことである。\ -ところがこのxcbはドキュメンテーションに乏しく、\ -X11を触るのが初めての人間にはなにをどうすればいいのかほとんど分からなかった。\ -知らない関数や構造体やらがでてきても(殆ど全部知らないものだが)、\ -その関数なり構造体なりの説明がどこにも見当たらない。\ -manページもない。あるのはdoxygenなるものでソースコードのコメントから\ -自動生成したいい加減なものだけで、使いものにならない。</p> -<p>とりあえずX11のことを少しは理解してからでないと初められそうもないと思い、\ -もう少しましな情報があるXlibから始めることにした。\ -</p> -<p>言語はC言語である。ソースコードは\ -<a href="https://git.mtkn.jp/xlib_playground/log.html">ここ</a>にある。 - -<h2>初期設定</h2> -<p>ディスプレイを開き、ウィンドウを作成する。\ -変数はとりあえずグローバルに宣言することにした。\ -<code>main</code>関数はできるだけ小さくして実際の処理は\ -それぞれの関数にさせてみる:</p> -<pre><code> -#include &lt;stdio.h&gt; -#include &lt;stdlib.h&gt; -#include &lt;X11/Xlib.h&gt; - -/* macros */ -#define INIT_WIDTH 800 -#define INIT_HEIGHT 600 - -/* variables */ -Display *display; -Window window; -unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; -GC gc; -Atom wm_delete_window; - -void -setup(void) -{ - // Open a display. - if ((display = XOpenDisplay(NULL)) == NULL){ - fprintf(stderr, "ERROR: could not open display\n"); - exit(1); - } - // Create a window. - window = XCreateSimpleWindow( - display, - XDefaultRootWindow(display), - 0, 0, - win_width, win_height, - 0, 0, - 0); - XStoreName(display, window, "UNKO"); - - // Setup a graphical context. - gc = XCreateGC(display, window, 0, NULL); - XSetForeground(display, gc, 0xFFFFFF); - - // Kill the application when the window is destroyed. - wm_delete_window = XInternAtom(display, - "WM_DELETE_WINDOW", False); - XSetWMProtocols(display, window, &wm_delete_window, 1); - - // Setup which input to process. - XSelectInput(display, window, - ExposureMask|KeyPressMask|KeyReleaseMask); - - // Actually draw the window. - XMapWindow(display, window); -} - -void -clean_up(void) -{ - XCloseDisplay(display); -} -</code></pre> - -<p>適当な四角形のものを表示し、その位置を時間の関数として動かしてみる。\ -</p> -<pre><code>#include &lt;time.h&gt; - -int -main(void) -{ - int px, py; - int quit; - struct timespec ts; - XEvent event; - - setup(); - quit = 0; - - while (!quit){ - while(XPending(display) > 0){ - XNextEvent(display, &event); - switch (event.type){ - case KeyPress: { - switch (XLookupKeysym(&event.xkey, 0)){ - case 'q': - quit = 1; - break; - default: - break; - } - } break; - case ClientMessage: { - if ((Atom) event.xclient.data.l[0] == wm_delete_window) { - quit = 1; - } - } break; - default: - break; - } - } - clock_gettime(CLOCK_MONOTONIC, &ts); - px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); - py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); - XClearArea(display, window, - 0, 0, // position - win_width, win_height, // width and height - False); - XFillRectangle(display, window, gc, - px, py, // position - 100, 100); // width and height - - ts.tv_sec = 0; - ts.tv_nsec = 10 * 1000 * 1000; - nanosleep(&ts, NULL); - } - - cleanup(); - return 0; -} -</code></pre> - -<p>ここまでのコードはgitリポジトリの\ -<a href="https://git.mtkn.jp/xlib_playground/file/example/test.c.html">example/test.c</a>\ -にある。</p> - -<h2>参考</h2> -<ul> -<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> -<li><a href="https://www.youtube.com/watch?v=764fnfEb1_c">X11 App in C with Xlib(youtube video by tsoding)</a></li> -</ul> diff --git a/pub/computer/videos/example1.webm b/pub/computer/videos/example1.webm Binary files differ. diff --git a/pub/computer/xlib_playground1.html b/pub/computer/xlib_playground1.html @@ -0,0 +1,167 @@ +<!DOCTYPE html> +<html lang="ja"> +<head> + <meta charset="utf-8" /> + <meta name="viewport" content="width=device-width,initial-scale=1" /> + <link rel="stylesheet" type="text/css" href="/style.css" /> + <link rel="icon" type="image/x-icon" href="/pics/favicon.ico" /> + <title>Xlibで遊んでみる</title> +</head> +<body> + <header> + <a href="/">主頁</a> | + <a href="/about.html">自己紹介</a> | + <a href="/journal">日記</a> | + <a href="/farm">農業</a> | + <a href="/kitchen">台所</a> | + <a href="/computer">コンピュータ</a> | + <a href="/poetry">詩</a> | + <a href="/books">本棚</a> | + <a href="https://git.mtkn.jp">Git</a> + </header> + <main> + <article> +<h1>Xlibで遊んでみる</h1> +<time>2022-12-16</time> + +<h2>はじめに</h2> +<p>X11でGUIのプログラミングをしてみようと思い、してみた。X11用の低レベルのライブラリはXlibとxcbの二つがあるようだ。x.orgのウェブページを見てみると、Xlibは古く、xcbに置きかわりつつあるという。そのため、新しくなにかを作る場合はxcbを使うようにとのことである。ところがこのxcbはドキュメンテーションに乏しく、X11を触るのが初めての人間にはなにをどうすればいいのかほとんど分からなかった。知らない関数や構造体やらがでてきても(殆ど全部知らないものだが)、その関数なり構造体なりの説明がどこにも見当たらない。manページもない。あるのはdoxygenなるものでソースコードのコメントから自動生成したいい加減なものだけで、使いものにならない。</p> +<p>とりあえずX11のことを少しは理解してからでないと初められそうもないと思い、もう少しましな情報があるXlibから始めることにした。</p> +<p>言語はC言語である。ソースコードは<a href="https://git.mtkn.jp/xlib_playground/log.html">ここ</a>にある。 + +<h2>初期設定</h2> +<p>ディスプレイを開き、ウィンドウを作成する。変数はとりあえずグローバルに宣言することにした。<code>main</code>関数はできるだけ小さくして実際の処理はそれぞれの関数にさせてみる:</p> +<pre><code> +#include &lt;stdio.h&gt; +#include &lt;stdlib.h&gt; +#include &lt;X11/Xlib.h&gt; + +/* macros */ +#define INIT_WIDTH 800 +#define INIT_HEIGHT 600 + +/* variables */ +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; + +void +setup(void) +{ + // Open a display. + if ((display = XOpenDisplay(NULL)) == NULL){ + fprintf(stderr, "ERROR: could not open display\n"); + exit(1); + } + // Create a window. + window = XCreateSimpleWindow( + display, + XDefaultRootWindow(display), + 0, 0, + win_width, win_height, + 0, 0, + 0); + XStoreName(display, window, "UNKO"); + + // Setup a graphical context. + gc = XCreateGC(display, window, 0, NULL); + XSetForeground(display, gc, 0xFFFFFF); + + // Kill the application when the window is destroyed. + wm_delete_window = XInternAtom(display, + "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &wm_delete_window, 1); + + // Setup which input to process. + XSelectInput(display, window, + ExposureMask|KeyPressMask|KeyReleaseMask); + + // Actually draw the window. + XMapWindow(display, window); +} + +void +clean_up(void) +{ + XCloseDisplay(display); +} +</code></pre> + +<p>適当な四角形のものを表示し、その位置を時間の関数として動かしてみる。</p> +<pre><code>#include &lt;time.h&gt; + +int +main(void) +{ + int px, py; + int quit; + struct timespec ts; + XEvent event; + + setup(); + quit = 0; + + while (!quit){ + while(XPending(display) > 0){ + XNextEvent(display, &event); + switch (event.type){ + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)){ + case 'q': + quit = 1; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + quit = 1; + } + } break; + default: + break; + } + } + clock_gettime(CLOCK_MONOTONIC, &ts); + px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XFillRectangle(display, window, gc, + px, py, // position + 100, 100); // width and height + + ts.tv_sec = 0; + ts.tv_nsec = 10 * 1000 * 1000; + nanosleep(&ts, NULL); + } + + cleanup(); + return 0; +} +</code></pre> + +<p>ここまでのコードはgitリポジトリの<a href="https://git.mtkn.jp/xlib_playground/file/example/test.c.html">example/test.c</a>にある。</p> +<p>完成品:</p> +<video controls> + <source src="videos/example1.webm" type="video/webm"> +</video> + +<h2>参考</h2> +<ul> +<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> +<li><a href="https://www.youtube.com/watch?v=764fnfEb1_c">X11 App in C with Xlib(youtube video by tsoding)</a></li> +</ul> + </article> + + </main> + <footer> + <address>info(at)mtkn(dot)jp</address> + </footer> +</body> +</html> diff --git a/pub/draft/xlib_playground.html b/pub/draft/xlib_playground.html @@ -1,163 +0,0 @@ -<!DOCTYPE html> -<html lang="ja"> -<head> - <meta charset="utf-8" /> - <meta name="viewport" content="width=device-width,initial-scale=1" /> - <link rel="stylesheet" type="text/css" href="/style.css" /> - <link rel="icon" type="image/x-icon" href="/pics/favicon.ico" /> - <title>Xlibで遊んでみる</title> -</head> -<body> - <header> - <a href="/">主頁</a> | - <a href="/about.html">自己紹介</a> | - <a href="/journal">日記</a> | - <a href="/farm">農業</a> | - <a href="/kitchen">台所</a> | - <a href="/computer">コンピュータ</a> | - <a href="/poetry">詩</a> | - <a href="/books">本棚</a> | - <a href="https://git.mtkn.jp">Git</a> - </header> - <main> - <article> -<h1>Xlibで遊んでみる</h1> -<time>2022-12-16</time> - -<h2>はじめに</h2> -<p>X11でGUIのプログラミングをしてみようと思い、してみた。X11用の低レベルのライブラリはXlibとxcbの二つがあるようだ。x.orgのウェブページを見てみると、Xlibは古く、xcbに置きかわりつつあるという。そのため、新しくなにかを作る場合はxcbを使うようにとのことである。ところがこのxcbはドキュメンテーションに乏しく、X11を触るのが初めての人間にはなにをどうすればいいのかほとんど分からなかった。知らない関数や構造体やらがでてきても(殆ど全部知らないものだが)、その関数なり構造体なりの説明がどこにも見当たらない。manページもない。あるのはdoxygenなるものでソースコードのコメントから自動生成したいい加減なものだけで、使いものにならない。</p> -<p>とりあえずX11のことを少しは理解してからでないと初められそうもないと思い、もう少しましな情報があるXlibから始めることにした。</p> -<p>言語はC言語である。ソースコードは<a href="https://git.mtkn.jp/xlib_playground/log.html">ここ</a>にある。 - -<h2>初期設定</h2> -<p>ディスプレイを開き、ウィンドウを作成する。変数はとりあえずグローバルに宣言することにした。<code>main</code>関数はできるだけ小さくして実際の処理はそれぞれの関数にさせてみる:</p> -<pre><code> -#include &lt;stdio.h&gt; -#include &lt;stdlib.h&gt; -#include &lt;X11/Xlib.h&gt; - -/* macros */ -#define INIT_WIDTH 800 -#define INIT_HEIGHT 600 - -/* variables */ -Display *display; -Window window; -unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; -GC gc; -Atom wm_delete_window; - -void -setup(void) -{ - // Open a display. - if ((display = XOpenDisplay(NULL)) == NULL){ - fprintf(stderr, "ERROR: could not open display\n"); - exit(1); - } - // Create a window. - window = XCreateSimpleWindow( - display, - XDefaultRootWindow(display), - 0, 0, - win_width, win_height, - 0, 0, - 0); - XStoreName(display, window, "UNKO"); - - // Setup a graphical context. - gc = XCreateGC(display, window, 0, NULL); - XSetForeground(display, gc, 0xFFFFFF); - - // Kill the application when the window is destroyed. - wm_delete_window = XInternAtom(display, - "WM_DELETE_WINDOW", False); - XSetWMProtocols(display, window, &wm_delete_window, 1); - - // Setup which input to process. - XSelectInput(display, window, - ExposureMask|KeyPressMask|KeyReleaseMask); - - // Actually draw the window. - XMapWindow(display, window); -} - -void -clean_up(void) -{ - XCloseDisplay(display); -} -</code></pre> - -<p>適当な四角形のものを表示し、その位置を時間の関数として動かしてみる。</p> -<pre><code>#include &lt;time.h&gt; - -int -main(void) -{ - int px, py; - int quit; - struct timespec ts; - XEvent event; - - setup(); - quit = 0; - - while (!quit){ - while(XPending(display) > 0){ - XNextEvent(display, &event); - switch (event.type){ - case KeyPress: { - switch (XLookupKeysym(&event.xkey, 0)){ - case 'q': - quit = 1; - break; - default: - break; - } - } break; - case ClientMessage: { - if ((Atom) event.xclient.data.l[0] == wm_delete_window) { - quit = 1; - } - } break; - default: - break; - } - } - clock_gettime(CLOCK_MONOTONIC, &ts); - px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); - py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); - XClearArea(display, window, - 0, 0, // position - win_width, win_height, // width and height - False); - XFillRectangle(display, window, gc, - px, py, // position - 100, 100); // width and height - - ts.tv_sec = 0; - ts.tv_nsec = 10 * 1000 * 1000; - nanosleep(&ts, NULL); - } - - cleanup(); - return 0; -} -</code></pre> - -<p>ここまでのコードはgitリポジトリの<a href="https://git.mtkn.jp/xlib_playground/file/example/test.c.html">example/test.c</a>にある。</p> - -<h2>参考</h2> -<ul> -<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> -<li><a href="https://www.youtube.com/watch?v=764fnfEb1_c">X11 App in C with Xlib(youtube video by tsoding)</a></li> -</ul> - </article> - - </main> - <footer> - <address>info(at)mtkn(dot)jp</address> - </footer> -</body> -</html> diff --git a/pub/rss.xml b/pub/rss.xml @@ -5,10 +5,171 @@ <description>ウェブページの更新履歴</description> <language>ja-jp</language> <link>https://www.mtkn.jp</link> -<lastBuildDate>Wed, 21 Dec 2022 15:55:25 +0900</lastBuildDate> -<pubDate>Wed, 21 Dec 2022 15:55:25 +0900</pubDate> +<lastBuildDate>Wed, 21 Dec 2022 17:33:47 +0900</lastBuildDate> +<pubDate>Wed, 21 Dec 2022 17:33:47 +0900</pubDate> <docs>https://www.rssboard.org/rss-specification</docs> <item> +<title>Xlibで遊んでみる</title> +<link>https://www.mtkn.jp/computer/xlib_playground1.html</link> +<guid>https://www.mtkn.jp/computer/xlib_playground1.html</guid> +<pubDate>Wed, 21 Dec 2022 00:00:00 +0900</pubDate> +<description><![CDATA[<h1>Xlibで遊んでみる</h1> +<time>2022-12-16</time> + +<h2>はじめに</h2> +<p>X11でGUIのプログラミングをしてみようと思い、してみた。\ +X11用の低レベルのライブラリはXlibとxcbの二つがあるようだ。\ +x.orgのウェブページを見てみると、Xlibは古く、xcbに置きかわりつつあるという。\ +そのため、新しくなにかを作る場合はxcbを使うようにとのことである。\ +ところがこのxcbはドキュメンテーションに乏しく、\ +X11を触るのが初めての人間にはなにをどうすればいいのかほとんど分からなかった。\ +知らない関数や構造体やらがでてきても(殆ど全部知らないものだが)、\ +その関数なり構造体なりの説明がどこにも見当たらない。\ +manページもない。あるのはdoxygenなるものでソースコードのコメントから\ +自動生成したいい加減なものだけで、使いものにならない。</p> +<p>とりあえずX11のことを少しは理解してからでないと初められそうもないと思い、\ +もう少しましな情報があるXlibから始めることにした。\ +</p> +<p>言語はC言語である。ソースコードは\ +<a href="https://git.mtkn.jp/xlib_playground/log.html">ここ</a>にある。 + +<h2>初期設定</h2> +<p>ディスプレイを開き、ウィンドウを作成する。\ +変数はとりあえずグローバルに宣言することにした。\ +<code>main</code>関数はできるだけ小さくして実際の処理は\ +それぞれの関数にさせてみる:</p> +<pre><code> +#include &lt;stdio.h&gt; +#include &lt;stdlib.h&gt; +#include &lt;X11/Xlib.h&gt; + +/* macros */ +#define INIT_WIDTH 800 +#define INIT_HEIGHT 600 + +/* variables */ +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; + +void +setup(void) +{ + // Open a display. + if ((display = XOpenDisplay(NULL)) == NULL){ + fprintf(stderr, "ERROR: could not open display\n"); + exit(1); + } + // Create a window. + window = XCreateSimpleWindow( + display, + XDefaultRootWindow(display), + 0, 0, + win_width, win_height, + 0, 0, + 0); + XStoreName(display, window, "UNKO"); + + // Setup a graphical context. + gc = XCreateGC(display, window, 0, NULL); + XSetForeground(display, gc, 0xFFFFFF); + + // Kill the application when the window is destroyed. + wm_delete_window = XInternAtom(display, + "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &wm_delete_window, 1); + + // Setup which input to process. + XSelectInput(display, window, + ExposureMask|KeyPressMask|KeyReleaseMask); + + // Actually draw the window. + XMapWindow(display, window); +} + +void +clean_up(void) +{ + XCloseDisplay(display); +} +</code></pre> + +<p>適当な四角形のものを表示し、その位置を時間の関数として動かしてみる。\ +</p> +<pre><code>#include &lt;time.h&gt; + +int +main(void) +{ + int px, py; + int quit; + struct timespec ts; + XEvent event; + + setup(); + quit = 0; + + while (!quit){ + while(XPending(display) > 0){ + XNextEvent(display, &event); + switch (event.type){ + case KeyPress: { + switch (XLookupKeysym(&event.xkey, 0)){ + case 'q': + quit = 1; + break; + default: + break; + } + } break; + case ClientMessage: { + if ((Atom) event.xclient.data.l[0] == wm_delete_window) { + quit = 1; + } + } break; + default: + break; + } + } + clock_gettime(CLOCK_MONOTONIC, &ts); + px = 200 + (int) (100 * sinf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + py = 200 + (int) (100 * cosf(ts.tv_sec + ts.tv_nsec / 1000.0 / 1000 / 1000)); + XClearArea(display, window, + 0, 0, // position + win_width, win_height, // width and height + False); + XFillRectangle(display, window, gc, + px, py, // position + 100, 100); // width and height + + ts.tv_sec = 0; + ts.tv_nsec = 10 * 1000 * 1000; + nanosleep(&ts, NULL); + } + + cleanup(); + return 0; +} +</code></pre> + +<p>ここまでのコードはgitリポジトリの\ +<a href="https://git.mtkn.jp/xlib_playground/file/example/test.c.html">example/test.c</a>\ +にある。</p> +<p>完成品:</p> +<video controls> + <source src="videos/example1.webm" type="video/webm"> +</video> + +<h2>参考</h2> +<ul> +<li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li> +<li><a href="https://www.youtube.com/watch?v=764fnfEb1_c">X11 App in C with Xlib(youtube video by tsoding)</a></li> +</ul> +]]></description> +</item> +<item> <title>不死身の特攻兵</title> <link>https://www.mtkn.jp/books/978-4-06-288451-8.html</link> <guid>https://www.mtkn.jp/books/978-4-06-288451-8.html</guid> diff --git a/pub/sitemap.xml b/pub/sitemap.xml @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> +<url><loc>https://www.mtkn.jp/computer/xlib_playground1.html</loc><lastmod>2022-12-21</lastmod></url> <url><loc>https://www.mtkn.jp/</loc><lastmod>2022-11-23</lastmod></url> <url><loc>https://www.mtkn.jp/books/</loc><lastmod>2022-11-23</lastmod></url> <url><loc>https://www.mtkn.jp/books/978-4-06-288451-8.html</loc><lastmod>2022-11-23</lastmod></url>