PageRenderTime 50ms CodeModel.GetById 19ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/scim-python-event.cpp

http://scim-python.googlecode.com/
C++ | 253 lines | 188 code | 39 blank | 26 comment | 11 complexity | 57cb8f751d511fbc66031719b9b97b8b MD5 | raw file
  1/* vim:set noet ts=4: */
  2/** 
  3 * scim-python
  4 * 
  5 * Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6 *
  7 *
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation; either
 11 * version 2 of the License, or (at your option) any later version.
 12 *
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU Lesser General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this program; if not, write to the
 20 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 21 * Boston, MA  02111-1307  USA
 22 *
 23 * $Id: $
 24 */
 25#include <Python.h>
 26#include "structmember.h"
 27#include "scim-python-event.h"
 28
 29struct PyKeyEvent {
 30	PyObject_HEAD
 31	/* Type-specific fields go here. */
 32	KeyEvent event;
 33};
 34
 35static PyObject *
 36PyKeyEvent_str (const PyKeyEvent* self)
 37{
 38	char buf[128];
 39	sprintf (buf, "KeyEvent (code=0x%08x, mask=0x%04x, layout=%d)",
 40				self->event.code, 
 41				self->event.mask, 
 42				self->event.layout);
 43
 44	return PyString_FromString (buf);
 45}
 46
 47
 48static PyMethodDef PyKeyEvent_methods[] = {
 49#if 0
 50	{ "show_preedit_string", (PyCFunction)PythonInstance::_show_preedit_string, METH_NOARGS,
 51	 "Return the name, combining the first and last name"
 52	},
 53#endif
 54	{NULL}  /* Sentinel */
 55};
 56
 57static PyMemberDef PyKeyEvent_members[] = {
 58	{NULL}  /* Sentinel */
 59};
 60
 61static PyObject *
 62PyKeyEvent_get_code (PyKeyEvent *self, void *closure)
 63{
 64	return PyInt_FromLong ((long)self->event.code);
 65}
 66
 67static int
 68PyKeyEvent_set_code (PyKeyEvent *self, PyObject *value, void *closure)
 69{
 70	if (value == NULL) {
 71		PyErr_SetString (PyExc_TypeError,
 72						"Cannot delete the code attribute");
 73		return -1;
 74	}
 75
 76	if (! PyInt_Check (value)) {
 77		PyErr_SetString (PyExc_TypeError, 
 78						"The code attribute value must be a int");
 79		return -1;
 80	}
 81
 82	self->event.code = (uint32)PyInt_AS_LONG (value);
 83
 84	return 0;
 85}
 86
 87static PyObject *
 88PyKeyEvent_get_mask (PyKeyEvent *self, void *closure)
 89{
 90
 91	return PyInt_FromLong ((long)self->event.mask);
 92}
 93
 94static int
 95PyKeyEvent_set_mask (PyKeyEvent *self, PyObject *value, void *closure)
 96{
 97	if (value == NULL) {
 98		PyErr_SetString (PyExc_TypeError,
 99						"Cannot delete the mask attribute");
100		return -1;
101	}
102
103	if (! PyInt_Check (value)) {
104		PyErr_SetString (PyExc_TypeError, 
105						"The mask attribute value must be a int");
106		return -1;
107	}
108
109	self->event.mask = (uint32)PyInt_AS_LONG (value);
110
111	return 0;
112}
113
114static PyObject *
115PyKeyEvent_get_layout (PyKeyEvent *self, void *closure)
116{
117
118	return PyInt_FromLong ((long)self->event.layout);
119}
120
121static int
122PyKeyEvent_set_layout (PyKeyEvent *self, PyObject *value, void *closure)
123{
124	if (value == NULL) {
125		PyErr_SetString (PyExc_TypeError, 
126					"Cannot delete the layout attribute");
127		return -1;
128	}
129
130	if (! PyInt_Check (value)) {
131		PyErr_SetString (PyExc_TypeError, 
132					"The layout attribute value must be a int");
133		return -1;
134	}
135 
136	self->event.layout = (uint32)PyInt_AS_LONG (value);
137
138	return 0;
139}
140
141static void
142PyKeyEvent_dealloc (PyKeyEvent *self)
143{
144	self->event.~KeyEvent ();
145	((PyObject *)self)->ob_type->tp_free (self);
146}
147
148static int
149PyKeyEvent_init (PyKeyEvent *self, PyObject *args, PyObject *kwds)
150{
151	uint32 code;
152	uint32 layout;
153	uint32 mask;
154
155	if (!PyArg_ParseTuple (args, "III:__init__", &code, &layout, &mask)) {
156		PyErr_Print ();
157		return -1;
158	}
159
160	new (&self->event) KeyEvent (code, layout, mask); 
161	return 0;
162}
163
164
165static PyObject *
166PyKeyEvent_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
167{
168	PyKeyEvent *self;
169	self = (PyKeyEvent *)type->tp_alloc (type, 0);
170	return (PyObject *)self;
171}
172
173static PyGetSetDef
174PyKeyEvent_getseters[] = {
175#define ENTRY(name, desc) \
176	{ \
177		#name, \
178		(getter)PyKeyEvent_get_##name, \
179		(setter)PyKeyEvent_set_##name, \
180		desc, \
181		NULL \
182	}
183	ENTRY(code, "Key Code"),
184	ENTRY(mask, "Key mask"),
185	ENTRY(layout, "keyboard layout"),
186#undef ENTRY
187	{NULL}  /* Sentinel */
188};
189
190static PyTypeObject PyKeyEventType = {
191	PyObject_HEAD_INIT (NULL)
192	0,						 				/*ob_size*/
193	"scim.KeyEvent", 						/*tp_name*/
194	sizeof (PyKeyEvent), 					/*tp_basicsize*/
195	0,						 				/*tp_itemsize*/
196	(destructor)PyKeyEvent_dealloc,			/*tp_dealloc*/
197	0,			  						/*tp_print*/
198	0,						 				/*tp_getattr*/
199	0,										/*tp_setattr*/
200	0,										/*tp_compare*/
201	0,			  						/*tp_repr*/
202	0,										/*tp_as_number*/
203	0,			  						/*tp_as_sequence*/
204	0,										/*tp_as_mapping*/
205	0,			  						/*tp_hash */
206	0,										/*tp_call*/
207	(reprfunc)PyKeyEvent_str,   							/*tp_str*/
208	0,					   				/*tp_getattro*/
209	0,										/*tp_setattro*/
210	0,					 					/*tp_as_buffer*/
211	Py_TPFLAGS_DEFAULT,						/*tp_flags*/
212	"KeyEvent objects",		 /* tp_doc */
213	0,					   /* tp_traverse */
214	0,					   /* tp_clear */
215	0,					   /* tp_richcompare */
216	0,					   /* tp_weaklistoffset */
217	0,					   /* tp_iter */
218	0,					   /* tp_iternext */
219	PyKeyEvent_methods,			 /* tp_methods */
220	PyKeyEvent_members,			 /* tp_members */
221	PyKeyEvent_getseters,						 /* tp_getset */
222	0,						 /* tp_base */
223	0,						 /* tp_dict */
224	0,						 /* tp_descr_get */
225	0,						 /* tp_descr_set */
226	0,						 /* tp_dictoffset */
227	(initproc) PyKeyEvent_init,
228							 /* tp_init */
229	0,						 /* tp_alloc */
230	PyKeyEvent_new,			 /* tp_new */
231	PyObject_Del,			 /* tp_free */
232};
233
234PyObject *PyKeyEvent_New (const KeyEvent &key)
235{
236	PyKeyEvent *obj = PyObject_New (PyKeyEvent, &PyKeyEventType);
237	new (&obj->event) KeyEvent (key);
238
239	return (PyObject *)obj;
240}
241
242
243void init_event (PyObject *module)
244{
245
246	if (PyType_Ready (&PyKeyEventType) < 0)
247		return;
248
249	Py_INCREF (&PyKeyEventType);
250	PyModule_AddObject (module, "KeyEvent", (PyObject *)&PyKeyEventType);
251
252}
253