[Bf-blender-cvs] [a759d01] asset-experiments: Big refactor of filelisting in filebrowser - decouple os/fs-related direntry from actual blender listing.

Bastien Montagne noreply at git.blender.org
Wed Feb 18 21:45:42 CET 2015


Commit: a759d0190bd12303e9f1f2cf22a3c533d59b9934
Author: Bastien Montagne
Date:   Wed Feb 18 21:36:48 2015 +0100
Branches: asset-experiments
https://developer.blender.org/rBa759d0190bd12303e9f1f2cf22a3c533d59b9934

Big refactor of filelisting in filebrowser - decouple os/fs-related direntry from actual blender listing.

Main idea is that direntry had quite a bit of things already absolutely useless
in the mere 'file listing' context (image pointer, void 'data' pointer, etc.).

On the other end, to handle future asset stuff, we need *more* 'meta data' for our 'files'
(tags, variants, revisions...).

Further more, we need all this to be easily usable through RNA, i.e. we need it to
be defined in DNA.

So instead of trying to use direntry in DNA, mixing low-level system stuff like stat
where we do not want to see it, we define our own 'file' representation,
with only what we really need (who cares about chars or block or dev files in filebrowser?
we only need to know whether it's a regular file or a directory, period).

That way, low-level dirlisting remains clean, and we can easily extend our own
data as needed.

Note this new code works, does not crash (so far...) nor memleak, but it
needs quite a bit of work still, we are still storing useless stuff in direntry
(all those strings representing modes, size etc.!), and mem handling
during dir/lib listing is confusing at best (would like to use a memarena
or mempool here, but it's not that easy).

===================================================================

M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/BLI_fileops_types.h
M	source/blender/blenlib/intern/storage.c
M	source/blender/editors/interface/interface_icons.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
M	source/blender/editors/space_file/filesel.c
M	source/blender/editors/space_file/space_file.c
M	source/blender/makesdna/DNA_space_types.h

===================================================================

diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index 7898a54..342498f 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -93,10 +93,10 @@ char  *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_NONNULL();
 /* Filelist */
 
 unsigned int BLI_filelist_dir_contents(const char *dir, struct direntry **filelist);
-void BLI_filelist_duplicate(
-        struct direntry **dest_filelist, struct direntry *src_filelist, unsigned int nrentries,
-        void *(*dup_poin)(void *));
-void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries, void (*free_poin)(void *));
+void BLI_filelist_entry_duplicate(struct direntry *dst, struct direntry *src);
+void BLI_filelist_duplicate(struct direntry **dest_filelist, struct direntry *src_filelist, unsigned int nrentries);
+void BLI_filelist_entry_free(struct direntry *entry);
+void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries);
 
 /* Files */
 
diff --git a/source/blender/blenlib/BLI_fileops_types.h b/source/blender/blenlib/BLI_fileops_types.h
index 0e6eab6..96e39f8 100644
--- a/source/blender/blenlib/BLI_fileops_types.h
+++ b/source/blender/blenlib/BLI_fileops_types.h
@@ -39,8 +39,6 @@
 typedef unsigned int mode_t;
 #endif
 
