File: fgui.c

package info (click to toggle)
freesweep 0.90-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 632 kB
  • ctags: 318
  • sloc: ansic: 5,525; sh: 2,180; makefile: 206
file content (328 lines) | stat: -rw-r--r-- 7,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**********************************************************************
*  This source code is copyright 1999 by Gus Hartmann & Peter Keller  *
*  It may be distributed under the terms of the GNU General Purpose   *
*  License, version 2 or above; see the file COPYING for more         *
*  information.                                                       *
*                                                                     *
*  $Id: fgui.c,v 1.17 2002/07/12 07:08:03 hartmann Exp $
*                                                                     *
**********************************************************************/

#include "sweep.h"

/* percentages of screen space utilized from LINES and COLS */
/* This means I want a window FSWIDTH% of the screen, centered and 
 * FSHEIGHT% of the height, centered */
#define FSWIDTH 0.80
#define FSHEIGHT 0.80

static struct FileBuf* CreateFileBuf(char *dir);
static void DestroyFileBuf(struct FileBuf *head);
static char *FSelector(void);
static char* Choose(struct FileBuf *fb);
static void Display(WINDOW *fgui, struct FileBuf *fb, int find, int cursor, 
	int amount);
static int qstrcmp(const void *l, const void *r);

/* make a linked list of filenames given a directory */
struct FileBuf* CreateFileBuf(char *dir)
{
	char *path = NULL;
	struct FileBuf *head = NULL;
	struct FileBuf *tail = NULL;
	struct FileBuf *del = NULL;
	struct FileBuf *tmp = NULL;
	DIR *dent = NULL;
	struct dirent *dp = NULL;
	struct FileBuf *farray = NULL;
	int count = 0;
	char **sort = NULL;

	chdir(dir);
#if defined(PATH_MAX)
	path = xgetcwd(NULL, PATH_MAX);
#elif defined(HAVE_GET_CURRENT_DIR_NAME)
	path = get_current_dir_name();
#else /* Now what? No idea! */
#error "Don't know how to proceed on systems without PATH_MAX or get_current_dir_name"
#endif

	dent = xopendir(dir);

	if ( dent == NULL )
	{
		return NULL;
	}

	/* create a linked list of entries, don't use some of the fields for now */
	while ((dp = readdir(dent)) != NULL)
	{
		tmp = xmalloc(sizeof(struct FileBuf) * 1);
		tmp->fpath = xmalloc(strlen(dp->d_name) + strlen(path) + 2);
		tmp->next = NULL;

		/* create the full name for it */
		strcpy(tmp->fpath, path);
		if (strcmp(path,"/")!=0)
		{
			strcat(tmp->fpath, "/");
		}
		strcat(tmp->fpath, dp->d_name);
		
		/* insert it into the list, the right way */
		if (head == NULL)
		{
			head = tmp;
		}
		else
		{
			tail->next = tmp;
		}
		tail = tmp;
	}
	closedir(dent);

	/* ok, take that linked list, and make an array out of it. Then free it */
	
	tmp = head;
	while(tmp != NULL)
	{
		count++;
		tmp = tmp->next;
	}
	
	farray = (struct FileBuf *)xmalloc(sizeof(struct FileBuf) * count);
	/* the first element in the array holds how many there are */
	farray[0].numents = count;
	farray[0].path = path;

	/* stick it into the temporary array for sorting */
	sort = (char**)xmalloc(sizeof(char*) * farray[0].numents);
	count = 0;
	tmp = head;
	while(tmp != NULL)
	{
		/* move the memory over to it */
		sort[count] = tmp->fpath;
		count++;
		tmp = tmp->next;
	}

	/* sort it nicely */
	qsort(sort, farray[0].numents, sizeof(char*), qstrcmp);

	/* now copy it out of the tmp array into the fb */
	count = 0;
	tmp = head;
	while(tmp != NULL)
	{
		/* move the memory over to it */
		farray[count].fpath = sort[count];
		count++;
		tmp = tmp->next;
	}
	free(sort);

	/* free the old list, fpath will be owned by farray now */
	tmp = head;
	while(tmp != NULL)
	{
		del = tmp;
		tmp = tmp->next;
		free(del);
	}

	return farray;
}

/* a glorified strcmp for qsort */
int qstrcmp(const void *l, const void *r)
{
	char *left = NULL;
	char *right = NULL;

	left = *(char**)l;
	right = *(char**)r;

	return strcmp(left,right);
}

