Remove ancient trunk folder from svn repository
[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                 if(input_device->get_type()==synfigapp::InputDevice::TYPE_MOUSE) {
75                         input_device->set_mode(synfigapp::InputDevice::MODE_SCREEN);
76                         synfigapp::Main::select_input_device(input_device);
77                 }
78         }
79 }
80
81 DeviceTracker::~DeviceTracker()
82 {
83 }
84
85 void
86 DeviceTracker::save_preferences()
87 {
88         GList * device_list = gdk_devices_list();
89         for (GList * itr = device_list; itr; itr = g_list_next(itr))
90         {
91                 GdkDevice * gdk_device = reinterpret_cast<GdkDevice*>(itr->data);
92
93                 InputDevice::Handle synfig_device = synfigapp::Main::find_input_device(gdk_device->name);
94                 if (synfig_device == NULL)
95                         continue;
96
97                 synfig_device->set_mode(InputDevice::Mode(gdk_device->mode));
98                 if (gdk_device->num_axes > 0) {
99                         vector<synfigapp::InputDevice::AxisUse> axes;
100                         axes.resize(gdk_device->num_axes);
101                         for (int i = 0; i < gdk_device->num_axes; i++)
102                                 axes[i] = InputDevice::AxisUse(gdk_device->axes[i].use);
103                         synfig_device->set_axes(axes);
104                 }
105
106                 if (gdk_device->num_keys > 0) {
107                         vector<synfigapp::InputDevice::DeviceKey> keys;
108                         keys.resize(gdk_device->num_keys);
109                         for (int i = 0; i < gdk_device->num_keys; i++) {
110                                 keys[i].keyval = gdk_device->keys[i].keyval;
111                                 keys[i].modifiers = gdk_device->keys[i].modifiers;
112                         }
113                         synfig_device->set_keys(keys);
114                 }
115         }
116 }
117
118 void
119 DeviceTracker::load_preferences()
120 {
121         GList * device_list = gdk_devices_list();
122         for (GList * itr = device_list; itr; itr = g_list_next(itr))
123         {
124                 GdkDevice * gdk_device = reinterpret_cast<GdkDevice*>(itr->data);
125
126                 InputDevice::Handle synfig_device = synfigapp::Main::find_input_device(gdk_device->name);
127                 if (synfig_device == NULL)
128                         continue;
129
130                 gdk_device_set_mode(gdk_device, GdkInputMode(synfig_device->get_mode()));
131
132                 const std::vector<synfigapp::InputDevice::AxisUse> axes = synfig_device->get_axes();
133                 for (int axis = 0; axis < (int) axes.size(); axis++)
134                         gdk_device_set_axis_use(gdk_device, axis, GdkAxisUse(axes[axis]));
135
136                 const std::vector<synfigapp::InputDevice::DeviceKey> keys = synfig_device->get_keys();
137                 for (int key = 0; key < (int) keys.size(); key++)
138                         gdk_device_set_key(gdk_device, key, keys[key].keyval,
139                                         GdkModifierType(keys[key].modifiers));
140         }
141 }