2000-05-31 21:50:38 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 11:33:08 +00:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2000-05-31 21:50:38 +00:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
2000-07-26 11:33:08 +00:00
|
|
|
* Lesser General Public License for more details.
|
2000-05-31 21:50:38 +00:00
|
|
|
*
|
2000-07-26 11:33:08 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2000-05-31 21:50:38 +00:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2000-07-26 11:33:08 +00:00
|
|
|
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
|
2000-05-31 21:50:38 +00:00
|
|
|
* file for a list of people on the GTK+ Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
2004-03-06 03:38:59 +00:00
|
|
|
#include <config.h>
|
2000-05-31 21:50:38 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "gdkcolor.h"
|
|
|
|
#include "gdkprivate-fb.h"
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
#define GDK_COLORMAP_PRIVATE_DATA(cmap) ((GdkColormapPrivateFB *) GDK_COLORMAP (cmap)->windowing_data)
|
|
|
|
|
2000-05-31 21:50:38 +00:00
|
|
|
static gint gdk_colormap_match_color (GdkColormap *cmap,
|
|
|
|
GdkColor *color,
|
|
|
|
const gchar *available);
|
2000-12-05 16:58:25 +00:00
|
|
|
static void gdk_fb_color_round_to_hw (GdkColor *color);
|
2000-07-24 16:19:00 +00:00
|
|
|
|
|
|
|
static gpointer parent_class;
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_colormap_finalize (GObject *object)
|
|
|
|
{
|
2000-11-13 17:40:23 +00:00
|
|
|
GdkColormap *colormap = GDK_COLORMAP (object);
|
|
|
|
GdkColormapPrivateFB *private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-07-24 16:19:00 +00:00
|
|
|
|
|
|
|
if (private->hash)
|
|
|
|
g_hash_table_destroy (private->hash);
|
|
|
|
|
|
|
|
g_free (private->info);
|
|
|
|
g_free (colormap->colors);
|
2002-11-25 22:52:36 +00:00
|
|
|
g_free (private);
|
2000-07-24 16:19:00 +00:00
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2000-07-24 16:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_colormap_init (GdkColormap *colormap)
|
|
|
|
{
|
2000-11-13 17:40:23 +00:00
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
|
|
|
|
private = g_new (GdkColormapPrivateFB, 1);
|
|
|
|
|
|
|
|
colormap->windowing_data = private;
|
2000-07-24 16:19:00 +00:00
|
|
|
|
|
|
|
colormap->size = 0;
|
|
|
|
colormap->colors = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gdk_colormap_class_init (GdkColormapClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
parent_class = g_type_class_peek_parent (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gdk_colormap_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
GType
|
|
|
|
gdk_colormap_get_type (void)
|
|
|
|
{
|
|
|
|
static GType object_type = 0;
|
|
|
|
|
|
|
|
if (!object_type)
|
|
|
|
{
|
|
|
|
static const GTypeInfo object_info =
|
|
|
|
{
|
2000-11-13 17:40:23 +00:00
|
|
|
sizeof (GdkColormapClass),
|
2000-07-24 16:19:00 +00:00
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gdk_colormap_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
2000-11-13 17:40:23 +00:00
|
|
|
sizeof (GdkColormap),
|
2000-07-24 16:19:00 +00:00
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) gdk_colormap_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
object_type = g_type_register_static (G_TYPE_OBJECT,
|
|
|
|
"GdkColormap",
|
2000-11-02 15:38:04 +00:00
|
|
|
&object_info,
|
|
|
|
0);
|
2000-07-24 16:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return object_type;
|
|
|
|
}
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
GdkColormap *
|
2000-05-31 21:50:38 +00:00
|
|
|
gdk_colormap_new (GdkVisual *visual,
|
|
|
|
gint private_cmap)
|
|
|
|
{
|
|
|
|
GdkColormap *colormap;
|
2000-12-05 16:58:25 +00:00
|
|
|
GdkColormap *system;
|
2000-05-31 21:50:38 +00:00
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
GdkFBDisplay *fbd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_return_val_if_fail (visual != NULL, NULL);
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
colormap = g_object_new (gdk_colormap_get_type (), NULL);
|
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
colormap->visual = visual;
|
2000-05-31 21:50:38 +00:00
|
|
|
fbd = gdk_display;
|
|
|
|
|
|
|
|
private->hash = NULL;
|
|
|
|
|
|
|
|
colormap->size = visual->colormap_size;
|
|
|
|
colormap->colors = NULL;
|
|
|
|
|
|
|
|
switch (visual->type)
|
|
|
|
{
|
|
|
|
case GDK_VISUAL_STATIC_GRAY:
|
|
|
|
case GDK_VISUAL_STATIC_COLOR:
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
|
|
|
private->info = g_new0 (GdkColorInfo, colormap->size);
|
|
|
|
colormap->colors = g_new (GdkColor, colormap->size);
|
|
|
|
|
|
|
|
private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
|
2000-11-13 13:15:41 +00:00
|
|
|
(GEqualFunc) gdk_color_equal);
|
2000-12-05 16:58:25 +00:00
|
|
|
|
|
|
|
system = gdk_colormap_get_system ();
|
|
|
|
memcpy (colormap->colors, system->colors, colormap->size * sizeof (GdkColor));
|
2000-05-31 21:50:38 +00:00
|
|
|
|
|
|
|
if (private_cmap)
|
|
|
|
{
|
|
|
|
guint16 red[256], green[256], blue[256];
|
|
|
|
struct fb_cmap fbc = {0, 256};
|
|
|
|
|
|
|
|
fbc.red = red;
|
|
|
|
fbc.green = green;
|
|
|
|
fbc.blue = blue;
|
|
|
|
|
2000-12-08 12:38:48 +00:00
|
|
|
if (ioctl (fbd->fb_fd, FBIOGETCMAP, &fbc))
|
2000-05-31 21:50:38 +00:00
|
|
|
g_error("ioctl(FBIOGETCMAP) failed");
|
|
|
|
|
|
|
|
for (i = 0; i < colormap->size; i++)
|
|
|
|
{
|
|
|
|
colormap->colors[i].pixel = i;
|
|
|
|
colormap->colors[i].red = red[i];
|
|
|
|
colormap->colors[i].green = green[i];
|
|
|
|
colormap->colors[i].blue = blue[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
gdk_colormap_change (colormap, colormap->size);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GDK_VISUAL_DIRECT_COLOR:
|
2001-05-04 21:41:17 +00:00
|
|
|
g_warning ("gdk_colormap_new () on a direct color visual not implemented");
|
2000-05-31 21:50:38 +00:00
|
|
|
#if 0
|
|
|
|
colormap->colors = g_new (GdkColor, colormap->size);
|
|
|
|
|
|
|
|
size = 1 << visual->red_prec;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
colormap->colors[i].red = i * 65535 / (size - 1);
|
|
|
|
|
|
|
|
size = 1 << visual->green_prec;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
colormap->colors[i].green = i * 65535 / (size - 1);
|
|
|
|
|
|
|
|
size = 1 << visual->blue_prec;
|
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
colormap->colors[i].blue = i * 65535 / (size - 1);
|
|
|
|
|
|
|
|
gdk_colormap_change (colormap, colormap->size);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-11-13 17:40:23 +00:00
|
|
|
g_assert_not_reached ();
|
2000-05-31 21:50:38 +00:00
|
|
|
|
|
|
|
case GDK_VISUAL_TRUE_COLOR:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return colormap;
|
|
|
|
}
|
|
|
|
|
|
|
|
GdkColormap*
|
2002-09-10 15:19:13 +00:00
|
|
|
gdk_screen_get_system_colormap (GdkScreen *screen)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
static GdkColormap *colormap = NULL;
|
|
|
|
|
|
|
|
if (!colormap)
|
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
GdkColormapPrivateFB *private;
|
2000-11-13 17:40:23 +00:00
|
|
|
GdkVisual *visual = gdk_visual_get_system ();
|
2000-12-05 16:58:25 +00:00
|
|
|
int i, r, g, b;
|
|
|
|
|
|
|
|
colormap = g_object_new (gdk_colormap_get_type (), NULL);
|
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2000-12-05 16:58:25 +00:00
|
|
|
colormap->visual = visual;
|
|
|
|
private->hash = NULL;
|
|
|
|
|
|
|
|
colormap->size = visual->colormap_size;
|
|
|
|
colormap->colors = NULL;
|
|
|
|
|
|
|
|
switch (visual->type)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
case GDK_VISUAL_STATIC_GRAY:
|
|
|
|
case GDK_VISUAL_STATIC_COLOR:
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
|
|
|
private->info = g_new0 (GdkColorInfo, colormap->size);
|
|
|
|
colormap->colors = g_new (GdkColor, colormap->size);
|
|
|
|
|
|
|
|
private->hash = g_hash_table_new ((GHashFunc) gdk_color_hash,
|
|
|
|
(GEqualFunc) gdk_color_equal);
|
2000-05-31 21:50:38 +00:00
|
|
|
switch(visual->type)
|
|
|
|
{
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
2000-12-05 16:58:25 +00:00
|
|
|
for(i = 0; i < 256; i++) {
|
|
|
|
colormap->colors[i].red =
|
|
|
|
colormap->colors[i].green =
|
|
|
|
colormap->colors[i].blue = i << 8;
|
|
|
|
gdk_fb_color_round_to_hw (&colormap->colors[i]);
|
|
|
|
}
|
2000-05-31 21:50:38 +00:00
|
|
|
i--;
|
2000-12-05 16:58:25 +00:00
|
|
|
colormap->colors[i].red =
|
|
|
|
colormap->colors[i].green =
|
|
|
|
colormap->colors[i].blue = 65535; /* Make it a true white */
|
|
|
|
gdk_fb_color_round_to_hw (&colormap->colors[i]);
|
2000-05-31 21:50:38 +00:00
|
|
|
break;
|
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
|
|
|
/* Color cube stolen from gdkrgb upon advice from Owen */
|
|
|
|
for(i = r = 0; r < 6; r++)
|
|
|
|
for(g = 0; g < 6; g++)
|
|
|
|
for(b = 0; b < 6; b++)
|
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
colormap->colors[i].red = r * 65535 / 5;
|
|
|
|
colormap->colors[i].green = g * 65535 / 5;
|
|
|
|
colormap->colors[i].blue = b * 65535 / 5;
|
|
|
|
gdk_fb_color_round_to_hw (&colormap->colors[i]);
|
2000-05-31 21:50:38 +00:00
|
|
|
i++;
|
|
|
|
}
|
2000-11-13 17:40:23 +00:00
|
|
|
g_assert (i == 216);
|
2000-12-05 16:58:25 +00:00
|
|
|
/* Fill in remaining space with grays */
|
2000-05-31 21:50:38 +00:00
|
|
|
for(i = 216; i < 256; i++)
|
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
colormap->colors[i].red =
|
|
|
|
colormap->colors[i].green =
|
|
|
|
colormap->colors[i].blue = (i - 216) * 40;
|
|
|
|
gdk_fb_color_round_to_hw (&colormap->colors[i]);
|
2000-05-31 21:50:38 +00:00
|
|
|
}
|
|
|
|
/* Real white */
|
2000-12-05 16:58:25 +00:00
|
|
|
colormap->colors[255].red =
|
|
|
|
colormap->colors[255].green =
|
|
|
|
colormap->colors[255].blue = 65535;
|
|
|
|
gdk_fb_color_round_to_hw (&colormap->colors[255]);
|
|
|
|
|
2000-05-31 21:50:38 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2000-12-05 16:58:25 +00:00
|
|
|
break;
|
|
|
|
case GDK_VISUAL_DIRECT_COLOR:
|
2001-05-04 21:41:17 +00:00
|
|
|
g_warning ("gdk_colormap_get_system() on a direct color visual is not implemented");
|
2000-12-05 16:58:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
case GDK_VISUAL_TRUE_COLOR:
|
|
|
|
break;
|
2000-05-31 21:50:38 +00:00
|
|
|
}
|
2000-12-05 16:58:25 +00:00
|
|
|
|
|
|
|
/* Lock all colors for the system colormap
|
|
|
|
* on pseudocolor visuals. The AA text rendering
|
|
|
|
* takes to many colors otherwise.
|
|
|
|
*/
|
|
|
|
if ((visual->type == GDK_VISUAL_GRAYSCALE) ||
|
|
|
|
(visual->type == GDK_VISUAL_PSEUDO_COLOR))
|
|
|
|
{
|
|
|
|
for(i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
colormap->colors[i].pixel = i;
|
|
|
|
private->info[i].ref_count = 1;
|
|
|
|
g_hash_table_insert (private->hash,
|
|
|
|
&colormap->colors[i],
|
|
|
|
&colormap->colors[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gdk_colormap_change (colormap, colormap->size);
|
2000-05-31 21:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return colormap;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
gdk_colormap_get_system_size (void)
|
|
|
|
{
|
|
|
|
return 1 << (gdk_display->modeinfo.bits_per_pixel);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gdk_colormap_change (GdkColormap *colormap,
|
|
|
|
gint ncolors)
|
|
|
|
{
|
|
|
|
guint16 red[256], green[256], blue[256];
|
|
|
|
struct fb_cmap fbc = {0,256};
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
g_return_if_fail (colormap != NULL);
|
|
|
|
|
|
|
|
fbc.red = red;
|
|
|
|
fbc.green = green;
|
|
|
|
fbc.blue = blue;
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
|
|
|
switch (colormap->visual->type)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
|
|
|
for(i = 0; i < ncolors; i++)
|
|
|
|
{
|
|
|
|
red[i] = green[i] = blue[i] =
|
|
|
|
(colormap->colors[i].red +
|
|
|
|
colormap->colors[i].green +
|
|
|
|
colormap->colors[i].blue)/3;
|
|
|
|
}
|
2000-12-08 12:38:48 +00:00
|
|
|
ioctl (gdk_display->fb_fd, FBIOPUTCMAP, &fbc);
|
2000-05-31 21:50:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
|
|
|
for (i = 0; i < ncolors; i++)
|
|
|
|
{
|
|
|
|
red[i] = colormap->colors[i].red;
|
|
|
|
green[i] = colormap->colors[i].green;
|
|
|
|
blue[i] = colormap->colors[i].blue;
|
|
|
|
}
|
2000-12-08 12:38:48 +00:00
|
|
|
ioctl (gdk_display->fb_fd, FBIOPUTCMAP, &fbc);
|
2000-05-31 21:50:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gdk_colormap_free_colors (GdkColormap *colormap,
|
|
|
|
GdkColor *colors,
|
|
|
|
gint ncolors)
|
|
|
|
{
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
gint i;
|
|
|
|
|
|
|
|
g_return_if_fail (colormap != NULL);
|
|
|
|
g_return_if_fail (colors != NULL);
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
|
|
|
|
(colormap->visual->type != GDK_VISUAL_GRAYSCALE))
|
2000-05-31 21:50:38 +00:00
|
|
|
return;
|
|
|
|
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
gulong pixel = colors[i].pixel;
|
|
|
|
|
|
|
|
if (private->info[pixel].ref_count)
|
|
|
|
{
|
|
|
|
private->info[pixel].ref_count--;
|
|
|
|
|
|
|
|
if (private->info[pixel].ref_count == 0)
|
|
|
|
{
|
|
|
|
if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
|
|
|
|
g_hash_table_remove (private->hash, &colormap->colors[pixel]);
|
|
|
|
private->info[pixel].flags = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************
|
|
|
|
* Color allocation *
|
|
|
|
********************/
|
|
|
|
|
2000-12-05 16:58:25 +00:00
|
|
|
static void
|
|
|
|
gdk_fb_color_round_to_hw (GdkColor *color)
|
|
|
|
{
|
|
|
|
guint rmask, gmask, bmask, len;
|
|
|
|
|
|
|
|
len = gdk_display->modeinfo.red.length;
|
|
|
|
rmask = ((1 << len) - 1) << (16-len);
|
|
|
|
len = gdk_display->modeinfo.green.length;
|
|
|
|
gmask = ((1 << len) - 1) << (16-len);
|
|
|
|
len = gdk_display->modeinfo.blue.length;
|
|
|
|
bmask = ((1 << len) - 1) << (16-len);
|
|
|
|
|
|
|
|
color->red &=rmask;
|
|
|
|
color->green &=gmask;
|
|
|
|
color->blue &=bmask;
|
|
|
|
}
|
|
|
|
|
2000-05-31 21:50:38 +00:00
|
|
|
/* Try to allocate a single color using XAllocColor. If it succeeds,
|
|
|
|
* cache the result in our colormap, and store in ret.
|
|
|
|
*/
|
|
|
|
static gboolean
|
|
|
|
gdk_colormap_alloc1 (GdkColormap *colormap,
|
|
|
|
GdkColor *color,
|
|
|
|
GdkColor *ret)
|
|
|
|
{
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
int i;
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
if (colormap->visual->type != GDK_VISUAL_GRAYSCALE
|
|
|
|
&& colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR)
|
2000-05-31 21:50:38 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*ret = *color;
|
2000-12-05 16:58:25 +00:00
|
|
|
|
|
|
|
gdk_fb_color_round_to_hw (ret);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2000-12-05 16:58:25 +00:00
|
|
|
for (i = 0; i<colormap->size; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
2001-09-19 20:17:44 +00:00
|
|
|
if (!(private->info[i].flags & GDK_COLOR_WRITEABLE) &&
|
2000-12-05 16:58:25 +00:00
|
|
|
(ret->red == colormap->colors[i].red) &&
|
|
|
|
(ret->green == colormap->colors[i].green) &&
|
|
|
|
(ret->blue == colormap->colors[i].blue))
|
|
|
|
{
|
|
|
|
ret->pixel = i;
|
|
|
|
colormap->colors[i].pixel = i;
|
|
|
|
if (private->info[i].ref_count == 0)
|
|
|
|
g_hash_table_insert (private->hash,
|
|
|
|
&colormap->colors[ret->pixel],
|
|
|
|
&colormap->colors[ret->pixel]);
|
|
|
|
private->info[i].ref_count++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2000-05-31 21:50:38 +00:00
|
|
|
}
|
|
|
|
|
2000-12-05 16:58:25 +00:00
|
|
|
for (i = 0; i<colormap->size; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
if (private->info[i].ref_count==0)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
guint16 red = color->red, green = color->green, blue = color->blue;
|
|
|
|
struct fb_cmap fbc;
|
2000-11-13 17:40:23 +00:00
|
|
|
|
2000-05-31 21:50:38 +00:00
|
|
|
fbc.len = 1;
|
|
|
|
fbc.start = i;
|
|
|
|
fbc.red = &red;
|
|
|
|
fbc.green = &green;
|
|
|
|
fbc.blue = &blue;
|
|
|
|
|
2000-12-08 12:38:48 +00:00
|
|
|
ioctl (gdk_display->fb_fd, FBIOPUTCMAP, &fbc);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
|
|
|
ret->pixel = i;
|
|
|
|
colormap->colors[ret->pixel] = *ret;
|
|
|
|
private->info[ret->pixel].ref_count = 1;
|
|
|
|
g_hash_table_insert (private->hash,
|
|
|
|
&colormap->colors[ret->pixel],
|
|
|
|
&colormap->colors[ret->pixel]);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2000-12-05 16:58:25 +00:00
|
|
|
|
2000-05-31 21:50:38 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gdk_colormap_alloc_colors_shared (GdkColormap *colormap,
|
|
|
|
GdkColor *colors,
|
|
|
|
gint ncolors,
|
|
|
|
gboolean writeable,
|
|
|
|
gboolean best_match,
|
|
|
|
gboolean *success)
|
|
|
|
{
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
gint i, index;
|
|
|
|
gint nremaining = 0;
|
|
|
|
gint nfailed = 0;
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
index = -1;
|
|
|
|
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
if (!success[i])
|
|
|
|
{
|
|
|
|
if (gdk_colormap_alloc1 (colormap, &colors[i], &colors[i]))
|
|
|
|
success[i] = TRUE;
|
|
|
|
else
|
|
|
|
nremaining++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (nremaining > 0 && best_match)
|
|
|
|
{
|
|
|
|
gchar *available = g_new (gchar, colormap->size);
|
|
|
|
|
|
|
|
for (i = 0; i < colormap->size; i++)
|
|
|
|
available[i] = ((private->info[i].ref_count == 0) ||
|
2001-09-19 20:17:44 +00:00
|
|
|
!(private->info[i].flags & GDK_COLOR_WRITEABLE));
|
2000-05-31 21:50:38 +00:00
|
|
|
|
|
|
|
while (nremaining > 0)
|
|
|
|
{
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
if (!success[i])
|
|
|
|
{
|
|
|
|
index = gdk_colormap_match_color (colormap, &colors[i], available);
|
|
|
|
if (index != -1)
|
|
|
|
{
|
|
|
|
if (private->info[index].ref_count)
|
|
|
|
{
|
|
|
|
private->info[index].ref_count++;
|
|
|
|
colors[i] = colormap->colors[index];
|
|
|
|
success[i] = TRUE;
|
|
|
|
nremaining--;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (gdk_colormap_alloc1 (colormap,
|
|
|
|
&colormap->colors[index],
|
|
|
|
&colors[i]))
|
|
|
|
{
|
|
|
|
success[i] = TRUE;
|
|
|
|
nremaining--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
available[index] = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nfailed++;
|
|
|
|
nremaining--;
|
|
|
|
success[i] = 2; /* flag as permanent failure */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free (available);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Change back the values we flagged as permanent failures */
|
|
|
|
if (nfailed > 0)
|
|
|
|
{
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
if (success[i] == 2)
|
|
|
|
success[i] = FALSE;
|
|
|
|
nremaining = nfailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ncolors - nremaining);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gdk_colormap_alloc_colors_pseudocolor (GdkColormap *colormap,
|
|
|
|
GdkColor *colors,
|
|
|
|
gint ncolors,
|
|
|
|
gboolean writeable,
|
|
|
|
gboolean best_match,
|
|
|
|
gboolean *success)
|
|
|
|
{
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
GdkColor *lookup_color;
|
|
|
|
gint i;
|
|
|
|
gint nremaining = 0;
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
|
|
|
/* Check for an exact match among previously allocated colors */
|
|
|
|
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
if (!success[i])
|
|
|
|
{
|
|
|
|
lookup_color = g_hash_table_lookup (private->hash, &colors[i]);
|
|
|
|
if (lookup_color)
|
|
|
|
{
|
|
|
|
private->info[lookup_color->pixel].ref_count++;
|
|
|
|
colors[i].pixel = lookup_color->pixel;
|
|
|
|
success[i] = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nremaining++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If that failed, we try to allocate a new color, or approxmiate
|
|
|
|
* with what we can get if best_match is TRUE.
|
|
|
|
*/
|
|
|
|
if (nremaining > 0)
|
|
|
|
return gdk_colormap_alloc_colors_shared (colormap, colors, ncolors, writeable, best_match, success);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
gdk_colormap_alloc_colors (GdkColormap *colormap,
|
|
|
|
GdkColor *colors,
|
|
|
|
gint ncolors,
|
|
|
|
gboolean writeable,
|
|
|
|
gboolean best_match,
|
|
|
|
gboolean *success)
|
|
|
|
{
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
GdkVisual *visual;
|
|
|
|
gint i;
|
|
|
|
gint nremaining = 0;
|
|
|
|
|
|
|
|
g_return_val_if_fail (colormap != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (colors != NULL, FALSE);
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
success[i] = FALSE;
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
visual = colormap->visual;
|
|
|
|
switch (visual->type)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
|
|
|
case GDK_VISUAL_STATIC_GRAY:
|
|
|
|
case GDK_VISUAL_STATIC_COLOR:
|
|
|
|
return gdk_colormap_alloc_colors_pseudocolor (colormap, colors, ncolors,
|
|
|
|
writeable, best_match, success);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GDK_VISUAL_DIRECT_COLOR:
|
|
|
|
case GDK_VISUAL_TRUE_COLOR:
|
2001-01-31 09:18:20 +00:00
|
|
|
for (i = 0; i < ncolors; i++)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
colors[i].pixel = (((colors[i].red >> (16 - visual->red_prec)) << visual->red_shift) +
|
|
|
|
((colors[i].green >> (16 - visual->green_prec)) << visual->green_shift) +
|
|
|
|
((colors[i].blue >> (16 - visual->blue_prec)) << visual->blue_shift));
|
|
|
|
success[i] = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return nremaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gdk_color_change (GdkColormap *colormap,
|
|
|
|
GdkColor *color)
|
|
|
|
{
|
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
struct fb_cmap fbc = {0, 1};
|
|
|
|
|
|
|
|
g_return_val_if_fail (colormap != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (color != NULL, FALSE);
|
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
2000-05-31 21:50:38 +00:00
|
|
|
|
2000-11-13 17:40:23 +00:00
|
|
|
switch(colormap->visual->type)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
|
|
|
color->red = color->green = color->blue = (color->red + color->green + color->blue)/3;
|
|
|
|
|
2000-12-05 16:58:25 +00:00
|
|
|
/* Fall through */
|
2000-05-31 21:50:38 +00:00
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
2000-12-05 16:58:25 +00:00
|
|
|
colormap->colors[color->pixel] = *color;
|
|
|
|
|
2000-05-31 21:50:38 +00:00
|
|
|
fbc.start = color->pixel;
|
|
|
|
fbc.red = &color->red;
|
|
|
|
fbc.green = &color->green;
|
|
|
|
fbc.blue = &color->blue;
|
2000-12-08 12:38:48 +00:00
|
|
|
ioctl (gdk_display->fb_fd, FBIOPUTCMAP, &fbc);
|
2000-05-31 21:50:38 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gint
|
|
|
|
gdk_colormap_match_color (GdkColormap *cmap,
|
|
|
|
GdkColor *color,
|
|
|
|
const gchar *available)
|
|
|
|
{
|
|
|
|
GdkColor *colors;
|
|
|
|
guint sum, max;
|
|
|
|
gint rdiff, gdiff, bdiff;
|
|
|
|
gint i, index;
|
|
|
|
|
|
|
|
g_return_val_if_fail (cmap != NULL, 0);
|
|
|
|
g_return_val_if_fail (color != NULL, 0);
|
|
|
|
|
|
|
|
colors = cmap->colors;
|
|
|
|
max = 3 * (65536);
|
|
|
|
index = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < cmap->size; i++)
|
|
|
|
{
|
|
|
|
if ((!available) || (available && available[i]))
|
|
|
|
{
|
|
|
|
rdiff = (color->red - colors[i].red);
|
|
|
|
gdiff = (color->green - colors[i].green);
|
|
|
|
bdiff = (color->blue - colors[i].blue);
|
|
|
|
|
|
|
|
sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
|
|
|
|
|
|
|
|
if (sum < max)
|
|
|
|
{
|
|
|
|
index = i;
|
|
|
|
max = sum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2000-12-05 16:58:25 +00:00
|
|
|
gboolean
|
2000-11-13 17:40:23 +00:00
|
|
|
gdk_colors_alloc (GdkColormap *colormap,
|
|
|
|
gboolean contiguous,
|
|
|
|
gulong *planes,
|
|
|
|
gint nplanes,
|
|
|
|
gulong *pixels,
|
|
|
|
gint npixels)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
gint found, i, col;
|
|
|
|
|
|
|
|
g_return_val_if_fail (colormap != NULL, FALSE);
|
|
|
|
|
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
|
|
|
|
|
|
|
if (nplanes > 0)
|
2004-01-30 19:49:40 +00:00
|
|
|
return FALSE;
|
2000-12-05 16:58:25 +00:00
|
|
|
|
|
|
|
found = 0;
|
|
|
|
for (i = 1; i < colormap->size; i++)
|
|
|
|
{
|
|
|
|
if (private->info[i].ref_count == 0)
|
|
|
|
{
|
|
|
|
found++;
|
|
|
|
if (found >= npixels)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found < npixels)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
col = 0;
|
|
|
|
for (i = 1; i < colormap->size; i++)
|
|
|
|
{
|
|
|
|
if (private->info[i].ref_count == 0)
|
|
|
|
{
|
|
|
|
pixels[col++] = i;
|
|
|
|
private->info[i].ref_count++;
|
|
|
|
private->info[i].flags |= GDK_COLOR_WRITEABLE;
|
|
|
|
if (col == npixels)
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
return FALSE;
|
2000-05-31 21:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-11-13 17:40:23 +00:00
|
|
|
gdk_colors_free (GdkColormap *colormap,
|
|
|
|
gulong *pixels,
|
|
|
|
gint npixels,
|
|
|
|
gulong planes)
|
2000-05-31 21:50:38 +00:00
|
|
|
{
|
2000-12-05 16:58:25 +00:00
|
|
|
GdkColormapPrivateFB *private;
|
|
|
|
gint i, pixel;
|
|
|
|
|
|
|
|
g_return_if_fail (colormap != NULL);
|
|
|
|
|
|
|
|
if ((colormap->visual->type != GDK_VISUAL_PSEUDO_COLOR) &&
|
|
|
|
(colormap->visual->type != GDK_VISUAL_GRAYSCALE))
|
|
|
|
return;
|
|
|
|
|
|
|
|
private = GDK_COLORMAP_PRIVATE_DATA (colormap);
|
|
|
|
|
|
|
|
for (i = 0; i < npixels; i++)
|
|
|
|
{
|
|
|
|
pixel = pixels[i];
|
|
|
|
|
|
|
|
if (private->info[pixel].ref_count)
|
|
|
|
{
|
|
|
|
private->info[pixel].ref_count--;
|
|
|
|
|
|
|
|
if (private->info[pixel].ref_count == 0)
|
|
|
|
{
|
|
|
|
if (!(private->info[pixel].flags & GDK_COLOR_WRITEABLE))
|
|
|
|
g_hash_table_remove (private->hash, &colormap->colors[pixel]);
|
|
|
|
private->info[pixel].flags = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-05-31 21:50:38 +00:00
|
|
|
}
|
2001-01-23 15:28:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
gdk_colormap_query_color (GdkColormap *colormap,
|
|
|
|
gulong pixel,
|
|
|
|
GdkColor *result)
|
|
|
|
{
|
|
|
|
GdkVisual *visual;
|
|
|
|
|
|
|
|
g_return_if_fail (GDK_IS_COLORMAP (colormap));
|
|
|
|
|
|
|
|
visual = gdk_colormap_get_visual (colormap);
|
|
|
|
|
2001-01-31 09:18:20 +00:00
|
|
|
switch (visual->type)
|
|
|
|
{
|
|
|
|
case GDK_VISUAL_DIRECT_COLOR:
|
|
|
|
case GDK_VISUAL_TRUE_COLOR:
|
|
|
|
result->red = 65535. * (gdouble) ((pixel & visual->red_mask) >> visual->red_shift) / ((1 << visual->red_prec) - 1);
|
|
|
|
result->green = 65535. * (gdouble) ((pixel & visual->green_mask) >> visual->green_shift) / ((1 << visual->green_prec) - 1);
|
|
|
|
result->blue = 65535. * (gdouble) ((pixel & visual->blue_mask) >> visual->blue_shift) / ((1 << visual->blue_prec) - 1);
|
|
|
|
break;
|
|
|
|
case GDK_VISUAL_STATIC_GRAY:
|
|
|
|
case GDK_VISUAL_GRAYSCALE:
|
|
|
|
result->red = result->green = result->blue = 65535. * (double)pixel/((1<<visual->depth) - 1);
|
|
|
|
break;
|
|
|
|
case GDK_VISUAL_PSEUDO_COLOR:
|
|
|
|
result->red = colormap->colors[pixel].red;
|
|
|
|
result->green = colormap->colors[pixel].green;
|
|
|
|
result->blue = colormap->colors[pixel].blue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
break;
|
|
|
|
}
|
2001-01-23 15:28:18 +00:00
|
|
|
}
|
2002-09-10 15:19:13 +00:00
|
|
|
|
|
|
|
GdkScreen*
|
|
|
|
gdk_colormap_get_screen (GdkColormap *cmap)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (cmap != NULL, NULL);
|
|
|
|
|
|
|
|
return gdk_screen_get_default ();
|
|
|
|
}
|