/* $Id$ * $URL$ * * mpd informations v0.82 * * Copyright (C) 2006 Stefan Kuhne * Copyright (C) 2007 Robert Buchholz * Copyright (C) 2008 Michael Vogt * Copyright (C) 2006 The LCD4Linux Team * * 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. * */ /* * changelog v0.5 (20.11.2007): * changed: mpd::artist(), mpd::title(), mpd::album() * init code, call only if a function is used. * removed: "old style" functions, duplicate code * fixed: strdup() mem leaks * added: getstate, function which return player status: play/pause/stop * getVolume, funtcion which returns the mpd volume * getFilename, return current filename * * * changelog v0.6 (05.12.2007): * changed: -connection handling * -verbose "NO TAG" messages * -init code * added: -added password support (MPD_PASSWORD env variable) * -mpd::getSongsInDb - how many songs are in the mpd db? * -mpd::getMpdUptime - uptime of mpd * -mpd::getMpdPlayTime - playtime of mpd * -mpd::getMpdDbPlayTime - playtime of all songs in mpd db * -basic error handler.. * -mpd configuration in lcd4linux.conf * -formatTime* - use those functions to format time values * removed: -reprand method * * * changelog v0.7 (14.12.2007): * changed: -connection handling improved, do not disconnect/reconnect for each query * -> uses less ressources * * changelog v0.8 (30.01.2008): * changed: -libmpd is not needed anymore, use libmpdclient.c instead * fixed: -getMpdUptime() * -getMpdPlaytime() * -type definition * added: -mpd::getSamplerateHz * -getAudioChannels * * changelog v0.83 (26.07.2008): * added: -mpd::cmd* commands * */ /* TODO: -what happens if the db is updating? */ #include "config.h" #include #include #include #include #include #include "debug.h" #include "plugin.h" #include "cfg.h" /* struct timeval */ #include /* source: http://www.musicpd.org/libmpdclient.shtml */ #include "libmpd/libmpdclient.h" #ifdef WITH_DMALLOC #include #endif #define TIMEOUT_IN_S 10 #define ERROR_DISPLAY 5 /* current song */ static int l_totalTimeSec; static int l_elapsedTimeSec; static int l_bitRate; static int l_repeatEnabled; static int l_randomEnabled; static int l_state; static int l_volume; static int l_numberOfSongs; static unsigned long l_uptime; static unsigned long l_playTime; static unsigned long l_dbPlayTime; static int l_playlistLength; /* pos in playlist */ static int l_currentSongPos; static unsigned int l_sampleRate; static int l_channels; static mpd_Song *currentSong; /* connection information */ static char host[255]; static char pw[255]; static int iport; static int plugin_enabled; static int waittime; struct timeval timestamp; static mpd_Connection *conn; static char Section[] = "Plugin:MPD"; static int errorcnt = 0; static int configure_mpd(void) { static int configured = 0; char *s; if (configured != 0) return configured; /* read enabled */ if (cfg_number(Section, "enabled", 0, 0, 1, &plugin_enabled) < 1) { plugin_enabled = 0; } if (plugin_enabled != 1) { info("[MPD] WARNING: Plugin is not enabled! (set 'enabled 1' to enable this plugin)"); configured = 1; return configured; } /* read server */ s = cfg_get(Section, "server", "localhost"); if (*s == '\0') { info("[MPD] empty '%s.server' entry from %s, assuming 'localhost'", Section, cfg_source()); strcpy(host, "localhost"); } else strcpy(host, s); free(s); /* read port */ if (cfg_number(Section, "port", 6600, 1, 65536, &iport) < 1) { info("[MPD] no '%s.port' entry from %s using MPD's default", Section, cfg_source()); } /* read minUpdateTime in ms */ if (cfg_number(Section, "minUpdateTime", 500, 1, 10000, &waittime) < 1) { info("[MPD] no '%s.minUpdateTime' entry from %s using MPD's default", Section, cfg_source()); } /* read password */ s = cfg_get(Section, "password", ""); if (*s == '\0') { info("[MPD] empty '%s.password' entry in %s, assuming none", Section, cfg_source()); memset(pw, 0, sizeof(pw)); } else { strcpy(pw, s); free(s); } debug("[MPD] connection detail: [%s:%d]", host, iport); configured = 1; return configured; } static int mpd_update() { int ret = -1; struct timeval now; /* reread every 1000 msec only */ gettimeofday(&now, NULL); int timedelta = (now.tv_sec - timestamp.tv_sec) * 1000 + (now.tv_usec - timestamp.tv_usec) / 1000; if (timedelta < waittime) { /* debug("[MPD] waittime not reached...\n"); */ return 1; } /* check if configured */ if (configure_mpd() < 0) { return -1; } /* check if connected */ if (conn == NULL || conn->error) { if (conn) { if (errorcnt < ERROR_DISPLAY) debug("[MPD] Error: [%s], try to reconnect to [%s]:[%i]\n", conn->errorStr, host, iport); mpd_closeConnection(conn); } else debug("[MPD] initialize connect to [%s]:[%i]\n", host, iport); conn = mpd_newConnection(host, iport, TIMEOUT_IN_S); if (conn->error) { if (errorcnt < ERROR_DISPLAY) error("[MPD] connection failed, give up..."); if (errorcnt == ERROR_DISPLAY) error("[MPD] stop logging, until connection is fixed!"); errorcnt++; gettimeofday(×tamp, NULL); return -1; } errorcnt = 0; debug("[MPD] connection fixed..."); } mpd_Status *status = NULL; mpd_Stats *stats = NULL; mpd_InfoEntity *entity; mpd_sendCommandListOkBegin(conn); mpd_sendStatsCommand(conn); if (conn->error) { error("[MPD] error: %s", conn->errorStr); return -1; } mpd_sendStatusCommand(conn); mpd_sendCurrentSongCommand(conn); mpd_sendCommandListEnd(conn); stats = mpd_getStats(conn); if (stats == NULL) { error("[MPD] error mpd_getStats: %s", conn->errorStr); goto cleanup; } mpd_nextListOkCommand(conn); if ((status = mpd_getStatus(conn)) == NULL) { error("[MPD] error mpd_nextListOkCommand: %s", conn->errorStr); goto cleanup; } mpd_nextListOkCommand(conn); while ((entity = mpd_getNextInfoEntity(conn))) { mpd_Song *song = entity->info.song; if (entity->type != MPD_INFO_ENTITY_TYPE_SONG) { mpd_freeInfoEntity(entity); continue; } if (currentSong != NULL) mpd_freeSong(currentSong); currentSong = mpd_songDup(song); mpd_freeInfoEntity(entity); } l_elapsedTimeSec = status->elapsedTime; l_totalTimeSec = status->totalTime; l_repeatEnabled = status->repeat; l_randomEnabled = status->random; l_bitRate = status->bitRate; l_state = status->state; l_volume = status->volume; l_playlistLength = status->playlistLength; l_currentSongPos = status->song + 1; l_sampleRate = status->sampleRate; l_channels = status->channels; l_numberOfSongs = stats->numberOfSongs; l_uptime = stats->uptime; l_playTime = stats->playTime; l_dbPlayTime = stats->dbPlayTime; /* sanity checks */ if (l_volume < 0 || l_volume > 100) l_volume = 0; if (l_bitRate < 0) l_bitRate = 0; if (l_elapsedTimeSec > l_totalTimeSec || l_elapsedTimeSec < 0) l_elapsedTimeSec = 0; ret = 0; cleanup: if (stats != NULL) mpd_freeStats(stats); if (status != NULL) mpd_freeStatus(status); if (conn->error) { error("[MPD] error: %s", conn->errorStr); return -1; } mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); return -1; } gettimeofday(×tamp, NULL); return ret; } static void elapsedTimeSec(RESULT * result) { double d; mpd_update(); d = (double) l_elapsedTimeSec; SetResult(&result, R_NUMBER, &d); } static void totalTimeSec(RESULT * result) { double d; mpd_update(); d = (double) l_totalTimeSec; SetResult(&result, R_NUMBER, &d); } static void bitRate(RESULT * result) { double d; mpd_update(); d = (double) l_bitRate; SetResult(&result, R_NUMBER, &d); } static void getRepeatInt(RESULT * result) { double d; mpd_update(); d = (double) l_repeatEnabled; SetResult(&result, R_NUMBER, &d); } static void getRandomInt(RESULT * result) { double d; mpd_update(); d = (double) l_randomEnabled; SetResult(&result, R_NUMBER, &d); } /* if no tag is availabe, use filename */ static void getArtist(RESULT * result) { mpd_update(); if (currentSong != NULL) { if (currentSong->artist != NULL) { SetResult(&result, R_STRING, currentSong->artist); } else { if (currentSong->file != NULL) SetResult(&result, R_STRING, currentSong->file); else SetResult(&result, R_STRING, ""); } } else SetResult(&result, R_STRING, ""); } static void getTitle(RESULT * result) { mpd_update(); if (currentSong != NULL) { if (currentSong->title != NULL) { SetResult(&result, R_STRING, currentSong->title); } else SetResult(&result, R_STRING, ""); } else SetResult(&result, R_STRING, ""); } static void getAlbum(RESULT * result) { mpd_update(); if (currentSong != NULL) { if (currentSong->album != NULL) SetResult(&result, R_STRING, currentSong->album); else SetResult(&result, R_STRING, ""); } else SetResult(&result, R_STRING, ""); } static void getFilename(RESULT * result) { mpd_update(); if (currentSong != NULL) { if (currentSong->file != NULL) SetResult(&result, R_STRING, currentSong->file); else SetResult(&result, R_STRING, ""); } else SetResult(&result, R_STRING, ""); } /* return player state: 0=unknown 1=play 2=pause 3=stop */ static void getStateInt(RESULT * result) { double ret; mpd_update(); switch (l_state) { case MPD_STATUS_STATE_PLAY: ret = 1; break; case MPD_STATUS_STATE_PAUSE: ret = 2; break; case MPD_STATUS_STATE_STOP: ret = 3; break; default: ret = 0; break; } SetResult(&result, R_NUMBER, &ret); } static void getVolume(RESULT * result) { double d; mpd_update(); d = (double) l_volume; /* return 0..100 or < 0 when failed */ SetResult(&result, R_NUMBER, &d); } /* return the # of songs in the mpd db .. */ static void getSongsInDb(RESULT * result) { double d; mpd_update(); d = (double) l_numberOfSongs; SetResult(&result, R_NUMBER, &d); } static void getMpdUptime(RESULT * result) { double d; mpd_update(); d = (double) l_uptime; SetResult(&result, R_NUMBER, &d); } static void getMpdPlayTime(RESULT * result) { double d; mpd_update(); d = (double) l_playTime; SetResult(&result, R_NUMBER, &d); } static void getMpdDbPlayTime(RESULT * result) { double d; mpd_update(); d = (double) l_dbPlayTime; SetResult(&result, R_NUMBER, &d); } static void getMpdPlaylistLength(RESULT * result) { double d; mpd_update(); d = (double) l_playlistLength; SetResult(&result, R_NUMBER, &d); } static void getCurrentSongPos(RESULT * result) { double d; mpd_update(); d = (double) l_currentSongPos; SetResult(&result, R_NUMBER, &d); } static void getAudioChannels(RESULT * result) { double d; mpd_update(); d = (double) l_channels; SetResult(&result, R_NUMBER, &d); } static void getSamplerateHz(RESULT * result) { double d; mpd_update(); d = (double) l_sampleRate; SetResult(&result, R_NUMBER, &d); } static void nextSong() { mpd_update(); if (currentSong != NULL) { mpd_sendNextCommand(conn); mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); } } } static void prevSong() { mpd_update(); if (currentSong != NULL) { mpd_sendPrevCommand(conn); mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); } } } static void stopSong() { mpd_update(); if (currentSong != NULL) { mpd_sendStopCommand(conn); mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); } } } static void pauseSong() { mpd_update(); if (currentSong != NULL) { if (l_state == MPD_STATUS_STATE_PAUSE) { mpd_sendPauseCommand(conn, 0); } else { mpd_sendPauseCommand(conn, 1); } mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); } } } static void volUp() { mpd_update(); if (currentSong != NULL) { l_volume += 5; if (l_volume > 100) l_volume = 100; mpd_sendSetvolCommand(conn, l_volume); mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); } } } static void volDown() { mpd_update(); if (currentSong != NULL) { if (l_volume > 5) l_volume -= 5; else l_volume = 0; mpd_sendSetvolCommand(conn, l_volume); mpd_finishCommand(conn); if (conn->error) { error("[MPD] error mpd_finishCommand: %s", conn->errorStr); } } } static void toggleRepeat() { mpd_update(); if (currentSong != NULL) {
/* $Id: widget_image.c,v 1.9 2006/06/21 05:12:43 reinelt Exp $
 *
 * image widget handling
 *
 * Copyright (C) 2006 Michael Reinelt <reinelt@eunet.at>
 * Copyright (C) 2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * 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.
 *
 *
 * $Log: widget_image.c,v $
 * Revision 1.9  2006/06/21 05:12:43  reinelt
 * added checks for libgd version 2 (thanks to Sam)
 *
 * Revision 1.8  2006/06/20 08:50:59  reinelt
 * widget_image linker error hopefully finally fixed
 *
 * Revision 1.7  2006/04/15 05:22:52  reinelt
 * mpd plugin from Stefan Kuhne
 *
 * Revision 1.6  2006/04/09 14:17:50  reinelt
 * autoconf/library fixes, image and graphic display inversion
 *
 * Revision 1.5  2006/02/25 13:36:33  geronet
 * updated indent.sh, applied coding style
 *
 * Revision 1.4  2006/02/19 07:20:54  reinelt
 * image support nearly finished
 *
 * Revision 1.3  2006/02/08 04:55:05  reinelt
 * moved widget registration to drv_generic_graphic
 *
 * Revision 1.2  2006/01/23 06:17:18  reinelt
 * timer widget added
 *
 * Revision 1.1  2006/01/22 09:16:11  reinelt
 * Image Widget framework added
 *
 */

