[6b8ec8]: src / lirc.c  Maximize  Restore  History

Download this file

234 lines (202 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright (C) 2001-2006 the xine-project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* lirc (infrared control) code, originally based on oxine
*/
#include "globals.h"
#include "defs.h"
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <glib.h>
#include <gtk/gtk.h>
#ifdef HAVE_LIRC
#include <lirc/lirc_client.h>
#endif
#include "lirc.h"
#include "engine.h"
#ifdef HAVE_LIRC
static pthread_t gxine_lirc_thread;
static pthread_mutex_t lirc_init_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t lirc_init_cond = PTHREAD_COND_INITIALIZER;
static struct lirc_config *lircconfig;
static int gxine_lirc_fd;
/* lirc thread
primary config file for lirc is ~/.gxine/lircrc
secondary is ~/.lircrc
prog name = gxine
possible config = lots :-)
*/
static int gxine_lirc_tryload (const char *file)
{
struct stat st;
char *fname = g_build_filename (g_get_home_dir(), file, NULL);
if (stat (fname, &st))
{
if (errno == ENOENT)
{
free (fname);
return 1;
}
g_printerr (_("lirc: problem with ~/%s: %s\n"), file, strerror (errno));
free (fname);
return 1;
}
if (lirc_readconfig (fname, &lircconfig, NULL) != 0)
{
g_printerr(_("lirc: lirc_readconfig of %s failed\n"), fname);
free (fname);
return 1;
}
free (fname);
return 0;
}
static void gxine_lirc_cleanup (void *data)
{
lirc_deinit ();
}
static __attribute__ ((noreturn)) void *gxine_lirc_run (void *data)
{
char *config;
gboolean first = TRUE;
for (;;)
{
pthread_mutex_lock (&lirc_init_mutex);
gxine_lirc_fd = -1;
while (gxine_lirc_fd == -1)
{
if ((gxine_lirc_fd = lirc_init ("gxine", 0)) != -1)
{
if (!first)
logprintf ("lirc: reconnected\n");
break;
}
if (!first)
{
pthread_cond_signal (&lirc_init_cond);
pthread_mutex_unlock (&lirc_init_mutex);
pthread_cleanup_push (gxine_lirc_cleanup, NULL);
sleep (1);
pthread_testcancel();
pthread_cleanup_pop (0);
pthread_mutex_lock (&lirc_init_mutex);
continue;
}
g_printerr (_("lirc: cannot initialise - disabling remote control\n"
"lirc: maybe lircd isn't running or you can't connect to the socket?\n"));
failed:
gxine_lirc_thread = 0;
pthread_cond_signal (&lirc_init_cond);
pthread_mutex_unlock (&lirc_init_mutex);
pthread_exit(NULL);
}
first = FALSE;
pthread_cleanup_push (gxine_lirc_cleanup, NULL);
/* read ~/.gxine/lircrc then ~/.lircrc - definitions are not overridden */
if ((gxine_lirc_tryload (".gxine/lircrc")
&& gxine_lirc_tryload (".lircrc"))
|| !lircconfig || !lircconfig->first)
{
/* if neither file has been read successfully or there's nothing for
* gxine, exit now */
g_printerr (_("lirc: no configuration found - disabling remote control\n"));
goto failed;
}
pthread_cond_signal (&lirc_init_cond);
pthread_mutex_unlock (&lirc_init_mutex);
for (;;)
{
char *code;
pthread_testcancel();
if (lirc_nextcode (&code) == 0)
{
pthread_testcancel();
if (code != NULL)
{
int ret;
while ((ret = lirc_code2char (lircconfig, code, &config) == 0)
&& config != NULL)
{
char *src = g_strdup_printf (_("LIRC binding %s.%s"),
lircconfig->current_mode, code);
engine_queue_push (config, NULL, NULL, NULL, NULL, src);
free (src);
}
free(code);
}
}
else
{
/* lircd has exited? try reinitialising in case it's been restarted */
logprintf ("lirc: socket read error\n");
pthread_testcancel();
break;
}
}
pthread_cleanup_pop (1);
}
}
static void gxine_lirc_start (void)
{
pthread_attr_t attr;
gxine_lirc_thread = 0;
pthread_mutex_lock (&lirc_init_mutex);
pthread_attr_init (&attr);
pthread_attr_setstacksize (&attr, 256 * 1024); /* probably overkill */
if (!pthread_create (&gxine_lirc_thread, &attr, gxine_lirc_run, NULL))
pthread_cond_wait (&lirc_init_cond, &lirc_init_mutex);
pthread_attr_destroy (&attr);
pthread_mutex_unlock (&lirc_init_mutex);
}
#else
#define gxine_lirc_thread (0)
#define gxine_lirc_start()
#endif
void gxine_lirc_quit (void)
{
#ifdef HAVE_LIRC
if (gxine_lirc_thread)
{
pthread_cancel (gxine_lirc_thread);
lirc_freeconfig (lircconfig);
}
#endif
}
static JSBool js_reload_lirc (JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{
se_log_fncall ("reload_lirc");
se_argc_check_max (0, "reload_lirc");
gxine_lirc_quit ();
if (gxine_lirc_thread)
pthread_join (gxine_lirc_thread, NULL);
gxine_lirc_start ();
*rval = gxine_lirc_thread ? JSVAL_TRUE : JSVAL_FALSE;
return JS_TRUE;
}
void gxine_lirc_init (void)
{
se_defun (gse, NULL, "reload_lirc", js_reload_lirc, 0, 0,
SE_GROUP_HIDDEN, NULL, NULL);
gxine_lirc_start ();
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks