/*
* 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 はこれらを **実行ファイルのあるディレクトリ基準**で
* 探すため、自分のアプリを別の場所に置く場合は明示が必要になる。
* 指定しないと "Invalid file descriptor to ICU data" で起動に失敗する。
*
* BLINKGTK_RESOURCES 環境変数があればそれを使い、無ければ標準の
* インストール先を試す。
*
* Tell BlinkGTK where Chromium's resources (icudtl.dat, *.pak, the V8
* snapshots) are. They are looked up relative to the directory containing
* the executable, so an application placed elsewhere has to say where they
* are. Without this, startup fails with
* "Invalid file descriptor to ICU data".
*
* Uses $BLINKGTK_RESOURCES if set, otherwise the standard install path. */
const char *res = g_getenv("BLINKGTK_RESOURCES");
if (!res) {
res = "/usr/lib64/blinkgtk-0.1/chromium";
}
blink_gtk_set_resources_path(res);
blink_gtk_set_icu_data_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. */
return blink_gtk_run_main_loop();
}