📄 interface.c
字号:
/* This file is part of AirFart. AirFart is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. AirFart is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with AirFart; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <pthread.h>#include <semaphore.h>#include <sys/time.h>#include <gdk/gdkkeysyms.h>#include <gtk/gtk.h>#include "interface.h"#include "about.h"#include "prefs.h"#define LIST_VIEW_ICON_HEIGHT 15#define LIST_VIEW_ICON_MAX_WIDTH 100sem_t semaphore;GtkWidget *treeview;int active_secs;/* menu items for the Show Columns menu */GtkWidget *col_eth_addr_menu, *col_ssid_menu, *col_manufacturer_menu, *col_strength_menu, *col_strength_bar_menu, *col_active_menu, *col_packet_count_menu;GtkTreeViewColumn *col_eth_addr, *col_ssid, *col_manufacturer, *col_strength, *col_strength_bar, *col_active, *col_packet_count;GdkPixbuf* get_icon (char* file){ GdkPixbuf *pixbuf = NULL; GdkPixbuf *pixbuf_icon = NULL; int width, height; double factor; pixbuf = gdk_pixbuf_new_from_file (file, NULL); if (pixbuf == NULL) { #ifdef DEBUG_INTERFACE printf("my %s pixfuf is null\n",file); #endif return NULL; } width = gdk_pixbuf_get_width (pixbuf); height = gdk_pixbuf_get_height (pixbuf); factor = (double) LIST_VIEW_ICON_HEIGHT / (double) height; pixbuf_icon = gdk_pixbuf_scale_simple (pixbuf, (int) (width * factor), LIST_VIEW_ICON_HEIGHT, GDK_INTERP_BILINEAR); g_object_unref (pixbuf); return pixbuf_icon;}GdkPixbuf* scale_icon_width (GdkPixbuf* pixbuf, int percentage){ GdkPixbuf* pixbuf_icon; pixbuf_icon = gdk_pixbuf_scale_simple (pixbuf, (percentage * 2), LIST_VIEW_ICON_HEIGHT, GDK_INTERP_BILINEAR); g_object_unref (pixbuf); return pixbuf_icon;}enum{ COLUMN_ETH_ADDR_STRING, COLUMN_SSID_STRING, COLUMN_VENDOR_ID_STRING, COLUMN_STRENGTH, COLUMN_STRENGTH_BAR, COLUMN_PACKET_COUNT, COLUMN_ACTIVE, COLUMN_ACTIVE_LIGHT, COLUMN_TIME_STAMP, NUM_COLUMNS};void set_active_secs(GtkMenuItem *menuitem, gpointer user_data){ active_secs = atoi( (char*)user_data );}int get_active_secs(){ return active_secs;}int deactivate (){ GtkTreeIter iter; long time; glong time_stamp; gboolean active; struct timeval tv; GtkTreeModel* model; GdkPixbuf* light; model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); sem_wait(&semaphore); /* Critical Section */ gettimeofday(&tv,NULL); time = tv.tv_sec; if (gtk_tree_model_get_iter_first (model, &iter)){//there is something in the tree do{ gtk_tree_model_get(model, &iter, COLUMN_TIME_STAMP, &time_stamp, COLUMN_ACTIVE, &active, -1); // Should we deactivate this entry? if (active && (time_stamp + get_active_secs() <= time)) { gtk_tree_model_get(model, &iter, COLUMN_ACTIVE_LIGHT, &light, -1); #ifdef DEBUG_INTERFACE printf("\n\n\n\nTurning off a light\n\n\n\n"); #endif light = get_icon("/usr/share/pixmaps/airfart/offlight.png"); gtk_list_store_set (GTK_LIST_STORE (model), &iter, COLUMN_ACTIVE_LIGHT, light, COLUMN_ACTIVE, FALSE, -1);//update the percentage } }while (gtk_tree_model_iter_next(model, &iter)); }//end if /* End Critical Section */ sem_post(&semaphore);return 1;}static GtkTreeModel *create_model (void){ //creates a new list store GtkListStore *store; store = gtk_list_store_new (NUM_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_BOOLEAN, GDK_TYPE_PIXBUF, G_TYPE_LONG ); return GTK_TREE_MODEL (store);}void update_treeview(unsigned char* eth_addr, char* ssid, char* vendor_id, gint strength, gint packet_count){ #ifdef DEBUG_INTERFACE printf( "update_treeview called -- ssid: [%s] vendor: [%s] strength: %d\n", ssid, vendor_id, strength ); #endif GdkPixbuf* strengthbar; GdkPixbuf* light; /* Used to create a new row in the tree view */ gchar eth_addr_string[255]; gchar ssid_string[255]; long time; GtkTreeIter iter; gchar* eth_addr_gstr; GtkTreeModel *model; struct timeval tv; gettimeofday(&tv,NULL); time = tv.tv_sec; gboolean made_change = FALSE; gboolean active; model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); light = get_icon("/usr/share/pixmaps/airfart/onlight.png"); sem_wait(&semaphore); /* Critical Section */ g_snprintf( eth_addr_string, 255, "%02x:%02x:%02x:%02x:%02x:%02x", eth_addr[0], eth_addr[1], eth_addr[2], eth_addr[3], eth_addr[4], eth_addr[5] ); int ssid_len = strlen( ssid ); g_snprintf( ssid_string, ssid_len+1, "%s", ssid); // Is there something in the tree? if (gtk_tree_model_get_iter_first (model, &iter)) { do { gtk_tree_model_get(model, &iter, COLUMN_ETH_ADDR_STRING, ð_addr_gstr, -1); // Do we already have this ethernet address? if ( strcmp( eth_addr_gstr, eth_addr_string ) == 0 ) { gtk_tree_model_get(model,&iter,COLUMN_STRENGTH_BAR,&strengthbar, COLUMN_ACTIVE, &active, -1); strengthbar = scale_icon_width(strengthbar,strength); gtk_list_store_set (GTK_LIST_STORE (model), &iter, COLUMN_STRENGTH, g_strdup_printf("%d%%", strength), COLUMN_STRENGTH_BAR,strengthbar, COLUMN_PACKET_COUNT, g_strdup_printf("%d", packet_count), COLUMN_ACTIVE, TRUE, COLUMN_TIME_STAMP, time, -1); if ( ! active ) { gtk_list_store_set (GTK_LIST_STORE (model), &iter, COLUMN_ACTIVE_LIGHT, light, -1); } made_change = TRUE; } } while ( ! made_change && gtk_tree_model_iter_next( model, &iter ) ); } if ( ! made_change) { #ifdef DEBUG_INTERFACE printf("I still haven't found one yet\n"); #endif strengthbar = get_icon("/usr/share/pixmaps/airfart/statusbar.png"); strengthbar = scale_icon_width(strengthbar,strength); gtk_list_store_append (GTK_LIST_STORE (model), &iter); gtk_list_store_set (GTK_LIST_STORE (model), &iter, COLUMN_ETH_ADDR_STRING, eth_addr_string, COLUMN_SSID_STRING, ssid_string, COLUMN_VENDOR_ID_STRING, vendor_id, COLUMN_STRENGTH, g_strdup_printf("%d%%", strength), COLUMN_STRENGTH_BAR,strengthbar, COLUMN_PACKET_COUNT, g_strdup_printf("%d", packet_count), COLUMN_ACTIVE_LIGHT, light, COLUMN_ACTIVE, TRUE, COLUMN_TIME_STAMP, time, -1); } gtk_widget_draw(GTK_WIDGET(treeview), NULL); /* End Critical Section */ sem_post(&semaphore);}void toggle_column( GtkMenuItem *menuitem, gpointer user_data ){ GtkTreeViewColumn *column; switch (atoi( (char*)user_data )) { case 1: column = col_eth_addr; break; case 2: column = col_ssid; break; case 3: column = col_manufacturer; break; case 4: column = col_strength; break; case 5: column = col_strength_bar; break; case 6: column = col_packet_count; break; case 7: column = col_active; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -