history.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * Authors Holger Hans Peter Freyther <zecke@openmoko.org>
  5. * Matt Hsu <matt_hsu@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 <string.h>
  21. #include <wikilib.h>
  22. #include <guilib.h>
  23. #include <glyph.h>
  24. #include <search.h>
  25. #include <stdlib.h>
  26. #include <file-io.h>
  27. #include <guilib.h>
  28. #include <lcd.h>
  29. #include "history.h"
  30. #include "search.h"
  31. #include "msg.h"
  32. #include "lcd_buf_draw.h"
  33. #include "wiki_info.h"
  34. #ifndef INCLUDED_FROM_KERNEL
  35. #include <stdio.h>
  36. #include <errno.h>
  37. #endif
  38. #define DBG_HISTORY 0
  39. #define HISTORY_MAX_ITEM 19
  40. #define HISTORY_MAX_DISPLAY_ITEM 18U
  41. #define MAX_VIEWING_LIST 30
  42. HISTORY history_list[MAX_HISTORY];
  43. struct _viewing_list {
  44. long idx_article;
  45. long last_y_pos;
  46. } viewing_list[MAX_VIEWING_LIST];
  47. long history_y_pos = 0;
  48. int viewing_count = 0;
  49. int history_count = 0;
  50. int rendered_history_count = -1;
  51. int history_changed = HISTORY_SAVE_NONE;
  52. extern int display_mode;
  53. static inline unsigned int history_modulus(int modulus) {
  54. return modulus % HISTORY_MAX_DISPLAY_ITEM;
  55. }
  56. long history_get_previous_idx(long current_idx_article, int b_drop_from_list)
  57. {
  58. long previous_idx_article;
  59. if (viewing_count > 0)
  60. {
  61. if (viewing_count > 1)
  62. {
  63. previous_idx_article = viewing_list[viewing_count - 2].idx_article;
  64. history_y_pos = viewing_list[viewing_count - 2].last_y_pos;
  65. }
  66. else if (viewing_list[0].idx_article != current_idx_article)
  67. {
  68. previous_idx_article = viewing_list[0].idx_article;
  69. history_y_pos = viewing_list[0].last_y_pos;
  70. }
  71. else
  72. previous_idx_article = 0;
  73. if (b_drop_from_list)
  74. viewing_count--;
  75. }
  76. else
  77. previous_idx_article = 0;
  78. return previous_idx_article;
  79. }
  80. void history_reload()
  81. {
  82. rendered_history_count = 0;
  83. render_history_with_pcf();
  84. }
  85. void history_add(long idx_article, const char *title, int b_keep_pos)
  86. {
  87. int i = 0;
  88. int bFound = 0;
  89. if (!(idx_article & 0xFF000000)) // idx_article for current wiki
  90. {
  91. idx_article |= get_wiki_id_from_idx(nCurrentWiki) << 24;
  92. }
  93. if (!viewing_count || viewing_list[viewing_count - 1].idx_article != idx_article)
  94. {
  95. if (viewing_count >= MAX_VIEWING_LIST)
  96. {
  97. for (i=0; i < MAX_VIEWING_LIST - 1; i++)
  98. viewing_list[i] = viewing_list[i + 1];
  99. viewing_count--;
  100. }
  101. viewing_list[viewing_count].idx_article = idx_article;
  102. viewing_list[viewing_count++].last_y_pos = 0;
  103. }
  104. history_changed = HISTORY_SAVE_NORMAL;
  105. while (!bFound && i < history_count)
  106. {
  107. if (idx_article == history_list[i].idx_article)
  108. {
  109. HISTORY history_tmp;
  110. history_tmp = history_list[i];
  111. if (!b_keep_pos)
  112. history_tmp.last_y_pos = 0;
  113. memrcpy((void*)&history_list[1],(void*)&history_list[0],sizeof(HISTORY)*i);
  114. history_list[0]=history_tmp;
  115. bFound = 1;
  116. }
  117. else
  118. i++;
  119. }
  120. if(bFound)
  121. return;
  122. if (history_count >= MAX_HISTORY)
  123. history_count = MAX_HISTORY - 1;
  124. memrcpy((void*)&history_list[1],(void*)&history_list[0],sizeof(HISTORY)*history_count);
  125. history_list[0].idx_article = idx_article;
  126. strcpy(history_list[0].title, title);
  127. history_list[0].last_y_pos = 0;
  128. history_count++;
  129. }
  130. void history_log_y_pos(const long y_pos)
  131. {
  132. if (history_changed != HISTORY_SAVE_NORMAL)
  133. history_changed = HISTORY_SAVE_POWER_OFF;
  134. history_list[0].last_y_pos = y_pos;
  135. if (viewing_count > 0)
  136. viewing_list[viewing_count - 1].last_y_pos = y_pos;
  137. }
  138. void history_set_y_pos(const long idx_article)
  139. {
  140. int i = 0;
  141. int bFound = 0;
  142. history_y_pos = 0;
  143. while (!bFound && i < history_count)
  144. {
  145. if (idx_article == history_list[i].idx_article)
  146. {
  147. history_y_pos = history_list[i].last_y_pos;
  148. bFound = 1;
  149. }
  150. else
  151. i++;
  152. }
  153. }
  154. long history_get_y_pos()
  155. {
  156. return history_y_pos;
  157. }
  158. unsigned int history_get_count()
  159. {
  160. return history_count;
  161. }
  162. void history_clear()
  163. {
  164. history_count = 0;
  165. history_changed = HISTORY_SAVE_NORMAL;
  166. }
  167. void history_list_init(void)
  168. {
  169. unsigned int len;
  170. int fd_hst;
  171. history_count = 0;
  172. fd_hst = wl_open("wiki.hst", WL_O_RDONLY);
  173. if (fd_hst >= 0)
  174. {
  175. while ((len = wl_read(fd_hst, (void *)&history_list[history_count], sizeof(HISTORY))) >= sizeof(HISTORY))
  176. {
  177. history_count++;
  178. }
  179. wl_close(fd_hst);
  180. }
  181. }
  182. int history_list_save(int level)
  183. {
  184. int fd_hst;
  185. int rc = 0;
  186. if (history_changed != HISTORY_SAVE_NONE)
  187. {
  188. if (level == HISTORY_SAVE_POWER_OFF || history_changed == HISTORY_SAVE_NORMAL)
  189. {
  190. fd_hst = wl_open("wiki.hst", WL_O_CREATE);
  191. if (fd_hst >= 0)
  192. {
  193. wl_write(fd_hst, (void *)history_list, sizeof(HISTORY) * history_count);
  194. wl_close(fd_hst);
  195. }
  196. history_changed = HISTORY_SAVE_NONE;
  197. rc = 1;
  198. }
  199. else
  200. rc = -1;
  201. }
  202. return rc;
  203. }
  204. void draw_clear_history(int bClear)
  205. {
  206. int i;
  207. static char localBuffer[27 * LCD_VRAM_WIDTH_PIXELS / 8];
  208. unsigned char *pText;
  209. if (bClear)
  210. {
  211. memcpy(&framebuffer[181 * LCD_VRAM_WIDTH_PIXELS / 8], localBuffer, 27 * LCD_VRAM_WIDTH_PIXELS / 8);
  212. }
  213. else
  214. {
  215. memcpy(localBuffer, &framebuffer[181 * LCD_VRAM_WIDTH_PIXELS / 8], 27 * LCD_VRAM_WIDTH_PIXELS / 8);
  216. memset(&framebuffer[181 * LCD_VRAM_WIDTH_PIXELS / 8], 0xFF, 27 * LCD_VRAM_WIDTH_PIXELS / 8);
  217. framebuffer[184 * LCD_VRAM_WIDTH_PIXELS / 8 + 18] = 0xFE;
  218. memset(&framebuffer[184 * LCD_VRAM_WIDTH_PIXELS / 8 + 19], 0, 4);
  219. framebuffer[184 * LCD_VRAM_WIDTH_PIXELS / 8 + 23] = 0x07;
  220. framebuffer[184 * LCD_VRAM_WIDTH_PIXELS / 8 + 24] = 0xF8;
  221. memset(&framebuffer[184 * LCD_VRAM_WIDTH_PIXELS / 8 + 25], 0, 4);
  222. framebuffer[184 * LCD_VRAM_WIDTH_PIXELS / 8 + 29] = 0x1F;
  223. for (i = 185; i <= 203; i++)
  224. {
  225. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 18] = 0xFC;
  226. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 19], 0, 4);
  227. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 23] = 0x03;
  228. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 24] = 0xF0;
  229. memset(&framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 25], 0, 4);
  230. framebuffer[i * LCD_VRAM_WIDTH_PIXELS / 8 + 29] = 0x0F;
  231. }
  232. framebuffer[204 * LCD_VRAM_WIDTH_PIXELS / 8 + 18] = 0xFE;
  233. memset(&framebuffer[204 * LCD_VRAM_WIDTH_PIXELS / 8 + 19], 0, 4);
  234. framebuffer[204 * LCD_VRAM_WIDTH_PIXELS / 8 + 23] = 0x07;
  235. framebuffer[204 * LCD_VRAM_WIDTH_PIXELS / 8 + 24] = 0xF8;
  236. memset(&framebuffer[204 * LCD_VRAM_WIDTH_PIXELS / 8 + 25], 0, 4);
  237. framebuffer[204 * LCD_VRAM_WIDTH_PIXELS / 8 + 29] = 0x1F;
  238. pText=get_nls_text("clear_history");
  239. render_string(SUBTITLE_FONT_IDX, LCD_LEFT_MARGIN, 185, pText, strlen(pText), 1);
  240. pText=get_nls_text("yes");
  241. render_string(SUBTITLE_FONT_IDX, 147 + (192 - 147 - get_external_str_pixel_width(pText, SUBTITLE_FONT_IDX)) / 2,
  242. 185, pText, strlen(pText), 0);
  243. pText=get_nls_text("no");
  244. render_string(SUBTITLE_FONT_IDX, 193 + (238 - 193 - get_external_str_pixel_width(pText, SUBTITLE_FONT_IDX)) / 2,
  245. 185, pText, strlen(pText), 0);
  246. }
  247. }