adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, 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. |
adamdunkels | 06f897e | 2004-06-06 05:59:20 +0000 | [diff] [blame] | 14 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 15 | * products derived from this software without specific prior |
| 16 | * written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 19 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 24 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * This file is part of the Contiki desktop environment |
| 31 | * |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 32 | * $Id: processes.c,v 1.14 2005/03/13 22:14:43 oliverschmidt Exp $ |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 36 | #include "ek.h" |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 37 | #include "ctk.h" |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 38 | #include "loader.h" |
| 39 | |
adamdunkels | 56dc363 | 2004-09-09 21:10:16 +0000 | [diff] [blame] | 40 | #include <string.h> |
| 41 | |
adamdunkels | 8bb556e | 2003-04-09 19:25:37 +0000 | [diff] [blame] | 42 | #define MAX_PROCESSLABELS 13 |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 43 | static struct ctk_window processwindow; |
adamdunkels | 8bb556e | 2003-04-09 19:25:37 +0000 | [diff] [blame] | 44 | static unsigned char ids[MAX_PROCESSLABELS][4]; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 45 | static struct ctk_label processidlabels[MAX_PROCESSLABELS]; |
| 46 | static struct ctk_label processnamelabels[MAX_PROCESSLABELS]; |
| 47 | |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 48 | static struct ctk_label killlabel = |
| 49 | {CTK_LABEL(0, 14, 12, 1, "Kill process")}; |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 50 | static char killprocnum[4]; |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 51 | static struct ctk_textentry killtextentry = |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 52 | {CTK_TEXTENTRY(13, 14, 3, 1, killprocnum, 3)}; |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 53 | static struct ctk_button killbutton = |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 54 | {CTK_BUTTON(19, 14, 2, "Ok")}; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 55 | static struct ctk_button processupdatebutton = |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 56 | {CTK_BUTTON(0, 15, 6, "Update")}; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 57 | static struct ctk_button processclosebutton = |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 58 | {CTK_BUTTON(19, 15, 5, "Close")}; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 59 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 60 | /*static DISPATCHER_SIGHANDLER(processes_sighandler, s, data); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 61 | static struct dispatcher_proc p = |
| 62 | {DISPATCHER_PROC("Process listing", NULL, processes_sighandler, NULL)}; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 63 | static ek_id_t id;*/ |
| 64 | EK_EVENTHANDLER(processes_eventhandler, ev, data); |
| 65 | EK_PROCESS(p, "Process listing", EK_PRIO_NORMAL, |
| 66 | processes_eventhandler, NULL, NULL); |
| 67 | static ek_id_t id = EK_ID_NONE; |
| 68 | |
| 69 | enum { |
| 70 | EVENT_UPDATE |
| 71 | }; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 72 | |
| 73 | /*-----------------------------------------------------------------------------------*/ |
| 74 | static void |
| 75 | update_processwindow(void) |
| 76 | { |
| 77 | unsigned char i, j, *idsptr; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 78 | struct ek_proc *p; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 79 | |
adamdunkels | b1a3d23 | 2003-06-30 20:48:10 +0000 | [diff] [blame] | 80 | /* Step through each possible process ID and see if there is a |
| 81 | matching process. */ |
| 82 | j = 0; |
adamdunkels | b0da0e0 | 2004-08-20 21:31:53 +0000 | [diff] [blame] | 83 | for(p = EK_PROCS(); p != NULL && j < MAX_PROCESSLABELS; p = p->next) { |
adamdunkels | b1a3d23 | 2003-06-30 20:48:10 +0000 | [diff] [blame] | 84 | idsptr = ids[j]; |
| 85 | i = p->id; |
| 86 | idsptr[0] = '0' + i / 100; |
| 87 | if(idsptr[0] == '0') { |
| 88 | idsptr[0] = ' '; |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 89 | } |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 90 | idsptr[1] = '0' + (i / 10) % 10; |
adamdunkels | b1a3d23 | 2003-06-30 20:48:10 +0000 | [diff] [blame] | 91 | idsptr[2] = '0' + i % 10; |
| 92 | idsptr[3] = 0; |
| 93 | CTK_LABEL_NEW(&processidlabels[j], |
| 94 | 0, j + 1, 3, 1, idsptr); |
| 95 | CTK_WIDGET_ADD(&processwindow, &processidlabels[j]); |
| 96 | |
| 97 | CTK_LABEL_NEW(&processnamelabels[j], |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 98 | 4, j + 1, 22, 1, (char *)p->name); |
adamdunkels | b1a3d23 | 2003-06-30 20:48:10 +0000 | [diff] [blame] | 99 | CTK_WIDGET_ADD(&processwindow, &processnamelabels[j]); |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 100 | |
adamdunkels | b1a3d23 | 2003-06-30 20:48:10 +0000 | [diff] [blame] | 101 | ++j; |
| 102 | } |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 103 | |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 104 | CTK_WIDGET_ADD(&processwindow, &killlabel); |
| 105 | |
| 106 | CTK_WIDGET_ADD(&processwindow, &killtextentry); |
| 107 | CTK_WIDGET_ADD(&processwindow, &killbutton); |
| 108 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 109 | CTK_WIDGET_ADD(&processwindow, &processupdatebutton); |
| 110 | CTK_WIDGET_ADD(&processwindow, &processclosebutton); |
| 111 | CTK_WIDGET_FOCUS(&processwindow, &processupdatebutton); |
| 112 | } |
| 113 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 114 | LOADER_INIT_FUNC(processes_init, arg) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 115 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 116 | arg_free(arg); |
| 117 | |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 118 | if(id == EK_ID_NONE) { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 119 | id = ek_start(&p); |
| 120 | } |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 121 | } |
| 122 | /*-----------------------------------------------------------------------------------*/ |
| 123 | static void |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 124 | processes_quit(void) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 125 | { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 126 | ek_exit(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 127 | id = EK_ID_NONE; |
| 128 | LOADER_UNLOAD(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 129 | } |
| 130 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 131 | static void |
| 132 | killproc(void) |
| 133 | { |
| 134 | int procnum; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 135 | unsigned char i, j; |
| 136 | struct ek_proc *p; |
| 137 | |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 138 | /* Find first zero char in killprocnum string. */ |
| 139 | for(i = 0; killprocnum[i] != 0 && |
| 140 | i < sizeof(killprocnum); ++i); |
| 141 | |
| 142 | if(i == 0) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | procnum = 0; |
| 147 | |
| 148 | for(j = 0; j < i; ++j) { |
| 149 | procnum = procnum * 10 + (killprocnum[j] - '0'); |
| 150 | killprocnum[j] = 0; |
| 151 | } |
| 152 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 153 | /* Make sure the process ID exists. */ |
| 154 | for(p = EK_PROCS(); p != NULL; p = p->next) { |
| 155 | if(EK_PROC_ID(p) == procnum) { |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if(p != NULL) { |
| 161 | ek_post(procnum, EK_EVENT_REQUEST_EXIT, NULL); |
| 162 | ek_post(id, EVENT_UPDATE, NULL); |
adamdunkels | 856c68b | 2004-09-05 07:16:22 +0000 | [diff] [blame] | 163 | CTK_TEXTENTRY_CLEAR(&killtextentry); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 164 | CTK_WIDGET_REDRAW(&killtextentry); |
| 165 | CTK_WIDGET_FOCUS(&processwindow, &processupdatebutton); |
| 166 | CTK_WIDGET_REDRAW(&killbutton); |
| 167 | CTK_WIDGET_REDRAW(&processupdatebutton); |
| 168 | } |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 169 | } |
| 170 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 171 | /*static |
| 172 | DISPATCHER_SIGHANDLER(processes_sighandler, s, data)*/ |
| 173 | EK_EVENTHANDLER(processes_eventhandler, ev, data) |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 174 | { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 175 | /* DISPATCHER_SIGHANDLER_ARGS(s, data);*/ |
| 176 | EK_EVENTHANDLER_ARGS(ev, data); |
| 177 | |
| 178 | |
| 179 | if(ev == EK_EVENT_INIT) { |
oliverschmidt | 01e9d1e | 2005-03-13 22:14:43 +0000 | [diff] [blame] | 180 | ctk_window_new(&processwindow, 26, 16, "Processes"); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 181 | update_processwindow(); |
| 182 | |
| 183 | ctk_window_open(&processwindow); |
| 184 | } else if(ev == EVENT_UPDATE) { |
| 185 | ctk_window_clear(&processwindow); |
| 186 | update_processwindow(); |
| 187 | ctk_window_open(&processwindow); |
| 188 | } else if(ev == ctk_signal_button_activate) { |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 189 | if(data == (ek_data_t)&processupdatebutton) { |
| 190 | ctk_window_clear(&processwindow); |
| 191 | update_processwindow(); |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 192 | ctk_window_open(&processwindow); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 193 | } else if(data == (ek_data_t)&processclosebutton) { |
| 194 | ctk_window_close(&processwindow); |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 195 | processes_quit(); |
adamdunkels | b1a3d23 | 2003-06-30 20:48:10 +0000 | [diff] [blame] | 196 | /* ctk_desktop_redraw(processwindow.desktop); */ |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 197 | } else if(data == (ek_data_t)&killbutton) { |
| 198 | killproc(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 199 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 200 | } else if(ev == EK_EVENT_REQUEST_EXIT || |
| 201 | (ev == ctk_signal_window_close && |
adamdunkels | 1a798bc | 2003-07-31 23:45:52 +0000 | [diff] [blame] | 202 | data == (ek_data_t)&processwindow)) { |
| 203 | ctk_window_close(&processwindow); |
adamdunkels | 0137b44 | 2003-04-08 23:27:33 +0000 | [diff] [blame] | 204 | processes_quit(); |
adamdunkels | 4292c86 | 2003-04-08 17:56:43 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | /*-----------------------------------------------------------------------------------*/ |