mirror of
https://github.com/lakinduakash/linux-wifi-hotspot.git
synced 2024-11-24 21:20:12 +00:00
commit
f90b7622de
@ -3,14 +3,16 @@ project(wihotspot)
|
||||
|
||||
include(FindPkgConfig)
|
||||
pkg_check_modules(GTK gtk+-3.0 REQUIRED)
|
||||
pkg_check_modules(X11 x11 REQUIRED)
|
||||
include_directories(${GTK_INCLUDE_DIRS})
|
||||
include_directories(${X11_INCLUDE_DIRS})
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set (CMAKE_CXX_STANDARD 11)
|
||||
|
||||
add_executable(wihotspot ui/main.c ui/h_prop.c ui/h_prop.h ui/ui.c ui/ui.h ui/read_config.cpp ui/read_config.h)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ${GTK_LIBRARIES} config)
|
||||
target_link_libraries(${PROJECT_NAME} ${GTK_LIBRARIES} ${X11_LIBRARIES} config)
|
||||
|
||||
include_directories(/usr/include)
|
||||
|
||||
|
@ -196,7 +196,32 @@
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<placeholder/>
|
||||
<object class="GtkProgressBar" id="progress_bar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="pulse_step">0.20000000000000001</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">4</property>
|
||||
</packing>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkLabel" id="label_status">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="halign">start</property>
|
||||
<property name="margin_left">5</property>
|
||||
<property name="margin_top">1</property>
|
||||
<property name="margin_bottom">1</property>
|
||||
<property name="label" translatable="yes">Running status</property>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="expand">False</property>
|
||||
<property name="fill">True</property>
|
||||
<property name="position">5</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
</child>
|
||||
|
@ -18,11 +18,13 @@
|
||||
|
||||
#define MKCONFIG "--mkconfig"
|
||||
#define LOAD_CONFIG "--config"
|
||||
#define STOP "--stop"
|
||||
|
||||
|
||||
char cmd_start[BUFSIZE];
|
||||
char cmd_mkconfig[BUFSIZE];
|
||||
char cmd_config[BUFSIZE];
|
||||
char cmd_kill[BUFSIZE];
|
||||
|
||||
char running_info[BUFSIZE];
|
||||
char interface_list[BUFSIZE];
|
||||
@ -86,6 +88,11 @@ int startShell(const char *cmd) {
|
||||
}
|
||||
|
||||
|
||||
const char* build_kill_create_ap_command(char* pid){
|
||||
snprintf(cmd_kill, BUFSIZE, "%s %s %s %s", SUDO, CREATE_AP,STOP,pid);
|
||||
return cmd_kill;
|
||||
}
|
||||
|
||||
int write_config(char* file){
|
||||
|
||||
config_t cfg;
|
||||
|
@ -21,5 +21,6 @@ static int init_get_running();
|
||||
|
||||
static int init_get_interface_list();
|
||||
char** get_interface_list(int*);
|
||||
const char* build_kill_create_ap_command(char* pid);
|
||||
|
||||
#endif //WIHOTSPOT_H_PROP_H
|
||||
|
178
src/ui/ui.c
178
src/ui/ui.c
@ -6,11 +6,15 @@
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
#include <stdlib.h>
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
#include "h_prop.h"
|
||||
#include "ui.h"
|
||||
#include "read_config.h"
|
||||
|
||||
#define BUFSIZE 1024
|
||||
#define AP_ENABLED "AP-ENABLED"
|
||||
|
||||
GtkBuilder *builder;
|
||||
GObject *window;
|
||||
GtkButton *button_create_hp;
|
||||
@ -21,26 +25,33 @@ GtkEntry *entry_pass;
|
||||
GtkComboBox *combo_wifi;
|
||||
GtkComboBox *combo_internet;
|
||||
|
||||
GtkProgressBar *progress_bar;
|
||||
|
||||
GtkLabel *label_status;
|
||||
|
||||
GError *error = NULL;
|
||||
|
||||
|
||||
const char** iface_list;
|
||||
int iface_list_length;
|
||||
char* running_info[3];
|
||||
guint id;
|
||||
|
||||
void init_interface_list();
|
||||
static int find_str(char *find, const char **array, int length);
|
||||
|
||||
void *threadFunc(void *args) {
|
||||
startShell(args);
|
||||
|
||||
void *stopHp();
|
||||
|
||||
void *stopHp() {
|
||||
if(running_info[0]!=NULL){
|
||||
start_pb_pulse();
|
||||
lock_all_views(TRUE);
|
||||
startShell(build_kill_create_ap_command(running_info[0]));
|
||||
g_thread_new("init_running",init_running_info,NULL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *stopHp() {
|
||||
startShell("sudo create_ap --stop ap0");
|
||||
}
|
||||
|
||||
static void on_create_hp_clicked(GtkWidget *widget,
|
||||
gpointer data) {
|
||||
static void on_create_hp_clicked(GtkWidget *widget, gpointer data) {
|
||||
|
||||
WIData *d = (WIData *) data;
|
||||
|
||||
@ -57,7 +68,7 @@ static void on_create_hp_clicked(GtkWidget *widget,
|
||||
startShell(build_wh_mkconfig_command(wifi, internet, (char *) gtk_entry_get_text(d->ssid),
|
||||
(char *) gtk_entry_get_text(d->pass)));
|
||||
|
||||
g_thread_new("shell_create_hp", threadFunc, (void*)build_wh_from_config());
|
||||
g_thread_new("shell_create_hp", run_create_hp_shell, (void*)build_wh_from_config());
|
||||
|
||||
g_free (wifi);
|
||||
g_free (internet);
|
||||
@ -76,6 +87,7 @@ static void on_stop_hp_clicked(GtkWidget *widget, gpointer data) {
|
||||
|
||||
int initUi(int argc, char *argv[]){
|
||||
|
||||
XInitThreads();
|
||||
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
@ -92,6 +104,9 @@ int initUi(int argc, char *argv[]){
|
||||
g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
|
||||
|
||||
|
||||
button_create_hp = (GtkButton *) gtk_builder_get_object(builder, "button_create_hp");
|
||||
button_stop_hp = (GtkButton *) gtk_builder_get_object(builder, "button_stop_hp");
|
||||
|
||||
entry_ssd = (GtkEntry *) gtk_builder_get_object(builder, "entry_ssid");
|
||||
entry_pass = (GtkEntry *) gtk_builder_get_object(builder, "entry_pass");
|
||||
|
||||
@ -99,6 +114,11 @@ int initUi(int argc, char *argv[]){
|
||||
combo_internet = (GtkComboBox *) gtk_builder_get_object(builder, "combo_internet");
|
||||
|
||||
|
||||
label_status = (GtkLabel *) gtk_builder_get_object(builder, "label_status");
|
||||
|
||||
progress_bar = (GtkProgressBar *) gtk_builder_get_object(builder, "progress_bar");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@ -107,14 +127,15 @@ int initUi(int argc, char *argv[]){
|
||||
.ssid= entry_ssd
|
||||
};
|
||||
|
||||
start_pb_pulse();
|
||||
g_thread_new("init_running",init_running_info,NULL);
|
||||
//init_running_info();
|
||||
|
||||
init_interface_list();
|
||||
|
||||
init_ui_from_config(&wiData);
|
||||
|
||||
button_create_hp = (GtkButton *) gtk_builder_get_object(builder, "button_create_hp");
|
||||
g_signal_connect (button_create_hp, "clicked", G_CALLBACK(on_create_hp_clicked), &wiData);
|
||||
|
||||
button_stop_hp = (GtkButton *) gtk_builder_get_object(builder, "button_stop_hp");
|
||||
g_signal_connect (button_stop_hp, "clicked", G_CALLBACK(on_stop_hp_clicked), NULL);
|
||||
|
||||
|
||||
@ -167,11 +188,104 @@ void init_interface_list(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
void lock_all_views(gboolean set_lock){
|
||||
if(set_lock){
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_ssd,FALSE);
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_pass,FALSE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_create_hp, FALSE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, FALSE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_internet, FALSE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_wifi, FALSE);
|
||||
} else{
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_ssd,TRUE);
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_pass,TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_create_hp, TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_internet, TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_wifi, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void lock_running_views(gboolean set_lock){
|
||||
if(set_lock){
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_ssd,FALSE);
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_pass,FALSE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_create_hp, FALSE);
|
||||
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, TRUE);
|
||||
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_internet, FALSE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_wifi, FALSE);
|
||||
} else{
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_ssd,TRUE);
|
||||
gtk_editable_set_editable( (GtkEditable*)entry_pass,TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_create_hp, TRUE);
|
||||
|
||||
gtk_widget_set_sensitive ((GtkWidget*)button_stop_hp, FALSE);
|
||||
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_internet, TRUE);
|
||||
gtk_widget_set_sensitive ((GtkWidget*)combo_wifi, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
static guint start_pb_pulse(){
|
||||
gtk_widget_set_visible((GtkWidget*)progress_bar,TRUE);
|
||||
return id= g_timeout_add (100, update_progress_in_timeout, progress_bar);
|
||||
}
|
||||
|
||||
static void stop_pb_pulse(){
|
||||
g_source_remove(id);
|
||||
gtk_widget_set_visible((GtkWidget*)progress_bar,FALSE);
|
||||
}
|
||||
|
||||
static gboolean update_progress_in_timeout (gpointer pbar)
|
||||
{
|
||||
gtk_progress_bar_pulse (pbar);
|
||||
return TRUE; /* keep running */
|
||||
}
|
||||
|
||||
|
||||
void clear_running_info(){
|
||||
|
||||
g_free(running_info[0]);
|
||||
running_info[0]=NULL;
|
||||
}
|
||||
|
||||
void* init_running_info(){
|
||||
|
||||
clear_running_info();
|
||||
lock_all_views(TRUE);
|
||||
|
||||
get_running_info(running_info);
|
||||
|
||||
if(running_info[0]!=NULL){
|
||||
|
||||
char a[BUFSIZE];
|
||||
snprintf(a,BUFSIZE,"Running as PID: %s",running_info[0]);
|
||||
gtk_label_set_label(label_status,a);
|
||||
|
||||
lock_all_views(FALSE);
|
||||
lock_running_views(TRUE);
|
||||
|
||||
|
||||
} else{
|
||||
|
||||
gtk_label_set_label(label_status,"Not running");
|
||||
lock_all_views(FALSE);
|
||||
lock_running_views(FALSE);
|
||||
}
|
||||
|
||||
stop_pb_pulse();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int find_str(char *find, const char **array, int length) {
|
||||
int i;
|
||||
|
||||
for ( i = 0; i < length; i++ ) {
|
||||
g_print("%s ", array[i]);
|
||||
if (strcmp(array[i], find) == 0) {
|
||||
return i;
|
||||
}
|
||||
@ -180,4 +294,36 @@ int find_str(char *find, const char **array, int length) {
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static void *run_create_hp_shell(void *cmd) {
|
||||
|
||||
char buf[BUFSIZE];
|
||||
FILE *fp;
|
||||
|
||||
if ((fp = popen(cmd, "r")) == NULL) {
|
||||
printf("Error opening pipe!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
start_pb_pulse();
|
||||
|
||||
while (fgets(buf, BUFSIZE, fp) != NULL) {
|
||||
buf[strcspn(buf, "\n")] = 0;
|
||||
gtk_label_set_label(label_status,buf);
|
||||
|
||||
if (strstr(buf, AP_ENABLED) != NULL) {
|
||||
init_running_info();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (pclose(fp)) {
|
||||
printf("Command not found or exited with error status\n");
|
||||
init_running_info();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
init_running_info();
|
||||
return 0;
|
||||
}
|
||||
|
19
src/ui/ui.h
19
src/ui/ui.h
@ -13,6 +13,25 @@ typedef struct {
|
||||
} WIData;
|
||||
|
||||
int initUi(int argc, char *argv[]);
|
||||
|
||||
void init_ui_from_config(WIData* data);
|
||||
|
||||
static void* run_create_hp_shell(void *cmd);
|
||||
|
||||
void init_interface_list();
|
||||
|
||||
static int find_str(char *find, const char **array, int length);
|
||||
|
||||
void* init_running_info();
|
||||
|
||||
static gboolean update_progress_in_timeout (gpointer pbar);
|
||||
|
||||
void lock_all_views(gboolean set_lock);
|
||||
|
||||
void lock_running_views(gboolean set_lock);
|
||||
|
||||
static guint start_pb_pulse();
|
||||
|
||||
static void on_create_hp_clicked(GtkWidget *widget,gpointer data);
|
||||
|
||||
#endif //WIHOTSPOT_UI_H
|
||||
|
Loading…
Reference in New Issue
Block a user