move statusbar related code from main.c into it's own main_statusbar.c
[obnox/wireshark/wip.git] / gtk / mtp3_stat.c
1 /* mtp3_stat.c
2  *
3  * Copyright 2004, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * Modified from gsm_map_stat.c
7  *
8  * $Id$
9  *
10  * Wireshark - Network traffic analyzer
11  * By Gerald Combs <gerald@wireshark.org>
12  * Copyright 1998 Gerald Combs
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27  */
28
29 /*
30  * This TAP provides statistics for MTP3:
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <string.h>
39
40 #include "epan/packet_info.h"
41 #include "epan/epan.h"
42 #include "epan/value_string.h"
43 #include <epan/stat_cmd_args.h>
44 #include "../stat_menu.h"
45 #include "gui_stat_menu.h"
46 #include "image/clist_ascend.xpm"
47 #include "image/clist_descend.xpm"
48 #include "simple_dialog.h"
49 #include "dlg_utils.h"
50 #include <epan/tap.h>
51 #include <epan/emem.h>
52 #include "../register.h"
53 #include "../globals.h"
54 #include "filter_dlg.h"
55 #include "gui_utils.h"
56
57 #include <epan/dissectors/packet-mtp3.h>
58 #include "mtp3_stat.h"
59
60 typedef struct column_arrows {
61     GtkWidget           *table;
62     GtkWidget           *ascend_pm;
63     GtkWidget           *descend_pm;
64 } column_arrows;
65
66 #define MTP3_INIT_TABLE_NUM_COLUMNS             6
67
68 typedef struct _my_columns_t {
69     guint32             value;
70     const gchar         *strptr;
71     GtkJustification    just;
72 } my_columns_t;
73
74 static my_columns_t columns[MTP3_INIT_TABLE_NUM_COLUMNS] = {
75     { 80,       "OPC",                  GTK_JUSTIFY_LEFT },
76     { 80,       "DPC",                  GTK_JUSTIFY_LEFT },
77     { 110,      "SI",                   GTK_JUSTIFY_LEFT },
78     { 80,       "Num MSUs",             GTK_JUSTIFY_RIGHT },
79     { 100,      "Num Bytes",            GTK_JUSTIFY_RIGHT },
80     { 80,       "Avg Bytes",            GTK_JUSTIFY_RIGHT }
81 };
82
83 typedef struct _mtp3_stat_dlg_t {
84     GtkWidget           *win;
85     GtkWidget           *scrolled_win;
86     GtkWidget           *table;
87     char                *entries[MTP3_INIT_TABLE_NUM_COLUMNS];
88 } mtp3_stat_dlg_t;
89
90 static mtp3_stat_dlg_t  dlg;
91
92 mtp3_stat_t             mtp3_stat[MTP3_MAX_NUM_OPC_DPC];
93 guint8                  mtp3_num_used;
94
95
96 static void
97 mtp3_stat_reset(
98     void                *tapdata)
99 {
100     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
101
102     mtp3_num_used = 0;
103     memset(stat_p, 0, MTP3_MAX_NUM_OPC_DPC * sizeof(mtp3_stat_t));
104
105     if (dlg.win != NULL)
106     {
107         gtk_clist_clear(GTK_CLIST(dlg.table));
108     }
109 }
110
111
112 static int
113 mtp3_stat_packet(
114     void                *tapdata,
115     packet_info         *pinfo _U_,
116     epan_dissect_t      *edt _U_,
117     const void          *data)
118 {
119     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
120     const mtp3_tap_rec_t        *data_p = data;
121     int                         i;
122
123     if (data_p->si_code >= MTP3_NUM_SI_CODE)
124     {
125         /*
126          * we thought this si_code was not used ?
127          * is MTP3_NUM_SI_CODE out of date ?
128          */
129         return(0);
130     }
131
132     /*
133      * look for opc/dpc pair
134      */
135     i = 0;
136     while (i < mtp3_num_used)
137     {
138         if (memcmp(&data_p->addr_opc, &(*stat_p)[i].addr_opc, sizeof(mtp3_addr_pc_t)) == 0)
139         {
140             if (memcmp(&data_p->addr_dpc, &(*stat_p)[i].addr_dpc, sizeof(mtp3_addr_pc_t)) == 0)
141             {
142                 break;
143             }
144         }
145
146         i++;
147     }
148
149     if (i == mtp3_num_used)
150     {
151         if (mtp3_num_used == MTP3_MAX_NUM_OPC_DPC)
152         {
153             /*
154              * too many
155              */
156             return(0);
157         }
158
159         mtp3_num_used++;
160     }
161
162     (*stat_p)[i].addr_opc = data_p->addr_opc;
163     (*stat_p)[i].addr_dpc = data_p->addr_dpc;
164     (*stat_p)[i].si_code[data_p->si_code].num_msus++;
165     (*stat_p)[i].si_code[data_p->si_code].size += data_p->size;
166
167     return(1);
168 }
169
170
171 static void
172 mtp3_stat_draw(
173     void                *tapdata)
174 {
175     mtp3_stat_t         (*stat_p)[MTP3_MAX_NUM_OPC_DPC] = tapdata;
176     int                 i, j, row_offset;
177     char                *str;
178
179     if (!dlg.win || !tapdata)
180     {
181         return;
182     }
183
184     str=ep_alloc(256);
185     i = 0;
186
187     while (i < mtp3_num_used)
188     {
189         row_offset = i * MTP3_NUM_SI_CODE;
190
191         mtp3_addr_to_str_buf(&(*stat_p)[i].addr_opc, str, 256);
192         dlg.entries[0] = g_strdup(str);
193
194         mtp3_addr_to_str_buf(&(*stat_p)[i].addr_dpc, str, 256);
195         dlg.entries[1] = g_strdup(str);
196
197         for (j=0; j < MTP3_NUM_SI_CODE; j++)
198         {
199             dlg.entries[2] = g_strdup(mtp3_service_indicator_code_short_vals[j].strptr);
200
201             dlg.entries[3] = g_strdup_printf("%u", (*stat_p)[i].si_code[j].num_msus);
202
203             dlg.entries[4] = g_strdup_printf("%.0f", (*stat_p)[i].si_code[j].size);
204
205             dlg.entries[5] =
206                 g_strdup_printf("%.2f",
207                     (*stat_p)[i].si_code[j].size/(*stat_p)[i].si_code[j].num_msus);
208
209             gtk_clist_insert(GTK_CLIST(dlg.table), row_offset + j, dlg.entries);
210         }
211
212         i++;
213     }
214
215     gtk_clist_sort(GTK_CLIST(dlg.table));
216 }
217
218
219 static void
220 mtp3_stat_gtk_click_column_cb(
221     GtkCList            *clist,
222     gint                column,
223     gpointer            data)
224 {
225     column_arrows       *col_arrows = (column_arrows *) data;
226     int                 i;
227
228
229     gtk_clist_freeze(clist);
230
231     for (i=0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
232     {
233         gtk_widget_hide(col_arrows[i].ascend_pm);
234         gtk_widget_hide(col_arrows[i].descend_pm);
235     }
236
237     if (column == clist->sort_column)
238     {
239         if (clist->sort_type == GTK_SORT_ASCENDING)
240         {
241             clist->sort_type = GTK_SORT_DESCENDING;
242             gtk_widget_show(col_arrows[column].descend_pm);
243         }
244         else
245         {
246             clist->sort_type = GTK_SORT_ASCENDING;
247             gtk_widget_show(col_arrows[column].ascend_pm);
248         }
249     }
250     else
251     {
252         /*
253          * Columns 0-1 sorted in descending order by default
254          */
255         if (column <= 1)
256         {
257             clist->sort_type = GTK_SORT_ASCENDING;
258             gtk_widget_show(col_arrows[column].ascend_pm);
259         }
260         else
261         {
262             clist->sort_type = GTK_SORT_DESCENDING;
263             gtk_widget_show(col_arrows[column].descend_pm);
264         }
265
266         gtk_clist_set_sort_column(clist, column);
267     }
268
269     gtk_clist_thaw(clist);
270     gtk_clist_sort(clist);
271 }
272
273
274 static gint
275 mtp3_stat_gtk_sort_column(
276     GtkCList            *clist,
277     gconstpointer       ptr1,
278     gconstpointer       ptr2)
279 {
280     const GtkCListRow   *row1 = ptr1;
281     const GtkCListRow   *row2 = ptr2;
282     char                *text1 = NULL;
283     char                *text2 = NULL;
284     int                 i1, i2;
285
286     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
287     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
288
289     switch (clist->sort_column)
290     {
291     case 0:
292     case 1:
293     case 2:
294         /* text columns */
295         return(strcmp(text1, text2));
296
297     default:
298         /* number columns */
299         i1 = strtol(text1, NULL, 0);
300         i2 = strtol(text2, NULL, 0);
301         return(i1 - i2);
302     }
303
304     g_assert_not_reached();
305
306     return(0);
307 }
308
309
310 static void
311 mtp3_stat_gtk_win_destroy_cb(
312     GtkWindow           *win _U_,
313     gpointer            user_data _U_)
314 {
315     memset((void *) user_data, 0, sizeof(mtp3_stat_dlg_t));
316 }
317
318
319 static void
320 mtp3_stat_gtk_win_create(
321     mtp3_stat_dlg_t     *dlg_p,
322     const char          *title)
323 {
324     int                 i;
325     column_arrows       *col_arrows;
326     GtkWidget           *column_lb;
327     GtkWidget           *vbox;
328     GtkWidget           *bt_close;
329     GtkWidget           *bbox;
330
331
332     dlg_p->win = window_new(GTK_WINDOW_TOPLEVEL, title);
333     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 640, 390);
334
335     vbox = gtk_vbox_new(FALSE, 3);
336         gtk_container_add(GTK_CONTAINER(dlg_p->win), vbox);
337     gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
338
339     dlg_p->scrolled_win = scrolled_window_new(NULL, NULL);
340     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
341
342     dlg_p->table = gtk_clist_new(MTP3_INIT_TABLE_NUM_COLUMNS);
343
344     col_arrows =
345         (column_arrows *) g_malloc(sizeof(column_arrows) * MTP3_INIT_TABLE_NUM_COLUMNS);
346
347     for (i = 0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
348     {
349         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
350
351         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
352
353         column_lb = gtk_label_new(columns[i].strptr);
354
355         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
356             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
357
358         gtk_widget_show(column_lb);
359
360         col_arrows[i].ascend_pm = xpm_to_widget(clist_ascend_xpm);
361
362         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
363             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
364
365         col_arrows[i].descend_pm = xpm_to_widget(clist_descend_xpm);
366
367         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
368             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
369
370         if (i == 0)
371         {
372             /* default column sorting */
373             gtk_widget_show(col_arrows[i].ascend_pm);
374         }
375
376         gtk_clist_set_column_justification(GTK_CLIST(dlg_p->table), i, columns[i].just);
377
378         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
379         gtk_widget_show(col_arrows[i].table);
380     }
381     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
382
383     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), mtp3_stat_gtk_sort_column);
384     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
385     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
386
387     for (i = 0; i < MTP3_INIT_TABLE_NUM_COLUMNS; i++)
388     {
389         gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), i, columns[i].value);
390     }
391
392     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
393     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
394     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
395
396     g_signal_connect(dlg_p->table, "click-column", G_CALLBACK(mtp3_stat_gtk_click_column_cb), col_arrows);
397
398     /* Button row. */
399     bbox = dlg_button_row_new(GTK_STOCK_CLOSE, NULL);
400     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, FALSE, 0);
401
402     bt_close = g_object_get_data(G_OBJECT(bbox), GTK_STOCK_CLOSE);
403     window_set_cancel_button(dlg_p->win, bt_close, window_cancel_button_cb);
404
405     g_signal_connect(dlg_p->win, "delete_event", G_CALLBACK(window_delete_event_cb), NULL);
406     g_signal_connect(dlg_p->win, "destroy", G_CALLBACK(mtp3_stat_gtk_win_destroy_cb), dlg_p);
407
408     gtk_widget_show_all(dlg_p->win);
409     window_present(dlg_p->win);
410 }
411
412
413 static void
414 mtp3_stat_gtk_cb(
415     GtkWidget           *w _U_,
416     gpointer            d _U_)
417 {
418
419     /*
420      * if the window is already open, bring it to front
421      */
422     if (dlg.win)
423     {
424         gdk_window_raise(dlg.win->window);
425         return;
426     }
427
428     mtp3_stat_gtk_win_create(&dlg, "MTP3 Statistics");
429
430     mtp3_stat_draw(&mtp3_stat);
431 }
432
433
434 static void
435 mtp3_stat_gtk_init( const char *optarg _U_, void* userdata _U_)
436 {
437     mtp3_stat_gtk_cb(NULL, NULL);
438 }
439
440
441 void
442 register_tap_listener_gtkmtp3_stat(void)
443 {
444     GString             *err_p;
445
446
447     memset((void *) &mtp3_stat, 0, sizeof(mtp3_stat_t));
448
449     err_p =
450         register_tap_listener("mtp3", &mtp3_stat, NULL,
451             mtp3_stat_reset,
452             mtp3_stat_packet,
453             mtp3_stat_draw);
454
455     if (err_p != NULL)
456     {
457         simple_dialog(ESD_TYPE_WARN, ESD_BTN_OK, err_p->str);
458         g_string_free(err_p, TRUE);
459
460         exit(1);
461     }
462
463     register_stat_menu_item("MTP3/MSUs",  REGISTER_STAT_GROUP_TELEPHONY,
464         mtp3_stat_gtk_cb, NULL, NULL, NULL);
465     register_stat_cmd_arg("mtp3,msus", mtp3_stat_gtk_init,NULL);
466 }