blob: cfc6835f98d35ab0f1c1e77974fcfe39ca235da5 [file] [log] [blame]
adamdunkels70c4c342003-04-24 17:01:17 +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. All advertising materials mentioning features or use of this
15 * software must display the following acknowledgement:
16 * This product includes software developed by Adam Dunkels.
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * This file is part of the Contiki desktop environment
34 *
adamdunkels68f6f4b2003-08-24 22:41:55 +000035 * $Id: memstat.c,v 1.2 2003/08/24 22:41:55 adamdunkels Exp $
adamdunkels70c4c342003-04-24 17:01:17 +000036 *
37 */
38
39#include <stdlib.h>
40
41#include "ctk.h"
42#include "dispatcher.h"
43#include "loader.h"
44
45static 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 =
53 {CTK_LABEL(2, 2, 14, 1, "Largest block:")};
54static char lblock[6];
55static struct ctk_label lblocknum =
56 {CTK_LABEL(18, 2, 5, 1, lblock)};
57
58static struct ctk_button updatebutton =
59 {CTK_BUTTON(0, 4, 6, "Update")};
60static struct ctk_button closebutton =
61 {CTK_BUTTON(17, 4, 5, "Close")};
62
63static DISPATCHER_SIGHANDLER(memstat_sighandler, s, data);
64static struct dispatcher_proc p =
65 {DISPATCHER_PROC("Memory statistics", NULL, memstat_sighandler, NULL)};
66static ek_id_t id;
67
68/*-----------------------------------------------------------------------------------*/
69static void
70update(void)
71{
72 int mem;
73
74 mem = _heapmemavail();
75 freemem[0] = (mem/10000) % 10 + '0';
76 freemem[1] = (mem/1000) % 10 + '0';
77 freemem[2] = (mem/100) % 10 + '0';
78 freemem[3] = (mem/10) % 10 + '0';
79 freemem[4] = (mem) % 10 + '0';
80
81 mem = _heapmaxavail();
82 lblock[0] = (mem/10000) % 10 + '0';
83 lblock[1] = (mem/1000) % 10 + '0';
84 lblock[2] = (mem/100) % 10 + '0';
85 lblock[3] = (mem/10) % 10 + '0';
86 lblock[4] = (mem) % 10 + '0';
87
88}
89/*-----------------------------------------------------------------------------------*/
adamdunkels68f6f4b2003-08-24 22:41:55 +000090LOADER_INIT_FUNC(memstat_init, arg)
adamdunkels70c4c342003-04-24 17:01:17 +000091{
adamdunkels68f6f4b2003-08-24 22:41:55 +000092 arg_free(arg);
93
adamdunkels70c4c342003-04-24 17:01:17 +000094 if(id == EK_ID_NONE) {
95 id = dispatcher_start(&p);
96
97 ctk_window_new(&window, 24, 5, "Memory stats");
98 ctk_window_move(&window, 0, 1);
99
100 CTK_WIDGET_ADD(&window, &freemsg);
101 CTK_WIDGET_ADD(&window, &freenum);
102
103 CTK_WIDGET_ADD(&window, &lblockmsg);
104 CTK_WIDGET_ADD(&window, &lblocknum);
105
106 CTK_WIDGET_ADD(&window, &updatebutton);
107 CTK_WIDGET_ADD(&window, &closebutton);
108
109 CTK_WIDGET_FOCUS(&window, &updatebutton);
110
111 update();
112
113 dispatcher_listen(ctk_signal_button_activate);
114 dispatcher_listen(ctk_signal_window_close);
115 }
116 ctk_window_open(&window);
117}
118/*-----------------------------------------------------------------------------------*/
119static void
120quit(void)
121{
122 dispatcher_exit(&p);
123 id = EK_ID_NONE;
124 LOADER_UNLOAD();
125 ctk_redraw();
126}
127/*-----------------------------------------------------------------------------------*/
128static
129DISPATCHER_SIGHANDLER(memstat_sighandler, s, data)
130{
131 DISPATCHER_SIGHANDLER_ARGS(s, data);
132
133 if(s == ctk_signal_button_activate) {
134 if(data == (ek_data_t)&updatebutton) {
135 update();
136 ctk_window_redraw(&window);
137 } else if(data == (ek_data_t)&closebutton) {
138 ctk_window_close(&window);
139 quit();
140 }
141 } else if(s == ctk_signal_window_close &&
142 data == (ek_data_t)&window) {
143 quit();
144 }
145}
146/*-----------------------------------------------------------------------------------*/