summaryrefslogtreecommitdiffstats
path: root/input
diff options
context:
space:
mode:
authorarpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2>2002-10-23 22:23:12 +0000
committerarpi <arpi@b3059339-0415-0410-9bf9-f77b7e298cf2>2002-10-23 22:23:12 +0000
commit288e2a0f4e0a02c762c65905d13ad704baece990 (patch)
treee35ca07c34471ccf4af10f95bac440134c9fdd7a /input
parent62e8e621431fe0b4ff9e15601b5110cbd861234a (diff)
downloadmpv-288e2a0f4e0a02c762c65905d13ad704baece990.tar.bz2
mpv-288e2a0f4e0a02c762c65905d13ad704baece990.tar.xz
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
Diffstat (limited to 'input')
-rw-r--r--input/input.c8
-rw-r--r--input/lirc.c157
-rw-r--r--input/lirc.h5
3 files changed, 73 insertions, 97 deletions
diff --git a/input/input.c b/input/input.c
index e12316c04d..50d9a04956 100644
--- a/input/input.c
+++ b/input/input.c
@@ -1294,7 +1294,7 @@ mp_input_init(void) {
if(use_lirc) {
int fd = mp_input_lirc_init();
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
@@ -1327,12 +1327,6 @@ mp_input_uninit(void) {
cmd_fds[i].close_func(cmd_fds[i].fd);
}
-
-#ifdef HAVE_LIRC
- if(use_lirc)
- mp_input_lirc_uninit();
-#endif
-
}
void
diff --git a/input/lirc.c b/input/lirc.c
index 1b1bd66b1f..03730e25aa 100644
--- a/input/lirc.c
+++ b/input/lirc.c
@@ -6,35 +6,24 @@
#include <lirc/lirc_client.h>
#include <errno.h>
#include <stdio.h>
-#include <sys/ioctl.h>
#include <string.h>
-#include <fcntl.h>
#include <unistd.h>
-#include <signal.h>
#include <sys/types.h>
-#include <sys/wait.h>
+#include <sys/time.h>
#include <stdlib.h>
-
#include "../mp_msg.h"
#include "../help_mp.h"
+#include "input.h"
static struct lirc_config *lirc_config;
char *lirc_configfile;
-static int child_pid=0;
-
-static void
-mp_input_lirc_process_quit(int sig);
-
-static void
-mp_input_lirc_process(int mp_fd);
-
+static char* cmd_buf = NULL;
int
mp_input_lirc_init(void) {
int lirc_sock;
- int p[2];
mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC);
if((lirc_sock=lirc_init("mplayer",1))==-1){
@@ -49,92 +38,82 @@ mp_input_lirc_init(void) {
return -1;
}
- if(pipe(p) != 0) {
- mp_msg(MSGT_LIRC,MSGL_ERR,"Can't create lirc pipe : %s\n",strerror(errno));
- lirc_deinit();
- }
-
- child_pid = fork();
+ return lirc_sock;
+}
- 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]);
+int mp_input_lirc_read(int fd,char* dest, int s) {
+ fd_set fds;
+ struct timeval tv;
+ int r,cl = 0;
+ char *code = NULL,*c = NULL;
+
+ // We have something in the buffer return it
+ if(cmd_buf != NULL) {
+ int l = strlen(cmd_buf), w = l > s ? s : l;
+ memcpy(dest,cmd_buf,w);
+ l -= w;
+ if(l > 0)
+ memmove(cmd_buf,&cmd_buf[w],l+1);
+ else {
+ free(cmd_buf);
+ cmd_buf = NULL;
+ }
+ return w;
+ }
+
+ // 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;
}
- // 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");
+ if(!code) return MP_INPUT_NOTHING;
- return p[0];
-}
+ // 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';
+ }
-static void
-mp_input_lirc_process_quit(int sig) {
- lirc_freeconfig(lirc_config);
- lirc_deinit();
- exit(sig > 0 ? 0 : -1);
-}
+ free(code);
-static void
-mp_input_lirc_process(int mp_fd) {
- char *cmd,*code;
- int ret;
+ 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;
- while(lirc_nextcode(&code)==0) {
- if(code==NULL)
- continue;
- while((ret=lirc_code2char(lirc_config,code,&cmd))==0 && cmd!=NULL) {
- int len = strlen(cmd)+1;
- 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);
- if(ret==-1)
- break;
- }
- mp_input_lirc_process_quit(-1);
}
void
-mp_input_lirc_uninit(void) {
- if(child_pid <= 0)
- return;
- if( kill(child_pid,SIGQUIT) != 0) {
- mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC can't kill subprocess %d : %s\n",
- child_pid,strerror(errno));
- return;
+mp_input_lirc_close(int fd) {
+ if(cmd_buf) {
+ free(cmd_buf);
+ cmd_buf = NULL;
}
-
- if(waitpid(child_pid,NULL,0) < 0)
- mp_msg(MSGT_LIRC,MSGL_ERR,"LIRC error while waiting subprocess %d : %s\n",
- child_pid,strerror(errno));
-
+ lirc_freeconfig(lirc_config);
+ lirc_deinit();
}
#endif
diff --git a/input/lirc.h b/input/lirc.h
index 8461d0473b..6271aaaeb2 100644
--- a/input/lirc.h
+++ b/input/lirc.h
@@ -3,5 +3,8 @@
int
mp_input_lirc_init(void);
+int
+mp_input_lirc_read(int fd,char* dest, int s);
+
void
-mp_input_lirc_uninit(void);
+mp_input_lirc_close(int fd);