1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
8 #include "otk/display.hh"
15 #define _(str) gettext(str)
20 static PyObject *get = NULL;
22 void python_init(char *argv0)
24 // start the python engine
25 Py_SetProgramName(argv0);
27 // prepend the openbox directories for python scripts to the sys path
28 PyRun_SimpleString("import sys");
29 PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
30 PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
31 otk::expandTilde("~/.openbox/python") +
35 PyObject *obmodule = PyImport_ImportModule("config");
36 if (obmodule == NULL) {
40 PyObject *configdict = PyModule_GetDict(obmodule);
43 get = PyDict_GetItemString(configdict, "get");
55 int python_exec(const std::string &path)
57 FILE *rcpyfd = fopen(path.c_str(), "r");
59 fprintf(stderr, _("Unabled to open python file %s\n"), path.c_str());
63 //PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
65 PyObject *module = PyImport_AddModule("__main__");
67 PyObject *dict = PyModule_GetDict(module);
69 PyObject *result = PyRun_File(rcpyfd, const_cast<char*>(path.c_str()),
70 Py_file_input, dict, dict);
71 int ret = result == NULL ? 2 : 0;
83 bool python_get_long(const char *name, long *value)
86 if (get == NULL) return false;
89 PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
92 else if (PyInt_Check(val)) {
93 *value = PyInt_AsLong(val);
95 } else if (PyLong_Check(val)) {
96 *value = PyLong_AsLong(val);
103 bool python_get_string(const char *name, otk::ustring *value)
106 if (get == NULL) return false;
109 PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
112 else if (PyString_Check(val)) {
113 *value = std::string(PyString_AsString(val), PyString_Size(val));
120 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
123 if (get == NULL) return false;
126 PyObject *val = PyObject_CallFunction(get, "ss", "openbox", name);
129 else if (PyList_Check(val)) {
130 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
131 PyObject *str = PyList_GET_ITEM(val, i);
132 if (PyString_Check(str))
133 value->push_back(std::string(PyString_AsString(str),
134 PyString_Size(str)));