/* 
 * exported functions:
 *
 * WIDGET_CLASS Widget_Image
 *   the image widget
 *
 */


#include "config.h"

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

#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "evaluator.h"
#include "timer.h"
#include "widget.h"
#include "widget_image.h"
#include "rgb.h"

#ifdef HAVE_GD_GD_H
#include <gd/gd.h>
#else
#ifdef HAVE_GD_H
#include <gd.h>
#else
#error "gd.h not found!"
#error "cannot compile image widget"
#endif
#endif

#if GD2_VERS != 2
#error "lcd4linux requires libgd version 2"
#error "cannot compile image widget"
#endif

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


static void widget_image_render(const char *Name, WIDGET_IMAGE * Image)
{
    int x, y;
    FILE *fd;
    gdImagePtr gdImage = NULL;

    /* clear bitmap */
    if (Image->bitmap) {
	int i;
	for (i = 0; i < Image->height * Image->width; i++) {
	  RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 };
	    Image->bitmap[i] = empty;
	}
    }

    if (Image->file == NULL || Image->file[0] == '\0') {
	error("Warning: Image %s has no file", Name);
	return;
    }

    fd = fopen(Image->file, "rb");
    if (fd == NULL) {
	error("Warning: Image %s: fopen(%s) failed: %s", Name, Image->file, strerror(errno));
	return;
    }

    gdImage = gdImageCreateFromPng(fd);
    fclose(fd);

    if (fd == NULL) {
	error("Warning: Image %s: CreateFromPng(%s) failed!", Name, Image->file);
	return;
    }

    /* maybe resize bitmap */
    if (gdImage->sx > Image->width) {
	Image->width = gdImage->sx;
	if (Image->bitmap)
	    free(Image->bitmap);
	Image->bitmap = NULL;
    }
    if (gdImage->sy > Image->height) {
	Image->height = gdImage->sy;
	if (Image->bitmap)
	    free(Image->bitmap);
	Image->bitmap = NULL;
    }
    if (Image->bitmap == NULL && Image->width > 0 && Image->height > 0) {
	int i = Image->width * Image->height * sizeof(Image->bitmap[0]);
	Image->bitmap = malloc(i);
	if (Image->bitmap == NULL) {
	    error("Warning: Image %s: malloc(%d) failed!", Name, i, strerror(errno));
	    return;
	}
	for (i = 0; i < Image->height * Image->width; i++) {
	  RGBA empty = { R: 0x00, G: 0x00, B: 0x00, A:0x00 };
	    Image->bitmap[i] = empty;
	}
    }

    /* finally really render it */
    if (Image->visible) {
	for (x = 0; x < gdImage->sx; x++) {
	    for (y = 0; y < gdImage->sy; y++) {
		int p = gdImageGetTrueColorPixel(gdImage, x, y);
		int a = gdTrueColorGetAlpha(p);
		int i = y * Image->width + x;
		Image->bitmap[i].R = gdTrueColorGetRed(p);
		Image->bitmap[i].G = gdTrueColorGetGreen(p);
		Image->bitmap[i].B = gdTrueColorGetBlue(p);
		/* GD's alpha is 0 (opaque) to 127 (tranparanet) */
		/* our alpha is 0 (transparent) to 255 (opaque) */
		Image->bitmap[i].A = (a == 127) ? 0 : 255 - 2 * a;
		if (Image->inverted) {
		    Image->bitmap[i].R = 255 - Image->bitmap[i].R;
		    Image->bitmap[i].G = 255 - Image->bitmap[i].G;
		    Image->bitmap[i].B = 255 - Image->bitmap[i].B;
		}
	    }
	}
    }
}


