1999-08-24 16:27:23 +00:00
|
|
|
/* colors.c
|
|
|
|
* Definitions for color structures and routines
|
|
|
|
*
|
2002-11-03 17:38:45 +00:00
|
|
|
* $Id: colors.c,v 1.26 2002/11/03 17:38:32 oabad Exp $
|
1999-08-24 16:27:23 +00:00
|
|
|
*
|
|
|
|
* Ethereal - Network traffic analyzer
|
2001-10-21 21:48:00 +00:00
|
|
|
* By Gerald Combs <gerald@ethereal.com>
|
1999-08-24 16:27:23 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1999-08-24 16:27:23 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1999-08-24 16:27:23 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1999-08-24 16:27:23 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
2001-12-02 00:16:02 +00:00
|
|
|
/*
|
|
|
|
* Updated 1 Dec 10 jjm
|
|
|
|
*/
|
1999-08-24 16:27:23 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2001-04-02 09:53:46 +00:00
|
|
|
#include <epan/filesystem.h>
|
2001-10-23 05:01:02 +00:00
|
|
|
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/packet.h>
|
1999-08-24 16:27:23 +00:00
|
|
|
#include "colors.h"
|
2002-09-23 19:09:52 +00:00
|
|
|
#include "color_utils.h"
|
1999-08-24 16:27:23 +00:00
|
|
|
#include "file.h"
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/dfilter/dfilter.h>
|
2000-01-03 06:59:25 +00:00
|
|
|
#include "simple_dialog.h"
|
2002-11-03 17:38:45 +00:00
|
|
|
#include "gtkglobals.h"
|
1999-08-24 16:27:23 +00:00
|
|
|
|
|
|
|
extern capture_file cf;
|
|
|
|
|
2002-05-27 22:00:37 +00:00
|
|
|
static gboolean read_filters(void);
|
1999-12-19 10:39:35 +00:00
|
|
|
|
1999-12-19 07:28:36 +00:00
|
|
|
GSList *filter_list;
|
1999-12-19 07:01:30 +00:00
|
|
|
|
Make a whole pile of routines private to "colors.c" static, and remove
their declarations from "colors.h". Do the same with some variables -
and remove the ones that aren't used any more.
Make some name changes suggested in the comments in "colors.c".
Make "color_delete_cb()" take, as the user-supplied argument,
"cf->colors", just as "color_new_cb()" does, and make it use its
argument to find the color filter, just as "color_new_cb()" does, rather
than using "cf.colors".
When there are no colors in the list, make the "Edit" button
insensitive; make it sensitive if there are any colors in the list, so
that we don't crash if you press "Edit" when there are no colors in the
list.
Make "remember_selected_row()" take, as the user-supplied argument,
"cf->colors", rather than "cf" - it needs only the color filter, not the
entire "capture_file" structure.
When using "gtk_clist_select_row()" to forcibly select a row, pass -1 as
the column number, to indicate that we don't care which column is
selected.
Fix some typos in function names.
svn path=/trunk/; revision=1161
1999-11-30 04:23:06 +00:00
|
|
|
static GdkColormap* sys_cmap;
|
|
|
|
static GdkColormap* our_cmap = NULL;
|
1999-08-24 16:27:23 +00:00
|
|
|
|
1999-10-05 04:34:00 +00:00
|
|
|
GdkColor WHITE = { 0, 65535, 65535, 65535 };
|
|
|
|
GdkColor BLACK = { 0, 0, 0, 0 };
|
1999-08-24 16:27:23 +00:00
|
|
|
|
2001-12-02 00:16:02 +00:00
|
|
|
/* Initialize the filter structures (reading from file) */
|
2002-05-27 22:00:37 +00:00
|
|
|
void
|
|
|
|
colfilter_init(void)
|
1999-08-24 16:27:23 +00:00
|
|
|
{
|
2002-01-08 21:35:17 +00:00
|
|
|
gboolean got_white, got_black;
|
|
|
|
|
|
|
|
sys_cmap = gdk_colormap_get_system();
|
|
|
|
|
|
|
|
/* Allocate "constant" colors. */
|
|
|
|
got_white = get_color(&WHITE);
|
|
|
|
got_black = get_color(&BLACK);
|
|
|
|
|
|
|
|
/* Got milk? */
|
|
|
|
if (!got_white) {
|
|
|
|
if (!got_black)
|
|
|
|
simple_dialog(ESD_TYPE_WARN, NULL,
|
|
|
|
"Could not allocate colors black or white.");
|
|
|
|
else
|
|
|
|
simple_dialog(ESD_TYPE_WARN, NULL,
|
|
|
|
"Could not allocate color white.");
|
|
|
|
} else {
|
|
|
|
if (!got_black)
|
|
|
|
simple_dialog(ESD_TYPE_WARN, NULL,
|
|
|
|
"Could not allocate color black.");
|
|
|
|
}
|
|
|
|
|
2002-05-27 22:00:37 +00:00
|
|
|
read_filters();
|
1999-08-24 16:27:23 +00:00
|
|
|
}
|
|
|
|
|
2001-12-02 00:16:02 +00:00
|
|
|
/* Create a new filter */
|
2000-02-12 08:31:49 +00:00
|
|
|
color_filter_t *
|
2002-05-27 22:00:37 +00:00
|
|
|
new_color_filter(gchar *name, /* The name of the filter to create */
|
2001-12-02 00:16:02 +00:00
|
|
|
gchar *filter_string) /* The string representing the filter */
|
1999-08-24 16:27:23 +00:00
|
|
|
{
|
|
|
|
color_filter_t *colorf;
|
2002-11-03 17:38:45 +00:00
|
|
|
GtkStyle *style;
|
1999-08-24 16:27:23 +00:00
|
|
|
|
|
|
|
colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
|
1999-12-19 09:22:19 +00:00
|
|
|
colorf->filter_name = g_strdup(name);
|
|
|
|
colorf->filter_text = g_strdup(filter_string);
|
2002-11-03 17:38:45 +00:00
|
|
|
style = gtk_widget_get_style(packet_list);
|
|
|
|
gdkcolor_to_color_t(&colorf->bg_color, &style->base[GTK_STATE_NORMAL]);
|
|
|
|
gdkcolor_to_color_t(&colorf->fg_color, &style->text[GTK_STATE_NORMAL]);
|
1999-08-24 16:27:23 +00:00
|
|
|
colorf->c_colorfilter = NULL;
|
1999-12-19 10:39:35 +00:00
|
|
|
colorf->edit_dialog = NULL;
|
1999-12-19 07:01:30 +00:00
|
|
|
filter_list = g_slist_append(filter_list, colorf);
|
1999-12-19 09:22:19 +00:00
|
|
|
return colorf;
|
1999-08-24 16:27:23 +00:00
|
|
|
}
|
|
|
|
|
2001-12-02 00:16:02 +00:00
|
|
|
/* delete the specified filter */
|
2002-01-08 21:35:17 +00:00
|
|
|
void
|
1999-12-19 07:01:30 +00:00
|
|
|
delete_color_filter(color_filter_t *colorf)
|
|
|
|
{
|
|
|
|
if (colorf->filter_name != NULL)
|
2002-01-08 21:35:17 +00:00
|
|
|
g_free(colorf->filter_name);
|
1999-12-19 07:01:30 +00:00
|
|
|
if (colorf->filter_text != NULL)
|
2002-01-08 21:35:17 +00:00
|
|
|
g_free(colorf->filter_text);
|
1999-12-19 07:01:30 +00:00
|
|
|
if (colorf->c_colorfilter != NULL)
|
2002-01-08 21:35:17 +00:00
|
|
|
dfilter_free(colorf->c_colorfilter);
|
1999-12-19 07:01:30 +00:00
|
|
|
filter_list = g_slist_remove(filter_list, colorf);
|
|
|
|
g_free(colorf);
|
|
|
|
}
|
|
|
|
|
2001-12-18 19:09:08 +00:00
|
|
|
static void
|
|
|
|
prime_edt(gpointer data, gpointer user_data)
|
|
|
|
{
|
2002-01-08 21:35:17 +00:00
|
|
|
color_filter_t *colorf = data;
|
|
|
|
epan_dissect_t *edt = user_data;
|
2001-12-18 19:09:08 +00:00
|
|
|
|
2002-01-08 21:35:17 +00:00
|
|
|
if (colorf->c_colorfilter != NULL)
|
|
|
|
epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
|
2002-08-28 21:04:11 +00:00
|
|
|
}
|
2001-12-18 19:09:08 +00:00
|
|
|
|
|
|
|
/* Prime the epan_dissect_t with all the compiler
|
|
|
|
* color filters in 'filter_list'. */
|
|
|
|
void
|
|
|
|
filter_list_prime_edt(epan_dissect_t *edt)
|
|
|
|
{
|
2002-01-08 21:35:17 +00:00
|
|
|
g_slist_foreach(filter_list, prime_edt, edt);
|
2001-12-18 19:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-02 00:16:02 +00:00
|
|
|
/* read filters from the file */
|
1999-08-24 16:27:23 +00:00
|
|
|
static gboolean
|
2002-05-27 22:00:37 +00:00
|
|
|
read_filters(void)
|
1999-08-24 16:27:23 +00:00
|
|
|
{
|
|
|
|
/* TODO: Lots more syntax checking on the file */
|
|
|
|
/* I hate these fixed length names! TODO: make more dynamic */
|
2001-12-02 00:16:02 +00:00
|
|
|
/* XXX - buffer overflow possibility here
|
2002-01-08 21:35:17 +00:00
|
|
|
* sscanf blocks max size of name and filter_exp; buf is used for
|
|
|
|
* reading only */
|
1999-11-30 05:33:05 +00:00
|
|
|
gchar name[256],filter_exp[256], buf[1024];
|
1999-08-24 16:27:23 +00:00
|
|
|
guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
|
|
|
|
GdkColor fg_color, bg_color;
|
1999-12-19 09:22:19 +00:00
|
|
|
color_filter_t *colorf;
|
2002-07-29 06:30:01 +00:00
|
|
|
gchar *path;
|
1999-08-24 16:27:23 +00:00
|
|
|
FILE *f;
|
2001-02-01 20:21:25 +00:00
|
|
|
dfilter_t *temp_dfilter;
|
1999-10-05 04:34:00 +00:00
|
|
|
|
1999-08-24 16:27:23 +00:00
|
|
|
/* decide what file to open (from dfilter code) */
|
2001-10-24 07:18:39 +00:00
|
|
|
path = get_persconffile_path("colorfilters", FALSE);
|
1999-08-24 16:27:23 +00:00
|
|
|
if ((f = fopen(path, "r")) == NULL) {
|
2002-01-08 21:35:17 +00:00
|
|
|
if (errno != ENOENT) {
|
|
|
|
simple_dialog(ESD_TYPE_CRIT, NULL,
|
|
|
|
"Could not open filter file\n\"%s\": %s.", path,
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2002-07-29 06:30:01 +00:00
|
|
|
g_free((gchar *)path);
|
2002-01-08 21:35:17 +00:00
|
|
|
return FALSE;
|
1999-08-24 16:27:23 +00:00
|
|
|
}
|
2002-07-29 06:30:01 +00:00
|
|
|
g_free((gchar *)path);
|
|
|
|
path = NULL;
|
1999-08-24 16:27:23 +00:00
|
|
|
|
2002-01-08 21:35:17 +00:00
|
|
|
do {
|
|
|
|
if (fgets(buf,sizeof buf, f) == NULL)
|
|
|
|
break;
|
2002-08-28 21:04:11 +00:00
|
|
|
|
2002-01-08 21:35:17 +00:00
|
|
|
if (strspn(buf," \t") == (size_t)((strchr(buf,'*') - buf))) {
|
|
|
|
/* leading # comment */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we get the @ delimiter. It is not in any strings
|
|
|
|
* Format is:
|
|
|
|
* @name@filter expression@[background r,g,b][foreground r,g,b]
|
|
|
|
*/
|
|
|
|
if (sscanf(buf," @%256[^@]@%256[^@]@[%hu,%hu,%hu][%hu,%hu,%hu]",
|
|
|
|
name, filter_exp, &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b)
|
|
|
|
== 8) {
|
|
|
|
/* we got a filter */
|
|
|
|
|
|
|
|
if (!dfilter_compile(filter_exp, &temp_dfilter)) {
|
|
|
|
simple_dialog(ESD_TYPE_CRIT, NULL,
|
|
|
|
"Could not compile color filter %s from saved filters.\n%s",
|
|
|
|
name, dfilter_error_msg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!get_color(&fg_color)) {
|
|
|
|
/* oops */
|
|
|
|
simple_dialog(ESD_TYPE_CRIT, NULL,
|
|
|
|
"Could not allocate foreground color "
|
|
|
|
"specified in input file for %s.", name);
|
|
|
|
dfilter_free(temp_dfilter);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!get_color(&bg_color)) {
|
|
|
|
/* oops */
|
|
|
|
simple_dialog(ESD_TYPE_CRIT, NULL,
|
|
|
|
"Could not allocate background color "
|
|
|
|
"specified in input file for %s.", name);
|
|
|
|
dfilter_free(temp_dfilter);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-05-27 22:00:37 +00:00
|
|
|
colorf = new_color_filter(name, filter_exp);
|
2002-01-08 21:35:17 +00:00
|
|
|
colorf->c_colorfilter = temp_dfilter;
|
|
|
|
fg_color.red = fg_r;
|
|
|
|
fg_color.green = fg_g;
|
|
|
|
fg_color.blue = fg_b;
|
|
|
|
bg_color.red = bg_r;
|
|
|
|
bg_color.green = bg_g;
|
|
|
|
bg_color.blue = bg_b;
|
|
|
|
|
2002-09-23 19:09:52 +00:00
|
|
|
gdkcolor_to_color_t(&colorf->bg_color, &bg_color);
|
|
|
|
gdkcolor_to_color_t(&colorf->fg_color, &fg_color);
|
2002-01-08 21:35:17 +00:00
|
|
|
} /* if sscanf */
|
|
|
|
} while(!feof(f));
|
1999-08-24 16:27:23 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
1999-12-19 07:11:26 +00:00
|
|
|
static void
|
|
|
|
write_filter(gpointer filter_arg, gpointer file_arg)
|
|
|
|
{
|
1999-12-19 09:22:19 +00:00
|
|
|
color_filter_t *colorf = filter_arg;
|
1999-12-19 07:11:26 +00:00
|
|
|
FILE *f = file_arg;
|
|
|
|
|
|
|
|
fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
|
1999-12-19 09:22:19 +00:00
|
|
|
colorf->filter_name,
|
|
|
|
colorf->filter_text,
|
|
|
|
colorf->bg_color.red,
|
|
|
|
colorf->bg_color.green,
|
|
|
|
colorf->bg_color.blue,
|
|
|
|
colorf->fg_color.red,
|
|
|
|
colorf->fg_color.green,
|
|
|
|
colorf->fg_color.blue);
|
1999-12-19 07:11:26 +00:00
|
|
|
}
|
2001-12-02 00:16:02 +00:00
|
|
|
|
|
|
|
/* save filters in filter file */
|
2000-02-12 08:31:49 +00:00
|
|
|
gboolean
|
2002-05-27 22:00:37 +00:00
|
|
|
write_filters(void)
|
1999-08-24 16:27:23 +00:00
|
|
|
{
|
2001-10-24 06:13:07 +00:00
|
|
|
gchar *pf_dir_path;
|
2001-10-22 22:59:26 +00:00
|
|
|
const gchar *path;
|
1999-08-24 16:27:23 +00:00
|
|
|
FILE *f;
|
|
|
|
|
2001-10-23 05:01:02 +00:00
|
|
|
/* Create the directory that holds personal configuration files,
|
|
|
|
if necessary. */
|
|
|
|
if (create_persconffile_dir(&pf_dir_path) == -1) {
|
2002-01-08 21:35:17 +00:00
|
|
|
simple_dialog(ESD_TYPE_WARN, NULL,
|
|
|
|
"Can't create directory\n\"%s\"\nfor color files: %s.",
|
|
|
|
pf_dir_path, strerror(errno));
|
|
|
|
g_free(pf_dir_path);
|
|
|
|
return FALSE;
|
2001-10-23 05:01:02 +00:00
|
|
|
}
|
|
|
|
|
2001-10-24 07:18:39 +00:00
|
|
|
path = get_persconffile_path("colorfilters", TRUE);
|
1999-08-24 16:27:23 +00:00
|
|
|
if ((f = fopen(path, "w+")) == NULL) {
|
2002-01-08 21:35:17 +00:00
|
|
|
simple_dialog(ESD_TYPE_CRIT, NULL,
|
|
|
|
"Could not open\n%s\nfor writing: %s.",
|
|
|
|
path, strerror(errno));
|
|
|
|
return FALSE;
|
1999-08-24 16:27:23 +00:00
|
|
|
}
|
|
|
|
fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
|
1999-12-19 07:11:26 +00:00
|
|
|
g_slist_foreach(filter_list, write_filter, f);
|
1999-08-24 16:27:23 +00:00
|
|
|
fclose(f);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2001-12-02 00:16:02 +00:00
|
|
|
/* allocate a color from the color map */
|
2000-02-12 08:31:49 +00:00
|
|
|
gboolean
|
Make a whole pile of routines private to "colors.c" static, and remove
their declarations from "colors.h". Do the same with some variables -
and remove the ones that aren't used any more.
Make some name changes suggested in the comments in "colors.c".
Make "color_delete_cb()" take, as the user-supplied argument,
"cf->colors", just as "color_new_cb()" does, and make it use its
argument to find the color filter, just as "color_new_cb()" does, rather
than using "cf.colors".
When there are no colors in the list, make the "Edit" button
insensitive; make it sensitive if there are any colors in the list, so
that we don't crash if you press "Edit" when there are no colors in the
list.
Make "remember_selected_row()" take, as the user-supplied argument,
"cf->colors", rather than "cf" - it needs only the color filter, not the
entire "capture_file" structure.
When using "gtk_clist_select_row()" to forcibly select a row, pass -1 as
the column number, to indicate that we don't care which column is
selected.
Fix some typos in function names.
svn path=/trunk/; revision=1161
1999-11-30 04:23:06 +00:00
|
|
|
get_color (GdkColor *new_color)
|
|
|
|
{
|
1999-10-07 22:50:45 +00:00
|
|
|
GdkVisual *pv;
|
|
|
|
|
|
|
|
if (!our_cmap) {
|
2001-12-02 00:16:02 +00:00
|
|
|
if ( !gdk_colormap_alloc_color (sys_cmap, new_color, FALSE, TRUE)) {
|
|
|
|
pv = gdk_visual_get_best();
|
|
|
|
if ( !(our_cmap = gdk_colormap_new(pv, TRUE)))
|
|
|
|
simple_dialog(ESD_TYPE_WARN, NULL, "Could not create new colormap");
|
|
|
|
} else
|
|
|
|
return (TRUE);
|
1999-10-07 22:50:45 +00:00
|
|
|
}
|
|
|
|
return ( gdk_colormap_alloc_color ( our_cmap, new_color, FALSE, TRUE) );
|
|
|
|
}
|