PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/src/xconfigurator.py

https://github.com/atareao/Touchpad-Indicator
Python | 165 lines | 113 code | 21 blank | 31 comment | 31 complexity | e60d001a990e66b1bca55cdd625fbd82 MD5 | raw file
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This file is part of Touchpad-Indicator
  5. #
  6. # Copyright (C) 2010-2019 Lorenzo Carbonell<lorenzo.carbonell.cerezo@gmail.com>
  7. # Copyright (C) 2010-2012 Miguel Angel SantamarĂ­a Rogado<leibag@gmail.com>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. import re
  22. import os
  23. import subprocess
  24. from subprocess import Popen, PIPE
  25. XFCONFQUERY = '/usr/bin/xfconf-query'
  26. def xfconfquery_exists():
  27. return os.path.exists(XFCONFQUERY)
  28. def is_running(process):
  29. # From http://www.bloggerpolis.com/2011/05/\
  30. # how-to-check-if-a-process-is-running-using-python/
  31. # and http://richarddingwall.name/2009/06/18/\
  32. # windows-equivalents-of-ps-and-kill-commands/
  33. try: # Linux/Unix
  34. s = subprocess.Popen(["ps", "axw"], stdout=subprocess.PIPE)
  35. except Exception as e: # Windows
  36. print(e)
  37. s = subprocess.Popen(["tasklist", "/v"], stdout=subprocess.PIPE)
  38. for x in s.stdout:
  39. if re.search(process, x.decode()):
  40. return True
  41. return False
  42. def get_desktop_environment():
  43. desktop_session = os.environ.get("DESKTOP_SESSION")
  44. # easier to match if we doesn't have to deal with caracter cases
  45. if desktop_session is not None:
  46. desktop_session = desktop_session.lower()
  47. if desktop_session in ["gnome", "unity", "cinnamon", "mate",
  48. "budgie-desktop", "xfce4", "lxde", "fluxbox",
  49. "blackbox", "openbox", "icewm", "jwm",
  50. "afterstep", "trinity", "kde"]:
  51. return desktop_session
  52. # ## Special cases ##
  53. # Canonical sets $DESKTOP_SESSION to Lubuntu rather than
  54. # LXDE if using LXDE.
  55. # There is no guarantee that they will not do the same with
  56. # the other desktop environments.
  57. elif "xfce" in desktop_session or\
  58. desktop_session.startswith("xubuntu"):
  59. return "xfce4"
  60. elif desktop_session.startswith("ubuntu"):
  61. return "unity"
  62. elif desktop_session.startswith("lubuntu"):
  63. return "lxde"
  64. elif desktop_session.startswith("kubuntu"):
  65. return "kde"
  66. elif desktop_session.startswith("razor"): # e.g. razorkwin
  67. return "razor-qt"
  68. elif desktop_session.startswith("wmaker"): # eg. wmaker-common
  69. return "windowmaker"
  70. if os.environ.get('KDE_FULL_SESSION') == 'true':
  71. return "kde"
  72. elif os.environ.get('GNOME_DESKTOP_SESSION_ID'):
  73. if "deprecated" not in os.environ.get(
  74. 'GNOME_DESKTOP_SESSION_ID'):
  75. return "gnome2"
  76. # From http://ubuntuforums.org/showthread.php?t=652320
  77. elif is_running("xfce-mcs-manage"):
  78. return "xfce4"
  79. elif is_running("ksmserver"):
  80. return "kde"
  81. return "unknown"
  82. def getoutput(cmd):
  83. val = Popen(cmd, shell=True, stdout=PIPE).communicate()[0].decode("utf-8")
  84. return val.rstrip().lstrip()
  85. class XFCEConfiguration:
  86. def __init__(self, channel):
  87. self.channel = channel
  88. def get_keys(self):
  89. out = getoutput('xfconf-query -c %s -l' % self.channel)
  90. keys = []
  91. for key in out.split('\n'):
  92. if '\override' not in key:
  93. key = key.rstrip().lstrip()
  94. value = self.get_value(key)
  95. keys.append({'key': key, 'value': value})
  96. return keys
  97. def set_property(self, property, value):
  98. val = getoutput('xfconf-query -c %s --create --property "%s" \
  99. --set "%s" --type string' % (self.channel, property, value))
  100. return val
  101. def reset_property(self, property):
  102. val = getoutput('xfconf-query -c %s --reset --property "%s"' % (
  103. self.channel, property))
  104. return val
  105. def get_value(self, property):
  106. if len(property) > 0:
  107. val = getoutput('xfconf-query -c %s --property "%s"' % (
  108. self.channel, property))
  109. return val
  110. return None
  111. def search_for_value_in_properties_startswith(self, startswith, value):
  112. found_keys = []
  113. keys = self.search_for_property_startswith(startswith)
  114. for key in keys:
  115. if key['value'] == value:
  116. found_keys.append(key)
  117. return found_keys
  118. def search_for_property_startswith(self, startswith):
  119. found_keys = []
  120. keys = self.get_keys()
  121. for key in keys:
  122. if key['key'].startswith(startswith) and key not in found_keys:
  123. found_keys.append(key)
  124. return found_keys
  125. if __name__ == '__main__':
  126. print(get_desktop_environment())
  127. if get_desktop_environment() == 'xfce4' and xfconfquery_exists():
  128. key = '<Control><Alt>t'
  129. xfceconf = XFCEConfiguration('xfce4-keyboard-shortcuts')
  130. akeys = xfceconf.search_for_value_in_properties_startswith(
  131. '/commands/custom/',
  132. '/usr/share/touchpad-indicator/change_touchpad_state.py')
  133. print('akeys: ' + str(akeys))
  134. if akeys:
  135. for akey in akeys:
  136. print('akey: ' + str(akey))
  137. xfceconf.reset_property(akey['key'])
  138. if True:
  139. key = key.replace('<Primary>', '<Control>')
  140. print(key)
  141. print(xfceconf.set_property(
  142. '/commands/custom/' + key,
  143. '/usr/share/touchpad-indicator/change_touchpad_state.py'))
  144. exit(0)