Linux 版のソースです。Windows 版は blinkgtk_browser-windows.c
で、リソースの既定の場所 (実行ファイルの隣) と初期化
(blink_gtk_sandbox_init() +
blink_gtk_init_with_sandbox()) の 2 点が違います。
This is the Linux source. The Windows version is blinkgtk_browser-windows.c;
it differs in two places: the default resource location (next to the
executable) and initialisation (blink_gtk_sandbox_init() +
blink_gtk_init_with_sandbox()).
/*
* blinkgtk_browser.c — BlinkGTK の最小構成サンプル
* A minimal BlinkGTK sample
*
* ウィンドウを 1 つ開き、BlinkWebView で https://google.com/ を表示するだけの
* プログラムです。BlinkGTK を組み込むのに最低限必要なものだけを示します。
*
* Opens a single window and displays https://google.com/ in a BlinkWebView.
* It shows only what is strictly required to embed BlinkGTK.
*
* ビルド / Build:
* make
* または / or:
* cc -o blinkgtk_browser blinkgtk_browser.c $(pkg-config --cflags --libs blinkgtk-0.1)
*
* 実行 / Run:
* ./blinkgtk_browser
* ./blinkgtk_browser https://example.com/ (URL を指定する場合 / to specify a URL)
* ./blinkgtk_browser --version (版を表示 / print the version)
* ./blinkgtk_browser --help (使い方 / usage)
*
* Copyright 2026 BlinkGTK Project
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <blink_gtk/blink_gtk.h>
#include <gtk/gtk.h>
/* 起動時に開く URL / The URL opened at startup */
#define DEFAULT_URL "https://google.com/"
/* ウィンドウを閉じたらメインループを抜ける。
* Quit the main loop when the window is closed. */
static gboolean on_close_request(GtkWindow *window, gpointer user_data)
{
(void)window;
(void)user_data;
blink_gtk_quit_main_loop();
return FALSE;
}
/* --version / --help は blink_gtk_init() より前に処理する。
* 版数の API は初期化前でも呼べるので、画面の無い環境でも答えられる。
* 不具合を報告するときは、この 1 行を貼れば版が一意に決まる。
*
* Handle --version / --help before blink_gtk_init(). The version API may be
* called before initialization, so this works where there is no display.
* When reporting a problem, this single line identifies the build. */
static int print_version(void)
{
const char *full = blink_gtk_get_version_full(); /* 例: "X.Y.Z-buildN" */
const char *chromium = blink_gtk_get_chromium_version();
g_print("BlinkGTK %s (Chromium %s)\n",
full ? full : blink_gtk_get_version(),
chromium ? chromium : "unknown");
return 0;
}
static int print_usage(const char *argv0)
{
g_print("使い方 / Usage: %s [URL]\n"
" URL 開く URL (既定 " DEFAULT_URL ")\n"
" the URL to open (default " DEFAULT_URL ")\n"
" --version 版を表示する / print the version\n"
" --help この使い方を表示する / print this usage\n",
argv0);
return 0;
}
int main(int argc, char **argv)
{
for (int i = 1; i < argc; i++) {
if (g_strcmp0(argv[i], "--version") == 0) {
return print_version();
}
if (g_strcmp0(argv[i], "--help") == 0 || g_strcmp0(argv[i], "-h") == 0) {
return print_usage(argv[0]);
}
}
/* 1. Chromium のリソース (icudtl.dat / *.pak / V8 スナップショット) の場所。
* BlinkGTK は blink_gtk_init() の中で次の順に自動で探すので、通常は
* 何も指定しなくてよい (1.2.3 以降)。
* (1) blink_gtk_set_resources_path() で指定した場所
* (2) 実行ファイルのあるディレクトリ (icudtl.dat があれば)
* (3) libblinkgtk のある場所から <libdir>/blinkgtk-0.1/chromium/、
* <libdir>/chromium/、<libdir>/
* RPM・DEB・tarball のどれで入れても (3) で見つかる。
* リソースを独自の場所に置いたときだけ、blink_gtk_init() の前に
* blink_gtk_set_resources_path() で教える。このサンプルでは、
* BLINKGTK_RESOURCES 環境変数 (このサンプルが読む変数。BlinkGTK 自体は
* 読まない) が設定されているときだけ指定する。
*
* Where Chromium's resources (icudtl.dat, *.pak, the V8 snapshots) are.
* blink_gtk_init() looks for them in this order, so usually nothing has
* to be set (1.2.3 and later):
* (1) the path given to blink_gtk_set_resources_path()
* (2) the directory containing the executable (if it has icudtl.dat)
* (3) relative to libblinkgtk: <libdir>/blinkgtk-0.1/chromium/,
* <libdir>/chromium/, <libdir>/
* RPM, DEB and tarball installs are all found by (3). Only when the
* resources are somewhere else, call blink_gtk_set_resources_path()
* before blink_gtk_init(). This sample does so only when the
* BLINKGTK_RESOURCES environment variable is set (a variable read by
* this sample, not by BlinkGTK itself). */
const char *res = g_getenv("BLINKGTK_RESOURCES");
if (res && res[0] != '\0') {
blink_gtk_set_resources_path(res);
}
/* 2. BlinkGTK (Chromium) を初期化する。GTK より先に呼ぶ。
* Initialize BlinkGTK (Chromium). Call this before GTK. */
if (!blink_gtk_init(&argc, &argv)) {
g_printerr("blink_gtk_init() に失敗しました / failed\n");
return 1;
}
gtk_init();
const char *url = (argc > 1 && argv[1][0] != '-') ? argv[1] : DEFAULT_URL;
/* 3. ウィンドウと BlinkWebView を作る。
* blink_web_view_new() は GtkWidget * を返すので、BlinkGTK の API に
* 渡すときは BLINK_WEB_VIEW() でキャストする。
*
* Create the window and the BlinkWebView. blink_web_view_new() returns a
* GtkWidget *, so cast with BLINK_WEB_VIEW() when passing it to the
* BlinkGTK API. */
GtkWidget *window = gtk_window_new();
gtk_window_set_title(GTK_WINDOW(window), "BlinkGTK");
gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
g_signal_connect(window, "close-request", G_CALLBACK(on_close_request), NULL);
GtkWidget *view = blink_web_view_new();
gtk_window_set_child(GTK_WINDOW(window), view);
/* 4. ウィンドウを表示してから URL を読み込む。
* Present the window, then load the URL. */
gtk_window_present(GTK_WINDOW(window));
blink_web_view_load_uri(BLINK_WEB_VIEW(view), url);
/* 5. Chromium のメインループを回す。
* GTK の g_application_run() ではなく、必ずこちらを使う。
* BlinkGTK は Chromium のメインループ上で動くため、GTK のループを
* 使うとレンダラからフレームが届かず、画面が白いままになる。
*
* Run Chromium's main loop. Always use this instead of GTK's
* g_application_run(): BlinkGTK runs on Chromium's main loop, and using
* GTK's loop means no frames arrive from the renderer, leaving the
* window blank. */
int rc = blink_gtk_run_main_loop();
/* 6. メインループが戻ったら、BlinkGTK を片づけてから終わる。
* 呼ばずに main() から戻ると、終了の後始末が atexit の時点に回る。
* Windows ではその時点では正しく片づけられず、終わるまでに時間が
* かかったうえ異常終了することがある。どの OS でも必ず呼ぶ。
*
* When the main loop returns, shut BlinkGTK down before exiting.
* Without this call the clean-up is left to atexit; on Windows it
* cannot finish cleanly there, and the process may take a long time
* to exit and then end abnormally. Always call it, on every OS. */
blink_gtk_shutdown();
return rc;
}