概述
Linux下一般來說提到界面編程
JAVA自然是Eclipse
C++就大概是Qt了吧
那么C呢? 於是我去學了學UnixC和GTK,先放個可用代碼放着吧~
啊對了,配環境,沒錯配環境
sudo apt-get install vim #使用vim來編寫代碼,當然您可以使用任何自己喜歡的編輯器。
sudo apt-get install build-essential #這將安裝gcc/g++/gdb/make 等基本編程工具。
sudo apt-get install gnome-core-devel #這將安裝 libgtk2.0-dev libglib2.0-dev 等開發相關的庫文件。
sudo apt-get install pkg-config #用於在編譯GTK程序時自動找出頭文件及庫文件位置。
sudo apt-get install devhelp #這將安裝 devhelp GTK文檔查看程序。
sudo apt-get install libglib2.0-doc libgtk2.0-doc #這將安裝 gtk/glib 的API參考手冊及其它幫助文檔。
sudo apt-get install glade libglade2-dev #這將安裝基於GTK的界面構造程序。
有的獲取不到要去官網下載下來tar.gz自己解壓縮安裝哦~
解壓小貼士:
tar –xvf file.tar //解壓 tar包
tar -xzvf file.tar.gz //解壓tar.gz
tar -xjvf file.tar.bz2 //解壓 tar.bz2
tar –xZvf file.tar.Z //解壓tar.Z
unrar e file.rar //解壓rar
unzip file.zip //解壓zip
東西都有了之后呢~
看看效果?
效果看完了咱——
就來看看代碼研讀研讀吧~
// UnixC Based on GTK
// Compile Method: #gcc cdr –o cdos.c `pkg-config --cflags --libs gtk+-2.0
// Exec Method: ./cdr
// Author :okcd00 @ CSDN_2014
#include "cdhd.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DENSITY 100 //No. Density of Graph
#define PMAX 100 //No. maxNum of Process
enum
{
NAME_COLUMN,
PID_COLUMN,
STATUS_COLUMN,
CPU_COLUMN,
MEMORY_COLUMN,
NP_COLUMNS
};
enum
{
MOD_COLUMN,
DEPEND_COLUMN,
NM_COLUMNS
};
//Declare of GtkWidget
GtkWidget *vbox;
GtkWidget *hbox;
GtkWidget *label;
GtkWidget *frame;
GtkWidget *window;
GtkWidget *menubar;
GtkWidget *notebook;
GtkWidget *main_vbox;
GtkWidget *mem_label;
GtkWidget *swap_label;
GtkWidget *info_label;
GtkWidget *status_bar;
GtkWidget *ptree_view;
GtkWidget *mtree_view;
GtkWidget *cpu_draw_area;
GtkWidget *mem_draw_area;
GtkWidget *scrolled_window;
GtkWidget *prefresh_button , *pdelete_button; //Refresh & Kill Button
// Global Varities
gint cpu_graph[DENSITY];//CPU Graphic Density
gint mem_graph[DENSITY];//RAM Graphic Density
gdouble rate = 0;//cpu Use_Rate
gdouble total = 0; //Curr. No.Process
gdouble fuser = 0;//Curr. CPUinfo
gdouble ftotal = 0;//Curr. CPUtotal
gdouble pfuser[PMAX]; //No. MaxProcess
GString *info;
GdkPixmap *cgraph = NULL;//pixmap_Pointer_CPU
GdkPixmap *mgraph = NULL;//pixmap_Pointer_RAM
//Others
GtkAccelGroup *accel_group;
GtkListStore *process_store;
GtkItemFactory *item_factory;
GtkCellRenderer *renderer;//Show each colTitle
GtkTreeViewColumn *column;//tree view's No.Column
//函數聲明
void about (GtkWidget *, gpointer);
void destroy_window (GtkWidget *, gpointer);
void sys_shutdown(GtkWidget *, gpointer);
void sys_reboot (GtkWidget *, gpointer);
void sys_halt (GtkWidget *, gpointer);
void about_author (GtkWidget *, gpointer);
gboolean handle_timeout (gpointer data);
gboolean load_graph_refresh (GtkWidget *widget);
gboolean get_cpu_rate (gpointer data);
void create_sys_status_page (void);
void create_process_page (void);
void create_sys_info_page (void);
void get_status_info (void);
void get_process_info (GtkListStore *);
void get_cpu_info (GString *);
void get_os_info (GString *);
void draw_cpu_load_graph (void);
void draw_mem_load_graph (void);
void prefresh_button_clicked (gpointer data);
void pdelete_button_clicked (gpointer data);
void mrefresh_button_clicked (gpointer data);
void mdelete_button_clicked (gpointer data);
gboolean cpu_configure_event (GtkWidget *, GdkEventConfigure *, gpointer);
gboolean cpu_expose_event (GtkWidget *, GdkEventExpose *, gpointer);
gboolean mem_configure_event (GtkWidget *, GdkEventConfigure *, gpointer);
gboolean mem_expose_event (GtkWidget *, GdkEventExpose *, gpointer);
gchar ABt[]="About us";
gchar ABc[]=" __<20125209>Chendian_&_<20125216>Xutianyuan_&_<20125212>Yangyeyu__ ";
void show_dialog (gchar *, gchar *);
static GtkItemFactoryEntry menu_items[] = {
{"/_File", NULL, NULL, 0, ""},
{"/File/Quit", "Q", destroy_window, 0, ""},
{"/_Option", NULL, NULL, 0, ""},
{"/Option/Shutdown", "U", sys_shutdown, 0 ,""},
{"/Option/Reboot", "R", sys_reboot, 0 ,""},
{"/Option/Halt", "H", sys_halt, 0 ,""},
{"/_About",NULL,NULL,0,""},
{"/About/AboutUs","B", about, 0, ""},
};
static gint nmenu_items = sizeof (menu_items) / sizeof (menu_items[0]);//No.Menu
int main (int argc, char **argv)
{
gtk_set_locale ();//Set LocalEnvir Vars
gtk_init (&argc, &argv);
memset (cpu_graph, 50, sizeof (cpu_graph));//Alloc 50size
memset (mem_graph, 50, sizeof (mem_graph));//Alloc 50size
memset (pfuser, 0 ,sizeof (pfuser));
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "okcd00's Linux System Moniter");//Title
gtk_window_set_default_size (GTK_WINDOW (window), 400, 300);//Default Window_Size
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);//Lock the Window_Size
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(destroy_window), NULL);
main_vbox =gtk_vbox_new (FALSE, 0);
gtk_widget_show (main_vbox);
gtk_container_add (GTK_CONTAINER (window), main_vbox);
accel_group = gtk_accel_group_new ();
item_factory = gtk_item_factory_new (GTK_TYPE_MENU_BAR,"", accel_group);//Create Menu
gtk_item_factory_create_items (item_factory, nmenu_items,menu_items, NULL);
gtk_window_add_accel_group (GTK_WINDOW (window), accel_group);
menubar = gtk_item_factory_get_widget (item_factory, "");
gtk_widget_show (menubar);
gtk_box_pack_start (GTK_BOX (main_vbox), menubar, FALSE, FALSE, 0);
notebook = gtk_notebook_new ();//New a notebook
gtk_widget_show (notebook);
gtk_box_pack_start (GTK_BOX (main_vbox), notebook, FALSE, FALSE, 0);
status_bar = gtk_statusbar_new ();//New a status_bar
gtk_widget_show (status_bar);
gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (status_bar),TRUE); //Can be Resized
gtk_box_pack_start (GTK_BOX (main_vbox), status_bar, TRUE, FALSE, 0);
gtk_timeout_add (2000, (GtkFunction)handle_timeout, NULL);
gtk_timeout_add (1000, (GtkFunction)load_graph_refresh, NULL); //graph refresh per second
gtk_timeout_add (1000, (GtkFunction)get_cpu_rate, NULL); //CPUrate refresh per second
create_process_page ();
create_sys_status_page ();
create_sys_info_page ();
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void about(GtkWidget *widget, gpointer data)
{
show_dialog(ABt,ABc);
}
void destroy_window (GtkWidget *widget, gpointer data)//QUIT
{
gtk_main_quit ();
}
void sys_shutdown (GtkWidget *widget, gpointer data)//Shotdown
{
system ("shutdown -r now");
}
void sys_reboot (GtkWidget *widget, gpointer data)//Restart the PC
{
system ("reboot");
}
void sys_halt (GtkWidget *widget, gpointer data)//Halt
{
system ("halt");
}
gboolean handle_timeout (gpointer data)//a series of operations by time
{
gint page_num;
page_num = gtk_notebook_get_current_page (GTK_NOTEBOOK (notebook));//Get Curr Page
switch (page_num) {
case 0:
gtk_list_store_clear (process_store);//Clear treeview
get_process_info (process_store);//Re-read Process_info
break;
default:
break;
}
return TRUE;
}
gboolean load_graph_refresh (GtkWidget *widget)
{
draw_cpu_load_graph ();
draw_mem_load_graph ();
return TRUE;
}
gboolean get_cpu_rate ( gpointer data)//Get CPU UseRate
{
int fd,i;
gchar buffer[256];
gchar *cpu_time[9];
gchar *tmp;
gchar *delim = " ";
gdouble cuser;
gdouble ctotal;
fd = open ("/proc/stat", O_RDONLY);//ReadFile
read (fd, buffer, sizeof (buffer));
close (fd);
tmp = strstr (buffer, "cpu0");//Buffer ended with "cpu0"
tmp--;
*tmp = '