-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmtrace.cpp
112 lines (85 loc) · 2.1 KB
/
pmtrace.cpp
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
/*
* Sample application for debugging Powermate class.
*
* It traces events received from the powermate.
*/
#include "powermate.h"
#include <fcntl.h>
#include <getopt.h>
#include <cassert>
#include <iostream>
#include <cstdlib>
using namespace std;
struct Options {
string device;
bool traceRaw;
bool traceEvents;
int position;
Options() : traceRaw( false ), traceEvents( false ), position( 0 ) {}
};
static bool parseArgs( Options& options, int argc, char* argv[] ) {
bool success = true;
while ( true ) {
enum {
DEVICE,
TRACERAW,
TRACEEVENTS,
POSITION,
};
static const struct option longOptions[] = {
{ "device", required_argument, 0, DEVICE },
{ "traceraw", no_argument, 0, TRACERAW },
{ "traceevents", no_argument, 0, TRACEEVENTS },
{ "position", required_argument, 0, POSITION },
{ 0, 0, 0, 0 }
};
int optionIndex = 0;
int c = getopt_long( argc, argv, "", longOptions, &optionIndex);
if (c == -1) break;
switch (c) {
case DEVICE:
assert( optarg );
options.device = optarg;
break;
case TRACERAW:
options.traceRaw = true;
break;
case TRACEEVENTS:
options.traceEvents = true;
break;
case POSITION:
assert( optarg );
options.position = atoi( optarg );
// FIXME check result.
break;
default:
success = false;
break;
}
}
if (optind < argc) {
success = false;
}
return success;
}
int main( int argc, char* argv[] ) {
Options options;
bool success = parseArgs( options, argc, argv );
if ( ! success ) {
cerr << "Unable to parse command line" << endl;
return 1;
}
Powermate powermate;
powermate.setTraceRaw( options.traceRaw );
powermate.setTraceEvents( options.traceEvents );
powermate.setPosition( options.position );
success = powermate.openReadDevice( options.device );
if ( ! success ) {
cerr << "Unable open Powermate" << endl;
return 1;
}
Powermate::State state;
while ( powermate.waitForInput( state ) ) {
}
return 0;
}