aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_pmp/DeviceCommands.cpp
blob: 5c34a84a1374d2ba148a27873674a719a289946f (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
184
185
186
187
188
189
190
191
192
#include "main.h"
#include "DeviceCommands.h"

#include <strsafe.h>
PortableCommand::PortableCommand(const char *name,  int title, int description) 
	: name(name), title(title), description(description)
{
}

const char *PortableCommand::GetName()
{ 
	return name; 
}

HRESULT PortableCommand::GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
{
	return E_NOTIMPL;
}

HRESULT PortableCommand::GetDisplayName(wchar_t *buffer, size_t bufferSize)
{
	if (NULL == buffer)
		return E_POINTER;

	WASABI_API_LNGSTRINGW_BUF(title, buffer, bufferSize);
	return S_OK;
}

HRESULT PortableCommand::GetDescription(wchar_t *buffer, size_t bufferSize)
{
	if (NULL == buffer)
		return E_POINTER;

	WASABI_API_LNGSTRINGW_BUF(description, buffer, bufferSize);
	return S_OK;
}

#define CBCLASS PortableCommand
START_DISPATCH;
CB(API_GETNAME, GetName);
CB(API_GETICON, GetIcon);
CB(API_GETDISPLAYNAME, GetDisplayName);
CB(API_GETDESCRIPTION, GetDescription);
END_DISPATCH;
#undef CBCLASS


BOOL SetDeviceCommandInfo(DeviceCommandInfo *info, const char *name, DeviceCommandFlags flags)
{
	if (NULL == info)
		return FALSE;

	info->name = name;
	info->flags = flags;
	return TRUE;
}

/* -------------- */
DeviceCommand::DeviceCommand(const char *name, DeviceCommandFlags flags)
: name(name), flags(flags)
{
}

DeviceCommand::DeviceCommand(const DeviceCommandInfo *commandInfo)
: name(commandInfo->name), flags(commandInfo->flags)
{
}

const char *DeviceCommand::GetName() 
{
	return name; 
}

HRESULT DeviceCommand::GetFlags(DeviceCommandFlags *flags) 
{
	*flags = this->flags; 
	return 0; 
}


#define CBCLASS DeviceCommand
START_DISPATCH;
REFERENCE_COUNTED;
CB(API_GETNAME, GetName);
CB(API_GETFLAGS, GetFlags);
END_DISPATCH;
#undef CBCLASS



DeviceCommandEnumerator::DeviceCommandEnumerator(const DeviceCommandInfo *commandInfoList, size_t listSize) 
	: position(0), commands(NULL), count(0)
{
	if (NULL != commandInfoList && 
		0 != listSize)
	{
		commands = (DeviceCommand**)calloc(listSize, sizeof(DeviceCommand*));
		if (NULL != commands)
		{
			for(count = 0; count < listSize; count++)
			{
				commands[count] = new DeviceCommand(&commandInfoList[count]);
			}
		}
	}
}

DeviceCommandEnumerator::~DeviceCommandEnumerator()
{
	if (NULL != commands)
	{
		while(count--)
			commands[count]->Release();
		
		free(commands);
	}
		
}

HRESULT DeviceCommandEnumerator::Next(ifc_devicesupportedcommand **buffer, size_t bufferMax, size_t *fetched)
{
	size_t available, copied, index;
	DeviceCommand **source;

	if (NULL == buffer)
		return E_POINTER;
	
	if (0 == bufferMax) 
		return E_INVALIDARG;

	if (position >= count)
	{
		if (NULL != fetched) 
			*fetched = 0;

		return S_FALSE;
	}

	available = count - position;
	copied = ((available > bufferMax) ? bufferMax : available);
	
	source = commands + position;
	CopyMemory(buffer, source, copied * sizeof(ifc_devicesupportedcommand*));
    
	for(index = 0; index < copied; index++)
		buffer[index]->AddRef();
	
	position += copied;

	if (NULL != fetched) 
		*fetched = copied;

	return (bufferMax == copied) ? S_OK : S_FALSE;
}

HRESULT DeviceCommandEnumerator::Reset(void)
{
	position=0;
	return S_OK;
}

HRESULT DeviceCommandEnumerator::Skip(size_t count)
{
	position += count;
	if (position > this->count)
		position = this->count;
	return (position < this->count) ? S_OK : S_FALSE;
}

HRESULT DeviceCommandEnumerator::GetCount(size_t *count)
{ 
	if (NULL == count)
		return E_POINTER;
	
	*count = this->count;

	return S_OK;
}


#define CBCLASS DeviceCommandEnumerator
START_DISPATCH;
CB(API_NEXT, Next);
CB(API_RESET, Reset);
CB(API_SKIP, Skip);
CB(API_GETCOUNT, GetCount);
REFERENCE_COUNTED;
END_DISPATCH;
#undef CBCLASS