Enable MT6797 Wi-Fi/BT, PipeWire audio GUI, cursor autohide, and dev tools

- Add MT6797 CONSYS Wi-Fi/BT factory-firmware patch (0005), native EAPOL TX, 5 GHz NO_IR/regulatory-notifier fixes, TKIP/CCMP admission, initramfs regdb, and geminux-wifi-nvram
- Order OpenRC polkit startup before NetworkManager/ModemManager and harden geminux-net with non-blocking scans, mesh SSID grouping, password masking toggle, and BT A2DP/HFP/LE/HID support
- Add geminux-audio PipeWire/WirePlumber/UCM2 playback & recording control GUI alongside st and dmenu XFixes cursor autohide
- Preinstall requested CLI/dev tools (bash, coreutils, build-base, git, nmtui, perl/SSL, curl-dev, janet, jpm, etc.) and set bash as system-wide default shell
This commit is contained in:
Luxferre committed 2026-11-22 22:31:41 +02:00
1 parent ba7b86e570
commit 6aea73cbeb
92 files changed
+5524 -198

No files matched your search

+2 -2
View File
@@ -6,8 +6,8 @@ MANPREFIX = $(PREFIX)/share/man
PKG_CONFIG ?= pkg-config
INCS = $(shell $(PKG_CONFIG) --cflags fontconfig freetype2 xft pango pangoxft harfbuzz x11 xinerama)
LIBS = -lm $(shell $(PKG_CONFIG) --libs fontconfig freetype2 xft pango pangoxft harfbuzz x11 xinerama)
INCS = $(shell $(PKG_CONFIG) --cflags fontconfig freetype2 xft pango pangoxft harfbuzz x11 xfixes xi xinerama)
LIBS = -lm $(shell $(PKG_CONFIG) --libs fontconfig freetype2 xft pango pangoxft harfbuzz x11 xfixes xi xinerama)
CPPFLAGS = -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" -DXINERAMA
CFLAGS = -std=gnu99 -pedantic -Wall -O2 $(INCS) $(CPPFLAGS)
+88 -1
View File
@@ -15,6 +15,9 @@
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/XInput2.h>
#ifdef XINERAMA
#include <X11/extensions/Xinerama.h>
#endif
@@ -58,6 +61,8 @@ static Drw *drw;
static Clr *scheme[SchemeLast];
static char xres_font[256] = "";
static char *dyn_colors[SchemeLast][2] = { {NULL, NULL}, {NULL, NULL}, {NULL, NULL} };
static Cursor vpointer = None, bpointer = None;
static int pointer_hidden = 0, xi_opcode = -1, xfixes_ok = 0;
#include "config.h"
@@ -175,9 +180,53 @@ static int max_textw(void) {
return len;
}
static void dmenu_xi_raw_select(int enable) {
unsigned char mask[XIMaskLen(XI_LASTEVENT)];
XIEventMask evmask;
if (xi_opcode < 0) return;
memset(mask, 0, sizeof(mask));
if (enable) {
XISetMask(mask, XI_RawMotion);
XISetMask(mask, XI_RawButtonPress);
XISetMask(mask, XI_RawTouchBegin);
}
evmask.deviceid = XIAllMasterDevices;
evmask.mask_len = (int)sizeof(mask);
evmask.mask = mask;
XISelectEvents(dpy, root, &evmask, 1);
}
static void hidecursor(void) {
if (pointer_hidden) return;
if (bpointer != None) XDefineCursor(dpy, win, bpointer);
if (xfixes_ok) XFixesHideCursor(dpy, root);
dmenu_xi_raw_select(1);
pointer_hidden = 1;
XFlush(dpy);
}
static void showcursor(void) {
if (!pointer_hidden) return;
pointer_hidden = 0;
dmenu_xi_raw_select(0);
if (xfixes_ok) XFixesShowCursor(dpy, root);
if (vpointer != None) XDefineCursor(dpy, win, vpointer);
XFlush(dpy);
}
static void cleanup(void) {
size_t i;
showcursor();
XUngrabKey(dpy, AnyKey, AnyModifier, root);
if (vpointer != None) {
XFreeCursor(dpy, vpointer);
vpointer = None;
}
if (bpointer != None) {
XFreeCursor(dpy, bpointer);
bpointer = None;
}
for (i = 0; i < SchemeLast; i++) {
free(scheme[i]);
free(dyn_colors[i][0]);
@@ -421,11 +470,13 @@ static void keypress(XKeyEvent *ev) {
default:
return;
case XLookupChars:
hidecursor();
goto insert_chars;
case XLookupKeySym:
case XLookupBoth:
break;
}
if (!IsModifierKey(ksym)) hidecursor();
if (ev->state & ControlMask) {
switch (ksym) {
@@ -605,6 +656,7 @@ static void buttonpress(XEvent *e) {
XButtonPressedEvent *ev = &e->xbutton;
int x = 0, y = 0, w;
showcursor();
if (ev->window != win) return;
if (ev->button == Button4) {
if (sel && sel->left) {
@@ -730,6 +782,17 @@ static void readstdin(void) {
static void run(void) {
XEvent ev;
while (!XNextEvent(dpy, &ev)) {
if (ev.type == GenericEvent) {
if (ev.xcookie.extension == xi_opcode &&
XGetEventData(dpy, &ev.xcookie)) {
if (ev.xcookie.evtype == XI_RawMotion ||
ev.xcookie.evtype == XI_RawButtonPress ||
ev.xcookie.evtype == XI_RawTouchBegin)
showcursor();
XFreeEventData(dpy, &ev.xcookie);
}
continue;
}
if (XFilterEvent(&ev, win)) continue;
switch (ev.type) {
case DestroyNotify:
@@ -749,6 +812,9 @@ static void run(void) {
case ButtonPress:
buttonpress(&ev);
break;
case MotionNotify:
showcursor();
break;
case SelectionNotify:
if (ev.xselection.property == utf8) pastesel();
break;
@@ -826,12 +892,33 @@ static void setup(void) {
swa.override_redirect = True;
swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | VisibilityChangeMask;
swa.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | PointerMotionMask | VisibilityChangeMask;
win = XCreateWindow(dpy, parentwin, x, y, (unsigned int)mw, (unsigned int)mh, 0,
CopyFromParent, CopyFromParent, CopyFromParent,
CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
XSetClassHint(dpy, win, &ch);
{
int ev_base = 0, err_base = 0;
char blank_bits[1] = {0};
Pixmap blank = XCreateBitmapFromData(dpy, win, blank_bits, 1, 1);
XColor black;
memset(&black, 0, sizeof(black));
xfixes_ok = XFixesQueryExtension(dpy, &ev_base, &err_base) ? 1 : 0;
xi_opcode = -1;
if (XQueryExtension(dpy, "XInputExtension", &xi_opcode, &ev_base, &err_base)) {
int maj = 2, min = 2;
if (XIQueryVersion(dpy, &maj, &min) != Success)
xi_opcode = -1;
} else {
xi_opcode = -1;
}
vpointer = XCreateFontCursor(dpy, XC_left_ptr);
bpointer = XCreatePixmapCursor(dpy, blank, blank, &black, &black, 0, 0);
XFreePixmap(dpy, blank);
if (vpointer != None) XDefineCursor(dpy, win, vpointer);
}
if ((xim = XOpenIM(dpy, NULL, NULL, NULL)) == NULL)
die("XOpenIM failed: could not open input device");
xic = XCreateIC(xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing,
+3
View File
@@ -33,6 +33,7 @@ static const Rule rules[] = {
{ "geminux-callscreen", NULL, NULL, ~0U, 0, -1 },
{ "geminux-lock", NULL, NULL, ~0U, 1, -1 },
{ "geminux-control", NULL, NULL, 0, 1, -1 },
{ "geminux-audio", NULL, NULL, 0, 1, -1 },
{ "geminux-themes", NULL, NULL, 0, 1, -1 },
};
@@ -67,6 +68,7 @@ static const char *controlcmd[] = { "geminux-control", NULL };
static const char *dialercmd[] = { "geminux-dialer", NULL };
static const char *smscmd[] = { "geminux-sms", NULL };
static const char *netcmd[] = { "geminux-net", NULL };
static const char *audiocmd[] = { "geminux-audio", NULL };
static const char *cameracmd[] = { "geminux-camera", NULL };
static const char *themescmd[] = { "geminux-themes", NULL };
static const char *lockcmd[] = { "geminux-lock", NULL };
@@ -98,6 +100,7 @@ static const Key keys[] = {
{ MODKEY, XK_d, spawn, {.v = dialercmd} },
{ MODKEY, XK_s, spawn, {.v = smscmd} },
{ MODKEY, XK_n, spawn, {.v = netcmd} },
{ MODKEY, XK_a, spawn, {.v = audiocmd} },
{ MODKEY, XK_v, spawn, {.v = cameracmd} },
{ MODKEY, XK_t, spawn, {.v = themescmd} },
{ MODKEY|ShiftMask, XK_l, spawn, {.v = lockcmd} },
+7
View File
@@ -13,6 +13,7 @@ APPS = \
geminux-sms \
geminux-contacts \
geminux-net \
geminux-audio \
geminux-camera \
geminux-control \
geminux-themes \
@@ -23,6 +24,12 @@ all: $(APPS)
geminux-camera: geminux-camera.c ../libgemui/gemui.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS) -ljpeg
geminux-net: geminux-net.c geminux-wifi-list.h geminux-bt-list.h ../libgemui/gemui.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
geminux-audio: geminux-audio.c geminux-audio-parse.h ../libgemui/gemui.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
geminux-%: geminux-%.c ../libgemui/gemui.h
$(CC) $(CFLAGS) -o $@ $< $(LIBS)
+284
View File
@@ -0,0 +1,284 @@
/*
* Header-only C11 parsers for PipeWire / wpctl / pactl audio endpoints, streams, and card profiles.
*/
#ifndef GEMINUX_AUDIO_PARSE_H
#define GEMINUX_AUDIO_PARSE_H
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GEMINUX_MAX_AUDIO_NODES 24
#define GEMINUX_MAX_AUDIO_PROFILES 32
typedef struct {
int index;
char name[128];
char label[128];
char state[24];
char port[64];
int vol_pct;
bool muted;
bool is_default;
bool is_stream;
} geminux_audio_node_t;
typedef struct {
char card_name[128];
char card_label[128];
char profile_name[96];
char profile_desc[128];
bool active;
bool available;
} geminux_audio_profile_t;
static inline bool geminux_parse_wpctl_volume(const char *text, int *vol_pct_out, bool *muted_out) {
if (!text) return false;
const char *p = strstr(text, "Volume:");
if (!p) return false;
double v = atof(p + 7);
if (v < 0.0) v = 0.0;
if (v > 2.0) v = 2.0;
if (vol_pct_out) *vol_pct_out = (int)(v * 100.0 + 0.5);
if (muted_out) *muted_out = (strstr(text, "[MUTED]") != NULL);
return true;
}
static inline void geminux_parse_pactl_defaults(const char *info_txt,
char *def_sink, size_t sink_sz,
char *def_src, size_t src_sz) {
if (def_sink && sink_sz > 0) def_sink[0] = '\0';
if (def_src && src_sz > 0) def_src[0] = '\0';
if (!info_txt) return;
const char *cur = info_txt;
while (*cur) {
const char *nl = strchr(cur, '\n');
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
char ln[256];
size_t cp = raw_len < sizeof(ln) - 1 ? raw_len : sizeof(ln) - 1;
memcpy(ln, cur, cp);
ln[cp] = '\0';
cur = nl ? nl + 1 : cur + raw_len;
while (cp > 0 && (ln[cp - 1] == '\r' || ln[cp - 1] == ' ')) ln[--cp] = '\0';
if (def_sink && strncmp(ln, "Default Sink:", 13) == 0) {
const char *v = ln + 13;
while (*v == ' ' || *v == '\t') v++;
snprintf(def_sink, sink_sz, "%s", v);
} else if (def_src && strncmp(ln, "Default Source:", 15) == 0) {
const char *v = ln + 15;
while (*v == ' ' || *v == '\t') v++;
snprintf(def_src, src_sz, "%s", v);
}
}
}
static inline int geminux_extract_first_pct(const char *s) {
if (!s) return 100;
for (const char *p = s; *p; p++) {
if (*p == '%' && p > s && isdigit((unsigned char)p[-1])) {
const char *q = p;
while (q > s && isdigit((unsigned char)q[-1])) q--;
int pct = atoi(q);
if (pct < 0) pct = 0;
if (pct > 200) pct = 200;
return pct;
}
}
return 100;
}
static inline bool geminux_extract_quoted_prop(const char *line, const char *key,
char *out, size_t out_sz) {
size_t klen = strlen(key);
if (strncmp(line, key, klen) != 0) return false;
const char *p = line + klen;
while (*p == ' ' || *p == '\t') p++;
if (*p != '=') return false;
p++;
while (*p == ' ' || *p == '\t') p++;
if (*p == '"') {
p++;
const char *end = strrchr(p, '"');
size_t len = end ? (size_t)(end - p) : strlen(p);
if (len >= out_sz) len = out_sz - 1;
memcpy(out, p, len);
out[len] = '\0';
} else {
snprintf(out, out_sz, "%s", p);
}
return out[0] != '\0';
}
static inline int geminux_parse_pactl_nodes(const char *txt, const char *header_prefix,
const char *default_name, bool is_stream,
geminux_audio_node_t *out, int max_out) {
if (!txt || !header_prefix || !out || max_out <= 0) return 0;
size_t hlen = strlen(header_prefix);
int count = 0;
geminux_audio_node_t *cur_node = NULL;
char app_name[64] = {0};
char media_name[64] = {0};
bool in_props = false;
const char *cur = txt;
while (*cur) {
const char *nl = strchr(cur, '\n');
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
char ln[384];
size_t cp = raw_len < sizeof(ln) - 1 ? raw_len : sizeof(ln) - 1;
memcpy(ln, cur, cp);
ln[cp] = '\0';
cur = nl ? nl + 1 : cur + raw_len;
while (cp > 0 && (ln[cp - 1] == '\r' || ln[cp - 1] == ' ')) ln[--cp] = '\0';
if (strncmp(ln, header_prefix, hlen) == 0) {
if (count >= max_out) {
cur_node = NULL;
continue;
}
cur_node = &out[count++];
memset(cur_node, 0, sizeof(*cur_node));
cur_node->index = atoi(ln + hlen);
cur_node->vol_pct = 100;
cur_node->is_stream = is_stream;
app_name[0] = '\0';
media_name[0] = '\0';
in_props = false;
continue;
}
if (!cur_node) continue;
if (ln[0] == '\t' && ln[1] != '\t') {
in_props = false;
const char *k = ln + 1;
if (strcmp(k, "Properties:") == 0) {
in_props = true;
} else if (strncmp(k, "State: ", 7) == 0) {
snprintf(cur_node->state, sizeof(cur_node->state), "%s", k + 7);
} else if (strncmp(k, "Name: ", 6) == 0) {
snprintf(cur_node->name, sizeof(cur_node->name), "%s", k + 6);
if (default_name && default_name[0] && strcmp(cur_node->name, default_name) == 0) {
cur_node->is_default = true;
}
if (!cur_node->label[0]) {
snprintf(cur_node->label, sizeof(cur_node->label), "%s", cur_node->name);
}
} else if (strncmp(k, "Description: ", 13) == 0) {
snprintf(cur_node->label, sizeof(cur_node->label), "%s", k + 13);
} else if (strncmp(k, "Mute: ", 6) == 0) {
cur_node->muted = (strcmp(k + 6, "yes") == 0);
} else if (strncmp(k, "Volume: ", 8) == 0) {
cur_node->vol_pct = geminux_extract_first_pct(k + 8);
} else if (strncmp(k, "Active Port: ", 13) == 0) {
snprintf(cur_node->port, sizeof(cur_node->port), "%s", k + 13);
}
} else if (in_props && ln[0] == '\t' && ln[1] == '\t') {
const char *k = ln + 2;
if (!geminux_extract_quoted_prop(k, "application.name", app_name, sizeof(app_name))) {
geminux_extract_quoted_prop(k, "media.name", media_name, sizeof(media_name));
}
if (is_stream) {
if (app_name[0] && media_name[0] && strcmp(app_name, media_name) != 0) {
snprintf(cur_node->label, sizeof(cur_node->label), "%.56s: %.56s", app_name, media_name);
} else if (app_name[0]) {
snprintf(cur_node->label, sizeof(cur_node->label), "%s", app_name);
} else if (media_name[0]) {
snprintf(cur_node->label, sizeof(cur_node->label), "%s", media_name);
}
} else if (!cur_node->label[0]) {
geminux_extract_quoted_prop(k, "device.description", cur_node->label, sizeof(cur_node->label));
}
}
}
return count;
}
static inline int geminux_parse_pactl_cards(const char *cards_txt,
geminux_audio_profile_t *out, int max_out) {
if (!cards_txt || !out || max_out <= 0) return 0;
int total = 0;
int card_start = 0;
char card_name[128] = {0};
char card_label[128] = {0};
char active_prof[96] = {0};
int sec = 0; /* 1=Properties, 2=Profiles */
const char *cur = cards_txt;
while (*cur) {
const char *nl = strchr(cur, '\n');
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
char ln[384];
size_t cp = raw_len < sizeof(ln) - 1 ? raw_len : sizeof(ln) - 1;
memcpy(ln, cur, cp);
ln[cp] = '\0';
cur = nl ? nl + 1 : cur + raw_len;
while (cp > 0 && (ln[cp - 1] == '\r' || ln[cp - 1] == ' ')) ln[--cp] = '\0';
if (strncmp(ln, "Card #", 6) == 0) {
card_start = total;
card_name[0] = '\0';
card_label[0] = '\0';
active_prof[0] = '\0';
sec = 0;
continue;
}
if (ln[0] == '\t' && ln[1] != '\t') {
const char *k = ln + 1;
sec = 0;
if (strcmp(k, "Properties:") == 0) {
sec = 1;
} else if (strcmp(k, "Profiles:") == 0) {
sec = 2;
} else if (strncmp(k, "Name: ", 6) == 0) {
snprintf(card_name, sizeof(card_name), "%s", k + 6);
if (!card_label[0]) snprintf(card_label, sizeof(card_label), "%s", card_name);
for (int i = card_start; i < total; i++) {
snprintf(out[i].card_name, sizeof(out[i].card_name), "%s", card_name);
}
} else if (strncmp(k, "Active Profile: ", 16) == 0) {
snprintf(active_prof, sizeof(active_prof), "%s", k + 16);
for (int i = card_start; i < total; i++) {
out[i].active = (strcmp(out[i].profile_name, active_prof) == 0);
}
}
} else if (sec == 1 && ln[0] == '\t' && ln[1] == '\t') {
const char *k = ln + 2;
if (geminux_extract_quoted_prop(k, "device.description", card_label, sizeof(card_label)) ||
(!card_label[0] && geminux_extract_quoted_prop(k, "alsa.card_name", card_label, sizeof(card_label)))) {
for (int i = card_start; i < total; i++) {
snprintf(out[i].card_label, sizeof(out[i].card_label), "%s", card_label);
}
}
} else if (sec == 2 && ln[0] == '\t' && ln[1] == '\t' && total < max_out) {
const char *k = ln + 2;
const char *sep = strstr(k, ": ");
if (!sep) continue;
geminux_audio_profile_t *p = &out[total++];
memset(p, 0, sizeof(*p));
snprintf(p->card_name, sizeof(p->card_name), "%s", card_name);
snprintf(p->card_label, sizeof(p->card_label), "%s", card_label[0] ? card_label : card_name);
size_t idlen = (size_t)(sep - k);
if (idlen >= sizeof(p->profile_name)) idlen = sizeof(p->profile_name) - 1;
memcpy(p->profile_name, k, idlen);
p->profile_name[idlen] = '\0';
const char *rhs = sep + 2;
p->available = (strstr(rhs, "available: no") == NULL);
const char *meta = strstr(rhs, " (sinks:");
if (!meta) meta = strstr(rhs, " (available:");
size_t dlen = meta ? (size_t)(meta - rhs) : strlen(rhs);
if (dlen >= sizeof(p->profile_desc)) dlen = sizeof(p->profile_desc) - 1;
memcpy(p->profile_desc, rhs, dlen);
p->profile_desc[dlen] = '\0';
if (active_prof[0] && strcmp(p->profile_name, active_prof) == 0) {
p->active = true;
}
}
}
return total;
}
#endif /* GEMINUX_AUDIO_PARSE_H */
+604
View File
@@ -0,0 +1,604 @@
#define _POSIX_C_SOURCE 200809L
#include "gemui.h"
#include "geminux-audio-parse.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HIT_TAB 1
#define HIT_SLIDER 2
#define HIT_MUTE_DEF 3
#define HIT_TEST 4
#define HIT_ROUTE_A 5
#define HIT_ROUTE_B 6
#define HIT_GOTO_CARDS 7
#define HIT_ROW 8
#define HIT_ACT_DEF 9
#define HIT_ACT_MUTE 10
#define HIT_ACT_DOWN 11
#define HIT_ACT_UP 12
#define HIT_ACT_REF 13
typedef struct {
bool is_stream;
int index;
int vol_pct;
bool muted;
bool is_default;
char name[128];
char label[128];
char col2[64];
char col3[48];
} audio_row_t;
typedef struct {
gemui_app_t *app;
int tab; /* 0=Playback, 1=Recording, 2=Cards & BT Profiles */
int out_vol_pct;
bool out_muted;
int in_vol_pct;
bool in_muted;
bool speaker_on;
bool headset_mic_on;
char def_sink[128];
char def_source[128];
audio_row_t rows[GEMINUX_MAX_AUDIO_NODES * 2];
geminux_audio_profile_t profiles[GEMINUX_MAX_AUDIO_PROFILES];
int num_rows;
int num_profiles;
int sel_row;
int scroll_off;
gemui_rect_t slider_rect;
bool dragging_slider;
char status_text[256];
} audio_state_t;
static int audio_visible_rows(const audio_state_t *st) {
int h = st->app ? gemui_height(st->app) : 940;
int list_y = (st->tab == 2) ? 260 : 366;
int box_h = h - list_y - 112;
int vis = (box_h - 12) / 72;
return vis > 0 ? vis : 1;
}
static void audio_clamp_scroll(audio_state_t *st) {
int vis = audio_visible_rows(st);
int count = (st->tab == 2) ? st->num_profiles : st->num_rows;
int max_off = count > vis ? count - vis : 0;
if (st->scroll_off < 0) st->scroll_off = 0;
if (st->scroll_off > max_off) st->scroll_off = max_off;
if (st->sel_row >= 0 && st->sel_row < count) {
if (st->sel_row < st->scroll_off) st->scroll_off = st->sel_row;
else if (st->sel_row >= st->scroll_off + vis) st->scroll_off = st->sel_row - vis + 1;
}
}
static void append_nodes_to_rows(audio_state_t *st, const geminux_audio_node_t *nodes,
int n, bool skip_monitors, const char *prev_name) {
for (int i = 0; i < n && st->num_rows < (int)(sizeof(st->rows) / sizeof(st->rows[0])); i++) {
const geminux_audio_node_t *nd = &nodes[i];
if (skip_monitors && !nd->is_stream && strstr(nd->name, ".monitor") != NULL) {
continue;
}
int ridx = st->num_rows++;
audio_row_t *r = &st->rows[ridx];
memset(r, 0, sizeof(*r));
r->is_stream = nd->is_stream;
r->index = nd->index;
r->vol_pct = nd->vol_pct;
r->muted = nd->muted;
r->is_default = nd->is_default;
snprintf(r->name, sizeof(r->name), "%s", nd->name);
snprintf(r->label, sizeof(r->label), "%s%s",
nd->is_stream ? "[APP] " : (nd->is_default ? "* " : " "),
nd->label[0] ? nd->label : nd->name);
snprintf(r->col2, sizeof(r->col2), "%s",
nd->is_stream ? "App Stream" : (nd->is_default ? "Default" : (nd->state[0] ? nd->state : "Ready")));
snprintf(r->col3, sizeof(r->col3), "%d%%%s", nd->vol_pct, nd->muted ? " MUTED" : "");
if (prev_name[0] && r->name[0] && strcmp(r->name, prev_name) == 0) {
st->sel_row = ridx;
}
}
}
static void refresh_alsa_hints(audio_state_t *st) {
char out[512] = {0};
const char *sp[] = {"amixer", "-c", "0", "cget", "name='Speaker Switch'", NULL};
if (gemui_run_cmd_timeout(sp, out, sizeof(out), 700) == 0) {
st->speaker_on = (strstr(out, ": values=on") != NULL || strstr(out, ": values=1") != NULL);
}
out[0] = '\0';
const char *mic[] = {"amixer", "-c", "0", "cget", "name='Headset Mic Switch'", NULL};
if (gemui_run_cmd_timeout(mic, out, sizeof(out), 700) == 0) {
st->headset_mic_on = (strstr(out, ": values=on") != NULL || strstr(out, ": values=1") != NULL);
}
}
static void refresh_audio(audio_state_t *st) {
char prev_name[128] = {0};
if (st->tab == 2 && st->sel_row >= 0 && st->sel_row < st->num_profiles) {
snprintf(prev_name, sizeof(prev_name), "%s", st->profiles[st->sel_row].profile_name);
} else if (st->tab != 2 && st->sel_row >= 0 && st->sel_row < st->num_rows) {
snprintf(prev_name, sizeof(prev_name), "%s", st->rows[st->sel_row].name);
}
char out[256] = {0};
const char *vo[] = {"wpctl", "get-volume", "@DEFAULT_AUDIO_SINK@", NULL};
if (gemui_run_cmd_timeout(vo, out, sizeof(out), 900) == 0) {
geminux_parse_wpctl_volume(out, &st->out_vol_pct, &st->out_muted);
}
out[0] = '\0';
const char *vi[] = {"wpctl", "get-volume", "@DEFAULT_AUDIO_SOURCE@", NULL};
if (gemui_run_cmd_timeout(vi, out, sizeof(out), 900) == 0) {
geminux_parse_wpctl_volume(out, &st->in_vol_pct, &st->in_muted);
}
refresh_alsa_hints(st);
static char big[32768];
big[0] = '\0';
const char *pi[] = {"pactl", "info", NULL};
if (gemui_run_cmd_timeout(pi, big, sizeof(big), 900) == 0) {
geminux_parse_pactl_defaults(big, st->def_sink, sizeof(st->def_sink),
st->def_source, sizeof(st->def_source));
}
st->num_rows = 0;
st->num_profiles = 0;
st->sel_row = -1;
if (st->tab == 0) {
geminux_audio_node_t sinks[GEMINUX_MAX_AUDIO_NODES];
geminux_audio_node_t inputs[GEMINUX_MAX_AUDIO_NODES];
big[0] = '\0';
const char *ps[] = {"pactl", "list", "sinks", NULL};
if (gemui_run_cmd_timeout(ps, big, sizeof(big), 1200) == 0) {
int ns = geminux_parse_pactl_nodes(big, "Sink #", st->def_sink, false,
sinks, GEMINUX_MAX_AUDIO_NODES);
append_nodes_to_rows(st, sinks, ns, false, prev_name);
}
big[0] = '\0';
const char *psi[] = {"pactl", "list", "sink-inputs", NULL};
if (gemui_run_cmd_timeout(psi, big, sizeof(big), 1200) == 0) {
int ni = geminux_parse_pactl_nodes(big, "Sink Input #", NULL, true,
inputs, GEMINUX_MAX_AUDIO_NODES);
append_nodes_to_rows(st, inputs, ni, false, prev_name);
}
snprintf(st->status_text, sizeof(st->status_text),
"PipeWire Output: %d%% %s | Route: %s",
st->out_vol_pct, st->out_muted ? "[MUTED]" : "",
st->speaker_on ? "Speakers" : "Headphones");
} else if (st->tab == 1) {
geminux_audio_node_t srcs[GEMINUX_MAX_AUDIO_NODES];
geminux_audio_node_t souts[GEMINUX_MAX_AUDIO_NODES];
big[0] = '\0';
const char *ps[] = {"pactl", "list", "sources", NULL};
if (gemui_run_cmd_timeout(ps, big, sizeof(big), 1200) == 0) {
int ns = geminux_parse_pactl_nodes(big, "Source #", st->def_source, false,
srcs, GEMINUX_MAX_AUDIO_NODES);
append_nodes_to_rows(st, srcs, ns, true, prev_name);
}
big[0] = '\0';
const char *pso[] = {"pactl", "list", "source-outputs", NULL};
if (gemui_run_cmd_timeout(pso, big, sizeof(big), 1200) == 0) {
int no = geminux_parse_pactl_nodes(big, "Source Output #", NULL, true,
souts, GEMINUX_MAX_AUDIO_NODES);
append_nodes_to_rows(st, souts, no, false, prev_name);
}
snprintf(st->status_text, sizeof(st->status_text),
"PipeWire Recording: %d%% %s | Mic: %s",
st->in_vol_pct, st->in_muted ? "[MUTED]" : "",
st->headset_mic_on ? "Headset Mic" : "Internal Mic");
} else {
big[0] = '\0';
const char *pc[] = {"pactl", "list", "cards", NULL};
if (gemui_run_cmd_timeout(pc, big, sizeof(big), 1200) == 0) {
st->num_profiles = geminux_parse_pactl_cards(big, st->profiles, GEMINUX_MAX_AUDIO_PROFILES);
for (int i = 0; i < st->num_profiles; i++) {
if (prev_name[0] && strcmp(st->profiles[i].profile_name, prev_name) == 0) {
st->sel_row = i;
break;
}
}
}
snprintf(st->status_text, sizeof(st->status_text),
"Audio Card & Bluetooth Profiles (A2DP / HSP / HFP / HiFi)");
}
audio_clamp_scroll(st);
}
static void apply_default_slider(audio_state_t *st, double norm) {
if (norm < 0.0) norm = 0.0;
if (norm > 1.0) norm = 1.0;
int max_pct = (st->tab == 1) ? 150 : 125;
int pct = (int)(norm * max_pct + 0.5);
char arg[32];
snprintf(arg, sizeof(arg), "%d%%", pct);
const char *target = (st->tab == 1) ? "@DEFAULT_AUDIO_SOURCE@" : "@DEFAULT_AUDIO_SINK@";
const char *limit = (st->tab == 1) ? "1.5" : "1.25";
const char *cmd[] = {"wpctl", "set-volume", "-l", limit, target, arg, NULL};
gemui_run_cmd_timeout(cmd, NULL, 0, 800);
if (st->tab == 1) {
st->in_vol_pct = pct;
} else {
st->out_vol_pct = pct;
const char *pk[] = {"pkill", "-USR1", "geminux-statusd", NULL};
gemui_run_cmd_timeout(pk, NULL, 0, 500);
}
gemui_request_redraw(st->app);
}
static void adjust_selected_node(audio_state_t *st, int action) {
if (st->sel_row < 0 || st->sel_row >= st->num_rows) {
gemui_show_toast(st->app, "Select a device or stream row first", 1600);
return;
}
const audio_row_t *r = &st->rows[st->sel_row];
char idx[24];
snprintf(idx, sizeof(idx), "%d", r->index);
if (action == HIT_ACT_DEF) {
if (r->is_stream || !r->name[0]) {
gemui_show_toast(st->app, "Select a hardware/Bluetooth device row", 1800);
return;
}
const char *cmd[] = {"pactl", st->tab == 1 ? "set-default-source" : "set-default-sink",
r->name, NULL};
int rc = gemui_run_cmd_timeout(cmd, NULL, 0, 1200);
gemui_show_toast(st->app, rc == 0 ? "Updated default audio device" : "Failed to switch default", 1800);
} else if (action == HIT_ACT_MUTE) {
const char *sub = (st->tab == 1)
? (r->is_stream ? "set-source-output-mute" : "set-source-mute")
: (r->is_stream ? "set-sink-input-mute" : "set-sink-mute");
const char *target = (r->is_stream || !r->name[0]) ? idx : r->name;
const char *cmd[] = {"pactl", sub, target, "toggle", NULL};
gemui_run_cmd_timeout(cmd, NULL, 0, 1200);
} else if (action == HIT_ACT_DOWN || action == HIT_ACT_UP) {
const char *delta = (action == HIT_ACT_UP) ? "+5%" : "-5%";
const char *sub = (st->tab == 1)
? (r->is_stream ? "set-source-output-volume" : "set-source-volume")
: (r->is_stream ? "set-sink-input-volume" : "set-sink-volume");
const char *target = (r->is_stream || !r->name[0]) ? idx : r->name;
const char *cmd[] = {"pactl", sub, target, delta, NULL};
gemui_run_cmd_timeout(cmd, NULL, 0, 1200);
}
const char *pk[] = {"pkill", "-USR1", "geminux-statusd", NULL};
gemui_run_cmd_timeout(pk, NULL, 0, 400);
refresh_audio(st);
}
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
audio_state_t *st = ud;
const gemui_theme_t *t = gemui_theme(app);
gemui_draw_header(app, cr, "\xf3\xb0\x95\xbe", "Geminux Sound & Mixer (PipeWire)", st->status_text);
int pad = 20;
int top = 92;
int tab_w = (w - pad * 2 - 2 * 12) / 3;
static const char *tabs[3] = {"Playback (Output)", "Recording (Input)", "Cards & BT Profiles"};
for (int i = 0; i < 3; i++) {
gemui_draw_button(app, cr, (gemui_rect_t){pad + i * (tab_w + 12), top, tab_w, 66},
tabs[i], HIT_TAB, i, false, st->tab == i, false);
}
int cw = w - pad * 2;
int cy = top + 82;
if (st->tab == 0 || st->tab == 1) {
int btn_w = 224;
int sl_w = cw - btn_w * 2 - 24;
int pct = (st->tab == 1) ? st->in_vol_pct : st->out_vol_pct;
bool muted = (st->tab == 1) ? st->in_muted : st->out_muted;
int max_pct = (st->tab == 1) ? 150 : 125;
double norm = (double)pct / (double)max_pct;
if (norm > 1.0) norm = 1.0;
char vtxt[40];
snprintf(vtxt, sizeof(vtxt), "%d%%%s", pct, muted ? " MUTED" : "");
st->slider_rect = (gemui_rect_t){pad, cy, sl_w, 96};
gemui_draw_slider(app, cr, st->slider_rect,
st->tab == 1 ? "Default Input Gain (Mic)" : "Default Output Volume",
norm, vtxt, HIT_SLIDER, 0, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + sl_w + 12, cy, btn_w, 96},
muted ? "Unmute" : "Mute", HIT_MUTE_DEF, 0, false, muted, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + sl_w + 12 + btn_w + 12, cy, btn_w, 96},
st->tab == 1 ? "3s Mic Test" : "Test Tone", HIT_TEST, 0, true, false, false);
int ry = cy + 108;
int rw3 = (cw - 24) / 3;
if (st->tab == 0) {
gemui_draw_button(app, cr, (gemui_rect_t){pad, ry, rw3, 68},
"Internal Speakers", HIT_ROUTE_A, 0, false, st->speaker_on, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + rw3 + 12, ry, rw3, 68},
"3.5mm Headphones", HIT_ROUTE_B, 0, false, !st->speaker_on, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 2 * (rw3 + 12), ry, rw3, 68},
"Bluetooth Audio Profiles", HIT_GOTO_CARDS, 0, false, false, false);
} else {
gemui_draw_button(app, cr, (gemui_rect_t){pad, ry, rw3, 68},
"Internal Dual Mic", HIT_ROUTE_A, 1, false, !st->headset_mic_on, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + rw3 + 12, ry, rw3, 68},
"3.5mm Headset Mic", HIT_ROUTE_B, 1, false, st->headset_mic_on, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 2 * (rw3 + 12), ry, rw3, 68},
"Bluetooth HFP Headset", HIT_GOTO_CARDS, 0, false, false, false);
}
} else {
gemui_draw_button(app, cr, (gemui_rect_t){pad, cy, cw, 68},
"Select card profile below (e.g. Bluetooth A2DP Stereo vs HSP/HFP Headset Mic)",
HIT_ACT_REF, 0, false, false, false);
}
int list_y = (st->tab == 2) ? 260 : 366;
gemui_rect_t box = {pad, list_y, cw, h - list_y - 112};
gemui_fill_round(cr, box, 12, t->bg_bar);
gemui_stroke_round(cr, box, 12, 1.5, t->border);
int count = (st->tab == 2) ? st->num_profiles : st->num_rows;
if (count == 0) {
gemui_draw_text(cr, t, box, "No PipeWire audio endpoints found",
0, false, GEMUI_ALIGN_CENTER, t->fg_muted);
} else {
int rh = 72;
for (int vis = 0; st->scroll_off + vis < count; vis++) {
int i = st->scroll_off + vis;
gemui_rect_t rr = {box.x + 10, box.y + 8 + vis * rh, box.w - 20, rh - 6};
if (rr.y + rr.h > box.y + box.h) break;
bool sel = (i == st->sel_row);
gemui_fill_round(cr, rr, 10, sel ? t->sel_bg : (i % 2 ? t->bg_alt_odd : t->bg_alt_even));
if (st->tab == 2) {
const geminux_audio_profile_t *p = &st->profiles[i];
char c1[280];
snprintf(c1, sizeof(c1), "%s%s — %s", p->active ? "* " : " ", p->card_label, p->profile_desc);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + 16, rr.y, rr.w - 460, rr.h},
c1, -2, p->active, GEMUI_ALIGN_LEFT,
sel ? t->sel_fg : t->fg_bright);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 440, rr.y, 290, rr.h},
p->profile_name, -3, false, GEMUI_ALIGN_RIGHT,
sel ? t->sel_fg : t->accent_alt);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 140, rr.y, 126, rr.h},
p->available ? (p->active ? "Active" : "Ready") : "Unavail", -3,
false, GEMUI_ALIGN_RIGHT, sel ? t->sel_fg : t->fg_muted);
} else {
const audio_row_t *r = &st->rows[i];
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + 16, rr.y, rr.w - 410, rr.h},
r->label, -1, !r->is_stream, GEMUI_ALIGN_LEFT,
sel ? t->sel_fg : t->fg_bright);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 390, rr.y, 200, rr.h},
r->col2, -2, false, GEMUI_ALIGN_RIGHT,
sel ? t->sel_fg : t->accent_alt);
gemui_draw_text(cr, t, (gemui_rect_t){rr.x + rr.w - 180, rr.y, 166, rr.h},
r->col3, -2, false, GEMUI_ALIGN_RIGHT,
sel ? t->sel_fg : t->fg_muted);
}
gemui_hit_add(app, rr, HIT_ROW, i);
}
}
int fy = h - 92;
if (st->tab == 2) {
int fw = (cw - 24) / 3;
gemui_draw_button(app, cr, (gemui_rect_t){pad, fy, fw, 72},
"Activate Profile", HIT_ACT_DEF, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + fw + 12, fy, fw, 72},
"Bluetooth Devices", HIT_GOTO_CARDS, 1, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 2 * (fw + 12), fy, fw, 72},
"Refresh", HIT_ACT_REF, 0, false, false, false);
} else {
int fw = (cw - 48) / 5;
gemui_draw_button(app, cr, (gemui_rect_t){pad, fy, fw, 72},
st->tab == 1 ? "Set Default In" : "Set Default Out",
HIT_ACT_DEF, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + (fw + 12), fy, fw, 72},
"Mute Item", HIT_ACT_MUTE, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 2 * (fw + 12), fy, fw, 72},
"Vol -5%", HIT_ACT_DOWN, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 3 * (fw + 12), fy, fw, 72},
"Vol +5%", HIT_ACT_UP, 0, false, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + 4 * (fw + 12), fy, fw, 72},
"Refresh", HIT_ACT_REF, 0, false, false, false);
}
}
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
(void)state; (void)utf8;
audio_state_t *st = ud;
int count = (st->tab == 2) ? st->num_profiles : st->num_rows;
if (ks == XK_Escape || ks == XK_q) {
gemui_quit(app, 0);
} else if (ks == XK_Tab) {
st->tab = (st->tab + 1) % 3;
refresh_audio(st);
gemui_request_redraw(app);
} else if (ks == XK_Up && count > 0) {
st->sel_row = (st->sel_row > 0) ? st->sel_row - 1 : 0;
audio_clamp_scroll(st);
gemui_request_redraw(app);
} else if (ks == XK_Down && count > 0) {
st->sel_row = (st->sel_row + 1 < count) ? st->sel_row + 1 : count - 1;
audio_clamp_scroll(st);
gemui_request_redraw(app);
} else if ((ks == XK_Left || ks == XK_minus) && st->tab < 2) {
int max_pct = (st->tab == 1) ? 150 : 125;
int cur = (st->tab == 1) ? st->in_vol_pct : st->out_vol_pct;
apply_default_slider(st, (double)(cur - 5) / (double)max_pct);
} else if ((ks == XK_Right || ks == XK_equal) && st->tab < 2) {
int max_pct = (st->tab == 1) ? 150 : 125;
int cur = (st->tab == 1) ? st->in_vol_pct : st->out_vol_pct;
apply_default_slider(st, (double)(cur + 5) / (double)max_pct);
} else if (ks == XK_Return || ks == XK_KP_Enter) {
if (st->tab == 2 && st->sel_row >= 0 && st->sel_row < st->num_profiles) {
const geminux_audio_profile_t *p = &st->profiles[st->sel_row];
const char *cmd[] = {"pactl", "set-card-profile", p->card_name, p->profile_name, NULL};
gemui_run_cmd_timeout(cmd, NULL, 0, 1200);
refresh_audio(st);
gemui_request_redraw(app);
} else if (st->tab < 2) {
adjust_selected_node(st, HIT_ACT_DEF);
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;
audio_state_t *st = ud;
if (type == GEMUI_PTR_UP) {
if (st->dragging_slider) {
st->dragging_slider = false;
refresh_audio(st);
gemui_request_redraw(app);
}
return;
}
if (type == GEMUI_PTR_MOVE) {
if (st->dragging_slider && st->slider_rect.w > 32) {
apply_default_slider(st, (double)(x - st->slider_rect.x - 16) / (st->slider_rect.w - 32));
}
return;
}
if (type == GEMUI_PTR_SCROLL_UP) {
if (st->scroll_off > 0) {
st->scroll_off--;
gemui_request_redraw(app);
}
return;
}
if (type == GEMUI_PTR_SCROLL_DOWN) {
st->scroll_off++;
audio_clamp_scroll(st);
gemui_request_redraw(app);
return;
}
if (type != GEMUI_PTR_DOWN) return;
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
if (!hit) return;
if (hit->id == HIT_TAB) {
st->tab = hit->val;
refresh_audio(st);
} else if (hit->id == HIT_SLIDER && st->slider_rect.w > 32) {
st->dragging_slider = true;
apply_default_slider(st, (double)(x - st->slider_rect.x - 16) / (st->slider_rect.w - 32));
return;
} else if (hit->id == HIT_MUTE_DEF) {
const char *tgt = (st->tab == 1) ? "@DEFAULT_AUDIO_SOURCE@" : "@DEFAULT_AUDIO_SINK@";
const char *cmd[] = {"wpctl", "set-mute", tgt, "toggle", NULL};
gemui_run_cmd_timeout(cmd, NULL, 0, 900);
const char *pk[] = {"pkill", "-USR1", "geminux-statusd", NULL};
gemui_run_cmd_timeout(pk, NULL, 0, 400);
refresh_audio(st);
} else if (hit->id == HIT_TEST) {
if (st->tab == 1) {
const char *cmd[] = {
"/bin/sh", "-c",
"f=/tmp/geminux-mic-$$.wav; timeout 3 pw-record \"$f\" >/dev/null 2>&1; pw-play \"$f\" >/dev/null 2>&1; rm -f \"$f\"",
NULL
};
gemui_spawn_detached(cmd);
gemui_show_toast(app, "Recording 3s mic loopback test...", 3200);
} else {
const char *cmd[] = {
"/bin/sh", "-c",
"timeout 2 speaker-test -D pulse -c 2 -t sine -f 660 -l 1 >/dev/null 2>&1 || true",
NULL
};
gemui_spawn_detached(cmd);
gemui_show_toast(app, "Playing stereo test tone", 1600);
}
} else if (hit->id == HIT_ROUTE_A) {
if (hit->val == 0) {
const char *p0[] = {"pactl", "set-sink-port", "alsa_output.platform-sound.HiFi__Speaker__sink", "[Out] Speaker", NULL};
const char *p1[] = {"pactl", "set-sink-port", "@DEFAULT_AUDIO_SINK@", "[Out] Speaker", NULL};
const char *c1[] = {"amixer", "-c", "0", "cset", "name='Headphone Switch'", "off", NULL};
const char *c2[] = {"amixer", "-c", "0", "cset", "name='Speaker Switch'", "on", NULL};
gemui_run_cmd_timeout(p0, NULL, 0, 700);
gemui_run_cmd_timeout(p1, NULL, 0, 700);
gemui_run_cmd_timeout(c1, NULL, 0, 700);
gemui_run_cmd_timeout(c2, NULL, 0, 700);
} else {
const char *p1[] = {"pactl", "set-source-port", "@DEFAULT_AUDIO_SOURCE@", "[In] Mic", NULL};
const char *c1[] = {"amixer", "-c", "0", "cset", "name='Headset Mic Switch'", "off", NULL};
const char *c2[] = {"amixer", "-c", "0", "cset", "name='PGA L Mux'", "AIN0", NULL};
const char *c3[] = {"amixer", "-c", "0", "cset", "name='PGA R Mux'", "AIN2", NULL};
const char *c4[] = {"amixer", "-c", "0", "cset", "name='Main Mic Switch'", "on", NULL};
const char *c5[] = {"amixer", "-c", "0", "cset", "name='Back Mic Switch'", "on", NULL};
gemui_run_cmd_timeout(p1, NULL, 0, 700);
gemui_run_cmd_timeout(c1, NULL, 0, 700);
gemui_run_cmd_timeout(c2, NULL, 0, 700);
gemui_run_cmd_timeout(c3, NULL, 0, 700);
gemui_run_cmd_timeout(c4, NULL, 0, 700);
gemui_run_cmd_timeout(c5, NULL, 0, 700);
}
refresh_audio(st);
} else if (hit->id == HIT_ROUTE_B) {
if (hit->val == 0) {
const char *p1[] = {"pactl", "set-sink-port", "@DEFAULT_AUDIO_SINK@", "[Out] Headphones", NULL};
const char *c1[] = {"amixer", "-c", "0", "cset", "name='Speaker Switch'", "off", NULL};
const char *c2[] = {"amixer", "-c", "0", "cset", "name='Headphone Switch'", "on", NULL};
gemui_run_cmd_timeout(p1, NULL, 0, 700);
gemui_run_cmd_timeout(c1, NULL, 0, 700);
gemui_run_cmd_timeout(c2, NULL, 0, 700);
} else {
const char *p1[] = {"pactl", "set-source-port", "@DEFAULT_AUDIO_SOURCE@", "[In] Headset", NULL};
const char *c1[] = {"amixer", "-c", "0", "cset", "name='Main Mic Switch'", "off", NULL};
const char *c2[] = {"amixer", "-c", "0", "cset", "name='Back Mic Switch'", "off", NULL};
const char *c3[] = {"amixer", "-c", "0", "cset", "name='PGA L Mux'", "AIN1", NULL};
const char *c4[] = {"amixer", "-c", "0", "cset", "name='PGA R Mux'", "AIN1", NULL};
const char *c5[] = {"amixer", "-c", "0", "cset", "name='Headset Mic Switch'", "on", NULL};
gemui_run_cmd_timeout(p1, NULL, 0, 700);
gemui_run_cmd_timeout(c1, NULL, 0, 700);
gemui_run_cmd_timeout(c2, NULL, 0, 700);
gemui_run_cmd_timeout(c3, NULL, 0, 700);
gemui_run_cmd_timeout(c4, NULL, 0, 700);
gemui_run_cmd_timeout(c5, NULL, 0, 700);
}
refresh_audio(st);
} else if (hit->id == HIT_GOTO_CARDS) {
if (hit->val == 1) {
const char *netcmd[] = {"geminux-net", NULL};
gemui_spawn_detached(netcmd);
} else {
st->tab = 2;
refresh_audio(st);
}
} else if (hit->id == HIT_ROW) {
st->sel_row = hit->val;
} else if (hit->id == HIT_ACT_DEF && st->tab == 2) {
if (st->sel_row >= 0 && st->sel_row < st->num_profiles) {
const geminux_audio_profile_t *p = &st->profiles[st->sel_row];
const char *cmd[] = {"pactl", "set-card-profile", p->card_name, p->profile_name, NULL};
int rc = gemui_run_cmd_timeout(cmd, NULL, 0, 1200);
gemui_show_toast(app, rc == 0 ? "Activated audio profile" : "Profile switch failed", 1800);
refresh_audio(st);
} else {
gemui_show_toast(app, "Select a profile first", 1600);
}
} else if (hit->id == HIT_ACT_DEF || hit->id == HIT_ACT_MUTE ||
hit->id == HIT_ACT_DOWN || hit->id == HIT_ACT_UP) {
adjust_selected_node(st, hit->id);
} else if (hit->id == HIT_ACT_REF) {
refresh_audio(st);
gemui_show_toast(app, "Refreshed audio state", 1200);
}
gemui_request_redraw(app);
}
int main(void) {
audio_state_t st;
memset(&st, 0, sizeof(st));
st.sel_row = -1;
st.out_vol_pct = 40;
st.in_vol_pct = 100;
refresh_audio(&st);
st.app = gemui_create("geminux-audio", "Geminux Sound & Mixer", 1920, 940, false);
if (!st.app) return 1;
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);
gemui_destroy(st.app);
return rc;
}
+133
View File
@@ -0,0 +1,133 @@
/*
* Header-only C11 helpers for parsing bluetoothctl device listings into sorted entries.
*/
#ifndef GEMINUX_BT_LIST_H
#define GEMINUX_BT_LIST_H
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GEMINUX_MAX_BT_DEVS 32
typedef struct {
char mac[24];
char name[96];
bool paired;
bool connected;
} geminux_bt_dev_t;
static inline bool geminux_bt_is_mac(const char *s) {
if (!s || strlen(s) < 17) return false;
for (int i = 0; i < 17; i++) {
if (i % 3 == 2) {
if (s[i] != ':') return false;
} else if (!isxdigit((unsigned char)s[i])) {
return false;
}
}
return s[17] == '\0' || s[17] == ' ' || s[17] == '\t';
}
static inline void geminux_bt_strip_ansi(const char *in, char *out, size_t out_sz) {
size_t j = 0;
for (size_t i = 0; in[i] && j + 1 < out_sz; i++) {
if ((unsigned char)in[i] == 0x1b && in[i + 1] == '[') {
i += 2;
while (in[i] && !((in[i] >= 'A' && in[i] <= 'Z') || (in[i] >= 'a' && in[i] <= 'z'))) {
i++;
}
continue;
}
if ((unsigned char)in[i] >= 0x20 || in[i] == '\t') {
out[j++] = in[i];
}
}
out[j] = '\0';
}
static inline void geminux_bt_feed_list(const char *text, geminux_bt_dev_t *devs,
int *count, int max_devs,
bool mark_paired, bool mark_connected) {
if (!text || !devs || !count || max_devs <= 0) return;
const char *cur = text;
while (*cur) {
const char *nl = strchr(cur, '\n');
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
char raw[256], ln[256];
size_t cp = raw_len < sizeof(raw) - 1 ? raw_len : sizeof(raw) - 1;
memcpy(raw, cur, cp);
raw[cp] = '\0';
cur = nl ? nl + 1 : cur + raw_len;
geminux_bt_strip_ansi(raw, ln, sizeof(ln));
char *p = ln;
while (*p == ' ' || *p == '\t') p++;
if (strncmp(p, "[NEW]", 5) == 0 || strncmp(p, "[CHG]", 5) == 0) p += 5;
while (*p == ' ' || *p == '\t') p++;
if (strncmp(p, "Device ", 7) != 0) continue;
p += 7;
if (!geminux_bt_is_mac(p)) continue;
char mac[24];
memcpy(mac, p, 17);
mac[17] = '\0';
for (int i = 0; i < 17; i++) mac[i] = (char)toupper((unsigned char)mac[i]);
const char *name = p + 17;
while (*name == ' ' || *name == '\t') name++;
size_t nlen = strlen(name);
while (nlen > 0 && (name[nlen - 1] == ' ' || name[nlen - 1] == '\r')) nlen--;
int idx = -1;
for (int i = 0; i < *count; i++) {
if (strcmp(devs[i].mac, mac) == 0) {
idx = i;
break;
}
}
if (idx < 0) {
if (*count >= max_devs) continue;
idx = (*count)++;
memset(&devs[idx], 0, sizeof(devs[idx]));
snprintf(devs[idx].mac, sizeof(devs[idx].mac), "%s", mac);
}
if (nlen > 0) {
size_t keep = nlen < sizeof(devs[idx].name) - 1 ? nlen : sizeof(devs[idx].name) - 1;
memcpy(devs[idx].name, name, keep);
devs[idx].name[keep] = '\0';
} else if (!devs[idx].name[0]) {
snprintf(devs[idx].name, sizeof(devs[idx].name), "%s", mac);
}
if (mark_paired) devs[idx].paired = true;
if (mark_connected) {
devs[idx].connected = true;
devs[idx].paired = true;
}
}
}
static inline int geminux_bt_dev_cmp(const void *pa, const void *pb) {
const geminux_bt_dev_t *a = (const geminux_bt_dev_t *)pa;
const geminux_bt_dev_t *b = (const geminux_bt_dev_t *)pb;
if (a->connected != b->connected) return b->connected ? 1 : -1;
if (a->paired != b->paired) return b->paired ? 1 : -1;
return strcmp(a->name, b->name);
}
static inline int geminux_parse_bt_devices(const char *all_txt,
const char *paired_txt,
const char *conn_txt,
geminux_bt_dev_t *devs,
int max_devs) {
int n = 0;
geminux_bt_feed_list(all_txt, devs, &n, max_devs, false, false);
geminux_bt_feed_list(paired_txt, devs, &n, max_devs, true, false);
geminux_bt_feed_list(conn_txt, devs, &n, max_devs, true, true);
if (n > 1) qsort(devs, (size_t)n, sizeof(devs[0]), geminux_bt_dev_cmp);
return n;
}
#endif /* GEMINUX_BT_LIST_H */
+7 -1
View File
@@ -14,6 +14,7 @@
#define HIT_SUSPEND 6
#define HIT_REBOOT 7
#define HIT_POWER 8
#define HIT_MIXER 9
typedef struct {
gemui_app_t *app;
@@ -104,10 +105,12 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
snprintf(btxt, sizeof(btxt), "%d%%", (int)(bnorm * 100.0 + 0.5));
gemui_draw_slider(app, cr, st->bri_rect, "Display Backlight", bnorm, btxt, HIT_BRI, 0, false);
st->vol_rect = (gemui_rect_t){pad, 210, cw, 104};
st->vol_rect = (gemui_rect_t){pad, 210, cw - 240, 104};
char vtxt[32];
snprintf(vtxt, sizeof(vtxt), "%d%%", st->vol_pct);
gemui_draw_slider(app, cr, st->vol_rect, "Master Audio Volume", st->vol_pct / 100.0, vtxt, HIT_VOL, 0, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad + cw - 228, 210, 228, 104},
"Audio Mixer", HIT_MIXER, 0, true, false, false);
gemui_draw_text(cr, t, (gemui_rect_t){pad, 330, cw, 40},
"MT6797X Power Profile (A72 Big Cluster + Schedutil)",
@@ -175,6 +178,9 @@ static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, v
} else if (hit->id == HIT_VOL) {
st->active_drag = HIT_VOL;
set_volume_norm(st, (double)(x - st->vol_rect.x - 16) / (st->vol_rect.w - 32));
} else if (hit->id == HIT_MIXER) {
const char *cmd[] = {"geminux-audio", NULL};
gemui_spawn_detached(cmd);
} else if (hit->id == HIT_PROF) {
static const char *pnames[3] = {"eco", "balanced", "performance"};
st->profile = hit->val;
File diff suppressed because it is too large. Load diff
+144
View File
@@ -0,0 +1,144 @@
/*
* Header-only C11 helpers for parsing and grouping nmcli terse Wi-Fi scans
* (deduplicating mesh BSSIDs per SSID and selecting the best joinable AP).
*/
#ifndef GEMINUX_WIFI_LIST_H
#define GEMINUX_WIFI_LIST_H
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define GEMINUX_MAX_WIFI_GROUPS 32
typedef struct {
char ssid[96];
char best_bssid[24];
char security[48];
int best_signal;
int best_chan;
int ap_count;
bool best_joinable;
bool in_use;
} geminux_wifi_group_t;
static inline int geminux_split_nmcli_terse(char *line, char **fields, int max_fields) {
if (!line || !fields || max_fields <= 0) return 0;
int count = 0;
fields[count++] = line;
char *rd = line;
char *wr = line;
while (*rd) {
if (rd[0] == '\\' && (rd[1] == ':' || rd[1] == '\\')) {
*wr++ = rd[1];
rd += 2;
} else if (rd[0] == ':' && count < max_fields) {
*wr++ = '\0';
fields[count++] = wr;
rd++;
} else {
*wr++ = *rd++;
}
}
*wr = '\0';
return count;
}
static inline bool geminux_wifi_chan_joinable(int chan) {
return (chan >= 1 && chan <= 13) ||
(chan >= 36 && chan <= 48) ||
(chan >= 149 && chan <= 165);
}
static inline int geminux_wifi_group_cmp(const void *pa, const void *pb) {
const geminux_wifi_group_t *a = (const geminux_wifi_group_t *)pa;
const geminux_wifi_group_t *b = (const geminux_wifi_group_t *)pb;
if (a->in_use != b->in_use) return b->in_use ? 1 : -1;
if (a->best_signal != b->best_signal) return (b->best_signal > a->best_signal) ? 1 : -1;
return strcmp(a->ssid, b->ssid);
}
static inline int geminux_parse_wifi_scan(const char *nmcli_out,
geminux_wifi_group_t *groups,
int max_groups) {
if (!nmcli_out || !groups || max_groups <= 0) return 0;
int n = 0;
const char *cur = nmcli_out;
while (*cur) {
const char *nl = strchr(cur, '\n');
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
char ln[512];
size_t l = raw_len < sizeof(ln) - 1 ? raw_len : sizeof(ln) - 1;
memcpy(ln, cur, l);
ln[l] = '\0';
cur = nl ? nl + 1 : cur + raw_len;
while (l > 0 && ln[l - 1] == '\r') ln[--l] = '\0';
if (!ln[0]) continue;
char *fields[6] = {0};
if (geminux_split_nmcli_terse(ln, fields, 6) < 6) continue;
const char *inuse = fields[0];
const char *bssid = fields[1];
const char *ssid = fields[2];
int chan = atoi(fields[3]);
int sig = atoi(fields[4]);
const char *sec = fields[5];
if (!ssid[0]) continue;
if (!sec[0] || strcmp(sec, "--") == 0) sec = "Open";
bool active = (inuse[0] == '*');
bool joinable = geminux_wifi_chan_joinable(chan);
int idx = -1;
for (int i = 0; i < n; i++) {
if (strcmp(groups[i].ssid, ssid) == 0) {
idx = i;
break;
}
}
if (idx < 0) {
if (n >= max_groups) continue;
geminux_wifi_group_t *g = &groups[n++];
memset(g, 0, sizeof(*g));
snprintf(g->ssid, sizeof(g->ssid), "%s", ssid);
snprintf(g->best_bssid, sizeof(g->best_bssid), "%s", bssid);
snprintf(g->security, sizeof(g->security), "%s", sec);
g->best_signal = sig;
g->best_chan = chan;
g->best_joinable = joinable;
g->ap_count = 1;
g->in_use = active;
} else {
geminux_wifi_group_t *g = &groups[idx];
g->ap_count++;
if (active) g->in_use = true;
if (strcmp(g->security, "Open") == 0 && strcmp(sec, "Open") != 0) {
snprintf(g->security, sizeof(g->security), "%s", sec);
}
bool better = false;
if (joinable && !g->best_joinable) {
better = true;
} else if (joinable == g->best_joinable) {
if (sig > g->best_signal) better = true;
else if (sig == g->best_signal && chan >= 36 && g->best_chan < 36) better = true;
}
if (better) {
snprintf(g->best_bssid, sizeof(g->best_bssid), "%s", bssid);
g->best_signal = sig;
g->best_chan = chan;
g->best_joinable = joinable;
snprintf(g->security, sizeof(g->security), "%s", sec);
}
}
}
if (n > 1) qsort(groups, (size_t)n, sizeof(groups[0]), geminux_wifi_group_cmp);
return n;
}
#endif /* GEMINUX_WIFI_LIST_H */
+6 -2
View File
@@ -22,7 +22,8 @@ BINS = \
geminux-bt-obex \
geminux-leds \
geminux-power-audit \
geminux-checkpw
geminux-checkpw \
geminux-wifi-nvram
all: $(BINS)
@@ -56,12 +57,15 @@ geminux-power-audit: geminux-power-audit.c
geminux-checkpw: geminux-checkpw.c
$(CC) $(CFLAGS) -o $@ $<
geminux-wifi-nvram: geminux-wifi-nvram.c geminux-wifi-nvram-core.c geminux-wifi-nvram-core.h
$(CC) $(CFLAGS) -o $@ geminux-wifi-nvram.c geminux-wifi-nvram-core.c
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/sbin
cp -f geminux-power-ctl geminux-statusd geminux-touchd geminux-modem-ctl \
geminux-bt-obex geminux-power-audit $(DESTDIR)$(PREFIX)/bin/
cp -f geminux-powerd geminux-ccci-daemon geminux-leds \
geminux-checkpw $(DESTDIR)$(PREFIX)/sbin/
geminux-checkpw geminux-wifi-nvram $(DESTDIR)$(PREFIX)/sbin/
chmod 0755 $(DESTDIR)$(PREFIX)/bin/geminux-* $(DESTDIR)$(PREFIX)/sbin/geminux-*
clean:
+132 -4
View File
@@ -1,10 +1,53 @@
#define _POSIX_C_SOURCE 200809L
#include <gio/gio.h>
#include <inttypes.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define GEMINUX_BT_AGENT_PATH "/org/geminux/bt_agent"
static GMainLoop *g_loop = NULL;
static GDBusConnection *g_sys = NULL;
static GDBusNodeInfo *g_agent_introspection = NULL;
static guint g_agent_reg_id = 0;
static const gchar g_agent_xml[] =
"<node>"
" <interface name='org.bluez.Agent1'>"
" <method name='Release'/>"
" <method name='RequestPinCode'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='s' name='pincode' direction='out'/>"
" </method>"
" <method name='DisplayPinCode'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='s' name='pincode' direction='in'/>"
" </method>"
" <method name='RequestPasskey'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='u' name='passkey' direction='out'/>"
" </method>"
" <method name='DisplayPasskey'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='u' name='passkey' direction='in'/>"
" <arg type='q' name='entered' direction='in'/>"
" </method>"
" <method name='RequestConfirmation'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='u' name='passkey' direction='in'/>"
" </method>"
" <method name='RequestAuthorization'>"
" <arg type='o' name='device' direction='in'/>"
" </method>"
" <method name='AuthorizeService'>"
" <arg type='o' name='device' direction='in'/>"
" <arg type='s' name='uuid' direction='in'/>"
" </method>"
" <method name='Cancel'/>"
" </interface>"
"</node>";
static void notify_event(const char *title, const char *body) {
const char *argv[] = {"notify-send", title, body, NULL};
@@ -12,6 +55,78 @@ static void notify_event(const char *title, const char *body) {
NULL, NULL, NULL, NULL);
}
static void on_agent_call(GDBusConnection *conn, const gchar *sender,
const gchar *obj_path, const gchar *iface_name,
const gchar *method_name, GVariant *params,
GDBusMethodInvocation *inv, gpointer ud) {
(void)conn; (void)sender; (void)obj_path; (void)iface_name; (void)ud;
if (strcmp(method_name, "RequestPinCode") == 0) {
notify_event("Bluetooth Pairing", "Using PIN 0000 for device pairing");
g_dbus_method_invocation_return_value(inv, g_variant_new("(s)", "0000"));
} else if (strcmp(method_name, "DisplayPinCode") == 0) {
const gchar *dev = NULL, *pin = NULL;
g_variant_get(params, "(&o&s)", &dev, &pin);
char msg[160];
snprintf(msg, sizeof(msg), "Type PIN %s + Enter on device", pin ? pin : "0000");
notify_event("Bluetooth Pairing PIN", msg);
g_dbus_method_invocation_return_value(inv, NULL);
} else if (strcmp(method_name, "RequestPasskey") == 0) {
g_dbus_method_invocation_return_value(inv, g_variant_new("(u)", (guint32)0));
} else if (strcmp(method_name, "DisplayPasskey") == 0) {
const gchar *dev = NULL;
guint32 passkey = 0;
guint16 entered = 0;
g_variant_get(params, "(&ouq)", &dev, &passkey, &entered);
if (entered == 0) {
char msg[160];
snprintf(msg, sizeof(msg), "Type passkey %06" PRIu32 " + Enter on device", passkey);
notify_event("Bluetooth Passkey", msg);
}
g_dbus_method_invocation_return_value(inv, NULL);
} else if (strcmp(method_name, "RequestConfirmation") == 0) {
const gchar *dev = NULL;
guint32 passkey = 0;
g_variant_get(params, "(&ou)", &dev, &passkey);
char msg[160];
snprintf(msg, sizeof(msg), "Confirmed pairing passkey %06" PRIu32, passkey);
notify_event("Bluetooth Pairing", msg);
g_dbus_method_invocation_return_value(inv, NULL);
} else {
if (strcmp(method_name, "Cancel") == 0) {
notify_event("Bluetooth Pairing", "Pairing cancelled");
}
g_dbus_method_invocation_return_value(inv, NULL);
}
}
static const GDBusInterfaceVTable g_agent_vtable = {
.method_call = on_agent_call,
};
static void on_agent_registered(GObject *src, GAsyncResult *res, gpointer ud) {
(void)ud;
GVariant *ret = g_dbus_connection_call_finish(G_DBUS_CONNECTION(src), res, NULL);
if (ret) {
g_variant_unref(ret);
g_dbus_connection_call(
G_DBUS_CONNECTION(src), "org.bluez", "/org/bluez",
"org.bluez.AgentManager1", "RequestDefaultAgent",
g_variant_new("(o)", GEMINUX_BT_AGENT_PATH), NULL,
G_DBUS_CALL_FLAGS_NONE, 3000, NULL, NULL, NULL);
}
}
static void on_bluez_appeared(GDBusConnection *conn, const gchar *name,
const gchar *owner, gpointer ud) {
(void)name; (void)owner; (void)ud;
if (!g_agent_reg_id) return;
g_dbus_connection_call(
conn, "org.bluez", "/org/bluez",
"org.bluez.AgentManager1", "RegisterAgent",
g_variant_new("(os)", GEMINUX_BT_AGENT_PATH, "KeyboardDisplay"), NULL,
G_DBUS_CALL_FLAGS_NONE, 3000, NULL, on_agent_registered, NULL);
}
static void on_bluez_props(GDBusConnection *conn, const gchar *sender,
const gchar *obj_path, const gchar *iface_name,
const gchar *signal_name, GVariant *params,
@@ -55,10 +170,20 @@ int main(void) {
signal(SIGCHLD, SIG_IGN);
g_loop = g_main_loop_new(NULL, FALSE);
GDBusConnection *sys = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
if (sys) {
g_sys = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
guint watch_id = 0;
if (g_sys) {
g_agent_introspection = g_dbus_node_info_new_for_xml(g_agent_xml, NULL);
if (g_agent_introspection) {
g_agent_reg_id = g_dbus_connection_register_object(
g_sys, GEMINUX_BT_AGENT_PATH, g_agent_introspection->interfaces[0],
&g_agent_vtable, NULL, NULL, NULL);
}
watch_id = g_bus_watch_name_on_connection(
g_sys, "org.bluez", G_BUS_NAME_WATCHER_FLAGS_NONE,
on_bluez_appeared, NULL, NULL, NULL);
g_dbus_connection_signal_subscribe(
sys, "org.bluez", "org.freedesktop.DBus.Properties",
g_sys, "org.bluez", "org.freedesktop.DBus.Properties",
"PropertiesChanged", NULL, "org.bluez.Device1",
G_DBUS_SIGNAL_FLAGS_NONE, on_bluez_props, NULL, NULL);
}
@@ -71,7 +196,10 @@ int main(void) {
}
g_main_loop_run(g_loop);
if (sys) g_object_unref(sys);
if (watch_id) g_bus_unwatch_name(watch_id);
if (g_sys && g_agent_reg_id) g_dbus_connection_unregister_object(g_sys, g_agent_reg_id);
if (g_agent_introspection) g_dbus_node_info_unref(g_agent_introspection);
if (g_sys) g_object_unref(g_sys);
if (ses) g_object_unref(ses);
g_main_loop_unref(g_loop);
return 0;
@@ -0,0 +1,105 @@
#include "geminux-wifi-nvram-core.h"
#include <string.h>
static uint16_t read_le16(const uint8_t *p) {
return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
}
uint8_t geminux_wifi_nvram_checksum(const uint8_t *rec512) {
uint8_t check = 0;
for (size_t i = 0; i < GEMINUX_WIFI_RECORD_BYTES; i++) {
check = (uint8_t)((i & 1U) ? (check ^ rec512[i]) : (check + rec512[i]));
}
return check;
}
bool geminux_wifi_nvram_has_valid_mac(const uint8_t *rec512) {
if (!rec512) return false;
/* Multicast bit must be clear (unicast address required by IEEE 802.11). */
if (rec512[4] & 0x01U) return false;
uint8_t or_acc = 0;
uint8_t and_acc = 0xffU;
for (size_t i = 4; i < 10; i++) {
or_acc |= rec512[i];
and_acc &= rec512[i];
}
return or_acc != 0 && and_acc != 0xffU;
}
bool geminux_wifi_nvram_valid_record(const uint8_t *buf, size_t len, bool require_mac) {
if (!buf || len != GEMINUX_WIFI_STORAGE_BYTES) return false;
if (buf[512] != 0xaaU) return false;
if (geminux_wifi_nvram_checksum(buf) != buf[513]) return false;
if (read_le16(buf + 2) > 0x0200U || read_le16(buf + 258) > 0x0200U) return false;
if (buf[83] || buf[104] || buf[155] || buf[266]) return false;
if (require_mac && !geminux_wifi_nvram_has_valid_mac(buf)) return false;
return true;
}
void geminux_wifi_nvram_apply_fallback_mac(uint8_t buf514[GEMINUX_WIFI_STORAGE_BYTES],
const char *seed_str) {
uint64_t h = 14695981039346656037ULL;
if (seed_str && seed_str[0]) {
for (const unsigned char *p = (const unsigned char *)seed_str; *p; p++) {
h ^= (uint64_t)(*p);
h *= 1099511628211ULL;
}
} else {
h ^= 0x6797a115ULL;
}
buf514[4] = 0x02U;
buf514[5] = (uint8_t)(h >> 8);
buf514[6] = (uint8_t)(h >> 16);
buf514[7] = (uint8_t)(h >> 24);
buf514[8] = (uint8_t)(h >> 32);
buf514[9] = (uint8_t)(h >> 40);
if ((buf514[5] | buf514[6] | buf514[7] | buf514[8] | buf514[9]) == 0) {
buf514[5] = 0x67U;
buf514[9] = 0x97U;
}
buf514[512] = 0xaaU;
buf514[513] = geminux_wifi_nvram_checksum(buf514);
}
bool geminux_wifi_nvram_scan_blob(const uint8_t *blob, size_t blob_len,
uint8_t out514[GEMINUX_WIFI_STORAGE_BYTES],
bool *has_valid_mac_out) {
if (has_valid_mac_out) *has_valid_mac_out = false;
if (!blob || !out514 || blob_len < GEMINUX_WIFI_STORAGE_BYTES) return false;
if (blob_len >= (size_t)GEMINUX_WIFI_NVRAM_FAST_OFFSET + GEMINUX_WIFI_STORAGE_BYTES) {
const uint8_t *fast = blob + GEMINUX_WIFI_NVRAM_FAST_OFFSET;
if (fast[0] == 0x06U && fast[1] == 0x01U &&
geminux_wifi_nvram_valid_record(fast, GEMINUX_WIFI_STORAGE_BYTES, true)) {
memcpy(out514, fast, GEMINUX_WIFI_STORAGE_BYTES);
if (has_valid_mac_out) *has_valid_mac_out = true;
return true;
}
}
bool have_backup = false;
uint8_t backup[GEMINUX_WIFI_STORAGE_BYTES];
size_t limit = blob_len - GEMINUX_WIFI_STORAGE_BYTES;
for (size_t off = 0; off <= limit; off++) {
const uint8_t *cand = blob + off;
if (cand[0] != 0x06U || cand[1] != 0x01U || cand[512] != 0xaaU) continue;
if (geminux_wifi_nvram_valid_record(cand, GEMINUX_WIFI_STORAGE_BYTES, true)) {
memcpy(out514, cand, GEMINUX_WIFI_STORAGE_BYTES);
if (has_valid_mac_out) *has_valid_mac_out = true;
return true;
}
if (!have_backup &&
geminux_wifi_nvram_valid_record(cand, GEMINUX_WIFI_STORAGE_BYTES, false)) {
memcpy(backup, cand, GEMINUX_WIFI_STORAGE_BYTES);
have_backup = true;
}
}
if (have_backup) {
memcpy(out514, backup, GEMINUX_WIFI_STORAGE_BYTES);
if (has_valid_mac_out) *has_valid_mac_out = false;
return true;
}
return false;
}
@@ -0,0 +1,21 @@
#ifndef GEMINUX_WIFI_NVRAM_CORE_H
#define GEMINUX_WIFI_NVRAM_CORE_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define GEMINUX_WIFI_RECORD_BYTES 512U
#define GEMINUX_WIFI_STORAGE_BYTES 514U
#define GEMINUX_WIFI_NVRAM_FAST_OFFSET 0x40042U
uint8_t geminux_wifi_nvram_checksum(const uint8_t *rec512);
bool geminux_wifi_nvram_has_valid_mac(const uint8_t *rec512);
bool geminux_wifi_nvram_valid_record(const uint8_t *buf, size_t len, bool require_mac);
void geminux_wifi_nvram_apply_fallback_mac(uint8_t buf514[GEMINUX_WIFI_STORAGE_BYTES],
const char *seed_str);
bool geminux_wifi_nvram_scan_blob(const uint8_t *blob, size_t blob_len,
uint8_t out514[GEMINUX_WIFI_STORAGE_BYTES],
bool *has_valid_mac_out);
#endif
+311
View File
@@ -0,0 +1,311 @@
#define _POSIX_C_SOURCE 200809L
#include "geminux-wifi-nvram-core.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <unistd.h>
#define MAX_NVRAM_READ_BYTES (8U * 1024U * 1024U)
#define DEFAULT_OUTPUT_PATH "/lib/firmware/mediatek/mt6797/WIFI.storage"
#define DEFAULT_BT_OUTPUT_PATH "/lib/firmware/mediatek/mt6797/BT.storage"
#define GEMINUX_BT_STORAGE_BYTES 66U
static int read_fd_exact_or_cap(int fd, uint8_t *buf, size_t cap, size_t *out_len) {
size_t total = 0;
while (total < cap) {
ssize_t r = read(fd, buf + total, cap - total);
if (r < 0) {
if (errno == EINTR) continue;
return -1;
}
if (r == 0) break;
total += (size_t)r;
}
*out_len = total;
return 0;
}
static bool read_file_exact(const char *path, uint8_t *buf, size_t cap, size_t *out_len) {
int fd = open(path, O_RDONLY);
if (fd < 0) return false;
int rc = read_fd_exact_or_cap(fd, buf, cap, out_len);
close(fd);
return rc == 0;
}
static bool find_partition_device(const char *partname, char *dev_out, size_t dev_sz) {
DIR *d = opendir("/sys/class/block");
if (!d) return false;
bool found = false;
struct dirent *de;
while ((de = readdir(d)) != NULL) {
if (de->d_name[0] == '.') continue;
char uevent_path[320];
snprintf(uevent_path, sizeof(uevent_path), "/sys/class/block/%s/uevent", de->d_name);
FILE *f = fopen(uevent_path, "r");
if (!f) continue;
char devname[128] = {0};
char pname[128] = {0};
char line[256];
while (fgets(line, sizeof(line), f)) {
size_t n = strlen(line);
while (n > 0 && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = '\0';
if (strncmp(line, "DEVNAME=", 8) == 0) {
snprintf(devname, sizeof(devname), "%s", line + 8);
} else if (strncmp(line, "PARTNAME=", 9) == 0) {
snprintf(pname, sizeof(pname), "%s", line + 9);
}
}
fclose(f);
if (strcmp(pname, partname) == 0 && devname[0]) {
snprintf(dev_out, dev_sz, "/dev/%s", devname);
found = true;
break;
}
}
closedir(d);
return found;
}
static int mkdir_p(const char *dir) {
char tmp[512];
snprintf(tmp, sizeof(tmp), "%s", dir);
size_t len = strlen(tmp);
if (len == 0) return 0;
if (tmp[len - 1] == '/') tmp[len - 1] = '\0';
for (char *p = tmp + 1; *p; p++) {
if (*p == '/') {
*p = '\0';
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) return -1;
*p = '/';
}
}
if (mkdir(tmp, 0755) != 0 && errno != EEXIST) return -1;
return 0;
}
static int write_atomic_bytes(const char *out_path, const uint8_t *rec, size_t len) {
char dir[512];
snprintf(dir, sizeof(dir), "%s", out_path);
char *slash = strrchr(dir, '/');
if (slash) {
*slash = '\0';
if (dir[0] && mkdir_p(dir) != 0) return -1;
}
char tmp_path[560];
snprintf(tmp_path, sizeof(tmp_path), "%s.tmp", out_path);
int fd = open(tmp_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) return -1;
size_t off = 0;
while (off < len) {
ssize_t w = write(fd, rec + off, len - off);
if (w < 0) {
if (errno == EINTR) continue;
close(fd);
unlink(tmp_path);
return -1;
}
off += (size_t)w;
}
fchmod(fd, 0644);
fsync(fd);
close(fd);
if (rename(tmp_path, out_path) != 0) {
unlink(tmp_path);
return -1;
}
return 0;
}
static bool bt_addr_nonzero(const uint8_t *buf, size_t len) {
if (len < 6) return false;
bool all_zero = true, all_ff = true;
for (size_t i = 0; i < 6; i++) {
if (buf[i] != 0x00) all_zero = false;
if (buf[i] != 0xff) all_ff = false;
}
return !all_zero && !all_ff;
}
static int mount_nvdata_ro(char *mnt_out, size_t mnt_sz) {
char dev_path[256] = "/dev/mmcblk0p6";
find_partition_device("nvdata", dev_path, sizeof(dev_path));
const char *mnt = "/run/geminux-nvdata-mnt";
if (mkdir_p("/run") != 0 || (mkdir(mnt, 0700) != 0 && errno != EEXIST)) {
mnt = "/tmp/geminux-nvdata-mnt";
if (mkdir(mnt, 0700) != 0 && errno != EEXIST) return -1;
}
if (mount(dev_path, mnt, "ext4", MS_RDONLY, "noload") != 0) {
rmdir(mnt);
return -1;
}
snprintf(mnt_out, mnt_sz, "%s", mnt);
return 0;
}
static bool try_nvram_partition(uint8_t rec_out[GEMINUX_WIFI_STORAGE_BYTES],
bool *has_mac_out) {
char dev_path[256] = "/dev/mmcblk0p19";
find_partition_device("nvram", dev_path, sizeof(dev_path));
int fd = open(dev_path, O_RDONLY);
if (fd < 0) return false;
uint8_t *buf = malloc(MAX_NVRAM_READ_BYTES);
if (!buf) {
close(fd);
return false;
}
size_t got = 0;
int rc = read_fd_exact_or_cap(fd, buf, MAX_NVRAM_READ_BYTES, &got);
close(fd);
bool ok = (rc == 0) && geminux_wifi_nvram_scan_blob(buf, got, rec_out, has_mac_out);
memset(buf, 0, got);
free(buf);
return ok;
}
static bool try_nvdata_partition(uint8_t rec_out[GEMINUX_WIFI_STORAGE_BYTES],
bool *has_mac_out) {
char mnt[128];
if (mount_nvdata_ro(mnt, sizeof(mnt)) != 0) return false;
char rec_path[320];
snprintf(rec_path, sizeof(rec_path), "%s/APCFG/APRDEB/WIFI", mnt);
uint8_t cand[GEMINUX_WIFI_STORAGE_BYTES];
size_t len = 0;
bool ok = read_file_exact(rec_path, cand, sizeof(cand), &len) &&
geminux_wifi_nvram_valid_record(cand, len, false);
if (ok) {
memcpy(rec_out, cand, GEMINUX_WIFI_STORAGE_BYTES);
*has_mac_out = geminux_wifi_nvram_has_valid_mac(cand);
}
memset(cand, 0, sizeof(cand));
umount2(mnt, MNT_DETACH);
rmdir(mnt);
return ok;
}
static void provision_bt_storage(const char *out_path) {
uint8_t existing[GEMINUX_BT_STORAGE_BYTES];
size_t existing_len = 0;
if (read_file_exact(out_path, existing, sizeof(existing), &existing_len) &&
existing_len == GEMINUX_BT_STORAGE_BYTES && bt_addr_nonzero(existing, existing_len)) {
memset(existing, 0, sizeof(existing));
return;
}
memset(existing, 0, sizeof(existing));
char mnt[128];
if (mount_nvdata_ro(mnt, sizeof(mnt)) != 0) return;
char bt_path[320];
snprintf(bt_path, sizeof(bt_path), "%s/APCFG/APRDEB/BT_Addr", mnt);
uint8_t cand[GEMINUX_BT_STORAGE_BYTES];
size_t len = 0;
bool ok = read_file_exact(bt_path, cand, sizeof(cand), &len) &&
len == GEMINUX_BT_STORAGE_BYTES && bt_addr_nonzero(cand, len);
umount2(mnt, MNT_DETACH);
rmdir(mnt);
if (ok && write_atomic_bytes(out_path, cand, GEMINUX_BT_STORAGE_BYTES) == 0) {
printf("geminux-wifi-nvram: provisioned %s (%u bytes)\n",
out_path, GEMINUX_BT_STORAGE_BYTES);
}
memset(cand, 0, sizeof(cand));
}
static void read_seed_string(char *seed, size_t sz) {
memset(seed, 0, sz);
size_t n = 0;
uint8_t raw[128] = {0};
if (read_file_exact("/etc/machine-id", raw, sizeof(raw) - 1, &n) && n > 0) {
while (n > 0 && (raw[n - 1] == '\n' || raw[n - 1] == '\r')) raw[--n] = '\0';
snprintf(seed, sz, "%s", (const char *)raw);
return;
}
if (read_file_exact("/sys/block/mmcblk0/device/cid", raw, sizeof(raw) - 1, &n) && n > 0) {
while (n > 0 && (raw[n - 1] == '\n' || raw[n - 1] == '\r')) raw[--n] = '\0';
snprintf(seed, sz, "%s", (const char *)raw);
return;
}
snprintf(seed, sz, "geminux-mt6797");
}
int main(int argc, char **argv) {
const char *out_path = (argc > 1 && argv[1][0]) ? argv[1] : DEFAULT_OUTPUT_PATH;
if (argc <= 1) {
provision_bt_storage(DEFAULT_BT_OUTPUT_PATH);
}
uint8_t existing[GEMINUX_WIFI_STORAGE_BYTES];
size_t existing_len = 0;
if (read_file_exact(out_path, existing, sizeof(existing), &existing_len) &&
geminux_wifi_nvram_valid_record(existing, existing_len, true)) {
memset(existing, 0, sizeof(existing));
return 0;
}
memset(existing, 0, sizeof(existing));
uint8_t chosen[GEMINUX_WIFI_STORAGE_BYTES];
bool have_rec = false;
bool have_mac = false;
if (try_nvram_partition(chosen, &have_mac)) {
have_rec = true;
}
if (!have_rec || !have_mac) {
uint8_t nvdata_rec[GEMINUX_WIFI_STORAGE_BYTES];
bool nvdata_mac = false;
if (try_nvdata_partition(nvdata_rec, &nvdata_mac)) {
if (!have_rec || nvdata_mac) {
memcpy(chosen, nvdata_rec, sizeof(chosen));
have_rec = true;
have_mac = nvdata_mac;
}
memset(nvdata_rec, 0, sizeof(nvdata_rec));
}
}
if (!have_rec) {
fprintf(stderr, "geminux-wifi-nvram: no valid NVRAM/nvdata WIFI record found\n");
return 1;
}
if (!have_mac) {
char seed[128];
read_seed_string(seed, sizeof(seed));
geminux_wifi_nvram_apply_fallback_mac(chosen, seed);
memset(seed, 0, sizeof(seed));
}
int wrc = write_atomic_bytes(out_path, chosen, GEMINUX_WIFI_STORAGE_BYTES);
memset(chosen, 0, sizeof(chosen));
if (wrc != 0) {
fprintf(stderr, "geminux-wifi-nvram: failed to write %s (%s)\n",
out_path, strerror(errno));
return 1;
}
printf("geminux-wifi-nvram: provisioned %s (%u bytes)\n",
out_path, GEMINUX_WIFI_STORAGE_BYTES);
return 0;
}
+79 -12
View File
@@ -3,6 +3,7 @@
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <pango/pangocairo.h>
#include <poll.h>
#include <signal.h>
@@ -303,6 +304,8 @@ static void gemui_ensure_backbuf(gemui_app_t *app) {
gemui_app_t *gemui_create(const char *res_name, const char *title,
int w, int h, bool fullscreen_override) {
setlocale(LC_CTYPE, "");
XSetLocaleModifiers("");
gemui_init_sigpipe();
Display *dpy = XOpenDisplay(NULL);
if (!dpy) {
@@ -550,6 +553,12 @@ static void gemui_render_frame(gemui_app_t *app) {
app->need_redraw = false;
}
void gemui_flush_draw(gemui_app_t *app) {
if (!app) return;
app->need_redraw = true;
gemui_render_frame(app);
}
static void gemui_handle_x_events(gemui_app_t *app) {
while (XPending(app->dpy) > 0 && app->running) {
XEvent ev;
@@ -925,6 +934,7 @@ void gemui_draw_input(gemui_app_t *app, cairo_t *cr, gemui_rect_t r,
size_t len = strlen(text);
if (len > 48) len = 48;
for (size_t i = 0; i < len; i++) display[i] = '*';
if (focused && len + 1 < sizeof(display)) display[len++] = '_';
display[len] = '\0';
} else {
snprintf(display, sizeof(display), "%s%s", text, focused ? "_" : "");
@@ -1005,7 +1015,7 @@ int gemui_read_file_line(const char *path, char *buf, size_t sz) {
return 0;
}
int gemui_run_cmd_capture(const char *const argv[], char *out, size_t out_sz) {
int gemui_run_cmd_timeout(const char *const argv[], char *out, size_t out_sz, int timeout_ms) {
if (!argv || !argv[0]) return -1;
int pfd[2];
if (pipe(pfd) < 0) return -1;
@@ -1016,33 +1026,90 @@ int gemui_run_cmd_capture(const char *const argv[], char *out, size_t out_sz) {
return -1;
}
if (pid == 0) {
setpgid(0, 0);
int devnull = open("/dev/null", O_RDONLY);
if (devnull >= 0) {
dup2(devnull, STDIN_FILENO);
if (devnull > 2) close(devnull);
} else {
close(STDIN_FILENO);
}
close(pfd[0]);
dup2(pfd[1], STDOUT_FILENO);
dup2(pfd[1], STDERR_FILENO);
close(pfd[1]);
if (pfd[1] > 2) close(pfd[1]);
execvp(argv[0], (char *const *)argv);
_exit(127);
}
close(pfd[1]);
int fl = fcntl(pfd[0], F_GETFL, 0);
if (fl >= 0) fcntl(pfd[0], F_SETFL, fl | O_NONBLOCK);
size_t off = 0;
char tmp[256];
while (1) {
ssize_t n = read(pfd[0], tmp, sizeof(tmp));
if (n <= 0) break;
if (out && off + 1 < out_sz) {
size_t cp = (size_t)n;
if (cp > out_sz - 1 - off) cp = out_sz - 1 - off;
memcpy(out + off, tmp, cp);
off += cp;
char tmp[512];
uint64_t deadline = (timeout_ms > 0) ? (gemui_now_ms() + (uint64_t)timeout_ms) : UINT64_MAX;
bool timed_out = false;
bool eof = false;
while (!eof) {
int wait_ms = -1;
if (timeout_ms > 0) {
uint64_t now = gemui_now_ms();
if (now >= deadline) {
timed_out = true;
break;
}
uint64_t rem = deadline - now;
wait_ms = rem > 60000ULL ? 60000 : (int)rem;
}
struct pollfd pf = { .fd = pfd[0], .events = POLLIN | POLLHUP };
int pr = poll(&pf, 1, wait_ms);
if (pr < 0) {
if (errno == EINTR) continue;
break;
}
if (pr == 0) {
timed_out = true;
break;
}
while (1) {
ssize_t n = read(pfd[0], tmp, sizeof(tmp));
if (n > 0) {
if (out && off + 1 < out_sz) {
size_t cp = (size_t)n;
if (cp > out_sz - 1 - off) cp = out_sz - 1 - off;
memcpy(out + off, tmp, cp);
off += cp;
}
} else if (n == 0) {
eof = true;
break;
} else {
if (errno == EINTR) continue;
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
eof = true;
break;
}
}
}
if (out && out_sz > 0) out[off] = '\0';
close(pfd[0]);
if (timed_out) {
kill(-pid, SIGKILL);
kill(pid, SIGKILL);
}
int status = 0;
if (waitpid(pid, &status, 0) < 0) return -1;
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) return timed_out ? 124 : -1;
}
if (timed_out) return 124;
return WIFEXITED(status) ? WEXITSTATUS(status) : 1;
}
int gemui_run_cmd_capture(const char *const argv[], char *out, size_t out_sz) {
return gemui_run_cmd_timeout(argv, out, out_sz, 2500);
}
int gemui_spawn_detached(const char *const argv[]) {
if (!argv || !argv[0]) return -1;
pid_t pid = fork();
+2
View File
@@ -121,6 +121,7 @@ int gemui_add_fd(gemui_app_t *app, int fd, gemui_fd_cb cb, void *userdata);
void gemui_del_fd(gemui_app_t *app, int fd);
void gemui_set_timer_ms(gemui_app_t *app, int interval_ms, gemui_timer_cb cb, void *userdata);
void gemui_request_redraw(gemui_app_t *app);
void gemui_flush_draw(gemui_app_t *app);
void gemui_show_toast(gemui_app_t *app, const char *msg, int duration_ms);
int gemui_run(gemui_app_t *app);
void gemui_quit(gemui_app_t *app, int code);
@@ -157,6 +158,7 @@ void gemui_draw_toast(gemui_app_t *app, cairo_t *cr);
int gemui_unix_cmd(const char *sock_path, const char *cmd, char *resp, size_t resp_sz);
int gemui_read_file_line(const char *path, char *buf, size_t sz);
int gemui_run_cmd_timeout(const char *const argv[], char *out, size_t out_sz, int timeout_ms);
int gemui_run_cmd_capture(const char *const argv[], char *out, size_t out_sz);
int gemui_spawn_detached(const char *const argv[]);
void gemui_utf8_pop_char(char *buf);
+1 -1
View File
@@ -16,7 +16,7 @@ static int borderpx = 6;
* 4: value of shell in /etc/passwd
* 5: value of shell in config.h
*/
static char *shell = "/bin/sh";
static char *shell = "/bin/bash";
char *utmp = NULL;
/* scroll program: to enable use a string like "scroll" */
char *scroll = NULL;
+1 -1
View File
@@ -16,7 +16,7 @@ static int borderpx = 6;
* 4: value of shell in /etc/passwd
* 5: value of shell in config.h
*/
static char *shell = "/bin/sh";
static char *shell = "/bin/bash";
char *utmp = NULL;
/* scroll program: to enable use a string like "scroll" */
char *scroll = NULL;
+2 -2
View File
@@ -6,8 +6,8 @@ MANPREFIX = $(PREFIX)/share/man
PKG_CONFIG ?= pkg-config
INCS = $(shell $(PKG_CONFIG) --cflags fontconfig freetype2 harfbuzz xft x11)
LIBS = -lm -lrt -lutil $(shell $(PKG_CONFIG) --libs fontconfig freetype2 harfbuzz xft x11)
INCS = $(shell $(PKG_CONFIG) --cflags fontconfig freetype2 harfbuzz xft x11 xfixes xi)
LIBS = -lm -lrt -lutil $(shell $(PKG_CONFIG) --libs fontconfig freetype2 harfbuzz xft x11 xfixes xi)
STCPPFLAGS = -DVERSION=\"$(VERSION)\" -D_XOPEN_SOURCE=600
STCFLAGS = $(INCS) $(STCPPFLAGS) $(CPPFLAGS) $(CFLAGS) -O2 -std=gnu99 -Wall
+111
View File
@@ -10,7 +10,10 @@
#include <libgen.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/XInput2.h>
#include <X11/keysym.h>
#include <X11/Xft/Xft.h>
#include <X11/XKBlib.h>
@@ -126,6 +129,10 @@ typedef struct {
int isfixed; /* is fixed geometry? */
int l, t; /* left and top offset */
int gm; /* geometry mask */
Cursor vpointer, bpointer;
int pointerisvisible;
int xi_opcode;
int xfixes_ok;
} XWindow;
typedef struct {
@@ -205,6 +212,9 @@ static void mousereport(XEvent *);
static char *kmap(KeySym, uint);
static int match(uint, uint);
static void updateselectionmodeatom(void);
static void st_xi_raw_select(int);
static void xhidepointer(void);
static void xshowpointer(void);
static void run(void);
static void usage(void);
@@ -529,6 +539,7 @@ bpress(XEvent *e)
struct timespec now;
int snap;
xshowpointer();
if (!selectionmodeenabled)
return;
@@ -768,6 +779,7 @@ brelease(XEvent *e)
void
bmotion(XEvent *e)
{
xshowpointer();
if (IS_SET(MODE_MOUSE) && !(e->xbutton.state & forcemousemod)) {
mousereport(e);
return;
@@ -1252,6 +1264,38 @@ xinit(int cols, int rows)
xw.selectionmode = XInternAtom(xw.dpy, "_ST_SELMODE", False);
updateselectionmodeatom();
/* pointer cursor setup and blank cursor for hide-while-typing */
{
int ev_base = 0, err_base = 0;
char blank_bits[1] = {0};
Pixmap blank = XCreateBitmapFromData(xw.dpy, xw.win, blank_bits, 1, 1);
XColor black = {0};
XColor xmousefg = {0}, xmousebg = {0};
xw.xfixes_ok = XFixesQueryExtension(xw.dpy, &ev_base, &err_base) ? 1 : 0;
xw.xi_opcode = -1;
if (XQueryExtension(xw.dpy, "XInputExtension", &xw.xi_opcode, &ev_base, &err_base)) {
int maj = 2, min = 2;
if (XIQueryVersion(xw.dpy, &maj, &min) != Success)
xw.xi_opcode = -1;
} else {
xw.xi_opcode = -1;
}
xw.vpointer = XCreateFontCursor(xw.dpy, mouseshape);
xmousefg.red = dc.col[mousefg].color.red;
xmousefg.green = dc.col[mousefg].color.green;
xmousefg.blue = dc.col[mousefg].color.blue;
xmousebg.red = dc.col[mousebg].color.red;
xmousebg.green = dc.col[mousebg].color.green;
xmousebg.blue = dc.col[mousebg].color.blue;
XRecolorCursor(xw.dpy, xw.vpointer, &xmousefg, &xmousebg);
xw.bpointer = XCreatePixmapCursor(xw.dpy, blank, blank, &black, &black, 0, 0);
XFreePixmap(xw.dpy, blank);
xw.pointerisvisible = 1;
XDefineCursor(xw.dpy, xw.win, xw.vpointer);
}
win.mode = MODE_NUMLOCK;
resettitle();
xhints();
@@ -1744,12 +1788,64 @@ visibility(XEvent *ev)
void
unmap(XEvent *ev)
{
xshowpointer();
win.mode &= ~MODE_VISIBLE;
}
void
st_xi_raw_select(int enable)
{
unsigned char mask[XIMaskLen(XI_LASTEVENT)];
XIEventMask evmask;
if (xw.xi_opcode < 0)
return;
memset(mask, 0, sizeof(mask));
if (enable) {
XISetMask(mask, XI_RawMotion);
XISetMask(mask, XI_RawButtonPress);
XISetMask(mask, XI_RawTouchBegin);
}
evmask.deviceid = XIAllMasterDevices;
evmask.mask_len = (int)sizeof(mask);
evmask.mask = mask;
XISelectEvents(xw.dpy, XRootWindow(xw.dpy, xw.scr), &evmask, 1);
}
void
xhidepointer(void)
{
if (!xw.pointerisvisible)
return;
XDefineCursor(xw.dpy, xw.win, xw.bpointer);
if (xw.xfixes_ok)
XFixesHideCursor(xw.dpy, XRootWindow(xw.dpy, xw.scr));
st_xi_raw_select(1);
xsetpointermotion(1);
xw.pointerisvisible = 0;
XFlush(xw.dpy);
}
void
xshowpointer(void)
{
if (xw.pointerisvisible)
return;
xw.pointerisvisible = 1;
st_xi_raw_select(0);
if (xw.xfixes_ok)
XFixesShowCursor(xw.dpy, XRootWindow(xw.dpy, xw.scr));
XDefineCursor(xw.dpy, xw.win, xw.vpointer);
if (!IS_SET(MODE_MOUSEMOTION) && !IS_SET(MODE_MOUSEMANY))
xsetpointermotion(0);
XFlush(xw.dpy);
}
void
xsetpointermotion(int set)
{
if (!set && !xw.pointerisvisible)
return;
MODBIT(xw.attrs.event_mask, set, PointerMotionMask);
XChangeWindowAttributes(xw.dpy, xw.win, CWEventMask, &xw.attrs);
}
@@ -1806,6 +1902,7 @@ focus(XEvent *ev)
if (IS_SET(MODE_FOCUS))
ttywrite("\033[I", 3, 0);
} else {
xshowpointer();
if (xw.ime.xic)
XUnsetICFocus(xw.ime.xic);
if (e->mode == NotifyGrab)
@@ -1879,12 +1976,15 @@ kpress(XEvent *ev)
if (status == XLookupNone)
return;
if (status == XLookupChars) {
xhidepointer();
ttywrite(buf, len, 1);
return;
}
} else {
len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
}
if (!IsModifierKey(ksym))
xhidepointer();
/* 1. shortcuts */
for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
if (ksym == bp->keysym && match(bp->mod, e->state)) {
@@ -2036,6 +2136,17 @@ run(void)
while (XPending(xw.dpy)) {
xev = 1;
XNextEvent(xw.dpy, &ev);
if (ev.type == GenericEvent) {
if (ev.xcookie.extension == xw.xi_opcode &&
XGetEventData(xw.dpy, &ev.xcookie)) {
if (ev.xcookie.evtype == XI_RawMotion ||
ev.xcookie.evtype == XI_RawButtonPress ||
ev.xcookie.evtype == XI_RawTouchBegin)
xshowpointer();
XFreeEventData(xw.dpy, &ev.xcookie);
}
continue;
}
if (ev.type == ClientMessage &&
(Atom)ev.xclient.data.l[0] == xw.wmdeletewin) {
cmessage(&ev);