Add GUI control panel (first draft)
[mouse-tracker-for-cubism.git] / include / mouse_cursor_tracker.h
1 // -*- mode: c++ -*-
2
3 #ifndef __MOUSE_CURSOR_TRACKER_H__
4 #define __MOUSE_CURSOR_TRACKER_H__
5
6 /****
7 Copyright (c) 2020 Adrian I. Lam
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26 ****/
27
28 #include <string>
29 #include <thread>
30 #include <vector>
31 #include <utility>
32 #include <mutex>
33 #include <map>
34 #include <gtkmm/application.h>
35 #include <gtkmm/builder.h>
36 #include <gtkmm/scale.h>
37 #include <gtkmm/checkbutton.h>
38 #include <gtkmm/button.h>
39 #include <gtkmm/window.h>
40
41 extern "C"
42 {
43 #include <xdo.h>
44 #include <pulse/simple.h>
45 }
46
47 class MouseCursorTracker
48 {
49 public:
50 MouseCursorTracker(std::string cfgPath,
51 std::vector<std::pair<std::string, int> > motions = {},
52 std::vector<std::string> expressions = {});
53 ~MouseCursorTracker();
54
55 enum class MotionPriority
56 {
57 // See LAppDefine.cpp in Demo
58 none,
59 idle,
60 normal,
61 force
62 };
63
64 struct Params
65 {
66 std::map<std::string, double> live2d;
67
68 bool autoBlink;
69 bool autoBreath;
70 bool randomIdleMotion;
71 bool useLipSync;
72 double lipSyncParam;
73
74 MotionPriority motionPriority;
75 std::string motionGroup;
76 int motionNumber;
77
78 std::string expression;
79 };
80
81 Params getParams(void);
82
83 void stop(void);
84
85 void mainLoop(void);
86
87 private:
88 struct Coord
89 {
90 int x;
91 int y;
92 };
93
94 struct Config
95 {
96 int sleepMs;
97 bool autoBlink;
98 bool autoBreath;
99 bool randomIdleMotion;
100 bool useLipSync;
101 double lipSyncGain;
102 double lipSyncCutOff;
103 unsigned int audioBufSize;
104 double mouthForm;
105 int top;
106 int bottom;
107 int left;
108 int right;
109 int screen;
110 Coord origin;
111 } m_cfg;
112
113 std::map<std::string, double> m_overrideMap;
114
115 bool m_stop;
116
117 Coord m_curPos;
118
119 xdo_t *m_xdo;
120
121 std::thread m_getVolumeThread;
122 std::thread m_parseCommandThread;
123 std::thread m_guiThread;
124 void audioLoop(void);
125 void cliLoop(void);
126 void guiLoop(void);
127 void processCommand(std::string);
128 double m_currentVol;
129 pa_simple *m_pulse;
130
131 MotionPriority m_motionPriority;
132 std::string m_motionGroup;
133 int m_motionNumber;
134 std::mutex m_motionMutex;
135 std::string m_expression;
136
137 void populateDefaultConfig(void);
138 void parseConfig(std::string cfgPath);
139
140 Glib::RefPtr<Gtk::Application> m_gtkapp;
141 Glib::RefPtr<Gtk::Builder> m_builder;
142
143 std::vector<std::string> m_onClearResettingParams;
144
145 // GUI signal handlers
146 void onMotionStartButton(void);
147 void onExpressionStartButton(void);
148 void onParamUpdateButton(Gtk::Scale *scale, bool isInc);
149 void onParamValChanged(Glib::RefPtr<Gtk::Adjustment> adj, std::string paramName);
150 void onAutoToggle(Gtk::CheckButton *check, Gtk::Scale *scale,
151 Gtk::Button *buttonDec, Gtk::Button *buttonInc,
152 std::string paramName);
153 void onClearButton(Glib::RefPtr<Gtk::Button> button, std::string paramName, Glib::RefPtr<Gtk::Adjustment> adj);
154 void onExpanderChange(Gtk::Window *window);
155 };
156
157 #endif