From 745e840547b5443ecfb9b6f0a4f14b0d035d59c2 Mon Sep 17 00:00:00 2001 From: Dana Jansens Date: Tue, 31 Dec 2002 19:15:24 +0000 Subject: load config options from the python environment --- src/python.cc | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/python.cc') diff --git a/src/python.cc b/src/python.cc index dbe35f5d..ddd10d0f 100644 --- a/src/python.cc +++ b/src/python.cc @@ -7,6 +7,15 @@ #include #include +extern "C" { +#include + +// The initializer in openbox_wrap.cc +extern void init_openbox(void); +// The initializer in otk_wrap.cc +extern void init_otk(void); +} + namespace ob { typedef std::vector FunctionList; @@ -14,6 +23,42 @@ typedef std::vector FunctionList; static FunctionList callbacks[OBActions::NUM_ACTIONS]; static FunctionList bindfuncs; +static PyObject *obdict; + +void python_init(char *argv0) +{ + Py_SetProgramName(argv0); + Py_Initialize(); + init_otk(); + init_openbox(); + PyRun_SimpleString("from _otk import *; from _openbox import *;"); + + // set up access to the python global variables + PyObject *obmodule = PyImport_AddModule("__main__"); + obdict = PyModule_GetDict(obmodule); +} + +bool python_exec(const char *file) { + FILE *rcpyfd = fopen(file, "r"); + if (!rcpyfd) { + printf("failed to load python file %s\n", file); + return false; + } + PyRun_SimpleFile(rcpyfd, const_cast(file)); + fclose(rcpyfd); + return true; +} + +bool python_get_string(const char *name, std::string *value) +{ + PyObject *val = PyDict_GetItemString(obdict, const_cast(name)); + if (!val) return false; + + *value = PyString_AsString(val); + return true; +} + + bool python_register(int action, PyObject *callback) { if (action < 0 || action >= OBActions::NUM_ACTIONS || -- cgit v1.2.3