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