Release 1.25.0 -- Buddy Idle Time, RTF
[siplcs.git] / src / miranda / miranda-schedule.c
bloba78f7bf6aa7fdb4e4b923d222570b334235826db
1 /**
2 * @file miranda-schedule.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <windows.h>
24 #include <stdio.h>
26 #include <glib.h>
28 #include "sipe-common.h"
29 #include "sipe-backend.h"
30 #include "sipe-core.h"
32 #include "newpluginapi.h"
33 #include "m_system.h"
34 #include "m_protosvc.h"
35 #include "m_protoint.h"
36 #include "miranda-private.h"
38 struct time_entry {
39 gpointer core_data;
40 guint timeout;
41 HANDLE sem;
42 gboolean cancelled;
43 SIPPROTO *pr;
44 void (*callback)(gpointer);
46 /* Private. For locking only */
47 HANDLE hDoneEvent;
50 static void __stdcall
51 timeout_cb_async(void *data)
53 struct time_entry *entry = (struct time_entry*)data;
54 SIPPROTO *pr = entry->pr;
56 if (entry->cancelled == TRUE)
58 SIPE_DEBUG_INFO("Entry <%08x> already cancelled. Not calling timeout function", entry);
59 } else {
60 SIPE_DEBUG_INFO("Calling timeout function for entry <%08x>", entry);
61 LOCK;
62 entry->callback(entry->core_data);
63 UNLOCK;
65 SetEvent(entry->hDoneEvent);
68 static unsigned __stdcall timeoutfunc(void* data)
70 struct time_entry *entry = (struct time_entry*)data;
71 DWORD ret;
72 SIPPROTO *pr = entry->pr;
74 SIPE_DEBUG_INFO("timeout start; <%08x> timeout is <%d>", entry, entry->timeout);
76 entry->sem = CreateSemaphore(NULL, 0, 100, NULL);
78 ret = WaitForSingleObjectEx( entry->sem, entry->timeout, FALSE);
79 if (entry->cancelled == TRUE)
81 SIPE_DEBUG_INFO("<%08x> Timeout cancelled by caller", entry);
83 else if (ret == WAIT_TIMEOUT)
85 SIPE_DEBUG_INFO("<%08x> about to run", entry);
86 if (entry->cancelled == TRUE)
88 SIPE_DEBUG_INFO("<%08x> Timeout cancelled by caller in the nick of time", entry);
90 else
92 entry->hDoneEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
93 CallFunctionAsync(timeout_cb_async, entry);
94 WaitForSingleObject(entry->hDoneEvent, INFINITE);
95 CloseHandle(entry->hDoneEvent);
97 SIPE_DEBUG_INFO("<%08x> exiting", entry);
99 else
101 SIPE_DEBUG_INFO("<%08x> Something unexpected happened: <%d>", entry, ret);
104 CloseHandle(entry->sem);
105 g_free(entry);
106 return 0;
110 gpointer sipe_miranda_schedule_mseconds(void (*callback)(gpointer),
111 guint timeout,
112 gpointer data)
114 struct time_entry *entry;
116 entry = g_new0(struct time_entry,1);
117 entry->timeout = timeout;
118 entry->core_data = data;
119 entry->cancelled = FALSE;
120 entry->pr = data; /* FIXME: Assumes data = SIPPROTO * */
121 entry->callback = callback;
123 SIPE_DEBUG_INFO("Scheduling timeout in <%u>ms for entry <%08x>", timeout, entry);
124 CloseHandle((HANDLE) mir_forkthreadex( timeoutfunc, entry, 65536, NULL ));
126 return entry;
129 gpointer sipe_backend_schedule_mseconds(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
130 guint timeout,
131 gpointer data)
133 struct time_entry *entry;
135 entry = g_new0(struct time_entry,1);
136 entry->timeout = timeout;
137 entry->core_data = data;
138 entry->cancelled = FALSE;
139 entry->pr = sipe_public->backend_private;
140 entry->callback = sipe_core_schedule_execute;
142 CloseHandle((HANDLE) mir_forkthreadex( timeoutfunc, entry, 65536, NULL ));
144 return entry;
147 gpointer sipe_backend_schedule_seconds(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
148 guint timeout,
149 gpointer data)
151 return sipe_backend_schedule_mseconds( sipe_public, timeout*1000, data);
154 void sipe_backend_schedule_cancel(SIPE_UNUSED_PARAMETER struct sipe_core_public *sipe_public,
155 gpointer data)
157 struct time_entry *entry = (struct time_entry*) data;
159 if (entry && entry->sem)
161 SIPE_DEBUG_INFO("Cancelling timeout <%08x>", entry);
162 entry->cancelled = TRUE;
163 ReleaseSemaphore(entry->sem, 1, NULL);
169 Local Variables:
170 mode: c
171 c-file-style: "bsd"
172 indent-tabs-mode: t
173 tab-width: 8
174 End: