0
0
mirror of https://github.com/mpv-player/mpv.git synced 2024-09-20 12:02:23 +02:00

rewrote the lirc code to remove the fork

patch by Albeu


git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@7884 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
arpi 2002-10-23 22:23:12 +00:00
parent 62e8e62143
commit 288e2a0f4e
3 changed files with 75 additions and 99 deletions

View File

@ -1294,7 +1294,7 @@ mp_input_init(void) {
if(use_lirc) { if(use_lirc) {
int fd = mp_input_lirc_init(); int fd = mp_input_lirc_init();
if(fd > 0) if(fd > 0)
mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)close); mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
} }
#endif #endif
@ -1327,12 +1327,6 @@ mp_input_uninit(void) {
cmd_fds[i].close_func(cmd_fds[i].fd); cmd_fds[i].close_func(cmd_fds[i].fd);
} }
#ifdef HAVE_LIRC
if(use_lirc)
mp_input_lirc_uninit();
#endif
} }
void void

View File

@ -6,35 +6,24 @@
#include <lirc/lirc_client.h> #include <lirc/lirc_client.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <sys/ioctl.h>
#include <string.h> #include <string.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/time.h>
#include <stdlib.h> #include <stdlib.h>
#include "../mp_msg.h" #include "../mp_msg.h"
#include "../help_mp.h" #include "../help_mp.h"
#include "input.h"
static struct lirc_config *lirc_config; static struct lirc_config *lirc_config;
char *lirc_configfile; char *lirc_configfile;
static int child_pid=0; static char* cmd_buf = NULL;
static void
mp_input_lirc_process_quit(int sig);
static void
mp_input_lirc_process(int mp_fd);
int int
mp_input_lirc_init(void) { mp_input_lirc_init(void) {
int lirc_sock; int lirc_sock;
int p[2];
mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC); mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC);
if((lirc_sock=lirc_init("mplayer",1))==-1){ if((lirc_sock=lirc_init("mplayer",1))==-1){
@ -49,92 +38,82 @@ mp_input_lirc_init(void) {
return -1; return -1;
} }
if(pipe(p) != 0) { return lirc_sock;
mp_msg(MSGT_LIRC,MSGL_ERR,"Can't create lirc pipe : %s\n",strerror(errno));
lirc_deinit();
}
child_pid = fork();
if(child_pid < 0) {
mp_msg(MSGT_LIRC,MSGL_ERR,"Can't fork lirc subprocess : %s\n",strerror(errno));
lirc_deinit();
close(p[0]);
close(p[1]);
return -1;
} else if(child_pid == 0) {// setup child process
close(p[0]);
// put some signal handlers
signal(SIGINT,mp_input_lirc_process_quit);
signal(SIGHUP,mp_input_lirc_process_quit);
signal(SIGQUIT,mp_input_lirc_process_quit);
// start the process
mp_input_lirc_process(p[1]);
}
// free unuseful ressources in parent process
lirc_freeconfig(lirc_config);
close(p[1]);
mp_msg(MSGT_LIRC,MSGL_V,"NEW LIRC init was successful.\n");
return p[0];
} }
static void int mp_input_lirc_read(int fd,char* dest, int s) {
mp_input_lirc_process_quit(int sig) { fd_set fds;
lirc_freeconfig(lirc_config); struct timeval tv;
lirc_deinit(); int r,cl = 0;
exit(sig > 0 ? 0 : -1); char *code = NULL,*c = NULL;
}
static void // We have something in the buffer return it
mp_input_lirc_process(int mp_fd) { if(cmd_buf != NULL) {
char *cmd,*code; int l = strlen(cmd_buf), w = l > s ? s : l;
int ret; memcpy(dest,cmd_buf,w);
l -= w;
while(lirc_nextcode(&code)==0) { if(l > 0)
if(code==NULL) memmove(cmd_buf,&cmd_buf[w],l+1);
continue; else {
while((ret=lirc_code2char(lirc_config,code,&cmd))==0 && cmd!=NULL) { free(cmd_buf);
int len = strlen(cmd)+1; cmd_buf = NULL;
char buf[len];
int w=0;
strcpy(buf,cmd);
buf[len-1] = '\n';
while(w < len) {
int r = write(mp_fd,buf+w,len-w);
if(r < 0) {
if(errno == EINTR)
continue;
mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC subprocess can't write in input pipe : %s\n",
strerror(errno));
mp_input_lirc_process_quit(-1);
}
w += r;
}
} }
free(code); return w;
if(ret==-1)
break;
} }
mp_input_lirc_process_quit(-1);
// Nothing in the buffer, pool the lirc fd
FD_ZERO(&fds);
FD_SET(fd,&fds);
memset(&tv,0,sizeof(tv));
while((r = select(fd+1,&fds,NULL,NULL,&tv)) <= 0) {
if(r < 0) {
if(errno == EINTR)
continue;
mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno));
return MP_INPUT_ERROR;
} else
return MP_INPUT_NOTHING;
}
// There's something to read
if(lirc_nextcode(&code) != 0) {
mp_msg(MSGT_INPUT,MSGL_ERR,"Lirc error :(\n");
return MP_INPUT_DEAD;
}
if(!code) return MP_INPUT_NOTHING;
// We put all cmds in a single buffer separated by \n
while((r = lirc_code2char(lirc_config,code,&c))==0 && c!=NULL) {
int l = strlen(c);
if(l <= 0)
continue;
cmd_buf = realloc(cmd_buf,cl+l+2);
memcpy(&cmd_buf[cl],c,l);
cl += l+1;
cmd_buf[cl-1] = '\n';
cmd_buf[cl] = '\0';
}
free(code);
if(r < 0)
return MP_INPUT_DEAD;
else if(cmd_buf) // return the first command in the buffer
return mp_input_lirc_read(fd,dest,s);
else
return MP_INPUT_NOTHING;
} }
void void
mp_input_lirc_uninit(void) { mp_input_lirc_close(int fd) {
if(child_pid <= 0) if(cmd_buf) {
return; free(cmd_buf);
if( kill(child_pid,SIGQUIT) != 0) { cmd_buf = NULL;
mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC can't kill subprocess %d : %s\n",
child_pid,strerror(errno));
return;
} }
lirc_freeconfig(lirc_config);
if(waitpid(child_pid,NULL,0) < 0) lirc_deinit();
mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC error while waiting subprocess %d : %s\n",
child_pid,strerror(errno));
} }
#endif #endif

View File

@ -3,5 +3,8 @@
int int
mp_input_lirc_init(void); mp_input_lirc_init(void);
int
mp_input_lirc_read(int fd,char* dest, int s);
void void
mp_input_lirc_uninit(void); mp_input_lirc_close(int fd);