blob: 818d219fbe481eae2bdecde3585efb2b0c6fa9c9 [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.
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 *
adamdunkels795ceea2003-07-31 23:48:42 +000035 * $Id: vnc.c,v 1.5 2003/07/31 23:48:42 adamdunkels Exp $
adamdunkelse0cc0582003-04-08 19:41:23 +000036 *
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
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/*-----------------------------------------------------------------------------------*/
106LOADER_INIT_FUNC(vnc_init)
107{
108 if(id == EK_ID_NONE) {
109 id = dispatcher_start(&p);
110
111 ctk_window_new(&mainwindow, 36, HEIGHT, "VNC client");
112 ctk_window_move(&mainwindow, 0, 0);
113
114 CTK_WIDGET_ADD(&mainwindow, &hosttextentry);
115 CTK_WIDGET_FOCUS(&mainwindow, &hosttextentry);
116 CTK_WIDGET_ADD(&mainwindow, &porttextentry);
117 CTK_WIDGET_ADD(&mainwindow, &connectbutton);
118
119 CTK_WIDGET_ADD(&mainwindow, &sep1);
120
121 CTK_WIDGET_ADD(&mainwindow, &vncbitmap);
122
123 CTK_WIDGET_ADD(&mainwindow, &leftbutton);
124 CTK_WIDGET_ADD(&mainwindow, &upbutton);
125 CTK_WIDGET_ADD(&mainwindow, &downbutton);
126 CTK_WIDGET_ADD(&mainwindow, &rightbutton);
127
128 dispatcher_listen(ctk_signal_window_close);
129 dispatcher_listen(ctk_signal_button_activate);
130 dispatcher_listen(resolv_signal_found);
131
adamdunkels8539c152003-04-17 18:55:38 +0000132 dispatcher_listen(ctk_signal_pointer_move);
adamdunkelse0cc0582003-04-08 19:41:23 +0000133
134 vnc_draw_init();
135 }
136
137 ctk_window_open(&mainwindow);
138
139}
140/*-----------------------------------------------------------------------------------*/
141static void
142show(char *text)
143{
144
145}
146/*-----------------------------------------------------------------------------------*/
147static void
148connect(void)
149{
150 u16_t addr[2], *addrptr;
151 u16_t port;
152 char *cptr;
153
154 /* Find the first space character in host and put a zero there
155 to end the string. */
156 for(cptr = host; *cptr != ' ' && *cptr != 0; ++cptr);
157 *cptr = 0;
158
159 addrptr = &addr[0];
160 if(uip_main_ipaddrconv(host, (unsigned char *)addr) == 0) {
161 addrptr = resolv_lookup(host);
162 if(addrptr == NULL) {
163 resolv_query(host);
164 show("Resolving host...");
165 return;
166 }
167 }
168
169 port = 0;
170 for(cptr = portentry; *cptr != ' ' && *cptr != 0; ++cptr) {
171 if(*cptr < '0' || *cptr > '9') {
172 show("Port number error");
173 return;
174 }
175 port = 10 * port + *cptr - '0';
176 }
177
178
179 vnc_viewer_connect(addrptr, port);
180
181 show("Connecting...");
182
183}
184/*-----------------------------------------------------------------------------------*/
adamdunkels78c03dc2003-04-09 13:45:05 +0000185static
186DISPATCHER_SIGHANDLER(vnc_sighandler, s, data)
adamdunkelse0cc0582003-04-08 19:41:23 +0000187{
adamdunkels8539c152003-04-17 18:55:38 +0000188 unsigned short x, y;
189 unsigned char xc, yc;
adamdunkels78c03dc2003-04-09 13:45:05 +0000190 DISPATCHER_SIGHANDLER_ARGS(s, data);
191
adamdunkelse0cc0582003-04-08 19:41:23 +0000192 if(s == ctk_signal_button_activate) {
193 if(data == (ek_data_t)&connectbutton) {
194 connect();
195 }
196 } else if(s == ctk_signal_window_close) {
197 dispatcher_exit(&p);
198 id = EK_ID_NONE;
199 LOADER_UNLOAD();
200 } else if(s == resolv_signal_found) {
201 if(strcmp(data, host) == 0) {
202 if(resolv_lookup(host) != NULL) {
203 connect();
204 } else {
205 show("Host not found");
206 }
207 }
adamdunkels8539c152003-04-17 18:55:38 +0000208 } else if(s == ctk_signal_pointer_move) {
209 /* Check if pointer is within the VNC viewer area */
210 x = ctk_mouse_x();
211 y = ctk_mouse_y();
212
213 xc = ctk_mouse_xtoc(x);
214 yc = ctk_mouse_ytoc(y);
215
216 if(xc >= 2 && yc >= 2 &&
217 xc < 2 + VNC_CONF_VIEWPORT_WIDTH / 8 &&
218 yc < 2 + VNC_CONF_VIEWPORT_HEIGHT / 8) {
219
220 VNC_VIEWER_POST_POINTER_EVENT(x, y, 0);
221 }
222
adamdunkelse0cc0582003-04-08 19:41:23 +0000223 }
224}
225/*-----------------------------------------------------------------------------------*/
226void
227vnc_viewer_refresh(void)
228{
229 CTK_WIDGET_REDRAW(&vncbitmap);
230}
231/*-----------------------------------------------------------------------------------*/