Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Primary module |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
c46caad7e99d05b4e2376a9fd97ce65b |
User & Date: | murphy 2018-08-31 09:22:32.396 |
Context
2018-08-31
| ||
10:59 | API Documentation check-in: 92945aef40 user: murphy tags: trunk | |
09:22 | Primary module check-in: c46caad7e9 user: murphy tags: trunk | |
2018-08-30
| ||
23:27 | Initial working code check-in: a8e6226dd4 user: murphy tags: trunk | |
Changes
Added webview.scm.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | ;; ;; MIT License ;; ;; Copyright (c) 2018 Thomas Chust ;; ;; Permission is hereby granted, free of charge, to any person obtaining a copy ;; of this software and associated documentation files (the "Software"), to deal ;; in the Software without restriction, including without limitation the rights ;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ;; copies of the Software, and to permit persons to whom the Software is ;; furnished to do so, subject to the following conditions: ;; ;; The above copyright notice and this permission notice shall be included in ;; all copies or substantial portions of the Software. ;; ;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ;; SOFTWARE. ;; (module webview (webview? webview webview-terminate! webview-title-set! webview-fullscreen-set! webview-eval webview-inject webview-dialog webview-log char-set:no-js-escape write-js html-tag-rule write-html) (import scheme (chicken base) (chicken condition) (chicken fixnum) (chicken bitwise) (chicken foreign) (only (chicken port) call-with-output-string) (only (chicken memory) tagged-pointer? tag-pointer) (only (chicken gc) set-finalizer!) (only srfi-1 every) (prefix (only utf8 string-length string-ref display) utf8-) (only srfi-13 string-trim string-pad) utf8-srfi-14 (prefix unicode-char-sets unicode-) srfi-18 srfi-69) (foreign-declare "#include <webview.h>") (define (type-error location message . arguments) (abort (make-composite-condition (make-property-condition 'exn 'location location 'message message 'arguments arguments) (make-property-condition 'type)))) (define tag:webview (gensym 'webview)) (define webview? (cut tagged-pointer? <> tag:webview)) (define-foreign-type webview (c-pointer "webview_t") (lambda (v) (and v (if (webview? v) v (type-error 'webview "bad argument type" v)))) (lambda (v) (and v (tag-pointer v tag:webview)))) (define-foreign-type nonnull-webview (nonnull-c-pointer "webview_t") (lambda (v) (if (webview? v) v (type-error 'webview "bad argument type" v))) (lambda (v) (tag-pointer v tag:webview))) (define webview-callbacks (make-mutex 'webview-callbacks)) (mutex-specific-set! webview-callbacks (make-hash-table equal? equal?-hash)) (define-external (on_webview_external_invoke [nonnull-webview view] [(const c-string) data]) void (mutex-lock! webview-callbacks) (let ([proc (hash-table-ref (mutex-specific webview-callbacks) view)]) (mutex-unlock! webview-callbacks) (when proc (handle-exceptions exn (webview-log (string-trim (call-with-output-string (cut print-error-message exn <> "Callback Error")))) (proc view data))))) (define (webview title proc #!key [url #f] [width 800] [height 600] [resizable? #t] [debug? #f]) (letrec ([view ((foreign-lambda webview "webview_new" nonnull-c-string c-string int int bool bool) title url width height resizable? debug?)] [loop (lambda () (when (zero? ((foreign-safe-lambda int "webview_loop" nonnull-webview bool) view #t)) (loop)))]) (if view (set-finalizer! view (foreign-lambda void "webview_destroy" nonnull-webview)) (error 'webview "initialization failed")) (when proc (mutex-lock! webview-callbacks) (hash-table-set! (mutex-specific webview-callbacks) view proc) (mutex-unlock! webview-callbacks) ((foreign-lambda* void ([nonnull-webview view]) "webview_set_external_invoke_cb(view, on_webview_external_invoke);") view)) (loop))) (define webview-terminate! (foreign-safe-lambda void "webview_terminate" nonnull-webview)) (define webview-title-set! (foreign-safe-lambda void "webview_set_title" nonnull-webview nonnull-c-string)) (define webview-fullscreen-set! (foreign-safe-lambda void "webview_set_fullscreen" nonnull-webview bool)) (define (webview-eval view script) (unless (zero? ((foreign-safe-lambda int "webview_eval" nonnull-webview nonnull-c-string) view script)) (error 'webview-eval "JavaScript evaluation failed"))) (define (webview-inject view id html) (webview-eval view (call-with-output-string (lambda (port) (display "document.getElementById(" port) (write-js id port) (display ").innerHTML = " port) (write-js (call-with-output-string (cut write-html html <>)) port) (display ";" port))))) (define (webview-dialog view title #!optional value #!rest type) (let ([type (cond [(memq #:open type) (foreign-value "WEBVIEW_DIALOG_TYPE_OPEN" int)] [(memq #:save type) (foreign-value "WEBVIEW_DIALOG_TYPE_SAVE" int)] [else (foreign-value "WEBVIEW_DIALOG_TYPE_ALERT" int)])] [flags (bitwise-ior (if (memq #:directory type) (foreign-value "WEBVIEW_DIALOG_FLAG_DIRECTORY" int) (foreign-value "WEBVIEW_DIALOG_FLAG_FILE" int)) (cond [(memq #:error type) (foreign-value "WEBVIEW_DIALOG_FLAG_ERROR" int)] [(memq #:warning type) (foreign-value "WEBVIEW_DIALOG_FLAG_WARNING" int)] [else (foreign-value "WEBVIEW_DIALOG_FLAG_INFO" int)]))] [buf (make-string 4096 #\nul)]) ((foreign-safe-lambda void "webview_dialog" nonnull-webview int int nonnull-c-string c-string scheme-pointer size_t) view type flags title value buf (string-length buf)) (let scan ([pos 0]) (if (or (fx>= pos (string-length buf)) (eqv? (string-ref buf pos) #\nul)) (and (fx> pos 0) (substring buf 0 (fx- pos 1))) (scan (fx+ pos 1)))))) (define webview-log (foreign-lambda void "webview_print_log" nonnull-c-string)) (define alist:js-escape '((#\' . #\') (#\" . #\") (#\\ . #\\) (#\backspace . #\b) (#\tab . #\t) (#\newline . #\n) (#\return . #\r))) (define char-set:no-js-escape (char-set-difference (char-set-union unicode-char-set:alphabetic char-set:digit char-set:punctuation char-set:symbol char-set:blank) (list->char-set (map car alist:js-escape)))) (define (write-js s #!optional [port (current-output-port)] [quotes #\"]) (let ([n (utf8-string-length s)]) (cond [quotes => (cut utf8-display <> port)]) (do ([i 0 (fx+ i 1)]) ((fx>= i n)) (let ([c (utf8-string-ref s i)]) (cond [(char-set-contains? char-set:no-js-escape c) (utf8-display c port)] [(assv c alist:js-escape) => (lambda (r) (utf8-display #\\ port) (utf8-display (cdr r) port))] [else (utf8-display "\\u" port) (utf8-display (string-pad (number->string (char->integer c) 16) 4 #\0) port)]))) (cond [quotes => (cut utf8-display <> port)]))) (define html-tag-rule (let ([tag-rules (make-hash-table eq? symbol-hash)]) (case-lambda [(tag) (hash-table-ref/default tag-rules tag 'normal)] [(tag rule) (cond [(not (memq rule '(void raw normal))) (error 'html-tag-rule "invalid tag rule" tag rule)] [(eq? rule 'normal) (hash-table-delete! tag-rules tag) (void)] [else (hash-table-set! tag-rules tag rule)])]))) (for-each (lambda (tag&rule) (html-tag-rule (car tag&rule) (cdr tag&rule))) '([area . void] [base . void] [br . void] [col . void] [command . void] [embed . void] [hr . void] [img . void] [input . void] [keygen . void] [link . void] [meta . void] [param . void] [source . void] [track . void] [wbr . void] [script . raw] [style . raw])) (define write-html (letrec ([write-escaped (lambda (s port) (let ([n (string-length s)]) (do ([i 0 (fx+ i 1)]) ((fx>= i n)) (let ([c (string-ref s i)]) (display (case c [(#\") """] [(#\&) "&"] [(#\<) "<"] [(#\>) ">"] [else c]) port)))))] [write-element (lambda (elt port) (unless (and (pair? elt) (symbol? (car elt)) (list? (cdr elt))) (error 'write-html "not a proper element" elt)) (let ([tag (car elt)] [attributes&contents (cdr elt)]) (when (eq? tag 'html) (display "<!DOCTYPE html>" port) (newline port)) (display #\< port) (display tag port) (let-values ([(rule) (html-tag-rule tag)] [(attributes contents) (cond [(null? attributes&contents) (values '() '())] [(and (list? (car attributes&contents)) (every pair? (car attributes&contents))) (values (car attributes&contents) (cdr attributes&contents))] [else (values '() attributes&contents)])]) (for-each (cut write-attribute <> port) attributes) (display #\> port) (case rule [(normal) (for-each (cut write-content #t <> port) contents)] [(raw) (for-each (cut write-content #f <> port) contents)] [(void) (unless (null? contents) (error 'write-html "void elements cannot have contents" elt))]) (case rule [(normal raw) (display "</" port) (display tag port) (display #\> port)])) (when (eq? tag 'html) (newline port))))] [write-attribute (lambda (attr port) (unless (and (pair? attr) (symbol? (car attr))) (error 'write-html "not a proper attribute" attr)) (let ([key (car attr)] [v (cdr attr)]) (cond [(not v) (void)] [(boolean? v) (display #\space port) (display key port)] [else (display #\space port) (display key port) (display "=\"" port) (if (list? v) (for-each (cut write-content #f <> port) v) (write-content #f v port)) (display #\" port)])))] [write-content (lambda (allow-elements? v port) (cond [(symbol? v) (display #\& port) (display v port) (display #\; port)] [(and (integer? v) (positive? v)) (display "&#" port) (display v port) (display #\; port)] [(string? v) (write-escaped v port)] [allow-elements? (write-element v port)] [else (error 'write-html "element not allowed in this context" v)]))]) (lambda (html #!optional [port (current-output-port)]) (write-element html port)))) ) ;; vim: set ai et ts=4 sts=2 sw=2 ft=scheme: ;; |