annotate input/lirc.c @ 24992:5701e23ebcb4

Better handling of win32 GUI thread: 1. Use _beginthreadex to create the GUI thread to avoid possible memory leak when linked to MS CRT. 2. Terminate the GUI thread in an cleaner way using PostThreadMessage() rather than the unrecommended TerminateThread().
author zuxy
date Sun, 11 Nov 2007 08:14:57 +0000
parents 871ae3b173bd
children 142c53391eb7
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
16860
a68ede010f66 Unify include paths, -I.. is in CFLAGS.
diego
parents: 15825
diff changeset
2 #include "config.h"
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
3
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
4 #include <lirc/lirc_client.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
5 #include <errno.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
6 #include <stdio.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
7 #include <string.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
8 #include <unistd.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
9 #include <sys/types.h>
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
10 #include <sys/time.h>
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
11 #include <stdlib.h>
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
12
16860
a68ede010f66 Unify include paths, -I.. is in CFLAGS.
diego
parents: 15825
diff changeset
13 #include "mp_msg.h"
a68ede010f66 Unify include paths, -I.. is in CFLAGS.
diego
parents: 15825
diff changeset
14 #include "help_mp.h"
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
15 #include "input.h"
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
16
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
17 static struct lirc_config *lirc_config;
4823
d25b898c4c44 Make old and new lirc support independant from each other
albeu
parents: 4526
diff changeset
18 char *lirc_configfile;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
19
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
20 static char* cmd_buf = NULL;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
21
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
22 int
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
23 mp_input_lirc_init(void) {
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
24 int lirc_sock;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
25
20185
b6eed21e0535 slight overall verbosity reduction
diego
parents: 19801
diff changeset
26 mp_msg(MSGT_LIRC,MSGL_V,MSGTR_SettingUpLIRC);
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
27 if((lirc_sock=lirc_init("mplayer",1))==-1){
20185
b6eed21e0535 slight overall verbosity reduction
diego
parents: 19801
diff changeset
28 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed);
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
29 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
30 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
31
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
32 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){
20191
871ae3b173bd litle bug, didn't compile :(
ptt
parents: 20185
diff changeset
33 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr,
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
34 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile);
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
35 lirc_deinit();
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
36 return -1;
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
37 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
38
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
39 return lirc_sock;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
40 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
41
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
42 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
43 fd_set fds;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
44 struct timeval tv;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
45 int r,cl = 0;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
46 char *code = NULL,*c = NULL;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
47
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
48 // We have something in the buffer return it
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
49 if(cmd_buf != NULL) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
50 int l = strlen(cmd_buf), w = l > s ? s : l;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
51 memcpy(dest,cmd_buf,w);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
52 l -= w;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
53 if(l > 0)
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
54 memmove(cmd_buf,&cmd_buf[w],l+1);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
55 else {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
56 free(cmd_buf);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
57 cmd_buf = NULL;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
58 }
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
59 return w;
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
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
62 // Nothing in the buffer, pool the lirc fd
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
63 FD_ZERO(&fds);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
64 FD_SET(fd,&fds);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
65 memset(&tv,0,sizeof(tv));
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
66 while((r = select(fd+1,&fds,NULL,NULL,&tv)) <= 0) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
67 if(r < 0) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
68 if(errno == EINTR)
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
69 continue;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
70 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
71 return MP_INPUT_ERROR;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
72 } else
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
73 return MP_INPUT_NOTHING;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
74 }
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
75
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
76 // There's something to read
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
77 if(lirc_nextcode(&code) != 0) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
78 mp_msg(MSGT_INPUT,MSGL_ERR,"Lirc error :(\n");
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
79 return MP_INPUT_DEAD;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
80 }
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
81
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
82 if(!code) return MP_INPUT_NOTHING;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
83
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
84 // 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
85 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
86 int l = strlen(c);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
87 if(l <= 0)
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
88 continue;
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
89 cmd_buf = realloc(cmd_buf,cl+l+2);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
90 memcpy(&cmd_buf[cl],c,l);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
91 cl += l+1;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
92 cmd_buf[cl-1] = '\n';
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
93 cmd_buf[cl] = '\0';
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
94 }
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
95
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
96 free(code);
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 if(r < 0)
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
99 return MP_INPUT_DEAD;
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
100 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
101 return mp_input_lirc_read(fd,dest,s);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
102 else
15825
8549fc0fb5b1 consume empty lirc events at once.
reimar
parents: 7883
diff changeset
103 return MP_INPUT_RETRY;
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
104
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
105 }
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
106
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
107 void
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
108 mp_input_lirc_close(int fd) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
109 if(cmd_buf) {
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
110 free(cmd_buf);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
111 cmd_buf = NULL;
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
112 }
7883
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
113 lirc_freeconfig(lirc_config);
d375383e1a48 rewrote the lirc code to remove the fork
arpi
parents: 5196
diff changeset
114 lirc_deinit();
4432
5105f5da01d6 Added lirc support in input
albeu
parents:
diff changeset
115 }