wikilib.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * Authors Daniel Mack <daniel@caiaq.de>
  5. * Holger Hans Peter Freyther <zecke@openmoko.org>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <stdlib.h>
  21. #include <inttypes.h>
  22. #include <wikilib.h>
  23. #include <guilib.h>
  24. #include <glyph.h>
  25. #include <history.h>
  26. #include <wl-keyboard.h>
  27. #include <input.h>
  28. #include <msg.h>
  29. #include <file-io.h>
  30. #include <search.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <stdio.h>
  34. #include <perf.h>
  35. #include <profile.h>
  36. #include <malloc-simple.h>
  37. #include <bmf.h>
  38. #include <lcd_buf_draw.h>
  39. #include <lcd.h>
  40. #include <tick.h>
  41. #include "delay.h"
  42. #include "search_hash.h"
  43. #include "restricted.h"
  44. #include "wiki_info.h"
  45. #ifndef INCLUDED_FROM_KERNEL
  46. #include "time.h"
  47. #else
  48. #include "samo.h"
  49. #endif
  50. //#include <t_services.h>
  51. //#include <kernel.h>
  52. //#include <wl-time.h>
  53. #define DBG_WL 0
  54. struct pos {
  55. unsigned int x;
  56. unsigned int y;
  57. };
  58. int last_display_mode = 0;
  59. int display_mode = DISPLAY_MODE_INDEX;
  60. static struct keyboard_key * pre_key= NULL;
  61. static unsigned int article_touch_down_handled = 0;
  62. static unsigned int touch_down_on_keyboard = 0;
  63. static unsigned int touch_down_on_list = 0;
  64. static struct pos article_touch_down_pos;
  65. static unsigned int touch_y_last_unreleased = 0;
  66. static unsigned int touch_y_last = 0;
  67. static unsigned long start_move_time = 0;
  68. static unsigned long last_unreleased_time = 0;
  69. int last_index_y_pos;
  70. int enter_touch_y_pos = -1;
  71. int last_history_y_pos;
  72. char * articleBuffer = 0;
  73. int articleLength = 0;
  74. unsigned char * membuffer = 0;
  75. int membuffersize = 0;
  76. int curBufferPos = 0;
  77. unsigned char *article_buf_pointer;
  78. //int is_rendering = 0;
  79. int last_selection = 0;
  80. unsigned long start_search_time, last_delete_time;
  81. int last_article_move_time,touch_y_last_article;
  82. int article_touch_count = 0;
  83. int touch_history = 0;
  84. extern unsigned long time_scroll_article_last;
  85. extern unsigned long time_scroll_article_start;
  86. extern long saved_idx_article;
  87. extern long saved_prev_idx_article;
  88. bool random_press = false;
  89. extern int stop_render_article;
  90. int time_random_last = 0;
  91. extern bool search_string_changed;
  92. extern unsigned int time_search_last;
  93. extern int b_show_scroll_bar;
  94. extern int display_first_page;
  95. bool press_delete_button = false;
  96. extern bool search_string_changed_remove;
  97. int history_touch_pos_y_last;
  98. int touch_search = 0,search_touch_pos_y_last=0;
  99. bool article_moved = false;
  100. int article_scroll_pixel = INITIAL_ARTICLE_SCROLL_THRESHOLD;
  101. int article_moved_pixels = 0;
  102. extern int link_to_be_inverted;
  103. extern int link_currently_inverted;
  104. long finger_move_speed = 0;
  105. #ifdef OVER_SCROLL_ENABLED
  106. extern int over_scroll_lines;
  107. #endif
  108. int finger_touched = 0;
  109. void repaint_search(void)
  110. {
  111. guilib_fb_lock();
  112. search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_KEEP_REFRESH);
  113. keyboard_paint();
  114. guilib_fb_unlock();
  115. }
  116. /* this is only called for the index page */
  117. static void toggle_soft_keyboard(void)
  118. {
  119. //guilib_fb_lock();
  120. int mode = keyboard_get_mode();
  121. /* Set the keyboard mode to what we want to change to. */
  122. if (mode == KEYBOARD_NONE || search_result_count()==0) {
  123. keyboard_set_mode(KEYBOARD_CHAR);
  124. if (mode == KEYBOARD_NONE)
  125. restore_search_list_page();
  126. keyboard_paint();
  127. } else {
  128. keyboard_set_mode(KEYBOARD_NONE);
  129. search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_KEEP_RESULT);
  130. }
  131. //guilib_fb_unlock();
  132. }
  133. static void print_intro()
  134. {
  135. //keyboard_set_mode(KEYBOARD_CHAR);
  136. guilib_fb_lock();
  137. guilib_clear();
  138. //membuffer = malloc_simple(1024*1024,MEM_TAG_ARTICLE_F1);
  139. keyboard_paint();
  140. guilib_fb_unlock();
  141. }
  142. static unsigned int s_article_y_pos;
  143. static uint32_t s_article_offset = 0;
  144. void invert_selection(int old_pos, int new_pos, int start_pos, int height)
  145. {
  146. guilib_fb_lock();
  147. if (old_pos != -1)
  148. guilib_invert(start_pos - 2 + old_pos * height, height);
  149. if (new_pos != -1)
  150. guilib_invert(start_pos - 2 + new_pos * height, height);
  151. guilib_fb_unlock();
  152. }
  153. void invert_area(int start_x, int start_y, int end_x, int end_y)
  154. {
  155. guilib_fb_lock();
  156. guilib_invert_area(start_x,start_y,end_x,end_y);
  157. guilib_fb_unlock();
  158. }
  159. int article_open(const char *article)
  160. {
  161. DP(DBG_WL, ("O article_open() '%s'\n", article));
  162. s_article_offset = strtoul(article, 0 /* endptr */, 16 /* base */);
  163. s_article_y_pos = 0;
  164. return 0;
  165. }
  166. #if 0
  167. // this is no longer used
  168. void article_display(enum article_nav nav)
  169. {
  170. unsigned int screen_height = guilib_framebuffer_height();
  171. DP(DBG_WL, ("O article_display() %i article_offset %u article_y_pos %u\n", nav, s_article_offset, s_article_y_pos));
  172. if (nav == ARTICLE_PAGE_NEXT)
  173. s_article_y_pos += screen_height;
  174. else if (nav == ARTICLE_PAGE_PREV)
  175. s_article_y_pos = (s_article_y_pos <= screen_height) ? 0 : s_article_y_pos - screen_height;
  176. // wom_draw(g_womh, s_article_offset, framebuffer, s_article_y_pos, screen_height);
  177. }
  178. #endif
  179. #if 0
  180. // this is no longer used
  181. int article_open_pcf(const char *filename)
  182. {
  183. FILE * pFile;
  184. long lSize;
  185. size_t result;
  186. pFile = fopen (filename , "rb" );
  187. if (pFile==NULL)
  188. return -1;
  189. fseek (pFile , 0 , SEEK_END);
  190. lSize = ftell (pFile);
  191. rewind (pFile);
  192. // allocate memory to contain the whole file:
  193. articleBuffer = (char*) malloc (sizeof(char)*lSize);
  194. if (articleBuffer == NULL)
  195. {
  196. fclose(pFile);
  197. return -1;
  198. }
  199. // copy the file into the buffer:
  200. result = fread (articleBuffer,1,lSize,pFile);
  201. if (result != lSize)
  202. {
  203. fclose(pFile);
  204. return -1;
  205. }
  206. // terminate
  207. fclose (pFile);
  208. framebuffer_article=(char*)malloc(guilib_framebuffer_width()*nLine);
  209. memset(framebuffer_article,0,guilib_framebuffer_width()*nLine);
  210. render_article(articleBuffer,articleLength,framebuffer_article);
  211. return lSize;
  212. }
  213. #endif
  214. void article_display_pcf(int yPixel)
  215. {
  216. int pos;
  217. int copysize;
  218. int framebuffersize;
  219. framebuffersize = guilib_framebuffer_size();
  220. pos = curBufferPos+((yPixel*LCD_VRAM_WIDTH_PIXELS)/8);
  221. if(pos<0 || pos>membuffersize)
  222. {
  223. return;
  224. }
  225. copysize = membuffersize-pos;
  226. if(copysize>framebuffersize)
  227. copysize = framebuffersize;
  228. else
  229. {
  230. return;
  231. }
  232. guilib_fb_lock();
  233. guilib_clear();
  234. memcpy(framebuffer,membuffer+pos,copysize);
  235. guilib_fb_unlock();
  236. curBufferPos = pos;
  237. }
  238. static void handle_search_key(char keycode, unsigned long ev_time)
  239. {
  240. int rc = 0;
  241. if (keycode == WL_KEY_NLS) {
  242. #ifdef INCLUDED_FROM_KERNEL
  243. delay_us(100000);
  244. #endif
  245. display_mode = DISPLAY_MODE_WIKI_SELECTION;
  246. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  247. keyboard_set_mode(KEYBOARD_NONE);
  248. wiki_selection();
  249. return;
  250. } else if (keycode == WL_KEY_BACKSPACE) {
  251. rc = search_remove_char(0, ev_time);
  252. } else if (is_supported_search_char(keycode)) {
  253. rc = search_add_char(tolower(keycode), ev_time);
  254. } else {
  255. if(keycode == -42) // toggling keyboard will be handled at key down
  256. {
  257. rc = -1;
  258. }
  259. else
  260. {
  261. return;
  262. }
  263. }
  264. guilib_fb_lock();
  265. if (!rc)
  266. search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_NO_POPULATE);
  267. //
  268. // keyboard_paint();
  269. guilib_fb_unlock();
  270. }
  271. static void handle_cursor(struct wl_input_event *ev)
  272. {
  273. DP(DBG_WL, ("O handle_cursor()\n"));
  274. if (display_mode == DISPLAY_MODE_ARTICLE) {
  275. if (ev->key_event.keycode == WL_INPUT_KEY_CURSOR_DOWN)
  276. display_article_with_pcf(50);
  277. else if (ev->key_event.keycode == WL_INPUT_KEY_CURSOR_UP)
  278. display_article_with_pcf(-50);
  279. } else if (display_mode == DISPLAY_MODE_INDEX && keyboard_get_mode() == KEYBOARD_NONE) {
  280. if (ev->key_event.keycode == WL_INPUT_KEY_CURSOR_DOWN)
  281. display_article_with_pcf(50);
  282. else if (ev->key_event.keycode == WL_INPUT_KEY_CURSOR_UP)
  283. display_article_with_pcf(-50);
  284. } else if (display_mode == DISPLAY_MODE_HISTORY && keyboard_get_mode() == KEYBOARD_NONE) {
  285. if (ev->key_event.keycode == WL_INPUT_KEY_CURSOR_DOWN)
  286. display_article_with_pcf(50);
  287. else if (ev->key_event.keycode == WL_INPUT_KEY_CURSOR_UP)
  288. display_article_with_pcf(-50);
  289. }
  290. }
  291. extern int restricted_article;
  292. static void handle_key_release(int keycode)
  293. {
  294. // static long idx_article = 0;
  295. static int first_time_random = 0;
  296. int mode;
  297. finger_move_speed = 0;
  298. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0); // reset invert immediately
  299. DP(DBG_WL, ("O handle_key_release()\n"));
  300. mode = keyboard_get_mode();
  301. if (keycode == WL_INPUT_KEY_POWER) {
  302. #ifdef INCLUDED_FROM_KERNEL
  303. if (history_list_save(HISTORY_SAVE_POWER_OFF))
  304. {
  305. delay_us(200000);
  306. }
  307. #endif
  308. } else if (keycode == WL_INPUT_KEY_SEARCH) {
  309. article_buf_pointer = NULL;
  310. /* back to search */
  311. if (display_mode == DISPLAY_MODE_INDEX) {
  312. toggle_soft_keyboard();
  313. } else {
  314. search_set_selection(-1);
  315. display_mode = DISPLAY_MODE_INDEX;
  316. keyboard_set_mode(KEYBOARD_CHAR);
  317. repaint_search();
  318. }
  319. } else if (keycode == WL_INPUT_KEY_HISTORY) {
  320. if (display_mode != DISPLAY_MODE_HISTORY) {
  321. article_buf_pointer = NULL;
  322. history_reload();
  323. display_mode = DISPLAY_MODE_HISTORY;
  324. keyboard_set_mode(KEYBOARD_NONE);
  325. } else {
  326. if (keyboard_get_mode() == KEYBOARD_CLEAR_HISTORY)
  327. {
  328. keyboard_set_mode(KEYBOARD_NONE);
  329. guilib_fb_lock();
  330. //keyboard_paint();
  331. draw_clear_history(1);
  332. guilib_fb_unlock();
  333. } else if (history_get_count() > 0 && display_first_page) {
  334. keyboard_set_mode(KEYBOARD_CLEAR_HISTORY);
  335. guilib_fb_lock();
  336. //keyboard_paint();
  337. draw_clear_history(0);
  338. guilib_fb_unlock();
  339. }
  340. }
  341. } else if (keycode == WL_INPUT_KEY_RANDOM) {
  342. if (first_time_random < 4)
  343. first_time_random++;
  344. if (first_time_random == 3)
  345. {
  346. first_time_random = 4;
  347. if (init_article_filter())
  348. return;
  349. }
  350. article_buf_pointer = NULL;
  351. display_mode = DISPLAY_MODE_ARTICLE;
  352. last_display_mode = DISPLAY_MODE_INDEX;
  353. random_article();
  354. } else if (display_mode == DISPLAY_MODE_INDEX) {
  355. article_buf_pointer = NULL;
  356. if (keycode == WL_KEY_RETURN) {
  357. int cur_selection = search_current_selection();
  358. retrieve_article(cur_selection);
  359. #ifdef PROFILER_ON
  360. } else if (keycode == WL_KEY_HASH) {
  361. /* activate if you want to run performance tests */
  362. /* perf_test(); */
  363. malloc_status_simple();
  364. prof_print();
  365. #endif
  366. } else {
  367. handle_search_key(keycode, get_time_ticks());
  368. }
  369. } else if (display_mode == DISPLAY_MODE_ARTICLE) {
  370. // article_buf_pointer = NULL;
  371. if (keycode == WL_KEY_BACKSPACE) {
  372. if (last_display_mode == DISPLAY_MODE_INDEX) {
  373. display_mode = DISPLAY_MODE_INDEX;
  374. repaint_search();
  375. } else if (last_display_mode == DISPLAY_MODE_HISTORY) {
  376. display_mode = DISPLAY_MODE_HISTORY;
  377. history_reload();
  378. }
  379. }
  380. }
  381. }
  382. static void average_xy(int *average_x, int *average_y, int last_5_x[], int last_5_y[], unsigned long last_5_y_time_ticks[])
  383. {
  384. int i;
  385. int xy_count = 1;
  386. *average_x = last_5_x[0];
  387. *average_y = last_5_y[0];
  388. for (i = 1; i < 5; i++)
  389. {
  390. if (last_5_x[i] >= 0 && time_diff(last_5_y_time_ticks[i], last_5_y_time_ticks[0]) < seconds_to_ticks(LINK_ACTIVATION_TIME_THRESHOLD))
  391. {
  392. xy_count++;
  393. *average_x += last_5_x[i];
  394. *average_y += last_5_y[i];
  395. }
  396. }
  397. *average_x /= xy_count;
  398. *average_y /= xy_count;
  399. }
  400. static void handle_touch(struct wl_input_event *ev)
  401. {
  402. //int offset,offset_count,
  403. int article_link_number=-1;
  404. int enter_touch_y_pos_record;
  405. //int time_diff_search;
  406. int mode;
  407. struct keyboard_key * key;
  408. static int last_5_x[5];
  409. static int last_5_y[5];
  410. static unsigned long last_5_y_time_ticks[5];
  411. int i;
  412. int average_x, average_y;
  413. long time_now;
  414. DP(DBG_WL, ("%s() touch event @%d,%d val %d\n", __func__,
  415. ev->touch_event.x, ev->touch_event.y, ev->touch_event.value));
  416. mode = keyboard_get_mode();
  417. if (display_mode == DISPLAY_MODE_INDEX && (mode == KEYBOARD_CHAR || mode == KEYBOARD_CHAR2 || mode == KEYBOARD_NUM))
  418. {
  419. article_buf_pointer = NULL;
  420. if (!touch_down_on_keyboard && !touch_down_on_list)
  421. {
  422. last_5_x[0] = ev->touch_event.x;
  423. last_5_y[0] = ev->touch_event.y;
  424. last_5_y_time_ticks[0] = ev->touch_event.ticks;
  425. for (i = 1; i < 5; i++)
  426. {
  427. last_5_x[i] = -1;
  428. last_5_y[i] = -1;
  429. }
  430. }
  431. else
  432. {
  433. for (i = 4; i >= 1; i--)
  434. {
  435. last_5_x[i] = last_5_x[i-1];
  436. last_5_y[i] = last_5_y[i-1];
  437. last_5_y_time_ticks[i] = last_5_y_time_ticks[i-1];
  438. }
  439. last_5_x[0] = ev->touch_event.x;
  440. last_5_y[0] = ev->touch_event.y;
  441. last_5_y_time_ticks[0] = ev->touch_event.ticks;
  442. }
  443. average_xy(&average_x, &average_y, last_5_x, last_5_y, last_5_y_time_ticks);
  444. key = keyboard_get_data(average_x, average_y);
  445. if (ev->touch_event.value == 0) {
  446. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_DELAY, ev->touch_event.ticks); // reset invert with delay
  447. enter_touch_y_pos_record = enter_touch_y_pos;
  448. enter_touch_y_pos = -1;
  449. touch_search = 0;
  450. if (pre_key && pre_key != key && keyboard_adjacent_keys(pre_key, key))
  451. {
  452. touch_down_on_keyboard = 0;
  453. touch_down_on_list = 0;
  454. pre_key = NULL;
  455. goto out;
  456. }
  457. pre_key = NULL;
  458. if (press_delete_button)
  459. {
  460. touch_down_on_keyboard = 0;
  461. touch_down_on_list = 0;
  462. press_delete_button = false;
  463. goto out;
  464. }
  465. if (key) {
  466. if (!touch_down_on_keyboard) {
  467. touch_down_on_keyboard = 0;
  468. touch_down_on_list = 0;
  469. goto out;
  470. }
  471. handle_search_key(key->key, ev->touch_event.ticks);
  472. }
  473. else {
  474. if (!touch_down_on_list || ev->touch_event.y < RESULT_START - RESULT_HEIGHT) {
  475. touch_down_on_keyboard = 0;
  476. touch_down_on_list = 0;
  477. goto out;
  478. }
  479. if(search_result_count()==0)
  480. goto out;
  481. //search_set_selection(last_selection);
  482. //search_open_article(last_selection);
  483. if(search_result_selected()>=0)
  484. {
  485. display_mode = DISPLAY_MODE_ARTICLE;
  486. last_display_mode = DISPLAY_MODE_INDEX;
  487. search_open_article(search_result_selected());
  488. }
  489. }
  490. touch_down_on_keyboard = 0;
  491. touch_down_on_list = 0;
  492. } else {
  493. if (pre_key && pre_key->key != key->key && keyboard_adjacent_keys(pre_key, key))
  494. {
  495. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  496. goto out;
  497. }
  498. if(enter_touch_y_pos<0) //record first touch y pos
  499. enter_touch_y_pos = ev->touch_event.y;
  500. last_index_y_pos = ev->touch_event.y;
  501. start_search_time = ev->touch_event.ticks;
  502. last_delete_time = start_search_time;
  503. if (key) {
  504. if(key->key==8)//press "<" button
  505. {
  506. if (!press_delete_button)
  507. {
  508. press_delete_button = true;
  509. if(get_search_string_len()>0)
  510. {
  511. time_now = get_time_ticks();
  512. if (!search_remove_char(0, time_now))
  513. {
  514. search_string_changed_remove = true;
  515. search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_NO_POPULATE);
  516. }
  517. last_delete_time = time_now;
  518. }
  519. }
  520. }
  521. else if(key->key == -42)
  522. {
  523. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  524. if (!touch_down_on_keyboard)
  525. {
  526. mode = keyboard_get_mode();
  527. if(mode == KEYBOARD_CHAR)
  528. keyboard_set_mode(KEYBOARD_NUM);
  529. else if(mode == KEYBOARD_NUM)
  530. {
  531. if (wiki_is_japanese())
  532. keyboard_set_mode(KEYBOARD_CHAR2);
  533. else
  534. keyboard_set_mode(KEYBOARD_CHAR);
  535. } else if (mode == KEYBOARD_CHAR2)
  536. keyboard_set_mode(KEYBOARD_CHAR);
  537. guilib_fb_lock();
  538. keyboard_paint();
  539. guilib_fb_unlock();
  540. }
  541. }
  542. if (!touch_down_on_keyboard && !touch_down_on_list)
  543. touch_down_on_keyboard = 1;
  544. if (pre_key && pre_key->key == key->key) goto out;
  545. if (touch_down_on_keyboard) {
  546. keyboard_key_invert(key);
  547. if (pre_key && !keyboard_adjacent_keys(pre_key, key))
  548. {
  549. handle_search_key(pre_key->key, ev->touch_event.ticks);
  550. pre_key = NULL;
  551. }
  552. else
  553. pre_key = key;
  554. }
  555. } else {
  556. if (!touch_down_on_keyboard && !touch_down_on_list)
  557. touch_down_on_list = 1;
  558. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_DELAY, ev->touch_event.ticks); // reset invert with delay
  559. pre_key = NULL;
  560. if (!search_result_count()) goto out;
  561. if(touch_search == 0)
  562. {
  563. //last_search_y_pos = ev->touch_event.y;
  564. touch_search = 1;
  565. }
  566. else
  567. {
  568. if(search_result_selected()>=0 && abs(ev->touch_event.y-search_touch_pos_y_last)>5)
  569. {
  570. invert_selection(search_result_selected(),-1, RESULT_START, RESULT_HEIGHT);
  571. search_set_selection(-1);
  572. }
  573. goto out;
  574. }
  575. int new_selection;
  576. if((ev->touch_event.y - RESULT_START)<0)
  577. new_selection = -1;
  578. else
  579. new_selection = ((unsigned int)ev->touch_event.y - RESULT_START) / RESULT_HEIGHT;
  580. if (new_selection == search_result_selected()) goto out;
  581. unsigned int avail_count = keyboard_get_mode() == KEYBOARD_NONE ?
  582. NUMBER_OF_FIRST_PAGE_RESULTS : NUMBER_OF_RESULTS_KEYBOARD;
  583. avail_count = search_result_count() > avail_count ? avail_count : search_result_count();
  584. if (new_selection >= avail_count) goto out;
  585. if (touch_down_on_keyboard) goto out;
  586. //invert_selection(search_result_selected(), new_selection, RESULT_START, RESULT_HEIGHT);
  587. invert_selection(-1, new_selection, RESULT_START, RESULT_HEIGHT);
  588. last_selection = new_selection ;
  589. search_set_selection(new_selection);
  590. search_touch_pos_y_last = ev->touch_event.y;
  591. }
  592. }
  593. }
  594. else if (display_mode == DISPLAY_MODE_HISTORY && mode == KEYBOARD_CLEAR_HISTORY)
  595. {
  596. key = keyboard_get_data(ev->touch_event.x, ev->touch_event.y);
  597. if (ev->touch_event.value == 0) {
  598. #ifdef INCLUDED_FROM_KERNEL
  599. delay_us(100000 * 2);
  600. #endif
  601. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  602. enter_touch_y_pos_record = enter_touch_y_pos;
  603. enter_touch_y_pos = -1;
  604. touch_search = 0;
  605. pre_key = NULL;
  606. if (press_delete_button)
  607. {
  608. press_delete_button = false;
  609. goto out;
  610. }
  611. if (key) {
  612. if (!touch_down_on_keyboard) {
  613. touch_down_on_keyboard = 0;
  614. touch_down_on_list = 0;
  615. goto out;
  616. }
  617. if (key->key == 'Y') {
  618. history_clear();
  619. keyboard_set_mode(KEYBOARD_NONE);
  620. history_reload();
  621. } else if (key->key == 'N') {
  622. keyboard_set_mode(KEYBOARD_NONE);
  623. guilib_fb_lock();
  624. draw_clear_history(1);
  625. guilib_fb_unlock();
  626. }
  627. }
  628. else {
  629. touch_down_on_keyboard = 0;
  630. touch_down_on_list = 0;
  631. goto out;
  632. }
  633. } else {
  634. if(enter_touch_y_pos<0) //record first touch y pos
  635. enter_touch_y_pos = ev->touch_event.y;
  636. last_index_y_pos = ev->touch_event.y;
  637. if (key) {
  638. if (!touch_down_on_keyboard)
  639. touch_down_on_keyboard = 1;
  640. if (pre_key && pre_key->key == key->key) goto out;
  641. if (touch_down_on_keyboard) {
  642. keyboard_key_invert(key);
  643. if (pre_key && !keyboard_adjacent_keys(pre_key, key))
  644. {
  645. handle_search_key(pre_key->key, ev->touch_event.ticks);
  646. pre_key = NULL;
  647. }
  648. else
  649. pre_key = key;
  650. }
  651. } else {
  652. touch_down_on_keyboard = 0;
  653. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_DELAY, ev->touch_event.ticks); // reset invert with delay
  654. pre_key = NULL;
  655. }
  656. }
  657. }
  658. else if (display_mode == DISPLAY_MODE_RESTRICTED)
  659. {
  660. key = keyboard_get_data(ev->touch_event.x, ev->touch_event.y);
  661. if (ev->touch_event.value == 0) {
  662. if (key->key == 'Y' || key->key == 'N' || key->key == 'P')
  663. {
  664. #ifdef INCLUDED_FROM_KERNEL
  665. delay_us(100000 * 2);
  666. #endif
  667. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  668. }
  669. else
  670. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_DELAY, ev->touch_event.ticks); // reset invert with delay
  671. enter_touch_y_pos_record = enter_touch_y_pos;
  672. enter_touch_y_pos = -1;
  673. touch_search = 0;
  674. if (pre_key && pre_key != key && keyboard_adjacent_keys(pre_key, key))
  675. {
  676. touch_down_on_keyboard = 0;
  677. touch_down_on_list = 0;
  678. pre_key = NULL;
  679. goto out;
  680. }
  681. pre_key = NULL;
  682. if (press_delete_button)
  683. {
  684. touch_down_on_keyboard = 0;
  685. touch_down_on_list = 0;
  686. press_delete_button = false;
  687. goto out;
  688. }
  689. if (key) {
  690. if (!touch_down_on_keyboard) {
  691. touch_down_on_keyboard = 0;
  692. goto out;
  693. }
  694. handle_password_key(key->key);
  695. }
  696. touch_down_on_keyboard = 0;
  697. } else {
  698. if (pre_key && pre_key->key != key->key && keyboard_adjacent_keys(pre_key, key))
  699. {
  700. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  701. goto out;
  702. }
  703. if(enter_touch_y_pos<0) //record first touch y pos
  704. enter_touch_y_pos = ev->touch_event.y;
  705. last_index_y_pos = ev->touch_event.y;
  706. start_search_time = ev->touch_event.ticks;
  707. last_delete_time = start_search_time;
  708. if (key) {
  709. if(key->key==8)//press "<" button
  710. {
  711. if (!press_delete_button)
  712. {
  713. press_delete_button = true;
  714. if (get_password_string_len()>0)
  715. {
  716. time_now = get_time_ticks();
  717. password_remove_char();
  718. last_delete_time = time_now;
  719. }
  720. }
  721. }
  722. else if(key->key == -42)
  723. {
  724. mode = keyboard_get_mode();
  725. if(mode == KEYBOARD_PASSWORD_CHAR)
  726. keyboard_set_mode(KEYBOARD_PASSWORD_NUM);
  727. else if(mode == KEYBOARD_PASSWORD_NUM)
  728. keyboard_set_mode(KEYBOARD_PASSWORD_CHAR);
  729. guilib_fb_lock();
  730. keyboard_paint();
  731. guilib_fb_unlock();
  732. }
  733. if (!touch_down_on_keyboard)
  734. touch_down_on_keyboard = 1;
  735. if (pre_key && pre_key->key == key->key) goto out;
  736. if (touch_down_on_keyboard) {
  737. keyboard_key_invert(key);
  738. pre_key = key;
  739. }
  740. } else {
  741. keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_NOW, 0);
  742. pre_key = NULL;
  743. search_touch_pos_y_last = ev->touch_event.y;
  744. }
  745. }
  746. } else {
  747. if (ev->touch_event.value == 0) {
  748. unsigned long diff_ticks = 0;
  749. long diff_y = 0;
  750. finger_touched = 0;
  751. time_scroll_article_last = ev->touch_event.ticks;
  752. for (i = 4; i > 0; i--)
  753. {
  754. if (last_5_y[i] >= 0)
  755. {
  756. diff_y = last_5_y[i] - ev->touch_event.y;
  757. diff_ticks = time_diff(ev->touch_event.ticks, last_5_y_time_ticks[i]);
  758. break;
  759. }
  760. }
  761. if (diff_ticks <= 0 || abs(article_moved_pixels) > SMOOTH_SCROLL_ACTIVATION_OFFSET_HIGH_THRESHOLD ||
  762. abs(article_moved_pixels) < SMOOTH_SCROLL_ACTIVATION_OFFSET_LOW_THRESHOLD)
  763. finger_move_speed = 0;
  764. else
  765. {
  766. // Event timesing is not good for short period due to the events are queued without timestamp
  767. finger_move_speed = (float)diff_y * ((float)seconds_to_ticks(1) / (float)diff_ticks);
  768. if (abs(finger_move_speed) > SMOOTH_SCROLL_ACTIVATION_SPPED_THRESHOLD)
  769. {
  770. if (finger_move_speed > 0)
  771. {
  772. if (display_mode == DISPLAY_MODE_ARTICLE)
  773. finger_move_speed = ARTICLE_SMOOTH_SCROLL_SPEED_FACTOR *
  774. (finger_move_speed - SMOOTH_SCROLL_ACTIVATION_SPPED_THRESHOLD);
  775. else
  776. finger_move_speed = LIST_SMOOTH_SCROLL_SPEED_FACTOR *
  777. (finger_move_speed - SMOOTH_SCROLL_ACTIVATION_SPPED_THRESHOLD);
  778. }
  779. else
  780. {
  781. if (display_mode == DISPLAY_MODE_ARTICLE)
  782. finger_move_speed = ARTICLE_SMOOTH_SCROLL_SPEED_FACTOR *
  783. (finger_move_speed + SMOOTH_SCROLL_ACTIVATION_SPPED_THRESHOLD);
  784. else
  785. finger_move_speed = LIST_SMOOTH_SCROLL_SPEED_FACTOR *
  786. (finger_move_speed + SMOOTH_SCROLL_ACTIVATION_SPPED_THRESHOLD);
  787. }
  788. }
  789. else
  790. finger_move_speed = 0;
  791. }
  792. article_moved = false;
  793. if (finger_move_speed == 0 && b_show_scroll_bar)
  794. {
  795. b_show_scroll_bar = 0;
  796. show_scroll_bar(0); // clear scroll bar
  797. }
  798. article_scroll_pixel = INITIAL_ARTICLE_SCROLL_THRESHOLD;
  799. article_moved_pixels = 0;
  800. touch_y_last_unreleased = 0;
  801. start_move_time = 0;
  802. article_link_number = get_activated_article_link_number();
  803. if(article_link_number>=0)
  804. {
  805. if (link_to_be_inverted >= 0)
  806. {
  807. if (link_currently_inverted >= 0)
  808. invert_link(link_currently_inverted);
  809. invert_link(link_to_be_inverted);
  810. }
  811. if (finger_move_speed == 0)
  812. {
  813. init_invert_link();
  814. last_display_mode = display_mode;
  815. if (display_mode == DISPLAY_MODE_WIKI_SELECTION)
  816. {
  817. set_wiki(article_link_number);
  818. search_init();
  819. search_set_selection(-1);
  820. display_mode = DISPLAY_MODE_INDEX;
  821. keyboard_set_mode(KEYBOARD_CHAR);
  822. repaint_search();
  823. }
  824. else
  825. {
  826. if (display_mode == DISPLAY_MODE_ARTICLE)
  827. {
  828. saved_prev_idx_article = saved_idx_article;
  829. if (!(saved_prev_idx_article & 0xFF000000)) // idx_article for current wiki
  830. {
  831. saved_prev_idx_article |= get_wiki_id_from_idx(nCurrentWiki) << 24;
  832. }
  833. }
  834. else
  835. saved_prev_idx_article = 0;
  836. display_mode = DISPLAY_MODE_ARTICLE;
  837. open_article_link_with_link_number(article_link_number);
  838. }
  839. }
  840. else
  841. {
  842. if (link_currently_inverted >= 0)
  843. invert_link(link_currently_inverted);
  844. init_invert_link();
  845. }
  846. return;
  847. }
  848. reset_article_link_number();
  849. article_touch_down_handled = 0;
  850. } else {
  851. finger_touched = 1;
  852. finger_move_speed = 0;
  853. if(touch_y_last_unreleased == 0)
  854. {
  855. touch_y_last_unreleased = ev->touch_event.y;
  856. touch_y_last = ev->touch_event.y;
  857. last_unreleased_time = ev->touch_event.ticks;
  858. reset_article_link_number();
  859. article_moved_pixels = 0;
  860. last_5_x[0] = ev->touch_event.x;
  861. last_5_y[0] = ev->touch_event.y;
  862. last_5_y_time_ticks[0] = ev->touch_event.ticks;
  863. for (i = 1; i < 5; i++)
  864. {
  865. last_5_x[i] = -1;
  866. last_5_y[i] = -1;
  867. }
  868. }
  869. else
  870. {
  871. article_moved_pixels += touch_y_last - ev->touch_event.y;
  872. if(article_moved || abs(article_moved_pixels) > article_scroll_pixel)
  873. {
  874. if (!article_moved)
  875. {
  876. article_moved = true;
  877. reset_article_link_number();
  878. if (article_moved_pixels > 0)
  879. article_moved_pixels -= article_scroll_pixel;
  880. else
  881. article_moved_pixels += article_scroll_pixel;
  882. display_article_with_pcf(article_moved_pixels);
  883. article_scroll_pixel = ARTICLE_MOVING_SCROLL_THRESHOLD;
  884. }
  885. else if (finger_move_speed == 0)
  886. {
  887. display_article_with_pcf(touch_y_last - ev->touch_event.y);
  888. }
  889. for (i = 4; i >= 1; i--)
  890. {
  891. last_5_x[i] = last_5_x[i-1];
  892. last_5_y[i] = last_5_y[i-1];
  893. last_5_y_time_ticks[i] = last_5_y_time_ticks[i-1];
  894. }
  895. last_5_x[0] = ev->touch_event.x;
  896. last_5_y[0] = ev->touch_event.y;
  897. last_5_y_time_ticks[0] = ev->touch_event.ticks;
  898. b_show_scroll_bar = 1;
  899. }
  900. touch_y_last = ev->touch_event.y;
  901. }
  902. if (!article_moved && get_activated_article_link_number() < 0)
  903. {
  904. average_xy(&average_x, &average_y, last_5_x, last_5_y, last_5_y_time_ticks);
  905. article_link_number = isArticleLinkSelected(average_x, average_y);
  906. if (article_link_number >= 0)
  907. set_article_link_number(article_link_number, ev->touch_event.ticks);
  908. }
  909. if (!article_touch_down_handled) {
  910. article_touch_down_pos.x = ev->touch_event.x;
  911. article_touch_down_pos.y = ev->touch_event.y;
  912. article_touch_down_handled = 1;
  913. }
  914. }
  915. }
  916. out:
  917. return;
  918. }
  919. int wikilib_init (void)
  920. {
  921. init_lcd_draw_buf();
  922. init_file_buffer();
  923. init_wiki_info();
  924. return 0;
  925. }
  926. #ifndef INCLUDED_FROM_KERNEL
  927. extern long idx_init_article;
  928. #endif
  929. int wikilib_run(void)
  930. {
  931. int sleep;
  932. long time_now;
  933. struct wl_input_event ev;
  934. int more_events = 0;
  935. unsigned long last_event_time = 0;
  936. int rc;
  937. char *pMsg;
  938. /*
  939. * test searching code...
  940. */
  941. article_buf_pointer = NULL;
  942. search_init();
  943. history_list_init();
  944. malloc_status_simple();
  945. print_intro();
  946. #ifndef INCLUDED_FROM_KERNEL
  947. if (!load_init_article(idx_init_article))
  948. {
  949. display_mode = DISPLAY_MODE_ARTICLE;
  950. last_display_mode = DISPLAY_MODE_INDEX;
  951. }
  952. #endif
  953. pMsg = get_nls_text("type_a_word");
  954. render_string(SUBTITLE_FONT_IDX, -1, 55, pMsg, strlen(pMsg), 0);
  955. for (;;) {
  956. if (more_events)
  957. sleep = 0;
  958. else
  959. sleep = 1;
  960. if (!more_events && display_mode == DISPLAY_MODE_ARTICLE && render_article_with_pcf())
  961. sleep = 0;
  962. else if (!more_events && display_mode == DISPLAY_MODE_INDEX && render_search_result_with_pcf())
  963. sleep = 0;
  964. else if (!more_events && display_mode == DISPLAY_MODE_HISTORY && render_history_with_pcf())
  965. sleep = 0;
  966. else if (!more_events && display_mode == DISPLAY_MODE_WIKI_SELECTION && render_wiki_selection_with_pcf())
  967. sleep = 0;
  968. if (finger_move_speed && !finger_touched)
  969. {
  970. scroll_article();
  971. sleep = 0;
  972. }
  973. #ifdef INCLUDED_FROM_KERNEL
  974. time_now = get_time_ticks();
  975. if(display_mode == DISPLAY_MODE_INDEX)
  976. {
  977. if (press_delete_button && get_search_string_len()>0)
  978. {
  979. sleep = 0;
  980. if(time_diff(time_now, start_search_time) > seconds_to_ticks(1.5))
  981. {
  982. if (!clear_search_string())
  983. {
  984. search_string_changed_remove = true;
  985. search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_NO_POPULATE);
  986. }
  987. press_delete_button = false;
  988. }
  989. else if (time_diff(time_now, start_search_time) > seconds_to_ticks(0.3) &&
  990. time_diff(time_now, last_delete_time) > seconds_to_ticks(0.2))
  991. {
  992. if (!search_remove_char(0, time_now))
  993. {
  994. search_string_changed_remove = true;
  995. search_to_be_reloaded(SEARCH_TO_BE_RELOADED_SET, SEARCH_RELOAD_NO_POPULATE);
  996. }
  997. last_delete_time = time_now;
  998. }
  999. }
  1000. }
  1001. else if (display_mode == DISPLAY_MODE_RESTRICTED)
  1002. {
  1003. if (press_delete_button && get_password_string_len()>0)
  1004. {
  1005. sleep = 0;
  1006. time_now = get_time_ticks();
  1007. if(time_diff(time_now, start_search_time) > seconds_to_ticks(1.5))
  1008. {
  1009. clear_password_string();
  1010. press_delete_button = false;
  1011. }
  1012. else if (time_diff(time_now, start_search_time) > seconds_to_ticks(0.3) &&
  1013. time_diff(time_now, last_delete_time) > seconds_to_ticks(0.2))
  1014. {
  1015. password_remove_char();
  1016. last_delete_time = time_now;
  1017. }
  1018. }
  1019. }
  1020. #endif
  1021. if (!more_events && display_mode == DISPLAY_MODE_INDEX && fetch_search_result(0, 0, 0))
  1022. {
  1023. sleep = 0;
  1024. }
  1025. if (!more_events && display_mode == DISPLAY_MODE_INDEX && check_search_string_change())
  1026. {
  1027. sleep = 0;
  1028. }
  1029. if (keyboard_key_reset_invert(KEYBOARD_RESET_INVERT_CHECK, 0)) // check if need to reset invert
  1030. sleep = 0;
  1031. if (check_invert_link()) // check if need to invert link
  1032. sleep = 0;
  1033. if (sleep)
  1034. {
  1035. if (time_diff(get_time_ticks(), last_event_time) > seconds_to_ticks(5))
  1036. rc = history_list_save(HISTORY_SAVE_POWER_OFF);
  1037. else if (time_diff(get_time_ticks(), last_event_time) > seconds_to_ticks(2))
  1038. rc = history_list_save(HISTORY_SAVE_NORMAL);
  1039. else
  1040. rc = -1;
  1041. if (rc > 0)
  1042. {
  1043. #ifdef INCLUDED_FROM_KERNEL
  1044. delay_us(200000); // for some reason, save may not work if no delay
  1045. #endif
  1046. }
  1047. else if (rc < 0)
  1048. sleep = 0; // waiting for last_event_time timeout to save the history
  1049. }
  1050. wl_input_wait(&ev, sleep);
  1051. more_events = 1;
  1052. switch (ev.type) {
  1053. case WL_INPUT_EV_TYPE_CURSOR:
  1054. handle_cursor(&ev);
  1055. last_event_time = get_time_ticks();
  1056. break;
  1057. case WL_INPUT_EV_TYPE_KEYBOARD:
  1058. if (ev.key_event.value != 0)
  1059. {
  1060. b_show_scroll_bar = 0;
  1061. handle_key_release(ev.key_event.keycode);
  1062. }
  1063. last_event_time = get_time_ticks();
  1064. break;
  1065. case WL_INPUT_EV_TYPE_TOUCH:
  1066. handle_touch(&ev);
  1067. last_event_time = ev.touch_event.ticks;
  1068. break;
  1069. default:
  1070. more_events = 0;
  1071. break;
  1072. }
  1073. }
  1074. /* never reached */
  1075. return 0;
  1076. }
  1077. unsigned long get_time_ticks(void)
  1078. {
  1079. long clock_ticks;
  1080. #ifdef INCLUDED_FROM_KERNEL
  1081. clock_ticks = Tick_get();
  1082. #else
  1083. clock_ticks = clock();
  1084. #endif
  1085. if (clock_ticks == 0)
  1086. clock_ticks = 1; // 0 is used for special purpose
  1087. return clock_ticks;
  1088. }
  1089. unsigned long time_diff(unsigned long t2, unsigned long t1)
  1090. {
  1091. unsigned long diff;
  1092. if (t2 < t1)
  1093. diff = (unsigned long) 0xFFFFFFFF - t1 + t2;
  1094. else
  1095. diff = t2 - t1;
  1096. return diff;
  1097. }
  1098. unsigned long seconds_to_ticks(float sec)
  1099. {
  1100. long clock_ticks;
  1101. #ifdef INCLUDED_FROM_KERNEL
  1102. clock_ticks = sec * Tick_TicksPerSecond;
  1103. #else
  1104. clock_ticks = sec * CLOCKS_PER_SEC;
  1105. #endif
  1106. return clock_ticks;
  1107. }
  1108. void fatal_error(char *sError)
  1109. {
  1110. struct wl_input_event ev;
  1111. guilib_fb_lock();
  1112. guilib_clear();
  1113. #ifndef INCLUDED_FROM_KERNEL
  1114. printf("Fatal error - %s\n", sError);
  1115. exit(-1);
  1116. #else
  1117. render_string(SUBTITLE_FONT_IDX, -1, 94, sError, strlen(sError), 0);
  1118. guilib_fb_unlock();
  1119. for (;;)
  1120. wl_input_wait(&ev, 1);
  1121. #endif
  1122. }