From cd5e3f8fa02cf3463b7c95fd1229efa3b0975607 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 8 Sep 2010 17:30:04 +0200 Subject: [PATCH] Make font and xftalpha lua settings --- src/conky.cc | 40 ++-------------- src/fonts.cc | 122 +++++++++++++++++++++++------------------------- src/fonts.h | 32 ++++++++++--- src/specials.cc | 3 +- 4 files changed, 88 insertions(+), 109 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 473ce89e..3bdabba7 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2543,8 +2543,7 @@ void clean_up_without_threads(void *memtofree1, void* memtofree2) if (x_initialised == YES) { clean_up_x11(); }else{ - free(fonts); //in set_default_configurations a font is set but not loaded - font_count = -1; + fonts.clear(); //in set_default_configurations a font is set but not loaded } #endif /* BUILD_X11 */ @@ -2622,9 +2621,6 @@ static void set_default_configurations(void) state->pushboolean(true); out_to_stdout.lua_set(*state); #endif -#ifdef BUILD_X11 - set_first_font("6x10"); -#endif /* BUILD_X11 */ set_update_interval(3); update_interval_bat = NOBATTERY; @@ -2772,37 +2768,6 @@ char load_config_file(const char *f) CONF_ERR; } } -#ifdef BUILD_X11 -#ifdef BUILD_XFT - CONF("font") { - if (value) { - set_first_font(value); - } - } - CONF("xftalpha") { - if (value && font_count >= 0) { - fonts[0].font_alpha = atof(value) * 65535.0; - } - } - CONF("xftfont") { - if (use_xft.get(*state)) { -#else - CONF("xftfont") { - /* xftfont silently ignored when no Xft */ - } - CONF("xftalpha") { - /* xftalpha is silently ignored when no Xft */ - } - CONF("font") { -#endif - if (value) { - set_first_font(value); - } -#ifdef BUILD_XFT - } -#endif - } -#endif /* BUILD_X11 */ CONF("update_interval_on_battery") { if (value) { update_interval_bat = strtod(value, 0); @@ -3107,7 +3072,8 @@ void initialisation(int argc, char **argv) { break; #ifdef BUILD_X11 case 'f': - set_first_font(optarg); + state->pushstring(optarg); + font.lua_set(*state); break; case 'a': state->pushstring(optarg); diff --git a/src/fonts.cc b/src/fonts.cc index 7e9ccc82..65388f90 100644 --- a/src/fonts.cc +++ b/src/fonts.cc @@ -32,16 +32,61 @@ #include "logging.h" int selected_font = 0; -int font_count = -1; -struct font_list *fonts = NULL; +std::vector fonts; char fontloaded = 0; +void font_setting::lua_setter(lua::state &l, bool init) +{ + lua::stack_sentry s(l, -2); + + Base::lua_setter(l, init); + + if(init && out_to_x.get(*state)) { + if(fonts.size() == 0) + fonts.resize(1); + fonts[0].name = do_convert(l, -1).first; + } + + ++s; +} + +font_setting font; + +#ifdef BUILD_XFT +namespace { + class xftalpha_setting: public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + + protected: + virtual void lua_setter(lua::state &l, bool init) + { + lua::stack_sentry s(l, -2); + + Base::lua_setter(l, init); + + if(init && out_to_x.get(*state)) { + fonts[0].font_alpha = do_convert(l, -1).first * 0xffff; + } + + ++s; + } + + public: + xftalpha_setting() + : Base("xftalpha", 1.0, false) + {} + }; + + xftalpha_setting xftalpha; +} +#endif /* BUILD_XFT */ + void set_font(void) { #ifdef BUILD_XFT if (use_xft.get(*state)) return; #endif /* BUILD_XFT */ - if (font_count > -1 && fonts[selected_font].font) { + if (fonts[selected_font].font) { XSetFont(display, window.gc, fonts[selected_font].font->fid); } } @@ -69,64 +114,18 @@ int add_font(const char *data_in) if (not out_to_x.get(*state)) { return 0; } - if (font_count > MAX_FONTS) { - CRIT_ERR(NULL, NULL, "you don't need that many fonts, sorry."); - } - font_count++; - if (font_count == 0) { - free_and_zero(fonts); - if ((fonts = (struct font_list *) malloc(sizeof(struct font_list))) - == NULL) { - CRIT_ERR(NULL, NULL, "malloc"); - } - memset(fonts, 0, sizeof(struct font_list)); - } - fonts = (font_list*) realloc(fonts, (sizeof(struct font_list) * (font_count + 1))); - memset(&fonts[font_count], 0, sizeof(struct font_list)); - if (fonts == NULL) { - CRIT_ERR(NULL, NULL, "realloc in add_font"); - } - // must account for null terminator - if (strlen(data_in) < DEFAULT_TEXT_BUFFER_SIZE) { - strncpy(fonts[font_count].name, data_in, DEFAULT_TEXT_BUFFER_SIZE); -#ifdef BUILD_XFT - fonts[font_count].font_alpha = 0xffff; -#endif - } else { - CRIT_ERR(NULL, NULL, "Oops...looks like something overflowed in add_font()."); - } - return font_count; -} + fonts.push_back(font_list()); + fonts.rbegin()->name = data_in; -void set_first_font(const char *data_in) -{ - if (not out_to_x.get(*state)) { - return; - } - if (font_count < 0) { - if ((fonts = (struct font_list *) malloc(sizeof(struct font_list))) - == NULL) { - CRIT_ERR(NULL, NULL, "malloc"); - } - memset(fonts, 0, sizeof(struct font_list)); - font_count++; - } - if (strlen(data_in) > 1) { - strncpy(fonts[0].name, data_in, DEFAULT_TEXT_BUFFER_SIZE); -#ifdef BUILD_XFT - fonts[0].font_alpha = 0xffff; -#endif - } + return fonts.size()-1; } void free_fonts(void) { - int i; - if (not out_to_x.get(*state)) { return; } - for (i = 0; i <= font_count; i++) { + for (size_t i = 0; i < fonts.size(); i++) { #ifdef BUILD_XFT if (use_xft.get(*state)) { /* @@ -136,18 +135,15 @@ void free_fonts(void) * * XftFontClose(display, fonts[i].xftfont); */ - fonts[i].xftfont = 0; } else #endif /* BUILD_XFT */ { if (fonts[i].font) { XFreeFont(display, fonts[i].font); - fonts[i].font = 0; } } } - free_and_zero(fonts); - font_count = -1; + fonts.clear(); selected_font = 0; #ifdef BUILD_XFT if (window.xftdraw) { @@ -159,22 +155,20 @@ void free_fonts(void) void load_fonts(void) { - int i; - if (not out_to_x.get(*state)) return; - for (i = 0; i <= font_count; i++) { + for (size_t i = 0; i <= fonts.size(); i++) { #ifdef BUILD_XFT /* load Xft font */ if (use_xft.get(*state)) { if(not fonts[i].xftfont) - fonts[i].xftfont = XftFontOpenName(display, screen, fonts[i].name); + fonts[i].xftfont = XftFontOpenName(display, screen, fonts[i].name.c_str()); if (fonts[i].xftfont) { continue; } - NORM_ERR("can't load Xft font '%s'", fonts[i].name); + NORM_ERR("can't load Xft font '%s'", fonts[i].name.c_str()); if ((fonts[i].xftfont = XftFontOpenName(display, screen, "courier-12")) != NULL) { continue; @@ -186,8 +180,8 @@ void load_fonts(void) } #endif /* load normal font */ - if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name)) == NULL) { - NORM_ERR("can't load font '%s'", fonts[i].name); + if (!fonts[i].font && (fonts[i].font = XLoadQueryFont(display, fonts[i].name.c_str())) == NULL) { + NORM_ERR("can't load font '%s'", fonts[i].name.c_str()); if ((fonts[i].font = XLoadQueryFont(display, "fixed")) == NULL) { CRIT_ERR(NULL, NULL, "can't load font '%s'", "fixed"); } diff --git a/src/fonts.h b/src/fonts.h index dc5e4b7d..5ac3c4f8 100644 --- a/src/fonts.h +++ b/src/fonts.h @@ -31,19 +31,27 @@ #ifndef _FONTS_H #define _FONTS_H +#include + #include "x11.h" /* for fonts */ struct font_list { - char name[DEFAULT_TEXT_BUFFER_SIZE]; - int num; + std::string name; XFontStruct *font; #ifdef BUILD_XFT XftFont *xftfont; int font_alpha; #endif + + font_list() + : name(), font(NULL) +#ifdef BUILD_XFT + , xftfont(NULL), font_alpha(0xffff) +#endif + {} }; #ifdef BUILD_XFT @@ -66,19 +74,29 @@ struct font_list { #endif -#define MAX_FONTS 256 - /* direct access to registered fonts (FIXME: bad encapsulation) */ -extern struct font_list *fonts; +extern std::vector fonts; extern int selected_font; -extern int font_count; void setup_fonts(void); void set_font(void); int add_font(const char *); -void set_first_font(const char *); void free_fonts(void); void load_fonts(void); +class font_setting: public conky::simple_config_setting { + typedef conky::simple_config_setting Base; + +protected: + virtual void lua_setter(lua::state &l, bool init); + +public: + font_setting() + : Base("font", "6x10", false) + {} +}; + +extern font_setting font; + #endif /* _FONTS_H */ #endif /* BUILD_X11 */ diff --git a/src/specials.cc b/src/specials.cc index a9ee7888..da0001f3 100644 --- a/src/specials.cc +++ b/src/specials.cc @@ -352,7 +352,8 @@ void new_font(struct text_object *obj, char *p, int p_max_size) s = new_special(p, FONT); if (obj->data.s) { - if (s->font_added > font_count || !s->font_added || (strncmp(obj->data.s, fonts[s->font_added].name, DEFAULT_TEXT_BUFFER_SIZE) != EQUAL) ) { + if (s->font_added >= (int)fonts.size() || !s->font_added + || obj->data.s != fonts[s->font_added].name ) { selected_font = s->font_added = add_font(obj->data.s); selected_font = tmp; }