-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinactivate.kwinscript
292 lines (253 loc) · 11 KB
/
winactivate.kwinscript
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
// qdbus org.kde.plasma-desktop /MainApplication showInteractiveKWinConsole
// plasmapkg -t kwinscript -l
// ls ~/.kde/share/apps/kwin/scripts
// http://techbase.kde.org/Development/Tutorials/KWin/Scripting
// http://techbase.kde.org/Development/Tutorials/KWin/Scripting/API_4.9
// http://techbase.kde.org/KDE_System_Administration/PlasmaDesktopScripting#Locating_Applications_and_Paths
// http://quickgit.kde.org/?p=kdeexamples.git&a=blob&f=plasma%2Fjavascript%2Fgeneric%2FfileOperations%2Fcontents%2Fcode%2Fmain.js
// http://quickgit.kde.org/?p=kdeexamples.git&a=blob&f=plasma%2Fjavascript%2Fgeneric%2FfileOperations%2Fmetadata.desktop
// http://kde-look.org/index.php?xcontentmode=91
/* Object structure
* windowGroups: Map<String name, WindowGroup>
* windowGroupHotkeys: Map<String name, String hotkey>
* WindowGroup extends Array<WindowType> { boolean matchAny(client) }
* WindowType{ classname: RegExp; title: RegExp; boolean match(client); }
*/
var windowGroups = new Object();
var windowGroupHotkeys = new Object();
function WindowType(classname, title) {
this.classname = classname;
this.title = title;
}
WindowType.prototype["match"] = function(client) {
//print("==> testing "+this.title+" === "+client.caption
//+ " "+(this.title == null || this.title.test(client.caption))
//+ " and "+this.classname+" === "+client.resourceClass.toString()
//+ " "+(this.classname == null || this.classname.test(client.resourceClass.toString())));
return ((this.title == null || this.title.test(client.caption)) &&
(this.classname == null || this.classname.test(client.resourceClass.toString())));
}
function WindowGroup() { }
WindowGroup.prototype = new Array();
WindowGroup.prototype["matchAny"] = function(client) {
for (var i = 0, l=this.length; i < l; ++i) {
var windowtype=this[i];
if (windowtype.match(client)) {
// print("Success!! "+client.caption);
return true;
}
}
return false;
}
function add2windowgroup(group, classname) {
add2windowgroup(group, classname, null);
}
function add2windowgroup(group, classname, title) {
if (windowGroups[group] == undefined) {
windowGroups[group] = new WindowGroup();
}
windowGroups[group].push( new WindowType(classname, title) );
}
var search = false;
/** Returns whether given client matched. */
function kwinactivateclient(client,targetGroupName) {
var success=false;
if (client == workspace.activeClient) {
search = !search;
} else if (search) {
// print(Object.getOwnPropertyNames(this.classname));
group = windowGroups[targetGroupName];
if (group === undefined) {
var knownGroups="";
for (kGroup in windowGroups) { knownGroups += " "+kGroup; }
print("Unknow window group name: "+targetGroupName)
print("Register it with add2windowgroup or try with these known groups: "+knownGroups);
} else {
if (group.matchAny(client)) {
print("Success!! "+client.caption);
workspace.activeClient = client;
search = false; //do nothing for remaining windows.
success = true; //found
} else {
print(client.caption+" does not match");
}
}
}
return success;
}
function kwinactivate(targetGroupName) {
search = false;
var found = false;
var cL = workspace.clientList();
//call twice to make sure we get through the whole window list while "search" is true
cL.concat(cL).forEach(function(client) {
if (client == undefined) continue;
if (!found) { found = kwinactivateclient(client,targetGroupName); }
});
return found;
}
function launch(launcher) {
print ("Launching "+launcher);
//qdbus org.kde.klauncher /KLauncher org.kde.KLauncher.start_service_by_desktop_name kate "" "" "" true
//qdbus org.kde.klauncher /KLauncher org.kde.KLauncher.exec_blind kate -u
//qdbus [servicename] [path] [interface].[method] [args]
//callDBus(QString service, QString path, QString interface, QString method, QVariant arg..., QScriptValue callback = QScriptValue())
//works: callDBus("org.kde.kwin", "/KWin", "org.kde.KWin", "setCurrentDesktop", 2);
//qdbus org.kde.kwin /KWin org.kde.KWin.currentDesktop
//callDBus("org.kde.kwin", "/KWin", "org.kde.KWin", "currentDesktop", function (desktop) {
// print("Current Desktop through D-Bus: ", desktop);
//});
// show krunner and fill it with the right content: callDBus("org.kde.krunner","/App","org.kde.krunner.App","query",launcher);
//qdbus org.kde.klauncher /KLauncher org.kde.KLauncher.start_service_by_desktop_path /usr/share/applications/chromium-browser.desktop "http://google.fr" "" "" true
callDBus("org.kde.klauncher","/KLauncher","org.kde.klauncher","start_service_by_desktop_path","/usr/share/applications/chromium-browser.desktop", "http://google.fr", "", "", "true", function() {
print("===> success");
});
//qdbus org.kde.klauncher /KLauncher org.kde.KLauncher.exec_blind kshell4 konsole
var arglist = new Array(launcher);
print("callDBus(\"org.kde.klauncher\", \"/KLauncher\", \"org.kde.KLauncher\", \"exec_blind\", \"kshell4\", "+launcher+");");
callDBus("org.kde.klauncher", "/KLauncher", "org.kde.KLauncher","exec_blind", "kshell4", arglist, function() {
print("=======> "+launcher+" launched successfully !");
});
}
function register(shortcut, targetGroupName) {
register(shortcut, targetGroupName, null);
}
function register(shortcut, targetGroupName, launcher) {
registerShortcut("Activate next "+targetGroupName+" window", "", shortcut, function() {
if (!kwinactivate(targetGroupName) && launcher!=null) {
launch(launcher);
}
});
windowGroupHotkeys[targetGroupName] = shortcut;
}
/** launcher = null for no value ; classname|title=null for "everything" */
function registerBoth(shortcut, targetGroupName, launcher) {
print("________________"+launcher);
register(shortcut, targetGroupName, launcher);
print("________________"+launcher);
if (launcher != undefined) {
print("_________Launch "+(launcher===undefined?title:classname)+" >> Shift"+shortcut);
registerShortcut("Launch "+(launcher===undefined?title:classname), "", "Shift"+shortcut, function() {
launch(launcher);
});
}
}
function registerBoth(shortcut, targetGroupName) {
group = windowGroups[targetGroupName];
launcher = undefined;
if (group != undefined && group[0] != undefined && group[0].classname != null) {
register(shortcut, targetGroupName, group[0].classname.source);
} else {
print("Could not find launcher, searched in '"+group+"'");
}
}
/** Change client's caption to indicate the shortcut that can be used to access it
* Does not work as of 4.10 because Client.caption is read-only. */
function tagClient(client) {
print("Tagging client: id="+client.windowId+" ; caption="+client.caption+"; resourceClass="+client.resourceClass);
for (someGroup in windowGroups) {
if (windowGroups[someGroup].matchAny(client)) {
shortcut = windowGroupHotkeys[someGroup]; // "Meta+...";
//print("Tag success with "+someGroup+" {"+shortcut+"}");
if (shortcut != undefined) {
client.caption += " {"+shortcut+"}";
}
}
}
}
workspace.clientAdded.connect(tagClient);
add2windowgroup("browser", /chromium-browser/);
add2windowgroup("browser", /firefox/);
add2windowgroup("browser", /opera/);
add2windowgroup("console", /konsole/);
add2windowgroup("desktop", /plasma(|-desktop)/, /plasma-desktop/);
add2windowgroup("editor", /kate/);
add2windowgroup("explorer", /dolphin/);
add2windowgroup("devjava", /eclipse/);
add2windowgroup("devjava", /.*jetbrains.*/); //intellij IDEA, jetbrains-pycharm, ...
//add2windowgroup("devjava", /jetbrains-pycharm/); //temporary
add2windowgroup("devlinux", /kdevelop/);
add2windowgroup("devlinux", /plasma(|-desktop)/, /plasma-desktop|Desktop Shell Scripting Console – Plasma Desktop Shell/); //FIXME
add2windowgroup("devlinux", /plasmate/);
add2windowgroup("music", /amarok/);
add2windowgroup("music", /vlc/);
add2windowgroup("settings", /systemsettings/);
add2windowgroup("settings", /kcmshell4/); //individual modules
add2windowgroup("virtual", /virtualbox/);
add2windowgroup("virtual", /wine/, /.*TeamViewer.*/);
add2windowgroup("writer", /libreoffice-writer/);
add2windowgroup("calc", /libreoffice-calc/);
add2windowgroup("present", /libreoffice-impress/);
add2windowgroup("present", /okular/); //PDFs
add2windowgroup("draw", /libreoffice-draw/);
add2windowgroup("base", /libreoffice-base/);
add2windowgroup("math", /libreoffice-math/);
registerBoth("Meta+F", "browser", "firefox");
registerBoth("Meta+Z", "console", "konsole");
//register ("Meta+D", "desktop"); => See below
registerBoth("Meta+J", "devjava", "dolphin");
registerBoth("Meta+Y", "devlinux", "kdevelop"); //like cYgwin under windows
registerBoth("Meta+E", "explorer", "dolphin");
registerBoth("Meta+N", "editor", "kate");
registerBoth("Meta+/", "music", "amarok"); //TODO vlc ?
registerBoth("Meta+S", "settings", "systemsettings");
registerBoth("Meta+V", "virtual", "virtualbox");
registerBoth("Meta+W", "writer", "libreoffice --writer");
registerBoth("Meta+X", "calc", "libreoffice --calc");
registerBoth("Meta+P", "present", "libreoffice --impress");
registerBoth("Meta+R", "draw", "libreoffice --draw");
registerBoth("Meta+B", "base", "libreoffice --base");
registerBoth("Meta+M", "math", "libreoffice --math");
registerShortcut("Toggle desktop", "", "Meta+D", function() {
workspace.slotToggleShowDesktop();
});
//registerShortcut("Middle-click", "Simulate mouse middle-click", "F1", function() {
//sh -c 'xsel | xvkbd -xsendevent -file - 2>/dev/null'
//sh -c 'xdotool type "`xsel`"'
//>>> qdbus org.kde.klauncher /KLauncher org.kde.KLauncher.exec_blind "sh -c 'xdotool type \"`xsel`\"'"
//1) try with dbus-send convert from a) format to b) format:
// qdbus org.kde.kglobalaccel /kglobalaccel org.kde.KGlobalAccel.action ...
// dbus-send /modules/kdedglobalaccel org.kde.KdedGlobalAccel.invokeAction array:string:"kwin","Snow","KWin","Snow" int64:9999999
//2) use dbus-monitor
//});
// q w e r t y u i o p
// ^ ^ ^ ^ ^ ^ ^ ↖ Presenter
// | | | | | ʟ ? ?
// | | | | cYgwin [win-only]
// | | | Test tool
// | | dRaw
// | Explorer
// Word processor
// a s d f g h j k l
// ^ ^ ^ ^ ^ ^ ↖ LateX
// | | | | | devlinux
// | | | | devJava
// | | internet (x2)
// | Desktop
// Settings
// z x c v b n m , . /
// ^ ^ ^ ^ ^ ↑ ↖ Music !
// | | | | | Mail/Messenger
// | | | | Notepad
// | | | Virtual
// | | Calc [TODO]
// | eXcel
// ʟ console
//[TODO Q U I O A H B]
/* Does not work as of 4.10 because Client.caption is read-only. */
workspace.clientList().forEach(function(client) {
if (client == undefined) continue;
if (! /.+{(Meta|Control|Alt\+[-}]}$)/.test(client.caption)) {
tagClient(client);
}
});
var knownGroups="";
for (kGroup in windowGroups) { knownGroups += " "+kGroup; }
print("+++ Known groups: "+knownGroups);
//print("active is "+workspace.activeClient+" "+workspace.activeClient.caption);
//workspace.activeClient.caption = "Hahahahaha !!!"
//launch("konsole");
//workspace.slotToggleShowDesktop();
//kwinactivate("browser");
//print(""); print("");
//kwinactivate("devlinux");