2008-12-13 18:28:00 +00: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 09:39:11 +00:00
|
|
|
|
2005-10-25 21:23:45 +00:00
|
|
|
#include "config.h"
|
2002-01-31 09:39:11 +00:00
|
|
|
|
|
|
|
#include <lirc/lirc_client.h>
|
|
|
|
#include <errno.h>
|
2009-01-27 19:06:50 +00:00
|
|
|
#include <fcntl.h>
|
2002-01-31 09:39:11 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2002-10-23 22:23:12 +00:00
|
|
|
#include "input.h"
|
2010-02-27 20:32:55 +00:00
|
|
|
#include "lirc.h"
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
static int mp_input_lirc_read(void *ctx, int fd,char* dest, int s);
|
|
|
|
static int mp_input_lirc_close(void *ctx, int fd);
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
struct ctx {
|
|
|
|
struct mp_log *log;
|
|
|
|
struct lirc_config *lirc_config;
|
|
|
|
char* cmd_buf;
|
|
|
|
};
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
int mp_input_lirc_init(struct input_ctx *ictx, struct mp_log *log,
|
|
|
|
char *lirc_configfile)
|
|
|
|
{
|
2002-01-31 09:39:11 +00:00
|
|
|
int lirc_sock;
|
2009-01-27 19:06:50 +00:00
|
|
|
int mode;
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
mp_verbose(log,"Setting up LIRC support...\n");
|
2012-10-11 00:04:08 +00:00
|
|
|
if((lirc_sock=lirc_init("mpv",0))==-1){
|
2013-12-21 18:33:45 +00:00
|
|
|
mp_verbose(log,"Failed to open LIRC support. You will not be able to use your remote control.\n");
|
2002-01-31 09:39:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-01-27 19:06:50 +00:00
|
|
|
mode = fcntl(lirc_sock, F_GETFL);
|
|
|
|
if (mode < 0 || fcntl(lirc_sock, F_SETFL, mode | O_NONBLOCK) < 0) {
|
2013-12-21 18:33:45 +00:00
|
|
|
mp_err(log, "setting non-blocking mode failed: %s\n", strerror(errno));
|
2009-01-27 19:06:50 +00:00
|
|
|
lirc_deinit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
struct lirc_config *lirc_config = NULL;
|
2009-01-30 15:41:45 +00:00
|
|
|
if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){
|
2013-12-21 18:33:45 +00:00
|
|
|
mp_err(log, "Failed to read LIRC config file %s.\n",
|
2014-04-13 16:00:51 +00:00
|
|
|
lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile);
|
2009-01-30 15:41:45 +00:00
|
|
|
lirc_deinit();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
struct ctx *ctx = talloc_ptrtype(NULL, ctx);
|
|
|
|
*ctx = (struct ctx){
|
|
|
|
.log = log,
|
|
|
|
.lirc_config = lirc_config,
|
|
|
|
};
|
|
|
|
mp_input_add_fd(ictx, lirc_sock, 0, mp_input_lirc_read, NULL, mp_input_lirc_close, ctx);
|
|
|
|
|
2002-10-23 22:23:12 +00:00
|
|
|
return lirc_sock;
|
|
|
|
}
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
static int mp_input_lirc_read(void *pctx,int fd,char* dest, int s) {
|
2002-10-23 22:23:12 +00:00
|
|
|
int r,cl = 0;
|
|
|
|
char *code = NULL,*c = NULL;
|
2013-12-21 18:33:45 +00:00
|
|
|
struct ctx *ctx = pctx;
|
2002-10-23 22:23:12 +00:00
|
|
|
|
|
|
|
// We have something in the buffer return it
|
2013-12-21 18:33:45 +00:00
|
|
|
if(ctx->cmd_buf != NULL) {
|
|
|
|
int l = strlen(ctx->cmd_buf), w = l > s ? s : l;
|
|
|
|
memcpy(dest,ctx->cmd_buf,w);
|
2002-10-23 22:23:12 +00:00
|
|
|
l -= w;
|
|
|
|
if(l > 0)
|
2013-12-21 18:33:45 +00:00
|
|
|
memmove(ctx->cmd_buf,&ctx->cmd_buf[w],l+1);
|
2002-10-23 22:23:12 +00:00
|
|
|
else {
|
2013-12-21 18:33:45 +00:00
|
|
|
free(ctx->cmd_buf);
|
|
|
|
ctx->cmd_buf = NULL;
|
2002-10-23 22:23:12 +00:00
|
|
|
}
|
|
|
|
return w;
|
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-01-25 18:46:24 +00:00
|
|
|
// Nothing in the buffer, poll the lirc fd
|
2002-10-23 22:23:12 +00:00
|
|
|
if(lirc_nextcode(&code) != 0) {
|
2013-12-21 18:33:45 +00:00
|
|
|
MP_ERR(ctx, "Lirc error.\n");
|
2002-10-23 22:23:12 +00:00
|
|
|
return MP_INPUT_DEAD;
|
2002-01-31 09:39:11 +00:00
|
|
|
}
|
|
|
|
|
2002-10-23 22:23:12 +00:00
|
|
|
if(!code) return MP_INPUT_NOTHING;
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2002-10-23 22:23:12 +00:00
|
|
|
// We put all cmds in a single buffer separated by \n
|
2013-12-21 18:33:45 +00:00
|
|
|
while((r = lirc_code2char(ctx->lirc_config,code,&c))==0 && c!=NULL) {
|
2002-10-23 22:23:12 +00:00
|
|
|
int l = strlen(c);
|
|
|
|
if(l <= 0)
|
|
|
|
continue;
|
2013-12-21 18:33:45 +00:00
|
|
|
ctx->cmd_buf = realloc(ctx->cmd_buf,cl+l+2);
|
|
|
|
memcpy(&ctx->cmd_buf[cl],c,l);
|
2002-10-23 22:23:12 +00:00
|
|
|
cl += l+1;
|
2013-12-21 18:33:45 +00:00
|
|
|
ctx->cmd_buf[cl-1] = '\n';
|
|
|
|
ctx->cmd_buf[cl] = '\0';
|
2002-10-23 22:23:12 +00:00
|
|
|
}
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2002-10-23 22:23:12 +00:00
|
|
|
free(code);
|
2002-01-31 09:39:11 +00:00
|
|
|
|
2002-10-23 22:23:12 +00:00
|
|
|
if(r < 0)
|
|
|
|
return MP_INPUT_DEAD;
|
2013-12-21 18:33:45 +00:00
|
|
|
else if(ctx->cmd_buf) // return the first command in the buffer
|
|
|
|
return mp_input_lirc_read(ctx,fd,dest,s);
|
2002-10-23 22:23:12 +00:00
|
|
|
else
|
2005-06-27 08:16:23 +00:00
|
|
|
return MP_INPUT_RETRY;
|
2002-01-31 09:39:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:33:45 +00:00
|
|
|
static int mp_input_lirc_close(void *pctx,int fd)
|
2011-07-16 15:17:48 +00:00
|
|
|
{
|
2013-12-21 18:33:45 +00:00
|
|
|
struct ctx *ctx = pctx;
|
|
|
|
free(ctx->cmd_buf);
|
|
|
|
lirc_freeconfig(ctx->lirc_config);
|
2002-10-23 22:23:12 +00:00
|
|
|
lirc_deinit();
|
2013-12-21 18:33:45 +00:00
|
|
|
talloc_free(ctx);
|
2011-09-04 05:04:31 +00:00
|
|
|
return 0;
|
2002-01-31 09:39:11 +00:00
|
|
|
}
|