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