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,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 */
|
||||
Reference in new issue
Block a user