2 *------------------------------------------------------------------
3 * Copyright (c) 2005-2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
28 event_def_t g_eventdefs[NEVENTS];
33 static GtkWidget *s_pointselbox;
35 static FILE *s_elog_hfp;
37 static GtkWidget *s_event_buttons[NEVENTS];
38 static int s_min_shown_pointsel;
39 static int s_max_shown_pointsel;
40 static GtkWidget *s_allbutton;
41 static GtkWidget *s_nonebutton;
42 static GtkWidget *s_pointselbuttons;
43 static GtkWidget *s_ps_vscroll;
44 static GtkObject *s_ps_vsadj;
45 static int g_neventdefs;
57 /****************************************************************************
58 * recompute_vscrollbar
59 ****************************************************************************/
61 static void recompute_ps_vscrollbar (void)
66 adj = GTK_ADJUSTMENT(s_ps_vsadj);
69 /* This seems like the right calculation, but seems not to work */
70 if (g_neventdefs > c_maxpointsel)
71 limit = g_neventdefs - c_maxpointsel;
75 limit = g_neventdefs-1;
78 adj->lower = (gfloat)0.00;
79 adj->upper = (gfloat)limit;
80 adj->value = (gfloat)0.00;
81 adj->step_increment = (gfloat)1.00;
82 adj->page_increment = (gfloat)(c_maxpointsel / 3);
83 adj->page_size = (gfloat)c_maxpointsel;
84 gtk_adjustment_changed(adj);
85 gtk_adjustment_value_changed(adj);
86 gtk_widget_show(s_ps_vscroll);
89 /****************************************************************************
90 * point_select_callback
91 ****************************************************************************/
93 static void point_select_callback(GtkToggleButton *item, gpointer data)
95 int i = (int) (unsigned long long) data;
97 g_eventdefs[i].selected = gtk_toggle_button_get_active(
98 GTK_TOGGLE_BUTTON(s_event_buttons[i]));
99 view1_display_when_idle();
102 /****************************************************************************
104 ****************************************************************************/
106 static void up_button(void)
109 int increment = c_maxpointsel/4;
111 if (s_min_shown_pointsel == 0)
114 s_min_shown_pointsel -= increment;
116 if (s_min_shown_pointsel < 0)
117 s_min_shown_pointsel = 0;
119 s_max_shown_pointsel = s_min_shown_pointsel + c_maxpointsel;
121 for (i = 0; i < g_neventdefs; i++) {
122 if (i >= s_min_shown_pointsel &&
123 i <= s_max_shown_pointsel)
124 gtk_widget_show(s_event_buttons[i]);
126 gtk_widget_hide(s_event_buttons[i]);
132 /****************************************************************************
134 ****************************************************************************/
136 static void down_button(void)
139 int increment = c_maxpointsel/4;
141 if (s_max_shown_pointsel == g_neventdefs)
144 s_max_shown_pointsel += increment;
146 if (s_max_shown_pointsel >= g_neventdefs)
147 s_max_shown_pointsel = (g_neventdefs-1);
149 s_min_shown_pointsel = s_max_shown_pointsel - c_maxpointsel;
151 if (s_min_shown_pointsel < 0)
152 s_min_shown_pointsel = 0;
154 for (i = 0; i < g_neventdefs; i++) {
155 if (i >= s_min_shown_pointsel &&
156 i <= s_max_shown_pointsel)
157 gtk_widget_show(s_event_buttons[i]);
159 gtk_widget_hide(s_event_buttons[i]);
165 /****************************************************************************
166 * button_click_callback
167 ****************************************************************************/
169 static void button_click_callback(GtkButton *item, gpointer data)
172 enum button_click click = (enum button_click)data;
176 for (i = 0; i < g_neventdefs; i++) {
177 gtk_toggle_button_set_active (
178 GTK_TOGGLE_BUTTON(s_event_buttons[i]), TRUE);
179 g_eventdefs[i].selected = TRUE;
184 for (i = 0; i < g_neventdefs; i++) {
185 gtk_toggle_button_set_active (
186 GTK_TOGGLE_BUTTON(s_event_buttons[i]), FALSE);
187 g_eventdefs[i].selected = FALSE;
193 /****************************************************************************
195 ****************************************************************************/
197 static void scroll_callback (GtkAdjustment *adj, GtkWidget *notused)
201 s_min_shown_pointsel = (int)adj->value;
202 s_max_shown_pointsel = s_min_shown_pointsel + c_maxpointsel;
204 for (i = 0; i < g_neventdefs; i++) {
205 if (i >= s_min_shown_pointsel &&
206 i <= s_max_shown_pointsel)
207 gtk_widget_show(s_event_buttons[i]);
209 gtk_widget_hide(s_event_buttons[i]);
213 /****************************************************************************
214 * point_selector_init
215 ****************************************************************************/
217 void point_selector_init(void)
220 c_maxpointsel = atol(getprop_default("event_selector_lines", "20"));
222 s_pointselbox = gtk_vbox_new(FALSE,5);
224 s_pointselbuttons = gtk_hbox_new(FALSE,5);
226 s_allbutton = gtk_button_new_with_label("ALL");
227 gtk_widget_show(s_allbutton);
228 s_nonebutton = gtk_button_new_with_label("NONE");
229 gtk_widget_show(s_nonebutton);
231 gtk_signal_connect (GTK_OBJECT(s_allbutton), "clicked",
232 GTK_SIGNAL_FUNC(button_click_callback),
233 (gpointer) ALL_BUTTON);
235 gtk_signal_connect (GTK_OBJECT(s_nonebutton), "clicked",
236 GTK_SIGNAL_FUNC(button_click_callback),
237 (gpointer) NONE_BUTTON);
239 gtk_box_pack_start(GTK_BOX(s_pointselbuttons), s_allbutton, FALSE,
241 gtk_box_pack_start(GTK_BOX(s_pointselbuttons), s_nonebutton, FALSE,
244 gtk_widget_show(s_pointselbuttons);
245 gtk_widget_ref(s_pointselbuttons);
247 gtk_box_pack_start(GTK_BOX(s_pointselbox), s_pointselbuttons, FALSE,
250 gtk_box_pack_end (GTK_BOX(g_mainhbox), s_pointselbox,
253 s_ps_vsadj = gtk_adjustment_new(0.0 /* initial value */,
254 0.0 /* minimum value */,
255 2000.0 /* maximum value */,
256 0.1 /* step increment */,
257 10.0/* page increment */,
258 10.0/* page size */);
260 s_ps_vscroll = gtk_vscrollbar_new (GTK_ADJUSTMENT(s_ps_vsadj));
261 gtk_signal_connect (GTK_OBJECT (s_ps_vsadj), "value-changed",
262 GTK_SIGNAL_FUNC (scroll_callback),
263 (gpointer)s_ps_vscroll);
264 gtk_box_pack_end(GTK_BOX(g_mainhbox), s_ps_vscroll, FALSE, FALSE, 0);
267 /****************************************************************************
269 ****************************************************************************/
271 char *sxerox (char *s)
275 /* Note: g_malloc does or dies... */
276 rv = (char *)g_malloc(strlen(s)+1);
281 /****************************************************************************
282 * reset_point_selector
283 ****************************************************************************/
285 static void reset_point_selector(void)
289 gtk_widget_hide(s_pointselbox);
290 gtk_widget_hide(s_pointselbuttons);
291 gtk_widget_hide(s_ps_vscroll);
292 gtk_container_remove(GTK_CONTAINER(s_pointselbox),
295 for (i = 0; i < g_neventdefs; i++) {
296 if (s_event_buttons[i]) {
297 gtk_container_remove(GTK_CONTAINER(s_pointselbox),
299 s_event_buttons[i] = 0;
304 /****************************************************************************
305 * create_point_selector
306 ****************************************************************************/
308 static void create_point_selector(void)
315 for (i = 0; i < g_neventdefs; i++) {
316 ep = &g_eventdefs[i];
317 snprintf(tmpbuf, sizeof(tmpbuf), "[%lu] %s", ep->event,
318 ep->name ? ep->name : "(none)");
319 /* Hack to reduce width of point selectors */
320 if (strlen(tmpbuf) > 50) {
324 wp = gtk_check_button_new_with_label (tmpbuf);
325 s_event_buttons[i] = wp;
326 gtk_signal_connect (GTK_OBJECT(wp), "toggled",
327 GTK_SIGNAL_FUNC(point_select_callback),
328 (gpointer) (unsigned long long) i);
329 gtk_toggle_button_set_active (
330 GTK_TOGGLE_BUTTON(wp), TRUE);
331 gtk_box_pack_start(GTK_BOX(s_pointselbox), wp, FALSE, FALSE, 0);
334 /* set up scroll parameters by faking an up-button */
335 s_min_shown_pointsel = 1;
338 gtk_box_pack_start(GTK_BOX(s_pointselbox), s_pointselbuttons, FALSE,
340 gtk_widget_show(s_pointselbuttons);
341 gtk_widget_show(s_pointselbox);
342 gtk_widget_show(s_ps_vscroll);
345 /****************************************************************************
347 ****************************************************************************/
349 static void remove_all_events(void)
354 for (i = 0; i < g_neventdefs; i++) {
355 ep = &g_eventdefs[i];
366 /****************************************************************************
368 ****************************************************************************/
370 static void add_event(ulong event, char *name, char *format)
375 if (g_neventdefs >= NEVENTS) {
376 g_error("Too many event definitions, increase NEVENTS!");
380 /* Simple dup check, probably not needed very often */
381 for (i = 0; i < g_neventdefs; i++) {
382 if (g_eventdefs[i].event == event) {
383 g_warning("Duplicate def event %lu: first definition retained\n",
389 ep = &g_eventdefs[g_neventdefs++];
392 ep->name = sxerox(name);
393 ep->format = sxerox(format);
397 /****************************************************************************
398 * add_event_from_cpel_file
399 ****************************************************************************/
401 void add_event_from_cpel_file(ulong event, char *event_format,
406 if (g_neventdefs >= NEVENTS) {
407 g_error("Too many event definitions, increase NEVENTS!");
411 ep = &g_eventdefs[g_neventdefs++];
415 * Duplicate the strings for backward compatibility. Otherwise,
416 * the g_free above will barf because the name/format strings are
417 * actually in mmap'ed memory
419 ep->name = sxerox(event_format);
420 ep->format = sxerox(datum_format);
424 /****************************************************************************
425 * add_event_from_clib_file
426 ****************************************************************************/
428 void add_event_from_clib_file(unsigned int event, char *name,
429 unsigned int vec_index)
433 if (g_neventdefs >= NEVENTS) {
434 g_error("Too many event definitions, increase NEVENTS!");
438 ep = &g_eventdefs[g_neventdefs++];
442 ep->name = sxerox(name);
443 ep->format = (void *)(unsigned long long) vec_index;
448 /****************************************************************************
449 * read_header_file - eats header file lines of the form
451 * #define EVENT_FOO 123 / * name: %d * /
453 ****************************************************************************/
455 static void read_header_file (void)
463 while (fgets (tmpbuf, sizeof (tmpbuf), s_hfp))
468 if (strncmp (cp, "#define", 7))
472 while (*cp && !(isspace ((int)*cp)))
478 /* skip ws after #define */
479 while (*cp && isspace ((int)*cp))
485 /* skip symbolic name */
486 while (*cp && !(isspace ((int)*cp)))
492 /* skip ws after symbolic name */
493 while (*cp && isspace ((int)*cp))
501 if (!strncmp(cp, "EV_NUM", 6)) {
505 while (*cp && *cp != '(')
513 while (*cp && isspace ((int)*cp))
518 /* eat event code. */
519 while (*cp && isdigit ((int)*cp))
521 event = event * 10 + (*cp - '0');
529 while (*cp && *cp != ')')
537 /* skip ws after event code */
538 while (*cp && isspace ((int)*cp))
551 /* skip ws after comment start */
552 while (*cp && isspace ((int)*cp))
560 /* accumulate name */
561 while (*cp && *cp != ':' && *cp != '*')
569 /* skip ws after name: */
570 while (*cp && isspace ((int)*cp))
573 if (*cp == 0 || *cp == '/')
581 /* accumulate format string */
582 while (*cp && !isspace ((int)*cp))
589 add_event (event, name, format);
593 /****************************************************************************
594 * read_header_files - eats header file lines of the form
596 * #define FILE1_BASE 100 / * pointdefs: ../vpn/vpn_points.h * /
598 ****************************************************************************/
600 static boolean read_header_files (void)
607 while (fgets (tmpbuf, sizeof (tmpbuf), s_elog_hfp))
611 if (strncmp (cp, "#define", 7))
616 /* skip ws after #define */
617 while (*cp && isspace ((int)*cp))
623 /* skip EV_COMPxxx_START */
624 while (*cp && !isspace((int)*cp))
630 /* skip ws after EV_COMPxxx_START */
631 while (*cp && isspace ((int)*cp))
640 while (*cp && (*cp != '/'))
652 /* skip ws after comment start */
653 while (*cp && isspace ((int)*cp))
659 if (strncmp (cp, "pointdefs:", 10))
664 /* skip ws after comment start */
665 while (*cp && isspace ((int)*cp))
670 while (*cp && !isspace ((int)*cp))
675 s_hfp = fopen (name, "rt");
678 g_warning ("Couldn't open header file %s\n", name);
692 /****************************************************************************
694 ****************************************************************************/
696 int event_def_cmp(const void *a1, const void *a2)
698 event_def_t *e1 = (event_def_t *)a1;
699 event_def_t *e2 = (event_def_t *)a2;
701 if (e1->event < e2->event)
703 else if (e1->event == e2->event)
709 /****************************************************************************
710 * sort_event_definitions
711 ****************************************************************************/
713 void sort_event_definitions(void)
715 qsort(&g_eventdefs[0], g_neventdefs, sizeof(event_def_t), event_def_cmp);
718 static boolean remove_needed=TRUE;
720 void finalize_events(void)
722 sort_event_definitions();
723 create_point_selector();
724 recompute_ps_vscrollbar();
725 view1_display_when_idle();
726 remove_needed = TRUE;
729 void initialize_events(void)
732 reset_point_selector();
734 remove_needed = FALSE;
738 /****************************************************************************
739 * read_event_definitions
740 ****************************************************************************/
742 boolean read_event_definitions (char *filename)
748 s_elog_hfp = fopen (filename, "rt");
749 if (s_elog_hfp == NULL) {
750 snprintf (tmpbuf, sizeof(tmpbuf), "Couldn't open %s\n", filename);
751 infobox ("Open Failed", tmpbuf);
754 /* Presume "elog.h". Note fallthrough... */
755 if (read_header_files()) {
756 sort_event_definitions();
757 create_point_selector();
758 recompute_ps_vscrollbar();
760 view1_display_when_idle();
761 remove_needed = TRUE;
766 s_hfp = fopen (filename, "rt");
768 snprintf (tmpbuf, sizeof(tmpbuf), "Couldn't open %s\n", filename);
769 infobox ("Read Event Definition Failure", tmpbuf);
775 /* Happens if the user feeds us the wrong file, for example */
776 if (g_neventdefs == 0) {
777 snprintf (tmpbuf, sizeof(tmpbuf),
778 "No event definitions found in %s\n", filename);
779 infobox ("No Event Definitions?", tmpbuf);
786 static event_def_t dummy_event;
787 static char dummy_string[32];
789 /****************************************************************************
790 * find_event_definition
791 * Binary search for first event whose time is >= t
792 ****************************************************************************/
794 event_def_t *find_event_definition (ulong code)
796 int index, bottom, top;
799 if (g_neventdefs == 0)
802 bottom = g_neventdefs-1;
806 index = (bottom + top) / 2;
808 edp = (g_eventdefs + index);
810 if (edp->event == code)
816 edp->selected = TRUE;
818 edp->format = "0x%x";
819 snprintf (dummy_string, sizeof(dummy_string), "E%lu", code);
820 edp->name = &dummy_string[0];
824 if (edp->event < code)
831 /****************************************************************************
832 * pointsel_next_snapshot
833 * Set dialog buttons from snapshot
834 ****************************************************************************/
836 void pointsel_next_snapshot(void)
840 for (i = 0; i < g_neventdefs; i++) {
841 gtk_toggle_button_set_active (
842 GTK_TOGGLE_BUTTON(s_event_buttons[i]),
843 g_eventdefs[i].selected);
847 /****************************************************************************
849 ****************************************************************************/
851 void pointsel_about (char *tmpbuf)
853 snprintf (tmpbuf+strlen(tmpbuf), 128, "%d event definitions\n",