/* $Id$ * $URL$ * * icon widget handling * * Copyright (C) 2003, 2004 Michael Reinelt * Copyright (C) 2004 The LCD4Linux Team * * 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, 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., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* * exported functions: * * WIDGET_CLASS Widget_Icon * the icon widget * */ #include "config.h" #include #include #include #include #include "debug.h" #include "cfg.h" #include "qprintf.h" #include "property.h" #include "timer.h" #include "widget.h" #include "widget_icon.h" #ifdef WITH_DMALLOC #include #endif /* icons size is same as char size */ extern int XRES, YRES; static void widget_icon_read_bitmap(const char *section, WIDGET_ICON * Icon) { int row, n; char key[15]; char *val, *v; unsigned char *map; for (row = 0; row < YRES; row++) { qprintf(key, sizeof(key), "Bitmap.Row%d", row + 1); val = cfg_get(section, key, ""); map = Icon->bitmap + row; n = 0; for (v = val; *v != '\0'; v++) { if (n >= Icon->maxmap) { Icon->maxmap++; Icon->bitmap = realloc(Icon->bitmap, Icon->maxmap * YRES * sizeof(char)); memset(Icon->bitmap + n * YRES, 0, YRES * sizeof(char)); map = Icon->bitmap + n * YRES + row; } switch (*v) { case '|': n++; map += YRES; break; case '*': (*map) <<= 1; (*map) |= 1; break; default: (*map) <<= 1; } } free(val); } } void widget_icon_update(void *Self) { WIDGET *W = (WIDGET *) Self; WIDGET_ICON *Icon = W->data; /* process the parent only */ if (W->parent == NULL) { /* evaluate properties */ property_eval(&Icon->speed); property_eval(&Icon->visible); /* rotate icon bitmap */ Icon->curmap++; if (Icon->curmap >= Icon->maxmap) Icon->curmap = 0; } /* finally, draw it! */ if (W->class->draw) W->class->draw(W); /* add a new one-shot timer */ if (P2N(&Icon->speed) > 0) { timer_add(widget_icon_update, Self, P2N(&Icon->speed), 1); } } int widget_icon_init(WIDGET * Self) { char *section; WIDGET_ICON *Icon; /* re-use the parent if one exists */ if (Self->parent == NULL) { /* prepare config section */ /* strlen("Widget:")=7 */ section = malloc(strlen(Self->name) + 8); strcpy(section, "Widget:"); strcat(section, Self->name); Icon = malloc(sizeof(WIDGET_ICON)); memset(Icon, 0, sizeof(WIDGET_ICON)); /* load properties */ property_load(section, "speed", "100", &Icon->speed); property_load(section, "visible", "1", &Icon->visible); /* read bitmap */ widget_icon_read_bitmap(section, Icon); free(section); Self->data = Icon; /* as the speed is evaluatod on every call, we use 'one-shot'-timers. */ /* The timer will be reactivated on every call to widget_icon_update(). */ /* We do the initial call here... */ Icon->prvmap = -1; /* reset ascii */ Icon->ascii = -1; } else { /* re-use the parent */ Self->data = Self->parent->data; } /* just do it! */ widget_icon_update(Self); return 0; } int widget_icon_quit(WIDGET * Self) { if (Self) { /* do not deallocate child widget! */ if (Self->parent == NULL) { if (Self->data) { WIDGET_ICON *Icon = Self->data; property_free(&Icon->speed); property_free(&Icon->visible); if (Icon->bitmap) free(Icon->bitmap); free(Self->data); Self->data = NULL; } } } return 0; } WIDGET_CLASS Widget_Icon = { .name = "icon", .type = WIDGET_TYPE_RC, .init = widget_icon_init, .draw = NULL, .quit = widget_icon_quit, }; /a> 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/* $Id$
 * $URL$
 *
 * iconv charset conversion plugin
 *
 * Copyright (C) 2006 Ernst Bachmann <e.bachmann@xebec.de>
 * Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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, or (at your option)
 * any later version.
 *
 * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_iconv (void)
 * int plugin_exit_iconv (void)
 *
 */


#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iconv.h>
#include <errno.h>

/* these should always be included */
#include "debug.h"
#include "plugin.h"

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif



/* iconv function, convert charsets */
/* valid "to" and "from" charsets can be listed by running "iconv --list" from a shell */
/* utf16 & utf32 encodings won't work, as they contain null bytes, confusing strlen */
static void my_iconv(RESULT * result, RESULT * charset_from, RESULT * charset_to, RESULT * arg)
{
    char *source;
    size_t source_left;
    char *dest;
    char *dest_pos;
    size_t dest_left;
    iconv_t cd;

    source = R2S(arg);
    source_left = strlen(source);

    /* use twice the memory needed in best case, but save lots of reallocs in worst case */
    /* increase to 4 if most conversions are to utf32 (quite unlikely) */
    /* also alloc a "safety byte" so we can always zero-terminate the string. */

    dest_left = 2 * source_left;
    dest = malloc(dest_left + 1);
    dest_pos = dest;

    cd = iconv_open(R2S(charset_to), R2S(charset_from));
    if (cd != (iconv_t) (-1)) {

	do {

	    /* quite spammy: debug("plugin_iconv: calling iconv with %ld,[%s]/%ld,%ld", cd, source, source_left, dest_left); */
	    if (iconv(cd, &source, &source_left, &dest_pos, &dest_left) == (size_t) (-1)) {
		switch (errno) {
		case EILSEQ:
		    /* illegal bytes in input sequence */
		    /* try to fix by skipping a byte */
		    info("plugin_iconv: illegal character in input string: %c", *source);
		    source_left--;
		    source++;
		    break;
		case EINVAL:
		    /* input string ends during a multibyte sequence */
		    /* try to fix by simply ignoring */
		    info("plugin_iconv: illegal character at end of input");
		    source_left = 0;
		    break;
		case E2BIG:
		    /* not enough bytes in outbuf. */
		    /* TODO: Realloc output buffer, probably doubling its size? */
		    /* for now, just bail out. For lcd4linux 99% of all conversions will go to ascii or latin1 anyways */
		    error
			("plugin_iconv: out of memory in destination buffer. Seems like Ernst was too lazy, complain to him!");
		    source_left = 0;
		    break;
		default:
		    error("plugin_iconv: strange errno state (%d) occurred", errno);
		    source_left = 0;
		}
	    }
	} while (source_left > 0);	/* don't check for == 0, could be negative in EILSEQ case */

	/* terminate the string, we're sure to have that byte left, see above */
	*dest_pos = 0;
	dest_pos++;

	iconv_close(cd);
    } else {
	error("plugin_iconv: could not open conversion descriptor. Check if your charsets are supported!");
	/* guaranteed to fit. */
	strcpy(dest, source);
    }

    SetResult(&result, R_STRING, dest);

    free(dest);
}


/* plugin initialization */
int plugin_init_iconv(void)
{

    AddFunction("iconv", 3, my_iconv);

    return 0;
}

void plugin_exit_iconv(void)
{
    /* nothing to clean */
}