test.cpp
author sl
Mon, 19 May 2014 21:11:52 +0200
changeset 1 5c2c48109457
child 2 2dbfd75c2bbe
permissions -rw-r--r--
Fixing solution file .
sl@0
     1
/*******************************************************
sl@0
     2
 Demo Program for HIDAPI
sl@0
     3
 
sl@0
     4
 Alan Ott
sl@0
     5
 Signal 11 Software
sl@0
     6
sl@0
     7
 2010-07-20
sl@0
     8
sl@0
     9
 Copyright 2010, All Rights Reserved
sl@0
    10
 
sl@0
    11
 This contents of this file may be used by anyone
sl@0
    12
 for any reason without any conditions and may be
sl@0
    13
 used as a starting point for your own applications
sl@0
    14
 which use HIDAPI.
sl@0
    15
********************************************************/
sl@0
    16
sl@0
    17
sl@0
    18
#include <fx.h>
sl@0
    19
sl@0
    20
#include "hidapi.h"
sl@0
    21
#include "mac_support.h"
sl@0
    22
#include <string.h>
sl@0
    23
#include <stdlib.h>
sl@0
    24
#include <limits.h>
sl@0
    25
sl@0
    26
#ifdef _WIN32
sl@0
    27
	// Thanks Microsoft, but I know how to use strncpy().
sl@0
    28
	#pragma warning(disable:4996)
sl@0
    29
#endif
sl@0
    30
sl@0
    31
class MainWindow : public FXMainWindow {
sl@0
    32
	FXDECLARE(MainWindow)
sl@0
    33
	
sl@0
    34
public:
sl@0
    35
	enum {
sl@0
    36
		ID_FIRST = FXMainWindow::ID_LAST,
sl@0
    37
		ID_CONNECT,
sl@0
    38
		ID_DISCONNECT,
sl@0
    39
		ID_RESCAN,
sl@0
    40
		ID_SEND_OUTPUT_REPORT,
sl@0
    41
		ID_SEND_FEATURE_REPORT,
sl@0
    42
		ID_GET_FEATURE_REPORT,
sl@0
    43
		ID_CLEAR,
sl@0
    44
		ID_TIMER,
sl@0
    45
		ID_MAC_TIMER,
sl@0
    46
		ID_LAST,
sl@0
    47
	};
sl@0
    48
	
sl@0
    49
private:
sl@0
    50
	FXList *device_list;
sl@0
    51
	FXButton *connect_button;
sl@0
    52
	FXButton *disconnect_button;
sl@0
    53
	FXButton *rescan_button;
sl@0
    54
	FXButton *output_button;
sl@0
    55
	FXLabel *connected_label;
sl@0
    56
	FXTextField *output_text;
sl@0
    57
	FXTextField *output_len;
sl@0
    58
	FXButton *feature_button;
sl@0
    59
	FXButton *get_feature_button;
sl@0
    60
	FXTextField *feature_text;
sl@0
    61
	FXTextField *feature_len;
sl@0
    62
	FXTextField *get_feature_text;
sl@0
    63
	FXText *input_text;
sl@0
    64
	FXFont *title_font;
sl@0
    65
	
sl@0
    66
	struct hid_device_info *devices;
sl@0
    67
	hid_device *connected_device;
sl@0
    68
	size_t getDataFromTextField(FXTextField *tf, char *buf, size_t len);
sl@0
    69
	int getLengthFromTextField(FXTextField *tf);
sl@0
    70
sl@0
    71
sl@0
    72
protected:
sl@0
    73
	MainWindow() {};
sl@0
    74
public:
sl@0
    75
	MainWindow(FXApp *a);
sl@0
    76
	~MainWindow();
sl@0
    77
	virtual void create();
sl@0
    78
	
sl@0
    79
	long onConnect(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    80
	long onDisconnect(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    81
	long onRescan(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    82
	long onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    83
	long onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    84
	long onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    85
	long onClear(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    86
	long onTimeout(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    87
	long onMacTimeout(FXObject *sender, FXSelector sel, void *ptr);
sl@0
    88
};
sl@0
    89
sl@0
    90
// FOX 1.7 changes the timeouts to all be nanoseconds.
sl@0
    91
// Fox 1.6 had all timeouts as milliseconds.
sl@0
    92
#if (FOX_MINOR >= 7)
sl@0
    93
	const int timeout_scalar = 1000*1000;
sl@0
    94
#else
sl@0
    95
	const int timeout_scalar = 1;
sl@0
    96
#endif
sl@0
    97
sl@0
    98
FXMainWindow *g_main_window;
sl@0
    99
sl@0
   100
sl@0
   101
FXDEFMAP(MainWindow) MainWindowMap [] = {
sl@0
   102
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CONNECT, MainWindow::onConnect ),
sl@0
   103
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_DISCONNECT, MainWindow::onDisconnect ),
sl@0
   104
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_RESCAN, MainWindow::onRescan ),
sl@0
   105
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_OUTPUT_REPORT, MainWindow::onSendOutputReport ),
sl@0
   106
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_SEND_FEATURE_REPORT, MainWindow::onSendFeatureReport ),
sl@0
   107
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_GET_FEATURE_REPORT, MainWindow::onGetFeatureReport ),
sl@0
   108
	FXMAPFUNC(SEL_COMMAND, MainWindow::ID_CLEAR, MainWindow::onClear ),
