aboutsummaryrefslogtreecommitdiffstats
path: root/Library/UefiShellUfmCommandLib/panel.c
blob: c8d356583000390ab5ef62fc2fb1747312f86c49 (plain)
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
#include <Library/UefiLib.h>
#include <Library/DebugLib.h>
#include <Library/ShellLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>

#include "tbi/screen.h"
#include "tbi/win.h"
#include "dir.h"
#include "fs.h"
#include "panel.h"

#define SIZE_COLS 7
#define MODIFYTIME_COLS 12
#define MONTH_LENGTH 3

#define HIGHLIGHT_LINE_AS_CURRENT(panel, line) \
	highlight_line(panel, line, -1, EFI_CYAN)
#define UNHIGHLIGHT_LINE_AS_CURRENT(panel, line) \
	highlight_line(panel, line, -1, EFI_BLACK)
#define HIGHLIGHT_LINE_AS_MARK(panel, line) \
	highlight_line(panel, line, EFI_YELLOW, -1)
#define UNHIGHLIGHT_LINE_AS_MARK(panel, line) \
	highlight_line(panel, line, EFI_LIGHTGRAY, -1)

STATIC CONST CHAR16 *get_month_string(UINT8 month)
{
	switch(month) {
		case 1: return L"jan";
		case 2: return L"feb";
		case 3: return L"mar";
		case 4: return L"apr";
		case 5: return L"may";
		case 6: return L"jun";
		case 7: return L"jul";
		case 8: return L"aug";
		case 9: return L"sep";
		case 10: return L"oct";
		case 11: return L"nov";
		case 12: return L"dec";
		default: break;
	}

	return L"xxx";
}

STATIC VOID clear_list_line(struct panel_ctx *p, UINTN line)
{
	mvwprintf(p->wlist, 0, line, L"%-*s%c%*s%c%*s",
		p->name_cols, L"", BOXDRAW_VERTICAL,
		SIZE_COLS, L"", BOXDRAW_VERTICAL,
		MODIFYTIME_COLS, L"");
}

STATIC VOID display_fs(struct panel_ctx *p, UINTN start_index)
{
	UINTN line, i;

	for(line = 0, i = start_index - 1; line < p->list_lines; line++, i++)
	{
		if(i >= p->fsa->len) {
			clear_list_line(p, line);
			continue;
		}

		mvwprintf(p->wlist, 0, line, L"%-*s%c%*s%c%*s",
			p->name_cols, p->fsa->full_name[i], BOXDRAW_VERTICAL,
			SIZE_COLS, L"<fsys>", BOXDRAW_VERTICAL,
			MODIFYTIME_COLS, L"");
	}
}

STATIC VOID display_files(struct panel_ctx *p, UINTN start_index)
{
	UINTN i;
	EFI_SHELL_FILE_INFO *list_head, *node;
	EFI_TIME cur_time, *mod_time;
	BOOLEAN directory;

	gRT->GetTime(&cur_time, NULL);
	list_head = p->dirs->list_head;

	node = dirl_getn(p->dirs, start_index);
	for(i = 0; i <= p->list_lines; i++)
	{
		if(IsNull(&list_head->Link, &node->Link) || 
				((start_index + i) > p->dirs->len)) {
			clear_list_line(p, i);
			continue;
		}

		mod_time = &(node->Info->ModificationTime);
		directory = FALSE;
		if((node->Info->Attribute & EFI_FILE_DIRECTORY) != 0)
			directory = TRUE;

		// mvwprintf doesn't support "-*.*s" :(

		// column "name"
		mvwprintf(p->wlist, 0, i, L"%c%-*s", directory ? L'/' : L' ',
			p->name_cols - 1, node->FileName);

		// column "size"
		if(directory)
			mvwprintf(p->wlist, p->name_cols, i, L"%c%*.*s%c", BOXDRAW_VERTICAL,
				SIZE_COLS, SIZE_COLS, L"<dir>");
		else
			mvwprintf(p->wlist, p->name_cols, i, L"%c%*d%c", BOXDRAW_VERTICAL,
				SIZE_COLS, node->Info->FileSize);

		// column "modify time"
		mvwprintf(p->wlist, p->wlist->width - MODIFYTIME_COLS - 1, i, 
			L"%c%*s %02u ", BOXDRAW_VERTICAL, MONTH_LENGTH,
			get_month_string(mod_time->Month), mod_time->Day);
		if(cur_time.Year == mod_time->Year)
			mvwprintf(p->wlist, p->wlist->width - MODIFYTIME_COLS + 7, i,
				L"%02u:%02u", mod_time->Hour, mod_time->Minute);
		else
			mvwprintf(p->wlist, p->wlist->width - MODIFYTIME_COLS + 7, i,
				L"%5u", mod_time->Year);

		node = (EFI_SHELL_FILE_INFO *)GetNextNode(&list_head->Link, &node->Link);
	}
}

STATIC VOID highlight_line(struct panel_ctx *p, UINTN line, INT32 fg, INT32 bg)
{
	CHAR16 *str;
	INT32 attr;
	UINTN ui_line;

	ui_line = (line-1) % p->list_lines;
	attr = p->wlist->attr[ui_line][0];
	if(fg < 0x0)
		fg = attr & 0x0F;
	if(bg < 0x0)
		bg = ((attr & 0xF0) >> 4);
	attr = EFI_TEXT_ATTR(fg, bg);
	
	wattrset(p->wlist, attr);
	str = p->wlist->text[ui_line];
	mvwprintf(p->wlist, 0, ui_line, str);
	wattroff(p->wlist);

	wrefresh(p->wlist);
}

