gi-glib-2.0.17: GLib bindings

Copyright | Will Thompson Iñaki García Etxebarria and Jonas Platte
License | LGPL-2.1
Maintainer | Iñaki García Etxebarria (garetxe@gmail.com)
Safe Haskell | None
Language | Haskell2010

GI.GLib.Structs.Source

Contents

- Exported types
- Methods
  - addChildSource
  - addPoll
  - addUnixFd
  - attach
  - destroy
  - getCanRecurse
  - getContext
  - getCurrentTime
  - getId
  - getName
  - getPriority
  - getReadyTime
  - getTime
  - isDestroyed
  - modifyUnixFd
  - new
  - queryUnixFd
  - ref
  - remove
  - removeByFuncsUserData
  - removeByUserData
  - removeChildSource
  - removePoll
  - removeUnixFd
  - setCallback
  - setCallbackIndirect
  - setCanRecurse
  - setFuncs
  - setName
  - setNameById
  - setPriority
  - setReadyTime
  - unref

Description

The `GSource` struct is an opaque data type representing an event source.

Synopsis

- newtype Source = Source (ManagedPtr Source)
- newZeroSource :: MonadIO m => m Source
- noSource :: Maybe Source
- sourceAddChildSource :: (HasCallStack, MonadIO m) => Source -> Source -> m ()
- sourceAddPoll :: (HasCallStack, MonadIO m) => Source -> PollFD -> m ()
- sourceAddUnixFd :: (HasCallStack, MonadIO m) => Source -> Int32 -> [IOCondition] -> m (Ptr ())
- sourceAttach :: (HasCallStack, MonadIO m) => Source -> Maybe MainContext -> m Word32
- sourceDestroy :: (HasCallStack, MonadIO m) => Source -> m ()
- sourceGetCanRecurse :: (HasCallStack, MonadIO m) => Source -> m Bool
- sourceGetContext :: (HasCallStack, MonadIO m) => Source -> m (Maybe MainContext)
- sourceGetCurrentTime :: (HasCallStack, MonadIO m) => Source -> TimeVal -> m ()
- sourceGetId :: (HasCallStack, MonadIO m) => Source -> m Word32
- sourceGetName :: (HasCallStack, MonadIO m) => Source -> m Text
- sourceGetPriority :: (HasCallStack, MonadIO m) => Source -> m Int32
- sourceGetReadyTime :: (HasCallStack, MonadIO m) => Source -> m Int64
- sourceGetTime :: (HasCallStack, MonadIO m) => Source -> m Int64
- sourceIsDestroyed :: (HasCallStack, MonadIO m) => Source -> m Bool
- sourceModifyUnixFd :: (HasCallStack, MonadIO m) => Source -> Ptr () -> [IOCondition] -> m ()
- sourceNew :: (HasCallStack, MonadIO m) => SourceFuncs -> Word32 -> m Source
- sourceQueryUnixFd :: (HasCallStack, MonadIO m) => Source -> Ptr () -> m [IOCondition]
- sourceRef :: (HasCallStack, MonadIO m) => Source -> m Source
- sourceRemove :: (HasCallStack, MonadIO m) => Word32 -> m Bool
- sourceRemoveByFuncsUserData :: (HasCallStack, MonadIO m) => SourceFuncs -> Ptr () -> m Bool
- sourceRemoveByUserData :: (HasCallStack, MonadIO m) => Ptr () -> m Bool
- sourceRemoveChildSource :: (HasCallStack, MonadIO m) => Source -> Source -> m ()
- sourceRemovePoll :: (HasCallStack, MonadIO m) => Source -> PollFD -> m ()
- sourceRemoveUnixFd :: (HasCallStack, MonadIO m) => Source -> Ptr () -> m ()
- sourceSetCallback :: (HasCallStack, MonadIO m) => Source -> SourceFunc -> m ()
- sourceSetCallbackIndirect :: (HasCallStack, MonadIO m) => Source -> Ptr () -> SourceCallbackFuncs -> m ()
- sourceSetCanRecurse :: (HasCallStack, MonadIO m) => Source -> Bool -> m ()
- sourceSetFuncs :: (HasCallStack, MonadIO m) => Source -> SourceFuncs -> m ()
- sourceSetName :: (HasCallStack, MonadIO m) => Source -> Text -> m ()
- sourceSetNameById :: (HasCallStack, MonadIO m) => Word32 -> Text -> m ()
- sourceSetPriority :: (HasCallStack, MonadIO m) => Source -> Int32 -> m ()
- sourceSetReadyTime :: (HasCallStack, MonadIO m) => Source -> Int64 -> m ()
- sourceUnref :: (HasCallStack, MonadIO m) => Source -> m ()

# Exported types

newtype Source Source #

Memory-managed wrapper type.