sl@0
   109
	FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_TIMER, MainWindow::onTimeout ),
sl@0
   110
	FXMAPFUNC(SEL_TIMEOUT, MainWindow::ID_MAC_TIMER, MainWindow::onMacTimeout ),
sl@0
   111
};
sl@0
   112
sl@0
   113
FXIMPLEMENT(MainWindow, FXMainWindow, MainWindowMap, ARRAYNUMBER(MainWindowMap));
sl@0
   114
sl@0
   115
MainWindow::MainWindow(FXApp *app)
sl@0
   116
	: FXMainWindow(app, "HIDAPI Test Application", NULL, NULL, DECOR_ALL, 200,100, 425,700)
sl@0
   117
{
sl@0
   118
	devices = NULL;
sl@0
   119
	connected_device = NULL;
sl@0
   120
sl@0
   121
	FXVerticalFrame *vf = new FXVerticalFrame(this, LAYOUT_FILL_Y|LAYOUT_FILL_X);
sl@0
   122
sl@0
   123
	FXLabel *label = new FXLabel(vf, "HIDAPI Test Tool");
sl@0
   124
	title_font = new FXFont(getApp(), "Arial", 14, FXFont::Bold);
sl@0
   125
	label->setFont(title_font);
sl@0
   126
	
sl@0
   127
	new FXLabel(vf,
sl@0
   128
		"Select a device and press Connect.", NULL, JUSTIFY_LEFT);
sl@0
   129
	new FXLabel(vf,
sl@0
   130
		"Output data bytes can be entered in the Output section, \n"
sl@0
   131
		"separated by space, comma or brackets. Data starting with 0x\n"
sl@0
   132
		"is treated as hex. Data beginning with a 0 is treated as \n"
sl@0
   133
		"octal. All other data is treated as decimal.", NULL, JUSTIFY_LEFT);
sl@0
   134
	new FXLabel(vf,
sl@0
   135
		"Data received from the device appears in the Input section.",
sl@0
   136
		NULL, JUSTIFY_LEFT);
sl@0
   137
	new FXLabel(vf,
sl@0
   138
		"Optionally, a report length may be specified. Extra bytes are\n"
sl@0
   139
		"padded with zeros. If no length is specified, the length is \n"
sl@0
   140
		"inferred from the data.",
sl@0
   141
		NULL, JUSTIFY_LEFT);
sl@0
   142
	new FXLabel(vf, "");
sl@0
   143
sl@0
   144
	// Device List and Connect/Disconnect buttons
sl@0
   145
	FXHorizontalFrame *hf = new FXHorizontalFrame(vf, LAYOUT_FILL_X);
sl@0
   146
	//device_list = new FXList(new FXHorizontalFrame(hf,FRAME_SUNKEN|FRAME_THICK, 0,0,0,0, 0,0,0,0), NULL, 0, LISTBOX_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y|LAYOUT_FIX_WIDTH|LAYOUT_FIX_HEIGHT, 0,0,300,200);
sl@0
   147
	device_list = new FXList(new FXHorizontalFrame(hf,FRAME_SUNKEN|FRAME_THICK|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,0,0, 0,0,0,0), NULL, 0, LISTBOX_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_Y, 0,0,300,200);
sl@0
   148
	FXVerticalFrame *buttonVF = new FXVerticalFrame(hf);
sl@0
   149
	connect_button = new FXButton(buttonVF, "Connect", NULL, this, ID_CONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   150
	disconnect_button = new FXButton(buttonVF, "Disconnect", NULL, this, ID_DISCONNECT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   151
	disconnect_button->disable();
sl@0
   152
	rescan_button = new FXButton(buttonVF, "Re-Scan devices", NULL, this, ID_RESCAN, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   153
	new FXHorizontalFrame(buttonVF, 0, 0,0,0,0, 0,0,50,0);
sl@0
   154
sl@0
   155
	connected_label = new FXLabel(vf, "Disconnected");
sl@0
   156
	
sl@0
   157
	new FXHorizontalFrame(vf);
sl@0
   158
	
sl@0
   159
	// Output Group Box
sl@0
   160
	FXGroupBox *gb = new FXGroupBox(vf, "Output", FRAME_GROOVE|LAYOUT_FILL_X);
sl@0
   161
	FXMatrix *matrix = new FXMatrix(gb, 3, MATRIX_BY_COLUMNS|LAYOUT_FILL_X);
sl@0
   162
	new FXLabel(matrix, "Data");
sl@0
   163
	new FXLabel(matrix, "Length");
sl@0
   164
	new FXLabel(matrix, "");
sl@0
   165
sl@0
   166
	//hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
sl@0
   167
	output_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   168
	output_text->setText("1 0x81 0");
sl@0
   169
	output_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   170
	output_button = new FXButton(matrix, "Send Output Report", NULL, this, ID_SEND_OUTPUT_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   171
	output_button->disable();
sl@0
   172
	//new FXHorizontalFrame(matrix, LAYOUT_FILL_X);
sl@0
   173
sl@0
   174
	//hf = new FXHorizontalFrame(gb, LAYOUT_FILL_X);
sl@0
   175
	feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   176
	feature_len = new FXTextField(matrix, 5, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   177
	feature_button = new FXButton(matrix, "Send Feature Report", NULL, this, ID_SEND_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   178
	feature_button->disable();
sl@0
   179
sl@0
   180
	get_feature_text = new FXTextField(matrix, 30, NULL, 0, TEXTFIELD_NORMAL|LAYOUT_FILL_X|LAYOUT_FILL_COLUMN);
sl@0
   181
	new FXWindow(matrix);
sl@0
   182
	get_feature_button = new FXButton(matrix, "Get Feature Report", NULL, this, ID_GET_FEATURE_REPORT, BUTTON_NORMAL|LAYOUT_FILL_X);
sl@0
   183
	get_feature_button->disable();
sl@0
   184
sl@0
   185
sl@0
   186
	// Input Group Box
sl@0
   187
	gb = new FXGroupBox(vf, "Input", FRAME_GROOVE|LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   188
	FXVerticalFrame *innerVF = new FXVerticalFrame(gb, LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   189
	input_text = new FXText(new FXHorizontalFrame(innerVF,LAYOUT_FILL_X|LAYOUT_FILL_Y|FRAME_SUNKEN|FRAME_THICK, 0,0,0,0, 0,0,0,0), NULL, 0, LAYOUT_FILL_X|LAYOUT_FILL_Y);
sl@0
   190
	input_text->setEditable(false);
sl@0
   191
	new FXButton(innerVF, "Clear", NULL, this, ID_CLEAR, BUTTON_NORMAL|LAYOUT_RIGHT);
sl@0
   192
	
sl@0
   193
sl@0
   194
}
sl@0
   195
sl@0
   196
MainWindow::~MainWindow()
sl@0
   197
{
sl@0
   198
	if (connected_device)
sl@0
   199
		hid_close(connected_device);
sl@0
   200
	hid_exit();
sl@0
   201
	delete title_font;
sl@0
   202
}
sl@0
   203
sl@0
   204
void
sl@0
   205
MainWindow::create()
sl@0
   206
{
sl@0
   207
	FXMainWindow::create();
sl@0
   208
	show();
sl@0
   209
sl@0
   210
	onRescan(NULL, 0, NULL);
sl@0
   211
	
sl@0
   212
sl@0
   213
#ifdef __APPLE__
sl@0
   214
	init_apple_message_system();
sl@0
   215
#endif
sl@0
   216
	
sl@0
   217
	getApp()->addTimeout(this, ID_MAC_TIMER,
sl@0
   218
		50 * timeout_scalar /*50ms*/);
sl@0
   219
}
sl@0
   220
sl@0
   221
long
sl@0
   222
MainWindow::onConnect(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   223
{
sl@0
   224
	if (connected_device != NULL)
sl@0
   225
		return 1;
sl@0
   226
	
sl@0
   227
	FXint cur_item = device_list->getCurrentItem();
sl@0
   228
	if (cur_item < 0)
sl@0
   229
		return -1;
sl@0
   230
	FXListItem *item = device_list->getItem(cur_item);
sl@0
   231
	if (!item)
sl@0
   232
		return -1;
sl@0
   233
	struct hid_device_info *device_info = (struct hid_device_info*) item->getData();
sl@0
   234
	if (!device_info)
sl@0
   235
		return -1;
sl@0
   236
	
sl@0
   237
	connected_device =  hid_open_path(device_info->path);
sl@0
   238
	
sl@0
   239
	if (!connected_device) {
sl@0
   240
		FXMessageBox::error(this, MBOX_OK, "Device Error", "Unable To Connect to Device");
sl@0
   241
		return -1;
sl@0
   242
	}
sl@0
   243
	
sl@0
   244
	hid_set_nonblocking(connected_device, 1);
sl@0
   245
sl@0
   246
	getApp()->addTimeout(this, ID_TIMER,
sl@0
   247
		5 * timeout_scalar /*5ms*/);
sl@0
   248
	
sl@0
   249
	FXString s;
sl@0
   250
	s.format("Connected to: %04hx:%04hx -", device_info->vendor_id, device_info->product_id);
sl@0
   251
	s += FXString(" ") + device_info->manufacturer_string;
sl@0
   252
	s += FXString(" ") + device_info->product_string;
sl@0
   253
	connected_label->setText(s);
sl@0
   254
	output_button->enable();
sl@0
   255
	feature_button->enable();
sl@0
   256
	get_feature_button->enable();
sl@0
   257
	connect_button->disable();
sl@0
   258
	disconnect_button->enable();
sl@0
   259
	input_text->setText("");
sl@0
   260
sl@0
   261
sl@0
   262
	return 1;
sl@0
   263
}
sl@0
   264
sl@0
   265
long
sl@0
   266
MainWindow::onDisconnect(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   267
{
sl@0
   268
	hid_close(connected_device);
sl@0
   269
	connected_device = NULL;
sl@0
   270
	connected_label->setText("Disconnected");
sl@0
   271
	output_button->disable();
sl@0
   272
	feature_button->disable();
sl@0
   273
	get_feature_button->disable();
sl@0
   274
	connect_button->enable();
sl@0
   275
	disconnect_button->disable();
sl@0
   276
sl@0
   277
	getApp()->removeTimeout(this, ID_TIMER);
sl@0
   278
	
sl@0
   279
	return 1;
sl@0
   280
}
sl@0
   281
sl@0
   282
long
sl@0
   283
MainWindow::onRescan(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   284
{
sl@0
   285
	struct hid_device_info *cur_dev;
sl@0
   286
sl@0
   287
	device_list->clearItems();
sl@0
   288
	
sl@0
   289
	// List the Devices
sl@0
   290
	hid_free_enumeration(devices);
sl@0
   291
	devices = hid_enumerate(0x0, 0x0);
sl@0
   292
	cur_dev = devices;	
sl@0
   293
	while (cur_dev) {
sl@0
   294
		// Add it to the List Box.
sl@0
   295
		FXString s;
sl@0
   296
		FXString usage_str;
sl@0
   297
		s.format("%04hx:%04hx -", cur_dev->vendor_id, cur_dev->product_id);
sl@0
   298
		s += FXString(" ") + cur_dev->manufacturer_string;
sl@0
   299
		s += FXString(" ") + cur_dev->product_string;
sl@0
   300
		usage_str.format(" (usage: %04hx:%04hx) ", cur_dev->usage_page, cur_dev->usage);
sl@0
   301
		s += usage_str;
sl@0
   302
		FXListItem *li = new FXListItem(s, NULL, cur_dev);
sl@0
   303
		device_list->appendItem(li);
sl@0
   304
		
sl@0
   305
		cur_dev = cur_dev->next;
sl@0
   306
	}
sl@0
   307
sl@0
   308
	if (device_list->getNumItems() == 0)
sl@0
   309
		device_list->appendItem("*** No Devices Connected ***");
sl@0
   310
	else {
sl@0
   311
		device_list->selectItem(0);
sl@0
   312
	}
sl@0
   313
sl@0
   314
	return 1;
sl@0
   315
}
sl@0
   316
sl@0
   317
size_t
sl@0
   318
MainWindow::getDataFromTextField(FXTextField *tf, char *buf, size_t len)
sl@0
   319
{
sl@0
   320
	const char *delim = " ,{}\t\r\n";
sl@0
   321
	FXString data = tf->getText();
sl@0
   322
	const FXchar *d = data.text();
sl@0
   323
	size_t i = 0;
sl@0
   324
	
sl@0
   325
	// Copy the string from the GUI.
sl@0
   326
	size_t sz = strlen(d);
sl@0
   327
	char *str = (char*) malloc(sz+1);
sl@0
   328
	strcpy(str, d);
sl@0
   329
	
sl@0
   330
	// For each token in the string, parse and store in buf[].
sl@0
   331
	char *token = strtok(str, delim);
sl@0
   332
	while (token) {
sl@0
   333
		char *endptr;
sl@0
   334
		long int val = strtol(token, &endptr, 0);
sl@0
   335
		buf[i++] = val;
sl@0
   336
		token = strtok(NULL, delim);
sl@0
   337
	}
sl@0
   338
	
sl@0
   339
	free(str);
sl@0
   340
	return i;
sl@0
   341
}
sl@0
   342
sl@0
   343
/* getLengthFromTextField()
sl@0
   344
   Returns length:
sl@0
   345
	 0: empty text field
sl@0
   346
	>0: valid length
sl@0
   347
	-1: invalid length */
sl@0
   348
int
sl@0
   349
MainWindow::getLengthFromTextField(FXTextField *tf)
sl@0
   350
{
sl@0
   351
	long int len;
sl@0
   352
	FXString str = tf->getText();
sl@0
   353
	size_t sz = str.length();
sl@0
   354
sl@0
   355
	if (sz > 0) {
sl@0
   356
		char *endptr;
sl@0
   357
		len = strtol(str.text(), &endptr, 0);
sl@0
   358
		if (endptr != str.text() && *endptr == '\0') {
sl@0
   359
			if (len <= 0) {
sl@0
   360
				FXMessageBox::error(this, MBOX_OK, "Invalid length", "Enter a length greater than zero.");
sl@0
   361
				return -1;
sl@0
   362
			}
sl@0
   363
			return len;
sl@0
   364
		}
sl@0
   365
		else
sl@0
   366
			return -1;
sl@0
   367
	}
sl@0
   368
sl@0
   369
	return 0;
sl@0
   370
}
sl@0
   371
sl@0
   372
long
sl@0
   373
MainWindow::onSendOutputReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   374
{
sl@0
   375
	char buf[256];
sl@0
   376
	size_t data_len, len;
sl@0
   377
	int textfield_len;
sl@0
   378
sl@0
   379
	memset(buf, 0x0, sizeof(buf));
sl@0
   380
	textfield_len = getLengthFromTextField(output_len);
sl@0
   381
	data_len = getDataFromTextField(output_text, buf, sizeof(buf));
sl@0
   382
sl@0
   383
	if (textfield_len < 0) {
sl@0
   384
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
sl@0
   385
		return 1;
sl@0
   386
	}
sl@0
   387
sl@0
   388
	if (textfield_len > sizeof(buf)) {
sl@0
   389
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
sl@0
   390
		return 1;
sl@0
   391
	}
sl@0
   392
sl@0
   393
	len = (textfield_len)? textfield_len: data_len;
sl@0
   394
sl@0
   395
	int res = hid_write(connected_device, (const unsigned char*)buf, len);
sl@0
   396
	if (res < 0) {
sl@0
   397
		FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not write to device. Error reported was: %ls", hid_error(connected_device));
sl@0
   398
	}
sl@0
   399
	
sl@0
   400
	return 1;
sl@0
   401
}
sl@0
   402
sl@0
   403
long
sl@0
   404
MainWindow::onSendFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   405
{
sl@0
   406
	char buf[256];
sl@0
   407
	size_t data_len, len;
sl@0
   408
	int textfield_len;
sl@0
   409
sl@0
   410
	memset(buf, 0x0, sizeof(buf));
sl@0
   411
	textfield_len = getLengthFromTextField(feature_len);
sl@0
   412
	data_len = getDataFromTextField(feature_text, buf, sizeof(buf));
sl@0
   413
sl@0
   414
	if (textfield_len < 0) {
sl@0
   415
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is invalid. Please enter a number in hex, octal, or decimal.");
sl@0
   416
		return 1;
sl@0
   417
	}
sl@0
   418
sl@0
   419
	if (textfield_len > sizeof(buf)) {
sl@0
   420
		FXMessageBox::error(this, MBOX_OK, "Invalid length", "Length field is too long.");
sl@0
   421
		return 1;
sl@0
   422
	}
sl@0
   423
sl@0
   424
	len = (textfield_len)? textfield_len: data_len;
sl@0
   425
sl@0
   426
	int res = hid_send_feature_report(connected_device, (const unsigned char*)buf, len); 
sl@0
   427
	if (res < 0) {
sl@0
   428
		FXMessageBox::error(this, MBOX_OK, "Error Writing", "Could not send feature report to device. Error reported was: %ls", hid_error(connected_device));
sl@0
   429
	}
sl@0
   430
sl@0
   431
	return 1;
sl@0
   432
}
sl@0
   433
sl@0
   434
long
sl@0
   435
MainWindow::onGetFeatureReport(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   436
{
sl@0
   437
	char buf[256];
sl@0
   438
	size_t len;
sl@0
   439
sl@0
   440
	memset(buf, 0x0, sizeof(buf));
sl@0
   441
	len = getDataFromTextField(get_feature_text, buf, sizeof(buf));
sl@0
   442
sl@0
   443
	if (len != 1) {
sl@0
   444
		FXMessageBox::error(this, MBOX_OK, "Too many numbers", "Enter only a single report number in the text field");
sl@0
   445
	}
sl@0
   446
sl@0
   447
	int res = hid_get_feature_report(connected_device, (unsigned char*)buf, sizeof(buf));
sl@0
   448
	if (res < 0) {
sl@0
   449
		FXMessageBox::error(this, MBOX_OK, "Error Getting Report", "Could not get feature report from device. Error reported was: %ls", hid_error(connected_device));
sl@0
   450
	}
sl@0
   451
sl@0
   452
	if (res > 0) {
sl@0
   453
		FXString s;
sl@0
   454
		s.format("Returned Feature Report. %d bytes:\n", res);
sl@0
   455
		for (int i = 0; i < res; i++) {
sl@0
   456
			FXString t;
sl@0
   457
			t.format("%02hhx ", buf[i]);
sl@0
   458
			s += t;
sl@0
   459
			if ((i+1) % 4 == 0)
sl@0
   460
				s += " ";
sl@0
   461
			if ((i+1) % 16 == 0)
sl@0
   462
				s += "\n";
sl@0
   463
		}
sl@0
   464
		s += "\n";
sl@0
   465
		input_text->appendText(s);
sl@0
   466
		input_text->setBottomLine(INT_MAX);
sl@0
   467
	}
sl@0
   468
	
sl@0
   469
	return 1;
sl@0
   470
}
sl@0
   471
sl@0
   472
long
sl@0
   473
MainWindow::onClear(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   474
{
sl@0
   475
	input_text->setText("");
sl@0
   476
	return 1;
sl@0
   477
}
sl@0
   478
sl@0
   479
long
sl@0
   480
MainWindow::onTimeout(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   481
{
sl@0
   482
	unsigned char buf[256];
sl@0
   483
	int res = hid_read(connected_device, buf, sizeof(buf));
sl@0
   484
	
sl@0
   485
	if (res > 0) {
sl@0
   486
		FXString s;
sl@0
   487
		s.format("Received %d bytes:\n", res);
sl@0
   488
		for (int i = 0; i < res; i++) {
sl@0
   489
			FXString t;
sl@0
   490
			t.format("%02hhx ", buf[i]);
sl@0
   491
			s += t;
sl@0
   492
			if ((i+1) % 4 == 0)
sl@0
   493
				s += " ";
sl@0
   494
			if ((i+1) % 16 == 0)
sl@0
   495
				s += "\n";
sl@0
   496
		}
sl@0
   497
		s += "\n";
sl@0
   498
		input_text->appendText(s);
sl@0
   499
		input_text->setBottomLine(INT_MAX);
sl@0
   500
	}
sl@0
   501
	if (res < 0) {
sl@0
   502
		input_text->appendText("hid_read() returned error\n");
sl@0
   503
		input_text->setBottomLine(INT_MAX);
sl@0
   504
	}
sl@0
   505
sl@0
   506
	getApp()->addTimeout(this, ID_TIMER,
sl@0
   507
		5 * timeout_scalar /*5ms*/);
sl@0
   508
	return 1;
sl@0
   509
}
sl@0
   510
sl@0
   511
long
sl@0
   512
MainWindow::onMacTimeout(FXObject *sender, FXSelector sel, void *ptr)
sl@0
   513
{
sl@0
   514
#ifdef __APPLE__
sl@0
   515
	check_apple_events();
sl@0
   516
	
sl@0
   517
	getApp()->addTimeout(this, ID_MAC_TIMER,
sl@0
   518
		50 * timeout_scalar /*50ms*/);
sl@0
   519
#endif
sl@0
   520
sl@0
   521
	return 1;
sl@0
   522
}
sl@0
   523
sl@0
   524
int main(int argc, char **argv)
sl@0
   525
{
sl@0
   526
	FXApp app("HIDAPI Test Application", "Signal 11 Software");
sl@0
   527
	app.init(argc, argv);
sl@0
   528
	g_main_window = new MainWindow(&app);
sl@0
   529
	app.create();
sl@0
   530
	app.run();
sl@0
   531
	return 0;
sl@0
   532
}