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