-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathprogram-handler.c
434 lines (394 loc) · 12.7 KB
/
program-handler.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/**
* \file
* The program handler, used for loading programs and starting the
* screensaver.
* \author Adam Dunkels <[email protected]>
*
* The Contiki program handler is responsible for the Contiki menu and
* the desktop icons, as well as for loading programs and displaying a
* dialog with a message telling which program that is loading.
*
* The program handler also is responsible for starting the
* screensaver when the CTK detects that it should be started.
*/
/*
* Copyright (c) 2003, Adam Dunkels.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This file is part of the Contiki desktop OS
*
* $Id: program-handler.c,v 1.10 2008/02/15 17:31:44 oliverschmidt Exp $
*
*/
#include <string.h>
#include <stdlib.h>
#include "contiki.h"
#include "ctk/ctk.h"
#include "ctk/ctk-draw.h"
#include "program-handler.h"
/* Menus */
static struct ctk_menu contikimenu = {NULL, "Contiki", 7, 0, 0};
#ifndef PROGRAM_HANDLER_CONF_MAX_NUMDSCS
#define MAX_NUMDSCS 10
#else /* PROGRAM_HANDLER_CONF_MAX_NUMDSCS */
#define MAX_NUMDSCS PROGRAM_HANDLER_CONF_MAX_NUMDSCS
#endif /* PROGRAM_HANDLER_CONF_MAX_NUMDSCS */
static struct dsc *contikidsc[MAX_NUMDSCS];
static unsigned char contikidsclast = 0;
#ifndef PROGRAM_HANDLER_CONF_QUIT_MENU
#define QUIT_MENU 0
#else /* PROGRAM_HANDLER_CONF_QUIT_MENU */
#define QUIT_MENU PROGRAM_HANDLER_CONF_QUIT_MENU
#endif /* PROGRAM_HANDLER_CONF_QUIT_MENU */
#if QUIT_MENU
static unsigned char quitmenuitem;
/* "Quit" dialog */
static struct ctk_window quitdialog;
static struct ctk_label quitdialoglabel =
{CTK_LABEL(2, 1, 20, 1, "Really quit Contiki?")};
static struct ctk_button quityesbutton =
{CTK_BUTTON(4, 3, 3, "Yes")};
static struct ctk_button quitnobutton =
{CTK_BUTTON(16, 3, 2, "No")};
#endif /* QUIT_MENU */
#if WITH_LOADER_ARCH
/* "Run..." window */
static struct ctk_window runwindow;
static unsigned char runmenuitem;
static struct ctk_label namelabel =
{CTK_LABEL(0, 0, 13, 1, "Program name:")};
static char name[31];
static struct ctk_textentry nameentry =
{CTK_TEXTENTRY(0, 1, 14, 1, name, 30)};
static struct ctk_button loadbutton =
{CTK_BUTTON(10, 2, 4, "Load")};
static struct ctk_window loadingdialog;
static struct ctk_label loadingmsg =
{CTK_LABEL(0, 0, 8, 1, "Starting")};
static struct ctk_label loadingname =
{CTK_LABEL(9, 0, 16, 1, name)};
static struct ctk_window errordialog;
static struct ctk_label errormsg =
{CTK_LABEL(0, 1, 22, 1, "Error loading program:")};
static char errorfilename[22];
static struct ctk_label errorfilelabel =
{CTK_LABEL(0, 3, 22, 1, errorfilename)};
static struct ctk_label errortype =
{CTK_LABEL(4, 5, 16, 1, "")};
static struct ctk_button errorokbutton =
{CTK_BUTTON(9, 7, 2, "Ok")};
static const char * const errormsgs[] = {
"Ok",
"Read error",
"Header error",
"OS error",
"Data format error",
"Out of memory",
"File not found",
"No loader"
};
#endif /* WITH_LOADER_ARCH */
#define LOADER_EVENT_LOAD 1
#define LOADER_EVENT_DISPLAY_NAME 2
PROCESS(program_handler_process, "Program handler");
AUTOSTART_PROCESSES(&program_handler_process);
static char *displayname;
#if CTK_CONF_SCREENSAVER
char program_handler_screensaver[20];
#endif /* CTK_CONF_SCREENSAVER */
/*-----------------------------------------------------------------------------------*/
/**
* Add a program to the program handler.
*
* \param dsc The DSC description structure for the program to be added.
*
* \param menuname The name that the program should have in the
* Contiki menu.
*
* \param desktop Flag which specifies if the program should show up
* as an icon on the desktop or not.
*/
/*-----------------------------------------------------------------------------------*/
void
program_handler_add(struct dsc *dsc, char *menuname,
unsigned char desktop)
{
contikidsc[contikidsclast++] = dsc;
ctk_menuitem_add(&contikimenu, menuname);
if(desktop) {
#if CTK_CONF_ICONS
CTK_ICON_ADD(dsc->icon, &program_handler_process);
#endif /* CTK_CONF_ICONS */
}
}
/*-----------------------------------------------------------------------------------*/
/**
* Initializes the program handler.
*
* Is called by the initialization before any programs have been added
* with program_handler_add().
*
*/
/*-----------------------------------------------------------------------------------*/
#ifdef WITH_LOADER_ARCH
#define NUM_PNARGS 6
#define NAMELEN 32
struct pnarg {
char name[NAMELEN];
char *arg;
};
static struct pnarg pnargs[NUM_PNARGS];
static struct pnarg *
pnarg_copy(char *name, char *arg)
{
char i;
struct pnarg *pnargsptr;
pnargsptr = pnargs;
/* Allocate a place in the loadernames table. */
for(i = 0; i < NUM_PNARGS; ++i) {
if(*(pnargsptr->name) == 0) {
strncpy(pnargsptr->name, name, NAMELEN);
pnargsptr->arg = arg;
return pnargsptr;
}
++pnargsptr;
}
return NULL;
}
static void
pnarg_free(struct pnarg *pn)
{
*(pn->name) = 0;
}
#endif /* WITH_LOADER_ARCH */
/*-----------------------------------------------------------------------------------*/
/**
* Loads a program and displays a dialog telling the user about it.
*
* \param name The name of the program to be loaded.
*
* \param arg An argument which is passed to the new process when it
* is loaded.
*/
/*-----------------------------------------------------------------------------------*/
void
program_handler_load(char *name, char *arg)
{
#ifdef WITH_LOADER_ARCH
struct pnarg *pnarg;
pnarg = pnarg_copy(name, arg);
if(pnarg != NULL) {
process_post(&program_handler_process, LOADER_EVENT_DISPLAY_NAME, pnarg);
} else {
ctk_label_set_text(&errortype, "Out of memory");
ctk_dialog_open(&errordialog);
}
/* ctk_redraw(); */
/* ctk_window_redraw(&loadingdialog);*/
#endif /* WITH_LOADER_ARCH */
}
#ifdef WITH_LOADER_ARCH
#define RUN(prg, name, arg) program_handler_load(prg, arg)
#else /* WITH_LOADER_ARCH */
#define RUN(prg, process, arg) process_start(process, arg)
#endif /* WITH_LOADER_ARCH */
/*-----------------------------------------------------------------------------------*/
/**
* Configures the name of the screensaver to be loaded when
* appropriate.
*
* \param name The name of the screensaver or NULL if no screensaver
* should be used.
*/
/*-----------------------------------------------------------------------------------*/
#if CTK_CONF_SCREENSAVER
void
program_handler_setscreensaver(char *name)
{
if(name == NULL) {
program_handler_screensaver[0] = 0;
} else {
strncpy(program_handler_screensaver, name, sizeof(program_handler_screensaver));
}
}
#endif /* CTK_CONF_SCREENSAVER */
/*-----------------------------------------------------------------------------------*/
#ifdef WITH_LOADER_ARCH
static void
make_windows(void)
{
ctk_window_new(&runwindow, 16, 3, "Run");
CTK_WIDGET_ADD(&runwindow, &namelabel);
CTK_WIDGET_ADD(&runwindow, &nameentry);
CTK_WIDGET_ADD(&runwindow, &loadbutton);
CTK_WIDGET_FOCUS(&runwindow, &nameentry);
ctk_dialog_new(&loadingdialog, 25, 1);
CTK_WIDGET_ADD(&loadingdialog, &loadingmsg);
CTK_WIDGET_ADD(&loadingdialog, &loadingname);
ctk_dialog_new(&errordialog, 22, 8);
CTK_WIDGET_ADD(&errordialog, &errormsg);
CTK_WIDGET_ADD(&errordialog, &errorfilelabel);
CTK_WIDGET_ADD(&errordialog, &errortype);
CTK_WIDGET_ADD(&errordialog, &errorokbutton);
CTK_WIDGET_FOCUS(&errordialog, &errorokbutton);
}
#endif /* WITH_LOADER_ARCH */
/*-----------------------------------------------------------------------------------*/
PROCESS_THREAD(program_handler_process, ev, data)
{
#ifdef WITH_LOADER_ARCH
unsigned char err;
struct dsc *dsc;
#endif /* WITH_LOADER_ARCH */
unsigned char i;
struct dsc **dscp;
PROCESS_BEGIN();
/* Create the menus */
ctk_menu_add(&contikimenu);
#if WITH_LOADER_ARCH
runmenuitem = ctk_menuitem_add(&contikimenu, "Run program...");
make_windows();
#endif /* WITH_LOADER_ARCH */
#if QUIT_MENU
quitmenuitem = ctk_menuitem_add(&contikimenu, "Quit");
#endif /* QUIT_MENU */
displayname = NULL;
#if CTK_CONF_SCREENSAVER
program_handler_screensaver[0] = 0;
#endif /* CTK_CONF_SCREENSAVER */
while(1) {
PROCESS_WAIT_EVENT();
if(ev == ctk_signal_button_activate) {
#ifdef WITH_LOADER_ARCH
if(data == (process_data_t)&loadbutton) {
ctk_window_close(&runwindow);
program_handler_load(name, NULL);
} else if(data == (process_data_t)&errorokbutton) {
ctk_dialog_close();
}
#endif /* WITH_LOADER_ARCH */
#if QUIT_MENU
if(data == (process_data_t)&quityesbutton) {
ctk_draw_init();
exit(EXIT_SUCCESS);
} else if(data == (process_data_t)&quitnobutton) {
ctk_dialog_close();
}
#endif /* QUIT_MENU */
dscp = &contikidsc[0];
for(i = 0; i < CTK_MAXMENUITEMS; ++i) {
if(*dscp != NULL
#if CTK_CONF_ICONS
&& data == (process_data_t)(*dscp)->icon
#endif /* CTK_CONF_ICONS */
) {
RUN((*dscp)->prgname, (*dscp)->process, NULL);
break;
}
++dscp;
}
} else if(ev == ctk_signal_menu_activate) {
if((struct ctk_menu *)data == &contikimenu) {
#if WITH_LOADER_ARCH
dsc = contikidsc[contikimenu.active];
if(dsc != NULL) {
RUN(dsc->prgname, dsc->process, NULL);
} else if(contikimenu.active == runmenuitem) {
make_windows();
ctk_window_close(&runwindow);
ctk_window_open(&runwindow);
CTK_WIDGET_FOCUS(&runwindow, &nameentry);
}
#else /* WITH_LOADER_ARCH */
if(contikidsc[contikimenu.active] != NULL) {
RUN(contikidsc[contikimenu.active]->prgname,
contikidsc[contikimenu.active]->process,
NULL);
}
#endif /* WITH_LOADER_ARCH */
#if QUIT_MENU
if(contikimenu.active == quitmenuitem) {
ctk_dialog_new(&quitdialog, 24, 5);
CTK_WIDGET_ADD(&quitdialog, &quitdialoglabel);
CTK_WIDGET_ADD(&quitdialog, &quityesbutton);
CTK_WIDGET_ADD(&quitdialog, &quitnobutton);
CTK_WIDGET_FOCUS(&quitdialog, &quitnobutton);
ctk_dialog_open(&quitdialog);
}
#endif /* QUIT_MENU */
}
#if CTK_CONF_SCREENSAVER
} else if(ev == ctk_signal_screensaver_start) {
#if WITH_LOADER_ARCH
if(program_handler_screensaver[0] != 0) {
program_handler_load(program_handler_screensaver, NULL);
}
#endif /* WITH_LOADER_ARCH */
#endif /* CTK_CONF_SCREENSAVER */
} else if(ev == LOADER_EVENT_DISPLAY_NAME) {
#if WITH_LOADER_ARCH
if(displayname == NULL) {
make_windows();
ctk_label_set_text(&loadingname, ((struct pnarg *)data)->name);
ctk_dialog_open(&loadingdialog);
process_post(&program_handler_process, LOADER_EVENT_LOAD, data);
displayname = data;
} else {
/* Try again. */
process_post(&program_handler_process, LOADER_EVENT_DISPLAY_NAME, data);
}
#endif /* WITH_LOADER_ARCH */
} else if(ev == LOADER_EVENT_LOAD) {
#if WITH_LOADER_ARCH
if(displayname == data) {
ctk_dialog_close();
displayname = NULL;
log_message("Loading ", ((struct pnarg *)data)->name);
err = LOADER_LOAD(((struct pnarg *)data)->name,
((struct pnarg *)data)->arg);
if(err != LOADER_OK) {
make_windows();
errorfilename[0] = '"';
strncpy(errorfilename + 1, ((struct pnarg *)data)->name,
sizeof(errorfilename) - 2);
errorfilename[1 + strlen(((struct pnarg *)data)->name)] = '"';
ctk_label_set_text(&errortype, (char *)errormsgs[err]);
ctk_dialog_open(&errordialog);
log_message((char *)errormsgs[err], errorfilename);
}
pnarg_free(data);
} else {
/* Try again. */
process_post(&program_handler_process, LOADER_EVENT_DISPLAY_NAME, data);
}
#endif /* WITH_LOADEER_ARCH */
}
}
PROCESS_END();
}
/*-----------------------------------------------------------------------------------*/