| 1 | -- flaw, a Lua OO management framework for Awesome WM widgets. |
| 2 | -- Copyright (C) 2012 David Soulayrol <david.soulayrol AT gmail DOT net> |
| 3 | |
| 4 | -- This program is free software: you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation, either version 3 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | |
| 14 | -- You should have received a copy of the GNU General Public License |
| 15 | -- along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | |
| 18 | -- Grab environment. |
| 19 | local io = io |
| 20 | local ipairs = ipairs |
| 21 | local os = os |
| 22 | local string = string |
| 23 | local table = table |
| 24 | |
| 25 | local dbus = require('dbus') |
| 26 | local image = require('image') |
| 27 | |
| 28 | local awful = { |
| 29 | button = require("awful.button"), |
| 30 | menu = require("awful.menu"), |
| 31 | util = require("awful.util"), |
| 32 | } |
| 33 | |
| 34 | local flaw = { |
| 35 | gadget = require('flaw.gadget'), |
| 36 | helper = require('flaw.helper'), -- DEBUG |
| 37 | provider = require('flaw.provider') |
| 38 | } |
| 39 | |
| 40 | local LANGS = { |
| 41 | ch = { short = 'CH', long = '中国的' }, |
| 42 | de = { short = 'DE', long = 'Deutsch' }, |
| 43 | en = { short = 'EN', long = 'English' }, |
| 44 | fr = { short = 'FR', long = 'Français', }, |
| 45 | jp = { short = 'JP', long = '日本人', }, |
| 46 | ru = { short = 'РУС', long = 'Русский' }, |
| 47 | us = { short = 'US', long = 'English' }, |
| 48 | } |
| 49 | |
| 50 | local FLAGS = nil |
| 51 | |
| 52 | local function get_current_layout() |
| 53 | local f = io.popen('dbus-send --print-reply=literal --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.getCurrentLayout') |
| 54 | local layout = 0 |
| 55 | |
| 56 | if f ~= nil then |
| 57 | line = f:read() |
| 58 | if line ~= nil then |
| 59 | layout = line:match('uint32%s+([%d]+)') |
| 60 | end |
| 61 | end |
| 62 | f:close() |
| 63 | |
| 64 | return layout |
| 65 | end |
| 66 | |
| 67 | |
| 68 | --- Keyboard layout gadget and provider. |
| 69 | -- |
| 70 | -- <p>This module contains a provider for keyboard layout information |
| 71 | -- and two gadgets: a text gadget and an icon gadget. This module was |
| 72 | -- written with the help of prior art found on <a |
| 73 | -- href="http://awesome.naquadah.org/wiki/Keyboard_layouts_with_kbdd">this |
| 74 | -- awesome's wiki page</a>.</p> |
| 75 | -- |
| 76 | -- <p>The module relies on <code>kbdd</code> dbus notifications to |
| 77 | -- update itself. <code>kbdd</code> in turn relies on |
| 78 | -- <code>Xorg</code> configuration. The <code>Xorg</code> input |
| 79 | -- configuration can be tweaked using the <code>setxkbmap</code> tool. |
| 80 | -- For example:</p> |
| 81 | -- |
| 82 | -- <div class='example'> |
| 83 | -- $ setxkbmap -query # displays the current configuration<br/> |
| 84 | -- $ setxkbmap -layout "en,fr,ru" -layout ",latin9," |
| 85 | -- </div> |
| 86 | -- |
| 87 | -- <h2>Gadgets</h2> |
| 88 | -- |
| 89 | -- <p>The ID parameter has no meaning for the keyboard gadgets. Those |
| 90 | -- gadgets provide no custom parameters. See the <a |
| 91 | -- href="flaw.gadget.html">gadget</a> module documentation to learn |
| 92 | -- about standard gadgets parameters.</p> |
| 93 | -- |
| 94 | -- <p>All keyboard gadgets specialize their standard counterpart to |
| 95 | -- provide a menu for direct selection of a keyboard layout. The |
| 96 | -- buttons 4 and 5 of the mouse - which should normally be bound to |
| 97 | -- the wheel up and down movements - allow you to select the previous |
| 98 | -- and next keyboard layout in the configured list, whereas the left |
| 99 | -- click opens a menu with the whole list.</p> |
| 100 | -- |
| 101 | -- <p>Both the Icon and the Text gadgets can automatically display the |
| 102 | -- flag icon matching the curent layout's language. To achieve this, |
| 103 | -- the function <code>set_flags_dict</code> must be called once before |
| 104 | -- creating any gadget. The argument to this function must be a table |
| 105 | -- containing the flags icons paths indexed by the two-letters |
| 106 | -- language codes, in lower case. This dictionnary can be set |
| 107 | -- manually, or could be built automatically by browsing an icons |
| 108 | -- collection. In the following example, a valid table of icon files |
| 109 | -- is built from filenames matching <code>EN.png</code>, |
| 110 | -- <code>FR.png</code>, <code>DE.png</code> and so on:</p> |
| 111 | -- |
| 112 | -- <div class='example'> |
| 113 | -- local lfs = require('lfs')<br/> |
| 114 | -- local path = '/home/david/.config/awesome/icons/flags/'<br/> |
| 115 | -- local flags = {}<br/> |
| 116 | -- for file in lfs.dir(path) do<br/> |
| 117 | -- lang = file:match('(%a%a)\.png')<br/> |
| 118 | -- if lang ~= nil then<br/> |
| 119 | -- flags[lang:lower()] = path .. file<br/> |
| 120 | -- end<br/> |
| 121 | -- end<br/> |
| 122 | -- flaw.keyboard.set_flags_dict(flags) |
| 123 | -- </div> |
| 124 | -- |
| 125 | -- <h3>Icon Gadget</h3> |
| 126 | -- |
| 127 | -- <p>The keyboard icon gadget can be instantiated by indexing the gadget |
| 128 | -- module with <code>icon.keyboard</code>. |
| 129 | -- |
| 130 | -- <div class='example'> |
| 131 | -- g = flaw.gadget.icon.keyboard('') |
| 132 | -- </div> |
| 133 | -- |
| 134 | -- <h3>Text Gadget</h3> |
| 135 | -- |
| 136 | -- <p>The keyboard text gadget can be instantiated by indexing the |
| 137 | -- gadget module with <code>text.keyboard</code>. By default, the |
| 138 | -- gadget pattern is <code>'$name'</code>. See the provider's |
| 139 | -- documentation below to learn about the available variables.</p> |
| 140 | -- |
| 141 | -- <div class='example'> |
| 142 | -- g = flaw.gadget.text.keyboard('') |
| 143 | -- </div> |
| 144 | -- |
| 145 | -- <h2>Provider</h2> |
| 146 | -- |
| 147 | -- <p>The keyboard provider is updated by the dbus notifications |
| 148 | -- emitted by <code>kbdd</code> everytime the keyboard layout is |
| 149 | -- modified. Since <code>kbdd</code> remembers the keyboard layout for |
| 150 | -- each window, these notifications can be received when the user |
| 151 | -- explicitly sets a new layout, or when a window with a different |
| 152 | -- layout gets the focus.</p> |
| 153 | -- |
| 154 | -- <p>The keyboard provider maintains a table named |
| 155 | -- <code>layouts</code> which stores the currently configured layouts. |
| 156 | -- This list is updated each time a dbus event is received so as to |
| 157 | -- remain synchronized with the <code>Xorg</code> current |
| 158 | -- configuration. When it is updated, the provider's <code>data</code> |
| 159 | -- attribute is also set to the line matching the current layout.</p> |
| 160 | -- |
| 161 | -- <p>Each entry of this table is composed of the following |
| 162 | -- fields.</p> |
| 163 | -- |
| 164 | -- <ul> |
| 165 | -- |
| 166 | -- <li><code>short_name</code> |
| 167 | -- |
| 168 | -- <p>A string which represents the current layout's language, in the |
| 169 | -- international two characters form (e.g. 'EN' or 'DE').</p></li> |
| 170 | -- |
| 171 | -- <li><code>name</code> |
| 172 | -- |
| 173 | -- <p>The full name of the current layout's language (e.g. 'English' |
| 174 | -- or 'Deutsch').</p></li> |
| 175 | -- |
| 176 | -- <li><code>img</code> |
| 177 | -- |
| 178 | -- <p>The path to the matching flag icon, if the function |
| 179 | -- <code>set_flags_dict</code> was called to initialize the available |
| 180 | -- icons paths.</p></li> |
| 181 | -- |
| 182 | -- </ul> |
| 183 | -- |
| 184 | -- <p>The provider also holds in its <code>menu</code> attribute the |
| 185 | -- menu which is to be opened when interacting with the gadgets to |
| 186 | -- select a new layout.</p> |
| 187 | -- |
| 188 | -- |
| 189 | -- @author David Soulayrol <david.soulayrol AT gmail DOT com> |
| 190 | -- @copyright 2012 David Soulayrol |
| 191 | module('flaw.keyboard') |
| 192 | |
| 193 | |
| 194 | --- The keyboard provider prototype. |
| 195 | -- |
| 196 | -- <p>The keyboard provider type is set to |
| 197 | -- <code>kbd._NAME</code>.</p> |
| 198 | -- |
| 199 | -- @class table |
| 200 | -- @name KbdProvider |
| 201 | KbdProvider = flaw.provider.Provider:new{ type = _NAME, menu = nil, layouts = {}, data = {} } |
| 202 | |
| 203 | function KbdProvider:update(layout) |
| 204 | self:update_keyboard_layouts() |
| 205 | self.data = self.layouts[layout + 1] |
| 206 | self:refresh_gadgets() |
| 207 | end |
| 208 | |
| 209 | -- Read the keyboard layouts configured list. |
| 210 | function KbdProvider:update_keyboard_layouts() |
| 211 | local f = io.popen('setxkbmap -query') |
| 212 | local menu_items = {} |
| 213 | local idx = 0 |
| 214 | local img = nil |
| 215 | self.layouts = {} |
| 216 | |
| 217 | if f ~= nil then |
| 218 | local line = nil |
| 219 | repeat |
| 220 | line = f:read() |
| 221 | if line then |
| 222 | list = line:match('layout:%s+([,%a]+)') |
| 223 | if list ~= nil then |
| 224 | for token in string.gmatch(list, '[^,]+') do |
| 225 | if FLAGS ~= nil then |
| 226 | img = FLAGS[token] |
| 227 | end |
| 228 | table.insert(self.layouts, { short_name = LANGS[token].short, name = LANGS[token].long, img = img }) |
| 229 | table.insert(menu_items, { LANGS[token].long, _M.set_layout(idx), img }) |
| 230 | idx = idx + 1 |
| 231 | end |
| 232 | end |
| 233 | end |
| 234 | until line == nil |
| 235 | f:close() |
| 236 | end |
| 237 | |
| 238 | self.menu = awful.menu({ items = menu_items }) |
| 239 | end |
| 240 | |
| 241 | |
| 242 | --- A factory for keyboard providers. |
| 243 | -- |
| 244 | -- <p>Only one provider is built.</p> |
| 245 | -- |
| 246 | -- @return a brand new keyboard provider, or the existing one if found |
| 247 | -- in the providers cache. |
| 248 | function provider_factory() |
| 249 | local p = flaw.provider.get(_NAME, '') |
| 250 | -- Create the provider if necessary. |
| 251 | if p == nil then |
| 252 | p = KbdProvider:new{id = ''} |
| 253 | p:update_keyboard_layouts() |
| 254 | p.data = p.layouts[get_current_layout() + 1] |
| 255 | flaw.provider.add(p) |
| 256 | end |
| 257 | return p |
| 258 | end |
| 259 | |
| 260 | |
| 261 | --- A Text gadget for current keyboard layout display. |
| 262 | -- |
| 263 | -- <p>This specialized text gadget is able to display a flag in the |
| 264 | -- text background.</p> |
| 265 | KbdTextGadget = flaw.gadget.TextGadget:new{} |
| 266 | |
| 267 | -- Create the wrapped gadget. |
| 268 | function KbdTextGadget:create(wopt) |
| 269 | flaw.gadget.TextGadget.create(self, wopt) |
| 270 | self.widget:buttons( |
| 271 | awful.util.table.join( |
| 272 | awful.button({ }, 1, function() self.provider.menu:toggle () end), |
| 273 | awful.button({ }, 4, _M.set_previous_layout), |
| 274 | awful.button({ }, 5, _M.set_next_layout))) |
| 275 | end |
| 276 | |
| 277 | function KbdTextGadget:redraw() |
| 278 | flaw.gadget.TextGadget.redraw(self) |
| 279 | self.widget.bg_image = image(self.provider.data.img) |
| 280 | end |
| 281 | |
| 282 | |
| 283 | -- An icon gadget for current keyboard layout display. |
| 284 | KbdIconGadget = flaw.gadget.IconGadget:new{} |
| 285 | |
| 286 | -- Create the wrapped gadget. |
| 287 | function KbdIconGadget:create(wopt) |
| 288 | flaw.gadget.IconGadget.create(self, wopt) |
| 289 | self.widget:buttons( |
| 290 | awful.util.table.join( |
| 291 | awful.button({ }, 1, function() self.provider.menu:toggle () end), |
| 292 | awful.button({ }, 4, _M.set_previous_layout), |
| 293 | awful.button({ }, 5, _M.set_next_layout))) |
| 294 | end |
| 295 | |
| 296 | function KbdIconGadget:redraw() |
| 297 | if self.flags ~= nil then |
| 298 | self.widget.image = image(self.flags[provider.data.short]) |
| 299 | end |
| 300 | end |
| 301 | |
| 302 | |
| 303 | --- Module initialization routine. |
| 304 | -- |
| 305 | -- <p> This method tests the available information on the system. If |
| 306 | -- kbdd is up and ready, it registers gadgets and returns the |
| 307 | -- module; otherwise it simply returns nil.</p> |
| 308 | -- |
| 309 | -- @return return this module if it can be used on the system, |
| 310 | -- nil otherwise. |
| 311 | function init() |
| 312 | if dbus and dbus.request_name('session', 'ru.gentoo.kbdd') then |
| 313 | -- TODO: request_name returns ok even if the daemon is not running. |
| 314 | |
| 315 | dbus.add_match('session', "interface='ru.gentoo.kbdd',member='layoutChanged'") |
| 316 | dbus.add_signal('ru.gentoo.kbdd', function(...) |
| 317 | local data = {...} |
| 318 | provider_factory():update(data[2]) |
| 319 | end) |
| 320 | |
| 321 | flaw.gadget.register.text(_M, { pattern = '$short_name' }, { align = 'center' }, KbdTextGadget) |
| 322 | flaw.gadget.register.icon(_M, {}, {}, KbdIconGadget) |
| 323 | |
| 324 | return _M |
| 325 | end |
| 326 | end |
| 327 | |
| 328 | --- Set the flag icons dictionnary. |
| 329 | -- |
| 330 | -- <p>This function must be called once to display flag icons in the |
| 331 | -- gadgets.</p> |
| 332 | function set_flags_dict(dict) |
| 333 | FLAGS = dict |
| 334 | end |
| 335 | |
| 336 | --- Explicitly set the previous configured keyboard layout. |
| 337 | function set_previous_layout() |
| 338 | os.execute('dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.prev_layout') |
| 339 | end |
| 340 | |
| 341 | --- Excplicitly set the next configured keyboard layout. |
| 342 | function set_next_layout() |
| 343 | os.execute('dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.next_layout') |
| 344 | end |
| 345 | |
| 346 | --- Set the keyboard layout specified by the given index. |
| 347 | -- |
| 348 | -- <p>The index in the order number in the <code>Xorg</code> |
| 349 | -- configuration. It starts from 0.</p> |
| 350 | function set_layout(layout) |
| 351 | return function() |
| 352 | os.execute('dbus-send --dest=ru.gentoo.KbddService /ru/gentoo/KbddService ru.gentoo.kbdd.set_layout uint32:' .. layout) |
| 353 | end |
| 354 | end |