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
|
/*
* 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 Library 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#ifdef HAVE_LIRC
#include <string.h>
#include <fcntl.h>
#include <gnome.h>
#ifdef HAVE_LIRC
#include <lirc/lirc_client.h>
#endif
#include "lirc.h"
#include "gui.h"
extern GtkWidget* mute_button, *preset_combo;
extern GtkAdjustment *adj;
extern GtkTooltips *tooltips;
extern int mom_ps;
extern gnomeradio_settings settings;
static int fd = -1;
static struct lirc_config *config = NULL;
static void execute_lirc_command (char *cmd)
{
printf("lirc command: %s\n", cmd);
int vol = (int)(gtk_volume_button_get_value(mute_button) + 0.5f);
if (strcasecmp (cmd, "tune up") == 0)
{
scfw_button_clicked_cb(NULL, NULL);
}
else if (strcasecmp (cmd, "tune down") == 0)
{
scbw_button_clicked_cb(NULL, NULL);
}
else if (strcasecmp (cmd, "volume up") == 0)
{
gtk_volume_button_set_value(mute_button, vol > 95 ? 100 : vol + 5);
/*gtk_adjustment_set_value(volume, (volume->value > 95) ? 100 : volume->value+5);*/
}
else if (strcasecmp (cmd, "volume down") == 0)
{
gtk_volume_button_set_value(mute_button, vol < 5 ? 0 : vol - 5);
/*gtk_adjustment_set_value(volume,(volume->value < 5) ? 0 : volume->value-5);*/
}
else if (strcasecmp (cmd, "mute") == 0)
{
toggle_volume();
}
else if (strcasecmp (cmd, "tv") == 0)
{
exit_gnome_radio();
gnome_execute_shell(NULL, "xawtv");
}
else if (strcasecmp (cmd, "quit") == 0)
{
exit_gnome_radio();
}
else if (strcasecmp (cmd, "preset up") == 0)
{
change_preset(TRUE);
}
else if (strcasecmp (cmd, "preset down") == 0)
{
change_preset(FALSE);
}
else if (strncasecmp (cmd, "preset ", 7) == 0)
{
int tmp = 0, ret;
ret = sscanf(cmd, "preset %i", &tmp);
if (ret && (tmp < g_list_length(settings.presets)))
{
preset *ps;
ps = g_list_nth_data(settings.presets, tmp);
gtk_adjustment_set_value(adj, ps->freq*STEPS);
mom_ps = tmp;
preset_combo_set_item(mom_ps);
}
}
else
{
printf ("unrecognized lirccmd: %s\n", cmd);
}
}
static char* map_code_to_default(char *code)
{
char event[21];
unsigned int dummy, repeat = 0;
int key = 0;
if (sscanf(code,"%x %x %20s", &dummy, &repeat, event) != 3)
{
printf("lirc: oops, parse error: %s", code);
return NULL;
}
if (!strcasecmp("VOL+", event))
return (char*)strdup("volume up");
else if (!strcasecmp("VOL-", event))
return (char*)strdup("volume down");
else if (!repeat && !strcasecmp("CH+", event))
return (char*)strdup("tune up");
else if (!repeat && !strcasecmp("CH-", event))
return (char*)strdup("tune down");
else if (!repeat && !strcasecmp("MUTE", event))
return (char*)strdup("mute");
else if (!repeat && !strcasecmp("MINIMIZE", event))
return (char*)strdup("quit");
if (sscanf(event, "%d", &key) == 1)
{
char *ret;
if (repeat || (key < 0) || (key > 9))
return NULL;
ret = malloc(strlen("preset xx"));
sprintf(ret, "preset %1.1d", key);
return ret;
}
return NULL;
}
int my_lirc_init(void)
{
/*printf("Trying to bring up lirc\n");*/
if ((fd = lirc_init ("gnomeradio", 0)) <=0)
{
perror("lirc_init");
return 0;
}
if (lirc_readconfig (NULL, &config, NULL) != 0)
{
perror("lirc_readconfig");
config = NULL;
/*lirc_deinit ();
fd = -1;
return 0;*/
printf("The lirc configfile (~/.lircrc) could not be opened.\n"
"Using default config.\n");
}
fcntl (fd, F_SETFL, O_NONBLOCK);
fcntl (fd, F_SETFD, FD_CLOEXEC);
return 1;
}
void my_lirc_deinit(void)
{
if (fd <= 0)
return;
/*printf("Shutting down lirc\n");*/
/*gdk_input_remove (input_tag);*/
lirc_freeconfig(config);
lirc_deinit ();
}
static gboolean lirc_has_data_cb(GIOChannel *source, GIOCondition condition, gpointer data)
{
char *code, *cmd;
int ret = -1;
while (lirc_nextcode (&code) == 0 && code != NULL)
{
ret = 0;
if (config)
{
while (lirc_code2char (config, code, &cmd) == 0 && cmd != NULL)
{
execute_lirc_command(cmd);
}
}
else
{
cmd = map_code_to_default(code);
if (cmd)
{
execute_lirc_command(cmd);
free(cmd);
}
}
free (code);
}
/* on LIRC error, shutdown added input*/
if (ret == -1)
{
printf("An lirc error occured\n");
/*gdk_input_remove (input_tag);*/
lirc_freeconfig (config);
config = NULL;
lirc_deinit ();
return FALSE;
}
return TRUE;
}
void start_lirc(void)
{
GIOChannel *ioc;
if (fd<0)
return;
ioc = g_io_channel_unix_new(fd);
g_io_add_watch(ioc, G_IO_IN, lirc_has_data_cb, NULL);
}
#endif
|