summaryrefslogtreecommitdiff
path: root/awesome/lib/awful/widget/keyboardlayout.lua
blob: 4f79a81e38f8ef672cbbebce73cc3981b8aa8891 (plain)
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
---------------------------------------------------------------------------
-- @author Aleksey Fedotov <lexa@cfotr.com>
-- @copyright 2015 Aleksey Fedotov
-- @classmod wibox.widget.keyboardlayout
---------------------------------------------------------------------------

local capi = {awesome = awesome}
local setmetatable = setmetatable
local textbox = require("wibox.widget.textbox")
local button = require("awful.button")
local util = require("awful.util")
local widget_base = require("wibox.widget.base")
local gdebug = require("gears.debug")

--- Keyboard Layout widget.
-- awful.widget.keyboardlayout
local keyboardlayout = { mt = {} }

-- As to the country-code-like symbols below, refer to the names of XKB's
-- data files in /.../xkb/symbols/*.
keyboardlayout.xkeyboard_country_code = {
    ["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
    ["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
}

-- Callback for updating current layout.
local function update_status (self)
    self._current = awesome.xkb_get_layout_group();
    local text = ""
    if (#self._layout > 0) then
        text = (" " .. self._layout[self._current] .. " ")
    end
    self.widget:set_text(text)
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".
-- @tparam string group_names The string awesome.xkb_get_group_names() returns.
-- @treturn table An array of tables whose keys are vendor, file, section, and group_idx.
function keyboardlayout.get_groups_from_group_names(group_names)
    if group_names == nil then
        return nil
    end

    -- 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
    }

    -- Split 'group_names' into 'tokens'.  The separator is "+".
    local tokens = {}
    string.gsub(group_names, "[^+]+", function(match)
        table.insert(tokens, match)
    end)

    -- 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

                if section then
                    section = string.gsub(section, "%(([%w-_]+)%)", "%1")
                end

                table.insert(layout_groups, { vendor = vendor,
                                              file = file,
                                              section = section,
                                              group_idx = tonumber(group_idx) })
                break
            end
        end
    end

    return layout_groups
end

-- Callback for updating list of layouts
local function update_layout(self)
    self._layout = {};
    local layouts = keyboardlayout.get_groups_from_group_names(awesome.xkb_get_group_names())
    if layouts == nil or layouts[1] == nil then
        gdebug.print_error("Failed to get list of keyboard groups")
        return
    end
    if #layouts == 1 then
        layouts[1].group_idx = 0
    end
    for _, v in ipairs(layouts) do
        local layout_name = self.layout_name(v)
        -- Please note that numbers of groups reported by xkb_get_group_names
        -- is greater by one than the real group number.
        self._layout[v.group_idx - 1] = layout_name
    end
    update_status(self)
end

--- Create a keyboard layout widget. It shows current keyboard layout name in a textbox.
-- @return A keyboard layout widget.
function keyboardlayout.new()
    local widget = textbox()
    local self = widget_base.make_widget(widget)

    self.widget = widget

    self.layout_name = function(v)
        local name = v.file
        if v.section ~= nil then
            name = name .. "(" .. v.section .. ")"
        end
        return name
    end

    self.next_layout = function()
        self.set_layout((self._current + 1) % (#self._layout + 1))
    end

    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

    update_layout(self);

    -- 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);

    -- Mouse bindings
    self:buttons(
        util.table.join(button({ }, 1, self.next_layout))
    )

    return self
end

local _instance = nil;

function keyboardlayout.mt:__call(...)
    if _instance == nil then
        _instance = keyboardlayout.new(...)
    end
    return _instance
end

return setmetatable(keyboardlayout, keyboardlayout.mt)

-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80