adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2002, 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 | ca9ddcb | 2003-03-19 14:13:31 +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 desktop environment |
| 31 | * |
oliverschmidt | 35514d5 | 2005-06-12 23:44:29 +0000 | [diff] [blame] | 32 | * $Id: www.c,v 1.31 2005/06/12 23:44:30 oliverschmidt Exp $ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 33 | * |
| 34 | */ |
| 35 | |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 36 | #include <string.h> |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 37 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 38 | #include "ek.h" |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 39 | #include "ctk.h" |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 40 | #include "webclient.h" |
| 41 | #include "htmlparser.h" |
| 42 | #include "http-strings.h" |
| 43 | #include "resolv.h" |
| 44 | |
| 45 | #include "petsciiconv.h" |
| 46 | |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 47 | #include "program-handler.h" |
| 48 | |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 49 | #include "uiplib.h" |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 50 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 51 | #include "tcpip.h" |
| 52 | |
adamdunkels | 8af703e | 2003-04-08 11:50:20 +0000 | [diff] [blame] | 53 | #include "loader.h" |
| 54 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 55 | #include "www-conf.h" |
| 56 | |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 57 | #if 1 |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 58 | #define PRINTF(x) |
| 59 | #else |
| 60 | #include <stdio.h> |
| 61 | #define PRINTF(x) printf x |
| 62 | #endif |
| 63 | |
| 64 | |
| 65 | /* The array that holds the current URL. */ |
| 66 | static char url[WWW_CONF_MAX_URLLEN + 1]; |
| 67 | static char tmpurl[WWW_CONF_MAX_URLLEN + 1]; |
| 68 | |
| 69 | /* The array that holds the web page text. */ |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 70 | static char webpage[WWW_CONF_WEBPAGE_WIDTH * |
| 71 | WWW_CONF_WEBPAGE_HEIGHT + 1]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 72 | |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 73 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 74 | /* The CTK widgets for the main window. */ |
| 75 | static struct ctk_window mainwindow; |
| 76 | |
| 77 | static struct ctk_button backbutton = |
| 78 | {CTK_BUTTON(0, 0, 4, "Back")}; |
| 79 | static struct ctk_button downbutton = |
| 80 | {CTK_BUTTON(10, 0, 4, "Down")}; |
| 81 | static struct ctk_button stopbutton = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 82 | {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 16, 0, 4, "Stop")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 83 | static struct ctk_button gobutton = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 84 | {CTK_BUTTON(WWW_CONF_WEBPAGE_WIDTH - 4, 0, 2, "Go")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 85 | |
| 86 | static struct ctk_separator sep1 = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 87 | {CTK_SEPARATOR(0, 2, WWW_CONF_WEBPAGE_WIDTH)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 88 | |
| 89 | static char editurl[WWW_CONF_MAX_URLLEN + 1]; |
| 90 | static struct ctk_textentry urlentry = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 91 | {CTK_TEXTENTRY(0, 1, WWW_CONF_WEBPAGE_WIDTH - 2, |
| 92 | 1, editurl, WWW_CONF_MAX_URLLEN)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 93 | static struct ctk_label webpagelabel = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 94 | {CTK_LABEL(0, 3, WWW_CONF_WEBPAGE_WIDTH, |
| 95 | WWW_CONF_WEBPAGE_HEIGHT, webpage)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 96 | |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 97 | static char statustexturl[WWW_CONF_WEBPAGE_WIDTH]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 98 | static struct ctk_label statustext = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 99 | {CTK_LABEL(0, WWW_CONF_WEBPAGE_HEIGHT + 4, |
| 100 | WWW_CONF_WEBPAGE_WIDTH, 1, "")}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 101 | static struct ctk_separator sep2 = |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 102 | {CTK_SEPARATOR(0, WWW_CONF_WEBPAGE_HEIGHT + 3, |
| 103 | WWW_CONF_WEBPAGE_WIDTH)}; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 104 | |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 105 | static struct ctk_window wgetdialog; |
| 106 | static struct ctk_label wgetlabel1 = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 107 | {CTK_LABEL(1, 1, 34, 1, "This web page cannot be displayed.")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 108 | static struct ctk_label wgetlabel2 = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 109 | {CTK_LABEL(1, 3, 35, 1, "Would you like to download instead?")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 110 | static struct ctk_button wgetnobutton = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 111 | {CTK_BUTTON(1, 5, 6, "Cancel")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 112 | static struct ctk_button wgetyesbutton = |
adamdunkels | 77b7a69 | 2003-08-29 20:36:23 +0000 | [diff] [blame] | 113 | {CTK_BUTTON(11, 5, 24, "Close browser & download")}; |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 114 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 115 | /* The char arrays that hold the history of visited URLs. */ |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 116 | static char history[WWW_CONF_HISTORY_SIZE][WWW_CONF_MAX_URLLEN]; |
oliverschmidt | 10f4b8d | 2005-02-07 23:13:00 +0000 | [diff] [blame] | 117 | static char history_last; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 118 | |
| 119 | |
| 120 | /* The CTK widget definitions for the hyperlinks and the char arrays |
| 121 | that hold the link URLs. */ |
| 122 | struct formattribs { |
| 123 | char formaction[WWW_CONF_MAX_FORMACTIONLEN]; |
| 124 | char formname[WWW_CONF_MAX_FORMNAMELEN]; |
| 125 | #define FORMINPUTTYPE_SUBMITBUTTON 1 |
| 126 | #define FORMINPUTTYPE_INPUTFIELD 2 |
| 127 | unsigned char inputtype; |
| 128 | char inputname[WWW_CONF_MAX_INPUTNAMELEN]; |
| 129 | char *inputvalue; |
| 130 | }; |
| 131 | |
| 132 | union pagewidgetattrib { |
| 133 | char url[WWW_CONF_MAX_URLLEN]; |
adamdunkels | d59dadf | 2003-08-15 18:48:35 +0000 | [diff] [blame] | 134 | #if WWW_CONF_FORMS |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 135 | struct formattribs form; |
adamdunkels | d59dadf | 2003-08-15 18:48:35 +0000 | [diff] [blame] | 136 | #endif /* WWW_CONF_FORMS */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 137 | }; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 138 | static struct ctk_widget pagewidgets[WWW_CONF_MAX_NUMPAGEWIDGETS]; |
| 139 | static union pagewidgetattrib pagewidgetattribs[WWW_CONF_MAX_NUMPAGEWIDGETS]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 140 | static unsigned char pagewidgetptr; |
| 141 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 142 | #if WWW_CONF_RENDERSTATE |
| 143 | static unsigned char renderstate; |
| 144 | #endif /* WWW_CONF_RENDERSTATE */ |
| 145 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 146 | #define ISO_nl 0x0a |
| 147 | #define ISO_space 0x20 |
| 148 | #define ISO_ampersand 0x26 |
| 149 | #define ISO_plus 0x2b |
| 150 | #define ISO_slash 0x2f |
| 151 | #define ISO_eq 0x3d |
| 152 | #define ISO_questionmark 0x3f |
| 153 | |
| 154 | /* The state of the rendering code. */ |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 155 | static char *webpageptr; |
| 156 | static unsigned char x, y; |
| 157 | static unsigned char loading; |
| 158 | static unsigned short firsty, pagey; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 159 | |
| 160 | static unsigned char count; |
| 161 | static char receivingmsgs[4][23] = { |
| 162 | "Receiving web page ...", |
| 163 | "Receiving web page. ..", |
| 164 | "Receiving web page.. .", |
| 165 | "Receiving web page... " |
| 166 | }; |
| 167 | |
| 168 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 169 | EK_EVENTHANDLER(www_eventhandler, ev, data); |
| 170 | EK_PROCESS(p, "Web browser", EK_PRIO_NORMAL, |
| 171 | www_eventhandler, NULL, NULL); |
| 172 | static ek_id_t id = EK_ID_NONE; |
| 173 | |
| 174 | /*static DISPATCHER_SIGHANDLER(www_sighandler, s, data); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 175 | static struct dispatcher_proc p = |
adamdunkels | 78c03dc | 2003-04-09 13:45:05 +0000 | [diff] [blame] | 176 | {DISPATCHER_PROC("Web browser", NULL, www_sighandler, webclient_appcall)}; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 177 | static ek_id_t id;*/ |
| 178 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 179 | |
| 180 | |
| 181 | static void formsubmit(struct formattribs *attribs); |
| 182 | /*-----------------------------------------------------------------------------------*/ |
| 183 | /* make_window() |
| 184 | * |
| 185 | * Creates the web browser's window. |
| 186 | */ |
| 187 | static void |
| 188 | make_window(void) |
| 189 | { |
| 190 | |
| 191 | CTK_WIDGET_ADD(&mainwindow, &backbutton); |
| 192 | CTK_WIDGET_ADD(&mainwindow, &downbutton); |
| 193 | CTK_WIDGET_ADD(&mainwindow, &stopbutton); |
| 194 | CTK_WIDGET_ADD(&mainwindow, &gobutton); |
| 195 | CTK_WIDGET_ADD(&mainwindow, &urlentry); |
| 196 | CTK_WIDGET_ADD(&mainwindow, &sep1); |
| 197 | CTK_WIDGET_ADD(&mainwindow, &webpagelabel); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 198 | CTK_WIDGET_SET_FLAG(&webpagelabel, CTK_WIDGET_FLAG_MONOSPACE); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 199 | CTK_WIDGET_ADD(&mainwindow, &sep2); |
| 200 | CTK_WIDGET_ADD(&mainwindow, &statustext); |
| 201 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 202 | pagewidgetptr = 0; |
| 203 | } |
| 204 | /*-----------------------------------------------------------------------------------*/ |
| 205 | /* redraw_window(): |
| 206 | * |
| 207 | * Convenience function that calls upon CTK to redraw the browser |
| 208 | * window. */ |
| 209 | static void |
| 210 | redraw_window(void) |
| 211 | { |
| 212 | ctk_window_redraw(&mainwindow); |
| 213 | } |
| 214 | /*-----------------------------------------------------------------------------------*/ |
| 215 | /* www_init(); |
| 216 | * |
| 217 | * Initializes and starts the web browser. Called either at startup or |
| 218 | * to open the browser window. |
| 219 | */ |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 220 | LOADER_INIT_FUNC(www_init, arg) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 221 | { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 222 | arg_free(arg); |
| 223 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 224 | if(id == EK_ID_NONE) { |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 225 | /* id = dispatcher_start(&p);*/ |
| 226 | id = ek_start(&p); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 227 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 228 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 229 | } |
| 230 | /*-----------------------------------------------------------------------------------*/ |
| 231 | static void |
| 232 | clear_page(void) |
| 233 | { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 234 | ctk_window_clear(&mainwindow); |
| 235 | make_window(); |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 236 | redraw_window(); |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 237 | memset(webpage, 0, WWW_CONF_WEBPAGE_WIDTH * WWW_CONF_WEBPAGE_HEIGHT); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 238 | } |
| 239 | /*-----------------------------------------------------------------------------------*/ |
| 240 | static void |
| 241 | show_url(void) |
| 242 | { |
| 243 | memcpy(editurl, url, WWW_CONF_MAX_URLLEN); |
| 244 | strncpy(editurl, "http://", 7); |
| 245 | petsciiconv_topetscii(editurl + 7, WWW_CONF_MAX_URLLEN - 7); |
| 246 | CTK_WIDGET_REDRAW(&urlentry); |
| 247 | } |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 248 | static void |
| 249 | start_loading(void) |
| 250 | { |
| 251 | loading = 1; |
| 252 | x = y = 0; |
| 253 | pagey = 0; |
| 254 | webpageptr = webpage; |
| 255 | |
| 256 | clear_page(); |
| 257 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 258 | /*-----------------------------------------------------------------------------------*/ |
| 259 | static void |
| 260 | show_statustext(char *text) |
| 261 | { |
| 262 | ctk_label_set_text(&statustext, text); |
| 263 | CTK_WIDGET_REDRAW(&statustext); |
| 264 | } |
| 265 | /*-----------------------------------------------------------------------------------*/ |
| 266 | /* open_url(): |
| 267 | * |
| 268 | * Called when the URL present in the global "url" variable should be |
| 269 | * opened. It will call the hostname resolver as well as the HTTP |
| 270 | * client requester. |
| 271 | */ |
| 272 | static void |
| 273 | open_url(void) |
| 274 | { |
| 275 | unsigned char i; |
| 276 | static char host[32]; |
| 277 | char *file; |
| 278 | register char *urlptr; |
adamdunkels | 75c276c | 2003-09-04 19:35:32 +0000 | [diff] [blame] | 279 | static u16_t addr[2]; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 280 | |
| 281 | /* Trim off any spaces in the end of the url. */ |
| 282 | urlptr = url + strlen(url) - 1; |
| 283 | while(*urlptr == ' ' && urlptr > url) { |
| 284 | *urlptr = 0; |
| 285 | --urlptr; |
| 286 | } |
| 287 | |
| 288 | /* Don't even try to go further if the URL is empty. */ |
| 289 | if(urlptr == url) { |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | /* See if the URL starts with http://, otherwise prepend it. */ |
| 294 | if(strncmp(url, http_http, 7) != 0) { |
| 295 | while(urlptr >= url) { |
| 296 | *(urlptr + 7) = *urlptr; |
| 297 | --urlptr; |
| 298 | } |
| 299 | strncpy(url, http_http, 7); |
| 300 | } |
| 301 | |
| 302 | /* Find host part of the URL. */ |
| 303 | urlptr = &url[7]; |
| 304 | for(i = 0; i < sizeof(host); ++i) { |
| 305 | if(*urlptr == 0 || |
| 306 | *urlptr == '/' || |
| 307 | *urlptr == ' ' || |
| 308 | *urlptr == ':') { |
| 309 | host[i] = 0; |
| 310 | break; |
| 311 | } |
| 312 | host[i] = *urlptr; |
| 313 | ++urlptr; |
| 314 | } |
| 315 | |
| 316 | /* XXX: Here we should find the port part of the URL, but this isn't |
| 317 | currently done because of laziness from the programmer's side |
| 318 | :-) */ |
| 319 | |
| 320 | /* Find file part of the URL. */ |
| 321 | while(*urlptr != '/' && *urlptr != 0) { |
| 322 | ++urlptr; |
| 323 | } |
| 324 | if(*urlptr == '/') { |
| 325 | file = urlptr; |
| 326 | } else { |
| 327 | file = "/"; |
| 328 | } |
| 329 | |
| 330 | /* Try to lookup the hostname. If it fails, we initiate a hostname |
| 331 | lookup and print out an informative message on the statusbar. */ |
adamdunkels | db300d2 | 2004-02-24 09:57:49 +0000 | [diff] [blame] | 332 | if(uiplib_ipaddrconv(host, (unsigned char *)addr) == 0) { |
adamdunkels | 75c276c | 2003-09-04 19:35:32 +0000 | [diff] [blame] | 333 | if(resolv_lookup(host) == NULL) { |
| 334 | resolv_query(host); |
| 335 | show_statustext("Resolving host..."); |
| 336 | return; |
| 337 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /* The hostname we present in the hostname table, so we send out the |
| 341 | initial GET request. */ |
| 342 | if(webclient_get(host, 80, file) == 0) { |
| 343 | show_statustext("Out of memory error."); |
| 344 | } else { |
| 345 | show_statustext("Connecting..."); |
| 346 | } |
| 347 | redraw_window(); |
| 348 | } |
| 349 | /*-----------------------------------------------------------------------------------*/ |
| 350 | /* open_link(link): |
| 351 | * |
| 352 | * Will format a link from the current web pages so that it suits the |
| 353 | * open_url() function and finally call it to open the requested URL. |
| 354 | */ |
| 355 | static void |
| 356 | open_link(char *link) |
| 357 | { |
| 358 | char *urlptr; |
| 359 | |
| 360 | if(strncmp(link, http_http, 7) == 0) { |
| 361 | /* The link starts with http://. We just copy the contents of the |
| 362 | link into the url string and jump away. */ |
| 363 | strncpy(url, link, WWW_CONF_MAX_URLLEN); |
| 364 | } else if(*link == ISO_slash && |
| 365 | *(link + 1) == ISO_slash) { |
| 366 | /* The link starts with //, so we'll copy it into the url |
| 367 | variable, starting after the http (which already is present in |
| 368 | the url variable since we were able to open the web page on |
| 369 | which this link was found in the first place). */ |
| 370 | strncpy(&url[5], link, WWW_CONF_MAX_URLLEN); |
| 371 | } else if(*link == ISO_slash) { |
| 372 | /* The link starts with a slash, so it is a non-relative link |
| 373 | within the same web site. We find the start of the filename of |
| 374 | the current URL and paste the contents of this link there, and |
| 375 | head off to the new URL. */ |
| 376 | for(urlptr = &url[7]; |
| 377 | *urlptr != 0 && *urlptr != ISO_slash; |
| 378 | ++urlptr); |
| 379 | strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url)); |
| 380 | } else { |
| 381 | /* A fully relative link is found. We find the last slash in the |
| 382 | current URL and paste the link there. */ |
| 383 | |
| 384 | /* XXX: we should really parse any ../ in the link as well. */ |
| 385 | for(urlptr = url + strlen(url); |
| 386 | urlptr != url && *urlptr != ISO_slash; |
| 387 | --urlptr); |
| 388 | ++urlptr; |
| 389 | strncpy(urlptr, link, WWW_CONF_MAX_URLLEN - (urlptr - url)); |
| 390 | } |
| 391 | |
| 392 | /* Open the URL. */ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 393 | show_url(); |
| 394 | open_url(); |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 395 | |
| 396 | |
| 397 | start_loading(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 398 | } |
| 399 | /*-----------------------------------------------------------------------------------*/ |
| 400 | /* log_back(): |
| 401 | * |
| 402 | * Copies the current URL from the url variable and into the log for |
| 403 | * the back button. |
| 404 | */ |
| 405 | static void |
| 406 | log_back(void) |
| 407 | { |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 408 | if(strncmp(url, history[(int)history_last], WWW_CONF_MAX_URLLEN) != 0) { |
| 409 | memcpy(history[(int)history_last], url, WWW_CONF_MAX_URLLEN); |
| 410 | ++history_last; |
| 411 | if(history_last >= WWW_CONF_HISTORY_SIZE) { |
| 412 | history_last = 0; |
| 413 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 417 | static void |
| 418 | quit(void) |
| 419 | { |
| 420 | ctk_window_close(&mainwindow); |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 421 | ek_exit(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 422 | id = EK_ID_NONE; |
| 423 | LOADER_UNLOAD(); |
| 424 | } |
| 425 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 426 | /* www_dispatcher(): |
| 427 | * |
| 428 | * The program's signal dispatcher function. Is called by the ek |
| 429 | * dispatcher whenever a signal arrives. |
| 430 | */ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 431 | /*static |
| 432 | DISPATCHER_SIGHANDLER(www_sighandler, s, data)*/ |
| 433 | EK_EVENTHANDLER(www_eventhandler, ev, data) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 434 | { |
| 435 | static struct ctk_widget *w; |
| 436 | static unsigned char i; |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 437 | static char *argptr; |
| 438 | |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 439 | /* DISPATCHER_SIGHANDLER_ARGS(s, data);*/ |
| 440 | EK_EVENTHANDLER_ARGS(ev, data); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 441 | |
| 442 | |
| 443 | w = (struct ctk_widget *)data; |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 444 | |
| 445 | if(ev == tcpip_event) { |
| 446 | webclient_appcall(data); |
| 447 | } else if(ev == EK_EVENT_INIT) { |
| 448 | /* Create the main window. */ |
| 449 | memset(webpage, 0, sizeof(webpage)); |
| 450 | ctk_window_new(&mainwindow, WWW_CONF_WEBPAGE_WIDTH, |
| 451 | WWW_CONF_WEBPAGE_HEIGHT+5, "Web browser"); |
| 452 | make_window(); |
| 453 | #ifdef WWW_CONF_HOMEPAGE |
| 454 | strncpy(editurl, WWW_CONF_HOMEPAGE, sizeof(editurl)); |
| 455 | #endif /* WWW_CONF_HOMEPAGE */ |
| 456 | CTK_WIDGET_FOCUS(&mainwindow, &urlentry); |
| 457 | |
| 458 | /* Create download dialog.*/ |
| 459 | ctk_dialog_new(&wgetdialog, 38, 7); |
| 460 | CTK_WIDGET_ADD(&wgetdialog, &wgetlabel1); |
| 461 | CTK_WIDGET_ADD(&wgetdialog, &wgetlabel2); |
| 462 | CTK_WIDGET_ADD(&wgetdialog, &wgetnobutton); |
| 463 | CTK_WIDGET_ADD(&wgetdialog, &wgetyesbutton); |
| 464 | |
| 465 | ctk_window_open(&mainwindow); |
| 466 | |
| 467 | } else if(ev == ctk_signal_widget_activate) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 468 | if(w == (struct ctk_widget *)&backbutton) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 469 | firsty = 0; |
| 470 | start_loading(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 471 | |
| 472 | --history_last; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 473 | if(history_last > WWW_CONF_HISTORY_SIZE) { |
| 474 | history_last = WWW_CONF_HISTORY_SIZE - 1; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 475 | } |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 476 | memcpy(url, history[(int)history_last], WWW_CONF_MAX_URLLEN); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 477 | open_url(); |
| 478 | CTK_WIDGET_FOCUS(&mainwindow, &backbutton); |
| 479 | } else if(w == (struct ctk_widget *)&downbutton) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 480 | firsty = pagey + WWW_CONF_WEBPAGE_HEIGHT - 4; |
| 481 | start_loading(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 482 | open_url(); |
| 483 | CTK_WIDGET_FOCUS(&mainwindow, &downbutton); |
adamdunkels | 328e535 | 2003-08-20 20:54:46 +0000 | [diff] [blame] | 484 | } else if(w == (struct ctk_widget *)&gobutton || |
| 485 | w == (struct ctk_widget *)&urlentry) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 486 | start_loading(); |
| 487 | firsty = 0; |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 488 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 489 | log_back(); |
| 490 | memcpy(url, editurl, WWW_CONF_MAX_URLLEN); |
| 491 | petsciiconv_toascii(url, WWW_CONF_MAX_URLLEN); |
| 492 | open_url(); |
| 493 | CTK_WIDGET_FOCUS(&mainwindow, &gobutton); |
| 494 | } else if(w == (struct ctk_widget *)&stopbutton) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 495 | loading = 0; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 496 | webclient_close(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 497 | } else if(w == (struct ctk_widget *)&wgetnobutton) { |
| 498 | ctk_dialog_close(); |
| 499 | } else if(w == (struct ctk_widget *)&wgetyesbutton) { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 500 | ctk_dialog_close(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 501 | quit(); |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 502 | argptr = arg_alloc(WWW_CONF_MAX_URLLEN); |
| 503 | if(argptr != NULL) { |
| 504 | strncpy(argptr, url, WWW_CONF_MAX_URLLEN); |
| 505 | } |
| 506 | program_handler_load("wget.prg", argptr); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 507 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 508 | #if WWW_CONF_FORMS |
| 509 | } else { |
| 510 | /* Check form buttons */ |
| 511 | for(i = 0; i < pagewidgetptr; ++i) { |
| 512 | if(&pagewidgets[i] == w) { |
| 513 | formsubmit(&pagewidgetattribs[i].form); |
| 514 | /* show_statustext(pagewidgetattribs[i].form.formaction);*/ |
| 515 | /* PRINTF(("Formaction %s formname %s inputname %s\n", |
| 516 | pagewidgetattribs[i].form.formaction, |
| 517 | pagewidgetattribs[i].form.formname, |
| 518 | pagewidgetattribs[i].form.inputname));*/ |
| 519 | break; |
| 520 | } |
| 521 | } |
| 522 | #endif /* WWW_CONF_FORMS */ |
| 523 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 524 | } else if(ev == ctk_signal_hyperlink_activate) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 525 | firsty = 0; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 526 | log_back(); |
| 527 | open_link(w->widget.hyperlink.url); |
| 528 | CTK_WIDGET_FOCUS(&mainwindow, &stopbutton); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 529 | /* ctk_window_open(&mainwindow);*/ |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 530 | } else if(ev == ctk_signal_hyperlink_hover) { |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 531 | if(CTK_WIDGET_TYPE((struct ctk_widget *)data) == |
| 532 | CTK_WIDGET_HYPERLINK) { |
adamdunkels | c4bf5ca | 2003-04-18 00:20:22 +0000 | [diff] [blame] | 533 | strncpy(statustexturl, w->widget.hyperlink.url, |
| 534 | sizeof(statustexturl)); |
| 535 | petsciiconv_topetscii(statustexturl, sizeof(statustexturl)); |
| 536 | show_statustext(statustexturl); |
| 537 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 538 | } else if(ev == resolv_event_found) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 539 | /* Either found a hostname, or not. */ |
| 540 | if((char *)data != NULL && |
| 541 | resolv_lookup((char *)data) != NULL) { |
| 542 | open_url(); |
| 543 | } else { |
| 544 | show_statustext("Host not found."); |
| 545 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 546 | } else if(ev == ctk_signal_window_close || |
| 547 | ev == EK_EVENT_REQUEST_EXIT) { |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 548 | quit(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | /*-----------------------------------------------------------------------------------*/ |
| 552 | /* set_url(): |
| 553 | * |
| 554 | * Constructs an URL from the arguments and puts it into the global |
| 555 | * "url" variable and the visible "editurl" (which is shown in the URL |
| 556 | * text entry widget in the browser window). |
| 557 | */ |
| 558 | static void |
| 559 | set_url(char *host, u16_t port, char *file) |
| 560 | { |
| 561 | char *urlptr; |
| 562 | |
| 563 | memset(url, 0, WWW_CONF_MAX_URLLEN); |
| 564 | |
| 565 | if(strncmp(file, http_http, 7) == 0) { |
| 566 | strncpy(url, file, sizeof(url)); |
| 567 | } else { |
| 568 | strncpy(url, http_http, 7); |
| 569 | urlptr = url + 7; |
| 570 | strcpy(urlptr, host); |
| 571 | urlptr += strlen(host); |
| 572 | strcpy(urlptr, file); |
| 573 | } |
| 574 | |
| 575 | show_url(); |
| 576 | } |
| 577 | /*-----------------------------------------------------------------------------------*/ |
| 578 | /* webclient_aborted(): |
| 579 | * |
| 580 | * Callback function. Called from the webclient when the HTTP |
| 581 | * connection was abruptly aborted. |
| 582 | */ |
| 583 | void |
| 584 | webclient_aborted(void) |
| 585 | { |
| 586 | show_statustext("Connection reset by peer"); |
| 587 | } |
| 588 | /*-----------------------------------------------------------------------------------*/ |
| 589 | /* webclient_timedout(): |
| 590 | * |
| 591 | * Callback function. Called from the webclient when the HTTP |
| 592 | * connection timed out. |
| 593 | */ |
| 594 | void |
| 595 | webclient_timedout(void) |
| 596 | { |
| 597 | show_statustext("Connection timed out"); |
| 598 | } |
| 599 | /*-----------------------------------------------------------------------------------*/ |
| 600 | /* webclient_closed(): |
| 601 | * |
| 602 | * Callback function. Called from the webclient when the HTTP |
| 603 | * connection was closed after a request from the "webclient_close()" |
| 604 | * function. . |
| 605 | */ |
| 606 | void |
| 607 | webclient_closed(void) |
| 608 | { |
| 609 | show_statustext("Stopped."); |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 610 | petsciiconv_topetscii(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 611 | WWW_CONF_WEBPAGE_WIDTH], WWW_CONF_WEBPAGE_WIDTH); |
oliverschmidt | bf5bffe | 2005-03-13 22:42:31 +0000 | [diff] [blame] | 612 | CTK_WIDGET_FOCUS(&mainwindow, &downbutton); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 613 | redraw_window(); |
| 614 | } |
| 615 | /*-----------------------------------------------------------------------------------*/ |
| 616 | /* webclient_closed(): |
| 617 | * |
| 618 | * Callback function. Called from the webclient when the HTTP |
| 619 | * connection is connected. |
| 620 | */ |
| 621 | void |
| 622 | webclient_connected(void) |
| 623 | { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 624 | start_loading(); |
| 625 | |
| 626 | clear_page(); |
adamdunkels | 30c449e | 2003-07-31 23:12:05 +0000 | [diff] [blame] | 627 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 628 | show_statustext("Request sent..."); |
| 629 | set_url(webclient_hostname(), webclient_port(), webclient_filename()); |
| 630 | |
| 631 | #if WWW_CONF_RENDERSTATE |
| 632 | renderstate = HTMLPARSER_RENDERSTATE_NONE; |
| 633 | #endif /* WWW_CONF_RENDERSTATE */ |
| 634 | htmlparser_init(); |
| 635 | } |
| 636 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 637 | /* webclient_datahandler(): |
| 638 | * |
| 639 | * Callback function. Called from the webclient module when HTTP data |
| 640 | * has arrived. |
| 641 | */ |
| 642 | void |
| 643 | webclient_datahandler(char *data, u16_t len) |
| 644 | { |
| 645 | if(len > 0) { |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 646 | if(strcmp(webclient_mimetype(), http_texthtml) == 0) { |
| 647 | count = (count + 1) & 3; |
| 648 | show_statustext(receivingmsgs[count]); |
| 649 | htmlparser_parse(data, len); |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 650 | redraw_window(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 651 | } else { |
adamdunkels | 8bb5cca | 2003-08-24 22:41:31 +0000 | [diff] [blame] | 652 | uip_abort(); |
adamdunkels | f6eea75 | 2003-08-22 19:24:40 +0000 | [diff] [blame] | 653 | ctk_dialog_open(&wgetdialog); |
| 654 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 655 | } else { |
| 656 | /* Clear remaining parts of page. */ |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 657 | loading = 0; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | if(data == NULL) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 661 | loading = 0; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 662 | show_statustext("Done."); |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 663 | petsciiconv_topetscii(&webpage[(WWW_CONF_WEBPAGE_HEIGHT - 1) * |
| 664 | WWW_CONF_WEBPAGE_WIDTH], WWW_CONF_WEBPAGE_WIDTH); |
oliverschmidt | ef13b02 | 2005-04-28 21:15:44 +0000 | [diff] [blame] | 665 | CTK_WIDGET_FOCUS(&mainwindow, &urlentry); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 666 | redraw_window(); |
| 667 | } |
| 668 | } |
| 669 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 670 | static void * |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 671 | add_pagewidget(char *text, unsigned char len, unsigned char type, |
| 672 | unsigned char border) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 673 | { |
adamdunkels | d59dadf | 2003-08-15 18:48:35 +0000 | [diff] [blame] | 674 | register struct ctk_widget *lptr; |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 675 | register unsigned char *wptr; |
| 676 | static unsigned char maxwidth; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 677 | static void *dataptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 678 | |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 679 | if(!loading) { |
| 680 | return NULL; |
| 681 | } |
| 682 | |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 683 | if(len + border == 0) { |
| 684 | return NULL; |
| 685 | } |
| 686 | |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 687 | maxwidth = WWW_CONF_WEBPAGE_WIDTH - (1 + 2 * border); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 688 | |
| 689 | /* If the text of the link is too long so that it does not fit into |
| 690 | the width of the current window, counting from the current x |
| 691 | coordinate, we first try to jump to the next line. */ |
| 692 | if(len + x > maxwidth) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 693 | htmlparser_newline(); |
| 694 | if(!loading) { |
| 695 | return NULL; |
| 696 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | /* If the text of the link still is too long, we just chop it off! |
| 700 | XXX: this is not really the right thing to do, we should probably |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 701 | either make a link into a multiline link, or add multiple |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 702 | buttons. But this will do for now. */ |
| 703 | if(len > maxwidth) { |
| 704 | text[maxwidth] = 0; |
| 705 | len = maxwidth; |
| 706 | } |
| 707 | |
| 708 | dataptr = NULL; |
| 709 | |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 710 | if(firsty == pagey) { |
| 711 | wptr = webpageptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 712 | /* To save memory, we'll copy the widget text to the web page |
| 713 | drawing area and reference it from there. */ |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 714 | wptr[0] = 0; |
| 715 | wptr += border; |
| 716 | memcpy(wptr, text, len); |
| 717 | wptr[len] = 0; |
| 718 | wptr[len + border] = ' '; |
adamdunkels | 17e84a3 | 2003-04-02 09:54:39 +0000 | [diff] [blame] | 719 | if(pagewidgetptr < WWW_CONF_MAX_NUMPAGEWIDGETS) { |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 720 | dataptr = &pagewidgetattribs[pagewidgetptr]; |
| 721 | lptr = &pagewidgets[pagewidgetptr]; |
| 722 | |
| 723 | switch(type) { |
| 724 | case CTK_WIDGET_HYPERLINK: |
| 725 | CTK_HYPERLINK_NEW((struct ctk_hyperlink *)lptr, x, |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 726 | y + 3, len, |
| 727 | wptr, dataptr); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 728 | break; |
| 729 | case CTK_WIDGET_BUTTON: |
| 730 | CTK_BUTTON_NEW((struct ctk_button *)lptr, x, |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 731 | y + 3, len, |
| 732 | wptr); |
| 733 | ((struct formattribs *)dataptr)->inputvalue = wptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 734 | break; |
| 735 | case CTK_WIDGET_TEXTENTRY: |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 736 | CTK_TEXTENTRY_NEW((struct ctk_textentry *)lptr, x, |
| 737 | y + 3, len, 1, |
| 738 | wptr, len); |
| 739 | ((struct formattribs *)dataptr)->inputvalue = wptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 740 | break; |
| 741 | } |
adamdunkels | f2f8cb2 | 2004-07-04 11:35:07 +0000 | [diff] [blame] | 742 | CTK_WIDGET_SET_FLAG(lptr, CTK_WIDGET_FLAG_MONOSPACE); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 743 | CTK_WIDGET_ADD(&mainwindow, lptr); |
| 744 | |
| 745 | ++pagewidgetptr; |
| 746 | } |
| 747 | } |
| 748 | /* Increase the x coordinate with the length of the link text plus |
| 749 | the extra space behind it and the CTK button markers. */ |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 750 | len = len + 1 + 2 * border; |
| 751 | x += len; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 752 | |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 753 | if(firsty == pagey) { |
| 754 | webpageptr += len; |
| 755 | } |
| 756 | |
| 757 | if(x == WWW_CONF_WEBPAGE_WIDTH) { |
| 758 | htmlparser_newline(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | return dataptr; |
| 762 | } |
| 763 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 764 | #if WWW_CONF_RENDERSTATE |
| 765 | static void |
| 766 | centerline(char *wptr) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 767 | { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 768 | unsigned char spaces, i; |
| 769 | char *cptr; |
| 770 | register struct ctk_widget *linksptr; |
| 771 | |
oliverschmidt | d11555c | 2005-01-30 20:22:17 +0000 | [diff] [blame] | 772 | cptr = wptr + WWW_CONF_WEBPAGE_WIDTH; |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 773 | for(spaces = 0; spaces < WWW_CONF_WEBPAGE_WIDTH; ++spaces) { |
oliverschmidt | d11555c | 2005-01-30 20:22:17 +0000 | [diff] [blame] | 774 | if(*--cptr != 0) { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 775 | break; |
| 776 | } |
| 777 | } |
| 778 | |
oliverschmidt | d11555c | 2005-01-30 20:22:17 +0000 | [diff] [blame] | 779 | spaces /= 2; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 780 | |
oliverschmidt | d11555c | 2005-01-30 20:22:17 +0000 | [diff] [blame] | 781 | while(cptr >= wptr) { |
| 782 | *(cptr + spaces) = *cptr; |
| 783 | --cptr; |
| 784 | } |
| 785 | |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 786 | memset(wptr, ' ', spaces); |
| 787 | |
| 788 | linksptr = pagewidgets; |
| 789 | |
| 790 | for(i = 0; i < pagewidgetptr; ++i) { |
| 791 | if(CTK_WIDGET_YPOS(linksptr) == y + 2) { |
| 792 | linksptr->x += spaces; |
| 793 | linksptr->widget.hyperlink.text += spaces; |
| 794 | } |
| 795 | ++linksptr; |
| 796 | } |
| 797 | } |
| 798 | #endif /* WWW_CONF_RENDERSTATE */ |
| 799 | /*-----------------------------------------------------------------------------------*/ |
| 800 | void |
| 801 | htmlparser_newline(void) |
| 802 | { |
| 803 | char *wptr; |
| 804 | |
| 805 | if(pagey < firsty) { |
| 806 | ++pagey; |
| 807 | x = 0; |
| 808 | return; |
| 809 | } |
| 810 | |
oliverschmidt | 453510a | 2005-01-21 14:25:27 +0000 | [diff] [blame] | 811 | if(!loading) { |
| 812 | return; |
| 813 | } |
| 814 | |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 815 | webpageptr += (WWW_CONF_WEBPAGE_WIDTH - x); |
| 816 | ++y; |
| 817 | x = 0; |
| 818 | |
| 819 | wptr = webpageptr - WWW_CONF_WEBPAGE_WIDTH; |
| 820 | petsciiconv_topetscii(wptr, |
| 821 | WWW_CONF_WEBPAGE_WIDTH); |
| 822 | #if WWW_CONF_RENDERSTATE |
| 823 | if(renderstate & HTMLPARSER_RENDERSTATE_CENTER) { |
| 824 | centerline(wptr); |
| 825 | } |
| 826 | #endif /* WWW_CONF_RENDERSTATE */ |
| 827 | |
| 828 | if(y == WWW_CONF_WEBPAGE_HEIGHT) { |
| 829 | loading = 0; |
| 830 | webclient_close(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | /*-----------------------------------------------------------------------------------*/ |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 834 | void |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 835 | htmlparser_word(char *word, unsigned char wordlen) |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 836 | { |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 837 | |
| 838 | if(loading) { |
| 839 | if(wordlen + 1 > WWW_CONF_WEBPAGE_WIDTH - x) { |
| 840 | htmlparser_newline(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 841 | } |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 842 | |
| 843 | if(loading) { |
| 844 | if(pagey == firsty) { |
| 845 | memcpy(webpageptr, word, wordlen); |
| 846 | webpageptr += wordlen; |
| 847 | *webpageptr = ' '; |
| 848 | ++webpageptr; |
| 849 | } |
| 850 | x += wordlen + 1; |
| 851 | if(x == WWW_CONF_WEBPAGE_WIDTH) { |
| 852 | htmlparser_newline(); |
| 853 | } |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | /*-----------------------------------------------------------------------------------*/ |
| 858 | void |
| 859 | htmlparser_link(char *text, unsigned char textlen, char *url) |
| 860 | { |
| 861 | static unsigned char *linkurlptr; |
| 862 | |
| 863 | linkurlptr = add_pagewidget(text, textlen, CTK_WIDGET_HYPERLINK, 0); |
| 864 | if(linkurlptr != NULL && |
| 865 | strlen(url) < WWW_CONF_MAX_URLLEN) { |
| 866 | strcpy(linkurlptr, url); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 867 | } |
| 868 | } |
| 869 | /*-----------------------------------------------------------------------------------*/ |
| 870 | #if WWW_CONF_RENDERSTATE |
| 871 | void |
| 872 | htmlparser_renderstate(unsigned char s) |
| 873 | { |
| 874 | if((s & HTMLPARSER_RENDERSTATE_STATUSMASK) == |
| 875 | HTMLPARSER_RENDERSTATE_BEGIN) { |
| 876 | renderstate |= s & ~HTMLPARSER_RENDERSTATE_STATUSMASK; |
| 877 | } else { |
| 878 | renderstate &= ~(s & ~HTMLPARSER_RENDERSTATE_STATUSMASK); |
| 879 | } |
| 880 | } |
| 881 | #endif /* WWW_CONF_RENDERSTATE */ |
| 882 | |
| 883 | #if WWW_CONF_FORMS |
| 884 | /*-----------------------------------------------------------------------------------*/ |
| 885 | void |
| 886 | htmlparser_submitbutton(char *text, char *name, |
| 887 | char *formname, char *formaction) |
| 888 | { |
| 889 | register struct formattribs *form; |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 890 | |
| 891 | form = add_pagewidget(text, strlen(text), CTK_WIDGET_BUTTON, 1); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 892 | if(form != NULL) { |
| 893 | strncpy(form->formaction, formaction, WWW_CONF_MAX_FORMACTIONLEN); |
| 894 | strncpy(form->formname, formname, WWW_CONF_MAX_FORMNAMELEN); |
| 895 | strncpy(form->inputname, name, WWW_CONF_MAX_INPUTNAMELEN); |
| 896 | form->inputtype = FORMINPUTTYPE_SUBMITBUTTON; |
| 897 | } |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 898 | } |
| 899 | /*-----------------------------------------------------------------------------------*/ |
| 900 | void |
oliverschmidt | 35514d5 | 2005-06-12 23:44:29 +0000 | [diff] [blame] | 901 | htmlparser_inputfield(unsigned char size, char *text, char *name, |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 902 | char *formname, char *formaction) |
| 903 | { |
| 904 | register struct formattribs *form; |
adamdunkels | 269d7be | 2004-09-03 09:55:22 +0000 | [diff] [blame] | 905 | |
oliverschmidt | 35514d5 | 2005-06-12 23:44:29 +0000 | [diff] [blame] | 906 | form = add_pagewidget(text, size, CTK_WIDGET_TEXTENTRY, 1); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 907 | if(form != NULL) { |
| 908 | strncpy(form->formaction, formaction, WWW_CONF_MAX_FORMACTIONLEN); |
| 909 | strncpy(form->formname, formname, WWW_CONF_MAX_FORMNAMELEN); |
| 910 | strncpy(form->inputname, name, WWW_CONF_MAX_INPUTNAMELEN); |
| 911 | form->inputtype = FORMINPUTTYPE_INPUTFIELD; |
| 912 | } |
| 913 | } |
| 914 | /*-----------------------------------------------------------------------------------*/ |
| 915 | static void |
| 916 | formsubmit(struct formattribs *attribs) |
| 917 | { |
| 918 | unsigned char i, j; |
adamdunkels | 1a23b5b | 2003-08-05 13:50:02 +0000 | [diff] [blame] | 919 | register char *urlptr, *valueptr; |
| 920 | register struct formattribs *faptr; |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 921 | |
| 922 | urlptr = &tmpurl[0]; |
| 923 | |
| 924 | strncpy(urlptr, attribs->formaction, WWW_CONF_MAX_URLLEN); |
| 925 | tmpurl[WWW_CONF_MAX_URLLEN] = 0; |
| 926 | urlptr += strlen(urlptr); |
| 927 | *urlptr = ISO_questionmark; |
| 928 | ++urlptr; |
| 929 | |
| 930 | |
| 931 | /* Construct an URL by finding all input field forms with the same |
| 932 | formname as the current submit button, and add the submit button |
| 933 | URL stuff as well. */ |
| 934 | for(i = 0; i < pagewidgetptr; ++i) { |
| 935 | if(urlptr - &tmpurl[0] >= WWW_CONF_MAX_URLLEN) { |
| 936 | break; |
| 937 | } |
| 938 | |
| 939 | faptr = &pagewidgetattribs[i].form; |
| 940 | |
| 941 | if(strcmp(attribs->formaction, faptr->formaction) == 0 && |
| 942 | strcmp(attribs->formname, faptr->formname) == 0 && |
| 943 | (faptr->inputtype == FORMINPUTTYPE_INPUTFIELD || |
| 944 | faptr == attribs)) { |
| 945 | |
| 946 | /* Copy the name of the input field into the URL and append a |
| 947 | questionmark. */ |
| 948 | strncpy(urlptr, faptr->inputname, WWW_CONF_MAX_URLLEN - strlen(tmpurl)); |
| 949 | tmpurl[WWW_CONF_MAX_URLLEN] = 0; |
| 950 | urlptr += strlen(urlptr); |
| 951 | *urlptr = ISO_eq; |
| 952 | ++urlptr; |
| 953 | |
| 954 | /* Convert and copy the contents of the input field to the URL |
| 955 | and append an ampersand. */ |
| 956 | valueptr = pagewidgets[i].widget.textentry.text; |
| 957 | petsciiconv_toascii(valueptr, WWW_CONF_MAX_INPUTVALUELEN); |
| 958 | for(j = 0; j < WWW_CONF_MAX_INPUTVALUELEN; ++j) { |
| 959 | if(urlptr - &tmpurl[0] >= WWW_CONF_MAX_URLLEN) { |
| 960 | break; |
| 961 | } |
| 962 | *urlptr = *valueptr; |
| 963 | if(*urlptr == ISO_space) { |
| 964 | *urlptr = ISO_plus; |
| 965 | } |
| 966 | if(*urlptr == 0) { |
| 967 | break; |
| 968 | } |
| 969 | ++urlptr; |
| 970 | ++valueptr; |
| 971 | } |
| 972 | |
| 973 | *urlptr = ISO_ampersand; |
| 974 | ++urlptr; |
| 975 | } |
| 976 | } |
| 977 | --urlptr; |
| 978 | *urlptr = 0; |
adamdunkels | 89c1f4e | 2003-08-09 13:33:25 +0000 | [diff] [blame] | 979 | log_back(); |
adamdunkels | ca9ddcb | 2003-03-19 14:13:31 +0000 | [diff] [blame] | 980 | open_link(tmpurl); |
| 981 | } |
| 982 | /*-----------------------------------------------------------------------------------*/ |
| 983 | #endif /* WWW_CONF_FORMS */ |