SwBr git · main
SwBr - status bar for sway
C 95.6% Markdown 4%git clone https://git.christianimmanuel.de/sway/SwBr.gitwget https://git.christianimmanuel.de/sway/SwBr/archive/SwBr.tar.gzAdd vcam res menu-entry
README.md | 36 ++-- commit_message.txt | 51 +++-- config.advanced.example | 14 +- swbr.c | 510 +++++++++++++++++++++++++++--------------------- 4 files changed, 347 insertions(+), 264 deletions(-) diff --git a/README.md b/README.md index f9a4c18..065ed66 100644 --- a/README.md +++ b/README.md @@ -91,10 +91,13 @@ word — `%c%i %w %h` gives `83- 21.5W 1.7h`. `src_fmt`: `%n` the cell's name, `%w` the workspaces, `%c` the count. **vcam** is the virtual webcam that loops your camera through a filter and -overlay chain. swbr asks its socket — `sock=`, else `$VCAM_SOCK`, else -`/tmp/vcam.sock` — rather than running `vcam-ctl` on a timer, so whether it -is up, what it is sharing and whether it is recording are facts. The cell is -not there at all while vcam is not running: no empty box, nothing. +overlay chain. swbr speaks vcam's control protocol itself on its socket — +`sock=`, else `$VCAM_SOCK`, else `/tmp/vcam.sock` — rather than running +`vcam-ctl` on a timer, so whether it is up, what it is sharing, where the +webcam sits and whether it is recording are facts. One round trip a tick +carries the menu's commands and reads the new state back, so the menu +redraws from what vcam says it did. The cell is not there at all while vcam +is not running: no empty box, nothing. If it never appears, `swbr --probe` says which socket it tried and what the answer was: @@ -122,25 +125,30 @@ Clicking it opens a menu in the panel under the bar: | Row | | |---|---| -| share a region… / stop sharing the region | pick it with slurp | -| share screen DP-1 / stop sharing DP-1 | one row per monitor, named from sway | -| stop sharing everything | only for shares none of those rows covers | -| show the webcam / webcam off | the webcam overlay on top of the share | +| share a region… / stop sharing the region | picked with slurp | +| share screen DP-1 / stop sharing DP-1 | one row per monitor, from vcam's own list | +| show the webcam / webcam off | the picture-in-picture over the share | | webcam: top right → bottom right | moves it on, round the six positions | +| camera: 1280x720 → 640x480 @30 | the camera's own size, a step at a time | | record the loopback / stop recording | | -Each row is a toggle: a share swbr started gets an overlay id of its own — -the monitor's name, or `region` — so the row that started it is the row that -stops it. Only what makes sense for the state it is in is offered. +Each row is a toggle against what vcam reports, so the monitor already +shared is the one offering to stop. Only what makes sense for the state it +is in is offered — no webcam position without a webcam, no camera row when +there is only one size. + +The camera row changes what the camera sends, not what apps see: the output +size is fixed when vcam starts, because the apps reading the loopback depend +on it. A smaller camera mode is softer but cheaper on USB and CPU. The menu stays open and follows along while you work through it, because setting a share up is usually several of these in a row: press the position row five times and the webcam walks round the frame, the label saying where it is and where it is going next. -The menu runs `vcam-ctl` for the doing — picking a region needs slurp, and -sharing a whole monitor needs geometry and down-scaling it already gets -right. `src_path=` points at it if it is not on `$PATH`. +Two rows cannot go down the socket and shell out instead: picking a region +needs `slurp`, and recording is `vcam-ctl`'s business. `src_path=` points at +`vcam-ctl` if it is not on `$PATH`. ## Folded diff --git a/commit_message.txt b/commit_message.txt index 51a28f9..502a151 100644 --- a/commit_message.txt +++ b/commit_message.txt @@ -1,23 +1,34 @@ -swbr: the vcam menu follows along, and its share rows toggle - -Cycling the webcam position needed the menu closed and reopened between -every step. The menu was only rebuilt when a crude bitmask of the vcam state -changed — running, recording, how many shares, webcam present — and moving -an overlay changes none of those. So the row went on offering the move it -had already made, and pressing it again sent the same command. The change -check is a digest of everything the cell and the menu are drawn from now, -overlay rectangles included, so the row updates in place and five presses -walk the webcam round the frame. - -The share rows are toggles. A share swbr starts gets an overlay id of its -own — the monitor's name, or "region" — so the row that started it is the -row that stops it: "share screen DP-1" becomes "stop sharing DP-1". The -separate stop row is gone; one remains for shares none of those rows -covers, started from the command line or elsewhere. - -The cell is blue in the advanced example — its own colour beside docker's -green and the clock's cyan — and so is the running lamp on the strip. The -recording lamp stays red. +swbr: talk to vcam's control API, and offer the camera's resolution + +vcam has a proper control protocol now — status in one line, monitors, +capture formats, share and cam commands — so the cell stops reverse +engineering state out of an overlay list and asks for it. + +What that replaces: + + - `list` parsing and guessing the webcam's corner from its pixel + rectangle. `status` says cam_pos outright, and share_monitor names the + monitor being shared, so the -i trick that made shares identifiable is + gone. + - sway's output list. vcam's own `monitors` is what `share start` takes, + which is the list that can actually be shared. + - vcam-ctl for most of the doing. Sharing, the webcam and the camera mode + go down the socket, in the same round trip that reads the new state + back, so the menu redraws from what vcam says it did rather than from + what we hoped. Two rows still shell out: picking a region needs slurp, + and recording is vcam-ctl's business. + +New row: the camera's resolution, stepped a size at a time, built from +`capture formats` for the format in use and deduplicated by size so a +camera with thirty near-identical modes offers three or four real choices. +It changes what the camera sends, not what apps see — the output size is +fixed at vcam's startup, because the loopback's readers depend on it. + +One round trip is now + + [a command from the menu] # status # monitors # capture formats # quit + +with `#` marking the end of each reply, as the API describes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SuAfijAAYjLpcVwhCq3AF4 diff --git a/config.advanced.example b/config.advanced.example index 62eeedf..efbb87e 100644 --- a/config.advanced.example +++ b/config.advanced.example @@ -235,15 +235,19 @@ cell=vcam vcam.source=vcam # /tmp/vcam.sock, or $VCAM_SOCK, or vcam.sock= below. # `swbr --probe` prints the one it tried and the answer # vcam.sock=/tmp/vcam.sock # if yours is somewhere else -# vcam.src_path=vcam-ctl # if it is not on $PATH +# vcam.src_path=vcam-ctl # for the two rows that shell out: picking + # a region (slurp) and recording. Everything else goes + # down the socket vcam.color=60A5FA # blue, so it reads as its own thing beside the # green of docker and the cyan of the clock vcam.slim_color=60A5FA # the running lamp on the folded strip vcam.slim_color2=E03A3A # the recording one beside it, red whatever the cell is - # Clicking the cell opens the menu: share a region, share - # a monitor by name, stop sharing, show and move the - # webcam overlay, start and stop recording. Only the rows - # that make sense for what it is doing are offered. + # Clicking the cell opens the menu: share a region or a + # monitor by name, stop sharing, show and move the webcam + # picture-in-picture, step the camera's resolution, start + # and stop recording. Only the rows that make sense for + # what it is doing are offered, and each one is a toggle + # against what vcam reports. # --- clock ----------------------------------------------------------------- cell=clock diff --git a/swbr.c b/swbr.c index 79f9f37..1a3d12c 100644 --- a/swbr.c +++ b/swbr.c @@ -73,7 +73,7 @@ #include "sw_theme.h" #define APP_ID "swbr" -#define SWBR_VERSION "0.12.3" +#define SWBR_VERSION "0.13.0" #ifndef SWBR_BUILD /* set by the Makefile: md5 of this file */ #define SWBR_BUILD "unknown" #endif @@ -2979,54 +2979,60 @@ static void window_scan(const JV *node, const char *want, const char *ws, /* ----------------------------------------------------------------- vcam * - * vcam is a virtual webcam: it reads the real camera, composites overlays — - * images, looping video, live screen regions, the webcam itself — and writes - * the result to a v4l2loopback device that browsers see as a camera. It - * listens on a unix socket and answers in lines. + * vcam is a virtual webcam: it reads the real camera, composes overlays and + * writes the result to a v4l2loopback device that video-call apps see as a + * camera. It listens on a unix socket and answers in lines. * - * swbr asks it directly rather than running vcam-ctl on a timer, for the same - * reason it asks cmus itself: whether vcam is up, what it is sharing and - * whether it is recording are then facts, not a guess at what a script - * printed. Reading is non-blocking and rides the tick the bar already has — - * a hung vcam must not take the bar down with it. + * swbr speaks that protocol itself rather than running vcam-ctl on a timer, + * for the same reason it asks cmus directly: whether vcam is up, what it is + * sharing, where the webcam sits and whether it is recording are then facts. + * One connection a tick carries the menu's commands and reads the new state + * back, non-blocking, on the tick the bar already has — a wedged vcam must + * not take the bar down with it. * - * Doing is different from asking. The menu shells out to vcam-ctl, because - * picking a region needs slurp and sharing a whole monitor needs geometry - * and down-scaling that vcam-ctl already gets right. + * Protocol, from vcam's API: one command a line, every reply line starts OK + * or ERR, `#` answers `OK noop` and so marks the end of a reply whose length + * we do not know, and `quit` closes the connection. So one round trip is + * + * [a command from the menu] # status # monitors # capture formats # quit + * + * and the sections come back separated by OK noop. */ -#define VCAM_MAX_OVL 16 +#define VCAM_MAX_MON 8 +#define VCAM_MAX_MODE 16 typedef struct { - char kind[8]; /* img | vid | webcam | screen */ - char id[32]; - int x, y, w, h; -} VcamOvl; - -typedef struct { - bool running; /* the socket answered */ - bool recording; - char rec_file[160]; - VcamOvl ovl[VCAM_MAX_OVL]; - int n; - int screens; /* how many of them are screen captures */ - int cam; /* index of the first webcam overlay, or -1 */ - uint32_t at; /* when this was last believed */ + bool running; /* the socket answered */ + bool share, share_fullscreen; + char share_monitor[48]; + bool cam, cam_over_screen; + char cam_pos[12]; /* tl tr bl br center full custom */ + bool recording; + bool capture; + char cap_fmt[16]; + int cap_w, cap_h, cap_fps; + struct { char name[48]; } mon[VCAM_MAX_MON]; + int nmon; + struct { int w, h, fps; } mode[VCAM_MAX_MODE]; /* current fmt, distinct */ + int nmode; } VcamState; -static VcamState VCAM; /* zeroed; VCAM.cam is fixed up at startup */ +static VcamState VCAM; enum { VC_IDLE, VC_CONNECTING, VC_WRITING, VC_READING }; static struct { int fd; /* -1 when nothing is in flight */ int phase; - size_t sent; /* of the request */ - char buf[4096]; + char req[512]; + size_t reqlen, sent; + char buf[8192]; size_t len; uint32_t began; - char why[192]; /* what went wrong last time, for --probe */ -} vcam_io = { -1, VC_IDLE, 0, {0}, 0, 0, "" }; + char why[192]; /* what happened last time, for --probe */ + char todo[256]; /* a command from the menu, waiting for a trip */ +} vcam_io = { -1, VC_IDLE, {0}, 0, 0, {0}, 0, 0, "", "" }; static int vcam_dbg(void) { @@ -3058,25 +3064,26 @@ static const char *vcam_sock_path(void) static void vcam_io_close(void) { if (vcam_io.fd >= 0) close(vcam_io.fd); - vcam_io.fd = -1; - vcam_io.phase = VC_IDLE; - vcam_io.len = 0; - vcam_io.sent = 0; + vcam_io.fd = -1; + vcam_io.phase = VC_IDLE; + vcam_io.len = 0; + vcam_io.sent = 0; + vcam_io.reqlen = 0; } static void vcam_down(const char *why) { - VCAM.running = false; - VCAM.recording = false; - VCAM.n = VCAM.screens = 0; - VCAM.cam = -1; - VCAM.at = now_ms(); + memset(&VCAM, 0, sizeof VCAM); vcam_note(why); } -/* Ask for everything in one connection: vcam-ctl's own protocol is "write - your commands, write quit, read until the far end closes". */ -static const char VCAM_REQ[] = "list\nrecord info\nquit\n"; +/* A command for the next round trip. The reply comes back in the same + connection, so the menu redraws from what vcam says it did, not from what + we hoped it would do. */ +static void vcam_do(const char *cmd) +{ + str_set(vcam_io.todo, sizeof(vcam_io.todo), cmd); +} static void vcam_ask(void) { @@ -3091,21 +3098,22 @@ static void vcam_ask(void) a.sun_family = AF_UNIX; str_set(a.sun_path, sizeof(a.sun_path), vcam_sock_path()); - vcam_io.fd = fd; - vcam_io.len = 0; - vcam_io.sent = 0; - vcam_io.began = now_ms(); + vcam_io.reqlen = (size_t)snprintf(vcam_io.req, sizeof(vcam_io.req), + "%s%sstatus\n#\nmonitors\n#\ncapture formats\n#\nquit\n", + vcam_io.todo, vcam_io.todo[0] ? "\n" : ""); + vcam_io.todo[0] = 0; + vcam_io.fd = fd; + vcam_io.len = 0; + vcam_io.sent = 0; + vcam_io.began = now_ms(); if (connect(fd, (struct sockaddr *)&a, sizeof a) == 0) { vcam_io.phase = VC_WRITING; /* the usual case */ return; } /* A unix socket normally connects inside connect(). It does not when the - far end has not got round to accepting — a vcam busy with frames, which - is every vcam — and then this is EINPROGRESS or EAGAIN and the socket - is not writable yet. Writing anyway fails, and throwing the connection - away on that is what left the cell empty against a vcam that was up - the whole time. Wait for it instead. */ + far end has not got round to accepting, and then the socket is not + writable yet — wait for it rather than throwing the connection away. */ if (errno == EINPROGRESS || errno == EALREADY || errno == EAGAIN #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN || errno == EWOULDBLOCK @@ -3122,54 +3130,122 @@ static void vcam_ask(void) } } -/* One line of `list`: OK 0: [screen] id=screen pos=(0,0) size=1280x720 (top) */ -static void vcam_parse_line(const char *ln) +/* key=value out of a status line. Values never contain spaces. */ +static bool vcam_kv(const char *ln, const char *key, char *out, size_t cap) { - const char *p; - VcamOvl o; - - if (!strncmp(ln, "OK recording", 12)) { - VCAM.recording = true; - p = strstr(ln, "file="); - if (p) str_set(VCAM.rec_file, sizeof(VCAM.rec_file), p + 5); - return; - } - if (!strcmp(ln, "OK stopped") || !strcmp(ln, "OK already stopped")) { - VCAM.recording = false; - VCAM.rec_file[0] = 0; - return; + size_t kl = strlen(key); + const char *p = ln; + while ((p = strstr(p, key)) != NULL) { + if ((p == ln || p[-1] == ' ') && p[kl] == '=') { + const char *v = p + kl + 1, *e = v; + while (*e && *e != ' ') e++; + if ((size_t)(e - v) >= cap) return false; + memcpy(out, v, (size_t)(e - v)); + out[e - v] = 0; + return true; + } + p += kl; } - p = strchr(ln, '['); - if (!p || VCAM.n >= VCAM_MAX_OVL) return; + return false; +} + +static bool vcam_kv_bool(const char *ln, const char *key) +{ + char v[8]; + return vcam_kv(ln, key, v, sizeof v) && !strcmp(v, "yes"); +} + +static int vcam_kv_int(const char *ln, const char *key) +{ + char v[16]; + return vcam_kv(ln, key, v, sizeof v) ? atoi(v) : 0; +} - memset(&o, 0, sizeof o); - if (sscanf(p, "[%7[^]]] id=%31s pos=(%d,%d) size=%dx%d", - o.kind, o.id, &o.x, &o.y, &o.w, &o.h) < 6) return; +static void vcam_parse_status(const char *ln) +{ + VCAM.running = true; + VCAM.share = vcam_kv_bool(ln, "share"); + VCAM.share_fullscreen = vcam_kv_bool(ln, "share_fullscreen"); + VCAM.cam = vcam_kv_bool(ln, "cam"); + VCAM.cam_over_screen = vcam_kv_bool(ln, "cam_over_screen"); + VCAM.recording = vcam_kv_bool(ln, "recording"); + VCAM.capture = vcam_kv_bool(ln, "capture"); + VCAM.cap_w = vcam_kv_int(ln, "capture_w"); + VCAM.cap_h = vcam_kv_int(ln, "capture_h"); + VCAM.cap_fps = vcam_kv_int(ln, "capture_fps"); + if (!vcam_kv(ln, "share_monitor", VCAM.share_monitor, sizeof VCAM.share_monitor)) + VCAM.share_monitor[0] = 0; + if (!vcam_kv(ln, "cam_pos", VCAM.cam_pos, sizeof VCAM.cam_pos)) + VCAM.cam_pos[0] = 0; + if (!vcam_kv(ln, "capture_fmt", VCAM.cap_fmt, sizeof VCAM.cap_fmt)) + VCAM.cap_fmt[0] = 0; + if (!strcmp(VCAM.share_monitor, "-")) VCAM.share_monitor[0] = 0; + if (!strcmp(VCAM.cam_pos, "-")) VCAM.cam_pos[0] = 0; + if (!strcmp(VCAM.cap_fmt, "-")) VCAM.cap_fmt[0] = 0; +} - if (!strcmp(o.kind, "screen")) VCAM.screens++; - else if (!strcmp(o.kind, "webcam") && VCAM.cam < 0) VCAM.cam = VCAM.n; - VCAM.ovl[VCAM.n++] = o; +/* OK <n> <name> <WxH+X+Y> */ +static void vcam_parse_monitor(const char *ln) +{ + char name[48]; + int n; + if (VCAM.nmon >= VCAM_MAX_MON) return; + if (sscanf(ln, "OK %d %47s", &n, name) != 2) return; + str_set(VCAM.mon[VCAM.nmon].name, sizeof(VCAM.mon[0].name), name); + VCAM.nmon++; +} + +/* OK <fmt> <WxH> <max fps> — kept only for the format in use, one entry a + size, so the menu offers sizes rather than a hundred near-identical modes. */ +static void vcam_parse_mode(const char *ln) +{ + char fmt[16]; + int w, h, fps, i; + if (VCAM.nmode >= VCAM_MAX_MODE) return; + if (sscanf(ln, "OK %15s %dx%d %d", fmt, &w, &h, &fps) != 4) return; + if (VCAM.cap_fmt[0] && strcmp(fmt, VCAM.cap_fmt)) return; + for (i = 0; i < VCAM.nmode; ++i) + if (VCAM.mode[i].w == w && VCAM.mode[i].h == h) { + if (fps > VCAM.mode[i].fps) VCAM.mode[i].fps = fps; + return; + } + VCAM.mode[VCAM.nmode].w = w; + VCAM.mode[VCAM.nmode].h = h; + VCAM.mode[VCAM.nmode].fps = fps; + VCAM.nmode++; } static void vcam_parse(void) { char *p = vcam_io.buf, *nl; + int section = 0; /* 0 status, 1 monitors, 2 formats */ - VCAM.running = true; - VCAM.recording = false; - VCAM.n = VCAM.screens = 0; - VCAM.cam = -1; - VCAM.at = now_ms(); - vcam_note("answered"); - + memset(&VCAM, 0, sizeof VCAM); vcam_io.buf[vcam_io.len] = 0; - while ((nl = strchr(p, '\n')) != NULL) { - *nl = 0; - while (*p == ' ') p++; - if (*p) vcam_parse_line(p); - p = nl + 1; + + for (;;) { + char *ln = p; + nl = strchr(p, '\n'); + if (nl) { *nl = 0; p = nl + 1; } + while (*ln == ' ') ln++; + if (*ln) { + if (!strcmp(ln, "OK noop")) { + section++; + } else if (!strncmp(ln, "ERR", 3)) { + /* a command this vcam does not have: skip, keep the rest */ + } else switch (section) { + case 0: if (!strncmp(ln, "OK version=", 11) || + strstr(ln, "output_w=")) vcam_parse_status(ln); + break; + case 1: vcam_parse_monitor(ln); break; + case 2: vcam_parse_mode(ln); break; + default: break; + } + } + if (!nl) break; } - if (*p) vcam_parse_line(p); + if (VCAM.running) vcam_note("answered"); + else vcam_note("answered, but no status line"); } /* Called whenever the socket may have moved on, and on the tick to time it @@ -3178,7 +3254,7 @@ static void vcam_pump(void) { if (vcam_io.fd < 0) return; - if (now_ms() - vcam_io.began > 2000) { /* wedged: drop it */ + if (now_ms() - vcam_io.began > 3000) { /* the API says allow 3s */ vcam_io_close(); vcam_down("timed out"); return; @@ -3204,9 +3280,9 @@ static void vcam_pump(void) } if (vcam_io.phase == VC_WRITING) { - while (vcam_io.sent < sizeof(VCAM_REQ) - 1) { - ssize_t w = write(vcam_io.fd, VCAM_REQ + vcam_io.sent, - sizeof(VCAM_REQ) - 1 - vcam_io.sent); + while (vcam_io.sent < vcam_io.reqlen) { + ssize_t w = write(vcam_io.fd, vcam_io.req + vcam_io.sent, + vcam_io.reqlen - vcam_io.sent); if (w > 0) { vcam_io.sent += (size_t)w; continue; } if (w < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) return; if (w < 0 && errno == EINTR) continue; @@ -3223,15 +3299,8 @@ static void vcam_pump(void) space = vcam_io.len < space ? space - vcam_io.len : 0; if (space == 0) break; /* said more than we asked */ r = read(vcam_io.fd, vcam_io.buf + vcam_io.len, space); - if (r > 0) { - vcam_io.len += (size_t)r; - continue; - } - if (r == 0) { /* it said quit and closed */ - vcam_parse(); - vcam_io_close(); - return; - } + if (r > 0) { vcam_io.len += (size_t)r; continue; } + if (r == 0) { vcam_parse(); vcam_io_close(); return; } if (errno == EAGAIN || errno == EWOULDBLOCK) return; if (errno == EINTR) continue; vcam_io_close(); @@ -3245,86 +3314,56 @@ static void vcam_pump(void) static int vcam_poll_fd(void) { return vcam_io.fd; } /* Everything the cell and the menu are drawn from, as one string: a cheap - way to notice a change that matters, including an overlay that only moved. */ + way to notice a change that matters, including a webcam that only moved. */ static void vcam_digest(char *out, size_t cap) { - size_t o = 0; - o += (size_t)snprintf(out + o, cap - o, "%d%d", - (int)VCAM.running, (int)VCAM.recording); - for (int i = 0; i < VCAM.n && o < cap - 40; ++i) - o += (size_t)snprintf(out + o, cap - o, "|%s:%s:%d,%d,%d,%d", - VCAM.ovl[i].kind, VCAM.ovl[i].id, - VCAM.ovl[i].x, VCAM.ovl[i].y, - VCAM.ovl[i].w, VCAM.ovl[i].h); + snprintf(out, cap, "%d%d%d%d%d|%s|%s|%s|%dx%d@%d|%d", + (int)VCAM.running, (int)VCAM.share, (int)VCAM.cam, + (int)VCAM.recording, (int)VCAM.cam_over_screen, + VCAM.share_monitor, VCAM.cam_pos, VCAM.cap_fmt, + VCAM.cap_w, VCAM.cap_h, VCAM.cap_fps, VCAM.nmode); } /* ---------------------------------------------------------- the vcam menu * * The panel that drops out of the bar already knows how to be a block of * text with its own clickable bits — that is what the calendar is. A menu is - * the same shape: one line a row, each with something to run. The rows are + * the same shape: one line a row, each with something to do. The rows are * built from what vcam just said, so what is offered is what makes sense: * no "stop sharing" when nothing is shared, no webcam position when there is * no webcam on screen. */ -#define VCAM_MENU_MAX 14 - -/* The monitors, which live further down with the rest of the Wayland side. */ -static int bar_output_count(void); -static const char *bar_output_name(int i); +#define VCAM_MENU_MAX 16 typedef struct { char label[72]; - char cmd[256]; /* empty: a heading, not a row to press */ + char cmd[256]; /* a vcam command, unless shell is set */ + bool shell; /* run it through the shell instead */ } VcamRow; static VcamRow VMENU[VCAM_MENU_MAX]; static int VMENU_N; -static const char *VCAM_POS[] = { "tr", "br", "bl", "tl", "center", "full" }; +static const char *VCAM_POS[] = { "tr", "br", "bl", "tl", "center", "full" }; static const char *VCAM_POS_SAY[] = { "top right", "bottom right", "bottom left", "top left", "centre", "full frame" }; -/* Where the webcam overlay sits now, as an index into VCAM_POS. The overlay - only knows its pixels, so this reads them back: which half of the frame - each edge is nearer. Unknown comes out as top right, which is where - `vcam-ctl webcam` puts one by default. */ -static int vcam_cam_pos(void) -{ - const VcamOvl *o; - int cx, cy; - if (VCAM.cam < 0) return 0; - o = &VCAM.ovl[VCAM.cam]; - if (o->x <= 2 && o->y <= 2 && o->w >= 640) return 5; /* full frame */ - cx = o->x + o->w / 2; - cy = o->y + o->h / 2; - /* the output is whatever vcam runs at; the overlay's own box is the only - scale we have, so judge against four of its widths across */ - { - int fw = o->w * 4, fh = o->h * 4; - if (cx > fw / 3 && cx < fw * 2 / 3 && cy > fh / 3 && cy < fh * 2 / 3) return 4; - if (cy < fh / 2) return cx < fw / 2 ? 3 : 0; - return cx < fw / 2 ? 2 : 1; - } -} - -/* an overlay by id, or -1 */ -static int vcam_ovl_find(const char *id) +static int vcam_pos_index(void) { - if (!id || !*id) return -1; - for (int i = 0; i < VCAM.n; ++i) - if (!strcmp(VCAM.ovl[i].id, id)) return i; - return -1; + for (int i = 0; i < (int)(sizeof(VCAM_POS) / sizeof(VCAM_POS[0])); ++i) + if (!strcmp(VCAM.cam_pos, VCAM_POS[i])) return i; + return -1; /* custom, or off */ } -static void vmenu_add(const char *label, const char *fmt, ...) +static void vmenu_add_(bool shell, const char *label, const char *fmt, ...) { VcamRow *r; if (VMENU_N >= VCAM_MENU_MAX) return; r = &VMENU[VMENU_N++]; str_set(r->label, sizeof(r->label), label); r->cmd[0] = 0; + r->shell = shell; if (fmt) { va_list ap; va_start(ap, fmt); @@ -3333,6 +3372,9 @@ static void vmenu_add(const char *label, const char *fmt, ...) } } +#define vmenu_add(label, ...) vmenu_add_(false, label, __VA_ARGS__) +#define vmenu_run(label, ...) vmenu_add_(true, label, __VA_ARGS__) + static const char *vcam_ctl(Cell *e) { return *e->src_path ? e->src_path : "vcam-ctl"; @@ -3341,7 +3383,7 @@ static const char *vcam_ctl(Cell *e) /* Build the rows, and the text the panel is sized and drawn from. */ static void vcam_menu_build(Cell *e) { - char out[1024]; + char out[1200]; size_t o = 0; const char *ctl = vcam_ctl(e); char hl[16]; @@ -3350,75 +3392,86 @@ static void vcam_menu_build(Cell *e) snprintf(hl, sizeof(hl), "%02x%02x%02x", cfg.hl.r, cfg.hl.g, cfg.hl.b); if (!VCAM.running) { - vmenu_add("vcam is not running", NULL); + vmenu_add_(false, "vcam is not running", NULL); str_set(e->out_hover, sizeof(e->out_hover), VMENU[0].label); return; } - /* what it is doing, as a heading */ - { + { /* what it is doing, as a heading */ char head[72]; - if (VCAM.screens > 0 && VCAM.cam >= 0) - snprintf(head, sizeof(head), "sharing %d · webcam on", VCAM.screens); - else if (VCAM.screens > 0) - snprintf(head, sizeof(head), "sharing %d", VCAM.screens); + if (VCAM.share && VCAM.cam) + snprintf(head, sizeof(head), "sharing %s \xc2\xb7 webcam on", + VCAM.share_monitor[0] ? VCAM.share_monitor : "a region"); + else if (VCAM.share) + snprintf(head, sizeof(head), "sharing %s", + VCAM.share_monitor[0] ? VCAM.share_monitor : "a region"); else snprintf(head, sizeof(head), "idle"); - vmenu_add(head, NULL); + vmenu_add_(false, head, NULL); } - /* Each share is a toggle. Sharing something gives the overlay an id of - our own — the monitor's name, or "region" — so this can tell which of - these rows is the one already running and offer to stop that one - rather than starting a second of it. */ - if (vcam_ovl_find("region") >= 0) - vmenu_add("stop sharing the region", "%s screen --remove region", ctl); + /* Sharing is one at a time in vcam, so each row is a toggle against + whatever is running: the monitor already shared offers to stop. */ + if (VCAM.share && !VCAM.share_monitor[0]) + vmenu_add("stop sharing the region", "share stop"); else - vmenu_add("share a region\xe2\x80\xa6", "%s screen -i region", ctl); + vmenu_run("share a region\xe2\x80\xa6", + "g=$(slurp -f '%%wx%%h+%%x+%%y') && %s share start \"$g\"", ctl); - for (int i = 0; i < bar_output_count() && VMENU_N < VCAM_MENU_MAX - 4; ++i) { - const char *nm = bar_output_name(i); + for (int i = 0; i < VCAM.nmon && VMENU_N < VCAM_MENU_MAX - 5; ++i) { + const char *nm = VCAM.mon[i].name; char lbl[72]; - if (!nm) continue; - if (vcam_ovl_find(nm) >= 0) { + if (VCAM.share && !strcmp(VCAM.share_monitor, nm)) { snprintf(lbl, sizeof(lbl), "stop sharing %s", nm); - vmenu_add(lbl, "%s screen --remove %s", ctl, nm); + vmenu_add(lbl, "share stop"); } else { snprintf(lbl, sizeof(lbl), "share screen %s", nm); - vmenu_add(lbl, "%s screen --full --monitor %s -i %s", ctl, nm, nm); + vmenu_add(lbl, "share start %s", nm); } } - /* Anything shared that none of those rows covers — started from the - command line, or a second region — gets one row to stop the lot. */ - { - int ours = 0; - for (int i = 0; i < VCAM.n; ++i) { - if (strcmp(VCAM.ovl[i].kind, "screen")) continue; - if (!strcmp(VCAM.ovl[i].id, "region")) { ours++; continue; } - for (int k = 0; k < bar_output_count(); ++k) { - const char *nm = bar_output_name(k); - if (nm && !strcmp(VCAM.ovl[i].id, nm)) { ours++; break; } - } - } - if (VCAM.screens > ours) - vmenu_add("stop sharing everything", "%s screen --remove-all", ctl); + if (VCAM.cam) { + int pos = vcam_pos_index(); + int n = (int)(sizeof(VCAM_POS) / sizeof(VCAM_POS[0])); + int next = pos < 0 ? 0 : (pos + 1) % n; + char lbl[72]; + vmenu_add("webcam off", "cam off"); + if (pos < 0) + snprintf(lbl, sizeof(lbl), "webcam: somewhere \xe2\x86\x92 %s", + VCAM_POS_SAY[next]); + else + snprintf(lbl, sizeof(lbl), "webcam: %s \xe2\x86\x92 %s", + VCAM_POS_SAY[pos], VCAM_POS_SAY[next]); + vmenu_add(lbl, "cam pos %s", VCAM_POS[next]); + } else { + vmenu_add("show the webcam", "cam on"); } - if (VCAM.cam >= 0) { + /* The camera's own resolution, cycled a size at a time. The output size + never changes — apps reading the loopback depend on it — so this is + about what the camera sends: smaller is softer, and cheaper on USB + and CPU. */ + if (VCAM.capture && VCAM.nmode > 0) { + int cur = -1, next; char lbl[72]; - int pos = vcam_cam_pos(); - int next = (pos + 1) % (int)(sizeof(VCAM_POS) / sizeof(VCAM_POS[0])); - vmenu_add("webcam off", "%s webcam off", ctl); - snprintf(lbl, sizeof(lbl), "webcam: %s \xe2\x86\x92 %s", - VCAM_POS_SAY[pos], VCAM_POS_SAY[next]); - vmenu_add(lbl, "%s webcam %s", ctl, VCAM_POS[next]); - } else { - vmenu_add("show the webcam", "%s webcam tr", ctl); + for (int i = 0; i < VCAM.nmode; ++i) + if (VCAM.mode[i].w == VCAM.cap_w && VCAM.mode[i].h == VCAM.cap_h) cur = i; + next = (cur + 1) % VCAM.nmode; + if (VCAM.nmode == 1 && cur == 0) { + snprintf(lbl, sizeof(lbl), "camera: %dx%d @%d (the only one)", + VCAM.cap_w, VCAM.cap_h, VCAM.cap_fps); + vmenu_add_(false, lbl, NULL); + } else { + snprintf(lbl, sizeof(lbl), "camera: %dx%d \xe2\x86\x92 %dx%d @%d", + VCAM.cap_w, VCAM.cap_h, + VCAM.mode[next].w, VCAM.mode[next].h, VCAM.mode[next].fps); + vmenu_add(lbl, "capture set %dx%d %d", + VCAM.mode[next].w, VCAM.mode[next].h, VCAM.mode[next].fps); + } } - if (VCAM.recording) vmenu_add("stop recording", "%s record stop", ctl); - else vmenu_add("record the loopback", "%s record start", ctl); + if (VCAM.recording) vmenu_run("stop recording", "%s record stop", ctl); + else vmenu_run("record the loopback", "%s record start", ctl); for (int i = 0; i < VMENU_N; ++i) { const char *sep = i + 1 < VMENU_N ? "\n" : ""; @@ -3429,15 +3482,11 @@ static void vcam_menu_build(Cell *e) else o += (size_t)snprintf(out + o, sizeof(out) - o, "%s%s", VMENU[i].label, sep); - if (o >= sizeof(out) - 80) break; + if (o >= sizeof(out) - 90) break; } str_set(e->out_hover, sizeof(e->out_hover), out); } -static bool cell_is_menu(const Cell *e) { return e->source == SRC_VCAM; } - - - /* What the cell says in the full bar. Short on purpose: the detail lives in the menu, and a bar is not the place for a sentence. */ static void vcam_read(Cell *e) @@ -3445,12 +3494,10 @@ static void vcam_read(Cell *e) e->state = VCAM.running ? PS_PLAYING : PS_STOPPED; if (!VCAM.running) { e->out[0] = 0; return; } - if (VCAM.screens > 0 && VCAM.cam >= 0) - snprintf(e->out, sizeof(e->out), "%s share+cam", e->name); - else if (VCAM.screens > 0) - snprintf(e->out, sizeof(e->out), "%s share", e->name); - else - snprintf(e->out, sizeof(e->out), "%s", e->name); + if (VCAM.share && VCAM.cam) snprintf(e->out, sizeof(e->out), "%s share+cam", e->name); + else if (VCAM.share) snprintf(e->out, sizeof(e->out), "%s share", e->name); + else if (VCAM.cam) snprintf(e->out, sizeof(e->out), "%s cam", e->name); + else snprintf(e->out, sizeof(e->out), "%s", e->name); if (VCAM.recording) { size_t n = strlen(e->out); @@ -3458,6 +3505,9 @@ static void vcam_read(Cell *e) } } +static bool cell_is_menu(const Cell *e) { return e->source == SRC_VCAM; } + + static void window_read(Cell *e) { e->out[0] = 0; @@ -4003,13 +4053,6 @@ static struct zwlr_layer_shell_v1 *layer_shell; static Output outputs[MAX_OUTPUTS]; static int output_count = 0; -static int bar_output_count(void) { return output_count; } -static const char *bar_output_name(int i) -{ - if (i < 0 || i >= output_count) return NULL; - if (!outputs[i].alive || !*outputs[i].name) return NULL; - return outputs[i].name; -} static Bar *pointer_bar = NULL; static bool running = true; @@ -6238,11 +6281,19 @@ static bool menu_click(Bar *b, int button) if (b->px < b->mrow[i].x0 || b->px >= b->mrow[i].x1) continue; if (b->py < b->mrow[i].y0 || b->py >= b->mrow[i].y1) continue; if (!*VMENU[i].cmd) return true; /* the heading: swallow it */ - run_command(VMENU[i].cmd); - /* Ask again shortly: vcam needs a moment to act, and the menu is - built from what it says. The panel stays open, because setting a - screen up is several of these in a row. */ - cfg.cell[b->tip_showing].next_run = now_ms() + 200; + if (VMENU[i].shell) { + /* the two that cannot go down the socket: picking a region needs + slurp, and recording is vcam-ctl's business */ + run_command(VMENU[i].cmd); + cfg.cell[b->tip_showing].next_run = now_ms() + 400; + } else { + /* Down the socket, on the next trip — which also reads the new + state back, so the menu redraws from what vcam says it did + rather than from what we hoped. The panel stays open, because + setting a share up is several of these in a row. */ + vcam_do(VMENU[i].cmd); + cfg.cell[b->tip_showing].next_run = 0; /* go now */ + } b->dirty = true; return true; } @@ -6613,8 +6664,9 @@ static void usage(void) " and recording are facts. Gone while vcam is. Folded\n" " it is lamps: green running, a second red one while\n" " recording. Click it for a menu: share a region or a\n" -" monitor, stop sharing, show and move the webcam\n" -" overlay, record. NAME.src_path= finds vcam-ctl\n" +" monitor, stop sharing, show and move the webcam,\n" +" step the camera resolution, record. NAME.src_path=\n" +" finds vcam-ctl, for the two rows that shell out\n" " NAME.source=cmus swbr runs cmus-remote itself: playing and paused are\n" " read from it rather than guessed from the text, and\n" " the command, interval, slim=media and the mouse binds\n" @@ -6887,10 +6939,18 @@ static void probe_report(void) vcam_read(&cfg.cell[i]); printf("vcam socket %s -> %s\n", vcam_sock_path(), *vcam_io.why ? vcam_io.why : "no answer"); - if (VCAM.running) - printf(" %d overlay(s), %d shared, webcam %s, %s\n", - VCAM.n, VCAM.screens, VCAM.cam >= 0 ? "on" : "off", + if (VCAM.running) { + printf(" sharing %s, webcam %s%s%s, %s\n", + VCAM.share ? (VCAM.share_monitor[0] ? VCAM.share_monitor + : "a region") : "nothing", + VCAM.cam ? "on" : "off", + VCAM.cam && VCAM.cam_pos[0] ? " at " : "", + VCAM.cam && VCAM.cam_pos[0] ? VCAM.cam_pos : "", VCAM.recording ? "recording" : "not recording"); + printf(" camera %s %dx%d @%d, %d monitor(s), %d size(s) offered\n", + VCAM.cap_fmt[0] ? VCAM.cap_fmt : "-", + VCAM.cap_w, VCAM.cap_h, VCAM.cap_fps, VCAM.nmon, VCAM.nmode); + } } for (int i = 0; i < cfg.cell_count; ++i) {