1 /* fsv - 3D File System Visualizer
2  * Copyright (C)2009-1010 Yury P. Fedorchenko <yuryfdr@users.sf.net>
3  * Copyright (C)1999 Daniel Richard G. <skunk@mit.edu>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 
20 #ifndef FSV_COMMON_H
21 #define FSV_COMMON_H
22 
23 #define GETTEXT_PACKAGE PACKAGE
24 
25 /* Autoconf */
26 #include "config.h"
27 
28 /* System stuff */
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include <string.h>
33 #include <sys/types.h>
34 
35 /* GLib */
36 #include <glib.h>
37 
38 /* Internationalization */
39 #ifdef ENABLE_NLS
40 #  include <libintl.h>
41 #  undef _
42 #  define _(String) dgettext (GETTEXT_PACKAGE, String)
43 #  ifdef gettext_noop
44 #    define N_(String) gettext_noop (String)
45 #    define __(String) gettext_noop (String)
46 #  else
47 #    define N_(String) (String)
48 #    define __(String) (String)
49 #  endif
50 #else
51 #  define textdomain(String) (String)
52 #  define gettext(String) (String)
53 #  define dgettext(Domain,Message) (Message)
54 #  define dcgettext(Domain,Message,Type) (Message)
55 #  define bindtextdomain(Domain,Directory) (Domain)
56 #  define _(String) (String)
57 #  define __(String) (String)
58 #  define N_(String) (String)
59 #endif
60 
61 /* Debugging */
62 #ifdef DEBUG
63 	#include "debug.h"
64 #else
65 	#define _xfree xfree
66 #endif
67 
68 
69 /**** Constants, macros, types ****************/
70 
71 /* Configuration file (in user's home directory) */
72 #define CONFIG_FILE		"~/.config/fsv2rc"
73 
74 /* Mathematical constants et. al. */
75 #define LN_2			0.69314718055994530942
76 #define SQRT_2			1.41421356237309504880
77 #define MAGIC_NUMBER		1.61803398874989484821
78 #define PI			3.14159265358979323846
79 #define EPSILON			1.0e-6
80 #define NIL			0
81 #define NULL_DLIST		0
82 
83 /* Alias for the root directory node */
84 #define root_dnode		globals.fstree->children
85 
86 /* Mathematical macros */
87 #define SQR(x)			((x)*(x))
88 #define NRATIO(x,y)		(MIN(ABS(x),ABS(y)) / MAX(ABS(x),ABS(y)))
89 #define DEG(angleRad)		((angleRad) * (180.0 / PI))
90 #define RAD(angle360)		((angle360) * (PI / 180.0))
91 #define DOT_PROD(v1,v2)		(v1.x * v2.x + v1.y * v2.y + v1.z * v2.z)
92 #define XY_LEN(v)		hypot( v.x, v.y )
93 #define XYZ_LEN(v)		sqrt( SQR(v.x) + SQR(v.y) + SQR(v.z) )
94 #define INTERPOLATE(k,a,b)	((a) + (k)*((b) - (a)))
95 
96 /* Convenience macros */
97 #define G_LIST_PREPEND(l,d)	l = g_list_prepend( l, d )
98 #define G_LIST_APPEND(l,d)	l = g_list_append( l, d )
99 #define G_LIST_INSERT_BEFORE(l,bd,d)	l = g_list_insert_before( l, bd, d )
100 #define G_LIST_REMOVE(l,d)	l = g_list_remove( l, d )
101 #define G_LIST_SORT(l,f)	l = g_list_sort( l, (GCompareFunc)f )
102 #define NEW(type)		(type *)xmalloc( sizeof(type) )
103 #define NEW_ARRAY(type,n)	(type *)xmalloc( (n) * sizeof(type) )
104 
105 /* For when a switch should never default */
106 #define SWITCH_FAIL		default: g_assert_not_reached( ); exit( EXIT_FAILURE );
107 
108 /* Cylindrical-space distance */
109 #define	_RTZDIST1(v1,v2)	SQR(v1.r) + SQR(v2.r) + SQR(v2.z - v1.z)
110 #define	_RTZDIST2(v1,v2)	(2.0 * v1.r * v2.r)
111 #define _RTZDIST3(v1,v2)	sin( RAD(v1.theta) ) * sin( RAD(v2.theta) )
112 #define _RTZDIST4(v1,v2)	cos( RAD(v1.theta) ) * cos( RAD(v2.theta) )
113 #define RTZ_DIST(v1,v2)		sqrt( fabs( _RTZDIST1(v1,v2) - _RTZDIST2(v1,v2) * (_RTZDIST3(v1,v2) + _RTZDIST4(v1,v2)) ) )
114 
115 /* Returns information about the given node */
116 #define NODE_DESC(node)		((NodeDesc *)(node)->data)
117 #define DIR_NODE_DESC(dnode)	((DirNodeDesc *)(dnode)->data)
118 #define NODE_IS_DIR(node)	(NODE_DESC(node)->type == NODE_DIRECTORY)
119 #define NODE_IS_METANODE(node)	(NODE_DESC(node)->type == NODE_METANODE)
120 #define DIR_COLLAPSED(dnode)	(DIR_NODE_DESC(dnode)->deployment < EPSILON)
121 #define DIR_EXPANDED(dnode)	(DIR_NODE_DESC(dnode)->deployment > (1.0 - EPSILON))
122 
123 
124 /* Nonstandard but nice */
125 typedef gint64 int64;
126 typedef unsigned int bitfield;
127 typedef guint8 byte;
128 typedef gboolean boolean;
129 
130 /* Program modes */
131 typedef enum {
132 	FSV_DISCV,
133 	FSV_MAPV,
134 	FSV_TREEV,
135 	FSV_SPLASH,
136 	FSV_NONE
137 } FsvMode;
138 
139 /* The various types of nodes */
140 typedef enum {
141 	NODE_METANODE,
142 	NODE_DIRECTORY,
143 	NODE_REGFILE,
144 	NODE_SYMLINK,
145 	NODE_FIFO,
146 	NODE_SOCKET,
147 	NODE_CHARDEV,
148 	NODE_BLOCKDEV,
149 	NODE_UNKNOWN,
150 	NUM_NODE_TYPES
151 } NodeType;
152 
153 
154 /**** Global data structures ****************/
155 
156 /* RGB color definition (components are in range [0, 1]) */
157 typedef struct _RGBcolor RGBcolor;
158 struct _RGBcolor {
159 	float	r;
160 	float	g;
161 	float	b;
162 };
163 
164 /* Point/vector definition (2D Cartesian coordinates) */
165 typedef struct _XYvec XYvec;
166 struct _XYvec {
167 	double	x;
168 	double	y;
169 };
170 
171 /* Point/vector definition (3D Cartesian coordinates) */
172 typedef struct _XYZvec XYZvec;
173 struct _XYZvec {
174 	double	x;
175 	double	y;
176 	double	z;
177 };
178 
179 /* Point/vector definition (2D polar coordinates) */
180 typedef struct _RTvec RTvec;
181 struct _RTvec {
182 	double	r;
183 	double	theta;
184 };
185 
186 /* Point/vector definition (3D cylindrical coordinates) */
187 typedef struct _RTZvec RTZvec;
188 struct _RTZvec {
189 	double	r;
190 	double	theta;
191 	double	z;
192 };
193 
194 /* Base node descriptor. Describes a filesystem node
195  * (file/symlink/whatever) */
196 typedef struct _NodeDesc NodeDesc;
197 struct _NodeDesc {
198 	NodeType	type;		/* Type of node */
199 	unsigned int	id;		/* Unique ID number */
200 	const char	*name;		/* Base name (w/o directory) */
201 	int64		size;		/* Size (bytes) */
202 	int64		size_alloc;	/* Size allocation on storage medium */
203 	uid_t		user_id;	/* Owner UID */
204 	gid_t		group_id;	/* Group GID */
205 	bitfield	perms : 10;	/* Permission flags */
206 	bitfield	flags : 2;	/* Extra (mode-specific) flags */
207 	time_t		atime;		/* Last access time */
208 	time_t		mtime;		/* Last modification time */
209 	time_t		ctime;		/* Last attribute change time */
210 	const RGBcolor	*color;		/* Node color */
211 	double		geomparams[5];	/* Geometry parameters */
212 };
213 
214 /* Directories have their own extended descriptor */
215 typedef struct _DirNodeDesc DirNodeDesc;
216 struct _DirNodeDesc {
217 	NodeDesc	node_desc;
218 	double		geomparams2[3];	/* More geometry parameters */
219 	double		deployment;	/* 0 == collapsed, 1 == expanded */
220 	/* Subtree information. The quantities here do not include the
221 	 * contribution of the root of the subtree (i.e. THIS node) */
222 	struct {
223 		int64		size;	/* Total subtree size (bytes) */
224 		unsigned int	counts[NUM_NODE_TYPES]; /* Node type totals */
225 	} subtree;
226 	/* Following pointer should be of type GtkTreePath */
227 	//Glib::RefPtr<Glib::ustring> ctnode;
228 	char *ctnode;	/* Directory tree entry */
229 	unsigned int	a_dlist;	/* Display list A */
230 	unsigned int	b_dlist;	/* Display list B */
231 	unsigned int	c_dlist;	/* Display list C */
232 	/* Flag: TRUE if directory geometry is being drawn expanded */
233 	bitfield	geom_expanded : 1;
234 	/* Flags: TRUE if geometry in X_dlist needs to be rebuilt */
235 	bitfield	a_dlist_stale : 1;
236 	bitfield	b_dlist_stale : 1;
237 	bitfield	c_dlist_stale : 1;
238 };
239 
240 /* Generalized node descriptor */
241 union AnyNodeDesc {
242 	NodeDesc	node_desc;
243 	DirNodeDesc	dir_node_desc;
244 };
245 #ifdef __cplusplus
246 #include <glibmm.h>
247 /*
248 // Node information struct. Everything here is a string.
249 //  get_node_info( ) fills in all the fields
250 struct NodeInfo {
251 	char *name;		// Name (without directory components)
252 	char *prefix;		// Leading directory components
253 	char *size;		// Size (in bytes)
254 	char *size_abbr;	// Abbreviated size (e.g. "5kB")
255 	char *size_alloc;	// Allocation size (bytes)
256 	char *size_alloc_abbr;	// Abbreviated allocation size
257 	char *user_name;	// Owner's user name
258 	char *group_name;	// Owner's group name
259 	char *atime;		// Last access time
260 	char *mtime;		// Last modification time
261 	char *ctime;		// Last attribute change time
262 	// For directories
263 	char *subtree_size;	// Total size of subtree (bytes)
264 	char *subtree_size_abbr; // Abbreviated total size of subtree
265 	// For regular files
266 	char *file_type_desc;	// Verbose description of file type
267 	// For symbolic links
268 	char *target;		// Target of symlink
269 	char *abstarget;	// Absolute name of target
270 };
271 */
272 struct Globals {
273 	/* The filesystem tree */
274 	GNode *fstree;
275 
276 	/* Current node of interest */
277 	GNode *current_node;
278 
279 	/* History of previously visited nodes
280 	 * (elements are of type GNode) */
281 	GList *history;
282   //Glib::NodeTree<AnyNodeDesc> AnyNodeTree;
283 };
284 
285 /**** Global variables ****************/
286 
287 extern struct Globals globals;
288 extern char **node_type_xpms[NUM_NODE_TYPES];
289 extern char **node_type_mini_xpms[NUM_NODE_TYPES];
290 extern const char *node_type_names[NUM_NODE_TYPES];
291 extern const char *node_type_plural_names[NUM_NODE_TYPES];
292 
293 
294 /**** Prototypes for common library functions ****************/
295 #include <string>
296 std::string abbrev_size( int64 size );
297 std::string get_file_type_desc( const std::string& filename );
298 std::string node_absname( const GNode *node );
299 extern "C" {
300 #endif
301 /* Global variables container */
302 struct Globalsc {
303 	/* Current program mode */
304 	FsvMode fsv_mode;
305 
306 	/* TRUE when viewport needs to be redrawn */
307 	boolean need_redraw;
308 };
309 
310 extern struct Globalsc globalsc;
311 
312 #ifndef DEBUG
313 void *xmalloc( size_t size );
314 void *xrealloc( void *block, size_t size );
315 char *xstrdup( const char *string );
316 //char *xstrredup( char *old_string, const char *string );
317 void xfree( void *block );
318 
319 #endif /* not DEBUG */
320 //char *strrecat( char *string, const char *add_string );
321 char *xstrstrip( char *string );
322 boolean xfork( void);
323 //const char *xgetcwd( void);
324 double xgettime( void);
325 //const char *i64toa( int64 number );
326 GNode *node_named( const char *absname );
327 //const struct NodeInfo *get_node_info( GNode *node );
328 const char *rgb2hex( RGBcolor *color );
329 RGBcolor hex2rgb( const char *hex_color );
330 RGBcolor rainbow_color( double x );
331 RGBcolor heat_color( double x );
332 //GList *g_list_replace( GList *list, gpointer old_data, gpointer new_data );
333 //!!!GList *g_list_insert_before( GList *list, gpointer before_data, gpointer data );
334 //int gnome_config_get_token( const char *path, const char **tokens );
335 //void gnome_config_set_token( const char *path, int new_value, const char **tokens );
336 void quit( char *message );
337 
338 #ifdef __cplusplus
339 };
340 #endif
341 
342 #endif /* end common.h */
343 
344