wmi

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

validators.cpp (8445B)


      1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us>
      2 // See LICENSE for license details.
      3 
      4 #include "validators.h"
      5 
      6 #include <map>
      7 
      8 #include "action.h"
      9 #include "client.h"
     10 #include "frame.h"
     11 #include "kernel.h"
     12 #include "inputbar.h"
     13 #include "monitor.h"
     14 #include "slot.h"
     15 #include "statusbar.h"
     16 #include "thing.h"
     17 #include "workspace.h"
     18 
     19 Validators::Validators() {
     20 }
     21 
     22 Validators::~Validators() {
     23 }
     24 
     25 bool Validators::validateSelectClient() {
     26 
     27     Monitor *focusedMonitor = KERNEL->focusedMonitor();
     28 
     29     for (LWorkspace::iterator wit = focusedMonitor->begin();
     30             wit != focusedMonitor->end(); wit++)
     31     {
     32         Workspace *workspace = *wit;
     33  
     34         if (workspace->floatingClients()->size() ||
     35             (workspace->focusedFrame() &&
     36              workspace->focusedFrame()->size()))
     37         {
     38             return true;
     39         }
     40 
     41     }
     42 
     43     return false;
     44 }
     45 
     46 bool Validators::validateCycleFrame() {
     47 
     48     return existFrames() && isFrameFocused();
     49 
     50 }
     51 
     52 bool Validators::validateInputMode() {
     53 
     54     Monitor *focusedMonitor = KERNEL->focusedMonitor();
     55     return !focusedMonitor->inputBar()->isVisible();
     56 }
     57 
     58 
     59 bool Validators::isAlwaysPossible() {
     60 
     61     return true;
     62 }
     63 
     64 bool Validators::isWorkspaceFocused() {
     65 
     66     Monitor *focusedMonitor = KERNEL->focusedMonitor();
     67 
     68     return focusedMonitor->focused() != 0;
     69 }
     70 
     71 bool Validators::existMonitors() {
     72     return KERNEL->monitors()->size() > 1;
     73 }
     74 
     75 bool Validators::existWorkspaces() {
     76 
     77     Monitor *focusedMonitor = KERNEL->focusedMonitor();
     78 
     79     return focusedMonitor->size() > 1;
     80 }
     81 
     82 bool Validators::isEmptyWorkspaceFocused() {
     83 
     84     Monitor *focusedMonitor = KERNEL->focusedMonitor();
     85     Workspace *workspace = focusedMonitor->focused();
     86 
     87     if (workspace->floatingClients()->size() ||
     88             (workspace->focusedFrame() &&
     89              workspace->focusedFrame()->size()))
     90     {
     91         return false;
     92     }
     93     return true;
     94 }
     95 
     96 bool Validators::validateDestroyAction() {
     97 
     98     MBindings *bindings = KERNEL->actionBindings();
     99     for (MBindings::iterator it = bindings->begin();
    100             it != bindings->end(); it++)
    101     {
    102         Action *action = (*it).second;
    103         if (action->type() != Action::INTERN) {
    104             return true;
    105         }
    106     }
    107     return false;
    108 }
    109 
    110 bool Validators::validateDestroyWorkspace() {
    111 
    112     return isEmptyWorkspaceFocused() && existWorkspaces();
    113 }
    114 
    115 bool Validators::existsFrameLeft() {
    116 
    117     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    118     Workspace *workspace = focusedMonitor->focused();
    119 
    120     return workspace->neighborTree(LEFT) != 0;
    121 }
    122 
    123 bool Validators::existsFrameRight() {
    124 
    125     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    126     Workspace *workspace = focusedMonitor->focused();
    127 
    128     return workspace->neighborTree(RIGHT) != 0;
    129 }
    130 
    131 bool Validators::existsFrameUp() {
    132 
    133     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    134     Workspace *workspace = focusedMonitor->focused();
    135 
    136     return workspace->neighborTree(UP) != 0;
    137 }
    138 
    139 bool Validators::existsFrameDown() {
    140 
    141     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    142     Workspace *workspace = focusedMonitor->focused();
    143 
    144     return workspace->neighborTree(DOWN) != 0;
    145 }
    146 
    147 bool Validators::validateToggleMode() {
    148 
    149     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    150     Workspace *workspace = focusedMonitor->focused();
    151 
    152     if (workspace->isFrameMode()) {
    153         return workspace->floatingClients()->size();
    154     }
    155     else {
    156         return workspace->frames()->size();
    157     }
    158 }
    159 
    160 bool Validators::existFrames() {
    161 
    162     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    163     Workspace *workspace = focusedMonitor->focused();
    164 
    165     if (workspace) {
    166         return workspace->frames()->size();
    167     }
    168 
    169     return false;
    170 }
    171 
    172 bool Validators::validateResizeLeft() {
    173 
    174     return isClientFrameFocused() || existsFrameLeft();
    175 }
    176 
    177 bool Validators::validateResizeRight() {
    178 
    179     return isClientFrameFocused() || existsFrameRight();
    180 }
    181 
    182 bool Validators::validateResizeUp() {
    183 
    184     return isClientFrameFocused() || existsFrameUp();
    185 }
    186 
    187 bool Validators::validateResizeDown() {
    188 
    189     return isClientFrameFocused() || existsFrameDown();
    190 }
    191 
    192 bool Validators::isFrameOrClientFrameFocused() {
    193 
    194     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    195     Workspace *workspace = focusedMonitor->focused();
    196 
    197     if (workspace) {
    198         Thing *thing = workspace->focusedThing();
    199 
    200         if (!thing) {
    201             return false;
    202         }
    203 
    204         return thing->hasDecoration();
    205     }
    206     return false;
    207 
    208 }
    209 
    210 
    211 bool Validators::isFrameFocused() {
    212 
    213     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    214     Workspace *workspace = focusedMonitor->focused();
    215 
    216     if (workspace) {
    217         return workspace->focusedFrame() && workspace->isFrameMode();
    218     }
    219     return false;
    220 }
    221 
    222 
    223 bool Validators::isClientFrameFocused() {
    224 
    225     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    226     Workspace *workspace = focusedMonitor->focused();
    227 
    228     if (!workspace) {
    229         return false;
    230     }
    231 
    232     Client *client = workspace->topClient();
    233 
    234     if (!client) {
    235         return false;
    236     }
    237 
    238     return !client->frame();
    239 }
    240 
    241 bool Validators::isFloatingClientFocused() {
    242 
    243     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    244     Workspace *workspace = focusedMonitor->focused();
    245 
    246     if (workspace) {
    247         Thing *thing = workspace->focusedThing();
    248 
    249         if (!thing) {
    250             return false;
    251         }
    252 
    253         return thing->type() != Thing::FRAME;
    254     }
    255 
    256     return false;
    257 }
    258 
    259 bool Validators::isClientFocused() {
    260 
    261     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    262     Workspace *workspace = focusedMonitor->focused();
    263 
    264     return workspace->focusedThing() != 0;
    265 
    266 }
    267 
    268 bool Validators::existClientsWithinFrame() {
    269 
    270     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    271     Workspace *workspace = focusedMonitor->focused();
    272 
    273     if (workspace) {
    274         Thing *thing = workspace->focusedThing();
    275         if (thing && (thing->type() == Thing::FRAME)) {
    276             Frame *frame = (Frame *)thing;
    277             return frame->size() > 1;
    278         }
    279     }
    280 
    281     return false;
    282 }
    283 
    284 bool Validators::existClients() {
    285 
    286     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    287     Workspace *workspace = focusedMonitor->focused();
    288 
    289     if (workspace->isFrameMode()) {
    290         Frame *frame = workspace->focusedFrame();
    291         return frame && (frame->size() > 1);
    292     }
    293     else {
    294         return (workspace->floatingClients()->size() > 1);
    295     }
    296 }
    297 
    298 bool Validators::existDetachedClients() {
    299 
    300     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    301 
    302     return isWorkspaceFocused() &&
    303         (focusedMonitor->detachedClients()->size() > 1);
    304 }
    305 
    306 bool Validators::existsDetachedClient() {
    307 
    308     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    309 
    310     return isWorkspaceFocused() &&
    311         (focusedMonitor->detachedClients()->size() > 0);
    312 }
    313 
    314 bool Validators::validateCancelRecord() {
    315     return KERNEL->isRecording();
    316 }
    317 
    318 bool Validators::validateEndRecord() {
    319     return KERNEL->isRecording() && (KERNEL->recordedActions()->size() > 0);
    320 }
    321 
    322 bool Validators::validateBeginRecord() {
    323     return !KERNEL->isRecording();
    324 }
    325 
    326 #ifdef SLOT_SUPPORT
    327 bool Validators::existsSlotClient() {
    328     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    329     return focusedMonitor->slot()->focused()->focused() != 0;
    330 }
    331 
    332 bool Validators::existSlotTabs() {
    333     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    334     return focusedMonitor->slot()->size() > 0;
    335 }
    336 #endif // SLOT_SUPPORT
    337 
    338 bool Validators::validateHookClient() {
    339 
    340 #ifdef SLOT_SUPPORT
    341     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    342     Slot *slot = focusedMonitor->slot();
    343     Client *client = slot->focused()->focused();
    344     if (client && slot->isGrabFocus()) {
    345         return true;
    346     }
    347 #endif // SLOT_SUPPORT
    348     return isClientFocused();
    349 }
    350 
    351 bool Validators::validateUnhookClient() {
    352 
    353     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    354     Client *client = 0;
    355 #ifdef SLOT_SUPPORT
    356     Slot *slot = focusedMonitor->slot();
    357     client = slot->focused()->focused();
    358     if (client && (client->hooked() == "")
    359         && slot->isGrabFocus())
    360     {
    361         return true;
    362     }
    363 #endif // SLOT_SUPPORT
    364     Workspace *workspace = focusedMonitor->focused();
    365     client = workspace->topClient();
    366     return client && (client->hooked() == "");
    367 }
    368 
    369 bool Validators::validateDestroyFrame() {
    370     Monitor *focusedMonitor = KERNEL->focusedMonitor();
    371     Workspace *workspace = focusedMonitor->focused();
    372     Frame *frame = workspace->focusedFrame();
    373 
    374     return frame && !frame->size();
    375 }