ui_main_screen.c 5.31 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1
2
3
4
5
6
#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdint.h>
winckel's avatar
winckel committed
7
#include <stdlib.h>
Cedric Roux's avatar
 
Cedric Roux committed
8
#include <string.h>
winckel's avatar
winckel committed
9
#include <unistd.h>
Cedric Roux's avatar
 
Cedric Roux committed
10
11
12

#include <gtk/gtk.h>

13
#include "rc.h"
Cedric Roux's avatar
 
Cedric Roux committed
14
15
16
17
18
19
20

#include "ui_interface.h"
#include "ui_main_screen.h"
#include "ui_menu_bar.h"
#include "ui_tree_view.h"
#include "ui_notebook.h"
#include "ui_notifications.h"
winckel's avatar
winckel committed
21
#include "ui_filters.h"
Cedric Roux's avatar
 
Cedric Roux committed
22
23
24

ui_main_data_t ui_main_data;

winckel's avatar
winckel committed
25
26
27
28
static void ui_help(void)
{
    printf ("Usage: itti_analyser [options]\n\n"
            "Options:\n"
29
            "  -d DISSECT   write DISSECT file with message types parse details\n"
winckel's avatar
winckel committed
30
31
32
            "  -f FILTERS   read filters from FILTERS file\n"
            "  -h           display this help and exit\n"
            "  -i IP        set ip address to IP\n"
33
            "  -l LEVEL     set log level to LEVEL in the range of 2 to 7\n"
winckel's avatar
winckel committed
34
35
36
37
38
39
40
41
            "  -m MESSAGES  read messages from MESSAGES file\n"
            "  -p PORT      set port to PORT\n");
}

static void ui_gtk_parse_arg(int argc, char *argv[])
{
    char c;

42
    while ((c = getopt (argc, argv, "d:f:hi:l:m:p:")) != -1)
winckel's avatar
winckel committed
43
44
45
    {
        switch (c)
        {
46
47
48
49
50
51
52
53
            case 'd':
                ui_main_data.dissect_file_name = malloc (strlen (optarg) + 1);
                if (ui_main_data.dissect_file_name != NULL)
                {
                    strcpy (ui_main_data.dissect_file_name, optarg);
                }
                break;

winckel's avatar
winckel committed
54
            case 'f':
55
56
57
58
59
                ui_main_data.filters_file_name = malloc (strlen (optarg) + 1);
                if (ui_main_data.filters_file_name != NULL)
                {
                    strcpy (ui_main_data.filters_file_name, optarg);
                }
winckel's avatar
winckel committed
60
61
62
                break;

            case 'h':
63
64
                ui_help ();
                exit (0);
winckel's avatar
winckel committed
65
66
67
68
69
70
                break;

            case 'i':
                ui_main_data.ip_entry_init = optarg;
                break;

71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
            case 'l':
            {
                GLogLevelFlags log_flag;

                log_flag = 1 << atoi(optarg);
                if (log_flag < G_LOG_LEVEL_ERROR)
                {
                    log_flag = G_LOG_LEVEL_ERROR;
                }
                else
                {
                    if (log_flag > G_LOG_LEVEL_DEBUG)
                    {
                        log_flag = G_LOG_LEVEL_DEBUG;
                    }
                }
                ui_main_data.log_flags = ((log_flag << 1) - 1) & G_LOG_LEVEL_MASK;
                break;
            }

winckel's avatar
winckel committed
91
            case 'm':
92
93
94
95
96
                ui_main_data.messages_file_name = malloc (strlen (optarg) + 1);
                if (ui_main_data.messages_file_name != NULL)
                {
                    strcpy (ui_main_data.messages_file_name, optarg);
                }
winckel's avatar
winckel committed
97
98
99
                break;

            case 'p':
100
101
                ui_main_data.port_entry_init = optarg;
                break;
winckel's avatar
winckel committed
102

103
            default:
winckel's avatar
winckel committed
104
105
106
107
108
109
110
                ui_help ();
                exit (-1);
                break;
        }
    }
}

111
112
113
114
115
116
117
static int ui_idle_callback(gpointer data)
{
    g_debug("Entering idle state");

    /* Read filters file */
    if (ui_main_data.filters_file_name != NULL)
    {
118
        ui_filters_read(ui_main_data.filters_file_name);
119
120
121
122
123
124
125
126
127
128
129
130
    }

    /* Read messages file */
    if (ui_main_data.messages_file_name != NULL)
    {
        ui_messages_read (ui_main_data.messages_file_name);
    }

    /* One shot execution */
    return FALSE;
}

131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
void ui_set_title(const char *fmt, ...)
{
    va_list args;
    char *name;
    char buffer[200];
    char title[220];

#if defined(PACKAGE_STRING)
    name = PACKAGE_NAME;
#else
    name = "itti_analyzer";
#endif

    va_start(args, fmt);

    vsnprintf (buffer, sizeof(buffer), fmt, args);
    snprintf (title, sizeof(title), "%s  %s", name, buffer);

    gtk_window_set_title (GTK_WINDOW(ui_main_data.window), title);
}

Cedric Roux's avatar
 
Cedric Roux committed
152
153
154
155
int ui_gtk_initialize(int argc, char *argv[])
{
    GtkWidget *vbox;

156
    memset (&ui_main_data, 0, sizeof(ui_main_data_t));
Cedric Roux's avatar
 
Cedric Roux committed
157

158
    /* Set some default initialization value for the IP address */
winckel's avatar
winckel committed
159
160
161
    ui_main_data.ip_entry_init = "127.0.0.1";
    ui_main_data.port_entry_init = "10007";

162
163
164
    /* Set default log level to all but debug message */
    ui_main_data.log_flags = (G_LOG_LEVEL_MASK & (~G_LOG_LEVEL_DEBUG));

165
    ui_gtk_parse_arg (argc, argv);
winckel's avatar
winckel committed
166

Cedric Roux's avatar
 
Cedric Roux committed
167
    /* Create the main window */
168
169
    ui_main_data.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    ui_init_filters (TRUE, FALSE);
Cedric Roux's avatar
 
Cedric Roux committed
170

171
172
    gtk_window_set_default_icon_name (GTK_STOCK_FIND);

173
174
    gtk_window_set_position (GTK_WINDOW(ui_main_data.window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size (GTK_WINDOW(ui_main_data.window), 1024, 800);
175
    ui_set_title("");
176
    gtk_window_set_resizable (GTK_WINDOW(ui_main_data.window), TRUE);
Cedric Roux's avatar
 
Cedric Roux committed
177

178
    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
Cedric Roux's avatar
 
Cedric Roux committed
179
180
181
182
183
184

    CHECK_FCT(ui_menu_bar_create(vbox));
    CHECK_FCT(ui_toolbar_create(vbox));
//     CHECK_FCT(ui_tree_view_create(ui_main_data.window, vbox));
    CHECK_FCT(ui_notebook_create(vbox));

185
186
    gtk_container_add (GTK_CONTAINER(ui_main_data.window), vbox);

Cedric Roux's avatar
 
Cedric Roux committed
187
188
189
190
191
192
    /* Assign the destroy event */
    g_signal_connect(ui_main_data.window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

    /* Show the application window */
    gtk_widget_show_all (ui_main_data.window);

193
    g_idle_add (ui_idle_callback, NULL);
winckel's avatar
winckel committed
194

Cedric Roux's avatar
 
Cedric Roux committed
195
196
    return RC_OK;
}
197
198
199
200
201
202
203
204

void ui_gtk_flush_events(void)
{
    while (gtk_events_pending ())
    {
        gtk_main_iteration();
    }
}