wmi

git clone git://oldgit.suckless.org/wmi/
Log | Files | Refs | LICENSE

action.cpp (2842B)


      1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us>
      2 // See LICENSE for license details.
      3 
      4 extern "C" {
      5 #include <stdlib.h>
      6 #include <string.h>
      7 }
      8 
      9 #include "wmi.h"
     10 
     11 #include "action.h"
     12 #include "actions.h"
     13 #include "kernel.h"
     14 #include "monitor.h"
     15 #include "prompt.h"
     16 #include "shortcut.h"
     17 #include "validators.h"
     18 
     19 
     20 Action::Action(string id, ToPerform toPerform,
     21            IsValid isValid, Type type, char *argument)
     22 {
     23     id_ = id;
     24     toPerform_ = toPerform;
     25     isValid_ = isValid;
     26     type_ = type;
     27     argument_ = argument;
     28     shortcut_ = 0;
     29     listenOn_ = 0;
     30 }
     31 
     32 Action::~Action() {
     33     if (argument_) {
     34         free(argument_);
     35     }
     36 }
     37 
     38 void Action::perform() {
     39     // great HACK to return from maximized frames back to normality
     40     Monitor *focusedMonitor = KERNEL->focusedMonitor();
     41 
     42     if (focusedMonitor->isThingMaximized() &&
     43         (toPerform_ != &Actions::cycleClientNext) &&
     44         (toPerform_ != &Actions::cycleClientPrev) &&
     45         (toPerform_ != &Actions::toggleBar) &&
     46         (toPerform_ != &Actions::toggleMaximization))
     47     {
     48 
     49         focusedMonitor->toggleThingMaximization();
     50     }
     51 
     52     if (KERNEL->isRecording() &&
     53         (toPerform_ != &Actions::endChainRecord) &&
     54         (toPerform_ != &Actions::endScriptRecord) &&
     55         (toPerform_ != &Actions::inputMode))
     56     {
     57          KERNEL->recordedActions()->push_back(
     58                  new Action(id_, 0, 0, type_, argument_ ? strdup(argument_) : 0));
     59     }
     60 
     61     Actions actions = *Actions::instance();
     62 
     63     if (isValid()) {
     64         (actions.*toPerform_)(this, argument_); // call action
     65     }
     66 }
     67 
     68 bool Action::isValid() {
     69     Validators validators = *Validators::instance();
     70 
     71     return (validators.*isValid_)();
     72 }
     73 
     74 Prompt *Action::prompt(unsigned int index) {
     75 
     76     unsigned int i = 0;
     77     for (LPrompt::iterator it = prompts_.begin();
     78          it != prompts_.end(); it++)
     79     {
     80         if (i == index) {
     81             return (*it);
     82         }
     83         i++;
     84     }
     85 
     86     // should never occure
     87     return 0;
     88 }
     89 
     90 string Action::id() const {
     91     return id_;
     92 }
     93 
     94 unsigned int Action::promptsCount() const {
     95     return prompts_.size();
     96 }
     97 
     98 
     99 IsValid Action::getIsValid() const {
    100     return isValid_;
    101 }
    102 
    103 void Action::setShortcut(Shortcut *shortcut) {
    104     shortcut_ = shortcut;
    105     listenOn_ = shortcut;
    106 }
    107 
    108 Shortcut *Action::shortcut() const {
    109     return shortcut_;
    110 }
    111 
    112 void Action::setArgument(char *argument) {
    113     argument_ = argument;
    114 }
    115 
    116 char *Action::argument() const {
    117     return argument_;
    118 }
    119 
    120 Action::Type Action::type() const {
    121     return type_;
    122 }
    123 
    124 void Action::setType(Type type) {
    125     type_ = type;
    126 }
    127 
    128 
    129 LPrompt *Action::prompts() {
    130     return &prompts_;
    131 }
    132 
    133 ToPerform Action::getToPerform() const {
    134     return toPerform_;
    135 }
    136 
    137 void Action::setListenOn(Shortcut *listenOn) {
    138     listenOn_ = listenOn;
    139 }
    140 
    141 Shortcut *Action::listenOn() const {
    142     return listenOn_;
    143 }