Add a inserted window to show connected devices

This commit is contained in:
nexround 2021-09-12 15:16:04 +08:00
parent 821e04a81e
commit fbe9fd7e4d
5 changed files with 247 additions and 6 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.2 -->
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkApplicationWindow" id="window">
@ -503,16 +503,133 @@
</packing>
</child>
<child>
<object class="GtkBox">
<object class="GtkExpander">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="can_focus">True</property>
<child>
<placeholder/>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<property name="min_content_height">100</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="column_homogeneous">True</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Number</property>
</object>
<packing>
<property name="left_attach">0</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Hostname</property>
</object>
<packing>
<property name="left_attach">1</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">IP</property>
</object>
<packing>
<property name="left_attach">2</property>
<property name="top_attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">MAC</property>
</object>
<packing>
<property name="left_attach">3</property>
<property name="top_attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkGrid" id="grid_devices">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="row_homogeneous">True</property>
<property name="column_homogeneous">True</property>
<property name="baseline_row">1</property>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="button_refresh">
<property name="label" translatable="yes">Refresh</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="pack_type">end</property>
<property name="position">2</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
</child>
<child type="label">
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Connected devices</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>

View File

@ -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;
}

View File

@ -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

View File

@ -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);

View File

@ -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