Advertisement
Guest User

Untitled

a guest
Jul 21st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.20 KB | None | 0 0
  1. #!/bin/bash
  2. #Name: ds-mouse
  3. #Depends: ds-mouse.py, xset
  4. #Author: Dave (david@____.___)
  5. #Purpose: Configure mouse on per user basis for a session. This is the gui frontend
  6.  
  7. conf_dir="$HOME/.desktop-session"
  8. conf_file="$conf_dir/mouse.conf"
  9. if [ ! -f "$conf_file" ]; then
  10. conf_file="/" + "etc/desktop-session/mouse.conf"
  11. fi
  12.  
  13. function acceleration() {
  14. acceleration="$(cat $conf_file | grep '^ACCELERATION' |cut -d '=' -f2 |cut -d ' ' -f2)"
  15. threshold="$(cat $conf_file | grep '^THRESHOLD' |cut -d '=' -f2 |cut -d ' ' -f2)"
  16. # ---------- AFAIK, ZERO IS A VALID xset VALUE FOR ACCELERATION
  17. if (( $(bc <<< "$acceleration >= 0") == 1 )) &&
  18. (( $(bc <<< "$acceleration <= 16") == 1 )) &&
  19. (( $(bc <<< "$threshold >= 0") == 1 )) &&
  20. (( $(bc <<< "$threshold < 101") == 1 )); then
  21. xset m "$acceleration" "$threshold"
  22. else
  23. printf "out-of-range value for acceleration or threshold, so applying xset default values"
  24. xset m default
  25. fi
  26. }
  27.  
  28. function buttonorder() {
  29. buttonorder="$(cat $conf_file | grep '^BUTTONORDER' |cut -d '=' -f2 |cut -d ' ' -f2)"
  30. case $buttonorder in
  31. 0) xmodmap -e 'pointer = 1 2 3 4 5' > /dev/null 2>&1;;
  32. 1) xmodmap -e 'pointer = 3 2 1 4 5' > /dev/null 2>&1;;
  33. *) xmodmap -e 'pointer = 1 2 3 4 5' > /dev/null 2>&1;;
  34. esac
  35. }
  36.  
  37. function size() {
  38. size="$(cat $conf_file | grep '^SIZE' |cut -d '=' -f2 |cut -d ' ' -f2)"
  39. #file="$HOME/\.Xdefaults";
  40. file="$HOME/.Xdefaults";
  41. if cat $file |grep "Xcursor.size:" > /dev/null ; then
  42. if [ "$size" = '0' ]; then
  43. sed -n -i "s/Xcursor\.size*//ig" $file
  44. else
  45. sed -i "s/Xcursor\.size.*/Xcursor\.size:$size/ig" $file
  46. fi
  47. else
  48. echo "Xcursor.size:$size" > $file
  49. fi
  50. }
  51.  
  52. function help() {
  53. echo "Help:";
  54. echo "-a | set mouse motion (acceleration and threshold)";
  55. echo "-b | set button order for left and right hand";
  56. echo "-s | set cursor size";
  57. echo "-all | set mouse motion, button order, and cursor size";
  58. echo "-h | show this help dialog";
  59. echo "No option start settings gui";
  60. }
  61.  
  62. action="$1" && shift;
  63. case $action in
  64. -a) acceleration ;;
  65. -o) buttonorder ;;
  66. -s) size ;;
  67. -all)
  68. acceleration;
  69. buttonorder;
  70. size;
  71. ;;
  72. -h) help ;;
  73. *) ./ds-mouse.py ;;
  74. esac
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. #!/usr/bin/env python
  87. #Name: ds-mouse.py
  88. #Depends: python, gtk, xset
  89. #Author: Dave (david@_______.___)
  90. #Purpose: Configure mouse on per user basis for a session. This is the gui frontend
  91.  
  92. import gtk
  93. import os
  94. import re
  95. import sys
  96. import fcntl
  97. import atexit
  98. import gettext
  99. gettext.install("antixccmouse.sh", "/usr/share/locale")
  100.  
  101. pid_file = '/tmp/ds-mouse.pid'
  102. fp = open(pid_file, 'w')
  103. try:
  104. fcntl.lockf(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
  105. except IOError:
  106. print 'Another instance of ds-mouse is already running.'
  107. print 'Will not launch another instance. Bye.'
  108. errbx = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
  109. _("Another instance of ds-mouse is already running.\nWill not launch another instance. Bye."))
  110. errbx.run()
  111. sys.exit(1)
  112.  
  113. def cleanuppid():
  114. os.remove(pid_file)
  115.  
  116. atexit.register(cleanuppid)
  117.  
  118. #class Error:
  119. # def __init__(self, error):
  120. # cmdstring = "yad --image=\"error\"\
  121. # --title=\"Error\"\
  122. # --text=\"There is an error,\
  123. # \nplease rerun and correct the following error!\
  124. # \n\n%s\n\"\
  125. # --button=\"gtk-ok:0\"" % (error)
  126. # os.system(cmdstring)
  127.  
  128. #class Success:
  129. # def __init__(self, success):
  130. # cmdstring = "yad --image=\"info\"\
  131. # --title=\"Success\"\
  132. # --text=\"Successfully Updated:\
  133. # \n\n%s\n\"\
  134. # --button=\"gtk-ok:0\"" % (success)
  135. # os.system(cmdstring)
  136.  
  137. class Var:
  138. def read(self):
  139. var = Var
  140. var.USER_HOME = os.environ['HOME']
  141. var.CONF_USER_DIR = var.USER_HOME+"/.desktop-session/"
  142. var.CONF_USER_FILE = var.CONF_USER_DIR+"mouse.conf"
  143. var.CONF_SYSTEM_FILE = "/" + "etc/desktop-session/mouse.conf"
  144.  
  145. if not os.path.exists(var.CONF_USER_DIR):
  146. os.system("mkdir %s" % (var.CONF_USER_DIR))
  147. os.system("cp %s %s" % ((var.CONF_SYSTEM_FILE),(var.CONF_USER_DIR)))
  148. else:
  149. if not os.path.isfile(var.CONF_USER_FILE):
  150. os.system("cp %s %s" % ((var.CONF_SYSTEM_FILE),(var.CONF_USER_DIR)))
  151.  
  152. for line in open(var.CONF_USER_FILE, "r").xreadlines():
  153. if "#" not in line:
  154. if re.search(r'^.*=', line):
  155. pieces = line.split('=')
  156. var.VARIABLE=(pieces[0])
  157. var.VARIABLE = re.sub(r'\n', '', var.VARIABLE)
  158. OBJECT=(pieces[1])
  159. OBJECT = re.sub(r'\n', '', OBJECT)
  160. setattr(var, var.VARIABLE, OBJECT)
  161.  
  162. def write(self, variable, item):
  163. WRITE_FILE = Var.CONF_USER_FILE+".tmp"
  164. READ_FILE = Var.CONF_USER_FILE
  165.  
  166. text = file((WRITE_FILE), "w");text.write("");text.close()
  167. text = file((WRITE_FILE), "a")
  168. for line in open(READ_FILE, "r").xreadlines():
  169. if "#" not in line:
  170. if re.search(r'^%s=' % (variable), line):
  171. text.write (variable+"="+str(item)+"\n")
  172. else:
  173. text.write (line)
  174. else:
  175. text.write (line)
  176. text.close()
  177. os.system("mv %s %s" % ((WRITE_FILE), (READ_FILE)))
  178.  
  179. class mainWindow():
  180. def apply(self,widget,option):
  181. if option == 0: # 'apply' button
  182. acceleration_value = self.acceleration.get_value()
  183. threshold_value = int(self.threshold.get_value())
  184. size_value = int(self.size.get_value())
  185. button_order_value = self.order.get_active()
  186. Var().write('ACCELERATION', acceleration_value)
  187. Var().write('THRESHOLD', threshold_value)
  188. Var().write('SIZE', size_value)
  189. Var().write('BUTTONORDER', button_order_value)
  190. os.system("ds-mouse -all")
  191. ### labeled 'undo' or whatnot (vs 'reset to zero') these invite confusion
  192. # elif option == 1: #reset motion button
  193. # acceleration_value = '0'
  194. # threshold_value = '0'
  195. # Var().write('ACCELERATION', acceleration_value)
  196. # Var().write('THRESHOLD', threshold_value)
  197. # os.system("ds-mouse -all")
  198. # elif option == 2: #reset size button
  199. # size_value = '0'
  200. # Var().write('SIZE', size_value)
  201. # os.system("ds-mouse -all")
  202. elif option == 3: #change cursor theme button
  203. os.system("rxvt-unicode -tr -sh 65 -fg white -T 'cursor theme' -e su -c 'update-alternatives --config x-cursor-theme' ")
  204.  
  205. #Success(_("Options Changed"))
  206. self.show_success()
  207.  
  208. def show_success(self):
  209. dlg = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
  210. gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE,
  211. message_format=_("Successfully updated:\n\noptions changed"))
  212. dlg.set_title(".") # avoid translation chore
  213. dlg.set_keep_above(True) # note: set_transient_for() is ineffective!
  214. dlg.run()
  215. dlg.destroy()
  216.  
  217. def make_frame(self, text):
  218. frame = gtk.Frame(_(text))
  219. frame.set_border_width(10)
  220. self.mainbox.pack_start(frame)
  221. frame.show()
  222.  
  223. self.framebox = gtk.VBox()
  224. frame.add(self.framebox)
  225. self.framebox.show()
  226.  
  227. def make_label(self, text):
  228. label = gtk.Label()
  229. label.set_text(_(text))
  230. self.framebox.pack_start(label)
  231. label.show()
  232.  
  233. def scale_set_default_values(self, scale):
  234. scale.set_update_policy(gtk.UPDATE_CONTINUOUS)
  235. scale.set_digits(0)
  236. scale.set_value_pos(gtk.POS_TOP)
  237. scale.set_draw_value(True)
  238.  
  239. def __init__(self):
  240. window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  241. #window.set_width(250)
  242. window.set_title(_("Mouse Options"))
  243. window.connect("destroy", lambda w: gtk.main_quit())
  244.  
  245. self.mainbox = gtk.VBox()
  246. window.add(self.mainbox)
  247. self.mainbox.show()
  248.  
  249. self.make_frame("Mouse Acceleration")
  250. self.make_label("Acceleration (Multiplier)")
  251.  
  252. adj1 = gtk.Adjustment(float(Var.ACCELERATION), 0.0, 17.0, 0.5, 1.0, 1.0 )
  253. self.acceleration = gtk.HScale(adj1)
  254. self.acceleration.set_update_policy(gtk.UPDATE_CONTINUOUS)
  255. self.acceleration.set_size_request(200, 45)
  256. self.framebox.pack_start(self.acceleration)
  257. self.acceleration.show()
  258.  
  259. self.make_label("Threshold (Pixels)")
  260.  
  261. adj2 = gtk.Adjustment(float(Var.THRESHOLD), 0.0, 97.0, 2.0, 1.0, 1.0 )
  262. self.threshold = gtk.HScale(adj2)
  263. self.threshold.set_size_request(200, 45)
  264. self.scale_set_default_values(self.threshold)
  265. self.framebox.pack_start(self.threshold)
  266. self.threshold.show()
  267.  
  268. ### labeled 'undo' or whatnot (vs 'reset to zero') invites confusion
  269. # reset_motion = gtk.Button(stock=gtk.STOCK_UNDO)
  270. # reset_motion.connect("clicked", self.apply, 1)
  271. # self.framebox.pack_start(reset_motion)
  272. # reset_motion.show()
  273.  
  274. self.make_frame("Button Order")
  275.  
  276. self.order = gtk.combo_box_new_text()
  277. self.order.append_text(_("Left hand layout"))
  278. self.order.append_text(_("Right hand layout"))
  279. self.order.set_active(int(Var.BUTTONORDER))
  280. self.framebox.pack_start(self.order)
  281. self.order.show()
  282.  
  283. self.make_frame("Cursor Size")
  284. self.make_label("Size (in pixels)")
  285. self.make_label("note: some applications ignore this setting")
  286.  
  287. adj2 = gtk.Adjustment(float(Var.SIZE), 0.0, 101.0, 1.0, 1.0, 1.0 )
  288. self.size = gtk.HScale(adj2)
  289. self.size.set_size_request(520, 45)
  290. self.scale_set_default_values(self.size)
  291. self.framebox.pack_start(self.size)
  292. self.size.show()
  293.  
  294. # reset_size = gtk.Button(stock=gtk.STOCK_UNDO)
  295. # reset_size.connect("clicked", self.apply, 2)S
  296. # self.framebox.pack_start(reset_size)
  297. # reset_size.show()
  298.  
  299. self.make_frame("Cursor Theme")
  300. self.make_label("\n After choosing a new theme, please logout/login to see the changes."
  301. + "\n\n note:\n if \"lxappearance\" is installed, you could use that, "
  302. + "instead, to change cursor theme. ")
  303.  
  304. theme = gtk.Button(_("Change cursor theme"))
  305. theme.connect("clicked", self.apply, 3)
  306. self.framebox.pack_start(theme)
  307. theme.show()
  308.  
  309. #BUTTON BOX
  310.  
  311. buttonbox = gtk.HButtonBox()
  312. self.mainbox.pack_start(buttonbox)
  313. buttonbox.show()
  314.  
  315. aply = gtk.Button(stock=gtk.STOCK_APPLY)
  316. aply.connect("clicked", self.apply, 0)
  317. buttonbox.pack_start(aply)
  318. aply.show()
  319.  
  320. close = gtk.Button(stock=gtk.STOCK_CLOSE)
  321. close.connect("clicked", lambda w: gtk.main_quit())
  322. buttonbox.add(close)
  323. close.show()
  324. window.show()
  325.  
  326. Var().read()
  327. mainWindow()
  328. gtk.main()
  329. [code]
  330.  
  331. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement