mask tweaks, focus follows centroid, gradient/colorpicker rework, no hard edges in...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / androidcontrol.C
1
2 #include "keys.h"
3 #include "mwindow.h"
4 #include "mwindowgui.h"
5 #include "androidcontrol.h"
6 #include "preferences.h"
7
8
9 int AndroidControl::open(unsigned short port)
10 {
11         sockfd = socket(PF_INET,SOCK_DGRAM,0);
12         if( sockfd < 0 ) { perror("socket:"); return -1; }
13
14         struct linger lgr;
15         lgr.l_onoff = 0;
16         lgr.l_linger = 0;
17
18         if( setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &lgr, sizeof(lgr)) < 0 ) {
19                 perror("setlinger:");
20                 return -1;
21         }
22
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;
28
29         if( bind(sockfd,(struct sockaddr *)&sadr,sizeof(sadr)) < 0 ) {
30                 perror("bind:");
31                 return -1;
32         }
33
34         return 0;
35 }
36
37 void AndroidControl::close()
38 {
39         if( sockfd >= 0 ) {
40                 ::close(sockfd);
41                 sockfd = -1;
42         }
43 }
44
45 AndroidControl::AndroidControl(MWindowGUI *mwindow_gui)
46  : Thread(1, 0, 0)
47 {
48         this->mwindow_gui = mwindow_gui;
49         Thread::start();
50 }
51
52 bool AndroidControl::is_msg(const char *cp)
53 {
54         if( msg_len != (int)strlen(cp) ) return false;
55         if( strncmp(cp, msg, msg_len) ) return false;
56         return true;
57 }
58
59 void AndroidControl::press(int key)
60 {
61 // printf("press 0x%04x\n",key);
62         if( mwindow_gui->key_listener(key) ) return;
63         mwindow_gui->remote_control->remote_key(key);
64 }
65
66 void AndroidControl::run()
67 {
68         unsigned short port = mwindow_gui->mwindow->preferences->android_port;
69         done = open(port) >= 0 ? 0 : 1;
70
71         while( !done ) {
72                 struct sockaddr sadr;
73                 socklen_t sadr_len;
74                 enable_cancel();
75                 int ret = recvfrom(sockfd, buf, sizeof(buf), 0, &sadr, &sadr_len);
76                 if( ret < 0 ) {
77                         perror("AndroidControl::run: recvfrom");
78                         sleep(1);
79                 }
80                 disable_cancel();
81                 // validate message
82                 msg = buf;  msg_len = ret;
83                 // cinelerra android remote magic number
84                 if( msg[0] != 'C' || msg[1] != 'A' || msg[2] != 'R' ) continue;
85                 // version number
86                 if( msg[3] != 1 ) continue;
87                 msg += 4;  msg_len -= 4;
88                 // pin
89                 const char *pin = mwindow_gui->mwindow->preferences->android_pin;
90                 int len = sizeof(mwindow_gui->mwindow->preferences->android_pin);
91                 while( len > 0 && msg_len > 0 && *pin != 0 && *msg != 0 ) {
92                         if( *pin != *msg ) break;
93                         ++pin;  --len;
94                         ++msg;  --msg_len;
95                 }
96                 if( !len || !msg_len || *pin != *msg ) continue;
97                 ++msg; --msg_len;
98                 if( msg_len <= 0 ) continue;
99                 if( is_msg("stop") ) press(KPSTOP);
100                 else if( is_msg("play") ) press(KPPLAY);
101                 else if( is_msg("rplay") ) press(KPREV);
102                 else if( is_msg("pause") ) press(' ');
103                 else if( is_msg("fast_lt") ) press(KPBACK);
104                 else if( is_msg("media_up") ) press(UP);
105                 else if( is_msg("fast_rt") ) press(KPFWRD);
106                 else if( is_msg("menu") ) press(KPMENU);
107                 else if( is_msg("media_lt") ) press(LEFT);
108                 else if( is_msg("media_rt") ) press(RIGHT);
109                 else if( is_msg("slow_lt") ) press(KPRECD);
110                 else if( is_msg("media_dn") ) press(DOWN);
111                 else if( is_msg("slow_rt") ) press(KPAUSE);
112                 else if( is_msg("key 0") ) press('0');
113                 else if( is_msg("key 1") ) press('1');
114                 else if( is_msg("key 2") ) press('2');
115                 else if( is_msg("key 3") ) press('3');
116                 else if( is_msg("key 4") ) press('4');
117                 else if( is_msg("key 5") ) press('5');
118                 else if( is_msg("key 6") ) press('6');
119                 else if( is_msg("key 7") ) press('7');
120                 else if( is_msg("key 8") ) press('8');
121                 else if( is_msg("key 9") ) press('9');
122                 else if( is_msg("key A") ) press('a');
123                 else if( is_msg("key B") ) press('b');
124                 else if( is_msg("key C") ) press('c');
125                 else if( is_msg("key D") ) press('d');
126                 else if( is_msg("key E") ) press('e');
127                 else if( is_msg("key F") ) press('f');
128                 else if( is_msg("suspend") ) {
129                         system("sync; sleep 1; acpitool -s");
130                 }
131                 else if( is_msg("power") ) {
132                         system("sync; sleep 1; poweroff");
133                 }
134                 else {
135                         printf("AndroidControl::run: unkn msg: %s\n", msg);
136                         sleep(1);
137                 }
138         }
139 }
140
141 AndroidControl::~AndroidControl()
142 {
143         if( Thread::running() ) {
144                 done = 1;
145                 Thread::cancel();
146         }
147         Thread::join();
148 }
149