*** empty log message ***
(Old svn revision: 527)
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
bin_PROGRAMS = xfce4-taskmanager
|
||||
|
||||
xfce4_taskmanager_SOURCES = xfce4-taskmanager.c
|
||||
xfce4_taskmanager_SOURCES = main.c types.h gui.c gui.h functions.c functions.h
|
||||
|
||||
INCLUDES = $(DEPS_CFLAGS)
|
||||
|
||||
|
||||
138
src/functions.c
Normal file
138
src/functions.c
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 "functions.h"
|
||||
|
||||
task_count = 0;
|
||||
|
||||
/* function to kill 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);
|
||||
|
||||
if(system(command) != 0)
|
||||
xfce_err("Couldn't %s the task with ID %s", signal, task_id);
|
||||
}
|
||||
}
|
||||
|
||||
void refresh_task_list()
|
||||
{
|
||||
/* markes all tasks to "not checked" */
|
||||
gint i;
|
||||
|
||||
for(i = 0; i < task_count; i++)
|
||||
all_tasks[i].checked = FALSE;
|
||||
|
||||
/* 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)
|
||||
{
|
||||
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);
|
||||
|
||||
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)
|
||||
strcpy(task.pid,g_strstrip(g_strsplit(buffer, ":", 2)[1]));
|
||||
else if(line_count == 5)
|
||||
strcpy(task.ppid,g_strstrip(g_strsplit(buffer, ":", 2)[1]));
|
||||
else if(line_count == 7)
|
||||
{
|
||||
passwdp = getpwuid(atoi(g_strsplit(g_strstrip(g_strsplit(buffer, ":", 2)[1]), "\t", 2)[0]));
|
||||
strcpy(task.uid, 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(strcmp(all_tasks[i].pid,task.pid) == 0)
|
||||
{
|
||||
all_tasks[i].checked = TRUE;
|
||||
new_task = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
if(new_task)
|
||||
{
|
||||
task.checked = TRUE;
|
||||
all_tasks[task_count] = task;
|
||||
task_count++;
|
||||
add_new_list_item(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
|
||||
/* removing all tasks which are not marked */
|
||||
i = 0;
|
||||
|
||||
while(i < task_count)
|
||||
{
|
||||
if(!all_tasks[i].checked)
|
||||
{
|
||||
remove_list_item(all_tasks[i]);
|
||||
remove_task_from_array(i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
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--;
|
||||
}
|
||||
34
src/functions.h
Normal file
34
src/functions.h
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 <gtk/gtk.h>
|
||||
#include <dirent.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
struct task all_tasks[512];
|
||||
gint task_count;
|
||||
|
||||
struct task task_list;
|
||||
|
||||
void refresh_task_list(void);
|
||||
void send_signal_to_task(gchar *task_id, gchar *signal);
|
||||
260
src/gui.c
Normal file
260
src/gui.c
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 "gui.h"
|
||||
|
||||
/* create the GUI */
|
||||
void create_gui(void)
|
||||
{
|
||||
GtkWidget *window, *eventbox, *box, *button, *scrolled_window, *tree_view, *main_popup_menu, *task_popup_menu;
|
||||
GtkCellRenderer *cell_renderer;
|
||||
GtkTreeViewColumn *column;
|
||||
|
||||
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_title(GTK_WINDOW(window), "xfce4-taskmanager");
|
||||
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
|
||||
gtk_widget_set_size_request(GTK_WIDGET(window), 450, 400);
|
||||
|
||||
/* popupmenus */
|
||||
task_popup_menu = create_task_popup_menu();
|
||||
main_popup_menu = create_main_popup_menu();
|
||||
|
||||
/* eventbox */
|
||||
eventbox = gtk_event_box_new();
|
||||
gtk_event_box_set_visible_window(GTK_EVENT_BOX(eventbox), FALSE);
|
||||
gtk_widget_add_events(GTK_WIDGET(eventbox), "BUTTON_PRESS");
|
||||
g_signal_connect_swapped(G_OBJECT(eventbox), "button-press-event", G_CALLBACK(handle_mouse_events), G_OBJECT(main_popup_menu));
|
||||
gtk_container_set_border_width(GTK_CONTAINER(eventbox), 0);
|
||||
|
||||
gtk_container_add(GTK_CONTAINER(window), eventbox);
|
||||
gtk_widget_show(eventbox);
|
||||
|
||||
box = gtk_vbox_new(FALSE, 10);
|
||||
gtk_container_set_border_width(GTK_CONTAINER(box), 10);
|
||||
gtk_container_add(GTK_CONTAINER(eventbox), box);
|
||||
gtk_widget_show(box);
|
||||
|
||||
/* tasklist */
|
||||
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
|
||||
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_ETCHED_IN);
|
||||
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
||||
gtk_box_pack_start(GTK_BOX(box), scrolled_window, TRUE, TRUE, 0);
|
||||
gtk_widget_show(scrolled_window);
|
||||
|
||||
tree_view = gtk_tree_view_new();
|
||||
g_signal_connect_swapped(G_OBJECT(tree_view), "button-press-event", G_CALLBACK(handle_mouse_events), G_OBJECT(task_popup_menu));
|
||||
gtk_container_add(GTK_CONTAINER(scrolled_window), tree_view);
|
||||
gtk_widget_show(tree_view);
|
||||
|
||||
selection = gtk_tree_view_get_selection(tree_view);
|
||||
|
||||
list_store = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
|
||||
gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view), list_store);
|
||||
|
||||
cell_renderer = gtk_cell_renderer_text_new();
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("User", cell_renderer, "text", 0, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("PID", cell_renderer, "text", 1, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("PPID", cell_renderer, "text", 2, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("Command", cell_renderer, "text", 3, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
|
||||
|
||||
/* load the tasklist */
|
||||
refresh_task_list();
|
||||
|
||||
/* Quit-button */
|
||||
button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
|
||||
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(gtk_main_quit), NULL);
|
||||
gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
|
||||
gtk_widget_show(button);
|
||||
|
||||
gtk_widget_show(window);
|
||||
}
|
||||
|
||||
/* add new tasks to the list */
|
||||
gboolean add_new_list_item(struct task task)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
|
||||
/* Append new line in the list */
|
||||
gtk_list_store_append(GTK_LIST_STORE(list_store), &iter);
|
||||
|
||||
/* Fill the appended line with data */
|
||||
gchar *list_value_1 = g_strdup_printf("%s", task.uid);
|
||||
gtk_list_store_set(GTK_LIST_STORE(list_store), &iter, 0, list_value_1, -1);
|
||||
g_free(list_value_1);
|
||||
|
||||
gchar *list_value_2 = g_strdup_printf("%s", task.pid);
|
||||
gtk_list_store_set(GTK_LIST_STORE(list_store), &iter, 1, list_value_2, -1);
|
||||
g_free(list_value_2);
|
||||
|
||||
gchar *list_value_3 = g_strdup_printf("%s", task.ppid);
|
||||
gtk_list_store_set(GTK_LIST_STORE(list_store), &iter, 2, list_value_3, -1);
|
||||
g_free(list_value_3);
|
||||
|
||||
gchar *list_value_4 = g_strdup_printf("%s", task.name);
|
||||
gtk_list_store_set(GTK_LIST_STORE(list_store), &iter, 3, list_value_4, -1);
|
||||
g_free(list_value_4);
|
||||
|
||||
return(TRUE);
|
||||
}
|
||||
|
||||
void remove_list_item(struct task task)
|
||||
{
|
||||
gboolean valid;
|
||||
GtkTreeIter iter;
|
||||
|
||||
valid = gtk_tree_model_get_iter_first(list_store, &iter);
|
||||
|
||||
while(valid)
|
||||
{
|
||||
gchar *str_data;
|
||||
gtk_tree_model_get(list_store, &iter, 1, &str_data, -1);
|
||||
|
||||
if(strcmp(task.pid,str_data) == 0)
|
||||
{
|
||||
gtk_list_store_remove(list_store, &iter);
|
||||
g_free(str_data);
|
||||
break;
|
||||
}
|
||||
g_free(str_data);
|
||||
|
||||
valid = gtk_tree_model_iter_next(list_store, &iter);
|
||||
}
|
||||
}
|
||||
|
||||
GtkWidget *create_task_popup_menu()
|
||||
{
|
||||
GtkWidget *menu, *menuitem;
|
||||
|
||||
menu = gtk_menu_new();
|
||||
|
||||
menuitem = gtk_menu_item_new_with_label("Stop");
|
||||
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(handle_task_menu), "STOP");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
menuitem = gtk_menu_item_new_with_label("Continue");
|
||||
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(handle_task_menu), "CONT");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
menuitem = gtk_menu_item_new_with_label("Term");
|
||||
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(handle_task_menu), "TERM");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
menuitem = gtk_menu_item_new_with_label("Kill");
|
||||
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(handle_task_menu), "KILL");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
return(menu);
|
||||
}
|
||||
|
||||
GtkWidget *create_main_popup_menu()
|
||||
{
|
||||
GtkWidget *menu, *menuitem;
|
||||
|
||||
menu = gtk_menu_new();
|
||||
|
||||
menuitem = gtk_menu_item_new_with_label("About");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
menuitem = gtk_separator_menu_item_new();
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
menuitem = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);
|
||||
g_signal_connect(G_OBJECT(menuitem), "activate", G_CALLBACK(gtk_main_quit), NULL);
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
|
||||
gtk_widget_show(menuitem);
|
||||
|
||||
return(menu);
|
||||
}
|
||||
|
||||
gboolean handle_mouse_events(GtkWidget *widget, GdkEventButton *event)
|
||||
{
|
||||
if(event->button == 3)
|
||||
{
|
||||
GdkEventButton *mouseevent = (GdkEventButton *)event;
|
||||
gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, mouseevent->button, mouseevent->time);
|
||||
return FALSE;
|
||||
}
|
||||
return(FALSE);
|
||||
}
|
||||
|
||||
void handle_task_menu(GtkWidget *widget, gchar *signal)
|
||||
{
|
||||
if(signal != NULL)
|
||||
{
|
||||
if(strcmp(signal, "TERM") == 0)
|
||||
{
|
||||
if(xfce_confirm("Really TERM the Task?", 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if(strcmp(signal, "KILL") == 0)
|
||||
{
|
||||
if(xfce_confirm("Really KILL the Task?", 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
src/gui.h
Normal file
36
src/gui.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 <gtk/gtk.h>
|
||||
#include <libxfcegui4/libxfcegui4.h>
|
||||
|
||||
#include "types.h"
|
||||
#include "functions.h"
|
||||
|
||||
GtkTreeSelection *selection;
|
||||
GtkListStore *list_store;
|
||||
|
||||
void gui_create(void);
|
||||
gboolean add_new_list_item(struct task task);
|
||||
void remove_list_item(struct task task);
|
||||
GtkWidget *create_task_popup_menu(void);
|
||||
GtkWidget *create_main_popup_menu(void);
|
||||
gboolean handle_mouse_events(GtkWidget *widget, GdkEventButton *event);
|
||||
void handle_task_menu(GtkWidget *widget, gchar *signal);
|
||||
53
src/main.c
Normal file
53
src/main.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 <gtk/gtk.h>
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "gui.h"
|
||||
|
||||
/* config vars */
|
||||
#define REFRESH_INTERVAL 1
|
||||
|
||||
/* handler for SIGALRM to refresh the list */
|
||||
void refresh_handler(void)
|
||||
{
|
||||
refresh_task_list();
|
||||
alarm(REFRESH_INTERVAL);
|
||||
}
|
||||
|
||||
/* main */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
create_gui();
|
||||
|
||||
signal(SIGALRM, refresh_handler);
|
||||
alarm(REFRESH_INTERVAL);
|
||||
|
||||
gtk_main();
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
src/types.h
Normal file
33
src/types.h
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 __TYPES_H_
|
||||
#define __TYPES_H_
|
||||
|
||||
struct task
|
||||
{
|
||||
gchar pid[256];
|
||||
gchar ppid[256];
|
||||
gchar uid[256];
|
||||
gchar name[256];
|
||||
gboolean checked;
|
||||
};
|
||||
|
||||
#endif
|
||||
Binary file not shown.
@@ -1,325 +0,0 @@
|
||||
/*
|
||||
* xfce4-taskmanager - very simple taskmanger
|
||||
*
|
||||
* Copyright (c) 2004 Johannes Zellner, <webmaster@nebulon.de>
|
||||
*
|
||||
* 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 <gtk/gtk.h>
|
||||
#include <glib.h>
|
||||
#include <stdio.h>
|
||||
#include <libxfcegui4/libxfcegui4.h>
|
||||
#include <dirent.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* config vars */
|
||||
#define REFRESH_INTERVAL 3
|
||||
|
||||
gchar *current_task_id = "";
|
||||
GtkListStore *model;
|
||||
GtkWidget *window;
|
||||
|
||||
struct task
|
||||
{
|
||||
gchar pid[256];
|
||||
gchar ppid[256];
|
||||
gchar uid[256];
|
||||
gchar name[256];
|
||||
};
|
||||
|
||||
/* called, if the user select a listitem */
|
||||
static void select_task(GtkTreeSelection *selection)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
GtkTreeModel *model;
|
||||
|
||||
if (gtk_tree_selection_get_selected (selection, &model, &iter))
|
||||
{
|
||||
gtk_tree_model_get (model, &iter, 1, ¤t_task_id, -1);
|
||||
}
|
||||
}
|
||||
|
||||
/* appends a new listitem */
|
||||
static void add_new_list_item(struct task task)
|
||||
{
|
||||
GtkTreeIter iter;
|
||||
struct passwd *passwdp;
|
||||
|
||||
/* Append new line in the list */
|
||||
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
|
||||
|
||||
/* Fill the appended line with data */
|
||||
passwdp = getpwuid(atoi(task.uid));
|
||||
gchar *list_value_1 = g_strdup_printf("%s", passwdp->pw_name);
|
||||
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 0, list_value_1, -1);
|
||||
g_free(list_value_1);
|
||||
|
||||
gchar *list_value_2 = g_strdup_printf("%s", task.pid);
|
||||
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 1, list_value_2, -1);
|
||||
g_free(list_value_2);
|
||||
|
||||
gchar *list_value_3 = g_strdup_printf("%s", task.ppid);
|
||||
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 2, list_value_3, -1);
|
||||
g_free(list_value_3);
|
||||
|
||||
gchar *list_value_4 = g_strdup_printf("%s", task.name);
|
||||
gtk_list_store_set(GTK_LIST_STORE(model), &iter, 3, list_value_4, -1);
|
||||
g_free(list_value_4);
|
||||
}
|
||||
|
||||
/* reads all taskes from 'ps' and creates the listitems */
|
||||
static void get_task_list(void)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
||||
gchar buffer[256];
|
||||
gint line_count = 0;
|
||||
struct task task;
|
||||
|
||||
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)
|
||||
strcpy(task.pid,g_strstrip(g_strsplit(buffer, ":", 2)[1]));
|
||||
else if(line_count == 5)
|
||||
strcpy(task.ppid,g_strstrip(g_strsplit(buffer, ":", 2)[1]));
|
||||
else if(line_count == 7)
|
||||
strcpy(task.uid,g_strsplit(g_strstrip(g_strsplit(buffer, ":", 2)[1]), "\t", 2)[0]);
|
||||
|
||||
line_count++;
|
||||
}
|
||||
|
||||
line_count = 0;
|
||||
fclose(task_file);
|
||||
|
||||
add_new_list_item(task);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
/* function to kill the current task */
|
||||
static void send_signal_to_task(gchar *signal)
|
||||
{
|
||||
if(current_task_id != "")
|
||||
{
|
||||
gchar command[64] = "kill -";
|
||||
g_strlcat(command,signal, 64);
|
||||
g_strlcat(command," ", 64);
|
||||
g_strlcat(command,current_task_id, 64);
|
||||
|
||||
if(system(command) != 0)
|
||||
xfce_err("Couldn't %s the task with ID %s", signal, current_task_id);
|
||||
}
|
||||
}
|
||||
|
||||
/* handles all the button events */
|
||||
static void handle_events(GtkWidget *widget, gchar *widget_action)
|
||||
{
|
||||
if(strcmp(widget_action, "task_stop") == 0)
|
||||
{
|
||||
send_signal_to_task("STOP");
|
||||
gtk_list_store_clear(GTK_LIST_STORE(model));
|
||||
get_task_list();
|
||||
}
|
||||
if(strcmp(widget_action, "task_cont") == 0)
|
||||
{
|
||||
send_signal_to_task("CONT");
|
||||
gtk_list_store_clear(GTK_LIST_STORE(model));
|
||||
get_task_list();
|
||||
}
|
||||
if(strcmp(widget_action, "task_term") == 0)
|
||||
{
|
||||
if(xfce_confirm("Really TERM the task?", GTK_STOCK_OK, NULL))
|
||||
{
|
||||
send_signal_to_task("TERM");
|
||||
gtk_list_store_clear(GTK_LIST_STORE(model));
|
||||
get_task_list();
|
||||
}
|
||||
}
|
||||
if(strcmp(widget_action, "task_kill") == 0)
|
||||
{
|
||||
if(xfce_confirm("Really KILL the task?", GTK_STOCK_OK, NULL))
|
||||
{
|
||||
send_signal_to_task("KILL");
|
||||
gtk_list_store_clear(GTK_LIST_STORE(model));
|
||||
get_task_list();
|
||||
}
|
||||
}
|
||||
if(strcmp(widget_action, "button_reload") == 0)
|
||||
{
|
||||
gtk_list_store_clear(GTK_LIST_STORE(model));
|
||||
get_task_list();
|
||||
}
|
||||
if(strcmp(widget_action, "button_quit") == 0)
|
||||
{
|
||||
gtk_main_quit();
|
||||
}
|
||||
}
|
||||
|
||||
/* handles all the mouse events */
|
||||
static gboolean handle_mouse_events(GtkWidget *widget, GdkEventButton *event)
|
||||
{
|
||||
printf("hallo");
|
||||
if(event->button == 3)
|
||||
{
|
||||
GdkEventButton *mouseevent = (GdkEventButton *)event;
|
||||
gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL, mouseevent->button, mouseevent->time);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* create the GUI */
|
||||
static void create_gui(void)
|
||||
{
|
||||
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
||||
gtk_window_set_title (GTK_WINDOW (window), "xfce4-taskmanager");
|
||||
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK (gtk_main_quit), NULL);
|
||||
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
|
||||
gtk_widget_set_size_request (GTK_WIDGET (window), 450, 400);
|
||||
|
||||
GtkWidget *button;
|
||||
GtkWidget *box1;
|
||||
|
||||
box1 = gtk_vbox_new(FALSE, 10);
|
||||
|
||||
/* list rightclickmenu */
|
||||
GtkWidget *list_popupmenu;
|
||||
GtkWidget *popupmenuitem;
|
||||
|
||||
list_popupmenu = gtk_menu_new();
|
||||
|
||||
popupmenuitem = gtk_menu_item_new_with_label("Stop");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(list_popupmenu), popupmenuitem);
|
||||
g_signal_connect(G_OBJECT(popupmenuitem), "activate", G_CALLBACK(handle_events), "task_stop");
|
||||
gtk_widget_show(popupmenuitem);
|
||||
|
||||
popupmenuitem = gtk_menu_item_new_with_label("Continue");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(list_popupmenu), popupmenuitem);
|
||||
g_signal_connect(G_OBJECT(popupmenuitem), "activate", G_CALLBACK(handle_events), "task_cont");
|
||||
gtk_widget_show(popupmenuitem);
|
||||
|
||||
popupmenuitem = gtk_menu_item_new_with_label("Term");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(list_popupmenu), popupmenuitem);
|
||||
g_signal_connect(G_OBJECT(popupmenuitem), "activate", G_CALLBACK(handle_events), "task_term");
|
||||
gtk_widget_show(popupmenuitem);
|
||||
|
||||
popupmenuitem = gtk_menu_item_new_with_label("Kill");
|
||||
gtk_menu_shell_append(GTK_MENU_SHELL(list_popupmenu), popupmenuitem);
|
||||
g_signal_connect(G_OBJECT(popupmenuitem), "activate", G_CALLBACK(handle_events), "task_kill");
|
||||
gtk_widget_show(popupmenuitem);
|
||||
|
||||
/* tasklistgui */
|
||||
GtkWidget *scrolled_window;
|
||||
GtkTreeSelection *select;
|
||||
GtkWidget *tree_view;
|
||||
GtkTreeIter iter;
|
||||
GtkCellRenderer *cell;
|
||||
GtkTreeViewColumn *column;
|
||||
|
||||
scrolled_window = gtk_scrolled_window_new (NULL, NULL);
|
||||
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_ETCHED_IN);
|
||||
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
||||
|
||||
model = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
|
||||
tree_view = gtk_tree_view_new();
|
||||
gtk_container_add (GTK_CONTAINER(scrolled_window), tree_view);
|
||||
gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (model));
|
||||
|
||||
g_signal_connect_swapped (G_OBJECT(tree_view), "button-press-event", G_CALLBACK(handle_mouse_events), G_OBJECT(list_popupmenu));
|
||||
|
||||
gtk_widget_show (tree_view);
|
||||
|
||||
cell = gtk_cell_renderer_text_new();
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("User", cell, "text", 0, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), GTK_TREE_VIEW_COLUMN (column));
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("PID", cell, "text", 1, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), GTK_TREE_VIEW_COLUMN (column));
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("PPID", cell, "text", 2, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), GTK_TREE_VIEW_COLUMN (column));
|
||||
|
||||
column = gtk_tree_view_column_new_with_attributes("Command", cell, "text", 3, NULL);
|
||||
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), GTK_TREE_VIEW_COLUMN (column));
|
||||
|
||||
select = gtk_tree_view_get_selection(GTK_TREE_VIEW (tree_view));
|
||||
gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
|
||||
g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(select_task), NULL);
|
||||
|
||||
/* load the tasklist */
|
||||
get_task_list();
|
||||
|
||||
gtk_box_pack_start(GTK_BOX(box1), scrolled_window, TRUE, TRUE, 0);
|
||||
gtk_widget_show(scrolled_window);
|
||||
|
||||
/* QUIT-Button */
|
||||
button = gtk_button_new_from_stock(GTK_STOCK_QUIT);
|
||||
g_signal_connect(G_OBJECT (button), "clicked", G_CALLBACK(handle_events), "button_quit");
|
||||
gtk_box_pack_start(GTK_BOX(box1), button, FALSE, TRUE, 0);
|
||||
gtk_widget_show(button);
|
||||
|
||||
gtk_container_add (GTK_CONTAINER(window), box1);
|
||||
gtk_widget_show(box1);
|
||||
|
||||
gtk_widget_show(window);
|
||||
}
|
||||
|
||||
/* handler for SIGALRM to refresh the list */
|
||||
void refresh_handler(void)
|
||||
{
|
||||
gtk_list_store_clear(GTK_LIST_STORE(model));
|
||||
get_task_list();
|
||||
alarm(REFRESH_INTERVAL);
|
||||
}
|
||||
|
||||
/* main */
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
gtk_init(&argc, &argv);
|
||||
|
||||
create_gui();
|
||||
|
||||
signal(SIGALRM, refresh_handler);
|
||||
alarm(REFRESH_INTERVAL);
|
||||
|
||||
gtk_main();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user