Passed tests on Raven 1284p in 3 seconds with 56KB program memory disk
[contiki-2.x.git] / apps / vnc / vnc.c
blobd532175d564cc985f98615e2b33e3bfbbd79ba18
1 /*
2 * Copyright (c) 2003, Adam Dunkels.
3 * All rights reserved.
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.
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.
30 * This file is part of the Contiki VNC client
32 * $Id: vnc.c,v 1.1 2006/06/17 23:08:35 adamdunkels Exp $
36 #include <string.h>
38 #include "contiki.h"
40 /*#include "petsciiconv.h"*/
41 #include "net/uiplib.h"
42 #include "net/uip.h"
43 #include "ctk/ctk.h"
44 #include "ctk/ctk-mouse.h"
45 #include "net/resolv.h"
46 /*#include "telnet.h"*/
47 #include "vnc.h"
48 #include "vnc-draw.h"
49 #include "vnc-viewer.h"
50 #include "contiki-conf.h"
52 #include "sys/loader.h"
54 #if 1
55 #define PRINTF(x)
56 #else
57 #include <stdio.h>
58 #define PRINTF(x) printf x
59 #endif
62 #ifdef VNC_CONF_VIEWPORT_WIDTH
63 #define VNC_VIEWPORT_WIDTH VNC_CONF_VIEWPORT_WIDTH
64 #else
65 #define VNC_VIEWPORT_WIDTH 10
66 #endif
68 #ifdef VNC_CONF_VIEWPORT_HEIGHT
69 #define VNC_VIEWPORT_HEIGHT VNC_CONF_VIEWPORT_HEIGHT
70 #else
71 #define VNC_VIEWPORT_HEIGHT 10
72 #endif
74 #define HEIGHT (4 + VNC_VIEWPORT_HEIGHT/8)
76 /* Main window */
77 static struct ctk_window mainwindow;
79 static char host[20];
80 static struct ctk_textentry hosttextentry =
81 {CTK_TEXTENTRY(0, 0, 18, 1, host, 18)};
83 static char portentry[4];
84 static struct ctk_textentry porttextentry =
85 {CTK_TEXTENTRY(21, 0, 3, 1, portentry, 3)};
87 static struct ctk_button connectbutton =
88 {CTK_BUTTON(27, 0, 7, "Connect")};
89 /*static struct ctk_button disconnectbutton =
90 {CTK_BUTTON(25, 3, 10, "Disconnect")};*/
92 static struct ctk_separator sep1 =
93 {CTK_SEPARATOR(0, 1, 36)};
95 static struct ctk_bitmap vncbitmap =
96 {CTK_BITMAP(2, 2,
97 VNC_VIEWPORT_WIDTH / 8,
98 VNC_VIEWPORT_HEIGHT / 8,
99 vnc_draw_bitmap,
100 VNC_VIEWPORT_WIDTH,
101 VNC_VIEWPORT_HEIGHT)};
103 static struct ctk_button leftbutton =
104 {CTK_BUTTON(6, HEIGHT - 1, 4, "Left")};
106 static struct ctk_button upbutton =
107 {CTK_BUTTON(13, HEIGHT - 1, 2, "Up")};
109 static struct ctk_button downbutton =
110 {CTK_BUTTON(18, HEIGHT - 1, 4, "Down")};
112 static struct ctk_button rightbutton =
113 {CTK_BUTTON(25, HEIGHT - 1, 5, "Right")};
115 PROCESS(vnc_process, "VNC viewer");
117 /*-----------------------------------------------------------------------------------*/
118 static void
119 show(char *text)
123 /*-----------------------------------------------------------------------------------*/
124 static void
125 connect(void)
127 u16_t addr[2], *addrptr;
128 u16_t port;
129 char *cptr;
131 /* Find the first space character in host and put a zero there
132 to end the string. */
133 for(cptr = host; *cptr != ' ' && *cptr != 0; ++cptr);
134 *cptr = 0;
136 addrptr = &addr[0];
137 if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) {
138 addrptr = resolv_lookup(host);
139 if(addrptr == NULL) {
140 resolv_query(host);
141 show("Resolving host...");
142 return;
146 port = 0;
147 for(cptr = portentry; *cptr != ' ' && *cptr != 0; ++cptr) {
148 if(*cptr < '0' || *cptr > '9') {
149 show("Port number error");
150 return;
152 port = 10 * port + *cptr - '0';
156 vnc_viewer_connect(addrptr, port);
158 show("Connecting...");
161 /*-----------------------------------------------------------------------------------*/
162 PROCESS_THREAD(vnc_process, ev, data)
164 unsigned short x, y;
165 unsigned char xc, yc;
167 PROCESS_BEGIN();
169 ctk_window_new(&mainwindow, 36, HEIGHT, "VNC client");
170 ctk_window_move(&mainwindow, 0, 0);
172 CTK_WIDGET_ADD(&mainwindow, &hosttextentry);
173 CTK_WIDGET_FOCUS(&mainwindow, &hosttextentry);
174 CTK_WIDGET_ADD(&mainwindow, &porttextentry);
175 CTK_WIDGET_ADD(&mainwindow, &connectbutton);
177 CTK_WIDGET_ADD(&mainwindow, &sep1);
179 CTK_WIDGET_ADD(&mainwindow, &vncbitmap);
181 CTK_WIDGET_ADD(&mainwindow, &leftbutton);
182 CTK_WIDGET_ADD(&mainwindow, &upbutton);
183 CTK_WIDGET_ADD(&mainwindow, &downbutton);
184 CTK_WIDGET_ADD(&mainwindow, &rightbutton);
186 vnc_draw_init();
188 ctk_window_open(&mainwindow);
190 while(1) {
191 PROCESS_WAIT_EVENT();
193 if(ev == ctk_signal_button_activate) {
194 if(data == (process_data_t)&connectbutton) {
195 connect();
197 } else if(ev == ctk_signal_window_close) {
198 process_exit(&vnc_process);
199 LOADER_UNLOAD();
200 } else if(ev == resolv_event_found) {
201 if(strcmp(data, host) == 0) {
202 if(resolv_lookup(host) != NULL) {
203 connect();
204 } else {
205 show("Host not found");
208 } else if(ev == ctk_signal_pointer_move) {
209 /* Check if pointer is within the VNC viewer area */
210 x = ctk_mouse_x();
211 y = ctk_mouse_y();
213 xc = ctk_mouse_xtoc(x);
214 yc = ctk_mouse_ytoc(y);
216 if(xc >= 2 && yc >= 2 &&
217 xc < 2 + VNC_VIEWPORT_WIDTH / 8 &&
218 yc < 2 + VNC_VIEWPORT_HEIGHT / 8) {
220 VNC_VIEWER_POST_POINTER_EVENT(x, y, 0);
223 } else if(ev == tcpip_event) {
224 vnc_viewer_appcall(data);
228 PROCESS_END();
230 /*-----------------------------------------------------------------------------------*/
231 void
232 vnc_viewer_refresh(void)
234 CTK_WIDGET_REDRAW(&vncbitmap);
236 /*-----------------------------------------------------------------------------------*/