2 * Definitions for color structures and routines
4 * $Id: colors.c,v 1.14 2001/10/24 06:13:06 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
41 #include <epan/filesystem.h>
47 #include "dfilter/dfilter.h"
48 #include "simple_dialog.h"
50 extern capture_file cf;
52 static gboolean read_filters(colfilter *filter);
56 static GdkColormap* sys_cmap;
57 static GdkColormap* our_cmap = NULL;
59 GdkColor WHITE = { 0, 65535, 65535, 65535 };
60 GdkColor BLACK = { 0, 0, 0, 0 };
62 /* This structure is used to allow you to compile in default colors if
63 * you wish. They can be later changed by a user.
65 #ifdef READ_DEFAULT_COLOR_LIST
66 struct _default_colors {
68 gchar* color; /* background only */
69 } default_colors[] = {
80 gboolean got_white, got_black;
81 #ifdef READ_DEFAULT_COLOR_LIST
82 color_filter_t *colorf;
87 filter = (colfilter *)g_malloc(sizeof(colfilter));
88 filter->num_of_filters = 0;
90 sys_cmap = gdk_colormap_get_system();
92 /* Allocate "constant" colors. */
93 got_white = get_color(&WHITE);
94 got_black = get_color(&BLACK);
99 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate colors black or white.");
101 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate color white.");
104 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate color black.");
107 #ifdef READ_DEFAULT_COLOR_LIST
108 /* Now process defaults */
109 for (i = 0 ; i < sizeof default_colors/sizeof (struct _default_colors); i++){
110 gdk_color_parse(default_colors[i].color, &color);
112 if( !get_color(&color)){
114 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate color %s.",
115 default_colors[i].color);
118 colorf = new_color_filter(filter, default_colors[i].proto,
119 default_colors[i].proto);
120 colorf->bg_color = color;
122 if (!dfilter_compile(default_colors[i].proto,
123 &colorf->c_colorfilter)) {
124 simple_dialog(ESD_TYPE_WARN, NULL,
125 "Cannot compile default color filter %s.\n%s",
126 default_colors[i].proto, dfilter_error_msg);
127 /* should reject this filter */
129 filter->num_of_filters++;
132 read_filters(filter);
137 new_color_filter(colfilter *filters, gchar *name, gchar *filter_string)
139 color_filter_t *colorf;
141 colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
142 colorf->filter_name = g_strdup(name);
143 colorf->filter_text = g_strdup(filter_string);
144 colorf->bg_color = WHITE;
145 colorf->fg_color = BLACK;
146 colorf->c_colorfilter = NULL;
147 colorf->edit_dialog = NULL;
148 filter_list = g_slist_append(filter_list, colorf);
153 delete_color_filter(color_filter_t *colorf)
155 if (colorf->filter_name != NULL)
156 g_free(colorf->filter_name);
157 if (colorf->filter_text != NULL)
158 g_free(colorf->filter_text);
159 if (colorf->c_colorfilter != NULL)
160 dfilter_free(colorf->c_colorfilter);
161 filter_list = g_slist_remove(filter_list, colorf);
166 * Get the pathname of the preferences file.
169 get_colorfilter_file_path(void)
171 static gchar *cf_path = NULL;
172 static const char fname[] = "colorfilters";
174 if (cf_path == NULL) {
175 cf_path = (gchar *) g_malloc(strlen(get_persconffile_dir()) +
177 sprintf(cf_path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
184 read_filters(colfilter *filter)
186 /* TODO: Lots more syntax checking on the file */
187 /* I hate these fixed length names! TODO: make more dynamic */
188 /* XXX - buffer overflow possibility here */
189 gchar name[256],filter_exp[256], buf[1024];
190 guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
191 GdkColor fg_color, bg_color;
192 color_filter_t *colorf;
196 dfilter_t *temp_dfilter;
198 /* decide what file to open (from dfilter code) */
200 /* should only be called by colors_init.
204 /* we have a clist */
206 path = get_colorfilter_file_path();
207 if ((f = fopen(path, "r")) == NULL) {
208 if (errno != ENOENT) {
209 simple_dialog(ESD_TYPE_CRIT, NULL,
210 "Could not open filter file\n\"%s\": %s.", path,
219 if(!fgets(buf,sizeof buf, f))
222 if(strspn( buf," \t") == (size_t)((strchr(buf,'*') - buf))){
223 /* leading # comment */
227 /* we get the @ delimiter. It is not in any strings */
228 if(sscanf(buf," @%[^@]@%[^@]@[%hu,%hu,%hu][%hu,%hu,%hu]",
229 name, filter_exp, &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 8){
230 /* we got a filter */
232 if(!dfilter_compile(filter_exp, &temp_dfilter)) {
233 simple_dialog(ESD_TYPE_CRIT, NULL,
234 "Could not compile color filter %s from saved filters.\n%s",
235 name, dfilter_error_msg);
238 colorf = new_color_filter(filter, name, filter_exp);
239 colorf->c_colorfilter = temp_dfilter;
240 filter->num_of_filters++;
242 fg_color.green = fg_g;
243 fg_color.blue = fg_b;
245 bg_color.green = bg_g;
246 bg_color.blue = bg_b;
247 if( !get_color(&fg_color)){
249 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate fg color specified"
250 "in input file for %s.", name);
255 if( !get_color(&bg_color)){
257 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate bg color specified"
258 "in input file for %s.", name);
263 colorf->bg_color = bg_color;
264 colorf->fg_color = fg_color;
272 write_filter(gpointer filter_arg, gpointer file_arg)
274 color_filter_t *colorf = filter_arg;
277 fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
280 colorf->bg_color.red,
281 colorf->bg_color.green,
282 colorf->bg_color.blue,
283 colorf->fg_color.red,
284 colorf->fg_color.green,
285 colorf->fg_color.blue);
289 write_filters(colfilter *filter)
295 /* Create the directory that holds personal configuration files,
297 if (create_persconffile_dir(&pf_dir_path) == -1) {
298 simple_dialog(ESD_TYPE_WARN, NULL,
299 "Can't create directory\n\"%s\"\nfor color files: %s.",
300 pf_dir_path, strerror(errno));
305 path = get_colorfilter_file_path();
306 if ((f = fopen(path, "w+")) == NULL) {
307 simple_dialog(ESD_TYPE_CRIT, NULL,
308 "Could not open\n%s\nfor writing: %s.",
309 path, strerror(errno));
312 fprintf(f,"# DO NOT EDIT THIS FILE! It was created by Ethereal\n");
313 g_slist_foreach(filter_list, write_filter, f);
319 get_color (GdkColor *new_color)
324 if ( !gdk_colormap_alloc_color (sys_cmap, new_color, FALSE, TRUE)) {
325 pv = gdk_visual_get_best();
326 if ( !(our_cmap = gdk_colormap_new(pv, TRUE)))
327 simple_dialog(ESD_TYPE_WARN, NULL, "Could not create new colormap");
331 return ( gdk_colormap_alloc_color ( our_cmap, new_color, FALSE, TRUE) );