changeset 12801:d24bc9737de8

[gaim-migrate @ 15148] Avoid infinite loopage when we can't generate enough colors. Hopefully someone will come up with a better solution and this will get removed; until then... committer: Tailor Script <tailor@pidgin.im>
author Daniel Atallah <daniel.atallah@gmail.com>
date Tue, 10 Jan 2006 03:30:37 +0000
parents 532db13558c3
children 26b31b4c43a2
files src/gtkconv.c
diffstat 1 files changed, 24 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/gtkconv.c	Tue Jan 10 00:51:28 2006 +0000
+++ b/src/gtkconv.c	Tue Jan 10 03:30:37 2006 +0000
@@ -128,6 +128,7 @@
 
 #define NUM_NICK_COLORS 220
 static GdkColor *nick_colors = NULL;
+static guint nbr_nick_colors;
 
 typedef struct {
 	GtkWidget *window;
@@ -163,7 +164,7 @@
 gboolean gaim_gtkconv_has_focus(GaimConversation *conv);
 static void gaim_gtkconv_custom_smiley_allocated(GdkPixbufLoader *loader, gpointer user_data);
 static void gaim_gtkconv_custom_smiley_closed(GdkPixbufLoader *loader, gpointer user_data);
-static GdkColor* generate_nick_colors(guint numcolors, GdkColor background);
+static GdkColor* generate_nick_colors(guint *numcolors, GdkColor background);
 static gboolean color_is_visible(GdkColor foreground, GdkColor background, int color_contrast, int brightness_contrast);
 static void gaim_gtkconv_update_fields(GaimConversation *conv, GaimGtkConvFields fields);
 
@@ -172,7 +173,7 @@
 	GtkStyle *style = gtk_widget_get_style(gtkconv->imhtml);
 	float scale;
 
-	col = nick_colors[g_str_hash(name) % NUM_NICK_COLORS];
+	col = nick_colors[g_str_hash(name) % nbr_nick_colors];
 	scale = ((1-(LUMINANCE(style->base[GTK_STATE_NORMAL]) / LUMINANCE(style->white))) *
 		       (LUMINANCE(style->white)/MAX(MAX(col.red, col.blue), col.green)));
 
@@ -4283,8 +4284,10 @@
 	else
 		gaim_gtkconv_placement_place(gtkconv);
 
-	if (nick_colors == NULL)
-		nick_colors = generate_nick_colors(NUM_NICK_COLORS, gtk_widget_get_style(gtkconv->imhtml)->base[GTK_STATE_NORMAL]);
+	if (nick_colors == NULL) {
+		nbr_nick_colors = NUM_NICK_COLORS;
+		nick_colors = generate_nick_colors(&nbr_nick_colors, gtk_widget_get_style(gtkconv->imhtml)->base[GTK_STATE_NORMAL]);
+	}
 }
 
 static void
@@ -8067,23 +8070,27 @@
 
 
 static GdkColor*
-generate_nick_colors(guint numcolors, GdkColor background)
-{
+generate_nick_colors(guint *color_count, GdkColor background)
+{
+	guint numcolors = *color_count;
 	guint i = 0, j = 0;
 	GdkColor *colors = g_new(GdkColor, numcolors);
 	GdkColor nick_highlight;
 	GdkColor send_color;
+	time_t breakout_time;
 
 	gdk_color_parse(HIGHLIGHT_COLOR, &nick_highlight);
 	gdk_color_parse(SEND_COLOR, &send_color);
 
 	srand(background.red + background.green + background.blue + 1);
 
+	breakout_time = time(NULL) + 3;
+
 	/* first we look through the list of "good" colors: colors that differ from every other color in the
 	 * list.  only some of them will differ from the background color though. lets see if we can find
 	 * numcolors of them that do
 	 */
-	while (i < numcolors && j < NUM_NICK_SEED_COLORS )
+	while (i < numcolors && j < NUM_NICK_SEED_COLORS && time(NULL) < breakout_time)
 	{
 		GdkColor color = nick_seed_colors[j];
 
@@ -8102,7 +8109,7 @@
 	 * expensive to find colors that not only don't conflict with the background, but also do not
 	 * conflict with each other.
 	 */
-	while(i < numcolors )
+	while(i < numcolors && time(NULL) < breakout_time)
 	{
 		GdkColor color = { 0, rand() % 65536, rand() % 65536, rand() % 65536 };
 
@@ -8118,5 +8125,14 @@
 		}
 	}
 
+	if (i < numcolors) {
+		GdkColor *c = colors;
+		gaim_debug(GAIM_DEBUG_WARNING, NULL, "Unable to generate enough random colors before timeout. %u colors found.\n", i);
+		colors = g_memdup(c, i * sizeof(GdkColor));
+		g_free(c);
+		*color_count = i;
+		
+	}
+
 	return colors;
 }