better dir-link visualization
[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         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
141
142         for(int i=0; i<nchildren; i++) {
143                 FSNode *node = cur_node->children[i];
144
145                 if(node->type != FSTYPE_DIR) {
146                         continue;
147                 }
148
149                 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
150
151                 Mat4 xform;
152                 xform.rotate_y(angle);
153                 xform.translate(0, -0.3, 0);
154
155                 glUseProgram(glow_link_sdr);
156                 glPushMatrix();
157                 glMultMatrixf(xform[0]);
158                 glDepthMask(0);
159
160                 glBegin(GL_QUADS);
161                 glColor3f(0.2, 0.3, 0.8);
162                 glTexCoord2f(0, 0);
163                 glVertex3f(-0.25, 0, 0.05);
164                 glTexCoord2f(1, 0);
165                 glVertex3f(0.25, 0, 0.05);
166                 glTexCoord2f(1, 1);
167                 glVertex3f(0.25, 0, -2.0);
168                 glTexCoord2f(0, 1);
169                 glVertex3f(-0.25, 0, -2.0);
170                 glColor3f(1, 1, 1);
171                 glEnd();
172                 glPopMatrix();
173
174                 glDepthMask(1);
175         }
176
177         // draw the directory labels
178         glUseProgram(glow_font_sdr);
179         col = 0;
180         for(int i=0; i<nchildren; i++) {
181                 FSNode *node = cur_node->children[i];
182
183                 if(node->type != FSTYPE_DIR) {
184                         continue;
185                 }
186
187                 float angle = (float)col++ / (float)(num_dirs - 1) * max_icon_angle - max_icon_angle * 0.5;
188
189                 draw_node_name(node, angle, -0.3, radius, false);
190         }
191
192         // then draw file icons
193         glDisable(GL_BLEND);
194         glUseProgram(0);
195         col = 0;
196         for(int i=0; i<nchildren; i++) {
197                 int idx = (i + first) % nchildren;
198                 FSNode *node = cur_node->children[idx];
199
200                 if(node->type == FSTYPE_DIR) {
201                         ++num_dirs;
202                         continue;
203                 }
204
205                 float angle = icon_angle(col, ncols, max_icon_angle);
206
207                 Mat4 xform = rot_xform;
208                 xform.translate(0, row * row_spacing, -radius);
209                 xform.rotate_y(angle);
210
211                 glPushMatrix();
212                 glMultMatrixf(xform[0]);
213                 iconrend->draw(node);
214                 glPopMatrix();
215
216                 if(++col >= ncols) {
217                         col = 0;
218                         ++row;
219                 }
220         }
221
222         // then draw the file labels
223         glUseProgram(chrome_font_sdr);
224         col = 0;
225         row = 0;
226         for(int i=0; i<nchildren; i++) {
227                 int idx = (i + first) % nchildren;
228                 FSNode *node = cur_node->children[idx];
229
230                 if(node->type == FSTYPE_DIR) {
231                         ++num_dirs;
232                         continue;
233                 }
234
235                 float angle = icon_angle(col, ncols, max_icon_angle);
236
237                 draw_node_name(node, angle, row * row_spacing - 0.1, radius, false);
238
239                 if(++col >= ncols) {
240                         col = 0;
241                         ++row;
242                 }
243         }
244
245         glPopMatrix();
246 }
247
248 static void draw_node_name(FSNode *node, float angle, float ypos, float dist, bool full)
249 {
250         dtx_use_font(fat_font, FAT_FONT_SZ);
251         int line_height = dtx_line_height();
252
253         int nlines = full ? node->name_lines.size() : 1;
254         for(int i=0; i<nlines; i++) {
255                 const char *name = full ? node->name_lines[i].c_str() : node->short_name.c_str();
256                 glPushMatrix();
257                 Mat4 xform;
258                 xform.translate(-dtx_string_width(name) / 2.0, -line_height * i, 0);
259                 if(node->type == FSTYPE_DIR) {
260                         xform.rotate_z(deg_to_rad(90));
261                         xform.rotate_x(deg_to_rad(-90));
262                         xform.scale(0.0017);
263                 } else {
264                         xform.scale(0.0012);
265                 }
266                 xform.translate(0, ypos, -dist);
267                 xform.rotate_y(angle);
268                 glMultMatrixf(xform[0]);
269
270                 dtx_string(name);
271                 glPopMatrix();
272         }
273 }
274
275 #define MAX_NAME_SZ     16
276
277 FSNode *get_fsnode(const char *path)
278 {
279         char *abspath = make_abs_path(path);
280
281         FSNode *node = node_cache[abspath];
282         if(!node) {
283                 node = new FSNode;
284                 node->path = path;
285
286                 const char *name = node->path.get_name();
287                 if(name) {
288                         const char *ptr = name;
289                         while(*ptr) {
290                                 if(ptr - name >= MAX_NAME_SZ) {
291                                         int len = ptr - name;
292                                         std::string s = std::string(name, len);
293                                         if(node->short_name.empty()) {
294                                                 node->short_name = s;
295                                                 node->short_name[len - 1] = node->short_name[len - 2] = '.';
296                                         }
297                                         node->name_lines.push_back(s);
298                                         name = ptr;
299                                 }
300                                 ++ptr;
301                         }
302                         if(*name) {
303                                 if(node->short_name.empty()) {
304                                         node->short_name = name;
305                                 }
306                                 node->name_lines.push_back(name);
307                         }
308                 }
309
310                 struct stat st;
311                 if(stat(node->path, &st) == -1) {
312                         fprintf(stderr, "failed to stat: %s\n", node->path.get_path());
313                         delete node;
314                         return 0;
315                 }
316                 node->size = st.st_size;
317
318                 switch(st.st_mode & S_IFMT) {
319                 case S_IFREG:
320                         node->type = FSTYPE_FILE;
321                         break;
322
323                 case S_IFDIR:
324                         node->type = FSTYPE_DIR;
325                         break;
326
327                 case S_IFBLK:
328                 case S_IFCHR:
329                         node->type = FSTYPE_DEV;
330                         break;
331
332                 default:
333                         node->type = FSTYPE_UNKNOWN;
334                 }
335                 node_cache[abspath] = node;
336         }
337
338         return node;
339 }
340
341 FSNode *get_fsnode(const char *dir, const char *name)
342 {
343         if(!dir) {
344                 return get_fsnode(name);
345         }
346         if(!name || *name == '/') {
347                 return 0;
348         }
349
350         int len = strlen(dir) + 1 + strlen(name);
351         char *buf = new char[len + 1];
352         sprintf(buf, "%s/%s", dir, name);
353         FSNode *res = get_fsnode(buf);
354         delete [] buf;
355         return res;
356 }
357
358 // ---- FSNode implementation ----
359 FSNode::FSNode()
360 {
361         type = FSTYPE_UNKNOWN;
362         size = 0;
363         parent = 0;
364         nfiles = ndirs = 0;
365 }
366
367 bool FSNode::expand()
368 {
369         if(type != FSTYPE_DIR) {
370                 return false;
371         }
372
373         DIR *dir = opendir(path);
374         if(!dir) {
375                 fprintf(stderr, "failed to open dir: %s: %s\n", path.get_path(), strerror(errno));
376                 return false;
377         }
378
379         struct dirent *dent;
380         while((dent = readdir(dir))) {
381                 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
382                         continue;
383                 }
384
385                 FSNode *node = get_fsnode(path, dent->d_name);
386                 if(!node) continue;
387
388                 children.push_back(node);
389                 switch(node->type) {
390                 case FSTYPE_FILE:
391                         ++nfiles;
392                         break;
393                 case FSTYPE_DIR:
394                         ++ndirs;
395                 default:
396                         break;
397                 }
398         }
399         printf("expanded %d children\n", (int)children.size());
400
401         parent = get_fsnode(path.get_parent());
402         return true;
403 }