Contiki 3.x
gui-shell.c
1 /*
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 copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Contiki desktop OS.
30  *
31  *
32  */
33 
34 #include "program-handler.h"
35 #include "contiki.h"
36 
37 #include "shell.h"
38 
39 #include "ctk/ctk-textentry-cmdline.h"
40 
41 #include <string.h>
42 
43 #ifdef SHELL_GUI_CONF_XSIZE
44 #define SHELL_GUI_XSIZE SHELL_GUI_CONF_XSIZE
45 #else
46 #define SHELL_GUI_XSIZE 10
47 #endif
48 
49 #ifdef SHELL_GUI_CONF_YSIZE
50 #define SHELL_GUI_YSIZE SHELL_GUI_CONF_YSIZE
51 #else
52 #define SHELL_GUI_YSIZE 10
53 #endif
54 
55 static struct ctk_window window;
56 static char log[SHELL_GUI_XSIZE * SHELL_GUI_YSIZE];
57 static struct ctk_label loglabel =
58  {CTK_LABEL(0, 0, SHELL_GUI_XSIZE, SHELL_GUI_YSIZE, log)};
59 static char command[SHELL_GUI_XSIZE - 1];
60 static struct ctk_textentry commandentry =
61  {CTK_TEXTENTRY_INPUT(0, SHELL_GUI_YSIZE, SHELL_GUI_XSIZE - 2, 1, command,
62  SHELL_GUI_XSIZE - 2, ctk_textentry_cmdline_input)};
63 
64 PROCESS(shell_gui_process, "Command shell");
65 
66 AUTOSTART_PROCESSES(&shell_gui_process);
67 
68 /*-----------------------------------------------------------------------------------*/
69 void
70 shell_default_output(const char *str1, int len1, const char *str2, int len2)
71 {
72  static unsigned char i;
73 
74  for(i = 1; i < SHELL_GUI_YSIZE; ++i) {
75  memcpy(&log[(i - 1) * SHELL_GUI_XSIZE],
76  &log[i * SHELL_GUI_XSIZE], SHELL_GUI_XSIZE);
77  }
78 
79  if(str1[len1 - 1] == '\n') {
80  --len1;
81  }
82  if(str2[len2 - 1] == '\n') {
83  --len2;
84  }
85 
86  strncpy(&log[(SHELL_GUI_YSIZE - 1) * SHELL_GUI_XSIZE],
87  str1, SHELL_GUI_XSIZE);
88  if(len1 < SHELL_GUI_XSIZE) {
89  strncpy(&log[(SHELL_GUI_YSIZE - 1) * SHELL_GUI_XSIZE] + len1,
90  str2, SHELL_GUI_XSIZE - len1);
91  if(len1 + len2 < SHELL_GUI_XSIZE) {
92  log[(SHELL_GUI_YSIZE - 1) * SHELL_GUI_XSIZE + len1 + len2] = 0;
93  }
94  }
95 
96  CTK_WIDGET_REDRAW(&loglabel);
97 }
98 /*-----------------------------------------------------------------------------------*/
99 void
100 shell_prompt(char *str)
101 {
102 }
103 /*-----------------------------------------------------------------------------------*/
104 void
105 shell_exit(void)
106 {
107  ctk_window_close(&window);
108 }
109 /*-----------------------------------------------------------------------------------*/
110 PROCESS_THREAD(shell_gui_process, ev, data)
111 {
112  PROCESS_BEGIN();
113 
114  ctk_window_new(&window, SHELL_GUI_XSIZE,
115  SHELL_GUI_YSIZE + 1, "Command shell");
116  CTK_WIDGET_ADD(&window, &loglabel);
117  /* CTK_WIDGET_SET_FLAG(&loglabel, CTK_WIDGET_FLAG_MONOSPACE);*/
118  CTK_WIDGET_ADD(&window, &commandentry);
119  /* CTK_WIDGET_SET_FLAG(&commandentry, CTK_WIDGET_FLAG_MONOSPACE);*/
120  CTK_WIDGET_FOCUS(&window, &commandentry);
121 
122  shell_init();
123  shell_file_init();
124  shell_ps_init();
125  shell_run_init();
126  shell_text_init();
127  shell_time_init();
128  shell_wget_init();
129 
130  ctk_window_open(&window);
131 
132  while(1) {
134 
135  if(ev == ctk_signal_widget_activate &&
136  data == (process_data_t)&commandentry) {
137  int command_len = (int)strlen(command);
138  shell_default_output("> ", 2, command, command_len);
139  shell_input(command, command_len);
140  if(shell_gui_process.state) {
141  CTK_TEXTENTRY_CLEAR(&commandentry);
142  CTK_WIDGET_REDRAW(&commandentry);
143  }
144  } else if(ev == ctk_signal_window_close ||
145  ev == PROCESS_EVENT_EXIT) {
146  shell_quit();
147  ctk_window_close(&window);
148  process_exit(&shell_gui_process);
149  LOADER_UNLOAD();
150  }
151  }
152  PROCESS_END();
153 }
154 /*-----------------------------------------------------------------------------------*/
#define CTK_WIDGET_ADD(win, widg)
Add a widget to a window.
Definition: ctk.h:752
process_event_t ctk_signal_window_close
Emitted when a window is closed.
Definition: ctk.c:155
#define CTK_TEXTENTRY_CLEAR(e)
Clears a text entry widget and sets the cursor to the start of the text line.
Definition: ctk.h:231
void shell_prompt(char *str)
Print a prompt.
Definition: serial-shell.c:82
void ctk_window_open(CC_REGISTER_ARG struct ctk_window *w)
Open a window, or bring window to front if already open.
Definition: ctk.c:347
void shell_init(void)
Initialize the shell.
Definition: shell.c:501
#define CTK_WIDGET_FOCUS(win, widg)
Set focus to a widget.
Definition: ctk.h:763
void ctk_window_close(struct ctk_window *w)
Close a window if it is open.
Definition: ctk.c:400
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Main header file for the Contiki shell
void shell_input(char *commandline, int commandline_len)
Send a line of input to the shell.
Definition: shell.c:359
void shell_quit(void)
Quit the shell.
Definition: shell.c:576
#define CTK_LABEL(x, y, w, h, text)
Instantiating macro for the ctk_label widget.
Definition: ctk.h:171
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void ctk_window_new(struct ctk_window *window, unsigned char w, unsigned char h, char *title)
Create a new window.
Definition: ctk.c:732
#define LOADER_UNLOAD()
Unload a program from memory.
Definition: loader.h:104
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
process_event_t ctk_signal_widget_activate
Emitted when a widget is activated (pressed).
Definition: ctk.c:126
Representation of a CTK window.
Definition: ctk.h:506
void shell_default_output(const char *text1, int len1, const char *text2, int len2)
Print a line of output from the shell.
Definition: serial-shell.c:58
#define CTK_WIDGET_REDRAW(widg)
Add a widget to the redraw queue.
Definition: ctk.h:771
void shell_exit(void)
Request shell exit.
Definition: serial-shell.c:89
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202