2015-02-23 20:30:02 +01:00
|
|
|
---------------------------------------------------------------------------
|
2021-12-21 06:54:15 +01:00
|
|
|
-- Display the current keyboard layout name in a widget.
|
|
|
|
--
|
|
|
|
--
|
2015-02-23 20:30:02 +01:00
|
|
|
-- @author Aleksey Fedotov <lexa@cfotr.com>
|
|
|
|
-- @copyright 2015 Aleksey Fedotov
|
2019-06-06 08:15:53 +02:00
|
|
|
-- @widgetmod awful.widget.keyboardlayout
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @supermodule wibox.widget.base
|
2015-02-23 20:30:02 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2015-06-12 10:57:22 +02:00
|
|
|
local capi = {awesome = awesome}
|
2015-02-23 20:30:02 +01:00
|
|
|
local setmetatable = setmetatable
|
|
|
|
local textbox = require("wibox.widget.textbox")
|
|
|
|
local button = require("awful.button")
|
|
|
|
local widget_base = require("wibox.widget.base")
|
2016-10-09 22:36:41 +02:00
|
|
|
local gdebug = require("gears.debug")
|
2015-02-23 20:30:02 +01:00
|
|
|
|
|
|
|
--- Keyboard Layout widget.
|
|
|
|
-- awful.widget.keyboardlayout
|
|
|
|
local keyboardlayout = { mt = {} }
|
|
|
|
|
2015-09-02 10:23:48 +02:00
|
|
|
-- As to the country-code-like symbols below, refer to the names of XKB's
|
|
|
|
-- data files in /.../xkb/symbols/*.
|
|
|
|
keyboardlayout.xkeyboard_country_code = {
|
2015-09-12 17:51:00 +02:00
|
|
|
["ad"] = true, -- Andorra
|
|
|
|
["af"] = true, -- Afganistan
|
|
|
|
["al"] = true, -- Albania
|
|
|
|
["am"] = true, -- Armenia
|
|
|
|
["ara"] = true, -- Arabic
|
|
|
|
["at"] = true, -- Austria
|
|
|
|
["az"] = true, -- Azerbaijan
|
|
|
|
["ba"] = true, -- Bosnia and Herzegovina
|
|
|
|
["bd"] = true, -- Bangladesh
|
|
|
|
["be"] = true, -- Belgium
|
|
|
|
["bg"] = true, -- Bulgaria
|
|
|
|
["br"] = true, -- Brazil
|
|
|
|
["bt"] = true, -- Bhutan
|
|
|
|
["bw"] = true, -- Botswana
|
|
|
|
["by"] = true, -- Belarus
|
|
|
|
["ca"] = true, -- Canada
|
|
|
|
["cd"] = true, -- Congo
|
|
|
|
["ch"] = true, -- Switzerland
|
|
|
|
["cm"] = true, -- Cameroon
|
|
|
|
["cn"] = true, -- China
|
|
|
|
["cz"] = true, -- Czechia
|
|
|
|
["de"] = true, -- Germany
|
|
|
|
["dk"] = true, -- Denmark
|
|
|
|
["ee"] = true, -- Estonia
|
|
|
|
["epo"] = true, -- Esperanto
|
|
|
|
["es"] = true, -- Spain
|
|
|
|
["et"] = true, -- Ethiopia
|
2017-08-17 17:21:40 +02:00
|
|
|
["eu"] = true, -- EurKey
|
2015-09-12 17:51:00 +02:00
|
|
|
["fi"] = true, -- Finland
|
|
|
|
["fo"] = true, -- Faroe Islands
|
|
|
|
["fr"] = true, -- France
|
|
|
|
["gb"] = true, -- United Kingdom
|
|
|
|
["ge"] = true, -- Georgia
|
|
|
|
["gh"] = true, -- Ghana
|
|
|
|
["gn"] = true, -- Guinea
|
|
|
|
["gr"] = true, -- Greece
|
|
|
|
["hr"] = true, -- Croatia
|
|
|
|
["hu"] = true, -- Hungary
|
|
|
|
["ie"] = true, -- Ireland
|
|
|
|
["il"] = true, -- Israel
|
|
|
|
["in"] = true, -- India
|
|
|
|
["iq"] = true, -- Iraq
|
|
|
|
["ir"] = true, -- Iran
|
|
|
|
["is"] = true, -- Iceland
|
|
|
|
["it"] = true, -- Italy
|
|
|
|
["jp"] = true, -- Japan
|
|
|
|
["ke"] = true, -- Kenya
|
|
|
|
["kg"] = true, -- Kyrgyzstan
|
|
|
|
["kh"] = true, -- Cambodia
|
|
|
|
["kr"] = true, -- Korea
|
|
|
|
["kz"] = true, -- Kazakhstan
|
|
|
|
["la"] = true, -- Laos
|
|
|
|
["latam"] = true, -- Latin America
|
|
|
|
["latin"] = true, -- Latin
|
|
|
|
["lk"] = true, -- Sri Lanka
|
|
|
|
["lt"] = true, -- Lithuania
|
|
|
|
["lv"] = true, -- Latvia
|
|
|
|
["ma"] = true, -- Morocco
|
|
|
|
["mao"] = true, -- Maori
|
|
|
|
["me"] = true, -- Montenegro
|
|
|
|
["mk"] = true, -- Macedonia
|
|
|
|
["ml"] = true, -- Mali
|
|
|
|
["mm"] = true, -- Myanmar
|
|
|
|
["mn"] = true, -- Mongolia
|
|
|
|
["mt"] = true, -- Malta
|
|
|
|
["mv"] = true, -- Maldives
|
|
|
|
["ng"] = true, -- Nigeria
|
|
|
|
["nl"] = true, -- Netherlands
|
|
|
|
["no"] = true, -- Norway
|
|
|
|
["np"] = true, -- Nepal
|
|
|
|
["ph"] = true, -- Philippines
|
|
|
|
["pk"] = true, -- Pakistan
|
|
|
|
["pl"] = true, -- Poland
|
|
|
|
["pt"] = true, -- Portugal
|
|
|
|
["ro"] = true, -- Romania
|
|
|
|
["rs"] = true, -- Serbia
|
|
|
|
["ru"] = true, -- Russia
|
|
|
|
["se"] = true, -- Sweden
|
|
|
|
["si"] = true, -- Slovenia
|
|
|
|
["sk"] = true, -- Slovakia
|
|
|
|
["sn"] = true, -- Senegal
|
|
|
|
["sy"] = true, -- Syria
|
|
|
|
["th"] = true, -- Thailand
|
|
|
|
["tj"] = true, -- Tajikistan
|
|
|
|
["tm"] = true, -- Turkmenistan
|
|
|
|
["tr"] = true, -- Turkey
|
|
|
|
["tw"] = true, -- Taiwan
|
|
|
|
["tz"] = true, -- Tanzania
|
|
|
|
["ua"] = true, -- Ukraine
|
|
|
|
["us"] = true, -- USA
|
|
|
|
["uz"] = true, -- Uzbekistan
|
|
|
|
["vn"] = true, -- Vietnam
|
|
|
|
["za"] = true, -- South Africa
|
2015-09-02 10:23:48 +02:00
|
|
|
}
|
|
|
|
|
2016-04-04 20:28:54 +02:00
|
|
|
-- Callback for updating current layout.
|
2015-09-02 10:23:48 +02:00
|
|
|
local function update_status (self)
|
2017-07-25 18:17:32 +02:00
|
|
|
self._current = awesome.xkb_get_layout_group()
|
2015-09-12 17:51:00 +02:00
|
|
|
local text = ""
|
2017-07-25 18:17:32 +02:00
|
|
|
if #self._layout > 0 then
|
2017-05-13 23:25:09 +02:00
|
|
|
-- Please note that the group number reported by xkb_get_layout_group
|
|
|
|
-- is lower by one than the group numbers reported by xkb_get_group_names.
|
2017-07-25 18:17:32 +02:00
|
|
|
local name = self._layout[self._current+1]
|
|
|
|
if name then
|
|
|
|
text = " " .. name .. " "
|
|
|
|
end
|
2015-09-12 17:51:00 +02:00
|
|
|
end
|
|
|
|
self.widget:set_text(text)
|
2015-09-02 10:23:48 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
--- Auxiliary function for the local function update_layout().
|
|
|
|
-- Create an array whose element is a table consisting of the four fields:
|
|
|
|
-- vendor, file, section and group_idx, which all correspond to the
|
|
|
|
-- xkb_symbols pattern "vendor/file(section):group_idx".
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam string group_names The string `awesome.xkb_get_group_names()` returns.
|
2015-09-02 10:23:48 +02:00
|
|
|
-- @treturn table An array of tables whose keys are vendor, file, section, and group_idx.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct awful.keyboardlayout.get_groups_from_group_names
|
2015-09-02 10:23:48 +02:00
|
|
|
function keyboardlayout.get_groups_from_group_names(group_names)
|
2015-09-12 17:51:00 +02:00
|
|
|
if group_names == nil then
|
|
|
|
return nil
|
|
|
|
end
|
2015-09-02 10:23:48 +02:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
-- Pattern elements to be captured.
|
|
|
|
local word_pat = "([%w_]+)"
|
|
|
|
local sec_pat = "(%b())"
|
|
|
|
local idx_pat = ":(%d)"
|
|
|
|
-- Pairs of a pattern and its callback. In callbacks, set 'group_idx' to 1
|
|
|
|
-- and return it if there's no specification on 'group_idx' in the given
|
|
|
|
-- pattern.
|
|
|
|
local pattern_and_callback_pairs = {
|
|
|
|
-- vendor/file(section):group_idx
|
|
|
|
["^" .. word_pat .. "/" .. word_pat .. sec_pat .. idx_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local vendor, file, section, group_idx = string.match(token, pattern)
|
|
|
|
return vendor, file, section, group_idx
|
|
|
|
end,
|
|
|
|
-- vendor/file(section)
|
|
|
|
["^" .. word_pat .. "/" .. word_pat .. sec_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local vendor, file, section = string.match(token, pattern)
|
|
|
|
return vendor, file, section, 1
|
|
|
|
end,
|
|
|
|
-- vendor/file:group_idx
|
|
|
|
["^" .. word_pat .. "/" .. word_pat .. idx_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local vendor, file, group_idx = string.match(token, pattern)
|
|
|
|
return vendor, file, nil, group_idx
|
|
|
|
end,
|
|
|
|
-- vendor/file
|
|
|
|
["^" .. word_pat .. "/" .. word_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local vendor, file = string.match(token, pattern)
|
|
|
|
return vendor, file, nil, 1
|
|
|
|
end,
|
|
|
|
-- file(section):group_idx
|
|
|
|
["^" .. word_pat .. sec_pat .. idx_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local file, section, group_idx = string.match(token, pattern)
|
|
|
|
return nil, file, section, group_idx
|
|
|
|
end,
|
|
|
|
-- file(section)
|
|
|
|
["^" .. word_pat .. sec_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local file, section = string.match(token, pattern)
|
|
|
|
return nil, file, section, 1
|
|
|
|
end,
|
|
|
|
-- file:group_idx
|
|
|
|
["^" .. word_pat .. idx_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local file, group_idx = string.match(token, pattern)
|
|
|
|
return nil, file, nil, group_idx
|
|
|
|
end,
|
|
|
|
-- file
|
|
|
|
["^" .. word_pat .. "$"]
|
|
|
|
= function(token, pattern)
|
|
|
|
local file = string.match(token, pattern)
|
|
|
|
return nil, file, nil, 1
|
|
|
|
end
|
|
|
|
}
|
2015-09-02 10:23:48 +02:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
-- Split 'group_names' into 'tokens'. The separator is "+".
|
|
|
|
local tokens = {}
|
|
|
|
string.gsub(group_names, "[^+]+", function(match)
|
|
|
|
table.insert(tokens, match)
|
|
|
|
end)
|
2015-09-02 10:23:48 +02:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
-- For each token in 'tokens', check if it matches one of the patterns in
|
|
|
|
-- the array 'pattern_and_callback_pairs', where the patterns are used as
|
|
|
|
-- key. If a match is found, extract captured strings using the
|
|
|
|
-- corresponding callback function. Check if those extracted is country
|
|
|
|
-- specific part of a layout. If so, add it to 'layout_groups'; otherwise,
|
|
|
|
-- ignore it.
|
|
|
|
local layout_groups = {}
|
|
|
|
for i = 1, #tokens do
|
|
|
|
for pattern, callback in pairs(pattern_and_callback_pairs) do
|
|
|
|
local vendor, file, section, group_idx = callback(tokens[i], pattern)
|
|
|
|
if file then
|
|
|
|
if not keyboardlayout.xkeyboard_country_code[file] then
|
|
|
|
break
|
|
|
|
end
|
2015-09-02 10:23:48 +02:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
if section then
|
2016-03-30 14:32:21 +02:00
|
|
|
section = string.gsub(section, "%(([%w-_]+)%)", "%1")
|
2015-09-12 17:51:00 +02:00
|
|
|
end
|
2015-09-02 10:23:48 +02:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
table.insert(layout_groups, { vendor = vendor,
|
|
|
|
file = file,
|
|
|
|
section = section,
|
|
|
|
group_idx = tonumber(group_idx) })
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-09-02 10:23:48 +02:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
return layout_groups
|
2015-02-23 20:30:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Callback for updating list of layouts
|
2015-09-02 10:23:48 +02:00
|
|
|
local function update_layout(self)
|
2015-09-12 17:51:00 +02:00
|
|
|
self._layout = {};
|
|
|
|
local layouts = keyboardlayout.get_groups_from_group_names(awesome.xkb_get_group_names())
|
|
|
|
if layouts == nil or layouts[1] == nil then
|
2016-10-09 22:36:41 +02:00
|
|
|
gdebug.print_error("Failed to get list of keyboard groups")
|
|
|
|
return
|
2015-09-12 17:51:00 +02:00
|
|
|
end
|
|
|
|
if #layouts == 1 then
|
2017-05-13 23:25:09 +02:00
|
|
|
layouts[1].group_idx = 1
|
2015-09-12 17:51:00 +02:00
|
|
|
end
|
|
|
|
for _, v in ipairs(layouts) do
|
2017-05-13 23:25:09 +02:00
|
|
|
self._layout[v.group_idx] = self.layout_name(v)
|
2015-09-12 17:51:00 +02:00
|
|
|
end
|
|
|
|
update_status(self)
|
2015-02-23 20:30:02 +01:00
|
|
|
end
|
|
|
|
|
2020-03-01 01:37:19 +01:00
|
|
|
--- Select the next layout.
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2020-03-01 01:37:19 +01:00
|
|
|
-- @method next_layout
|
|
|
|
|
2019-06-07 20:59:34 +02:00
|
|
|
--- Create a keyboard layout widget.
|
|
|
|
--
|
|
|
|
-- It shows current keyboard layout name in a textbox.
|
|
|
|
--
|
|
|
|
-- @constructorfct awful.widget.keyboardlayout
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @treturn awful.widget.keyboardlayout A keyboard layout widget.
|
2015-02-23 20:30:02 +01:00
|
|
|
function keyboardlayout.new()
|
2015-09-12 17:51:00 +02:00
|
|
|
local widget = textbox()
|
2020-03-01 00:54:52 +01:00
|
|
|
local self = widget_base.make_widget(widget, nil, {enable_properties=true})
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
self.widget = widget
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2016-03-30 14:37:18 +02:00
|
|
|
self.layout_name = function(v)
|
|
|
|
local name = v.file
|
|
|
|
if v.section ~= nil then
|
|
|
|
name = name .. "(" .. v.section .. ")"
|
|
|
|
end
|
|
|
|
return name
|
|
|
|
end
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
self.next_layout = function()
|
2016-02-07 15:12:22 +01:00
|
|
|
self.set_layout((self._current + 1) % (#self._layout + 1))
|
2015-09-12 17:51:00 +02:00
|
|
|
end
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
self.set_layout = function(group_number)
|
|
|
|
if (0 > group_number) or (group_number > #self._layout) then
|
|
|
|
error("Invalid group number: " .. group_number ..
|
|
|
|
"expected number from 0 to " .. #self._layout)
|
|
|
|
return;
|
|
|
|
end
|
|
|
|
awesome.xkb_set_layout_group(group_number);
|
|
|
|
end
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2016-03-30 14:37:18 +02:00
|
|
|
update_layout(self);
|
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
-- callback for processing layout changes
|
|
|
|
capi.awesome.connect_signal("xkb::map_changed",
|
|
|
|
function () update_layout(self) end)
|
|
|
|
capi.awesome.connect_signal("xkb::group_changed",
|
|
|
|
function () update_status(self) end);
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
-- Mouse bindings
|
2018-12-28 09:06:03 +01:00
|
|
|
self.buttons = {
|
|
|
|
button({ }, 1, self.next_layout)
|
|
|
|
}
|
2015-02-23 20:30:02 +01:00
|
|
|
|
2015-09-12 17:51:00 +02:00
|
|
|
return self
|
2015-02-23 20:30:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local _instance = nil;
|
|
|
|
|
|
|
|
function keyboardlayout.mt:__call(...)
|
2015-09-12 17:51:00 +02:00
|
|
|
if _instance == nil then
|
|
|
|
_instance = keyboardlayout.new(...)
|
|
|
|
end
|
|
|
|
return _instance
|
2015-02-23 20:30:02 +01:00
|
|
|
end
|
|
|
|
|
2019-12-22 06:53:57 +01:00
|
|
|
|
2015-02-23 20:30:02 +01:00
|
|
|
return setmetatable(keyboardlayout, keyboardlayout.mt)
|
|
|
|
|
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|