Keyboard layouts with kbdd

From awesome
Jump to: navigation, search

Awesome 3 and kbdd

kbdd stands for keyboard daemon. It is a simple daemon, which is designed to be run in X11 session and remember keyboard layouts on a per-window basis. That's very useful if don't want to switch layouts back and forth, while typing in terminals (probably mostly in English layouts) and some kind of chat (Greek, for example). Another useful thing about it is D-Bus notification support (optional) — it can emit signals on layout change, thus making it possible to create a indicator widget.

First, head over to kbdd homepage and install latest version (chances to find it in repos are low). Gentoo users are advised to use rion overlay.

Make sure you've configured your Xorg server properly, kbdd uses its settings. Run kbdd executable and behold its unbelievable greatness in layout remembering!

Next thing is to create Awesome widget:

-- Keyboard layout widget
kbdwidget = widget({type = "textbox", name = "kbdwidget"})
kbdwidget.border_width = 1
kbdwidget.border_color = beautiful.fg_normal
kbdwidget.text = " Eng "

…place it where you want and make it listen to D-Bus events:

dbus.request_name("session", "ru.gentoo.kbdd")
dbus.add_match("session", "interface='ru.gentoo.kbdd',member='layoutChanged'")
dbus.add_signal("ru.gentoo.kbdd", function(...)
    local data = {...}
    local layout = data[2]
    lts = {[0] = "Eng", [1] = "Рус"}
    kbdwidget.text = " "..lts[layout].." "
    end
)

In this example the first layout (numbered "0") is English and therefore indicated as "Eng", and the second one is Russian ("1", "Рус"). Adjust it as you need: you could play with background and foreground colours, display images instead of boring letters and so on.

That's simplest example. See kbdd wiki and manpage for details regarding D-Bus methods and other features.

Advanced layout managing and prettier widget

Here's much more complex example of kbdd-powered widget with background images and layout selection menu written by Mellon.

* Left mouse click switches two most recent layouts.
* Middle click switches to next layout.
* Right click opens menu.

Widget state is changed by kbdd D-BUS signals, layouts are switched by calling kbdd D-BUS methods. Code is subject to changes, so look into example here and make sure you've checked most recent version at GoogleCode.

Mellon kbdd.png

kbd.lua

-- {{{ Variable definitions
kbd_dbus_sw_cmd = "qdbus ru.gentoo.KbddService /ru/gentoo/KbddService  ru.gentoo.kbdd.set_layout "
-- kbd_dbus_sw_cmd = "dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.set_layout uint32:"
kbd_dbus_prev_cmd = "qdbus ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.prev_layout"
-- kbd_dbus_prev_cmd = "dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.prev_layout"
kbd_dbus_next_cmd = "qdbus ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.next_layout"
-- kbd_dbus_next_cmd = "dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.next_layout"
kbd_img_path = "/usr/share/icons/kbflags/"
-- }}}

-- {{{ Keyboard layout widgets
--- Create the menu
kbdmenu =awful.menu({ items = {  { "English", kbd_dbus_sw_cmd .. "0",  kbd_img_path .. "us.png" },
	{ "Русский", kbd_dbus_sw_cmd .. "1", kbd_img_path .. "ru.png" },
	{ "Hebrew", kbd_dbus_sw_cmd .. "2", kbd_img_path .. "il.png" },
	{ "Deutsch", kbd_dbus_sw_cmd .. "3", kbd_img_path .. "de.png" }
	}
})

-- Create simple text widget
kbdwidget = widget({type = "textbox", name = "kbdwidget"})
-- kbdwidget.border_width = 1
-- kbdwidget.border_color = beautiful.fg_normal
kbdwidget.align="center"
kbdwidget.text = "<b>Eng</b>"
kbdwidget.bg_image = image (kbd_img_path .. "us.png")
kbdwidget.bg_align = "center"
kbdwidget.bg_resize = true
awful.widget.layout.margins[kbdwidget] = { left = 0, right = 10 }
kbdwidget:buttons(awful.util.table.join(
	awful.button({ }, 1, function() os.execute(kbd_dbus_prev_cmd) end),
	awful.button({ }, 2, function() os.execute(kbd_dbus_next_cmd) end),
	awful.button({ }, 3, function() kbdmenu:toggle () end)
))
-- }}}

-- {{{ Signals
dbus.request_name("session", "ru.gentoo.kbdd")
dbus.add_match("session", "interface='ru.gentoo.kbdd',member='layoutChanged'")
dbus.add_signal("ru.gentoo.kbdd", function(...)
	local data = {...}
	local layout = data[2]
	lts = {[0] = "Eng", [1] = "Рус", [2] = "Heb", [3] = "Deu"}
	lts_img = {[0] = kbd_img_path .. "us.png", [1] = kbd_img_path .. "ru.png", [2] = kbd_img_path .. "il.png", [3] = kbd_img_path .. "de.png" }
	kbdwidget.text = "<b>"..lts[layout].."</b>"
	kbdwidget.bg_image = image(lts_img[layout])
	end)
-- }}}

rc.lua

-- {{{ Load user widgets
dofile(awful.util.getdir("config") .. "/kbd.lua")
-- }}}

    -- Add widgets to the wibox - order matters
    mywibox[s].widgets = {
--     ...
        s == 1 and kbdwidget or nil,
--     ...
    }

-- My keybindings
globalkeys = awful.util.table.join(globalkeys,
    awful.key({ modkey, "Mod1"    }, "1",     function () os.execute(kbd_dbus_sw_cmd .. "0") end),
    awful.key({ modkey, "Mod1"    }, "2",     function () os.execute(kbd_dbus_sw_cmd .. "1") end),
    awful.key({ modkey, "Mod1"    }, "3",     function () os.execute(kbd_dbus_sw_cmd .. "2") end),
    awful.key({ modkey, "Mod1"    }, "4",     function () os.execute(kbd_dbus_sw_cmd .. "3") end),
    awful.key({  "Control"  }, "ISO_Level3_Shift",     function () os.execute(kbd_dbus_prev_cmd) end)
)

Icons used are translatoid's (translator plasmoid using google translator).

Personal tools