static void widget_image_update(void *Self)
{
    WIDGET *W = (WIDGET *) Self;
    WIDGET_IMAGE *Image = W->data;
    RESULT result = { 0, 0, 0, NULL };

    /* process the parent only */
    if (W->parent == NULL) {

	/* evaluate expressions */
	if (Image->file) {
	    free(Image->file);
	    Image->file = NULL;
	}
	if (Image->file_tree != NULL) {
	    Eval(Image->file_tree, &result);
	    Image->file = strdup(R2S(&result));
	    DelResult(&result);
	}

	Image->update = 0;
	if (Image->update_tree != NULL) {
	    Eval(Image->update_tree, &result);
	    Image->update = R2N(&result);
	    if (Image->update < 0)
		Image->update = 0;
	    DelResult(&result);
	}

	Image->visible = 1;
	if (Image->visible_tree != NULL) {
	    Eval(Image->visible_tree, &result);
	    Image->visible = R2N(&result);
	    Image->visible = Image->visible > 0;
	    DelResult(&result);
	}

	Image->inverted = 0;
	if (Image->inverted_tree != NULL) {
	    Eval(Image->inverted_tree, &result);
	    Image->inverted = R2N(&result);
	    Image->inverted = Image->inverted > 0;
	    DelResult(&result);
	}

	/* render image into bitmap */
	widget_image_render(W->name, Image);

    }

    /* finally, draw it! */
    if (W->class->draw)
	W->class->draw(W);

    /* add a new one-shot timer */
    timer_add(widget_image_update, Self, Image->update, 1);

}



