Files
chandra/camera/chandra-camera-ui.c
nandi 52b42890d6
CI / test (push) Canceled after 0s
Camera: C ISP pipeline, contrast autofocus, and VFR video
- Add chandra-camera-isp.c (C/OpenMP) for Quad-Bayer unpack, CCM, and tone mapping, with a NumPy fallback when it is not built
- Add non-blocking contrast-detection VCM autofocus, with lens subdev discovery on the sensor's I2C bus
- Tune Quad-Bayer AE: a video-mode vblank cap, damped steps, and full analog gain before extending blanking
- Record variable-frame-rate video through a streaming Matroska muxer, and count dropped frames
- Ship libcamera IPA tuning files for imx376, imx519, and uncalibrated sensors
- Map camera buffers by fd and sync them with dma-buf ioctls
2026-11-28 19:25:43 -08:00

1547 lines
57 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#define _POSIX_C_SOURCE 200809L
#include <cairo.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <gdk/gdk.h>
#include <glib-unix.h>
#include <gtk/gtk.h>
#include <math.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
typedef struct {
GtkWidget *window;
GtkWidget *stack;
/* Viewfinder widgets */
GtkWidget *view_area;
GtkWidget *cam_badge_btn;
GtkWidget *flash_btn;
GtkWidget *grid_btn;
GtkWidget *timer_btn;
GtkWidget *res_btn;
GtkWidget *photo_mode_btn;
GtkWidget *video_mode_btn;
GtkWidget *rec_label;
GtkWidget *thumb_area;
GtkWidget *shutter_area;
GtkWidget *switch_btn;
GtkWidget *switch_area;
/* Gallery widgets */
GtkWidget *gallery_title;
GtkWidget *gallery_meta;
GtkWidget *gallery_area;
GtkWidget *delete_btn;
GtkWidget *prev_btn;
GtkWidget *next_btn;
/* Worker process & pipes */
gchar *worker_exe;
gchar *initial_camera;
gboolean open_gallery_start;
pid_t worker_pid;
FILE *to_worker;
FILE *from_worker;
GThread *reader_thread;
gboolean shutting_down;
/* Shared latest frame from worker (CAIRO_FORMAT_RGB24 BGRA32) */
GMutex frame_mutex;
uint8_t *frame_buf;
size_t frame_cap;
int frame_w;
int frame_h;
size_t frame_len;
uint64_t frame_seq;
gboolean draw_pending;
/* Paint & reader buffers swapped under frame_mutex (zero per-frame alloc/memcpy) */
uint8_t *paint_buf;
size_t paint_cap;
int paint_w;
int paint_h;
size_t paint_len;
uint64_t paint_seq;
uint8_t *read_buf;
size_t read_cap;
/* Viewfinder draw rect & tap-to-focus reticle */
double view_rx;
double view_ry;
double view_rw;
double view_rh;
gboolean has_af;
gboolean af_active;
gboolean af_locked;
double af_nx;
double af_ny;
int64_t af_until_us;
/* Camera & UI state */
int cam_count;
gboolean has_flash;
int active_idx;
gboolean active_is_front;
gboolean active_is_back;
gchar *active_label;
gboolean mode_video;
gboolean high_res;
gboolean show_grid;
gboolean torch_on;
gboolean is_recording;
int64_t rec_start_us;
int timer_sec;
int countdown_left;
guint countdown_src;
double flash_alpha;
gchar *toast_text;
int64_t toast_until_us;
/* Gallery & thumbnail */
gchar **gallery_files;
int gallery_count;
int gallery_idx;
GdkPixbuf *thumb_pb;
GdkPixbuf *gallery_pb;
} CameraApp;
typedef struct {
CameraApp *app;
gchar *line;
} WorkerMsg;
static void sync_ui_labels(CameraApp *app);
static void refresh_thumbnail(CameraApp *app);
static void open_gallery_view(CameraApp *app);
static void close_gallery_view(CameraApp *app);
static void shutdown_app(CameraApp *app);
static gchar *pictures_camera_dir(void) {
const gchar *home = g_get_home_dir();
return g_build_filename(home, "Pictures", "Camera", NULL);
}
static void send_worker(CameraApp *app, const char *cmd) {
if (!app->to_worker || app->shutting_down) return;
fprintf(app->to_worker, "%s\n", cmd);
fflush(app->to_worker);
}
static void show_toast(CameraApp *app, const char *msg, double seconds) {
g_free(app->toast_text);
app->toast_text = g_strdup(msg ? msg : "");
app->toast_until_us = g_get_monotonic_time() + (int64_t)(seconds * 1000000.0);
gtk_widget_queue_draw(app->view_area);
}
static gboolean is_image_filename(const char *name) {
if (!name) return FALSE;
const char *dot = strrchr(name, '.');
if (!dot) return FALSE;
return g_ascii_strcasecmp(dot, ".jpg") == 0 ||
g_ascii_strcasecmp(dot, ".jpeg") == 0 ||
g_ascii_strcasecmp(dot, ".png") == 0 ||
g_ascii_strcasecmp(dot, ".webp") == 0;
}
typedef struct {
gchar *path;
time_t mtime;
} GalleryEntry;
static int cmp_gallery_desc(const void *a, const void *b) {
const GalleryEntry *ea = (const GalleryEntry *)a;
const GalleryEntry *eb = (const GalleryEntry *)b;
if (eb->mtime != ea->mtime) {
return (eb->mtime > ea->mtime) ? 1 : -1;
}
return strcmp(eb->path, ea->path);
}
static void free_gallery_files(CameraApp *app) {
if (app->gallery_files) {
for (int i = 0; i < app->gallery_count; i++) {
g_free(app->gallery_files[i]);
}
g_free(app->gallery_files);
app->gallery_files = NULL;
}
app->gallery_count = 0;
}
static void scan_gallery_files(CameraApp *app) {
free_gallery_files(app);
gchar *dir_path = pictures_camera_dir();
DIR *d = opendir(dir_path);
if (!d) {
g_free(dir_path);
return;
}
GArray *arr = g_array_new(FALSE, FALSE, sizeof(GalleryEntry));
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
if (ent->d_name[0] == '.') continue;
if (!is_image_filename(ent->d_name)) continue;
gchar *full = g_build_filename(dir_path, ent->d_name, NULL);
struct stat st;
if (stat(full, &st) == 0 && S_ISREG(st.st_mode)) {
GalleryEntry ge = {full, st.st_mtime};
g_array_append_val(arr, ge);
} else {
g_free(full);
}
}
closedir(d);
g_free(dir_path);
if (arr->len > 0) {
qsort(arr->data, arr->len, sizeof(GalleryEntry), cmp_gallery_desc);
app->gallery_count = (int)arr->len;
app->gallery_files = g_new0(gchar *, arr->len + 1);
for (guint i = 0; i < arr->len; i++) {
GalleryEntry *ge = &g_array_index(arr, GalleryEntry, i);
app->gallery_files[i] = ge->path;
}
}
g_array_free(arr, TRUE);
}
static void refresh_thumbnail(CameraApp *app) {
if (app->thumb_pb) {
g_object_unref(app->thumb_pb);
app->thumb_pb = NULL;
}
scan_gallery_files(app);
if (app->gallery_count > 0) {
GdkPixbuf *raw = gdk_pixbuf_new_from_file_at_scale(app->gallery_files[0], 128, 128, TRUE, NULL);
if (raw) {
app->thumb_pb = gdk_pixbuf_apply_embedded_orientation(raw);
g_object_unref(raw);
}
}
gtk_widget_queue_draw(app->thumb_area);
}
static void load_gallery_image(CameraApp *app) {
if (app->gallery_pb) {
g_object_unref(app->gallery_pb);
app->gallery_pb = NULL;
}
if (app->gallery_count == 0) {
gtk_label_set_text(GTK_LABEL(app->gallery_title), "Gallery (empty)");
gtk_label_set_text(GTK_LABEL(app->gallery_meta), "");
gtk_widget_set_sensitive(app->delete_btn, FALSE);
gtk_widget_set_sensitive(app->prev_btn, FALSE);
gtk_widget_set_sensitive(app->next_btn, FALSE);
gtk_widget_queue_draw(app->gallery_area);
return;
}
if (app->gallery_idx < 0) app->gallery_idx = 0;
if (app->gallery_idx >= app->gallery_count) app->gallery_idx = app->gallery_count - 1;
const char *path = app->gallery_files[app->gallery_idx];
gchar *base = g_path_get_basename(path);
gchar *title = g_strdup_printf("%s · %d/%d", base, app->gallery_idx + 1, app->gallery_count);
gtk_label_set_text(GTK_LABEL(app->gallery_title), title);
g_free(base);
g_free(title);
GdkPixbuf *raw = gdk_pixbuf_new_from_file(path, NULL);
if (raw) {
app->gallery_pb = gdk_pixbuf_apply_embedded_orientation(raw);
g_object_unref(raw);
}
struct stat st;
memset(&st, 0, sizeof(st));
long kb = (stat(path, &st) == 0) ? (long)(st.st_size / 1024) : 0;
if (kb < 1 && st.st_size > 0) kb = 1;
if (app->gallery_pb) {
gchar *meta = g_strdup_printf("%dx%d · %ld KB",
gdk_pixbuf_get_width(app->gallery_pb),
gdk_pixbuf_get_height(app->gallery_pb),
kb);
gtk_label_set_text(GTK_LABEL(app->gallery_meta), meta);
g_free(meta);
} else {
gtk_label_set_text(GTK_LABEL(app->gallery_meta), "Unreadable image");
}
gtk_widget_set_sensitive(app->delete_btn, TRUE);
gtk_widget_set_sensitive(app->prev_btn, app->gallery_idx > 0);
gtk_widget_set_sensitive(app->next_btn, app->gallery_idx + 1 < app->gallery_count);
gtk_widget_queue_draw(app->gallery_area);
}
static void open_gallery_view(CameraApp *app) {
scan_gallery_files(app);
app->gallery_idx = 0;
load_gallery_image(app);
gtk_stack_set_visible_child_name(GTK_STACK(app->stack), "gallery");
}
static void close_gallery_view(CameraApp *app) {
gtk_stack_set_visible_child_name(GTK_STACK(app->stack), "viewfinder");
refresh_thumbnail(app);
gtk_widget_queue_draw(app->view_area);
}
static void set_pill_active(GtkWidget *btn, gboolean active) {
GtkStyleContext *ctx = gtk_widget_get_style_context(btn);
if (active) {
gtk_style_context_add_class(ctx, "cam-pill-active");
} else {
gtk_style_context_remove_class(ctx, "cam-pill-active");
}
}
static void sync_ui_labels(CameraApp *app) {
gtk_button_set_label(GTK_BUTTON(app->cam_badge_btn),
app->active_label ? app->active_label : "Camera");
if (app->has_flash && app->active_is_back) {
gtk_widget_show(app->flash_btn);
} else {
gtk_widget_hide(app->flash_btn);
app->torch_on = FALSE;
}
if (!app->mode_video) {
gtk_widget_show(app->res_btn);
gtk_widget_show(app->timer_btn);
} else {
gtk_widget_hide(app->res_btn);
gtk_widget_hide(app->timer_btn);
}
gtk_widget_set_visible(app->switch_btn, app->cam_count > 1);
set_pill_active(app->flash_btn, app->torch_on);
gtk_button_set_label(GTK_BUTTON(app->flash_btn), app->torch_on ? "Flash On" : "Flash");
set_pill_active(app->grid_btn, app->show_grid);
set_pill_active(app->timer_btn, app->timer_sec > 0);
if (app->timer_sec > 0) {
gchar *tlbl = g_strdup_printf("Timer %ds", app->timer_sec);
gtk_button_set_label(GTK_BUTTON(app->timer_btn), tlbl);
g_free(tlbl);
} else {
gtk_button_set_label(GTK_BUTTON(app->timer_btn), "Timer");
}
set_pill_active(app->res_btn, app->high_res);
gtk_button_set_label(GTK_BUTTON(app->res_btn), app->high_res ? "Full" : "HD");
GtkStyleContext *p_ctx = gtk_widget_get_style_context(app->photo_mode_btn);
GtkStyleContext *v_ctx = gtk_widget_get_style_context(app->video_mode_btn);
if (!app->mode_video) {
gtk_style_context_add_class(p_ctx, "cam-mode-selected");
gtk_style_context_remove_class(v_ctx, "cam-mode-selected-video");
} else {
gtk_style_context_remove_class(p_ctx, "cam-mode-selected");
gtk_style_context_add_class(v_ctx, "cam-mode-selected-video");
}
if (app->is_recording) {
gtk_widget_show(app->rec_label);
} else {
gtk_widget_hide(app->rec_label);
}
gtk_widget_queue_draw(app->shutter_area);
}
static void rounded_rect_path(cairo_t *cr, double x, double y, double w, double h, double r) {
cairo_new_sub_path(cr);
cairo_arc(cr, x + w - r, y + r, r, -M_PI / 2.0, 0.0);
cairo_arc(cr, x + w - r, y + h - r, r, 0.0, M_PI / 2.0);
cairo_arc(cr, x + r, y + h - r, r, M_PI / 2.0, M_PI);
cairo_arc(cr, x + r, y + r, r, M_PI, 3.0 * M_PI / 2.0);
cairo_close_path(cr);
}
static gboolean draw_viewfinder(GtkWidget *widget, cairo_t *cr, gpointer data) {
CameraApp *app = (CameraApp *)data;
int w = gtk_widget_get_allocated_width(widget);
int h = gtk_widget_get_allocated_height(widget);
cairo_set_source_rgb(cr, 0.043, 0.051, 0.078);
cairo_paint(cr);
/* Swap latest frame pointer without holding frame_mutex during Cairo paint. */
g_mutex_lock(&app->frame_mutex);
if (app->frame_buf && app->frame_seq != app->paint_seq) {
uint8_t *tmp_buf = app->paint_buf;
size_t tmp_cap = app->paint_cap;
app->paint_buf = app->frame_buf;
app->paint_cap = app->frame_cap;
app->paint_w = app->frame_w;
app->paint_h = app->frame_h;
app->paint_len = app->frame_len;
app->paint_seq = app->frame_seq;
app->frame_buf = tmp_buf;
app->frame_cap = tmp_cap;
}
g_mutex_unlock(&app->frame_mutex);
double rx = 20.0, ry = 80.0;
double rw = fmax(100.0, w - 40.0), rh = fmax(120.0, h - 220.0);
if (app->paint_buf && app->paint_w > 0 && app->paint_h > 0) {
int iw = app->paint_w;
int ih = app->paint_h;
double top_pad = 56.0;
double bottom_pad = 138.0;
double avail_h = fmax(60.0, h - top_pad - bottom_pad);
double scale = fmin(w / (double)iw, avail_h / (double)ih);
double dw = fmax(1.0, round(iw * scale));
double dh = fmax(1.0, round(ih * scale));
double x = (w - dw) / 2.0;
double y = top_pad + (avail_h - dh) / 2.0;
rx = x;
ry = y;
rw = dw;
rh = dh;
app->view_rx = rx;
app->view_ry = ry;
app->view_rw = rw;
app->view_rh = rh;
cairo_surface_t *surf = cairo_image_surface_create_for_data(
app->paint_buf, CAIRO_FORMAT_RGB24, iw, ih, iw * 4);
if (cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {
cairo_save(cr);
if (app->active_is_front) {
cairo_translate(cr, x + dw, y);
cairo_scale(cr, -dw / (double)iw, dh / (double)ih);
} else {
cairo_translate(cr, x, y);
cairo_scale(cr, dw / (double)iw, dh / (double)ih);
}
cairo_set_source_surface(cr, surf, 0.0, 0.0);
cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR);
cairo_paint(cr);
cairo_restore(cr);
}
cairo_surface_destroy(surf);
} else {
const char *msg = app->cam_count > 0 ? "Starting camera..." : "No camera connected";
cairo_set_source_rgba(cr, 0.75, 0.80, 0.92, 0.78);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 16.0);
cairo_text_extents_t ext;
cairo_text_extents(cr, msg, &ext);
cairo_move_to(cr, (w - ext.width) / 2.0, h / 2.0 - 30.0);
cairo_show_text(cr, msg);
}
if (app->show_grid) {
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.30);
cairo_set_line_width(cr, 1.0);
for (int i = 1; i <= 2; i++) {
double f = i / 3.0;
cairo_move_to(cr, rx + rw * f, ry);
cairo_line_to(cr, rx + rw * f, ry + rh);
cairo_move_to(cr, rx, ry + rh * f);
cairo_line_to(cr, rx + rw, ry + rh * f);
}
cairo_stroke(cr);
}
if (app->af_active && g_get_monotonic_time() <= app->af_until_us) {
double fx = rx + app->af_nx * rw;
double fy = ry + app->af_ny * rh;
double sz = app->af_locked ? 46.0 : 54.0;
rounded_rect_path(cr, fx - sz / 2.0, fy - sz / 2.0, sz, sz, 10.0);
if (app->af_locked) {
cairo_set_source_rgba(cr, 0.20, 0.83, 0.60, 0.92);
} else {
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.88);
}
cairo_set_line_width(cr, 2.0);
cairo_stroke(cr);
}
if (app->countdown_left > 0) {
char buf[16];
snprintf(buf, sizeof(buf), "%d", app->countdown_left);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.92);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 84.0);
cairo_text_extents_t ext;
cairo_text_extents(cr, buf, &ext);
cairo_move_to(cr, (w - ext.width) / 2.0, (h - 80.0 + ext.height) / 2.0);
cairo_show_text(cr, buf);
}
if (app->flash_alpha > 0.01) {
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, app->flash_alpha);
cairo_paint(cr);
}
if (app->toast_text && app->toast_text[0] && g_get_monotonic_time() <= app->toast_until_us) {
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 13.0);
cairo_text_extents_t ext;
cairo_text_extents(cr, app->toast_text, &ext);
double pw = ext.width + 28.0;
double ph = 32.0;
double px = (w - pw) / 2.0;
double py = fmax(72.0, h - 186.0);
rounded_rect_path(cr, px, py, pw, ph, 16.0);
cairo_set_source_rgba(cr, 0.06, 0.08, 0.14, 0.85);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.22);
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
cairo_set_source_rgb(cr, 0.96, 0.97, 1.0);
cairo_move_to(cr, (w - ext.width) / 2.0, py + (ph + ext.height) / 2.0 - 1.0);
cairo_show_text(cr, app->toast_text);
}
return FALSE;
}
static gboolean draw_thumb_button(GtkWidget *widget, cairo_t *cr, gpointer data) {
CameraApp *app = (CameraApp *)data;
int w = gtk_widget_get_allocated_width(widget);
int h = gtk_widget_get_allocated_height(widget);
cairo_save(cr);
rounded_rect_path(cr, 0, 0, w, h, 14.0);
cairo_clip(cr);
if (app->thumb_pb) {
int pw = gdk_pixbuf_get_width(app->thumb_pb);
int ph = gdk_pixbuf_get_height(app->thumb_pb);
double scale = fmax(w / (double)pw, h / (double)ph);
double dw = pw * scale;
double dh = ph * scale;
cairo_translate(cr, (w - dw) / 2.0, (h - dh) / 2.0);
cairo_scale(cr, scale, scale);
gdk_cairo_set_source_pixbuf(cr, app->thumb_pb, 0.0, 0.0);
cairo_paint(cr);
} else {
cairo_set_source_rgba(cr, 0.16, 0.19, 0.28, 0.95);
cairo_paint(cr);
cairo_set_source_rgba(cr, 0.82, 0.86, 0.96, 0.85);
cairo_set_line_width(cr, 1.8);
cairo_rectangle(cr, 13.0, 15.0, w - 26.0, h - 30.0);
cairo_stroke(cr);
cairo_arc(cr, 20.0, 22.0, 2.5, 0.0, 2.0 * M_PI);
cairo_fill(cr);
cairo_move_to(cr, 15.0, h - 18.0);
cairo_line_to(cr, 24.0, h - 27.0);
cairo_line_to(cr, 30.0, h - 22.0);
cairo_line_to(cr, 35.0, h - 25.0);
cairo_line_to(cr, w - 15.0, h - 18.0);
cairo_stroke(cr);
}
cairo_restore(cr);
rounded_rect_path(cr, 0.75, 0.75, w - 1.5, h - 1.5, 13.5);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.35);
cairo_set_line_width(cr, 1.5);
cairo_stroke(cr);
return FALSE;
}
static gboolean draw_shutter_button(GtkWidget *widget, cairo_t *cr, gpointer data) {
CameraApp *app = (CameraApp *)data;
int w = gtk_widget_get_allocated_width(widget);
int h = gtk_widget_get_allocated_height(widget);
double cx = w / 2.0, cy = h / 2.0;
double outer_r = fmin(w, h) / 2.0 - 3.0;
cairo_arc(cr, cx, cy, outer_r, 0.0, 2.0 * M_PI);
cairo_set_source_rgb(cr, 0.95, 0.97, 1.0);
cairo_set_line_width(cr, 3.5);
cairo_stroke(cr);
if (app->mode_video) {
if (app->is_recording) {
double sz = 26.0;
rounded_rect_path(cr, cx - sz / 2.0, cy - sz / 2.0, sz, sz, 6.0);
cairo_set_source_rgb(cr, 0.94, 0.27, 0.27);
cairo_fill(cr);
} else {
cairo_arc(cr, cx, cy, outer_r - 7.5, 0.0, 2.0 * M_PI);
cairo_set_source_rgb(cr, 0.94, 0.27, 0.27);
cairo_fill(cr);
}
} else {
cairo_arc(cr, cx, cy, outer_r - 7.5, 0.0, 2.0 * M_PI);
if (app->countdown_left > 0) {
cairo_set_source_rgb(cr, 0.43, 0.55, 1.0);
} else {
cairo_set_source_rgb(cr, 0.96, 0.97, 1.0);
}
cairo_fill(cr);
}
return FALSE;
}
static gboolean draw_switch_button(GtkWidget *widget, cairo_t *cr, gpointer data) {
(void)data;
int w = gtk_widget_get_allocated_width(widget);
int h = gtk_widget_get_allocated_height(widget);
double cx = w / 2.0, cy = h / 2.0;
cairo_arc(cr, cx, cy, fmin(w, h) / 2.0 - 1.5, 0.0, 2.0 * M_PI);
cairo_set_source_rgba(cr, 0.16, 0.19, 0.28, 0.92);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.22);
cairo_set_line_width(cr, 1.5);
cairo_stroke(cr);
cairo_set_source_rgb(cr, 0.94, 0.96, 1.0);
cairo_set_line_width(cr, 2.2);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
double r = 12.0;
cairo_arc(cr, cx, cy, r, -2.5, -0.35);
cairo_stroke(cr);
cairo_arc(cr, cx, cy, r, 0.65, 2.8);
cairo_stroke(cr);
double ax1 = cx + r * 0.94, ay1 = cy - r * 0.34;
cairo_move_to(cr, ax1 - 5.0, ay1 - 2.0);
cairo_line_to(cr, ax1, ay1);
cairo_line_to(cr, ax1 - 1.0, ay1 - 5.0);
double ax2 = cx - r * 0.94, ay2 = cy + r * 0.34;
cairo_move_to(cr, ax2 + 5.0, ay2 + 2.0);
cairo_line_to(cr, ax2, ay2);
cairo_line_to(cr, ax2 + 1.0, ay2 + 5.0);
cairo_stroke(cr);
return FALSE;
}
static gboolean draw_gallery(GtkWidget *widget, cairo_t *cr, gpointer data) {
CameraApp *app = (CameraApp *)data;
int w = gtk_widget_get_allocated_width(widget);
int h = gtk_widget_get_allocated_height(widget);
cairo_set_source_rgb(cr, 0.043, 0.051, 0.078);
cairo_paint(cr);
if (app->gallery_pb) {
int iw = gdk_pixbuf_get_width(app->gallery_pb);
int ih = gdk_pixbuf_get_height(app->gallery_pb);
double avail_w = fmax(40.0, w - 16.0);
double avail_h = fmax(40.0, h - 16.0);
double scale = fmin(avail_w / (double)iw, avail_h / (double)ih);
double dw = iw * scale;
double dh = ih * scale;
cairo_translate(cr, (w - dw) / 2.0, (h - dh) / 2.0);
cairo_scale(cr, scale, scale);
gdk_cairo_set_source_pixbuf(cr, app->gallery_pb, 0.0, 0.0);
cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR);
cairo_paint(cr);
} else {
cairo_set_source_rgba(cr, 0.75, 0.80, 0.92, 0.80);
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 16.0);
const char *msg = "No photos yet";
cairo_text_extents_t ext;
cairo_text_extents(cr, msg, &ext);
cairo_move_to(cr, (w - ext.width) / 2.0, h / 2.0);
cairo_show_text(cr, msg);
}
return FALSE;
}
static gboolean redraw_from_idle(gpointer data) {
CameraApp *app = (CameraApp *)data;
g_mutex_lock(&app->frame_mutex);
app->draw_pending = FALSE;
g_mutex_unlock(&app->frame_mutex);
if (!app->shutting_down &&
g_strcmp0(gtk_stack_get_visible_child_name(GTK_STACK(app->stack)), "viewfinder") == 0) {
gtk_widget_queue_draw(app->view_area);
}
return G_SOURCE_REMOVE;
}
static gboolean handle_worker_line_idle(gpointer data) {
WorkerMsg *m = (WorkerMsg *)data;
CameraApp *app = m->app;
if (!app->shutting_down) {
char *line = m->line;
size_t n = strlen(line);
while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) {
line[--n] = '\0';
}
if (strncmp(line, "info ", 5) == 0) {
int cnt = 0, hf = 0;
if (sscanf(line + 5, "%d %d", &cnt, &hf) == 2) {
app->cam_count = cnt;
app->has_flash = (hf != 0);
sync_ui_labels(app);
}
} else if (strncmp(line, "active ", 7) == 0) {
int idx = 0, is_f = 0, is_b = 0;
const char *p = line + 7;
if (sscanf(p, "%d %d %d", &idx, &is_f, &is_b) == 3) {
const char *lbl = p;
for (int k = 0; k < 3; k++) {
lbl = strchr(lbl, ' ');
if (!lbl) break;
lbl++;
}
app->active_idx = idx;
app->active_is_front = (is_f != 0);
app->active_is_back = (is_b != 0);
g_free(app->active_label);
app->active_label = g_strdup(lbl ? lbl : "Camera");
sync_ui_labels(app);
}
} else if (strncmp(line, "flash ", 6) == 0) {
app->torch_on = (atoi(line + 6) != 0);
sync_ui_labels(app);
} else if (strncmp(line, "rec_state ", 10) == 0) {
gboolean rec = (atoi(line + 10) != 0);
app->is_recording = rec;
if (rec) {
app->rec_start_us = g_get_monotonic_time();
gtk_label_set_text(GTK_LABEL(app->rec_label), "● 00:00");
}
sync_ui_labels(app);
} else if (strncmp(line, "af_caps ", 8) == 0) {
app->has_af = (atoi(line + 8) != 0);
if (!app->has_af) {
app->af_active = FALSE;
gtk_widget_queue_draw(app->view_area);
}
} else if (strncmp(line, "af_state ", 9) == 0) {
char st[32] = "";
double nx = 0.5, ny = 0.5;
if (sscanf(line + 9, "%31s %lf %lf", st, &nx, &ny) >= 1) {
if (strcmp(st, "idle") == 0) {
app->af_active = FALSE;
} else {
app->af_nx = fmax(0.05, fmin(0.95, nx));
app->af_ny = fmax(0.05, fmin(0.95, ny));
app->af_locked = (strcmp(st, "locked") == 0);
app->af_active = TRUE;
app->af_until_us = g_get_monotonic_time() + (app->af_locked ? 1200000LL : 4000000LL);
}
gtk_widget_queue_draw(app->view_area);
}
} else if (strncmp(line, "saved_photo ", 12) == 0) {
gchar *base = g_path_get_basename(line + 12);
gchar *msg = g_strdup_printf("Saved %s", base);
show_toast(app, msg, 2.4);
g_free(base);
g_free(msg);
refresh_thumbnail(app);
} else if (strncmp(line, "saved_video ", 12) == 0) {
int frames = 0;
int dur_ms = 0;
const char *p = line + 12;
const char *sp = strchr(p, ' ');
if (sp) {
frames = atoi(p);
const char *rest = sp + 1;
const char *sp2 = strchr(rest, ' ');
gboolean has_dur = FALSE;
if (sp2 && isdigit((unsigned char)rest[0])) {
has_dur = TRUE;
for (const char *c = rest; c < sp2; c++) {
if (!isdigit((unsigned char)*c)) {
has_dur = FALSE;
break;
}
}
}
if (has_dur) {
dur_ms = atoi(rest);
gchar *base = g_path_get_basename(sp2 + 1);
double secs = dur_ms > 0 ? (dur_ms / 1000.0) : 0.0;
double fps = (secs > 0.05 && frames > 0) ? (frames / secs) : 0.0;
gchar *msg = (fps > 0.5)
? g_strdup_printf("Saved %s (%.1fs · %.0f fps)", base, secs, fps)
: g_strdup_printf("Saved %s (%d frames)", base, frames);
show_toast(app, msg, 2.8);
g_free(base);
g_free(msg);
} else {
gchar *base = g_path_get_basename(rest);
gchar *msg = g_strdup_printf("Saved %s (%d frames)", base, frames);
show_toast(app, msg, 2.8);
g_free(base);
g_free(msg);
}
}
} else if (strncmp(line, "toast ", 6) == 0) {
show_toast(app, line + 6, 3.0);
}
}
g_free(m->line);
g_free(m);
return G_SOURCE_REMOVE;
}
static gpointer worker_reader_func(gpointer data) {
CameraApp *app = (CameraApp *)data;
char line[1024];
while (fgets(line, sizeof(line), app->from_worker) != NULL) {
if (strncmp(line, "frame ", 6) == 0) {
int w = 0, h = 0;
unsigned long len = 0;
if (sscanf(line + 6, "%d %d %lu", &w, &h, &len) == 3 &&
w > 0 && h > 0 && len == (unsigned long)w * (unsigned long)h * 4UL &&
len <= 16UL * 1024UL * 1024UL) {
if ((size_t)len > app->read_cap) {
uint8_t *nb = (uint8_t *)realloc(app->read_buf, (size_t)len);
if (!nb) break;
app->read_buf = nb;
app->read_cap = (size_t)len;
}
size_t got = 0;
while (got < (size_t)len) {
size_t n = fread(app->read_buf + got, 1, (size_t)len - got, app->from_worker);
if (n == 0) break;
got += n;
}
if (got != (size_t)len) {
break;
}
g_mutex_lock(&app->frame_mutex);
uint8_t *tmp_buf = app->frame_buf;
size_t tmp_cap = app->frame_cap;
app->frame_buf = app->read_buf;
app->frame_cap = app->read_cap;
app->frame_w = w;
app->frame_h = h;
app->frame_len = (size_t)len;
app->frame_seq++;
app->read_buf = tmp_buf;
app->read_cap = tmp_cap;
gboolean need_schedule = !app->draw_pending;
if (need_schedule) app->draw_pending = TRUE;
g_mutex_unlock(&app->frame_mutex);
if (need_schedule && !app->shutting_down) {
g_idle_add(redraw_from_idle, app);
}
}
continue;
}
WorkerMsg *msg = g_new(WorkerMsg, 1);
msg->app = app;
msg->line = g_strdup(line);
g_idle_add(handle_worker_line_idle, msg);
}
return NULL;
}
static gboolean tick_ui(gpointer data) {
CameraApp *app = (CameraApp *)data;
if (app->shutting_down) return G_SOURCE_REMOVE;
int64_t now = g_get_monotonic_time();
gboolean dirty = FALSE;
if (app->flash_alpha > 0.01) {
app->flash_alpha = fmax(0.0, app->flash_alpha - 0.18);
dirty = TRUE;
}
if (app->toast_text && app->toast_text[0] && now > app->toast_until_us) {
g_free(app->toast_text);
app->toast_text = NULL;
dirty = TRUE;
}
if (app->af_active && now > app->af_until_us) {
app->af_active = FALSE;
dirty = TRUE;
}
if (app->is_recording) {
int secs = (int)((now - app->rec_start_us) / 1000000LL);
if (secs < 0) secs = 0;
char buf[32];
snprintf(buf, sizeof(buf), "● %02d:%02d", secs / 60, secs % 60);
gtk_label_set_text(GTK_LABEL(app->rec_label), buf);
}
if (dirty && g_strcmp0(gtk_stack_get_visible_child_name(GTK_STACK(app->stack)), "viewfinder") == 0) {
gtk_widget_queue_draw(app->view_area);
}
return G_SOURCE_CONTINUE;
}
static void trigger_photo_now(CameraApp *app) {
app->flash_alpha = 0.72;
gtk_widget_queue_draw(app->view_area);
send_worker(app, app->high_res ? "snap_full" : "snap");
}
static gboolean countdown_step(gpointer data) {
CameraApp *app = (CameraApp *)data;
app->countdown_left--;
if (app->countdown_left > 0) {
gtk_widget_queue_draw(app->view_area);
return G_SOURCE_CONTINUE;
}
app->countdown_src = 0;
sync_ui_labels(app);
trigger_photo_now(app);
return G_SOURCE_REMOVE;
}
static void on_shutter(CameraApp *app) {
if (app->mode_video) {
send_worker(app, app->is_recording ? "rec_stop" : "rec_start");
return;
}
if (app->countdown_left > 0) {
if (app->countdown_src > 0) {
g_source_remove(app->countdown_src);
app->countdown_src = 0;
}
app->countdown_left = 0;
sync_ui_labels(app);
gtk_widget_queue_draw(app->view_area);
return;
}
if (app->timer_sec > 0) {
app->countdown_left = app->timer_sec;
sync_ui_labels(app);
gtk_widget_queue_draw(app->view_area);
app->countdown_src = g_timeout_add(1000, countdown_step, app);
return;
}
trigger_photo_now(app);
}
static void on_switch_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (app->countdown_left > 0 || app->cam_count <= 1) return;
if (app->is_recording) {
send_worker(app, "rec_stop");
}
app->torch_on = FALSE;
send_worker(app, "switch");
sync_ui_labels(app);
}
static void on_flash_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
app->torch_on = !app->torch_on;
send_worker(app, app->torch_on ? "flash 1" : "flash 0");
sync_ui_labels(app);
}
static void on_grid_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
app->show_grid = !app->show_grid;
sync_ui_labels(app);
gtk_widget_queue_draw(app->view_area);
}
static void on_timer_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (app->timer_sec == 0) app->timer_sec = 3;
else if (app->timer_sec == 3) app->timer_sec = 10;
else app->timer_sec = 0;
sync_ui_labels(app);
}
static void on_res_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
app->high_res = !app->high_res;
sync_ui_labels(app);
}
static void set_mode_video(CameraApp *app, gboolean video) {
if (app->is_recording) return;
app->mode_video = video;
sync_ui_labels(app);
}
static void on_photo_mode_clicked(GtkWidget *w, gpointer data) {
(void)w;
set_mode_video((CameraApp *)data, FALSE);
}
static void on_video_mode_clicked(GtkWidget *w, gpointer data) {
(void)w;
set_mode_video((CameraApp *)data, TRUE);
}
static void on_gallery_btn_clicked(GtkWidget *w, gpointer data) {
(void)w;
open_gallery_view((CameraApp *)data);
}
static void on_shutter_btn_clicked(GtkWidget *w, gpointer data) {
(void)w;
on_shutter((CameraApp *)data);
}
static void on_gallery_back_clicked(GtkWidget *w, gpointer data) {
(void)w;
close_gallery_view((CameraApp *)data);
}
static void on_gallery_prev_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (app->gallery_idx > 0) {
app->gallery_idx--;
load_gallery_image(app);
}
}
static void on_gallery_next_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (app->gallery_idx + 1 < app->gallery_count) {
app->gallery_idx++;
load_gallery_image(app);
}
}
static void on_gallery_delete_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (app->gallery_count == 0) return;
if (unlink(app->gallery_files[app->gallery_idx]) != 0) return;
scan_gallery_files(app);
if (app->gallery_idx >= app->gallery_count) {
app->gallery_idx = app->gallery_count - 1;
}
load_gallery_image(app);
refresh_thumbnail(app);
}
static void on_gallery_folder_clicked(GtkWidget *w, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
gchar *dir = pictures_camera_dir();
g_mkdir_with_parents(dir, 0755);
gchar *argv[] = {"thunar", dir, NULL};
if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL)) {
show_toast(app, "Could not launch file manager", 2.5);
}
g_free(dir);
}
static gboolean on_key_press(GtkWidget *w, GdkEventKey *ev, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
guint kv = ev->keyval;
const char *page = gtk_stack_get_visible_child_name(GTK_STACK(app->stack));
if (g_strcmp0(page, "gallery") == 0) {
if (kv == GDK_KEY_Left || kv == GDK_KEY_Up) {
on_gallery_prev_clicked(NULL, app);
return TRUE;
}
if (kv == GDK_KEY_Right || kv == GDK_KEY_Down) {
on_gallery_next_clicked(NULL, app);
return TRUE;
}
if (kv == GDK_KEY_Escape) {
close_gallery_view(app);
return TRUE;
}
return FALSE;
}
if (kv == GDK_KEY_space || kv == GDK_KEY_Return || kv == GDK_KEY_KP_Enter
#ifdef GDK_KEY_AudioRaiseVolume
|| kv == GDK_KEY_AudioRaiseVolume
#endif
#ifdef GDK_KEY_AudioLowerVolume
|| kv == GDK_KEY_AudioLowerVolume
#endif
) {
on_shutter(app);
return TRUE;
}
if (kv == GDK_KEY_s || kv == GDK_KEY_S || kv == GDK_KEY_Tab) {
on_switch_clicked(NULL, app);
return TRUE;
}
if (kv == GDK_KEY_v || kv == GDK_KEY_V) {
set_mode_video(app, !app->mode_video);
return TRUE;
}
if (kv == GDK_KEY_g || kv == GDK_KEY_G) {
on_grid_clicked(NULL, app);
return TRUE;
}
if (kv == GDK_KEY_Escape) {
if (app->is_recording) {
on_shutter(app);
} else {
shutdown_app(app);
}
return TRUE;
}
return FALSE;
}
static void shutdown_app(CameraApp *app) {
if (app->shutting_down) return;
app->shutting_down = TRUE;
if (app->countdown_src > 0) {
g_source_remove(app->countdown_src);
app->countdown_src = 0;
}
if (app->to_worker) {
fprintf(app->to_worker, "quit\n");
fflush(app->to_worker);
fclose(app->to_worker);
app->to_worker = NULL;
}
if (app->worker_pid > 0) {
int status = 0;
gboolean reaped = FALSE;
for (int i = 0; i < 40; i++) {
pid_t r = waitpid(app->worker_pid, &status, WNOHANG);
if (r == app->worker_pid) {
reaped = TRUE;
break;
}
g_usleep(50000);
}
if (!reaped) {
kill(app->worker_pid, SIGTERM);
waitpid(app->worker_pid, &status, 0);
}
app->worker_pid = -1;
}
if (app->reader_thread) {
g_thread_join(app->reader_thread);
app->reader_thread = NULL;
}
if (app->from_worker) {
fclose(app->from_worker);
app->from_worker = NULL;
}
gtk_main_quit();
}
static gboolean on_delete_event(GtkWidget *w, GdkEvent *ev, gpointer data) {
(void)w;
(void)ev;
shutdown_app((CameraApp *)data);
return FALSE;
}
static gboolean on_unix_sig(gpointer data) {
shutdown_app((CameraApp *)data);
return G_SOURCE_REMOVE;
}
static gboolean start_worker_process(CameraApp *app) {
int in_pipe[2] = {-1, -1};
int out_pipe[2] = {-1, -1};
if (pipe(in_pipe) != 0 || pipe(out_pipe) != 0) {
return FALSE;
}
pid_t pid = fork();
if (pid < 0) {
close(in_pipe[0]);
close(in_pipe[1]);
close(out_pipe[0]);
close(out_pipe[1]);
return FALSE;
}
if (pid == 0) {
dup2(in_pipe[0], STDIN_FILENO);
dup2(out_pipe[1], STDOUT_FILENO);
close(in_pipe[0]);
close(in_pipe[1]);
close(out_pipe[0]);
close(out_pipe[1]);
if (app->initial_camera && app->initial_camera[0]) {
char *argv[] = {app->worker_exe, "--worker", "--camera", app->initial_camera, NULL};
execvp(app->worker_exe, argv);
} else {
char *argv[] = {app->worker_exe, "--worker", NULL};
execvp(app->worker_exe, argv);
}
_exit(127);
}
close(in_pipe[0]);
close(out_pipe[1]);
app->worker_pid = pid;
app->to_worker = fdopen(in_pipe[1], "w");
app->from_worker = fdopen(out_pipe[0], "r");
if (!app->to_worker || !app->from_worker) {
return FALSE;
}
setvbuf(app->from_worker, NULL, _IOFBF, 256 * 1024);
app->reader_thread = g_thread_new("cam-reader", worker_reader_func, app);
return TRUE;
}
static gboolean on_view_button_press(GtkWidget *w, GdkEventButton *ev, gpointer data) {
(void)w;
CameraApp *app = (CameraApp *)data;
if (ev->type != GDK_BUTTON_PRESS || ev->button != 1) return FALSE;
if (!app->has_af || app->view_rw <= 20.0 || app->view_rh <= 20.0) return FALSE;
if (ev->x < app->view_rx || ev->x > app->view_rx + app->view_rw ||
ev->y < app->view_ry || ev->y > app->view_ry + app->view_rh) {
return FALSE;
}
double nx = (ev->x - app->view_rx) / app->view_rw;
double ny = (ev->y - app->view_ry) / app->view_rh;
if (app->active_is_front) nx = 1.0 - nx;
nx = fmax(0.1, fmin(0.9, nx));
ny = fmax(0.1, fmin(0.9, ny));
char cmd[64];
snprintf(cmd, sizeof(cmd), "focus %.3f %.3f", nx, ny);
send_worker(app, cmd);
app->af_nx = app->active_is_front ? (1.0 - nx) : nx;
app->af_ny = ny;
app->af_locked = FALSE;
app->af_active = TRUE;
app->af_until_us = g_get_monotonic_time() + 4000000LL;
gtk_widget_queue_draw(app->view_area);
return TRUE;
}
static void build_ui(CameraApp *app) {
GtkSettings *settings = gtk_settings_get_default();
if (settings) {
g_object_set(settings, "gtk-application-prefer-dark-theme", TRUE, NULL);
}
const char *css =
"window, .cam-root { background-color: #0b0d14; background-image: none; color: #f4f6fc; }\n"
"button.cam-pill { background-color: rgba(14, 18, 30, 0.82); background-image: none; color: #f4f6fc;\n"
" border: 1px solid rgba(255, 255, 255, 0.22); border-radius: 18px;\n"
" box-shadow: none; text-shadow: none; -gtk-icon-shadow: none;\n"
" padding: 4px 10px; font-size: 12px; font-weight: 600; min-height: 24px; }\n"
"button.cam-pill label { color: inherit; }\n"
"button.cam-pill:hover { background-color: rgba(34, 43, 68, 0.92); background-image: none; }\n"
"button.cam-pill.cam-pill-active { background-color: #6e8cff; background-image: none;\n"
" color: #090d16; border-color: rgba(180, 200, 255, 0.95); }\n"
"button.cam-pill.cam-pill-danger { background-color: #ef4444; background-image: none;\n"
" color: #ffffff; border-color: rgba(255, 180, 180, 0.85); }\n"
".cam-dock { background-color: rgba(10, 12, 20, 0.88); background-image: none;\n"
" border-top: 1px solid rgba(255, 255, 255, 0.12); padding: 10px 14px 14px 14px; }\n"
".cam-mode-bar { background-color: rgba(255, 255, 255, 0.10); background-image: none;\n"
" border-radius: 18px; padding: 3px; }\n"
"button.cam-mode-btn { background-color: transparent; background-image: none; color: #c2cadc;\n"
" border: none; box-shadow: none; text-shadow: none; -gtk-icon-shadow: none;\n"
" border-radius: 15px; padding: 4px 18px; font-size: 13px; font-weight: 600; min-height: 22px; }\n"
"button.cam-mode-btn label { color: inherit; }\n"
"button.cam-mode-btn.cam-mode-selected { background-color: #6e8cff; background-image: none; color: #0b0d14; }\n"
"button.cam-mode-btn.cam-mode-selected-video { background-color: #ef4444; background-image: none; color: #ffffff; }\n"
".cam-rec-badge { background-color: rgba(239, 68, 68, 0.92); color: #ffffff;\n"
" border-radius: 12px; padding: 2px 10px; font-size: 12px; font-weight: 700; }\n"
"button.cam-icon-btn, button.cam-icon-btn:hover { background-color: transparent; background-image: none;\n"
" border: none; padding: 0; box-shadow: none; outline: none; }\n"
".cam-gallery-bar { background-color: #111523; background-image: none;\n"
" border-bottom: 1px solid rgba(255, 255, 255, 0.12); padding: 8px 10px; }\n"
".cam-gallery-foot { background-color: #111523; background-image: none;\n"
" border-top: 1px solid rgba(255, 255, 255, 0.12); padding: 8px 12px; }\n"
".cam-title { font-size: 14px; font-weight: 700; color: #f4f6fc; }\n"
".cam-sub { font-size: 12px; color: #9ba6c2; }\n";
GtkCssProvider *prov = gtk_css_provider_new();
gtk_css_provider_load_from_data(prov, css, -1, NULL);
gtk_style_context_add_provider_for_screen(
gdk_screen_get_default(), GTK_STYLE_PROVIDER(prov), GTK_STYLE_PROVIDER_PRIORITY_USER);
g_object_unref(prov);
app->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(app->window), "Camera");
gtk_window_set_default_size(GTK_WINDOW(app->window), 480, 860);
gtk_widget_set_size_request(app->window, 340, 480);
gtk_style_context_add_class(gtk_widget_get_style_context(app->window), "cam-root");
app->stack = gtk_stack_new();
gtk_stack_set_transition_type(GTK_STACK(app->stack), GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT);
gtk_stack_set_transition_duration(GTK_STACK(app->stack), 180);
gtk_container_add(GTK_CONTAINER(app->window), app->stack);
/* Viewfinder page */
GtkWidget *overlay = gtk_overlay_new();
app->view_area = gtk_drawing_area_new();
gtk_widget_set_hexpand(app->view_area, TRUE);
gtk_widget_set_vexpand(app->view_area, TRUE);
gtk_widget_add_events(app->view_area, GDK_BUTTON_PRESS_MASK);
g_signal_connect(app->view_area, "draw", G_CALLBACK(draw_viewfinder), app);
g_signal_connect(app->view_area, "button-press-event", G_CALLBACK(on_view_button_press), app);
gtk_container_add(GTK_CONTAINER(overlay), app->view_area);
GtkWidget *top_bar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
gtk_widget_set_halign(top_bar, GTK_ALIGN_FILL);
gtk_widget_set_valign(top_bar, GTK_ALIGN_START);
gtk_widget_set_margin_top(top_bar, 12);
gtk_widget_set_margin_start(top_bar, 12);
gtk_widget_set_margin_end(top_bar, 12);
app->cam_badge_btn = gtk_button_new_with_label("Camera");
gtk_style_context_add_class(gtk_widget_get_style_context(app->cam_badge_btn), "cam-pill");
gtk_widget_set_can_focus(app->cam_badge_btn, FALSE);
g_signal_connect(app->cam_badge_btn, "clicked", G_CALLBACK(on_switch_clicked), app);
gtk_box_pack_start(GTK_BOX(top_bar), app->cam_badge_btn, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(top_bar), gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0), TRUE, TRUE, 0);
app->flash_btn = gtk_button_new_with_label("Flash");
gtk_style_context_add_class(gtk_widget_get_style_context(app->flash_btn), "cam-pill");
gtk_widget_set_can_focus(app->flash_btn, FALSE);
g_signal_connect(app->flash_btn, "clicked", G_CALLBACK(on_flash_clicked), app);
gtk_box_pack_start(GTK_BOX(top_bar), app->flash_btn, FALSE, FALSE, 0);
app->grid_btn = gtk_button_new_with_label("Grid");
gtk_style_context_add_class(gtk_widget_get_style_context(app->grid_btn), "cam-pill");
gtk_widget_set_can_focus(app->grid_btn, FALSE);
g_signal_connect(app->grid_btn, "clicked", G_CALLBACK(on_grid_clicked), app);
gtk_box_pack_start(GTK_BOX(top_bar), app->grid_btn, FALSE, FALSE, 0);
app->timer_btn = gtk_button_new_with_label("Timer");
gtk_style_context_add_class(gtk_widget_get_style_context(app->timer_btn), "cam-pill");
gtk_widget_set_can_focus(app->timer_btn, FALSE);
g_signal_connect(app->timer_btn, "clicked", G_CALLBACK(on_timer_clicked), app);
gtk_box_pack_start(GTK_BOX(top_bar), app->timer_btn, FALSE, FALSE, 0);
app->res_btn = gtk_button_new_with_label("HD");
gtk_style_context_add_class(gtk_widget_get_style_context(app->res_btn), "cam-pill");
gtk_widget_set_can_focus(app->res_btn, FALSE);
g_signal_connect(app->res_btn, "clicked", G_CALLBACK(on_res_clicked), app);
gtk_box_pack_start(GTK_BOX(top_bar), app->res_btn, FALSE, FALSE, 0);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), top_bar);
GtkWidget *dock = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_style_context_add_class(gtk_widget_get_style_context(dock), "cam-dock");
gtk_widget_set_halign(dock, GTK_ALIGN_FILL);
gtk_widget_set_valign(dock, GTK_ALIGN_END);
GtkWidget *mode_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
gtk_widget_set_halign(mode_row, GTK_ALIGN_CENTER);
GtkWidget *mode_pill = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2);
gtk_style_context_add_class(gtk_widget_get_style_context(mode_pill), "cam-mode-bar");
app->photo_mode_btn = gtk_button_new_with_label("Photo");
gtk_style_context_add_class(gtk_widget_get_style_context(app->photo_mode_btn), "cam-mode-btn");
gtk_widget_set_can_focus(app->photo_mode_btn, FALSE);
g_signal_connect(app->photo_mode_btn, "clicked", G_CALLBACK(on_photo_mode_clicked), app);
app->video_mode_btn = gtk_button_new_with_label("Video");
gtk_style_context_add_class(gtk_widget_get_style_context(app->video_mode_btn), "cam-mode-btn");
gtk_widget_set_can_focus(app->video_mode_btn, FALSE);
g_signal_connect(app->video_mode_btn, "clicked", G_CALLBACK(on_video_mode_clicked), app);
gtk_box_pack_start(GTK_BOX(mode_pill), app->photo_mode_btn, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(mode_pill), app->video_mode_btn, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(mode_row), mode_pill, FALSE, FALSE, 0);
app->rec_label = gtk_label_new("● 00:00");
gtk_style_context_add_class(gtk_widget_get_style_context(app->rec_label), "cam-rec-badge");
gtk_widget_set_no_show_all(app->rec_label, TRUE);
gtk_box_pack_start(GTK_BOX(mode_row), app->rec_label, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(dock), mode_row, FALSE, FALSE, 0);
GtkWidget *action_row = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
gtk_box_set_homogeneous(GTK_BOX(action_row), TRUE);
GtkWidget *gallery_btn = gtk_button_new();
gtk_style_context_add_class(gtk_widget_get_style_context(gallery_btn), "cam-icon-btn");
gtk_widget_set_halign(gallery_btn, GTK_ALIGN_CENTER);
gtk_widget_set_valign(gallery_btn, GTK_ALIGN_CENTER);
gtk_widget_set_size_request(gallery_btn, 56, 56);
gtk_widget_set_can_focus(gallery_btn, FALSE);
gtk_widget_set_tooltip_text(gallery_btn, "Open Camera Gallery");
app->thumb_area = gtk_drawing_area_new();
gtk_widget_set_size_request(app->thumb_area, 52, 52);
g_signal_connect(app->thumb_area, "draw", G_CALLBACK(draw_thumb_button), app);
gtk_container_add(GTK_CONTAINER(gallery_btn), app->thumb_area);
g_signal_connect(gallery_btn, "clicked", G_CALLBACK(on_gallery_btn_clicked), app);
gtk_box_pack_start(GTK_BOX(action_row), gallery_btn, TRUE, TRUE, 0);
GtkWidget *shutter_btn = gtk_button_new();
gtk_style_context_add_class(gtk_widget_get_style_context(shutter_btn), "cam-icon-btn");
gtk_widget_set_halign(shutter_btn, GTK_ALIGN_CENTER);
gtk_widget_set_valign(shutter_btn, GTK_ALIGN_CENTER);
gtk_widget_set_size_request(shutter_btn, 76, 76);
gtk_widget_set_can_focus(shutter_btn, FALSE);
gtk_widget_set_tooltip_text(shutter_btn, "Take Photo / Record Video (Space)");
app->shutter_area = gtk_drawing_area_new();
gtk_widget_set_size_request(app->shutter_area, 76, 76);
g_signal_connect(app->shutter_area, "draw", G_CALLBACK(draw_shutter_button), app);
gtk_container_add(GTK_CONTAINER(shutter_btn), app->shutter_area);
g_signal_connect(shutter_btn, "clicked", G_CALLBACK(on_shutter_btn_clicked), app);
gtk_box_pack_start(GTK_BOX(action_row), shutter_btn, TRUE, TRUE, 0);
app->switch_btn = gtk_button_new();
gtk_style_context_add_class(gtk_widget_get_style_context(app->switch_btn), "cam-icon-btn");
gtk_widget_set_halign(app->switch_btn, GTK_ALIGN_CENTER);
gtk_widget_set_valign(app->switch_btn, GTK_ALIGN_CENTER);
gtk_widget_set_size_request(app->switch_btn, 56, 56);
gtk_widget_set_can_focus(app->switch_btn, FALSE);
gtk_widget_set_tooltip_text(app->switch_btn, "Switch Camera (S)");
app->switch_area = gtk_drawing_area_new();
gtk_widget_set_size_request(app->switch_area, 52, 52);
g_signal_connect(app->switch_area, "draw", G_CALLBACK(draw_switch_button), app);
gtk_container_add(GTK_CONTAINER(app->switch_btn), app->switch_area);
g_signal_connect(app->switch_btn, "clicked", G_CALLBACK(on_switch_clicked), app);
gtk_box_pack_start(GTK_BOX(action_row), app->switch_btn, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(dock), action_row, FALSE, FALSE, 0);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), dock);
gtk_stack_add_named(GTK_STACK(app->stack), overlay, "viewfinder");
/* Gallery page */
GtkWidget *gbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget *gbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 8);
gtk_style_context_add_class(gtk_widget_get_style_context(gbar), "cam-gallery-bar");
GtkWidget *back_btn = gtk_button_new_with_label("← Camera");
gtk_style_context_add_class(gtk_widget_get_style_context(back_btn), "cam-pill");
gtk_widget_set_can_focus(back_btn, FALSE);
g_signal_connect(back_btn, "clicked", G_CALLBACK(on_gallery_back_clicked), app);
gtk_box_pack_start(GTK_BOX(gbar), back_btn, FALSE, FALSE, 0);
app->gallery_title = gtk_label_new("Gallery");
gtk_style_context_add_class(gtk_widget_get_style_context(app->gallery_title), "cam-title");
gtk_label_set_ellipsize(GTK_LABEL(app->gallery_title), PANGO_ELLIPSIZE_END);
gtk_box_pack_start(GTK_BOX(gbar), app->gallery_title, TRUE, TRUE, 4);
GtkWidget *folder_btn = gtk_button_new_with_label("Folder");
gtk_style_context_add_class(gtk_widget_get_style_context(folder_btn), "cam-pill");
gtk_widget_set_can_focus(folder_btn, FALSE);
g_signal_connect(folder_btn, "clicked", G_CALLBACK(on_gallery_folder_clicked), app);
gtk_box_pack_start(GTK_BOX(gbar), folder_btn, FALSE, FALSE, 0);
app->delete_btn = gtk_button_new_with_label("Delete");
gtk_style_context_add_class(gtk_widget_get_style_context(app->delete_btn), "cam-pill");
gtk_style_context_add_class(gtk_widget_get_style_context(app->delete_btn), "cam-pill-danger");
gtk_widget_set_can_focus(app->delete_btn, FALSE);
g_signal_connect(app->delete_btn, "clicked", G_CALLBACK(on_gallery_delete_clicked), app);
gtk_box_pack_start(GTK_BOX(gbar), app->delete_btn, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(gbox), gbar, FALSE, FALSE, 0);
app->gallery_area = gtk_drawing_area_new();
gtk_widget_set_hexpand(app->gallery_area, TRUE);
gtk_widget_set_vexpand(app->gallery_area, TRUE);
g_signal_connect(app->gallery_area, "draw", G_CALLBACK(draw_gallery), app);
gtk_box_pack_start(GTK_BOX(gbox), app->gallery_area, TRUE, TRUE, 0);
GtkWidget *gfoot = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10);
gtk_style_context_add_class(gtk_widget_get_style_context(gfoot), "cam-gallery-foot");
app->prev_btn = gtk_button_new_with_label("‹ Prev");
gtk_style_context_add_class(gtk_widget_get_style_context(app->prev_btn), "cam-pill");
gtk_widget_set_can_focus(app->prev_btn, FALSE);
g_signal_connect(app->prev_btn, "clicked", G_CALLBACK(on_gallery_prev_clicked), app);
gtk_box_pack_start(GTK_BOX(gfoot), app->prev_btn, FALSE, FALSE, 0);
app->gallery_meta = gtk_label_new("");
gtk_style_context_add_class(gtk_widget_get_style_context(app->gallery_meta), "cam-sub");
gtk_box_pack_start(GTK_BOX(gfoot), app->gallery_meta, TRUE, TRUE, 4);
app->next_btn = gtk_button_new_with_label("Next ›");
gtk_style_context_add_class(gtk_widget_get_style_context(app->next_btn), "cam-pill");
gtk_widget_set_can_focus(app->next_btn, FALSE);
g_signal_connect(app->next_btn, "clicked", G_CALLBACK(on_gallery_next_clicked), app);
gtk_box_pack_start(GTK_BOX(gfoot), app->next_btn, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(gbox), gfoot, FALSE, FALSE, 0);
gtk_stack_add_named(GTK_STACK(app->stack), gbox, "gallery");
g_signal_connect(app->window, "delete-event", G_CALLBACK(on_delete_event), app);
g_signal_connect(app->window, "key-press-event", G_CALLBACK(on_key_press), app);
}
int main(int argc, char **argv) {
signal(SIGPIPE, SIG_IGN);
setenv("GDK_BACKEND", "wayland", 0);
CameraApp app;
memset(&app, 0, sizeof(app));
app.worker_pid = -1;
app.active_label = g_strdup("Camera");
g_mutex_init(&app.frame_mutex);
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--worker-exe") == 0 && i + 1 < argc) {
app.worker_exe = g_strdup(argv[++i]);
} else if (strcmp(argv[i], "--camera") == 0 && i + 1 < argc) {
app.initial_camera = g_strdup(argv[++i]);
} else if (strcmp(argv[i], "--open-gallery") == 0) {
app.open_gallery_start = TRUE;
}
}
if (!app.worker_exe) {
app.worker_exe = g_strdup("/usr/local/bin/chandra-camera");
}
g_set_prgname("chandra-camera");
g_set_application_name("Camera");
gtk_init(&argc, &argv);
build_ui(&app);
refresh_thumbnail(&app);
sync_ui_labels(&app);
gtk_widget_show_all(app.window);
GdkDisplay *dpy = gdk_display_get_default();
GdkMonitor *mon = dpy ? gdk_display_get_monitor(dpy, 0) : NULL;
if (mon) {
GdkRectangle geo;
gdk_monitor_get_geometry(mon, &geo);
if (geo.width <= 900 || geo.height > geo.width) {
gtk_window_maximize(GTK_WINDOW(app.window));
}
}
if (app.open_gallery_start) {
open_gallery_view(&app);
} else {
gtk_stack_set_visible_child_name(GTK_STACK(app.stack), "viewfinder");
}
if (!start_worker_process(&app)) {
show_toast(&app, "Failed to start camera worker", 5.0);
}
g_timeout_add(120, tick_ui, &app);
g_unix_signal_add(SIGINT, on_unix_sig, &app);
g_unix_signal_add(SIGTERM, on_unix_sig, &app);
gtk_main();
free(app.frame_buf);
free(app.paint_buf);
free(app.read_buf);
g_free(app.worker_exe);
g_free(app.initial_camera);
g_free(app.active_label);
g_free(app.toast_text);
free_gallery_files(&app);
if (app.thumb_pb) g_object_unref(app.thumb_pb);
if (app.gallery_pb) g_object_unref(app.gallery_pb);
g_mutex_clear(&app.frame_mutex);
return 0;
}