slightly better?
[vrfileman] / src / fs.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <string>
6 #include <map>
7 #include <algorithm>
8 #include <unistd.h>
9 #include <dirent.h>
10 #include <sys/stat.h>
11 #include "fs.h"
12 #include "icon.h"
13 #include "gmath/gmath.h"
14 #include "opengl.h"
15 #include "app.h"
16 #include "drawtext.h"
17 #include "sdr.h"
18
19 static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bool full);
20
21 static IconRenderer *iconrend;
22
23 static std::map<std::string, FSNode*> node_cache;
24 static FSNode *cur_node;
25 static int start_child;
26
27 static dtx_font *fat_font;
28 #define FAT_FONT_SZ     32
29 static unsigned int glow_link_sdr;
30 static unsigned int chrome_font_sdr, glow_font_sdr;
31
32
33 bool init_fs(const char *path)
34 {
35         iconrend = new ShapesIcons;
36         if(!iconrend->init()) {
37                 return false;
38         }
39
40         if(!(fat_font = dtx_open_font_glyphmap("data/fat.glyphmap")) ||
41                         dtx_get_glyphmap_ptsize(dtx_get_glyphmap(fat_font, 0)) != FAT_FONT_SZ) {
42
43                 dtx_set(DTX_PADDING, 64);
44
45                 if(!(fat_font = dtx_open_font("data/fat.font", 0))) {
46                         fprintf(stderr, "failed to open font file data/fat.font\n");
47                         return false;
48                 }
49                 dtx_prepare_range(fat_font, FAT_FONT_SZ * 8, 32, 127);
50                 dtx_calc_font_distfield(fat_font, 1, 8);
51                 dtx_save_glyphmap("data/fat.glyphmap", dtx_get_glyphmap(fat_font, 0));
52         }
53         dtx_use_font(fat_font, FAT_FONT_SZ);
54
55         struct dtx_glyphmap *fat_gmap = dtx_get_glyphmap(fat_font, 0);
56         Vec2 pixsz;
57         pixsz.x = 1.0 / dtx_get_glyphmap_width(fat_gmap);
58         pixsz.y = 1.0 / dtx_get_glyphmap_height(fat_gmap);
59
60         if(!(chrome_font_sdr = create_program_load("sdr/chrome_font.v.glsl", "sdr/chrome_font.p.glsl"))) {
61                 return false;
62         }
63         set_uniform_float(chrome_font_sdr, "height", dtx_line_height());
64         set_uniform_float(chrome_font_sdr, "smoothness", 0.01);
65         set_uniform_float2(chrome_font_sdr, "pix_sz", pixsz.x, pixsz.y);
66
67         if(!(glow_font_sdr = create_program_load("sdr/dfont.v.glsl", "sdr/glow_font.p.glsl"))) {
68                 return false;
69         }
70         set_uniform_float(glow_font_sdr, "smoothness", 0.01);
71         set_uniform_float2(glow_font_sdr, "pix_sz", pixsz.x, pixsz.y);
72
73         if(!(glow_link_sdr = create_program_load("sdr/glink.v.glsl", "sdr/glink.p.glsl"))) {
74                 return false;
75         }
76
77         if(!(cur_node = get_fsnode(path))) {
78                 return false;
79         }
80         cur_node->expand();
81         return true;
82 }
83
84 void cleanup_fs()
85 {
86         std::map<std::string, FSNode*>::iterator it = node_cache.begin();
87         while(it != node_cache.end()) {
88                 FSNode *node = it++->second;
89                 delete node;
90         }
91         node_cache.clear();
92         dtx_close_font(fat_font);
93         delete iconrend;
94 }
95
96 static float icon_angle(int col, int ncols, float max_angle = 0.0f)
97 {
98         if(max_angle > 0) {
99                 return max_angle * ((float)col / (float)(ncols - 1) - 0.5);
100         }
101         return 2.0 * M_PI * (float)col / (float)ncols;
102 }
103
104 void draw_fs()
105 {
106         static const float row_spacing = 0.25;
107         static const float radius = 0.6;
108         static const float umax = 0.42;
109         static const float max_icon_angle = M_PI * 2.0 * umax;
110
111         int max_ncols = std::max<int>(1, umax * 12);
112
113         glPushMatrix();
114         glTranslatef(0, cam_height, 0);
115
116         Mat4 rot_xform;
117         rot_xform.rotate(time_sec, 0, 0);
118         rot_xform.rotate(0, 0, time_sec * 0.5);
119
120         glDisable(GL_TEXTURE_2D);
121         glEnable(GL_BLEND);
122         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
123
124         int nchildren = (int)cur_node->children.size();
125         int ncols = std::min(cur_node->nfiles, max_ncols);
126
127         int first = start_child % ncols;
128         int col = 0, row = 0;
129         int num_dirs = 0;
130
131         // count directories ...
132         for(int i=0; i<nchildren; i++) {
133                 FSNode *node = cur_node->children[i];
134                 if(node->type == FSTYPE_DIR) {
135                         ++num_dirs;
136                 }
137         }
138
139         // draw the directory link lines
140         glUseProgram(glow_link_sdr);
141         set_uniform_float(glow_link_sdr, "tsec", time_sec);
142         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
143
144         for(int i=0; i<nchildren; i++) {
145                 FSNode *node = cur_node->children[i];
146
147                 if(node->type != FSTYPE_DIR) {
148                         continue;
149                 }
150
151                 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
152
153                 Mat4 xform;
154                 xform.rotate_y(angle);
155                 xform.translate(0, -0.3, 0);
156
157                 glPushMatrix();
158                 glMultMatrixf(xform[0]);
159                 glDepthMask(0);
160
161                 set_uniform_float(glow_link_sdr, "phase", col * 42.0);
162
163                 glBegin(GL_QUADS);
164                 glColor3f(0.2, 0.3, 0.8);
165                 glTexCoord2f(0, 0);
166                 glVertex3f(-0.2, 0, 0.05);
167                 glTexCoord2f(1, 0);
168                 glVertex3f(0.2, 0, 0.05);
169                 glTexCoord2f(1, 1);
170                 glVertex3f(0.2, 0, -5.0);
171                 glTexCoord2f(0, 1);
172                 glVertex3f(-0.2, 0, -5.0);
173                 glColor3f(1, 1, 1);
174                 glEnd();
175                 glPopMatrix();
176
177                 glDepthMask(1);
178         }
179
180         // draw the directory labels
181         glUseProgram(glow_font_sdr);
182         col = 0;
183         for(int i=0; i<nchildren; i++) {
184                 FSNode *node = cur_node->children[i];
185
186                 if(node->type != FSTYPE_DIR) {
187                         continue;
188                 }
189
190                 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
191
192                 draw_node_name(node, angle, -0.3, radius, false);
193         }
194
195         // then draw file icons
196         glDisable(GL_BLEND);
197         glUseProgram(0);
198         col = 0;
199         for(int i=0; i<nchildren; i++) {
200                 int idx = (i + first) % nchildren;
201                 FSNode *node = cur_node->children[idx];
202
203                 if(node->type == FSTYPE_DIR) {
204                         ++num_dirs;
205                         continue;
206                 }
207
208                 float angle = icon_angle(col, ncols, max_icon_angle);
209
210                 Mat4 xform = rot_xform;
211                 xform.translate(0, row * row_spacing, -radius);
212                 xform.rotate_y(angle);
213
214                 glPushMatrix();
215                 glMultMatrixf(xform[0]);
216                 iconrend->draw(node);
217                 glPopMatrix();
218
219                 if(++col >= ncols) {
220                         col = 0;
221                         ++row;
222                 }
223         }
224
225         // then draw the file labels
226         glUseProgram(chrome_font_sdr);
227         col = 0;
228         row = 0;
229         for(int i=0; i<nchildren; i++) {
230                 int idx = (i + first) % nchildren;
231                 FSNode *node = cur_node->children[idx];
232
233                 if(node->type == FSTYPE_DIR) {
234                         ++num_dirs;
235                         continue;
236                 }
237
238                 float angle = icon_angle(col, ncols, max_icon_angle);
239
240                 draw_node_name(node, angle, row * row_spacing - 0.1, radius, false);
241
242                 if(++col >= ncols) {
243                         col = 0;
244                         ++row;
245                 }
246         }
247
248         glPopMatrix();
249 }
250
251 static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bool full)
252 {
253         dtx_use_font(fat_font, FAT_FONT_SZ);
254         int line_height = dtx_line_height();
255
256         int nlines = full ? node->name_lines.size() : 1;
257         for(int i=0; i<nlines; i++) {
258                 const char *name = full ? node->name_lines[i].c_str() : node->short_name.c_str();
259                 glPushMatrix();
260                 Mat4 xform;
261                 xform.translate(-dtx_string_width(name) / 2.0, -line_height * i, 0);
262                 if(node->type == FSTYPE_DIR) {
263                         xform.rotate_z(deg_to_rad(90));
264                         xform.rotate_x(deg_to_rad(-90));
265                         xform.scale(0.0017);
266                 } else {
267                         xform.scale(0.0012);
268                 }
269                 xform.translate(0, ypos, -dist);
270                 xform.rotate_y(angle);
271                 glMultMatrixf(xform[0]);
272
273                 dtx_string(name);
274                 glPopMatrix();
275         }
276 }
277
278 #define MAX_NAME_SZ     16
279
280 FSNode *get_fsnode(const char *path)
281 {
282         char *abspath = make_abs_path(path);
283
284         FSNode *node = node_cache[abspath];
285         if(!node) {
286                 node = new FSNode;
287                 node->path = path;
288
289                 const char *name = node->path.get_name();
290                 if(name) {
291                         const char *ptr = name;
292                         while(*ptr) {
293                                 if(ptr - name >= MAX_NAME_SZ) {
294                                         int len = ptr - name;
295                                         std::string s = std::string(name, len);
296                                         if(node->short_name.empty()) {
297                                                 node->short_name = s;
298                                                 node->short_name[len - 1] = node->short_name[len - 2] = '.';
299                                         }
300                                         node->name_lines.push_back(s);
301                                         name = ptr;
302                                 }
303                                 ++ptr;
304                         }
305                         if(*name) {
306                                 if(node->short_name.empty()) {
307                                         node->short_name = name;
308                                 }
309                                 node->name_lines.push_back(name);
310                         }
311                 }
312
313                 struct stat st;
314                 if(stat(node->path, &st) == -1) {
315                         fprintf(stderr, "failed to stat: %s\n", node->path.get_path());
316                         delete node;
317                         return 0;
318                 }
319                 node->size = st.st_size;
320
321                 switch(st.st_mode & S_IFMT) {
322                 case S_IFREG:
323                         node->type = FSTYPE_FILE;
324                         break;
325
326                 case S_IFDIR:
327                         node->type = FSTYPE_DIR;
328                         break;
329
330                 case S_IFBLK:
331                 case S_IFCHR:
332                         node->type = FSTYPE_DEV;
333                         break;
334
335                 default:
336                         node->type = FSTYPE_UNKNOWN;
337                 }
338                 node_cache[abspath] = node;
339         }
340
341         return node;
342 }
343
344 FSNode *get_fsnode(const char *dir, const char *name)
345 {
346         if(!dir) {
347                 return get_fsnode(name);
348         }
349         if(!name || *name == '/') {
350                 return 0;
351         }
352
353         int len = strlen(dir) + 1 + strlen(name);
354         char *buf = new char[len + 1];
355         sprintf(buf, "%s/%s", dir, name);
356         FSNode *res = get_fsnode(buf);
357         delete [] buf;
358         return res;
359 }
360
361 // ---- FSNode implementation ----
362 FSNode::FSNode()
363 {
364         type = FSTYPE_UNKNOWN;
365         size = 0;
366         parent = 0;
367         nfiles = ndirs = 0;
368 }
369
370 bool FSNode::expand()
371 {
372         if(type != FSTYPE_DIR) {
373                 return false;
374         }
375
376         DIR *dir = opendir(path);
377         if(!dir) {
378                 fprintf(stderr, "failed to open dir: %s: %s\n", path.get_path(), strerror(errno));
379                 return false;
380         }
381
382         struct dirent *dent;
383         while((dent = readdir(dir))) {
384                 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
385                         continue;
386                 }
387
388                 FSNode *node = get_fsnode(path, dent->d_name);
389                 if(!node) continue;
390
391                 children.push_back(node);
392                 switch(node->type) {
393                 case FSTYPE_FILE:
394                         ++nfiles;
395                         break;
396                 case FSTYPE_DIR:
397                         ++ndirs;
398                 default:
399                         break;
400                 }
401         }
402         printf("expanded %d children\n", (int)children.size());
403
404         parent = get_fsnode(path.get_parent());
405         return true;
406 }