# HG changeset patch # User John Tsiombikas # Date 1373334411 -10800 # Node ID 0219182eb47b23e925607a5a59b4ce825295cdca initial commit diff -r 000000000000 -r 0219182eb47b .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Tue Jul 09 04:46:51 2013 +0300 @@ -0,0 +1,8 @@ +\.o$ +\.d$ +\.swp$ +\.ncb$ +\.suo$ +\.vcproj\..*\.user$ +^Debug/ +^Release/ diff -r 000000000000 -r 0219182eb47b 3dxware_test.sln --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/3dxware_test.sln Tue Jul 09 04:46:51 2013 +0300 @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 10.00 +# Visual Studio 2008 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "3dxware_test", "3dxware_test.vcproj", "{60E6ACB4-4069-41CD-8195-062C66AC4C88}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {60E6ACB4-4069-41CD-8195-062C66AC4C88}.Debug|Win32.ActiveCfg = Debug|Win32 + {60E6ACB4-4069-41CD-8195-062C66AC4C88}.Debug|Win32.Build.0 = Debug|Win32 + {60E6ACB4-4069-41CD-8195-062C66AC4C88}.Release|Win32.ActiveCfg = Release|Win32 + {60E6ACB4-4069-41CD-8195-062C66AC4C88}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff -r 000000000000 -r 0219182eb47b 3dxware_test.vcproj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/3dxware_test.vcproj Tue Jul 09 04:46:51 2013 +0300 @@ -0,0 +1,247 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 000000000000 -r 0219182eb47b main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cc Tue Jul 09 04:46:51 2013 +0300 @@ -0,0 +1,149 @@ +#include +#include +#include +#include "si.h" +#include "siapp.h" + +HWND create_window(); +void destroy_window(HWND win); +bool connect_spnav(HWND win); +void close_spnav(); +LRESULT CALLBACK winmsg_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam); + +SiHdl si_dev; + +int main() +{ + HWND win = create_window(); + if(!win) { + return 1; + } + if(!connect_spnav(win)) { + return 1; + } + + MSG msg; + while(GetMessage(&msg, win, 0, 0)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + destroy_window(win); + return 0; +} + +HWND create_window() +{ + HINSTANCE hinst = GetModuleHandle(0); + + WNDCLASS wc; + memset(&wc, 0, sizeof wc); + wc.lpfnWndProc = winmsg_handler; + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.hCursor = LoadCursor(0, IDC_ARROW); + wc.hIcon = LoadIcon(0, IDI_APPLICATION); + wc.lpszClassName = "foobar_window"; + wc.style = CS_HREDRAW | CS_VREDRAW; + RegisterClass(&wc); + + HWND win = CreateWindow("foobar_window", "test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, + 100, 100, 0, 0, hinst, 0); + if(!win) { + return 0; + } + ShowWindow(win, 1); + UpdateWindow(win); + + return win; +} + +void destroy_window(HWND win) +{ + DestroyWindow(win); + UnregisterClass("foobar_window", GetModuleHandle(0)); +} + +bool connect_spnav(HWND win) +{ + SiOpenData sidata; + + if(SiInitialize() != 0) { + fprintf(stderr, "SiInitialize failed\n"); + return false; + } + SiOpenWinInit(&sidata, win); + + if(!(si_dev = SiOpen("spnavtest", SI_ANY_DEVICE, SI_NO_MASK, SI_EVENT, &sidata))) { + fprintf(stderr, "SiOpen failed\n"); + return false; + } + return true; +} + +void close_spnav() +{ + if(si_dev) { + SiClose(si_dev); + } + SiTerminate(); +} + +void handle_si_event(SiSpwEvent *ev) +{ + int bn; + + switch(ev->type) { + case SI_BUTTON_EVENT: + if((bn = SiButtonPressed(ev)) != SI_NO_BUTTON) { + printf("button %d pressed\n", bn); + } + if((bn = SiButtonReleased(ev)) != SI_NO_BUTTON) { + printf("button %d released\n", bn); + } + break; + + case SI_MOTION_EVENT: + printf("motion: "); + for(int i=0; i<6; i++) { + printf("%ld ", ev->u.spwData.mData[i]); + if(i == 2) { + printf("- "); + } + } + fputc('\n', stdout); + break; + + default: + break; + } +} + +LRESULT CALLBACK winmsg_handler(HWND win, unsigned int msg, WPARAM wparam, LPARAM lparam) +{ + SiSpwEvent siev; + SiGetEventData siev_data; + + SiGetEventWinInit(&siev_data, msg, wparam, lparam); + SpwRetVal val = SiGetEvent(si_dev, 0, &siev_data, &siev); + if(val == SI_SKIP_EVENT) { + return 0; + } else if(val == SI_IS_EVENT) { + handle_si_event(&siev); + return 0; + } + + switch(msg) { + case WM_CLOSE: + printf("WM_CLOSE\n"); + DestroyWindow(win); + break; + + case WM_DESTROY: + printf("WM_DESTROY\n"); + PostQuitMessage(0); + break; + + default: + return DefWindowProc(win, msg, wparam, lparam); + } + return 0; +} \ No newline at end of file