From 854e71168960439ed10d6ac057725dbe42f95929 Mon Sep 17 00:00:00 2001 From: reinelt Date: Tue, 3 May 2005 11:13:24 +0000 Subject: [lcd4linux @ 2005-05-03 11:13:23 by reinelt] rearranged autoconf a bit, libX11 will be linked only if really needed (i.e. when the X11 driver has been selected) plugin_python filled with life git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@537 3ae390bd-cb1e-0410-b409-cd5a39f66f1f --- Makefile.am | 4 +-- Makefile.in | 4 +-- configure | 19 +++++----- drivers.m4 | 11 +++--- plugin_python.c | 110 +++++++++++++++++++++++++++++++++++++++++++++++++------- plugins.m4 | 4 ++- 6 files changed, 122 insertions(+), 30 deletions(-) diff --git a/Makefile.am b/Makefile.am index 7caf41e..d98a71f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,9 +7,9 @@ PLUGINS=@PLUGINS@ bin_PROGRAMS = lcd4linux -AM_CFLAGS = $(X_CFLAGS) -D_GNU_SOURCE -Wall -W +AM_CFLAGS = -D_GNU_SOURCE -Wall -W -lcd4linux_LDFLAGS = $(X_LIBS) +lcd4linux_LDFLAGS = lcd4linux_LDADD = @DRIVERS@ @PLUGINS@ @DRVLIBS@ @PLUGINLIBS@ lcd4linux_DEPENDENCIES = @DRIVERS@ @PLUGINS@ diff --git a/Makefile.in b/Makefile.in index ba776b9..f63fc60 100644 --- a/Makefile.in +++ b/Makefile.in @@ -95,9 +95,9 @@ PLUGINS = @PLUGINS@ bin_PROGRAMS = lcd4linux -AM_CFLAGS = $(X_CFLAGS) -D_GNU_SOURCE -Wall -W +AM_CFLAGS = -D_GNU_SOURCE -Wall -W -lcd4linux_LDFLAGS = $(X_LIBS) +lcd4linux_LDFLAGS = lcd4linux_LDADD = @DRIVERS@ @PLUGINS@ @DRVLIBS@ @PLUGINLIBS@ lcd4linux_DEPENDENCIES = @DRIVERS@ @PLUGINS@ diff --git a/configure b/configure index 448ab60..075e9c0 100755 --- a/configure +++ b/configure @@ -5921,12 +5921,12 @@ if test "$BWCT" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_BWCT.o" + DRVLIBS="$DRVLIBS -lusb" cat >>confdefs.h <<\_ACEOF #define WITH_BWCT 1 _ACEOF - DRVLIBS="$DRVLIBS -lusb" else { echo "$as_me:$LINENO: WARNING: usb.h not found: BWCT driver disabled" >&5 echo "$as_me: WARNING: usb.h not found: BWCT driver disabled" >&2;} @@ -6047,12 +6047,12 @@ if test "$PNG" = "yes"; then if test "$has_gd" = "true"; then GRAPHIC="yes" IMAGE="yes" + DRVLIBS="$DRVLIBS -lgd" cat >>confdefs.h <<\_ACEOF #define WITH_PNG 1 _ACEOF - DRVLIBS="$DRVLIBS -lgd" else { echo "$as_me:$LINENO: WARNING: gd.h not found: PNG driver disabled" >&5 echo "$as_me: WARNING: gd.h not found: PNG driver disabled" >&2;} @@ -6109,12 +6109,12 @@ if test "$Trefon" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_Trefon.o" + DRVLIBS="$DRVLIBS -lusb" cat >>confdefs.h <<\_ACEOF #define WITH_TREFON 1 _ACEOF - DRVLIBS="$DRVLIBS -lusb" else { echo "$as_me:$LINENO: WARNING: usb.h not found: Trefon driver disabled" >&5 echo "$as_me: WARNING: usb.h not found: Trefon driver disabled" >&2;} @@ -6125,14 +6125,14 @@ if test "$USBLCD" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_USBLCD.o" + if test "$has_usb" = "true"; then + DRVLIBS="$DRVLIBS -lusb" + fi cat >>confdefs.h <<\_ACEOF #define WITH_USBLCD 1 _ACEOF - if test "$has_usb" = "true"; then - DRVLIBS="$DRVLIBS -lusb" - fi fi if test "$X11" = "yes"; then @@ -6143,6 +6143,7 @@ echo "$as_me: WARNING: X11 headers or libraries not available: X11 driver disabl GRAPHIC="yes" DRIVERS="$DRIVERS drv_X11.o" DRVLIBS="$DRVLIBS -L$ac_x_libraries -lX11" + CPP_FLAGS="$CPPFLAGS $X_CFLAGS" cat >>confdefs.h <<\_ACEOF #define WITH_X11 1 @@ -6184,12 +6185,12 @@ fi # generic i2c driver if test "$I2C" = "yes"; then if test "$has_i2c" = true; then + DRIVERS="$DRIVERS drv_generic_i2c.o" cat >>confdefs.h <<\_ACEOF #define WITH_I2C 1 _ACEOF - DRIVERS="$DRIVERS drv_generic_i2c.o" else I2C="no" { echo "$as_me:$LINENO: WARNING: I2C include files not found: I2C bus driver disabled" >&5 @@ -6953,12 +6954,12 @@ fi if test "$has_mysql_lib" = "true"; then PLUGINS="$PLUGINS plugin_mysql.o" + PLUGINLIBS="$PLUGINLIBS -lmysqlclient" cat >>confdefs.h <<\_ACEOF #define PLUGIN_MYSQL 1 _ACEOF - PLUGINLIBS="$PLUGINLIBS -lmysqlclient" else { echo "$as_me:$LINENO: WARNING: mysqlclient lib not found: mysql plugin disabled" >&5 echo "$as_me: WARNING: mysqlclient lib not found: mysql plugin disabled" >&2;} @@ -7162,6 +7163,8 @@ if test "$PLUGIN_PYTHON" = "yes"; then echo "$as_me: WARNING: python headers not found: python plugin disabled" >&2;} else PLUGINS="$PLUGINS plugin_python.o" + CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS" + PLUGINLIBS="$PLUGINLIBS $PYTHON_LDFLAGS $PYTHON_EXTRA_LIBS" cat >>confdefs.h <<\_ACEOF #define PLUGIN_PYTHON 1 diff --git a/drivers.m4 b/drivers.m4 index 5b44db6..ce2a011 100644 --- a/drivers.m4 +++ b/drivers.m4 @@ -164,8 +164,8 @@ if test "$BWCT" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_BWCT.o" - AC_DEFINE(WITH_BWCT,1,[BWCT driver]) DRVLIBS="$DRVLIBS -lusb" + AC_DEFINE(WITH_BWCT,1,[BWCT driver]) else AC_MSG_WARN(usb.h not found: BWCT driver disabled) fi @@ -244,8 +244,8 @@ if test "$PNG" = "yes"; then if test "$has_gd" = "true"; then GRAPHIC="yes" IMAGE="yes" - AC_DEFINE(WITH_PNG,1,[ driver]) DRVLIBS="$DRVLIBS -lgd" + AC_DEFINE(WITH_PNG,1,[ driver]) else AC_MSG_WARN(gd.h not found: PNG driver disabled) fi @@ -285,8 +285,8 @@ if test "$Trefon" = "yes"; then if test "$has_usb" = "true"; then TEXT="yes" DRIVERS="$DRIVERS drv_Trefon.o" - AC_DEFINE(WITH_TREFON,1,[TREFON driver]) DRVLIBS="$DRVLIBS -lusb" + AC_DEFINE(WITH_TREFON,1,[TREFON driver]) else AC_MSG_WARN(usb.h not found: Trefon driver disabled) fi @@ -296,10 +296,10 @@ if test "$USBLCD" = "yes"; then TEXT="yes" SERIAL="yes" DRIVERS="$DRIVERS drv_USBLCD.o" - AC_DEFINE(WITH_USBLCD,1,[USBLCD driver]) if test "$has_usb" = "true"; then DRVLIBS="$DRVLIBS -lusb" fi + AC_DEFINE(WITH_USBLCD,1,[USBLCD driver]) fi if test "$X11" = "yes"; then @@ -309,6 +309,7 @@ if test "$X11" = "yes"; then GRAPHIC="yes" DRIVERS="$DRIVERS drv_X11.o" DRVLIBS="$DRVLIBS -L$ac_x_libraries -lX11" + CPP_FLAGS="$CPPFLAGS $X_CFLAGS" AC_DEFINE(WITH_X11, 1, [X11 driver]) fi fi @@ -344,8 +345,8 @@ fi # generic i2c driver if test "$I2C" = "yes"; then if test "$has_i2c" = true; then - AC_DEFINE(WITH_I2C, 1, [I2C bus driver]) DRIVERS="$DRIVERS drv_generic_i2c.o" + AC_DEFINE(WITH_I2C, 1, [I2C bus driver]) else I2C="no" AC_MSG_WARN(I2C include files not found: I2C bus driver disabled) diff --git a/plugin_python.c b/plugin_python.c index 88e60e8..7b61096 100644 --- a/plugin_python.c +++ b/plugin_python.c @@ -1,8 +1,8 @@ -/* $Id: plugin_python.c,v 1.1 2005/05/02 10:29:20 reinelt Exp $ +/* $Id: plugin_python.c,v 1.2 2005/05/03 11:13:24 reinelt Exp $ * * Python plugin * - * Copyright 2005 Fixme! + * Copyright 2005 Dan Fritz * Copyright 2005 The LCD4Linux Team * * This file is part of LCD4Linux. @@ -23,6 +23,11 @@ * * * $Log: plugin_python.c,v $ + * Revision 1.2 2005/05/03 11:13:24 reinelt + * rearranged autoconf a bit, + * libX11 will be linked only if really needed (i.e. when the X11 driver has been selected) + * plugin_python filled with life + * * Revision 1.1 2005/05/02 10:29:20 reinelt * preparations for python bindings and python plugin * @@ -36,6 +41,7 @@ * */ +#include #include "config.h" @@ -46,25 +52,105 @@ #include "plugin.h" #include "hash.h" -// Fixme: #include "lcd4linux_wrap.h" +/* + * Executes a python function specified by function name and module. + * + * This method is more or less a copy of an example found in the python + * documentation. Kudos goes to Guido van Rossum and Fred L. Drake. + * + * Returns a char* directly from PyString_AsString() !!! DO NOT DEALLOCATE !!! + */ +static const char* +pyt_exec_str(const char* module, const char* function, int argc, const char* argv[]) { + + PyObject *pName, *pModule, *pDict, *pFunc; + PyObject *pArgs, *pValue; + const char * rv = NULL; + int i; -static void my_exec (RESULT *result, RESULT *arg1) -{ -#if 0 - const char* value = pyt_exec_str(R2S(arg1)); -#else - const char *value = "Fixme"; -#endif + pName = PyString_FromString(module); + /* Error checking of pName left out */ + + pModule = PyImport_Import(pName); + Py_DECREF(pName); + + if (pModule != NULL) { + pDict = PyModule_GetDict(pModule); + /* pDict is a borrowed reference */ + + pFunc = PyDict_GetItemString(pDict, function); + /* pFun: Borrowed reference */ + + if (pFunc && PyCallable_Check(pFunc)) { + pArgs = PyTuple_New(argc); + for (i = 0; i < argc; ++i) { + pValue = PyString_FromString(argv[i]); + if (!pValue) { + Py_DECREF(pArgs); + Py_DECREF(pModule); + fprintf(stderr, "Cannot convert argument %s\n", argv[i]); + return NULL; + } + /* pValue reference stolen here: */ + PyTuple_SetItem(pArgs, i, pValue); + } + pValue = PyObject_CallObject(pFunc, pArgs); + Py_DECREF(pArgs); + if (pValue != NULL) { + rv = PyString_AsString(pValue); + //printf("Result of call: %s\n", rv); + Py_DECREF(pValue); + return rv; + } + else { + Py_DECREF(pModule); + PyErr_Print(); + fprintf(stderr,"Call failed\n"); + return NULL; + } + /* pDict and pFunc are borrowed and must not be Py_DECREF-ed */ + } + else { + if (PyErr_Occurred()) + PyErr_Print(); + fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]); + } + Py_DECREF(pModule); + } + else { + PyErr_Print(); + fprintf(stderr, "Failed to load \"%s\"\n", argv[1]); + return NULL; + } + return NULL; +} + +static int python_cleanup_responsibility = 0; + +static void my_exec (RESULT *result, RESULT *module, RESULT *function, RESULT *arg ) +{ + const char* args[] = {R2S(arg)}; + const char* value = pyt_exec_str(R2S(module),R2S(function),1,args); SetResult(&result, R_STRING, value); } int plugin_init_python (void) { - AddFunction ("python::exec", 1, my_exec); + if (!Py_IsInitialized()) { + Py_Initialize(); + python_cleanup_responsibility = 1; + } + AddFunction ("python::exec", 3, my_exec); return 0; } void plugin_exit_python (void) { - /* empty */ + /* Make sure NOT to call Py_Finalize() When (and if) the entire lcd4linux process + * is started from inside python + */ + if (python_cleanup_responsibility) { + python_cleanup_responsibility = 0; + Py_Finalize(); + } } diff --git a/plugins.m4 b/plugins.m4 index 927029f..d72daf6 100644 --- a/plugins.m4 +++ b/plugins.m4 @@ -200,8 +200,8 @@ if test "$PLUGIN_MYSQL" = "yes"; then AC_CHECK_LIB(mysqlclient, mysql_init, [has_mysql_lib="true"], [has_mysql_lib="false"]) if test "$has_mysql_lib" = "true"; then PLUGINS="$PLUGINS plugin_mysql.o" - AC_DEFINE(PLUGIN_MYSQL,1,[mysql plugin]) PLUGINLIBS="$PLUGINLIBS -lmysqlclient" + AC_DEFINE(PLUGIN_MYSQL,1,[mysql plugin]) else AC_MSG_WARN(mysqlclient lib not found: mysql plugin disabled) fi @@ -235,6 +235,8 @@ if test "$PLUGIN_PYTHON" = "yes"; then AC_MSG_WARN(python headers not found: python plugin disabled) else PLUGINS="$PLUGINS plugin_python.o" + CPPFLAGS="$CPPFLAGS $PYTHON_CPPFLAGS" + PLUGINLIBS="$PLUGINLIBS $PYTHON_LDFLAGS $PYTHON_EXTRA_LIBS" AC_DEFINE(PLUGIN_PYTHON,1,[python plugin]) fi fi -- cgit v1.2.3