lib.scm 31.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
;; Starwisp Copyright (C) 2013 Dave Griffiths
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as
;; published by the Free Software Foundation, either version 3 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU Affero General Public License for more details.
;;
;; You should have received a copy of the GNU Affero General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

16
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; debugging and unit tests
18

Dave Griffiths's avatar
Dave Griffiths committed
19
20
21
22
23
24
25
26
(define (msg . args)
  (for-each
   (lambda (i) (display i)(display " "))
   args)
  (newline))

(define (dbg i) (msg i) i)

27
28
29
30
31
32
33
34
35
36
37
38
39
(define (assert msg v)
  (display (string-append "testing " msg))(newline)
  (when (not v)
        (error "unit " msg)))

(define (asserteq msg a b)
  (display (string-append "testing " msg))(newline)
  (when (not (equal? a b))
        (error "unit " msg a b)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; list stuff

40
41
42
43
44
45
46
47
48
49
50
51
52
(define (filter fn l)
  (foldl
   (lambda (i r)
     (if (fn i) (append r (list i)) r))
   '()
   l))

(define (sort lst fn)
  (if (null? lst)
      '()
      (insert (car lst) fn
              (sort (cdr lst) fn))))

Dave Griffiths's avatar
Dave Griffiths committed
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;; (chop (1 2 3 4) 2) -> ((1 2) (3 4))
(define (chop l n)
  (define (_ in out c)
    (cond
      ((null? in) out)
      ((zero? c) (_ (cdr in) (cons (list (car in)) out) (- n 1)))
      (else (_ (cdr in) (cons (cons (car in) (car out)) (cdr out)) (- c 1)))))
  (reverse (map reverse (_ l '(()) n))))

(define (crop l n)
  (cond
   ((null? l) '())
   ((zero? n) '())
   (else (cons (car l) (crop (cdr l) (- n 1))))))

(define (in-list? n l)
  (cond
    ((null? l) #f)
    ((equal? n (car l)) #t)
    (else (in-list? n (cdr l)))))

74
75
76
77
78
79
(define (find n l)
  (cond
    ((null? l) #f)
    ((equal? n (car (car l))) (car l))
    (else (find n (cdr l)))))

80
81
82
83
84
85
(define (findv n l)
  (cond
    ((null? l) #f)
    ((eqv? n (car (car l))) (car l))
    (else (findv n (cdr l)))))

Dave Griffiths's avatar
Dave Griffiths committed
86
87
88
89
90
91
92
93
94
;; find the index of an item in a flat list
(define (index-find n l)
  (define (_ l i)
    (cond
     ((null? l) #f)
     ((equal? n (car l)) i)
     (else (_ (cdr l) (+ i 1)))))
  (_ l 0))

95
96
97
98
(define (sorted-add l i)
  (cond
   ((null? l) (list i))
   ;; overwrite existing
99
100
   ((equal? (car i) (caar l)) (cons i (cdr l)))
   ((string<? (car i) (caar l))
101
102
103
104
105
    (cons i l))
   (else
    (cons (car l) (sorted-add (cdr l) i)))))

(define (sorted-find l k)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  (define (_ bot top)
    (if (null? l) #f
        (let* ((m (inexact->exact (floor (+ bot (/ (- top bot) 2)))))
               (mid (list-ref l m))
               (v (car mid)))
          (cond
           ((equal? k v) mid)
           ((eqv? top bot) #f)
           ((string<? k v) (_ bot m))
           (else (_ (+ m 1) top))))))
  (_ 0 (- (length l) 1)))

(define (sorted-addv l i)
  (cond
   ((null? l) (list i))
   ;; overwrite existing
   ((eqv? (car i) (caar l)) (cons i (cdr l)))
   ((< (car i) (caar l))
    (cons i l))
   (else
    (cons (car l) (sorted-addv (cdr l) i)))))

(define (sorted-findv l k)
129
130
131
132
133
134
135
136
137
138
139
140
  (define (_ bot top)
    (if (null? l) #f
        (let* ((m (inexact->exact (floor (+ bot (/ (- top bot) 2)))))
               (mid (list-ref l m))
               (v (car mid)))
          (cond
           ((eqv? k v) mid)
           ((eqv? top bot) #f)
           ((< k v) (_ bot m))
           (else (_ (+ m 1) top))))))
  (_ 0 (- (length l) 1)))

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
; utils funcs for using lists as sets
(define (set-remove a l)
  (cond
   ((null? l) '())
   (else
    (if (eqv? (car l) a)
        (set-remove a (cdr l))
        (cons (car l) (set-remove a (cdr l)))))))

(define (set-add a l)
  (if (not (memv a l))
      (cons a l) l))

(define (set-contains a l)
  (if (not (memq a l)) #f #t))

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
(define (build-list fn n)
  (define (_ fn n l)
    (cond ((zero? n) l)
          (else
           (_ fn (- n 1) (cons (fn (- n 1)) l)))))
  (_ fn n '()))

(define (foldl op initial seq)
  (define (iter result rest)
    (if (null? rest)
        result
        (iter (op (car rest) result) (cdr rest))))
  (iter initial seq))

(define (insert-to i p l)
  (cond
   ((null? l) (list i))
   ((zero? p) (cons i l))
   (else
    (cons (car l) (insert-to i (- p 1) (cdr l))))))

;; (list-replace '(1 2 3 4) 2 100) => '(1 2 100 4)
(define (list-replace l i v)
  (cond
    ((null? l) l)
    ((zero? i) (cons v (list-replace (cdr l) (- i 1) v)))
    (else (cons (car l) (list-replace (cdr l) (- i 1) v)))))
185

186
187
188
189
190
191
192
(define (insert elt fn sorted-lst)
  (if (null? sorted-lst)
      (list elt)
      (if (fn elt (car sorted-lst))
          (cons elt sorted-lst)
          (cons (car sorted-lst)
                (insert elt fn (cdr sorted-lst))))))
193

194
(define (choose l)
Dave Griffiths's avatar
Dave Griffiths committed
195
  (list-ref l (random (length l))))
196

197
198
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; time
199

200
201
202
(define (time->seconds t)
   (+ (car t) (/ (cadr t) 1000000)))

Dave Griffiths's avatar
Dave Griffiths committed
203
(define start-time (time->seconds (time-of-day)))
204
205

(define (time-now)
Dave Griffiths's avatar
Dave Griffiths committed
206
  (- (time->seconds (time-of-day)) start-time))
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
;; just for graph so don't have to be accurate!!!
(define (date->day d)
  (+ (* (list-ref d 2) 360)
     (* (list-ref d 1) 30)
     (list-ref d 0)))

(define (date< a b)
  (cond
   ((< (list-ref a 2) (list-ref b 2)) #t)
   ((> (list-ref a 2) (list-ref b 2)) #f)
   (else ;; year is the same
    (cond
     ((< (list-ref a 1) (list-ref b 1)) #t)
     ((> (list-ref a 1) (list-ref b 1)) #f)
     (else ;; month is the same
      (cond
       ((< (list-ref a 0) (list-ref b 0)) #t)
       ((> (list-ref a 0) (list-ref b 0)) #f)
       (else #f)))))))


(define (date->string d)
  (string-append
   (number->string (list-ref d 0))
Dave Griffiths's avatar
Dave Griffiths committed
232
233
234
235
   "-"
   (substring (number->string (+ (list-ref d 1) 100)) 1 3)
   "-"
   (substring (number->string (+ (list-ref d 2) 100)) 1 3)))
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
;; random

(define random-maker
  (let* ((multiplier 48271)
         (modulus 2147483647)
         (apply-congruence
          (lambda (current-seed)
            (let ((candidate (modulo (* current-seed multiplier)
                                     modulus)))
              (if (zero? candidate)
                  modulus
                  candidate))))
         (coerce
          (lambda (proposed-seed)
            (if (integer? proposed-seed)
                (- modulus (modulo proposed-seed modulus))
                19860617))))  ;; an arbitrarily chosen birthday
  (lambda (initial-seed)
    (let ((seed (coerce initial-seed)))
      (lambda args
        (cond ((null? args)
               (set! seed (apply-congruence seed))
               (/ (- modulus seed) modulus))
              ((null? (cdr args))
               (let* ((proposed-top
                       (ceiling (abs (car args))))
                      (exact-top
                       (if (inexact? proposed-top)
                           (inexact->exact proposed-top)
                           proposed-top))
                      (top
                       (if (zero? exact-top)
                           1
                           exact-top)))
                 (set! seed (apply-congruence seed))
                 (inexact->exact (floor (* top (/ seed modulus))))))
              ((eq? (cadr args) 'reset)
               (set! seed (coerce (car args))))
              (else
               (display "random: unrecognized message")
               (newline))))))))

Dave Griffiths's avatar
Dave Griffiths committed
280
(define rand
281
282
  (random-maker 19781116))  ;; another arbitrarily chosen birthday

Dave Griffiths's avatar
Dave Griffiths committed
283
284
285
286
(define (random n)
  (abs (modulo (rand n) n)))

(define rndf rand)
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

(define (rndvec) (vector (rndf) (rndf) (rndf)))

(define (crndf)
  (* (- (rndf) 0.5) 2))

(define (crndvec)
  (vector (crndf) (crndf) (crndf)))

(define (srndvec)
  (let loop ((v (crndvec)))
    (if (> (vmag v) 1) ; todo: use non sqrt version
        (loop (crndvec))
        v)))

(define (hsrndvec)
  (let loop ((v (crndvec)))
    (let ((l (vmag v)))
      (if (or (> l 1) (eq? l 0))
          (loop (crndvec))
          (vdiv v l)))))

(define (grndf)
  (let loop ((x (crndf)) (y (crndf)))
    (let ((l (+ (* x x) (* y y))))
      (if (or (>= l 1) (eq? l 0))
          (loop (crndf) (crndf))
          (* (sqrt (/ (* -2 (log l)) l)) x)))))

(define (grndvec)
  (vector (grndf) (grndf) (grndf)))

(define (rndbary)
	(let*
		((a (- 1.0 (sqrt (rndf))))
		 (b (* (rndf) (- 1.0 a)))
		 (c (- 1.0 (+ a b))))
		(vector a b c)))

; return a line on the hemisphere
(define (rndhemi n)
  (let loop ((v (srndvec)))
    (if (> (vdot n v) 0)
        v
        (loop (srndvec)))))

(define (hrndhemi n)
  (let loop ((v (hsrndvec)))
    (if (> (vdot n v) 0)
        v
        (loop (hsrndvec)))))
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define (string-split str . rest)
		; maxsplit is a positive number
  (define (split-by-whitespace str maxsplit)
    (define (skip-ws i yet-to-split-count)
      (cond
        ((>= i (string-length str)) '())
        ((char-whitespace? (string-ref str i))
          (skip-ws (+ 1 i) yet-to-split-count))
        (else (scan-beg-word (+ 1 i) i yet-to-split-count))))
    (define (scan-beg-word i from yet-to-split-count)
      (cond
        ((zero? yet-to-split-count)
          (cons (substring str from (string-length str)) '()))
        (else (scan-word i from yet-to-split-count))))
    (define (scan-word i from yet-to-split-count)
      (cond
        ((>= i (string-length str))
          (cons (substring str from i) '()))
        ((char-whitespace? (string-ref str i))
          (cons (substring str from i)
            (skip-ws (+ 1 i) (- yet-to-split-count 1))))
        (else (scan-word (+ 1 i) from yet-to-split-count))))
    (skip-ws 0 (- maxsplit 1)))

		; maxsplit is a positive number
		; str is not empty
  (define (split-by-charset str delimeters maxsplit)
    (define (scan-beg-word from yet-to-split-count)
      (cond
        ((>= from (string-length str)) '(""))
        ((zero? yet-to-split-count)
          (cons (substring str from (string-length str)) '()))
        (else (scan-word from from yet-to-split-count))))
    (define (scan-word i from yet-to-split-count)
      (cond
        ((>= i (string-length str))
          (cons (substring str from i) '()))
        ((memv (string-ref str i) delimeters)
          (cons (substring str from i)
            (scan-beg-word (+ 1 i) (- yet-to-split-count 1))))
        (else (scan-word (+ 1 i) from yet-to-split-count))))
    (scan-beg-word 0 (- maxsplit 1)))

			; resolver of overloading...
			; if omitted, maxsplit defaults to
			; (inc (string-length str))
  (if (equal? str "") '()
    (if (null? rest)
      (split-by-whitespace str (+ 1 (string-length str)))
      (let ((charset (car rest))
          (maxsplit
            (if (pair? (cdr rest)) (cadr rest) (+ 1 (string-length str)))))
        (cond
          ((not (positive? maxsplit)) '())
          ((null? charset) (split-by-whitespace str maxsplit))
          (else (split-by-charset str charset maxsplit))))))
  )

399
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
;; convert scheme values into equivilent json strings

(define (scheme->json v)
  (cond
   ((number? v) (number->string v))
   ((symbol? v) (string-append "\"" (symbol->string v) "\""))
   ((string? v) (string-append "\"" v "\""))
   ((boolean? v) (if v "true" "false"))
   ((list? v)
    (cond
     ((null? v) "[]")
     (else
      ; if it quacks like an assoc list...
      (if (and (not (null? v)) (not (list? (car v))) (pair? (car v)))
          (assoc->json v)
          (list->json v)))))
   (else "[]"))) ;;(display "value->js, unsupported type for ") (display v) (newline) "[]")))

(define (list->json l)
  (define (_ l s)
    (cond
     ((null? l) s)
     (else
      (_ (cdr l)
         (string-append
          s
          (if (not (string=? s "")) ", " "")
          (scheme->json (car l)))))))
  (string-append "[" (_ l "") "]"))

; ((one . 1) (two . "three")) -> { "one": 1, "two": "three }

(define (assoc->json l)
  (define (_ l s)
    (cond
     ((null? l) s)
     (else
      (let ((token (scheme->json (car (car l))))
            (value (scheme->json (cdr (car l)))))
        (_ (cdr l) (string-append s (if (not (string=? s "")) "," "")
                                  "\n" token ": " value))))))
  (string-append "{" (_ l "") "\n" "}"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
444
;; android ui
445

446
(define (layout width height weight gravity margin) (list "layout" width height weight gravity margin))
Dave Griffiths's avatar
Dave Griffiths committed
447
(define (rlayout width height margin rules) (list "relative-layout" width height margin rules))
Dave Griffiths's avatar
Dave Griffiths committed
448

449
450
451
(define (widget-type w) (list-ref w 0))
(define (widget-id w) (list-ref w 1))

Dave Griffiths's avatar
Dave Griffiths committed
452
;; all the widgets!
453
454
455
(define (linear-layout id orientation layout colour children)
  (list "linear-layout" id orientation layout colour children))
(define (linear-layout-children t) (list-ref t 5))
Dave Griffiths's avatar
Dave Griffiths committed
456
457
458
(define (relative-layout id layout colour children)
  (list "relative-layout" id layout colour children))
(define (relative-layout-children t) (list-ref t 4))
Dave Griffiths's avatar
Dave Griffiths committed
459
460
461
462
463
(define (frame-layout id layout children)
  (list "frame-layout" id layout children))
(define (frame-layout-children t) (list-ref t 3))
(define (scroll-view id layout children)
  (list "scroll-view" id layout children))
464
465
(define (scroll-view-vert id layout children)
  (list "scroll-view-vert" id layout children))
Dave Griffiths's avatar
Dave Griffiths committed
466
(define (scroll-view-children t) (list-ref t 3))
Dave Griffiths's avatar
Dave Griffiths committed
467
468
469
470
(define (draggable id orientation layout colour children listener)
  (list "draggable" id orientation layout colour children listener))
(define (draggable-children t) (list-ref t 5))
(define (draggable-listener t) (list-ref t 6))
471
472
(define (view-pager id layout fragment-list)
  (list "view-pager" id layout fragment-list))
473
474
475
476
477
(define (space layout) (list "space" "999" layout))
(define (space-view-layout t) (list-ref t 2))
(define (image-view id image layout) (list "image-view" id image layout))
(define (camera-preview id layout) (list "camera-preview" id layout))
(define (text-view id text size layout) (list "text-view" id text size layout))
Dave Griffiths's avatar
Dave Griffiths committed
478
(define (debug-text-view id text size layout) (list "debug-text-view" id text size layout))
479
(define (web-view id data layout) (list "web-view" id data layout))
Dave Griffiths's avatar
Dave Griffiths committed
480
481
(define (edit-text id text size type layout listener) (list "edit-text" id text size type layout listener))
(define (edit-text-listener t) (list-ref t 6))
482
483
(define (button id text text-size layout listener) (list "button" id text text-size layout listener))
(define (button-listener t) (list-ref t 5))
Dave Griffiths's avatar
Dave Griffiths committed
484
485
(define (image-button id image layout listener) (list "image-button" id image layout listener))
(define (image-button-listener t) (list-ref t 4))
486
487
(define (toggle-button id text text-size layout style listener) (list "toggle-button" id text text-size layout style listener))
(define (toggle-button-listener t) (list-ref t 6))
488
489
490
491
492
493
(define (seek-bar id max layout listener) (list "seek-bar" id max layout listener))
(define (seek-bar-listener t) (list-ref t 4))
(define (spinner id items layout listener) (list "spinner" id items layout listener))
(define (spinner-listener t) (list-ref t 4))
(define (canvas id layout drawlist) (list "canvas" id layout drawlist))
(define (canvas-drawlist t) (list-ref t 3))
494
495
496
(define (button-grid id type height textsize layout buttons listener)
  (list "button-grid" id type height textsize layout buttons listener))
(define (button-grid-listener b) (list-ref b 7))
497
498
499
500
(define (drawlist-line colour width points) (list "line" colour width points))
(define (drawlist-text text x y colour size align) (list "text" text x y colour size align))

(define (toast msg) (list "toast" 0 "toast" msg))
501
502
(define (play-sound wav) (list "play-sound" 0 "play-sound" wav))
(define (vibrate time) (list "vibrate" 0 "vibrate" time))
503
504
505
(define (make-directory name) (list "make-directory" 0 "make-directory" name))
;; treat this like a dialog so the callback fires
(define (list-files name path fn) (list "list-files" 0 "list-files" name fn path))
Dave Griffiths's avatar
Dave Griffiths committed
506
(define (gps-start name fn) (list "gps-start" 0 "gps-start" name fn))
507
(define (delayed name delay fn) (list "delayed" 0 "delayed" name fn delay))
508
509
(define (network-connect name ssid fn) (list "network-connect" 0 "network-connect" name fn ssid))
(define (http-request name url fn) (list "http-request" 0 "http-request" name fn url))
510
(define (http-download name url filename) (list "http-download" 0 "http-download" name filename url))
Dave Griffiths's avatar
Dave Griffiths committed
511
(define (http-upload name url filename) (list "http-upload" 0 "http-upload" name filename url))
512
(define (send-mail to subject body attachments) (list "send-mail" 0 "send-mail" to subject body attachments))
Dave Griffiths's avatar
Dave Griffiths committed
513
514
(define (take-photo filename code) (list "take-photo" 0 "take-photo" filename code))
(define (walk-draggable name id fn) (list "walk-draggable" 0 "walk-draggable" name fn id))
515

516
517
518
(define (dialog-fragment id layout fragment-name fn)
  (list "dialog-fragment" 0 "dialog-fragment" id layout fragment-name fn))

519
520
521
522
523
524
525
526
527
528
529
530
531
532
(define (time-picker-dialog name fn)
  (list "time-picker-dialog" 0 "time-picker-dialog" name fn))
(define (date-picker-dialog name fn)
  (list "date-picker-dialog" 0 "date-picker-dialog" name fn))
(define (alert-dialog name msg fn)
  (list "alert-dialog" 0 "alert-dialog" name fn msg))
(define (dialog-type d) (list-ref d 2))
(define (dialog-name d) (list-ref d 3))
(define (dialog-fn d) (list-ref d 4))

(define (start-activity act request arg) (list "start-activity" 0 "start-activity" act request arg))
(define (start-activity-goto act request arg) (list "start-activity" 0 "start-activity-goto" act arg))
(define (finish-activity result) (list "finish-activity" 0 "finish-activity" result))

Dave Griffiths's avatar
Dave Griffiths committed
533
(define (build-fragment type id layout) (list "build-fragment" type id layout))
Dave Griffiths's avatar
Dave Griffiths committed
534
535
(define (replace-fragment id type) (list "replace-fragment" id type))

536
(define (update-widget type id token value) (list type id token value))
537
538
539
540
(define (update-widget-type l) (list-ref l 0))
(define (update-widget-id l) (list-ref l 1))
(define (update-widget-token l) (list-ref l 2))
(define (update-widget-value l) (list-ref l 3))
541
542
543
544

(define id-map ())
(define current-id 1)

545
546
547
548
549
;(define (find-id name id-map)
;  (cond
;   ((null? id-map) #f)
;   ((equal? name (car (car id-map))) (cadr (car id-map)))
;   (else (find-id name (cdr id-map)))))
550

551
552
;(define (get-id name)
;  (find-id name id-map))
553
554

;(define (make-id name)
555
556
557
558
559
560
561
562
;  (let ((existing (get-id name)))
;    (cond
;     (existing existing)
;     (else
;      (set! id-map (cons (list name current-id) id-map))
;      (set! current-id (+ current-id 1))
;      (get-id name)))))

563
564
;(define (get-id name)
;  (cadr (sorted-find id-map name)))
565

566
567
;(define (make-id name)
;  (prof-start "make-id")
568
;  (prof-start "make-id sorted find")
569
570
571
572
573
574
575
576
577
578
;  (let ((sf (sorted-find id-map name)))
;    (prof-end "make-id sorted find")
;    (let ((r (if (not sf)
;                 (let ((id current-id))
;                   (prof-start "make-id sorted add")
;                   (set! id-map (sorted-add id-map (list name id)))
 ;                  (prof-end "make-id sorted add")
;                   (set! current-id (+ current-id 1))
;                   id)
;                 (cadr sf))))
579
;      (prof-end "make-id")
580
;      r)))
581

582
(define (get-id name)
Dave Griffiths's avatar
Dave Griffiths committed
583
584
585
586
  (let ((r (id-map-get name)))
    (cond
     ((zero? r) (msg "no id found for" name) 0)
     (else r))))
587

588
(define (make-id name)
Dave Griffiths's avatar
Dave Griffiths committed
589
  ;;(msg "making id for" name)
590
  (let ((id (id-map-get name)))
591
    (cond
592
     ((zero? id)
Dave Griffiths's avatar
Dave Griffiths committed
593
      ;;(msg "this is a new id")
594
595
596
597
598
     ; (prof-start "make-id")
      (id-map-add name current-id)
      (set! current-id (+ current-id 1))
     ; (prof-end "make-id")
      (- current-id 1))
Dave Griffiths's avatar
Dave Griffiths committed
599
600
601
602
603
     (else
      ;; seems scheme is shut down while the id store keeps going?
      (when (> id current-id) (set! current-id (+ id 1)))
      ;;(msg "we have seen this one before")
      id))))
604
605
606
607
608
609
610
611
612
613
614

(define prof-map '())

(define (new-prof-item id)
  (list id (time-now) 0 0))
(define (prof-item-id p) (list-ref p 0))
(define (prof-item-time p) (list-ref p 1))
(define (prof-item-accum p) (list-ref p 2))
(define (prof-item-calls p) (list-ref p 3))

(define (prof-item-restart p)
615
  (list
616
617
618
619
620
621
   (prof-item-id p)
   (time-now)
   (prof-item-accum p)
   (prof-item-calls p)))

(define (prof-item-end p)
622
  (list
623
624
   (prof-item-id p)
   0
625
   (+ (prof-item-accum p)
626
627
628
629
630
631
      (- (time-now) (prof-item-time p)))
   (+ (prof-item-calls p) 1)))

(define (prof-start id)
  (let ((dd (sorted-find prof-map id)))
    (if dd
632
633
        (set! prof-map
              (sorted-add
634
               prof-map (prof-item-restart dd)))
635
636
        (set! prof-map
              (sorted-add
637
638
639
640
               prof-map (new-prof-item id))))))

(define (prof-end id)
  (let ((d (sorted-find prof-map id)))
641
642
643
    (set! prof-map
          (sorted-add
           prof-map
644
           (prof-item-end d)))))
645

646
(define (prof-print)
647
  (let ((tot (foldl
648
649
650
651
652
              (lambda (d r)
                (+ (prof-item-accum d) r))
              0 prof-map)))
    (for-each
     (lambda (d)
653
654
       (msg (prof-item-id d)
            (prof-item-calls d)
655
656
657
             (prof-item-accum d)
             (* (/ (prof-item-accum d) tot) 100) "%"))
     prof-map)))
658

659
660
661
662
(define wrap (layout 'wrap-content 'wrap-content 1 'left 0))
(define fillwrap (layout 'fill-parent 'wrap-content 1 'left 0))
(define wrapfill (layout 'wrap-content 'fill-parent 1 'left 0))
(define fill (layout 'fill-parent 'fill-parent 1 'left 0))
663

664
(define (spacer size) (space (layout 'fill-parent size 1 'left 0)))
665

666

667
668
(define (horiz . l)
  (linear-layout
669
   0 'horizontal
Dave Griffiths's avatar
Dave Griffiths committed
670
   (layout 'fill-parent 'wrap-content -1 'centre 0)
671
   (list 0 0 0 0)
672
673
   l))

Dave Griffiths's avatar
Dave Griffiths committed
674
675
676
677
678
679
680
(define (horiz-colour col . l)
  (linear-layout
   0 'horizontal
   (layout 'fill-parent 'wrap-content -1 'centre 0)
   col
   l))

681
682
(define (vert . l)
  (linear-layout
683
   0 'vertical
684
   (layout 'fill-parent 'wrap-content 1 'centre 20)
Dave Griffiths's avatar
Dave Griffiths committed
685
686
687
688
689
690
691
692
693
694
695
696
697
698
   (list 0 0 0 0)
   l))

(define (vert-colour col . l)
  (linear-layout
   0 'vertical
   (layout 'fill-parent 'wrap-content 1 'centre 20)
   col
   l))

(define (vert-fill . l)
  (linear-layout
   0 'vertical
   (layout 'fill-parent 'fill-parent 1 'left 0)
699
   (list 0 0 0 0)
700
701
   l))

Dave Griffiths's avatar
Dave Griffiths committed
702
703
704
705
706
707
(define (relative rules colour . l)
  (relative-layout
   0 (rlayout 'fill-parent 'wrap-content 20 rules)
   colour
   l))

708
709
710
(define (activity name layout on-create on-start on-resume on-pause on-stop on-destroy on-activity-result)
  (list name layout on-create on-start on-resume on-pause on-stop on-destroy on-activity-result))

Dave Griffiths's avatar
Dave Griffiths committed
711
712
713
(define (fragment name layout on-create on-start on-resume on-pause on-stop on-destroy)
  (list name layout on-create on-start on-resume on-pause on-stop on-destroy))

714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
(define (activity-name a) (list-ref a 0))
(define (activity-layout a) (list-ref a 1))
(define (activity-modify-layout a v) (list-replace a 1 v))
(define (activity-on-create a) (list-ref a 2))
(define (activity-on-start a) (list-ref a 3))
(define (activity-on-resume a) (list-ref a 4))
(define (activity-on-pause a) (list-ref a 5))
(define (activity-on-stop a) (list-ref a 6))
(define (activity-on-destroy a) (list-ref a 7))
(define (activity-on-activity-result a) (list-ref a 8))

(define (activity-list l) l)

(define (activity-list-find l name)
  (cond
   ((null? l) #f)
   ((equal? (activity-name (car l)) name) (car l))
   (else (activity-list-find (cdr l) name))))

733
734
735
736
737
738
739
740
(define activities 0)
(define fragments 0)

(define callbacks '())
(define (callback id type fn) (list id type fn))
(define (callback-id l) (list-ref l 0))
(define (callback-type l) (list-ref l 1))
(define (callback-fn l) (list-ref l 2))
741
(define (find-callback id) (sorted-findv callbacks id))
742
743
(define (add-callback! cb)
  ;;(msg "adding" cb)
744
  (set! callbacks (sorted-addv callbacks cb)))
745
746

(define (widget-get-children w)
747
  (cond
748
   ((equal? (widget-type w) "linear-layout") (linear-layout-children w))
Dave Griffiths's avatar
Dave Griffiths committed
749
   ((equal? (widget-type w) "relative-layout") (relative-layout-children w))
750
751
   ((equal? (widget-type w) "frame-layout") (frame-layout-children w))
   ((equal? (widget-type w) "scroll-view") (scroll-view-children w))
Dave Griffiths's avatar
Dave Griffiths committed
752
753
   ((equal? (widget-type w) "scroll-view-vert") (scroll-view-children w))
   ((equal? (widget-type w) "draggable") (draggable-children w))
754
;;   ((equal? (widget-type w) "grid-layout") (grid-layout-children w))
755
756
757
758
759
760
   (else '())))

(define (widget-get-callback w)
  (cond
   ((equal? (widget-type w) "edit-text") (edit-text-listener w))
   ((equal? (widget-type w) "button") (button-listener w))
Dave Griffiths's avatar
Dave Griffiths committed
761
   ((equal? (widget-type w) "image-button") (image-button-listener w))
762
763
764
   ((equal? (widget-type w) "toggle-button") (toggle-button-listener w))
   ((equal? (widget-type w) "seek-bar") (seek-bar-listener w))
   ((equal? (widget-type w) "spinner") (spinner-listener w))
765
   ((equal? (widget-type w) "button-grid") (button-grid-listener w))
Dave Griffiths's avatar
Dave Griffiths committed
766
   ((equal? (widget-type w) "draggable") (draggable-listener w))
767
768
769
   (else #f)))

;; walk through activity stripping callbacks
770
;; version called from on-create
771
(define (update-callbacks! widget-list)
772
773
  (cond
   ((null? widget-list) #f)
774
775
776
   (else
    (let* ((w (car widget-list))
           (c (widget-get-children w)))
Dave Griffiths's avatar
Dave Griffiths committed
777
778
779
780
781
      (when (not (null? c))
            (update-callbacks! c))
      (let ((cb (widget-get-callback w)))
        (when cb
              (add-callback! (callback (widget-id w) (widget-type w) cb)))))
782
783
784
    (update-callbacks! (cdr widget-list)))))

;; walk through update stripping callbacks
785
;; version called with update-widgets (after on-create version above)
786
787
788
789
790
791
792
(define (update-callbacks-from-update! widget-list)
  (if (null? widget-list) #f
      (let ((w (car widget-list)))
        (cond
         ((null? w) #f)
         ;; drill deeper
         ((eq? (update-widget-token w) 'contents)
Dave Griffiths's avatar
Dave Griffiths committed
793
          ;;(msg "updateing contents from callback")
794
795
796
797
          (update-callbacks! (update-widget-value w)))
         ((eq? (update-widget-token w) 'grid-buttons)
          (add-callback! (callback (update-widget-id w)
                                   "button-grid"
798
                                   (list-ref (update-widget-value w) 5)))))
799
        (update-callbacks-from-update! (cdr widget-list)))))
800
801

(define (define-activity-list . args)
Dave Griffiths's avatar
Dave Griffiths committed
802
803
804
805
  (set! activities (activity-list args)))

(define (define-fragment-list . args)
  (set! fragments (activity-list args)))
806

807
808
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; replace with new cb system
809
810
811
812
813
814
815
816
817

(define dialogs '())

(define (dialog-find dl name)
  (cond
   ((null? dl) #f)
   ((equal? (dialog-name (car dl)) name) (car dl))
   (else (dialog-find (cdr dl) name))))

818
819
820
821
822
823
824
825
(define (dialog-replace dl name d)
  (cond
   ((null? dl) (list d))
   ((equal? (dialog-name (car dl)) name)
    (cons d (cdr dl)))
   (else (cons (car dl) (dialog-replace (cdr dl) name d)))))


826
(define (add-new-dialog! d)
827
  (set! dialogs (dialog-replace dialogs (dialog-name d) d))
828
  ;; todo - when to clear out?
829
  ;;(when (not (dialog-find dialogs (dialog-name d)))
830
        ;;(display "adding dialog ")(display d)(newline)
831
832
833
  ;;      (set! dialogs (cons d dialogs)))
  )

834
835

(define (update-dialogs! events)
Dave Griffiths's avatar
Dave Griffiths committed
836
  ;;(msg "update-dialogs" events)
837
838
839
840
841
842
843
  (when (list? events)
        (for-each
         (lambda (event)
           (when (or
                  ;; todo - something a bit more fancy
                  (equal? (list-ref event 0) "date-picker-dialog")
                  (equal? (list-ref event 0) "alert-dialog")
844
845
                  (equal? (list-ref event 0) "list-files")
                  (equal? (list-ref event 0) "http-request")
846
                  (equal? (list-ref event 0) "network-connect")
847
                  (equal? (list-ref event 0) "delayed")
Dave Griffiths's avatar
Dave Griffiths committed
848
                  (equal? (list-ref event 0) "walk-draggable")
849
                  (equal? (list-ref event 0) "gps-start"))
850
                 (add-new-dialog! event)))
Dave Griffiths's avatar
Dave Griffiths committed
851
         events))
Dave Griffiths's avatar
Dave Griffiths committed
852
853
  ;;(msg "update-dialogs end")
  )
854

855
856
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

857
858
859
860
(define (dialog-callback name args)
  (let ((dialog (dialog-find dialogs name)))
    (if (not dialog)
        (begin (display "no dialog called ")(display name)(newline))
Dave Griffiths's avatar
Dave Griffiths committed
861
862
863
        (let ((events (apply (dialog-fn dialog) args)))
          (update-dialogs! events)
          (send (scheme->json events))))))
864
865
866

;; called by java
(define (activity-callback type activity-name args)
867
868
869
870
871
  (prof-start "activity-callback")
  (let ((r (let ((activity (activity-list-find activities activity-name)))
             (top-callback type activity-name activity args))))
    (prof-end "activity-callback")
    r))
Dave Griffiths's avatar
Dave Griffiths committed
872
873
874

;; called by java
(define (fragment-callback type fragment-name args)
875
876
877
878
879
  (prof-start "activity-callback")
  (let ((r (let ((fragment (activity-list-find fragments fragment-name)))
             (top-callback type fragment-name fragment args))))
    (prof-end "activity-callback")
    r))
Dave Griffiths's avatar
Dave Griffiths committed
880

881
(define (top-callback type activity-name activity args)
Dave Griffiths's avatar
Dave Griffiths committed
882
  ;;(display "activity/fragment-callback ")(display type)(display " ")(display args)(newline)
Dave Griffiths's avatar
Dave Griffiths committed
883
884
885
886
887
  (if (not activity)
      (begin (display "no activity/fragment called ")(display activity-name)(newline))
      (let ((ret (cond
                  ;; todo update activity...?
                  ((eq? type 'on-create) ((activity-on-create activity) activity (car args)))
888
                  ((eq? type 'on-start) ((activity-on-start activity) activity (car args)))
Dave Griffiths's avatar
Dave Griffiths committed
889
890
891
892
893
894
895
896
                  ((eq? type 'on-stop) ((activity-on-stop activity) activity))
                  ((eq? type 'on-resume) ((activity-on-resume activity) activity))
                  ((eq? type 'on-pause) ((activity-on-pause activity) activity))
                  ((eq? type 'on-destroy) ((activity-on-destroy activity) activity))
                  ((eq? type 'on-activity-result) ((activity-on-activity-result activity) activity (car args) (cadr args)))
                  (else
                   (display "no callback called ")(display type)(newline)
                   '()))))
897
898
899
900
901
        (cond
         ((eq? type 'on-create)
          (update-callbacks! (list ret)))
         (else
          (update-dialogs! ret)
Dave Griffiths's avatar
Dave Griffiths committed
902
          ;;(msg "top-callback inbetween")
903
          (update-callbacks-from-update! ret)))
Dave Griffiths's avatar
Dave Griffiths committed
904
905
906
907
908
909
        (send (scheme->json ret)))))

(define (find-activity-or-fragment name)
  (let ((r (activity-list-find activities name)))
    (if r r
        (activity-list-find fragments name))))
910
911

(define (widget-callback activity-name widget-id args)
Dave Griffiths's avatar
Dave Griffiths committed
912
  ;;(msg "widget-callback" activity-name widget-id args)
913
  (prof-start "widget-callback")
914
  (let ((cb (find-callback widget-id)))
Dave Griffiths's avatar
Dave Griffiths committed
915
    ;;(msg cb)
916
917
918
919
920
921
922
923
    (if (not cb)
        (msg "no widget" widget-id "found!")
        (let ((events
               (cond
                ((equal? (callback-type cb) "edit-text")
                 ((callback-fn cb) (car args)))
                ((equal? (callback-type cb) "button")
                 ((callback-fn cb)))
Dave Griffiths's avatar
Dave Griffiths committed
924
925
                ((equal? (callback-type cb) "image-button")
                 ((callback-fn cb)))
926
927
928
929
                ((equal? (callback-type cb) "toggle-button")
                 ((callback-fn cb) (car args)))
                ((equal? (callback-type cb) "seek-bar")
                 ((callback-fn cb) (car args)))
930
931
932
                ((equal? (callback-type cb) "spinner")
                 ((callback-fn cb) (car args)))
                ((equal? (callback-type cb) "button-grid")
933
                 ((callback-fn cb) (car args) (cadr args)))
Dave Griffiths's avatar
Dave Griffiths committed
934
935
                ((equal? (callback-type cb) "draggable")
                 ((callback-fn cb)))
936
937
                (else
                 (msg "no callbacks for type" (callback-type cb))))))
Dave Griffiths's avatar
Dave Griffiths committed
938
939
940
          ;; this was just update-callbacks, commented out,
          ;; expecting trouble here... (but seems to fix new widgets from
          ;; widget callbacks so far)
Dave Griffiths's avatar
Dave Griffiths committed
941
          ;;(msg "callback returned" events)
Dave Griffiths's avatar
Dave Griffiths committed
942
          (update-callbacks-from-update! events)
943
          (update-dialogs! events)
944
945
          (send (scheme->json events))
          (prof-end "widget-callback")))))
Dave Griffiths's avatar
Dave Griffiths committed
946
947
948


(alog "lib.scm done")