diff options
Diffstat (limited to 'src/python.cc')
| -rw-r--r-- | src/python.cc | 45 |
1 files changed, 45 insertions, 0 deletions
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 <vector> #include <algorithm> +extern "C" { +#include <Python.h> + +// 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<PyObject*> FunctionList; @@ -14,6 +23,42 @@ typedef std::vector<PyObject*> 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<char*>(file)); + fclose(rcpyfd); + return true; +} + +bool python_get_string(const char *name, std::string *value) +{ + PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(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 || |
