-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyakuake.go
412 lines (370 loc) · 13.3 KB
/
yakuake.go
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* yakctl - control the yakuake terminal
*
* 2020 emschu https://github.com/emschu/yakctl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"github.com/gookit/color"
"os/exec"
"strconv"
"strings"
"time"
)
// remember: a dbus cmd consists of service + path + interface method
const (
DbusApp = "qdbus"
DbusService = "org.kde.yakuake"
// paths
DbusPathSessions = "/yakuake/sessions"
DbusPathTabs = "/yakuake/tabs"
DbusPathWindow = "/yakuake/window"
DbusPathMainwindow = "/yakuake/MainWindow_1"
// methods for paths = sessions
DbusMethodAddSession = "org.kde.yakuake.addSession"
DbusMethodAddSessionLr = "org.kde.yakuake.addSessionTwoHorizontal"
DbusMethodAddSessionTb = "org.kde.yakuake.addSessionTwoVertical"
DbusMethodAddSessionQu = "org.kde.yakuake.addSessionQuad"
DbusMethodSetSessionMonitorSilence = "org.kde.yakuake.setSessionMonitorSilenceEnabled"
DbusMethodSetSessionMonitorActivity = "org.kde.yakuake.setSessionMonitorActivityEnabled"
DbusMethodSetKeyboardInputEnabled = "org.kde.yakuake.setSessionKeyboardInputEnabled"
DbusMethodTerminalIDList = "org.kde.yakuake.terminalIdList"
DbusMethodTerminalIDListForSessionID = "org.kde.yakuake.terminalIdsForSessionId"
DbusMethodTerminalRemoval = "org.kde.yakuake.removeTerminal"
DbusMethodSessionIDForTerminalID = "org.kde.yakuake.sessionIdForTerminalId"
DbusMethodSessionIDList = "org.kde.yakuake.sessionIdList"
DbusMethodIsSessionClosable = "org.kde.yakuake.isSessionClosable"
DbusMethodSetSessionClosable = "org.kde.yakuake.setSessionClosable"
DbusMethodRunCommandInTerminal = "org.kde.yakuake.runCommandInTerminal"
DbusMethodActiveSessionId = "org.kde.yakuake.activeSessionId"
// methods for paths = tabs
DbusMethodTabTitle = "org.kde.yakuake.tabTitle"
DbusMethodSetTabTitle = "org.kde.yakuake.setTabTitle"
// methods for path = window
DbusMethodToggleState = "org.kde.yakuake.toggleWindowState"
// methods for path = MainWindow_1
DbusMethodQwidgetVisible = "org.qtproject.Qt.QWidget.visible"
DbusMethodPing = "org.freedesktop.DBus.Peer.Ping"
)
// LoadSession method to load a yakuake session defined in yaml configuration
func LoadSession(configuration *YakCtlConfiguration, profileID int64) error {
profile, err := GetProfile(configuration, profileID)
if err != nil {
return err
}
currentlyOpenedSessionID := getCurrentSessionId()
// store these ids for later to avoid killing the shell we possibly run in
openedTerminalsBeforeLoad, termErrs := getAllTerminalIDs()
if termErrs != nil {
return termErrs
}
for _, tab := range profile.Tabs {
sessionID := startSession(&tab)
if sessionID == nil || len(*sessionID) == 0 {
return fmt.Errorf("problem creating session for new tab")
}
// set title
executeCmdVoid(DbusPathTabs, DbusMethodSetTabTitle, *sessionID, tab.Name)
// get terminal ids of session
terminalIDs := getTerminalIDsForSessionID(sessionID)
// commands are executed on each terminal, before the specific stuff commands will be executed
for _, command := range tab.Commands {
for _, terminalID := range terminalIDs {
executeCommandInTerminal(command, terminalID)
}
}
// handle different terminals
if len(terminalIDs) > 0 {
for _, t1Cmd := range tab.Terminal1 {
executeCommandInTerminal(t1Cmd, terminalIDs[0])
}
}
if len(terminalIDs) > 1 {
for _, t2Cmd := range tab.Terminal2 {
executeCommandInTerminal(t2Cmd, terminalIDs[1])
}
}
if len(terminalIDs) > 2 {
for _, t3Cmd := range tab.Terminal3 {
executeCommandInTerminal(t3Cmd, terminalIDs[2])
}
}
if len(terminalIDs) > 3 {
for _, t4Cmd := range tab.Terminal4 {
executeCommandInTerminal(t4Cmd, terminalIDs[3])
}
}
// handle flags
if tab.Protected {
executeCmdVoid(DbusPathSessions, DbusMethodSetSessionClosable, *sessionID, "false")
}
if tab.MonitorSilence {
executeCmdVoid(DbusPathSessions, DbusMethodSetSessionMonitorSilence, *sessionID, "true")
}
if tab.MonitorActivity {
executeCmdVoid(DbusPathSessions, DbusMethodSetSessionMonitorActivity, *sessionID, "true")
}
if tab.DisableKeyboardInput {
executeCmdVoid(DbusPathSessions, DbusMethodSetKeyboardInputEnabled, *sessionID, "false")
}
color.Success.Printf("Created new session #%s\n", *sessionID)
}
// toggle window
if !isWindowShown() {
// toggle window
executeCmdVoid(DbusPathWindow, DbusMethodToggleState)
}
// clean up
if profile.ClearAll {
clearSessions(profile.ForceClear, openedTerminalsBeforeLoad, ¤tlyOpenedSessionID)
}
return nil
}
// ClearSession method to reset yakuake
func ClearSession(forceDeletion bool) {
// get all terminal Ids and remove them afterwards
var err error
terminalIDs, err := getAllTerminalIDs()
if err != nil {
color.Error.Printf("%v.\n", err)
return
}
clearSessions(forceDeletion, terminalIDs, nil)
}
// ExecuteCommand method to execute a command in all or in specified terminals
func ExecuteCommand(command string, affectedTerminals *[]string) {
if len(*affectedTerminals) == 0 {
var err error
*affectedTerminals, err = getAllTerminalIDs()
if err != nil {
color.Error.Printf("%v\n", err)
return
}
}
for _, tID := range *affectedTerminals {
executeCommandInTerminal(command, tID)
}
}
// ShowStatus show sessions and terminals of the current yakuake instance
func ShowStatus() {
sessionIDs, err := getAllSessionIDs()
if err != nil {
color.Errorf("%v\n", sessionIDs)
return
}
for _, sessionID := range sessionIDs {
tabTitle := getTitleOfSession(sessionID)
color.Info.Printf("- session #%s, tab title: %s\n", sessionID, *tabTitle)
terminalIds := getTerminalIDsForSessionID(&sessionID)
for _, terminalID := range terminalIds {
color.Info.Printf("\t|- Terminal #%s\n", terminalID)
}
}
}
// clear the specified terminals
func clearSessions(forceDeletion bool, terminalIDs []string, lastSessionId *string) {
var currentlyActiveSessionID string
if lastSessionId == nil {
currentlyActiveSessionID = getCurrentSessionId()
} else {
currentlyActiveSessionID = *lastSessionId
}
if len(terminalIDs) == 0 {
color.Info.Printf("Found NO open terminal\n")
} else if len(terminalIDs) == 1 {
color.Info.Printf("Found one open terminal that will be tried to close\n")
} else {
color.Info.Printf("Found %d open terminals that will be tried to close\n", len(terminalIDs))
}
didSomething := false
if forceDeletion {
color.Warn.Printf("Closing of tabs will be forced!\n")
}
// strip currently active shell from the terminal id slice
// split the slice into now and postponed
var isCurrentTerminalPostponed = false
var cleanedUpTerminalIDList = terminalIDs
var postponedTerminalIDs []string
for i, tID := range terminalIDs {
sessionIDOfTerminal := getSessionIDForTerminalID(tID)
if sessionIDOfTerminal == currentlyActiveSessionID {
cleanedUpTerminalIDList = append(terminalIDs[:i], terminalIDs[i+1:]...)
isCurrentTerminalPostponed = true
postponedTerminalIDs = append(postponedTerminalIDs, tID)
}
}
processTerminalRemoval(&forceDeletion, &cleanedUpTerminalIDList, &didSomething)
// remove the currently opened terminal at the end
if isCurrentTerminalPostponed {
processTerminalRemoval(&forceDeletion, &postponedTerminalIDs, &didSomething)
}
if didSomething {
color.Success.Println("All sessions cleared!")
}
}
func getCurrentSessionId() string {
currentlyActiveSessionID, activeSessionIDErr := executeCmd(DbusPathSessions, DbusMethodActiveSessionId)
if activeSessionIDErr != nil {
color.Errorf("problem fetching current active session id")
}
if currentlyActiveSessionID == "-1" {
currentlyActiveSessionID = ""
}
return currentlyActiveSessionID
}
func processTerminalRemoval(forceDeletion *bool, terminalIDs *[]string, didSomething *bool) {
for _, tID := range *terminalIDs {
closable, title := isTerminalClosable(tID)
if !closable && !*forceDeletion {
color.Warn.Printf("Terminal #%s ('%s') is protected and not closable. Do it manually!\n", tID, *title)
}
sessionID := getSessionIDForTerminalID(tID)
if *forceDeletion {
// set closable by dbus command
for _, id := range strings.Split(sessionID, ",") {
executeCmdVoid(DbusPathSessions, DbusMethodSetSessionClosable, id, "true")
time.Sleep(10 * time.Millisecond)
}
}
if !closable && !*forceDeletion {
continue
}
tabTitle := getTitleOfSession(sessionID)
_, terminalRemovalErr := executeCmd(DbusPathSessions, DbusMethodTerminalRemoval, tID)
if terminalRemovalErr != nil {
color.Warn.Printf("Terminal with terminalId #%s can't be removed! %v\n", tID, terminalRemovalErr)
} else {
*didSomething = true
color.Info.Printf("Closing terminal #%s with session #%s and title '%s'\n", tID, sessionID, *tabTitle)
}
}
}
// method to get terminal_ids of all open sessions
func getAllTerminalIDs() ([]string, error) {
terminalIDOutput, err := executeCmd(DbusPathSessions, DbusMethodTerminalIDList)
if err != nil {
color.Error.Printf("Problem fetching terminalIDs of yakuake\n")
return nil, err
}
var terminalIDs = strings.Split(terminalIDOutput, ",")
return terminalIDs, nil
}
// get all session ids currently open
func getAllSessionIDs() ([]string, error) {
sessionIDOutput, err := executeCmd(DbusPathSessions, DbusMethodSessionIDList)
if err != nil {
color.Error.Printf("Problem fetching sessionIDs of yakuake\n")
return nil, err
}
var sessionIDs = strings.Split(sessionIDOutput, ",")
return sessionIDs, nil
}
// get terminal ids of a single sessions id
func getTerminalIDsForSessionID(sessionID *string) []string {
terminalIDOutput, _ := executeCmd(DbusPathSessions, DbusMethodTerminalIDListForSessionID, *sessionID)
terminalIDs := strings.Split(terminalIDOutput, ",")
return terminalIDs
}
// checks if yakuake window is shown
func isWindowShown() bool {
isShownOutput, visibleErr := executeCmd(DbusPathMainwindow, DbusMethodQwidgetVisible)
if visibleErr != nil {
color.Error.Printf("Problem fetching open state of yakuake window. %v.\n", visibleErr)
return true
}
parseBool, err := strconv.ParseBool(isShownOutput)
if err != nil {
color.Error.Printf("Problem parsing opening state of yakuake window. Value: '%s'. %v\n", isShownOutput, visibleErr)
return false
}
return parseBool
}
// wrapper method to execute a command in a specific terminal
func executeCommandInTerminal(command string, terminalID string) {
color.Info.Printf("Execute command '%s' in terminal #%s\n", command, terminalID)
executeCmdVoid(DbusPathSessions, DbusMethodRunCommandInTerminal, terminalID, command)
}
// start new session (open a new tab) depending on split settings of this tab
func startSession(tab *TabDescription) *string {
switch strings.ToLower(tab.SplitMode) {
case "left-right", "horizontal", "lr":
sessionID, _ := executeCmd(DbusPathSessions, DbusMethodAddSessionLr)
return &sessionID
case "top-bottom", "vertical", "tb":
sessionID, _ := executeCmd(DbusPathSessions, DbusMethodAddSessionTb)
return &sessionID
case "quad", "qu":
sessionID, _ := executeCmd(DbusPathSessions, DbusMethodAddSessionQu)
return &sessionID
default:
// open single session
sessionID, _ := executeCmd(DbusPathSessions, DbusMethodAddSession)
return &sessionID
}
}
// get tab title by session's id
func getTitleOfSession(sessionID string) *string {
titleOutput, _ := executeCmd(DbusPathTabs, DbusMethodTabTitle, sessionID)
return &titleOutput
}
// check if terminal and its sessions can be closed by this tool
func isTerminalClosable(terminalID string) (bool, *string) {
sessionsIDOutput := getSessionIDForTerminalID(terminalID)
sessionIDs := strings.Split(sessionsIDOutput, ",")
for _, sessionID := range sessionIDs {
title := getTitleOfSession(sessionID)
out, err := executeCmd(DbusPathSessions, DbusMethodIsSessionClosable, sessionID)
if err != nil {
color.Error.Printf("Error fetching closable information of session '%s'\n", sessionID)
return false, title
}
isClosable, _ := strconv.ParseBool(out)
if !isClosable {
return false, title
}
}
return true, nil
}
// get session id by terminal id
func getSessionIDForTerminalID(terminalID string) string {
output, err := executeCmd(DbusPathSessions, DbusMethodSessionIDForTerminalID, terminalID)
if err != nil {
color.Error.Printf("Error %v\n", err)
return ""
}
return output
}
// method wrapper to execute all the dbus commands
func executeCmd(args ...string) (string, error) {
arguments := append([]string{DbusService}, args...)
cmd := exec.Command(DbusApp, arguments...)
out, err := cmd.Output()
if err != nil {
color.Error.Println(err.Error())
return "", err
}
return strings.Trim(string(out), "\n"), nil
}
// execute dbus cmd without using the output, errors are printed
func executeCmdVoid(args ...string) {
_, err := executeCmd(args...)
if err != nil {
color.Warn.Println(err.Error())
return
}
return
}