blob: e64e0c68ee091efd605357afa13e485e972c9dca [file] [log] [blame]
kthacker62e146c2006-04-17 15:11:35 +00001/*
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 * $Id: memstat.c,v 1.1 2006/04/17 15:18:19 kthacker Exp $
33 *
34 */
35
36#include <stdlib.h>
37
38#include "ctk.h"
39#include "ek.h"
40#include "loader.h"
41
PulkoMandy832c5212014-07-01 19:15:21 +020042extern unsigned int _heapmemavail();
43extern unsigned int _heapmaxavail;
44
kthacker62e146c2006-04-17 15:11:35 +000045static struct ctk_window window;
46static struct ctk_label freemsg =
47 {CTK_LABEL(2, 0, 12, 1, "Free memory:")};
48static char freemem[6];
49static struct ctk_label freenum =
50 {CTK_LABEL(18, 0, 5, 1, freemem)};
51
52static struct ctk_label lblockmsg =
PulkoMandy832c5212014-07-01 19:15:21 +020053 {CTK_LABEL(2, 2, 14, 1, "Heap size:")};
kthacker62e146c2006-04-17 15:11:35 +000054static char lblock[6];
55static struct ctk_label lblocknum =
56 {CTK_LABEL(18, 2, 5, 1, lblock)};
57
58const static struct ctk_button updatebutton =
59 {CTK_BUTTON(0, 4, 6, "Update")};
60const static struct ctk_button closebutton =
61 {CTK_BUTTON(17, 4, 5, "Close")};
62
63/*static DISPATCHER_SIGHANDLER(memstat_sighandler, s, data);
64static struct dispatcher_proc p =
65 {DISPATCHER_PROC("Memory statistics", NULL, memstat_sighandler, NULL)};
66 static ek_id_t id;*/
67EK_EVENTHANDLER(memstat_eventhandler, ev, data);
68EK_PROCESS(p, "Memory statistics", EK_PRIO_NORMAL,
69 memstat_eventhandler, NULL, NULL);
70static ek_id_t id = EK_ID_NONE;
71
PulkoMandy832c5212014-07-01 19:15:21 +020072
kthacker62e146c2006-04-17 15:11:35 +000073/*-----------------------------------------------------------------------------------*/
74static void
75update(void)
76{
PulkoMandy832c5212014-07-01 19:15:21 +020077 unsigned int mem;
kthacker62e146c2006-04-17 15:11:35 +000078
79 mem = _heapmemavail();
80 freemem[0] = (mem/10000) % 10 + '0';
81 freemem[1] = (mem/1000) % 10 + '0';
82 freemem[2] = (mem/100) % 10 + '0';
83 freemem[3] = (mem/10) % 10 + '0';
84 freemem[4] = (mem) % 10 + '0';
85
PulkoMandy832c5212014-07-01 19:15:21 +020086 mem = _heapmaxavail;
kthacker62e146c2006-04-17 15:11:35 +000087 lblock[0] = (mem/10000) % 10 + '0';
88 lblock[1] = (mem/1000) % 10 + '0';
89 lblock[2] = (mem/100) % 10 + '0';
90 lblock[3] = (mem/10) % 10 + '0';
91 lblock[4] = (mem) % 10 + '0';
92
93}
94/*-----------------------------------------------------------------------------------*/
95LOADER_INIT_FUNC(memstat_init, arg)
96{
97 arg_free(arg);
98
99 if(id == EK_ID_NONE) {
100 id = ek_start(&p);
101
102 } else {
103 ctk_window_open(&window);
104 }
105}
106/*-----------------------------------------------------------------------------------*/
107static void
108quit(void)
109{
110 ek_exit();
111 id = EK_ID_NONE;
112 LOADER_UNLOAD();
113}
114/*-----------------------------------------------------------------------------------*/
115EK_EVENTHANDLER(memstat_eventhandler, ev, data)
116{
117 EK_EVENTHANDLER_ARGS(ev, data);
118
119 if(ev == EK_EVENT_INIT) {
120 ctk_window_new(&window, 24, 5, "Memory stats");
121 /* ctk_window_move(&window, 0, 1);*/
122
123 CTK_WIDGET_ADD(&window, &freemsg);
124 CTK_WIDGET_ADD(&window, &freenum);
125
126 CTK_WIDGET_ADD(&window, &lblockmsg);
127 CTK_WIDGET_ADD(&window, &lblocknum);
128
129 CTK_WIDGET_ADD(&window, &updatebutton);
130 CTK_WIDGET_ADD(&window, &closebutton);
131
132 CTK_WIDGET_FOCUS(&window, &updatebutton);
133
134 update();
135
136 ctk_window_open(&window);
137
138 } else if(ev == ctk_signal_button_activate) {
139 if(data == (ek_data_t)&updatebutton) {
140 update();
141 ctk_window_redraw(&window);
142 } else if(data == (ek_data_t)&closebutton) {
143 ctk_window_close(&window);
144 quit();
145 }
146 } else if((ev == ctk_signal_window_close &&
147 data == (ek_data_t)&window) ||
148 ev == EK_EVENT_REQUEST_EXIT) {
149 quit();
150 }
151}
152/*-----------------------------------------------------------------------------------*/