| 1 |
/* |
|---|
| 2 |
* SpaceNavigator |
|---|
| 3 |
* |
|---|
| 4 |
* Licensed under the MIT License |
|---|
| 5 |
* |
|---|
| 6 |
* Copyright (c) 2009 Lyuta Nakashima (www.atuyl.jp) |
|---|
| 7 |
* |
|---|
| 8 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 9 |
* of this software and associated documentation files (the "Software"), to deal |
|---|
| 10 |
* in the Software without restriction, including without limitation the rights |
|---|
| 11 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 12 |
* copies of the Software, and to permit persons to whom the Software is |
|---|
| 13 |
* furnished to do so, subject to the following conditions: |
|---|
| 14 |
* |
|---|
| 15 |
* The above copyright notice and this permission notice shall be included in |
|---|
| 16 |
* all copies or substantial portions of the Software. |
|---|
| 17 |
* |
|---|
| 18 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 19 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 20 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 21 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 22 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 23 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|---|
| 24 |
* THE SOFTWARE. |
|---|
| 25 |
*/ |
|---|
| 26 |
|
|---|
| 27 |
import comtypes.client as client |
|---|
| 28 |
from ctypes import * |
|---|
| 29 |
import socket |
|---|
| 30 |
|
|---|
| 31 |
import logging |
|---|
| 32 |
console = logging.StreamHandler() |
|---|
| 33 |
console.setLevel(logging.DEBUG) |
|---|
| 34 |
logging.getLogger('').addHandler(console) |
|---|
| 35 |
|
|---|
| 36 |
HOST = 'localhost' |
|---|
| 37 |
PORT = 15000 |
|---|
| 38 |
|
|---|
| 39 |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|---|
| 40 |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|---|
| 41 |
server.bind((HOST,PORT)) |
|---|
| 42 |
server.listen(1) |
|---|
| 43 |
|
|---|
| 44 |
class POINT(Structure): _fields_ = [("x", c_long),("y", c_long)] |
|---|
| 45 |
class MSG(Structure): _fields_ = [("hWnd", c_ulong),("message", c_uint),("wParam", c_ulong),("lParam", c_ulong),("time", c_ulong),("pt", POINT)] |
|---|
| 46 |
|
|---|
| 47 |
class SensorListener: |
|---|
| 48 |
def __init__(self, sensor,conn): |
|---|
| 49 |
self.sensor = sensor |
|---|
| 50 |
self.conn = conn |
|---|
| 51 |
|
|---|
| 52 |
def SensorInput(self, this, inval=None): |
|---|
| 53 |
val = str(self.sensor.Translation.X) + "," + str(self.sensor.Translation.Y) + "," + str(self.sensor.Translation.Z) + "," |
|---|
| 54 |
val += str(self.sensor.Rotation.X) + "," + str(self.sensor.Rotation.Y) + "," + str(self.sensor.Rotation.Z) + "\x00" |
|---|
| 55 |
self.conn.sendall(unicode(val,"utf_8")) |
|---|
| 56 |
|
|---|
| 57 |
if __name__ == '__main__': |
|---|
| 58 |
print 'Waiting for connections...' |
|---|
| 59 |
conn, address = server.accept() |
|---|
| 60 |
print "connect from:" , address |
|---|
| 61 |
|
|---|
| 62 |
device = client.CreateObject("TDxInput.Device") |
|---|
| 63 |
|
|---|
| 64 |
if not device.Connect(): print "Connected!" |
|---|
| 65 |
|
|---|
| 66 |
sensor = device.Sensor |
|---|
| 67 |
listener = SensorListener(sensor,conn) |
|---|
| 68 |
client.GetEvents(sensor,listener) |
|---|
| 69 |
|
|---|
| 70 |
while True: |
|---|
| 71 |
msg = byref(MSG()) |
|---|
| 72 |
windll.user32.GetMessageW(msg, None, 0, 0) |
|---|
| 73 |
windll.user32.DispatchMessageW(msg) |
|---|