file-browser.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  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. * file-browser - Graphical file manager.
  7. *
  8. * Based on the original Python implementation and inspired by
  9. * Nautilus and Thunar. Also provides a "wallpaper" mode for
  10. * managing the desktop backgrond.
  11. */
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <dirent.h>
  15. #include <time.h>
  16. #include <math.h>
  17. #include <libgen.h>
  18. #include <signal.h>
  19. #include <ctype.h>
  20. #include <errno.h>
  21. #include <sys/stat.h>
  22. #include <sys/time.h>
  23. #include <sys/wait.h>
  24. #include <sys/fswait.h>
  25. #include <toaru/yutani.h>
  26. #include <toaru/graphics.h>
  27. #include <toaru/decorations.h>
  28. #include <toaru/menu.h>
  29. #include <toaru/icon_cache.h>
  30. #include <toaru/list.h>
  31. #include <toaru/sdf.h>
  32. #include <toaru/button.h>
  33. #include <toaru/jpeg.h>
  34. #define APPLICATION_TITLE "File Browser"
  35. #define SCROLL_AMOUNT 120
  36. #define WALLPAPER_PATH "/usr/share/wallpaper.jpg"
  37. struct File {
  38. char name[256]; /* Displayed name (icon label) */
  39. char icon[256]; /* Icon identifier */
  40. char link[256]; /* Link target for symlinks */
  41. char launcher[256]; /* Launcher spec */
  42. char filename[256]; /* Actual filename for launchers */
  43. char filetype[256]; /* Textual description of file type */
  44. uint64_t size; /* File size */
  45. int type; /* File type: 0 = normal, 1 = directory, 2 = launcher */
  46. int selected; /* Selection status */
  47. };
  48. static yutani_t * yctx;
  49. static yutani_window_t * main_window;
  50. static gfx_context_t * ctx;
  51. static int application_running = 1; /* Big loop exit condition */
  52. static int show_hidden = 0; /* Whether or not show hidden files */
  53. static int scroll_offset = 0; /* How far the icon view should be scrolled */
  54. static int available_height = 0; /* How much space is available in the main window for the icon view */
  55. static int is_desktop_background = 0; /* If we're in desktop background mode */
  56. static int menu_bar_height = MENU_BAR_HEIGHT + 36; /* Height of the menu bar, if present - it's not in desktop mode */
  57. static sprite_t * wallpaper_buffer = NULL; /* Prebaked wallpaper texture */
  58. static sprite_t * wallpaper_old = NULL;
  59. static uint64_t timer = 0;
  60. static int restart = 0;
  61. static char title[512]; /* Application title bar */
  62. static int FILE_HEIGHT = 80; /* Height of one row of icons */
  63. static int FILE_WIDTH = 100; /* Width of one column of icons */
  64. static int FILE_PTR_WIDTH = 1; /* How many icons wide the display should be */
  65. static sprite_t * contents_sprite = NULL; /* Icon view rendering context */
  66. static gfx_context_t * contents = NULL; /* Icon view rendering context */
  67. static char * current_directory = NULL; /* Current directory path */
  68. static int hilighted_offset = -1; /* Which file is hovered by the mouse */
  69. static struct File ** file_pointers = NULL; /* List of file pointers */
  70. static ssize_t file_pointers_len = 0; /* How many files are in the current list */
  71. static uint64_t last_click = 0; /* For double click */
  72. static int last_click_offset = -1; /* So that clicking two different things quickly doesn't count as a double click */
  73. static char nav_bar[512] = {0};
  74. static int nav_bar_cursor = 0;
  75. static int nav_bar_cursor_x = 0;
  76. static int nav_bar_focused = 0;
  77. static char window_status[512] = {0};
  78. static int _button_hilights[4] = {3,3,3,3};
  79. static int _button_disabled[4] = {1,1,0,0};
  80. static int _button_hover = -1;
  81. /* Menu bar entries */
  82. static struct menu_bar menu_bar = {0};
  83. static struct menu_bar_entries menu_entries[] = {
  84. {"File", "file"}, /* 0 */
  85. {"Edit", "edit"}, /* 1 */
  86. {"View", "view"}, /* 2 */
  87. {"Go", "go"}, /* 3 */
  88. {"Help", "help"}, /* 4 */
  89. {NULL, NULL},
  90. };
  91. static struct MenuList * context_menu = NULL;
  92. /**
  93. * Accurate time comparison.
  94. *
  95. * These methods were taken from the compositor and
  96. * allow us to time double-clicks accurately.
  97. */
  98. static uint64_t precise_current_time(void) {
  99. struct timeval t;
  100. gettimeofday(&t, NULL);
  101. time_t sec_diff = t.tv_sec;
  102. suseconds_t usec_diff = t.tv_usec;
  103. return (uint64_t)((uint64_t)sec_diff * 1000LL + usec_diff / 1000);
  104. }
  105. static uint64_t precise_time_since(uint64_t start_time) {
  106. uint64_t now = precise_current_time();
  107. uint64_t diff = now - start_time; /* Milliseconds */
  108. return diff;
  109. }
  110. /**
  111. * When in desktop mode, we fake decoration boundaries to
  112. * position the icon view correctly. When in normal mode,
  113. * we just passt through the actual bounds.
  114. */
  115. static int _decor_get_bounds(yutani_window_t * win, struct decor_bounds * bounds) {
  116. if (is_desktop_background) {
  117. memset(bounds, 0, sizeof(struct decor_bounds));
  118. bounds->top_height = 54;
  119. bounds->left_width = 20;
  120. return 0;
  121. }
  122. return decor_get_bounds(win, bounds);
  123. }
  124. /**
  125. * This should probably be in a yutani core library...
  126. *
  127. * If a down and up event were close enough together to be considered a click.
  128. */
  129. static int _close_enough(struct yutani_msg_window_mouse_event * me) {
  130. 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) {
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. /**
  136. * Select view mode.
  137. */
  138. #define VIEW_MODE_ICONS 0
  139. #define VIEW_MODE_TILES 1
  140. #define VIEW_MODE_LIST 2
  141. static int view_mode = VIEW_MODE_ICONS;
  142. /**
  143. * Clear out the space for an icon.
  144. * We clear to transparent so that the desktop background can be shown in desktop mode.
  145. */
  146. static void clear_offset(int offset) {
  147. /* From the flat array offset, figure out the x/y offset. */
  148. int offset_y = offset / FILE_PTR_WIDTH;
  149. int offset_x = offset % FILE_PTR_WIDTH;
  150. draw_rectangle_solid(contents, offset_x * FILE_WIDTH, offset_y * FILE_HEIGHT, FILE_WIDTH, FILE_HEIGHT, rgba(0,0,0,0));
  151. }
  152. static int print_human_readable_size(char * _out, uint64_t s) {
  153. if (s >= 1LL << 30) {
  154. size_t t = s / (1LL << 30);
  155. return sprintf(_out, "%d.%1d GiB", (int)t, (int)((int)(s - t * (1LL << 30)) / ((1LL << 30) / 10)));
  156. } else if (s >= 1<<20) {
  157. size_t t = s / (1 << 20);
  158. return sprintf(_out, "%d.%1d MiB", (int)t, (int)(s - t * (1 << 20)) / ((1 << 20) / 10));
  159. } else if (s >= 1<<10) {
  160. size_t t = s / (1 << 10);
  161. return sprintf(_out, "%d.%1d KiB", (int)t, (int)(s - t * (1 << 10)) / ((1 << 10) / 10));
  162. } else {
  163. return sprintf(_out, "%d B", (int)s);
  164. }
  165. }
  166. #define HILIGHT_BORDER_TOP rgb(54,128,205)
  167. #define HILIGHT_GRADIENT_TOP rgb(93,163,236)
  168. #define HILIGHT_GRADIENT_BOTTOM rgb(56,137,220)
  169. #define HILIGHT_BORDER_BOTTOM rgb(47,106,167)
  170. /**
  171. * Draw an icon view entry
  172. */
  173. static void draw_file(struct File * f, int offset) {
  174. /* From the flat array offset, figure out the x/y offset. */
  175. int offset_y = offset / FILE_PTR_WIDTH;
  176. int offset_x = offset % FILE_PTR_WIDTH;
  177. int x = offset_x * FILE_WIDTH;
  178. int y = offset_y * FILE_HEIGHT;
  179. /* Load the icon sprite from the cache */
  180. if (view_mode == VIEW_MODE_ICONS) {
  181. sprite_t * icon = icon_get_48(f->icon);
  182. /* If the display name is too long to fit, cut it with an ellipsis. */
  183. int len = strlen(f->name);
  184. char * name = malloc(len + 4);
  185. memcpy(name, f->name, len + 1);
  186. int name_width;
  187. while ((name_width = draw_sdf_string_width(name, 16, SDF_FONT_THIN)) > FILE_WIDTH - 8 /* Padding */) {
  188. len--;
  189. name[len+0] = '.';
  190. name[len+1] = '.';
  191. name[len+2] = '.';
  192. name[len+3] = '\0';
  193. }
  194. /* Draw the icon */
  195. int center_x_icon = (FILE_WIDTH - icon->width) / 2;
  196. int center_x_text = (FILE_WIDTH - name_width) / 2;
  197. draw_sprite(contents, icon, center_x_icon + x, y + 2);
  198. if (f->selected) {
  199. /* If this file is selected, paint the icon blue... */
  200. if (main_window->focused) {
  201. draw_sprite_alpha_paint(contents, icon, center_x_icon + x, y + 2, 0.5, rgb(72,167,255));
  202. }
  203. /* And draw the name with a blue background and white text */
  204. draw_rounded_rectangle(contents, center_x_text + x - 2, y + 54, name_width + 6, 20, 3, rgb(72,167,255));
  205. draw_sdf_string(contents, center_x_text + x, y + 54, name, 16, rgb(255,255,255), SDF_FONT_THIN);
  206. } else {
  207. if (is_desktop_background) {
  208. /* If this is the desktop view, white text with a drop shadow */
  209. draw_sdf_string_stroke(contents, center_x_text + x + 1, y + 55, name, 16, rgba(0,0,0,120), SDF_FONT_THIN, 1.7, 0.5);
  210. draw_sdf_string(contents, center_x_text + x, y + 54, name, 16, rgb(255,255,255), SDF_FONT_THIN);
  211. } else {
  212. /* Otherwise, black text */
  213. draw_sdf_string(contents, center_x_text + x, y + 54, name, 16, rgb(0,0,0), SDF_FONT_THIN);
  214. }
  215. }
  216. if (offset == hilighted_offset) {
  217. /* The hovered icon should have some added brightness, so paint it white */
  218. draw_sprite_alpha_paint(contents, icon, center_x_icon + x, y + 2, 0.3, rgb(255,255,255));
  219. }
  220. if (f->link[0]) {
  221. /* For symlinks, draw an indicator */
  222. sprite_t * arrow = icon_get_16("forward");
  223. draw_sprite(contents, arrow, center_x_icon + 32 + x, y + 32);
  224. }
  225. free(name);
  226. } else if (view_mode == VIEW_MODE_TILES) {
  227. sprite_t * icon = icon_get_48(f->icon);
  228. uint32_t text_color = rgb(0,0,0);
  229. /* If selected, draw background box */
  230. if (f->selected) {
  231. struct gradient_definition edge = {FILE_HEIGHT - 4, y+2, HILIGHT_BORDER_TOP, HILIGHT_BORDER_BOTTOM};
  232. struct gradient_definition body = {FILE_HEIGHT - 6, y+3, HILIGHT_GRADIENT_TOP, HILIGHT_GRADIENT_BOTTOM};
  233. draw_rounded_rectangle_pattern(contents, x + 2,y + 2, FILE_WIDTH-4, FILE_HEIGHT-4, 3, gfx_vertical_gradient_pattern, &edge);
  234. draw_rounded_rectangle_pattern(contents, x + 3,y + 3, FILE_WIDTH-6, FILE_HEIGHT-6, 4, gfx_vertical_gradient_pattern, &body);
  235. text_color = rgb(255,255,255);
  236. }
  237. draw_sprite(contents, icon, x + 11, y + 11);
  238. if (offset == hilighted_offset) {
  239. /* The hovered icon should have some added brightness, so paint it white */
  240. draw_sprite_alpha_paint(contents, icon, x + 11, y + 11, 0.3, rgb(255,255,255));
  241. }
  242. int len = strlen(f->name);
  243. char * name = malloc(len + 4);
  244. memcpy(name, f->name, len + 1);
  245. int name_width;
  246. while ((name_width = draw_sdf_string_width(name, 16, SDF_FONT_BOLD)) > FILE_WIDTH - 81) {
  247. len--;
  248. name[len+0] = '.';
  249. name[len+1] = '.';
  250. name[len+2] = '.';
  251. name[len+3] = '\0';
  252. }
  253. if (f->type == 0) {
  254. draw_sdf_string(contents, x + 70, y + 8, name, 16, text_color, SDF_FONT_BOLD);
  255. draw_sdf_string(contents, x + 70, y + 25, f->filetype, 16, text_color, SDF_FONT_THIN);
  256. char line_three[48] = {0};
  257. if (*f->link) {
  258. sprintf(line_three, "Symbolic link");
  259. } else {
  260. print_human_readable_size(line_three, f->size);
  261. }
  262. draw_sdf_string(contents, x + 70, y + 42, line_three, 16, text_color, SDF_FONT_THIN);
  263. } else {
  264. draw_sdf_string(contents, x + 70, y + 15, name, 16, text_color, SDF_FONT_BOLD);
  265. draw_sdf_string(contents, x + 70, y + 32, f->filetype, 16, text_color, SDF_FONT_THIN);
  266. }
  267. free(name);
  268. } else if (view_mode == VIEW_MODE_LIST) {
  269. sprite_t * icon = icon_get_16(f->icon);
  270. uint32_t text_color = rgb(0,0,0);
  271. if (f->selected) {
  272. struct gradient_definition edge = {FILE_HEIGHT - 4, y+2, HILIGHT_BORDER_TOP, HILIGHT_BORDER_BOTTOM};
  273. struct gradient_definition body = {FILE_HEIGHT - 6, y+3, HILIGHT_GRADIENT_TOP, HILIGHT_GRADIENT_BOTTOM};
  274. draw_rounded_rectangle_pattern(contents, x + 2,y + 2, FILE_WIDTH-4, FILE_HEIGHT-4, 3, gfx_vertical_gradient_pattern, &edge);
  275. draw_rounded_rectangle_pattern(contents, x + 3,y + 3, FILE_WIDTH-6, FILE_HEIGHT-6, 4, gfx_vertical_gradient_pattern, &body);
  276. text_color = rgb(255,255,255);
  277. } else if (offset == hilighted_offset) {
  278. draw_rounded_rectangle(contents, x + 2, y + 2, FILE_WIDTH - 4, FILE_HEIGHT - 4, 3, rgb(180,180,180));
  279. draw_rounded_rectangle(contents, x + 3, y + 3, FILE_WIDTH - 6, FILE_HEIGHT - 6, 4, rgb(255,255,255));
  280. }
  281. if (icon->width != 16 || icon->height != 16) {
  282. draw_sprite_scaled(contents, icon, x + 4, y + 4, 16, 16);
  283. } else {
  284. draw_sprite(contents, icon, x + 4, y + 4);
  285. }
  286. int len = strlen(f->name);
  287. char * name = malloc(len + 4);
  288. memcpy(name, f->name, len + 1);
  289. int name_width;
  290. while ((name_width = draw_sdf_string_width(name, 16, SDF_FONT_THIN)) > FILE_WIDTH - 26) {
  291. len--;
  292. name[len+0] = '.';
  293. name[len+1] = '.';
  294. name[len+2] = '.';
  295. name[len+3] = '\0';
  296. }
  297. draw_sdf_string(contents, x + 24, y + 2, name, 16, text_color, SDF_FONT_THIN);
  298. }
  299. }
  300. /**
  301. * Get file from array offset, with bounds check
  302. */
  303. static struct File * get_file_at_offset(int offset) {
  304. if (offset >= 0 && offset < file_pointers_len) {
  305. return file_pointers[offset];
  306. }
  307. return NULL;
  308. }
  309. /**
  310. * Redraw all icon view entries
  311. */
  312. static void redraw_files(void) {
  313. /* Fill to blank */
  314. draw_fill(contents, rgba(0,0,0,0));
  315. for (int i = 0; i < file_pointers_len; ++i) {
  316. draw_file(file_pointers[i], i);
  317. }
  318. }
  319. /**
  320. * Set the application title.
  321. */
  322. static void set_title(char * directory) {
  323. /* Do nothing in desktop mode to avoid advertisement. */
  324. if (is_desktop_background) return;
  325. /* If the directory name is set... */
  326. if (directory) {
  327. sprintf(title, "%s - " APPLICATION_TITLE, directory);
  328. } else {
  329. /* Otherwise, just "File Browser" */
  330. sprintf(title, APPLICATION_TITLE);
  331. }
  332. /* Advertise to the panel */
  333. yutani_window_advertise_icon(yctx, main_window, title, "folder");
  334. }
  335. /**
  336. * Check if a file name ends with an extension.
  337. *
  338. * Can also be used for exact matches.
  339. */
  340. static int has_extension(struct File * f, char * extension) {
  341. int i = strlen(f->name);
  342. int j = strlen(extension);
  343. do {
  344. if (f->name[i] != (extension)[j]) break;
  345. if (j == 0) return 1;
  346. if (i == 0) break;
  347. i--;
  348. j--;
  349. } while (1);
  350. return 0;
  351. }
  352. static list_t * history_back;
  353. static list_t * history_forward;
  354. static void update_status(void) {
  355. uint64_t total_size = 0;
  356. uint64_t selected_size = 0;
  357. int selected_count = 0;
  358. struct File * selected = NULL;
  359. for (int i = 0; i < file_pointers_len; ++i) {
  360. total_size += file_pointers[i]->size;
  361. if (file_pointers[i]->selected) {
  362. selected_count += 1;
  363. selected_size += file_pointers[i]->size;
  364. selected = file_pointers[i];
  365. }
  366. }
  367. char tmp_size[50];
  368. if (selected_count == 0) {
  369. print_human_readable_size(tmp_size, total_size);
  370. sprintf(window_status, "%d item%s (%s)", file_pointers_len, file_pointers_len == 1 ? "" : "s", tmp_size);
  371. } else if (selected_count == 1) {
  372. print_human_readable_size(tmp_size, selected->size);
  373. sprintf(window_status, "\"%s\" (%s) %s", selected->name, tmp_size, selected->filetype);
  374. } else {
  375. print_human_readable_size(tmp_size, selected_size);
  376. sprintf(window_status, "%d items selected (%s)", selected_count, tmp_size);
  377. }
  378. }
  379. /**
  380. * Read the contents of a directory into the icon view.
  381. */
  382. static void load_directory(const char * path, int modifies_history) {
  383. /* Free the current icon view entries */
  384. DIR * dirp = opendir(path);
  385. if (!dirp) {
  386. /* XXX show a dialog */
  387. if (!fork()) {
  388. char tmp[512];
  389. sprintf(tmp, "Could not open directory \"%s\": %s", path, strerror(errno));
  390. char * args[] = {"showdialog","File Browser","/usr/share/icons/48/folder.bmp",tmp,NULL};
  391. execvp(args[0],args);
  392. exit(0);
  393. }
  394. return;
  395. }
  396. if (file_pointers) {
  397. for (int i = 0; i < file_pointers_len; ++i) {
  398. free(file_pointers[i]);
  399. }
  400. free(file_pointers);
  401. }
  402. if (modifies_history) {
  403. /* Clear forward history */
  404. list_destroy(history_forward);
  405. list_free(history_forward);
  406. free(history_forward);
  407. history_forward = list_create();
  408. /* Append current pointer */
  409. if (current_directory) {
  410. list_insert(history_back, strdup(current_directory));
  411. }
  412. }
  413. if (current_directory) {
  414. free(current_directory);
  415. }
  416. _button_disabled[0] = !(history_back->length);
  417. _button_disabled[1] = !(history_forward->length);
  418. _button_disabled[2] = 0;
  419. _button_disabled[3] = 0;
  420. char * home = getenv("HOME");
  421. if (home && !strcmp(path, home)) {
  422. /* If the current directory is the user's homedir, present it that way in the title */
  423. set_title("Home");
  424. _button_disabled[3] = 1;
  425. } else if (!strcmp(path, "/")) {
  426. set_title("File System");
  427. _button_disabled[2] = 1;
  428. } else {
  429. /* Otherwise use just the directory base name */
  430. char * tmp = strdup(path);
  431. char * base = basename(tmp);
  432. set_title(base);
  433. free(tmp);
  434. }
  435. /* If we ended up in a path with //two/initial/slashes, fix that. */
  436. if (path[0] == '/' && path[1] == '/') {
  437. current_directory = strdup(path+1);
  438. } else {
  439. current_directory = strdup(path);
  440. }
  441. strcpy(nav_bar, current_directory);
  442. /* TODO: Show relative time informaton... */
  443. #if 0
  444. /* Get the current time */
  445. struct tm * timeinfo;
  446. struct timeval now;
  447. gettimeofday(&now, NULL); //time(NULL);
  448. timeinfo = localtime((time_t *)&now.tv_sec);
  449. int this_year = timeinfo->tm_year;
  450. #endif
  451. list_t * file_list = list_create();
  452. struct dirent * ent = readdir(dirp);
  453. while (ent != NULL) {
  454. if (ent->d_name[0] == '.' &&
  455. (ent->d_name[1] == '\0' ||
  456. (ent->d_name[1] == '.' &&
  457. ent->d_name[2] == '\0'))) {
  458. /* skip . and .. */
  459. ent = readdir(dirp);
  460. continue;
  461. }
  462. if (show_hidden || (ent->d_name[0] != '.')) {
  463. /* Set display name from file name */
  464. struct File * f = malloc(sizeof(struct File));
  465. sprintf(f->name, "%s", ent->d_name); /* snprintf? copy min()? */
  466. struct stat statbuf;
  467. struct stat statbufl;
  468. /* Calculate absolute path to file */
  469. char tmp[strlen(path)+strlen(ent->d_name)+2];
  470. sprintf(tmp, "%s/%s", path, ent->d_name);
  471. lstat(tmp, &statbuf);
  472. f->size = statbuf.st_size;
  473. /* Read link target for symlinks */
  474. if (S_ISLNK(statbuf.st_mode)) {
  475. memcpy(&statbufl, &statbuf, sizeof(struct stat));
  476. stat(tmp, &statbuf);
  477. readlink(tmp, f->link, 256);
  478. } else {
  479. f->link[0] = '\0';
  480. }
  481. f->launcher[0] = '\0';
  482. f->filetype[0] = '\0';
  483. f->selected = 0;
  484. if (S_ISDIR(statbuf.st_mode)) {
  485. /* Directory */
  486. sprintf(f->icon, "folder");
  487. sprintf(f->filetype, "Directory");
  488. f->type = 1;
  489. } else {
  490. /* Regular file */
  491. /* Default regular files to open in bim */
  492. sprintf(f->launcher, "exec terminal bim");
  493. if (is_desktop_background && has_extension(f, ".launcher")) {
  494. /* In desktop mode, read launchers specially */
  495. FILE * file = fopen(tmp,"r");
  496. char tbuf[1024];
  497. while (!feof(file)) {
  498. fgets(tbuf, 1024, file);
  499. char * nl = strchr(tbuf,'\n');
  500. if (nl) *nl = '\0';
  501. char * eq = strchr(tbuf,'=');
  502. if (!eq) continue;
  503. *eq = '\0'; eq++;
  504. if (!strcmp(tbuf, "icon")) {
  505. sprintf(f->icon, "%s", eq);
  506. } else if (!strcmp(tbuf, "run")) {
  507. sprintf(f->launcher, "%s #", eq);
  508. } else if (!strcmp(tbuf, "title")) {
  509. sprintf(f->name, eq);
  510. }
  511. }
  512. sprintf(f->filetype, "Launcher");
  513. sprintf(f->filename, "%s", ent->d_name);
  514. f->type = 2;
  515. } else {
  516. /* Handle various file types */
  517. if (has_extension(f, ".c")) {
  518. sprintf(f->icon, "c");
  519. sprintf(f->filetype, "C Source");
  520. } else if (has_extension(f, ".h")) {
  521. sprintf(f->icon, "h");
  522. sprintf(f->filetype, "C Header");
  523. } else if (has_extension(f, ".bmp")) {
  524. sprintf(f->icon, "image");
  525. sprintf(f->launcher, "exec imgviewer");
  526. sprintf(f->filetype, "Bitmap Image");
  527. } else if (has_extension(f, ".tga")) {
  528. sprintf(f->icon, "image");
  529. sprintf(f->launcher, "exec imgviewer");
  530. sprintf(f->filetype, "Targa Image");
  531. } else if (has_extension(f, ".jpg") || has_extension(f,".jpeg")) {
  532. sprintf(f->icon, "image");
  533. sprintf(f->launcher, "exec imgviewer");
  534. sprintf(f->filetype, "JPEG Image");
  535. } else if (has_extension(f, ".sdf")) {
  536. sprintf(f->icon, "font");
  537. sprintf(f->filetype, "SDF Font");
  538. /* TODO: Font viewer for SDF and TrueType */
  539. } else if (has_extension(f, ".ttf")) {
  540. sprintf(f->icon, "font");
  541. sprintf(f->filetype, "TrueType Font");
  542. } else if (has_extension(f, ".tgz") || has_extension(f, ".tar.gz")) {
  543. sprintf(f->icon, "package");
  544. sprintf(f->filetype, "Compressed Archive File");
  545. } else if (has_extension(f, ".tar")) {
  546. sprintf(f->icon, "package");
  547. sprintf(f->filetype, "Archive File");
  548. } else if (has_extension(f, ".sh")) {
  549. sprintf(f->icon, "sh");
  550. if (statbuf.st_mode & 0111) {
  551. /* Make executable */
  552. sprintf(f->launcher, "SELF");
  553. sprintf(f->filetype, "Executable Shell Script");
  554. } else {
  555. sprintf(f->filetype, "Shell Script");
  556. }
  557. } else if (has_extension(f, ".py")) {
  558. sprintf(f->icon, "py");
  559. if (statbuf.st_mode & 0111) {
  560. /* Make executable */
  561. sprintf(f->launcher, "SELF");
  562. sprintf(f->filetype, "Executable Python Script");
  563. } else {
  564. sprintf(f->filetype, "Python Script");
  565. }
  566. } else if (has_extension(f, ".ko")) {
  567. sprintf(f->icon, "file");
  568. sprintf(f->filetype, "Kernel Module");
  569. } else if (has_extension(f, ".o")) {
  570. sprintf(f->icon, "file");
  571. sprintf(f->filetype, "Object File");
  572. } else if (has_extension(f, ".so")) {
  573. sprintf(f->icon, "file");
  574. sprintf(f->filetype, "Shared Object File");
  575. } else if (has_extension(f, ".S")) {
  576. sprintf(f->icon, "file");
  577. sprintf(f->filetype, "Assembly Source");
  578. } else if (has_extension(f, ".ld")) {
  579. sprintf(f->icon, "file");
  580. sprintf(f->filetype, "Linker Script");
  581. } else if (statbuf.st_mode & 0111) {
  582. /* Executable files - use their name for their icon, and launch themselves. */
  583. sprintf(f->icon, "%s", f->name);
  584. sprintf(f->launcher, "SELF");
  585. sprintf(f->filetype, "Executable");
  586. } else {
  587. sprintf(f->icon, "file");
  588. sprintf(f->filetype, "File");
  589. }
  590. f->type = 0;
  591. }
  592. }
  593. list_insert(file_list, f);
  594. }
  595. ent = readdir(dirp);
  596. }
  597. closedir(dirp);
  598. /* Store the entries in a flat array. */
  599. file_pointers = malloc(sizeof(struct File *) * file_list->length);
  600. file_pointers_len = file_list->length;
  601. int i = 0;
  602. foreach (node, file_list) {
  603. file_pointers[i] = node->value;
  604. i++;
  605. }
  606. update_status();
  607. /* Free our temporary linked list */
  608. list_free(file_list);
  609. free(file_list);
  610. /* Sort files */
  611. int comparator(const void * c1, const void * c2) {
  612. const struct File * f1 = *(const struct File **)(c1);
  613. const struct File * f2 = *(const struct File **)(c2);
  614. /* Launchers before directories before files */
  615. if (f1->type > f2->type) return -1;
  616. if (f2->type > f1->type) return 1;
  617. /* Launchers sorted by filename, not by display name */
  618. if (f1->type == 2 && f2->type == 2) {
  619. return strcmp(f1->filename, f2->filename);
  620. }
  621. /* Files sorted by name */
  622. return strcmp(f1->name, f2->name);
  623. }
  624. qsort(file_pointers, file_pointers_len, sizeof(struct File *), comparator);
  625. /* Reset scroll offset when navigating */
  626. scroll_offset = 0;
  627. }
  628. /**
  629. * Resize and redraw the icon view */
  630. static void reinitialize_contents(void) {
  631. /* If there already is a context, free it. */
  632. if (contents) {
  633. free(contents);
  634. }
  635. /* If there already is a context buffer, free it. */
  636. if (contents_sprite) {
  637. sprite_free(contents_sprite);
  638. }
  639. /* Get window bounds to determine how wide we can make our icon view */
  640. struct decor_bounds bounds;
  641. _decor_get_bounds(main_window, &bounds);
  642. if (is_desktop_background) {
  643. /**
  644. * TODO: Actually calculate an optimal FILE_PTR_WIDTH or fix this to
  645. * work properly with vertical rows of files
  646. */
  647. FILE_PTR_WIDTH = 1;
  648. } else if (view_mode == VIEW_MODE_LIST) {
  649. FILE_PTR_WIDTH = 1;
  650. FILE_WIDTH = (ctx->width - bounds.width);
  651. } else {
  652. FILE_PTR_WIDTH = (ctx->width - bounds.width) / FILE_WIDTH;
  653. }
  654. /* Calculate required height to fit files */
  655. int calculated_height = (file_pointers_len / FILE_PTR_WIDTH + 1) * FILE_HEIGHT;
  656. /* Create buffer */
  657. contents_sprite = create_sprite(FILE_PTR_WIDTH * FILE_WIDTH, calculated_height, ALPHA_EMBEDDED);
  658. contents = init_graphics_sprite(contents_sprite);
  659. /* Draw file entries */
  660. redraw_files();
  661. }
  662. #define BUTTON_SPACE 34
  663. #define BUTTON_COUNT 4
  664. static void _draw_buttons(struct decor_bounds bounds) {
  665. uint32_t gradient_top = rgb(59,59,59);
  666. uint32_t gradient_bot = rgb(40,40,40);
  667. for (int i = 0; i < 36; ++i) {
  668. uint32_t c = interp_colors(gradient_top, gradient_bot, i * 255 / 36);
  669. draw_rectangle(ctx, bounds.left_width, bounds.top_height + MENU_BAR_HEIGHT + i,
  670. BUTTON_SPACE * BUTTON_COUNT, 1, c);
  671. //ctx->width - bounds.width, 1, c);
  672. }
  673. int x = 0;
  674. int i = 0;
  675. #define draw_button(label) do { \
  676. struct TTKButton _up = {bounds.left_width + 2 + x,bounds.top_height + MENU_BAR_HEIGHT + 2,32,32,"\033" label,_button_hilights[i] | (_button_disabled[i] << 8)}; \
  677. ttk_button_draw(ctx, &_up); \
  678. x += BUTTON_SPACE; i++; } while (0)
  679. draw_button("back");
  680. draw_button("forward");
  681. draw_button("up");
  682. draw_button("home");
  683. }
  684. static void _figure_out_navbar_cursor(int x, struct decor_bounds bounds) {
  685. x = x - bounds.left_width - 2 - BUTTON_SPACE * BUTTON_COUNT - 5;
  686. if (x <= 0) {
  687. nav_bar_cursor_x = 0;
  688. return;
  689. }
  690. char * tmp = strdup(nav_bar);
  691. int candidate = 0;
  692. while (*tmp && x + 2 < (candidate = draw_sdf_string_width(tmp, 16, SDF_FONT_THIN))) {
  693. tmp[strlen(tmp)-1] = '\0';
  694. }
  695. nav_bar_cursor_x = candidate + 2;
  696. nav_bar_cursor = strlen(tmp);
  697. free(tmp);
  698. }
  699. static void _recalculate_nav_bar_cursor(void) {
  700. if (nav_bar_cursor < 0) {
  701. nav_bar_cursor = 0;
  702. }
  703. if (nav_bar_cursor > (int)strlen(nav_bar)) {
  704. nav_bar_cursor = strlen(nav_bar);
  705. }
  706. char * tmp = strdup(nav_bar);
  707. tmp[nav_bar_cursor] = '\0';
  708. nav_bar_cursor_x = draw_sdf_string_width(tmp, 16, SDF_FONT_THIN) + 2;
  709. free(tmp);
  710. }
  711. static void _draw_nav_bar(struct decor_bounds bounds) {
  712. uint32_t gradient_top = rgb(59,59,59);
  713. uint32_t gradient_bot = rgb(40,40,40);
  714. int x = BUTTON_SPACE * BUTTON_COUNT;
  715. for (int i = 0; i < 36; ++i) {
  716. uint32_t c = interp_colors(gradient_top, gradient_bot, i * 255 / 36);
  717. draw_rectangle(ctx, bounds.left_width + BUTTON_SPACE * BUTTON_COUNT, bounds.top_height + MENU_BAR_HEIGHT + i,
  718. ctx->width - bounds.width - BUTTON_SPACE * BUTTON_COUNT, 1, c);
  719. }
  720. if (nav_bar_focused) {
  721. struct gradient_definition edge = {28, bounds.top_height + MENU_BAR_HEIGHT + 3, rgb(0,120,220), rgb(0,120,220)};
  722. draw_rounded_rectangle_pattern(ctx, bounds.left_width + 2 + x + 1, bounds.top_height + MENU_BAR_HEIGHT + 4, main_window->width - bounds.width - x - 6, 26, 4, gfx_vertical_gradient_pattern, &edge);
  723. draw_rounded_rectangle(ctx, bounds.left_width + 2 + x + 3, bounds.top_height + MENU_BAR_HEIGHT + 6, main_window->width - bounds.width - x - 10, 22, 3, rgb(250,250,250));
  724. } else {
  725. struct gradient_definition edge = {28, bounds.top_height + MENU_BAR_HEIGHT + 3, rgb(90,90,90), rgb(110,110,110)};
  726. draw_rounded_rectangle_pattern(ctx, bounds.left_width + 2 + x + 1, bounds.top_height + MENU_BAR_HEIGHT + 4, main_window->width - bounds.width - x - 6, 26, 4, gfx_vertical_gradient_pattern, &edge);
  727. draw_rounded_rectangle(ctx, bounds.left_width + 2 + x + 2, bounds.top_height + MENU_BAR_HEIGHT + 5, main_window->width - bounds.width - x - 8, 24, 3, rgb(250,250,250));
  728. }
  729. /* Draw the nav bar text */
  730. int max_width = main_window->width - bounds.width - x - 12;
  731. int len = strlen(nav_bar);
  732. char * name = malloc(len + 5);
  733. memcpy(name, nav_bar, len + 1);
  734. int name_width;
  735. while ((name_width = draw_sdf_string_width(name, 16, SDF_FONT_THIN)) > max_width) {
  736. len--;
  737. name[len+0] = '.';
  738. name[len+1] = '.';
  739. name[len+2] = '.';
  740. name[len+3] = '\0';
  741. }
  742. draw_sdf_string(ctx, bounds.left_width + 2 + x + 5, bounds.top_height + MENU_BAR_HEIGHT + 8, name, 16, rgb(0,0,0), SDF_FONT_THIN);
  743. if (nav_bar_focused) {
  744. /* Draw cursor indicator at cursor_x */
  745. draw_line(ctx,
  746. bounds.left_width + 2 + x + 5 + nav_bar_cursor_x,
  747. bounds.left_width + 2 + x + 5 + nav_bar_cursor_x,
  748. bounds.top_height + MENU_BAR_HEIGHT + 8,
  749. bounds.top_height + MENU_BAR_HEIGHT + 8 + 15,
  750. rgb(0,0,0));
  751. }
  752. }
  753. #define STATUS_HEIGHT 24
  754. static void _draw_status(struct decor_bounds bounds) {
  755. uint32_t gradient_top = rgb(80,80,80);
  756. uint32_t gradient_bot = rgb(59,59,59);
  757. draw_rectangle(ctx, bounds.left_width, ctx->height - bounds.bottom_height - STATUS_HEIGHT,
  758. ctx->width - bounds.width, 1, rgb(110,110,110) );
  759. for (int i = 1; i < STATUS_HEIGHT; ++i) {
  760. uint32_t c = interp_colors(gradient_top, gradient_bot, i * 255 / STATUS_HEIGHT);
  761. draw_rectangle(ctx, bounds.left_width, ctx->height - bounds.bottom_height - STATUS_HEIGHT + i,
  762. ctx->width - bounds.width, 1, c );
  763. }
  764. {
  765. sprite_t * _tmp_s = create_sprite(ctx->width - bounds.width - 4, STATUS_HEIGHT-3, ALPHA_EMBEDDED);
  766. gfx_context_t * _tmp = init_graphics_sprite(_tmp_s);
  767. draw_fill(_tmp, rgba(0,0,0,0));
  768. draw_sdf_string(_tmp, 1, 1, window_status, 16, rgb(0,0,0), SDF_FONT_THIN);
  769. blur_context_box(_tmp, 4);
  770. draw_sdf_string(_tmp, 0, 0, window_status, 16, rgb(255,255,255), SDF_FONT_THIN);
  771. free(_tmp);
  772. draw_sprite(ctx, _tmp_s, bounds.left_width + 4, ctx->height - bounds.bottom_height - STATUS_HEIGHT + 3);
  773. sprite_free(_tmp_s);
  774. }
  775. }
  776. static void _redraw_nav_bar(void) {
  777. struct decor_bounds bounds;
  778. _decor_get_bounds(main_window, &bounds);
  779. _draw_nav_bar(bounds);
  780. flip(ctx);
  781. yutani_flip(yctx, main_window);
  782. }
  783. static void nav_bar_backspace_word(void) {
  784. if (!*nav_bar) return;
  785. if (nav_bar_cursor == 0) return;
  786. char * after = strdup(&nav_bar[nav_bar_cursor]);
  787. if (nav_bar[nav_bar_cursor-1] == '/') {
  788. nav_bar[nav_bar_cursor-1] = '\0';
  789. nav_bar_cursor--;
  790. }
  791. while (nav_bar_cursor && nav_bar[nav_bar_cursor-1] != '/') {
  792. nav_bar[nav_bar_cursor-1] = '\0';
  793. nav_bar_cursor--;
  794. }
  795. strcat(nav_bar, after);
  796. free(after);
  797. _recalculate_nav_bar_cursor();
  798. _redraw_nav_bar();
  799. }
  800. static void nav_bar_backspace(void) {
  801. if (nav_bar_cursor == 0) return;
  802. char * after = strdup(&nav_bar[nav_bar_cursor]);
  803. nav_bar[nav_bar_cursor-1] = '\0';
  804. nav_bar_cursor--;
  805. strcat(nav_bar, after);
  806. free(after);
  807. _recalculate_nav_bar_cursor();
  808. _redraw_nav_bar();
  809. }
  810. static void nav_bar_insert_char(char c) {
  811. char * tmp = strdup(nav_bar);
  812. tmp[nav_bar_cursor] = '\0';
  813. char * after = strdup(&nav_bar[nav_bar_cursor]);
  814. sprintf(nav_bar, "%s%c%s", tmp, c, after);
  815. free(tmp);
  816. free(after);
  817. nav_bar_cursor += 1;
  818. _recalculate_nav_bar_cursor();
  819. _redraw_nav_bar();
  820. }
  821. static void nav_bar_cursor_left(void) {
  822. nav_bar_cursor--;
  823. _recalculate_nav_bar_cursor();
  824. _redraw_nav_bar();
  825. }
  826. static void nav_bar_cursor_right(void) {
  827. nav_bar_cursor++;
  828. _recalculate_nav_bar_cursor();
  829. _redraw_nav_bar();
  830. }
  831. /**
  832. * Redraw the entire window.
  833. */
  834. static void redraw_window(void) {
  835. if (!is_desktop_background) {
  836. /* Clear to white and draw decorations */
  837. draw_fill(ctx, rgb(255,255,255));
  838. render_decorations(main_window, ctx, title);
  839. } else {
  840. /* Draw wallpaper in desktop mode */
  841. if (wallpaper_old) {
  842. draw_sprite(ctx, wallpaper_old, 0, 0);
  843. uint64_t ellapsed = precise_time_since(timer);
  844. if (ellapsed > 1000) {
  845. free(wallpaper_old);
  846. wallpaper_old = NULL;
  847. draw_sprite(ctx, wallpaper_buffer, 0, 0);
  848. restart = 1; /* quietly restart */
  849. } else {
  850. draw_sprite_alpha(ctx, wallpaper_buffer, 0, 0, (float)ellapsed / 1000.0);
  851. }
  852. } else {
  853. draw_sprite(ctx, wallpaper_buffer, 0, 0);
  854. }
  855. }
  856. struct decor_bounds bounds;
  857. _decor_get_bounds(main_window, &bounds);
  858. if (!is_desktop_background) {
  859. /* Position, size, and draw the menu bar */
  860. menu_bar.x = bounds.left_width;
  861. menu_bar.y = bounds.top_height;
  862. menu_bar.width = ctx->width - bounds.width;
  863. menu_bar.window = main_window;
  864. menu_bar_render(&menu_bar, ctx);
  865. /* Draw toolbar */
  866. _draw_buttons(bounds);
  867. _draw_nav_bar(bounds);
  868. _draw_status(bounds);
  869. }
  870. /* Draw the icon view, clipped to the viewport and scrolled appropriately. */
  871. gfx_clear_clip(ctx);
  872. gfx_add_clip(ctx, bounds.left_width, bounds.top_height + menu_bar_height, ctx->width - bounds.width, available_height);
  873. draw_sprite(ctx, contents_sprite, bounds.left_width, bounds.top_height + menu_bar_height - scroll_offset);
  874. gfx_clear_clip(ctx);
  875. gfx_add_clip(ctx, 0, 0, ctx->width, ctx->height);
  876. /* Flip graphics context and inform compositor */
  877. flip(ctx);
  878. yutani_flip(yctx, main_window);
  879. }
  880. /**
  881. * Loads and bakes the wallpaper to the appropriate size.
  882. */
  883. static void draw_background(int width, int height) {
  884. /* If the wallpaper is already loaded, free it. */
  885. if (wallpaper_buffer) {
  886. if (wallpaper_old) {
  887. free(wallpaper_old);
  888. }
  889. wallpaper_old = wallpaper_buffer;
  890. timer = precise_current_time();
  891. }
  892. /* Open the wallpaper */
  893. sprite_t * wallpaper = malloc(sizeof(sprite_t));
  894. char * wallpaper_path = WALLPAPER_PATH;
  895. int free_it = 0;
  896. char * home = getenv("HOME");
  897. if (home) {
  898. char tmp[512];
  899. sprintf(tmp, "%s/.wallpaper.conf", home);
  900. FILE * c = fopen(tmp, "r");
  901. if (c) {
  902. char line[1024];
  903. while (!feof(c)) {
  904. fgets(line, 1024, c);
  905. char * nl = strchr(line, '\n');
  906. if (nl) *nl = '\0';
  907. if (line[0] == ';') {
  908. continue;
  909. }
  910. if (strstr(line, "wallpaper=") == line) {
  911. free_it = 1;
  912. wallpaper_path = strdup(line+strlen("wallpaper="));
  913. break;
  914. }
  915. }
  916. fclose(c);
  917. }
  918. }
  919. load_sprite_jpg(wallpaper, wallpaper_path);
  920. if (free_it) {
  921. free(wallpaper_path);
  922. }
  923. /* Create a new buffer to hold the baked wallpaper */
  924. wallpaper_buffer = create_sprite(width, height, 0);
  925. gfx_context_t * ctx = init_graphics_sprite(wallpaper_buffer);
  926. /* Calculate the appropriate scaled size to fit the screen. */
  927. float x = (float)width / (float)wallpaper->width;
  928. float y = (float)height / (float)wallpaper->height;
  929. int nh = (int)(x * (float)wallpaper->height);
  930. int nw = (int)(y * (float)wallpaper->width);
  931. /* Clear to black to avoid odd transparency issues along edges */
  932. draw_fill(ctx, rgb(0,0,0));
  933. /* Scale the wallpaper into the buffer. */
  934. if (nw == wallpaper->width && nh == wallpaper->height) {
  935. /* No scaling necessary */
  936. draw_sprite(ctx, wallpaper, 0, 0);
  937. } else if (nw >= width) {
  938. /* Scaled wallpaper is wider, height should match. */
  939. draw_sprite_scaled(ctx, wallpaper, ((int)width - nw) / 2, 0, nw+2, height);
  940. } else {
  941. /* Scaled wallpaper is taller, width should match. */
  942. draw_sprite_scaled(ctx, wallpaper, 0, ((int)height - nh) / 2, width+2, nh);
  943. }
  944. /* Free the original wallpaper. */
  945. sprite_free(wallpaper);
  946. free(ctx);
  947. }
  948. /**
  949. * Resize window when asked by the compositor.
  950. */
  951. static void resize_finish(int w, int h) {
  952. if (w < 300 || h < 300) {
  953. yutani_window_resize_offer(yctx, main_window, w < 300 ? 300 : w, h < 300 ? 300 : h);
  954. return;
  955. }
  956. int width_changed = (main_window->width != (unsigned int)w);
  957. yutani_window_resize_accept(yctx, main_window, w, h);
  958. reinit_graphics_yutani(ctx, main_window);
  959. struct decor_bounds bounds;
  960. _decor_get_bounds(main_window, &bounds);
  961. /* Recalculate available size */
  962. available_height = ctx->height - menu_bar_height - bounds.height - (is_desktop_background ? 0 : STATUS_HEIGHT);
  963. fprintf(stderr, "available_height = %d; bounds.bottom_height = %d, (isd...) = %d\n", available_height, bounds.bottom_height, (is_desktop_background ? 0 : STATUS_HEIGHT));
  964. /* If the width changed, we need to rebuild the icon view */
  965. if (width_changed) {
  966. reinitialize_contents();
  967. }
  968. /* Make sure we're not scrolled weirdly after resizing */
  969. if (available_height > contents->height) {
  970. scroll_offset = 0;
  971. } else {
  972. if (scroll_offset > contents->height - available_height) {
  973. scroll_offset = contents->height - available_height;
  974. }
  975. }
  976. /* If the desktop background changes size, we have to reload and rescale the wallpaper */
  977. if (is_desktop_background) {
  978. draw_background(w, h);
  979. }
  980. /* Redraw */
  981. redraw_window();
  982. yutani_window_resize_done(yctx, main_window);
  983. yutani_flip(yctx, main_window);
  984. }
  985. /* TODO: We don't have an input box yet. */
  986. #if 0
  987. static void _menu_action_input_path(struct MenuEntry * entry) {
  988. }
  989. #endif
  990. /* File > Exit */
  991. static void _menu_action_exit(struct MenuEntry * entry) {
  992. application_running = 0;
  993. }
  994. /* Go > ... generic handler */
  995. static void _menu_action_navigate(struct MenuEntry * entry) {
  996. /* go to entry->action */
  997. struct MenuEntry_Normal * _entry = (void*)entry;
  998. load_directory(_entry->action, 1);
  999. reinitialize_contents();
  1000. redraw_window();
  1001. }
  1002. /* Go > Up */
  1003. static void _menu_action_up(struct MenuEntry * entry) {
  1004. /* go up */
  1005. char * tmp = strdup(current_directory);
  1006. char * dir = dirname(tmp);
  1007. load_directory(dir, 1);
  1008. reinitialize_contents();
  1009. redraw_window();
  1010. }
  1011. /* [Context] > Refresh */
  1012. static void _menu_action_refresh(struct MenuEntry * entry) {
  1013. char * tmp = strdup(current_directory);
  1014. load_directory(tmp, 0);
  1015. reinitialize_contents();
  1016. redraw_window();
  1017. }
  1018. /* Help > Contents */
  1019. static void _menu_action_help(struct MenuEntry * entry) {
  1020. /* show help documentation */
  1021. system("help-browser file-browser.trt &");
  1022. redraw_window();
  1023. }
  1024. /* [Context] > Copy */
  1025. static void _menu_action_copy(struct MenuEntry * entry) {
  1026. size_t output_size = 0;
  1027. /* Calculate required space for the clipboard */
  1028. int base_is_root = !strcmp(current_directory, "/"); /* avoid redundant slash */
  1029. for (int i = 0; i < file_pointers_len; ++i) {
  1030. if (file_pointers[i]->selected) {
  1031. output_size += strlen(current_directory) + !base_is_root + strlen(file_pointers[i]->type == 2 ? file_pointers[i]->filename : file_pointers[i]->name) + 1; /* base / file \n */
  1032. }
  1033. }
  1034. /* Nothing to copy? */
  1035. if (!output_size) return;
  1036. /* Create the clipboard contents as a LF-separated list of absolute paths */
  1037. char * clipboard = malloc(output_size+1); /* last nil */
  1038. clipboard[0] = '\0';
  1039. for (int i = 0; i < file_pointers_len; ++i) {
  1040. if (file_pointers[i]->selected) {
  1041. strcat(clipboard, current_directory);
  1042. if (!base_is_root) { strcat(clipboard, "/"); }
  1043. strcat(clipboard, file_pointers[i]->type == 2 ? file_pointers[i]->filename : file_pointers[i]->name);
  1044. strcat(clipboard, "\n");
  1045. }
  1046. }
  1047. if (clipboard[output_size-1] == '\n') {
  1048. /* Remove trailing line feed */
  1049. clipboard[output_size-1] = '\0';
  1050. }
  1051. yutani_set_clipboard(yctx, clipboard);
  1052. free(clipboard);
  1053. }
  1054. static void _menu_action_paste(struct MenuEntry * entry) {
  1055. yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD);
  1056. }
  1057. /* Help > About File Browser */
  1058. static void _menu_action_about(struct MenuEntry * entry) {
  1059. /* Show About dialog */
  1060. char about_cmd[1024] = "\0";
  1061. strcat(about_cmd, "about \"About File Browser\" /usr/share/icons/48/folder.bmp \"ToaruOS File Browser\" \"(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\" ");
  1062. char coords[100];
  1063. sprintf(coords, "%d %d &", (int)main_window->x + (int)main_window->width / 2, (int)main_window->y + (int)main_window->height / 2);
  1064. strcat(about_cmd, coords);
  1065. system(about_cmd);
  1066. redraw_window();
  1067. }
  1068. /**
  1069. * Generic application launcher - like system(), but without the wait.
  1070. * Also sets the working directory to the currently-opened directory.
  1071. */
  1072. static void launch_application(char * app) {
  1073. if (!fork()) {
  1074. if (current_directory) chdir(current_directory);
  1075. char * tmp = malloc(strlen(app) + 10);
  1076. sprintf(tmp, "%s", app);
  1077. char * args[] = {"/bin/sh", "-c", tmp, NULL};
  1078. execvp(args[0], args);
  1079. exit(1);
  1080. }
  1081. }
  1082. /* Generic handler for various launcher menus */
  1083. static void launch_application_menu(struct MenuEntry * self) {
  1084. struct MenuEntry_Normal * _self = (void *)self;
  1085. launch_application((char *)_self->action);
  1086. }
  1087. /**
  1088. * Perform the appropriate action to open a File
  1089. */
  1090. static void open_file(struct File * f) {
  1091. if (f->type == 1) {
  1092. char tmp[1024];
  1093. if (is_desktop_background) {
  1094. /* Always open directories in new file browser windows when launched from desktop */
  1095. sprintf(tmp,"file-browser \"%s/%s\"", current_directory, f->name);
  1096. launch_application(tmp);
  1097. } else {
  1098. /* In normal mode, navigate to this directory. */
  1099. sprintf(tmp,"%s/%s", current_directory, f->name);
  1100. load_directory(tmp, 1);
  1101. reinitialize_contents();
  1102. redraw_window();
  1103. }
  1104. } else if (f->launcher[0]) {
  1105. char tmp[4096];
  1106. if (!strcmp(f->launcher, "SELF")) {
  1107. /* "SELF" launchers are for binaries. */
  1108. sprintf(tmp, "exec ./%s", f->name);
  1109. } else {
  1110. /* Other launchers shouuld take file names as arguments.
  1111. * NOTE: If you don't want the file name, you can append # to your launcher.
  1112. * Since it's parsed by the shell, this will yield a comment.
  1113. */
  1114. sprintf(tmp, "%s \"%s\"", f->launcher, f->name);
  1115. }
  1116. launch_application(tmp);
  1117. }
  1118. }
  1119. /* [Context] > Open */
  1120. static void _menu_action_open(struct MenuEntry * self) {
  1121. for (int i = 0; i < file_pointers_len; ++i) {
  1122. if (file_pointers[i]->selected) {
  1123. open_file(file_pointers[i]);
  1124. }
  1125. }
  1126. }
  1127. /* [Context] > Edit in Bim */
  1128. static void _menu_action_edit(struct MenuEntry * self) {
  1129. for (int i = 0; i < file_pointers_len; ++i) {
  1130. if (file_pointers[i]->selected) {
  1131. char tmp[1024];
  1132. sprintf(tmp, "exec terminal bim \"%s\"", file_pointers[i]->type == 2 ? file_pointers[i]->filename : file_pointers[i]->name);
  1133. launch_application(tmp);
  1134. }
  1135. }
  1136. }
  1137. /* View > (Show/Hide) Hidden Files */
  1138. static void _menu_action_toggle_hidden(struct MenuEntry * self) {
  1139. show_hidden = !show_hidden;
  1140. menu_update_title(self, show_hidden ? "Hide Hidden Files" : "Show Hidden Files");
  1141. _menu_action_refresh(NULL);
  1142. }
  1143. static void _menu_action_select_all(struct MenuEntry * self) {
  1144. for (int i = 0; i < file_pointers_len; ++i) {
  1145. file_pointers[i]->selected = 1;
  1146. }
  1147. reinitialize_contents();
  1148. update_status();
  1149. redraw_window();
  1150. }
  1151. static void set_view_mode(int mode) {
  1152. switch (mode) {
  1153. default:
  1154. case VIEW_MODE_ICONS:
  1155. FILE_HEIGHT = 80;
  1156. FILE_WIDTH = 100;
  1157. view_mode = VIEW_MODE_ICONS;
  1158. break;
  1159. case VIEW_MODE_TILES:
  1160. FILE_HEIGHT = 70;
  1161. FILE_WIDTH = 260;
  1162. view_mode = VIEW_MODE_TILES;
  1163. break;
  1164. case VIEW_MODE_LIST:
  1165. FILE_HEIGHT = 24;
  1166. FILE_WIDTH = 100; /* Readjusts elsewhere */
  1167. view_mode = VIEW_MODE_LIST;
  1168. break;
  1169. }
  1170. reinitialize_contents();
  1171. redraw_window();
  1172. }
  1173. static void _menu_action_view_mode(struct MenuEntry * entry) {
  1174. struct MenuEntry_Normal * _entry = (void*)entry;
  1175. int mode = VIEW_MODE_ICONS;
  1176. if (!strcmp(_entry->action, "icons")) {
  1177. mode = VIEW_MODE_ICONS;
  1178. } else if (!strcmp(_entry->action, "tiles")) {
  1179. mode = VIEW_MODE_TILES;
  1180. } else if (!strcmp(_entry->action, "list")) {
  1181. mode = VIEW_MODE_LIST;
  1182. }
  1183. set_view_mode(mode);
  1184. }
  1185. static void handle_clipboard(char * contents) {
  1186. fprintf(stderr, "Received clipboard:\n%s\n",contents);
  1187. char * file = contents;
  1188. while (file && *file) {
  1189. char * next_file = strchr(file, '\n');
  1190. if (next_file) {
  1191. *next_file = '\0';
  1192. next_file++;
  1193. }
  1194. /* determine if the destination already exists */
  1195. char * cheap_basename = strrchr(file, '/');
  1196. if (!cheap_basename) cheap_basename = file;
  1197. else cheap_basename++;
  1198. char destination[4096];
  1199. sprintf(destination, "%s/%s", current_directory, cheap_basename);
  1200. struct stat statbuf;
  1201. if (!stat(destination, &statbuf)) {
  1202. char message[4096];
  1203. sprintf(message, "showdialog \"File Browser\" /usr/share/icons/48/folder.bmp \"Not overwriting file '%s'.\"", cheap_basename);
  1204. launch_application(message);
  1205. } else {
  1206. char cp[1024];
  1207. sprintf(cp, "cp -r \"%s\" \"%s\"", file, current_directory);
  1208. if (system(cp)) {
  1209. char message[4096];
  1210. sprintf(message, "showdialog \"File Browser\" /usr/share/icons/48/folder.bmp \"Error copying file '%s'.\"", cheap_basename);
  1211. launch_application(message);
  1212. }
  1213. }
  1214. file = next_file;
  1215. }
  1216. _menu_action_refresh(NULL);
  1217. }
  1218. /**
  1219. * Toggle the selected status of the highlighted icon.
  1220. *
  1221. * When Ctrl is held, the current selection is maintained.
  1222. */
  1223. static void toggle_selected(int hilighted_offset, int modifiers) {
  1224. struct File * f = get_file_at_offset(hilighted_offset);
  1225. /* No file at this offset, do nothing. */
  1226. if (!f) return;
  1227. /* Toggle selection of the current file */
  1228. f->selected = !f->selected;
  1229. /* If Ctrl wasn't held, unselect everything else. */
  1230. if (!(modifiers & YUTANI_KEY_MODIFIER_CTRL)) {
  1231. for (int i = 0; i < file_pointers_len; ++i) {
  1232. if (file_pointers[i] != f && file_pointers[i]->selected) {
  1233. file_pointers[i]->selected = 0;
  1234. clear_offset(i);
  1235. draw_file(file_pointers[i], i);
  1236. }
  1237. }
  1238. }
  1239. update_status();
  1240. /* Redraw the file */
  1241. clear_offset(hilighted_offset);
  1242. draw_file(f, hilighted_offset);
  1243. /* And repaint the window */
  1244. redraw_window();
  1245. }
  1246. static int _down_button = -1;
  1247. static void _set_hilight(int index, int hilight) {
  1248. int _update = 0;
  1249. if (_button_hover != index || (_button_hover == index && index != -1 && _button_hilights[index] != hilight)) {
  1250. if (_button_hover != -1 && _button_hilights[_button_hover] != 3) {
  1251. _button_hilights[_button_hover] = 3;
  1252. _update = 1;
  1253. }
  1254. _button_hover = index;
  1255. if (index != -1 && !_button_disabled[index]) {
  1256. _button_hilights[_button_hover] = hilight;
  1257. _update = 1;
  1258. }
  1259. if (_update) {
  1260. redraw_window();
  1261. }
  1262. }
  1263. }
  1264. static void _handle_button_press(int index) {
  1265. if (index != -1 && _button_disabled[index]) return; /* can't click disabled buttons */
  1266. switch (index) {
  1267. case 0:
  1268. /* Back */
  1269. if (history_back->length) {
  1270. list_insert(history_forward, strdup(current_directory));
  1271. node_t * next = list_pop(history_back);
  1272. load_directory(next->value, 0);
  1273. free(next->value);
  1274. free(next);
  1275. reinitialize_contents();
  1276. redraw_window();
  1277. }
  1278. break;
  1279. case 1:
  1280. /* Forward */
  1281. if (history_forward->length) {
  1282. list_insert(history_back, strdup(current_directory));
  1283. node_t * next = list_pop(history_forward);
  1284. load_directory(next->value, 0);
  1285. free(next->value);
  1286. free(next);
  1287. reinitialize_contents();
  1288. redraw_window();
  1289. }
  1290. break;
  1291. case 2:
  1292. /* Up */
  1293. _menu_action_up(NULL);
  1294. break;
  1295. case 3:
  1296. /* Home */
  1297. {
  1298. struct MenuEntry_Normal _fake = {.action = getenv("HOME") };
  1299. _menu_action_navigate(&_fake);
  1300. }
  1301. break;
  1302. default:
  1303. /* ??? */
  1304. break;
  1305. }
  1306. }
  1307. static void _scroll_up(void) {
  1308. scroll_offset -= SCROLL_AMOUNT;
  1309. if (scroll_offset < 0) {
  1310. scroll_offset = 0;
  1311. }
  1312. }
  1313. static void _scroll_down(void) {
  1314. if (available_height > contents->height) {
  1315. scroll_offset = 0;
  1316. } else {
  1317. scroll_offset += SCROLL_AMOUNT;
  1318. if (scroll_offset > contents->height - available_height) {
  1319. scroll_offset = contents->height - available_height;
  1320. }
  1321. }
  1322. }
  1323. /**
  1324. * Desktop mode responsds to sig_usr2 by returning to
  1325. * the bottom of the Z-order stack.
  1326. */
  1327. static void sig_usr2(int sig) {
  1328. yutani_set_stack(yctx, main_window, YUTANI_ZORDER_BOTTOM);
  1329. _menu_action_refresh(NULL);
  1330. signal(SIGUSR2, sig_usr2);
  1331. }
  1332. static void sig_usr1(int sig) {
  1333. yutani_window_resize_offer(yctx, main_window, yctx->display_width, yctx->display_height);
  1334. signal(SIGUSR1, sig_usr1);
  1335. }
  1336. static void arrow_select(int x, int y) {
  1337. if (!file_pointers_len) return;
  1338. /* Find first selected */
  1339. int selected = -1;
  1340. for (int i = 0; i < file_pointers_len; ++i) {
  1341. if (file_pointers[i]->selected) {
  1342. selected = i;
  1343. }
  1344. file_pointers[i]->selected = 0;
  1345. }
  1346. if (selected == -1) {
  1347. selected = 0;
  1348. } else {
  1349. int offset_y = selected / FILE_PTR_WIDTH;
  1350. int offset_x = selected % FILE_PTR_WIDTH;
  1351. offset_y += y;
  1352. offset_x += x;
  1353. if (offset_x >= FILE_PTR_WIDTH) {
  1354. offset_x = FILE_PTR_WIDTH - 1;
  1355. }
  1356. if (offset_x < 0) {
  1357. offset_x = 0;
  1358. }
  1359. if (offset_y < 0) {
  1360. offset_y = 0;
  1361. }
  1362. selected = offset_y * FILE_PTR_WIDTH + offset_x;
  1363. if (selected >= file_pointers_len) selected = file_pointers_len - 1;
  1364. if (selected < 0) selected = 0;
  1365. }
  1366. int offset_y = selected / FILE_PTR_WIDTH;
  1367. if (offset_y * FILE_HEIGHT < scroll_offset) {
  1368. scroll_offset = offset_y * FILE_HEIGHT;
  1369. }
  1370. if (offset_y * FILE_HEIGHT + FILE_HEIGHT > scroll_offset + available_height) {
  1371. scroll_offset = offset_y * FILE_HEIGHT + FILE_HEIGHT - available_height;
  1372. }
  1373. file_pointers[selected]->selected = 1;
  1374. update_status();
  1375. reinitialize_contents();
  1376. redraw_window();
  1377. }
  1378. int main(int argc, char * argv[]) {
  1379. yctx = yutani_init();
  1380. init_decorations();
  1381. int arg_ind = 1;
  1382. if (argc > 1 && !strcmp(argv[1], "--wallpaper")) {
  1383. is_desktop_background = 1;
  1384. menu_bar_height = 0;
  1385. signal(SIGUSR1, sig_usr1);
  1386. signal(SIGUSR2, sig_usr2);
  1387. draw_background(yctx->display_width, yctx->display_height);
  1388. main_window = yutani_window_create_flags(yctx, yctx->display_width, yctx->display_height, YUTANI_WINDOW_FLAG_NO_STEAL_FOCUS);
  1389. yutani_window_move(yctx, main_window, 0, 0);
  1390. yutani_set_stack(yctx, main_window, YUTANI_ZORDER_BOTTOM);
  1391. arg_ind++;
  1392. FILE * f = fopen("/var/run/.wallpaper.pid", "w");
  1393. fprintf(f, "%d\n", getpid());
  1394. fclose(f);
  1395. } else {
  1396. main_window = yutani_window_create(yctx, 800, 600);
  1397. yutani_window_move(yctx, main_window, yctx->display_width / 2 - main_window->width / 2, yctx->display_height / 2 - main_window->height / 2);
  1398. }
  1399. if (arg_ind < argc) {
  1400. chdir(argv[arg_ind]);
  1401. }
  1402. ctx = init_graphics_yutani_double_buffer(main_window);
  1403. struct decor_bounds bounds;
  1404. _decor_get_bounds(main_window, &bounds);
  1405. set_title(NULL);
  1406. menu_bar.entries = menu_entries;
  1407. menu_bar.redraw_callback = redraw_window;
  1408. menu_bar.set = menu_set_create();
  1409. struct MenuList * m = menu_create(); /* File */
  1410. menu_insert(m, menu_create_normal("exit",NULL,"Exit", _menu_action_exit));
  1411. menu_set_insert(menu_bar.set, "file", m);
  1412. m = menu_create();
  1413. menu_insert(m, menu_create_normal(NULL,NULL,"Copy",_menu_action_copy));
  1414. menu_insert(m, menu_create_normal(NULL,NULL,"Paste",_menu_action_paste));
  1415. menu_insert(m, menu_create_separator());
  1416. menu_insert(m, menu_create_normal(NULL,NULL,"Select all",_menu_action_select_all));
  1417. menu_set_insert(menu_bar.set, "edit", m);
  1418. m = menu_create();
  1419. menu_insert(m, menu_create_normal("refresh",NULL,"Refresh", _menu_action_refresh));
  1420. menu_insert(m, menu_create_separator());
  1421. menu_insert(m, menu_create_normal(NULL,"icons","Show Icons", _menu_action_view_mode));
  1422. menu_insert(m, menu_create_normal(NULL,"tiles","Show Tiles", _menu_action_view_mode));
  1423. menu_insert(m, menu_create_normal(NULL,"list","Show List", _menu_action_view_mode));
  1424. menu_insert(m, menu_create_separator());
  1425. menu_insert(m, menu_create_normal(NULL,NULL,"Show Hidden Files", _menu_action_toggle_hidden));
  1426. menu_set_insert(menu_bar.set, "view", m);
  1427. m = menu_create(); /* Go */
  1428. /* TODO implement input dialog for Path... */
  1429. #if 0
  1430. menu_insert(m, menu_create_normal("open",NULL,"Path...", _menu_action_input_path));
  1431. menu_insert(m, menu_create_separator());
  1432. #endif
  1433. menu_insert(m, menu_create_normal("home",getenv("HOME"),"Home",_menu_action_navigate));
  1434. menu_insert(m, menu_create_normal(NULL,"/","File System",_menu_action_navigate));
  1435. menu_insert(m, menu_create_normal("up",NULL,"Up",_menu_action_up));
  1436. menu_set_insert(menu_bar.set, "go", m);
  1437. m = menu_create();
  1438. menu_insert(m, menu_create_normal("help",NULL,"Contents",_menu_action_help));
  1439. menu_insert(m, menu_create_separator());
  1440. menu_insert(m, menu_create_normal("star",NULL,"About " APPLICATION_TITLE,_menu_action_about));
  1441. menu_set_insert(menu_bar.set, "help", m);
  1442. available_height = ctx->height - menu_bar_height - bounds.height - (is_desktop_background ? 0 : STATUS_HEIGHT);
  1443. fprintf(stderr, "available_height = %d\n", available_height);
  1444. context_menu = menu_create(); /* Right-click menu */
  1445. menu_insert(context_menu, menu_create_normal(NULL,NULL,"Open",_menu_action_open));
  1446. menu_insert(context_menu, menu_create_normal(NULL,NULL,"Edit in Bim",_menu_action_edit));
  1447. menu_insert(context_menu, menu_create_separator());
  1448. menu_insert(context_menu, menu_create_normal(NULL,NULL,"Copy",_menu_action_copy));
  1449. menu_insert(context_menu, menu_create_normal(NULL,NULL,"Paste",_menu_action_paste));
  1450. menu_insert(context_menu, menu_create_separator());
  1451. if (!is_desktop_background) {
  1452. menu_insert(context_menu, menu_create_normal("up",NULL,"Up",_menu_action_up));
  1453. }
  1454. menu_insert(context_menu, menu_create_normal("refresh",NULL,"Refresh",_menu_action_refresh));
  1455. menu_insert(context_menu, menu_create_normal("utilities-terminal","terminal","Open Terminal",launch_application_menu));
  1456. history_back = list_create();
  1457. history_forward = list_create();
  1458. /* Load the current working directory */
  1459. char tmp[1024];
  1460. getcwd(tmp, 1024);
  1461. load_directory(tmp, 1);
  1462. /* Draw files */
  1463. reinitialize_contents();
  1464. redraw_window();
  1465. while (application_running) {
  1466. waitpid(-1, NULL, WNOHANG);
  1467. int fds[1] = {fileno(yctx->sock)};
  1468. int index = fswait2(1,fds,wallpaper_old ? 10 : 200);
  1469. if (restart) {
  1470. execvp(argv[0],argv);
  1471. return 1;
  1472. }
  1473. if (index == 1) {
  1474. if (wallpaper_old) {
  1475. redraw_window();
  1476. }
  1477. continue;
  1478. }
  1479. yutani_msg_t * m = yutani_poll(yctx);
  1480. while (m) {
  1481. int redraw = 0;
  1482. if (menu_process_event(yctx, m)) {
  1483. redraw = 1;
  1484. }
  1485. switch (m->type) {
  1486. case YUTANI_MSG_WELCOME:
  1487. if (is_desktop_background) {
  1488. yutani_window_resize_offer(yctx, main_window, yctx->display_width, yctx->display_height);
  1489. }
  1490. break;
  1491. case YUTANI_MSG_KEY_EVENT:
  1492. {
  1493. struct yutani_msg_key_event * ke = (void*)m->data;
  1494. if (ke->event.action == KEY_ACTION_DOWN && ke->wid == main_window->wid) {
  1495. if (nav_bar_focused) {
  1496. switch (ke->event.key) {
  1497. case KEY_ESCAPE:
  1498. nav_bar_focused = 0;
  1499. redraw_window();
  1500. break;
  1501. case KEY_BACKSPACE:
  1502. nav_bar_backspace();
  1503. break;
  1504. case KEY_CTRL_W:
  1505. nav_bar_backspace_word();
  1506. break;
  1507. case '\n':
  1508. nav_bar_focused = 0;
  1509. char * tmp = strdup(nav_bar);
  1510. load_directory(tmp, 1);
  1511. reinitialize_contents();
  1512. redraw_window();
  1513. break;
  1514. default:
  1515. if (isgraph(ke->event.key)) {
  1516. nav_bar_insert_char(ke->event.key);
  1517. } else {
  1518. switch (ke->event.keycode) {
  1519. case KEY_ARROW_LEFT:
  1520. nav_bar_cursor_left();
  1521. break;
  1522. case KEY_ARROW_RIGHT:
  1523. nav_bar_cursor_right();
  1524. break;
  1525. }
  1526. }
  1527. break;
  1528. }
  1529. break;
  1530. }
  1531. switch (ke->event.keycode) {
  1532. case KEY_PAGE_UP:
  1533. _scroll_up();
  1534. redraw = 1;
  1535. break;
  1536. case KEY_PAGE_DOWN:
  1537. _scroll_down();
  1538. redraw = 1;
  1539. break;
  1540. /* if not focused on anything focusable */
  1541. case KEY_ARROW_DOWN:
  1542. arrow_select(0,1);
  1543. break;
  1544. case KEY_ARROW_UP:
  1545. arrow_select(0,-1);
  1546. break;
  1547. case KEY_ARROW_LEFT:
  1548. arrow_select(-1,0);
  1549. break;
  1550. case KEY_ARROW_RIGHT:
  1551. arrow_select(1,0);
  1552. break;
  1553. case KEY_BACKSPACE:
  1554. _menu_action_up(NULL);
  1555. break;
  1556. case '\n':
  1557. _menu_action_open(NULL);
  1558. break;
  1559. case 'l':
  1560. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_CTRL && !is_desktop_background) {
  1561. nav_bar_focused = 1;
  1562. redraw_window();
  1563. }
  1564. break;
  1565. case 'f':
  1566. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT && !is_desktop_background) {
  1567. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[0]);
  1568. }
  1569. break;
  1570. case 'e':
  1571. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT && !is_desktop_background) {
  1572. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[1]);
  1573. }
  1574. break;
  1575. case 'v':
  1576. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT && !is_desktop_background) {
  1577. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[2]);
  1578. }
  1579. break;
  1580. case 'g':
  1581. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT && !is_desktop_background) {
  1582. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[3]);
  1583. }
  1584. break;
  1585. case 'h':
  1586. if (ke->event.modifiers & YUTANI_KEY_MODIFIER_ALT && !is_desktop_background) {
  1587. menu_bar_show_menu(yctx,main_window,&menu_bar,-1,&menu_entries[4]);
  1588. }
  1589. break;
  1590. case 'q':
  1591. if (!is_desktop_background) {
  1592. _menu_action_exit(NULL);
  1593. }
  1594. break;
  1595. default:
  1596. break;
  1597. }
  1598. }
  1599. }
  1600. break;
  1601. case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
  1602. {
  1603. struct yutani_msg_window_focus_change * wf = (void*)m->data;
  1604. yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
  1605. if (win == main_window) {
  1606. win->focused = wf->focused;
  1607. redraw_files();
  1608. redraw = 1;
  1609. }
  1610. }
  1611. break;
  1612. case YUTANI_MSG_RESIZE_OFFER:
  1613. {
  1614. struct yutani_msg_window_resize * wr = (void*)m->data;
  1615. if (wr->wid == main_window->wid) {
  1616. resize_finish(wr->width, wr->height);
  1617. }
  1618. }
  1619. break;
  1620. case YUTANI_MSG_CLIPBOARD:
  1621. {
  1622. struct yutani_msg_clipboard * cb = (void *)m->data;
  1623. char * selection_text;
  1624. if (*cb->content == '\002') {
  1625. int size = atoi(&cb->content[2]);
  1626. FILE * clipboard = yutani_open_clipboard(yctx);
  1627. selection_text = malloc(size + 1);
  1628. fread(selection_text, 1, size, clipboard);
  1629. selection_text[size] = '\0';
  1630. fclose(clipboard);
  1631. } else {
  1632. selection_text = malloc(cb->size+1);
  1633. memcpy(selection_text, cb->content, cb->size);
  1634. selection_text[cb->size] = '\0';
  1635. }
  1636. handle_clipboard(selection_text);
  1637. free(selection_text);
  1638. }
  1639. break;
  1640. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  1641. {
  1642. struct yutani_msg_window_mouse_event * me = (void*)m->data;
  1643. yutani_window_t * win = hashmap_get(yctx->windows, (void*)me->wid);
  1644. struct decor_bounds bounds;
  1645. _decor_get_bounds(win, &bounds);
  1646. if (win == main_window) {
  1647. int result = decor_handle_event(yctx, m);
  1648. switch (result) {
  1649. case DECOR_CLOSE:
  1650. _menu_action_exit(NULL);
  1651. break;
  1652. case DECOR_RIGHT:
  1653. /* right click in decoration, show appropriate menu */
  1654. decor_show_default_menu(main_window, main_window->x + me->new_x, main_window->y + me->new_y);
  1655. break;
  1656. default:
  1657. /* Other actions */
  1658. break;
  1659. }
  1660. /* Menu bar */
  1661. menu_bar_mouse_event(yctx, main_window, &menu_bar, me, me->new_x, me->new_y);
  1662. if (menu_bar_height &&
  1663. me->new_y > (int)(bounds.top_height + menu_bar_height - 36) &&
  1664. me->new_y < (int)(bounds.top_height + menu_bar_height) &&
  1665. me->new_x > (int)(bounds.left_width) &&
  1666. me->new_x < (int)(main_window->width - bounds.right_width)) {
  1667. int x = me->new_x - bounds.left_width - 2;
  1668. if (x >= 0) {
  1669. int i = x / 34;
  1670. if (i < 4) {
  1671. if (me->command == YUTANI_MOUSE_EVENT_DOWN) {
  1672. _set_hilight(i, 2);
  1673. nav_bar_focused = 0;
  1674. _down_button = i;
  1675. } else if (me->command == YUTANI_MOUSE_EVENT_RAISE || me->command == YUTANI_MOUSE_EVENT_CLICK) {
  1676. if (_down_button != -1 && _down_button == i) {
  1677. _handle_button_press(i);
  1678. _set_hilight(i, 1);
  1679. }
  1680. _down_button = -1;
  1681. } else {
  1682. if (!(me->buttons & YUTANI_MOUSE_BUTTON_LEFT)) {
  1683. _set_hilight(i, 1);
  1684. } else {
  1685. if (_down_button == i) {
  1686. _set_hilight(i, 2);
  1687. } else if (_down_button != -1) {
  1688. _set_hilight(_down_button, 3);
  1689. }
  1690. }
  1691. }
  1692. } else {
  1693. _set_hilight(-1,0);
  1694. if (me->command == YUTANI_MOUSE_EVENT_DOWN) {
  1695. nav_bar_focused = 1;
  1696. _figure_out_navbar_cursor(me->new_x, bounds);
  1697. redraw = 1;
  1698. }
  1699. }
  1700. }
  1701. } else {
  1702. if (me->command == YUTANI_MOUSE_EVENT_DOWN) {
  1703. if (nav_bar_focused) {
  1704. nav_bar_focused = 0;
  1705. redraw = 1;
  1706. }
  1707. }
  1708. if (_button_hover != -1) {
  1709. _button_hilights[_button_hover] = 3;
  1710. _button_hover = -1;
  1711. redraw = 1; /* Double redraw ??? */
  1712. }
  1713. }
  1714. if (!is_desktop_background && me->new_y > (int)(main_window->height - bounds.bottom_height - STATUS_HEIGHT)) {
  1715. } else if (me->new_y > (int)(bounds.top_height + menu_bar_height) &&
  1716. me->new_y < (int)(main_window->height - bounds.bottom_height) &&
  1717. me->new_x > (int)(bounds.left_width) &&
  1718. me->new_x < (int)(main_window->width - bounds.right_width) &&
  1719. me->command != YUTANI_MOUSE_EVENT_LEAVE) {
  1720. if (me->buttons & YUTANI_MOUSE_SCROLL_UP) {
  1721. /* Scroll up */
  1722. _scroll_up();
  1723. redraw = 1;
  1724. } else if (me->buttons & YUTANI_MOUSE_SCROLL_DOWN) {
  1725. _scroll_down();
  1726. redraw = 1;
  1727. }
  1728. /* Get offset into contents */
  1729. int y_into = me->new_y - bounds.top_height - menu_bar_height + scroll_offset;
  1730. int x_into = me->new_x - bounds.left_width;
  1731. int offset = (y_into / FILE_HEIGHT) * FILE_PTR_WIDTH + x_into / FILE_WIDTH;
  1732. if (x_into > FILE_PTR_WIDTH * FILE_WIDTH) {
  1733. offset = -1;
  1734. }
  1735. if (offset != hilighted_offset) {
  1736. int old_offset = hilighted_offset;
  1737. hilighted_offset = offset;
  1738. if (old_offset != -1) {
  1739. clear_offset(old_offset);
  1740. struct File * f = get_file_at_offset(old_offset);
  1741. if (f) {
  1742. clear_offset(old_offset);
  1743. draw_file(f, old_offset);
  1744. }
  1745. }
  1746. struct File * f = get_file_at_offset(hilighted_offset);
  1747. if (f) {
  1748. clear_offset(hilighted_offset);
  1749. draw_file(f, hilighted_offset);
  1750. }
  1751. redraw = 1;
  1752. }
  1753. if (me->command == YUTANI_MOUSE_EVENT_CLICK || _close_enough(me)) {
  1754. struct File * f = get_file_at_offset(hilighted_offset);
  1755. if (f) {
  1756. if (last_click_offset == hilighted_offset && precise_time_since(last_click) < 400) {
  1757. open_file(f);
  1758. last_click = 0;
  1759. } else {
  1760. last_click = precise_current_time();
  1761. last_click_offset = hilighted_offset;
  1762. toggle_selected(hilighted_offset, me->modifiers);
  1763. }
  1764. } else {
  1765. if (!(me->modifiers & YUTANI_KEY_MODIFIER_CTRL)) {
  1766. for (int i = 0; i < file_pointers_len; ++i) {
  1767. if (file_pointers[i]->selected) {
  1768. file_pointers[i]->selected = 0;
  1769. update_status();
  1770. clear_offset(i);
  1771. draw_file(file_pointers[i], i);
  1772. }
  1773. }
  1774. redraw = 1;
  1775. }
  1776. }
  1777. } else if (me->buttons & YUTANI_MOUSE_BUTTON_RIGHT) {
  1778. if (!context_menu->window) {
  1779. struct File * f = get_file_at_offset(hilighted_offset);
  1780. if (f && !f->selected) {
  1781. toggle_selected(hilighted_offset, me->modifiers);
  1782. }
  1783. menu_show_at(context_menu, main_window, me->new_x, me->new_y);
  1784. }
  1785. }
  1786. } else {
  1787. int old_offset = hilighted_offset;
  1788. hilighted_offset = -1;
  1789. if (old_offset != -1) {
  1790. clear_offset(old_offset);
  1791. struct File * f = get_file_at_offset(old_offset);
  1792. if (f) {
  1793. clear_offset(old_offset);
  1794. draw_file(f, old_offset);
  1795. }
  1796. redraw = 1;
  1797. }
  1798. }
  1799. }
  1800. }
  1801. break;
  1802. case YUTANI_MSG_WINDOW_CLOSE:
  1803. case YUTANI_MSG_SESSION_END:
  1804. _menu_action_exit(NULL);
  1805. break;
  1806. default:
  1807. break;
  1808. }
  1809. if (redraw || wallpaper_old) {
  1810. redraw_window();
  1811. }
  1812. free(m);
  1813. m = yutani_poll_async(yctx);
  1814. }
  1815. }
  1816. }