blob: 8853d1459ee5e32d60a3da79e7ac7ed85accae69 [file] [log] [blame]
adamdunkelse0cc0582003-04-08 19:41:23 +00001/*
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.
adamdunkels06f897e2004-06-06 05:59:20 +000014 * 3. The name of the author may not be used to endorse or promote
adamdunkelse0cc0582003-04-08 19:41:23 +000015 * 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 *
adamdunkels0c388812004-09-12 07:32:05 +000032 * $Id: vnc.c,v 1.10 2004/09/12 07:32:05 adamdunkels Exp $
adamdunkelse0cc0582003-04-08 19:41:23 +000033 *
34 */
35
adamdunkelsdb300d22004-02-24 09:57:49 +000036#include <string.h>
37
adamdunkelsf2f8cb22004-07-04 11:35:07 +000038#include "contiki.h"
39
adamdunkelse0cc0582003-04-08 19:41:23 +000040#include "petsciiconv.h"
adamdunkelsdb300d22004-02-24 09:57:49 +000041#include "uiplib.h"
adamdunkelse0cc0582003-04-08 19:41:23 +000042#include "uip.h"
43#include "ctk.h"
adamdunkelsdb300d22004-02-24 09:57:49 +000044#include "ctk-mouse.h"
adamdunkelse0cc0582003-04-08 19:41:23 +000045#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
adamdunkels795ceea2003-07-31 23:48:42 +000054#if 1
adamdunkels8539c152003-04-17 18:55:38 +000055#define PRINTF(x)
56#else
57#include <stdio.h>
58#define PRINTF(x) printf x
59#endif
60
adamdunkelse0cc0582003-04-08 19:41:23 +000061#define HEIGHT (4 + VNC_CONF_VIEWPORT_HEIGHT/8)
62
63/* Main window */
64static struct ctk_window mainwindow;
65
66static char host[20];
67static struct ctk_textentry hosttextentry =
68 {CTK_TEXTENTRY(0, 0, 18, 1, host, 18)};
69
70static char portentry[4];
71static struct ctk_textentry porttextentry =
72 {CTK_TEXTENTRY(21, 0, 3, 1, portentry, 3)};
73
74static 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
79static struct ctk_separator sep1 =
80 {CTK_SEPARATOR(0, 1, 36)};
81
82static struct ctk_bitmap vncbitmap =
83 {CTK_BITMAP(2, 2,
84 VNC_CONF_VIEWPORT_WIDTH / 8,
85 VNC_CONF_VIEWPORT_HEIGHT / 8,
adamdunkels0c388812004-09-12 07:32:05 +000086 vnc_draw_bitmap,
87 VNC_CONF_VIEWPORT_WIDTH,
88 VNC_CONF_VIEWPORT_HEIGHT)};
adamdunkelse0cc0582003-04-08 19:41:23 +000089
90static struct ctk_button leftbutton =
91 {CTK_BUTTON(6, HEIGHT - 1, 4, "Left")};
92
93static struct ctk_button upbutton =
94 {CTK_BUTTON(13, HEIGHT - 1, 2, "Up")};
95
96static struct ctk_button downbutton =
97 {CTK_BUTTON(18, HEIGHT - 1, 4, "Down")};
98
99static struct ctk_button rightbutton =
100 {CTK_BUTTON(25, HEIGHT - 1, 5, "Right")};
101
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000102/*static DISPATCHER_SIGHANDLER(vnc_sighandler, s, data);
adamdunkelse0cc0582003-04-08 19:41:23 +0000103static struct dispatcher_proc p =
adamdunkels78c03dc2003-04-09 13:45:05 +0000104 {DISPATCHER_PROC("VNC client", NULL, vnc_sighandler,
adamdunkels2e415422003-04-10 09:04:49 +0000105 vnc_viewer_app)};
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000106 static ek_id_t id;*/
107EK_EVENTHANDLER(eventhandler, ev, data);
108EK_PROCESS(p, "VNC viewer", EK_PRIO_NORMAL,
109 eventhandler, NULL, NULL);
110static ek_id_t id = EK_ID_NONE;
adamdunkelse0cc0582003-04-08 19:41:23 +0000111
112/*-----------------------------------------------------------------------------------*/
adamdunkels8bb5cca2003-08-24 22:41:31 +0000113LOADER_INIT_FUNC(vnc_init, arg)
adamdunkelse0cc0582003-04-08 19:41:23 +0000114{
adamdunkels8bb5cca2003-08-24 22:41:31 +0000115 arg_free(arg);
116
adamdunkelse0cc0582003-04-08 19:41:23 +0000117 if(id == EK_ID_NONE) {
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000118 id = ek_start(&p);
adamdunkelse0cc0582003-04-08 19:41:23 +0000119 }
adamdunkelse0cc0582003-04-08 19:41:23 +0000120}
121/*-----------------------------------------------------------------------------------*/
122static void
123show(char *text)
124{
125
126}
127/*-----------------------------------------------------------------------------------*/
128static void
129connect(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];
adamdunkelsdb300d22004-02-24 09:57:49 +0000141 if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) {
adamdunkelse0cc0582003-04-08 19:41:23 +0000142 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/*-----------------------------------------------------------------------------------*/
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000166EK_EVENTHANDLER(eventhandler, ev, data)
adamdunkelse0cc0582003-04-08 19:41:23 +0000167{
adamdunkels8539c152003-04-17 18:55:38 +0000168 unsigned short x, y;
169 unsigned char xc, yc;
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000170 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);
adamdunkels78c03dc2003-04-09 13:45:05 +0000175
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000176 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) {
adamdunkelse0cc0582003-04-08 19:41:23 +0000195 if(data == (ek_data_t)&connectbutton) {
196 connect();
197 }
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000198 } else if(ev == ctk_signal_window_close) {
199 ek_exit();
adamdunkelse0cc0582003-04-08 19:41:23 +0000200 id = EK_ID_NONE;
201 LOADER_UNLOAD();
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000202 } else if(ev == resolv_event_found) {
adamdunkelse0cc0582003-04-08 19:41:23 +0000203 if(strcmp(data, host) == 0) {
204 if(resolv_lookup(host) != NULL) {
205 connect();
206 } else {
207 show("Host not found");
208 }
209 }
adamdunkelsf2f8cb22004-07-04 11:35:07 +0000210 } else if(ev == ctk_signal_pointer_move) {
adamdunkels8539c152003-04-17 18:55:38 +0000211 /* 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
adamdunkels0c388812004-09-12 07:32:05 +0000225 } else if(ev == tcpip_event) {
226 vnc_viewer_appcall(data);
adamdunkelse0cc0582003-04-08 19:41:23 +0000227 }
228}
229/*-----------------------------------------------------------------------------------*/
230void
231vnc_viewer_refresh(void)
232{
233 CTK_WIDGET_REDRAW(&vncbitmap);
234}
235/*-----------------------------------------------------------------------------------*/