Merge branch 'master' into nikitakit_master
[synfig.git] / synfig-studio / src / gtkmm / devicetracker.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file devicetracker.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **  Copyright (c) 2009 Gerco Ballintijn
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 // FIXME: The code here doesn't use the GTKmm layer but uses GTK+ directly
25 // since the GTKmm wrapper for Gdk::Device is incomplete. When the wrapper
26 // gets fixed, this code should be updated accoordingly.
27
28 /* === H E A D E R S ======================================================= */
29
30 #ifdef USING_PCH
31 #       include "pch.h"
32 #else
33 #ifdef HAVE_CONFIG_H
34 #       include <config.h>
35 #endif
36
37 #include "devicetracker.h"
38 #include <gdk/gdk.h>
39 #include <gtk/gtk.h>
40 #include <synfigapp/main.h>
41
42 #include "general.h"
43
44 #endif
45
46 /* === U S I N G =========================================================== */
47
48 using namespace std;
49 using namespace etl;
50 using namespace synfig;
51 using namespace synfigapp;
52 using namespace studio;
53
54 /* === M A C R O S ========================================================= */
55
56 /* === G L O B A L S ======================================================= */
57
58 /* === P R O C E D U R E S ================================================= */
59
60 /* === M E T H O D S ======================================================= */
61
62 DeviceTracker::DeviceTracker()
63 {
64         GList*  device_list;
65         GList*  iter;
66         device_list=gdk_devices_list();
67
68         for(iter=device_list;iter;iter=g_list_next(iter))
69         {
70                 GdkDevice* device=reinterpret_cast<GdkDevice*>(iter->data);
71
72                 synfigapp::InputDevice::Handle input_device;
73                 input_device=synfigapp::Main::add_input_device(device->name,synfigapp::InputDevice::Type(device->source));
74                 //Disable all extended devices by default. This tries to fix several
75                 // bugs reported in track and forums
76                 if(     input_device->get_type()==synfigapp::InputDevice::TYPE_MOUSE  ||
77                         input_device->get_type()==synfigapp::InputDevice::TYPE_PEN    ||
78                         input_device->get_type()==synfigapp::InputDevice::TYPE_ERASER ||
79                         input_device->get_type()==synfigapp::InputDevice::TYPE_CURSOR  ) {
80                         input_device->set_mode(synfigapp::InputDevice::MODE_DISABLED);
81                         //synfigapp::Main::select_input_device(input_device);
82                 }
83         }
84         // Once all devices are disabled be sure that the core pointer is the
85         // one selected. The user should decide later whether enable and save the
86         // rest of input devices found.
87         synfigapp::Main::select_input_device(gdk_device_get_core_pointer()->name);
88 }
89
90 DeviceTracker::~DeviceTracker()
91 {
92 }
93
94 void
95 DeviceTracker::save_preferences()
96 {
97         GList * device_list = gdk_devices_list();
98         for (GList * itr = device_list; itr; itr = g_list_next(itr))
99         {
100                 GdkDevice * gdk_device = reinterpret_cast<GdkDevice*>(itr->data);
101
102                 InputDevice::Handle synfig_device = synfigapp::Main::find_input_device(gdk_device->name);
103                 if (synfig_device == NULL)
104                         continue;
105
106                 synfig_device->set_mode(InputDevice::Mode(gdk_device->mode));
107                 if (gdk_device->num_axes > 0) {
108                         vector<synfigapp::InputDevice::AxisUse> axes;
109                         axes.resize(gdk_device->num_axes);
110                         for (int i = 0; i < gdk_device->num_axes; i++)
111                                 axes[i] = InputDevice::AxisUse(gdk_device->axes[i].use);
112                         synfig_device->set_axes(axes);
113                 }
114
115                 if (gdk_device->num_keys > 0) {
116                         vector<synfigapp::InputDevice::DeviceKey> keys;
117                         keys.resize(gdk_device->num_keys);
118                         for (int i = 0; i < gdk_device->num_keys; i++) {
119                                 keys[i].keyval = gdk_device->keys[i].keyval;
120                                 keys[i].modifiers = gdk_device->keys[i].modifiers;
121                         }
122                         synfig_device->set_keys(keys);
123                 }
124         }
125 }
126
127 void
128 DeviceTracker::load_preferences()
129 {
130         GList * device_list = gdk_devices_list();
131         for (GList * itr = device_list; itr; itr = g_list_next(itr))
132         {
133                 GdkDevice * gdk_device = reinterpret_cast<GdkDevice*>(itr->data);
134
135                 InputDevice::Handle synfig_device = synfigapp::Main::find_input_device(gdk_device->name);
136                 if (synfig_device == NULL)
137                         continue;
138
139                 gdk_device_set_mode(gdk_device, GdkInputMode(synfig_device->get_mode()));
140
141                 const std::vector<synfigapp::InputDevice::AxisUse> axes = synfig_device->get_axes();
142                 for (int axis = 0; axis < (int) axes.size(); axis++)
143                         gdk_device_set_axis_use(gdk_device, axis, GdkAxisUse(axes[axis]));
144
145                 const std::vector<synfigapp::InputDevice::DeviceKey> keys = synfig_device->get_keys();
146                 for (int key = 0; key < (int) keys.size(); key++)
147                         gdk_device_set_key(gdk_device, key, keys[key].keyval,
148                                         GdkModifierType(keys[key].modifiers));
149         }
150 }