package-manager.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * package-manager - Graphical interface to msk
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <math.h>
  11. #include <sys/time.h>
  12. #include <sys/stat.h>
  13. #include <toaru/yutani.h>
  14. #include <toaru/graphics.h>
  15. #include <toaru/decorations.h>
  16. #include <toaru/menu.h>
  17. #include <toaru/sdf.h>
  18. #include <toaru/confreader.h>
  19. #include <toaru/icon_cache.h>
  20. #define APPLICATION_TITLE "Package Manager"
  21. #define SCROLL_AMOUNT 120
  22. #define VAR_PATH "/var/msk"
  23. static yutani_t * yctx;
  24. static yutani_window_t * main_window;
  25. static gfx_context_t * ctx;
  26. static int application_running = 1;
  27. static gfx_context_t * contents = NULL;
  28. static sprite_t * contents_sprite = NULL;
  29. static int available_height = 0; /* How much space is available in the main window for the icon view */
  30. static int scroll_offset = 0; /* How far the icon view should be scrolled */
  31. static int hilighted_offset = -1; /* Which file is hovered by the mouse */
  32. static uint64_t last_click = 0; /* For double click */
  33. static int last_click_offset = -1; /* So that clicking two different things quickly doesn't count as a double click */
  34. struct Package {
  35. char name[256];
  36. char friendly_name[256];
  37. char description[1024];
  38. char version[256]; /* Really doesn't need to be that long */
  39. int selected;
  40. int installed;
  41. };
  42. static struct Package ** pkg_pointers = NULL; /* List of package pointers */
  43. static ssize_t pkg_pointers_len = 0; /* How many packages are in the current list */
  44. static struct menu_bar menu_bar = {0};
  45. static struct menu_bar_entries menu_entries[] = {
  46. {"File", "file"},
  47. {"Index", "index"},
  48. {"Help", "help"},
  49. {NULL, NULL},
  50. };
  51. static void _menu_action_exit(struct MenuEntry * entry) {
  52. application_running = 0;
  53. }
  54. /**
  55. * Accurate time comparison.
  56. *
  57. * These methods were taken from the compositor and
  58. * allow us to time double-clicks accurately.
  59. */
  60. static uint64_t precise_current_time(void) {
  61. struct timeval t;
  62. gettimeofday(&t, NULL);
  63. time_t sec_diff = t.tv_sec;
  64. suseconds_t usec_diff = t.tv_usec;
  65. return (uint64_t)((uint64_t)sec_diff * 1000LL + usec_diff / 1000);
  66. }
  67. static uint64_t precise_time_since(uint64_t start_time) {
  68. uint64_t now = precise_current_time();
  69. uint64_t diff = now - start_time; /* Milliseconds */
  70. return diff;
  71. }
  72. static int _close_enough(struct yutani_msg_window_mouse_event * me) {
  73. if (me->command == YUTANI_MOUSE_EVENT_RAISE && sqrt(pow(me->new_x - me->old_x, 2) + pow(me->new_y - me->old_y, 2)) < 10) {
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. #define BUTTON_HEIGHT 28
  79. #define BUTTON_WIDTH 86
  80. #define BUTTON_PADDING 14
  81. #define HILIGHT_BORDER_TOP rgb(54,128,205)
  82. #define HILIGHT_GRADIENT_TOP rgb(93,163,236)
  83. #define HILIGHT_GRADIENT_BOTTOM rgb(56,137,220)
  84. #define HILIGHT_BORDER_BOTTOM rgb(47,106,167)
  85. #define PKG_HEIGHT 70
  86. static void draw_package(struct Package * package, int index) {
  87. int offset_y = index * PKG_HEIGHT;
  88. if (package->selected) {
  89. if (main_window->focused) {
  90. draw_rectangle_solid(contents, 0, offset_y, contents->width, PKG_HEIGHT, rgb(93,163,236));
  91. draw_line(contents, 0, contents->width, offset_y, offset_y, HILIGHT_BORDER_TOP);
  92. draw_line(contents, 0, contents->width, offset_y + PKG_HEIGHT-1, offset_y + PKG_HEIGHT-1, HILIGHT_BORDER_BOTTOM);
  93. for (int i = 1; i < PKG_HEIGHT - 2; ++i) {
  94. int thing = ((i - 1) * 256) / (PKG_HEIGHT - 3);
  95. if (thing > 255) thing = 255;
  96. if (thing < 0) thing = 0;
  97. uint32_t c = interp_colors(HILIGHT_GRADIENT_TOP, HILIGHT_GRADIENT_BOTTOM, thing);
  98. draw_line(contents, 0, contents->width, offset_y + i, offset_y + i, c);
  99. }
  100. } else {
  101. draw_rectangle_solid(contents, 0, offset_y, contents->width, PKG_HEIGHT, rgb(180,180,180));
  102. }
  103. }
  104. sprite_t * icon = package->installed ? icon_get_48("package") : icon_get_48("package-uninstalled");
  105. draw_sprite(contents, icon, 8, offset_y + 11);
  106. uint32_t text_color = package->selected ? rgb(255,255,255) : rgb(0,0,0);
  107. char tmp[2048];
  108. sprintf(tmp, "%s - %s", package->friendly_name, package->version);
  109. draw_sdf_string(contents, 64, offset_y + 4, tmp, 20, text_color, SDF_FONT_BOLD);
  110. sprintf(tmp, "%s - %s", package->name, package->description);
  111. int x = draw_sdf_string(contents, 65, offset_y + 24, package->name, 16, rgb(150,150,150), SDF_FONT_THIN);
  112. draw_sdf_string(contents, 64 + x + 4, offset_y + 24, package->description, 16, text_color, SDF_FONT_THIN);
  113. }
  114. static void redraw_packages(void) {
  115. draw_fill(contents, rgba(0,0,0,0));
  116. for (int i = 0; i < pkg_pointers_len; ++i) {
  117. draw_package(pkg_pointers[i], i);
  118. }
  119. }
  120. static void load_manifest(void) {
  121. if (pkg_pointers) {
  122. for (int i = 0; i < pkg_pointers_len; ++i) {
  123. free(pkg_pointers[i]);
  124. }
  125. free(pkg_pointers);
  126. pkg_pointers_len = 0;
  127. }
  128. confreader_t * conf = confreader_load(VAR_PATH "/manifest");
  129. if (conf) {
  130. hashmap_t * msk_installed = hashmap_create(10);
  131. FILE * installed = fopen(VAR_PATH "/installed", "r");
  132. if (installed) {
  133. while (!feof(installed)) {
  134. char tmp[128] = {0};
  135. if (!fgets(tmp, 128, installed)) break;
  136. char * nl = strstr(tmp, "\n");
  137. if (nl) *nl = '\0';
  138. char * eqeq = strstr(tmp, "==");
  139. if (!eqeq) {
  140. /* show error */
  141. break;
  142. }
  143. *eqeq = '\0';
  144. char * version = eqeq+2;
  145. hashmap_set(msk_installed, tmp, strdup(version));
  146. }
  147. }
  148. list_t * package_list = list_create();
  149. /* Go through sections */
  150. list_t * packages = hashmap_keys(conf->sections);
  151. foreach(node, packages) {
  152. char * name = node->value;
  153. if (!strlen(name)) continue; /* skip empty section */
  154. char * desc = confreader_get(conf, name, "description");
  155. char * version = confreader_get(conf, name, "version");
  156. char * friendly_name = confreader_get(conf, name, "friendly-name");
  157. struct Package * p = malloc(sizeof(struct Package));
  158. sprintf(p->name, name);
  159. sprintf(p->friendly_name, friendly_name);
  160. sprintf(p->description, desc);
  161. sprintf(p->version, version);
  162. p->selected = 0;
  163. p->installed = hashmap_has(msk_installed, name);
  164. list_insert(package_list, p);
  165. }
  166. list_free(packages);
  167. free(packages);
  168. hashmap_free(msk_installed);
  169. free(msk_installed);
  170. pkg_pointers = malloc(sizeof(struct Package *) * package_list->length);
  171. pkg_pointers_len = package_list->length;
  172. int i = 0;
  173. foreach (node, package_list) {
  174. pkg_pointers[i] = node->value;
  175. i++;
  176. }
  177. list_free(package_list);
  178. free(package_list);
  179. int comparator(const void * c1, const void * c2) {
  180. const struct Package * f1 = *(const struct Package **)(c1);
  181. const struct Package * f2 = *(const struct Package **)(c2);
  182. return strcmp(f1->name, f2->name);
  183. }
  184. qsort(pkg_pointers, pkg_pointers_len, sizeof(struct Package *), comparator);
  185. }
  186. }
  187. static struct Package * get_package_at_offset(int offset) {
  188. if (offset >= 0 && offset < pkg_pointers_len) {
  189. return pkg_pointers[offset];
  190. }
  191. return NULL;
  192. }
  193. static void clear_offset(int offset) {
  194. draw_rectangle_solid(contents, 0, offset * PKG_HEIGHT, contents->width, PKG_HEIGHT, rgba(0,0,0,0));
  195. }
  196. static void reinitialize_contents(void) {
  197. if (contents) {
  198. free(contents);
  199. }
  200. if (contents_sprite) {
  201. sprite_free(contents_sprite);
  202. }
  203. /* Calculate height for current directory */
  204. int calculated_height = pkg_pointers_len * PKG_HEIGHT;
  205. struct decor_bounds bounds;
  206. decor_get_bounds(main_window, &bounds);
  207. contents_sprite = create_sprite(main_window->width - bounds.width, calculated_height, ALPHA_EMBEDDED);
  208. contents = init_graphics_sprite(contents_sprite);
  209. /* Draw packages */
  210. redraw_packages();
  211. }
  212. static void redraw_window(void) {
  213. draw_fill(ctx, rgb(255,255,255));
  214. render_decorations(main_window, ctx, APPLICATION_TITLE);
  215. struct decor_bounds bounds;
  216. decor_get_bounds(main_window, &bounds);
  217. menu_bar.x = bounds.left_width;
  218. menu_bar.y = bounds.top_height;
  219. menu_bar.width = ctx->width - bounds.width;
  220. menu_bar.window = main_window;
  221. menu_bar_render(&menu_bar, ctx);
  222. gfx_clear_clip(ctx);
  223. gfx_add_clip(ctx, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT, ctx->width - bounds.width, ctx->height - MENU_BAR_HEIGHT - bounds.height);
  224. draw_sprite(ctx, contents_sprite, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT - scroll_offset);
  225. gfx_clear_clip(ctx);
  226. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  227. flip(ctx);
  228. yutani_flip(yctx, main_window);
  229. }
  230. static void resize_finish(int w, int h) {
  231. int width_changed = (main_window->width != (unsigned int)w);
  232. yutani_window_resize_accept(yctx, main_window, w, h);
  233. reinit_graphics_yutani(ctx, main_window);
  234. struct decor_bounds bounds;
  235. decor_get_bounds(main_window, &bounds);
  236. /* Recalculate available size */
  237. available_height = ctx->height - MENU_BAR_HEIGHT - bounds.height;
  238. /* If the width changed, we need to rebuild the icon view */
  239. if (width_changed) {
  240. reinitialize_contents();
  241. }
  242. /* Make sure we're not scrolled weirdly after resizing */
  243. if (available_height > contents->height) {
  244. scroll_offset = 0;
  245. } else {
  246. if (scroll_offset > contents->height - available_height) {
  247. scroll_offset = contents->height - available_height;
  248. }
  249. }
  250. /* Redraw */
  251. redraw_window();
  252. yutani_window_resize_done(yctx, main_window);
  253. yutani_flip(yctx, main_window);
  254. }
  255. static void _menu_action_refresh(struct MenuEntry * entry) {
  256. /* refresh msk manifest */
  257. system("terminal msk update");
  258. load_manifest();
  259. reinitialize_contents();
  260. redraw_window();
  261. }
  262. static void install_package(struct Package * package) {
  263. if (package->installed) return;
  264. putenv("MSK_YES=1");
  265. char tmp[1024];
  266. sprintf(tmp, "terminal msk install %s", package->name);
  267. system(tmp);
  268. load_manifest();
  269. reinitialize_contents();
  270. redraw_window();
  271. }
  272. static void _menu_action_about(struct MenuEntry * entry) {
  273. /* Show About dialog */
  274. char about_cmd[1024] = "\0";
  275. strcat(about_cmd, "about \"About " APPLICATION_TITLE "\" /usr/share/icons/48/package.bmp \"ToaruOS " APPLICATION_TITLE "\" \"(C) 2018 K. Lange\n-\nPart of ToaruOS, which is free software\nreleased under the NCSA/University of Illinois\nlicense.\n-\n%https://toaruos.org\n%https://github.com/klange/toaruos\" ");
  276. char coords[100];
  277. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  278. strcat(about_cmd, coords);
  279. system(about_cmd);
  280. redraw_window();
  281. }
  282. static void _menu_action_help(struct MenuEntry * entry) {
  283. /* show help documentation */
  284. system("help-browser package-manager.trt &");
  285. redraw_window();
  286. }
  287. static void toggle_selected(int hilighted_offset, int modifiers) {
  288. struct Package * f = get_package_at_offset(hilighted_offset);
  289. /* No file at this offset, do nothing. */
  290. if (!f) return;
  291. /* Toggle selection of the current file */
  292. f->selected = !f->selected;
  293. /* If Ctrl wasn't held, unselect everything else. */
  294. if (!(modifiers & KEY_MOD_LEFT_CTRL)) {
  295. for (int i = 0; i < pkg_pointers_len; ++i) {
  296. if (pkg_pointers[i] != f && pkg_pointers[i]->selected) {
  297. pkg_pointers[i]->selected = 0;
  298. clear_offset(i);
  299. draw_package(pkg_pointers[i], i);
  300. }
  301. }
  302. }
  303. /* Redraw the pkg */
  304. clear_offset(hilighted_offset);
  305. draw_package(f, hilighted_offset);
  306. /* And repaint the window */
  307. redraw_window();
  308. }
  309. static void _scroll_up(void) {
  310. scroll_offset -= SCROLL_AMOUNT;
  311. if (scroll_offset < 0) {
  312. scroll_offset = 0;
  313. }
  314. }
  315. static void _scroll_down(void) {
  316. if (available_height > contents->height) {
  317. scroll_offset = 0;
  318. } else {
  319. scroll_offset += SCROLL_AMOUNT;
  320. if (scroll_offset > contents->height - available_height) {
  321. scroll_offset = contents->height - available_height;
  322. }
  323. }
  324. }
  325. static void arrow_select(int y) {
  326. if (!pkg_pointers_len) return;
  327. /* Find first selected */
  328. int selected = -1;
  329. for (int i = 0; i <pkg_pointers_len; ++i) {
  330. if (pkg_pointers[i]->selected) {
  331. selected = i;
  332. }
  333. pkg_pointers[i]->selected = 0;
  334. }
  335. if (selected == -1) {
  336. selected = 0;
  337. } else {
  338. selected += y;
  339. if (selected >= pkg_pointers_len) selected = pkg_pointers_len - 1;
  340. if (selected < 0) selected = 0;
  341. }
  342. if (selected * PKG_HEIGHT < scroll_offset) {
  343. scroll_offset = selected * PKG_HEIGHT;
  344. }
  345. if (selected * PKG_HEIGHT + PKG_HEIGHT > scroll_offset + available_height) {
  346. scroll_offset = selected * PKG_HEIGHT + PKG_HEIGHT - available_height;
  347. }
  348. pkg_pointers[selected]->selected = 1;
  349. reinitialize_contents();
  350. redraw_window();
  351. }
  352. int main(int argc, char * argv[]) {
  353. if (geteuid() != 0) {
  354. char * args[] = {
  355. "showdialog",
  356. "Package Manager",
  357. "/usr/share/icons/48/package.bmp",
  358. "Only root can manage packages.",
  359. NULL,
  360. };
  361. return execvp("showdialog", args);
  362. }
  363. yctx = yutani_init();
  364. init_decorations();
  365. main_window = yutani_window_create(yctx, 640, 480);
  366. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  367. ctx = init_graphics_yutani_double_buffer(main_window);
  368. yutani_window_advertise_icon(yctx, main_window, APPLICATION_TITLE, "package");
  369. menu_bar.entries = menu_entries;
  370. menu_bar.redraw_callback = redraw_window;
  371. menu_bar.set = menu_set_create();
  372. struct MenuList * m = menu_create(); /* File */
  373. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  374. menu_set_insert(menu_bar.set, "file", m);
  375. m = menu_create(); /* Go */
  376. menu_insert(m, menu_create_normal("refresh",NULL,"Refresh",_menu_action_refresh));
  377. menu_set_insert(menu_bar.set, "index", m);
  378. m = menu_create();
  379. menu_insert(m, menu_create_normal("help","help_browser.trt","Contents",_menu_action_help));
  380. menu_insert(m, menu_create_separator());
  381. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  382. menu_set_insert(menu_bar.set, "help", m);
  383. struct decor_bounds bounds;
  384. decor_get_bounds(main_window, &bounds);
  385. available_height = ctx->height - MENU_BAR_HEIGHT - bounds.height;
  386. struct stat statbuf;
  387. if (!stat(VAR_PATH "/manifest",&statbuf)) {
  388. load_manifest();
  389. reinitialize_contents();
  390. redraw_window();
  391. } else {
  392. _menu_action_refresh(NULL);
  393. /* also redraws window */
  394. }
  395. while (application_running) {
  396. yutani_msg_t * m = yutani_poll(yctx);
  397. while (m) {
  398. if (menu_process_event(yctx, m)) {
  399. redraw_window();
  400. }
  401. switch (m->type) {
  402. case YUTANI_MSG_KEY_EVENT:
  403. {
  404. struct yutani_msg_key_event * ke = (void*)m->data;
  405. if (ke->event.action == KEY_ACTION_DOWN && ke->wid == main_window->wid) {
  406. switch (ke->event.keycode) {
  407. case KEY_PAGE_UP:
  408. _scroll_up();
  409. redraw_window();
  410. break;
  411. case KEY_PAGE_DOWN:
  412. _scroll_down();
  413. redraw_window();
  414. break;
  415. case KEY_ARROW_DOWN:
  416. arrow_select(1);
  417. break;
  418. case KEY_ARROW_UP:
  419. arrow_select(-1);
  420. break;
  421. case '\n':
  422. for (int i = 0; i <pkg_pointers_len; ++i) {
  423. if (pkg_pointers[i]->selected) {
  424. install_package(pkg_pointers[i]);
  425. }
  426. }
  427. break;
  428. case 'f':
  429. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT) {
  430. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[0]);
  431. }
  432. break;
  433. case 'i':
  434. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT) {
  435. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[1]);
  436. }
  437. break;
  438. case 'h':
  439. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT) {
  440. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[2]);
  441. }
  442. break;
  443. case 'q':
  444. _menu_action_exit(NULL);
  445. break;
  446. default:
  447. break;
  448. }
  449. }
  450. }
  451. break;
  452. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  453. {
  454. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  455. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  456. if (win == main_window) {
  457. win->focused = wf->focused;
  458. redraw_packages();
  459. redraw_window();
  460. }
  461. }
  462. break;
  463. case YUTANI_MSG_RESIZE_OFFER:
  464. {
  465. struct yutani_msg_window_resize * wr = (void*)m->data;
  466. if (wr->wid == main_window->wid) {
  467. resize_finish(wr->width, wr->height);
  468. }
  469. }
  470. break;
  471. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  472. {
  473. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  474. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  475. if (win == main_window) {
  476. int result = decor_handle_event(yctx, m);
  477. switch (result) {
  478. case DECOR_CLOSE:
  479. _menu_action_exit(NULL);
  480. break;
  481. case DECOR_RIGHT:
  482. /* right click in decoration, show appropriate menu */
  483. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  484. break;
  485. default:
  486. /* Other actions */
  487. break;
  488. }
  489. /* Menu bar */
  490. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  491. if (me->new_y > (int)(bounds.top_height + MENU_BAR_HEIGHT) &&
  492. me->new_y < (int)(main_window->height - bounds.bottom_height) &&
  493. me->new_x > (int)(bounds.left_width) &&
  494. me->new_x < (int)(main_window->width - bounds.right_width)) {
  495. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  496. _scroll_up();
  497. redraw_window();
  498. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  499. _scroll_down();
  500. redraw_window();
  501. }
  502. /* Get offset into contents */
  503. int y_into = me->new_y - bounds.top_height - MENU_BAR_HEIGHT + scroll_offset;
  504. int offset = (y_into / PKG_HEIGHT);
  505. if (offset != hilighted_offset) {
  506. int old_offset = hilighted_offset;
  507. hilighted_offset = offset;
  508. if (old_offset != -1) {
  509. clear_offset(old_offset);
  510. struct Package * f = get_package_at_offset(old_offset);
  511. if (f) {
  512. clear_offset(old_offset);
  513. draw_package(f, old_offset);
  514. }
  515. }
  516. struct Package * f = get_package_at_offset(hilighted_offset);
  517. if (f) {
  518. clear_offset(hilighted_offset);
  519. draw_package(f, hilighted_offset);
  520. }
  521. redraw_window();
  522. }
  523. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  524. struct Package * f = get_package_at_offset(hilighted_offset);
  525. if (f) {
  526. if (last_click_offset == hilighted_offset && precise_time_since(last_click) < 400) {
  527. install_package(f);
  528. //open_file(f);
  529. last_click = 0;
  530. } else {
  531. last_click = precise_current_time();
  532. last_click_offset = hilighted_offset;
  533. toggle_selected(hilighted_offset, me->modifiers);
  534. }
  535. } else {
  536. if (!(me->modifiers & YUTANI_KEY_MODIFIER_CTRL)) {
  537. for (int i = 0; i < pkg_pointers_len; ++i) {
  538. if (pkg_pointers[i]->selected) {
  539. pkg_pointers[i]->selected = 0;
  540. clear_offset(i);
  541. draw_package(pkg_pointers[i], i);
  542. }
  543. }
  544. redraw_window();
  545. }
  546. }
  547. } else if (me->buttons & YUTANI_MOUSE_BUTTON_RIGHT) {
  548. #if 0
  549. if (!context_menu->window) {
  550. struct Package * f = get_package_at_offset(hilighted_offset);
  551. if (f && !f->selected) {
  552. toggle_selected(hilighted_offset, me->modifiers);
  553. }
  554. menu_show(context_menu, main_window->ctx);
  555. yutani_window_move(main_window->ctx, context_menu->window, me->new_x + main_window->x, me->new_y + main_window->y);
  556. }
  557. #endif
  558. }
  559. } else {
  560. int old_offset = hilighted_offset;
  561. hilighted_offset = -1;
  562. if (old_offset != -1) {
  563. clear_offset(old_offset);
  564. struct Package * f = get_package_at_offset(old_offset);
  565. if (f) {
  566. clear_offset(old_offset);
  567. draw_package(f, old_offset);
  568. }
  569. redraw_window();
  570. }
  571. }
  572. }
  573. }
  574. break;
  575. case YUTANI_MSG_WINDOW_CLOSE:
  576. case YUTANI_MSG_SESSION_END:
  577. _menu_action_exit(NULL);
  578. break;
  579. default:
  580. break;
  581. }
  582. free(m);
  583. m = yutani_poll_async(yctx);
  584. }
  585. }
  586. }