3 * Copyright (C) 2004 Jan Kratochvil <project-udpforward@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "ui-gnome.h" /* self */
23 #include <glib/gmessages.h>
25 #include <libgnomeui/gnome-app.h>
26 #include <sys/types.h>
30 #include <libgnomeui/gnome-appbar.h>
31 #include <gtk/gtkbutton.h>
34 #include <gtk/gtkentry.h>
35 #include <gtk/gtktogglebutton.h>
37 #include <gtk/gtkmain.h>
41 #define PATHNAME_PID "/var/run/udpforward.pid"
42 #define DAEMON_CHECK_INTERVAL_MS 500
43 #define PORT_RANGE_BEGIN 2048
44 #define PORT_RANGE_END 10240
48 static GtkButton *ButtonStart;
49 static GtkButton *ButtonStop;
50 static GtkHBox *PortHBox;
51 static GnomeAppBar *AppBar;
52 static GtkEntry *PortEntry;
55 static pid_t daemon_pid;
57 static gboolean is_daemon_running(void)
60 char buf[LINE_MAX],*got;
64 if (!(f=fopen(PATHNAME_PID,"r")))
66 got=fgets(buf,sizeof(buf),f);
67 fclose(f); /* FIXME: error ignored */
77 if (kill((pid_t)pid_int,0)) {
82 daemon_pid=(pid_t)pid_int;
86 static void state_start_stop(void)
88 gboolean daemon_running;
90 daemon_running=is_daemon_running();
91 gtk_widget_set_sensitive(GTK_WIDGET(ButtonStart),!daemon_running);
92 gtk_widget_set_sensitive(GTK_WIDGET(ButtonStop) , daemon_running);
93 gtk_widget_set_sensitive(GTK_WIDGET(PortHBox) ,!daemon_running);
95 gnome_appbar_set_status(AppBar,
96 udpforward_printf_alloca(_("udpforward daemon running as PID %d."),(int)daemon_pid));
98 gnome_appbar_set_status(AppBar,_("No udpforward daemon currently running."));
101 static gboolean daemon_check_timeout_func(gpointer data /* unused */)
104 return TRUE; /* continue running */
107 void on_PortButtonRandom_clicked(GtkButton *button,gpointer user_data)
109 g_return_if_fail(GTK_IS_BUTTON(button));
112 if ((pid_t)-1!=daemon_pid)
114 gtk_entry_set_text(PortEntry,
115 udpforward_printf_alloca("%d",(int)g_random_int_range(PORT_RANGE_BEGIN,PORT_RANGE_END)));
118 void on_AutostartCheckButton_toggled(GtkToggleButton *togglebutton,gpointer user_data)
120 g_return_if_fail(GTK_IS_TOGGLE_BUTTON(togglebutton));
123 void on_ButtonStart_clicked(GtkButton *button,gpointer user_data)
125 g_return_if_fail(GTK_IS_BUTTON(button));
128 if ((pid_t)-1!=daemon_pid)
132 void on_ButtonStop_clicked(GtkButton *button,gpointer user_data)
136 g_return_if_fail(GTK_IS_BUTTON(button));
139 if ((pid_t)-1==daemon_pid)
142 kill(daemon_pid,SIGKILL);
145 g_warning(udpforward_printf_alloca(_("Unable to stop the daemon at PID %d: %s"),
146 (int)daemon_pid,strerror(errno_save)));
149 void on_ButtonHide_clicked(GtkButton *button,gpointer user_data)
151 g_return_if_fail(GTK_IS_BUTTON(button));
156 /* of "ui-gnome-interface.h": */
157 GtkWidget *create_App(void);
158 /* of "ui-gnome-support.h": */
159 GtkWidget *lookup_widget(GtkWidget *widget,const gchar *widget_name);
161 gboolean ui_gnome_init(void)
163 App=GNOME_APP(create_App());
165 ButtonStart=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStart"));
166 ButtonStop=GTK_BUTTON(lookup_widget(GTK_WIDGET(App),"ButtonStop"));
167 PortHBox=GTK_HBOX(lookup_widget(GTK_WIDGET(App),"PortHBox"));
168 AppBar=GNOME_APPBAR(lookup_widget(GTK_WIDGET(App),"AppBar"));
169 PortEntry=GTK_ENTRY(lookup_widget(GTK_WIDGET(App),"PortEntry"));
171 gtk_widget_show_all(GTK_WIDGET(App));
173 DAEMON_CHECK_INTERVAL_MS, /* interval */
174 daemon_check_timeout_func, /* function */
175 NULL); /* data; unused */
179 void ui_gnome_interactive(void)