2008-12-13 19:28:00 +01:00
|
|
|
/*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MPlayer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2005-10-25 23:23:45 +02:00
|
|
|
#include "config.h"
|
2002-01-31 10:39:11 +01:00
|
|
|
|
|
|
|
#include <lirc/lirc_client.h>
|
|
|
|
#include <errno.h>
|
2009-01-27 20:06:50 +01:00
|
|
|
#include <fcntl.h>
|
2002-01-31 10:39:11 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-08-06 22:41:30 +02:00
|
|
|
#include "mpvcore/mp_msg.h"
|
2002-10-24 00:23:12 +02:00
|
|
|
#include "input.h"
|
2010-02-27 21:32:55 +01:00
|
|
|
#include "lirc.h"
|
2002-01-31 10:39:11 +01:00
|
|
|
|
|
|
|
static struct lirc_config *lirc_config;
|
2002-02-23 22:18:40 +01:00
|
|
|
char *lirc_configfile;
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
static char* cmd_buf = NULL;
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2009-07-07 01:26:13 +02:00
|
|
|
int
|
2002-01-31 10:39:11 +01:00
|
|
|
mp_input_lirc_init(void) {
|
|
|
|
int lirc_sock;
|
2009-01-27 20:06:50 +01:00
|
|
|
int mode;
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2009-07-07 00:15:02 +02:00
|
|
|
mp_tmsg(MSGT_LIRC,MSGL_V,"Setting up LIRC support...\n");
|
2012-10-11 02:04:08 +02:00
|
|
|
if((lirc_sock=lirc_init("mpv",0))==-1){
|
2012-07-29 21:53:23 +02:00
|
|
|
mp_tmsg(MSGT_LIRC,MSGL_V,"Failed to open LIRC support. You will not be able to use your remote control.\n");
|
2002-01-31 10:39:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-27 20:06:50 +01:00
|
|
|
mode = fcntl(lirc_sock, F_GETFL);
|
|
|
|
if (mode < 0 || fcntl(lirc_sock, F_SETFL, mode | O_NONBLOCK) < 0) {
|
|
|
|
mp_msg(MSGT_LIRC, MSGL_ERR, "setting non-blocking mode failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
lirc_deinit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-30 16:41:45 +01:00
|
|
|
if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){
|
2009-07-07 00:15:02 +02:00
|
|
|
mp_tmsg(MSGT_LIRC,MSGL_ERR,"Failed to read LIRC config file %s.\n",
|
2009-01-30 16:41:45 +01:00
|
|
|
lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile);
|
|
|
|
lirc_deinit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
return lirc_sock;
|
|
|
|
}
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
int mp_input_lirc_read(int fd,char* dest, int s) {
|
|
|
|
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;
|
|
|
|
}
|
2009-07-07 01:26:13 +02:00
|
|
|
|
2009-01-25 19:46:24 +01:00
|
|
|
// Nothing in the buffer, poll the lirc fd
|
2002-10-24 00:23:12 +02:00
|
|
|
if(lirc_nextcode(&code) != 0) {
|
2009-01-30 16:38:54 +01:00
|
|
|
mp_msg(MSGT_LIRC,MSGL_ERR,"Lirc error :(\n");
|
2002-10-24 00:23:12 +02:00
|
|
|
return MP_INPUT_DEAD;
|
2002-01-31 10:39:11 +01:00
|
|
|
}
|
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
if(!code) return MP_INPUT_NOTHING;
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
// 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';
|
|
|
|
}
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
free(code);
|
2002-01-31 10:39:11 +01:00
|
|
|
|
2002-10-24 00:23:12 +02:00
|
|
|
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
|
2005-06-27 10:16:23 +02:00
|
|
|
return MP_INPUT_RETRY;
|
2002-01-31 10:39:11 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-07-16 17:17:48 +02:00
|
|
|
int mp_input_lirc_close(int fd)
|
|
|
|
{
|
2010-11-14 10:12:34 +01:00
|
|
|
free(cmd_buf);
|
|
|
|
cmd_buf = NULL;
|
2002-10-24 00:23:12 +02:00
|
|
|
lirc_freeconfig(lirc_config);
|
|
|
|
lirc_deinit();
|
2011-09-04 07:04:31 +02:00
|
|
|
return 0;
|
2002-01-31 10:39:11 +01:00
|
|
|
}
|