~mc-return/compiz/compiz.merge-src-header-files-cleanup

« back to all changes in this revision

Viewing changes to src/action.cpp

action.cpp and actions.cpp code cleanup:

Use pre- instead of postfix increment.
Declaration and assignment of variables in one line.
Merged if condition checks.
Removed redundant brackets.
Added and removed newlines.
Fixed indentation.

Approved by PS Jenkins bot, Sam Spilsbury.

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
namespace ca = compiz::actions;
37
37
 
38
 
struct _Modifier {
 
38
struct _Modifier
 
39
{
39
40
    const char *name;
40
41
    int        modifier;
41
 
} modifiers[] = {
42
 
    { "<Shift>",      ShiftMask          },
43
 
    { "<Control>",    ControlMask        },
44
 
    { "<Mod1>",       Mod1Mask           },
45
 
    { "<Mod2>",       Mod2Mask           },
46
 
    { "<Mod3>",       Mod3Mask           },
47
 
    { "<Mod4>",       Mod4Mask           },
48
 
    { "<Mod5>",       Mod5Mask           },
49
 
    { "<Alt>",        CompAltMask        },
50
 
    { "<Meta>",       CompMetaMask       },
 
42
} modifiers[] =
 
43
{
 
44
    { "<Shift>",      ShiftMask          },
 
45
    { "<Control>",    ControlMask        },
 
46
    { "<Mod1>",       Mod1Mask           },
 
47
    { "<Mod2>",       Mod2Mask           },
 
48
    { "<Mod3>",       Mod3Mask           },
 
49
    { "<Mod4>",       Mod4Mask           },
 
50
    { "<Mod5>",       Mod5Mask           },
 
51
    { "<Alt>",        CompAltMask        },
 
52
    { "<Meta>",       CompMetaMask       },
51
53
    { "<Super>",      CompSuperMask      },
52
 
    { "<Hyper>",      CompHyperMask      },
 
54
    { "<Hyper>",      CompHyperMask      },
53
55
    { "<ModeSwitch>", CompModeSwitchMask }
54
56
};
55
57
 
56
58
#define N_MODIFIERS (sizeof (modifiers) / sizeof (struct _Modifier))
57
59
 
58
 
struct _Edge {
 
60
struct _Edge
 
61
{
59
62
    const char *name;
60
63
    const char *modifierName;
61
 
} edges[] = {
62
 
    { "Left",        "<LeftEdge>"        },
63
 
    { "Right",       "<RightEdge>"       },
64
 
    { "Top",         "<TopEdge>"         },
65
 
    { "Bottom",      "<BottomEdge>"      },
66
 
    { "TopLeft",     "<TopLeftEdge>"     },
67
 
    { "TopRight",    "<TopRightEdge>"    },
68
 
    { "BottomLeft",  "<BottomLeftEdge>"  },
 
64
} edges[] =
 
65
{
 
66
    { "Left",        "<LeftEdge>"        },
 
67
    { "Right",       "<RightEdge>"       },
 
68
    { "Top",         "<TopEdge>"         },
 
69
    { "Bottom",      "<BottomEdge>"      },
 
70
    { "TopLeft",     "<TopLeftEdge>"     },
 
71
    { "TopRight",    "<TopRightEdge>"    },
 
72
    { "BottomLeft",  "<BottomLeftEdge>"  },
69
73
    { "BottomRight", "<BottomRightEdge>" }
70
74
};
71
75
 
74
78
{
75
79
    CompString binding;
76
80
 
77
 
    for (unsigned int i = 0; i < N_MODIFIERS; i++)
 
81
    for (unsigned int i = 0; i < N_MODIFIERS; ++i)
78
82
    {
79
83
        if (modMask & modifiers[i].modifier)
80
84
            binding += modifiers[i].name;
84
88
}
85
89
 
86
90
static unsigned int
87
 
stringToModifiers (CompString  str)
 
91
stringToModifiers (const CompString &str)
88
92
{
89
93
    unsigned int mods = 0;
90
94
 
91
 
    for (unsigned int i = 0; i < N_MODIFIERS; i++)
92
 
    {
 
95
    for (unsigned int i = 0; i < N_MODIFIERS; ++i)
93
96
        if (str.find (modifiers[i].name) != std::string::npos)
94
97
            mods |= modifiers[i].modifier;
95
 
    }
96
98
 
97
99
    return mods;
98
100
}
99
101
 
100
102
static unsigned int
101
 
bindingStringToEdgeMask (CompString  str)
 
103
bindingStringToEdgeMask (CompString str)
102
104
{
103
105
    unsigned int edgeMask = 0;
104
106
 
105
 
    for (int i = 0; i < SCREEN_EDGE_NUM; i++)
 
107
    for (int i = 0; i < SCREEN_EDGE_NUM; ++i)
106
108
        if (str.find (edges[i].modifierName) != std::string::npos)
107
109
            edgeMask |= 1 << i;
108
110
 
114
116
{
115
117
    CompString binding;
116
118
 
117
 
    for (int i = 0; i < SCREEN_EDGE_NUM; i++)
 
119
    for (int i = 0; i < SCREEN_EDGE_NUM; ++i)
118
120
        if (edgeMask & (1 << i))
119
121
            binding += edges[i].modifierName;
120
122
 
133
135
{
134
136
}
135
137
 
136
 
CompAction::KeyBinding::KeyBinding (int keycode, unsigned int modifiers) :
 
138
CompAction::KeyBinding::KeyBinding (int          keycode,
 
139
                                    unsigned int modifiers) :
137
140
    mModifiers (modifiers),
138
141
    mKeycode (keycode)
139
142
{
154
157
bool
155
158
CompAction::KeyBinding::fromString (const CompString &str)
156
159
{
157
 
    CompString   sStr;
158
 
    unsigned int mods;
159
 
    size_t       pos, start = 0;
160
 
    KeySym       keysym;
161
 
 
162
160
    /* This assertion is a regression test for LP: #930412 */
163
161
    assert (screen);
 
162
 
164
163
    if (!screen)
165
164
        return false;
166
165
 
167
 
    mods = stringToModifiers (str);
168
 
 
169
 
    pos = str.rfind ('>');
 
166
    unsigned int mods = stringToModifiers (str);
 
167
 
 
168
    size_t start = 0;
 
169
    size_t pos   = str.rfind ('>');
 
170
 
170
171
    if (pos != std::string::npos)
171
172
        start = pos + 1;
172
173
 
173
174
    while (start < str.size () && !isalnum (str[start]))
174
 
        start++;
 
175
        ++start;
175
176
 
176
177
    if (start == str.size ())
177
178
    {
186
187
        return false;
187
188
    }
188
189
 
189
 
    sStr   = str.substr (start);
190
 
    keysym = XStringToKeysym (sStr.c_str ());
 
190
    CompString sStr   = str.substr (start);
 
191
    KeySym     keysym = XStringToKeysym (sStr.c_str ());
191
192
 
192
193
    if (keysym != NoSymbol)
193
194
    {
194
195
        KeyCode keycode;
195
196
 
196
197
        keycode = XKeysymToKeycode (screen->dpy (), keysym);
 
198
 
197
199
        if (keycode)
198
200
        {
199
201
            mKeycode   = keycode;
217
219
CompString
218
220
CompAction::KeyBinding::toString () const
219
221
{
220
 
    CompString binding;
221
 
 
222
222
    if (!screen)
223
223
        return "";
224
224
 
225
 
    binding = modifiersToString (mModifiers);
 
225
    CompString binding = modifiersToString (mModifiers);
226
226
 
227
227
    if (mKeycode != 0)
228
228
    {
229
 
        KeySym keysym;
230
 
        char   *keyname;
231
 
 
232
 
        keysym  = XKeycodeToKeysym (screen->dpy (), mKeycode, 0);
233
 
        keyname = XKeysymToString (keysym);
 
229
        KeySym keysym   = XKeycodeToKeysym (screen->dpy (), mKeycode, 0);
 
230
        char   *keyname = XKeysymToString (keysym);
234
231
 
235
232
        if (keyname)
236
233
            binding += keyname;
274
271
bool
275
272
CompAction::ButtonBinding::fromString (const CompString &str)
276
273
{
277
 
    unsigned int mods;
278
 
    size_t       pos, start = 0;
279
 
 
280
 
    mods = stringToModifiers (str);
281
 
 
282
 
    pos = str.rfind ('>');
 
274
    unsigned int mods = stringToModifiers (str);
 
275
 
 
276
    size_t start = 0;
 
277
    size_t pos   = str.rfind ('>');
 
278
 
283
279
    if (pos != std::string::npos)
284
280
        start = pos + 1;
285
281
 
286
282
    while (start < str.size () && !isalnum (str[start]))
287
 
        start++;
 
283
        ++start;
288
284
 
289
285
    if (start != str.size () && str.compare (start, 6, "Button") == 0)
290
286
    {
305
301
CompString
306
302
CompAction::ButtonBinding::toString () const
307
303
{
308
 
    CompString binding;
309
 
 
310
304
    if (!mModifiers && !mButton)
311
305
        return "";
312
306
 
313
 
    binding = modifiersToString (mModifiers);
314
 
    binding += compPrintf ("Button%d", mButton);
 
307
    CompString binding = modifiersToString (mModifiers);
 
308
    binding           += compPrintf ("Button%d", mButton);
315
309
 
316
310
    return binding;
317
311
}
403
397
            priv->type = CompAction::BindingTypeButton;
404
398
    }
405
399
    else
406
 
    {
407
400
        priv->type = CompAction::BindingTypeNone;
408
 
    }
409
401
}
410
402
 
411
403
unsigned int
420
412
    priv->edgeMask = edge;
421
413
 
422
414
    if (priv->type == CompAction::BindingTypeEdgeButton ||
423
 
        priv->type == CompAction::BindingTypeButton)
 
415
        priv->type == CompAction::BindingTypeButton)
424
416
    {
425
417
        if (priv->edgeMask)
426
418
            priv->type = CompAction::BindingTypeEdgeButton;
460
452
bool
461
453
CompAction::operator== (const CompAction& val) const
462
454
{
463
 
    if (priv->state != val.priv->state)
464
 
        return false;
465
 
    if (priv->type != val.priv->type)
466
 
        return false;
467
 
    if (priv->key.modifiers () != val.priv->key.modifiers ())
468
 
        return false;
469
 
    if (priv->key.keycode () != val.priv->key.keycode ())
470
 
        return false;
471
 
    if (priv->button.modifiers () != val.priv->button.modifiers ())
472
 
        return false;
473
 
    if (priv->button.button () != val.priv->button.button ())
474
 
        return false;
475
 
    if (priv->bell != val.priv->bell)
476
 
        return false;
477
 
    if (priv->edgeMask != val.priv->edgeMask)
478
 
        return false;
479
 
    if (memcmp (&priv->priv, &val.priv->priv, sizeof (CompPrivate)) != 0)
 
455
    if (priv->state               != val.priv->state                ||
 
456
        priv->type                != val.priv->type                 ||
 
457
        priv->key.modifiers ()    != val.priv->key.modifiers ()     ||
 
458
        priv->key.keycode ()      != val.priv->key.keycode ()       ||
 
459
        priv->button.modifiers () != val.priv->button.modifiers ()  ||
 
460
        priv->button.button ()    != val.priv->button.button ()     ||
 
461
        priv->bell                != val.priv->bell                 ||
 
462
        priv->edgeMask            != val.priv->edgeMask             ||
 
463
        memcmp (&priv->priv, &val.priv->priv, sizeof (CompPrivate)) != 0)
480
464
        return false;
481
465
 
482
466
    return true;
500
484
    bool retval = priv->key.fromString (str);
501
485
 
502
486
    if (retval)
503
 
    {
504
487
        priv->type = CompAction::BindingTypeKey;
505
 
    }
506
488
    else
507
489
    {
508
490
        priv->type = CompAction::BindingTypeNone;
 
491
 
509
492
        if (str == "Disabled")
510
493
            retval = true;
511
494
    }
521
504
    if (retval)
522
505
    {
523
506
        priv->edgeMask = bindingStringToEdgeMask (str);
 
507
 
524
508
        if (priv->edgeMask)
525
509
            priv->type = CompAction::BindingTypeEdgeButton;
526
510
        else
529
513
    else
530
514
    {
531
515
        priv->type = CompAction::BindingTypeNone;
 
516
 
532
517
        if (str == "Disabled")
533
518
            retval = true;
534
519
    }
542
527
    unsigned int edgeMask = 0;
543
528
    size_t       pos;
544
529
 
545
 
    for (int i = 0; i < SCREEN_EDGE_NUM; i++)
 
530
    for (int i = 0; i < SCREEN_EDGE_NUM; ++i)
546
531
    {
547
532
        pos = 0;
 
533
 
548
534
        while ((pos = str.find (edgeToString (i), pos)) != std::string::npos)
549
535
        {
550
536
            if (pos > 0 && isalnum (str[pos - 1]))
551
537
            {
552
 
                pos++;
 
538
                ++pos;
553
539
                continue;
554
540
            }
555
541
 
570
556
CompString
571
557
CompAction::keyToString ()
572
558
{
573
 
    CompString binding;
 
559
    CompString binding = priv->key.toString ();
574
560
 
575
 
    binding = priv->key.toString ();
576
561
    if (!binding.size ())
577
562
        return "Disabled";
578
563
 
582
567
CompString
583
568
CompAction::buttonToString ()
584
569
{
585
 
    CompString binding, edge;
586
 
 
587
 
    binding = modifiersToString (priv->button.modifiers ());
588
 
    binding += edgeMaskToBindingString (priv->edgeMask);
589
 
 
590
 
    binding += compPrintf ("Button%d", priv->button.button ());
 
570
    CompString edge;
 
571
 
 
572
    CompString binding = modifiersToString (priv->button.modifiers ());
 
573
    binding           += edgeMaskToBindingString (priv->edgeMask);
 
574
    binding           += compPrintf ("Button%d", priv->button.button ());
591
575
 
592
576
    if (!priv->button.button ())
593
577
        return "Disabled";
600
584
{
601
585
    CompString edge;
602
586
 
603
 
    for (int i = 0; i < SCREEN_EDGE_NUM; i++)
 
587
    for (int i = 0; i < SCREEN_EDGE_NUM; ++i)
604
588
    {
605
589
        if (priv->edgeMask & (1 << i))
606
590
        {
614
598
    return edge;
615
599
}
616
600
 
617
 
 
618
601
CompString
619
602
CompAction::edgeToString (unsigned int edge)
620
603
{
635
618
 
636
619
void
637
620
ca::setActionActiveState (const CompAction &action,
638
 
                          bool                  active)
 
621
                          bool             active)
639
622
{
640
623
    action.priv->setActive (active);
641
624
}
642
625
 
643
626
PrivateAction::PrivateAction () :
644
 
    initiate (),
 
627
    initiate  (),
645
628
    terminate (),
646
 
    state (0),
647
 
    type (0),
648
 
    key (),
649
 
    button (),
650
 
    bell (false),
651
 
    edgeMask (0),
652
 
    active (false)
 
629
    state     (0),
 
630
    type      (0),
 
631
    key       (),
 
632
    button    (),
 
633
    bell      (false),
 
634
    edgeMask  (0),
 
635
    active    (false)
653
636
{
654
637
    memset (&priv, 0, sizeof (CompPrivate));
655
638
}
656
639
 
657
 
PrivateAction::PrivateAction (const PrivateAction& a) :
658
 
    initiate (a.initiate),
 
640
PrivateAction::PrivateAction (const PrivateAction &a) :
 
641
    initiate  (a.initiate),
659
642
    terminate (a.terminate),
660
 
    state (a.state),
661
 
    type (a.type),
662
 
    key (a.key),
663
 
    button (a.button),
664
 
    bell (a.bell),
665
 
    edgeMask (a.edgeMask),
666
 
    active (a.active)
 
643
    state     (a.state),
 
644
    type      (a.type),
 
645
    key       (a.key),
 
646
    button    (a.button),
 
647
    bell      (a.bell),
 
648
    edgeMask  (a.edgeMask),
 
649
    active    (a.active)
667
650
{
668
651
    memcpy (&priv, &a.priv, sizeof (CompPrivate));
669
652
}