annotate input/lirc.c @ 10716:aaaf37fb73ff

sync
author nauj27
date Fri, 29 Aug 2003 16:19:23 +0000
parents d375383e1a48
children 8549fc0fb5b1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
1
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
2 #include "../config.h"
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
3
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
4 #ifdef HAVE_LIRC
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
5
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
6 #include <lirc/lirc_client.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
7 #include <errno.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
8 #include <stdio.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
9 #include <string.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
10 #include <unistd.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
11 #include <sys/types.h>
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
12 #include <sys/time.h>
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
13 #include <stdlib.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
14
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
15 #include "../mp_msg.h"
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
16 #include "../help_mp.h"
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
17 #include "input.h"
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
18
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
19 static struct lirc_config *lirc_config;
4823
d25b898c4c44 Make old and new lirc support independant from each other
albeu
parents: 4526
diff changeset
20 char *lirc_configfile;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
21
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
22 static char* cmd_buf = NULL;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
23
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
24 int
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
25 mp_input_lirc_init(void) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
26 int lirc_sock;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
27
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
28 mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
29 if((lirc_sock=lirc_init("mplayer",1))==-1){
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
30 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed MSGTR_LIRCdisabled);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
31 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
32 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
33
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
34 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
35 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr MSGTR_LIRCdisabled,
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
36 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
37 lirc_deinit();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
38 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
39 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
40
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
41 return lirc_sock;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
42 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
43
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
44 int mp_input_lirc_read(int fd,char* dest, int s) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
45 fd_set fds;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
46 struct timeval tv;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
47 int r,cl = 0;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
48 char *code = NULL,*c = NULL;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
49
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
50 // We have something in the buffer return it
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
51 if(cmd_buf != NULL) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
52 int l = strlen(cmd_buf), w = l > s ? s : l;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
53 memcpy(dest,cmd_buf,w);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
54 l -= w;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
55 if(l > 0)
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
56 memmove(cmd_buf,&cmd_buf[w],l+1);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
57 else {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
58 free(cmd_buf);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
59 cmd_buf = NULL;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
60 }
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
61 return w;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
62 }
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
63
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
64 // Nothing in the buffer, pool the lirc fd
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
65 FD_ZERO(&fds);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
66 FD_SET(fd,&fds);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
67 memset(&tv,0,sizeof(tv));
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
68 while((r = select(fd+1,&fds,NULL,NULL,&tv)) <= 0) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
69 if(r < 0) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
70 if(errno == EINTR)
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
71 continue;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
72 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno));
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
73 return MP_INPUT_ERROR;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
74 } else
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
75 return MP_INPUT_NOTHING;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
76 }
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
77
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
78 // There's something to read
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
79 if(lirc_nextcode(&code) != 0) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
80 mp_msg(MSGT_INPUT,MSGL_ERR,"Lirc error :(\n");
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
81 return MP_INPUT_DEAD;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
82 }
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
83
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
84 if(!code) return MP_INPUT_NOTHING;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
85
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
86 // We put all cmds in a single buffer separated by \n
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
87 while((r = lirc_code2char(lirc_config,code,&c))==0 && c!=NULL) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
88 int l = strlen(c);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
89 if(l <= 0)
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
90 continue;
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
91 cmd_buf = realloc(cmd_buf,cl+l+2);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
92 memcpy(&cmd_buf[cl],c,l);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
93 cl += l+1;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
94 cmd_buf[cl-1] = '\n';
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
95 cmd_buf[cl] = '\0';
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
96 }
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
97
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
98 free(code);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
99
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
100 if(r < 0)
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
101 return MP_INPUT_DEAD;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
102 else if(cmd_buf) // return the first command in the buffer
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
103 return mp_input_lirc_read(fd,dest,s);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
104 else
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
105 return MP_INPUT_NOTHING;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
106
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
107 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
108
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
109 void
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
110 mp_input_lirc_close(int fd) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
111 if(cmd_buf) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
112 free(cmd_buf);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
113 cmd_buf = NULL;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
114 }
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
115 lirc_freeconfig(lirc_config);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
116 lirc_deinit();
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
117 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
118
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
119 #endif