/* destroy a list of directory names */
void DestroyFileBuf(struct FileBuf *head)
{
	int i = 0;

	free(head[0].path);

	for (i = 0; i < head[0].numents; i++)
	{
		free(head[i].fpath);
	}

	free(head);
}

/* allow the user to continue selecting stuff until a file is selected */
char *FSelector(void)
{
	char *selection = NULL;
	struct stat buf;
	struct FileBuf *fb = NULL;

	fb = CreateFileBuf(".");

	selection = Choose(fb);
	stat(selection, &buf);

	/* keep going until I get something that isn't a directory */
	while(S_ISDIR(buf.st_mode))
	{
		DestroyFileBuf(fb);
		if ( ( fb = CreateFileBuf(selection) ) == NULL )
		{
			return NULL;
		}
		free(selection);
		selection = Choose(fb);
		stat(selection, &buf);
	}

	DestroyFileBuf(fb);

	return selection;
}

/* this is the nasty function that parses the filedesc and spits up a window
	for you so you can select something out of it. XXX Must implement "this
	is correct" option */
char* Choose(struct FileBuf *fb)
{
	WINDOW *gui = NULL, *fgui = NULL;
	int nlines, ncols, bx, by;
	int xdist, ydist;
	chtype in;
	int cursor = 1; /* where the cursor starts in relation to the window */
	int find = 0;	/* the index of the top of the fb display */

	/* the area I have to put the window */
	xdist = (COLS-INFO_W-2);
	ydist = (LINES-6);

	/* find out how big, and where the window is, it is also centered */
	nlines = (int)(ydist * FSHEIGHT);
	ncols = (int)(xdist * FSWIDTH);
	bx = xdist/2 - ncols/2;
	by = ydist/2 - nlines/2;

	/* make border window */
	gui = newwin(nlines, ncols, by, bx);
	if (gui == NULL)
	{
		perror("Choose::newwin");
		exit(EXIT_FAILURE);
	}
	wborder(gui,CharSet.VLine,CharSet.VLine,CharSet.HLine,CharSet.HLine,
		CharSet.ULCorner,CharSet.URCorner,CharSet.LLCorner,CharSet.LRCorner);
	wmove(gui, by, bx);
	wrefresh(gui);

	/* make the window that will hold the information I want */
	fgui = derwin(gui, nlines-2, ncols-2, 1, 1);

	/* set it to the head of the list */
	werase(fgui);
	Display(fgui, fb, find, cursor, nlines-3);
	wrefresh(fgui);

	/* most of the nlines-3 stuff is because the first line is reserved for
	 * the path. */

	in=getch();

	if ( in == KEY_LEFT )
	{
		/* Find a way to return without catching an error. */
		return NULL;
	}
	
	while ((in != ' ') && (in != KEY_RIGHT))
	{
		switch (in)
		{
			/* move the cursor down */
			case 'j': case KEY_DOWN:

				if (cursor < (nlines - 3) && cursor < fb[0].numents)
				{
					cursor++;
				}
				else if (find != fb[0].numents - (nlines-3) && 
					fb[0].numents > nlines-3)
				{
					find++;
				}
				break;
			/* move the cursor up */
			case 'k': case KEY_UP:
				if (cursor > 1)
				{
					cursor--;
				}
				else if (find > 0)
				{
					find--;
				}
				break;
			default:
				break;
		}
		werase(fgui);
		Display(fgui, fb, find, cursor, nlines-3);
		wrefresh(fgui);
		in=getch();
	}
	
	werase(fgui);
	werase(gui);
	wrefresh(fgui);
	wrefresh(gui);
	delwin(fgui);
	delwin(gui);

	return strdup(fb[find + cursor - 1].fpath);
}

/* draw the part of the FileBuf I have specified in the window */
void Display(WINDOW *fgui, struct FileBuf *fb, int find, 
	int cursor, int amount)
{
	char *p = NULL;
	int i;

	/* display the path, on the first line */
	mvwprintw(fgui, 0, 0, "-->");
	mvwprintw(fgui, 0, 3, fb[0].path);

	for (i = 0; i < amount && i+find < fb[0].numents; i++)
	{
		p = fb[find+i].fpath + strlen(fb[find+i].fpath) - 1;
		while(*p != '/') p--; p++; /* there will always be a / in it */
		mvwprintw(fgui, i+1, 1, p);
	}

	/* highlight the cursor line */
	mvwprintw(fgui, cursor, 0, ">");
}

/* This controls all of the logic for saving a file, when it is done, it
	returns the file name to where to save the game. */
char* FSGUI(void)
{
	char *file = NULL;

	StopTimer();
	file = FSelector();
	StartTimer();

	return file;
}