blob: d00245c3edcd992ec9a16481bd8b845e6f6fdfc2 [file] [log] [blame]
kthacker62e146c2006-04-17 15:11:35 +00001/*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
14 * 3. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment
34 *
35 * $Id: directory.c,v 1.1 2006/04/17 15:18:26 kthacker Exp $
36 *
37 */
38
39#include <stdlib.h>
40//#include <cbm.h>
41#include <string.h>
42
43#include "ctk.h"
44#include "ctk-draw.h"
45#include "dispatcher.h"
46#include "loader.h"
47
48#include "program-handler.h"
49
50#define MAX_NUMFILES 40
51#define WIDTH 36
52#define HEIGHT 22
53
54static struct dsc *dscs[MAX_NUMFILES];
55static char filenames[MAX_NUMFILES][16];
56static unsigned char numfiles, morestart;
57
58static struct ctk_window window;
59
60static struct ctk_label description =
61 {CTK_LABEL(0, HEIGHT - 1, WIDTH, 1, "")};
62
63static char autoexit = 1;
64const static struct ctk_button autoexitbutton =
65 {CTK_BUTTON(0, 20, 9, "Auto-exit")};
66const static char autoexiton[] = "is On ";
67const static char autoexitoff[] = "is Off";
68static struct ctk_label autoexitlabel =
69 {CTK_LABEL(12, 20, 6, 1, autoexiton)};
70
71const static struct ctk_button morebutton =
72 {CTK_BUTTON(0, 20, 4, "More")};
73
74const static struct ctk_button backbutton =
75 {CTK_BUTTON(0, 20, 4, "Back")};
76
77const static struct ctk_button reloadbutton =
78 {CTK_BUTTON(30, 20, 6, "Reload")};
79
80static DISPATCHER_SIGHANDLER(directory_sighandler, s, data);
81static struct dispatcher_proc p =
82 {DISPATCHER_PROC("Directory browser", NULL, directory_sighandler, NULL)};
83static ek_id_t id;
84
85static unsigned char width, height;
86
87/*-----------------------------------------------------------------------------------*/
88//static struct cbm_dirent dirent;
89/*-----------------------------------------------------------------------------------*/
90static void
91show_statustext(char *text)
92{
93 ctk_label_set_text(&description, text);
94 CTK_WIDGET_REDRAW(&description);
95}
96/*-----------------------------------------------------------------------------------*/
97#define LFN 9
98static void
99loaddirectory(void)
100{
101#if 0
102 unsigned char i, j;
103
104 if(cbm_opendir(LFN, 8) != 0) {
105 show_statustext("Cannot open directory");
106 } else {
107 i = 0;
108 while(cbm_readdir(LFN, &dirent) == 0) {
109 if(strcmp(&dirent.name[strlen(dirent.name) - 4], ".dsc") == 0) {
110 strncpy(filenames[i], dirent.name, 16);
111 ++i;
112 if(i == MAX_NUMFILES) {
113 break;
114 }
115 }
116 }
117 cbm_closedir(LFN);
118
119 numfiles = i;
120
121 j = 0;
122 for(i = 0; i < numfiles; ++i) {
123 dscs[j] = LOADER_LOAD_DSC(filenames[i]);
124 if(dscs[j] != NULL) {
125 ++j;
126 }
127 }
128 show_statustext("Directory loaded");
129 }
130#endif
131}
132/*-----------------------------------------------------------------------------------*/
133static void
134makewindow(unsigned char i)
135{
136 unsigned char x, y;
137
138 ctk_window_clear(&window);
139 CTK_WIDGET_SET_YPOS(&description, height - 3);
140 CTK_WIDGET_SET_WIDTH(&description, width);
141 CTK_WIDGET_ADD(&window, &description);
142
143 morestart = i;
144
145 x = 0; y = 1;
146 for(; dscs[i] != NULL; ++i) {
147
148 if(x + strlen(dscs[i]->icon->title) >= width) {
149 y += 5;
150 x = 0;
151 if(y >= height - 2 - 4) {
152 morestart = i;
153 break;
154 }
155 }
156 CTK_WIDGET_SET_XPOS(dscs[i]->icon, x);
157 CTK_WIDGET_SET_YPOS(dscs[i]->icon, y);
158 CTK_WIDGET_ADD(&window, dscs[i]->icon);
159
160 x += strlen(dscs[i]->icon->title) + 1;
161 }
162 CTK_WIDGET_SET_YPOS(&autoexitbutton, height - 2);
163 CTK_WIDGET_ADD(&window, &autoexitbutton);
164 CTK_WIDGET_SET_YPOS(&autoexitlabel, height - 2);
165 CTK_WIDGET_ADD(&window, &autoexitlabel);
166
167 if(i != morestart) {
168 CTK_WIDGET_SET_YPOS(&backbutton, height - 1);
169 CTK_WIDGET_ADD(&window, &backbutton);
170 } else {
171 CTK_WIDGET_SET_YPOS(&morebutton, height - 1);
172 CTK_WIDGET_ADD(&window, &morebutton);
173 }
174 CTK_WIDGET_SET_XPOS(&reloadbutton, width - 8);
175 CTK_WIDGET_SET_YPOS(&reloadbutton, height - 1);
176 CTK_WIDGET_ADD(&window, &reloadbutton);
177}
178/*-----------------------------------------------------------------------------------*/
179LOADER_INIT_FUNC(directory_init, arg)
180{
181 arg_free(arg);
182
183 if(id == EK_ID_NONE) {
184 id = dispatcher_start(&p);
185
186 width = ctk_draw_width() - 4;
187 height = ctk_draw_height() - 4;
188
189 ctk_window_new(&window, width, height, "Directory");
190
191 loaddirectory();
192 makewindow(0);
193
194 dispatcher_listen(ctk_signal_widget_activate);
195 dispatcher_listen(ctk_signal_widget_select);
196 dispatcher_listen(ctk_signal_window_close);
197 }
198 ctk_window_open(&window);
199}
200/*-----------------------------------------------------------------------------------*/
201static void
202quit(void)
203{
204 unsigned char i;
205
206 ctk_window_close(&window);
207 for(i = 0; dscs[i] != NULL; ++i) {
208 LOADER_UNLOAD_DSC(dscs[i]);
209 }
210 dispatcher_exit(&p);
211 id = EK_ID_NONE;
212 LOADER_UNLOAD();
213}
214/*-----------------------------------------------------------------------------------*/
215static
216DISPATCHER_SIGHANDLER(directory_sighandler, s, data)
217{
218 unsigned char i;
219 DISPATCHER_SIGHANDLER_ARGS(s, data);
220
221 if(s == ctk_signal_widget_activate) {
222 if(data == (ek_data_t)&reloadbutton) {
223 for(i = 0; dscs[i] != NULL; ++i) {
224 LOADER_UNLOAD_DSC(dscs[i]);
225 }
226 loaddirectory();
227 makewindow(0);
228 ctk_window_open(&window);
229 } else if(data == (ek_data_t)&morebutton) {
230 makewindow(morestart);
231 ctk_window_open(&window);
232 } else if(data == (ek_data_t)&backbutton) {
233 makewindow(0);
234 ctk_window_open(&window);
235 } else if(data == (ek_data_t)&autoexitbutton) {
236 autoexit = 1 - autoexit;
237 if(autoexit == 1) {
238 ctk_label_set_text(&autoexitlabel, autoexiton);
239 } else {
240 ctk_label_set_text(&autoexitlabel, autoexitoff);
241 }
242 CTK_WIDGET_REDRAW(&autoexitlabel);
243 } else {
244 for(i = 0; dscs[i] != NULL; ++i) {
245 if(data == (ek_data_t)(dscs[i]->icon)) {
246 program_handler_load(dscs[i]->prgname);
247 if(autoexit) {
248 ctk_window_close(&window);
249 quit();
250 }
251 break;
252 }
253 }
254 }
255 } else if(s == ctk_signal_widget_select) {
256 if(data == (ek_data_t)&reloadbutton) {
257 show_statustext("Reload directory");
258 } else if(data == (ek_data_t)&morebutton) {
259 show_statustext("Show more files");
260 } else if(data == (ek_data_t)&backbutton) {
261 show_statustext("Show first files");
262 } else if(data == (ek_data_t)&autoexitbutton) {
263 show_statustext("Exit when loading program");
264 } else {
265 for(i = 0; dscs[i] != NULL; ++i) {
266 if(data == (ek_data_t)(dscs[i]->icon)) {
267 show_statustext(dscs[i]->description);
268 break;
269 }
270 }
271 }
272 } else if(s == ctk_signal_window_close &&
273 data == (ek_data_t)&window) {
274 quit();
275 } else if(s == dispatcher_signal_quit) {
276 ctk_window_close(&window);
277 quit();
278 }
279}
280/*-----------------------------------------------------------------------------------*/