PageRenderTime 407ms CodeModel.GetById 101ms app.highlight 191ms RepoModel.GetById 110ms app.codeStats 1ms

/src/scim-python-helper.cpp

http://scim-python.googlecode.com/
C++ | 516 lines | 405 code | 85 blank | 26 comment | 27 complexity | dfe01fdafeae6ce53b402ed80c498e4c 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
 26#include "scim-python.h"
 27#include <glib.h>
 28
 29using namespace scim;
 30
 31struct PyHelperAgentObject {
 32	PyListObject list;
 33	/* Type-specific fields go here. */
 34	PyHelperAgent helper_agent;
 35};
 36
 37PyHelperAgent::PyHelperAgent (PyObject *self)
 38	:self ((PyHelperAgentObject *)self)
 39{
 40	Py_INCREF (self);
 41
 42	exit_signal_connection = 
 43		signal_connect_exit (slot (this, &PyHelperAgent::slot_exit));
 44	attach_input_context_signal_connection = 
 45		signal_connect_attach_input_context (slot (this, &PyHelperAgent::slot_attach_input_context));
 46	detach_input_context_signal_connection =
 47		signal_connect_detach_input_context (slot (this, &PyHelperAgent::slot_detach_input_context));
 48	reload_config_signal_connection =
 49		signal_connect_reload_config (slot (this, &PyHelperAgent::slot_reload_config));
 50	update_screen_signal_connection = 
 51		signal_connect_update_screen (slot (this, &PyHelperAgent::slot_update_screen));
 52	update_spot_location_signal_connection = 
 53		signal_connect_update_spot_location (slot (this, &PyHelperAgent::slot_update_spot_location));
 54	trigger_property_connection = 
 55		signal_connect_trigger_property (slot (this, &PyHelperAgent::slot_trigger_property));
 56	process_imengine_event_connection = 
 57		signal_connect_process_imengine_event (slot (this, &PyHelperAgent::slot_process_imengine_event));
 58
 59};
 60
 61PyHelperAgent::~PyHelperAgent () 
 62{
 63	exit_signal_connection.disconnect ();
 64	attach_input_context_signal_connection.disconnect ();
 65	detach_input_context_signal_connection.disconnect ();
 66	reload_config_signal_connection.disconnect ();
 67	update_screen_signal_connection.disconnect ();
 68	update_spot_location_signal_connection.disconnect ();
 69	trigger_property_connection.disconnect ();
 70	process_imengine_event_connection.disconnect ();
 71
 72	Py_XDECREF ((PyObject *)self);
 73}
 74
 75void
 76PyHelperAgent::operator delete (void *p)
 77{
 78	// do nothing
 79}
 80
 81PyObject *
 82PyHelperAgent::py_open_connection (PyHelperAgentObject *self, PyObject *args)
 83{
 84	char *uuid = NULL;
 85	char *name = NULL;
 86	char *icon = NULL;
 87	char *desc = NULL;
 88	int opt = 0;
 89	char *display;
 90	int retval;
 91
 92	if (!PyArg_ParseTuple (args, "(ssssi)s:open_connection", &uuid, &name, &icon, &desc, &opt, &display))
 93			return NULL;
 94
 95	retval = self->helper_agent.open_connection (HelperInfo (uuid, name, icon, desc, opt), display);
 96
 97	return PyInt_FromLong (retval);
 98}
 99
