Blender V2.61 - r43446

GHOST_NDOFManagerX11.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version. 
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s):
00019  *   Mike Erwin
00020  *
00021  * ***** END GPL LICENSE BLOCK *****
00022  */
00023 
00024 #ifdef WITH_INPUT_NDOF
00025 
00026 #include "GHOST_NDOFManagerX11.h"
00027 #include "GHOST_SystemX11.h"
00028 #include <spnav.h>
00029 #include <stdio.h>
00030 
00031 
00032 GHOST_NDOFManagerX11::GHOST_NDOFManagerX11(GHOST_System& sys)
00033     :
00034       GHOST_NDOFManager(sys),
00035       m_available(false)
00036 {
00037     setDeadZone(0.1f); /* how to calibrate on Linux? throw away slight motion! */
00038 
00039     if (spnav_open() != -1) {
00040         /* determine exactly which device (if any) is plugged in */
00041 
00042 #define MAX_LINE_LENGTH 100
00043 
00044         /* look for USB devices with Logitech's vendor ID */
00045         FILE* command_output = popen("lsusb -d 046d:","r");
00046         if (command_output) {
00047             char line[MAX_LINE_LENGTH] = {0};
00048             while (fgets(line, MAX_LINE_LENGTH, command_output)) {
00049                 unsigned short vendor_id = 0, product_id = 0;
00050                 if (sscanf(line, "Bus %*d Device %*d: ID %hx:%hx", &vendor_id, &product_id) == 2)
00051                     if (setDevice(vendor_id, product_id)) {
00052                         m_available = true;
00053                         break; /* stop looking once the first 3D mouse is found */
00054                     }
00055             }
00056             pclose(command_output);
00057         }
00058     }
00059     else {
00060         puts("ndof: spacenavd not found");
00061         /* This isn't a hard error, just means the user doesn't have a 3D mouse. */
00062     }
00063 }
00064 
00065 GHOST_NDOFManagerX11::~GHOST_NDOFManagerX11()
00066 {
00067     if (m_available)
00068         spnav_close();
00069 }
00070 
00071 bool GHOST_NDOFManagerX11::available()
00072 {
00073     return m_available;
00074 }
00075 
00076 bool GHOST_NDOFManagerX11::processEvents()
00077 {
00078     GHOST_TUns64 now = m_system.getMilliSeconds();
00079 
00080     bool anyProcessed = false;
00081     spnav_event e;
00082     while (spnav_poll_event(&e)) {
00083         switch (e.type) {
00084             case SPNAV_EVENT_MOTION:
00085             {
00086                 /* convert to blender view coords */
00087                 short t[3] = {e.motion.x, e.motion.y, -e.motion.z};
00088                 short r[3] = {-e.motion.rx, -e.motion.ry, e.motion.rz};
00089 
00090                 updateTranslation(t, now);
00091                 updateRotation(r, now);
00092                 break;
00093             }
00094             case SPNAV_EVENT_BUTTON:
00095                 updateButton(e.button.bnum, e.button.press, now);
00096                 break;
00097         }
00098         anyProcessed = true;
00099     }
00100     return anyProcessed;
00101 }
00102 
00103 #endif /* WITH_INPUT_NDOF */