blob: 849abcab0fd09c5f90d4bbfb916745f0a779f48d [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 *
adamdunkels06f897e2004-06-06 05:59:20 +000032 * $Id: vnc.c,v 1.8 2004/06/06 06:03:03 adamdunkels Exp $
adamdunkelse0cc0582003-04-08 19:41:23 +000033 *
34 */
35
adamdunkelsdb300d22004-02-24 09:57:49 +000036#include <string.h>
37
adamdunkelse0cc0582003-04-08 19:41:23 +000038#include "petsciiconv.h"
adamdunkelsdb300d22004-02-24 09:57:49 +000039#include "uiplib.h"
adamdunkelse0cc0582003-04-08 19:41:23 +000040#include "uip.h"
41#include "ctk.h"
adamdunkelsdb300d22004-02-24 09:57:49 +000042#include "ctk-mouse.h"
adamdunkelse0cc0582003-04-08 19:41:23 +000043#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
adamdunkels795ceea2003-07-31 23:48:42 +000053#if 1
adamdunkels8539c152003-04-17 18:55:38 +000054#define PRINTF(x)
55#else
56#include <stdio.h>
57#define PRINTF(x) printf x
58#endif
59
adamdunkelse0cc0582003-04-08 19:41:23 +000060#define HEIGHT (4 + VNC_CONF_VIEWPORT_HEIGHT/8)
61
62/* Main window */
63static struct ctk_window mainwindow;
64
65static char host[20];
66static struct ctk_textentry hosttextentry =
67 {CTK_TEXTENTRY(0, 0, 18, 1, host, 18)};
68
69static char portentry[4];
70static struct ctk_textentry porttextentry =
71 {CTK_TEXTENTRY(21, 0, 3, 1, portentry, 3)};
72
73static struct ctk_button connectbutton =
74 {CTK_BUTTON(27, 0, 7, "Connect")};
75/*static struct ctk_button disconnectbutton =
76 {CTK_BUTTON(25, 3, 10, "Disconnect")};*/
77
78static struct ctk_separator sep1 =
79 {CTK_SEPARATOR(0, 1, 36)};
80
81static struct ctk_bitmap vncbitmap =
82 {CTK_BITMAP(2, 2,
83 VNC_CONF_VIEWPORT_WIDTH / 8,
84 VNC_CONF_VIEWPORT_HEIGHT / 8,
85 vnc_draw_bitmap)};
86
87static struct ctk_button leftbutton =
88 {CTK_BUTTON(6, HEIGHT - 1, 4, "Left")};
89
90static struct ctk_button upbutton =
91 {CTK_BUTTON(13, HEIGHT - 1, 2, "Up")};
92
93static struct ctk_button downbutton =
94 {CTK_BUTTON(18, HEIGHT - 1, 4, "Down")};
95
96static struct ctk_button rightbutton =
97 {CTK_BUTTON(25, HEIGHT - 1, 5, "Right")};
98
adamdunkels78c03dc2003-04-09 13:45:05 +000099static DISPATCHER_SIGHANDLER(vnc_sighandler, s, data);
adamdunkelse0cc0582003-04-08 19:41:23 +0000100static struct dispatcher_proc p =
adamdunkels78c03dc2003-04-09 13:45:05 +0000101 {DISPATCHER_PROC("VNC client", NULL, vnc_sighandler,
adamdunkels2e415422003-04-10 09:04:49 +0000102 vnc_viewer_app)};
adamdunkelse0cc0582003-04-08 19:41:23 +0000103static ek_id_t id;
104
105/*-----------------------------------------------------------------------------------*/
adamdunkels8bb5cca2003-08-24 22:41:31 +0000106LOADER_INIT_FUNC(vnc_init, arg)
adamdunkelse0cc0582003-04-08 19:41:23 +0000107{
adamdunkels8bb5cca2003-08-24 22:41:31 +0000108 arg_free(arg);
109
adamdunkelse0cc0582003-04-08 19:41:23 +0000110 if(id == EK_ID_NONE) {
111 id = dispatcher_start(&p);
112
113 ctk_window_new(&mainwindow, 36, HEIGHT, "VNC client");
114 ctk_window_move(&mainwindow, 0, 0);
115
116 CTK_WIDGET_ADD(&mainwindow, &hosttextentry);
117 CTK_WIDGET_FOCUS(&mainwindow, &hosttextentry);
118 CTK_WIDGET_ADD(&mainwindow, &porttextentry);
119 CTK_WIDGET_ADD(&mainwindow, &connectbutton);
120
121 CTK_WIDGET_ADD(&mainwindow, &sep1);
122
123 CTK_WIDGET_ADD(&mainwindow, &vncbitmap);
124
125 CTK_WIDGET_ADD(&mainwindow, &leftbutton);
126 CTK_WIDGET_ADD(&mainwindow, &upbutton);
127 CTK_WIDGET_ADD(&mainwindow, &downbutton);
128 CTK_WIDGET_ADD(&mainwindow, &rightbutton);
129
130 dispatcher_listen(ctk_signal_window_close);
131 dispatcher_listen(ctk_signal_button_activate);
132 dispatcher_listen(resolv_signal_found);
133
adamdunkels8539c152003-04-17 18:55:38 +0000134 dispatcher_listen(ctk_signal_pointer_move);
adamdunkelse0cc0582003-04-08 19:41:23 +0000135
136 vnc_draw_init();
137 }
138
139 ctk_window_open(&mainwindow);
140
141}
142/*-----------------------------------------------------------------------------------*/
143static void
144show(char *text)
145{
146
147}
148/*-----------------------------------------------------------------------------------*/
149static void
150connect(void)
151{
152 u16_t addr[2], *addrptr;
153 u16_t port;
154 char *cptr;
155
156 /* Find the first space character in host and put a zero there
157 to end the string. */
158 for(cptr = host; *cptr != ' ' && *cptr != 0; ++cptr);
159 *cptr = 0;
160
161 addrptr = &addr[0];
adamdunkelsdb300d22004-02-24 09:57:49 +0000162 if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) {
adamdunkelse0cc0582003-04-08 19:41:23 +0000163 addrptr = resolv_lookup(host);
164 if(addrptr == NULL) {
165 resolv_query(host);
166 show("Resolving host...");
167 return;
168 }
169 }
170
171 port = 0;
172 for(cptr = portentry; *cptr != ' ' && *cptr != 0; ++cptr) {
173 if(*cptr < '0' || *cptr > '9') {
174 show("Port number error");
175 return;
176 }
177 port = 10 * port + *cptr - '0';
178 }
179
180
181 vnc_viewer_connect(addrptr, port);
182
183 show("Connecting...");
184
185}
186/*-----------------------------------------------------------------------------------*/
adamdunkels78c03dc2003-04-09 13:45:05 +0000187static
188DISPATCHER_SIGHANDLER(vnc_sighandler, s, data)
adamdunkelse0cc0582003-04-08 19:41:23 +0000189{
adamdunkels8539c152003-04-17 18:55:38 +0000190 unsigned short x, y;
191 unsigned char xc, yc;
adamdunkels78c03dc2003-04-09 13:45:05 +0000192 DISPATCHER_SIGHANDLER_ARGS(s, data);
193
adamdunkelse0cc0582003-04-08 19:41:23 +0000194 if(s == ctk_signal_button_activate) {
195 if(data == (ek_data_t)&connectbutton) {
196 connect();
197 }
198 } else if(s == ctk_signal_window_close) {
199 dispatcher_exit(&p);
200 id = EK_ID_NONE;
201 LOADER_UNLOAD();
202 } else if(s == resolv_signal_found) {
203 if(strcmp(data, host) == 0) {
204 if(resolv_lookup(host) != NULL) {
205 connect();
206 } else {
207 show("Host not found");
208 }
209 }
adamdunkels8539c152003-04-17 18:55:38 +0000210 } else if(s == ctk_signal_pointer_move) {
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
adamdunkelse0cc0582003-04-08 19:41:23 +0000225 }
226}
227/*-----------------------------------------------------------------------------------*/
228void
229vnc_viewer_refresh(void)
230{
231 CTK_WIDGET_REDRAW(&vncbitmap);
232}
233/*-----------------------------------------------------------------------------------*/