summaryrefslogtreecommitdiff
path: root/src/utilities/trigger.vala
blob: aeed3fb50dc12a8145254d4eee43492d92a1ee22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011-2016 by Simon Schneegans
//
// 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 3 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, see <http://www.gnu.org/licenses/>.
/////////////////////////////////////////////////////////////////////////

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////
/// This class represents a hotkey, used to open pies. It supports any
/// combination of modifier keys with keyboard and mouse buttons.
/////////////////////////////////////////////////////////////////////////

public class Trigger : GLib.Object {

    /////////////////////////////////////////////////////////////////////
    /// Returns a human-readable version of this Trigger.
    /////////////////////////////////////////////////////////////////////

    public string label { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// Returns a human-readable version of this Trigger. Small
    /// identifiers for turbo mode and delayed mode are added.
    /////////////////////////////////////////////////////////////////////

    public string label_with_specials { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The Trigger string. Like [delayed]<Control>button3
    /////////////////////////////////////////////////////////////////////

    public string name { get; private set; default=""; }

    /////////////////////////////////////////////////////////////////////
    /// The key code of the hotkey or the button number of the mouse.
    /////////////////////////////////////////////////////////////////////

    public int key_code { get; private set; default=0; }

    /////////////////////////////////////////////////////////////////////
    /// The keysym of the hotkey or the button number of the mouse.
    /////////////////////////////////////////////////////////////////////

    public uint key_sym { get; private set; default=0; }

    /////////////////////////////////////////////////////////////////////
    /// Modifier keys pressed for this hotkey.
    /////////////////////////////////////////////////////////////////////

    public Gdk.ModifierType modifiers { get; private set; default=0; }

    /////////////////////////////////////////////////////////////////////
    /// True if this hotkey involves the mouse.
    /////////////////////////////////////////////////////////////////////

    public bool with_mouse { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// True if the pie closes when the trigger hotkey is released.
    /////////////////////////////////////////////////////////////////////

    public bool turbo { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// True if the trigger should wait a short delay before being
    /// triggered.
    /////////////////////////////////////////////////////////////////////

    public bool delayed { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// True if the pie opens in the middle of the screen.
    /////////////////////////////////////////////////////////////////////

    public bool centered { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// True if the mouse pointer is warped to the pie's center.
    /////////////////////////////////////////////////////////////////////

    public bool warp { get; private set; default=false; }

    /////////////////////////////////////////////////////////////////////
    /// Returns the current selected "radio-button" shape: 0= automatic
    /// 5= full pie; 1,3,7,8= quarters; 2,4,6,8=halves
    /// 1 | 4 | 7
    /// 2 | 5 | 8
    /// 3 | 6 | 9
    /////////////////////////////////////////////////////////////////////

    public int shape { get; private set; default=5; }

    /////////////////////////////////////////////////////////////////////
    /// C'tor, creates a new, "unbound" Trigger.
    /////////////////////////////////////////////////////////////////////

    public Trigger() {
        this.set_unbound();
    }

    /////////////////////////////////////////////////////////////////////
    /// C'tor, creates a new Trigger from a given Trigger string. This is
    /// in this format: "[option(s)]<modifier(s)>button" where
    /// "<modifier>" is something like "<Alt>" or "<Control>", "button"
    /// something like "s", "F4" or "button0" and "[option]" is either
    /// "[turbo]", "[centered]", "[warp]", "["delayed"]" or "["shape#"]"
    /////////////////////////////////////////////////////////////////////

    public Trigger.from_string(string trigger) {
        this.parse_string(trigger);
    }

    /////////////////////////////////////////////////////////////////////
    /// C'tor, creates a new Trigger from the key values.
    /////////////////////////////////////////////////////////////////////

    public Trigger.from_values(uint key_sym, Gdk.ModifierType modifiers,
                               bool with_mouse, bool turbo, bool delayed,
                               bool centered, bool warp, int shape ) {

        string trigger = (turbo ? "[turbo]" : "")
                       + (delayed ? "[delayed]" : "")
                       + (centered ? "[centered]" : "")
                       + (warp ? "[warp]" : "")
                       + (shape!=5 ? "[shape%d]".printf(shape) : "");

        if (with_mouse) {
            trigger += Gtk.accelerator_name(0, modifiers) + "button%u".printf(key_sym);
        } else {
            trigger += Gtk.accelerator_name(key_sym, modifiers);
        }

        this.parse_string(trigger);
    }

    /////////////////////////////////////////////////////////////////////
    /// Parses a Trigger string. This is
    /// in this format: "[option(s)]<modifier(s)>button" where
    /// "<modifier>" is something like "<Alt>" or "<Control>", "button"
    /// something like "s", "F4" or "button0" and "[option]" is either
    /// "[turbo]", "[centered]", "[warp]", "["delayed"]" or "["shape#"]"
    /////////////////////////////////////////////////////////////////////

    public void parse_string(string trigger) {
        if (this.is_valid(trigger)) {
            // copy string
            string check_string = trigger;

            this.name = check_string;

            this.turbo = check_string.contains("[turbo]");
            this.delayed = check_string.contains("[delayed]");
            this.centered = check_string.contains("[centered]");
            this.warp = check_string.contains("[warp]");

            this.shape= parse_shape( check_string );

            // remove optional arguments
            check_string = remove_optional(check_string);

            int button = this.get_mouse_button(check_string);
            if (button > 0) {
                check_string = check_string.substring(0, check_string.index_of("button"));

                this.with_mouse = true;
                this.key_code = button;
                this.key_sym = button;

                Gtk.accelerator_parse(check_string, null, out this._modifiers);
                this.label = Gtk.accelerator_get_label(0, this.modifiers);

                if (this.label != "") {
                    label += "+";
                }

                string button_text = _("Button %i").printf(this.key_code);

                if (this.key_code == 1)
                    button_text = _("LeftButton");
                else if (this.key_code == 3)
                    button_text = _("RightButton");
                else if (this.key_code == 2)
                    button_text = _("MiddleButton");

                this.label += button_text;
            } else {
                //empty triggers are ok now, they carry open options as well
                if (check_string == "") {
                    this.label = _("Not bound");
                    this.key_code = 0;
                    this.key_sym = 0;
                    this.modifiers = 0;
                } else {
                    this.with_mouse = false;

                    var display = new X.Display();

                    uint keysym = 0;
                    Gtk.accelerator_parse(check_string, out keysym, out this._modifiers);
                    this.key_code = display.keysym_to_keycode(keysym);
                    this.key_sym = keysym;
                    this.label = Gtk.accelerator_get_label(keysym, this.modifiers);
                }
            }

            this.label_with_specials = GLib.Markup.escape_text(this.label);

            string msg= "";
            if (this.turbo) {
                msg= _("Turbo");
            }
            if (this.delayed) {
                if (msg == "")
                    msg= _("Delayed");
                else
                    msg += " | " + _("Delayed");
            }
            if (this.centered) {
                if (msg == "")
                    msg= _("Centered");
                else
                    msg += " | " + _("Centered");
            }
            if (this.warp) {
                if (msg == "")
                    msg= _("Warp");
                else
                    msg += " | " + _("Warp");
            }
            if (this.shape == 0) {
                if (msg == "")
                    msg= _("Auto-shaped");
                else
                    msg += " | " + _("Auto-shaped");
            } else if (this.shape == 1 || this.shape ==3 || this.shape == 7 || this.shape == 9) {
                if (msg == "")
                    msg= _("Quarter pie");
                else
                    msg += " | " + _("Quarter pie");

            } else if (this.shape == 2 || this.shape == 4 || this.shape == 6 || this.shape == 8) {
                if (msg == "")
                    msg= _("Half pie");
                else
                    msg += " | " + _("Half pie");
            }
            if (msg != "")
                this.label_with_specials += ("  [ " + msg + " ]");

        } else {
            this.set_unbound();
        }
    }

    /////////////////////////////////////////////////////////////////////
    /// Extract shape number from trigger string
    /// "[0]".."[9]" 0:auto 5:full pie (default)
    /// 1,3,7,9=quarters    2,4,6,8= halves
    /////////////////////////////////////////////////////////////////////

    private int parse_shape(string trigger) {
        int rs;
        for( rs= 0; rs < 10; rs++ )
            if (trigger.contains("[shape%d]".printf(rs) ))
                return rs;
        return 5; //default= full pie
    }

    /////////////////////////////////////////////////////////////////////
    /// Resets all member variables to their defaults.
    /////////////////////////////////////////////////////////////////////

    private void set_unbound() {
        this.label = _("Not bound");
        this.label_with_specials = _("Not bound");
        this.name = "";
        this.key_code = 0;
        this.key_sym = 0;
        this.modifiers = 0;
        this.turbo = false;
        this.delayed = false;
        this.centered = false;
        this.warp = false;
        this.shape = 5; //full pie
        this.with_mouse = false;
    }

    /////////////////////////////////////////////////////////////////////
    /// Remove optional arguments from the given string
    /// "[turbo]", "[delayed]", "[warp]" "[centered]" and "[shape#]"
    /////////////////////////////////////////////////////////////////////

    public static string remove_optional(string trigger) {
        string trg= trigger;
        trg = trg.replace("[turbo]", "");
        trg = trg.replace("[delayed]", "");
        trg = trg.replace("[centered]", "");
        trg = trg.replace("[warp]", "");
        for (int rs= 0; rs < 10; rs++)
            trg = trg.replace("[shape%d]".printf(rs), "");
        return trg;
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns true, if the trigger string is in a valid format.
    /////////////////////////////////////////////////////////////////////

    private bool is_valid(string trigger) {
        // remove optional arguments
        string check_string = remove_optional(trigger);

        if (this.get_mouse_button(check_string) > 0) {
            // it seems to be a valid mouse-trigger so replace button part,
            // with something accepted by gtk, and check it with gtk
            int button_index = check_string.index_of("button");
            check_string = check_string.slice(0, button_index) + "a";
        }

        //empty triggers are ok now, they carry open options as well
        if (check_string == "")
            return true;

        // now it shouls be a normal gtk accelerator
        uint keysym = 0;
        Gdk.ModifierType modifiers = 0;
        Gtk.accelerator_parse(check_string, out keysym, out modifiers);
        if (keysym == 0)
            return false;

        return true;
    }

    /////////////////////////////////////////////////////////////////////
    /// Returns the mouse button number of the given trigger string.
    /// Returns -1 if it is not a mouse trigger.
    /////////////////////////////////////////////////////////////////////

    private int get_mouse_button(string trigger) {
        if (trigger.contains("button")) {
            // it seems to be a mouse-trigger so check the button part.
            int button_index = trigger.index_of("button");
            int number = int.parse(trigger.slice(button_index + 6, trigger.length));
            if (number > 0)
                return number;
        }

        return -1;
    }
}

}