[4de1c7]: / app / resources / LeapSDK / v3_python27 / src / Config.cs

Download this file

285 lines (261 with data), 8.0 kB

  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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/******************************************************************************\
* Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved. *
* Leap Motion proprietary and confidential. Not for distribution. *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
\******************************************************************************/
namespace Leap
{
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using LeapInternal;
/**
* The Config class provides access to Leap Motion system configuration information.
*
* @since 1.0
*/
public class Config
{
private Connection _connection;
private Dictionary<UInt32, object> _transactions = new Dictionary<UInt32, object>();
/**
* Creates a new Config object for setting runtime configuration settings.
*
* Note that the Controller.Config provides a properly initialized Config object already.
*
* @param connectionKey The id of the connnection. This id must match the key used to create
* the Controller.
* @since 3.0
*/
public Config(int connectionKey)
{
_connection = Connection.GetConnection(connectionKey);
_connection.LeapConfigChange += handleConfigChange;
_connection.LeapConfigResponse += handleConfigResponse;
}
private void handleConfigChange(object sender, ConfigChangeEventArgs eventArgs)
{
object actionDelegate;
if (_transactions.TryGetValue(eventArgs.RequestId, out actionDelegate))
{
Action<bool> changeAction = actionDelegate as Action<bool>;
changeAction(eventArgs.Succeeded);
_transactions.Remove(eventArgs.RequestId);
}
}
private void handleConfigResponse(object sender, SetConfigResponseEventArgs eventArgs)
{
object actionDelegate = new object();
if (_transactions.TryGetValue(eventArgs.RequestId, out actionDelegate))
{
switch (eventArgs.DataType)
{
case Config.ValueType.TYPE_BOOLEAN:
Action<bool> boolAction = actionDelegate as Action<bool>;
boolAction((int)eventArgs.Value != 0);
break;
case Config.ValueType.TYPE_FLOAT:
Action<float> floatAction = actionDelegate as Action<float>;
floatAction((float)eventArgs.Value);
break;
case Config.ValueType.TYPE_INT32:
Action<Int32> intAction = actionDelegate as Action<Int32>;
intAction((Int32)eventArgs.Value);
break;
case Config.ValueType.TYPE_STRING:
Action<string> stringAction = actionDelegate as Action<string>;
stringAction((string)eventArgs.Value);
break;
default:
break;
}
_transactions.Remove(eventArgs.RequestId);
}
}
/**
* Requests a configuration value.
*
* @param key The string identifier for the setting.
* @param onResult The action to take when the Leap service returns the config value.
* The Action delegate must take a parameter matching the config value type. The current
* value of the setting is passed to this delegate.
*
* @since 3.0
*/
public bool Get<T>(string key, Action<T> onResult)
{
uint requestId = _connection.GetConfigValue(key);
if (requestId > 0)
{
_transactions.Add(requestId, onResult);
return true;
}
return false;
}
/**
* Sets a configuration value.
*
* @param key The string identifier for the setting.
* @param onResult The action to take when the Leap service sets the config value.
* The Action delegate must take a boolean parameter. The service calls this delegate
* with the value true if the setting was changed successfully and false, otherwise.
*
* @since 3.0
*/
public bool Set<T>(string key, T value, Action<bool> onResult) where T : IConvertible
{
uint requestId = _connection.SetConfigValue<T>(key, value);
if (requestId > 0)
{
_transactions.Add(requestId, onResult);
return true;
}
return false;
}
/**
* The data type for a configuration parameter.
* @ deprecated Always returns Config.ValueType.TYPE_UNKNOWN
*/
public Config.ValueType Type(string key)
{
return Config.ValueType.TYPE_UNKNOWN;
}
/**
* Gets the boolean representation for the specified key.
*
* @ deprecated use Get<bool>(key, delegate)
*
* @since 1.0
*/
public bool GetBool(string key)
{
return Get<bool>(key, delegate (bool value)
{
});
}
/** Sets the boolean representation for the specified key.
*
* \include Config_setBool.txt
*
* @returns true on success, false on failure.
* @since 1.0
*/
public bool SetBool(string key, bool value)
{
return Set<bool>(key, value, delegate (bool success) { });
}
/**
* Gets the 32-bit integer representation for the specified key.
* @ deprecated use Get<Int32>(key, delegate)
* @since 1.0
*/
public bool GetInt32(string key)
{
return Get<Int32>(key, delegate (Int32 value)
{
});
}
/** Sets the 32-bit integer representation for the specified key.
*
* \include Config_setInt32.txt
*
* @returns true on success, false on failure.
* @since 1.0
*/
public bool SetInt32(string key, int value)
{
return Set<Int32>(key, value, delegate (bool success) { });
}
/**
* Gets the floating point representation for the specified key.
* @ deprecated use Get<float>(key, delegate)
* @since 1.0
*/
public bool GetFloat(string key)
{
return Get<float>(key, delegate (float value)
{
});
}
/** Sets the floating point representation for the specified key.
*
* \include Config_setFloat.txt
*
* @returns true on success, false on failure.
* @since 1.0
*/
public bool SetFloat(string key, float value)
{
return Set<float>(key, value, delegate (bool success) { });
}
/**
* Gets the string representation for the specified key.
* @ deprecated use Get<string>(key, delegate)
* @since 1.0
*/
public bool GetString(string key)
{
return Get<string>(key, delegate (string value)
{
});
}
/** Sets the string representation for the specified key.
*
* \include Config_setString.txt
*
* @returns true on success, false on failure.
* @since 1.0
*/
public bool SetString(string key, string value)
{
return Set<string>(key, value, delegate (bool success) { });
}
/**
* Saves the current state of the config.
*
* @deprecated no longer required, configuration changes are saved immediately.
* @returns always returns false.
* @since 1.0
*/
public bool Save()
{
return false;
}
/**
* Enumerates the possible data types for configuration values.
*
* The Config::type() function returns an item from the ValueType enumeration.
* @since 1.0
*/
public enum ValueType
{
/**
* The data type is unknown.
* @since 1.0
*/
TYPE_UNKNOWN = 0,
/**
* A boolean value.
* @since 1.0
*/
TYPE_BOOLEAN = 1,
/**
* A 32-bit integer.
* @since 1.0
*/
TYPE_INT32 = 2,
/**
* A floating-point number.
* @since 1.0
*/
TYPE_FLOAT = 6,
/**
* A string of characters.
* @since 1.0
*/
TYPE_STRING = 8,
}
}
}