adamdunkels | e0cc058 | 2003-04-08 19:41:23 +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 file is part of the Contiki VNC client |
| 34 | * |
| 35 | * $Id: vnc.c,v 1.1 2003/04/08 19:41:23 adamdunkels Exp $ |
| 36 | * |
| 37 | */ |
| 38 | |
| 39 | #include "petsciiconv.h" |
| 40 | #include "uip_main.h" |
| 41 | #include "uip.h" |
| 42 | #include "ctk.h" |
| 43 | #include "dispatcher.h" |
| 44 | #include "resolv.h" |
| 45 | #include "telnet.h" |
| 46 | #include "vnc.h" |
| 47 | #include "vnc-draw.h" |
| 48 | #include "vnc-viewer.h" |
| 49 | #include "vnc-conf.h" |
| 50 | |
| 51 | #include "loader.h" |
| 52 | |
| 53 | #define HEIGHT (4 + VNC_CONF_VIEWPORT_HEIGHT/8) |
| 54 | |
| 55 | /* Main window */ |
| 56 | static struct ctk_window mainwindow; |
| 57 | |
| 58 | static char host[20]; |
| 59 | static struct ctk_textentry hosttextentry = |
| 60 | {CTK_TEXTENTRY(0, 0, 18, 1, host, 18)}; |
| 61 | |
| 62 | static char portentry[4]; |
| 63 | static struct ctk_textentry porttextentry = |
| 64 | {CTK_TEXTENTRY(21, 0, 3, 1, portentry, 3)}; |
| 65 | |
| 66 | static struct ctk_button connectbutton = |
| 67 | {CTK_BUTTON(27, 0, 7, "Connect")}; |
| 68 | /*static struct ctk_button disconnectbutton = |
| 69 | {CTK_BUTTON(25, 3, 10, "Disconnect")};*/ |
| 70 | |
| 71 | static struct ctk_separator sep1 = |
| 72 | {CTK_SEPARATOR(0, 1, 36)}; |
| 73 | |
| 74 | static struct ctk_bitmap vncbitmap = |
| 75 | {CTK_BITMAP(2, 2, |
| 76 | VNC_CONF_VIEWPORT_WIDTH / 8, |
| 77 | VNC_CONF_VIEWPORT_HEIGHT / 8, |
| 78 | vnc_draw_bitmap)}; |
| 79 | |
| 80 | static struct ctk_button leftbutton = |
| 81 | {CTK_BUTTON(6, HEIGHT - 1, 4, "Left")}; |
| 82 | |
| 83 | static struct ctk_button upbutton = |
| 84 | {CTK_BUTTON(13, HEIGHT - 1, 2, "Up")}; |
| 85 | |
| 86 | static struct ctk_button downbutton = |
| 87 | {CTK_BUTTON(18, HEIGHT - 1, 4, "Down")}; |
| 88 | |
| 89 | static struct ctk_button rightbutton = |
| 90 | {CTK_BUTTON(25, HEIGHT - 1, 5, "Right")}; |
| 91 | |
| 92 | static void sighandler(ek_signal_t s, ek_data_t data); |
| 93 | static struct dispatcher_proc p = |
| 94 | {DISPATCHER_PROC("VNC client", NULL, sighandler, |
| 95 | (void (*)(void *))vnc_viewer_app)}; |
| 96 | static ek_id_t id; |
| 97 | |
| 98 | /*-----------------------------------------------------------------------------------*/ |
| 99 | LOADER_INIT_FUNC(vnc_init) |
| 100 | { |
| 101 | if(id == EK_ID_NONE) { |
| 102 | id = dispatcher_start(&p); |
| 103 | |
| 104 | ctk_window_new(&mainwindow, 36, HEIGHT, "VNC client"); |
| 105 | ctk_window_move(&mainwindow, 0, 0); |
| 106 | |
| 107 | CTK_WIDGET_ADD(&mainwindow, &hosttextentry); |
| 108 | CTK_WIDGET_FOCUS(&mainwindow, &hosttextentry); |
| 109 | CTK_WIDGET_ADD(&mainwindow, &porttextentry); |
| 110 | CTK_WIDGET_ADD(&mainwindow, &connectbutton); |
| 111 | |
| 112 | CTK_WIDGET_ADD(&mainwindow, &sep1); |
| 113 | |
| 114 | CTK_WIDGET_ADD(&mainwindow, &vncbitmap); |
| 115 | |
| 116 | CTK_WIDGET_ADD(&mainwindow, &leftbutton); |
| 117 | CTK_WIDGET_ADD(&mainwindow, &upbutton); |
| 118 | CTK_WIDGET_ADD(&mainwindow, &downbutton); |
| 119 | CTK_WIDGET_ADD(&mainwindow, &rightbutton); |
| 120 | |
| 121 | dispatcher_listen(ctk_signal_window_close); |
| 122 | dispatcher_listen(ctk_signal_button_activate); |
| 123 | dispatcher_listen(resolv_signal_found); |
| 124 | |
| 125 | |
| 126 | vnc_draw_init(); |
| 127 | } |
| 128 | |
| 129 | ctk_window_open(&mainwindow); |
| 130 | |
| 131 | } |
| 132 | /*-----------------------------------------------------------------------------------*/ |
| 133 | static void |
| 134 | show(char *text) |
| 135 | { |
| 136 | |
| 137 | } |
| 138 | /*-----------------------------------------------------------------------------------*/ |
| 139 | static void |
| 140 | connect(void) |
| 141 | { |
| 142 | u16_t addr[2], *addrptr; |
| 143 | u16_t port; |
| 144 | char *cptr; |
| 145 | |
| 146 | /* Find the first space character in host and put a zero there |
| 147 | to end the string. */ |
| 148 | for(cptr = host; *cptr != ' ' && *cptr != 0; ++cptr); |
| 149 | *cptr = 0; |
| 150 | |
| 151 | addrptr = &addr[0]; |
| 152 | if(uip_main_ipaddrconv(host, (unsigned char *)addr) == 0) { |
| 153 | addrptr = resolv_lookup(host); |
| 154 | if(addrptr == NULL) { |
| 155 | resolv_query(host); |
| 156 | show("Resolving host..."); |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | port = 0; |
| 162 | for(cptr = portentry; *cptr != ' ' && *cptr != 0; ++cptr) { |
| 163 | if(*cptr < '0' || *cptr > '9') { |
| 164 | show("Port number error"); |
| 165 | return; |
| 166 | } |
| 167 | port = 10 * port + *cptr - '0'; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | vnc_viewer_connect(addrptr, port); |
| 172 | |
| 173 | show("Connecting..."); |
| 174 | |
| 175 | } |
| 176 | /*-----------------------------------------------------------------------------------*/ |
| 177 | static void |
| 178 | sighandler(ek_signal_t s, ek_data_t data) |
| 179 | { |
| 180 | if(s == ctk_signal_button_activate) { |
| 181 | if(data == (ek_data_t)&connectbutton) { |
| 182 | connect(); |
| 183 | } |
| 184 | } else if(s == ctk_signal_window_close) { |
| 185 | dispatcher_exit(&p); |
| 186 | id = EK_ID_NONE; |
| 187 | LOADER_UNLOAD(); |
| 188 | } else if(s == resolv_signal_found) { |
| 189 | if(strcmp(data, host) == 0) { |
| 190 | if(resolv_lookup(host) != NULL) { |
| 191 | connect(); |
| 192 | } else { |
| 193 | show("Host not found"); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | /*-----------------------------------------------------------------------------------*/ |
| 199 | void |
| 200 | vnc_viewer_refresh(void) |
| 201 | { |
| 202 | CTK_WIDGET_REDRAW(&vncbitmap); |
| 203 | } |
| 204 | /*-----------------------------------------------------------------------------------*/ |