Contiki 3.x
process-list.c
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.
14  * 3. The name of the author may not be used to endorse or promote
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  *
32  *
33  */
34 
35 #include "ctk/ctk.h"
36 #include "contiki.h"
37 
38 #include <string.h>
39 
40 #ifdef PROCESSLIST_CONF_HEIGHT
41 #define PROCESSLIST_HEIGHT PROCESSLIST_CONF_HEIGHT
42 #else /* PROCESSLIST_CONF_HEIGHT */
43 #define PROCESSLIST_HEIGHT 16
44 #endif /* PROCESSLIST_CONF_HEIGHT */
45 
46 #define MAX_PROCESSLABELS 13
47 
48 static struct ctk_window processwindow;
49 static struct {struct process *p; char id[2];} processes[MAX_PROCESSLABELS];
50 static struct ctk_label processidlabels[MAX_PROCESSLABELS];
51 static struct ctk_label processnamelabels[MAX_PROCESSLABELS];
52 
53 static struct ctk_label killlabel =
54  {CTK_LABEL(0, PROCESSLIST_HEIGHT - 2, 12, 1, "Kill process")};
55 static char killprocnum[3];
56 static struct ctk_textentry killtextentry =
57  {CTK_TEXTENTRY(13, PROCESSLIST_HEIGHT - 2, 2, 1, killprocnum, 2)};
58 static struct ctk_button killbutton =
59  {CTK_BUTTON(19, PROCESSLIST_HEIGHT - 2, 2, "Ok")};
60 static struct ctk_button processupdatebutton =
61  {CTK_BUTTON(0, PROCESSLIST_HEIGHT - 1, 6, "Update")};
62 static struct ctk_button processclosebutton =
63  {CTK_BUTTON(19, PROCESSLIST_HEIGHT - 1, 5, "Close")};
64 
65 PROCESS(processes_process, "Process listing");
66 
67 AUTOSTART_PROCESSES(&processes_process);
68 
69 enum {
70  EVENT_UPDATE
71 };
72 
73 /*-----------------------------------------------------------------------------------*/
74 static void
75 update_processwindow(void)
76 {
77  unsigned char i;
78  struct process *p;
79  char *idptr;
80 
81  i = 0;
82  for(p = PROCESS_LIST(); p != NULL && i < MAX_PROCESSLABELS; p = p->next) {
83  processes[i].p = p;
84  idptr = processes[i].id;
85  idptr[0] = '0' + (i / 10) % 10;
86  idptr[1] = '0' + i % 10;
87  CTK_LABEL_NEW(&processidlabels[i],
88  1, i + 1, 2, 1, idptr);
89  CTK_WIDGET_ADD(&processwindow, &processidlabels[i]);
90 
91  CTK_LABEL_NEW(&processnamelabels[i],
92  4, i + 1, 22, 1, PROCESS_NAME_STRING(p));
93  CTK_WIDGET_ADD(&processwindow, &processnamelabels[i]);
94 
95  ++i;
96  }
97 
98  CTK_WIDGET_ADD(&processwindow, &killlabel);
99 
100  CTK_WIDGET_ADD(&processwindow, &killtextentry);
101  CTK_WIDGET_ADD(&processwindow, &killbutton);
102 
103  CTK_WIDGET_ADD(&processwindow, &processupdatebutton);
104  CTK_WIDGET_ADD(&processwindow, &processclosebutton);
105  CTK_WIDGET_FOCUS(&processwindow, &processupdatebutton);
106 
107 }
108 /*-----------------------------------------------------------------------------------*/
109 static void
110 processes_quit(void)
111 {
112  process_exit(&processes_process);
113  LOADER_UNLOAD();
114 }
115 /*-----------------------------------------------------------------------------------*/
116 static void
117 killproc(void)
118 {
119  unsigned char procnum, valid, i;
120  struct process *p;
121 
122  procnum = 0;
123  valid = 0;
124  for(i = 0; i < 2; ++i) {
125  if(killprocnum[i] >= '0' && killprocnum[i] <= '9') {
126  procnum = procnum * 10 + (killprocnum[i] - '0');
127  valid = 1;
128  }
129  }
130 
131  if(valid == 0) {
132  return;
133  }
134 
135  /* Make sure the process ID exists. */
136  for(p = PROCESS_LIST(); p != NULL; p = p->next) {
137  if(p == processes[procnum].p) {
138  break;
139  }
140  }
141 
142  if(p != NULL) {
143  process_post(p, PROCESS_EVENT_EXIT, NULL);
144  CTK_WIDGET_FOCUS(&processwindow, &processupdatebutton);
145  CTK_WIDGET_REDRAW(&killbutton);
146  CTK_WIDGET_REDRAW(&processupdatebutton);
147  }
148 
149  CTK_TEXTENTRY_CLEAR(&killtextentry);
150  CTK_WIDGET_REDRAW(&killtextentry);
151 }
152 /*-----------------------------------------------------------------------------------*/
153 PROCESS_THREAD(processes_process, ev, data)
154 {
155 
156  PROCESS_BEGIN();
157 
158  ctk_window_new(&processwindow, 26, PROCESSLIST_HEIGHT, "Processes");
159  update_processwindow();
160 
161  ctk_window_open(&processwindow);
162 
163  while(1) {
165  if(ev == EVENT_UPDATE) {
166  ctk_window_clear(&processwindow);
167  update_processwindow();
168  ctk_window_open(&processwindow);
169  } else if(ev == ctk_signal_button_activate) {
170  if(data == (process_data_t)&processupdatebutton) {
171  ctk_window_clear(&processwindow);
172  update_processwindow();
173  ctk_window_open(&processwindow);
174  } else if(data == (process_data_t)&processclosebutton) {
175  ctk_window_close(&processwindow);
176  processes_quit();
177  /* ctk_desktop_redraw(processwindow.desktop); */
178  } else if(data == (process_data_t)&killbutton) {
179  killproc();
180  }
181  } else if(ev == PROCESS_EVENT_EXIT ||
182  (ev == ctk_signal_window_close &&
183  data == (process_data_t)&processwindow)) {
184  ctk_window_close(&processwindow);
185  processes_quit();
186  }
187  }
188 
189  PROCESS_END();
190 }
191 /*-----------------------------------------------------------------------------------*/
#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
process_event_t ctk_signal_button_activate
Same as ctk_signal_widget_activate.
Definition: ctk.c:126
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
#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
#define CTK_TEXTENTRY(x, y, w, h, text, len)
Instantiating macro for the ctk_textentry widget.
Definition: ctk.h:275
#define NULL
The null pointer.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
#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
void ctk_window_clear(struct ctk_window *w)
Remove all widgets from a window.
Definition: ctk.c:485
#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
#define CTK_BUTTON(x, y, w, text)
Instantiating macro for the ctk_button widget.
Definition: ctk.h:140
Representation of a CTK window.
Definition: ctk.h:506
#define CTK_WIDGET_REDRAW(widg)
Add a widget to the redraw queue.
Definition: ctk.h:771
CTK header file.
void process_exit(struct process *p)
Cause a process to exit.
Definition: process.c:202