adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003, 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 an example program for the Contiki desktop OS |
| 34 | * |
adamdunkels | e9900d8 | 2003-04-10 07:02:05 +0000 | [diff] [blame] | 35 | * $Id: hello.c,v 1.3 2003/04/10 07:02:05 adamdunkels Exp $ |
adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 36 | * |
| 37 | */ |
| 38 | |
adamdunkels | caebe7d | 2003-04-09 19:15:25 +0000 | [diff] [blame] | 39 | /* This is an example of how to write programs for Contiki. It |
| 40 | displays a window with the famous message "Hello world" and two |
| 41 | buttons; one which changes the message slightly and one which quits |
| 42 | the application. */ |
adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 43 | |
| 44 | #include "ctk.h" |
| 45 | #include "dispatcher.h" |
| 46 | #include "loader.h" |
| 47 | |
| 48 | static struct ctk_window window; |
| 49 | |
| 50 | static struct ctk_label hellolabel = |
| 51 | {CTK_LABEL(0, 0, 8, 1, "Hello")}; |
| 52 | static struct ctk_label worldlabel = |
| 53 | {CTK_LABEL(8, 1, 6, 1, "world!")}; |
| 54 | |
| 55 | static struct ctk_button updatebutton = |
| 56 | {CTK_BUTTON(0, 3, 6, "Update")}; |
| 57 | static struct ctk_button closebutton = |
| 58 | {CTK_BUTTON(8, 3, 5, "Close")}; |
| 59 | |
adamdunkels | caebe7d | 2003-04-09 19:15:25 +0000 | [diff] [blame] | 60 | static DISPATCHER_SIGHANDLER(hello_sighandler, s, data); |
adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 61 | static struct dispatcher_proc p = |
| 62 | {DISPATCHER_PROC("Hello world", NULL, hello_sighandler, NULL)}; |
| 63 | static ek_id_t id; |
| 64 | |
| 65 | /*-----------------------------------------------------------------------------------*/ |
| 66 | LOADER_INIT_FUNC(hello_init) |
| 67 | { |
| 68 | if(id == EK_ID_NONE) { |
| 69 | id = dispatcher_start(&p); |
| 70 | |
| 71 | ctk_window_new(&window, 15, 4, "Hello"); |
adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 72 | |
| 73 | CTK_WIDGET_ADD(&window, &hellolabel); |
| 74 | CTK_WIDGET_ADD(&window, &worldlabel); |
| 75 | |
| 76 | CTK_WIDGET_ADD(&window, &updatebutton); |
| 77 | CTK_WIDGET_ADD(&window, &closebutton); |
| 78 | |
| 79 | CTK_WIDGET_FOCUS(&window, &updatebutton); |
| 80 | |
| 81 | dispatcher_listen(ctk_signal_button_activate); |
| 82 | dispatcher_listen(ctk_signal_window_close); |
| 83 | } |
| 84 | ctk_window_open(&window); |
| 85 | } |
| 86 | /*-----------------------------------------------------------------------------------*/ |
| 87 | static void |
| 88 | hello_quit(void) |
| 89 | { |
| 90 | dispatcher_exit(&p); |
| 91 | id = EK_ID_NONE; |
| 92 | LOADER_UNLOAD(); |
| 93 | ctk_redraw(); |
| 94 | } |
| 95 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | caebe7d | 2003-04-09 19:15:25 +0000 | [diff] [blame] | 96 | static DISPATCHER_SIGHANDLER(hello_sighandler, s, data) |
adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 97 | { |
adamdunkels | caebe7d | 2003-04-09 19:15:25 +0000 | [diff] [blame] | 98 | DISPATCHER_SIGHANDLER_ARGS(s, data); |
| 99 | |
adamdunkels | 2f5291c | 2003-04-09 12:55:06 +0000 | [diff] [blame] | 100 | if(s == ctk_signal_button_activate) { |
| 101 | if(data == (ek_data_t)&updatebutton) { |
| 102 | ctk_label_set_text(&hellolabel, "Good bye"); |
| 103 | CTK_WIDGET_REDRAW(&hellolabel); |
| 104 | } else if(data == (ek_data_t)&closebutton) { |
| 105 | ctk_window_close(&window); |
| 106 | hello_quit(); |
| 107 | } |
| 108 | } else if(s == ctk_signal_window_close && |
| 109 | data == (ek_data_t)&window) { |
| 110 | hello_quit(); |
| 111 | } |
| 112 | } |
| 113 | /*-----------------------------------------------------------------------------------*/ |