int widget_image_init(WIDGET * Self)
{
    char *section;
    WIDGET_IMAGE *Image;

    /* 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);

	Image = malloc(sizeof(WIDGET_IMAGE));
	memset(Image, 0, sizeof(WIDGET_IMAGE));

	/* initial size */
	Image->width = 0;
	Image->height = 0;
	Image->bitmap = NULL;
	Image->file = NULL;

	/* get raw expressions (we evaluate them ourselves) */
	Image->file_expr = cfg_get_raw(section, "file", NULL);
	Image->update_expr = cfg_get_raw(section, "update", NULL);
	Image->visible_expr = cfg_get_raw(section, "visible", NULL);
	Image->inverted_expr = cfg_get_raw(section, "inverted", NULL);

	/* sanity checks */
	if (Image->file_expr == NULL || *Image->file_expr == '\0') {
	    error("Warning: Image %s has no file", Self->name);
	}
	if (Image->update_expr == NULL || *Image->update_expr == '\0') {
	    error("Image %s has no update, using '100'", Self->name);
	    Image->update_expr = "100";
	}

	/* compile'em */
	Compile(Image->file_expr, &Image->file_tree);
	Compile(Image->update_expr, &Image->update_tree);
	Compile(Image->visible_expr, &Image->visible_tree);
	Compile(Image->inverted_expr, &Image->inverted_tree);

	free(section);
	Self->data = Image;

    } else {

	/* re-use the parent */
	Self->data = Self->parent->data;

    }

    /* just do it! */
    widget_image_update(Self);

    return 0;
}


int widget_image_quit(WIDGET * Self)
{
    if (Self) {
	/* do not deallocate child widget! */
	if (Self->parent == NULL) {
	    if (Self->data) {
		WIDGET_IMAGE *Image = Self->data;
		if (Image->bitmap)
		    free(Image->bitmap);
		if (Image->file)
		    free(Image->file);
		DelTree(Image->file_tree);
		DelTree(Image->update_tree);
		DelTree(Image->visible_tree);
		DelTree(Image->inverted_tree);
		free(Self->data);
		Self->data = NULL;
	    }
	}
    }

    return 0;

}



WIDGET_CLASS Widget_Image = {
  name:"image",
  type:WIDGET_TYPE_XY,
  init:widget_image_init,
  draw:NULL,
  quit:widget_image_quit,
};