2002-12-25 02:19:49 +00:00
|
|
|
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
|
|
|
|
|
|
|
|
#include "python.hh"
|
2002-12-30 06:31:45 +00:00
|
|
|
#include "openbox.hh"
|
2003-01-02 20:36:14 +00:00
|
|
|
#include "actions.hh"
|
|
|
|
#include "python.hh"
|
|
|
|
#include "bindings.hh"
|
2002-12-31 06:59:46 +00:00
|
|
|
#include "otk/display.hh"
|
2003-01-10 03:11:48 +00:00
|
|
|
#include "otk/util.hh"
|
2002-12-25 02:19:49 +00:00
|
|
|
|
|
|
|
namespace ob {
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static PyObject *obdict = NULL;
|
|
|
|
|
|
|
|
void python_init(char *argv0)
|
2002-12-30 06:31:45 +00:00
|
|
|
{
|
2003-01-10 06:43:52 +00:00
|
|
|
// start the python engine
|
2003-01-11 11:16:36 +00:00
|
|
|
Py_SetProgramName(argv0);
|
|
|
|
Py_Initialize();
|
2003-01-31 06:01:16 +00:00
|
|
|
// prepend the openbox directories for python scripts to the sys path
|
2003-01-11 11:16:36 +00:00
|
|
|
PyRun_SimpleString("import sys");
|
2003-01-31 09:23:23 +00:00
|
|
|
PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
|
2003-01-31 06:01:16 +00:00
|
|
|
PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
|
2003-01-11 11:16:36 +00:00
|
|
|
otk::expandTilde("~/.openbox/python") +
|
|
|
|
"')").c_str()));
|
2003-02-05 15:38:29 +00:00
|
|
|
//PyRun_SimpleString("import ob; import otk; import config;");
|
|
|
|
PyRun_SimpleString("import config;");
|
2003-01-10 06:43:52 +00:00
|
|
|
// set up convenience global variables
|
2003-02-05 15:38:29 +00:00
|
|
|
//PyRun_SimpleString("ob.openbox = ob.Openbox_instance()");
|
|
|
|
//PyRun_SimpleString("otk.display = otk.Display_instance()");
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
// set up access to the python global variables
|
2003-01-25 16:36:55 +00:00
|
|
|
PyObject *obmodule = PyImport_AddModule("config");
|
2003-01-02 20:36:14 +00:00
|
|
|
obdict = PyModule_GetDict(obmodule);
|
2002-12-30 16:42:15 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
void python_destroy()
|
2002-12-31 06:59:46 +00:00
|
|
|
{
|
2003-02-02 22:00:38 +00:00
|
|
|
Py_Finalize();
|
2002-12-31 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
bool python_exec(const std::string &path)
|
2002-12-30 06:31:45 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
FILE *rcpyfd = fopen(path.c_str(), "r");
|
|
|
|
if (!rcpyfd) {
|
2003-02-02 22:00:38 +00:00
|
|
|
printf("Failed to load python file %s\n", path.c_str());
|
2003-01-02 20:36:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
|
|
|
|
fclose(rcpyfd);
|
|
|
|
return true;
|
2002-12-30 06:31:45 +00:00
|
|
|
}
|
|
|
|
|
2003-01-03 21:36:09 +00:00
|
|
|
bool python_get_long(const char *name, long *value)
|
|
|
|
{
|
|
|
|
PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
|
2003-01-12 20:47:55 +00:00
|
|
|
if (!(val && PyInt_Check(val))) return false;
|
2003-01-03 21:36:09 +00:00
|
|
|
|
2003-01-12 20:47:55 +00:00
|
|
|
*value = PyInt_AsLong(val);
|
2003-01-03 21:36:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-01-13 05:54:40 +00:00
|
|
|
bool python_get_string(const char *name, otk::ustring *value)
|
2003-01-02 20:36:14 +00:00
|
|
|
{
|
|
|
|
PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
|
|
|
|
if (!(val && PyString_Check(val))) return false;
|
|
|
|
|
|
|
|
*value = PyString_AsString(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-01-13 05:54:40 +00:00
|
|
|
bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
|
2003-01-02 21:05:29 +00:00
|
|
|
{
|
|
|
|
PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
|
|
|
|
if (!(val && PyList_Check(val))) return false;
|
|
|
|
|
|
|
|
for (int i = 0, end = PyList_Size(val); i < end; ++i) {
|
|
|
|
PyObject *str = PyList_GetItem(val, i);
|
|
|
|
if (PyString_Check(str))
|
|
|
|
value->push_back(PyString_AsString(str));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2003-01-02 20:36:14 +00:00
|
|
|
|
2002-12-25 02:19:49 +00:00
|
|
|
}
|