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
|
#include <fstream>
#include <sstream>
#include <cassert>
#include <iostream>
#include <cstdlib>
#include "keymap.h"
#include "logger.h"
using namespace std;
void GetLine(istream& is, istringstream& iss){
char buf[200];
do {
is.getline(buf,200);
} while (is.good() && buf[0]=='#');
string sbuf(buf);
iss.str(sbuf);
}
int Keymap::GetFunId(string f){
int fun_id = -1;
for (int i = 0; i<functions.size(); ++i)
if (functions[i] == f){
fun_id = i;
break;
}
if (fun_id == -1){
fun_id = functions.size();
functions.push_back(f);
keys.push_back(vector<Key>());
}
return fun_id;
}
Keymap::Keymap():function(0), is_alt(false), is_ctrl(false), keep_fun(true){
}
void Keymap::ReadMap(const string& filename){
keys.clear();
functions.clear();
function = 0;
is_alt = false;
is_ctrl = false;
ifstream is;
is.open(filename.c_str());
GetFunId("normal");
string first;
int minx=10000, miny=10000, maxx=-10000, maxy=-10000;
bool eatlast = false;
while (is.good()){
if (!eatlast)
is >> first;
else
eatlast = false;
if (first == "key"){
int px, py, w, h;
is >> px >> py >> w >> h;
minx = min(minx, px);
miny = min(miny, py);
maxx = max(maxx, px+w);
maxy = max(maxy, py+h);
Key k(px, py, w, h);
do {
is >> first;
if (!is.good())
break;
//cout << "in key "<< first<<endl;
if (first == "key"){
eatlast = true;
break;
}
string look, action;
is >> look >> action;
int function = GetFunId(first);
k.look = look;
k.action = action;
keys[function].push_back(k);
if (first == "shift" && action!="capslock")
keys[GetFunId("capslock")].push_back(k);
} while (1);
}
}
for (int i=0; i<keys.size(); ++i)
for (int j=0; j<keys[i].size(); ++j){
keys[i][j].px-=minx;
keys[i][j].py-=miny;
}
width=maxx-minx+1;
height=maxy-miny+1;
}
void Keymap::SetFunction(string f){
if (f == "ctrl"){
is_ctrl = !is_ctrl;
return;
}
if (f == "alt"){
is_alt = !is_alt;
return;
}
keep_fun = true;
if (f == "shift")
keep_fun = false;
int new_fun_id = GetFunId(f);
if (new_fun_id == function) // we are currently on that function so set back to normal
function = GetFunId("normal");
else
function = new_fun_id;
}
string Keymap::GetAction(int x, int y){
string ret;
vector<Key>& fkeys = keys[function];
for (int i=0; i<fkeys.size(); ++i)
if (x>=fkeys[i].px && y>=fkeys[i].py && x<fkeys[i].px+fkeys[i].w && y<fkeys[i].py+fkeys[i].h){
ret = fkeys[i].action;
break;
}
if (is_alt && ret[0] != '$'){
ret = string("alt:")+ret;
is_alt = false;
}
if (is_ctrl && ret[0] != '$'){
ret = string("ctrl:")+ret;
is_ctrl = false;
}
if (!keep_fun){
ret = string("refresh:")+ret;
function = GetFunId("normal");
keep_fun = true;
}
return ret;
}
bool Keymap::GetKey(int x, int y, Key* key) const {
const vector<Key>& fkeys = keys[function];
for (int i=0; i<fkeys.size(); ++i)
if (x>=fkeys[i].px && y>=fkeys[i].py && x<fkeys[i].px+fkeys[i].w && y<fkeys[i].py+fkeys[i].h){
*key = fkeys[i];
return true;
}
return false;
}
const vector<Keymap::Key>& Keymap::GetCurrentKeys() const {
return keys[function];
}
int Keymap::Height() const {
return height;
}
int Keymap::Width() const {
return width;
}
int Keymap::CurrentFunction() const {
return function;
}
|