diff options
| author | Dana Jansens <danakj@orodu.net> | 2002-12-31 19:15:24 +0000 |
|---|---|---|
| committer | Dana Jansens <danakj@orodu.net> | 2002-12-31 19:15:24 +0000 |
| commit | 745e840547b5443ecfb9b6f0a4f14b0d035d59c2 (patch) | |
| tree | e20da017ab6d93775136f0cd2267a25f7c83f8d6 /src/python.cc | |
| parent | 1161a90a70b21d3064a9dee62c72dd4be3025ada (diff) | |
load config options from the python environment
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 || |
