1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.UnixUtils;
26 
27 private import glib.ErrorG;
28 private import glib.GException;
29 private import glib.Source;
30 private import glib.c.functions;
31 public  import glib.c.types;
32 public  import gtkc.glibtypes;
33 
34 
35 /** */
36 public struct UnixUtils
37 {
38 
39 	/** */
40 	public static GQuark errorQuark()
41 	{
42 		return g_unix_error_quark();
43 	}
44 
45 	/**
46 	 * Sets a function to be called when the IO condition, as specified by
47 	 * @condition becomes true for @fd.
48 	 *
49 	 * @function will be called when the specified IO condition becomes
50 	 * %TRUE.  The function is expected to clear whatever event caused the
51 	 * IO condition to become true and return %TRUE in order to be notified
52 	 * when it happens again.  If @function returns %FALSE then the watch
53 	 * will be cancelled.
54 	 *
55 	 * The return value of this function can be passed to g_source_remove()
56 	 * to cancel the watch at any time that it exists.
57 	 *
58 	 * The source will never close the fd -- you must do it yourself.
59 	 *
60 	 * Params:
61 	 *     fd = a file descriptor
62 	 *     condition = IO conditions to watch for on @fd
63 	 *     funct = a #GUnixFDSourceFunc
64 	 *     userData = data to pass to @function
65 	 *
66 	 * Returns: the ID (greater than 0) of the event source
67 	 *
68 	 * Since: 2.36
69 	 */
70 	public static uint fdAdd(int fd, GIOCondition condition, GUnixFDSourceFunc funct, void* userData)
71 	{
72 		return g_unix_fd_add(fd, condition, funct, userData);
73 	}
74 
75 	/**
76 	 * Sets a function to be called when the IO condition, as specified by
77 	 * @condition becomes true for @fd.
78 	 *
79 	 * This is the same as g_unix_fd_add(), except that it allows you to
80 	 * specify a non-default priority and a provide a #GDestroyNotify for
81 	 * @user_data.
82 	 *
83 	 * Params:
84 	 *     priority = the priority of the source
85 	 *     fd = a file descriptor
86 	 *     condition = IO conditions to watch for on @fd
87 	 *     funct = a #GUnixFDSourceFunc
88 	 *     userData = data to pass to @function
89 	 *     notify = function to call when the idle is removed, or %NULL
90 	 *
91 	 * Returns: the ID (greater than 0) of the event source
92 	 *
93 	 * Since: 2.36
94 	 */
95 	public static uint fdAddFull(int priority, int fd, GIOCondition condition, GUnixFDSourceFunc funct, void* userData, GDestroyNotify notify)
96 	{
97 		return g_unix_fd_add_full(priority, fd, condition, funct, userData, notify);
98 	}
99 
100 	/**
101 	 * Creates a #GSource to watch for a particular IO condition on a file
102 	 * descriptor.
103 	 *
104 	 * The source will never close the fd -- you must do it yourself.
105 	 *
106 	 * Params:
107 	 *     fd = a file descriptor
108 	 *     condition = IO conditions to watch for on @fd
109 	 *
110 	 * Returns: the newly created #GSource
111 	 *
112 	 * Since: 2.36
113 	 */
114 	public static Source fdSourceNew(int fd, GIOCondition condition)
115 	{
116 		auto p = g_unix_fd_source_new(fd, condition);
117 
118 		if(p is null)
119 		{
120 			return null;
121 		}
122 
123 		return new Source(cast(GSource*) p, true);
124 	}
125 
126 	/**
127 	 * Similar to the UNIX pipe() call, but on modern systems like Linux
128 	 * uses the pipe2() system call, which atomically creates a pipe with
129 	 * the configured flags. The only supported flag currently is
130 	 * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK, that
131 	 * must still be done separately with fcntl().
132 	 *
133 	 * This function does not take %O_CLOEXEC, it takes %FD_CLOEXEC as if
134 	 * for fcntl(); these are different on Linux/glibc.
135 	 *
136 	 * Params:
137 	 *     fds = Array of two integers
138 	 *     flags = Bitfield of file descriptor flags, as for fcntl()
139 	 *
140 	 * Returns: %TRUE on success, %FALSE if not (and errno will be set).
141 	 *
142 	 * Since: 2.30
143 	 *
144 	 * Throws: GException on failure.
145 	 */
146 	public static bool openPipe(int* fds, int flags)
147 	{
148 		GError* err = null;
149 
150 		auto p = g_unix_open_pipe(fds, flags, &err) != 0;
151 
152 		if (err !is null)
153 		{
154 			throw new GException( new ErrorG(err) );
155 		}
156 
157 		return p;
158 	}
159 
160 	/**
161 	 * Control the non-blocking state of the given file descriptor,
162 	 * according to @nonblock. On most systems this uses %O_NONBLOCK, but
163 	 * on some older ones may use %O_NDELAY.
164 	 *
165 	 * Params:
166 	 *     fd = A file descriptor
167 	 *     nonblock = If %TRUE, set the descriptor to be non-blocking
168 	 *
169 	 * Returns: %TRUE if successful
170 	 *
171 	 * Since: 2.30
172 	 *
173 	 * Throws: GException on failure.
174 	 */
175 	public static bool setFdNonblocking(int fd, bool nonblock)
176 	{
177 		GError* err = null;
178 
179 		auto p = g_unix_set_fd_nonblocking(fd, nonblock, &err) != 0;
180 
181 		if (err !is null)
182 		{
183 			throw new GException( new ErrorG(err) );
184 		}
185 
186 		return p;
187 	}
188 
189 	/**
190 	 * A convenience function for g_unix_signal_source_new(), which
191 	 * attaches to the default #GMainContext.  You can remove the watch
192 	 * using g_source_remove().
193 	 *
194 	 * Params:
195 	 *     signum = Signal number
196 	 *     handler = Callback
197 	 *     userData = Data for @handler
198 	 *
199 	 * Returns: An ID (greater than 0) for the event source
200 	 *
201 	 * Since: 2.30
202 	 */
203 	public static uint signalAdd(int signum, GSourceFunc handler, void* userData)
204 	{
205 		return g_unix_signal_add(signum, handler, userData);
206 	}
207 
208 	/**
209 	 * A convenience function for g_unix_signal_source_new(), which
210 	 * attaches to the default #GMainContext.  You can remove the watch
211 	 * using g_source_remove().
212 	 *
213 	 * Params:
214 	 *     priority = the priority of the signal source. Typically this will be in
215 	 *         the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
216 	 *     signum = Signal number
217 	 *     handler = Callback
218 	 *     userData = Data for @handler
219 	 *     notify = #GDestroyNotify for @handler
220 	 *
221 	 * Returns: An ID (greater than 0) for the event source
222 	 *
223 	 * Since: 2.30
224 	 */
225 	public static uint signalAddFull(int priority, int signum, GSourceFunc handler, void* userData, GDestroyNotify notify)
226 	{
227 		return g_unix_signal_add_full(priority, signum, handler, userData, notify);
228 	}
229 
230 	/**
231 	 * Create a #GSource that will be dispatched upon delivery of the UNIX
232 	 * signal @signum.  In GLib versions before 2.36, only `SIGHUP`, `SIGINT`,
233 	 * `SIGTERM` can be monitored.  In GLib 2.36, `SIGUSR1` and `SIGUSR2`
234 	 * were added. In GLib 2.54, `SIGWINCH` was added.
235 	 *
236 	 * Note that unlike the UNIX default, all sources which have created a
237 	 * watch will be dispatched, regardless of which underlying thread
238 	 * invoked g_unix_signal_source_new().
239 	 *
240 	 * For example, an effective use of this function is to handle `SIGTERM`
241 	 * cleanly; flushing any outstanding files, and then calling
242 	 * g_main_loop_quit ().  It is not safe to do any of this a regular
243 	 * UNIX signal handler; your handler may be invoked while malloc() or
244 	 * another library function is running, causing reentrancy if you
245 	 * attempt to use it from the handler.  None of the GLib/GObject API
246 	 * is safe against this kind of reentrancy.
247 	 *
248 	 * The interaction of this source when combined with native UNIX
249 	 * functions like sigprocmask() is not defined.
250 	 *
251 	 * The source will not initially be associated with any #GMainContext
252 	 * and must be added to one with g_source_attach() before it will be
253 	 * executed.
254 	 *
255 	 * Params:
256 	 *     signum = A signal number
257 	 *
258 	 * Returns: A newly created #GSource
259 	 *
260 	 * Since: 2.30
261 	 */
262 	public static Source signalSourceNew(int signum)
263 	{
264 		auto p = g_unix_signal_source_new(signum);
265 
266 		if(p is null)
267 		{
268 			return null;
269 		}
270 
271 		return new Source(cast(GSource*) p, true);
272 	}
273 }