-struct ImBuf;
-
 struct direntry {
 	mode_t  type;
 	char   *relname;
@@ -56,7 +54,6 @@ struct direntry {
 #else
 	struct stat s;
 #endif
-	unsigned int flags;
 	char    size[16];
 	char    mode1[4];
 	char    mode2[4];
@@ -64,11 +61,6 @@ struct direntry {
 	char    owner[16];
 	char    time[8];
 	char    date[16];
-	char    extra[16];
-	void   *poin;
-	int     nr;
-	struct ImBuf *image;
-	unsigned int selflag; /* selection flag */
 };
 
 struct dirlink {
diff --git a/source/blender/blenlib/intern/storage.c b/source/blender/blenlib/intern/storage.c
index 46c5a11..31b548d 100644
--- a/source/blender/blenlib/intern/storage.c
+++ b/source/blender/blenlib/intern/storage.c
@@ -256,7 +256,6 @@ static void bli_builddir(struct BuildDirCtx *dir_ctx, const char *dirname)
 					if (BLI_stat(fullname, &file->s) != -1) {
 						file->type = file->s.st_mode;
 					}
-					file->flags = 0;
 					dir_ctx->nrfiles++;
 					file++;
 					dlink = dlink->next;
@@ -411,53 +410,60 @@ unsigned int BLI_filelist_dir_contents(const char *dirname,  struct direntry **f
 }
 
 /**
+ * Deep-duplicate of a single direntry.
+ *
+ * \param dup_poin If given, called for each non-NULL direntry->poin. Otherwise, pointer is always simply copied over.
+ */
+void BLI_filelist_entry_duplicate(struct direntry *dst, struct direntry *src)
+{
+	*dst = *src;
+	if (dst->relname) {
+		dst->relname = MEM_dupallocN(src->relname);
+	}
+	if (dst->path) {
+		dst->path = MEM_dupallocN(src->path);
+	}
+}
+
+/**
  * Deep-duplicate of an array of direntries, including the array itself.
  *
  * \param dup_poin If given, called for each non-NULL direntry->poin. Otherwise, pointer is always simply copied over.
  */
 void BLI_filelist_duplicate(
-        struct direntry **dest_filelist, struct direntry *src_filelist, unsigned int nrentries,
-        void *(*dup_poin)(void *))
+        struct direntry **dest_filelist, struct direntry *src_filelist, unsigned int nrentries)
 {
 	unsigned int i;
 
 	*dest_filelist = MEM_mallocN(sizeof(**dest_filelist) * (size_t)(nrentries), __func__);
 	for (i = 0; i < nrentries; ++i) {
 		struct direntry * const src = &src_filelist[i];
-		struct direntry *dest = &(*dest_filelist)[i];
-		*dest = *src;
-		if (dest->image) {
-			dest->image = IMB_dupImBuf(src->image);
-		}
-		if (dest->relname) {
-			dest->relname = MEM_dupallocN(src->relname);
-		}
-		if (dest->path) {
-			dest->path = MEM_dupallocN(src->path);
-		}
-		if (dest->poin && dup_poin) {
-			dest->poin = dup_poin(src->poin);
-		}
+		struct direntry *dst = &(*dest_filelist)[i];
+		BLI_filelist_entry_duplicate(dst, src);
+	}
+}
+
+/**
+ * frees storage for a single direntry, not the direntry itself.
+ */
+void BLI_filelist_entry_free(struct direntry *entry)
+{
+	if (entry->relname) {
+		MEM_freeN(entry->relname);
+	}
+	if (entry->path) {
+		MEM_freeN(entry->path);
 	}
 }
 
 /**
  * frees storage for an array of direntries, including the array itself.
  */
-void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries, void (*free_poin)(void *))
+void BLI_filelist_free(struct direntry *filelist, unsigned int nrentries)
 {
 	unsigned int i;
 	for (i = 0; i < nrentries; ++i) {
-		struct direntry *entry = filelist + i;
-		if (entry->image) {
-			IMB_freeImBuf(entry->image);
-		}
-		if (entry->relname)
-			MEM_freeN(entry->relname);
-		if (entry->path)
-			MEM_freeN(entry->path);
-		if (entry->poin && free_poin)
-			free_poin(entry->poin);
+		BLI_filelist_entry_free(&filelist[i]);
 	}
 
 	if (filelist != NULL) {
diff --git a/source/blender/editors/interface/interface_icons.c b/source/blender/editors/interface/interface_icons.c
index 5e3089f..9702b65 100644
--- a/source/blender/editors/interface/interface_icons.c
+++ b/source/blender/editors/interface/interface_icons.c
@@ -756,7 +756,7 @@ static void init_iconfile_list(struct ListBase *list)
 		}
 	}
 
-	BLI_filelist_free(dir, totfile, NULL);
+	BLI_filelist_free(dir, totfile);
 	dir = NULL;
 }
 
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index 2eca1dd..70216d2 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -287,7 +287,7 @@ void file_calc_previews(const bContext *C, ARegion *ar)
 	UI_view2d_totRect_set(v2d, sfile->layout->width, sfile->layout->height);
 }
 
