PageRenderTime 887ms CodeModel.GetById 47ms RepoModel.GetById 9ms app.codeStats 1ms

/unused/old/tmp.c

https://github.com/AWildColin/Webkey
C | 1447 lines | 1325 code | 78 blank | 44 comment | 330 complexity | 1b2e994c9d0511b7e206619fd76bfcf2 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, GPL-2.0, LGPL-2.1
  1. /*
  2. Copyright (C) 2010 Peter Mora, Zoltan Papp
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "suinput.h"
  15. #include <stdio.h>
  16. //#include <sys/types.h>
  17. //#include <sys/socket.h>
  18. //#include <netinet/in.h>
  19. #include <stdlib.h>
  20. #include <strings.h>
  21. #include "KeycodeLabels.h"
  22. #include <vector>
  23. #include "kcm.h"
  24. #include <sys/mman.h>
  25. #include <linux/fb.h>
  26. #include "png.h"
  27. #include <errno.h>
  28. #include <signal.h>
  29. #include <sys/wait.h>
  30. #include <limits.h>
  31. #include "mongoose.h"
  32. //#include <sys/wait.h>
  33. //#include <unistd.h>
  34. #include <linux/input.h>
  35. #undef stderr
  36. FILE *stderr = stderr;
  37. #ifdef MYDEB
  38. #define FB_DEVICE "/android/dev/graphics/fb0"
  39. #else
  40. #define FB_DEVICE "/dev/graphics/fb0"
  41. #endif
  42. static int exit_flag;
  43. static int touchcount=0;
  44. static std::string dir;
  45. static int dirdepth = 0;
  46. static std::string passfile;
  47. static std::string admin_password;
  48. struct mg_context *ctx;
  49. static void
  50. signal_handler(int sig_num)
  51. {
  52. if (sig_num == SIGCHLD)
  53. while (waitpid(-1, &sig_num, WNOHANG) > 0);
  54. else
  55. exit_flag = sig_num;
  56. }
  57. static int fbfd = -1;
  58. static void *fbmmap = MAP_FAILED;
  59. static int bytespp = 0;
  60. static struct fb_var_screeninfo scrinfo;
  61. static png_byte* pict = NULL;
  62. static png_byte** graph = NULL;
  63. static int touchfd = -1;
  64. static __s32 xmin;
  65. static __s32 xmax;
  66. static __s32 ymin;
  67. static __s32 ymax;
  68. static int newsockfd = -1;
  69. //base
  70. static const char* KCM_BASE[] ={
  71. "A 'A' '2' 'a' 'A'",
  72. "B 'B' '2' 'b' 'B'",
  73. "C 'C' '2' 'c' 'C'",
  74. "D 'D' '3' 'd' 'D'",
  75. "E 'E' '3' 'e' 'E'",
  76. "F 'F' '3' 'f' 'F'",
  77. "G 'G' '4' 'g' 'G'",
  78. "H 'H' '4' 'h' 'H'",
  79. "I 'I' '4' 'i' 'I'",
  80. "J 'J' '5' 'j' 'J'",
  81. "K 'K' '5' 'k' 'K'",
  82. "L 'L' '5' 'l' 'L'",
  83. "M 'M' '6' 'm' 'M'",
  84. "N 'N' '6' 'n' 'N'",
  85. "O 'O' '6' 'o' 'O'",
  86. "P 'P' '7' 'p' 'P'",
  87. "Q 'Q' '7' 'q' 'Q'",
  88. "R 'R' '7' 'r' 'R'",
  89. "S 'S' '7' 's' 'S'",
  90. "T 'T' '8' 't' 'T'",
  91. "U 'U' '8' 'u' 'U'",
  92. "V 'V' '8' 'v' 'V'",
  93. "W 'W' '9' 'w' 'W'",
  94. "X 'X' '9' 'x' 'X'",
  95. "Y 'Y' '9' 'y' 'Y'",
  96. "Z 'Z' '9' 'z' 'Z'",
  97. "COMMA ',' ',' ',' '?'",
  98. "PERIOD '.' '.' '.' '/'",
  99. "AT '@' 0x00 '@' '~'",
  100. "SPACE 0x20 0x20 0x20 0x20",
  101. "ENTER 0xa 0xa 0xa 0xa",
  102. "0 '0' '0' '0' ')'",
  103. "1 '1' '1' '1' '!'",
  104. "2 '2' '2' '2' '@'",
  105. "3 '3' '3' '3' '#'",
  106. "4 '4' '4' '4' '$'",
  107. "5 '5' '5' '5' '%'",
  108. "6 '6' '6' '6' '^'",
  109. "7 '7' '7' '7' '&'",
  110. "8 '8' '8' '8' '*'",
  111. "9 '9' '9' '9' '('",
  112. "TAB 0x9 0x9 0x9 0x9",
  113. "GRAVE '`' '`' '`' '~'",
  114. "MINUS '-' '-' '-' '_'",
  115. "EQUALS '=' '=' '=' '+'",
  116. "LEFT_BRACKET '[' '[' '[' '{'",
  117. "RIGHT_BRACKET ']' ']' ']' '}'",
  118. "BACKSLASH '\\' '\\' '\\' '|'",
  119. "SEMICOLON ';' ';' ';' ':'",
  120. "APOSTROPHE '\'' '\'' '\'' '\"'",
  121. "STAR '*' '*' '*' '<'",
  122. "POUND '#' '#' '#' '>'",
  123. "PLUS '+' '+' '+' '+'",
  124. "SLASH '/' '/' '/' '?'"
  125. };
  126. // ,!,",#,$,%,&,',(,),*,+,,,-,.,/
  127. int spec1[] = {62,8,75,10,11,12,14,75,16,7,15,70,55,69,56,56,52};
  128. int spec1sh[] = {0,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1};
  129. // :,;,<,=,>,?,@
  130. int spec2[] = {74,74,17,70,18,55,77};
  131. int spec2sh[] = {1,0,1,0,1,1,0};
  132. // [,\,],^,_,`
  133. int spec3[] = {71,73,72,13,69,68};
  134. int spec3sh[] = {0,0,0,1,1,0};
  135. // {,|,},~
  136. int spec4[] = {71,73,72,68};
  137. int spec4sh[] = {1,1,1,1,0};
  138. struct BIND{
  139. int ajax;
  140. int disp;
  141. int kcm;
  142. bool kcm_sh;
  143. bool sms;
  144. };
  145. struct FAST{
  146. bool show;
  147. char* name;
  148. int ajax;
  149. };
  150. static std::vector<BIND*> speckeys;
  151. static std::vector<FAST*> fastkeys;
  152. static int uinput_fd = -1;
  153. bool startswith(char* st, char* patt)
  154. {
  155. int i = 0;
  156. while(patt[i])
  157. {
  158. if (st[i] == 0 || st[i]-patt[i])
  159. return false;
  160. i++;
  161. }
  162. return true;
  163. }
  164. char* itoa(char* buff, int value)
  165. {
  166. unsigned int i = 0;
  167. bool neg = false;
  168. if (value<0)
  169. {
  170. neg = true;
  171. value=-value;
  172. }
  173. if (value==0)
  174. buff[i++] = '0';
  175. else
  176. while(value)
  177. {
  178. buff[i++] = '0'+(char)(value%10);
  179. value = value/10;
  180. }
  181. if (neg)
  182. buff[i++] = '-';
  183. unsigned int j = 0;
  184. for(j = 0; j < (i>>1); j++)
  185. {
  186. char t = buff[j];
  187. buff[j] = buff[i-j-1];
  188. buff[i-j-1] = t;
  189. }
  190. buff[i++]=0;
  191. return buff;
  192. }
  193. int getnum(char* st)
  194. {
  195. int r = 0;
  196. int i = 0;
  197. bool neg = false;
  198. if (st[i]=='-')
  199. {
  200. neg = true;
  201. i++;
  202. }
  203. while ('0'<=st[i] && '9'>=st[i])
  204. {
  205. r = r*10+(int)(st[i]-'0');
  206. i++;
  207. }
  208. if (neg)
  209. return -r;
  210. else
  211. return r;
  212. }
  213. void send_ok(struct mg_connection *conn)
  214. {
  215. mg_printf(conn,"HTTP/1.1 200 OK\r\nCache-Control: no-store, no-cache, must-revalidate\r\nCache-Control: post-check=0, pre-check=0\r\nPragma: no-cache\r\n\r\n");
  216. }
  217. void clear()
  218. {
  219. int i;
  220. for(i=0; i < speckeys.size(); i++)
  221. if (speckeys[i])
  222. delete speckeys[i];
  223. speckeys.clear();
  224. for(i=0; i < fastkeys.size(); i++)
  225. if (fastkeys[i])
  226. {
  227. delete[] fastkeys[i]->name;
  228. delete fastkeys[i];
  229. }
  230. fastkeys.clear();
  231. if (uinput_fd != -1)
  232. suinput_close(uinput_fd);
  233. if (pict)
  234. delete[] pict;
  235. pict = NULL;
  236. if (graph)
  237. delete[] graph;
  238. graph = NULL;
  239. }
  240. void error(const char *msg,const char *msg2 = NULL, const char *msg3=NULL, const char * msg4=NULL)
  241. {
  242. perror(msg);
  243. if (msg2) perror(msg2);
  244. if (msg3) perror(msg3);
  245. if (msg4) perror(msg4);
  246. clear();
  247. exit(1);
  248. }
  249. FILE* fo(const char* filename, const char* mode)
  250. {
  251. std::string longname = dir + filename;
  252. FILE* ret = fopen(longname.c_str(),mode);
  253. if (!ret)
  254. error("Couldn't open ",longname.c_str(), " with mode ", mode);
  255. else
  256. printf("%s is opened.\n",longname.c_str());
  257. return ret;
  258. }
  259. void init_uinput()
  260. {
  261. clear();
  262. int i;
  263. // FILE* kl = fopen("/android/system/usr/keylayout/AndroControll.kl","w");
  264. #ifdef MYDEB
  265. FILE* kl = fopen("/android/dev/AndroControll.kl","w");
  266. #else
  267. FILE* kl = fopen("/dev/AndroControll.kl","w");
  268. #endif
  269. if (!kl)
  270. {
  271. error("Couldn't open AndroControll.kl for writing.\n");
  272. }
  273. printf("AndroControll.kl is opened.\n");
  274. i = 0;
  275. while (KEYCODES[i].value)
  276. {
  277. fprintf(kl,"key %d %s WAKE\n",KEYCODES[i].value,KEYCODES[i].literal);
  278. FAST* load = new FAST;
  279. load->show = 0;
  280. load->name = new char[strlen(KEYCODES[i].literal)+1];
  281. strcpy(load->name,KEYCODES[i].literal);
  282. load->ajax = 0;
  283. fastkeys.push_back(load);
  284. i++;
  285. }
  286. fclose(kl);
  287. FILE* fk = fo("fast_keys.txt","r");
  288. int test;
  289. int ajax, show, id;
  290. while(fscanf(fk,"%d %d %d\n",&id,&show,&ajax) == 3)
  291. {
  292. if (id-1 < i)
  293. {
  294. fastkeys[id-1]->ajax = ajax;
  295. fastkeys[id-1]->show = show;
  296. }
  297. }
  298. fclose(fk);
  299. FILE* sk = fo("spec_keys.txt","r");
  300. int disp, sms;
  301. while(fscanf(sk,"%d %d %d\n",&sms,&ajax,&disp) == 3)
  302. {
  303. BIND* load = new BIND;
  304. load->ajax = ajax;
  305. load->disp = disp;
  306. load->sms = sms;
  307. speckeys.push_back(load);
  308. }
  309. fclose(sk);
  310. // FILE* kcm = fopen("/android/system/usr/keychars/AndroControll.kcm","w");
  311. #ifdef MYDEB
  312. FILE* kcm = fopen("/android/dev/AndroControll.kcm","w");
  313. #else
  314. FILE* kcm = fopen("/dev/AndroControll.kcm","w");
  315. #endif
  316. if (!kcm)
  317. {
  318. error("Couldn't open AndroControll.kcm for writing.\n");
  319. }
  320. printf("AndroControll.kcm is opened.\n");
  321. fprintf(kcm,"[type=QWERTY]\n# keycode display number base caps alt caps_alt\n");
  322. for(i=0; i<53; i++)
  323. {
  324. int k = 0;
  325. if (2*i < speckeys.size())
  326. {
  327. k = speckeys[2*i]->disp;
  328. speckeys[2*i]->kcm_sh = false;
  329. if (i<28)
  330. speckeys[2*i]->kcm = 29+i;
  331. if (28==i)
  332. speckeys[2*i]->kcm = 77;
  333. if (29==i)
  334. speckeys[2*i]->kcm = 62;
  335. if (30==i)
  336. speckeys[2*i]->kcm = 66;
  337. if (30<i && i<41)
  338. speckeys[2*i]->kcm = i-31+7;
  339. if (42==i)
  340. speckeys[2*i]->kcm = 61;
  341. //finish it....
  342. }
  343. int l = 0;
  344. if (2*i+1 < speckeys.size())
  345. {
  346. l = speckeys[2*i+1]->disp;
  347. speckeys[2*i+1]->kcm_sh = true;
  348. if (i<28)
  349. speckeys[2*i+1]->kcm = 29+i;
  350. if (28==i)
  351. speckeys[2*i+1]->kcm = 77;
  352. if (29==i)
  353. speckeys[2*i+1]->kcm = 62;
  354. if (30==i)
  355. speckeys[2*i+1]->kcm = 66;
  356. if (30<i && i<41)
  357. speckeys[2*i+1]->kcm = i-31+7;
  358. if (42==i)
  359. speckeys[2*i+1]->kcm = 61;
  360. //finish it....
  361. }
  362. fprintf(kcm,"%s %d %d\n",KCM_BASE[i],k,l);
  363. }
  364. fclose(kcm);
  365. // if (compile("/android/system/usr/keychars/AndroControll.kcm","/android/system/usr/keychars/AndroControll.kcm.bin"))
  366. #ifdef MYDEB
  367. if (compile("/android/dev/AndroControll.kcm","/android/dev/AndroControll.kcm.bin"))
  368. #else
  369. if (compile("/dev/AndroControll.kcm","/dev/AndroControll.kcm.bin"))
  370. #endif
  371. {
  372. error("Couldn't compile kcm to kcm.bin\n");
  373. }
  374. printf("kcm.bin is compiled.\n");
  375. struct input_id uid = {
  376. 0x06,//BUS_VIRTUAL, /* Bus type. */
  377. 1, /* Vendor id. */
  378. 1, /* Product id. */
  379. 1 /* Version id. */
  380. };
  381. // uinput_fd = suinput_open("../../../sdcard/AndroControll", &uid);
  382. printf("suinput init...\n");
  383. std::string devname;
  384. for (i=0; i < dirdepth; i++)
  385. devname = devname + "../";
  386. devname = devname + "dev/AndroControll";
  387. uinput_fd = suinput_open(devname.c_str(), &uid);
  388. }
  389. //from android-vnc-server
  390. static void init_fb(void)
  391. {
  392. size_t pixels;
  393. pixels = scrinfo.xres * scrinfo.yres;
  394. bytespp = scrinfo.bits_per_pixel / 8;
  395. printf("xres=%d, yres=%d, xresv=%d, yresv=%d, xoffs=%d, yoffs=%d, bpp=%d\n",
  396. (int)scrinfo.xres, (int)scrinfo.yres,
  397. (int)scrinfo.xres_virtual, (int)scrinfo.yres_virtual,
  398. (int)scrinfo.xoffset, (int)scrinfo.yoffset,
  399. (int)scrinfo.bits_per_pixel);
  400. fbmmap = mmap(NULL, pixels * bytespp, PROT_READ, MAP_SHARED, fbfd, 0);
  401. if (fbmmap == MAP_FAILED)
  402. {
  403. perror("mmap");
  404. exit(EXIT_FAILURE);
  405. }
  406. pict = new png_byte[scrinfo.yres*scrinfo.xres*3];
  407. if (scrinfo.yres > scrinfo.xres) //orientation might be changed
  408. graph = new png_byte*[scrinfo.yres];
  409. else
  410. graph = new png_byte*[scrinfo.xres];
  411. }
  412. void init_touch()
  413. {
  414. int i;
  415. #ifdef MYDEB
  416. char touch_device[26] = "/android/dev/input/event0";
  417. #else
  418. char touch_device[18] = "/dev/input/event0";
  419. #endif
  420. for (i=0; i<10; i++)
  421. {
  422. char name[256]="Unknown";
  423. touch_device[sizeof(touch_device)-2] = '0'+(char)i;
  424. struct input_absinfo info;
  425. if((touchfd = open(touch_device, O_RDWR)) == -1)
  426. {
  427. continue;
  428. }
  429. printf("searching for touch device, opening %s ... ",touch_device);
  430. // Get the Range of X and Y
  431. if(ioctl(touchfd, EVIOCGABS(ABS_X), &info))
  432. {
  433. printf("failed\n");
  434. close(touchfd);
  435. touchfd = -1;
  436. continue;
  437. }
  438. xmin = info.minimum;
  439. xmax = info.maximum;
  440. if(ioctl(touchfd, EVIOCGABS(ABS_Y), &info)) {
  441. printf("failed\n");
  442. close(touchfd);
  443. touchfd = -1;
  444. continue;
  445. }
  446. if (xmin < 0 || xmin == xmax) // xmin < 0 for the compass
  447. {
  448. printf("failed\n");
  449. close(touchfd);
  450. touchfd = -1;
  451. continue;
  452. }
  453. if (ioctl(touchfd, EVIOCGNAME(sizeof(name)),name) < 0)
  454. {
  455. printf("failed\n");
  456. close(touchfd);
  457. touchfd = -1;
  458. continue;
  459. }
  460. printf("success, device name is %s\n",name);
  461. ymin = info.minimum;
  462. ymax = info.maximum;
  463. printf("xmin = %d, xmax = %d, ymin = %d, ymax = %d\n",xmin,xmax,ymin,ymax);
  464. return;
  465. }
  466. }
  467. void update_image(int orient,int lowres)
  468. {
  469. FILE *fp;
  470. png_structp png_ptr;
  471. png_infop info_ptr;
  472. fp = fo("tmp.png", "wb");
  473. png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  474. info_ptr = png_create_info_struct(png_ptr);
  475. png_init_io(png_ptr, fp);
  476. png_set_compression_level(png_ptr, 1);
  477. int rr = scrinfo.red.offset;
  478. int rl = 8-scrinfo.red.length;
  479. int gr = scrinfo.green.offset;
  480. int gl = 8-scrinfo.green.length;
  481. int br = scrinfo.blue.offset;
  482. int bl = 8-scrinfo.blue.length;
  483. int i = 0;
  484. int j;
  485. printf("rr=%d, rl=%d, gr=%d, gl=%d, br=%d, bl=%d\n",rr,rl,gr,gl,br,bl);
  486. if (orient == 0)
  487. png_set_IHDR(png_ptr, info_ptr, scrinfo.xres>>lowres, scrinfo.yres>>lowres,
  488. 8,// scrinfo.bits_per_pixel,
  489. PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  490. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  491. else
  492. png_set_IHDR(png_ptr, info_ptr, scrinfo.yres>>lowres, scrinfo.xres>>lowres,
  493. 8,// scrinfo.bits_per_pixel,
  494. PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
  495. PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  496. int x = scrinfo.xres;
  497. int y = scrinfo.yres;
  498. int m = scrinfo.yres*scrinfo.xres;
  499. int k;
  500. int p = 0;
  501. i = 0;
  502. if (lowres) // I use if outside the for, maybe it's faster this way
  503. {
  504. if (bytespp == 2) //16 bit
  505. {
  506. unsigned short int* map = (unsigned short int*)fbmmap;
  507. if (orient == 0) //vertical
  508. {
  509. for (j = 0; j < y; j+=2)
  510. {
  511. for (k = 0; k < x; k+=2)
  512. {
  513. pict[i++] = ((map[p]>>rr<<rl)+(map[p+1]>>rr<<rl)+(map[p+x]>>rr<<rl)+(map[p+x+1]>>rr<<rl)>>2);
  514. pict[i++] = ((map[p]>>gr<<gl)+(map[p+1]>>gr<<gl)+(map[p+x]>>gr<<gl)+(map[p+x+1]>>gr<<gl)>>2);
  515. pict[i++] = ((map[p]>>br<<bl)+(map[p+1]>>br<<bl)+(map[p+x]>>br<<bl)+(map[p+x+1]>>br<<bl)>>2);
  516. p+=2;
  517. }
  518. p+=x;
  519. }
  520. }
  521. else //horizontal
  522. {
  523. for (j = 0; j < x; j+=2)
  524. {
  525. p = (x-j-1);
  526. for (k = 0; k < y; k+=2)
  527. {
  528. pict[i++] = ((map[p]>>rr<<rl)+(map[p-1]>>rr<<rl)+(map[p+x]>>rr<<rl)+(map[p+x-1]>>rr<<rl)>>2);
  529. pict[i++] = ((map[p]>>gr<<gl)+(map[p-1]>>gr<<gl)+(map[p+x]>>gr<<gl)+(map[p+x-1]>>gr<<gl)>>2);
  530. pict[i++] = ((map[p]>>br<<bl)+(map[p-1]>>br<<bl)+(map[p+x]>>br<<bl)+(map[p+x-1]>>br<<bl)>>2);
  531. p += 2*x;
  532. }
  533. }
  534. }
  535. }
  536. if (bytespp == 4) //32 bit
  537. {
  538. unsigned int* map = (unsigned int*)fbmmap;
  539. if (orient == 0) //vertical
  540. {
  541. for (j = 0; j < y; j+=2)
  542. {
  543. for (k = 0; k < x; k+=2)
  544. {
  545. pict[i++] = ((map[p]>>rr<<rl)+(map[p+1]>>rr<<rl)+(map[p+x]>>rr<<rl)+(map[p+x+1]>>rr<<rl)>>2);
  546. pict[i++] = ((map[p]>>gr<<gl)+(map[p+1]>>gr<<gl)+(map[p+x]>>gr<<gl)+(map[p+x+1]>>gr<<gl)>>2);
  547. pict[i++] = ((map[p]>>br<<bl)+(map[p+1]>>br<<bl)+(map[p+x]>>br<<bl)+(map[p+x+1]>>br<<bl)>>2);
  548. p+=2;
  549. }
  550. p+=x;
  551. }
  552. }
  553. else //horizontal
  554. {
  555. for (j = 0; j < x; j+=2)
  556. {
  557. p = (x-j-1);
  558. for (k = 0; k < y; k+=2)
  559. {
  560. pict[i++] = ((map[p]>>rr<<rl)+(map[p-1]>>rr<<rl)+(map[p+x]>>rr<<rl)+(map[p+x-1]>>rr<<rl)>>2);
  561. pict[i++] = ((map[p]>>gr<<gl)+(map[p-1]>>gr<<gl)+(map[p+x]>>gr<<gl)+(map[p+x-1]>>gr<<gl)>>2);
  562. pict[i++] = ((map[p]>>br<<bl)+(map[p-1]>>br<<bl)+(map[p+x]>>br<<bl)+(map[p+x-1]>>br<<bl)>>2);
  563. p += 2*x;
  564. }
  565. }
  566. }
  567. }
  568. }
  569. else //hires
  570. {
  571. if (bytespp == 2)
  572. {
  573. unsigned short int* map = (unsigned short int*)fbmmap;
  574. if (orient == 0) //vertical
  575. {
  576. for (j = 0; j < m; j++)
  577. {
  578. pict[i++] = map[j]>>rr<<rl;
  579. pict[i++] = map[j]>>gr<<gl;
  580. pict[i++] = map[j]>>br<<bl;
  581. }
  582. }
  583. else //horizontal
  584. {
  585. for (j = 0; j < x; j++)
  586. {
  587. p = (x-j-1);
  588. for (k = 0; k < y; k++)
  589. {
  590. pict[i++] = map[p]>>rr<<rl;
  591. pict[i++] = map[p]>>gr<<gl;
  592. pict[i++] = map[p]>>br<<bl;
  593. p += x;
  594. }
  595. }
  596. }
  597. }
  598. if (bytespp == 4)
  599. {
  600. unsigned int* map = (unsigned int*)fbmmap;
  601. if (orient == 0) //vertical
  602. {
  603. for (j = 0; j < m; j++)
  604. {
  605. pict[i++] = map[j]>>rr<<rl;
  606. pict[i++] = map[j]>>gr<<gl;
  607. pict[i++] = map[j]>>br<<bl;
  608. }
  609. }
  610. else //horizontal
  611. {
  612. for (j = 0; j < x; j++)
  613. {
  614. p = (x-j-1);
  615. for (k = 0; k < y; k++)
  616. {
  617. pict[i++] = map[p]>>rr<<rl;
  618. pict[i++] = map[p]>>gr<<gl;
  619. pict[i++] = map[p]>>br<<bl;
  620. p += x;
  621. }
  622. }
  623. }
  624. }
  625. }
  626. if (orient == 0)
  627. {
  628. for (i = 0; i < y>>lowres; i++)
  629. graph[i] = pict+(i*x*3>>lowres);
  630. }
  631. else
  632. {
  633. for (i = 0; i < x>>lowres; i++)
  634. graph[i] = pict+(i*y*3>>lowres);
  635. }
  636. png_write_info(png_ptr, info_ptr);
  637. png_write_image(png_ptr, graph);
  638. png_write_end(png_ptr, info_ptr);
  639. png_destroy_write_struct(&png_ptr, &info_ptr);
  640. fclose(fp);
  641. }
  642. static void
  643. touch(struct mg_connection *conn,
  644. const struct mg_request_info *ri, void *data)
  645. {
  646. int n = strlen(ri->uri);
  647. if (n<8)
  648. return;
  649. int orient = 0;
  650. if (ri->uri[7]=='h')
  651. orient = 1;
  652. int i = 8;
  653. int x = getnum(ri->uri+i);
  654. while (i<n && ri->uri[i++]!='_');
  655. int y = getnum(ri->uri+i);
  656. while (i<n && ri->uri[i++]!='_');
  657. int down = getnum(ri->uri+i);
  658. if(touchfd != -1 && scrinfo.xres && scrinfo.yres)
  659. {
  660. struct input_event ev;
  661. if (orient)
  662. {
  663. int t = x;
  664. x = scrinfo.xres-y;
  665. y = t;
  666. }
  667. if (x < 0) x = 0;
  668. if (y < 0) y = 0;
  669. if (x > scrinfo.xres) x = scrinfo.xres;
  670. if (y > scrinfo.yres) y = scrinfo.yres;
  671. // Calculate the final x and y
  672. x = xmin + (x * (xmax - xmin)) / (scrinfo.xres);
  673. y = ymin + (y * (ymax - ymin)) / (scrinfo.yres);
  674. memset(&ev, 0, sizeof(ev));
  675. // Then send a BTN_TOUCH
  676. if (down != 2)
  677. {
  678. gettimeofday(&ev.time,0);
  679. ev.type = EV_KEY;
  680. ev.code = BTN_TOUCH;
  681. ev.value = down;
  682. if(write(touchfd, &ev, sizeof(ev)) < 0)
  683. printf("touchfd write failed.\n");
  684. }
  685. gettimeofday(&ev.time,0);
  686. ev.type = EV_ABS;
  687. ev.code = 48;
  688. ev.value = down>0?100:0;
  689. if(write(touchfd, &ev, sizeof(ev)) < 0)
  690. printf("touchfd write failed.\n");
  691. // Then send the X
  692. gettimeofday(&ev.time,0);
  693. ev.type = EV_ABS;
  694. ev.code = ABS_X;
  695. ev.value = x;
  696. if(write(touchfd, &ev, sizeof(ev)) < 0)
  697. printf("touchfd write failed.\n");
  698. // Then send the X
  699. gettimeofday(&ev.time,0);
  700. ev.type = EV_ABS;
  701. ev.code = 53;//ABS_X;
  702. ev.value = x;
  703. if(write(touchfd, &ev, sizeof(ev)) < 0)
  704. printf("touchfd write failed.\n");
  705. // Then send the Y
  706. gettimeofday(&ev.time,0);
  707. ev.type = EV_ABS;
  708. ev.code = ABS_Y;
  709. ev.value = y;
  710. if(write(touchfd, &ev, sizeof(ev)) < 0)
  711. printf("touchfd write failed.\n");
  712. // Then send the Y
  713. gettimeofday(&ev.time,0);
  714. ev.type = EV_ABS;
  715. ev.code = 54;//ABS_Y;
  716. ev.value = y;
  717. if(write(touchfd, &ev, sizeof(ev)) < 0)
  718. printf("touchfd write failed.\n");
  719. // Finally send the SYN
  720. gettimeofday(&ev.time,0);
  721. ev.type = EV_SYN;
  722. ev.code = 2;
  723. ev.value = 0;
  724. if(write(touchfd, &ev, sizeof(ev)) < 0)
  725. printf("touchfd write failed.\n");
  726. gettimeofday(&ev.time,0);
  727. ev.type = EV_SYN;
  728. ev.code = 0;
  729. ev.value = 0;
  730. if(write(touchfd, &ev, sizeof(ev)) < 0)
  731. printf("touchfd write failed.\n");
  732. //printf("%d. injectTouch x=%d, y=%d, down=%d\n", touchcount++, x, y, down);
  733. }
  734. send_ok(conn);
  735. }
  736. static void
  737. key(struct mg_connection *conn,
  738. const struct mg_request_info *ri, void *data)
  739. {
  740. bool sms_mode = false;
  741. int key = 0;
  742. bool old = false;
  743. int orient = 0;
  744. printf("%s\n",ri->uri);
  745. // /oldkey_hn-22 -> -22 key with normal mode, horisontal
  746. int n = 0;
  747. if (startswith(ri->uri,"/oldkey") && strlen(ri->uri)>10)
  748. {
  749. n = 9;
  750. old = true;
  751. }
  752. else
  753. if (strlen(ri->uri)>6)
  754. {
  755. n = 6;
  756. }
  757. else
  758. return;
  759. if (ri->uri[n-2] == 'h')
  760. orient = 1;
  761. if (ri->uri[n-1] == 's')
  762. sms_mode = true;
  763. if (ri->uri[++n] == 0)
  764. return;
  765. send_ok(conn);
  766. while(1)
  767. {
  768. key = getnum(ri->uri+n);
  769. if (old && key < 0)
  770. key = -key;
  771. if (key == 0) // I don't know how, but it happens
  772. {
  773. send_ok(conn);
  774. return;
  775. }
  776. printf("KEY %d\n",key);
  777. int i;
  778. if (sms_mode)
  779. for (i=0; i < speckeys.size(); i++)
  780. if ((speckeys[i]->ajax == key || (old && speckeys[i]->ajax == -key)) && speckeys[i]->sms)
  781. {
  782. printf("pressed: %d\n",speckeys[i]->kcm);
  783. suinput_press(uinput_fd, 57); //left alt
  784. if (speckeys[i]->kcm_sh)
  785. suinput_press(uinput_fd, 59); //left shift
  786. suinput_click(uinput_fd, speckeys[i]->kcm);
  787. suinput_release(uinput_fd, 57); //left alt
  788. if (speckeys[i]->kcm_sh)
  789. suinput_release(uinput_fd, 59); //left shift
  790. break;
  791. }
  792. if (!sms_mode || i == speckeys.size())
  793. for (i=0; i < speckeys.size(); i++)
  794. if ((speckeys[i]->ajax == key|| (old && speckeys[i]->ajax == -key) ) && speckeys[i]->sms == false)
  795. {
  796. printf("pressed: %d\n",speckeys[i]->kcm);
  797. suinput_press(uinput_fd, 57); //left alt
  798. if (speckeys[i]->kcm_sh)
  799. suinput_press(uinput_fd, 59); //left shift
  800. suinput_click(uinput_fd, speckeys[i]->kcm);
  801. suinput_release(uinput_fd, 57); //left alt
  802. if (speckeys[i]->kcm_sh)
  803. suinput_release(uinput_fd, 59); //left shift
  804. }
  805. for (i=0; i < fastkeys.size(); i++)
  806. if (fastkeys[i]->ajax == key || (old && fastkeys[i]->ajax == -key))
  807. {
  808. int k = i+1;
  809. if (orient)
  810. {
  811. if (k == 19) //up -> right
  812. k = 22;
  813. else if (k == 20) //down -> left
  814. k = 21;
  815. else if (k == 21) //left -> up
  816. k = 19;
  817. else if (k == 22) //right -> down
  818. k = 20;
  819. }
  820. suinput_click(uinput_fd, k);
  821. }
  822. if (48<=key && key < 58)
  823. suinput_click(uinput_fd, key-48+7);
  824. if (97<=key && key < 123)
  825. suinput_click(uinput_fd, key-97+29);
  826. if (65<=key && key < 91)
  827. {
  828. suinput_press(uinput_fd, 59); //left shift
  829. suinput_click(uinput_fd, key-65+29);
  830. suinput_release(uinput_fd, 59); //left shift
  831. }
  832. if (32<=key && key <= 47)
  833. {
  834. if (spec1sh[key-32]) suinput_press(uinput_fd, 59); //left shift
  835. suinput_click(uinput_fd, spec1[key-32]);
  836. if (spec1sh[key-32]) suinput_release(uinput_fd, 59); //left shift
  837. }
  838. if (58<=key && key <= 64)
  839. {
  840. if (spec2sh[key-58]) suinput_press(uinput_fd, 59); //left shift
  841. suinput_click(uinput_fd, spec2[key-58]);
  842. if (spec2sh[key-58]) suinput_release(uinput_fd, 59); //left shift
  843. }
  844. if (91<=key && key <= 96)
  845. {
  846. if (spec3sh[key-91]) suinput_press(uinput_fd, 59); //left shift
  847. suinput_click(uinput_fd, spec3[key-91]);
  848. if (spec3sh[key-91]) suinput_release(uinput_fd, 59); //left shift
  849. }
  850. if (123<=key && key <= 127)
  851. {
  852. if (spec4sh[key-123]) suinput_press(uinput_fd, 59); //left shift
  853. suinput_click(uinput_fd, spec4[key-123]);
  854. if (spec4sh[key-123]) suinput_release(uinput_fd, 59); //left shift
  855. }
  856. if (key == -8 || (old && key == 8)) suinput_click(uinput_fd, 67); //BACKSPACE -> DEL
  857. if (key == -13 || (old && key == 13)) suinput_click(uinput_fd, 66); //ENTER
  858. while (ri->uri[n] != '_')
  859. {
  860. if (ri->uri[++n] == 0)
  861. return;
  862. }
  863. if (ri->uri[++n] == 0)
  864. return;
  865. usleep(1000);
  866. }
  867. }
  868. static void
  869. savebuttons(struct mg_connection *conn,
  870. const struct mg_request_info *ri, void *data)
  871. {
  872. int i;
  873. for (i=0; i < fastkeys.size(); i++)
  874. fastkeys[i]->show = false;
  875. int n = ri->post_data_len;
  876. i = 0;
  877. while(i<n)
  878. {
  879. if (startswith(ri->post_data+i,"show_"))
  880. {
  881. i+=5;
  882. int id = getnum(ri->post_data+i);
  883. if (id-1<fastkeys.size())
  884. fastkeys[id-1]->show = true;
  885. printf("SHOW %d\n",id-1);
  886. }
  887. else if (startswith(ri->post_data+i,"keycode_"))
  888. {
  889. i+=8;
  890. int id = getnum(ri->post_data+i);
  891. while (i<n && ri->post_data[i++]!='=');
  892. int ajax = getnum(ri->post_data+i);
  893. if (id-1<fastkeys.size())
  894. fastkeys[id-1]->ajax = ajax;
  895. }
  896. while (i<n && ri->post_data[i++]!='&');
  897. }
  898. FILE* fk = fo("fast_keys.txt","w");
  899. for (i=0;i<fastkeys.size();i++)
  900. {
  901. fprintf(fk,"%d %d %d\n",i+1,fastkeys[i]->show,fastkeys[i]->ajax);
  902. }
  903. fclose(fk);
  904. mg_printf(conn,"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<html><head><meta http-equiv=\"refresh\" content=\"0;url=/\"></head><body>redirecting</body></html>");
  905. }
  906. static void
  907. savekeys(struct mg_connection *conn,
  908. const struct mg_request_info *ri, void *data)
  909. {
  910. int i;
  911. int n = ri->post_data_len;
  912. for (i=0; i < speckeys.size(); i++)
  913. if (speckeys[i])
  914. delete speckeys[i];
  915. speckeys.clear();
  916. for (i=0; i < 106; i++)
  917. {
  918. BIND* load = new BIND;
  919. load->ajax = load->disp = load->sms = 0;
  920. speckeys.push_back(load);
  921. }
  922. i = 0;
  923. while(i<n)
  924. {
  925. if (startswith(ri->post_data+i,"sms_"))
  926. {
  927. i+=4;
  928. int id = getnum(ri->post_data+i);
  929. if (id-1<speckeys.size())
  930. speckeys[id-1]->sms = true;
  931. }
  932. else if (startswith(ri->post_data+i,"keycode_"))
  933. {
  934. i+=8;
  935. int id = getnum(ri->post_data+i);
  936. while (i<n && ri->post_data[i++]!='=');
  937. int ajax = getnum(ri->post_data+i);
  938. if (id-1<speckeys.size())
  939. speckeys[id-1]->ajax = ajax;
  940. }
  941. else if (startswith(ri->post_data+i,"tokeycode_"))
  942. {
  943. i+=10;
  944. int id = getnum(ri->post_data+i);
  945. while (i<n && ri->post_data[i++]!='=');
  946. int disp = getnum(ri->post_data+i);
  947. if (id-1<speckeys.size())
  948. speckeys[id-1]->disp = disp;
  949. }
  950. while (i<n && ri->post_data[i++]!='&');
  951. }
  952. FILE* sk = fo("spec_keys.txt","w");
  953. for(i=0; i <speckeys.size(); i++)
  954. if (speckeys[i]->ajax || speckeys[i]->disp || speckeys[i]->disp)
  955. fprintf(sk,"%d %d %d\n",speckeys[i]->sms,speckeys[i]->ajax,speckeys[i]->disp);
  956. fclose(sk);
  957. init_uinput();
  958. mg_printf(conn,"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<html><head><meta http-equiv=\"refresh\" content=\"0;url=/\"></head><body>redirecting</body></html>");
  959. }
  960. static void
  961. helppng(struct mg_connection *conn,
  962. const struct mg_request_info *ri, void *data)
  963. {
  964. send_ok(conn);
  965. FILE* f = fo("help.png","rb");
  966. fseek (f , 0 , SEEK_END);
  967. int lSize = ftell (f);
  968. rewind (f);
  969. char* filebuffer = new char[lSize+1];
  970. fread(filebuffer,1,lSize,f);
  971. filebuffer[lSize] = 0;
  972. mg_write(conn,filebuffer,lSize);
  973. fclose(f);
  974. }
  975. static void
  976. helphtml(struct mg_connection *conn,
  977. const struct mg_request_info *ri, void *data)
  978. {
  979. send_ok(conn);
  980. FILE* f = fo("help.html","rb");
  981. fseek (f , 0 , SEEK_END);
  982. int lSize = ftell (f);
  983. rewind (f);
  984. char* filebuffer = new char[lSize+1];
  985. fread(filebuffer,1,lSize,f);
  986. filebuffer[lSize] = 0;
  987. mg_write(conn,filebuffer,lSize);
  988. fclose(f);
  989. }
  990. static void
  991. index(struct mg_connection *conn,
  992. const struct mg_request_info *ri, void *data)
  993. {
  994. send_ok(conn);
  995. FILE* f = fo("index.html","rb");
  996. fseek (f , 0 , SEEK_END);
  997. int lSize = ftell (f);
  998. rewind (f);
  999. char* filebuffer = new char[lSize+1];
  1000. fread(filebuffer,1,lSize,f);
  1001. filebuffer[lSize] = 0;
  1002. mg_write(conn,filebuffer,lSize);
  1003. fclose(f);
  1004. int i;
  1005. for (i=0; i < fastkeys.size(); i++)
  1006. if (fastkeys[i]->show)
  1007. {
  1008. mg_printf(conn,"<input type=\"button\" class=\"butt\" value=\"%s\" onclick=\"makeRequest('button_%d','')\"/>",fastkeys[i]->name,i+1);
  1009. }
  1010. mg_printf(conn,"</div><div id=\"res\">results</div></div><script type=\"text/javascript\" language=\"javascript\">document.images.screenshot.onmousemove=move; function setWidth(orient){ if (orient == 1) document.images.screenshot.width=\"%d\"; else document.images.screenshot.width=\"%d\";}</script></body></html>",scrinfo.yres,scrinfo.xres);
  1011. delete[] filebuffer;
  1012. }
  1013. static void
  1014. button(struct mg_connection *conn,
  1015. const struct mg_request_info *ri, void *data)
  1016. {
  1017. int key = getnum(ri->uri+8);
  1018. printf("button %d\n",key);
  1019. suinput_click(uinput_fd, key);
  1020. send_ok(conn);
  1021. }
  1022. static void
  1023. config_buttons(struct mg_connection *conn,
  1024. const struct mg_request_info *ri, void *data)
  1025. {
  1026. FILE* f = fo("config_buttons.html","rb");
  1027. fseek (f , 0 , SEEK_END);
  1028. printf("file fseeked\n");
  1029. int lSize = ftell (f);
  1030. printf("file ftold, size=%d\n",lSize);
  1031. rewind (f);
  1032. printf("file rewinded\n");
  1033. char* filebuffer = new char[lSize+1];
  1034. fread(filebuffer,1,lSize,f);
  1035. printf("file read\n");
  1036. filebuffer[lSize] = 0;
  1037. mg_write(conn,filebuffer,lSize);
  1038. printf("file sent\n");
  1039. fclose(f);
  1040. printf("file closed\n");
  1041. mg_printf(conn,"<table border=\"1\">\n");
  1042. int i;
  1043. for (i=0; i < fastkeys.size(); i++)
  1044. {
  1045. mg_printf(conn, "<tr><td>%s</td><td><input type=\"checkbox\" name=\"show_%d\">Show</input></td><td>Key: <input type=\"text\" name=\"key_%d\" maxlength=\"1\" size=\"1\" onkeypress=\"var unicode=event.charCode? event.charCode : -event.keyCode;document.config_buttons.keycode_%d.value=unicode; document.config_buttons.key_%d.value=String.fromCharCode(document.config_buttons.keycode_%d.value)\"/></td><td>Keycode: <input type=\"text\" name=\"keycode_%d\" maxlength=\"8\" size=\"8\" onkeyup=\"document.config_buttons.key_%d.value=String.fromCharCode(document.config_buttons.keycode_%d.value)\"/></td></tr>\n",fastkeys[i]->name,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1);
  1046. }
  1047. mg_printf(conn,"</table></form>\n<script type=\"text/javascript\" language=\"javascript\">");
  1048. for (i=0; i < fastkeys.size(); i++)
  1049. {
  1050. mg_printf(conn,"document.config_buttons.key_%d.value=String.fromCharCode(%d);document.config_buttons.keycode_%d.value='%d';document.config_buttons.show_%d",i+1,fastkeys[i]->ajax,i+1,fastkeys[i]->ajax,i+1);
  1051. if (fastkeys[i]->show)
  1052. mg_printf(conn,".checked='checked';\n");
  1053. else
  1054. mg_printf(conn,".checked='';\n");
  1055. }
  1056. mg_printf(conn,"</script></body></html>");
  1057. delete[] filebuffer;
  1058. }
  1059. static void
  1060. config_keys(struct mg_connection *conn,
  1061. const struct mg_request_info *ri, void *data)
  1062. {
  1063. FILE* f = fo("config_keys.html","rb");
  1064. fseek (f , 0 , SEEK_END);
  1065. printf("file fseeked\n");
  1066. int lSize = ftell (f);
  1067. printf("file ftold, size=%d\n",lSize);
  1068. rewind (f);
  1069. printf("file rewinded\n");
  1070. char* filebuffer = new char[lSize+1];
  1071. fread(filebuffer,1,lSize,f);
  1072. printf("file read\n");
  1073. filebuffer[lSize] = 0;
  1074. mg_write(conn,filebuffer,lSize);
  1075. printf("file sent\n");
  1076. fclose(f);
  1077. printf("file closed\n");
  1078. mg_printf(conn,"<table border=\"1\">\n");
  1079. int i;
  1080. for (i=0; i < 106; i++)
  1081. {
  1082. mg_printf(conn,"<tr><td>Char: <input type=\"text\" name=\"key_%d\" maxlength=\"1\" size=\"1\" onkeypress=\"var unicode=event.charCode? event.charCode : event.keyCode;document.config_keys.keycode_%d.value=unicode; document.config_keys.key_%d.value=String.fromCharCode(document.config_keys.keycode_%d.value)\"/></td><td>Keycode: <input type=\"text\" name=\"keycode_%d\" maxlength=\"8\" size=\"8\" onkeyup=\"document.config_keys.key_%d.value=String.fromCharCode(document.config_keys.keycode_%d.value)\"/></td><td> converts to </td><td>Char: <input type=\"text\" name=\"tokey_%d\" maxlength=\"1\" size=\"1\" onkeypress=\"var unicode=event.charCode? event.charCode : event.keyCode;document.config_keys.tokeycode_%d.value=unicode; document.config_keys.tokey_%d.value=String.fromCharCode(document.config_keys.tokeycode_%d.value)\"/></td><td>Keycode: <input type=\"text\" name=\"tokeycode_%d\" maxlength=\"8\" size=\"8\" onkeyup=\"document.config_keys.tokey_%d.value=String.fromCharCode(document.config_keys.tokeycode_%d.value)\"/></td><td><input type=\"checkbox\" name=\"sms_%d\">works in SMS mode</input></td></tr>\n",i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1,i+1);
  1083. }
  1084. mg_printf(conn,"</table></form>\n<script type=\"text/javascript\" language=\"javascript\">\n");
  1085. for (i=0; i < speckeys.size(); i++)
  1086. {
  1087. mg_printf(conn,"document.config_keys.key_%d.value=String.fromCharCode(%d);document.config_keys.keycode_%d.value='%d';document.config_keys.tokey_%d.value=String.fromCharCode(%d);document.config_keys.tokeycode_%d.value='%d';document.config_keys.sms_%d",i+1,speckeys[i]->ajax,i+1,speckeys[i]->ajax,i+1,speckeys[i]->disp,i+1,speckeys[i]->disp,i+1);
  1088. if (speckeys[i]->sms)
  1089. mg_printf(conn,".checked='checked';\n");
  1090. else
  1091. mg_printf(conn,".checked='';\n");
  1092. }
  1093. mg_printf(conn,"</script></body></html>");
  1094. delete[] filebuffer;
  1095. }
  1096. static void
  1097. config(struct mg_connection *conn,
  1098. const struct mg_request_info *ri, void *data)
  1099. {
  1100. int n = ri->post_data_len;
  1101. int i = 0;
  1102. char name[256];
  1103. char pass[256];
  1104. bool changed = false;
  1105. int j = 0;
  1106. while(i < n)
  1107. {
  1108. printf("%s\n",ri->post_data);
  1109. if (!memcmp(ri->post_data+i, "username",8))
  1110. {
  1111. i+=9;
  1112. j = 0;
  1113. while(i<n && ri->post_data[i] != '&' && j<255)
  1114. name[j++] = ri->post_data[i++];
  1115. name[j] = 0; i++;
  1116. }
  1117. else if (!memcmp(ri->post_data+i, "password",8))
  1118. {
  1119. i+=9;
  1120. int k = 0;
  1121. while(i<n && ri->post_data[i] != '&' && k<255)
  1122. pass[k++] = ri->post_data[i++];
  1123. pass[k] = 0; i++;
  1124. if (j && k)
  1125. {
  1126. mg_modify_passwords_file(ctx, (dir+passfile).c_str(), name, pass);
  1127. changed = true;
  1128. }
  1129. }
  1130. else if (!memcmp(ri->post_data+i, "remove",6))
  1131. {
  1132. i=n;
  1133. if (j)
  1134. {
  1135. mg_modify_passwords_file(ctx, (dir+passfile).c_str(), name, "");
  1136. changed = true;
  1137. }
  1138. }
  1139. else
  1140. i++;
  1141. }
  1142. if (changed)
  1143. {
  1144. mg_printf(conn,"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<html><head><meta http-equiv=\"refresh\" content=\"0;url=/config\"></head><body>redirecting</body></html>");
  1145. return;
  1146. }
  1147. send_ok(conn);
  1148. mg_printf(conn,"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><style>.menu a{color: #000000; font-size: 18px; text-decoration: none; padding-right: 20px; padding-left: 20px;} </style>\n<title>WebKey for Android</title>\n</head>\n");
  1149. mg_printf(conn,"<body><div class=\"menu\" style=\"background-color: #eeaa44; padding-top: 8px; padding-bottom: 8px; text-align: center;\"><a href=\"/\">phone</a> <a href=\"/config_buttons.html\">configure buttons</a> <a href=\"/config_keys.html\">configure keys</a><a href=\"/config\">users</a> <a href=\"/sdcard\">sdcard</a><a href=\"/help.html\">help</a></div>Users (empty passwords are not allowed):");
  1150. char line[256]; char domain[256];
  1151. FILE* fp = fo(passfile.c_str(),"r");
  1152. while (fgets(line, sizeof(line), fp) != NULL)
  1153. {
  1154. if (sscanf(line, "%[^:]:%[^:]:%s", name, domain, pass) != 3)
  1155. continue;
  1156. mg_printf(conn,"<form name=\"%s_form\" method=\"post\">username: <input type=\"text\" readonly=\"readonly\" value=\"%s\" name=\"username\"> password: <input type=\"password\" name=\"password\"></input><input type=\"submit\" value=\"Change password\"></input><input name=\"remove\" type=\"submit\" value=\"Remove\"></input>",name,name,name);
  1157. if (!strcmp(name,"admin"))
  1158. mg_printf(conn,"This password will change on every restart\n");
  1159. mg_printf(conn,"</form>\n");
  1160. }
  1161. mg_printf(conn,"<form name=\"newuser\" method=\"post\">New user:<input type=\"text\" name=\"username\"> password: <input type=\"password\" name=\"password\"></input><input type=\"submit\" value=\"Create\"></input></form>\n");
  1162. fclose(fp);
  1163. mg_printf(conn,"</body></html>");
  1164. }
  1165. static void
  1166. screenshot(struct mg_connection *conn,
  1167. const struct mg_request_info *ri, void *data)
  1168. {
  1169. int orient = 0;
  1170. if (ri->uri[16] == 'h') // horizontal
  1171. orient = 1;
  1172. int lowres = 0;
  1173. if (ri->uri[17] == 'l') // low res
  1174. lowres = 1;
  1175. if (!pict)
  1176. init_fb();
  1177. update_image(orient,lowres);
  1178. send_ok(conn);
  1179. FILE* f = fo("tmp.png","rb");
  1180. fseek (f , 0 , SEEK_END);
  1181. int lSize = ftell (f);
  1182. rewind (f);
  1183. char* filebuffer = new char[lSize+1];
  1184. if(!filebuffer)
  1185. {
  1186. error("not enough memory for loading tmp.png\n");
  1187. }
  1188. fread(filebuffer,1,lSize,f);
  1189. filebuffer[lSize] = 0;
  1190. printf("sent bytes = %d\n",mg_write(conn,filebuffer,lSize));
  1191. fclose(f);
  1192. delete[] filebuffer;
  1193. }
  1194. static void
  1195. stop(struct mg_connection *conn,
  1196. const struct mg_request_info *ri, void *data)
  1197. {
  1198. send_ok(conn);
  1199. if (ri->remote_ip==2130706433) //localhost
  1200. {
  1201. printf("Stopping server...\n");
  1202. exit_flag = 2;
  1203. mg_printf(conn,"Goodbye.\n");
  1204. }
  1205. }
  1206. static void
  1207. run(struct mg_connection *conn,
  1208. const struct mg_request_info *ri, void *data)
  1209. {
  1210. send_ok(conn);
  1211. int n = 4;
  1212. std::string call = "";
  1213. while (ri->uri[++n])
  1214. {
  1215. int k = getnum(ri->uri+n);
  1216. while(ri->uri[n] && ri->uri[n] != '_') n++;
  1217. if (k<=0 || 255<k)
  1218. continue;
  1219. call += (char)k;
  1220. }
  1221. call += " 2>&1";
  1222. FILE* in;
  1223. printf("%s\n",call.c_str());
  1224. if ((in = popen(call.c_str(),"r")) == NULL)
  1225. return;
  1226. char buff[256];
  1227. bool empty = true;
  1228. while (fgets(buff, sizeof(buff), in) != NULL)
  1229. {
  1230. mg_printf(conn, "%s",buff);
  1231. empty = false;
  1232. }
  1233. if (empty)
  1234. mg_printf(conn,"</pre>empty<pre>");
  1235. pclose(in);
  1236. }
  1237. static void
  1238. intent(struct mg_connection *conn,
  1239. const struct mg_request_info *ri, void *data)
  1240. {
  1241. send_ok(conn);
  1242. int n = 7;
  1243. std::string call = "/system/bin/am start ";
  1244. while (ri->uri[++n])
  1245. {
  1246. int k = getnum(ri->uri+n);
  1247. while(ri->uri[n] && ri->uri[n] != '_') n++;
  1248. if (k<=0 || 255<k)
  1249. continue;
  1250. char ch = (char)k;
  1251. if ((ch >= 'a' && ch <= 'z') ||
  1252. (ch >= 'A' && ch <= 'Z') || ch == ' ')
  1253. call += ch;
  1254. else
  1255. {
  1256. call += '\\';
  1257. call += ch;
  1258. }
  1259. }
  1260. printf("%s\n",ri->uri+8);
  1261. printf("%s\n",call.c_str());
  1262. system(call.c_str());
  1263. }
  1264. static void
  1265. password(struct mg_connection *conn,
  1266. const struct mg_request_info *ri, void *data)
  1267. {
  1268. send_ok(conn);
  1269. if (ri->remote_ip==2130706433) //localhost
  1270. {
  1271. mg_printf(conn,admin_password.c_str());
  1272. }
  1273. }
  1274. int main(int argc, char **argv)
  1275. {
  1276. int port;
  1277. if (argc == 2)
  1278. port = strtol (argv[1], 0, 10);
  1279. else
  1280. port = 80;
  1281. if (port <= 0)
  1282. error("Invalid port\n");
  1283. int i;
  1284. for (i = strlen(argv[0])-1; i>=0; i--)
  1285. if (argv[0][i] == '/')
  1286. {
  1287. argv[0][i+1]=0;
  1288. break;
  1289. }
  1290. dir = argv[0];
  1291. dirdepth = -1;
  1292. for (i = 0; i < strlen(argv[0]); i++)
  1293. if (argv[0][i] == '/')
  1294. dirdepth++;
  1295. printf("%d\n",dirdepth);
  1296. if ((fbfd = open(FB_DEVICE, O_RDONLY)) == -1)
  1297. {
  1298. error("open framebuffer\n");
  1299. }
  1300. if (ioctl(fbfd, FBIOGET_VSCREENINFO, &scrinfo) != 0)
  1301. {
  1302. error("error reading screeninfo\n");
  1303. }
  1304. char buffer[8192];
  1305. (void) signal(SIGCHLD, signal_handler);
  1306. (void) signal(SIGTERM, signal_handler);
  1307. (void) signal(SIGINT, signal_handler);
  1308. if ((ctx = mg_start()) == NULL) {
  1309. error("Cannot initialize Mongoose context");
  1310. }
  1311. itoa(buffer,port);
  1312. if (mg_set_option(ctx, "ports", buffer) != 1)
  1313. error("Error in configurate port.\n");
  1314. mg_set_option(ctx, "aliases", "/sdcard=/sdcard");
  1315. passfile = "passwords.txt";
  1316. mg_set_option(ctx, "auth_realm", "webkey");
  1317. admin_password = "";
  1318. struct timeval tv;
  1319. gettimeofday(&tv,0);
  1320. srand ( time(NULL)+tv.tv_usec );
  1321. for (i = 0; i < 8; i++)
  1322. {
  1323. char c[2]; c[1] = 0; c[0] = (char)(rand()%26+97);
  1324. admin_password += c;
  1325. }
  1326. mg_modify_passwords_file(ctx, (dir+passfile).c_str(), "admin", admin_password.c_str());
  1327. mg_set_uri_callback(ctx, "/", &index, NULL);
  1328. mg_set_uri_callback(ctx, "/key*", &key, NULL);
  1329. mg_set_uri_callback(ctx, "/oldkey*", &key, NULL);
  1330. mg_set_uri_callback(ctx, "/touch*", &touch, NULL);
  1331. mg_set_uri_callback(ctx, "/savebuttons", &savebuttons, NULL);
  1332. mg_set_uri_callback(ctx, "/savekeys", &savekeys, NULL);
  1333. mg_set_uri_callback(ctx, "/button*", &button, NULL);
  1334. mg_set_uri_callback(ctx, "/config_buttons.html", &config_buttons, NULL);
  1335. mg_set_uri_callback(ctx, "/config_keys.html", &config_keys, NULL);
  1336. mg_set_uri_callback(ctx, "/screenshot.png*", &screenshot, NULL);
  1337. mg_set_uri_callback(ctx, "/intent_*", &intent, NULL);
  1338. mg_set_uri_callback(ctx, "/run_*", &run, NULL);
  1339. mg_set_uri_callback(ctx, "/stop", &stop, NULL);
  1340. mg_set_uri_callback(ctx, "/password", &password, NULL);
  1341. mg_set_uri_callback(ctx, "/config", &config, NULL);
  1342. mg_set_uri_callback(ctx, "/help.png", &helppng, NULL);
  1343. mg_set_uri_callback(ctx, "/help.html", &helphtml, NULL);
  1344. mg_set_option(ctx, "protect", (std::string("/password=,/stop=,/=")+dir+passfile).c_str());
  1345. // mg_set_option(ctx, "protect", (std::string("/password=,/stop=,/config=")+dir+passfile).c_str());
  1346. mg_set_option(ctx, "root","none");
  1347. printf("WebKey %s started on port(s) [%s], serving directory [%s]\n",
  1348. mg_version(),
  1349. mg_get_option(ctx, "ports"),
  1350. mg_get_option(ctx, "root"));
  1351. printf("starting uinput...\n");
  1352. init_uinput();
  1353. printf("starting touch...\n");
  1354. init_touch();
  1355. fflush(stdout);
  1356. while (exit_flag == 0)
  1357. sleep(1);
  1358. (void) printf("Exiting on signal %d, "
  1359. "waiting for all threads to finish...", exit_flag);
  1360. fflush(stdout);
  1361. mg_stop(ctx);
  1362. (void) printf("%s", " done.\n");
  1363. clear();
  1364. return (EXIT_SUCCESS);
  1365. return 0;
  1366. }