summaryrefslogtreecommitdiffstats
path: root/test/bus-test-loop.c
blob: aa9e4fcc9bdbb62442dac261e3cc7f840560f0eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "bus-test-loop.h"
#include <sys/time.h>
#include <stdio.h>

#define DBUS_COMPILATION /* cheat and use DBusList */
#include <dbus/dbus-list.h>
#undef DBUS_COMPILATION

typedef struct
{
  long time;
  DBusTimeout *timeout;

} LoopTimeout;

static DBusList *timeouts;

static long
get_time (void)
{
  struct timeval r;
  long time;

  /* Can't use dbus-sysdeps here since that isn't
   * available outside of libdbus.
   */
  gettimeofday (&r, NULL);

  time = r.tv_sec * 1000;
  time += r.tv_usec / 1000;

  return time;
}

static void
add_timeout (DBusTimeout *timeout,
	     void        *data)
{
  LoopTimeout *lt;

  lt = dbus_new (LoopTimeout, 1);
  lt->time = get_time () + dbus_timeout_get_interval (timeout);
  lt->timeout = timeout;

  _dbus_list_append (&timeouts, lt);
}

static void
remove_timeout (DBusTimeout *timeout,
		void        *data)
{
  DBusList *link;
  
  link = _dbus_list_get_first_link (&timeouts);
  while (link != NULL)
    {
      LoopTimeout *lt = link->data;
      if (lt->timeout == timeout)
	{
	  _dbus_list_remove (&timeouts, lt);
	  return;
	}
      link = _dbus_list_get_next_link (&timeouts, link);
    }
}

static dbus_bool_t running_loop;


void
bus_test_loop_quit (void)
{
  running_loop = FALSE;
}

void
bus_test_loop_run (void)
{
  running_loop = TRUE;

  /* Horribly inefficient main loop */
  while (running_loop)
    {
      DBusList *link, *list_copy;
      long time;

      time = get_time ();

      _dbus_list_copy (&timeouts, &list_copy);

      link = _dbus_list_get_first_link (&list_copy);
      while (link != NULL)
	{
	  LoopTimeout *lt = link->data;
	  if (lt->time <= time)
	    {
	      dbus_timeout_handle (lt->timeout);
	      _dbus_list_remove (&timeouts, lt);
	    }
	  link = _dbus_list_get_next_link (&list_copy, link);
	}
    }
}


void
bus_test_loop_hookup_with_server (DBusServer *server)
{
  dbus_server_set_timeout_functions (server,
				     add_timeout, remove_timeout,
				     NULL, NULL);
}

void
bus_test_loop_hookup_with_connection (DBusConnection *connection)
{
  dbus_connection_set_timeout_functions (connection,
					 add_timeout, remove_timeout,
					 NULL, NULL);
}