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. |
adamdunkels | 06f897e | 2004-06-06 05:59:20 +0000 | [diff] [blame] | 14 | * 3. The name of the author may not be used to endorse or promote |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 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 VNC client |
| 31 | * |
adamdunkels | 0c38881 | 2004-09-12 07:32:05 +0000 | [diff] [blame] | 32 | * $Id: vnc.c,v 1.10 2004/09/12 07:32:05 adamdunkels Exp $ |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 36 | #include <string.h> |
| 37 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 38 | #include "contiki.h" |
| 39 | |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 40 | #include "petsciiconv.h" |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 41 | #include "uiplib.h" |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 42 | #include "uip.h" |
| 43 | #include "ctk.h" |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 44 | #include "ctk-mouse.h" |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 45 | #include "resolv.h" |
| 46 | #include "telnet.h" |
| 47 | #include "vnc.h" |
| 48 | #include "vnc-draw.h" |
| 49 | #include "vnc-viewer.h" |
| 50 | #include "vnc-conf.h" |
| 51 | |
| 52 | #include "loader.h" |
| 53 | |
adamdunkels | 795ceea | 2003-07-31 23:48:42 +0000 | [diff] [blame] | 54 | #if 1 |
adamdunkels | 8539c15 | 2003-04-17 18:55:38 +0000 | [diff] [blame] | 55 | #define PRINTF(x) |
| 56 | #else |
| 57 | #include <stdio.h> |
| 58 | #define PRINTF(x) printf x |
| 59 | #endif |
| 60 | |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 61 | #define HEIGHT (4 + VNC_CONF_VIEWPORT_HEIGHT/8) |
| 62 | |
| 63 | /* Main window */ |
| 64 | static struct ctk_window mainwindow; |
| 65 | |
| 66 | static char host[20]; |
| 67 | static struct ctk_textentry hosttextentry = |
| 68 | {CTK_TEXTENTRY(0, 0, 18, 1, host, 18)}; |
| 69 | |
| 70 | static char portentry[4]; |
| 71 | static struct ctk_textentry porttextentry = |
| 72 | {CTK_TEXTENTRY(21, 0, 3, 1, portentry, 3)}; |
| 73 | |
| 74 | static struct ctk_button connectbutton = |
| 75 | {CTK_BUTTON(27, 0, 7, "Connect")}; |
| 76 | /*static struct ctk_button disconnectbutton = |
| 77 | {CTK_BUTTON(25, 3, 10, "Disconnect")};*/ |
| 78 | |
| 79 | static struct ctk_separator sep1 = |
| 80 | {CTK_SEPARATOR(0, 1, 36)}; |
| 81 | |
| 82 | static struct ctk_bitmap vncbitmap = |
| 83 | {CTK_BITMAP(2, 2, |
| 84 | VNC_CONF_VIEWPORT_WIDTH / 8, |
| 85 | VNC_CONF_VIEWPORT_HEIGHT / 8, |
adamdunkels | 0c38881 | 2004-09-12 07:32:05 +0000 | [diff] [blame] | 86 | vnc_draw_bitmap, |
| 87 | VNC_CONF_VIEWPORT_WIDTH, |
| 88 | VNC_CONF_VIEWPORT_HEIGHT)}; |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 89 | |
| 90 | static struct ctk_button leftbutton = |
| 91 | {CTK_BUTTON(6, HEIGHT - 1, 4, "Left")}; |
| 92 | |
| 93 | static struct ctk_button upbutton = |
| 94 | {CTK_BUTTON(13, HEIGHT - 1, 2, "Up")}; |
| 95 | |
| 96 | static struct ctk_button downbutton = |
| 97 | {CTK_BUTTON(18, HEIGHT - 1, 4, "Down")}; |
| 98 | |
| 99 | static struct ctk_button rightbutton = |
| 100 | {CTK_BUTTON(25, HEIGHT - 1, 5, "Right")}; |
| 101 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 102 | /*static DISPATCHER_SIGHANDLER(vnc_sighandler, s, data); |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 103 | static struct dispatcher_proc p = |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 104 | {DISPATCHER_PROC("VNC client", NULL, vnc_sighandler, |
adamdunkels | 2e41542 | 2003-04-10 09:04:49 +0000 | [diff] [blame] | 105 | vnc_viewer_app)}; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 106 | static ek_id_t id;*/ |
| 107 | EK_EVENTHANDLER(eventhandler, ev, data); |
| 108 | EK_PROCESS(p, "VNC viewer", EK_PRIO_NORMAL, |
| 109 | eventhandler, NULL, NULL); |
| 110 | static ek_id_t id = EK_ID_NONE; |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 111 | |
| 112 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 113 | LOADER_INIT_FUNC(vnc_init, arg) |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 114 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 115 | arg_free(arg); |
| 116 | |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 117 | if(id == EK_ID_NONE) { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 118 | id = ek_start(&p); |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 119 | } |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 120 | } |
| 121 | /*-----------------------------------------------------------------------------------*/ |
| 122 | static void |
| 123 | show(char *text) |
| 124 | { |
| 125 | |
| 126 | } |
| 127 | /*-----------------------------------------------------------------------------------*/ |
| 128 | static void |
| 129 | connect(void) |
| 130 | { |
| 131 | u16_t addr[2], *addrptr; |
| 132 | u16_t port; |
| 133 | char *cptr; |
| 134 | |
| 135 | /* Find the first space character in host and put a zero there |
| 136 | to end the string. */ |
| 137 | for(cptr = host; *cptr != ' ' && *cptr != 0; ++cptr); |
| 138 | *cptr = 0; |
| 139 | |
| 140 | addrptr = &addr[0]; |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 141 | if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) { |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 142 | addrptr = resolv_lookup(host); |
| 143 | if(addrptr == NULL) { |
| 144 | resolv_query(host); |
| 145 | show("Resolving host..."); |
| 146 | return; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | port = 0; |
| 151 | for(cptr = portentry; *cptr != ' ' && *cptr != 0; ++cptr) { |
| 152 | if(*cptr < '0' || *cptr > '9') { |
| 153 | show("Port number error"); |
| 154 | return; |
| 155 | } |
| 156 | port = 10 * port + *cptr - '0'; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | vnc_viewer_connect(addrptr, port); |
| 161 | |
| 162 | show("Connecting..."); |
| 163 | |
| 164 | } |
| 165 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 166 | EK_EVENTHANDLER(eventhandler, ev, data) |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 167 | { |
adamdunkels | 8539c15 | 2003-04-17 18:55:38 +0000 | [diff] [blame] | 168 | unsigned short x, y; |
| 169 | unsigned char xc, yc; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 170 | EK_EVENTHANDLER_ARGS(ev, data); |
| 171 | |
| 172 | if(ev == EK_EVENT_INIT) { |
| 173 | ctk_window_new(&mainwindow, 36, HEIGHT, "VNC client"); |
| 174 | ctk_window_move(&mainwindow, 0, 0); |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 175 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 176 | CTK_WIDGET_ADD(&mainwindow, &hosttextentry); |
| 177 | CTK_WIDGET_FOCUS(&mainwindow, &hosttextentry); |
| 178 | CTK_WIDGET_ADD(&mainwindow, &porttextentry); |
| 179 | CTK_WIDGET_ADD(&mainwindow, &connectbutton); |
| 180 | |
| 181 | CTK_WIDGET_ADD(&mainwindow, &sep1); |
| 182 | |
| 183 | CTK_WIDGET_ADD(&mainwindow, &vncbitmap); |
| 184 | |
| 185 | CTK_WIDGET_ADD(&mainwindow, &leftbutton); |
| 186 | CTK_WIDGET_ADD(&mainwindow, &upbutton); |
| 187 | CTK_WIDGET_ADD(&mainwindow, &downbutton); |
| 188 | CTK_WIDGET_ADD(&mainwindow, &rightbutton); |
| 189 | |
| 190 | vnc_draw_init(); |
| 191 | |
| 192 | ctk_window_open(&mainwindow); |
| 193 | |
| 194 | } else if(ev == ctk_signal_button_activate) { |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 195 | if(data == (ek_data_t)&connectbutton) { |
| 196 | connect(); |
| 197 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 198 | } else if(ev == ctk_signal_window_close) { |
| 199 | ek_exit(); |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 200 | id = EK_ID_NONE; |
| 201 | LOADER_UNLOAD(); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 202 | } else if(ev == resolv_event_found) { |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 203 | if(strcmp(data, host) == 0) { |
| 204 | if(resolv_lookup(host) != NULL) { |
| 205 | connect(); |
| 206 | } else { |
| 207 | show("Host not found"); |
| 208 | } |
| 209 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 210 | } else if(ev == ctk_signal_pointer_move) { |
adamdunkels | 8539c15 | 2003-04-17 18:55:38 +0000 | [diff] [blame] | 211 | /* Check if pointer is within the VNC viewer area */ |
| 212 | x = ctk_mouse_x(); |
| 213 | y = ctk_mouse_y(); |
| 214 | |
| 215 | xc = ctk_mouse_xtoc(x); |
| 216 | yc = ctk_mouse_ytoc(y); |
| 217 | |
| 218 | if(xc >= 2 && yc >= 2 && |
| 219 | xc < 2 + VNC_CONF_VIEWPORT_WIDTH / 8 && |
| 220 | yc < 2 + VNC_CONF_VIEWPORT_HEIGHT / 8) { |
| 221 | |
| 222 | VNC_VIEWER_POST_POINTER_EVENT(x, y, 0); |
| 223 | } |
| 224 | |
adamdunkels | 0c38881 | 2004-09-12 07:32:05 +0000 | [diff] [blame] | 225 | } else if(ev == tcpip_event) { |
| 226 | vnc_viewer_appcall(data); |
adamdunkels | e0cc058 | 2003-04-08 19:41:23 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | /*-----------------------------------------------------------------------------------*/ |
| 230 | void |
| 231 | vnc_viewer_refresh(void) |
| 232 | { |
| 233 | CTK_WIDGET_REDRAW(&vncbitmap); |
| 234 | } |
| 235 | /*-----------------------------------------------------------------------------------*/ |