Add keyboard_layout_switcher program, use it for click action on waybar and as bindsym.

This commit is contained in:
Érico Rolim 2019-09-09 13:42:13 -03:00
parent 669843b798
commit e335d0ad6d
5 changed files with 112 additions and 13 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*~
*/lnch
*/keyboard_layout_switcher

View File

@ -1,8 +1,18 @@
.PHONY: all lnch
BUILD_DIR = ../script/.local/bin
all: lnch
GOSRC = $(wildcard *.go)
GOTARGET = $(GOSRC:%.go=$(BUILD_DIR)/%)
lnch: ../script/.local/bin/lnch
TARGETS = $(GOTARGET)
../script/.local/bin/%: %.go
.PHONY: all go clean
all: go
go: $(GOTARGET)
$(BUILD_DIR)/%: %.go
go build -o $@ $<
clean:
rm -f $(TARGETS)

View File

@ -0,0 +1,80 @@
package main
import (
"fmt"
"strings"
"strconv"
"encoding/json"
"os/exec"
"log"
)
func main() {
// collecting the JSON output from swaymsg
cmd_in := exec.Command("swaymsg", "--raw", "--type", "get_inputs")
stdout, err := cmd_in.Output()
if err != nil {
log.Fatal(err)
}
// struct that represents the Input
type Input struct {
Identifier, Name string
Vendor, Product int
Type string
Xkb_Layout_Names []string
Xkb_Active_Layout_Index int
Xkb_Active_Layout_Name string
}
// JSON decoder and getting the first token ('[')
dec := json.NewDecoder(strings.NewReader(string(stdout)))
_, err = dec.Token()
if err != nil {
log.Fatal(err)
}
// filtering amongst the keeyboards for the one with the most layouts setup
max_length := 0
var keyboard_input Input
for dec.More() {
var input Input
err := dec.Decode(&input)
if err != nil {
log.Fatal(err)
}
length := len(input.Xkb_Layout_Names)
if input.Type == "keyboard" && length > max_length {
max_length = length
keyboard_input = input
}
}
// finding the index of the input
var current_index int
for i, layout := range keyboard_input.Xkb_Layout_Names {
if keyboard_input.Xkb_Active_Layout_Name == layout {
current_index = i
break
}
}
// setting the next index of the output
var next_index int
if current_index < (len(keyboard_input.Xkb_Layout_Names) - 1) {
next_index = current_index + 1
} else {
next_index = 0
}
// setting the new layout index
cmd_out := exec.Command("swaymsg", "input", keyboard_input.Identifier, "xkb_switch_layout", strconv.Itoa(next_index))
_, err = cmd_out.Output()
if err != nil {
log.Fatal(err)
} else {
fmt.Printf("%v switched to layout %v successfully.\n", keyboard_input.Name, keyboard_input.Xkb_Layout_Names[next_index])
}
}

View File

@ -42,8 +42,8 @@ bindsym $mod+Shift+e exec swaynag -t warning -m 'You pressed the exit shortcut.
set $keyboard "1:1:AT_Translated_Set_2_keyboard"
input $keyboard {
xkb_layout "us,br"
xkb_options grp:win_space_toggle
}
bindsym $mod+Space exec keyboard_layout_switcher
# Touchpad
set $touchpad "1739:31251:SYNA2393:00_06CB:7A13_Touchpad"
input $touchpad {
@ -72,7 +72,7 @@ bindsym $mod+Shift+f exec dolphin
bindsym $mod+Shift+q kill
# Start launcher
bindsym $mod+d exec $menu
for_window [title="launcher"] floating enable; border none
for_window [title="launcher"] focus, floating enable, border none
# Drag floating windows by holding down $mod and left mouse button.
# Resize them with right mouse button + $mod.
# Despite the name, also works for non-floating windows.
@ -80,6 +80,13 @@ for_window [title="launcher"] floating enable; border none
# mouse button for dragging.
floating_modifier $mod normal
## Enable further services for complete session
# Polkit authenticator
exec /usr/lib/polkit-kde-authentication-agent-1
for_window [app_id="polkit"] focus, floating enable, border pixel 4
# Notifications daemon
exec mako
## Appearance
# Default wallpaper
output * bg /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill
@ -216,18 +223,18 @@ for_window [app_id="waybar" floating] {
## Pre-configured workspaces
# Music
exec swaymsg 'workspace media; layout tabbed'
for_window [class="spotify"] move container to workspace $wsm
for_window [app_id="spotify"] move container to workspace $wsm
# Work
exec swaymsg 'workspace nth; layout tabbed'
assign [class="Kicad"] $wsn
assign [app_id="Kicad"] $wsn
assign [title="CubeMX"] $wsn
# PDF
exec swaymsg 'workspace pdf; layout tabbed'
assign [class="okular"] $wsp
assign [app_id="okular"] $wsp
# Return to first workspace
exec swaymsg 'workspace 1;'
## Application specific settings
for_window [class="krunner"] floating enable;
for_window [class="konsole"] floating enable;
for_window [class="pavucontrol"] floating enable;
for_window [app_id="krunner"] floating enable
for_window [app_id="konsole"] floating enable
for_window [app_id="pavucontrol"] focus, floating enable

View File

@ -145,6 +145,7 @@
},
"custom/keyboard_layout": {
"format": "🎹 {}", // piano keyboard. Normal keyboard: ⌨️
"exec": "keyboard_layout_monitor"
"exec": "keyboard_layout_monitor",
"on-click": "keyboard_layout_switcher"
}
}