Mercurial > mplayer.hg
annotate input/lirc.c @ 7760:5262223af043
memleak fix
author | arpi |
---|---|
date | Wed, 16 Oct 2002 20:17:20 +0000 |
parents | aed5971d73a8 |
children | d375383e1a48 |
rev | line source |
---|---|
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; | |
4823
d25b898c4c44
Make old and new lirc support independant from each other
albeu
parents:
4526
diff
changeset
|
23 char *lirc_configfile; |
4432 | 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_sock; | |
37 int p[2]; | |
38 | |
39 mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC); | |
40 if((lirc_sock=lirc_init("mplayer",1))==-1){ | |
41 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed MSGTR_LIRCdisabled); | |
42 return -1; | |
43 } | |
44 | |
45 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){ | |
46 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr MSGTR_LIRCdisabled, | |
47 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile); | |
48 lirc_deinit(); | |
49 return -1; | |
50 } | |
51 | |
52 if(pipe(p) != 0) { | |
53 mp_msg(MSGT_LIRC,MSGL_ERR,"Can't create lirc pipe : %s\n",strerror(errno)); | |
54 lirc_deinit(); | |
55 } | |
56 | |
57 child_pid = fork(); | |
58 | |
59 if(child_pid < 0) { | |
60 mp_msg(MSGT_LIRC,MSGL_ERR,"Can't fork lirc subprocess : %s\n",strerror(errno)); | |
61 lirc_deinit(); | |
62 close(p[0]); | |
63 close(p[1]); | |
64 return -1; | |
65 } else if(child_pid == 0) {// setup child process | |
66 close(p[0]); | |
67 // put some signal handlers | |
68 signal(SIGINT,mp_input_lirc_process_quit); | |
69 signal(SIGHUP,mp_input_lirc_process_quit); | |
70 signal(SIGQUIT,mp_input_lirc_process_quit); | |
71 // start the process | |
72 mp_input_lirc_process(p[1]); | |
73 } | |
74 | |
75 // free unuseful ressources in parent process | |
76 lirc_freeconfig(lirc_config); | |
77 close(p[1]); | |
78 | |
79 mp_msg(MSGT_LIRC,MSGL_V,"NEW LIRC init was successful.\n"); | |
80 | |
81 return p[0]; | |
82 } | |
83 | |
84 static void | |
85 mp_input_lirc_process_quit(int sig) { | |
86 lirc_freeconfig(lirc_config); | |
87 lirc_deinit(); | |
88 exit(sig > 0 ? 0 : -1); | |
89 } | |
90 | |
91 static void | |
92 mp_input_lirc_process(int mp_fd) { | |
93 char *cmd,*code; | |
94 int ret; | |
95 | |
96 while(lirc_nextcode(&code)==0) { | |
97 if(code==NULL) | |
98 continue; | |
99 while((ret=lirc_code2char(lirc_config,code,&cmd))==0 && cmd!=NULL) { | |
100 int len = strlen(cmd)+1; | |
101 char buf[len]; | |
102 int w=0; | |
103 strcpy(buf,cmd); | |
104 buf[len-1] = '\n'; | |
105 while(w < len) { | |
5196 | 106 int r = write(mp_fd,buf+w,len-w); |
4432 | 107 if(r < 0) { |
108 if(errno == EINTR) | |
109 continue; | |
5196 | 110 mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC subprocess can't write in input pipe : %s\n", |
4432 | 111 strerror(errno)); |
112 mp_input_lirc_process_quit(-1); | |
113 } | |
114 w += r; | |
115 } | |
116 } | |
117 free(code); | |
118 if(ret==-1) | |
119 break; | |
120 } | |
121 mp_input_lirc_process_quit(-1); | |
122 } | |
123 | |
124 void | |
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 | 128 if( kill(child_pid,SIGQUIT) != 0) { |
5196 | 129 mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC can't kill subprocess %d : %s\n", |
4432 | 130 child_pid,strerror(errno)); |
131 return; | |
132 } | |
133 | |
134 if(waitpid(child_pid,NULL,0) < 0) | |
5196 | 135 mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC error while waiting subprocess %d : %s\n", |
4432 | 136 child_pid,strerror(errno)); |
137 | |
138 } | |
139 | |
140 #endif |