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