feat: initial ANODE desktop environment adapted from Geminux
This commit is contained in:
commit
65bfe3c65a
137 files changed
+33788
No files matched your search
@@ -0,0 +1,355 @@
|
||||
/*
|
||||
* Header-only C11 parsers for PipeWire / PulseAudio / ALSA / sndio / OSS audio endpoints.
|
||||
*/
|
||||
#ifndef ANODE_AUDIO_PARSE_H
|
||||
#define ANODE_AUDIO_PARSE_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ANODE_MAX_AUDIO_NODES 24
|
||||
#define ANODE_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;
|
||||
} anode_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;
|
||||
} anode_audio_profile_t;
|
||||
|
||||
static inline bool anode_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 anode_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", (int)(sink_sz - 1), 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", (int)(src_sz - 1), v);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline int anode_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 anode_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 anode_parse_pactl_nodes(const char *txt, const char *header_prefix,
|
||||
const char *default_name, bool is_stream,
|
||||
anode_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;
|
||||
anode_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), "%.23s", k + 7);
|
||||
} else if (strncmp(k, "Name: ", 6) == 0) {
|
||||
snprintf(cur_node->name, sizeof(cur_node->name), "%.127s", 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), "%.127s", cur_node->name);
|
||||
}
|
||||
} else if (strncmp(k, "Description: ", 13) == 0) {
|
||||
snprintf(cur_node->label, sizeof(cur_node->label), "%.127s", 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 = anode_extract_first_pct(k + 8);
|
||||
} else if (strncmp(k, "Active Port: ", 13) == 0) {
|
||||
snprintf(cur_node->port, sizeof(cur_node->port), "%.63s", k + 13);
|
||||
}
|
||||
} else if (in_props && ln[0] == '\t' && ln[1] == '\t') {
|
||||
const char *k = ln + 2;
|
||||
if (!anode_extract_quoted_prop(k, "application.name", app_name, sizeof(app_name))) {
|
||||
anode_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]) {
|
||||
anode_extract_quoted_prop(k, "device.description", cur_node->label, sizeof(cur_node->label));
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static inline int anode_parse_pactl_cards(const char *cards_txt,
|
||||
anode_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;
|
||||
|
||||
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), "%.127s", k + 6);
|
||||
if (!card_label[0]) snprintf(card_label, sizeof(card_label), "%.127s", card_name);
|
||||
for (int i = card_start; i < total; i++) {
|
||||
snprintf(out[i].card_name, sizeof(out[i].card_name), "%.127s", card_name);
|
||||
}
|
||||
} else if (strncmp(k, "Active Profile: ", 16) == 0) {
|
||||
snprintf(active_prof, sizeof(active_prof), "%.95s", 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 (anode_extract_quoted_prop(k, "device.description", card_label, sizeof(card_label)) ||
|
||||
(!card_label[0] && anode_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;
|
||||
anode_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;
|
||||
}
|
||||
|
||||
static inline int anode_parse_amixer_scontrols(const char *txt,
|
||||
anode_audio_node_t *out, int max_out) {
|
||||
if (!txt || !out || max_out <= 0) return 0;
|
||||
int n = 0;
|
||||
const char *cur = txt;
|
||||
while (*cur && n < max_out) {
|
||||
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;
|
||||
|
||||
const char *q1 = strchr(ln, '\'');
|
||||
if (!q1) continue;
|
||||
const char *q2 = strchr(q1 + 1, '\'');
|
||||
if (!q2 || q2 == q1 + 1) continue;
|
||||
|
||||
anode_audio_node_t *nd = &out[n];
|
||||
memset(nd, 0, sizeof(*nd));
|
||||
nd->index = n;
|
||||
size_t len = (size_t)(q2 - (q1 + 1));
|
||||
if (len >= sizeof(nd->name)) len = sizeof(nd->name) - 1;
|
||||
memcpy(nd->name, q1 + 1, len);
|
||||
nd->name[len] = '\0';
|
||||
snprintf(nd->label, sizeof(nd->label), "%s", nd->name);
|
||||
snprintf(nd->state, sizeof(nd->state), "ALSA");
|
||||
nd->vol_pct = 100;
|
||||
nd->is_default = (strcmp(nd->name, "Master") == 0);
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline int anode_parse_sndioctl(const char *txt, bool want_input,
|
||||
anode_audio_node_t *out, int max_out) {
|
||||
if (!txt || !out || max_out <= 0) return 0;
|
||||
int n = 0;
|
||||
const char *cur = txt;
|
||||
while (*cur && n < max_out) {
|
||||
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;
|
||||
|
||||
char *eq = strchr(ln, '=');
|
||||
if (!eq) continue;
|
||||
*eq = '\0';
|
||||
const char *key = ln;
|
||||
const char *val = eq + 1;
|
||||
if (!strstr(key, ".level")) continue;
|
||||
bool is_in = (strncmp(key, "input.", 6) == 0 || strncmp(key, "record.", 7) == 0);
|
||||
if (is_in != want_input) continue;
|
||||
|
||||
anode_audio_node_t *nd = &out[n];
|
||||
memset(nd, 0, sizeof(*nd));
|
||||
nd->index = n;
|
||||
snprintf(nd->name, sizeof(nd->name), "%.127s", key);
|
||||
snprintf(nd->label, sizeof(nd->label), "%.127s", key);
|
||||
snprintf(nd->state, sizeof(nd->state), "sndio");
|
||||
nd->vol_pct = (int)(atof(val) * 100.0 + 0.5);
|
||||
nd->is_default = (n == 0);
|
||||
n++;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif /* ANODE_AUDIO_PARSE_H */
|
||||
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* Header-only C11 helpers for parsing bluetoothctl device listings into sorted entries.
|
||||
*/
|
||||
#ifndef ANODE_BT_LIST_H
|
||||
#define ANODE_BT_LIST_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ANODE_MAX_BT_DEVS 32
|
||||
|
||||
typedef struct {
|
||||
char mac[24];
|
||||
char name[96];
|
||||
bool paired;
|
||||
bool connected;
|
||||
} anode_bt_dev_t;
|
||||
|
||||
static inline bool anode_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 anode_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 anode_bt_feed_list(const char *text, anode_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;
|
||||
|
||||
anode_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 (!anode_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 anode_bt_dev_cmp(const void *pa, const void *pb) {
|
||||
const anode_bt_dev_t *a = (const anode_bt_dev_t *)pa;
|
||||
const anode_bt_dev_t *b = (const anode_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 anode_parse_bt_devices(const char *all_txt,
|
||||
const char *paired_txt,
|
||||
const char *conn_txt,
|
||||
anode_bt_dev_t *devs,
|
||||
int max_devs) {
|
||||
int n = 0;
|
||||
anode_bt_feed_list(all_txt, devs, &n, max_devs, false, false);
|
||||
anode_bt_feed_list(paired_txt, devs, &n, max_devs, true, false);
|
||||
anode_bt_feed_list(conn_txt, devs, &n, max_devs, true, true);
|
||||
if (n > 1) qsort(devs, (size_t)n, sizeof(devs[0]), anode_bt_dev_cmp);
|
||||
return n;
|
||||
}
|
||||
|
||||
#endif /* ANODE_BT_LIST_H */
|
||||
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* Header-only C11 helpers for validating DNS resolver presets and custom nameservers.
|
||||
*/
|
||||
#ifndef ANODE_DNS_VALID_H
|
||||
#define ANODE_DNS_VALID_H
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ANODE_DNS_DEFAULT 0
|
||||
#define ANODE_DNS_CLOUDFLARE 1
|
||||
#define ANODE_DNS_GOOGLE 2
|
||||
#define ANODE_DNS_RADICAL 3
|
||||
#define ANODE_DNS_QUAD9 4
|
||||
#define ANODE_DNS_CUSTOM 5
|
||||
#define ANODE_DNS_COUNT 6
|
||||
|
||||
#define ANODE_DNS_MAX_SERVERS 6
|
||||
|
||||
typedef struct {
|
||||
const char *mode;
|
||||
const char *label;
|
||||
const char *v4_csv;
|
||||
const char *v6_csv;
|
||||
} anode_dns_preset_t;
|
||||
|
||||
static const anode_dns_preset_t ANODE_DNS_PRESETS[ANODE_DNS_COUNT] = {
|
||||
{"default", "Default (Router / DHCP)", "", ""},
|
||||
{"cloudflare", "Cloudflare DNS", "1.1.1.1,1.0.0.1", "2606:4700:4700::1111"},
|
||||
{"google", "Google Public DNS", "8.8.8.8,8.8.4.4", "2001:4860:4860::8888"},
|
||||
{"radicaldns", "RadicalDNS", "88.198.92.222,192.71.166.92", "2a01:4f8:1c0c:82c0::1"},
|
||||
{"quad9", "Quad9 (Filtered)", "9.9.9.9,149.112.112.112", "2620:fe::fe"},
|
||||
{"custom", "Custom Nameservers", "", ""},
|
||||
};
|
||||
|
||||
static inline int anode_dns_mode_index(const char *mode) {
|
||||
if (!mode || !mode[0]) return -1;
|
||||
for (int i = 0; i < ANODE_DNS_COUNT; i++) {
|
||||
if (strcmp(mode, ANODE_DNS_PRESETS[i].mode) == 0) return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static inline bool anode_dns_mode_valid(const char *mode) {
|
||||
return anode_dns_mode_index(mode) >= 0;
|
||||
}
|
||||
|
||||
static inline bool anode_dns_token_chars_ok(const char *tok) {
|
||||
if (!tok || !tok[0]) return false;
|
||||
for (const unsigned char *p = (const unsigned char *)tok; *p; p++) {
|
||||
bool ok = (*p >= '0' && *p <= '9') ||
|
||||
(*p >= 'a' && *p <= 'f') ||
|
||||
(*p >= 'A' && *p <= 'F') ||
|
||||
*p == '.' || *p == ':';
|
||||
if (!ok) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool anode_dns_v4_valid(const struct in_addr *a) {
|
||||
uint32_t ip = ntohl(a->s_addr);
|
||||
if (ip == 0x00000000U || ip == 0xffffffffU) return false;
|
||||
if ((ip >> 24) == 0U) return false;
|
||||
if ((ip & 0xf0000000U) == 0xe0000000U) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool anode_dns_v6_valid(const struct in6_addr *a) {
|
||||
static const uint8_t z[16] = {0};
|
||||
if (memcmp(a->s6_addr, z, 16) == 0) return false;
|
||||
if (a->s6_addr[0] == 0xff) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline int anode_parse_dns_servers(const char *raw,
|
||||
char v4[][16], int *n_v4, int max_v4,
|
||||
char v6[][46], int *n_v6, int max_v6,
|
||||
char *norm_csv, size_t norm_sz) {
|
||||
int c4 = 0, c6 = 0;
|
||||
char loc_v4[ANODE_DNS_MAX_SERVERS][16];
|
||||
char loc_v6[ANODE_DNS_MAX_SERVERS][46];
|
||||
|
||||
if (n_v4) *n_v4 = 0;
|
||||
if (n_v6) *n_v6 = 0;
|
||||
if (norm_csv && norm_sz > 0) norm_csv[0] = '\0';
|
||||
if (!raw || !raw[0]) return -1;
|
||||
|
||||
const char *p = raw;
|
||||
while (*p) {
|
||||
while (*p == ',' || *p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p++;
|
||||
if (!*p) break;
|
||||
const char *start = p;
|
||||
while (*p && *p != ',' && *p != ' ' && *p != '\t' && *p != '\r' && *p != '\n') p++;
|
||||
size_t len = (size_t)(p - start);
|
||||
if (len == 0 || len >= 46) return -1;
|
||||
|
||||
char tok[48];
|
||||
memcpy(tok, start, len);
|
||||
tok[len] = '\0';
|
||||
if (!anode_dns_token_chars_ok(tok)) return -1;
|
||||
|
||||
struct in_addr a4;
|
||||
struct in6_addr a6;
|
||||
if (inet_pton(AF_INET, tok, &a4) == 1) {
|
||||
if (!anode_dns_v4_valid(&a4)) return -1;
|
||||
char canon[16];
|
||||
if (!inet_ntop(AF_INET, &a4, canon, sizeof(canon))) return -1;
|
||||
bool dup = false;
|
||||
for (int i = 0; i < c4; i++) {
|
||||
if (strcmp(loc_v4[i], canon) == 0) {
|
||||
dup = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!dup) {
|
||||
if (c4 + c6 >= ANODE_DNS_MAX_SERVERS) return -1;
|
||||
snprintf(loc_v4[c4], sizeof(loc_v4[c4]), "%s", canon);
|
||||
if (v4 && c4 < max_v4) snprintf(v4[c4], 16, "%s", canon);
|
||||
c4++;
|
||||
}
|
||||
} else if (inet_pton(AF_INET6, tok, &a6) == 1) {
|
||||
if (!anode_dns_v6_valid(&a6)) return -1;
|
||||
char canon[46];
|
||||
if (!inet_ntop(AF_INET6, &a6, canon, sizeof(canon))) return -1;
|
||||
bool dup = false;
|
||||
for (int i = 0; i < c6; i++) {
|
||||
if (strcmp(loc_v6[i], canon) == 0) {
|
||||
dup = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!dup) {
|
||||
if (c4 + c6 >= ANODE_DNS_MAX_SERVERS) return -1;
|
||||
snprintf(loc_v6[c6], sizeof(loc_v6[c6]), "%s", canon);
|
||||
if (v6 && c6 < max_v6) snprintf(v6[c6], 46, "%s", canon);
|
||||
c6++;
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int total = c4 + c6;
|
||||
if (total <= 0) return -1;
|
||||
if (n_v4) *n_v4 = c4;
|
||||
if (n_v6) *n_v6 = c6;
|
||||
|
||||
if (norm_csv && norm_sz > 0) {
|
||||
size_t pos = 0;
|
||||
for (int i = 0; i < c4; i++) {
|
||||
int w = snprintf(norm_csv + pos, norm_sz - pos, "%s%s", pos ? "," : "", loc_v4[i]);
|
||||
if (w < 0 || (size_t)w >= norm_sz - pos) return -1;
|
||||
pos += (size_t)w;
|
||||
}
|
||||
for (int i = 0; i < c6; i++) {
|
||||
int w = snprintf(norm_csv + pos, norm_sz - pos, "%s%s", pos ? "," : "", loc_v6[i]);
|
||||
if (w < 0 || (size_t)w >= norm_sz - pos) return -1;
|
||||
pos += (size_t)w;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
static inline int anode_parse_resolv_conf(const char *text, char *summary, size_t summary_sz) {
|
||||
if (summary && summary_sz > 0) summary[0] = '\0';
|
||||
if (!text || !text[0]) return 0;
|
||||
|
||||
int count = 0;
|
||||
size_t pos = 0;
|
||||
const char *cur = text;
|
||||
while (*cur && count < 3) {
|
||||
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;
|
||||
|
||||
char *p = ln;
|
||||
while (*p == ' ' || *p == '\t') p++;
|
||||
if (strncmp(p, "nameserver", 10) != 0) continue;
|
||||
p += 10;
|
||||
if (*p != ' ' && *p != '\t') continue;
|
||||
while (*p == ' ' || *p == '\t') p++;
|
||||
char *end = p;
|
||||
while (*end && *end != ' ' && *end != '\t' && *end != '\r' && *end != '#') end++;
|
||||
*end = '\0';
|
||||
if (!anode_dns_token_chars_ok(p)) continue;
|
||||
|
||||
struct in_addr a4;
|
||||
struct in6_addr a6;
|
||||
if (inet_pton(AF_INET, p, &a4) != 1 && inet_pton(AF_INET6, p, &a6) != 1) continue;
|
||||
|
||||
if (summary && pos + 2 < summary_sz) {
|
||||
int w = snprintf(summary + pos, summary_sz - pos, "%s%s", count ? ", " : "", p);
|
||||
if (w > 0 && (size_t)w < summary_sz - pos) pos += (size_t)w;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif /* ANODE_DNS_VALID_H */
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* ANODE Cross-Unix Platform Abstraction (Linux, FreeBSD, OpenBSD, NetBSD, POSIX)
|
||||
*/
|
||||
#ifndef ANODE_PLAT_H
|
||||
#define ANODE_PLAT_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ANODE_MAX_IFACES 32
|
||||
|
||||
typedef enum {
|
||||
ANODE_AUDIO_NONE = 0,
|
||||
ANODE_AUDIO_PIPEWIRE,
|
||||
ANODE_AUDIO_PULSE,
|
||||
ANODE_AUDIO_ALSA,
|
||||
ANODE_AUDIO_SNDIO,
|
||||
ANODE_AUDIO_OSS
|
||||
} anode_audio_backend_t;
|
||||
|
||||
typedef struct {
|
||||
bool present;
|
||||
bool on_ac;
|
||||
int capacity; /* 0..100, or -1 if unknown */
|
||||
char status[32]; /* Charging, Discharging, Full, AC, Unknown */
|
||||
char detail[64];
|
||||
} anode_battery_info_t;
|
||||
|
||||
typedef struct {
|
||||
char name[32];
|
||||
bool is_up;
|
||||
bool is_running;
|
||||
bool is_loopback;
|
||||
bool is_wireless;
|
||||
char ipv4[48];
|
||||
char ipv6[64];
|
||||
} anode_net_iface_t;
|
||||
|
||||
/* Directories & environment */
|
||||
void anode_config_dir(char *buf, size_t sz);
|
||||
void anode_runtime_dir(char *buf, size_t sz);
|
||||
void anode_themes_sys_dir(char *buf, size_t sz);
|
||||
void anode_default_xth_path(char *buf, size_t sz);
|
||||
|
||||
/* Process execution & privilege escalation */
|
||||
bool anode_have_cmd(const char *cmd);
|
||||
int anode_priv_prefix(const char *argv_out[], int max_slots);
|
||||
int anode_run_cmd_timeout(const char *const argv[], char *out, size_t out_sz, int timeout_ms);
|
||||
int anode_run_priv_timeout(const char *const cmd_argv[], char *out, size_t out_sz, int timeout_ms);
|
||||
int anode_spawn_detached(const char *const argv[]);
|
||||
void anode_signal_procs(const char *const names[], int num_names, int sig);
|
||||
|
||||
/* Battery & AC status */
|
||||
void anode_read_battery(anode_battery_info_t *out);
|
||||
|
||||
/* Backlight (sysfs / brightnessctl / BSD backlight / wsconsctl / xrandr fallback) */
|
||||
bool anode_backlight_get(int *cur_out, int *max_out, char *backend_out, size_t bsz);
|
||||
bool anode_backlight_set(double norm);
|
||||
|
||||
/* Audio backend abstraction */
|
||||
anode_audio_backend_t anode_detect_audio_backend(void);
|
||||
const char *anode_audio_backend_name(anode_audio_backend_t b);
|
||||
bool anode_audio_get_default(bool input, int *pct_out, bool *muted_out);
|
||||
bool anode_audio_set_vol(bool input, int pct);
|
||||
bool anode_audio_toggle_mute(bool input);
|
||||
|
||||
/* Power profiles & system power transitions */
|
||||
bool anode_power_profile_get(char *out, size_t sz);
|
||||
bool anode_power_profile_set(const char *profile);
|
||||
int anode_power_action(const char *action); /* suspend, hibernate, reboot, poweroff */
|
||||
|
||||
/* Network interfaces (POSIX getifaddrs across Linux & BSDs) */
|
||||
int anode_list_net_ifaces(anode_net_iface_t *out, int max_ifaces);
|
||||
bool anode_find_wifi_iface(char *out, size_t sz);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ANODE_PLAT_H */
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* ANODE Power Policy State Machine (shared across anode-powerd and tests)
|
||||
*/
|
||||
#ifndef ANODE_POWER_POLICY_H
|
||||
#define ANODE_POWER_POLICY_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum {
|
||||
ANODE_PWR_ACTIVE = 0,
|
||||
ANODE_PWR_DIMMED,
|
||||
ANODE_PWR_LOCKED,
|
||||
ANODE_PWR_SUSPENDED
|
||||
} anode_pwr_state_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t dim_ms;
|
||||
uint32_t lock_ms;
|
||||
uint32_t suspend_ms;
|
||||
int crit_batt_pct;
|
||||
bool suspend_on_ac;
|
||||
} anode_power_cfg_t;
|
||||
|
||||
typedef struct {
|
||||
bool dim_now;
|
||||
bool restore_brightness;
|
||||
bool lock_now;
|
||||
bool suspend_now;
|
||||
bool warn_low_batt;
|
||||
} anode_power_actions_t;
|
||||
|
||||
static inline anode_power_cfg_t anode_power_cfg_default(void) {
|
||||
anode_power_cfg_t c;
|
||||
c.dim_ms = 60000U;
|
||||
c.lock_ms = 180000U;
|
||||
c.suspend_ms = 600000U;
|
||||
c.crit_batt_pct = 5;
|
||||
c.suspend_on_ac = false;
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline void anode_power_policy_step(const anode_power_cfg_t *cfg,
|
||||
anode_pwr_state_t *state,
|
||||
uint32_t idle_ms,
|
||||
bool on_ac,
|
||||
int batt_pct,
|
||||
bool batt_present,
|
||||
bool screen_locked,
|
||||
anode_power_actions_t *out) {
|
||||
anode_power_actions_t a = {0};
|
||||
if (!cfg || !state) {
|
||||
if (out) *out = a;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Critical battery check takes precedence on battery */
|
||||
if (batt_present && !on_ac && batt_pct >= 0 && batt_pct <= cfg->crit_batt_pct) {
|
||||
a.warn_low_batt = true;
|
||||
if (batt_pct <= (cfg->crit_batt_pct > 2 ? cfg->crit_batt_pct - 2 : 1)) {
|
||||
if (*state != ANODE_PWR_SUSPENDED) {
|
||||
*state = ANODE_PWR_SUSPENDED;
|
||||
a.suspend_now = true;
|
||||
}
|
||||
if (out) *out = a;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* User activity resumes from dimmed/suspended */
|
||||
if (idle_ms < cfg->dim_ms) {
|
||||
if (*state == ANODE_PWR_DIMMED || *state == ANODE_PWR_SUSPENDED) {
|
||||
a.restore_brightness = true;
|
||||
}
|
||||
*state = screen_locked ? ANODE_PWR_LOCKED : ANODE_PWR_ACTIVE;
|
||||
if (out) *out = a;
|
||||
return;
|
||||
}
|
||||
|
||||
if (cfg->suspend_ms > 0 && idle_ms >= cfg->suspend_ms && (cfg->suspend_on_ac || !on_ac)) {
|
||||
if (*state != ANODE_PWR_SUSPENDED) {
|
||||
if (!screen_locked) a.lock_now = true;
|
||||
a.suspend_now = true;
|
||||
*state = ANODE_PWR_SUSPENDED;
|
||||
}
|
||||
if (out) *out = a;
|
||||
return;
|
||||
}
|
||||
|
||||
if (cfg->lock_ms > 0 && idle_ms >= cfg->lock_ms) {
|
||||
if (*state < ANODE_PWR_LOCKED) {
|
||||
if (!screen_locked) a.lock_now = true;
|
||||
*state = ANODE_PWR_LOCKED;
|
||||
}
|
||||
if (out) *out = a;
|
||||
return;
|
||||
}
|
||||
|
||||
if (cfg->dim_ms > 0 && idle_ms >= cfg->dim_ms && *state == ANODE_PWR_ACTIVE) {
|
||||
*state = ANODE_PWR_DIMMED;
|
||||
a.dim_now = true;
|
||||
}
|
||||
|
||||
if (out) *out = a;
|
||||
}
|
||||
|
||||
#endif /* ANODE_POWER_POLICY_H */
|
||||
@@ -0,0 +1,173 @@
|
||||
/*
|
||||
* ANODE Fractional DPI Scaling & Xresources Helpers
|
||||
*/
|
||||
#ifndef ANODE_SCALE_H
|
||||
#define ANODE_SCALE_H
|
||||
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <X11/Xatom.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xresource.h>
|
||||
|
||||
static inline double anode_clamp_scale(double s) {
|
||||
if (s < 0.5) return 0.5;
|
||||
if (s > 4.0) return 4.0;
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline double anode_clamp_dpi(double d) {
|
||||
if (d < 48.0) return 48.0;
|
||||
if (d > 384.0) return 384.0;
|
||||
return d;
|
||||
}
|
||||
|
||||
static inline double anode_scale_from_dpi(double dpi) {
|
||||
if (dpi <= 1.0) return 1.0;
|
||||
return anode_clamp_scale(dpi / 96.0);
|
||||
}
|
||||
|
||||
static inline int anode_scale_px(int logical_px, double scale) {
|
||||
if (logical_px <= 0) return 0;
|
||||
double s = anode_clamp_scale(scale);
|
||||
int v = (int)floor((double)logical_px * s + 0.5);
|
||||
return v < 1 ? 1 : v;
|
||||
}
|
||||
|
||||
static inline int anode_parse_positive_double(const char *s, double *out) {
|
||||
if (!s || !s[0] || !out) return 0;
|
||||
char *end = NULL;
|
||||
double v = strtod(s, &end);
|
||||
if (end == s || v <= 0.0) return 0;
|
||||
*out = v;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline int anode_parse_bool_str(const char *s, int defval) {
|
||||
if (!s || !s[0]) return defval;
|
||||
if (s[0] == '1' || s[0] == 'y' || s[0] == 'Y' || s[0] == 't' || s[0] == 'T') return 1;
|
||||
if (s[0] == '0' || s[0] == 'n' || s[0] == 'N' || s[0] == 'f' || s[0] == 'F') return 0;
|
||||
return defval;
|
||||
}
|
||||
|
||||
static inline double anode_detect_edid_dpi(Display *dpy) {
|
||||
if (!dpy) return 96.0;
|
||||
int scr = DefaultScreen(dpy);
|
||||
int pw = DisplayWidth(dpy, scr);
|
||||
int mm = DisplayWidthMM(dpy, scr);
|
||||
if (pw > 0 && mm > 20) {
|
||||
double dpi = (double)pw * 25.4 / (double)mm;
|
||||
if (dpi >= 72.0 && dpi <= 384.0) {
|
||||
return dpi;
|
||||
}
|
||||
}
|
||||
return 96.0;
|
||||
}
|
||||
|
||||
static inline XrmDatabase anode_open_xres_db(Display *dpy) {
|
||||
XrmInitialize();
|
||||
XrmDatabase db = NULL;
|
||||
const char *override = getenv("ANODE_XRESOURCES");
|
||||
if (override && override[0]) {
|
||||
db = XrmGetFileDatabase(override);
|
||||
if (db) return db;
|
||||
}
|
||||
|
||||
const char *home = getenv("HOME");
|
||||
if (home && home[0]) {
|
||||
char path[512];
|
||||
snprintf(path, sizeof(path), "%s/.Xresources", home);
|
||||
db = XrmGetFileDatabase(path);
|
||||
}
|
||||
|
||||
if (dpy) {
|
||||
char *xrm = XResourceManagerString(dpy);
|
||||
XrmDatabase mdb = NULL;
|
||||
if (xrm && xrm[0]) {
|
||||
mdb = XrmGetStringDatabase(xrm);
|
||||
} else {
|
||||
Atom actual_type = None;
|
||||
int actual_format = 0;
|
||||
unsigned long nitems = 0, bytes_after = 0;
|
||||
unsigned char *prop = NULL;
|
||||
if (XGetWindowProperty(dpy, DefaultRootWindow(dpy), XA_RESOURCE_MANAGER,
|
||||
0L, 65536L, False, XA_STRING, &actual_type,
|
||||
&actual_format, &nitems, &bytes_after, &prop) == Success && prop) {
|
||||
if (actual_type == XA_STRING && actual_format == 8 && nitems > 0) {
|
||||
mdb = XrmGetStringDatabase((const char *)prop);
|
||||
}
|
||||
XFree(prop);
|
||||
}
|
||||
}
|
||||
if (mdb) {
|
||||
if (db) {
|
||||
XrmMergeDatabases(mdb, &db);
|
||||
} else {
|
||||
db = mdb;
|
||||
}
|
||||
}
|
||||
}
|
||||
return db;
|
||||
}
|
||||
|
||||
static inline void anode_read_xres_scale_from_db(XrmDatabase db, Display *dpy,
|
||||
double *dpi_out, double *scale_out,
|
||||
int *touch_out) {
|
||||
double dpi = 0.0;
|
||||
double scale = 0.0;
|
||||
int touch = 0;
|
||||
int have_dpi = 0;
|
||||
int have_scale = 0;
|
||||
|
||||
if (db) {
|
||||
char *type = NULL;
|
||||
XrmValue val;
|
||||
double v = 0.0;
|
||||
if (XrmGetResource(db, "Xft.dpi", "Xft.Dpi", &type, &val) && val.addr &&
|
||||
anode_parse_positive_double(val.addr, &v)) {
|
||||
dpi = anode_clamp_dpi(v);
|
||||
have_dpi = 1;
|
||||
}
|
||||
if (XrmGetResource(db, "anode.scale", "Anode.Scale", &type, &val) && val.addr &&
|
||||
anode_parse_positive_double(val.addr, &v)) {
|
||||
scale = anode_clamp_scale(v);
|
||||
have_scale = 1;
|
||||
}
|
||||
if (XrmGetResource(db, "anode.touchMode", "Anode.TouchMode", &type, &val) && val.addr) {
|
||||
touch = anode_parse_bool_str(val.addr, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (!have_scale && !have_dpi) {
|
||||
double env_s = 0.0;
|
||||
if (anode_parse_positive_double(getenv("ANODE_SCALE"), &env_s)) {
|
||||
scale = anode_clamp_scale(env_s);
|
||||
dpi = 96.0 * scale;
|
||||
} else {
|
||||
dpi = anode_detect_edid_dpi(dpy);
|
||||
/* Avoid tiny/odd virtual screen DPI shifts on standard ~96 DPI displays */
|
||||
if (dpi >= 84.0 && dpi <= 108.0) dpi = 96.0;
|
||||
scale = anode_scale_from_dpi(dpi);
|
||||
}
|
||||
} else if (have_scale && !have_dpi) {
|
||||
dpi = anode_clamp_dpi(96.0 * scale);
|
||||
} else if (have_dpi && !have_scale) {
|
||||
scale = anode_scale_from_dpi(dpi);
|
||||
}
|
||||
|
||||
if (dpi_out) *dpi_out = dpi;
|
||||
if (scale_out) *scale_out = scale;
|
||||
if (touch_out) *touch_out = touch;
|
||||
}
|
||||
|
||||
static inline void anode_read_xres_scale(Display *dpy, double *dpi_out,
|
||||
double *scale_out, int *touch_out) {
|
||||
XrmDatabase db = anode_open_xres_db(dpy);
|
||||
anode_read_xres_scale_from_db(db, dpy, dpi_out, scale_out, touch_out);
|
||||
if (db) XrmDestroyDatabase(db);
|
||||
}
|
||||
|
||||
#endif /* ANODE_SCALE_H */
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Header-only C11 helpers for validating IANA timezone names and TZif files.
|
||||
*/
|
||||
#ifndef ANODE_TZ_VALID_H
|
||||
#define ANODE_TZ_VALID_H
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static inline bool anode_tz_name_valid(const char *tz) {
|
||||
size_t len;
|
||||
if (!tz) return false;
|
||||
len = strlen(tz);
|
||||
if (len == 0 || len > 95) return false;
|
||||
if (tz[0] == '/' || tz[len - 1] == '/') return false;
|
||||
if (strstr(tz, "..") != NULL || strstr(tz, "//") != NULL) return false;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
unsigned char c = (unsigned char)tz[i];
|
||||
bool ok = (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
|
||||
(c >= '0' && c <= '9') || c == '_' || c == '+' || c == '-' || c == '/';
|
||||
if (!ok) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline bool anode_tz_file_valid(const char *zoneinfo_root, const char *tz) {
|
||||
if (!zoneinfo_root || !zoneinfo_root[0] || !anode_tz_name_valid(tz)) return false;
|
||||
char path[512];
|
||||
int n = snprintf(path, sizeof(path), "%s/%s", zoneinfo_root, tz);
|
||||
if (n < 0 || (size_t)n >= sizeof(path)) return false;
|
||||
struct stat st;
|
||||
if (stat(path, &st) != 0 || !S_ISREG(st.st_mode)) return false;
|
||||
int fd = open(path, O_RDONLY);
|
||||
if (fd < 0) return false;
|
||||
char hdr[4];
|
||||
ssize_t r = read(fd, hdr, sizeof(hdr));
|
||||
close(fd);
|
||||
return r == 4 && memcmp(hdr, "TZif", 4) == 0;
|
||||
}
|
||||
|
||||
#endif /* ANODE_TZ_VALID_H */
|
||||
@@ -0,0 +1,301 @@
|
||||
/*
|
||||
* Header-only C11 helpers for parsing and grouping Wi-Fi scan output
|
||||
* (nmcli terse, wpa_cli scan_results, and BSD ifconfig list scan).
|
||||
*/
|
||||
#ifndef ANODE_WIFI_LIST_H
|
||||
#define ANODE_WIFI_LIST_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ANODE_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;
|
||||
} anode_wifi_group_t;
|
||||
|
||||
static inline int anode_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 anode_wifi_chan_joinable(int chan) {
|
||||
return (chan >= 1 && chan <= 13) ||
|
||||
(chan >= 36 && chan <= 48) ||
|
||||
(chan >= 149 && chan <= 165);
|
||||
}
|
||||
|
||||
static inline int anode_wifi_group_cmp(const void *pa, const void *pb) {
|
||||
const anode_wifi_group_t *a = (const anode_wifi_group_t *)pa;
|
||||
const anode_wifi_group_t *b = (const anode_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 anode_wifi_freq_to_chan(int mhz) {
|
||||
if (mhz == 2484) return 14;
|
||||
if (mhz >= 2412 && mhz <= 2472) return (mhz - 2407) / 5;
|
||||
if (mhz >= 5000 && mhz <= 5895) return (mhz - 5000) / 5;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline void anode_wifi_upsert_ap(anode_wifi_group_t *groups, int *count, int max_groups,
|
||||
const char *ssid, const char *bssid,
|
||||
int chan, int sig, const char *sec, bool active) {
|
||||
if (!ssid || !ssid[0] || !groups || !count || max_groups <= 0) return;
|
||||
if (!sec || !sec[0] || strcmp(sec, "--") == 0) sec = "Open";
|
||||
bool joinable = anode_wifi_chan_joinable(chan);
|
||||
|
||||
int idx = -1;
|
||||
for (int i = 0; i < *count; i++) {
|
||||
if (strcmp(groups[i].ssid, ssid) == 0) {
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (idx < 0) {
|
||||
if (*count >= max_groups) return;
|
||||
anode_wifi_group_t *g = &groups[(*count)++];
|
||||
memset(g, 0, sizeof(*g));
|
||||
snprintf(g->ssid, sizeof(g->ssid), "%s", ssid);
|
||||
snprintf(g->best_bssid, sizeof(g->best_bssid), "%s", bssid ? 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 {
|
||||
anode_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 ? bssid : "");
|
||||
g->best_signal = sig;
|
||||
g->best_chan = chan;
|
||||
g->best_joinable = joinable;
|
||||
snprintf(g->security, sizeof(g->security), "%s", sec);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static inline int anode_parse_wifi_scan(const char *nmcli_out,
|
||||
anode_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 (anode_split_nmcli_terse(ln, fields, 6) < 6) continue;
|
||||
anode_wifi_upsert_ap(groups, &n, max_groups,
|
||||
fields[2], fields[1], atoi(fields[3]), atoi(fields[4]),
|
||||
fields[5], fields[0][0] == '*');
|
||||
}
|
||||
if (n > 1) qsort(groups, (size_t)n, sizeof(groups[0]), anode_wifi_group_cmp);
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline int anode_parse_wpa_scan(const char *wpa_out, const char *active_ssid,
|
||||
anode_wifi_group_t *groups, int max_groups) {
|
||||
if (!wpa_out || !groups || max_groups <= 0) return 0;
|
||||
int n = 0;
|
||||
const char *cur = wpa_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] || strncmp(ln, "bssid", 5) == 0) continue;
|
||||
|
||||
char *bssid = ln;
|
||||
char *t1 = strchr(bssid, '\t');
|
||||
if (!t1) continue;
|
||||
*t1++ = '\0';
|
||||
char *t2 = strchr(t1, '\t');
|
||||
if (!t2) continue;
|
||||
*t2++ = '\0';
|
||||
char *t3 = strchr(t2, '\t');
|
||||
if (!t3) continue;
|
||||
*t3++ = '\0';
|
||||
char *t4 = strchr(t3, '\t');
|
||||
if (!t4) continue;
|
||||
*t4++ = '\0';
|
||||
|
||||
int chan = anode_wifi_freq_to_chan(atoi(t1));
|
||||
int dbm = atoi(t2);
|
||||
int pct = 2 * (dbm + 100);
|
||||
if (pct < 5) pct = 5;
|
||||
if (pct > 100) pct = 100;
|
||||
const char *sec = strstr(t3, "WPA") ? "WPA2" : (strstr(t3, "WEP") ? "WEP" : "Open");
|
||||
bool act = (active_ssid && active_ssid[0] && strcmp(t4, active_ssid) == 0);
|
||||
anode_wifi_upsert_ap(groups, &n, max_groups, t4, bssid, chan, pct, sec, act);
|
||||
}
|
||||
if (n > 1) qsort(groups, (size_t)n, sizeof(groups[0]), anode_wifi_group_cmp);
|
||||
return n;
|
||||
}
|
||||
|
||||
static inline bool anode_parse_iw_link(const char *iw_out,
|
||||
char *bssid, size_t bssid_sz,
|
||||
char *ssid, size_t ssid_sz,
|
||||
int *chan_out, int *sig_pct_out) {
|
||||
if (bssid && bssid_sz > 0) bssid[0] = '\0';
|
||||
if (ssid && ssid_sz > 0) ssid[0] = '\0';
|
||||
if (chan_out) *chan_out = 0;
|
||||
if (sig_pct_out) *sig_pct_out = 0;
|
||||
if (!iw_out || !iw_out[0]) return false;
|
||||
|
||||
bool connected = false;
|
||||
const char *cur = iw_out;
|
||||
while (*cur) {
|
||||
const char *nl = strchr(cur, '\n');
|
||||
size_t raw_len = nl ? (size_t)(nl - cur) : strlen(cur);
|
||||
char ln[256];
|
||||
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';
|
||||
|
||||
const char *p = ln;
|
||||
while (*p == ' ' || *p == '\t') p++;
|
||||
if (strncmp(p, "Not connected", 13) == 0) return false;
|
||||
|
||||
if (strncmp(p, "Connected to ", 13) == 0) {
|
||||
connected = true;
|
||||
p += 13;
|
||||
if (bssid && bssid_sz > 0) {
|
||||
size_t i = 0;
|
||||
while (p[i] && p[i] != ' ' && p[i] != '\t' && i + 1 < bssid_sz) {
|
||||
bssid[i] = p[i];
|
||||
i++;
|
||||
}
|
||||
bssid[i] = '\0';
|
||||
}
|
||||
} else if (strncmp(p, "SSID: ", 6) == 0) {
|
||||
if (ssid && ssid_sz > 0) snprintf(ssid, ssid_sz, "%s", p + 6);
|
||||
} else if (strncmp(p, "freq: ", 6) == 0) {
|
||||
int ch = anode_wifi_freq_to_chan(atoi(p + 6));
|
||||
if (chan_out && ch > 0) *chan_out = ch;
|
||||
} else if (strncmp(p, "signal: ", 8) == 0) {
|
||||
int dbm = atoi(p + 8);
|
||||
int pct = 2 * (dbm + 100);
|
||||
if (pct < 5) pct = 5;
|
||||
if (pct > 100) pct = 100;
|
||||
if (sig_pct_out) *sig_pct_out = pct;
|
||||
}
|
||||
}
|
||||
return connected;
|
||||
}
|
||||
|
||||
static inline int anode_merge_active_wifi(anode_wifi_group_t *groups,
|
||||
int count,
|
||||
int max_groups,
|
||||
const char *active_ssid,
|
||||
const char *active_bssid,
|
||||
int active_chan,
|
||||
int active_signal,
|
||||
const char *active_security) {
|
||||
if (!groups || max_groups <= 0) return 0;
|
||||
if (count < 0) count = 0;
|
||||
if (count > max_groups) count = max_groups;
|
||||
if (!active_ssid || !active_ssid[0]) return count;
|
||||
|
||||
int idx = -1;
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (strcmp(groups[i].ssid, active_ssid) == 0) {
|
||||
groups[i].in_use = true;
|
||||
idx = i;
|
||||
} else {
|
||||
groups[i].in_use = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (idx >= 0) {
|
||||
anode_wifi_group_t *g = &groups[idx];
|
||||
if (g->best_signal <= 0 && active_signal > 0) g->best_signal = active_signal;
|
||||
if (g->best_chan <= 0 && active_chan > 0) {
|
||||
g->best_chan = active_chan;
|
||||
g->best_joinable = anode_wifi_chan_joinable(active_chan);
|
||||
}
|
||||
if (!g->best_bssid[0] && active_bssid && active_bssid[0]) {
|
||||
snprintf(g->best_bssid, sizeof(g->best_bssid), "%s", active_bssid);
|
||||
}
|
||||
if ((!g->security[0] || strcmp(g->security, "--") == 0) &&
|
||||
active_security && active_security[0]) {
|
||||
snprintf(g->security, sizeof(g->security), "%s", active_security);
|
||||
}
|
||||
} else {
|
||||
int pos = (count < max_groups) ? count++ : max_groups - 1;
|
||||
anode_wifi_group_t *g = &groups[pos];
|
||||
memset(g, 0, sizeof(*g));
|
||||
snprintf(g->ssid, sizeof(g->ssid), "%s", active_ssid);
|
||||
if (active_bssid && active_bssid[0]) {
|
||||
snprintf(g->best_bssid, sizeof(g->best_bssid), "%s", active_bssid);
|
||||
}
|
||||
snprintf(g->security, sizeof(g->security), "%s",
|
||||
(active_security && active_security[0]) ? active_security : "Connected");
|
||||
g->best_signal = (active_signal > 0) ? active_signal : 0;
|
||||
g->best_chan = active_chan;
|
||||
g->best_joinable = anode_wifi_chan_joinable(active_chan);
|
||||
g->ap_count = 1;
|
||||
g->in_use = true;
|
||||
}
|
||||
|
||||
if (count > 1) qsort(groups, (size_t)count, sizeof(groups[0]), anode_wifi_group_cmp);
|
||||
return count;
|
||||
}
|
||||
|
||||
#endif /* ANODE_WIFI_LIST_H */
|
||||
Reference in new issue
Block a user