Blame synfig-studio/src/gui/devicetracker.cpp

Carlos Lopez a09598
/* === S Y N F I G ========================================================= */
Carlos Lopez a09598
/*!	\file devicetracker.cpp
Carlos Lopez a09598
**	\brief Template File
Carlos Lopez a09598
**
Carlos Lopez a09598
**	$Id$
Carlos Lopez a09598
**
Carlos Lopez a09598
**	\legal
Carlos Lopez a09598
**	Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
Carlos Lopez a09598
**  Copyright (c) 2009 Gerco Ballintijn
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is free software; you can redistribute it and/or
Carlos Lopez a09598
**	modify it under the terms of the GNU General Public License as
Carlos Lopez a09598
**	published by the Free Software Foundation; either version 2 of
Carlos Lopez a09598
**	the License, or (at your option) any later version.
Carlos Lopez a09598
**
Carlos Lopez a09598
**	This package is distributed in the hope that it will be useful,
Carlos Lopez a09598
**	but WITHOUT ANY WARRANTY; without even the implied warranty of
Carlos Lopez a09598
**	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Carlos Lopez a09598
**	General Public License for more details.
Carlos Lopez a09598
**	\endlegal
Carlos Lopez a09598
*/
Carlos Lopez a09598
/* ========================================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
// FIXME: The code here doesn't use the GTKmm layer but uses GTK+ directly
Carlos Lopez a09598
// since the GTKmm wrapper for Gdk::Device is incomplete. When the wrapper
Carlos Lopez a09598
// gets fixed, this code should be updated accoordingly.
Carlos Lopez a09598
Carlos Lopez a09598
/* === H E A D E R S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
#ifdef USING_PCH
Carlos Lopez a09598
#	include "pch.h"
Carlos Lopez a09598
#else
Carlos Lopez a09598
#ifdef HAVE_CONFIG_H
Carlos Lopez a09598
#	include <config.h></config.h>
Carlos Lopez a09598
#endif
Carlos Lopez a09598
17223e
#include <gdkmm device.h=""></gdkmm>
17223e
#include <gdkmm display.h=""></gdkmm>
17223e
#include <gdkmm displaymanager.h=""></gdkmm>
17223e
#include <gdkmm seat.h=""></gdkmm>
17223e
bw 94d8a6
#include <synfig general.h=""></synfig>
bw 94d8a6
Carlos Lopez a09598
#include <synfigapp main.h=""></synfigapp>
Carlos Lopez a09598
17223e
#include "devicetracker.h"
17223e
abdbf2
#include <gui localization.h=""></gui>
Carlos Lopez a09598
Carlos Lopez a09598
#endif
Carlos Lopez a09598
Carlos Lopez a09598
/* === U S I N G =========================================================== */
Carlos Lopez a09598
Carlos Lopez a09598
using namespace synfigapp;
Carlos Lopez a09598
using namespace studio;
Carlos Lopez a09598
Carlos Lopez a09598
/* === M A C R O S ========================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === G L O B A L S ======================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === P R O C E D U R E S ================================================= */
Carlos Lopez a09598
Carlos Lopez a09598
/* === M E T H O D S ======================================================= */
Carlos Lopez a09598
17223e
void
17223e
DeviceTracker::list_devices(DeviceList &out_devices)
17223e
{
17223e
	out_devices.clear();
17223e
	Glib::RefPtr<gdk::seat> seat = Gdk::DisplayManager::get()->get_default_display()->get_default_seat();</gdk::seat>
17223e
	if (seat->get_keyboard())
17223e
		out_devices.push_back(seat->get_keyboard());
17223e
	if (seat->get_pointer())
17223e
		out_devices.push_back(seat->get_pointer());
7cca1a
	DeviceList slaves = seat->get_slaves(Gdk::SEAT_CAPABILITY_ALL);
17223e
	out_devices.reserve(out_devices.size() + slaves.size());
17223e
	out_devices.insert(out_devices.end(), slaves.begin(), slaves.end());
17223e
}
17223e
Carlos Lopez a09598
DeviceTracker::DeviceTracker()
Carlos Lopez a09598
{
17223e
	DeviceList devices;
17223e
	list_devices(devices);
17223e
	for(DeviceList::const_iterator i = devices.begin(); i != devices.end(); ++i) {
17223e
		const Glib::RefPtr<gdk::device> &device = *i;</gdk::device>
17223e
17223e
		bool unknown_type = false;
17223e
		InputDevice::Type type = InputDevice::TYPE_MOUSE;
17223e
		switch(device->get_source()) {
17223e
		case Gdk::SOURCE_MOUSE:  type = InputDevice::TYPE_MOUSE;  break;
17223e
		case Gdk::SOURCE_PEN:    type = InputDevice::TYPE_PEN;    break;
17223e
		case Gdk::SOURCE_ERASER: type = InputDevice::TYPE_ERASER; break;
17223e
		case Gdk::SOURCE_CURSOR: type = InputDevice::TYPE_CURSOR; break;
17223e
		default: unknown_type = true; break;
Carlos Lopez a09598
		}
b8e147
17223e
		InputDevice::Handle input_device;
17223e
		input_device = synfigapp::Main::add_input_device(device->get_name(), type);
17223e
17223e
		// Disable all extended devices by default.
17223e
		// This tries to fix several bugs reported in track and forums
17223e
		if (!unknown_type) {
17223e
			input_device->set_mode(InputDevice::MODE_DISABLED);
17223e
			//synfigapp::Main::select_input_device(input_device);
17223e
		}
Carlos Lopez a09598
	}
b8e147
Carlos Lopez 66d124
	// Once all devices are disabled be sure that the core pointer is the
Carlos Lopez 66d124
	// one selected. The user should decide later whether enable and save the
Carlos Lopez 66d124
	// rest of input devices found.
17223e
	Glib::RefPtr<gdk::seat> seat = Gdk::DisplayManager::get()->get_default_display()->get_default_seat();</gdk::seat>
17223e
	if (seat->get_pointer())
17223e
		synfigapp::Main::select_input_device(seat->get_pointer()->get_name());
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
DeviceTracker::~DeviceTracker()
17223e
	{ }
Carlos Lopez a09598
Carlos Lopez a09598
void
Carlos Lopez a09598
DeviceTracker::save_preferences()
Carlos Lopez a09598
{
17223e
	DeviceList devices;
17223e
	list_devices(devices);
17223e
	for(DeviceList::const_iterator i = devices.begin(); i != devices.end(); ++i) {
17223e
		const Glib::RefPtr<gdk::device> &device = *i;</gdk::device>
17223e
17223e
		InputDevice::Handle synfig_device = synfigapp::Main::find_input_device(device->get_name());
17223e
		if (!synfig_device)
17223e
			continue;
17223e
17223e
		InputDevice::Mode mode = InputDevice::MODE_DISABLED;
17223e
		switch(device->get_mode()) {
17223e
		case Gdk::MODE_SCREEN: mode = InputDevice::MODE_SCREEN;  break;
17223e
		case Gdk::MODE_WINDOW: mode = InputDevice::MODE_WINDOW;  break;
17223e
		default: break;
17223e
		}
17223e
		synfig_device->set_mode(mode);
17223e
17223e
		int n_axes = device->get_source() == Gdk::SOURCE_KEYBOARD ? 0 : device->get_axes();
17223e
		if (n_axes > 0) {
17223e
			std::vector<inputdevice::axisuse> axes(n_axes);</inputdevice::axisuse>
17223e
			for(int j = 0; j < n_axes; ++j)
17223e
				switch(device->get_axis_use(j)) {
17223e
				case Gdk::AXIS_X:        axes[j] = InputDevice::AXIS_X; break;
17223e
				case Gdk::AXIS_Y:        axes[j] = InputDevice::AXIS_Y; break;
17223e
				case Gdk::AXIS_PRESSURE: axes[j] = InputDevice::AXIS_PRESSURE; break;
17223e
				case Gdk::AXIS_XTILT:    axes[j] = InputDevice::AXIS_XTILT; break;
17223e
				case Gdk::AXIS_YTILT:    axes[j] = InputDevice::AXIS_YTILT; break;
17223e
				case Gdk::AXIS_WHEEL:    axes[j] = InputDevice::AXIS_WHEEL; break;
17223e
				case Gdk::AXIS_LAST:     axes[j] = InputDevice::AXIS_LAST; break;
17223e
				default:                 axes[j] = InputDevice::AXIS_IGNORE; break;
b8e147
				}
17223e
			synfig_device->set_axes(axes);
Carlos Lopez a09598
		}
b8e147
17223e
		int n_keys = device->get_n_keys();
17223e
		if (n_keys > 0) {
17223e
			std::vector<inputdevice::devicekey> keys(n_keys);</inputdevice::devicekey>
17223e
			for(int j = 0; j < n_keys; ++j) {
17223e
				guint gdk_keyval = 0;
17223e
				Gdk::ModifierType gdk_modifiers = Gdk::ModifierType();
17223e
				device->get_key(j, gdk_keyval, gdk_modifiers);
17223e
				keys[j].keyval = gdk_keyval;
17223e
				keys[j].modifiers = gdk_modifiers;
17223e
			}
17223e
			synfig_device->set_keys(keys);
17223e
		}
Carlos Lopez a09598
	}
Carlos Lopez a09598
}
Carlos Lopez a09598
Carlos Lopez a09598
void
Carlos Lopez a09598
DeviceTracker::load_preferences()
Carlos Lopez a09598
{
17223e
	DeviceList devices;
17223e
	list_devices(devices);
17223e
	for(DeviceList::const_iterator i = devices.begin(); i != devices.end(); ++i) {
17223e
		const Glib::RefPtr<gdk::device> &device = *i;</gdk::device>
17223e
17223e
		InputDevice::Handle synfig_device = synfigapp::Main::find_input_device(device->get_name());
17223e
		if (!synfig_device)
17223e
			continue;
17223e
17223e
		Gdk::InputMode gdk_mode = Gdk::MODE_DISABLED;
17223e
		switch(synfig_device->get_mode()) {
17223e
		case InputDevice::MODE_SCREEN: gdk_mode = Gdk::MODE_SCREEN;  break;
17223e
		case InputDevice::MODE_WINDOW: gdk_mode = Gdk::MODE_WINDOW;  break;
17223e
		default: break;
b8e147
		}
17223e
		device->set_mode(gdk_mode);
17223e
17223e
		const std::vector<inputdevice::axisuse> axes = synfig_device->get_axes();</inputdevice::axisuse>
17223e
		for(int axis = 0; axis < (int)axes.size(); ++axis)
17223e
			switch(axes[axis]) {
17223e
			case InputDevice::AXIS_X:        device->set_axis_use(axis, Gdk::AXIS_X);        break;
17223e
			case InputDevice::AXIS_Y:        device->set_axis_use(axis, Gdk::AXIS_Y);        break;
17223e
			case InputDevice::AXIS_PRESSURE: device->set_axis_use(axis, Gdk::AXIS_PRESSURE); break;
17223e
			case InputDevice::AXIS_XTILT:    device->set_axis_use(axis, Gdk::AXIS_XTILT);    break;
17223e
			case InputDevice::AXIS_YTILT:    device->set_axis_use(axis, Gdk::AXIS_YTILT);    break;
17223e
			case InputDevice::AXIS_WHEEL:    device->set_axis_use(axis, Gdk::AXIS_WHEEL);    break;
17223e
			case InputDevice::AXIS_LAST:     device->set_axis_use(axis, Gdk::AXIS_LAST);     break;
17223e
			default:                         device->set_axis_use(axis, Gdk::AXIS_IGNORE);   break;
17223e
			}
Carlos Lopez a09598
17223e
		const std::vector<inputdevice::devicekey> keys = synfig_device->get_keys();</inputdevice::devicekey>
17223e
		for (int key = 0; key < (int) keys.size(); key++)
17223e
			device->set_key(key, keys[key].keyval, Gdk::ModifierType(keys[key].modifiers));
Carlos Lopez a09598
	}
Carlos Lopez a09598
}