split capture_loop from capture.c, some more code cleanup
[obnox/wireshark/wip.git] / gtk / capture_info_dlg.c
1 /* capture_info_dlg.c
2  * Routines for packet capture info dialog
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_LIBPCAP
30
31 #include <pcap.h>
32 #include <gtk/gtk.h>
33 #include "gtk/compat_macros.h"
34
35 #include <time.h>
36
37 #include <epan/packet.h>
38 #include "../capture.h"
39 #include "globals.h"
40 #include "capture_combo_utils.h"
41 #include "dlg_utils.h"
42 #include "ui_util.h"
43
44 /* a single capture counter value (with title, pointer to value and GtkWidgets) */
45 /* as the packet_counts is a struct, not an array, keep a pointer to the */
46 /* corresponding value packet_counts, to speed up (and simplify) output of values */
47 typedef struct {
48     const gchar *title;
49     gint        *value_ptr;
50     GtkWidget   *label, *value_lb, *percent_pb, *percent_lb;
51 } capture_info_counts_t;
52
53 /* all data we need to know of this dialog, after creation finished */
54 typedef struct {
55     GtkWidget               *cap_w;
56     GtkWidget               *running_time_lb;
57     capture_info_counts_t   counts[PACKET_COUNTS_SIZE];
58 } capture_info_ui_t;
59
60
61
62 /* calculate the percentage of the current packet type */
63 static float
64 pct(gint num, gint denom) {
65   if (denom) {
66     return (float) (num * 100.0 / denom);
67   } else {
68     return 0.0;
69   }
70 }
71
72 static void
73 capture_info_delete_cb(GtkWidget *w _U_, GdkEvent *event _U_, gpointer data _U_) {
74   capture_stop();
75 }
76
77
78 /* create the capture info dialog */
79 /* will keep pointers to the fields in the counts parameter */
80 void capture_info_create(
81 capture_info    *cinfo,
82 gchar           *iface)
83 {
84   unsigned int      i;
85   GtkWidget         *main_vb, *stop_bt, *counts_tb;
86   GtkWidget         *counts_fr, *running_tb, *running_label, *bbox;
87   capture_info_ui_t *info;
88   gchar             *cap_w_title;
89
90   info = g_malloc0(sizeof(capture_info_ui_t));
91   info->counts[0].title = "Total";
92   info->counts[0].value_ptr = &(cinfo->counts->total);
93   info->counts[1].title = "SCTP";
94   info->counts[1].value_ptr = &(cinfo->counts->sctp);
95   info->counts[2].title = "TCP";
96   info->counts[2].value_ptr = &(cinfo->counts->tcp);
97   info->counts[3].title = "UDP";
98   info->counts[3].value_ptr = &(cinfo->counts->udp);
99   info->counts[4].title = "ICMP";
100   info->counts[4].value_ptr = &(cinfo->counts->icmp);
101   info->counts[5].title = "ARP";
102   info->counts[5].value_ptr = &(cinfo->counts->arp);
103   info->counts[6].title = "OSPF";
104   info->counts[6].value_ptr = &(cinfo->counts->ospf);
105   info->counts[7].title = "GRE";
106   info->counts[7].value_ptr = &(cinfo->counts->gre);
107   info->counts[8].title = "NetBIOS";
108   info->counts[8].value_ptr = &(cinfo->counts->netbios);
109   info->counts[9].title = "IPX";
110   info->counts[9].value_ptr = &(cinfo->counts->ipx);
111   info->counts[10].title = "VINES";
112   info->counts[10].value_ptr = &(cinfo->counts->vines);
113   info->counts[11].title = "Other";
114   info->counts[11].value_ptr = &(cinfo->counts->other);
115
116   cap_w_title = g_strdup_printf("Ethereal: Capture - Interface %s", iface);
117
118   info->cap_w = dlg_window_new(cap_w_title);
119   g_free(cap_w_title);
120   gtk_window_set_modal(GTK_WINDOW(info->cap_w), TRUE);
121
122   /* Container for capture display widgets */
123   main_vb = gtk_vbox_new(FALSE, 1);
124   gtk_container_border_width(GTK_CONTAINER(main_vb), 5);
125   gtk_container_add(GTK_CONTAINER(info->cap_w), main_vb);
126   gtk_widget_show(main_vb);
127
128   counts_fr = gtk_frame_new("Captured Packets");
129   gtk_box_pack_start(GTK_BOX(main_vb), counts_fr, FALSE, FALSE, 3);
130   gtk_widget_show(counts_fr);
131
132   /* Individual statistic elements */
133   counts_tb = gtk_table_new(PACKET_COUNTS_SIZE, 4, TRUE);
134   gtk_container_add(GTK_CONTAINER(counts_fr), counts_tb);
135   gtk_container_border_width(GTK_CONTAINER(counts_tb), 5);
136   gtk_widget_show(counts_tb);
137
138   gtk_table_set_row_spacings(GTK_TABLE(counts_tb), 0);
139   gtk_table_set_col_spacings(GTK_TABLE(counts_tb), 5);
140
141   for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
142       info->counts[i].label = gtk_label_new(info->counts[i].title);
143       gtk_misc_set_alignment(GTK_MISC(info->counts[i].label), 0.0f, 0.5f);
144
145       info->counts[i].value_lb = gtk_label_new("0");
146       gtk_misc_set_alignment(GTK_MISC(info->counts[i].value_lb), 0.5f, 0.5f);
147
148       if (i == 0) {
149           /* do not build a progress bar for the "total" row */
150           /* (as this could suggest a "buffer full" to the user) */
151           /* simply put a label here */
152           info->counts[i].percent_pb = gtk_label_new("% of total");
153       } else {
154           /* build a progress bar in the other rows */
155           info->counts[i].percent_pb = gtk_progress_bar_new();
156
157           /* downsize the default size of this progress bar in x direction (def:150), */
158           /* otherwise it will become too large and the dialog will look ugly */
159           /* XXX: use a TreeView instead of a table in order to fix this */
160           WIDGET_SET_SIZE(info->counts[i].percent_pb, 70, -1);
161       }
162
163       info->counts[i].percent_lb = gtk_label_new("0.0%");
164       gtk_misc_set_alignment(GTK_MISC(info->counts[i].percent_lb), 1.0f, 0.5f);
165
166       gtk_table_attach_defaults(GTK_TABLE(counts_tb),
167                                 info->counts[i].label, 0, 1, i, i + 1);
168       gtk_table_attach_defaults(GTK_TABLE(counts_tb),
169                                 info->counts[i].value_lb, 1, 2, i, i + 1);
170           gtk_table_attach_defaults(GTK_TABLE(counts_tb),
171                                                                 info->counts[i].percent_pb, 2, 3, i, i + 1);
172           gtk_table_attach_defaults(GTK_TABLE(counts_tb),
173                                                                 info->counts[i].percent_lb, 3, 4, i, i + 1);
174
175       gtk_widget_show(info->counts[i].label);
176       gtk_widget_show(info->counts[i].value_lb);
177       gtk_widget_show(info->counts[i].percent_pb);
178       /* don't show percentages for the "total" row */
179       if (i != 0) {
180         gtk_widget_show(info->counts[i].percent_lb);
181       }
182   }
183
184   /* Running time */
185   running_tb = gtk_table_new(1, 4, TRUE);
186   gtk_box_pack_start(GTK_BOX(main_vb), running_tb, FALSE, FALSE, 3);
187   gtk_widget_show(running_tb);
188
189   running_label = gtk_label_new("Running");
190   gtk_misc_set_alignment(GTK_MISC(running_label), 0.0f, 0.0f);
191   gtk_widget_show(running_label);
192   gtk_table_attach_defaults(GTK_TABLE(running_tb),
193                                 running_label, 0, 1, 0, 1);
194
195   info->running_time_lb = gtk_label_new("00:00:00");
196   gtk_misc_set_alignment(GTK_MISC(info->running_time_lb), 0.0f, 0.0f);
197   gtk_widget_show(info->running_time_lb);
198   gtk_table_attach(GTK_TABLE(running_tb),
199                        info->running_time_lb,
200                        1, 2, 0, 1, 0, 0, 5, 0);
201
202   /* allow user to either click a stop button, or the close button on
203         the window to stop a capture in progress. */
204   bbox = dlg_button_row_new(GTK_STOCK_STOP, NULL);
205   gtk_box_pack_start(GTK_BOX(main_vb), bbox, FALSE, FALSE, 3);
206   gtk_widget_show(bbox);
207
208   stop_bt = OBJECT_GET_DATA(bbox, GTK_STOCK_STOP);
209   window_set_cancel_button(info->cap_w, stop_bt, NULL);
210   SIGNAL_CONNECT(stop_bt, "clicked", capture_info_delete_cb, NULL);
211
212   SIGNAL_CONNECT(info->cap_w, "delete_event", capture_info_delete_cb,
213                  NULL);
214
215   gtk_widget_show(info->cap_w);
216   window_present(info->cap_w);
217
218   cinfo->ui = info;
219 }
220
221
222 /* update the capture info dialog */
223 /* As this function is a bit time critical while capturing, */
224 /* prepare everything possible in the capture_info_create() function above! */
225 void capture_info_update(
226 capture_info    *cinfo)
227 {
228   unsigned int      i;
229   gchar             label_str[64];
230   capture_info_ui_t *info = cinfo->ui;
231
232
233   /* calculate and display running time */
234   g_snprintf(label_str, sizeof(label_str), "%02ld:%02ld:%02ld", 
235            (long)(cinfo->running_time/3600), (long)((cinfo->running_time%3600)/60),
236            (long)(cinfo->running_time%60));
237   gtk_label_set(GTK_LABEL(info->running_time_lb), label_str);
238
239   if (cinfo->new_packets) {
240
241     for (i = 0; i < PACKET_COUNTS_SIZE; i++) {
242         g_snprintf(label_str, sizeof(label_str), "%d",
243                  *info->counts[i].value_ptr);
244         gtk_label_set(GTK_LABEL(info->counts[i].value_lb), label_str);
245
246         /* don't try to update the "total" row progress bar */
247         if (i != 0) {
248             gtk_progress_bar_update(GTK_PROGRESS_BAR(info->counts[i].percent_pb),
249                      (gfloat) (pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr) / 100.0));
250         }
251
252         g_snprintf(label_str, sizeof(label_str), "%.1f%%",
253                  pct(*info->counts[i].value_ptr, *info->counts[0].value_ptr));
254
255         gtk_label_set(GTK_LABEL(info->counts[i].percent_lb), label_str);
256     }
257   }
258 }
259
260
261 /* destroy the capture info dialog again */
262 void capture_info_destroy(
263 capture_info    *cinfo)
264 {
265   capture_info_ui_t *info = cinfo->ui;
266
267   /* called from capture engine, so it's ok to destroy the dialog here */
268   gtk_grab_remove(GTK_WIDGET(info->cap_w));
269   window_destroy(GTK_WIDGET(info->cap_w));
270   g_free(info);
271 }
272
273
274 #endif /* HAVE_LIBPCAP */