From f5646a553880de1bad63db8d7df6c1bf346c4400 Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Wed, 7 Sep 2005 04:36:02 +0000 Subject: [PATCH] (Old svn revision: 776) --- src/Makefile.am | 25 ++- src/callbacks.c | 93 ++++++++++ src/callbacks.h | 40 ++++ src/functions.c | 473 +++++++++++++++++++++++++++--------------------- src/functions.h | 40 ++-- src/interface.c | 254 ++++++++++++++++++++++++++ src/interface.h | 58 ++++++ src/main.c | 50 +++-- src/types.h | 28 ++- 9 files changed, 804 insertions(+), 257 deletions(-) create mode 100644 src/callbacks.c create mode 100644 src/callbacks.h create mode 100644 src/interface.c create mode 100644 src/interface.h diff --git a/src/Makefile.am b/src/Makefile.am index 300c380..aaa7dc3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,7 +1,26 @@ +@SET_MAKE@ + +INCLUDES = -I$(top_srcdir)/include + bin_PROGRAMS = xfce4-taskmanager -xfce4_taskmanager_SOURCES = main.c types.h gui.c gui.h functions.c functions.h +xfce4_taskmanager_SOURCES = \ + main.c \ + callbacks.c callbacks.h \ + functions.c functions.h \ + interface.c interface.h \ + types.h -INCLUDES = $(DEPS_CFLAGS) +xfce4_taskmanager_CFLAGS = \ + -DPACKAGE_LOCALE_DIR=\"$(localedir)\" \ + @XFCE4_GUI_CFLAGS@ \ + @GTK_CFLAGS@ + +xfce4_taskmanager_LDADD = \ + @XFCE4_GUI_LIBS@ \ + @GTK_LIBS@ -LDADD = $(DEPS_LIBS) +ACLOCAL_AMFLAGS = -I m4 + +dist-bz2: dist + zcat $(PACKAGE)-$(VERSION).tar.gz | bzip2 --best -c > $(PACKAGE)-$(VERSION).tar.bz2 diff --git a/src/callbacks.c b/src/callbacks.c new file mode 100644 index 0000000..3aa6cee --- /dev/null +++ b/src/callbacks.c @@ -0,0 +1,93 @@ +/* + * xfce4-taskmanager - very simple taskmanger + * + * Copyright (c) 2005 Johannes Zellner, + * + * This program 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. + * + * This program 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "callbacks.h" + +void on_button1_button_press_event(GtkButton *button, GdkEventButton *event) +{ + GdkEventButton *mouseevent = (GdkEventButton *)event; + if(mainmenu == NULL) + mainmenu = create_mainmenu (); + gtk_menu_popup(GTK_MENU(mainmenu), NULL, NULL, NULL, NULL, mouseevent->button, mouseevent->time); +} + +void on_button3_toggled_event(GtkButton *button, GdkEventButton *event) +{ + if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) + { + change_list_store(TRUE); + } + else + { + change_list_store(FALSE); + } +} + +gboolean on_treeview1_button_press_event(GtkButton *button, GdkEventButton *event) +{ + if(event->button == 3) + { + GdkEventButton *mouseevent = (GdkEventButton *)event; + if(taskpopup == NULL) + taskpopup = create_taskpopup (); + gtk_menu_popup(GTK_MENU(taskpopup), NULL, NULL, NULL, NULL, mouseevent->button, mouseevent->time); + } + return FALSE; +} + +void on_info1_activate(GtkMenuItem *menuitem, gpointer user_data) +{ + show_about_dialog(); +} + +void handle_task_menu(GtkWidget *widget, gchar *signal) +{ + if(signal != NULL) + { + gchar s[32]; + + sprintf(s, "Really %s the Task?", signal); + if(strcmp(signal, "STOP") == 0 || strcmp(signal, "CONT") == 0 || xfce_confirm(s, GTK_STOCK_YES, NULL)) + { + gchar *task_id = ""; + GtkTreeModel *model; + GtkTreeIter iter; + + if(gtk_tree_selection_get_selected(selection, &model, &iter)) + { + gtk_tree_model_get(model, &iter, 1, &task_id, -1); + send_signal_to_task(task_id, signal); + refresh_task_list(); + } + } + } +} + +void on_show_tasks_toggled (GtkMenuItem *menuitem, gint uid) +{ + if(uid == own_uid) + show_user_tasks = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)); + else if(uid == 0) + show_root_tasks = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)); + else + show_other_tasks = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(menuitem)); + + change_task_view(); +} diff --git a/src/callbacks.h b/src/callbacks.h new file mode 100644 index 0000000..1966ecd --- /dev/null +++ b/src/callbacks.h @@ -0,0 +1,40 @@ +/* + * xfce4-taskmanager - very simple taskmanger + * + * Copyright (c) 2005 Johannes Zellner, + * + * This program 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. + * + * This program 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __CALLBACKS_H_ +#define __CALLBACKS_H_ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#include "functions.h" +#include "interface.h" + +void on_button1_button_press_event(GtkButton *button, GdkEventButton *event); +void on_button3_toggled_event(GtkButton *button, GdkEventButton *event); +gboolean on_treeview1_button_press_event(GtkButton *button, GdkEventButton *event); +void on_info1_activate (GtkMenuItem *menuitem, gpointer user_data); +void handle_task_menu(GtkWidget *widget, gchar *signal); +void on_show_tasks_toggled (GtkMenuItem *menuitem, gint uid); + +#endif diff --git a/src/functions.c b/src/functions.c index a3032a4..2d31a11 100644 --- a/src/functions.c +++ b/src/functions.c @@ -1,7 +1,7 @@ /* * xfce4-taskmanager - very simple taskmanger * - * Copyright (c) 2004 Johannes Zellner, + * Copyright (c) 2005 Johannes Zellner, * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,233 +20,284 @@ #include "functions.h" -gint task_count = 0; +gboolean refresh_task_list(void) +{ + DIR *dir; -/* function to kill the current task */ + /* load the /proc dir */ + if((dir = opendir(PROC_DIR_1)) == NULL) + { + if((dir = opendir(PROC_DIR_2)) == NULL) + { + if((dir = opendir(PROC_DIR_3)) == NULL) + { + fprintf(stderr, "Warning: couldn't load the /proc directory\n"); + return FALSE; + } + } + } + + struct dirent *dir_entry; + gint i; + + /* markes all tasks to "not checked" */ + for(i = 0; i < tasks; i++) + { + struct task *tmp = &g_array_index(task_array, struct task, i); + tmp->checked = FALSE; + } + + while((dir_entry = readdir(dir)) != NULL) + { + if(atoi(dir_entry->d_name) != 0) + { + FILE *task_file_status; + + gchar task_file_name_status[64] = "/proc/"; + g_strlcat(task_file_name_status,dir_entry->d_name, sizeof task_file_name_status); + g_strlcat(task_file_name_status,"/status", sizeof task_file_name_status); + + gchar buffer_status[256]; + struct task task; + struct passwd *passwdp; + + if((task_file_status = fopen(task_file_name_status,"r")) != NULL) + { + while(fgets(buffer_status, sizeof buffer_status, task_file_status) != NULL) + { + sscanf(buffer_status,"Uid: %i",&task.uid); + sscanf(buffer_status,"Pid: %i",&task.pid); + sscanf(buffer_status,"PPid: %i",&task.ppid); + sscanf(buffer_status,"Name: %s",&task.name); + sscanf(buffer_status,"VmSize: %i",&task.size); + sscanf(buffer_status,"VmRSS: %i",&task.rss); + // fix for freebsd with linux emo + sscanf(buffer_status,"VmRss: %i",&task.rss); + sscanf(buffer_status,"State: %c",&task.state); + } + + passwdp = getpwuid(task.uid); + if(passwdp != NULL && passwdp->pw_name != NULL) + g_strlcpy(task.uname, passwdp->pw_name, sizeof task.uname); + } + + fclose(task_file_status); + + /* check if task is new and marks the task that its checked*/ + gboolean new_task = TRUE; + + if(tasks < 1) + { + task_array = g_array_append_val(task_array, task); + tasks++; + } + + for(i = 0; i < tasks; i++) + { + struct task *data = &g_array_index(task_array, struct task, i); + + if((gint)data->pid == task.pid) + { + if((gint)data->ppid != task.ppid || (gchar)data->state != task.state || (unsigned int)data->size != task.size || (unsigned int)data->rss != task.rss) + { + data->ppid = task.ppid; + data->state = task.state; + data->size = task.size; + data->rss = task.rss; + + refresh_list_item(i); + } + new_task = FALSE; + data->checked = TRUE; + } + } + + if(new_task) + { + g_array_append_val(task_array, task); + tasks++; + if((show_user_tasks && task.uid == own_uid) || (show_root_tasks && task.uid == 0) || (show_other_tasks && task.uid != own_uid && task.uid != 0)) + add_new_list_item(tasks-1); + } + } + } + closedir(dir); + + /* removing all tasks, which are not "checked" */ + for(i = 0; i < tasks; i++) + { + struct task *data = &g_array_index(task_array, struct task, i); + + if(!data->checked) + { + remove_list_item((gint)data->pid); + task_array = g_array_remove_index(task_array, i); + tasks--; + } + } + + return TRUE; +} + +void fill_list_item(gint i, GtkTreeIter *iter) +{ + if(iter != NULL) + { + struct task *task = &g_array_index(task_array, struct task, i); + + gchar *pid = g_strdup_printf("%i", task->pid); + gchar *ppid = g_strdup_printf("%i", task->ppid); + gchar *state = g_strdup_printf("%c", task->state); + gchar *size = g_strdup_printf("%i kb", task->size); + gchar *rss = g_strdup_printf("%i kb", task->rss); + gchar *name = g_strdup_printf("%s", task->name); + gchar *uname = g_strdup_printf("%s", task->uname); + + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 0, name, -1); + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 1, pid, -1); + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 2, ppid, -1); + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 3, state, -1); + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 4, size, -1); + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 5, rss, -1); + gtk_list_store_set(GTK_LIST_STORE(list_store), iter, 6, uname, -1); + + free(pid); + free(ppid); + free(state); + free(size); + free(rss); + free(name); + free(uname); + } +} + +void add_new_list_item(gint i) +{ + GtkTreeIter iter; + + gtk_list_store_append(GTK_LIST_STORE(list_store), &iter); + + fill_list_item(i, &iter); +} + +void refresh_list_item(gint i) +{ + GtkTreeIter iter; + + gboolean valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(list_store), &iter); + + struct task task = g_array_index(task_array, struct task, i); + + while(valid) + { + gchar *str_data = ""; + gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter, 1, &str_data, -1); + + if(task.pid == atoi(str_data)) + { + g_free(str_data); + fill_list_item(i, &iter); + break; + } + + g_free(str_data); + valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(list_store), &iter); + } +} + +void remove_list_item(gint pid) +{ + GtkTreeIter iter; + + gboolean valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(list_store), &iter); + + while(valid) + { + gchar *str_data = ""; + gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter, 1, &str_data, -1); + + if(pid == atoi(str_data)) + { + free(str_data); + gtk_list_store_remove(GTK_LIST_STORE(list_store), &iter); + break; + } + + free(str_data); + valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(list_store), &iter); + } +} + +gint compare_list_item(GtkTreeModel *model, GtkTreeIter *iter1, GtkTreeIter *iter2, gpointer colnum) +{ + gchar *s1 = ""; + gchar *s2 = ""; + gint ret = 0; + + gtk_tree_model_get(model, iter1, colnum, &s1, -1); + gtk_tree_model_get(model, iter2, colnum, &s2, -1); + + if((gint)colnum == 1 || (gint)colnum == 2 || (gint)colnum == 4 || (gint)colnum == 5) + { + gint i1 = 0; + gint i2 = 0; + + if(s1 != NULL) + i1 = atoi(s1); + + if(s2 != NULL) + i2 = atoi(s2); + + if(i1 < i2) + ret = -1; + else + ret = 1; + } + else + { + if(s1 == NULL) + s1 = ""; + if(s2 == NULL) + s2 = ""; + + ret = strcmp(s1, s2); + } + + free(s1); + free(s2); + + return ret; +} + +/* function to send the signal to the current task */ void send_signal_to_task(gchar *task_id, gchar *signal) { if(task_id != "" && signal != NULL) { gchar command[64] = "kill -"; - g_strlcat(command,signal, 64); - g_strlcat(command," ", 64); - g_strlcat(command,task_id, 64); + g_strlcat(command,signal, sizeof command); + g_strlcat(command," ", sizeof command); + g_strlcat(command,task_id, sizeof command); if(system(command) != 0) xfce_err("Couldn't %s the task with ID %s", signal, task_id); } } -void refresh_task_list(gboolean first_time) +/* change the task view (user, root, other) */ +void change_task_view(void) { - /* markes all tasks to "not checked" */ - gint i; + gtk_list_store_clear(GTK_LIST_STORE(list_store)); - for(i = 0; i < task_count; i++) - all_tasks[i].checked = FALSE; + gint i = 0; - /* load the current taskdetails */ - DIR *dir; - struct dirent *dir_entry; - - if((dir = opendir("/proc")) == NULL) - printf("Error: couldn't load the directory\n"); - - while((dir_entry = readdir(dir)) != NULL) + for(i = 0; i < tasks; i++) { - if(atoi(dir_entry->d_name) != 0) - { - FILE *task_file; - gchar task_file_name[256] = "/proc/"; - g_strlcat(task_file_name,dir_entry->d_name, 256); - g_strlcat(task_file_name,"/status", 256); + struct task task = g_array_index(task_array, struct task, i); - gchar buffer[256]; - gint line_count = 0; - struct task task; - struct passwd *passwdp; - - if((task_file = fopen(task_file_name,"r")) != NULL) - { - while(fgets(buffer, 256, task_file) != NULL) - { - if(line_count == 0) - strcpy(task.name,g_strstrip(g_strsplit(buffer, ":", 2)[1])); - else if(line_count == 3) - task.pid = atoi(g_strstrip(g_strsplit(buffer, ":", 2)[1])); - else if(line_count == 5) - task.ppid = atoi(g_strstrip(g_strsplit(buffer, ":", 2)[1])); - else if(line_count == 7) - { - task.uid = atoi(g_strsplit(g_strstrip(g_strsplit(buffer, ":", 2)[1]), "\t", 2)[0]); - passwdp = getpwuid(task.uid); - strcpy(task.uname, passwdp->pw_name); - } - - line_count++; - } - - line_count = 0; - fclose(task_file); - - /* check if task is new and marks the task that its checked*/ - gboolean new_task = TRUE; - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].pid == task.pid) - { - all_tasks[i].checked = TRUE; - new_task = FALSE; - } - } - - if(new_task) - { - task.checked = TRUE; - all_tasks[task_count] = task; - task_count++; - - if(!first_time) - add_tree_item(task); - } - } - } + if((task.uid == own_uid && show_user_tasks) || (task.uid == 0 && show_root_tasks) || (task.uid != own_uid && task.uid != 0 && show_other_tasks)) + add_new_list_item(i); } - closedir(dir); - if(!first_time) - { - /* removing all tasks which are not marked */ - i = 0; - - while(i < task_count) - { - if(!all_tasks[i].checked) - { - remove_tree_item(all_tasks[i]); - remove_task_from_array(i); - } - i++; - } - } -} - -/* removes a task from the taskarray */ -void remove_task_from_array(gint count) -{ - gint i; - - for(i = count; i < task_count; i++) - { - all_tasks[i] = all_tasks[i+1]; - } - task_count--; -} - -/* checks if there is a parent task (ppid) */ -struct task *get_parent_task(struct task task) -{ - gint i; - - for(i = 0; i < task_count; i++) - { - if(task.ppid == all_tasks[i].pid && all_tasks[i].pid != task.pid) - return(&all_tasks[i]); - } - - return(NULL); -} - -void show_user_tasks(void) -{ - gint i; - gint user_id = getuid(); - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].uid == user_id) - add_tree_item(all_tasks[i]); - } -} - -void hide_user_tasks(void) -{ - gint i; - gint user_id = getuid(); - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].uid == user_id) - remove_tree_item(all_tasks[i]); - } -} - -void show_root_tasks(void) -{ - gint i; - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].uid == 0) - add_tree_item(all_tasks[i]); - } -} - -void hide_root_tasks(void) -{ - gint i; - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].uid == 0) - remove_tree_item(all_tasks[i]); - } -} - -void show_other_tasks(void) -{ - gint i; - gint user_id = getuid(); - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].uid != user_id && all_tasks[i].uid != 0) - add_tree_item(all_tasks[i]); - } -} - -void hide_other_tasks(void) -{ - gint i; - gint user_id = getuid(); - - for(i = 0; i < task_count; i++) - { - if(all_tasks[i].uid != user_id && all_tasks[i].uid != 0) - remove_tree_item(all_tasks[i]); - } -} - -void load_config(void) -{ - gchar * homedir; - - homedir = xfce_resource_save_location(XFCE_RESOURCE_CONFIG, "xfce4", FALSE); - XfceRc *config_obj = xfce_rc_simple_open(g_build_filename(homedir, "taskmanager.rc", NULL), TRUE); - g_free(homedir); - - if(config_obj == NULL) - { - config_show_user_tasks = TRUE; - config_show_root_tasks = FALSE; - config_show_other_tasks = FALSE; - printf("Could not open configfile\n"); - } - else - { - config_show_user_tasks = xfce_rc_read_bool_entry(config_obj, "show_user_tasks", TRUE); - config_show_root_tasks = xfce_rc_read_bool_entry(config_obj, "show_root_tasks", FALSE); - config_show_other_tasks = xfce_rc_read_bool_entry(config_obj, "show_other_tasks", FALSE); - } + refresh_task_list(); } diff --git a/src/functions.h b/src/functions.h index 712559d..1ce833b 100644 --- a/src/functions.h +++ b/src/functions.h @@ -1,7 +1,7 @@ /* * xfce4-taskmanager - very simple taskmanger * - * Copyright (c) 2004 Johannes Zellner, + * Copyright (c) 2005 Johannes Zellner, * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,34 +18,30 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#ifndef __FUNCTIONS_H_ +#define __FUNCTIONS_H_ + #include #include #include #include #include -#include -#include -#include #include "types.h" -/* config vars */ -gboolean config_show_user_tasks; -gboolean config_show_root_tasks; -gboolean config_show_other_tasks; +#define PROC_DIR_1 "/compat/linux/proc" +#define PROC_DIR_2 "/emul/linux/proc" +#define PROC_DIR_3 "/proc" -struct task all_tasks[512]; - -struct task task_list; - -void refresh_task_list(gboolean first_time); +gboolean refresh_task_list(void); +void fill_list_item(gint i, GtkTreeIter *iter); +void add_new_list_item(gint i); +gint compare_list_item(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data); +void remove_list_item(gint i); +void refresh_list_item(gint i); void send_signal_to_task(gchar *task_id, gchar *signal); -void remove_task_from_array(gint count); -struct task *get_parent_task(struct task task); -void show_user_tasks(void); -void hide_user_tasks(void); -void show_root_tasks(void); -void hide_root_tasks(void); -void show_other_tasks(void); -void hide_other_tasks(void); -void load_config(void); +void change_task_view(void); + +#endif + + diff --git a/src/interface.c b/src/interface.c new file mode 100644 index 0000000..52224d9 --- /dev/null +++ b/src/interface.c @@ -0,0 +1,254 @@ +/* + * xfce4-taskmanager - very simple taskmanger + * + * Copyright (c) 2005 Johannes Zellner, + * + * This program 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. + * + * This program 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "interface.h" + +#define GLADE_HOOKUP_OBJECT(component,widget,name) \ + g_object_set_data_full (G_OBJECT (component), name, \ + gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref) + +#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \ + g_object_set_data (G_OBJECT (component), name, widget) + +GtkWidget* create_window1 (void) +{ + GtkWidget *window1; + GtkWidget *vbox1; + GtkWidget *bbox1; + GtkWidget *scrolledwindow1; + GtkWidget *button1; + GtkWidget *button2; + GtkWidget *button3; + + window1 = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window1), _("xfce4-taskmanager")); + gtk_window_set_default_size (GTK_WINDOW (window1), 500, 400); + + vbox1 = gtk_vbox_new (FALSE, 10); + gtk_widget_show (vbox1); + gtk_container_add (GTK_CONTAINER (window1), vbox1); + gtk_container_set_border_width (GTK_CONTAINER (vbox1), 10); + + scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL); + gtk_widget_show (scrolledwindow1); + gtk_scrolled_window_set_policy (scrolledwindow1, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); + gtk_box_pack_start (GTK_BOX (vbox1), scrolledwindow1, TRUE, TRUE, 0); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow1), GTK_SHADOW_IN); + + treeview1 = gtk_tree_view_new (); + gtk_widget_show (treeview1); + gtk_container_add (GTK_CONTAINER (scrolledwindow1), treeview1); + + create_list_store(); + + selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview1)); + + gtk_tree_view_set_model(GTK_TREE_VIEW(treeview1), GTK_TREE_MODEL(list_store)); + + gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(list_store), 1, GTK_SORT_ASCENDING); + + bbox1 = gtk_hbutton_box_new(); + gtk_box_pack_start(GTK_BOX(vbox1), bbox1, FALSE, TRUE, 0); + gtk_widget_show (bbox1); + + button2 = gtk_button_new_from_stock ("gtk-preferences"); + gtk_widget_show (button2); + gtk_box_pack_start (GTK_BOX (bbox1), button2, FALSE, FALSE, 0); + + button3 = gtk_toggle_button_new_with_label (_("more details")); + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button3), FULL_VIEW); + gtk_widget_show (button3); + gtk_box_pack_start (GTK_BOX (bbox1), button3, FALSE, FALSE, 0); + + button1 = gtk_button_new_from_stock ("gtk-quit"); + gtk_widget_show (button1); + gtk_box_pack_start (GTK_BOX (bbox1), button1, FALSE, FALSE, 0); + + g_signal_connect ((gpointer) window1, "destroy", G_CALLBACK (gtk_main_quit), NULL); + g_signal_connect_swapped ((gpointer) treeview1, "button-press-event", G_CALLBACK(on_treeview1_button_press_event), NULL); + g_signal_connect ((gpointer) button1, "clicked", G_CALLBACK (gtk_main_quit), NULL); + g_signal_connect ((gpointer) button2, "button_release_event", G_CALLBACK (on_button1_button_press_event), NULL); + g_signal_connect ((gpointer) button3, "toggled", G_CALLBACK (on_button3_toggled_event), NULL); + + return window1; +} + +void change_list_store(gboolean tmp_full_view) +{ + gtk_tree_view_column_set_visible (column3, tmp_full_view); + gtk_tree_view_column_set_visible (column4, tmp_full_view); + gtk_tree_view_column_set_visible (column5, tmp_full_view); + gtk_tree_view_column_set_visible (column6, tmp_full_view); +} + +void create_list_store(void) +{ + GtkCellRenderer *cell_renderer; + + list_store = gtk_list_store_new(7, G_TYPE_STRING, G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING); + + cell_renderer = gtk_cell_renderer_text_new(); + + column1 = gtk_tree_view_column_new_with_attributes(_("Command"), cell_renderer, "text", 0, NULL); + gtk_tree_view_column_set_resizable(column1, TRUE); + gtk_tree_view_column_set_sort_column_id(column1, 0); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 0, compare_list_item, (void *)0, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column1); + + column2 = gtk_tree_view_column_new_with_attributes(_("PID"), cell_renderer, "text", 1, NULL); + gtk_tree_view_column_set_resizable(column2, TRUE); + gtk_tree_view_column_set_sort_column_id(column2, 1); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 1, compare_list_item, (void *)1, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column2); + + column3 = gtk_tree_view_column_new_with_attributes(_("PPID"), cell_renderer, "text", 2, NULL); + gtk_tree_view_column_set_resizable(column3, TRUE); + gtk_tree_view_column_set_sort_column_id(column3, 2); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 2, compare_list_item, (void *)2, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column3); + + column4 = gtk_tree_view_column_new_with_attributes(_("State"), cell_renderer, "text", 3, NULL); + gtk_tree_view_column_set_resizable(column4, TRUE); + gtk_tree_view_column_set_sort_column_id(column4, 3); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 3, compare_list_item, (void *)3, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column4); + + column5 = gtk_tree_view_column_new_with_attributes(_("VM-Size"), cell_renderer, "text", 4, NULL); + gtk_tree_view_column_set_resizable(column5, TRUE); + gtk_tree_view_column_set_sort_column_id(column5, 4); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 4, compare_list_item, (void *)4, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column5); + + column6 = gtk_tree_view_column_new_with_attributes(_("RSS"), cell_renderer, "text", 5, NULL); + gtk_tree_view_column_set_resizable(column6, TRUE); + gtk_tree_view_column_set_sort_column_id(column6, 5); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 5, compare_list_item, (void *)5, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column6); + + column7 = gtk_tree_view_column_new_with_attributes(_("User"), cell_renderer, "text", 6, NULL); + gtk_tree_view_column_set_resizable(column7, TRUE); + gtk_tree_view_column_set_sort_column_id(column7, 6); + gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(list_store), 6, compare_list_item, (void *)6, NULL); + gtk_tree_view_append_column(GTK_TREE_VIEW(treeview1), column7); + + change_list_store(FULL_VIEW); +} + +GtkWidget* create_taskpopup (void) +{ + GtkWidget *taskpopup; + GtkWidget *stop1; + GtkWidget *continue1; + GtkWidget *term1; + GtkWidget *kill1; + + taskpopup = gtk_menu_new (); + + stop1 = gtk_menu_item_new_with_mnemonic (_("Stop")); + gtk_widget_show (stop1); + gtk_container_add (GTK_CONTAINER (taskpopup), stop1); + + continue1 = gtk_menu_item_new_with_mnemonic (_("Continue")); + gtk_widget_show (continue1); + gtk_container_add (GTK_CONTAINER (taskpopup), continue1); + + term1 = gtk_menu_item_new_with_mnemonic (_("Term")); + gtk_widget_show (term1); + gtk_container_add (GTK_CONTAINER (taskpopup), term1); + + kill1 = gtk_menu_item_new_with_mnemonic (_("Kill")); + gtk_widget_show (kill1); + gtk_container_add (GTK_CONTAINER (taskpopup), kill1); + + g_signal_connect ((gpointer) stop1, "activate", G_CALLBACK (handle_task_menu), "STOP"); + g_signal_connect ((gpointer) continue1, "activate", G_CALLBACK (handle_task_menu), "CONT"); + g_signal_connect ((gpointer) term1, "activate", G_CALLBACK (handle_task_menu), "TERM"); + g_signal_connect ((gpointer) kill1, "activate", G_CALLBACK (handle_task_menu), "KILL"); + + return taskpopup; +} + +GtkWidget* create_mainmenu (void) +{ + GtkWidget *mainmenu; + GtkWidget *info1; + GtkWidget *trennlinie1; + GtkWidget *show_user_tasks1; + GtkWidget *show_root_tasks1; + GtkWidget *show_other_tasks1; + GtkAccelGroup *accel_group; + + accel_group = gtk_accel_group_new (); + + mainmenu = gtk_menu_new (); + + info1 = gtk_image_menu_item_new_from_stock ("gtk-about", accel_group); + gtk_widget_show (info1); + gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), info1); + + trennlinie1 = gtk_separator_menu_item_new (); + gtk_widget_show (trennlinie1); + gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), trennlinie1); + gtk_widget_set_sensitive (trennlinie1, FALSE); + + show_user_tasks1 = gtk_check_menu_item_new_with_mnemonic (_("Show user tasks")); + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(show_user_tasks1), show_user_tasks); + gtk_widget_show (show_user_tasks1); + gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), show_user_tasks1); + + show_root_tasks1 = gtk_check_menu_item_new_with_mnemonic (_("Show root tasks")); + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(show_root_tasks1), show_root_tasks); + gtk_widget_show (show_root_tasks1); + gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), show_root_tasks1); + + show_other_tasks1 = gtk_check_menu_item_new_with_mnemonic (_("Show other tasks")); + gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM(show_other_tasks1), show_other_tasks); + gtk_widget_show (show_other_tasks1); + gtk_menu_shell_append(GTK_MENU_SHELL(mainmenu), show_other_tasks1); + + g_signal_connect ((gpointer) info1, "activate", G_CALLBACK (on_info1_activate), NULL); + g_signal_connect ((gpointer) show_user_tasks1, "toggled", G_CALLBACK (on_show_tasks_toggled), (void *)own_uid); + g_signal_connect ((gpointer) show_root_tasks1, "toggled", G_CALLBACK (on_show_tasks_toggled), (void *)0); + g_signal_connect ((gpointer) show_other_tasks1, "toggled", G_CALLBACK (on_show_tasks_toggled), (void *)-1); + + gtk_menu_set_accel_group (GTK_MENU (mainmenu), accel_group); + + return mainmenu; +} + +void show_about_dialog(void) +{ + GtkWidget *about_dialog; + XfceAboutInfo *about_info; + + about_info = xfce_about_info_new("xfce4-taskmanager", VERSION, "Xfce4-Taskmanager is a easy to use Taskmanager.",XFCE_COPYRIGHT_TEXT("2005", "Johannes Zellner"), XFCE_LICENSE_GPL); + xfce_about_info_set_homepage(about_info, "http://developer.berlios.de/projects/xfce-goodies/"); + xfce_about_info_add_credit(about_info, "Johannes Zellner", "webmaster@nebulon.de", "Original Author"); + + about_dialog = xfce_about_dialog_new(GTK_WINDOW(window1), about_info, NULL); + g_signal_connect(G_OBJECT(about_dialog), "response", G_CALLBACK(gtk_widget_destroy), NULL); + gtk_window_set_title (GTK_WINDOW (about_dialog), _("xfce4-taskmanager")); + gtk_widget_show(about_dialog); + + xfce_about_info_free(about_info); +} + + + diff --git a/src/interface.h b/src/interface.h new file mode 100644 index 0000000..7c64ea3 --- /dev/null +++ b/src/interface.h @@ -0,0 +1,58 @@ +/* + * xfce4-taskmanager - very simple taskmanger + * + * Copyright (c) 2005 Johannes Zellner, + * + * This program 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. + * + * This program 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __INTERFACE_H_ +#define __INTERFACE_H_ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "functions.h" +#include "callbacks.h" +#include "types.h" + +GtkTreeSelection *selection; +GtkWidget *treeview1; +GtkWidget *mainmenu; +GtkWidget *taskpopup; + +GtkTreeViewColumn *column1, *column2, *column3, *column4, *column5, *column6, *column7; + +void change_list_store(gboolean tmp_full_view); +void create_list_store(void); + +GtkWidget* create_window1 (void); +GtkWidget* create_taskpopup (void); +GtkWidget* create_mainmenu (void); + +void show_about_dialog(void); + +#endif diff --git a/src/main.c b/src/main.c index cc37765..6635b16 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,7 @@ /* * xfce4-taskmanager - very simple taskmanger * - * Copyright (c) 2004 Johannes Zellner, + * Copyright (c) 2005 Johannes Zellner, * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,37 +18,55 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#ifdef HAVE_CONFIG_H +# include +#endif + #include -#include -#include -#include #include -#include -#include "gui.h" +#include "types.h" -/* config vars */ -#define REFRESH_INTERVAL 1 +#include "interface.h" +#include "functions.h" /* handler for SIGALRM to refresh the list */ void refresh_handler(void) { - refresh_task_list(FALSE); + refresh_task_list(); alarm(REFRESH_INTERVAL); } -/* main */ -int main(int argc, char *argv[]) +int main (int argc, char *argv[]) { - gtk_init(&argc, &argv); +#ifdef ENABLE_NLS + bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR); + bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); + textdomain (GETTEXT_PACKAGE); +#endif + + gtk_set_locale (); + gtk_init (&argc, &argv); + + window1 = create_window1 (); + gtk_widget_show (window1); - load_config(); - create_gui(); + own_uid = getuid(); + show_user_tasks = TRUE; + show_root_tasks = FALSE; + show_other_tasks = FALSE; + + task_array = g_array_new (FALSE, FALSE, sizeof (struct task)); + tasks = 0; - signal(SIGALRM, refresh_handler); + if(!refresh_task_list()) + return 0; + + signal(SIGALRM, (void *)refresh_handler); alarm(REFRESH_INTERVAL); - gtk_main(); + gtk_main (); return 0; } + diff --git a/src/types.h b/src/types.h index 092ff0a..a3c445e 100644 --- a/src/types.h +++ b/src/types.h @@ -1,7 +1,7 @@ /* * xfce4-taskmanager - very simple taskmanger * - * Copyright (c) 2004 Johannes Zellner, + * Copyright (c) 2005 Johannes Zellner, * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,13 +21,31 @@ #ifndef __TYPES_H_ #define __TYPES_H_ +#define REFRESH_INTERVAL 1 +#define FULL_VIEW FALSE + struct task { - gchar pid[256]; - gchar ppid[256]; - gchar uid[256]; - gchar name[256]; + gint pid; + gint ppid; + gint uid; + gchar uname[64]; + gchar name[64]; + gchar state; + gint size; + gint rss; gboolean checked; }; +GtkWidget *window1; +GtkListStore *list_store; + +GArray *task_array; +gint tasks; +gint own_uid; + +gboolean show_user_tasks; +gboolean show_root_tasks; +gboolean show_other_tasks; + #endif