4 #include "mwindowgui.h"
5 #include "androidcontrol.h"
6 #include "preferences.h"
9 int AndroidControl::open(unsigned short port)
11 sockfd = socket(PF_INET,SOCK_DGRAM,0);
12 if( sockfd < 0 ) { perror("socket:"); return -1; }
18 if( setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &lgr, sizeof(lgr)) < 0 ) {
23 struct sockaddr_in sadr;
24 memset(&sadr,0,sizeof(sadr));
25 sadr.sin_family = AF_INET;
26 sadr.sin_port = htons(port);
27 sadr.sin_addr.s_addr = 0;
29 if( bind(sockfd,(struct sockaddr *)&sadr,sizeof(sadr)) < 0 ) {
37 void AndroidControl::close()
45 AndroidControl::AndroidControl(MWindowGUI *mwindow_gui)
48 this->mwindow_gui = mwindow_gui;
52 bool AndroidControl::is_msg(const char *cp)
54 if( msg_len != (int)strlen(cp) ) return false;
55 if( strncmp(cp, msg, msg_len) ) return false;
59 void AndroidControl::press(int key)
61 // printf("press 0x%04x\n",key);
62 if( key == KPMENU && mwindow_gui->keyev_grab_remote() )
63 printf("android grab remote\n");
64 if( mwindow_gui->key_listener(key) ) return;
65 mwindow_gui->remote_control->remote_key(key);
68 void AndroidControl::run()
70 unsigned short port = mwindow_gui->mwindow->preferences->android_port;
71 done = open(port) >= 0 ? 0 : 1;
77 int ret = recvfrom(sockfd, buf, sizeof(buf), 0, &sadr, &sadr_len);
79 perror("AndroidControl::run: recvfrom");
84 msg = buf; msg_len = ret;
85 // cinelerra android remote magic number
86 if( msg[0] != 'C' || msg[1] != 'A' || msg[2] != 'R' ) continue;
88 if( msg[3] != 1 ) continue;
89 msg += 4; msg_len -= 4;
91 const char *pin = mwindow_gui->mwindow->preferences->android_pin;
92 int len = sizeof(mwindow_gui->mwindow->preferences->android_pin);
93 while( len > 0 && msg_len > 0 && *pin != 0 && *msg != 0 ) {
94 if( *pin != *msg ) break;
98 if( !len || !msg_len || *pin != *msg ) continue;
100 if( msg_len <= 0 ) continue;
101 if( is_msg("menu") ) press(KPMENU);
102 else if( is_msg("key 0") ) press('0');
103 else if( is_msg("key 1") ) press('1');
104 else if( is_msg("key 2") ) press('2');
105 else if( is_msg("key 3") ) press('3');
106 else if( is_msg("key 4") ) press('4');
107 else if( is_msg("key 5") ) press('5');
108 else if( is_msg("key 6") ) press('6');
109 else if( is_msg("key 7") ) press('7');
110 else if( is_msg("key 8") ) press('8');
111 else if( is_msg("key 9") ) press('9');
112 else if( is_msg("key A") ) press('a');
113 else if( is_msg("key B") ) press('b');
114 else if( is_msg("key C") ) press('c');
115 else if( is_msg("key D") ) press('d');
116 else if( is_msg("key E") ) press('e');
117 else if( is_msg("book") ) press(KPBOOK);
118 else if( is_msg("rplay") ) press(KPREV);
119 else if( is_msg("stop") ) press(KPSTOP);
120 else if( is_msg("play") ) press(KPPLAY);
121 else if( is_msg("media_lt") ) press(LEFT);
122 else if( is_msg("media_rt") ) press(RIGHT);
123 else if( is_msg("media_up") ) press(UP);
124 else if( is_msg("media_dn") ) press(DOWN);
125 else if( is_msg("pause") ) press(' ');
126 else if( is_msg("slow_lt") ) press(KPRECD);
127 else if( is_msg("slow_rt") ) press(KPAUSE);
128 else if( is_msg("fast_lt") ) press(KPBACK);
129 else if( is_msg("fast_rt") ) press(KPFWRD);
130 else if( is_msg("fscrn") ) press(KPFSCRN);
131 else if( is_msg("mute") ) press(KPMUTE);
132 else if( is_msg("vol_up") ) press(KPVOLUP);
133 else if( is_msg("vol_dn") ) press(KPVOLDN);
134 else if( is_msg("ch_up") ) press(KPCHUP);
135 else if( is_msg("ch_dn") ) press(KPCHDN);
136 else if( is_msg("key dot") ) press('.');
137 else if( is_msg("key cc") ) press(KPCC);
138 else if( is_msg("key tv") ) press(KPTV);
139 else if( is_msg("hand") ) press(KPHAND);
140 else if( is_msg("suspend") ) {
141 system("sync; sleep 1; acpitool -s");
143 else if( is_msg("power") ) {
144 system("sync; sleep 1; poweroff");
147 printf("AndroidControl::run: unkn msg: %s\n", msg);
153 AndroidControl::~AndroidControl()
155 if( Thread::running() ) {