aboutsummaryrefslogtreecommitdiff
path: root/Src/nu/ServiceWatcher.cpp
blob: dcf8e096c0fd659d6dc713959a968a033a1c0874 (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "ServiceWatcher.h"
#include <api/service/waservicefactory.h>


static void *GetService(api_service *p_serviceManager, GUID p_serviceGUID)
{
	waServiceFactory *sf = p_serviceManager->service_getServiceByGuid( p_serviceGUID);
	if (sf)
		return sf->getInterface();
	else
		return 0;
}

static void ReleaseService(api_service *p_serviceManager, GUID p_serviceGUID, void *p_service)
{
	waServiceFactory *sf = p_serviceManager->service_getServiceByGuid( p_serviceGUID);
	if (sf)
		sf->releaseInterface( p_service);
}

void ServiceWatcher::WatchWith(api_service *_serviceApi) 
{ 
	serviceManager =_serviceApi;
	systemCallbacks=(api_syscb*)GetService( serviceManager, syscbApiServiceGuid);
}

void ServiceWatcher::StopWatching()
{
	if (systemCallbacks)
	{
		systemCallbacks->syscb_deregisterCallback(this);
		ReleaseService( serviceManager, syscbApiServiceGuid, systemCallbacks);
	}
	systemCallbacks=0;
}
void ServiceWatcher::Clear() 
{
	//watchList.Reset();
	watchList.clear();
}

ServiceWatcher::~ServiceWatcher()
{
	//StopWatching();
}

void ServiceWatcher::WatchForT(void **ptr, GUID watchGUID)
{
	watchList[watchGUID]=ptr;
	if (!*ptr) // try to get it if we need it
	{
		*ptr = GetService( serviceManager, watchGUID);
	}
}

int ServiceWatcher::Notify(int msg, intptr_t param1, intptr_t param2)
{
	switch (msg)
	{
		case SvcCallback::ONREGISTER:
		{
			waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
			GUID serviceGUID = sf->getGuid();
			if (serviceGUID != INVALID_GUID)
			{
				WatchList::iterator itr = watchList.find(serviceGUID);
				if (itr!=watchList.end())
				{
					void **ptr = itr->second;
					if (ptr && !*ptr) // don't re-retrieve service if we already have it
					{
						*ptr = sf->getInterface();
					}
				}
			}
		}
		break;
		case SvcCallback::ONDEREGISTER:
		{
			waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
			GUID serviceGUID = sf->getGuid();
			if (serviceGUID != INVALID_GUID)
			{
				WatchList::iterator itr = watchList.find(serviceGUID);
				if (itr!=watchList.end())
				{
					void **ptr = itr->second;
					if (ptr && *ptr)
					{
						// benski> probably not safe to do, so i'll leave it commented out: sf->releaseInterface(*ptr);
						*ptr = 0;
					}
				}
			}
		}
		break;
		default: return 0;
	}
	return 1;
}

#define CBCLASS ServiceWatcher
START_DISPATCH;
CB(SYSCALLBACK_GETEVENTTYPE, GetEventType);
CB(SYSCALLBACK_NOTIFY, Notify);
END_DISPATCH;
#undef CBCLASS

ServiceWatcherSingle::~ServiceWatcherSingle()
{
	//StopWatching();
}

void ServiceWatcherSingle::StopWatching()
{
	if (systemCallbacks)
	{
		systemCallbacks->syscb_deregisterCallback(this);
		ReleaseService( serviceManager, syscbApiServiceGuid, systemCallbacks);
	}
	systemCallbacks=0;
}

void ServiceWatcherSingle::WatchWith(api_service *_serviceApi) 
{
	serviceManager =_serviceApi;
	systemCallbacks=(api_syscb*)GetService( serviceManager, syscbApiServiceGuid);
}

void ServiceWatcherSingle::WatchForT(void **ptr, GUID watchGUID)
{
	service=ptr;
	serviceGUID=watchGUID;
	if (ptr && !*ptr) // try to get it if we need it
	{
		*ptr = GetService( serviceManager, watchGUID);
		if (*ptr)
			OnRegister();
	}
}

int ServiceWatcherSingle::Notify(int msg, intptr_t param1, intptr_t param2)
{
	switch (msg)
	{
		case SvcCallback::ONREGISTER:
		{
			if (service && !*service) // don't re-retrieve service if we already have it
			{
				waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
				if (sf && sf->getGuid() == serviceGUID)
				{
					*service = sf->getInterface();
					if (*service)
					OnRegister();
				}
			}
		}
		break;
		case SvcCallback::ONDEREGISTER:
		{
			if (service && *service)
			{
				waServiceFactory *sf = reinterpret_cast<waServiceFactory*>(param2);
				if (serviceGUID == sf->getGuid())
				{
					OnDeregister();
					*service=0;
				}
			}
		}
		break;
		default: return 0;
	}
	return 1;
}

#define CBCLASS ServiceWatcherSingle
START_DISPATCH;
CB(SYSCALLBACK_GETEVENTTYPE, GetEventType);
CB(SYSCALLBACK_NOTIFY, Notify);
END_DISPATCH;
#undef CBCLASS