Constructors

Source (ManagedPtr Source) |

Instances

BoxedObject Source Source # |
Instance details Defined in GI.GLib.Structs.Source Methods boxedType :: Source -> IO GType Source #
tag ~ AttrSet => Constructible Source tag Source # |
Instance details Defined in GI.GLib.Structs.Source Methods new :: MonadIO m => (ManagedPtr Source -> Source) -> [AttrOp Source tag] -> m Source Source #

newZeroSource :: MonadIO m => m Source Source #

Construct a `Source` struct initialized to zero.

noSource :: Maybe Source Source #

A convenience alias for `Nothing` :: `Maybe` `Source`.

# Methods

## addChildSource

sourceAddChildSource Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Source | `childSource`: a second `Source` that `source` should "poll"
-> m () |

Adds `childSource` to `source` as a "polled" source; when `source` is added to a `MainContext`, `childSource` will be automatically added with the same priority, when `childSource` is triggered, it will cause `source` to dispatch (in addition to calling its own callback), and when `source` is destroyed, it will destroy `childSource` as well. (`source` will also still be dispatched if its own prepare/check functions indicate that it is ready.)

If you don't need `childSource` to do anything on its own when it triggers, you can call `g_source_set_dummy_callback()` on it to set a callback that does nothing (except return `True` if appropriate).

`source` will hold a reference on `childSource` while `childSource` is attached to it.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

Since: 2.28

## addPoll

sourceAddPoll Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> PollFD | `fd`: a `PollFD` structure holding information about a file descriptor to watch.
-> m () |

Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined with `sourceNew` to add an event source. The event source's check function will typically test the `revents` field in the `PollFD` struct and return `True` if events need to be processed.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

Using this API forces the linear scanning of event sources on each main loop iteration. Newly-written event sources should try to use `sourceAddUnixFd` instead of this API.

## addUnixFd

sourceAddUnixFd Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Int32 | `fd`: the fd to monitor
-> [IOCondition] | `events`: an event mask
-> m (Ptr ()) | Returns: an opaque tag

Monitors `fd` for the IO events in `events`.

The tag returned by this function can be used to remove or modify the monitoring of the fd using `sourceRemoveUnixFd` or `sourceModifyUnixFd`.

It is not necessary to remove the fd before destroying the source; it will be cleaned up automatically.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

As the name suggests, this function is not available on Windows.

Since: 2.36

## attach

sourceAttach Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Maybe MainContext | `context`: a `MainContext` (if `Nothing`, the default context will be used)
-> m Word32 | Returns: the ID (greater than 0) for the source within the `MainContext`.

Adds a `Source` to a `context` so that it will be executed within that context. Remove it by calling `sourceDestroy`.

## destroy

sourceDestroy Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m () |

Removes a source from its `MainContext`, if any, and mark it as destroyed. The source cannot be subsequently added to another context. It is safe to call this on sources which have already been removed from their context.

## getCanRecurse

sourceGetCanRecurse Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Bool | Returns: whether recursion is allowed.

Checks whether a source is allowed to be called recursively. see `sourceSetCanRecurse`.

## getContext

sourceGetContext Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m (Maybe MainContext) | Returns: the `MainContext` with which the source is associated, or `Nothing` if the context has not yet been added to a source.

Gets the `MainContext` with which the source is associated.

You can call this on a source that has been destroyed, provided that the `MainContext` it was attached to still exists (in which case it will return that `MainContext`). In particular, you can always call this function on the source returned from `mainCurrentSource`. But calling this function on a source whose `MainContext` has been destroyed is an error.

## getCurrentTime

sourceGetCurrentTime Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> TimeVal | `timeval`: `TimeVal` structure in which to store current time.
-> m () |

Deprecated: (Since version 2.28)use `sourceGetTime` instead

This function ignores `source` and is otherwise the same as `getCurrentTime`.

## getId

sourceGetId Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Word32 | Returns: the ID (greater than 0) for the source

Returns the numeric ID for a particular source. The ID of a source is a positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by `mainContextFindSourceById`.

You can only call this function while the source is associated to a `MainContext` instance; calling this function before `sourceAttach` or after `sourceDestroy` yields undefined behavior. The ID returned is unique within the `MainContext` instance passed to `sourceAttach`.

## getName

sourceGetName Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Text | Returns: the name of the source

Gets a name for the source, used in debugging and profiling. The name may be `NULL` if it has never been set with `sourceSetName`.

Since: 2.26

## getPriority

sourceGetPriority Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Int32 | Returns: the priority of the source

Gets the priority of a source.

## getReadyTime

sourceGetReadyTime Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Int64 | Returns: the monotonic ready time, -1 for "never"

Gets the "ready time" of `source`, as set by `sourceSetReadyTime`.

Any time before the current monotonic time (including 0) is an indication that the source will fire immediately.

