2026-11-22 10:49:59 +02:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
|
#include "gemui.h"
|
|
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#define HIT_THREAD 1
|
|
|
|
|
#define HIT_NEW 2
|
|
|
|
|
#define HIT_INPUT 3
|
|
|
|
|
#define HIT_SEND 4
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
bool outgoing;
|
|
|
|
|
char ts[32];
|
|
|
|
|
char text[256];
|
|
|
|
|
} msg_t;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
char peer[64];
|
|
|
|
|
msg_t msgs[64];
|
|
|
|
|
int num_msgs;
|
|
|
|
|
} thread_t;
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
gemui_app_t *app;
|
|
|
|
|
char sms_dir[512];
|
|
|
|
|
thread_t threads[32];
|
|
|
|
|
int num_threads;
|
|
|
|
|
int sel_thread;
|
|
|
|
|
int inotify_fd;
|
|
|
|
|
int focus_mode; /* 0 = compose, 1 = new peer number */
|
|
|
|
|
char compose[256];
|
|
|
|
|
char new_peer[64];
|
|
|
|
|
} sms_state_t;
|
|
|
|
|
|
|
|
|
|
static void load_threads(sms_state_t *st) {
|
|
|
|
|
char prev_peer[64] = {0};
|
|
|
|
|
if (st->sel_thread >= 0 && st->sel_thread < st->num_threads) {
|
|
|
|
|
snprintf(prev_peer, sizeof(prev_peer), "%s", st->threads[st->sel_thread].peer);
|
|
|
|
|
}
|
|
|
|
|
st->num_threads = 0;
|
|
|
|
|
|
|
|
|
|
DIR *d = opendir(st->sms_dir);
|
|
|
|
|
if (!d) return;
|
|
|
|
|
struct dirent *de;
|
|
|
|
|
while ((de = readdir(d)) != NULL && st->num_threads < 32) {
|
|
|
|
|
size_t len = strlen(de->d_name);
|
|
|
|
|
if (len < 5 || strcmp(de->d_name + len - 4, ".tsv") != 0) continue;
|
|
|
|
|
thread_t *th = &st->threads[st->num_threads++];
|
|
|
|
|
memset(th, 0, sizeof(*th));
|
|
|
|
|
snprintf(th->peer, sizeof(th->peer), "%.*s", (int)(len - 4), de->d_name);
|
|
|
|
|
|
|
|
|
|
char fp[576];
|
|
|
|
|
snprintf(fp, sizeof(fp), "%s/%s", st->sms_dir, de->d_name);
|
|
|
|
|
FILE *f = fopen(fp, "r");
|
|
|
|
|
if (!f) continue;
|
|
|
|
|
char line[384];
|
|
|
|
|
while (fgets(line, sizeof(line), f) && th->num_msgs < 64) {
|
|
|
|
|
size_t n = strlen(line);
|
|
|
|
|
while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = '\0';
|
|
|
|
|
char *t1 = strchr(line, '\t');
|
|
|
|
|
char *t2 = t1 ? strchr(t1 + 1, '\t') : NULL;
|
|
|
|
|
if (!t1 || !t2) continue;
|
|
|
|
|
*t1 = '\0';
|
|
|
|
|
*t2 = '\0';
|
|
|
|
|
msg_t *m = &th->msgs[th->num_msgs++];
|
|
|
|
|
m->outgoing = (strcmp(line, "OUT") == 0);
|
|
|
|
|
snprintf(m->ts, sizeof(m->ts), "%s", t1 + 1);
|
|
|
|
|
snprintf(m->text, sizeof(m->text), "%s", t2 + 1);
|
|
|
|
|
}
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
closedir(d);
|
|
|
|
|
|
|
|
|
|
st->sel_thread = st->num_threads > 0 ? 0 : -1;
|
|
|
|
|
if (prev_peer[0]) {
|
|
|
|
|
for (int i = 0; i < st->num_threads; i++) {
|
|
|
|
|
if (strcmp(st->threads[i].peer, prev_peer) == 0) {
|
|
|
|
|
st->sel_thread = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_inotify(gemui_app_t *app, int fd, void *ud) {
|
|
|
|
|
sms_state_t *st = ud;
|
|
|
|
|
char buf[512];
|
|
|
|
|
while (read(fd, buf, sizeof(buf)) > 0) {}
|
|
|
|
|
load_threads(st);
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void send_sms(sms_state_t *st) {
|
|
|
|
|
const char *peer = NULL;
|
|
|
|
|
if (st->new_peer[0]) {
|
|
|
|
|
peer = st->new_peer;
|
|
|
|
|
} else if (st->sel_thread >= 0 && st->sel_thread < st->num_threads) {
|
|
|
|
|
peer = st->threads[st->sel_thread].peer;
|
|
|
|
|
}
|
|
|
|
|
if (!peer || !peer[0] || !st->compose[0]) {
|
|
|
|
|
gemui_show_toast(st->app, "Select/enter recipient and message text", 1800);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char cmd[384], resp[128];
|
|
|
|
|
snprintf(cmd, sizeof(cmd), "sms-send %s %s", peer, st->compose);
|
|
|
|
|
if (gemui_unix_cmd("/run/geminux/modem.sock", cmd, resp, sizeof(resp)) != 0) {
|
|
|
|
|
/* Persist locally if modem daemon isn't running yet */
|
|
|
|
|
char fp[576];
|
|
|
|
|
snprintf(fp, sizeof(fp), "%s/%s.tsv", st->sms_dir, peer);
|
|
|
|
|
FILE *f = fopen(fp, "a");
|
|
|
|
|
if (f) {
|
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
struct tm tm;
|
|
|
|
|
localtime_r(&now, &tm);
|
|
|
|
|
char ts[32];
|
|
|
|
|
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M", &tm);
|
|
|
|
|
fprintf(f, "OUT\t%s\t%s\n", ts, st->compose);
|
|
|
|
|
fclose(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
snprintf(st->new_peer, sizeof(st->new_peer), "%s", peer);
|
|
|
|
|
st->compose[0] = '\0';
|
|
|
|
|
st->focus_mode = 0;
|
|
|
|
|
load_threads(st);
|
|
|
|
|
for (int i = 0; i < st->num_threads; i++) {
|
|
|
|
|
if (strcmp(st->threads[i].peer, peer) == 0) st->sel_thread = i;
|
|
|
|
|
}
|
|
|
|
|
st->new_peer[0] = '\0';
|
|
|
|
|
gemui_show_toast(st->app, "SMS queued for transmission", 1800);
|
|
|
|
|
gemui_request_redraw(st->app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
|
|
|
|
sms_state_t *st = ud;
|
|
|
|
|
const gemui_theme_t *t = gemui_theme(app);
|
|
|
|
|
|
|
|
|
|
gemui_draw_header(app, cr, "\xef\x83\xa0", "Geminux SMS Messages",
|
|
|
|
|
"Tab: Switch Field | Enter: Send");
|
|
|
|
|
|
|
|
|
|
int pad = 20;
|
2026-11-22 15:49:38 +02:00
|
|
|
int top = 92;
|
|
|
|
|
int left_w = 560;
|
2026-11-22 10:49:59 +02:00
|
|
|
if (left_w > w / 3) left_w = w / 3;
|
|
|
|
|
int right_x = pad * 2 + left_w;
|
|
|
|
|
int right_w = w - right_x - pad;
|
|
|
|
|
|
|
|
|
|
/* Left pane: recipient input + thread list */
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_input(app, cr, (gemui_rect_t){pad, top, left_w, 68},
|
2026-11-22 10:49:59 +02:00
|
|
|
"New recipient number...", st->new_peer, false,
|
|
|
|
|
st->focus_mode == 1, HIT_NEW, 0);
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_rect_t pane = {pad, top + 82, left_w, h - (top + 82) - pad};
|
2026-11-22 10:49:59 +02:00
|
|
|
gemui_fill_round(cr, pane, 12, t->bg_bar);
|
|
|
|
|
gemui_stroke_round(cr, pane, 12, 1.5, t->border);
|
|
|
|
|
|
|
|
|
|
if (st->num_threads == 0) {
|
|
|
|
|
gemui_draw_text(cr, t, pane, "No SMS threads yet", 0, false,
|
|
|
|
|
GEMUI_ALIGN_CENTER, t->fg_muted);
|
|
|
|
|
} else {
|
2026-11-22 15:49:38 +02:00
|
|
|
int rh = 84;
|
2026-11-22 10:49:59 +02:00
|
|
|
for (int i = 0; i < st->num_threads; i++) {
|
|
|
|
|
gemui_rect_t rr = {pane.x + 10, pane.y + 10 + i * rh, pane.w - 20, rh - 8};
|
|
|
|
|
if (rr.y + rr.h > pane.y + pane.h) break;
|
|
|
|
|
bool sel = (i == st->sel_thread);
|
|
|
|
|
gemui_fill_round(cr, rr, 10, sel ? t->sel_bg : (i % 2 ? t->bg_alt_odd : t->bg_alt_even));
|
|
|
|
|
thread_t *th = &st->threads[i];
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + 14, rr.y + 4, rr.w - 28, 38},
|
2026-11-22 10:49:59 +02:00
|
|
|
th->peer, -1, true, GEMUI_ALIGN_LEFT,
|
|
|
|
|
sel ? t->sel_fg : t->fg_bright);
|
|
|
|
|
const char *preview = (th->num_msgs > 0) ? th->msgs[th->num_msgs - 1].text : "";
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + 14, rr.y + 42, rr.w - 28, 34},
|
2026-11-22 10:49:59 +02:00
|
|
|
preview, -2, false, GEMUI_ALIGN_LEFT,
|
|
|
|
|
sel ? t->sel_fg : t->fg_muted);
|
|
|
|
|
gemui_hit_add(app, rr, HIT_THREAD, i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Right pane: chat history bubbles + compose bar */
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_rect_t chat = {right_x, top, right_w, h - top - 114};
|
2026-11-22 10:49:59 +02:00
|
|
|
gemui_fill_round(cr, chat, 12, t->bg_overlay);
|
|
|
|
|
gemui_stroke_round(cr, chat, 12, 1.5, t->border);
|
|
|
|
|
|
|
|
|
|
if (st->sel_thread >= 0 && st->sel_thread < st->num_threads) {
|
|
|
|
|
thread_t *th = &st->threads[st->sel_thread];
|
2026-11-22 15:49:38 +02:00
|
|
|
int bh = 94;
|
2026-11-22 10:49:59 +02:00
|
|
|
int cap = (chat.h - 24) / bh;
|
|
|
|
|
int start = (th->num_msgs > cap) ? (th->num_msgs - cap) : 0;
|
|
|
|
|
int y = chat.y + 12;
|
|
|
|
|
for (int i = start; i < th->num_msgs; i++) {
|
|
|
|
|
msg_t *m = &th->msgs[i];
|
|
|
|
|
int bw = (chat.w * 70) / 100;
|
|
|
|
|
int bx = m->outgoing ? (chat.x + chat.w - bw - 16) : (chat.x + 16);
|
|
|
|
|
gemui_rect_t br = {bx, y, bw, bh - 10};
|
|
|
|
|
gemui_fill_round(cr, br, 12, m->outgoing ? t->bg_bubble_out : t->bg_bubble_in);
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){br.x + 14, br.y + 6, br.w - 28, 42},
|
2026-11-22 10:49:59 +02:00
|
|
|
m->text, -1, false, GEMUI_ALIGN_LEFT, t->fg_bright);
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_text(cr, t, (gemui_rect_t){br.x + 14, br.y + 50, br.w - 28, 28},
|
2026-11-22 10:49:59 +02:00
|
|
|
m->ts, -4, false, m->outgoing ? GEMUI_ALIGN_RIGHT : GEMUI_ALIGN_LEFT, t->fg_muted);
|
|
|
|
|
y += bh;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
gemui_draw_text(cr, t, chat, "Select a thread or enter a new number", 0,
|
|
|
|
|
false, GEMUI_ALIGN_CENTER, t->fg_muted);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
int send_w = 210;
|
|
|
|
|
gemui_draw_input(app, cr, (gemui_rect_t){right_x, h - 94, right_w - send_w - 12, 74},
|
2026-11-22 10:49:59 +02:00
|
|
|
"Type SMS message...", st->compose, false,
|
|
|
|
|
st->focus_mode == 0, HIT_INPUT, 0);
|
2026-11-22 15:49:38 +02:00
|
|
|
gemui_draw_button(app, cr, (gemui_rect_t){right_x + right_w - send_w, h - 94, send_w, 74},
|
2026-11-22 10:49:59 +02:00
|
|
|
"Send", HIT_SEND, 0, true, false, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
|
|
|
|
|
(void)state;
|
|
|
|
|
sms_state_t *st = ud;
|
|
|
|
|
char *target = (st->focus_mode == 1) ? st->new_peer : st->compose;
|
|
|
|
|
size_t max_sz = (st->focus_mode == 1) ? sizeof(st->new_peer) : sizeof(st->compose);
|
|
|
|
|
|
|
|
|
|
if (ks == XK_Escape) {
|
|
|
|
|
gemui_quit(app, 0);
|
|
|
|
|
} else if (ks == XK_Tab) {
|
|
|
|
|
st->focus_mode = 1 - st->focus_mode;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (ks == XK_Up && st->sel_thread > 0) {
|
|
|
|
|
st->sel_thread--;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (ks == XK_Down && st->sel_thread + 1 < st->num_threads) {
|
|
|
|
|
st->sel_thread++;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (ks == XK_Return || ks == XK_KP_Enter) {
|
|
|
|
|
if (st->focus_mode == 1) {
|
|
|
|
|
st->focus_mode = 0;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else {
|
|
|
|
|
send_sms(st);
|
|
|
|
|
}
|
|
|
|
|
} else if (ks == XK_BackSpace) {
|
|
|
|
|
gemui_utf8_pop_char(target);
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (utf8 && utf8[0]) {
|
|
|
|
|
size_t cur = strlen(target);
|
|
|
|
|
size_t add = strlen(utf8);
|
|
|
|
|
if (cur + add < max_sz) {
|
|
|
|
|
memcpy(target + cur, utf8, add + 1);
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
|
|
|
|
|
(void)btn;
|
|
|
|
|
sms_state_t *st = ud;
|
|
|
|
|
if (type != GEMUI_PTR_DOWN) return;
|
|
|
|
|
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
|
|
|
|
|
if (!hit) return;
|
|
|
|
|
|
|
|
|
|
if (hit->id == HIT_THREAD) {
|
|
|
|
|
st->sel_thread = hit->val;
|
|
|
|
|
st->new_peer[0] = '\0';
|
|
|
|
|
st->focus_mode = 0;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (hit->id == HIT_NEW) {
|
|
|
|
|
st->focus_mode = 1;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (hit->id == HIT_INPUT) {
|
|
|
|
|
st->focus_mode = 0;
|
|
|
|
|
gemui_request_redraw(app);
|
|
|
|
|
} else if (hit->id == HIT_SEND) {
|
|
|
|
|
send_sms(st);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
|
sms_state_t st;
|
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
|
st.sel_thread = -1;
|
|
|
|
|
const char *home = getenv("HOME");
|
|
|
|
|
snprintf(st.sms_dir, sizeof(st.sms_dir), "%s/.local/share/geminux/sms",
|
|
|
|
|
(home && home[0]) ? home : "/home/user");
|
|
|
|
|
const char *mk[] = {"mkdir", "-p", st.sms_dir, NULL};
|
|
|
|
|
gemui_run_cmd_capture(mk, NULL, 0);
|
|
|
|
|
|
|
|
|
|
if (argc > 1 && argv[1][0]) {
|
|
|
|
|
snprintf(st.new_peer, sizeof(st.new_peer), "%s", argv[1]);
|
|
|
|
|
}
|
|
|
|
|
load_threads(&st);
|
|
|
|
|
|
2026-11-22 15:49:38 +02:00
|
|
|
st.app = gemui_create("geminux-sms", "Geminux SMS Messages", 1920, 940, false);
|
2026-11-22 10:49:59 +02:00
|
|
|
if (!st.app) return 1;
|
|
|
|
|
|
|
|
|
|
st.inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
|
|
|
|
if (st.inotify_fd >= 0) {
|
|
|
|
|
inotify_add_watch(st.inotify_fd, st.sms_dir, IN_MODIFY | IN_CREATE | IN_MOVED_TO);
|
|
|
|
|
gemui_add_fd(st.app, st.inotify_fd, on_inotify, &st);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gemui_handlers_t h = { .on_draw = on_draw, .on_key = on_key, .on_ptr = on_ptr };
|
|
|
|
|
gemui_set_handlers(st.app, &h, &st);
|
|
|
|
|
int rc = gemui_run(st.app);
|
|
|
|
|
if (st.inotify_fd >= 0) close(st.inotify_fd);
|
|
|
|
|
gemui_destroy(st.app);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|