PageRenderTime 38ms CodeModel.GetById 21ms RepoModel.GetById 0ms 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. struct PyKeyEvent {
  29. PyObject_HEAD
  30. /* Type-specific fields go here. */
  31. KeyEvent event;
  32. };
  33. static PyObject *
  34. PyKeyEvent_str (const PyKeyEvent* self)
  35. {
  36. char buf[128];
  37. sprintf (buf, "KeyEvent (code=0x%08x, mask=0x%04x, layout=%d)",
  38. self->event.code,
  39. self->event.mask,
  40. self->event.layout);
  41. return PyString_FromString (buf);
  42. }
  43. static PyMethodDef PyKeyEvent_methods[] = {
  44. #if 0
  45. { "show_preedit_string", (PyCFunction)PythonInstance::_show_preedit_string, METH_NOARGS,
  46. "Return the name, combining the first and last name"
  47. },
  48. #endif
  49. {NULL} /* Sentinel */
  50. };
  51. static PyMemberDef PyKeyEvent_members[] = {
  52. {NULL} /* Sentinel */
  53. };
  54. static PyObject *
  55. PyKeyEvent_get_code (PyKeyEvent *self, void *closure)
  56. {
  57. return PyInt_FromLong ((long)self->event.code);
  58. }
  59. static int
  60. PyKeyEvent_set_code (PyKeyEvent *self, PyObject *value, void *closure)
  61. {
  62. if (value == NULL) {
  63. PyErr_SetString (PyExc_TypeError,
  64. "Cannot delete the code attribute");
  65. return -1;
  66. }
  67. if (! PyInt_Check (value)) {
  68. PyErr_SetString (PyExc_TypeError,
  69. "The code attribute value must be a int");
  70. return -1;
  71. }
  72. self->event.code = (uint32)PyInt_AS_LONG (value);
  73. return 0;
  74. }
  75. static PyObject *
  76. PyKeyEvent_get_mask (PyKeyEvent *self, void *closure)
  77. {
  78. return PyInt_FromLong ((long)self->event.mask);
  79. }
  80. static int
  81. PyKeyEvent_set_mask (PyKeyEvent *self, PyObject *value, void *closure)
  82. {
  83. if (value == NULL) {
  84. PyErr_SetString (PyExc_TypeError,
  85. "Cannot delete the mask attribute");
  86. return -1;
  87. }
  88. if (! PyInt_Check (value)) {
  89. PyErr_SetString (PyExc_TypeError,
  90. "The mask attribute value must be a int");
  91. return -1;
  92. }
  93. self->event.mask = (uint32)PyInt_AS_LONG (value);
  94. return 0;
  95. }
  96. static PyObject *
  97. PyKeyEvent_get_layout (PyKeyEvent *self, void *closure)
  98. {
  99. return PyInt_FromLong ((long)self->event.layout);
  100. }
  101. static int
  102. PyKeyEvent_set_layout (PyKeyEvent *self, PyObject *value, void *closure)
  103. {
  104. if (value == NULL) {
  105. PyErr_SetString (PyExc_TypeError,
  106. "Cannot delete the layout attribute");
  107. return -1;
  108. }
  109. if (! PyInt_Check (value)) {
  110. PyErr_SetString (PyExc_TypeError,
  111. "The layout attribute value must be a int");
  112. return -1;
  113. }
  114. self->event.layout = (uint32)PyInt_AS_LONG (value);
  115. return 0;
  116. }
  117. static void
  118. PyKeyEvent_dealloc (PyKeyEvent *self)
  119. {
  120. self->event.~KeyEvent ();
  121. ((PyObject *)self)->ob_type->tp_free (self);
  122. }
  123. static int
  124. PyKeyEvent_init (PyKeyEvent *self, PyObject *args, PyObject *kwds)
  125. {
  126. uint32 code;
  127. uint32 layout;
  128. uint32 mask;
  129. if (!PyArg_ParseTuple (args, "III:__init__", &code, &layout, &mask)) {
  130. PyErr_Print ();
  131. return -1;
  132. }
  133. new (&self->event) KeyEvent (code, layout, mask);
  134. return 0;
  135. }
  136. static PyObject *
  137. PyKeyEvent_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
  138. {
  139. PyKeyEvent *self;
  140. self = (PyKeyEvent *)type->tp_alloc (type, 0);
  141. return (PyObject *)self;
  142. }
  143. static PyGetSetDef
  144. PyKeyEvent_getseters[] = {
  145. #define ENTRY(name, desc) \
  146. { \
  147. #name, \
  148. (getter)PyKeyEvent_get_##name, \
  149. (setter)PyKeyEvent_set_##name, \
  150. desc, \
  151. NULL \
  152. }
  153. ENTRY(code, "Key Code"),
  154. ENTRY(mask, "Key mask"),
  155. ENTRY(layout, "keyboard layout"),
  156. #undef ENTRY
  157. {NULL} /* Sentinel */
  158. };
  159. static PyTypeObject PyKeyEventType = {
  160. PyObject_HEAD_INIT (NULL)
  161. 0, /*ob_size*/
  162. "scim.KeyEvent", /*tp_name*/
  163. sizeof (PyKeyEvent), /*tp_basicsize*/
  164. 0, /*tp_itemsize*/
  165. (destructor)PyKeyEvent_dealloc, /*tp_dealloc*/
  166. 0, /*tp_print*/
  167. 0, /*tp_getattr*/
  168. 0, /*tp_setattr*/
  169. 0, /*tp_compare*/
  170. 0, /*tp_repr*/
  171. 0, /*tp_as_number*/
  172. 0, /*tp_as_sequence*/
  173. 0, /*tp_as_mapping*/
  174. 0, /*tp_hash */
  175. 0, /*tp_call*/
  176. (reprfunc)PyKeyEvent_str, /*tp_str*/
  177. 0, /*tp_getattro*/
  178. 0, /*tp_setattro*/
  179. 0, /*tp_as_buffer*/
  180. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  181. "KeyEvent objects", /* tp_doc */
  182. 0, /* tp_traverse */
  183. 0, /* tp_clear */
  184. 0, /* tp_richcompare */
  185. 0, /* tp_weaklistoffset */
  186. 0, /* tp_iter */
  187. 0, /* tp_iternext */
  188. PyKeyEvent_methods, /* tp_methods */
  189. PyKeyEvent_members, /* tp_members */
  190. PyKeyEvent_getseters, /* tp_getset */
  191. 0, /* tp_base */
  192. 0, /* tp_dict */
  193. 0, /* tp_descr_get */
  194. 0, /* tp_descr_set */
  195. 0, /* tp_dictoffset */
  196. (initproc) PyKeyEvent_init,
  197. /* tp_init */
  198. 0, /* tp_alloc */
  199. PyKeyEvent_new, /* tp_new */
  200. PyObject_Del, /* tp_free */
  201. };
  202. PyObject *PyKeyEvent_New (const KeyEvent &key)
  203. {
  204. PyKeyEvent *obj = PyObject_New (PyKeyEvent, &PyKeyEventType);
  205. new (&obj->event) KeyEvent (key);
  206. return (PyObject *)obj;
  207. }
  208. void init_event (PyObject *module)
  209. {
  210. if (PyType_Ready (&PyKeyEventType) < 0)
  211. return;
  212. Py_INCREF (&PyKeyEventType);
  213. PyModule_AddObject (module, "KeyEvent", (PyObject *)&PyKeyEventType);
  214. }