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"
|
2002-12-25 02:19:49 +00:00
|
|
|
|
2002-12-31 19:15:24 +00:00
|
|
|
extern "C" {
|
|
|
|
// The initializer in openbox_wrap.cc
|
|
|
|
extern void init_openbox(void);
|
|
|
|
// The initializer in otk_wrap.cc
|
|
|
|
extern void init_otk(void);
|
|
|
|
}
|
|
|
|
|
2002-12-25 02:19:49 +00:00
|
|
|
namespace ob {
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static PyObject *obdict = NULL;
|
|
|
|
|
|
|
|
// ************************************************************* //
|
|
|
|
// Define some custom types which are passed to python callbacks //
|
|
|
|
// ************************************************************* //
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD;
|
|
|
|
OBActions::ActionType action;
|
|
|
|
Window window;
|
|
|
|
OBWidget::WidgetType type;
|
|
|
|
unsigned int state;
|
|
|
|
unsigned int button;
|
|
|
|
int xroot;
|
|
|
|
int yroot;
|
|
|
|
Time time;
|
|
|
|
} ActionData;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD;
|
|
|
|
Window window;
|
|
|
|
unsigned int state;
|
|
|
|
unsigned int key;
|
|
|
|
Time time;
|
|
|
|
} BindingData;
|
|
|
|
|
|
|
|
static void ActionDataDealloc(ActionData *self)
|
2002-12-31 19:15:24 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject_Del((PyObject*)self);
|
2002-12-31 19:15:24 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static void BindingDataDealloc(BindingData *self)
|
|
|
|
{
|
|
|
|
PyObject_Del((PyObject*)self);
|
2002-12-31 19:15:24 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_action(ActionData *self, PyObject *args)
|
2002-12-31 19:15:24 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":action")) return NULL;
|
|
|
|
return PyLong_FromLong((int)self->action);
|
2002-12-31 19:15:24 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_window(ActionData *self, PyObject *args)
|
2002-12-25 02:19:49 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":window")) return NULL;
|
|
|
|
return PyLong_FromLong(self->window);
|
2002-12-25 02:19:49 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_target(ActionData *self, PyObject *args)
|
2002-12-25 22:02:34 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":target")) return NULL;
|
|
|
|
return PyLong_FromLong((int)self->type);
|
2002-12-25 22:02:34 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_modifiers(ActionData *self, PyObject *args)
|
2002-12-25 02:19:49 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
|
|
|
|
return PyLong_FromUnsignedLong(self->state);
|
2002-12-25 02:19:49 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_button(ActionData *self, PyObject *args)
|
2002-12-27 08:39:53 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":button")) return NULL;
|
|
|
|
int b = 0;
|
|
|
|
switch (self->button) {
|
|
|
|
case Button5: b++;
|
|
|
|
case Button4: b++;
|
|
|
|
case Button3: b++;
|
|
|
|
case Button2: b++;
|
|
|
|
case Button1: b++;
|
|
|
|
default: ;
|
2002-12-27 08:39:53 +00:00
|
|
|
}
|
2003-01-02 20:36:14 +00:00
|
|
|
return PyLong_FromLong(b);
|
2002-12-27 08:39:53 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_xroot(ActionData *self, PyObject *args)
|
2002-12-25 02:19:49 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":xroot")) return NULL;
|
|
|
|
return PyLong_FromLong(self->xroot);
|
|
|
|
}
|
2002-12-25 02:19:49 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_yroot(ActionData *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if(!PyArg_ParseTuple(args,":yroot")) return NULL;
|
|
|
|
return PyLong_FromLong(self->yroot);
|
2002-12-25 02:19:49 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *ActionData_time(ActionData *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if(!PyArg_ParseTuple(args,":time")) return NULL;
|
|
|
|
return PyLong_FromLong(self->time);
|
|
|
|
}
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static PyMethodDef ActionData_methods[] = {
|
|
|
|
{"action", (PyCFunction)ActionData_action, METH_VARARGS,
|
|
|
|
"Return the action being executed."},
|
|
|
|
{"window", (PyCFunction)ActionData_window, METH_VARARGS,
|
|
|
|
"Return the client window id."},
|
|
|
|
{"target", (PyCFunction)ActionData_target, METH_VARARGS,
|
|
|
|
"Return the target type that the action is occuring on."},
|
|
|
|
{"modifiers", (PyCFunction)ActionData_modifiers, METH_VARARGS,
|
|
|
|
"Return the modifier keys state."},
|
|
|
|
{"button", (PyCFunction)ActionData_button, METH_VARARGS,
|
|
|
|
"Return the number of the pressed button (1-5)."},
|
|
|
|
{"xroot", (PyCFunction)ActionData_xroot, METH_VARARGS,
|
|
|
|
"Return the X-position of the mouse cursor on the root window."},
|
|
|
|
{"yroot", (PyCFunction)ActionData_yroot, METH_VARARGS,
|
|
|
|
"Return the Y-position of the mouse cursor on the root window."},
|
|
|
|
{"time", (PyCFunction)ActionData_time, METH_VARARGS,
|
|
|
|
"Return the time at which the event occured."},
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *BindingData_window(BindingData *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if(!PyArg_ParseTuple(args,":window")) return NULL;
|
|
|
|
return PyLong_FromLong(self->window);
|
|
|
|
}
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *BindingData_modifiers(BindingData *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
|
|
|
|
return PyLong_FromUnsignedLong(self->state);
|
|
|
|
}
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *BindingData_key(BindingData *self, PyObject *args)
|
|
|
|
{
|
|
|
|
if(!PyArg_ParseTuple(args,":key")) return NULL;
|
|
|
|
return PyString_FromString(
|
|
|
|
XKeysymToString(XKeycodeToKeysym(otk::OBDisplay::display, self->key, 0)));
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
}
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
PyObject *BindingData_time(BindingData *self, PyObject *args)
|
2002-12-30 06:31:45 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
if(!PyArg_ParseTuple(args,":time")) return NULL;
|
|
|
|
return PyLong_FromLong(self->time);
|
|
|
|
}
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static PyMethodDef BindingData_methods[] = {
|
|
|
|
{"window", (PyCFunction)BindingData_window, METH_VARARGS,
|
|
|
|
"Return the client window id."},
|
|
|
|
{"modifiers", (PyCFunction)BindingData_modifiers, METH_VARARGS,
|
|
|
|
"Return the modifier keys state."},
|
|
|
|
{"key", (PyCFunction)BindingData_key, METH_VARARGS,
|
|
|
|
"Return the name of the pressed key."},
|
|
|
|
{"time", (PyCFunction)BindingData_time, METH_VARARGS,
|
|
|
|
"Return the time at which the event occured."},
|
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *ActionDataGetAttr(PyObject *obj, char *name)
|
|
|
|
{
|
|
|
|
return Py_FindMethod(ActionData_methods, obj, name);
|
|
|
|
}
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static PyObject *BindingDataGetAttr(PyObject *obj, char *name)
|
|
|
|
{
|
|
|
|
return Py_FindMethod(BindingData_methods, obj, name);
|
2002-12-30 06:31:45 +00:00
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
static PyTypeObject ActionData_Type = {
|
|
|
|
PyObject_HEAD_INIT(NULL)
|
|
|
|
0,
|
|
|
|
"ActionData",
|
|
|
|
sizeof(ActionData),
|
|
|
|
0,
|
|
|
|
(destructor)ActionDataDealloc,
|
|
|
|
0,
|
|
|
|
(getattrfunc)ActionDataGetAttr,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyTypeObject BindingData_Type = {
|
|
|
|
PyObject_HEAD_INIT(NULL)
|
|
|
|
0,
|
|
|
|
"BindingData",
|
|
|
|
sizeof(BindingData),
|
|
|
|
0,
|
|
|
|
(destructor)BindingDataDealloc,
|
|
|
|
0,
|
|
|
|
(getattrfunc)BindingDataGetAttr,
|
|
|
|
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
|
|
|
|
};
|
|
|
|
|
|
|
|
// **************** //
|
|
|
|
// End custom types //
|
|
|
|
// **************** //
|
|
|
|
|
|
|
|
void python_init(char *argv0)
|
2002-12-30 06:31:45 +00:00
|
|
|
{
|
2003-01-02 20:36:14 +00:00
|
|
|
Py_SetProgramName(argv0);
|
|
|
|
Py_Initialize();
|
|
|
|
init_otk();
|
|
|
|
init_openbox();
|
|
|
|
PyRun_SimpleString("from _otk import *; from _openbox import *;");
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
// set up access to the python global variables
|
|
|
|
PyObject *obmodule = PyImport_AddModule("__main__");
|
|
|
|
obdict = PyModule_GetDict(obmodule);
|
2002-12-30 06:31:45 +00:00
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
// set up the custom types
|
|
|
|
ActionData_Type.ob_type = &PyType_Type;
|
|
|
|
BindingData_Type.ob_type = &PyType_Type;
|
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-01-02 20:36:14 +00:00
|
|
|
Py_DECREF(obdict);
|
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) {
|
|
|
|
printf("failed to load python file %s\n", path.c_str());
|
|
|
|
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-02 20:36:14 +00:00
|
|
|
static void call(PyObject *func, PyObject *data)
|
2002-12-30 22:27:46 +00:00
|
|
|
{
|
|
|
|
PyObject *arglist;
|
|
|
|
PyObject *result;
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
arglist = Py_BuildValue("(O)", data);
|
|
|
|
|
2002-12-30 22:27:46 +00:00
|
|
|
// call the callback
|
2003-01-02 20:36:14 +00:00
|
|
|
result = PyEval_CallObject(func, arglist);
|
|
|
|
if (!result) {
|
2002-12-30 22:27:46 +00:00
|
|
|
// an exception occured in the script, display it
|
|
|
|
PyErr_Print();
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
Py_XDECREF(result);
|
2002-12-30 22:27:46 +00:00
|
|
|
Py_DECREF(arglist);
|
|
|
|
}
|
|
|
|
|
2003-01-02 20:36:14 +00:00
|
|
|
void python_callback(PyObject *func, OBActions::ActionType action,
|
|
|
|
Window window, OBWidget::WidgetType type,
|
|
|
|
unsigned int state, unsigned int button,
|
|
|
|
int xroot, int yroot, Time time)
|
|
|
|
{
|
|
|
|
assert(func);
|
|
|
|
|
|
|
|
ActionData *data = PyObject_New(ActionData, &ActionData_Type);
|
|
|
|
data->action = action;
|
|
|
|
data->window = window;
|
|
|
|
data->type = type;
|
|
|
|
data->state = state;
|
|
|
|
data->button = button;
|
|
|
|
data->xroot = xroot;
|
|
|
|
data->yroot = yroot;
|
|
|
|
data->time = time;
|
|
|
|
|
|
|
|
call(func, (PyObject*)data);
|
|
|
|
Py_DECREF(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void python_callback(PyObject *func, Window window, unsigned int state,
|
|
|
|
unsigned int key, Time time)
|
|
|
|
{
|
|
|
|
if (!func) return;
|
|
|
|
|
|
|
|
BindingData *data = PyObject_New(BindingData, &BindingData_Type);
|
|
|
|
data->window = window;
|
|
|
|
data->state = state;
|
|
|
|
data->key = key;
|
|
|
|
data->time = time;
|
|
|
|
|
|
|
|
call(func, (PyObject*)data);
|
|
|
|
Py_DECREF(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool python_get_string(const char *name, std::string *value)
|
|
|
|
{
|
|
|
|
PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
|
|
|
|
if (!(val && PyString_Check(val))) return false;
|
|
|
|
|
|
|
|
*value = PyString_AsString(val);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-25 02:19:49 +00:00
|
|
|
}
|