STATIC VOID set_cwd(struct panel_ctx *p, CONST CHAR16 *path)
{
	if(p->cwd) {
		FreePool(p->cwd);
		p->cwd = NULL;
	}
	if(path)
		StrnCatGrow(&p->cwd, NULL, path, 0);

	mvwhline(p->wcwd, 0, 0, BOXDRAW_HORIZONTAL, p->wcwd->width);
	wattrset(p->wcwd, EFI_TEXT_ATTR(EFI_WHITE, EFI_BLACK));
	mvwprintf(p->wcwd, 0, 0, L" %s ", p->cwd ? p->cwd : L" ");
	wattroff(p->wcwd);
	wrefresh(p->wcwd);
}

STATIC VOID update_file_info(struct panel_ctx *p)
{
	EFI_SHELL_FILE_INFO *file;
	CONST CHAR16 *name;

	if(!p->cwd)
		name = p->fsa->full_name[p->curline - 1];
	else {
		file = dirl_getn(p->dirs, p->curline);
		name = file->FileName;
	}

	mvwprintf(p->wfname, 0, 0, L"%-*s", p->wfname->width, name);
	wrefresh(p->wfname);
}

STATIC VOID update_marked_info(struct panel_ctx *p)
{
	mvwhline(p->wmarked, 0, 0, BOXDRAW_HORIZONTAL, p->wmarked->width);
	if(p->marked) {
		wattrset(p->wmarked, EFI_TEXT_ATTR(EFI_WHITE, EFI_BLACK));
		mvwprintf(p->wmarked, 0, 0, L" Files: %d ", p->marked);
		wattroff(p->wmarked);
	}
	wrefresh(p->wmarked);
}

struct panel_ctx *panel_alloc(struct screen *scr, CONST CHAR16 *path,
		INT32 cols, INT32 lines, INT32 x, INT32 y)
{
	ASSERT(scr != NULL);

	struct panel_ctx *panel;
	UINTN name_cols = cols - 1 - MODIFYTIME_COLS - 1 - SIZE_COLS - 1 - 1;
	BOOLEAN res = FALSE;

	panel = AllocateZeroPool(sizeof(struct panel_ctx));
	if(!panel)
		return NULL;

	do { // START DO

	panel->wbg = newwin(scr, cols, lines, x, y);
	if(!panel->wbg)
		break;
	wborder(panel->wbg,
		BOXDRAW_VERTICAL, BOXDRAW_VERTICAL,
		BOXDRAW_HORIZONTAL, BOXDRAW_HORIZONTAL,
		BOXDRAW_DOWN_RIGHT, BOXDRAW_DOWN_LEFT,
		BOXDRAW_UP_RIGHT, BOXDRAW_UP_LEFT
	);
	mvwprintf(panel->wbg, 1 + ((name_cols - 4) / 2), 1, L"Name");
	mvwvline(panel->wbg, 1 + name_cols, 1, BOXDRAW_VERTICAL, 1);
	mvwprintf(panel->wbg, 1 + name_cols + 1 + ((SIZE_COLS - 4) / 2), 1, L"Size");
	mvwvline(panel->wbg, cols - 1 - MODIFYTIME_COLS - 1, 1, BOXDRAW_VERTICAL, 1);
	mvwprintf(panel->wbg, 1 + name_cols + 1 + SIZE_COLS + 1 + ((MODIFYTIME_COLS - 11) / 2), 1, L"Modify Time");
	
	panel->wcwd = newwin(scr, cols - 4, 1, x + 2, y);
	if(!panel->wcwd)
		break;

	panel->wlist = newwin(scr, cols - 2, lines - 5, x + 1, y + 2);
	if(!panel->wlist)
		break;

	mvwhline(panel->wbg, 1, lines - 3, BOXDRAW_HORIZONTAL, cols - 2);
	panel->wfname = newwin(scr, cols - 2, 1, x + 1, lines - 1);
	if(!panel->wfname)
		break;

	panel->wmarked = newwin(scr, cols - 4, 1, x + 2, lines - 2);
	if(!panel->wmarked)
		break;

	res = TRUE;

	} while(0); // END DO

	if(!res) {
		panel_release(panel);
		return NULL;
	}

	panel->name_cols = name_cols;
	panel->curline = 1;
	panel->list_lines = lines - 5;
	panel->start_entry = 1;

	wrefresh(panel->wbg);
	res = panel_show(panel, path);
	if(!res) {
		panel_release(panel);
		return NULL;
	}
	return panel;
}

VOID panel_release(struct panel_ctx *p)
{
	ASSERT(p != NULL);

	if(p->wbg)
		delwin(p->wbg);
	if(p->wcwd)
		delwin(p->wcwd);
	if(p->wlist)
		delwin(p->wlist);
	if(p->wfname)
		delwin(p->wfname);
	if(p->wmarked)
		delwin(p->wmarked);
	if(p->cwd)
		FreePool(p->cwd);

	FreePool(p);
}
BOOLEAN panel_move_cursor(struct panel_ctx *p, UINTN line)
{
	UINTN oldline = p->curline;
	UINTN maxlen = (p->cwd) ? p->dirs->len : p->fsa->len;

	if(line < 1 || line > maxlen)
		return FALSE;

	if(line < p->start_entry || line >= (p->start_entry + p->list_lines) ||
			p->start_entry == 0)
	{
		p->start_entry = line - ((line-1) % p->list_lines);
		
		if(!p->cwd)
			display_fs(p, p->start_entry);
		else
			display_files(p, p->start_entry);
	}
	else
		UNHIGHLIGHT_LINE_AS_CURRENT(p, oldline);

	p->curline = line;
	update_file_info(p);
	HIGHLIGHT_LINE_AS_CURRENT(p, p->curline);
	return TRUE;
}