- 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 ||
- action == OBActions::Action_KeyPress) {
- PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
- return false;
- }
- if (!PyCallable_Check(callback)) {
- PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
- return false;
- }
-
- FunctionList::iterator it = std::find(callbacks[action].begin(),
- callbacks[action].end(),
- callback);
- if (it == callbacks[action].end()) { // not already in there
- Py_XINCREF(callback); // Add a reference to new callback
- callbacks[action].push_back(callback);
- }
- return true;
-}
-
-bool python_preregister(int action, PyObject *callback)
-{
- if (action < 0 || action >= OBActions::NUM_ACTIONS ||
- action == OBActions::Action_KeyPress) {
- PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
- return false;
- }
- if (!PyCallable_Check(callback)) {
- PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
- return false;
- }
-
- FunctionList::iterator it = std::find(callbacks[action].begin(),
- callbacks[action].end(),
- callback);
- if (it == callbacks[action].end()) { // not already in there
- Py_XINCREF(callback); // Add a reference to new callback
- callbacks[action].insert(callbacks[action].begin(), callback);
- }
- return true;
-}
-
-bool python_unregister(int action, PyObject *callback)
-{
- if (action < 0 || action >= OBActions::NUM_ACTIONS ||
- action == OBActions::Action_KeyPress) {
- PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
- return false;
- }
- if (!PyCallable_Check(callback)) {
- PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
- return false;
- }
-
- FunctionList::iterator it = std::find(callbacks[action].begin(),
- callbacks[action].end(),
- callback);
- if (it != callbacks[action].end()) { // its been registered before
- Py_XDECREF(*it); // Dispose of previous callback
- callbacks[action].erase(it);
- }
- return true;
-}
-
-bool python_unregister_all(int action)
-{
- if (action < 0 || action >= OBActions::NUM_ACTIONS) {
- PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
- return false;
- }
-
- while (!callbacks[action].empty()) {
- Py_XDECREF(callbacks[action].back());
- callbacks[action].pop_back();
- }
- return true;