Added within-main-loop and call-from-main-loop macros; ensured single initialization...
[cl-gtk2.git] / glib / glib.lisp
1 (defpackage :glib
2   (:use :cl :cffi :iter)
3   (:export #:gsize
4            #:gssize
5            #:goffset
6            #:*glib-major-version*
7            #:*glib-minor-version*
8            #:*glib-micro-version*
9            #:*glib-binary-age*
10            #:*glib-interface-age*
11            #:g-free
12            #:glist
13            #:gstrv
14            #:g-malloc
15            #:g-strdup
16            #:g-string
17            #:gslist
18            #:g-quark
19            #:+g-priority-high+
20            #:+g-priority-default+
21            #:+g-priority-high-idle+
22            #:+g-priority-default-idle+
23            #:+g-priority-low+
24            #:g-idle-add-full))
25
26 (in-package :glib)
27
28 (eval-when (:compile-toplevel :load-toplevel :execute)
29   (define-foreign-library glib
30     (:unix "libglib-2.0.so")
31     (t "glib-2.0")))
32
33 (use-foreign-library glib)
34
35 (load-foreign-library "libgthread-2.0.so")
36
37 ;;
38 ;; Glib Fundamentals
39 ;;
40
41 ;;
42 ;; Fundamentals - Basic types
43 ;;
44
45
46 ;; TODO: not sure about these: for amd64 they are ok
47 (eval-when (:compile-toplevel :load-toplevel :execute)
48   (if (cffi-features:cffi-feature-p :x86-64)
49       (defctype gsize :uint64)
50       (error "Unknown type 'gsize'")))
51
52 (defctype gssize :long)
53
54 (defctype goffset :uint64)
55
56
57 ;;
58 ;; Fundamentals - Version information
59 ;;
60
61 (defcvar (*glib-major-version* "glib_major_version" :read-only t :library glib) :uint)
62 (defcvar (*glib-minor-version* "glib_minor_version" :read-only t :library glib) :uint)
63 (defcvar (*glib-micro-version* "glib_micro_version" :read-only t :library glib) :uint)
64 (defcvar (*glib-binary-age* "glib_binary_age" :read-only t :library glib) :uint)
65 (defcvar (*glib-interface-age* "glib_interface_age" :read-only t :library glib) :uint)
66
67 ;;
68 ;; Omitted:
69 ;; Limits of Basic Types, Standard Macros, Type Conversion Macros, Byte Order Macros, 
70 ;; Numerical Definitions, Miscellaneous Macros, Atomic operations
71 ;;
72
73 ;; Core Application Support - The Main Event Loop
74
75 (defcstruct g-main-loop)
76 (defcstruct g-main-context)
77 (defcstruct g-source)
78 (defcstruct g-source-funcs
79   (prepare :pointer)
80   (check :pointer)
81   (dispatch :pointer)
82   (finalize :pointer)
83   (closure-callback :pointer)
84   (closure-marshal :pointer))
85 (defcstruct g-source-callback-funcs
86   (ref :pointer)
87   (unref :pointer)
88   (get :pointer))
89 (defcstruct g-cond)
90 (defcstruct g-mutex)
91
92 (defcstruct g-poll-fd
93   (fd :int) ;; TODO: #if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8
94   (events :ushort)
95   (revent :ushort))
96
97 (defcstruct g-time-val
98   (seconds :long)
99   (microseconds :long))
100
101 (defcstruct g-thread)
102
103 (defcfun (g-main-loop-new "g_main_loop_new" :library glib) (:pointer g-main-loop)
104   (context (:pointer g-main-context))
105   (is-running :boolean))
106
107 (defcfun (g-main-loop-ref "g_main_loop_ref" :library glib) (:pointer g-main-loop)
108   (loop (:pointer g-main-loop)))
109
110 (defcfun (g-main-loop-unref "g_main_loop_unref" :library glib) (:pointer g-main-loop)
111   (loop (:pointer g-main-loop)))
112
113 (defcfun (g-main-loop-run "g_main_loop_run" :library glib) :void
114   (loop (:pointer g-main-loop)))
115
116 (defcfun (g-main-loop-quit "g_main_loop_quit" :library glib) :void
117   (loop (:pointer g-main-loop)))
118
119 (defcfun (g-main-loop-is-running "g_main_loop_is_running" :library glib) :boolean
120   (loop (:pointer g-main-loop)))
121
122 (defcfun (g-main-loop-get-context "g_main_loop_get_context" :library glib) (:pointer g-main-context)
123   (loop (:pointer g-main-loop)))
124
125 (defconstant +g-priority-high+ -100)
126 (defconstant +g-priority-default+ 0)
127 (defconstant +g-priority-high-idle+ 100)
128 (defconstant +g-priority-default-idle+ 200)
129 (defconstant +g-priority-low+ 300)
130
131 (defcfun (g-main-context-new "g_main_context_new" :library glib) (:pointer g-main-context))
132
133 (defcfun (g-main-context-ref "g_main_context_ref" :library glib) (:pointer g-main-context)
134   (context (:pointer g-main-context)))
135
136 (defcfun (g-main-context-unref "g_main_context_unref" :library glib) (:pointer g-main-context)
137   (context (:pointer g-main-context)))
138
139 (defcfun (g-main-context-default "g_main_context_default" :library glib) (:pointer g-main-context))
140
141 (defcfun (g-main-context-iteration "g_main_context_iteration" :library glib) :boolean
142   (context (:pointer g-main-context))
143   (may-block :boolean))
144
145 (defcfun (g-main-context-pending "g_main_context_pending" :library glib) :boolean
146   (context (:pointer g-main-context)))
147
148 (defcfun (g-main-context-find-source-by-id "g_main_context_find_source_by_id" :library glib) (:pointer g-source)
149   (context (:pointer g-main-context))
150   (source-id :uint))
151
152 (defcfun (g-main-context-find-source-by-user-data "g_main_context_find_source_by_user_data" :library glib) (:pointer g-source)
153   (context (:pointer g-main-context))
154   (user-data :pointer))
155
156 (defcfun (g-main-context-find-source-by-funcs-user-data "g_main_context_find_source_by_funcs_user_data" :library glib) (:pointer g-source)
157   (context (:pointer g-main-context))
158   (funcs (:pointer g-source-funcs))
159   (user-data :pointer))
160
161 (defcfun (g-main-context-wakeup "g_main_context_wakeup" :library glib) :void
162   (context (:pointer g-main-context)))
163
164 (defcfun (g-main-context-acquire "g_main_context_acquire" :library glib) :boolean
165   (context (:pointer g-main-context)))
166
167 (defcfun (g-main-context-release "g_main_context_release" :library glib) :void
168   (context (:pointer g-main-context)))
169
170 (defcfun (g-main-context-is-owner "g_main_context_is_owner" :library glib) :boolean
171   (context (:pointer g-main-context)))
172
173 (defcfun (g-main-context-wait "g_main_context_wait" :library glib) :boolean
174   (context (:pointer g-main-context))
175   (cond (:pointer g-cond))
176   (mutex (:pointer g-mutex)))
177
178 (defcfun (g_main_context_prepare "g_main_context_prepare" :library glib) :boolean
179   (context (:pointer g-main-context))
180   (priority-ret (:pointer :int)))
181
182 (defcfun (g_main_context_query "g_main_context_query" :library glib) :int
183   (context (:pointer g-main-context))
184   (max-priority :int)
185   (timeout-ret (:pointer :int))
186   (fds-ret (:pointer g-poll-fd))
187   (n-dfs :int))
188
189 (defcfun (g-main-context-check "g_main_context_check" :library glib) :int
190   (context (:pointer g-main-context))
191   (max-priority :int)
192   (fds (:pointer g-poll-fd))
193   (n-fds :int))
194
195 (defcfun (g-main-context-dispatch "g_main_context_dispatch" :library glib) :void
196   (context (:pointer g-main-context)))
197
198 (defcfun (g-main-context-set-poll-func "g_main_context_set_poll_func" :library glib) :void
199   (context (:pointer g-main-context))
200   (func :pointer))
201
202 (defcfun (g-main-context-get-poll-func "g_main_context_get_poll_func" :library glib) :pointer
203   (context (:pointer g-main-context)))
204
205 (defcfun (g-main-context-add-poll "g_main_context_add_poll" :library glib) :void
206   (context (:pointer g-main-context))
207   (fd (:pointer g-poll-fd))
208   (priority :int))
209
210 (defcfun (g-main-context-remove-poll "g_main_context_remove_poll" :library glib) :void
211   (context (:pointer g-main-context))
212   (fd (:pointer g-poll-fd)))
213
214 (defcfun (g-main-depth "g_main_depth" :library glib) :int)
215
216 (defcfun (g-main-current-source "g_main_current_source" :library glib) (:pointer g-source))
217
218 (defcfun (g-timeout-source-new "g_timeout_source_new" :library glib) (:pointer g-source)
219   (interval-milliseconds :int))
220
221 (defcfun (g-timeout-source-new-seconds "g_timeout_source_new_seconds" :library glib) (:pointer g-source)
222   (interval-seconds :int))
223
224 (defcfun (g-timeout-add "g_timeout_add" :library glib) :uint
225   (interval-milliseconds :uint)
226   (function :pointer)
227   (data :pointer))
228
229 (defcfun (g-timeout-add-full "g_timeout_add_full" :library glib) :uint
230   (priority :int)
231   (interval-milliseconds :uint)
232   (function :pointer)
233   (data :pointer)
234   (destroy-notify :pointer))
235
236 (defcfun (g-timeout-add-seconds "g_timeout_add_seconds" :library glib) :uint
237   (interval-seconds :uint)
238   (function :pointer)
239   (data :pointer))
240
241 (defcfun (g-timeout-add-seconds-full "g_timeout_add_seconds_full" :library glib) :uint
242   (priority :int)
243   (interval-seconds :uint)
244   (function :pointer)
245   (data :pointer)
246   (destroy-notify :pointer))
247
248 (defcfun (g-idle-source-new "g_idle_source_new" :library glib) (:pointer g-source))
249
250 (defcfun (g-idle-add "g_idle_add" :library glib) :uint
251   (function :pointer)
252   (data :pointer))
253
254 (defcfun (g-idle-add-full "g_idle_add_full" :library glib) :uint
255   (priority :uint)
256   (function :pointer)
257   (data :pointer)
258   (notify :pointer))
259
260 (defcfun (g-idle-remove-by-data "g_idle_remove_by_data" :library glib) :boolean
261   (data :pointer))
262
263 ;(defctype g-pid :int) ;;TODO: might work on amd64 linux, but on others
264
265 ;; Omitted GPid, g_child_add_watch, g_child_add_watch_full
266
267 (defcfun (g-source-new "g_source_new" :library glib) (:pointer g-source)
268   (source-funcs (:pointer g-source-funcs))
269   (struct-size :uint))
270
271 (defcfun (g-source-ref "g_source_ref" :library glib) (:pointer g-source)
272   (source (:pointer g-source)))
273
274 (defcfun (g-source-unref "g_source_unref" :library glib) :void
275   (source (:pointer g-source)))
276
277 (defcfun (g-source-set-funcs "g_source_set_funcs" :library glib) :void
278   (source (:pointer g-source))
279   (funcs (:pointer g-source-funcs)))
280
281 (defcfun (g-source-attach "g_source_attach" :library glib) :uint
282   (source (:pointer g-source))
283   (context (:pointer g-main-context)))
284
285 (defcfun (g-source-destroy "g_source_destroy" :library glib) :void
286   (source (:pointer g-source)))
287
288 (defcfun (g-source-is-destroyed "g_source_is_destroyed" :library glib) :boolean
289   (source (:pointer g-source)))
290
291 (defcfun (g-source-set-priority "g_source_set_priority" :library glib) :void
292   (source (:pointer g-source))
293   (priority :int))
294
295 (defcfun (g-source-get-priority "g_source_get_priority" :library glib) :int
296   (source (:pointer g-source)))
297
298 (defcfun (g-source-set-can-recurse "g_source_set_can_recurse" :library glib) :void
299   (source (:pointer g-source))
300   (can-recurse :boolean))
301
302 (defcfun (g-source-get-can-recurse "g_source_get_can_recurse" :library glib) :boolean
303   (source (:pointer g-source)))
304
305 (defcfun (g-source-get-id "g_source_get_id" :library glib) :uint
306   (source (:pointer g-source)))
307
308 (defcfun (g-source-get-context "g_source_get_context" :library glib) (:pointer g-main-context)
309   (source (:pointer g-source)))
310
311 (defcfun (g-source-set-callback "g_source_set_callback" :library glib) :void
312   (source (:pointer g-source))
313   (func :pointer)
314   (data :pointer)
315   (notify :pointer))
316
317 (defcfun (g-source-add-poll "g_source_add_poll" :library glib) :void
318   (source (:pointer g-source))
319   (fd (:pointer g-poll-fd)))
320
321 (defcfun (g-source-remove-poll "g_source_remove_poll" :library glib) :void
322   (source (:pointer g-source))
323   (fd (:pointer g-poll-fd)))
324
325 (defcfun (g-source-get-current-time "g_source_get_current_time" :library glib) :void
326   (source (:pointer g-source))
327   (timeval-ret (:pointer g-time-val)))
328
329 (defcfun (g-source-remove "g_source_remove" :library glib) :boolean
330   (id :uint))
331
332 (defcfun (g-source-remove-by-funcs-user-data "g_source_remove_by_funcs_user_data" :library glib) :boolean
333   (funcs (:pointer g-source-funcs))
334   (data :pointer))
335
336 (defcfun (g-source-remove-by-user-data "g_source_remove_by_user_data" :library glib) :boolean
337   (data :pointer))
338
339 ;;
340 ;; Core Application Support - Threads
341 ;;
342
343 (defcenum g-thread-error
344   :g-thread-error-again)
345
346 ;omitted: struct GThreadFunctions
347
348 (defcfun (g-thread-init "g_thread_init") :void
349   (vtable :pointer))
350
351 (defvar *threads-initialized-p* nil)
352
353 (unless *threads-initialized-p*
354   (g-thread-init (null-pointer))
355   (setf *threads-initialized-p* t))
356
357 (defcenum g-thread-priority
358   :g-thread-priority-low
359   :g-thread-priority-normal
360   :g-thread-priority-hight
361   :g-thread-priority-urgent)
362
363 ;omitted: g_thread_create, g_thread_create_full, g_thread_yield, g_thread_exit, g_thread_foreach
364
365 (defcfun (g-thread-self "g_thread_self" :library glib) (:pointer g-thread))
366
367 (defcfun (g-thread-join "g_thread_join" :library glib) :pointer
368   (thread (:pointer g-thread)))
369
370 (defcfun (g-thread-priority "g_thread_set_priority" :library glib) :void
371   (thread (:pointer g-thread))
372   (priority g-thread-priority))
373
374 (defcfun (g-mutex-new "g_mutex_new" :library glib) (:pointer g-mutex))
375
376 (defcfun (g-mutex-lock "g_mutex_lock" :library glib) :void
377   (mutex (:pointer g-mutex)))
378
379 (defcfun (g-mutex-try-lock "g_mutex_trylock" :library glib) :boolean
380   (mutex (:pointer g-mutex)))
381
382 (defcfun (g-mutex-free "g_mutex_free" :library glib) :void
383   (mutex (:pointer g-mutex)))
384
385 ;omitted: GStaticMutex, GStaticRWLock stuff
386
387 (defcfun (g-cond-new "g_cond_new" :library glib) (:pointer g-cond))
388
389 (defcfun (g-cond-signal "g_cond_signal" :library glib) :void
390   (cond (:pointer g-cond)))
391
392 (defcfun (g-cond-broadcast "g_cond_broadcast" :library glib) :void
393   (cond (:pointer g-cond)))
394
395 (defcfun (g-cond-wait "g_cond_wait" :library glib) :void
396   (cond (:pointer g-cond))
397   (mutex (:pointer g-mutex)))
398
399 (defcfun (g-cond-timed-wait "g_cond_timed_wait" :library glib) :boolean
400   (cond (:pointer g-cond))
401   (mutex (:pointer g-mutex))
402   (abs-time (:pointer g-time-val)))
403
404 (defcfun (g-cond-free "g_cond_free" :library glib) :void
405   (cond (:pointer g-cond)))
406
407 ;omitted: GPrivate, GOnce stuff
408
409 ;omitted: Thread pools, Asynchronous queues, Dynamic Loading of Modules,
410 ; Memory Allocation, IO Channels, Error Reporting, Message Output and Debugging  Functions, Message Logging
411
412 (defcfun g-free :void
413   (ptr :pointer))
414
415 (defcfun (g-malloc "g_malloc0") :pointer
416   (n-bytes gsize))
417
418 (defcfun g-strdup :pointer
419   (str (:string :free-to-foreign t)))
420
421 ;omitted all GLib Utilites
422 ;TODO: omitted Date and Time Functions
423