1// Package glib was automatically generated by github.com/bnema/puregotk DO NOT EDIT
2package glib
3
4import (
5 "structs"
6 "unsafe"
7
8 "github.com/ebitengine/purego"
9
10 "github.com/bnema/puregotk/pkg/core"
11)
12
13// The type of functions to be called when a UNIX fd watch source
14// triggers.
15type UnixFDSourceFunc func(int, IOCondition, uintptr) bool
16
17// A Unix pipe. The advantage of this type over `int[2]` is that it can
18// be closed automatically when it goes out of scope, using `g_auto(GUnixPipe)`,
19// on compilers that support that feature.
20type UnixPipe struct {
21 _ structs.HostLayout
22
23 Fds [2]int
24}
25
26func (x *UnixPipe) GoPointer() uintptr {
27 return uintptr(unsafe.Pointer(x))
28}
29
30// Mnemonic constants for the ends of a Unix pipe.
31type UnixPipeEnd int
32
33const (
34
35 // The readable file descriptor 0
36 GUnixPipeEndReadValue UnixPipeEnd = 0
37 // The writable file descriptor 1
38 GUnixPipeEndWriteValue UnixPipeEnd = 1
39)
40
41var xClosefrom func(int) int
42
43// Close every file descriptor equal to or greater than @lowfd.
44//
45// Typically @lowfd will be 3, to leave standard input, standard output
46// and standard error open.
47//
48// This is the same as Linux `close_range (lowfd, ~0U, 0)`,
49// but portable to other OSs and to older versions of Linux.
50// Equivalently, it is the same as BSD `closefrom (lowfd)`, but portable,
51// and async-signal-safe on all OSs.
52//
53// This function is async-signal safe, making it safe to call from a
54// signal handler or a [callback@GLib.SpawnChildSetupFunc], as long as @lowfd is
55// non-negative.
56// See [`signal(7)`](man:signal(7)) and
57// [`signal-safety(7)`](man:signal-safety(7)) for more details.
58func Closefrom(LowfdVar int) int {
59
60 cret := xClosefrom(LowfdVar)
61
62 return cret
63}
64
65var xFdwalkSetCloexec func(int) int
66
67// Mark every file descriptor equal to or greater than @lowfd to be closed
68// at the next `execve()` or similar, as if via the `FD_CLOEXEC` flag.
69//
70// Typically @lowfd will be 3, to leave standard input, standard output
71// and standard error open after exec.
72//
73// This is the same as Linux `close_range (lowfd, ~0U, CLOSE_RANGE_CLOEXEC)`,
74// but portable to other OSs and to older versions of Linux.
75//
76// This function is async-signal safe, making it safe to call from a
77// signal handler or a [callback@GLib.SpawnChildSetupFunc], as long as @lowfd is
78// non-negative.
79// See [`signal(7)`](man:signal(7)) and
80// [`signal-safety(7)`](man:signal-safety(7)) for more details.
81func FdwalkSetCloexec(LowfdVar int) int {
82
83 cret := xFdwalkSetCloexec(LowfdVar)
84
85 return cret
86}
87
88var xUnixFdAdd func(int, IOCondition, uintptr, uintptr) uint
89
90// Sets a function to be called when the IO condition, as specified by
91// @condition becomes true for @fd.
92//
93// @function will be called when the specified IO condition becomes
94// %TRUE. The function is expected to clear whatever event caused the
95// IO condition to become true and return %TRUE in order to be notified
96// when it happens again. If @function returns %FALSE then the watch
97// will be cancelled.
98//
99// The return value of this function can be passed to g_source_remove()
100// to cancel the watch at any time that it exists.
101//
102// The source will never close the fd -- you must do it yourself.
103func UnixFdAdd(FdVar int, ConditionVar IOCondition, FunctionVar *UnixFDSourceFunc, UserDataVar uintptr) uint {
104
105 var FunctionVarRef uintptr
106 if FunctionVar != nil {
107 FunctionVarPtr := uintptr(unsafe.Pointer(FunctionVar))
108 if cbRefPtr, ok := GetCallback(FunctionVarPtr); ok {
109 FunctionVarRef = cbRefPtr
110 } else {
111 fcb := func(arg0 int, arg1 IOCondition, arg2 uintptr) bool {
112 cbFn := *FunctionVar
113 return cbFn(arg0, arg1, arg2)
114 }
115 FunctionVarRef = purego.NewCallback(fcb)
116 SaveCallbackWithClosure(FunctionVarPtr, FunctionVarRef, FunctionVar)
117 }
118 }
119
120 cret := xUnixFdAdd(FdVar, ConditionVar, FunctionVarRef, UserDataVar)
121
122 return cret
123}
124
125var xUnixFdAddFull func(int, int, IOCondition, uintptr, uintptr, uintptr) uint
126
127// Sets a function to be called when the IO condition, as specified by
128// @condition becomes true for @fd.
129//
130// This is the same as g_unix_fd_add(), except that it allows you to
131// specify a non-default priority and a provide a #GDestroyNotify for
132// @user_data.
133func UnixFdAddFull(PriorityVar int, FdVar int, ConditionVar IOCondition, FunctionVar *UnixFDSourceFunc, UserDataVar uintptr, NotifyVar *DestroyNotify) uint {
134
135 var FunctionVarRef uintptr
136 if FunctionVar != nil {
137 FunctionVarPtr := uintptr(unsafe.Pointer(FunctionVar))
138 if cbRefPtr, ok := GetCallback(FunctionVarPtr); ok {
139 FunctionVarRef = cbRefPtr
140 } else {
141 fcb := func(arg0 int, arg1 IOCondition, arg2 uintptr) bool {
142 cbFn := *FunctionVar
143 return cbFn(arg0, arg1, arg2)
144 }
145 FunctionVarRef = purego.NewCallback(fcb)
146 SaveCallbackWithClosure(FunctionVarPtr, FunctionVarRef, FunctionVar)
147 }
148 }
149
150 var NotifyVarRef uintptr
151 if NotifyVar != nil {
152 NotifyVarPtr := uintptr(unsafe.Pointer(NotifyVar))
153 if cbRefPtr, ok := GetCallback(NotifyVarPtr); ok {
154 NotifyVarRef = cbRefPtr
155 } else {
156 fcb := func(arg0 uintptr) {
157 cbFn := *NotifyVar
158 cbFn(arg0)
159 }
160 NotifyVarRef = purego.NewCallback(fcb)
161 SaveCallbackWithClosure(NotifyVarPtr, NotifyVarRef, NotifyVar)
162 }
163 }
164
165 cret := xUnixFdAddFull(PriorityVar, FdVar, ConditionVar, FunctionVarRef, UserDataVar, NotifyVarRef)
166
167 return cret
168}
169
170var xUnixFdSourceNew func(int, IOCondition) *Source
171
172// Creates a #GSource to watch for a particular I/O condition on a file
173// descriptor.
174//
175// The source will never close the @fd — you must do it yourself.
176//
177// Any callback attached to the returned #GSource must have type
178// #GUnixFDSourceFunc.
179func UnixFdSourceNew(FdVar int, ConditionVar IOCondition) *Source {
180
181 cret := xUnixFdSourceNew(FdVar, ConditionVar)
182
183 return cret
184}
185
186var xUnixGetPasswdEntry func(string, **Error) uintptr
187
188// Get the `passwd` file entry for the given @user_name using `getpwnam_r()`.
189// This can fail if the given @user_name doesn’t exist.
190//
191// The returned `struct passwd` has been allocated using g_malloc() and should
192// be freed using g_free(). The strings referenced by the returned struct are
193// included in the same allocation, so are valid until the `struct passwd` is
194// freed.
195//
196// This function is safe to call from multiple threads concurrently.
197//
198// You will need to include `pwd.h` to get the definition of `struct passwd`.
199func UnixGetPasswdEntry(UserNameVar string) (uintptr, error) {
200 var cerr *Error
201
202 cret := xUnixGetPasswdEntry(UserNameVar, &cerr)
203
204 if cerr == nil {
205 return cret, nil
206 }
207 return cret, cerr
208
209}
210
211var xUnixOpenPipe func(*[2]int, int, **Error) bool
212
213// Similar to the UNIX pipe() call, but on modern systems like Linux
214// uses the pipe2() system call, which atomically creates a pipe with
215// the configured flags.
216//
217// As of GLib 2.78, the supported flags are `O_CLOEXEC`/`FD_CLOEXEC` (see below)
218// and `O_NONBLOCK`. Prior to GLib 2.78, only `FD_CLOEXEC` was supported — if
219// you wanted to configure `O_NONBLOCK` then that had to be done separately with
220// `fcntl()`.
221//
222// Since GLib 2.80, the constants %G_UNIX_PIPE_END_READ and
223// %G_UNIX_PIPE_END_WRITE can be used as mnemonic indexes in @fds.
224//
225// It is a programmer error to call this function with unsupported flags, and a
226// critical warning will be raised.
227//
228// As of GLib 2.78, it is preferred to pass `O_CLOEXEC` in, rather than
229// `FD_CLOEXEC`, as that matches the underlying `pipe()` API more closely. Prior
230// to 2.78, only `FD_CLOEXEC` was supported. Support for `FD_CLOEXEC` may be
231// deprecated and removed in future.
232func UnixOpenPipe(FdsVar [2]int, FlagsVar int) (bool, error) {
233 var cerr *Error
234
235 cret := xUnixOpenPipe(&FdsVar, FlagsVar, &cerr)
236
237 if cerr == nil {
238 return cret, nil
239 }
240 return cret, cerr
241
242}
243
244var xUnixSetFdNonblocking func(int, bool, **Error) bool
245
246// Control the non-blocking state of the given file descriptor,
247// according to @nonblock. On most systems this uses %O_NONBLOCK, but
248// on some older ones may use %O_NDELAY.
249func UnixSetFdNonblocking(FdVar int, NonblockVar bool) (bool, error) {
250 var cerr *Error
251
252 cret := xUnixSetFdNonblocking(FdVar, NonblockVar, &cerr)
253
254 if cerr == nil {
255 return cret, nil
256 }
257 return cret, cerr
258
259}
260
261var xUnixSignalAdd func(int, uintptr, uintptr) uint
262
263// A convenience function for g_unix_signal_source_new(), which
264// attaches to the default #GMainContext. You can remove the watch
265// using g_source_remove().
266func UnixSignalAdd(SignumVar int, HandlerVar *SourceFunc, UserDataVar uintptr) uint {
267
268 var HandlerVarRef uintptr
269 if HandlerVar != nil {
270 HandlerVarPtr := uintptr(unsafe.Pointer(HandlerVar))
271 if cbRefPtr, ok := GetCallback(HandlerVarPtr); ok {
272 HandlerVarRef = cbRefPtr
273 } else {
274 fcb := func(arg0 uintptr) bool {
275 cbFn := *HandlerVar
276 return cbFn(arg0)
277 }
278 HandlerVarRef = purego.NewCallback(fcb)
279 SaveCallbackWithClosure(HandlerVarPtr, HandlerVarRef, HandlerVar)
280 }
281 }
282
283 cret := xUnixSignalAdd(SignumVar, HandlerVarRef, UserDataVar)
284
285 return cret
286}
287
288var xUnixSignalAddFull func(int, int, uintptr, uintptr, uintptr) uint
289
290// A convenience function for g_unix_signal_source_new(), which
291// attaches to the default #GMainContext. You can remove the watch
292// using g_source_remove().
293func UnixSignalAddFull(PriorityVar int, SignumVar int, HandlerVar *SourceFunc, UserDataVar uintptr, NotifyVar *DestroyNotify) uint {
294
295 var HandlerVarRef uintptr
296 if HandlerVar != nil {
297 HandlerVarPtr := uintptr(unsafe.Pointer(HandlerVar))
298 if cbRefPtr, ok := GetCallback(HandlerVarPtr); ok {
299 HandlerVarRef = cbRefPtr
300 } else {
301 fcb := func(arg0 uintptr) bool {
302 cbFn := *HandlerVar
303 return cbFn(arg0)
304 }
305 HandlerVarRef = purego.NewCallback(fcb)
306 SaveCallbackWithClosure(HandlerVarPtr, HandlerVarRef, HandlerVar)
307 }
308 }
309
310 var NotifyVarRef uintptr
311 if NotifyVar != nil {
312 NotifyVarPtr := uintptr(unsafe.Pointer(NotifyVar))
313 if cbRefPtr, ok := GetCallback(NotifyVarPtr); ok {
314 NotifyVarRef = cbRefPtr
315 } else {
316 fcb := func(arg0 uintptr) {
317 cbFn := *NotifyVar
318 cbFn(arg0)
319 }
320 NotifyVarRef = purego.NewCallback(fcb)
321 SaveCallbackWithClosure(NotifyVarPtr, NotifyVarRef, NotifyVar)
322 }
323 }
324
325 cret := xUnixSignalAddFull(PriorityVar, SignumVar, HandlerVarRef, UserDataVar, NotifyVarRef)
326
327 return cret
328}
329
330var xUnixSignalSourceNew func(int) *Source
331
332// Create a #GSource that will be dispatched upon delivery of the UNIX
333// signal @signum. In GLib versions before 2.36, only `SIGHUP`, `SIGINT`,
334// `SIGTERM` can be monitored. In GLib 2.36, `SIGUSR1` and `SIGUSR2`
335// were added. In GLib 2.54, `SIGWINCH` was added.
336//
337// Note that unlike the UNIX default, all sources which have created a
338// watch will be dispatched, regardless of which underlying thread
339// invoked g_unix_signal_source_new().
340//
341// For example, an effective use of this function is to handle `SIGTERM`
342// cleanly; flushing any outstanding files, and then calling
343// g_main_loop_quit(). It is not safe to do any of this from a regular
344// UNIX signal handler; such a handler may be invoked while malloc() or
345// another library function is running, causing reentrancy issues if the
346// handler attempts to use those functions. None of the GLib/GObject
347// API is safe against this kind of reentrancy.
348//
349// The interaction of this source when combined with native UNIX
350// functions like sigprocmask() is not defined.
351//
352// The source will not initially be associated with any #GMainContext
353// and must be added to one with g_source_attach() before it will be
354// executed.
355func UnixSignalSourceNew(SignumVar int) *Source {
356
357 cret := xUnixSignalSourceNew(SignumVar)
358
359 return cret
360}
361
362func init() {
363 core.SetPackageName("GLIB", "glib-2.0")
364 core.SetSharedLibraries("GLIB", []string{"libgobject-2.0.so.0", "libglib-2.0.so.0"})
365 var libs []uintptr
366 for _, libPath := range core.GetPaths("GLIB") {
367 lib, err := purego.Dlopen(libPath, purego.RTLD_NOW|purego.RTLD_GLOBAL)
368 if err != nil {
369 panic(err)
370 }
371 libs = append(libs, lib)
372 }
373
374 core.PuregoSafeRegister(&xClosefrom, libs, "g_closefrom")
375 core.PuregoSafeRegister(&xFdwalkSetCloexec, libs, "g_fdwalk_set_cloexec")
376 core.PuregoSafeRegister(&xUnixFdAdd, libs, "g_unix_fd_add")
377 core.PuregoSafeRegister(&xUnixFdAddFull, libs, "g_unix_fd_add_full")
378 core.PuregoSafeRegister(&xUnixFdSourceNew, libs, "g_unix_fd_source_new")
379 core.PuregoSafeRegister(&xUnixGetPasswdEntry, libs, "g_unix_get_passwd_entry")
380 core.PuregoSafeRegister(&xUnixOpenPipe, libs, "g_unix_open_pipe")
381 core.PuregoSafeRegister(&xUnixSetFdNonblocking, libs, "g_unix_set_fd_nonblocking")
382 core.PuregoSafeRegister(&xUnixSignalAdd, libs, "g_unix_signal_add")
383 core.PuregoSafeRegister(&xUnixSignalAddFull, libs, "g_unix_signal_add_full")
384 core.PuregoSafeRegister(&xUnixSignalSourceNew, libs, "g_unix_signal_source_new")
385
386}