12 #include "gmath/gmath.h"
16 static IconRenderer *iconrend;
18 static std::map<std::string, FSNode*> node_cache;
19 static FSNode *cur_node;
20 static int start_child;
24 iconrend = new ShapesIcons;
25 if(!iconrend->init()) {
29 cur_node = get_fsnode(0);
36 std::map<std::string, FSNode*>::iterator it = node_cache.begin();
37 while(it != node_cache.end()) {
38 FSNode *node = it++->second;
45 static Vec3 icon_pos(int row, int col, int ncols, float row_spacing, float radius)
47 float angle = 2.0 * M_PI * (float)col / (float)ncols;
48 float x = sin(angle) * radius;
49 float z = -cos(angle) * radius;
50 float y = (float)row * row_spacing;
56 static const int ncols = 8;
57 static const float row_spacing = 2.0;
58 static const float radius = 5;
61 base_xform.rotate(time_sec, 0, 0);
62 base_xform.rotate(0, 0, time_sec * 0.5);
63 base_xform.translate(0, 2, 0);
66 glDisable(GL_TEXTURE_2D);
68 int first = start_child % ncols;
69 int nchildren = (int)cur_node->children.size();
72 for(int i=0; i<nchildren; i++) {
73 int idx = (i + first) % nchildren;
75 if(cur_node->children[idx]->type == FSTYPE_DIR) {
79 Vec3 pos = icon_pos(row, col, ncols, row_spacing, radius);
81 Mat4 xform = base_xform;
85 glMultMatrixf(xform[0]);
86 iconrend->draw(cur_node->children[idx]);
96 FSNode *get_fsnode(const char *path)
98 char *abspath = make_abs_path(path);
100 FSNode *node = node_cache[abspath];
106 if(stat(node->path, &st) == -1) {
107 fprintf(stderr, "failed to stat: %s\n", node->path.get_path());
111 node->size = st.st_size;
113 switch(st.st_mode & S_IFMT) {
115 node->type = FSTYPE_FILE;
119 node->type = FSTYPE_DIR;
124 node->type = FSTYPE_DEV;
128 node->type = FSTYPE_UNKNOWN;
130 node_cache[abspath] = node;
136 FSNode *get_fsnode(const char *dir, const char *name)
139 return get_fsnode(name);
141 if(!name || *name == '/') {
145 int len = strlen(dir) + 1 + strlen(name);
146 char *buf = new char[len + 1];
147 sprintf(buf, "%s/%s", dir, name);
148 FSNode *res = get_fsnode(buf);
153 // ---- FSNode implementation ----
156 type = FSTYPE_UNKNOWN;
161 bool FSNode::expand()
163 if(type != FSTYPE_DIR) {
167 DIR *dir = opendir(path);
169 fprintf(stderr, "failed to open dir: %s: %s\n", path.get_path(), strerror(errno));
174 while((dent = readdir(dir))) {
175 if(strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
179 FSNode *node = get_fsnode(path, dent->d_name);
182 children.push_back(node);
184 printf("expanded %d children\n", (int)children.size());
186 parent = get_fsnode(path.get_parent());