1 | // Copyright (c) 2013 by István Váradi
|
---|
2 |
|
---|
3 | // This file is part of XPLRA, a remote-access plugin for X-Plane
|
---|
4 |
|
---|
5 | // Redistribution and use in source and binary forms, with or without
|
---|
6 | // modification, are permitted provided that the following conditions are met:
|
---|
7 |
|
---|
8 | // 1. Redistributions of source code must retain the above copyright notice, this
|
---|
9 | // list of conditions and the following disclaimer.
|
---|
10 | // 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | // this list of conditions and the following disclaimer in the documentation
|
---|
12 | // and/or other materials provided with the distribution.
|
---|
13 |
|
---|
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
---|
15 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
---|
16 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
---|
17 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
---|
18 | // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
---|
19 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
---|
20 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
---|
21 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
22 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
23 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
24 |
|
---|
25 | // The views and conclusions contained in the software and documentation are those
|
---|
26 | // of the authors and should not be interpreted as representing official policies,
|
---|
27 | // either expressed or implied, of the FreeBSD Project.
|
---|
28 |
|
---|
29 | //------------------------------------------------------------------------------
|
---|
30 |
|
---|
31 | #include <hu/varadiistvan/xplra/xplra.h>
|
---|
32 |
|
---|
33 | #include "ccommon.h"
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <string.h>
|
---|
37 |
|
---|
38 | #ifdef _WIN32
|
---|
39 | #include <windows.h>
|
---|
40 | #else
|
---|
41 | #include <unistd.h>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | //------------------------------------------------------------------------------
|
---|
45 |
|
---|
46 | #ifdef _WIN32
|
---|
47 | void xplra_sleep(int ms)
|
---|
48 | {
|
---|
49 | Sleep(ms);
|
---|
50 | }
|
---|
51 | #else
|
---|
52 | void xplra_sleep(int ms)
|
---|
53 | {
|
---|
54 | usleep(ms*1000);
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 |
|
---|
59 | //------------------------------------------------------------------------------
|
---|
60 |
|
---|
61 | int main(int argc, char* argv[])
|
---|
62 | {
|
---|
63 | int retval = 0;
|
---|
64 | const char* errorString = 0;
|
---|
65 |
|
---|
66 | uint16_t codes[] = { 0x0241, 0x0142, 0x0343, 0x0251 };
|
---|
67 |
|
---|
68 | printf("Connecting to X-Plane...\n");
|
---|
69 | int connectionID = connect_xplane(argc, argv);
|
---|
70 |
|
---|
71 | if (connectionID>=0) {
|
---|
72 | printf("Connected.\n\n");
|
---|
73 | } else {
|
---|
74 | printf("Connection failed.\n");
|
---|
75 | goto cleanup;
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | printf("Registering hotkeys: Ctrl+A, Shift+B, Ctrl+Shift+C, Ctrl+Q...\n");
|
---|
80 | if (xplra_register_hotkeys(connectionID,
|
---|
81 | codes, sizeof(codes)/sizeof(uint16_t))<0)
|
---|
82 | {
|
---|
83 | goto error;
|
---|
84 | }
|
---|
85 | printf("Registered hotkeys...\n\n");
|
---|
86 |
|
---|
87 | printf("Listening to hotkeys...\n");
|
---|
88 | while(1) {
|
---|
89 | uint8_t states[4];
|
---|
90 | char message[256];
|
---|
91 |
|
---|
92 | if (xplra_query_hotkeys(connectionID, states, sizeof(states))<0) {
|
---|
93 | goto error;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (states[3]!=0) {
|
---|
97 | if (xplra_unregister_hotkeys(connectionID)<0) {
|
---|
98 | goto error;
|
---|
99 | }
|
---|
100 | break;
|
---|
101 | }
|
---|
102 |
|
---|
103 | message[0] = '\0';
|
---|
104 |
|
---|
105 | if (states[0]!=0) {
|
---|
106 | if (message[0]!='\0') strcat(message, ", ");
|
---|
107 | strcat(message, "Ctrl+A");
|
---|
108 | }
|
---|
109 | if (states[1]!=0) {
|
---|
110 | if (message[0]!='\0') strcat(message, ", ");
|
---|
111 | strcat(message, "Shift+B");
|
---|
112 | }
|
---|
113 | if (states[2]!=0) {
|
---|
114 | if (message[0]!='\0') strcat(message, ", ");
|
---|
115 | strcat(message, "Ctrl+Shift+C");
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (message[0]!='\0') {
|
---|
119 | strcat(message, " pressed");
|
---|
120 | if (xplra_show_message(connectionID, message, 3.0)<0) {
|
---|
121 | goto error;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | xplra_sleep(500);
|
---|
126 | }
|
---|
127 |
|
---|
128 | goto cleanup;
|
---|
129 |
|
---|
130 | error:
|
---|
131 | errorString = xplra_get_last_error_string(connectionID);
|
---|
132 | if (errorString==0) {
|
---|
133 | printf("\nUnknown error occured!\n");
|
---|
134 | } else {
|
---|
135 | printf("\nError: %s\n", errorString);
|
---|
136 | }
|
---|
137 |
|
---|
138 | cleanup:
|
---|
139 | if (connectionID>=0) xplra_disconnect(connectionID);
|
---|
140 | return retval;
|
---|
141 | }
|
---|
142 |
|
---|
143 | //------------------------------------------------------------------------------
|
---|
144 |
|
---|
145 | // Local Variables:
|
---|
146 | // mode: C++
|
---|
147 | // c-basic-offset: 4
|
---|
148 | // indent-tabs-mode: nil
|
---|
149 | // End:
|
---|