Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a inserted window to show connected devices #182

Merged
merged 3 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
* Support both 2.4GHz and 5GHz (Need to be compatible with your wifi adapter). Ex: You have connected to the 5GHz network and share a connection with 2.4GHz.
* Select Channel.
* Hide SSID
* Show the connected devices

![screenshot](docs/sc2.png)
![screenshot](docs/sc3.png)


### Command line help and documentation
Expand Down
Binary file added docs/sc3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
129 changes: 123 additions & 6 deletions src/ui/glade/wifih.ui
Original file line number Diff line number Diff line change
@@ -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">
Expand Down Expand Up @@ -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>
Expand Down
53 changes: 53 additions & 0 deletions src/ui/h_prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions src/ui/h_prop.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *);
Expand All @@ -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
54 changes: 54 additions & 0 deletions src/ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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