-static void file_draw_preview(uiBlock *block, struct direntry *file, const int UNUSED(index),
+static void file_draw_preview(uiBlock *block, const char *path,
                               int sx, int sy, ImBuf *imb, int icon, FileLayout *layout, bool dropshadow, bool drag)
 {
 	uiBut *but;
@@ -351,10 +351,10 @@ static void file_draw_preview(uiBlock *block, struct direntry *file, const int U
 		fdrawbox((float)xco, (float)yco, (float)(xco + ex), (float)(yco + ey));
 	}
 
-	but = uiDefBut(block, UI_BTYPE_LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, file->path);
+	but = uiDefBut(block, UI_BTYPE_LABEL, 0, "", xco, yco, ex, ey, NULL, 0.0, 0.0, 0, 0, path);
 	/* dragregion */
 	if (drag) {
-		UI_but_drag_set_image(but, file->path, icon, imb, scale);
+		UI_but_drag_set_image(but, path, icon, imb, scale);
 	}
 
 	glDisable(GL_BLEND);
@@ -442,7 +442,7 @@ void file_draw_list(const bContext *C, ARegion *ar)
 	FileLayout *layout = ED_fileselect_get_layout(sfile, ar);
 	View2D *v2d = &ar->v2d;
 	struct FileList *files = sfile->files;
-	struct direntry *file;
+	struct FileDirEntry *file;
 	ImBuf *imb;
 	uiBlock *block = UI_block_begin(C, ar, __func__, UI_EMBOSS);
 	int numfiles;
@@ -491,19 +491,21 @@ void file_draw_list(const bContext *C, ARegion *ar)
 
 		file = filelist_file(files, i);
 
-		BLI_join_dirfile(path, sizeof(path), filelist_dir(files), file->relname);
+		BLI_join_dirfile(path, sizeof(path), filelist_dir(files), file->entry->relpath);
 		name = fileentry_uiname(file, dir);
 
 		UI_ThemeColor4(TH_TEXT);
 
 
-		if (!(file->selflag & FILE_SEL_EDITING)) {
-			if ((params->active_file == i) || (file->selflag & FILE_SEL_HIGHLIGHTED) || (file->selflag & FILE_SEL_SELECTED)) {
-				int colorid = (file->selflag & FILE_SEL_SELECTED) ? TH_HILITE : TH_BACK;
-				int shade = (params->active_file == i) || (file->selflag & FILE_SEL_HIGHLIGHTED) ? 20 : 0;
+		if (!(file->entry->selflag & FILE_SEL_EDITING)) {
+			if ((params->active_file == i) || (file->entry->selflag & FILE_SEL_HIGHLIGHTED) ||
+			    (file->entry->selflag & FILE_SEL_SELECTED))
+			{
+				int colorid = (file->entry->selflag & FILE_SEL_SELECTED) ? TH_HILITE : TH_BACK;
+				int shade = (params->active_file == i) || (file->entry->selflag & FILE_SEL_HIGHLIGHTED) ? 20 : 0;
 
 				/* readonly files (".." and ".") must not be drawn as selected - set color back to normal */
-				if (FILENAME_IS_CURRPAR(file->relname)) {
+				if (FILENAME_IS_CURRPAR(file->entry->relpath)) {
 					colorid = TH_BACK;
 				}
 				draw_tile(sx, sy - 1, layout->tile_w + 4, sfile->layout->tile_h + layout->tile_border_y, colorid, shade);
@@ -512,7 +514,7 @@ void file_draw_list(const bContext *C, ARegion *ar)
 		UI_draw_roundbox_corner_set(UI_CNR_NONE);
 
 		/* don't drag parent or refresh items */
-		do_drag = !(FILENAME_IS_CURRPAR(file->relname));
+		do_drag = !(FILENAME_IS_CURRPAR(file->entry->relpath));
 
 		if (FILE_IMGDISPLAY == params->display) {
 			const int icon = filelist_geticon(files, i, false);
@@ -523,17 +525,18 @@ void file_draw_list(const bContext *C, ARegion *ar)
 				is_icon = 1;
 			}
 
-			file_draw_preview(block, file, i, sx, sy, imb, icon, layout, !is_icon && (file->flags & FILE_TYPE_IMAGE), do_drag);
+			file_draw_preview(block, path, sx, sy, imb, icon, layout,
+			                  !is_icon && (file->entry->typeflag & FILE_TYPE_IMAGE), do_drag);
 		}
 		else {
-			file_draw_icon(block, file->path, sx, sy - (UI_UNIT_Y / 6), filelist_geticon(files, i, true),
+			file_draw_icon(block, path, sx, sy - (UI_UNIT_Y / 6), filelist_geticon(files, i, true),
 			               ICON_DEFAULT_WIDTH_SCALE, ICON_DEFAULT_HEIGHT_SCALE, do_drag);
 			sx += ICON_DEFAULT_WIDTH_SCALE + 0.2f * UI_UNIT_X;
 		}
 
 		UI_ThemeColor4(TH_TEXT);
 
-		if (file->selflag & FILE_SEL_EDITING) {
+		if (file->entry->selflag & FILE_SEL_EDITING) {
 			uiBut *but;
 			short width;
 
@@ -556,19 +559,19 @@ void file_draw_list(const bContext *C, ARegion *ar)
 			UI_but_flag_enable(but, UI_BUT_NO_UTF8); /* allow non utf8 names */
 			UI_but_flag_disable(but, UI_BUT_UNDO);
 			if (false == UI_but_active_only(C, ar, block, but)) {
-				file->selflag &= ~FILE_SEL_EDITING;
+				file->entry->selflag &= ~FILE_SEL_EDITING;
 			}
 		}
 
-		if (!(file->selflag & FILE_SEL_EDITING)) {
+		if (!(file->entry->selflag & FILE_SEL_EDITING)) {
 			int tpos = (FILE_IMGDISPLAY == params->display) ? sy - layout->tile_h + layout->textheight : sy;
 			file_draw_string(sx + 1, tpos, name, (float)textwidth, textheight, align);
 		}
 
 		if (params->display == FILE_SHORTDISPLAY) {
 			sx += (int)layout->column_widths

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list