100PyObject *
101PyHelperAgent::py_close_connection (PyHelperAgentObject *self, PyObject *args)
102{
103	self->helper_agent.close_connection ();
104
105	Py_INCREF (Py_None);
106	return Py_None;
107}
108
109PyObject *
110PyHelperAgent::py_get_connection_number (PyHelperAgentObject *self, PyObject *args)
111{
112	int retval;
113
114	retval = self->helper_agent.get_connection_number ();
115
116	return PyInt_FromLong (retval);
117}
118
119PyObject *
120PyHelperAgent::py_is_connected (PyHelperAgentObject *self, PyObject *args)
121{
122	PyObject *result = NULL;
123	bool retval;
124
125	retval = self->helper_agent.is_connected ();
126
127	if (retval) {
128		result = Py_True;
129	}
130	else {
131		result = Py_False;
132	}
133	Py_INCREF (result);
134	return result;
135}
136
137PyObject *
138PyHelperAgent::py_has_pending_event (PyHelperAgentObject *self, PyObject *args)
139{
140	PyObject *result = NULL;
141	bool retval;
142
143	retval = self->helper_agent.has_pending_event ();
144
145	if (retval) {
146		result = Py_True;
147	}
148	else {
149		result = Py_False;
150	}
151	Py_INCREF (result);
152	return result;
153}
154
155PyObject *
156PyHelperAgent::py_filter_event (PyHelperAgentObject *self, PyObject *args)
157{
158	PyObject *result = NULL;
159	bool retval;
160
161	retval = self->helper_agent.filter_event ();
162
163	if (retval) {
164		result = Py_True;
165	}
166	else {
167		result = Py_False;
168	}
169	Py_INCREF (result);
170	return result;
171}
172
173PyObject *
174PyHelperAgent::py_reload_config (PyHelperAgentObject *self, PyObject *args)
175{
176	self->helper_agent.reload_config ();
177
178	Py_INCREF (Py_None);
179	return Py_None;
180}
181
182PyObject *
183PyHelperAgent::py_register_properties (PyHelperAgentObject *self, PyObject *args)
184{
185	PyObject *props = NULL;
186	PropertyList proplist;
187	int i;
188
189	if (!PyArg_ParseTuple (args, "O:register_properties", &props))
190		return NULL;
191
192	if (PyList_Check (props)) {
193		for (i = 0; i < PyList_Size (props); i++) {
194			PyObject *prop = PyList_GetItem (props, i);
195			proplist.push_back (PyProperty_AsProperty (prop));
196		}
197	}
198	else if (PyTuple_Check (props)) {
199		for (i = 0; i < PyTuple_Size (props); i++) {
200			PyObject *prop = PyTuple_GetItem (props, i);
201			proplist.push_back (PyProperty_AsProperty (prop));
202		}
203
204	}
205	else {
206		PyErr_SetString (PyExc_TypeError, "the argument must be a list or a tuple that contains propertys");
207		return NULL;
208	}
209
210	self->helper_agent.register_properties (proplist);
211
212	Py_INCREF (Py_None);
213	return Py_None;
214
215}
216
217PyObject *
218PyHelperAgent::py_update_property (PyHelperAgentObject *self, PyObject *args)
219{
220	PyObject *prop = NULL;
221
222	if (!PyArg_ParseTuple (args, "O:update_property", &prop))
223		return NULL;
224
225	self->helper_agent.update_property (PyProperty_AsProperty (prop));
226
227	Py_INCREF (Py_None);
228	return Py_None;
229}
230
231PyObject *
232PyHelperAgent::py_send_imengine_event (PyHelperAgentObject *self, PyObject *args)
233{
234	int ic;
235	char *ic_uuid = NULL;
236	char *buf = NULL;
237	int len = 0;
238	Transaction trans;
239
240	if (!PyArg_ParseTuple (args, "isst#:send_imengine_event", &ic, &ic_uuid, &buf, &len)) {
241		return NULL;
242	}
243
244	trans.read_from_buffer (buf, len);
245	self->helper_agent.send_imengine_event (ic, String (ic_uuid), trans);
246
247	Py_INCREF (Py_None);
248	return Py_None;
249
250	Py_INCREF (Py_None);
251	return Py_None;
252}
253
254PyObject *
255PyHelperAgent::py_send_key_event (PyHelperAgentObject *self, PyObject *args)
256{
257	int ic;
258	char *ic_uuid = NULL;
259	int code, mask, layout;
260
261	if (!PyArg_ParseTuple (args, "isiii:send_key_event", &ic, &ic_uuid, &code, &mask, &layout)) {
262		return NULL;
263	}
264
265	self->helper_agent.send_key_event (ic, String (ic_uuid), KeyEvent (code, mask, layout));
266
267	Py_INCREF (Py_None);
268	return Py_None;
269}
270
271PyObject *
272PyHelperAgent::py_forward_key_event	(PyHelperAgentObject *self, PyObject *args)
273{
274	int ic;
275	char *ic_uuid = NULL;
276	int code, mask, layout;
277
278	if (!PyArg_ParseTuple (args, "isiii:forward_key_event", &ic, &ic_uuid, &code, &mask, &layout)) {
279		return NULL;
280	}
281
282	self->helper_agent.forward_key_event (ic, String (ic_uuid), KeyEvent (code, mask, layout));
283
284	Py_INCREF (Py_None);
285	return Py_None;
286}
287
288PyObject *
289PyHelperAgent::py_commit_string (PyHelperAgentObject *self, PyObject *args)
290{
291	int ic;
292	char *ic_uuid = NULL;
293	char *str = NULL;
294
295	if (!PyArg_ParseTuple (args, "iss:commit_string", &ic, &ic_uuid, &str)) {
296		return NULL;
297	}
298
299	self->helper_agent.commit_string (ic, String (ic_uuid), utf8_mbstowcs (str));
300
301	Py_INCREF (Py_None);
302	return Py_None;
303}
304
305#define PY_CALL(fun, args) ({						\
306	PyObject *pValue = NULL;						\
307	PyObject *pFunc = NULL;							\
308	pFunc = PyObject_GetAttrString ((PyObject *)this->self, fun);\
309	if (pFunc != NULL) {							\
310		pValue = PyObject_CallObject (pFunc, args);	\
311		Py_DECREF (pFunc);							\
312	}												\
313	pValue;})
314
315#define PY_CHECK_RET(v)							\
316	if (v == NULL) {							\
317		PyErr_Print ();							\
318		return;									\
319	}
320void
321PyHelperAgent::slot_exit (const HelperAgent *helper, int ic, const String &ic_uuid)
322{
323}
324
325void
326PyHelperAgent::slot_attach_input_context (const HelperAgent *helper, int ic, const String &ic_uuid)
327{
328}
329
330void
331PyHelperAgent::slot_detach_input_context (const HelperAgent *helper, int ic, const String &ic_uuid)
332{
333}
334
335void
336PyHelperAgent::slot_reload_config (const HelperAgent *helper, int ic, const String &ic_uuid)
337{
338}
339
340void
341PyHelperAgent::slot_update_screen (const HelperAgent *helper, int ic, const String &ic_uuid, int screen_number)
342{
343}
344
345void
346PyHelperAgent::slot_update_spot_location (const HelperAgent *helper, int ic, const String &ic_uuid, int x, int y)
347{
348}
349
350void
351PyHelperAgent::slot_trigger_property (const HelperAgent *helper, int ic, const String &ic_uuid, const String &property)
352{
353	PyObject *pValue = NULL;
354	PyObject *pArgs = NULL;
355
356	pArgs = Py_BuildValue ("(iss)", ic, ic_uuid.c_str (), property.c_str ());
357
358	pValue = PY_CALL ("trigger_property", pArgs);
359	PY_CHECK_RET (pValue);
360
361	Py_XDECREF (pArgs);
362	Py_XDECREF (pValue);
363}
364
365void
366PyHelperAgent::slot_process_imengine_event (const HelperAgent *helper, int ic, const String &ic_uuid, const Transaction &transaction)
367{
368}
369
370
371PyMethodDef 
372PyHelperAgent::py_methods[] = { 
373		{	"open_connection", (PyCFunction)PyHelperAgent::py_open_connection, METH_VARARGS,
374			"open a connection"
375		},
376		{	"get_connection_number", (PyCFunction)PyHelperAgent::py_get_connection_number, METH_NOARGS,
377			"return id, that was returned by open_connection previously."
378		},
379		{	"close_connection", (PyCFunction)PyHelperAgent::py_close_connection, METH_NOARGS,
380			"close the connection"
381		},
382		{	"is_connected", (PyCFunction)PyHelperAgent::py_is_connected, METH_NOARGS,
383			"If agent is connected, the function returns True"
384		},
385		{	"has_pending_event", (PyCFunction)PyHelperAgent::py_has_pending_event, METH_NOARGS,
386			"If agent has pending event, the function returns True"
387		},
388		{	"filter_event", (PyCFunction)PyHelperAgent::py_filter_event, METH_NOARGS,
389			"filter events"
390		},
391		{	"reload_config", (PyCFunction)PyHelperAgent::py_reload_config, METH_NOARGS,
392			"This function will cause all IM engines to reload config"
393		},
394		{	"register_properties", (PyCFunction)PyHelperAgent::py_register_properties, METH_VARARGS,
395			"Register properties"
396		},
397		{	"update_property", (PyCFunction)PyHelperAgent::py_update_property, METH_VARARGS,
398			"Update a property"
399		},
400		{	"send_imengine_event", (PyCFunction)PyHelperAgent::py_send_imengine_event, METH_VARARGS,
401			"Send a event to IMEngine"
402		},
403		{	"send_key_event", (PyCFunction)PyHelperAgent::py_send_key_event, METH_VARARGS,
404			"Send a keyevent"
405		},
406		{	"forward_key_event", (PyCFunction)PyHelperAgent::py_forward_key_event, METH_VARARGS,
407			"Forward a keyevent"
408		},
409		{	"commit_string", (PyCFunction)PyHelperAgent::py_commit_string, METH_VARARGS,
410			"Commit a string to IC"
411		},
412		{ NULL }
413};
414
415PyObject *
416PyHelperAgent::py_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
417{
418	PyHelperAgentObject *self;
419
420	self = (PyHelperAgentObject *)type->tp_alloc (type, 0);
421	return (PyObject *)self;
422}
423
424int
425PyHelperAgent::py_init (PyHelperAgentObject *self, PyObject *args, PyObject *kwds)
426{
427	new (&self->helper_agent) PyHelperAgent ((PyObject *)self);
428
429	return 0;
430}
431
432void
433PyHelperAgent::py_dealloc (PyHelperAgentObject *self)
434{
435	self->helper_agent.~PyHelperAgent ();
436	((PyObject *) self)->ob_type->tp_free (self);
437}
438
439
440HelperAgent *
441PyHelperAgent::from_pyobject (PyObject *object)
442{
443	PyHelperAgentObject *self = (PyHelperAgentObject *) object;
444	return (HelperAgent *)&self->helper_agent;
445}
446
447PyTypeObject PyHelperAgentType = {
448	PyObject_HEAD_INIT (NULL)
449	0,						 							/*ob_size*/
450	"scim.HelperAgent", 							/*tp_name*/
451	sizeof (PyHelperAgentObject),						/*tp_basicsize*/
452	0,						 							/*tp_itemsize*/
453	(destructor)PyHelperAgent::py_dealloc,	/*tp_dealloc*/
454	0,			  										/*tp_print*/
455	0,						 							/*tp_getattr*/
456	0,													/*tp_setattr*/
457	0,													/*tp_compare*/
458	0,									/*tp_repr*/
459	0,									/*tp_as_number*/
460	0,									/*tp_as_sequence*/
461	0,									/*tp_as_mapping*/
462	0,									/*tp_hash */
463	0,									/*tp_call*/
464	0,		  							/*tp_str*/
465	0,					   				/*tp_getattro*/
466	0,									/*tp_setattro*/
467	0,					 				/*tp_as_buffer*/
468	Py_TPFLAGS_DEFAULT | 
469	Py_TPFLAGS_BASETYPE,				/*tp_flags*/
470	"HelperAgent objects",		/* tp_doc */
471	0,					   /* tp_traverse */
472	0,					   /* tp_clear */
473	0,					   /* tp_richcompare */
474	0,					   /* tp_weaklistoffset */
475	0,					   /* tp_iter */
476	0,					   /* tp_iternext */
477	PyHelperAgent::py_methods,			 /* tp_methods */
478	0,			 /* tp_members */
479	0,						 /* tp_getset */
480	0,						 /* tp_base */
481	0,						 /* tp_dict */
482	0,						 /* tp_descr_get */
483	0,						 /* tp_descr_set */
484	0,						 /* tp_dictoffset */
485	(initproc)PyHelperAgent::py_init,	  /* tp_init */
486	0,						 /* tp_alloc */
487	PyHelperAgent::py_new,				 /* tp_new */
488};
489
490
491static void
492setint (PyObject *d, const char *name, long value)
493{
494	PyObject *o = PyInt_FromLong (value);
495	if (o && PyDict_SetItemString (d, name, o) == 0) {
496		Py_DECREF (o);
497	}
498}
499
500void init_helper (PyObject *module)
501{
502	PyObject *dict = NULL;
503
504	if (PyType_Ready (&PyHelperAgentType) < 0)
505		return;
506
507	Py_INCREF (&PyHelperAgentType);
508	PyModule_AddObject (module, "HelperAgent", (PyObject *)&PyHelperAgentType);
509
510	dict = PyModule_GetDict (module);
511	setint (dict, "SCIM_HELPER_STAND_ALONE", SCIM_HELPER_STAND_ALONE);
512	setint (dict, "SCIM_HELPER_AUTO_START", SCIM_HELPER_AUTO_START);
513	setint (dict, "SCIM_HELPER_AUTO_RESTART", SCIM_HELPER_AUTO_RESTART);
514	setint (dict, "SCIM_HELPER_NEED_SCREEN_INFO", SCIM_HELPER_NEED_SCREEN_INFO);
515	setint (dict, "SCIM_HELPER_NEED_SPOT_LOCATION_INFO", SCIM_HELPER_NEED_SPOT_LOCATION_INFO);
516}