dbus-timeout.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-timeout.c DBusTimeout implementation
*
* Copyright (C) 2003 CodeFactory AB
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-internals.h"
#include "dbus-timeout.h"
#include "dbus-list.h"
struct DBusTimeout
{
int refcount;
int interval;
DBusTimeoutHandler handler;
void *handler_data;
DBusFreeFunction free_handler_data_function;
void *data;
DBusFreeFunction free_data_function;
unsigned int enabled : 1;
unsigned int needs_restart : 1;
};
DBusTimeout*
_dbus_timeout_new (int interval,
DBusTimeoutHandler handler,
void *data,
DBusFreeFunction free_data_function)
{
DBusTimeout *timeout;
timeout = dbus_new0 (DBusTimeout, 1);
if (timeout == NULL)
return NULL;
timeout->refcount = 1;
timeout->interval = interval;
timeout->handler = handler;
timeout->handler_data = data;
timeout->free_handler_data_function = free_data_function;
timeout->enabled = TRUE;
timeout->needs_restart = FALSE;
return timeout;
}
DBusTimeout *
_dbus_timeout_ref (DBusTimeout *timeout)
{
timeout->refcount += 1;
return timeout;
}
void
_dbus_timeout_unref (DBusTimeout *timeout)
{
_dbus_assert (timeout != NULL);
_dbus_assert (timeout->refcount > 0);
timeout->refcount -= 1;
if (timeout->refcount == 0)
{
dbus_timeout_set_data (timeout, NULL, NULL); /* call free_data_function */
if (timeout->free_handler_data_function)
(* timeout->free_handler_data_function) (timeout->handler_data);
dbus_free (timeout);
}
}
void
_dbus_timeout_restart (DBusTimeout *timeout,
int interval)
{
_dbus_assert (interval >= 0);
timeout->interval = interval;
timeout->enabled = TRUE;
timeout->needs_restart = TRUE;
}
void
_dbus_timeout_disable (DBusTimeout *timeout)
{
timeout->enabled = FALSE;
}
struct DBusTimeoutList
{
DBusList *timeouts;
DBusAddTimeoutFunction add_timeout_function;
DBusRemoveTimeoutFunction remove_timeout_function;
DBusTimeoutToggledFunction timeout_toggled_function;
void *timeout_data;
DBusFreeFunction timeout_free_data_function;
};
DBusTimeoutList*
_dbus_timeout_list_new (void)
{
DBusTimeoutList *timeout_list;
timeout_list = dbus_new0 (DBusTimeoutList, 1);
if (timeout_list == NULL)
return NULL;
return timeout_list;
}
void
_dbus_timeout_list_free (DBusTimeoutList *timeout_list)
{
/* free timeout_data and remove timeouts as a side effect */
_dbus_timeout_list_set_functions (timeout_list,
NULL, NULL, NULL, NULL, NULL);
_dbus_list_clear_full (&timeout_list->timeouts,
(DBusFreeFunction) _dbus_timeout_unref);
dbus_free (timeout_list);
}
dbus_bool_t
_dbus_timeout_list_set_functions (DBusTimeoutList *timeout_list,
DBusAddTimeoutFunction add_function,
DBusRemoveTimeoutFunction remove_function,
DBusTimeoutToggledFunction toggled_function,
void *data,
DBusFreeFunction free_data_function)
{
/* Add timeouts with the new function, failing on OOM */
if (add_function != NULL)
{
DBusList *link;
link = _dbus_list_get_first_link (&timeout_list->timeouts);
while (link != NULL)
{
DBusList *next = _dbus_list_get_next_link (&timeout_list->timeouts,
link);
if (!(* add_function) (link->data, data))
{
/* remove it all again and return FALSE */
DBusList *link2;
link2 = _dbus_list_get_first_link (&timeout_list->timeouts);
while (link2 != link)
{
DBusList *next2 = _dbus_list_get_next_link (&timeout_list->timeouts,
link2);
(* remove_function) (link2->data, data);
link2 = next2;
}
return FALSE;
}
link = next;
}
}
/* Remove all current timeouts from previous timeout handlers */
if (timeout_list->remove_timeout_function != NULL)
{
_dbus_list_foreach (&timeout_list->timeouts,
(DBusForeachFunction) timeout_list->remove_timeout_function,
timeout_list->timeout_data);
}
if (timeout_list->timeout_free_data_function != NULL)
(* timeout_list->timeout_free_data_function) (timeout_list->timeout_data);
timeout_list->add_timeout_function = add_function;
timeout_list->remove_timeout_function = remove_function;
timeout_list->timeout_toggled_function = toggled_function;
timeout_list->timeout_data = data;
timeout_list->timeout_free_data_function = free_data_function;
return TRUE;
}
dbus_bool_t
_dbus_timeout_list_add_timeout (DBusTimeoutList *timeout_list,
DBusTimeout *timeout)
{
if (!_dbus_list_append (&timeout_list->timeouts, timeout))
return FALSE;
_dbus_timeout_ref (timeout);
if (timeout_list->add_timeout_function != NULL)
{
if (!(* timeout_list->add_timeout_function) (timeout,
timeout_list->timeout_data))
{
_dbus_list_remove_last (&timeout_list->timeouts, timeout);
_dbus_timeout_unref (timeout);
return FALSE;
}
}
return TRUE;
}
void
_dbus_timeout_list_remove_timeout (DBusTimeoutList *timeout_list,
DBusTimeout *timeout)
{
if (!_dbus_list_remove (&timeout_list->timeouts, timeout))
_dbus_assert_not_reached ("Nonexistent timeout was removed");
if (timeout_list->remove_timeout_function != NULL)
(* timeout_list->remove_timeout_function) (timeout,
timeout_list->timeout_data);
_dbus_timeout_unref (timeout);
}
void
_dbus_timeout_list_toggle_timeout (DBusTimeoutList *timeout_list,
DBusTimeout *timeout,
dbus_bool_t enabled)
{
enabled = !!enabled;
if (enabled == timeout->enabled)
return;
timeout->enabled = enabled;
if (timeout_list->timeout_toggled_function != NULL)
(* timeout_list->timeout_toggled_function) (timeout,
timeout_list->timeout_data);
}
dbus_bool_t
_dbus_timeout_needs_restart (DBusTimeout *timeout)
{
return timeout->needs_restart;
}
void
_dbus_timeout_restarted (DBusTimeout *timeout)
{
timeout->needs_restart = FALSE;
}
int
dbus_timeout_get_interval (DBusTimeout *timeout)
{
return timeout->interval;
}
void*
dbus_timeout_get_data (DBusTimeout *timeout)
{
return timeout->data;
}
void
dbus_timeout_set_data (DBusTimeout *timeout,
void *data,
DBusFreeFunction free_data_function)
{
if (timeout->free_data_function != NULL)
(* timeout->free_data_function) (timeout->data);
timeout->data = data;
timeout->free_data_function = free_data_function;
}
dbus_bool_t
dbus_timeout_handle (DBusTimeout *timeout)
{
return (* timeout->handler) (timeout->handler_data);
}
dbus_bool_t
dbus_timeout_get_enabled (DBusTimeout *timeout)
{
return timeout->enabled;
}
DBusTimeoutToggledFunction
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
Definition dbus-connection.h:120
DBusAddTimeoutFunction
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
Definition dbus-connection.h:113
DBusRemoveTimeoutFunction
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
Definition dbus-connection.h:126
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition dbus-internals.h:164
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
DBusForeachFunction
void(* DBusForeachFunction)(void *element, void *data)
Used to iterate over each item in a collection, such as a DBusList.
Definition dbus-internals.h:347
_dbus_list_get_first_link
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition dbus-list.c:597
_dbus_list_remove
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition dbus-list.c:418
_dbus_list_clear_full
void _dbus_list_clear_full(DBusList **list, DBusFreeFunction function)
Free every link and every element in the list.
Definition dbus-list.c:570
_dbus_list_foreach
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition dbus-list.c:789
_dbus_list_remove_last
dbus_bool_t _dbus_list_remove_last(DBusList **list, void *data)
Removes a value from the list.
Definition dbus-list.c:449
_dbus_list_append
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition dbus-list.c:273
_dbus_list_get_next_link
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition dbus-list.h:121
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
DBusFreeFunction
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition dbus-memory.h:65
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
_dbus_timeout_list_add_timeout
dbus_bool_t _dbus_timeout_list_add_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Adds a new timeout to the timeout list, invoking the application DBusAddTimeoutFunction if appropriat...
Definition dbus-timeout.c:314
_dbus_timeout_list_free
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Definition dbus-timeout.c:217
_dbus_timeout_list_toggle_timeout
void _dbus_timeout_list_toggle_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout, dbus_bool_t enabled)
Sets a timeout to the given enabled state, invoking the application's DBusTimeoutToggledFunction if a...
Definition dbus-timeout.c:366
_dbus_timeout_list_new
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
Definition dbus-timeout.c:200
_dbus_timeout_unref
void _dbus_timeout_unref(DBusTimeout *timeout)
Decrements the reference count of a DBusTimeout object and finalizes the object if the count reaches ...
Definition dbus-timeout.c:111
_dbus_timeout_list_set_functions
dbus_bool_t _dbus_timeout_list_set_functions(DBusTimeoutList *timeout_list, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions.
Definition dbus-timeout.c:243
_dbus_timeout_disable
void _dbus_timeout_disable(DBusTimeout *timeout)
Disable the timeout.
Definition dbus-timeout.c:161
_dbus_timeout_new
DBusTimeout * _dbus_timeout_new(int interval, DBusTimeoutHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusTimeout, enabled by default.
Definition dbus-timeout.c:66
_dbus_timeout_restart
void _dbus_timeout_restart(DBusTimeout *timeout, int interval)
Change the timeout interval to be interval milliseconds from now (forgetting when the timeout was ini...
Definition dbus-timeout.c:140
DBusTimeoutHandler
dbus_bool_t(* DBusTimeoutHandler)(void *data)
function to run when the timeout is handled
Definition dbus-timeout.h:43
_dbus_timeout_ref
DBusTimeout * _dbus_timeout_ref(DBusTimeout *timeout)
Increments the reference count of a DBusTimeout object.
Definition dbus-timeout.c:97
_dbus_timeout_restarted
void _dbus_timeout_restarted(DBusTimeout *timeout)
Mark timeout as restarted (setting timestamps is responsibility of the event loop).
Definition dbus-timeout.c:401
_dbus_timeout_list_remove_timeout
void _dbus_timeout_list_remove_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Removes a timeout from the timeout list, invoking the application's DBusRemoveTimeoutFunction if appr...
Definition dbus-timeout.c:344
_dbus_timeout_needs_restart
dbus_bool_t _dbus_timeout_needs_restart(DBusTimeout *timeout)
Returns whether a timeout needs restart time counting in the event loop.
Definition dbus-timeout.c:389
dbus_timeout_handle
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
Definition dbus-timeout.c:500
dbus_timeout_get_enabled
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
Definition dbus-timeout.c:514
dbus_timeout_get_interval
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
Definition dbus-timeout.c:444
dbus_timeout_get_data
DBUS_EXPORT void * dbus_timeout_get_data(DBusTimeout *timeout)
Gets data previously set with dbus_timeout_set_data() or NULL if none.
Definition dbus-timeout.c:457
dbus_timeout_set_data
DBUS_EXPORT void dbus_timeout_set_data(DBusTimeout *timeout, void *data, DBusFreeFunction free_data_function)
Sets data which can be retrieved with dbus_timeout_get_data().
Definition dbus-timeout.c:474
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusList
A node in a linked list.
Definition dbus-list.h:37
DBusList::data
void * data
Data stored at this element.
Definition dbus-list.h:40
DBusTimeoutList
DBusTimeoutList implementation details.
Definition dbus-timeout.c:183
DBusTimeoutList::timeout_toggled_function
DBusTimeoutToggledFunction timeout_toggled_function
Callback when timeout is enabled/disabled or changes interval.
Definition dbus-timeout.c:188
DBusTimeoutList::remove_timeout_function
DBusRemoveTimeoutFunction remove_timeout_function
Callback for removing a timeout.
Definition dbus-timeout.c:187
DBusTimeoutList::timeouts
DBusList * timeouts
Timeout objects.
Definition dbus-timeout.c:184
DBusTimeoutList::add_timeout_function
DBusAddTimeoutFunction add_timeout_function
Callback for adding a timeout.
Definition dbus-timeout.c:186
DBusTimeoutList::timeout_free_data_function
DBusFreeFunction timeout_free_data_function
Free function for timeout callback data.
Definition dbus-timeout.c:190
DBusTimeoutList::timeout_data
void * timeout_data
Data for timeout callbacks.
Definition dbus-timeout.c:189
DBusTimeout
Internals of DBusTimeout.
Definition dbus-timeout.c:43
DBusTimeout::data
void * data
Application data.
Definition dbus-timeout.c:51
DBusTimeout::refcount
int refcount
Reference count.
Definition dbus-timeout.c:44
DBusTimeout::enabled
unsigned int enabled
True if timeout is active.
Definition dbus-timeout.c:53
DBusTimeout::handler
DBusTimeoutHandler handler
Timeout handler.
Definition dbus-timeout.c:47
DBusTimeout::handler_data
void * handler_data
Timeout handler data.
Definition dbus-timeout.c:48
DBusTimeout::interval
int interval
Timeout interval in milliseconds.
Definition dbus-timeout.c:45
DBusTimeout::needs_restart
unsigned int needs_restart
Flag that timeout should be restarted after re-enabling.
Definition dbus-timeout.c:54
DBusTimeout::free_handler_data_function
DBusFreeFunction free_handler_data_function
Free the timeout handler data.
Definition dbus-timeout.c:49
DBusTimeout::free_data_function
DBusFreeFunction free_data_function
Free the application data.
Definition dbus-timeout.c:52