From ad3d752b126333a36dd13d3689ccf7948c33c7cb Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 29 Apr 2026 12:50:01 +0700 Subject: [PATCH] iwd: extract shared state/security label helpers Both popup.c and gadget.c carried near-identical _state_label/_sec_label helpers, with the gadget version using bare ints instead of the Iwd_Security enum. Move to iwd/iwd_labels.{c,h} and use the enum consistently. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/e_mod_gadget.c | 29 +++-------------------------- src/e_mod_popup.c | 36 +++++------------------------------- src/iwd/iwd_labels.c | 28 ++++++++++++++++++++++++++++ src/iwd/iwd_labels.h | 12 ++++++++++++ src/meson.build | 1 + 5 files changed, 49 insertions(+), 57 deletions(-) create mode 100644 src/iwd/iwd_labels.c create mode 100644 src/iwd/iwd_labels.h diff --git a/src/e_mod_gadget.c b/src/e_mod_gadget.c index bc7a59a..1256e2f 100644 --- a/src/e_mod_gadget.c +++ b/src/e_mod_gadget.c @@ -5,6 +5,7 @@ #include "iwd/iwd_manager.h" #include "iwd/iwd_device.h" #include "iwd/iwd_network.h" +#include "iwd/iwd_labels.h" #include #include @@ -75,30 +76,6 @@ _icon_name_for_state(Iwd_State s) return "network-wireless"; } -static const char * -_state_label(Iwd_State s) -{ - switch (s) - { - case IWD_STATE_OFF: return "Wi-Fi disabled"; - case IWD_STATE_IDLE: return "Disconnected"; - case IWD_STATE_SCANNING: return "Scanning"; - case IWD_STATE_CONNECTING: return "Connecting"; - case IWD_STATE_CONNECTED: return "Connected"; - case IWD_STATE_ERROR: return "Error"; - } - return ""; -} - -static const char * -_sec_label(int s) -{ - /* Iwd_Security values, kept in sync with iwd_network.h. */ - switch (s) { case 0: return "open"; case 1: return "WPA"; - case 2: return "802.1X"; case 3: return "WEP"; } - return "?"; -} - static void _build_tooltip(Instance *inst, Iwd_State s) { @@ -109,13 +86,13 @@ _build_tooltip(Instance *inst, Iwd_State s) if (n) snprintf(buf, sizeof(buf), "Wi-Fi: %s — %s — signal %d/4", n->ssid ? n->ssid : "?", - _sec_label(n->security), + iwd_security_label(n->security), iwd_network_signal_tier(n)); else snprintf(buf, sizeof(buf), "Wi-Fi: connected"); } else - snprintf(buf, sizeof(buf), "Wi-Fi: %s", _state_label(s)); + snprintf(buf, sizeof(buf), "Wi-Fi: %s", iwd_state_label(s)); elm_object_tooltip_text_set(inst->o_base, buf); } diff --git a/src/e_mod_popup.c b/src/e_mod_popup.c index 5417b47..d1cd9b2 100644 --- a/src/e_mod_popup.c +++ b/src/e_mod_popup.c @@ -4,6 +4,7 @@ #include "iwd/iwd_device.h" #include "iwd/iwd_network.h" #include "iwd/iwd_agent.h" +#include "iwd/iwd_labels.h" #include "ui/wifi_auth.h" #include "ui/wifi_hidden.h" #include @@ -66,33 +67,6 @@ _hidden_pending_timeout(void *data EINA_UNUSED) /* ----- helpers --------------------------------------------------------- */ -static const char * -_state_label(Iwd_State s) -{ - switch (s) { - case IWD_STATE_OFF: return "Wi-Fi disabled"; - case IWD_STATE_IDLE: return "Disconnected"; - case IWD_STATE_SCANNING: return "Scanning…"; - case IWD_STATE_CONNECTING: return "Connecting…"; - case IWD_STATE_CONNECTED: return "Connected"; - case IWD_STATE_ERROR: return "Error"; - } - return ""; -} - -static const char * -_sec_label(Iwd_Security s) -{ - switch (s) { - case IWD_SEC_OPEN: return "open"; - case IWD_SEC_PSK: return "WPA"; - case IWD_SEC_8021X: return "802.1X"; - case IWD_SEC_WEP: return "WEP"; - case IWD_SEC_UNKNOWN: return "?"; - } - return ""; -} - static int _net_cmp(const void *a, const void *b) { @@ -255,7 +229,7 @@ _rebuild_list(Popup *p) _signal_bars(iwd_network_signal_tier(n)), n->known_path ? "★ " : " ", raw_ssid, - _sec_label(n->security), + iwd_security_label(n->security), n->connected ? " ✔" : ""); elm_object_text_set(btn, label); evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, 0); @@ -291,11 +265,11 @@ _refresh(Popup *p) if (err) { char buf[320]; - snprintf(buf, sizeof(buf), "%s — %s", _state_label(s), err); + snprintf(buf, sizeof(buf), "%s — %s", iwd_state_label(s), err); elm_object_text_set(p->status_lbl, buf); } else - elm_object_text_set(p->status_lbl, _state_label(s)); + elm_object_text_set(p->status_lbl, iwd_state_label(s)); } if (p->btn_toggle) elm_object_text_set(p->btn_toggle, s == IWD_STATE_OFF ? "Enable" : "Disable"); @@ -444,7 +418,7 @@ _on_passphrase_request(void *data EINA_UNUSED, Iwd_Agent_Request *req, const cha _pending_req = req; const char *ssid = req_ssid ? req_ssid : "network"; - const char *sec = n ? _sec_label(n->security) : NULL; + const char *sec = n ? iwd_security_label(n->security) : NULL; _pending_dialog = wifi_auth_prompt(_popup ? _popup->box : e_comp->elm, ssid, sec, _on_auth_done, NULL); } diff --git a/src/iwd/iwd_labels.c b/src/iwd/iwd_labels.c new file mode 100644 index 0000000..c3cfcf7 --- /dev/null +++ b/src/iwd/iwd_labels.c @@ -0,0 +1,28 @@ +#include "iwd_labels.h" + +const char * +iwd_state_label(Iwd_State s) +{ + switch (s) { + case IWD_STATE_OFF: return "Wi-Fi disabled"; + case IWD_STATE_IDLE: return "Disconnected"; + case IWD_STATE_SCANNING: return "Scanning…"; + case IWD_STATE_CONNECTING: return "Connecting…"; + case IWD_STATE_CONNECTED: return "Connected"; + case IWD_STATE_ERROR: return "Error"; + } + return ""; +} + +const char * +iwd_security_label(Iwd_Security s) +{ + switch (s) { + case IWD_SEC_OPEN: return "open"; + case IWD_SEC_PSK: return "WPA"; + case IWD_SEC_8021X: return "802.1X"; + case IWD_SEC_WEP: return "WEP"; + case IWD_SEC_UNKNOWN: return "?"; + } + return ""; +} diff --git a/src/iwd/iwd_labels.h b/src/iwd/iwd_labels.h new file mode 100644 index 0000000..9a4dee4 --- /dev/null +++ b/src/iwd/iwd_labels.h @@ -0,0 +1,12 @@ +#ifndef IWD_LABELS_H +#define IWD_LABELS_H + +#include "iwd_manager.h" +#include "iwd_network.h" + +/* Short, user-facing labels for state and security enums. The pointers + * returned are static literals — do not free. */ +const char *iwd_state_label (Iwd_State s); +const char *iwd_security_label(Iwd_Security s); + +#endif diff --git a/src/meson.build b/src/meson.build index a7a4324..e0f31bb 100644 --- a/src/meson.build +++ b/src/meson.build @@ -10,6 +10,7 @@ e_iwd_sources = [ 'iwd/iwd_manager.c', 'iwd/iwd_device.c', 'iwd/iwd_network.c', + 'iwd/iwd_labels.c', 'ui/wifi_list.c', 'ui/wifi_auth.c', 'ui/wifi_hidden.c',