adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 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. |
adamdunkels | 06f897e | 2004-06-06 05:59:20 +0000 | [diff] [blame] | 13 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 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 | * |
oliverschmidt | ed605d1 | 2005-05-05 23:32:01 +0000 | [diff] [blame] | 31 | * $Id: shell-gui.c,v 1.13 2005/05/05 23:32:01 oliverschmidt Exp $ |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 32 | * |
| 33 | */ |
| 34 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 35 | #include "ek.h" |
| 36 | |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 37 | #include "program-handler.h" |
| 38 | #include "loader.h" |
adamdunkels | 0983060 | 2003-09-01 22:22:36 +0000 | [diff] [blame] | 39 | |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 40 | #include "uip.h" |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 41 | #include "uip_arp.h" |
| 42 | #include "resolv.h" |
| 43 | |
| 44 | #include "shell.h" |
| 45 | |
oliverschmidt | ed605d1 | 2005-05-05 23:32:01 +0000 | [diff] [blame] | 46 | #include "ctk-textentry-cmdline.h" |
| 47 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 48 | #include "shell-gui-conf.h" |
| 49 | |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 50 | #include <string.h> |
| 51 | |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 52 | |
| 53 | static struct ctk_window window; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 54 | static char log[SHELL_GUI_CONF_XSIZE * SHELL_GUI_CONF_YSIZE]; |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 55 | static struct ctk_label loglabel = |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 56 | {CTK_LABEL(0, 0, SHELL_GUI_CONF_XSIZE, SHELL_GUI_CONF_YSIZE, log)}; |
oliverschmidt | d2f04de | 2005-04-28 20:56:15 +0000 | [diff] [blame] | 57 | static char command[SHELL_GUI_CONF_XSIZE - 1]; |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 58 | static struct ctk_textentry commandentry = |
oliverschmidt | ed605d1 | 2005-05-05 23:32:01 +0000 | [diff] [blame] | 59 | {CTK_TEXTENTRY_INPUT(0, SHELL_GUI_CONF_YSIZE, SHELL_GUI_CONF_XSIZE - 2, 1, command, |
PulkoMandy | eb45bf9 | 2014-06-26 13:28:43 +0200 | [diff] [blame] | 60 | SHELL_GUI_CONF_XSIZE - 2, NULL /*ctk_textentry_cmdline_input*/)}; |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 61 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 62 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 63 | EK_PROCESS(p, "Command shell", EK_PRIO_NORMAL, |
| 64 | eventhandler, NULL, NULL); |
| 65 | static ek_id_t id = EK_ID_NONE; |
| 66 | |
| 67 | /*static DISPATCHER_SIGHANDLER(sighandler, s, data); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 68 | static struct dispatcher_proc p = |
| 69 | {DISPATCHER_PROC("Command shell", shell_idle, sighandler, |
| 70 | NULL)}; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 71 | static ek_id_t id;*/ |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 72 | |
| 73 | /*-----------------------------------------------------------------------------------*/ |
| 74 | void |
| 75 | shell_quit(char *str) |
| 76 | { |
| 77 | ctk_window_close(&window); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 78 | ek_exit(); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 79 | id = EK_ID_NONE; |
| 80 | LOADER_UNLOAD(); |
| 81 | } |
| 82 | /*-----------------------------------------------------------------------------------*/ |
| 83 | void |
Adrien Destugues | 69a6e72 | 2017-06-03 10:17:17 +0200 | [diff] [blame^] | 84 | shell_output(const char *str1, const char *str2) |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 85 | { |
| 86 | static unsigned char i, len; |
| 87 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 88 | for(i = 1; i < SHELL_GUI_CONF_YSIZE; ++i) { |
| 89 | memcpy(&log[(i - 1) * SHELL_GUI_CONF_XSIZE], |
| 90 | &log[i * SHELL_GUI_CONF_XSIZE], SHELL_GUI_CONF_XSIZE); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 91 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 92 | memset(&log[(SHELL_GUI_CONF_YSIZE - 1) * SHELL_GUI_CONF_XSIZE], |
| 93 | 0, SHELL_GUI_CONF_XSIZE); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 94 | |
| 95 | len = strlen(str1); |
| 96 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 97 | strncpy(&log[(SHELL_GUI_CONF_YSIZE - 1) * SHELL_GUI_CONF_XSIZE], |
| 98 | str1, SHELL_GUI_CONF_XSIZE); |
| 99 | if(len < SHELL_GUI_CONF_XSIZE) { |
| 100 | strncpy(&log[(SHELL_GUI_CONF_YSIZE - 1) * SHELL_GUI_CONF_XSIZE] + len, |
| 101 | str2, SHELL_GUI_CONF_XSIZE - len); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | CTK_WIDGET_REDRAW(&loglabel); |
| 105 | } |
| 106 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 8e5d990 | 2003-10-14 11:23:04 +0000 | [diff] [blame] | 107 | void |
| 108 | shell_prompt(char *str) |
| 109 | { |
| 110 | |
| 111 | } |
| 112 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 113 | LOADER_INIT_FUNC(shell_gui_init, arg) |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 114 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 115 | arg_free(arg); |
| 116 | |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 117 | if(id == EK_ID_NONE) { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 118 | id = ek_start(&p); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 119 | } |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 120 | } |
| 121 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 122 | EK_EVENTHANDLER(eventhandler, ev, data) |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 123 | { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 124 | EK_EVENTHANDLER_ARGS(ev, data); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 125 | |
| 126 | if(ev == EK_EVENT_INIT) { |
PulkoMandy | eb45bf9 | 2014-06-26 13:28:43 +0200 | [diff] [blame] | 127 | commandentry.input = (void*)ctk_textentry_cmdline_input; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 128 | ctk_window_new(&window, SHELL_GUI_CONF_XSIZE, |
| 129 | SHELL_GUI_CONF_YSIZE + 1, "Command shell"); |
| 130 | CTK_WIDGET_ADD(&window, &loglabel); |
| 131 | /* CTK_WIDGET_SET_FLAG(&loglabel, CTK_WIDGET_FLAG_MONOSPACE);*/ |
| 132 | CTK_WIDGET_ADD(&window, &commandentry); |
| 133 | /* CTK_WIDGET_SET_FLAG(&commandentry, CTK_WIDGET_FLAG_MONOSPACE);*/ |
| 134 | CTK_WIDGET_FOCUS(&window, &commandentry); |
oliverschmidt | d2f04de | 2005-04-28 20:56:15 +0000 | [diff] [blame] | 135 | memset(log, 0, sizeof(log)); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 136 | |
| 137 | shell_init(); |
oliverschmidt | 02949e1 | 2005-04-24 13:38:35 +0000 | [diff] [blame] | 138 | ctk_window_open(&window); |
| 139 | shell_start(); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 140 | } else if(ev == ctk_signal_widget_activate && |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 141 | data == (ek_data_t)&commandentry) { |
adamdunkels | 8e5d990 | 2003-10-14 11:23:04 +0000 | [diff] [blame] | 142 | shell_output("> ", command); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 143 | shell_input(command); |
| 144 | CTK_TEXTENTRY_CLEAR(&commandentry); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 145 | CTK_WIDGET_REDRAW(&commandentry); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 146 | } else if(ev == ctk_signal_window_close || |
| 147 | ev == EK_EVENT_REQUEST_EXIT) { |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 148 | shell_quit(NULL); |
adamdunkels | 30c70da | 2004-08-20 21:37:39 +0000 | [diff] [blame] | 149 | } else { |
| 150 | shell_eventhandler(ev, data); |
adamdunkels | be17526 | 2003-08-21 22:24:29 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | /*-----------------------------------------------------------------------------------*/ |