Mercurial > mplayer.hg
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 |
rev | line source |
---|---|
4432 | 1 |
16860 | 2 #include "config.h" |
4432 | 3 |
4 #include <lirc/lirc_client.h> | |
5 #include <errno.h> | |
6 #include <stdio.h> | |
7 #include <string.h> | |
8 #include <unistd.h> | |
9 #include <sys/types.h> | |
7883 | 10 #include <sys/time.h> |
4432 | 11 #include <stdlib.h> |
12 | |
16860 | 13 #include "mp_msg.h" |
14 #include "help_mp.h" | |
7883 | 15 #include "input.h" |
4432 | 16 |
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 | 19 |
7883 | 20 static char* cmd_buf = NULL; |
4432 | 21 |
22 int | |
23 mp_input_lirc_init(void) { | |
24 int lirc_sock; | |
25 | |
20185 | 26 mp_msg(MSGT_LIRC,MSGL_V,MSGTR_SettingUpLIRC); |
4432 | 27 if((lirc_sock=lirc_init("mplayer",1))==-1){ |
20185 | 28 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed); |
4432 | 29 return -1; |
30 } | |
31 | |
32 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){ | |
20191 | 33 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr, |
4432 | 34 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile); |
35 lirc_deinit(); | |
36 return -1; | |
37 } | |
38 | |
7883 | 39 return lirc_sock; |
4432 | 40 } |
41 | |
7883 | 42 int mp_input_lirc_read(int fd,char* dest, int s) { |
43 fd_set fds; | |
44 struct timeval tv; | |
45 int r,cl = 0; | |
46 char *code = NULL,*c = NULL; | |
4432 | 47 |
7883 | 48 // We have something in the buffer return it |
49 if(cmd_buf != NULL) { | |
50 int l = strlen(cmd_buf), w = l > s ? s : l; | |
51 memcpy(dest,cmd_buf,w); | |
52 l -= w; | |
53 if(l > 0) | |
54 memmove(cmd_buf,&cmd_buf[w],l+1); | |
55 else { | |
56 free(cmd_buf); | |
57 cmd_buf = NULL; | |
58 } | |
59 return w; | |
60 } | |
61 | |
62 // Nothing in the buffer, pool the lirc fd | |
63 FD_ZERO(&fds); | |
64 FD_SET(fd,&fds); | |
65 memset(&tv,0,sizeof(tv)); | |
66 while((r = select(fd+1,&fds,NULL,NULL,&tv)) <= 0) { | |
67 if(r < 0) { | |
68 if(errno == EINTR) | |
69 continue; | |
70 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno)); | |
71 return MP_INPUT_ERROR; | |
72 } else | |
73 return MP_INPUT_NOTHING; | |
74 } | |
75 | |
76 // There's something to read | |
77 if(lirc_nextcode(&code) != 0) { | |
78 mp_msg(MSGT_INPUT,MSGL_ERR,"Lirc error :(\n"); | |
79 return MP_INPUT_DEAD; | |
80 } | |
4432 | 81 |
7883 | 82 if(!code) return MP_INPUT_NOTHING; |
83 | |
84 // We put all cmds in a single buffer separated by \n | |
85 while((r = lirc_code2char(lirc_config,code,&c))==0 && c!=NULL) { | |
86 int l = strlen(c); | |
87 if(l <= 0) | |
4432 | 88 continue; |
7883 | 89 cmd_buf = realloc(cmd_buf,cl+l+2); |
90 memcpy(&cmd_buf[cl],c,l); | |
91 cl += l+1; | |
92 cmd_buf[cl-1] = '\n'; | |
93 cmd_buf[cl] = '\0'; | |
4432 | 94 } |
7883 | 95 |
96 free(code); | |
97 | |
98 if(r < 0) | |
99 return MP_INPUT_DEAD; | |
100 else if(cmd_buf) // return the first command in the buffer | |
101 return mp_input_lirc_read(fd,dest,s); | |
102 else | |
15825 | 103 return MP_INPUT_RETRY; |
7883 | 104 |
4432 | 105 } |
106 | |
107 void | |
7883 | 108 mp_input_lirc_close(int fd) { |
109 if(cmd_buf) { | |
110 free(cmd_buf); | |
111 cmd_buf = NULL; | |
4432 | 112 } |
7883 | 113 lirc_freeconfig(lirc_config); |
114 lirc_deinit(); | |
4432 | 115 } |