]> git.mxchange.org Git - flightgear.git/blob - utils/js_server/js_server.cxx
Canvas: Add new element type map for geo mapping.
[flightgear.git] / utils / js_server / js_server.cxx
1 /*
2      Plib based joystick server based on PLIBs js_demo.cxx
3
4      js_server is Copyright (C) 2003 
5      by Stephen Lowry and Manuel Bessler
6
7      PLIB - A Suite of Portable Game Libraries
8      Copyright (C) 2001  Steve Baker
9
10      This program is free software; you can redistribute it and/or modify
11      it under the terms of the GNU General Public License as published by
12      the Free Software Foundation; either version 2 of the License, or
13      (at your option) any later version.
14
15      This program is distributed in the hope that it will be useful,
16      but WITHOUT ANY WARRANTY; without even the implied warranty of
17      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18      GNU General Public License for more details.
19
20      You should have received a copy of the GNU General Public License
21      along with this program; if not, write to the Free Software
22      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23
24      For further information visit http://plib.sourceforge.net
25
26 */
27
28 #include <math.h>
29 #include <stdio.h>
30 #include <stdint.h>
31
32 #include <plib/netSocket.h>
33 #include <plib/js.h>
34
35 void usage(char * progname)
36 {
37     printf("This is an UDP based remote joystick server.\n");
38     printf("usage: %s <hostname> <port>\n", progname);
39 }
40
41 int main ( int argc, char ** argv )
42 {
43   jsJoystick * js ;
44   float      * ax;
45   int port = 16759;
46   char * host; /* = "192.168.1.7"; */
47   int activeaxes = 4;
48
49   if( argc != 3 )
50   {
51     usage(argv[0]); 
52     exit(1);
53   }
54   host = argv[1];
55   port = atoi(argv[2]);
56
57   jsInit () ;
58
59   js = new jsJoystick ( 0 ) ;
60
61   if ( js->notWorking () )
62   {
63     printf ( "no Joystick detected... exitting\n" ) ;
64     exit(1);     
65   }
66   printf ( "Joystick is \"%s\"\n", js->getName() ) ;
67
68   int numaxes = js->getNumAxes();
69   ax = new float [ numaxes ] ;
70   activeaxes = numaxes;
71   
72   if( numaxes > 4 )
73   {
74     printf("max 4 axes joysticks supported at the moment, however %i axes were detected\nWill only use the first 4 axes!\n", numaxes);
75     activeaxes = 4;
76   }
77
78   // Must call this before any other net stuff
79   netInit( &argc,argv );
80
81   netSocket c;
82
83   if ( ! c.open( false ) ) {    // open a UDP socket
84         printf("error opening socket\n");
85         return -1;
86     }
87
88     c.setBlocking( false );
89
90     if ( c.connect( host, port ) == -1 ) {
91         printf("error connecting to %s:%d\n", host, port);
92         return -1;
93     }
94
95
96   char packet[256] = "Hello world!";
97   while(1)
98   {
99         int b;
100         int len = 0;
101         int axis = 0;
102
103         js->read( &b, ax );
104         for ( axis = 0 ; axis < activeaxes ; axis++ )
105         {
106           int32_t axisvalue = (int32_t)(ax[axis]*2147483647.0);
107           printf("axisval=%li\n", (long)axisvalue);
108           memcpy(packet+len, &axisvalue, sizeof(axisvalue));
109           len+=sizeof(axisvalue);
110         }
111         // fill emtpy values into packes when less than 4 axes
112         for( ; axis < 4; axis++ )
113         {
114           int32_t axisvalue = 0;
115           memcpy(packet+len, &axisvalue, sizeof(axisvalue));
116           len+=sizeof(axisvalue);
117         }
118
119         int32_t b_l = b;
120         memcpy(packet+len, &b_l, sizeof(b_l));
121         len+=sizeof(b_l);
122
123         const char * termstr = "\0\0\r\n";
124         memcpy(packet+len, termstr, 4);
125         len += 4;
126
127         c.send( packet, len, 0 );
128
129     /* give other processes a chance */
130
131 #ifdef _WIN32
132     Sleep ( 1 ) ;
133 #elif defined(sgi)
134     sginap ( 1 ) ;
135 #else
136     usleep ( 200 ) ;
137 #endif
138     printf(".");
139     fflush(stdout);
140   }
141
142   return 0 ;
143 }