4432
|
1
|
|
2 #include "../config.h"
|
|
3
|
|
4 #ifdef HAVE_LIRC
|
|
5
|
|
6 #include <lirc/lirc_client.h>
|
|
7 #include <errno.h>
|
|
8 #include <stdio.h>
|
|
9 #include <sys/ioctl.h>
|
|
10 #include <string.h>
|
|
11 #include <fcntl.h>
|
|
12 #include <unistd.h>
|
|
13 #include <signal.h>
|
|
14 #include <sys/types.h>
|
|
15 #include <sys/wait.h>
|
|
16 #include <stdlib.h>
|
|
17
|
|
18
|
|
19 #include "../mp_msg.h"
|
|
20 #include "../help_mp.h"
|
|
21
|
|
22 static struct lirc_config *lirc_config;
|
|
23 extern char *lirc_configfile;
|
|
24
|
|
25 static int child_pid=0;
|
|
26
|
|
27 static void
|
|
28 mp_input_lirc_process_quit(int sig);
|
|
29
|
|
30 static void
|
|
31 mp_input_lirc_process(int mp_fd);
|
|
32
|
|
33
|
|
34 int
|
|
35 mp_input_lirc_init(void) {
|
|
36 int lirc_flags;
|
|
37 int lirc_sock;
|
|
38 int p[2];
|
|
39
|
|
40 mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC);
|
|
41 if((lirc_sock=lirc_init("mplayer",1))==-1){
|
|
42 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed MSGTR_LIRCdisabled);
|
|
43 return -1;
|
|
44 }
|
|
45
|
|
46 #if 0
|
|
47 fcntl(lirc_sock,F_SETOWN,getpid());
|
|
48 lirc_flags=fcntl(lirc_sock,F_GETFL,0);
|
|
49 if(lirc_flags!=-1) {
|
|
50 fcntl(lirc_sock,F_SETFL,lirc_flags|O_NONBLOCK);
|
|
51 } else {
|
|
52 lirc_deinit();
|
|
53 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCsocketerr MSGTR_LIRCdisabled,strerror(errno));
|
|
54 return -1;
|
|
55 }
|
|
56 #endif
|
|
57
|
|
58
|
|
59 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){
|
|
60 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr MSGTR_LIRCdisabled,
|
|
61 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile);
|
|
62 lirc_deinit();
|
|
63 return -1;
|
|
64 }
|
|
65
|
|
66 if(pipe(p) != 0) {
|
|
67 mp_msg(MSGT_LIRC,MSGL_ERR,"Can't create lirc pipe : %s\n",strerror(errno));
|
|
68 lirc_deinit();
|
|
69 }
|
|
70
|
|
71 child_pid = fork();
|
|
72
|
|
73 if(child_pid < 0) {
|
|
74 mp_msg(MSGT_LIRC,MSGL_ERR,"Can't fork lirc subprocess : %s\n",strerror(errno));
|
|
75 lirc_deinit();
|
|
76 close(p[0]);
|
|
77 close(p[1]);
|
|
78 return -1;
|
|
79 } else if(child_pid == 0) {// setup child process
|
|
80 close(p[0]);
|
|
81 // put some signal handlers
|
|
82 signal(SIGINT,mp_input_lirc_process_quit);
|
|
83 signal(SIGHUP,mp_input_lirc_process_quit);
|
|
84 signal(SIGQUIT,mp_input_lirc_process_quit);
|
|
85 // start the process
|
|
86 mp_input_lirc_process(p[1]);
|
|
87 }
|
|
88
|
|
89 // free unuseful ressources in parent process
|
|
90 lirc_freeconfig(lirc_config);
|
|
91 close(p[1]);
|
|
92
|
|
93 mp_msg(MSGT_LIRC,MSGL_V,"NEW LIRC init was successful.\n");
|
|
94
|
|
95 return p[0];
|
|
96 }
|
|
97
|
|
98 static void
|
|
99 mp_input_lirc_process_quit(int sig) {
|
|
100 lirc_freeconfig(lirc_config);
|
|
101 lirc_deinit();
|
|
102 exit(sig > 0 ? 0 : -1);
|
|
103 }
|
|
104
|
|
105 static void
|
|
106 mp_input_lirc_process(int mp_fd) {
|
|
107 char *cmd,*code;
|
|
108 int ret;
|
|
109
|
|
110 while(lirc_nextcode(&code)==0) {
|
|
111 if(code==NULL)
|
|
112 continue;
|
|
113 while((ret=lirc_code2char(lirc_config,code,&cmd))==0 && cmd!=NULL) {
|
|
114 int len = strlen(cmd)+1;
|
|
115 char buf[len];
|
|
116 int w=0;
|
|
117 strcpy(buf,cmd);
|
|
118 buf[len-1] = '\n';
|
|
119 while(w < len) {
|
|
120 int r = write(mp_fd,buf,len-w);
|
|
121 if(r < 0) {
|
|
122 if(errno == EINTR)
|
|
123 continue;
|
|
124 mp_msg(MSGT_LIRC,MSGL_V,"LIRC subprocess can't write in input pipe : %s\n",
|
|
125 strerror(errno));
|
|
126 mp_input_lirc_process_quit(-1);
|
|
127 }
|
|
128 w += r;
|
|
129 }
|
|
130 }
|
|
131 free(code);
|
|
132 if(ret==-1)
|
|
133 break;
|
|
134 }
|
|
135 mp_input_lirc_process_quit(-1);
|
|
136 }
|
|
137
|
|
138 void
|
|
139 mp_input_lirc_uninit(void) {
|
|
140 if( kill(child_pid,SIGQUIT) != 0) {
|
|
141 mp_msg(MSGT_LIRC,MSGL_V,"LIRC can't kill subprocess %d : %s\n",
|
|
142 child_pid,strerror(errno));
|
|
143 return;
|
|
144 }
|
|
145
|
|
146 if(waitpid(child_pid,NULL,0) < 0)
|
|
147 mp_msg(MSGT_LIRC,MSGL_V,"LIRC error while waiting subprocess %d : %s\n",
|
|
148 child_pid,strerror(errno));
|
|
149
|
|
150 }
|
|
151
|
|
152 #endif
|