annotate input/lirc.c @ 7170:4ae1a0fecac5

upsz, 10l
author pontscho
date Fri, 30 Aug 2002 19:32:43 +0000
parents aed5971d73a8
children d375383e1a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
1
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
2 #include "../config.h"
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
3
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
4 #ifdef HAVE_LIRC
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
5
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
6 #include <lirc/lirc_client.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
7 #include <errno.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
8 #include <stdio.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
9 #include <sys/ioctl.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
10 #include <string.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
11 #include <fcntl.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
12 #include <unistd.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
13 #include <signal.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
14 #include <sys/types.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
15 #include <sys/wait.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
16 #include <stdlib.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
17
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
18
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
19 #include "../mp_msg.h"
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
20 #include "../help_mp.h"
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
21
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
22 static struct lirc_config *lirc_config;
4823
d25b898c4c44 Make old and new lirc support independant from each other
albeu
parents: 4526
diff changeset
23 char *lirc_configfile;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
24
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
25 static int child_pid=0;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
26
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
27 static void
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
28 mp_input_lirc_process_quit(int sig);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
29
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
30 static void
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
31 mp_input_lirc_process(int mp_fd);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
32
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
33
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
34 int
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
35 mp_input_lirc_init(void) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
36 int lirc_sock;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
37 int p[2];
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
38
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
39 mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
40 if((lirc_sock=lirc_init("mplayer",1))==-1){
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
41 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed MSGTR_LIRCdisabled);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
42 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
43 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
44
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
45 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
46 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr MSGTR_LIRCdisabled,
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
47 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
48 lirc_deinit();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
49 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
50 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
51
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
52 if(pipe(p) != 0) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
53 mp_msg(MSGT_LIRC,MSGL_ERR,"Can't create lirc pipe : %s\n",strerror(errno));
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
54 lirc_deinit();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
55 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
56
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
57 child_pid = fork();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
58
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
59 if(child_pid < 0) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
60 mp_msg(MSGT_LIRC,MSGL_ERR,"Can't fork lirc subprocess : %s\n",strerror(errno));
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
61 lirc_deinit();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
62 close(p[0]);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
63 close(p[1]);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
64 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
65 } else if(child_pid == 0) {// setup child process
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
66 close(p[0]);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
67 // put some signal handlers
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
68 signal(SIGINT,mp_input_lirc_process_quit);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
69 signal(SIGHUP,mp_input_lirc_process_quit);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
70 signal(SIGQUIT,mp_input_lirc_process_quit);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
71 // start the process
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
72 mp_input_lirc_process(p[1]);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
73 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
74
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
75 // free unuseful ressources in parent process
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
76 lirc_freeconfig(lirc_config);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
77 close(p[1]);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
78
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
79 mp_msg(MSGT_LIRC,MSGL_V,"NEW LIRC init was successful.\n");
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
80
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
81 return p[0];
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
82 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
83
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
84 static void
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
85 mp_input_lirc_process_quit(int sig) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
86 lirc_freeconfig(lirc_config);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
87 lirc_deinit();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
88 exit(sig > 0 ? 0 : -1);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
89 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
90
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
91 static void
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
92 mp_input_lirc_process(int mp_fd) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
93 char *cmd,*code;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
94 int ret;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
95
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
96 while(lirc_nextcode(&code)==0) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
97 if(code==NULL)
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
98 continue;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
99 while((ret=lirc_code2char(lirc_config,code,&cmd))==0 && cmd!=NULL) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
100 int len = strlen(cmd)+1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
101 char buf[len];
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
102 int w=0;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
103 strcpy(buf,cmd);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
104 buf[len-1] = '\n';
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
105 while(w < len) {
5196
aed5971d73a8 A bug fix + moved error msg from verbose to error
albeu
parents: 4823
diff changeset
106 int r = write(mp_fd,buf+w,len-w);
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
107 if(r < 0) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
108 if(errno == EINTR)
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
109 continue;
5196
aed5971d73a8 A bug fix + moved error msg from verbose to error
albeu
parents: 4823
diff changeset
110 mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC subprocess can't write in input pipe : %s\n",
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
111 strerror(errno));
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
112 mp_input_lirc_process_quit(-1);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
113 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
114 w += r;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
115 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
116 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
117 free(code);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
118 if(ret==-1)
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
119 break;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
120 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
121 mp_input_lirc_process_quit(-1);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
122 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
123
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
124 void
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
125 mp_input_lirc_uninit(void) {
4526
c619a7271690 Remove some unuseful stuff and don't try to kill an unexisting
albeu
parents: 4432
diff changeset
126 if(child_pid <= 0)
c619a7271690 Remove some unuseful stuff and don't try to kill an unexisting
albeu
parents: 4432
diff changeset
127 return;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
128 if( kill(child_pid,SIGQUIT) != 0) {
5196
aed5971d73a8 A bug fix + moved error msg from verbose to error
albeu
parents: 4823
diff changeset
129 mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC can't kill subprocess %d : %s\n",
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
130 child_pid,strerror(errno));
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
131 return;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
132 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
133
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
134 if(waitpid(child_pid,NULL,0) < 0)
5196
aed5971d73a8 A bug fix + moved error msg from verbose to error
albeu
parents: 4823
diff changeset
135 mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC error while waiting subprocess %d : %s\n",
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
136 child_pid,strerror(errno));
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
137
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
138 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
139
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
140 #endif