This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
quick-text-bar/modules/linuxnm.c

97 lines
2.9 KiB
C
Raw Normal View History

2022-08-28 14:27:03 -07:00
#include "linuxnm.h"
#include "../util.h"
#include <NetworkManager.h>
#include <signal.h>
#include <string.h>
#define TYPE_NO_MANAGER 0x1
#define TYPE_WIRED 0x2
#define TYPE_WIRELESS 0x4
#define TYPE_VPN 0x8
typedef int connection_type;
static connection_type active_types;
static int is_connection_vpn(NMConnection *connection) {
if (nm_connection_get_setting_vpn(connection)) {
return TRUE;
}
const char *type = nm_connection_get_connection_type(connection);
return strcmp(type, "wireguard") == 0;
}
static void update_connections(NMClient *client) {
const GPtrArray *active_connections =
nm_client_get_active_connections(client);
guint i;
for (i = 0; i < active_connections->len; ++i) {
NMActiveConnection *current_active =
g_ptr_array_index(active_connections, i);
NMConnection *current =
NM_CONNECTION(nm_active_connection_get_connection(current_active));
const char *type = nm_connection_get_connection_type(current);
if (is_connection_vpn(current)) {
active_types |= TYPE_VPN;
} else if (nm_connection_get_setting_wired(current)) {
active_types |= TYPE_WIRED;
} else if (nm_connection_get_setting_wireless(current)) {
active_types |= TYPE_WIRELESS;
}
}
}
static void connections_changed(NMClient *client, GParamSpec *pspec,
gpointer user_data) {
active_types = 0;
if (!nm_client_get_nm_running(client)) {
active_types = TYPE_NO_MANAGER;
} else {
update_connections(client);
}
qtb_signal_modules(2);
}
static void *thread_action(gpointer user_data) {
GMainContext *context = g_main_context_new();
g_main_context_push_thread_default(context);
GError *err = NULL;
NMClient *client = nm_client_new(NULL, &err);
if (err) {
return err->message;
}
g_signal_connect(client, "notify::" NM_CLIENT_ACTIVE_CONNECTIONS,
G_CALLBACK(connections_changed), NULL);
g_signal_connect(client, "notify::" NM_CLIENT_NM_RUNNING,
G_CALLBACK(connections_changed), NULL);
connections_changed(client, NULL, NULL);
GMainLoop *main_loop = g_main_loop_new(context, FALSE);
g_main_loop_run(main_loop);
return NULL;
}
void linuxnm_module_init() {
g_thread_new("network-module", &thread_action, NULL);
}
char *linuxnm_module_poll() {
char *output = qtb_calloc(8, 1);
if ((active_types & TYPE_NO_MANAGER) == TYPE_NO_MANAGER) {
strcat(output, "");
}
if ((active_types & TYPE_VPN) == TYPE_VPN) {
strcat(output, "");
}
if ((active_types & TYPE_WIRED) == TYPE_WIRED) {
strcat(output, "");
}
if ((active_types & TYPE_WIRELESS) == TYPE_WIRELESS) {
strcat(output, "");
}
if (active_types == 0) { /* no valid connections found */
strcat(output, "");
}
return output;
}