diff --git a/CMakeLists.txt b/CMakeLists.txt index 74c8e79e..2e2a95e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,6 +63,7 @@ set(AWE_SRCS ${SOURCE_DIR}/window.c ${SOURCE_DIR}/image.c ${SOURCE_DIR}/draw.c + ${SOURCE_DIR}/color.c ${SOURCE_DIR}/swindow.c ${SOURCE_DIR}/common/buffer.c ${SOURCE_DIR}/common/atoms.c diff --git a/color.c b/color.c new file mode 100644 index 00000000..dd727063 --- /dev/null +++ b/color.c @@ -0,0 +1,152 @@ +/* + * color.c - color functions + * + * Copyright © 2008-2009 Julien Danjou + * Copyright © 2009 Uli Schlachter + * + * 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. + * + * 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. + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include "color.h" +#include "structs.h" + +#define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255) + +/** Send a request to initialize a X color. + * \param color xcolor_t struct to store color into. + * \param colstr Color specification. + * \return request informations. + */ +xcolor_init_request_t +xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len) +{ + xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen); + xcolor_init_request_t req; + unsigned long colnum; + uint16_t red, green, blue; + + p_clear(&req, 1); + + if(!len) + { + req.has_error = true; + return req; + } + + req.alpha = 0xffff; + req.color = color; + + /* The color is given in RGB value */ + if(colstr[0] == '#') + { + char *p; + + if(len == 7) + { + colnum = strtoul(colstr + 1, &p, 16); + if(p - colstr != 7) + goto invalid; + } + /* we have alpha */ + else if(len == 9) + { + colnum = strtoul(colstr + 1, &p, 16); + if(p - colstr != 9) + goto invalid; + req.alpha = RGB_COLOR_8_TO_16(colnum); + colnum >>= 8; + } + else + { + invalid: + warn("awesome: error, invalid color '%s'", colstr); + req.has_error = true; + return req; + } + + red = RGB_COLOR_8_TO_16(colnum >> 16); + green = RGB_COLOR_8_TO_16(colnum >> 8); + blue = RGB_COLOR_8_TO_16(colnum); + + req.is_hexa = true; + req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection, + s->default_colormap, + red, green, blue); + } + else + { + req.is_hexa = false; + req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection, + s->default_colormap, len, + colstr); + } + + req.has_error = false; + req.colstr = colstr; + + return req; +} + +/** Initialize a X color. + * \param req xcolor_init request. + * \return True if color allocation was successfull. + */ +bool +xcolor_init_reply(xcolor_init_request_t req) +{ + if(req.has_error) + return false; + + if(req.is_hexa) + { + xcb_alloc_color_reply_t *hexa_color; + + if((hexa_color = xcb_alloc_color_reply(globalconf.connection, + req.cookie_hexa, NULL))) + { + req.color->pixel = hexa_color->pixel; + req.color->red = hexa_color->red; + req.color->green = hexa_color->green; + req.color->blue = hexa_color->blue; + req.color->alpha = req.alpha; + req.color->initialized = true; + p_delete(&hexa_color); + return true; + } + } + else + { + xcb_alloc_named_color_reply_t *named_color; + + if((named_color = xcb_alloc_named_color_reply(globalconf.connection, + req.cookie_named, NULL))) + { + req.color->pixel = named_color->pixel; + req.color->red = named_color->visual_red; + req.color->green = named_color->visual_green; + req.color->blue = named_color->visual_blue; + req.color->alpha = req.alpha; + req.color->initialized = true; + p_delete(&named_color); + return true; + } + } + + warn("awesome: error, cannot allocate color '%s'", req.colstr); + return false; +} + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/color.h b/color.h new file mode 100644 index 00000000..40a6b308 --- /dev/null +++ b/color.h @@ -0,0 +1,57 @@ +/* + * color.h - color functions header + * + * Copyright © 2008-2009 Julien Danjou + * Copyright © 2009 Uli Schlachter + * + * 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. + * + * 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. + * + * 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., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#ifndef AWESOME_COLOR_H +#define AWESOME_COLOR_H + +#include "common/xutil.h" + +typedef struct +{ + uint32_t pixel; + uint16_t red; + uint16_t green; + uint16_t blue; + uint16_t alpha; + bool initialized; +} xcolor_t; + +typedef struct +{ + union + { + xcb_alloc_color_cookie_t cookie_hexa; + xcb_alloc_named_color_cookie_t cookie_named; + }; + + uint16_t alpha; + xcolor_t *color; + bool is_hexa, has_error; + const char *colstr; +} xcolor_init_request_t; + +xcolor_init_request_t xcolor_init_unchecked(xcolor_t *, const char *, ssize_t); +bool xcolor_init_reply(xcolor_init_request_t); + +#endif + +// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/draw.c b/draw.c index 9a650e58..7a574d67 100644 --- a/draw.c +++ b/draw.c @@ -728,130 +728,4 @@ draw_align_tostr(alignment_t a) } } -#define RGB_COLOR_8_TO_16(i) (65535 * ((i) & 0xff) / 255) - -/** Send a request to initialize a X color. - * \param color xcolor_t struct to store color into. - * \param colstr Color specification. - * \return request informations. - */ -xcolor_init_request_t -xcolor_init_unchecked(xcolor_t *color, const char *colstr, ssize_t len) -{ - xcb_screen_t *s = xutil_screen_get(globalconf.connection, globalconf.default_screen); - xcolor_init_request_t req; - unsigned long colnum; - uint16_t red, green, blue; - - p_clear(&req, 1); - - if(!len) - { - req.has_error = true; - return req; - } - - req.alpha = 0xffff; - req.color = color; - - /* The color is given in RGB value */ - if(colstr[0] == '#') - { - char *p; - - if(len == 7) - { - colnum = strtoul(colstr + 1, &p, 16); - if(p - colstr != 7) - goto invalid; - } - /* we have alpha */ - else if(len == 9) - { - colnum = strtoul(colstr + 1, &p, 16); - if(p - colstr != 9) - goto invalid; - req.alpha = RGB_COLOR_8_TO_16(colnum); - colnum >>= 8; - } - else - { - invalid: - warn("awesome: error, invalid color '%s'", colstr); - req.has_error = true; - return req; - } - - red = RGB_COLOR_8_TO_16(colnum >> 16); - green = RGB_COLOR_8_TO_16(colnum >> 8); - blue = RGB_COLOR_8_TO_16(colnum); - - req.is_hexa = true; - req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection, - s->default_colormap, - red, green, blue); - } - else - { - req.is_hexa = false; - req.cookie_named = xcb_alloc_named_color_unchecked(globalconf.connection, - s->default_colormap, len, - colstr); - } - - req.has_error = false; - req.colstr = colstr; - - return req; -} - -/** Initialize a X color. - * \param req xcolor_init request. - * \return True if color allocation was successfull. - */ -bool -xcolor_init_reply(xcolor_init_request_t req) -{ - if(req.has_error) - return false; - - if(req.is_hexa) - { - xcb_alloc_color_reply_t *hexa_color; - - if((hexa_color = xcb_alloc_color_reply(globalconf.connection, - req.cookie_hexa, NULL))) - { - req.color->pixel = hexa_color->pixel; - req.color->red = hexa_color->red; - req.color->green = hexa_color->green; - req.color->blue = hexa_color->blue; - req.color->alpha = req.alpha; - req.color->initialized = true; - p_delete(&hexa_color); - return true; - } - } - else - { - xcb_alloc_named_color_reply_t *named_color; - - if((named_color = xcb_alloc_named_color_reply(globalconf.connection, - req.cookie_named, NULL))) - { - req.color->pixel = named_color->pixel; - req.color->red = named_color->visual_red; - req.color->green = named_color->visual_green; - req.color->blue = named_color->visual_blue; - req.color->alpha = req.alpha; - req.color->initialized = true; - p_delete(&named_color); - return true; - } - } - - warn("awesome: error, cannot allocate color '%s'", req.colstr); - return false; -} - // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 diff --git a/draw.h b/draw.h index 517367cf..f196f225 100644 --- a/draw.h +++ b/draw.h @@ -28,6 +28,7 @@ #include #include "image.h" +#include "color.h" #include "common/array.h" /** Padding type */ @@ -43,16 +44,6 @@ typedef struct int right; } padding_t; -typedef struct -{ - uint32_t pixel; - uint16_t red; - uint16_t green; - uint16_t blue; - uint16_t alpha; - bool initialized; -} xcolor_t; - typedef enum { AlignLeft = (0), @@ -190,23 +181,6 @@ area_t draw_text_extents(draw_text_context_t *); alignment_t draw_align_fromstr(const char *, ssize_t); const char *draw_align_tostr(alignment_t); -typedef struct -{ - union - { - xcb_alloc_color_cookie_t cookie_hexa; - xcb_alloc_named_color_cookie_t cookie_named; - }; - - uint16_t alpha; - xcolor_t *color; - bool is_hexa, has_error; - const char *colstr; -} xcolor_init_request_t; - -xcolor_init_request_t xcolor_init_unchecked(xcolor_t *, const char *, ssize_t); -bool xcolor_init_reply(xcolor_init_request_t); - static inline void draw_text_context_wipe(draw_text_context_t *pdata) {