42cda9433b809dbfae4e2a97f0928d5396b0632f
[blender.git] / source / gameengine / GameLogic / SCA_PythonKeyboard.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): none yet.
19  *
20  * ***** END GPL LICENSE BLOCK *****
21  */
22
23 /** \file gameengine/GameLogic/SCA_PythonKeyboard.cpp
24  *  \ingroup gamelogic
25  */
26
27
28 #include "SCA_PythonKeyboard.h"
29 #include "SCA_IInputDevice.h"
30
31 #include "GHOST_C-api.h"
32
33 /* ------------------------------------------------------------------------- */
34 /* Native functions                                                          */
35 /* ------------------------------------------------------------------------- */
36
37 SCA_PythonKeyboard::SCA_PythonKeyboard(SCA_IInputDevice* keyboard)
38 : PyObjectPlus(),
39 m_keyboard(keyboard)
40 {
41 #ifdef WITH_PYTHON
42         m_event_dict = PyDict_New();
43 #endif
44 }
45
46 SCA_PythonKeyboard::~SCA_PythonKeyboard()
47 {
48 #ifdef WITH_PYTHON
49         PyDict_Clear(m_event_dict);
50         Py_DECREF(m_event_dict);
51 #endif
52 }
53
54 #ifdef WITH_PYTHON
55
56 /* ------------------------------------------------------------------------- */
57 /* Python functions                                                          */
58 /* ------------------------------------------------------------------------- */
59
60 /* clipboard */
61 static PyObject *gPyGetClipboard(PyObject *args, PyObject *kwds)
62 {
63         char *buf = (char *)GHOST_getClipboard(0);
64         return PyUnicode_FromString(buf?buf:"");
65 }
66
67 static PyObject *gPySetClipboard(PyObject *args, PyObject *value)
68 {
69         char* buf;
70         if (!PyArg_ParseTuple(value,"s:setClipboard",&buf))
71                 Py_RETURN_NONE;
72
73         GHOST_putClipboard((GHOST_TInt8 *)buf, 0);
74         Py_RETURN_NONE;
75 }
76
77 /* Integration hooks ------------------------------------------------------- */
78 PyTypeObject SCA_PythonKeyboard::Type = {
79         PyVarObject_HEAD_INIT(NULL, 0)
80         "SCA_PythonKeyboard",
81         sizeof(PyObjectPlus_Proxy),
82         0,
83         py_base_dealloc,
84         0,
85         0,
86         0,
87         0,
88         py_base_repr,
89         0,0,0,0,0,0,0,0,0,
90         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
91         0,0,0,0,0,0,0,
92         Methods,
93         0,
94         0,
95         &PyObjectPlus::Type,
96         0,0,0,0,0,0,
97         py_base_new
98 };
99
100 PyMethodDef SCA_PythonKeyboard::Methods[] = {
101         {"getClipboard", (PyCFunction) gPyGetClipboard, METH_VARARGS, "getCliboard doc"},
102         {"setClipboard", (PyCFunction) gPySetClipboard, METH_VARARGS, "setCliboard doc"},
103         {NULL,NULL} //Sentinel
104 };
105
106 PyAttributeDef SCA_PythonKeyboard::Attributes[] = {
107         KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonKeyboard, pyattr_get_events),
108         KX_PYATTRIBUTE_RO_FUNCTION("active_events", SCA_PythonKeyboard, pyattr_get_active_events),
109         { NULL }        //Sentinel
110 };
111
112 PyObject *SCA_PythonKeyboard::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
113 {
114         SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v);
115         
116         for (int i=SCA_IInputDevice::KX_BEGINKEY; i<=SCA_IInputDevice::KX_ENDKEY; i++)
117         {
118                 const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
119                 
120                 PyDict_SetItem(self->m_event_dict, PyLong_FromSsize_t(i), PyLong_FromSsize_t(inevent.m_status));
121         }
122         Py_INCREF(self->m_event_dict);
123         return self->m_event_dict;
124 }
125
126 PyObject *SCA_PythonKeyboard::pyattr_get_active_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
127 {
128         SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v);
129
130         PyDict_Clear(self->m_event_dict);
131         
132         for (int i=SCA_IInputDevice::KX_BEGINKEY; i<=SCA_IInputDevice::KX_ENDKEY; i++)
133         {
134                 const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
135                 
136                 if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
137                         PyDict_SetItem(self->m_event_dict, PyLong_FromSsize_t(i), PyLong_FromSsize_t(inevent.m_status));
138         }
139         Py_INCREF(self->m_event_dict);
140         return self->m_event_dict;
141 }
142
143 #endif