[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Pingus-CVS] r3051 - trunk/pingus/src/input2
From: |
grumbel at BerliOS |
Subject: |
[Pingus-CVS] r3051 - trunk/pingus/src/input2 |
Date: |
Sat, 1 Sep 2007 23:45:26 +0200 |
Author: grumbel
Date: 2007-09-01 23:45:25 +0200 (Sat, 01 Sep 2007)
New Revision: 3051
Added:
trunk/pingus/src/input2/usbmouse_driver.cpp
Modified:
trunk/pingus/src/input2/SConstruct
trunk/pingus/src/input2/core_driver.cpp
trunk/pingus/src/input2/core_driver.hpp
trunk/pingus/src/input2/driver.hpp
trunk/pingus/src/input2/manager.cpp
trunk/pingus/src/input2/sdl_driver.hpp
trunk/pingus/src/input2/usbmouse_driver.hpp
Log:
- added USBMouse support
Modified: trunk/pingus/src/input2/SConstruct
===================================================================
--- trunk/pingus/src/input2/SConstruct 2007-09-01 19:48:17 UTC (rev 3050)
+++ trunk/pingus/src/input2/SConstruct 2007-09-01 21:45:25 UTC (rev 3051)
@@ -11,6 +11,7 @@
'controller_description.cpp',
'controller.cpp',
'core_driver.cpp',
+ 'usbmouse_driver.cpp',
'sdl_driver.cpp',
'manager.cpp',
'../file_reader.cpp',
Modified: trunk/pingus/src/input2/core_driver.cpp
===================================================================
--- trunk/pingus/src/input2/core_driver.cpp 2007-09-01 19:48:17 UTC (rev
3050)
+++ trunk/pingus/src/input2/core_driver.cpp 2007-09-01 21:45:25 UTC (rev
3051)
@@ -34,13 +34,14 @@
private:
Axis* x_axis;
Axis* y_axis;
+ Button* speed_button;
float speed;
public:
AxisPointer(Control* parent)
: Pointer(parent),
- x_axis(0), y_axis(0),
- speed(800.0f)
+ x_axis(0), y_axis(0), speed_button(0),
+ speed(200.0f)
{
}
@@ -48,10 +49,11 @@
{
}
- void set_axis(Axis* x, Axis* y)
+ void setup(Axis* x, Axis* y, Button* s = 0)
{
x_axis = x;
y_axis = y;
+ speed_button = s;
}
void update(Control* )
@@ -61,10 +63,20 @@
void update(float delta)
{
+ x_axis->update(delta);
+ y_axis->update(delta);
+ if (speed_button) speed_button->update(delta);
+
Vector2f new_pos = pos;
+ float c_speed = speed;
+
+ if (speed_button && speed_button->get_state() == BUTTON_PRESSED)
+ {
+ c_speed *= 5.0f;
+ }
- new_pos.x += x_axis->get_pos() * speed * delta;
- new_pos.y += y_axis->get_pos() * speed * delta;
+ new_pos.x += x_axis->get_pos() * c_speed * delta;
+ new_pos.y += y_axis->get_pos() * c_speed * delta;
// FIXME: Shouldn't be hardcored, but shouldn't depend on Display
// either
@@ -123,9 +135,16 @@
Axis* x_axis = manager->create_axis(x_reader.get_sections().front(),
axis);
Axis* y_axis = manager->create_axis(y_reader.get_sections().front(),
axis);
+ Button* button = 0;
+ FileReader button_reader;
+ if (reader.read_section("button", button_reader))
+ {
+ button =
manager->create_button(button_reader.get_sections().front(), axis);
+ }
+
if (x_axis && y_axis)
{
- axis->set_axis(x_axis, y_axis);
+ axis->setup(x_axis, y_axis, button);
return axis;
}
else
Modified: trunk/pingus/src/input2/core_driver.hpp
===================================================================
--- trunk/pingus/src/input2/core_driver.hpp 2007-09-01 19:48:17 UTC (rev
3050)
+++ trunk/pingus/src/input2/core_driver.hpp 2007-09-01 21:45:25 UTC (rev
3051)
@@ -42,7 +42,7 @@
CoreDriver(Manager* manager_) : manager(manager_) {}
virtual ~CoreDriver() {}
- std::string get_name() { return "core"; }
+ std::string get_name() const { return "core"; }
void update(float delta) {}
Button* create_button (const FileReader& reader, Control* parent);
Modified: trunk/pingus/src/input2/driver.hpp
===================================================================
--- trunk/pingus/src/input2/driver.hpp 2007-09-01 19:48:17 UTC (rev 3050)
+++ trunk/pingus/src/input2/driver.hpp 2007-09-01 21:45:25 UTC (rev 3051)
@@ -31,7 +31,7 @@
Driver() {}
virtual ~Driver() {}
- virtual std::string get_name() =0;
+ virtual std::string get_name() const =0;
virtual void update(float delta) =0;
virtual Button* create_button (const FileReader& reader, Control* parent)
=0;
Modified: trunk/pingus/src/input2/manager.cpp
===================================================================
--- trunk/pingus/src/input2/manager.cpp 2007-09-01 19:48:17 UTC (rev 3050)
+++ trunk/pingus/src/input2/manager.cpp 2007-09-01 21:45:25 UTC (rev 3051)
@@ -24,6 +24,7 @@
#include "path_manager.hpp"
#include "sdl_driver.hpp"
#include "core_driver.hpp"
+#include "usbmouse_driver.hpp"
#include "manager.hpp"
namespace Input {
@@ -109,7 +110,7 @@
if (pointer)
ctrl_pointer->add_pointer(pointer);
else
- std::cout << "Manager: pointer: Couldn't create pointer"
<< j->get_name() << std::endl;
+ std::cout << "Manager: pointer: Couldn't create pointer "
<< j->get_name() << std::endl;
}
}
@@ -124,7 +125,7 @@
if (scroller)
ctrl_scroller->add_scroller(scroller);
else
- std::cout << "Manager: scroller: Couldn't create scroller"
<< j->get_name() << std::endl;
+ std::cout << "Manager: scroller: Couldn't create scroller
" << j->get_name() << std::endl;
}
}
@@ -155,7 +156,7 @@
else
std::cout << "Manager: axis: Couldn't create axis" <<
j->get_name() << std::endl;
}
- }
+ }
else
{
PingusError::raise(std::string("Manager: Unkown Element in
Controller Config: ")
@@ -204,6 +205,8 @@
driver = new SDLDriver();
} else if (name == "core") {
driver = new CoreDriver(this);
+ } else if (name == "usbmouse") {
+ driver = new USBMouseDriver();
} else {
std::cout << "Manager: Unknown driver: " << name << std::endl;
return 0;
Modified: trunk/pingus/src/input2/sdl_driver.hpp
===================================================================
--- trunk/pingus/src/input2/sdl_driver.hpp 2007-09-01 19:48:17 UTC (rev
3050)
+++ trunk/pingus/src/input2/sdl_driver.hpp 2007-09-01 21:45:25 UTC (rev
3051)
@@ -97,7 +97,7 @@
Pointer* create_pointer (const FileReader& reader, Control* parent);
void update(float delta);
- std::string get_name() { return "sdl"; }
+ std::string get_name() const { return "sdl"; }
};
} // namespace Input
Added: trunk/pingus/src/input2/usbmouse_driver.cpp
===================================================================
--- trunk/pingus/src/input2/usbmouse_driver.cpp 2007-09-01 19:48:17 UTC (rev
3050)
+++ trunk/pingus/src/input2/usbmouse_driver.cpp 2007-09-01 21:45:25 UTC (rev
3051)
@@ -0,0 +1,249 @@
+/* $Id$
+** __ __ __ ___ __ __ __ __
+** / \ / \__| ____ __| _/_______/ |_|__| | | | ____
+** \ \/\/ / |/ \ / __ |/ ___/\ __\ | | | | _/ __ \
+** \ /| | | \/ /_/ |\___ \ | | | | |_| |_\ ___/
+** \__/\ / |__|___| /\____ /____ > |__| |__|____/____/\___ >
+** \/ \/ \/ \/ \/
+** Copyright (C) 2007 Ingo Ruhnke <address@hidden>
+**
+** This program is free software; you can redistribute it and/or
+** modify it under the terms of the GNU General Public License
+** as published by the Free Software Foundation; either version 2
+** of the License, or (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program; if not, write to the Free Software
+** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+** 02111-1307, USA.
+*/
+
+#include <stdexcept>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <iostream>
+#include <errno.h>
+
+#include "math/vector2i.hpp"
+#include "usbmouse_driver.hpp"
+
+namespace Input {
+
+class USBMouse
+{
+private:
+ int fd;
+ Vector2i mouse_pos;
+
+ std::string device;
+ std::vector<bool> buttons;
+ std::vector<Pointer*> pointer;
+
+public:
+ USBMouse(const std::string& device_)
+ : device(device_),
+ buttons(5)
+ {
+ fd = open(device.c_str (), O_RDWR | O_NONBLOCK);
+
+ if (fd == -1)
+ {
+ throw std::runtime_error(strerror(errno));
+ }
+
+ {
+ // Microsoft init sequence for Explorer mouse (wheel + 5 buttons)
+ static unsigned char data[] = { 0xF3, 0xC8,
+ 0xF3, 0xC8,
+ 0xF3, 0x50 };
+ write(fd, data, sizeof(data));
+ }
+
+ char data[4];
+ read(fd, data, sizeof (data));
+ read(fd, data, sizeof (data));
+ read(fd, data, sizeof (data));
+ }
+
+ ~USBMouse()
+ {
+ close(fd);
+ }
+
+ void add_listener(Pointer* p)
+ {
+ pointer.push_back(p);
+ }
+
+ std::string get_device() const {
+ return device;
+ }
+
+ void update(float delta)
+ {
+ unsigned char data[4];
+ while(read(fd, data, sizeof (data)) > 0)
+ {
+ // Mouse Move:
+ int delta_x = (data[0] & 0x10) ? data[1]-256 : data[1];
+ int delta_y = (data[0] & 0x20) ? data[2]-256 : data[2];
+
+ if (delta_x != 0 || delta_y != 0)
+ {
+ mouse_pos.x += delta_x;
+ mouse_pos.y -= delta_y; // y-axis is reversed on-screen
+
+ if (mouse_pos.x < 0)
+ mouse_pos.x = 0;
+ else if (mouse_pos.x > 800) // FIXME: Shouldn't hardcore 800x600
resolution
+ mouse_pos.x = 800 - 1;
+
+ if (mouse_pos.y < 0)
+ mouse_pos.y = 0;
+ else if (mouse_pos.y > 600)
+ mouse_pos.y = 600 - 1;
+
+ for(std::vector<Pointer*>::iterator i = pointer.begin(); i !=
pointer.end(); ++i)
+ (*i)->set_pos(mouse_pos);
+
+ // send_ball_move(delta_x, delta_y);
+ // send_pointer_move(mouse_pos);
+ }
+
+ // Scrollwheel move
+ int delta_z = (data[3] & 0x08) ? (data[3] & 0x0F)-16 : (data[3] &
0x0F);
+
+ if (delta_z > 0)
+ {
+ while (delta_z != 0)
+ {
+ --delta_z;
+ //send_key_event(CL_MOUSE_WHEEL_DOWN, true);
+ //send_key_event(CL_MOUSE_WHEEL_DOWN, false);
+ }
+ }
+ else if (delta_z < 0)
+ {
+ while (delta_z != 0)
+ {
+ ++delta_z;
+ //send_key_event(CL_MOUSE_WHEEL_UP, true);
+ //send_key_event(CL_MOUSE_WHEEL_UP, false);
+ }
+ }
+
+ // Button event
+ std::vector<bool> new_state(5);
+
+ new_state[0] = ((data[0] & 1)>0);
+ new_state[1] = ((data[0] & 2)>0);
+ new_state[2] = ((data[0] & 4)>0);
+ new_state[3] = ((data[3] & 16)>0);
+ new_state[4] = ((data[3] & 32)>0);
+
+ for (int i = 0; i < 5; ++i)
+ {
+ if (new_state[i] != buttons[i])
+ {
+ buttons[i] = new_state[i];
+ }
+ }
+
+ buttons = new_state;
+ }
+ }
+};
+
+USBMouseDriver::USBMouseDriver()
+{
+}
+
+USBMouseDriver::~USBMouseDriver()
+{
+ for(USBMice::iterator i = usbmice.begin(); i != usbmice.end(); ++i)
+ delete *i;
+}
+
+void
+USBMouseDriver::update(float delta)
+{
+ for(USBMice::iterator i = usbmice.begin(); i != usbmice.end(); ++i)
+ (*i)->update(delta);
+}
+
+Button*
+USBMouseDriver::create_button(const FileReader& reader, Control* parent)
+{
+ if (reader.get_name() == "usbmouse:button")
+ {
+ return 0;
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+Pointer*
+USBMouseDriver::create_pointer(const FileReader& reader, Control* parent)
+{
+ if (reader.get_name() == "usbmouse:pointer")
+ {
+ std::string device;
+ if (reader.read_string("device", device))
+ {
+ USBMouse* mouse = get_mouse(device);
+ if (mouse)
+ {
+ Pointer* pointer = new Pointer(parent);
+ mouse->add_listener(pointer);
+ return pointer;
+ }
+ else
+ {
+ return 0;
+ }
+ }
+ else
+ {
+ std::cout << "USBMouseDriver: 'device' entry is missing" <<
std::endl;
+ return 0;
+ }
+ }
+ else
+ {
+ return 0;
+ }
+}
+
+USBMouse*
+USBMouseDriver::get_mouse(const std::string& device)
+{
+ for(USBMice::iterator i = usbmice.begin(); i != usbmice.end(); ++i)
+ {
+ if ((*i)->get_device() == device)
+ return *i;
+ }
+
+ try
+ {
+ USBMouse* usbmouse = new USBMouse(device);
+ usbmice.push_back(usbmouse);
+ return usbmouse;
+ }
+ catch (std::exception& err)
+ {
+ std::cout << "USBMouseDriver: " << err.what() << std::endl;
+ return 0;
+ }
+}
+
+} // namespace Input
+
+/* EOF */
Property changes on: trunk/pingus/src/input2/usbmouse_driver.cpp
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: trunk/pingus/src/input2/usbmouse_driver.hpp
===================================================================
--- trunk/pingus/src/input2/usbmouse_driver.hpp 2007-09-01 19:48:17 UTC (rev
3050)
+++ trunk/pingus/src/input2/usbmouse_driver.hpp 2007-09-01 21:45:25 UTC (rev
3051)
@@ -22,15 +22,40 @@
#ifndef HEADER_USBMOUSE_DRIVER_HPP
#define HEADER_USBMOUSE_DRIVER_HPP
-/** */
-class USBMouseDriver
+#include <vector>
+#include "driver.hpp"
+
+namespace Input {
+
+class USBMouse;
+
+/**
+ */
+class USBMouseDriver : public Driver
{
+private:
+ typedef std::vector<USBMouse*> USBMice;
+ USBMice usbmice;
+
public:
USBMouseDriver();
+ ~USBMouseDriver();
+ std::string get_name() const { return "usbmouse"; }
+
void update(float delta);
+
+ Button* create_button (const FileReader& reader, Control* parent);
+ Axis* create_axis (const FileReader& reader, Control* parent) {
return 0; }
+ Scroller* create_scroller(const FileReader& reader, Control* parent) {
return 0; }
+ Pointer* create_pointer (const FileReader& reader, Control* parent);
+
+private:
+ USBMouse* get_mouse(const std::string& device);
};
+} // namespace Input
+
#endif
/* EOF */
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Pingus-CVS] r3051 - trunk/pingus/src/input2,
grumbel at BerliOS <=