agar

Check-in [77e4925213]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Basic bindings and documentation
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:77e49252135f7844efb23057ff34cf5692cf8c8a
User & Date: murphy 2016-09-24 17:19:16
Context
2016-09-24
17:20
Corrected date in license file check-in: 1ada5735ed user: murphy tags: trunk
17:19
Basic bindings and documentation check-in: 77e4925213 user: murphy tags: trunk
2016-09-22
08:59
initial empty check-in check-in: c3e64246f3 user: murphy tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added LICENSE.txt.







































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Copyright (C) 2010 Thomas Chust <chust@web.de>.  All rights reserved.

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 ASIS, 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.

Added agar-types.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
;; -*- mode: Scheme; tab-width: 2; -*- ;;

(define-foreign-type nonnull-object (nonnull-c-pointer "AG_Object")
  (object->pointer #t)
  (pointer->object #t))

(define-foreign-type object (c-pointer "AG_Object")
  (object->pointer #f)
  (pointer->object #f))

(define-foreign-type nonnull-class (nonnull-c-pointer "AG_ObjectClass")
  (class->pointer #t)
  (pointer->class #t))

(define-foreign-type class (c-pointer "AG_ObjectClass")
  (class->pointer #f)
  (pointer->class #f))

(define-foreign-type nonnull-widget (nonnull-c-pointer "AG_Widget")
  (object->pointer #t)
  (pointer->object #t))

(define-foreign-type widget (c-pointer "AG_Widget")
  (object->pointer #f)
  (pointer->object #f))

(define-foreign-type nonnull-window (nonnull-c-pointer "AG_Window")
  (object->pointer #t)
  (pointer->object #t))

(define-foreign-type window (c-pointer "AG_Window")
  (object->pointer #f)
  (pointer->object #f))

(define-foreign-type nonnull-box (nonnull-c-pointer "AG_Box")
  (object->pointer #t)
  (pointer->object #t))

(define-foreign-type box (c-pointer "AG_Box")
  (object->pointer #f)
  (pointer->object #f))

(define-foreign-type nonnull-button (nonnull-c-pointer "AG_Button")
  (object->pointer #t)
  (pointer->object #t))

(define-foreign-type button (c-pointer "AG_Button")
  (object->pointer #f)
  (pointer->object #f))

(define-foreign-type nonnull-textbox (nonnull-c-pointer "AG_Textbox")
  (object->pointer #t)
  (pointer->object #t))

(define-foreign-type textbox (c-pointer "AG_Textbox")
  (object->pointer #f)
  (pointer->object #f))

;; vim:set ft=scheme ts=2 sts=2 sw=2 ai: ;;

Added agar.meta.























>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
;; -*- mode: Scheme; -*-

((category ui)
 (license "BSD")
 (author "Thomas Chust")
 (synopsis "Bindings to the Agar GUI library")
 (doc-from-wiki)
 (needs srfi-42)
 (files "agar.scm" "agar-types.scm" "agar.setup" "agar.meta" "agar.release-info"))

;; vim:set ft=scheme ai: ;;

Added agar.release-info.

















>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
;; -*- mode: Scheme; -*-

(repo fossil "http://chust.org/fossils/{egg-name}")

(uri meta-file "http://chust.org/fossils/{egg-name}/doc/v{egg-release}/chicken/{egg-name}.meta")
(release "0.1.0")

;; vim:set ft=scheme ai: ;;

Added agar.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
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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
;; -*- mode: Scheme; tab-width: 2; -*- ;;

(require-library
  lolevel data-structures ports srfi-42 srfi-69)

(module agar
  (agar-condition? make-agar-condition current-agar-condition check-agar-condition
   init-agar!
   pointer->object object->pointer pointer->class class->pointer
   object? lookup-object make-object destroy-object! object-class object-name object-name-set!
   class? lookup-class class-hierarchy class-version
   object-ref object-set! event-add!
   widget-expand! widget-position-set! widget-size-set!
   make-window window-caption window-caption-set! window-visible? window-visible-set!
   make-box make-button
   make-textbox textbox-string textbox-string-set!
   event-loop terminate!)
  (import
    scheme chicken foreign
    (only lolevel pointer? pointer=? tag-pointer tagged-pointer?)
    data-structures ports srfi-42 srfi-69)

(foreign-declare
  "#include <agar/core.h>"
  "#include <agar/gui.h>"
  "C_externexport void raise_fatal_condition(const char *msg);"
  "C_externexport void dispatch_event(AG_Event *evt);")

(define agar-condition?
  (conjoin
    (condition-predicate 'exn)
    (condition-predicate 'agar)))

(define condition-message
  (condition-property-accessor 'exn 'message))

(define (make-agar-condition msg)
  (make-composite-condition
    (make-property-condition 'exn 'message (or msg "Agar error"))
    (make-property-condition 'agar)))

(define-external (raise_fatal_condition [(const c-string) msg]) void
  (abort (make-agar-condition msg)))

(define current-agar-condition
  (case-lambda
    [()
     (cond
       [((foreign-lambda c-string "AG_GetError")) => make-agar-condition]
       [else #f])]
    [(v)
     (let ([msg
            (cond
              [(agar-condition? v) (condition-message v)]
              [else (call-with-output-string (cut print-error-message v <>))])])
       ((foreign-lambda* void ([nonnull-scheme-pointer msg] [int len])
          "AG_SetError(\"%.*s\", len, (const char *) msg);")
        msg (string-length msg)))]))

(define check-agar-condition
  (case-lambda
    [(v)
     (cond
       [(and (or (not v) (negative? v)) (current-agar-condition)) => signal]
       [else v])]
    [()
     (cond
       [(current-agar-condition) => signal])]))

(define (init-agar! #!key program verbose? create-datadir? soft-timers? graphics)
  (check-agar-condition
   ((foreign-safe-lambda int "AG_InitCore" nonnull-c-string unsigned-int)
    (or program (program-name))
    (bitwise-ior
      (if verbose? (foreign-value "AG_VERBOSE" unsigned-int) 0)
      (if create-datadir? (foreign-value "AG_CREATE_DATADIR" unsigned-int) 0)
      (if soft-timers? (foreign-value "AG_SOFT_TIMERS" unsigned-int) 0))))
  (when graphics
    (check-agar-condition
     ((foreign-safe-lambda int "AG_InitGraphics" c-string)
      (if (eq? graphics #t) #f graphics))))
  (void))

(define *object-tag* "AG_Object")
(define *class-tag* "AG_ObjectClass")

(define (pointer->object nonnull?)
  (if nonnull?
    (lambda (ptr)
      (ensure pointer? ptr)
      (tag-pointer ptr *object-tag*))
    (lambda (ptr)
      (and ptr (tag-pointer ptr *object-tag*)))))

(define (object->pointer nonnull?)
  (if nonnull?
    (lambda (obj)
      (ensure object? obj)
      obj)
    (lambda (obj)
      (ensure (disjoin not object?) obj)
      obj)))

(define (pointer->class nonnull?)
  (if nonnull?
    (lambda (ptr)
      (ensure pointer? ptr)
      (tag-pointer ptr *class-tag*))
    (lambda (ptr)
      (and ptr (tag-pointer ptr *class-tag*)))))

(define (class->pointer nonnull?)
  (if nonnull?
    (lambda (cls)
      (ensure class? cls)
      cls)
    (lambda (cls)
      (ensure (disjoin not class?) cls)
      cls)))

(define object?
  (case-lambda
    [(v)
     (tagged-pointer? v *object-tag*)]
    [(v class)
     (and
       (tagged-pointer? v *object-tag*)
       (tagged-pointer? class *class-tag*)
       (pointer=?
         ((foreign-lambda* c-pointer ([(nonnull-c-pointer "AG_Object") obj]) "C_return(obj->cls);") v)
         class))]))

(define (class? v)
  (tagged-pointer? v *class-tag*))

(include
  "agar-types.scm")

(define lookup-object
  (foreign-safe-lambda object "AG_ObjectFindS" nonnull-object nonnull-c-string))

(define (make-object class #!key parent name)
  ((foreign-safe-lambda nonnull-object "AG_ObjectNew" object c-string nonnull-class)
   parent name class))

(define destroy-object!
  (foreign-safe-lambda void "AG_ObjectDestroy" nonnull-object))

(define object-class
  (foreign-lambda* nonnull-class ([nonnull-object obj]) "C_return(obj->cls);"))

(define-values (object-name object-name-set!)
  (let ([object-name
         (foreign-lambda* c-string ([nonnull-object obj]) "C_return(obj->name);")]
        [object-name-set!
         (foreign-safe-lambda void "AG_ObjectSetNameS" nonnull-object c-string)])
    (values
      (getter-with-setter object-name object-name-set!)
      object-name-set!)))

(define lookup-class
  (foreign-safe-lambda class "AG_LookupClass" nonnull-c-string))

(define class-hierarchy
  (foreign-lambda* c-string ([nonnull-class cls]) "C_return(cls->hier);"))

(define class-version
  (foreign-primitive ([nonnull-class cls])
    "C_word av[4] = { C_SCHEME_UNDEFINED, C_k, C_SCHEME_UNDEFINED, C_SCHEME_UNDEFINED };\n"
    "av[2] = C_fix(cls->ver.major);\n"
    "av[3] = C_fix(cls->ver.minor);\n"
    "C_values(4, av);\n"))

(define var-type
  (foreign-lambda* int ([(nonnull-c-pointer "AG_Variable") var])
    "C_return(var->type);"))

(define (var-ref var)
  (select (var-type var)
    [((foreign-value "AG_VARIABLE_NULL" int))
     #f]
    [((foreign-value "AG_VARIABLE_UINT" int))
     ((foreign-lambda* unsigned-int ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.u);")
      var)]
    [((foreign-value "AG_VARIABLE_INT" int))
     ((foreign-lambda* int ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.i);")
      var)]
    [((foreign-value "AG_VARIABLE_UINT8" int))
     ((foreign-lambda* unsigned-byte ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.u8);")
      var)]
    [((foreign-value "AG_VARIABLE_SINT8" int))
     ((foreign-lambda* byte ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.s8);")
      var)]
    [((foreign-value "AG_VARIABLE_UINT16" int))
     ((foreign-lambda* unsigned-short ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.u16);")
      var)]
    [((foreign-value "AG_VARIABLE_SINT16" int))
     ((foreign-lambda* short ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.s16);")
      var)]
    [((foreign-value "AG_VARIABLE_UINT32" int))
     ((foreign-lambda* unsigned-integer32 ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.u32);")
      var)]
    [((foreign-value "AG_VARIABLE_SINT32" int))
     ((foreign-lambda* integer32 ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.s32);")
      var)]
    [((foreign-value "AG_VARIABLE_UINT64" int))
     ((foreign-lambda* unsigned-integer64 ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.u64);")
      var)]
    [((foreign-value "AG_VARIABLE_SINT64" int))
     ((foreign-lambda* integer64 ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.s64);")
      var)]
    [((foreign-value "AG_VARIABLE_FLOAT" int))
     ((foreign-lambda* float ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.flt);")
      var)]
    [((foreign-value "AG_VARIABLE_DOUBLE" int))
     ((foreign-lambda* double ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.dbl);")
      var)]
    [((foreign-value "AG_VARIABLE_STRING" int) (foreign-value "AG_VARIABLE_CONST_STRING" int))
     ((foreign-lambda* c-string ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.Cs);")
      var)]
    [((foreign-value "AG_VARIABLE_POINTER" int) (foreign-value "AG_VARIABLE_CONST_POINTER" int))
     ((foreign-lambda* c-pointer ([(nonnull-c-pointer "AG_Variable") var])
        "C_return(var->data.Cp);")
      var)]))

(define (var-set! var v)
  (select (var-type var)
    [((foreign-value "AG_VARIABLE_UINT" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [unsigned-int v])
        "var->data.u = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_INT" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [int v])
        "var->data.i = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_UINT8" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [unsigned-byte v])
        "var->data.u8 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_SINT8" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [byte v])
        "var->data.s8 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_UINT16" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [unsigned-short v])
        "var->data.u16 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_SINT16" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [short v])
        "var->data.s16 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_UINT32" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [unsigned-integer32 v])
        "var->data.u32 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_SINT32" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [integer32 v])
        "var->data.s32 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_UINT64" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [unsigned-integer64 v])
        "var->data.u64 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_SINT64" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [integer64 v])
        "var->data.s64 = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_FLOAT" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [float v])
        "var->data.flt = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_DOUBLE" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [double v])
        "var->data.dbl = v;")
      var v)]
    [((foreign-value "AG_VARIABLE_STRING" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [c-string v])
        "char *old = var->data.s;\n"
        "var->data.s = AG_Strdup(v);\n"
        "if (old) AG_Free(old);\n")
      var v)]
    [((foreign-value "AG_VARIABLE_POINTER" int) (foreign-value "AG_VARIABLE_CONST_POINTER" int))
     ((foreign-lambda* void ([(nonnull-c-pointer "AG_Variable") var] [c-pointer v])
        "var->data.p = v;")
      var v)]))

(define object-var
  (foreign-safe-lambda (c-pointer "AG_Variable") "AG_GetVariable" nonnull-object nonnull-c-string c-pointer))

(define-values (object-ref object-set!)
  (let ([object-ref
         (lambda (obj name)
           (cond
             [(object-var obj name #f) => var-ref]
             [else #f]))]
        [object-set!
         (lambda (obj name v)
           (cond
             [(object-var obj name #f) => (cut var-set! <> v)]
             [else (error 'object-ref "no such variable" name)]))])
    (values
      (getter-with-setter object-ref object-set!)
      object-set!)))

(define *event-handlers*
  (make-hash-table eqv? eqv?-hash))

(define arg-self
  (foreign-lambda* object ([(nonnull-c-pointer "AG_Event") event])
    "C_return(AG_SELF());"))
(define arg-sender
  (foreign-lambda* object ([(nonnull-c-pointer "AG_Event") event])
    "C_return(AG_SENDER());"))
(define arg-count
  (foreign-lambda* int ([(nonnull-c-pointer "AG_Event") event])
    "C_return(event->argc);"))
(define arg-ref
  (foreign-lambda* (c-pointer "AG_Variable") ([(nonnull-c-pointer "AG_Event") event] [int i])
    "C_return(&event->argv[i]);"))

(define-external (dispatch_event [(nonnull-c-pointer "AG_Event") event]) void
  (call-with-current-continuation
    (lambda (return)
      (with-exception-handler
        (lambda (exn)
          (current-agar-condition exn)
          (return))
        (lambda ()
          (apply
            (hash-table-ref *event-handlers* (var-ref (arg-ref event 1)))
            (arg-self event) (arg-sender event)
            (list-ec (:range i 2 (arg-count event))
              (var-ref (arg-ref event i)))))))))

(define (event-add! obj name handler)
  (let retry ([key (hash handler)])
    (if (hash-table-exists? *event-handlers* key)
      (retry (add1 key))
      (begin
        (hash-table-set! *event-handlers* key handler)
        ((foreign-safe-lambda* void ([nonnull-object obj] [nonnull-c-string name] [unsigned-int key])
           "AG_AddEvent(obj, name, dispatch_event, \"%u\", key);")
         obj name key)))))

(define (widget-expand! widget #!key hfill vfill expand)
  (cond
    [(or (and hfill vfill) expand)
     ((foreign-safe-lambda void "AG_Expand" nonnull-widget) widget)]
    [hfill
     ((foreign-safe-lambda void "AG_ExpandHoriz" nonnull-widget) widget)]
    [vfill
     ((foreign-safe-lambda void "AG_ExpandVert" nonnull-widget) widget)]))

(define widget-position-set!
  (foreign-safe-lambda void "AG_WidgetSetPosition" nonnull-widget int int))

(define widget-size-set!
  (foreign-safe-lambda void "AG_WidgetSetSize" nonnull-widget int int))

(define (make-window #!key name caption main?)
  (let ([win
         ((foreign-safe-lambda nonnull-window "AG_WindowNew" unsigned-int)
          (bitwise-ior
            (if main? (foreign-value "AG_WINDOW_MAIN" unsigned-int) 0)))])
    (when name
      (set! (object-name win) name))
    (when caption
      (set! (window-caption win) caption))
    win))

(define-values (window-caption window-caption-set!)
  (let ([window-caption
         (foreign-lambda* c-string ([nonnull-window win]) "C_return(win->caption);")]
        [window-caption-set!
         (foreign-safe-lambda void "AG_WindowSetCaptionS" nonnull-window c-string)])
    (values
      (getter-with-setter window-caption window-caption-set!)
      window-caption-set!)))

(define-values (window-visible? window-visible-set!)
  (let ([window-visible?
         (foreign-lambda bool "AG_WindowIsVisible" nonnull-window)]
        [window-visible-set!
         (lambda (win show?)
           ((if show?
              (foreign-safe-lambda void "AG_WindowShow" nonnull-window)
              (foreign-safe-lambda void "AG_WindowHide" nonnull-window))
            win))])
    (values
      (getter-with-setter window-visible? window-visible-set!)
      window-visible-set!)))

(define (make-box parent orientation #!key name homogenous? padding spacing hfill? vfill? expand?)
  (let ([box
         ((foreign-safe-lambda nonnull-box "AG_BoxNew" widget int unsigned-int)
          parent
          (case orientation
            [(horizontal horiz) (foreign-value "AG_BOX_HORIZ" int)]
            [(vertical vert) (foreign-value "AG_BOX_VERT" int)])
          (bitwise-ior
            (if homogenous? (foreign-value "AG_BOX_HOMOGENOUS" unsigned-int) 0)
            (if (or hfill? expand?) (foreign-value "AG_BOX_HFILL" unsigned-int) 0)
            (if (or vfill? expand?) (foreign-value "AG_BOX_VFILL" unsigned-int) 0)))])
    (when name
      (set! (object-name box) name))
    (when padding
      ((foreign-safe-lambda void "AG_BoxSetPadding" nonnull-box int)
       box padding))
    (when spacing
      ((foreign-safe-lambda void "AG_BoxSetSpacing" nonnull-box int)
       box spacing))
    box))

(define (make-button parent #!key name label sticky? hfill? vfill? expand?)
  (let ([btn
         ((foreign-safe-lambda nonnull-button "AG_ButtonNewS" widget unsigned-int c-string)
          parent
          (bitwise-ior
            (if sticky? (foreign-value "AG_BUTTON_STICKY" unsigned-int) 0)
            (if (or hfill? expand?) (foreign-value "AG_BUTTON_HFILL" unsigned-int) 0)
            (if (or vfill? expand?) (foreign-value "AG_BUTTON_VFILL" unsigned-int) 0))
          label)])
    (when name
      (set! (object-name btn) name))
    btn))

(define (make-textbox parent #!key name label multiline? multilingual? password? int-only? flt-only? catch-tab? noemacs? nolatin1? nopopup? readonly? hfill? vfill? expand?)
  (let ([txt
         ((foreign-safe-lambda nonnull-textbox "AG_TextboxNewS" widget unsigned-int c-string)
          parent
          (bitwise-ior
            (if multiline? (foreign-value "AG_TEXTBOX_MULTILINE" unsigned-int) 0)
            (if multilingual? (foreign-value "AG_TEXTBOX_MULTILINGUAL" unsigned-int) 0)
            (if password? (foreign-value "AG_TEXTBOX_PASSWORD" unsigned-int) 0)
            (if int-only? (foreign-value "AG_TEXTBOX_INT_ONLY" unsigned-int) 0)
            (if flt-only? (foreign-value "AG_TEXTBOX_FLT_ONLY" unsigned-int) 0)
            (if catch-tab? (foreign-value "AG_TEXTBOX_CATCH_TAB" unsigned-int) 0)
            (if noemacs? (foreign-value "AG_TEXTBOX_NOEMACS" unsigned-int) 0)
            (if nolatin1? (foreign-value "AG_TEXTBOX_NOLATIN1" unsigned-int) 0)
            (if nopopup? (foreign-value "AG_TEXTBOX_NOPOPUP" unsigned-int) 0)
            (if readonly? (foreign-value "AG_TEXTBOX_READONLY" unsigned-int) 0)
            (if (or hfill? expand?) (foreign-value "AG_TEXTBOX_HFILL" unsigned-int) 0)
            (if (or vfill? expand?) (foreign-value "AG_TEXTBOX_VFILL" unsigned-int) 0))
          label)])
    (when name
      (set! (object-name txt) name))
    txt))

(define-values (textbox-string textbox-string-set!)
  (letrec ([textbox-copy-string
            (foreign-lambda size_t "AG_TextboxCopyString" nonnull-textbox scheme-pointer size_t)]
           [textbox-string
            (lambda (txt)
              (let* ([len (textbox-copy-string txt #f 0)]
                     [buf (make-string (add1 len))])
                (set! len (textbox-copy-string txt buf (string-length buf)))
                (substring buf 0 len)))]
           [textbox-string-set!
            (foreign-lambda void "AG_TextboxSetString" nonnull-textbox c-string)])
    (values
      (getter-with-setter textbox-string textbox-string-set!)
      textbox-string-set!)))

(define (event-loop)
  (check-agar-condition ((foreign-safe-lambda int "AG_EventLoop"))))

(define (terminate! #!optional v)
  ((foreign-safe-lambda void "AG_Terminate" int) (or v 0)))

(foreign-code
  "AG_SetFatalCallback(raise_fatal_condition);")

)

;; vim:set ft=scheme ts=2 sts=2 sw=2 ai: ;;

Added agar.setup.









































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
;; -*- mode: Scheme; tab-width: 2; -*- ;;

(define native-cflags
  (string-split (call-with-input-pipe "agar-config --cflags" read-all)))

(define native-libs
  (string-split (call-with-input-pipe "agar-config --libs" read-all)))

(compile -s -O2 -d1 "agar.scm" -j agar ,@native-cflags ,@native-libs)
(compile -c -O2 -d1 "agar.scm" -unit agar ,@native-cflags)
(compile -s -O2 -d0 "agar.import.scm")

(install-extension
 'agar
 `("agar.so" "agar.o" "agar-types.scm" "agar.import.so")
 `((version 0.1.0)
	 (static "agar.o")
	 (static-options ,(string-intersperse native-libs))))

;; vim:set ft=scheme ts=2 sts=2 sw=2 ai: ;;

Added agar.wiki.





































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
<h1>API Documentation</h1>

An extension is provided that you can import:

<verbatim>
  (require-extension agar)
</verbatim>

In addition, foreign types used by the library can be included:

<verbatim>
  (include "agar-types.scm")
</verbatim>

<h2>Error Handling</h2>

All the procedures do automatic error checking and may throw exceptions of the
kind <tt>(exn agar)</tt>. Note that some errors put the native library into an
inconsistent state.

<h3><a id="agar-condition_">procedure: <code><nowiki>(agar-condition? ANY) → BOOLEAN</nowiki></code></a></h3>

Checks whether the given object is a condition of kind <tt>(exn agar)</tt>.

<h3><a id="make-agar-condition">procedure: <code><nowiki>(make-agar-condition MESSAGE) → CONDITION</nowiki></code></a></h3>

Creates a new condition of kind <tt>(exn agar)</tt> with the given error
message.

<h3><a id="current-agar-condition">parameter: <code><nowiki>current-agar-condition</nowiki></code></a></h3>

Synthetic parameter holding the current error state of Agar as a condition
object. Returns <tt>#f</tt> or a condition of kind <tt>(exn agar)</tt> when
called without arguments. Turns any argument into an error message that is set
as the Agar error state.

<h3><a id="check-agar-condition">procedure: <code><nowiki>(check-agar-condition [STATUS]) → STATUS</nowiki></code></a></h3>

Checks if status is <tt>#f</tt> or negative. If that is the case, or if status
is not given at all, checks the values of <tt>current-agar-condition</tt>. If
there is an error state, it is raised as a continuable exception.

If there is no error state, the status (or an unspecified value) is returned.

<h2>Library Initialization</h2>

<h3><a id="init-agar_">procedure: <code><nowiki>(init-agar! [program: STRING] [verbose?: BOOLEAN] [create-datadir?: BOOLEAN] [soft-timers?: BOOLEAN] [graphics: BOOLEAN|STRING]) → UNSPECIFIED</nowiki></code></a></h3>

Initializes the Agar library, passing various flags as specified. If the
graphics keyword argument is given and either <tt>#t</tt> or a driver name, the
graphics subsystem is also initialized.

<h2>Object System</h2>

<h3><a id="object_">procedure: <code><nowiki>(object? ANY [CLASS]) → BOOLEAN</nowiki></code></a></h3>

Checks whether the given value is an Agar object (of the given class).

<h3><a id="lookup-object">procedure: <code><nowiki>(lookup-object OBJECT PATH) → OBJECT</nowiki></code></a></h3>

Locates an object by its name and the names of its parents, starting at a given
root object.

<h3><a id="make-object">procedure: <code><nowiki>(make-object CLASS [parent: OBJECT] [name: STRING]) → OBJECT</nowiki></code></a></h3>

Creates a new object of the given class, optionally attached to the given
parent and identified by the given name.

<h3><a id="destroy-object_">procedure: <code><nowiki>(destroy-object! OBJECT) → UNSPECIFIED</nowiki></code></a></h3>

Destroys the given object.

<h3><a id="object-class">procedure: <code><nowiki>(object-class OBJECT) → CLASS</nowiki></code></a></h3>

Returns the class descriptor of the given Agar object.

<h3><a id="object-name">procedure: <code><nowiki>(object-name OBJECT) → STRING</nowiki></code></a></h3>
<h3><a id="object-name-set_">procedure: <code><nowiki>(object-name-set! OBJECT STRING) → UNSPECIFIED</nowiki></code></a></h3>

Accessors for the name identifying an Agar object within the context of the
parent it is attached to.

<tt>object-name</tt> has <tt>object-name-set!</tt> attached as a setter.

<h3><a id="class_">procedure: <code><nowiki>(class? ANY) → BOOLEAN</nowiki></code></a></h3>

Checks whether the given value is an Agar class descriptor.

<h3><a id="lookup-class">procedure: <code><nowiki>(lookup-class NAME) → CLASS</nowiki></code></a></h3>

Locates an Agar class descriptor by name. Returns <tt>#f</tt> if no matching
class is found.

<h3><a id="class-hierarchy">procedure: <code><nowiki>(class-hierarchy CLASS) → STRING</nowiki></code></a></h3>

Returns the full name of the given class descriptor.

<h3><a id="class-version">procedure: <code><nowiki>(class-version CLASS) → (values INTEGER INTEGER)</nowiki></code></a></h3>

Returns the major and minor version of the given class descriptor.

<h3><a id="object-ref">procedure: <code><nowiki>(object-ref OBJECT NAME) → ANY</nowiki></code></a></h3>
<h3><a id="object-set_">procedure: <code><nowiki>(object-set! OBJECT NAME ANY) → UNSPECIFIED</nowiki></code></a></h3>

Accessors for the value of a bound variable identified by the given name in
the given Agar object.

<tt>object-ref</tt> has <tt>object-set!</tt> attached as a setter.

<h3><a id="event-add_">procedure: <code><nowiki>(event-add! OBJECT NAME PROC) → UNSPECIFIED</nowiki></code></a></h3>

Adds a handler procedure to the event identifier by the given name in the given
Agar object.

<h2>Widgets</h2>

<h3><a id="widget-expand_">procedure: <code><nowiki>(widget-expand! WIDGET [hfill: BOOLEAN] [vfill: BOOLEAN] [expand: BOOLEAN]) → UNSPECIFIED</nowiki></code></a></h3>

Makes the given widget fill available layout space in horizontal, vertical or
both directions.

<h3><a id="widget-position-set_">procedure: <code><nowiki>(widget-position-set! WIDGET X Y) → UNSPECIFIED</nowiki></code></a></h3>

Sets the layout position of the given widget.

<h3><a id="widget-size-set_">procedure: <code><nowiki>(widget-size-set! WIDGET WIDTH HEIGHT) → UNSPECIFIED</nowiki></code></a></h3>

Sets the layout dimensions of the given widget.

<h3><a id="make-window">procedure: <code><nowiki>(make-window [name: STRING] [caption: STRING] [main?: BOOLEAN]) → OBJECT</nowiki></code></a></h3>

Creates a new window, passing various flags and settings as specified.

<h3><a id="window-caption">procedure: <code><nowiki>(window-caption WINDOW) → STRING</nowiki></code></a></h3>
<h3><a id="window-caption-set_">procedure: <code><nowiki>(window-caption-set! WINDOW STRING) → UNSPECIFIED</nowiki></code></a></h3>

Accessors for the title of a given window.

<tt>window-caption</tt> has <tt>window-caption-set!</tt> attached as a setter.

<h3><a id="window-visible_">procedure: <code><nowiki>(window-visible? WINDOW) → STRING</nowiki></code></a></h3>
<h3><a id="window-visible-set_">procedure: <code><nowiki>(window-visible-set! WINDOW STRING) → UNSPECIFIED</nowiki></code></a></h3>

Accessors for the visibility state of a given window.

<tt>window-visible?</tt> has <tt>window-visible-set!</tt> attached as a setter.

<h3><a id="make-box">procedure: <code><nowiki>(make-box PARENT ORIENTATION [name: STRING] [homogenous?: BOOLEAN] [padding: INTEGER] [spacing: INTEGER] [hfill?: BOOLEAN] [vfill?: BOOLEAN] [expand?: BOOLEAN]) → OBJECT</nowiki></code></a></h3>

Creates a new layout container, passing various flags and settings as
specified. The orientation can be one of the symbols <tt>horizontal</tt>,
<tt>horiz</tt>, <tt>vertical</tt>, <tt>vert</tt>.

<h3><a id="make-button">procedure: <code><nowiki>(make-button PARENT [name: STRING] [label: STRING] [sticky?: BOOLEAN] [hfill?: BOOLEAN] [vfill?: BOOLEAN] [expand?: BOOLEAN]) → OBJECT</nowiki></code></a></h3>

Creates a new button, passing various flags and settings as specified.

<h3><a id="make-textbox">procedure: <code><nowiki>(make-textbox PARENT [name: STRING] [label: STRING] [multiline?: BOOLEAN] [multilingual?: BOOLEAN] [password?: BOOLEAN] [int-only?: BOOLEAN] [flt-only?: BOOLEAN] [catch-tab?: BOOLEAN] [noemacs?: BOOLEAN] [nolatin1?: BOOLEAN] [nopopup?: BOOLEAN] [readonly?: BOOLEAN] [hfill?: BOOLEAN] [vfill?: BOOLEAN] [expand?: BOOLEAN]) → OBJECT</nowiki></code></a></h3>

Creates a new textbox, passing various flags and settings as specified.

<h3><a id="textbox-string">procedure: <code><nowiki>(textbox-string TEXTBOX) → STRING</nowiki></code></a></h3>
<h3><a id="textbox-string-set_">procedure: <code><nowiki>(textbox-string-set! TEXTBOX STRING) → STRING</nowiki></code></a></h3>

Accessors for the string content in a textbox.

<tt>textbox-string</tt> has <tt>textbox-string-set!</tt> attached as a setter.

<h2>Event Loop</h2>

<h3><a id="event-loop">procedure: <code><nowiki>(event-loop) → INTEGER</nowiki></code></a></h3>

Runs the event loop until [#terminate_|<tt>terminate!</tt>] is called
(explicitly or implicitly). Returns an arbitrary exit status code.

<h3><a id="terminate_">procedure: <code><nowiki>(terminate! [INTEGER]) → UNSPECIFIED</nowiki></code></a></h3>

Terminates a running event loop, optionally passing the given status code.