## getTime

sourceGetTime Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Int64 | Returns: the monotonic time in microseconds

Gets the time to be used when checking this source. The advantage of calling this function over calling `getMonotonicTime` directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system monotonic time.

The time here is the system monotonic time, if available, or some other reasonable alternative otherwise. See `getMonotonicTime`.

Since: 2.28

## isDestroyed

sourceIsDestroyed Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Bool | Returns: `True` if the source has been destroyed

Returns whether `source` has been destroyed.

This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.

### C code

    static gboolean
    idle_callback (gpointer data)
    {
      SomeWidget *self = data;

      GDK_THREADS_ENTER ();
      // do stuff with self
      self->idle_id = 0;
      GDK_THREADS_LEAVE ();

      return G_SOURCE_REMOVE;
    }

    static void
    some_widget_do_stuff_later (SomeWidget *self)
    {
      self->idle_id = g_idle_add (idle_callback, self);
    }

    static void
    some_widget_finalize (GObject *object)
    {
      SomeWidget *self = SOME_WIDGET (object);

      if (self->idle_id)
        g_source_remove (self->idle_id);

      G_OBJECT_CLASS (parent_class)->finalize (object);
    }

This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.

### C code

    static gboolean
    idle_callback (gpointer data)
    {
      SomeWidget *self = data;

      GDK_THREADS_ENTER ();
      if (!g_source_is_destroyed (g_main_current_source ()))
        {
          // do stuff with self
        }
      GDK_THREADS_LEAVE ();

      return FALSE;
    }

Calls to this function from a thread other than the one acquired by the `MainContext` the `Source` is attached to are typically redundant, as the source could be destroyed immediately after this function returns. However, once a source is destroyed it cannot be un-destroyed, so this function can be used for opportunistic checks from any thread.

Since: 2.12

## modifyUnixFd

sourceModifyUnixFd Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Ptr () | `tag`: the tag from `sourceAddUnixFd`
-> [IOCondition] | `newEvents`: the new event mask to watch
-> m () |

Updates the event mask to watch for the fd identified by `tag`.

`tag` is the tag returned from `sourceAddUnixFd`.

If you want to remove a fd, don't set its event mask to zero. Instead, call `sourceRemoveUnixFd`.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

As the name suggests, this function is not available on Windows.

Since: 2.36

## new

sourceNew Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> SourceFuncs | `sourceFuncs`: structure containing functions that implement the sources behavior.
-> Word32 | `structSize`: size of the `Source` structure to create.
-> m Source | Returns: the newly-created `Source`.

Creates a new `Source` structure. The size is specified to allow creating structures derived from `Source` that contain additional data. The size passed in must be at least `sizeof (GSource)`.

The source will not initially be associated with any `MainContext` and must be added to one with `sourceAttach` before it will be executed.

## queryUnixFd

sourceQueryUnixFd Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Ptr () | `tag`: the tag from `sourceAddUnixFd`
-> m [IOCondition] | Returns: the conditions reported on the fd

Queries the events reported for the fd corresponding to `tag` on `source` during the last poll.

The return value of this function is only defined when the function is called from the check or dispatch functions for `source`.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

As the name suggests, this function is not available on Windows.

Since: 2.36

## ref

sourceRef Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m Source | Returns: `source`

Increases the reference count on a source by one.

## remove

sourceRemove Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Word32 | `tag`: the ID of the source to remove.
-> m Bool | Returns: For historical reasons, this function always returns `True`

Removes the source with the given ID from the default main context. You must use `sourceDestroy` for sources added to a non-default main context.

The ID of a `Source` is given by `sourceGetId`, or will be returned by the functions `sourceAttach`, `g_idle_add()`, `idleAdd`, `g_timeout_add()`, `timeoutAdd`, `g_child_watch_add()`, `childWatchAdd`, `g_io_add_watch()`, and `ioAddWatch`.

It is a programmer error to attempt to remove a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with `g_idle_add()`: the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

## removeByFuncsUserData

sourceRemoveByFuncsUserData Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> SourceFuncs | `funcs`: The `sourceFuncs` passed to `sourceNew`
-> Ptr () | `userData`: the user data for the callback
-> m Bool | Returns: `True` if a source was found and removed.

Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.

## removeByUserData

sourceRemoveByUserData Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Ptr () | `userData`: the user_data for the callback.
-> m Bool | Returns: `True` if a source was found and removed.

Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.

## removeChildSource

sourceRemoveChildSource Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Source | `childSource`: a `Source` previously passed to `sourceAddChildSource`.
-> m () |

Detaches `childSource` from `source` and destroys it.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

Since: 2.28

## removePoll

sourceRemovePoll Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> PollFD | `fd`: a `PollFD` structure previously passed to `sourceAddPoll`.
-> m () |

