source: xplra/test/hotkeyctest.c@ 99:bb320ead601d

Last change on this file since 99:bb320ead601d was 55:fa05d8dd30a2, checked in by István Váradi <ivaradi@…>, 12 years ago

Added the C client API for hotkey handling and the test program. Also changed the name of _sleep to xplra_sleep to be able to compile the code for Windows

File size: 4.2 KB
Line 
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 <stdio.h>
34#include <string.h>
35
36#ifdef _WIN32
37#include <windows.h>
38#else
39#include <unistd.h>
40#endif
41
42//------------------------------------------------------------------------------
43
44#ifdef _WIN32
45void xplra_sleep(int ms)
46{
47 Sleep(ms);
48}
49#else
50void xplra_sleep(int ms)
51{
52 usleep(ms*1000);
53}
54#endif
55
56
57//------------------------------------------------------------------------------
58
59int main()
60{
61 int retval = 0;
62 const char* errorString = 0;
63
64 uint16_t codes[] = { 0x0241, 0x0142, 0x0343, 0x0251 };
65
66 printf("Connecting to X-Plane...\n");
67 int connectionID = xplra_connect();
68
69 if (connectionID>=0) {
70 printf("Connected.\n\n");
71 } else {
72 printf("Connection failed.\n");
73 goto cleanup;
74 }
75
76
77 printf("Registering hotkeys: Ctrl+A, Shift+B, Ctrl+Shift+C, Ctrl+Q...\n");
78 if (xplra_register_hotkeys(connectionID,
79 codes, sizeof(codes)/sizeof(uint16_t))<0)
80 {
81 goto error;
82 }
83 printf("Registered hotkeys...\n\n");
84
85 printf("Listening to hotkeys...\n");
86 while(1) {
87 uint8_t states[4];
88 char message[256];
89
90 if (xplra_query_hotkeys(connectionID, states, sizeof(states))<0) {
91 goto error;
92 }
93
94 if (states[3]!=0) {
95 if (xplra_unregister_hotkeys(connectionID)<0) {
96 goto error;
97 }
98 break;
99 }
100
101 message[0] = '\0';
102
103 if (states[0]!=0) {
104 if (message[0]!='\0') strcat(message, ", ");
105 strcat(message, "Ctrl+A");
106 }
107 if (states[1]!=0) {
108 if (message[0]!='\0') strcat(message, ", ");
109 strcat(message, "Shift+B");
110 }
111 if (states[2]!=0) {
112 if (message[0]!='\0') strcat(message, ", ");
113 strcat(message, "Ctrl+Shift+C");
114 }
115
116 if (message[0]!='\0') {
117 strcat(message, " pressed");
118 if (xplra_show_message(connectionID, message, 3.0)<0) {
119 goto error;
120 }
121 }
122
123 xplra_sleep(500);
124 }
125
126 goto cleanup;
127
128error:
129 errorString = xplra_get_last_error_string(connectionID);
130 if (errorString==0) {
131 printf("\nUnknown error occured!\n");
132 } else {
133 printf("\nError: %s\n", errorString);
134 }
135
136cleanup:
137 if (connectionID>=0) xplra_disconnect(connectionID);
138 return retval;
139}
140
141//------------------------------------------------------------------------------
142
143// Local Variables:
144// mode: C++
145// c-basic-offset: 4
146// indent-tabs-mode: nil
147// End:
Note: See TracBrowser for help on using the repository browser.