blob: faa6eb42bfd3f529105faa2624ed8ac78f1e2007 [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.
adamdunkelscf09a872004-07-04 17:55:54 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkels70c4c342003-04-24 17:01:17 +000015 * 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 *
adamdunkels88de6602004-09-09 21:29:03 +000032 * $Id: memstat.c,v 1.5 2004/09/09 21:29:03 adamdunkels Exp $
adamdunkels70c4c342003-04-24 17:01:17 +000033 *
34 */
35
36#include <stdlib.h>
37
38#include "ctk.h"
adamdunkelscf09a872004-07-04 17:55:54 +000039#include "ek.h"
adamdunkels70c4c342003-04-24 17:01:17 +000040#include "loader.h"
41
42static struct ctk_window window;
43static struct ctk_label freemsg =
44 {CTK_LABEL(2, 0, 12, 1, "Free memory:")};
45static char freemem[6];
46static struct ctk_label freenum =
47 {CTK_LABEL(18, 0, 5, 1, freemem)};
48
49static struct ctk_label lblockmsg =
50 {CTK_LABEL(2, 2, 14, 1, "Largest block:")};
51static char lblock[6];
52static struct ctk_label lblocknum =
53 {CTK_LABEL(18, 2, 5, 1, lblock)};
54
55static struct ctk_button updatebutton =
56 {CTK_BUTTON(0, 4, 6, "Update")};
57static struct ctk_button closebutton =
58 {CTK_BUTTON(17, 4, 5, "Close")};
59
adamdunkelscf09a872004-07-04 17:55:54 +000060/*static DISPATCHER_SIGHANDLER(memstat_sighandler, s, data);
adamdunkels70c4c342003-04-24 17:01:17 +000061static struct dispatcher_proc p =
62 {DISPATCHER_PROC("Memory statistics", NULL, memstat_sighandler, NULL)};
adamdunkelscf09a872004-07-04 17:55:54 +000063 static ek_id_t id;*/
64EK_EVENTHANDLER(memstat_eventhandler, ev, data);
65EK_PROCESS(p, "Memory statistics", EK_PRIO_NORMAL,
66 memstat_eventhandler, NULL, NULL);
67static ek_id_t id = EK_ID_NONE;
adamdunkels70c4c342003-04-24 17:01:17 +000068
69/*-----------------------------------------------------------------------------------*/
70static void
71update(void)
72{
73 int mem;
74
75 mem = _heapmemavail();
76 freemem[0] = (mem/10000) % 10 + '0';
77 freemem[1] = (mem/1000) % 10 + '0';
78 freemem[2] = (mem/100) % 10 + '0';
79 freemem[3] = (mem/10) % 10 + '0';
80 freemem[4] = (mem) % 10 + '0';
81
82 mem = _heapmaxavail();
83 lblock[0] = (mem/10000) % 10 + '0';
84 lblock[1] = (mem/1000) % 10 + '0';
85 lblock[2] = (mem/100) % 10 + '0';
86 lblock[3] = (mem/10) % 10 + '0';
87 lblock[4] = (mem) % 10 + '0';
88
89}
90/*-----------------------------------------------------------------------------------*/
adamdunkels68f6f4b2003-08-24 22:41:55 +000091LOADER_INIT_FUNC(memstat_init, arg)
adamdunkels70c4c342003-04-24 17:01:17 +000092{
adamdunkels68f6f4b2003-08-24 22:41:55 +000093 arg_free(arg);
94
adamdunkels70c4c342003-04-24 17:01:17 +000095 if(id == EK_ID_NONE) {
adamdunkelscf09a872004-07-04 17:55:54 +000096 id = ek_start(&p);
adamdunkels70c4c342003-04-24 17:01:17 +000097
adamdunkelscf09a872004-07-04 17:55:54 +000098 } else {
99 ctk_window_open(&window);
100 }
101}
102/*-----------------------------------------------------------------------------------*/
103static void
104quit(void)
105{
106 ek_exit();
107 id = EK_ID_NONE;
108 LOADER_UNLOAD();
109}
110/*-----------------------------------------------------------------------------------*/
111EK_EVENTHANDLER(memstat_eventhandler, ev, data)
112{
113 EK_EVENTHANDLER_ARGS(ev, data);
114
115 if(ev == EK_EVENT_INIT) {
adamdunkels70c4c342003-04-24 17:01:17 +0000116 ctk_window_new(&window, 24, 5, "Memory stats");
adamdunkels88de6602004-09-09 21:29:03 +0000117 /* ctk_window_move(&window, 0, 1);*/
adamdunkels70c4c342003-04-24 17:01:17 +0000118
119 CTK_WIDGET_ADD(&window, &freemsg);
120 CTK_WIDGET_ADD(&window, &freenum);
121
122 CTK_WIDGET_ADD(&window, &lblockmsg);
123 CTK_WIDGET_ADD(&window, &lblocknum);
124
125 CTK_WIDGET_ADD(&window, &updatebutton);
126 CTK_WIDGET_ADD(&window, &closebutton);
127
128 CTK_WIDGET_FOCUS(&window, &updatebutton);
129
130 update();
131
adamdunkelscf09a872004-07-04 17:55:54 +0000132 ctk_window_open(&window);
133
134 } else if(ev == ctk_signal_button_activate) {
adamdunkels70c4c342003-04-24 17:01:17 +0000135 if(data == (ek_data_t)&updatebutton) {
136 update();
137 ctk_window_redraw(&window);
138 } else if(data == (ek_data_t)&closebutton) {
139 ctk_window_close(&window);
140 quit();
141 }
adamdunkelscf09a872004-07-04 17:55:54 +0000142 } else if((ev == ctk_signal_window_close &&
143 data == (ek_data_t)&window) ||
144 ev == EK_EVENT_REQUEST_EXIT) {
adamdunkels70c4c342003-04-24 17:01:17 +0000145 quit();
146 }
147}
148/*-----------------------------------------------------------------------------------*/