Removes a file descriptor from the set of file descriptors polled for this source.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

## removeUnixFd

sourceRemoveUnixFd Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Ptr () | `tag`: the tag from `sourceAddUnixFd`
-> m () |

Reverses the effect of a previous call to `sourceAddUnixFd`.

You only need to call this if you want to remove an fd from being watched while keeping the same source around. In the normal case you will just want to destroy the source.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

As the name suggests, this function is not available on Windows.

Since: 2.36

## setCallback

sourceSetCallback Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: the source
-> SourceFunc | `func`: a callback function
-> m () |

Sets the callback function for a source. The callback for a source is called from the source's dispatch function.

The exact type of `func` depends on the type of source; ie. you should not count on `func` being called with `data` as its first parameter. Cast `func` with `G_SOURCE_FUNC()` to avoid warnings about incompatible function types.

See [memory management of sources][mainloop-memory-management] for details on how to handle memory management of `data`.

Typically, you won't use this function. Instead use functions specific to the type of source you are using, such as `g_idle_add()` or `g_timeout_add()`.

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

## setCallbackIndirect

sourceSetCallbackIndirect Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: the source
-> Ptr () | `callbackData`: pointer to callback data "object"
-> SourceCallbackFuncs | `callbackFuncs`: functions for reference counting `callbackData` and getting the callback and data
-> m () |

Sets the callback function storing the data as a refcounted callback "object". This is used internally. Note that calling `sourceSetCallbackIndirect` assumes an initial reference count on `callbackData`, and thus `callbackFuncs`->unref will eventually be called once more than `callbackFuncs`->ref.

It is safe to call this function multiple times on a source which has already been attached to a context. The changes will take effect for the next time the source is dispatched after this call returns.

## setCanRecurse

sourceSetCanRecurse Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Bool | `canRecurse`: whether recursion is allowed for this source
-> m () |

Sets whether a source can be called recursively. If `canRecurse` is `True`, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.

## setFuncs

sourceSetFuncs Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> SourceFuncs | `funcs`: the new `SourceFuncs`
-> m () |

Sets the source functions (can be used to override default implementations) of an unattached source.

Since: 2.12

## setName

sourceSetName Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Text | `name`: debug name for the source
-> m () |

Sets a name for the source, used in debugging and profiling. The name defaults to `NULL`.

The source name should describe in a human-readable way what the source does. For example, "X11 event queue" or "GTK+ repaint idle handler" or whatever it is.

It is permitted to call this function multiple times, but is not recommended due to the potential performance impact. For example, one could change the name in the "check" function of a `SourceFuncs` to include details like the event type in the source name.

Use caution if changing the name while another thread may be accessing it with `sourceGetName`; that function does not copy the value, and changing the value will free it while the other thread may be attempting to use it.

Since: 2.26

## setNameById

sourceSetNameById Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Word32 | `tag`: a `Source` ID
-> Text | `name`: debug name for the source
-> m () |

Sets the name of a source using its ID.

This is a convenience utility to set source names from the return value of `g_idle_add()`, `g_timeout_add()`, etc.

It is a programmer error to attempt to set the name of a non-existent source.

More specifically: source IDs can be reissued after a source has been destroyed and therefore it is never valid to use this function with a source ID which may have already been removed. An example is when scheduling an idle to run in another thread with `g_idle_add()`: the idle may already have run and been removed by the time this function is called on its (now invalid) source ID. This source ID may have been reissued, leading to the operation being performed against the wrong source.

Since: 2.26

## setPriority

sourceSetPriority Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Int32 | `priority`: the new priority.
-> m () |

Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.

A child source always has the same priority as its parent. It is not permitted to change the priority of a source once it has been added as a child of another source.

## setReadyTime

sourceSetReadyTime Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> Int64 | `readyTime`: the monotonic time at which the source will be ready, 0 for "immediately", -1 for "never"
-> m () |

Sets a `Source` to be dispatched when the given monotonic time is reached (or passed). If the monotonic time is in the past (as it always will be if `readyTime` is 0) then the source will be dispatched immediately.

If `readyTime` is -1 then the source is never woken up on the basis of the passage of time.

Dispatching the source does not reset the ready time. You should do so yourself, from the source dispatch function.

Note that if you have a pair of sources where the ready time of one suggests that it will be delivered first but the priority for the other suggests that it would be delivered first, and the ready time for both sources is reached during the same main context iteration, then the order of dispatch is undefined.

It is a no-op to call this function on a `Source` which has already been destroyed with `sourceDestroy`.

This API is only intended to be used by implementations of `Source`. Do not call this API on a `Source` that you did not create.

Since: 2.36

## unref

sourceUnref Source #

Arguments

:: (HasCallStack, MonadIO m) |
=> Source | `source`: a `Source`
-> m () |

Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.