region.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * BURG - Brand-new Universal loadeR from GRUB
  3. * Copyright 2009 Bean Lee - All Rights Reserved
  4. *
  5. * BURG is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * BURG is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with BURG. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/mm.h>
  19. #include <grub/misc.h>
  20. #include <grub/tree.h>
  21. #include <grub/menu_region.h>
  22. GRUB_EXPORT(grub_menu_region_class);
  23. GRUB_EXPORT(grub_menu_region_create_text);
  24. GRUB_EXPORT(grub_menu_region_create_rect);
  25. GRUB_EXPORT(grub_menu_region_create_bitmap);
  26. GRUB_EXPORT(grub_menu_region_scale);
  27. GRUB_EXPORT(grub_menu_region_free);
  28. GRUB_EXPORT(grub_menu_region_check_rect);
  29. GRUB_EXPORT(grub_menu_region_add_update);
  30. GRUB_EXPORT(grub_menu_region_apply_update);
  31. GRUB_EXPORT(grub_menu_region_text_term);
  32. grub_term_output_t grub_menu_region_text_term;
  33. grub_bitmap_cache_t cache_head;
  34. struct grub_handler_class grub_menu_region_class =
  35. {
  36. .name = "menu_region"
  37. };
  38. #define grub_cur_menu_region grub_menu_region_get_current ()
  39. grub_menu_region_text_t
  40. grub_menu_region_create_text (grub_font_t font, grub_uint32_t color,
  41. const char *str)
  42. {
  43. grub_menu_region_text_t region;
  44. if (! str)
  45. str = "";
  46. region = grub_zalloc (sizeof (*region) + grub_strlen (str) + 1);
  47. if (! region)
  48. return 0;
  49. region->common.type = GRUB_MENU_REGION_TYPE_TEXT;
  50. region->common.width = grub_menu_region_get_text_width (font, str, 0, 0);
  51. region->common.height = grub_menu_region_get_text_height (font);
  52. region->font = font;
  53. region->color = color;
  54. grub_strcpy (region->text, str);
  55. return region;
  56. }
  57. grub_menu_region_rect_t
  58. grub_menu_region_create_rect (int width, int height, grub_video_color_t color,
  59. grub_uint32_t fill)
  60. {
  61. grub_menu_region_rect_t region;
  62. region = grub_zalloc (sizeof (*region));
  63. if (! region)
  64. return 0;
  65. region->common.type = GRUB_MENU_REGION_TYPE_RECT;
  66. region->common.width = width;
  67. region->common.height = height;
  68. region->color = color;
  69. region->fill = fill;
  70. return region;
  71. }
  72. grub_menu_region_bitmap_t
  73. grub_menu_region_create_bitmap (const char *name, int scale,
  74. grub_video_color_t color)
  75. {
  76. grub_menu_region_bitmap_t region = 0;
  77. struct grub_video_bitmap *bitmap;
  78. grub_bitmap_cache_t cache = 0;
  79. if (! grub_cur_menu_region->get_bitmap)
  80. return 0;
  81. region = grub_zalloc (sizeof (*region));
  82. if (! region)
  83. return 0;
  84. cache = grub_named_list_find (GRUB_AS_NAMED_LIST (cache_head), name);
  85. if (cache)
  86. bitmap = cache->bitmap;
  87. else
  88. {
  89. cache = grub_zalloc (sizeof (*cache));
  90. if (! cache)
  91. goto quit;
  92. bitmap = grub_cur_menu_region->get_bitmap (name);
  93. if (! bitmap)
  94. goto quit;
  95. cache->bitmap = bitmap;
  96. cache->name = grub_strdup (name);
  97. grub_list_push (GRUB_AS_LIST_P (&cache_head), GRUB_AS_LIST (cache));
  98. }
  99. cache->count++;
  100. region->common.type = GRUB_MENU_REGION_TYPE_BITMAP;
  101. region->common.width = bitmap->mode_info.width;
  102. region->common.height = bitmap->mode_info.height;
  103. region->bitmap = bitmap;
  104. region->cache = cache;
  105. region->scale = scale;
  106. region->color = color;
  107. return region;
  108. quit:
  109. grub_free (region);
  110. grub_free (cache);
  111. return 0;
  112. }
  113. void
  114. grub_menu_region_scale (grub_menu_region_common_t region, int width,
  115. int height)
  116. {
  117. if ((! region) || ((width == region->width) && (height == region->height)))
  118. return;
  119. region->width = width;
  120. region->height = height;
  121. if ((region->type == GRUB_MENU_REGION_TYPE_BITMAP) &&
  122. (grub_cur_menu_region->scale_bitmap))
  123. {
  124. grub_menu_region_bitmap_t b;
  125. b = (grub_menu_region_bitmap_t) region;
  126. if ((b->cache->scaled_bitmap) &&
  127. (width == (int) b->cache->scaled_bitmap->mode_info.width) &&
  128. (height == (int) b->cache->scaled_bitmap->mode_info.height))
  129. {
  130. b->bitmap = b->cache->scaled_bitmap;
  131. }
  132. else
  133. {
  134. grub_cur_menu_region->scale_bitmap (b);
  135. if (! b->cache->scaled_bitmap)
  136. b->cache->scaled_bitmap = b->bitmap;
  137. }
  138. }
  139. }
  140. static void
  141. grub_bitmap_cache_free (grub_bitmap_cache_t cache)
  142. {
  143. cache->count--;
  144. if (cache->count == 0)
  145. {
  146. grub_list_remove (GRUB_AS_LIST_P (&cache_head), GRUB_AS_LIST (cache));
  147. grub_cur_menu_region->free_bitmap (cache->bitmap);
  148. grub_cur_menu_region->free_bitmap (cache->scaled_bitmap);
  149. grub_free ((char *) cache->name);
  150. grub_free (cache);
  151. }
  152. }
  153. void
  154. grub_menu_region_free (grub_menu_region_common_t region)
  155. {
  156. if (! region)
  157. return;
  158. if ((region->type == GRUB_MENU_REGION_TYPE_BITMAP) &&
  159. (grub_cur_menu_region->free_bitmap))
  160. {
  161. grub_menu_region_bitmap_t r = (grub_menu_region_bitmap_t) region;
  162. if (r->cache)
  163. {
  164. if ((r->bitmap != r->cache->bitmap) &&
  165. (r->bitmap != r->cache->scaled_bitmap))
  166. grub_cur_menu_region->free_bitmap (r->bitmap);
  167. grub_bitmap_cache_free (r->cache);
  168. }
  169. else
  170. grub_cur_menu_region->free_bitmap (r->bitmap);
  171. }
  172. grub_free (region);
  173. }
  174. grub_menu_region_bitmap_t
  175. grub_menu_region_new_bitmap (int width, int height)
  176. {
  177. grub_menu_region_bitmap_t region = 0;
  178. struct grub_video_bitmap *bm;
  179. if (! grub_menu_region_get_current ()->new_bitmap)
  180. return 0;
  181. region = grub_zalloc (sizeof (*region));
  182. if (! region)
  183. return 0;
  184. bm = grub_menu_region_get_current ()->new_bitmap (width, height);
  185. if (! bm)
  186. {
  187. grub_free (region);
  188. return 0;
  189. }
  190. region->common.type = GRUB_MENU_REGION_TYPE_BITMAP;
  191. region->common.width = width;
  192. region->common.height = height;
  193. region->bitmap = bm;
  194. return region;
  195. }
  196. int
  197. grub_menu_region_check_rect (int *x1, int *y1, int *w1, int *h1,
  198. int x2, int y2, int w2, int h2)
  199. {
  200. *w1 += *x1;
  201. *h1 += *y1;
  202. w2 += x2;
  203. h2 += y2;
  204. if (*x1 < x2)
  205. *x1 = x2;
  206. if (*y1 < y2)
  207. *y1 = y2;
  208. if (*w1 > w2)
  209. *w1 = w2;
  210. if (*h1 > h2)
  211. *h1 = h2;
  212. if ((*w1 <= *x1) || (*h1 <= *y1))
  213. return 0;
  214. *w1 -= *x1;
  215. *h1 -= *y1;
  216. return 1;
  217. }
  218. void
  219. grub_menu_region_add_update (grub_menu_region_update_list_t *head,
  220. grub_menu_region_common_t region,
  221. int org_x, int org_y, int x, int y,
  222. int width, int height)
  223. {
  224. grub_menu_region_update_list_t u;
  225. if (! region)
  226. return;
  227. org_x += region->ofs_x;
  228. org_y += region->ofs_y;
  229. x -= region->ofs_x;
  230. y -= region->ofs_y;
  231. if (! grub_menu_region_check_rect (&x, &y, &width, &height,
  232. 0, 0, region->width, region->height))
  233. return;
  234. u = grub_malloc (sizeof (*u));
  235. if (! u)
  236. return;
  237. u->region = region;
  238. u->org_x = org_x;
  239. u->org_y = org_y;
  240. u->x = x;
  241. u->y = y;
  242. u->width = width;
  243. u->height = height;
  244. if ((! grub_menu_region_gfx_mode ()) ||
  245. ((region->type == GRUB_MENU_REGION_TYPE_RECT) &&
  246. (! ((grub_menu_region_rect_t) region)->fill)) ||
  247. ((region->type == GRUB_MENU_REGION_TYPE_BITMAP) &&
  248. (! ((grub_menu_region_bitmap_t) region)->bitmap->transparent)))
  249. {
  250. grub_menu_region_update_list_t *p, q;
  251. for (p = head, q = *p; q;)
  252. {
  253. int x1, y1, w1, h1;
  254. x1 = org_x + x - q->org_x;
  255. y1 = org_y + y - q->org_y;
  256. w1 = width;
  257. h1 = height;
  258. if (! grub_menu_region_check_rect (&x1, &y1, &w1, &h1,
  259. q->x, q->y, q->width, q->height))
  260. {
  261. p = &(q->next);
  262. q = q->next;
  263. continue;
  264. }
  265. if (y1 > q->y)
  266. {
  267. grub_menu_region_update_list_t n;
  268. n = grub_malloc (sizeof (*u));
  269. if (n)
  270. {
  271. n->region = q->region;
  272. n->org_x = q->org_x;
  273. n->org_y = q->org_y;
  274. n->x = q->x;
  275. n->y = q->y;
  276. n->width = q->width;
  277. n->height = y1 - q->y;
  278. *p = n;
  279. p = &(n->next);
  280. }
  281. }
  282. if (x1 > q->x)
  283. {
  284. grub_menu_region_update_list_t n;
  285. n = grub_malloc (sizeof (*u));
  286. if (n)
  287. {
  288. n->region = q->region;
  289. n->org_x = q->org_x;
  290. n->org_y = q->org_y;
  291. n->x = q->x;
  292. n->y = y1;
  293. n->width = x1 - q->x;
  294. n->height = h1;
  295. *p = n;
  296. p = &(n->next);
  297. }
  298. }
  299. if (x1 + w1 < q->x + q->width)
  300. {
  301. grub_menu_region_update_list_t n;
  302. n = grub_malloc (sizeof (*u));
  303. if (n)
  304. {
  305. n->region = q->region;
  306. n->org_x = q->org_x;
  307. n->org_y = q->org_y;
  308. n->x = x1 + w1;
  309. n->y = y1;
  310. n->width = q->x + q->width - n->x;
  311. n->height = h1;
  312. *p = n;
  313. p = &(n->next);
  314. }
  315. }
  316. if (y1 + h1 < q->y + q->height)
  317. {
  318. grub_menu_region_update_list_t n;
  319. n = grub_malloc (sizeof (*u));
  320. if (n)
  321. {
  322. n->region = q->region;
  323. n->org_x = q->org_x;
  324. n->org_y = q->org_y;
  325. n->x = q->x;
  326. n->y = y1 + h1;
  327. n->width = q->width;
  328. n->height = q->y + q->height - n->y;
  329. *p = n;
  330. p = &(n->next);
  331. }
  332. }
  333. *p = q->next;
  334. grub_free (q);
  335. q = *p;
  336. }
  337. }
  338. u->next = *head;
  339. *head = u;
  340. }
  341. void
  342. grub_menu_region_apply_update (grub_menu_region_update_list_t head)
  343. {
  344. grub_menu_region_update_list_t prev = 0;
  345. grub_menu_region_hide_cursor ();
  346. while (head)
  347. {
  348. grub_menu_region_update_list_t temp;
  349. temp = head->next;
  350. head->next = prev;
  351. prev = head;
  352. head = temp;
  353. }
  354. head = prev;
  355. while (head)
  356. {
  357. grub_menu_region_update_list_t c;
  358. grub_menu_region_common_t r;
  359. int scn_x, scn_y;
  360. c = head;
  361. head = head->next;
  362. r = c->region;
  363. scn_x = c->org_x + c->x;
  364. scn_y = c->org_y + c->y;
  365. #if 0
  366. grub_printf ("[%d: %d %d %d %d %d %d] ", r->type, c->x, c->y,
  367. c->width, c->height, scn_x, scn_y);
  368. #else
  369. switch (r->type)
  370. {
  371. case GRUB_MENU_REGION_TYPE_RECT:
  372. grub_cur_menu_region->update_rect ((grub_menu_region_rect_t ) r,
  373. c->x, c->y, c->width, c->height,
  374. scn_x, scn_y);
  375. break;
  376. case GRUB_MENU_REGION_TYPE_TEXT:
  377. grub_cur_menu_region->update_text ((grub_menu_region_text_t ) r,
  378. c->x, c->y, c->width, c->height,
  379. scn_x, scn_y);
  380. break;
  381. case GRUB_MENU_REGION_TYPE_BITMAP:
  382. grub_cur_menu_region->update_bitmap ((grub_menu_region_bitmap_t ) r,
  383. c->x, c->y, c->width, c->height,
  384. scn_x, scn_y);
  385. break;
  386. }
  387. #endif
  388. grub_free (c);
  389. }
  390. }