"sizeof(array)" is the size of the array in bytes, not the number of
[obnox/wireshark/wip.git] / gtk / ansi_map_stat.c
1 /* ansi_map_stat.c
2  *
3  * Copyright 2003, Michael Lum <mlum [AT] telostech.com>
4  * In association with Telos Technology Inc.
5  *
6  * MUCH code modified from service_response_time_table.c.
7  *
8  * $Id: ansi_map_stat.c,v 1.6 2003/12/21 22:21:45 guy Exp $
9  *
10  * Ethereal - Network traffic analyzer
11  * By Gerald Combs <gerald@ethereal.com>
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 ANSI MAP:
31  */
32
33 #ifdef HAVE_CONFIG_H
34 # include "config.h"
35 #endif
36
37 #include <gtk/gtk.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #include "epan/packet_info.h"
42 #include "epan/epan.h"
43 #include "epan/value_string.h"
44 #include "menu.h"
45 #include "image/clist_ascend.xpm"
46 #include "image/clist_descend.xpm"
47 #include "simple_dialog.h"
48 #include "dlg_utils.h"
49 #include "tap.h"
50 #include "../register.h"
51 #include "../globals.h"
52 #include "filter_prefs.h"
53 #include "compat_macros.h"
54
55 #include "packet-ansi_map.h"
56
57 typedef struct column_arrows {
58     GtkWidget           *table;
59     GtkWidget           *ascend_pm;
60     GtkWidget           *descend_pm;
61 } column_arrows;
62
63 typedef struct _ansi_map_stat_dlg_t {
64     GtkWidget           *win;
65     GtkWidget           *scrolled_win;
66     GtkWidget           *table;
67     char                *entries[3];
68 } ansi_map_stat_dlg_t;
69
70 #define N_MESSAGE_TYPES 0xff
71
72 typedef struct _ansi_map_stat_t {
73     int                 message_type[N_MESSAGE_TYPES];
74 } ansi_map_stat_t;
75
76 static ansi_map_stat_dlg_t      dlg;
77 static ansi_map_stat_t          stat;
78
79
80 static void
81 ansi_map_stat_reset(
82     void                *tapdata)
83 {
84     tapdata = tapdata;
85
86     memset((void *) &stat, 0, sizeof(ansi_map_stat_t));
87 }
88
89
90 static int
91 ansi_map_stat_packet(
92     void                *tapdata,
93     packet_info         *pinfo,
94     epan_dissect_t      *edt _U_,
95     void                *data)
96 {
97     ansi_map_tap_rec_t  *data_p = data;
98
99
100     tapdata = tapdata;
101     pinfo = pinfo;
102
103     if (data_p->message_type > N_MESSAGE_TYPES)
104     {
105         /*
106          * unknown PDU type !!!
107          */
108         return(0);
109     }
110
111     stat.message_type[data_p->message_type]++;
112
113     return(1);
114 }
115
116
117 static void
118 ansi_map_stat_draw(
119     void                *tapdata)
120 {
121     int                 i, j;
122     char                str[256], *strp;
123
124
125     tapdata = tapdata;
126
127     if (dlg.win != NULL)
128     {
129         i = 0;
130
131         while (ansi_map_opr_code_strings[i].strptr)
132         {
133             j = gtk_clist_find_row_from_data(GTK_CLIST(dlg.table), (gpointer) i);
134
135             sprintf(str, "%d",
136                 stat.message_type[ansi_map_opr_code_strings[i].value]);
137             strp = g_strdup(str);
138             gtk_clist_set_text(GTK_CLIST(dlg.table), j, 2, strp);
139             g_free(strp);
140
141             i++;
142         }
143
144         gtk_clist_sort(GTK_CLIST(dlg.table));
145     }
146 }
147
148
149 static void
150 ansi_map_stat_gtk_click_column_cb(
151     GtkCList            *clist,
152     gint                column,
153     gpointer            data)
154 {
155     column_arrows       *col_arrows = (column_arrows *) data;
156     int                 i;
157
158
159     gtk_clist_freeze(clist);
160
161     for (i=0; i < 3; i++)
162     {
163         gtk_widget_hide(col_arrows[i].ascend_pm);
164         gtk_widget_hide(col_arrows[i].descend_pm);
165     }
166
167     if (column == clist->sort_column)
168     {
169         if (clist->sort_type == GTK_SORT_ASCENDING)
170         {
171             clist->sort_type = GTK_SORT_DESCENDING;
172             gtk_widget_show(col_arrows[column].descend_pm);
173         }
174         else
175         {
176             clist->sort_type = GTK_SORT_ASCENDING;
177             gtk_widget_show(col_arrows[column].ascend_pm);
178         }
179     }
180     else
181     {
182         /*
183          * Columns 0-1 sorted in descending order by default
184          * Columns 2 sorted in ascending order by default
185          */
186         if (column <= 1)
187         {
188             clist->sort_type = GTK_SORT_ASCENDING;
189             gtk_widget_show(col_arrows[column].ascend_pm);
190         }
191         else
192         {
193             clist->sort_type = GTK_SORT_DESCENDING;
194             gtk_widget_show(col_arrows[column].descend_pm);
195         }
196
197         gtk_clist_set_sort_column(clist, column);
198     }
199
200     gtk_clist_thaw(clist);
201     gtk_clist_sort(clist);
202 }
203
204
205 static gint
206 ansi_map_stat_gtk_sort_column(
207     GtkCList            *clist,
208     gconstpointer       ptr1,
209     gconstpointer       ptr2)
210 {
211     GtkCListRow         *row1 = (GtkCListRow *) ptr1;
212     GtkCListRow         *row2 = (GtkCListRow *) ptr2;
213     char                *text1 = NULL;
214     char                *text2 = NULL;
215     int                 i1, i2;
216
217     text1 = GTK_CELL_TEXT(row1->cell[clist->sort_column])->text;
218     text2 = GTK_CELL_TEXT(row2->cell[clist->sort_column])->text;
219
220     switch (clist->sort_column)
221     {
222     case 0:
223         /* FALLTHRU */
224
225     case 2:
226         i1 = strtol(text1, NULL, 0);
227         i2 = strtol(text2, NULL, 0);
228         return(i1 - i2);
229
230     case 1:
231         return(strcmp(text1, text2));
232     }
233
234     g_assert_not_reached();
235
236     return(0);
237 }
238
239
240 static void
241 ansi_map_stat_gtk_dlg_close_cb(
242     GtkButton           *button _U_,
243     gpointer            user_data _U_)
244 {
245     ansi_map_stat_dlg_t *dlg_p = user_data;
246
247     gtk_grab_remove(GTK_WIDGET(dlg_p->win));
248     gtk_widget_destroy(GTK_WIDGET(dlg_p->win));
249 }
250
251
252 static void
253 ansi_map_stat_gtk_win_destroy_cb(
254     GtkWindow           *win _U_,
255     gpointer            user_data _U_)
256 {
257     memset((void *) user_data, 0, sizeof(ansi_map_stat_dlg_t));
258 }
259
260
261 static void
262 ansi_map_stat_gtk_win_create(
263     ansi_map_stat_dlg_t *dlg_p,
264     char                *title)
265 {
266 #define INIT_TABLE_NUM_COLUMNS  3
267     char                *default_titles[] = { "Op Code", "Operation Name", "Count" };
268     int                 i;
269     column_arrows       *col_arrows;
270     GdkBitmap           *ascend_bm, *descend_bm;
271     GdkPixmap           *ascend_pm, *descend_pm;
272     GtkStyle            *win_style;
273     GtkWidget           *column_lb;
274     GtkWidget           *vbox;
275     GtkWidget           *bt_close;
276     GtkWidget           *hbuttonbox;
277     GtkWidget           *dialog_vbox;
278     GtkWidget           *dialog_action_area;
279
280
281     dlg_p->win = gtk_dialog_new();
282     gtk_window_set_default_size(GTK_WINDOW(dlg_p->win), 500, 450);
283     gtk_window_set_title(GTK_WINDOW(dlg_p->win), title);
284     SIGNAL_CONNECT(dlg_p->win, "destroy", ansi_map_stat_gtk_win_destroy_cb, dlg_p);
285
286     dialog_vbox = GTK_DIALOG(dlg_p->win)->vbox;
287     gtk_widget_show(dialog_vbox);
288
289     dialog_action_area = GTK_DIALOG(dlg_p->win)->action_area;
290     gtk_widget_show(dialog_action_area);
291     gtk_container_set_border_width(GTK_CONTAINER(dialog_action_area), 10);
292
293     hbuttonbox = gtk_hbutton_box_new();
294     gtk_widget_ref(hbuttonbox);
295     OBJECT_SET_DATA_FULL(dlg_p->win, "hbuttonbox", hbuttonbox,
296                          gtk_widget_unref);
297     gtk_widget_show(hbuttonbox);
298     gtk_box_pack_start(GTK_BOX(dialog_action_area), hbuttonbox, FALSE, FALSE, 0);
299     gtk_button_box_set_layout(GTK_BUTTON_BOX(hbuttonbox), GTK_BUTTONBOX_END);
300     gtk_button_box_set_spacing(GTK_BUTTON_BOX(hbuttonbox), 0);
301
302     bt_close = gtk_button_new_with_label("Close");
303     gtk_widget_ref(bt_close);
304     OBJECT_SET_DATA_FULL(dlg_p->win, "bt_close", bt_close, gtk_widget_unref);
305     gtk_widget_show(bt_close);
306     gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_close);
307     GTK_WIDGET_SET_FLAGS(bt_close, GTK_CAN_DEFAULT);
308     SIGNAL_CONNECT(bt_close, "clicked", ansi_map_stat_gtk_dlg_close_cb, dlg_p);
309
310     vbox = gtk_vbox_new(FALSE, 0);
311     gtk_widget_ref(vbox);
312     OBJECT_SET_DATA_FULL(dlg_p->win, "vbox", vbox, gtk_widget_unref);
313     gtk_widget_show(vbox);
314     gtk_box_pack_start(GTK_BOX(dialog_vbox), vbox, TRUE, TRUE, 0);
315     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
316
317     dlg_p->scrolled_win = gtk_scrolled_window_new(NULL, NULL);
318     gtk_widget_ref(dlg_p->scrolled_win);
319     OBJECT_SET_DATA_FULL(dlg_p->win, "scrolled_win", dlg_p->scrolled_win,
320                          gtk_widget_unref);
321     gtk_widget_show(dlg_p->scrolled_win);
322     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dlg_p->scrolled_win),
323         GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
324     gtk_box_pack_start(GTK_BOX(vbox), dlg_p->scrolled_win, TRUE, TRUE, 0);
325
326     dlg_p->table = gtk_clist_new(INIT_TABLE_NUM_COLUMNS);
327     gtk_widget_ref(dlg_p->table);
328     OBJECT_SET_DATA_FULL(dlg_p->win, "table", dlg_p->table, gtk_widget_unref);
329     gtk_widget_show(dlg_p->table);
330
331     gtk_widget_show(dlg_p->win);
332
333     col_arrows =
334         (column_arrows *) g_malloc(sizeof(column_arrows) * INIT_TABLE_NUM_COLUMNS);
335
336     win_style =
337         gtk_widget_get_style(dlg_p->scrolled_win);
338
339     ascend_pm =
340         gdk_pixmap_create_from_xpm_d(dlg_p->scrolled_win->window,
341             &ascend_bm,
342             &win_style->bg[GTK_STATE_NORMAL],
343             (gchar **) clist_ascend_xpm);
344
345     descend_pm =
346         gdk_pixmap_create_from_xpm_d(dlg_p->scrolled_win->window,
347             &descend_bm,
348             &win_style->bg[GTK_STATE_NORMAL],
349             (gchar **)clist_descend_xpm);
350
351     for (i = 0; i < INIT_TABLE_NUM_COLUMNS; i++)
352     {
353         col_arrows[i].table = gtk_table_new(2, 2, FALSE);
354
355         gtk_table_set_col_spacings(GTK_TABLE(col_arrows[i].table), 5);
356
357         column_lb = gtk_label_new(default_titles[i]);
358
359         gtk_table_attach(GTK_TABLE(col_arrows[i].table), column_lb,
360             0, 1, 0, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
361
362         gtk_widget_show(column_lb);
363
364         col_arrows[i].ascend_pm =
365             gtk_pixmap_new(ascend_pm, ascend_bm);
366
367         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].ascend_pm,
368             1, 2, 1, 2, GTK_SHRINK, GTK_SHRINK, 0, 0);
369
370         col_arrows[i].descend_pm =
371             gtk_pixmap_new(descend_pm, descend_bm);
372
373         gtk_table_attach(GTK_TABLE(col_arrows[i].table), col_arrows[i].descend_pm,
374             1, 2, 0, 1, GTK_SHRINK, GTK_SHRINK, 0, 0);
375
376         if (i == 0)
377         {
378             /* default column sorting */
379             gtk_widget_show(col_arrows[i].ascend_pm);
380         }
381
382         gtk_clist_set_column_widget(GTK_CLIST(dlg_p->table), i, col_arrows[i].table);
383         gtk_widget_show(col_arrows[i].table);
384     }
385     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
386
387     gtk_clist_set_compare_func(GTK_CLIST(dlg_p->table), ansi_map_stat_gtk_sort_column);
388     gtk_clist_set_sort_column(GTK_CLIST(dlg_p->table), 0);
389     gtk_clist_set_sort_type(GTK_CLIST(dlg_p->table), GTK_SORT_ASCENDING);
390
391     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 0, 60);
392     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 1, 290);
393     gtk_clist_set_column_width(GTK_CLIST(dlg_p->table), 2, 50);
394
395     gtk_clist_set_shadow_type(GTK_CLIST(dlg_p->table), GTK_SHADOW_IN);
396     gtk_clist_column_titles_show(GTK_CLIST(dlg_p->table));
397     gtk_container_add(GTK_CONTAINER(dlg_p->scrolled_win), dlg_p->table);
398
399     SIGNAL_CONNECT(dlg_p->table, "click-column", ansi_map_stat_gtk_click_column_cb, col_arrows);
400 }
401
402
403 /*
404  * Never gets called ?
405  */
406 static void
407 ansi_map_stat_gtk_init(
408     char                *optarg)
409 {
410     /* does not appear to be called */
411
412     optarg = optarg;
413 }
414
415
416 static void
417 ansi_map_stat_gtk_cb(
418     GtkWidget           *w _U_,
419     gpointer            d _U_)
420 {
421     int                 i;
422     char                str[100];
423
424
425     /*
426      * if the window is already open, bring it to front
427      */
428     if (dlg.win)
429     {
430         gdk_window_raise(dlg.win->window);
431         return;
432     }
433
434     ansi_map_stat_gtk_win_create(&dlg, "ANSI MAP Operation Statistics");
435
436     i = 0;
437     while (ansi_map_opr_code_strings[i].strptr)
438     {
439         sprintf(str, "0x%02x", ansi_map_opr_code_strings[i].value);
440         dlg.entries[0] = g_strdup(str);
441
442         dlg.entries[1] = g_strdup(ansi_map_opr_code_strings[i].strptr);
443
444         dlg.entries[2] = g_strdup("0");
445
446         gtk_clist_insert(GTK_CLIST(dlg.table), i, dlg.entries);
447         gtk_clist_set_row_data(GTK_CLIST(dlg.table), i, (gpointer) i);
448
449         i++;
450     }
451
452     ansi_map_stat_draw(NULL);
453 }
454
455
456 void
457 register_tap_listener_gtkansi_map_stat(void)
458 {
459     GString             *err_p;
460
461
462     register_ethereal_tap("ansi_map,", ansi_map_stat_gtk_init);
463
464     memset((void *) &stat, 0, sizeof(ansi_map_stat_t));
465
466     err_p =
467         register_tap_listener("ansi_map", NULL, NULL,
468             ansi_map_stat_reset,
469             ansi_map_stat_packet,
470             ansi_map_stat_draw);
471
472     if (err_p != NULL)
473     {
474         simple_dialog(ESD_TYPE_WARN, NULL, err_p->str);
475         g_string_free(err_p, TRUE);
476
477         exit(1);
478     }
479 }
480
481
482 void
483 register_tap_menu_gtkansi_map_stat(void)
484 {
485     register_tap_menu_item("Statistics/ANSI MAP Operation", ansi_map_stat_gtk_cb, NULL, NULL, NULL);
486 }