LIRC libraries: /tmp/work/src/lirc-0.10.2/python-pkg/lirc/_client.c Source File

LIRC libraries Linux Infrared Remote Control

Loading...
Searching...
No Matches
_client.c
Go to the documentation of this file.
1#include <Python.h>
2#include "lirc_client.h"
3
14static const char* const LIRCRC_ID = "lircd.lircrc";
15
16
18static PyObject* client_lirc_init(PyObject *self, PyObject *args)
19{
20 const char* prog;
21 int r;
22 PyObject* intval;
23
24 if (!PyArg_ParseTuple(args, "s", &prog))
25 return NULL;
26 r = lirc_init(prog, 1);
27 if (r == -1) {
28 PyErr_SetString(PyExc_RuntimeError,
29 "Cannot open lircd socket");
30 return NULL;
31 }
32 intval = Py_BuildValue("i", r);
33 return intval;
34}
35
36
38static PyObject* client_lirc_deinit(PyObject *self, PyObject *args)
39{
40 int r;
41
42 r = lirc_deinit();
43 if (r == -1)
44 return PyErr_SetFromErrno(PyExc_OSError);
45 return Py_BuildValue("i", r);
46}
47
48
50static PyObject* client_lirc_readconfig(PyObject *self, PyObject *args)
51{
52 const char* path;
53 struct lirc_config* config;
54 PyObject* lircrc;
55 char text[128];
56
57 if (!PyArg_ParseTuple(args, "s", &path))
58 return NULL;
59 if (access(path, R_OK) != 0) {
60 snprintf(text, sizeof(text), "Cannot open %s for read", path);
61 PyErr_SetString(PyExc_RuntimeError, text);
62 return NULL;
63 }
64 if (lirc_readconfig(path, &config, NULL) != 0) {
65 PyErr_SetString(PyExc_RuntimeError,
66 "Cannot parse lircrc file");
67 return NULL;
68 }
69 lircrc = PyCapsule_New(config, LIRCRC_ID, NULL);
70 return lircrc;
71}
72
73
75static PyObject* client_lirc_freeconfig(PyObject *self, PyObject *args)
76{
77 PyObject* lircrc;
78 struct lirc_config* config;
79
80 if (!PyArg_ParseTuple(args, "O", &lircrc))
81 return NULL;
82 config = PyCapsule_GetPointer(lircrc, LIRCRC_ID);
83 lirc_freeconfig(config);
84 Py_RETURN_NONE;
85}
86
87
92static PyObject* client_lirc_code2char(PyObject *self, PyObject *args)
93{
94 PyObject* lircrc;
95 char* program;
96 char* code;
97
98 struct lirc_config* config;
99 char* s;
100 int i;
101 int r;
102 PyObject* list = PyList_New(0);
103 PyObject* string;
104
105 if (!PyArg_ParseTuple(args, "Oss", &lircrc, &program, &code)) {
106 PyErr_SetString(PyExc_RuntimeError, "Cannot parse arguments");
107 return NULL;
108 }
109 config = PyCapsule_GetPointer(lircrc, LIRCRC_ID);
110 for (i = 0; i < 10; i += 1) {
111 r = lirc_code2char(config, code, &s);
112 if (r != 0 || s == NULL || !*s)
113 break;
114 string = Py_BuildValue("s", s);
115 if (!string) {
116 PyErr_SetString(
117 PyExc_RuntimeError, "Cannot decode string");
118 Py_DECREF(list);
119 return NULL;
120 }
121 if (PyList_Append(list, string) == -1) {
122 PyErr_SetString(PyExc_RuntimeError,
123 "Cannot append string");
124 Py_DECREF(string);
125 Py_DECREF(list);
126 return NULL;
127 }
128 Py_DECREF(string);
129 }
130 return list;
131}
132
133
134PyMethodDef ClientMethods[] = {
135 {"lirc_init", client_lirc_init, METH_VARARGS,
136 "initiate lirc connection"},
137 {"lirc_deinit", client_lirc_deinit, METH_VARARGS,
138 "close lirc connection opened by lirc_init()"},
139 {"lirc_readconfig", client_lirc_readconfig, METH_VARARGS,
140 "parse a lircrc file"},
141 {"lirc_freeconfig", client_lirc_freeconfig, METH_VARARGS,
142 "Deallocate memory obtained using lirc_readconfig()"},
143 {"lirc_code2char", client_lirc_code2char, METH_VARARGS,
144 "lircrc-translate a keypress"},
145 {0}
146};
147
148
149static struct PyModuleDef clientmodule = {
150 PyModuleDef_HEAD_INIT,
151 "_client",
152 NULL, /* module documentation, may be NULL */
153 -1,
154 ClientMethods
155};
156
157
158PyMODINIT_FUNC PyInit__client(void)
159{
160 return PyModule_Create(&clientmodule);
161}
lirc_deinit
int lirc_deinit(void)
Release resources allocated by lirc_init(), basically disconnect from socket.
Definition lirc_client.c:365
lirc_readconfig
int lirc_readconfig(const char *file, struct lirc_config **config, int(check)(char *s))
Parse a lircrc configuration file.
Definition lirc_client.c:1447
lirc_freeconfig
void lirc_freeconfig(struct lirc_config *config)
Deallocate an object retrieved using lirc_readconfig().
Definition lirc_client.c:1534
lirc_code2char
int lirc_code2char(struct lirc_config *config, char *code, char **string)
Translate a code string to an application string using .lircrc.
Definition lirc_client.c:1813
lirc_init
int lirc_init(const char *prog, int verbose)
Initial setup: connect to lircd socket.
Definition lirc_client.c:344
lirc_client.h
3-rd party application interface.
lirc_config
Definition lirc_client.h:156