1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <pthread.h>
8 
9 #include "config.h"
10 
11 #ifdef HAVE_LIBLIRC_CLIENT
12 # include <lirc/lirc_client.h>
13 #endif
14 #include "grab-ng.h"
15 #include "commands.h"
16 #include "lirc.h"
17 #include "event.h"
18 
19 /*-----------------------------------------------------------------------*/
20 
21 extern int debug;
22 
23 #ifdef HAVE_LIBLIRC_CLIENT
24 static struct lirc_config *config = NULL;
25 
26 static struct event_entry lirc_events[] = {
27     {
28 	.event =  "lirc-key-ch+",
29 	.action = "setstation next",
30     },{
31 	.event =  "lirc-key-ch-",
32 	.action = "setstation prev",
33     },{
34 	.event =  "lirc-key-vol+",
35 	.action = "volume inc",
36     },{
37 	.event =  "lirc-key-vol-",
38 	.action = "volume dec",
39     },{
40 	.event =  "lirc-key-mute",
41 	.action = "volume mute",
42     },{
43 	.event =  "lirc-key-full_screen",
44 	.action = "fullscreen toggle",
45     },{
46 	.event =  "lirc-key-source",
47 	.action = "setinput next",
48     },{
49 	.event =  "lirc-key-reserved",
50 	.action = "quit",
51     },{
52 	.event =  "lirc-key-0",
53 	.action = "keypad 0",
54     },{
55 	.event =  "lirc-key-1",
56 	.action = "keypad 1",
57     },{
58 	.event =  "lirc-key-2",
59 	.action = "keypad 2",
60     },{
61 	.event =  "lirc-key-3",
62 	.action = "keypad 3",
63     },{
64 	.event =  "lirc-key-4",
65 	.action = "keypad 4",
66     },{
67 	.event =  "lirc-key-5",
68 	.action = "keypad 5",
69     },{
70 	.event =  "lirc-key-6",
71 	.action = "keypad 6",
72     },{
73 	.event =  "lirc-key-7",
74 	.action = "keypad 7",
75     },{
76 	.event =  "lirc-key-8",
77 	.action = "keypad 8",
78     },{
79 	.event =  "lirc-key-9",
80 	.action = "keypad 9",
81     },{
82 	/* end of list */
83     }
84 };
85 #endif
86 
lirc_tv_init()87 int lirc_tv_init()
88 {
89 #ifdef HAVE_LIBLIRC_CLIENT
90     int fd;
91 
92     if (-1 == (fd = lirc_init("xawtv",debug))) {
93 	if (debug)
94 	    fprintf(stderr,"lirc: no infrared remote support available\n");
95 	return -1;
96     }
97     if (0 != lirc_readconfig(NULL,&config,NULL)) {
98 	config = NULL;
99     }
100     if (debug)
101 	fprintf(stderr, "lirc: ~/.lircrc file %sfound\n",
102 		config ? "" : "not ");
103 
104     fcntl(fd,F_SETFL,O_NONBLOCK);
105     fcntl(fd,F_SETFD,FD_CLOEXEC);
106     event_register_list(lirc_events);
107     if (debug)
108 	fprintf(stderr,"lirc: init ok\n");
109 
110     return fd;
111 #else
112     if (debug)
113 	fprintf(stderr,"lirc: not enabled at compile time\n");
114     return -1;
115 #endif
116 }
117 
lirc_tv_havedata()118 int lirc_tv_havedata()
119 {
120 #ifdef HAVE_LIBLIRC_CLIENT
121     char *code,event[32],*cmd,**argv;
122     int dummy,repeat,argc;
123     int ret=-1;
124 
125     strcpy(event,"lirc-key-");
126     while (lirc_nextcode(&code)==0  &&  code!=NULL) {
127 	ret = 0;
128 	if (3 != sscanf(code,"%x %x %20s",&dummy,&repeat,event+9)) {
129 	    fprintf(stderr,"lirc: oops, parse error: %s",code);
130 	    continue;
131 	}
132 	if (debug)
133 	    fprintf(stderr,"lirc: key=%s repeat=%d\n", event+9, repeat);
134 	if (config) {
135 	    /* use ~/.lircrc */
136 	    while (lirc_code2char(config,code,&cmd)==0 && cmd != NULL) {
137 		if (debug)
138 		    fprintf(stderr,"lirc: cmd \"%s\"\n", cmd);
139 		if (0 == strcasecmp(cmd,"eventmap")) {
140 		    event_dispatch(event);
141 		} else {
142 		    argv = split_cmdline(cmd,&argc);
143 		    do_command(argc,argv);
144 		}
145 	    }
146 	} else {
147 	    /* standalone mode */
148 	    if (!repeat)
149 		event_dispatch(event);
150 	}
151 	free(code);
152     }
153     return ret;
154 #else
155     return 0;
156 #endif
157 }
158