kthacker | 62e146c | 2006-04-17 15:11:35 +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. |
| 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 | |
PulkoMandy | 832c521 | 2014-07-01 19:15:21 +0200 | [diff] [blame] | 42 | extern unsigned int _heapmemavail(); |
| 43 | extern unsigned int _heapmaxavail; |
| 44 | |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 45 | static struct ctk_window window; |
| 46 | static struct ctk_label freemsg = |
| 47 | {CTK_LABEL(2, 0, 12, 1, "Free memory:")}; |
| 48 | static char freemem[6]; |
| 49 | static struct ctk_label freenum = |
| 50 | {CTK_LABEL(18, 0, 5, 1, freemem)}; |
| 51 | |
| 52 | static struct ctk_label lblockmsg = |
PulkoMandy | 832c521 | 2014-07-01 19:15:21 +0200 | [diff] [blame] | 53 | {CTK_LABEL(2, 2, 14, 1, "Heap size:")}; |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 54 | static char lblock[6]; |
| 55 | static struct ctk_label lblocknum = |
| 56 | {CTK_LABEL(18, 2, 5, 1, lblock)}; |
| 57 | |
| 58 | const static struct ctk_button updatebutton = |
| 59 | {CTK_BUTTON(0, 4, 6, "Update")}; |
| 60 | const static struct ctk_button closebutton = |
| 61 | {CTK_BUTTON(17, 4, 5, "Close")}; |
| 62 | |
| 63 | /*static DISPATCHER_SIGHANDLER(memstat_sighandler, s, data); |
| 64 | static struct dispatcher_proc p = |
| 65 | {DISPATCHER_PROC("Memory statistics", NULL, memstat_sighandler, NULL)}; |
| 66 | static ek_id_t id;*/ |
| 67 | EK_EVENTHANDLER(memstat_eventhandler, ev, data); |
| 68 | EK_PROCESS(p, "Memory statistics", EK_PRIO_NORMAL, |
| 69 | memstat_eventhandler, NULL, NULL); |
| 70 | static ek_id_t id = EK_ID_NONE; |
| 71 | |
PulkoMandy | 832c521 | 2014-07-01 19:15:21 +0200 | [diff] [blame] | 72 | |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 73 | /*-----------------------------------------------------------------------------------*/ |
| 74 | static void |
| 75 | update(void) |
| 76 | { |
PulkoMandy | 832c521 | 2014-07-01 19:15:21 +0200 | [diff] [blame] | 77 | unsigned int mem; |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 78 | |
| 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 | |
PulkoMandy | 832c521 | 2014-07-01 19:15:21 +0200 | [diff] [blame] | 86 | mem = _heapmaxavail; |
kthacker | 62e146c | 2006-04-17 15:11:35 +0000 | [diff] [blame] | 87 | 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 | /*-----------------------------------------------------------------------------------*/ |
| 95 | LOADER_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 | /*-----------------------------------------------------------------------------------*/ |
| 107 | static void |
| 108 | quit(void) |
| 109 | { |
| 110 | ek_exit(); |
| 111 | id = EK_ID_NONE; |
| 112 | LOADER_UNLOAD(); |
| 113 | } |
| 114 | /*-----------------------------------------------------------------------------------*/ |
| 115 | EK_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 | /*-----------------------------------------------------------------------------------*/ |