No need for toolkit-dependent color initialization.

We're not allocating colors ourselves in GTK+ (and haven't been doing so
since at least 1.12), and all color_t values are valid colors, so
we don't need any toolkit-specific processing to fill in a color_t.

While we're at it, catch read errors when reading color filter files.

Change-Id: Ieb520d141cf15e371a31a01459d466c95ba2209b
Reviewed-on: https://code.wireshark.org/review/12985
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2015-12-31 20:14:08 -08:00
parent 93f9416c36
commit ee9f102aa9
15 changed files with 127 additions and 120 deletions

View File

@ -45,7 +45,7 @@
#define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
#define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))
static gboolean read_users_filters(GSList **cfl, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb);
static gboolean read_users_filters(GSList **cfl, gchar** err_msg, color_filter_add_cb_func add_cb);
/* the currently active filters */
static GSList *color_filter_list = NULL;
@ -84,7 +84,7 @@ color_filter_new(const gchar *name, /* The name of the filter to create
/* Add ten empty (temporary) colorfilters for easy coloring */
static void
color_filters_add_tmp(GSList **cfl, initialize_color_func init_func)
color_filters_add_tmp(GSList **cfl)
{
gchar *name = NULL;
guint32 i;
@ -104,13 +104,13 @@ color_filters_add_tmp(GSList **cfl, initialize_color_func init_func)
/* retrieve background and foreground colors */
cval = strtoul(fg_colors[i-1], NULL, 16);
init_func(&fg_color, RED_COMPONENT(cval),
GREEN_COMPONENT(cval),
BLUE_COMPONENT(cval) );
fg_color.red = RED_COMPONENT(cval);
fg_color.green = GREEN_COMPONENT(cval);
fg_color.blue = BLUE_COMPONENT(cval);
cval = strtoul(bg_colors[i-1], NULL, 16);
init_func(&bg_color, RED_COMPONENT(cval),
GREEN_COMPONENT(cval),
BLUE_COMPONENT(cval) );
bg_color.red = RED_COMPONENT(cval);
bg_color.green = GREEN_COMPONENT(cval);
bg_color.blue = BLUE_COMPONENT(cval);
colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
colorf->filter_text = g_strdup("frame");
*cfl = g_slist_append(*cfl, colorf);
@ -293,20 +293,20 @@ color_filter_list_clone(GSList *cfl)
/* Initialize the filter structures (reading from file) for general running, including app startup */
gboolean
color_filters_init(gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb)
color_filters_init(gchar** err_msg, color_filter_add_cb_func add_cb)
{
/* delete all currently existing filters */
color_filter_list_delete(&color_filter_list);
/* start the list with the temporary colorizing rules */
color_filters_add_tmp(&color_filter_list, init_func);
color_filters_add_tmp(&color_filter_list);
/* try to read the users filters */
if (!read_users_filters(&color_filter_list, err_msg, init_func, add_cb)) {
if (!read_users_filters(&color_filter_list, err_msg, add_cb)) {
gchar* local_err_msg = NULL;
/* if that failed, try to read the global filters */
if (!color_filters_read_globals(&color_filter_list, &local_err_msg, init_func, add_cb)) {
if (!color_filters_read_globals(&color_filter_list, &local_err_msg, add_cb)) {
/* Show the first error */
g_free(local_err_msg);
}
@ -318,7 +318,7 @@ color_filters_init(gchar** err_msg, initialize_color_func init_func, color_filte
}
gboolean
color_filters_reload(gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb)
color_filters_reload(gchar** err_msg, color_filter_add_cb_func add_cb)
{
/* "move" old entries to the deleted list
* we must keep them until the dissection no longer needs them */
@ -326,14 +326,14 @@ color_filters_reload(gchar** err_msg, initialize_color_func init_func, color_fil
color_filter_list = NULL;
/* start the list with the temporary colorizing rules */
color_filters_add_tmp(&color_filter_list, init_func);
color_filters_add_tmp(&color_filter_list);
/* try to read the users filters */
if (!read_users_filters(&color_filter_list, err_msg, init_func, add_cb)) {
if (!read_users_filters(&color_filter_list, err_msg, add_cb)) {
gchar* local_err_msg = NULL;
/* if that failed, try to read the global filters */
if (!color_filters_read_globals(&color_filter_list, &local_err_msg, init_func, add_cb)) {
if (!color_filters_read_globals(&color_filter_list, &local_err_msg, add_cb)) {
/* Show the first error */
g_free(local_err_msg);
}
@ -509,8 +509,8 @@ color_filters_colorize_packet(epan_dissect_t *edt)
/* read filters from the given file */
/* XXX - Would it make more sense to use GStrings here instead of reallocing
our buffers? */
static gboolean
read_filters_file(FILE *f, gpointer user_data, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb)
static int
read_filters_file(FILE *f, gpointer user_data, color_filter_add_cb_func add_cb)
{
#define INIT_BUF_SIZE 128
gchar *name = NULL;
@ -522,7 +522,7 @@ read_filters_file(FILE *f, gpointer user_data, gchar** err_msg, initialize_color
guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
gboolean disabled = FALSE;
gboolean skip_end_of_line = FALSE;
gboolean ret = TRUE;
int ret = 0;
name = (gchar *)g_malloc(name_len + 1);
filter_exp = (gchar *)g_malloc(filter_exp_len + 1);
@ -629,22 +629,13 @@ read_filters_file(FILE *f, gpointer user_data, gchar** err_msg, initialize_color
continue;
}
if (!init_func(&fg_color, fg_r, fg_g, fg_b)) {
/* oops */
*err_msg = g_strdup_printf("Could not allocate foreground color specified in input file for %s.", name);
dfilter_free(temp_dfilter);
skip_end_of_line = TRUE;
ret = FALSE;
continue;
}
if (!init_func(&bg_color, bg_r, bg_g, bg_b)) {
/* oops */
*err_msg = g_strdup_printf("Could not allocate background color specified in input file for %s.", name);
dfilter_free(temp_dfilter);
skip_end_of_line = TRUE;
ret = FALSE;
continue;
}
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;
colorf = color_filter_new(name, filter_exp, &bg_color,
&fg_color, disabled);
@ -665,6 +656,9 @@ read_filters_file(FILE *f, gpointer user_data, gchar** err_msg, initialize_color
skip_end_of_line = TRUE;
}
if (ferror(f))
ret = errno;
g_free(name);
g_free(filter_exp);
return ret;
@ -672,11 +666,11 @@ read_filters_file(FILE *f, gpointer user_data, gchar** err_msg, initialize_color
/* read filters from the user's filter file */
static gboolean
read_users_filters(GSList **cfl, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb)
read_users_filters(GSList **cfl, gchar** err_msg, color_filter_add_cb_func add_cb)
{
gchar *path;
FILE *f;
gboolean ret;
int ret;
/* decide what file to open (from dfilter code) */
path = get_persconffile_path("colorfilters", TRUE);
@ -691,18 +685,25 @@ read_users_filters(GSList **cfl, gchar** err_msg, initialize_color_func init_fun
g_free(path);
path = NULL;
ret = read_filters_file(f, cfl, err_msg, init_func, add_cb);
ret = read_filters_file(f, cfl, add_cb);
if (ret != 0) {
*err_msg = g_strdup_printf("Error reading filter file\n\"%s\": %s.",
path, g_strerror(errno));
fclose(f);
return FALSE;
}
fclose(f);
return ret;
return TRUE;
}
/* read filters from the filter file */
gboolean
color_filters_read_globals(gpointer user_data, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb)
color_filters_read_globals(gpointer user_data, gchar** err_msg, color_filter_add_cb_func add_cb)
{
gchar *path;
FILE *f;
gboolean ret;
int ret;
/* decide what file to open (from dfilter code) */
path = get_datafile_path("colorfilters");
@ -717,27 +718,41 @@ color_filters_read_globals(gpointer user_data, gchar** err_msg, initialize_color
g_free(path);
path = NULL;
ret = read_filters_file(f, user_data, err_msg, init_func, add_cb);
ret = read_filters_file(f, user_data, add_cb);
if (ret != 0) {
*err_msg = g_strdup_printf("Error reading global filter file\n\"%s\": %s.",
path, g_strerror(errno));
fclose(f);
return FALSE;
}
fclose(f);
return ret;
return TRUE;
}
/* read filters from some other filter file (import) */
gboolean
color_filters_import(const gchar *path, const gpointer user_data, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb)
color_filters_import(const gchar *path, const gpointer user_data, gchar** err_msg, color_filter_add_cb_func add_cb)
{
FILE *f;
gboolean ret;
int ret;
if ((f = ws_fopen(path, "r")) == NULL) {
*err_msg = g_strdup_printf("Could not open\n%s\nfor reading: %s.",
*err_msg = g_strdup_printf("Could not open filter file\n%s\nfor reading: %s.",
path, g_strerror(errno));
return FALSE;
}
ret = read_filters_file(f, user_data, err_msg, init_func, add_cb);
ret = read_filters_file(f, user_data, add_cb);
if (ret != 0) {
*err_msg = g_strdup_printf("Error reading filter file\n\"%s\": %s.",
path, g_strerror(errno));
fclose(f);
return FALSE;
}
fclose(f);
return ret;
return TRUE;
}
struct write_filter_data

View File

@ -39,17 +39,6 @@ typedef struct {
guint16 blue;
} color_t;
/** Initialize a color with R, G, and B values, including any toolkit-dependent
** work that needs to be done.
*
* @param color the color_t to be filled
* @param red the red value for the color
* @param green the green value for the color
* @param blue the blue value for the color
* @return TRUE if it succeeds, FALSE if it fails
*/
typedef gboolean (*initialize_color_func)(color_t *color, guint16 red, guint16 green, guint16 blue);
#define CONVERSATION_COLOR_PREFIX "___conversation_color_filter___"
/** @file
* Color filters.
@ -80,10 +69,10 @@ typedef struct _color_filter {
typedef void (*color_filter_add_cb_func)(color_filter_t *colorf, gpointer user_data);
/** Init the color filters (incl. initial read from file). */
WS_DLL_PUBLIC gboolean color_filters_init(gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb);
WS_DLL_PUBLIC gboolean color_filters_init(gchar** err_msg, color_filter_add_cb_func add_cb);
/** Reload the color filters */
WS_DLL_PUBLIC gboolean color_filters_reload(gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb);
WS_DLL_PUBLIC gboolean color_filters_reload(gchar** err_msg, color_filter_add_cb_func add_cb);
/** Cleanup remaining color filter zombies */
WS_DLL_PUBLIC void color_filters_cleanup(void);
@ -150,14 +139,14 @@ WS_DLL_PUBLIC void color_filters_clone(gpointer user_data, color_filter_add_cb_f
* @param user_data will be returned by each call to to color_filter_add_cb()
* @return TRUE, if read succeeded
*/
WS_DLL_PUBLIC gboolean color_filters_import(const gchar *path, const gpointer user_data, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb);
WS_DLL_PUBLIC gboolean color_filters_import(const gchar *path, const gpointer user_data, gchar** err_msg, color_filter_add_cb_func add_cb);
/** Read filters from the global filter file (not the users file).
*
* @param user_data will be returned by each call to to color_filter_add_cb()
* @return TRUE, if read succeeded
*/
WS_DLL_PUBLIC gboolean color_filters_read_globals(gpointer user_data, gchar** err_msg, initialize_color_func init_func, color_filter_add_cb_func add_cb);
WS_DLL_PUBLIC gboolean color_filters_read_globals(gpointer user_data, gchar** err_msg, color_filter_add_cb_func add_cb);
/** Apply a changed filter list.

View File

@ -3810,6 +3810,10 @@ prefs_capture_options_dialog_column_is_visible(const gchar *column)
#define PRS_GUI_FILTER_EXPR "gui.filter_expressions.expr"
#define PRS_GUI_FILTER_ENABLED "gui.filter_expressions.enabled"
/*
* Extract the red, green, and blue components of a 24-bit RGB value
* and convert them from [0,255] to [0,65535].
*/
#define RED_COMPONENT(x) (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
#define GREEN_COMPONENT(x) (guint16) (((((x) >> 8) & 0xff) * 65535 / 255))
#define BLUE_COMPONENT(x) (guint16) ( (((x) & 0xff) * 65535 / 255))

View File

@ -2277,7 +2277,7 @@ file_color_import_cmd_cb(GtkWidget *color_filters, gpointer filter_list _U_)
}
/* Try to open the color filter file. */
if (!color_filters_import(cf_name, color_filters, &err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_import(cf_name, color_filters, &err_msg, color_filter_add_cb)) {
/* We couldn't open it; don't dismiss the open dialog box,
just leave it around so that the user can, after they
dismiss the alert box popped up for the open error,

View File

@ -963,7 +963,7 @@ color_clear_cmd(GtkWidget *widget)
}
/* try to read the global filters */
if (!color_filters_read_globals(color_filters, &err_msg, initialize_color, color_filter_add_cb))
if (!color_filters_read_globals(color_filters, &err_msg, color_filter_add_cb))
{
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);

View File

@ -1,5 +1,5 @@
/* color_utils.c
* Toolkit-dependent implementations of routines to handle colors.
* GTK+ color conversion routines.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
@ -28,25 +28,6 @@
#include "ui/gtk/color_utils.h"
/*
* Initialize a color with R, G, and B values, including any toolkit-dependent
* work that needs to be done.
* Returns TRUE if it succeeds, FALSE if it fails.
*/
gboolean
initialize_color(color_t *color, guint16 red, guint16 green, guint16 blue)
{
GdkColor gdk_color;
gdk_color.pixel = 0;
gdk_color.red = red;
gdk_color.green = green;
gdk_color.blue = blue;
gdkcolor_to_color_t(color, &gdk_color);
return TRUE;
}
void
color_t_to_gdkcolor(GdkColor *target, const color_t *source)
{
@ -64,6 +45,7 @@ color_t_to_gdkRGBAcolor(GdkRGBA *target, const color_t *source)
target->green = source->green / 65535.0;
target->blue = source->blue / 65535.0;
}
void
gdkcolor_to_color_t(color_t *target, const GdkColor *source)
{

View File

@ -1,5 +1,5 @@
/* colors.h
* Definitions for color structures and routines
/* color_utils.h
* Definitions for GTK+ color conversion routines.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
@ -20,8 +20,8 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __COLORS_H__
#define __COLORS_H__
#ifndef __COLOR_UTILS_H__
#define __COLOR_UTILS_H__
#include "ui/gtk/gui_utils.h"
#include <epan/color_filters.h>
@ -36,15 +36,39 @@
* @param source the source color_t
*/
void color_t_to_gdkcolor(GdkColor *target, const color_t *source);
/** Convert color_t to GdkRGBA.
*
* @param target the GdkRGBA to be filled
* @param source the source color_t
*/
void color_t_to_gdkRGBAcolor(GdkRGBA *target, const color_t *source);
/** Convert GdkColor to color_t.
*
* @param target the source color_t
* @param source the GdkColor to be filled
*/
void gdkcolor_to_color_t(color_t *target, const GdkColor *source);
/** Convert GdkRGBA to color_t.
*
* @param target the source color_t
* @param source the GdkRGBA to be filled
*/
void gdkRGBAcolor_to_color_t(color_t *target, const GdkRGBA *source);
/** Convert GdkColor to GdkRGBA.
*
* @param target the source GdkColor
* @param source the GdkRGBA to be filled
*/
void GdkColor_to_GdkRGBA(GdkRGBA *target, const GdkColor *source);
/** Convert GdkRGBA to GdkColor.
*
* @param target the source GdkColor
* @param source the GdkRGBA to be filled
*/
void gdkRGBAcolor_to_GdkColor(GdkColor *target, const GdkRGBA *source);
#endif /* __COLORS_H__ */
#endif /* __COLOR_UTILS_H__ */

View File

@ -3177,7 +3177,7 @@ main(int argc, char *argv[])
dnd_init(top_level);
if (!color_filters_init(&err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_init(&err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}
@ -3963,7 +3963,7 @@ void change_configuration_profile (const gchar *profile_name)
}
/* Reload color filters */
if (!color_filters_reload(&err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_reload(&err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}
@ -3989,7 +3989,7 @@ main_fields_changed (void)
gchar* err_msg = NULL;
/* Reload color filters */
if (!color_filters_reload(&err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_reload(&err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}

View File

@ -54,8 +54,21 @@ ColorUtils::ColorUtils(QObject *parent) :
{
}
//
// A color_t has RGB values in [0,65535].
// Qt RGB colors have RGB values in [0,255].
//
// 65535/255 = 257 = 0x0101, so converting from [0,255] to
// [0,65535] involves just shifting the 8-bit value left 8 bits
// and ORing them together.
//
// Converting from [0,65535] to [0,255] without rounding involves
// just shifting the 16-bit value right 8 bits; I guess you could
// round them by adding 0x80 to the value before shifting.
//
QColor ColorUtils::fromColorT (const color_t *color) {
if (!color) return QColor();
// Convert [0,65535] values to [0,255] values
return QColor(color->red >> 8, color->green >> 8, color->blue >> 8);
}
@ -67,6 +80,8 @@ QColor ColorUtils::fromColorT(color_t color)
const color_t ColorUtils::toColorT(const QColor color)
{
color_t colort;
// Convert [0,255] values to [0,65535] values
colort.red = (color.red() << 8) | color.red();
colort.green = (color.green() << 8) | color.green();
colort.blue = (color.blue() << 8) | color.blue();

View File

@ -324,7 +324,7 @@ void ColoringRulesDialog::on_buttonBox_clicked(QAbstractButton *button)
QString file_name = QFileDialog::getOpenFileName(this, wsApp->windowTitleString(tr("Import Coloring Rules")),
wsApp->lastOpenDir().path());
gchar* err_msg = NULL;
if (!color_filters_import(file_name.toUtf8().constData(), this, &err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_import(file_name.toUtf8().constData(), this, &err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}
@ -436,27 +436,6 @@ void ColoringRulesTreeDelegate::ruleNameChanged(const QString name)
}
/*
* Initialize a color with R, G, and B values, including any toolkit-dependent
* work that needs to be done.
*/
gboolean
initialize_color(color_t *color, guint16 red, guint16 green, guint16 blue)
{
QColor qc;
// color_t uses 16-bit components to match Gtk+. Qt use 8.
qc.setRgb(red>>8, green>>8, blue>>8);
if (!qc.isValid())
return FALSE;
// Match what color_filters.c does.
color->red = red;
color->green = green;
color->blue = blue;
return TRUE;
}
// Callback for color_filters_clone.
void
color_filter_add_cb(color_filter_t *colorf, gpointer user_data)

View File

@ -1398,7 +1398,7 @@ void MainWindow::checkDisplayFilter()
void MainWindow::fieldsChanged()
{
gchar *err_msg = NULL;
if (!color_filters_reload(&err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_reload(&err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}

View File

@ -394,7 +394,7 @@ void WiresharkApplication::setConfigurationProfile(const gchar *profile_name)
}
/* Reload color filters */
if (!color_filters_reload(&err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_reload(&err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}

View File

@ -89,7 +89,6 @@ void packet_list_resize_column(gint col);
files
Function names make it clear where they are coming from
*/
gboolean initialize_color(color_t *color, guint16 red, guint16 green, guint16 blue);
void color_filter_add_cb(color_filter_t *colorf, gpointer user_data);
#ifdef __cplusplus

View File

@ -978,7 +978,7 @@ win32_import_color_file(HWND h_wnd, gpointer color_filters) {
/* XXX - Support export limited to selected filters */
if (GetOpenFileName(ofn)) {
g_free( (void *) ofn);
if (!color_filters_import(utf_16to8(file_name), color_filters, &err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_import(utf_16to8(file_name), color_filters, &err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
return;

View File

@ -1354,7 +1354,7 @@ int main(int argc, char *argv[])
////////
gchar* err_msg = NULL;
if (!color_filters_init(&err_msg, initialize_color, color_filter_add_cb)) {
if (!color_filters_init(&err_msg, color_filter_add_cb)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "%s", err_msg);
g_free(err_msg);
}