From fbe9fd7e4dfeff8c50669d443607e73f71c32bac Mon Sep 17 00:00:00 2001 From: nexround <527159991@qq.com> Date: Sun, 12 Sep 2021 15:16:04 +0800 Subject: [PATCH] Add a inserted window to show connected devices --- src/ui/glade/wifih.ui | 129 ++++++++++++++++++++++++++++++++++++++++-- src/ui/h_prop.c | 53 +++++++++++++++++ src/ui/h_prop.h | 15 +++++ src/ui/ui.c | 54 ++++++++++++++++++ src/ui/ui.h | 2 + 5 files changed, 247 insertions(+), 6 deletions(-) diff --git a/src/ui/glade/wifih.ui b/src/ui/glade/wifih.ui index 3d893bd..0f204dc 100644 --- a/src/ui/glade/wifih.ui +++ b/src/ui/glade/wifih.ui @@ -1,5 +1,5 @@ - + @@ -503,16 +503,133 @@ - + True - False - vertical + True - + + True + True + in + 100 + + + True + False + + + True + False + vertical + + + True + False + True + + + True + False + Number + + + 0 + 0 + + + + + True + False + Hostname + + + 1 + 0 + + + + + True + False + IP + + + 2 + 0 + + + + + True + False + MAC + + + 3 + 0 + + + + + False + True + 0 + + + + + True + False + True + True + 1 + + + + + + + + + + + + False + True + 1 + + + + + Refresh + True + True + True + + + False + True + end + 2 + + + + + + + + + + + True + False + Connected devices + - True + False True 4 diff --git a/src/ui/h_prop.c b/src/ui/h_prop.c index 7f18987..09107f3 100644 --- a/src/ui/h_prop.c +++ b/src/ui/h_prop.c @@ -420,3 +420,56 @@ char** get_wifi_interface_list(int *length){ return NULL; } + +Node get_connected_devices(char *PID) +{ + char cmd[BUFSIZE]; + snprintf(cmd, BUFSIZE, "%s --list-clients %s", CREATE_AP, PID); + FILE *fp; + Node l = (struct Device *)malloc(sizeof(struct Device)); + Position head = l; + fp = popen(cmd, "r"); + char line[BUFSIZE]; + + int _n = 0; //Device number + while (fgets(line, BUFSIZE, fp) != NULL) + { + if (strstr(line, "MAC") != NULL) + continue; + + _n++; + int size = strlen(line); + int marker[3] = {0}; + int n = 0; // For marker + line[size - 1] = '\0'; // Remove "\n" + for (int i = 0; i < size; i++) + { + if (*(line + i) != ' ' && *(line + i + 1) == ' ') + { + // End + *(line + i + 1) = '\0'; + i++; + } + if (*(line + i) == ' ' && *(line + i + 1) != ' ') + { + // Head + *(line + i) = '\0'; + marker[++n] = i + 1; + } + } + l = add_device_node(l, _n, line, marker); + } + return head; +} + +PtrToNode add_device_node(PtrToNode l, int number, char line[BUFSIZE], int marker[3]) +{ + Node next = (PtrToNode)malloc(sizeof(struct Device)); + strcpy(next->MAC, line); + strcpy(next->IP, line + marker[1]); + strcpy(next->HOSTNAME, line + marker[2]); + next->Number = number; + next->Next = NULL; + l->Next = next; + return next; +} \ No newline at end of file diff --git a/src/ui/h_prop.h b/src/ui/h_prop.h index e03c736..f407086 100644 --- a/src/ui/h_prop.h +++ b/src/ui/h_prop.h @@ -35,6 +35,18 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "read_config.h" +typedef struct Device *PtrToNode; +struct Device +{ + char HOSTNAME[2048]; + char IP[2048]; + char MAC[2048]; + unsigned int Number; + PtrToNode Next; +}; // Head node is null +typedef PtrToNode Position; +typedef PtrToNode Node; + static int parse_output(const char *); const char *build_wh_start_command(char *, char *, char *, char *); @@ -59,4 +71,7 @@ void write_accepted_macs(char* filename, char* accepted_macs); char * read_mac_filter_file(char * filename); +Node get_connected_devices(char *PID); +PtrToNode add_device_node(Node l, int number, char *line, int marker[3]); + #endif //WIHOTSPOT_H_PROP_H diff --git a/src/ui/ui.c b/src/ui/ui.c index 4bab3cc..f124720 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -57,6 +57,14 @@ GObject *window; GtkButton *button_create_hp; GtkButton *button_stop_hp; GtkButton *button_about; +GtkButton *button_refresh; + +GtkGrid *grid_devices; +GtkWidget *h; +GtkWidget *i; +GtkWidget *m; +GtkWidget *n; +PtrToNode l; GtkEntry *entry_ssd; GtkEntry *entry_pass; @@ -153,6 +161,48 @@ static void on_about_open_click(GtkWidget *widget, gpointer data){ show_info(widget,data); } +static void set_connected_devices_label() +{ + Position tmp; + l = get_connected_devices(running_info[0]); // running_info[0] PID + + // Remove all the children widgets + GList *children, *iter; + + children = gtk_container_get_children(GTK_CONTAINER(grid_devices)); + for (iter = children; iter != NULL; iter = g_list_next(iter)) + { + gtk_widget_destroy(GTK_WIDGET(iter->data)); + } + g_list_free(children); + + while (l->Next != NULL) + { + tmp = l; // Save the last one + l = l->Next; + char number[1]; + sprintf(number, "%d", l->Number); + n = gtk_label_new(number); + h = gtk_label_new(l->HOSTNAME); + i = gtk_label_new(l->IP); + m = gtk_label_new(l->MAC); + + gtk_grid_attach(grid_devices, n, 0, l->Number, 1, 1); + gtk_grid_attach(grid_devices, h, 1, l->Number, 1, 1); + gtk_grid_attach(grid_devices, i, 2, l->Number, 1, 1); + gtk_grid_attach(grid_devices, m, 3, l->Number, 1, 1); + gtk_widget_show_all((GtkWidget *)grid_devices); + free(tmp); // Free the last pointer + } +} + +static void on_refresh_clicked(GtkWidget *widget, gpointer data) +{ + if (running_info[0] != NULL) + { + set_connected_devices_label(); + } +} static void loadStyles(){ provider = gtk_css_provider_new(); @@ -360,6 +410,9 @@ int initUi(int argc, char *argv[]){ button_create_hp = (GtkButton *) gtk_builder_get_object(builder, "button_create_hp"); button_stop_hp = (GtkButton *) gtk_builder_get_object(builder, "button_stop_hp"); button_about = (GtkButton *) gtk_builder_get_object(builder, "button_about"); + button_refresh = (GtkButton *)gtk_builder_get_object(builder, "button_refresh"); + + grid_devices = (GtkGrid *)gtk_builder_get_object(builder, "grid_devices"); entry_ssd = (GtkEntry *) gtk_builder_get_object(builder, "entry_ssid"); entry_pass = (GtkEntry *) gtk_builder_get_object(builder, "entry_pass"); @@ -403,6 +456,7 @@ int initUi(int argc, char *argv[]){ g_signal_connect (button_create_hp, "clicked", G_CALLBACK(on_create_hp_clicked), NULL); g_signal_connect (button_stop_hp, "clicked", G_CALLBACK(on_stop_hp_clicked), NULL); g_signal_connect (button_about, "clicked", G_CALLBACK(on_about_open_click), NULL); + g_signal_connect (button_refresh, "clicked", G_CALLBACK(on_refresh_clicked), NULL); //new g_signal_connect (entry_mac, "changed", G_CALLBACK(entry_mac_warn), NULL); g_signal_connect (entry_ssd, "changed", G_CALLBACK(entry_ssid_warn), NULL); diff --git a/src/ui/ui.h b/src/ui/ui.h index a264008..7216927 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -70,4 +70,6 @@ static void set_error_text(char * text); gchar* get_accepted_macs(); +static void set_connected_devices_label(); // new + #endif //WIHOTSPOT_UI_H