-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmisc.i
More file actions
186 lines (146 loc) · 5.34 KB
/
misc.i
File metadata and controls
186 lines (146 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%{
#include <Misc/CallbackData.h>
#include <Misc/CallbackList.h>
#include <GLMotif/Button.h>
#include <GLMotif/ToggleButton.h>
#include <Vrui/ToolManager.h>
#include <Vrui/LocatorTool.h>
#include <Vrui/DraggingTool.h>
#include <typeinfo>
%}
%feature("director") Misc::CallbackData;
%feature("director") Misc::CallbackList;
%include <Misc/CallbackData.h>
%inline %{
class LocatorToolCreationCallbackData : public Misc::CallbackData
{
public:
Vrui::LocatorTool* tool;
LocatorToolCreationCallbackData(Vrui::LocatorTool* sTool) : tool(sTool) { }
};
class DraggingToolCreationCallbackData : public Misc::CallbackData
{
public:
Vrui::DraggingTool* tool;
DraggingToolCreationCallbackData(Vrui::DraggingTool* sTool) : tool(sTool) { }
};
%}
/* Insert wrapper code for re-routing python callbacks */
%{
struct CallbackTypeData
{
PyObject* func;
swig_type_info* type_info;
PyObject* additional_data;
};
/* I think it's safe to say that I am not 100% pleased with this solution. */
PyObject* shenanigans(Misc::CallbackData* cbData, CallbackTypeData* typeData)
{
PyObject* obj = NULL;
if (strcmp(typeData->type_info->str, "ToolManagerToolCreationCallbackData *") == 0)
{
/* Cast the callback data to ToolCreationCallbakData type */
Vrui::ToolManager::ToolCreationCallbackData* data = dynamic_cast<Vrui::ToolManager::ToolCreationCallbackData*>(cbData);
/* Determine the type of tool being created */
std::string toolName(typeid(*(data->tool)).name());
if (toolName.find("LocatorTool") != std::string::npos)
{
LocatorToolCreationCallbackData* cbd = new LocatorToolCreationCallbackData((Vrui::LocatorTool*)data->tool);
swig_type_info* ti = SWIG_TypeQueryModule(&swig_module, &swig_module,"LocatorToolCreationCallbackData *");
obj = SWIG_NewPointerObj(SWIG_as_voidptr(cbd), ti, 0);
}
else if (toolName.find("DraggingTool") != std::string::npos)
{
DraggingToolCreationCallbackData* cbd = new DraggingToolCreationCallbackData((Vrui::DraggingTool*)data->tool);
swig_type_info* ti = SWIG_TypeQueryModule(&swig_module, &swig_module, "DraggingToolCreationCallbackData *");
obj = SWIG_NewPointerObj(SWIG_as_voidptr(cbd), ti, 0);
}
}
else if (strcmp(typeData->type_info->str, "ToolManagerToolDestructionCallbackData *") == 0)
{
/* Tool desctruction */
}
else {
}
return obj;
}
/* Callback with signature required by CallbackList::add() method */
static void add_PythonCallback(Misc::CallbackData* cbData, void* userData)
{
PyObject* obj;
CallbackTypeData* type_data = reinterpret_cast<CallbackTypeData*>(userData);
obj = shenanigans(cbData, type_data);
if (obj == NULL)
{
obj = SWIG_NewPointerObj(SWIG_as_voidptr(cbData), type_data->type_info, 0);
}
/* Create the arguments list */
PyObject* arglist; /* = Py_BuildValue("(O)", obj);*/
if (type_data->additional_data != NULL)
{
arglist = Py_BuildValue("(OO)", obj, type_data->additional_data);
}
else
{
arglist = Py_BuildValue("(O)", obj);
}
if (arglist == NULL)
{
printf("add_PythonCallback: Error building arglist\n");
return;
}
/* Call the python method with the converted callback data */
PyObject* result = PyEval_CallObject(type_data->func, arglist);
if (result == NULL)
{
printf("add_PythonCallback: Error calling function\n");
return;
}
Py_DECREF(arglist);
Py_XDECREF(result);
return;
}
%}
namespace Misc {
class CallbackList
{
public:
CallbackList();
~CallbackList();
void call(CallbackData* callbackData) const;
/* Expose the add method, but rename it so the python_add method
(defined below) can be used instead. */
%rename(ORIGINAL_add) add;
void add(CallbackType newCallbackFunction, void* newUserData);
/* Add a method which takes a python method. This is method that
will actually be called. */
%extend {
void python_add(PyObject* PyFunc, PyObject* data=NULL)
{
Py_XINCREF(PyFunc);
PyObject* callbackClassAttr = PyObject_GetAttrString(PyFunc, "__cbclass__");
char* callbackClassName = PyString_AsString(callbackClassAttr);
CallbackTypeData* type_data = new CallbackTypeData;
type_data->func = PyFunc;
type_data->type_info = SWIG_TypeQueryModule(&swig_module, &swig_module, callbackClassName);
type_data->additional_data = data;
/* Call the original add method. With the re-routing function
defined above. */
$self->add(add_PythonCallback, (void*)type_data);
}
}
};
}
/* Rename the python callback so that the re-routing mechanism is
transparent to the user. */
%pythoncode %{
CallbackList.add = CallbackList.python_add
class Callback:
def __init__(self, cb):
self.cbclass = cb
def __call__(self, f):
def wrapper(self, *args):
return f(self, *args)
wrapper.__cbclass__ = self.cbclass + 'CallbackData *'
return wrapper
%}