keyboardkbdd.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # Copyright (c) 2015 Ali Mousavi
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. import re
  21. import dbus
  22. from dbus.mainloop.glib import DBusGMainLoop
  23. from libqtile.log_utils import logger
  24. from libqtile.widget import base
  25. class KeyboardKbdd(base.ThreadPoolText):
  26. """Widget for changing keyboard layouts per window, using kbdd
  27. kbdd should be installed and running, you can get it from:
  28. https://github.com/qnikst/kbdd
  29. """
  30. orientations = base.ORIENTATION_HORIZONTAL
  31. defaults = [
  32. ("update_interval", 1, "Update interval in seconds."),
  33. ("configured_keyboards", ["us", "ir"],
  34. "your predefined list of keyboard layouts."
  35. "example: ['us', 'ir', 'es']"),
  36. ("colours", None,
  37. "foreground colour for each layout"
  38. "either 'None' or a list of colours."
  39. "example: ['ffffff', 'E6F0AF']. ")
  40. ]
  41. def __init__(self, **config):
  42. base.ThreadPoolText.__init__(self, "", **config)
  43. self.add_defaults(KeyboardKbdd.defaults)
  44. self.keyboard = self.configured_keyboards[0]
  45. self.is_kbdd_running = self._check_kbdd()
  46. if not self.is_kbdd_running:
  47. logger.error('Please check if kbdd is running')
  48. self.keyboard = "N/A"
  49. self._dbus_init()
  50. def _check_kbdd(self):
  51. running_list = self.call_process(["ps", "axw"])
  52. if re.search("kbdd", running_list):
  53. self.keyboard = self.configured_keyboards[0]
  54. return True
  55. return False
  56. def _dbus_init(self):
  57. dbus_loop = DBusGMainLoop()
  58. bus = dbus.SessionBus(mainloop=dbus_loop)
  59. bus.add_signal_receiver(self._layout_changed,
  60. dbus_interface='ru.gentoo.kbdd',
  61. signal_name='layoutChanged')
  62. def _layout_changed(self, layout_changed):
  63. """
  64. Handler for "layoutChanged" dbus signal.
  65. """
  66. if self.colours:
  67. self._set_colour(layout_changed)
  68. self.keyboard = self.configured_keyboards[layout_changed]
  69. def _set_colour(self, index):
  70. if isinstance(self.colours, list):
  71. try:
  72. self.layout.colour = self.colours[index]
  73. except ValueError:
  74. self._setColour(index - 1)
  75. else:
  76. logger.error('variable "colours" should be a list, to set a\
  77. colour for all layouts, use "foreground".')
  78. def poll(self):
  79. if not self.is_kbdd_running:
  80. if self._check_kbdd():
  81. self.is_kbdd_running = True
  82. return self.configured_keyboards[0]
  83. return self.keyboard