From 05683dad2e6c15c21ab5109a9ab11376bf1af6df Mon Sep 17 00:00:00 2001 From: Ben Reaves Date: Fri, 16 Oct 2020 02:07:00 -0500 Subject: [PATCH] - GUI & sys tray interaction issues resolved - Added fix to kill spawned processes to GUI app - Added proper handling of spawn from disabling sys tray app - Improved checkTray function - Set full path to avoid issues on About --- xkeysnail-config/gui/kinto-gui.py | 85 +++++++++++++++++-- .../trayapps/appindicator/kintotray.desktop | 8 ++ .../trayapps/appindicator/kintotray.py | 26 +++++- xkeysnail-config/xkeysnail.desktop | 2 + xkeysnail_service.sh | 3 + 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 xkeysnail-config/trayapps/appindicator/kintotray.desktop diff --git a/xkeysnail-config/gui/kinto-gui.py b/xkeysnail-config/gui/kinto-gui.py index f3dcec8..6fc0d45 100755 --- a/xkeysnail-config/gui/kinto-gui.py +++ b/xkeysnail-config/gui/kinto-gui.py @@ -6,13 +6,26 @@ from gi.repository import Gtk,Gdk,GdkPixbuf from gi.repository import Vte,GLib from subprocess import Popen,PIPE,CalledProcessError +import signal + +def kill_child(): + if child_pid is None: + pass + else: + os.kill(child_pid, signal.SIGTERM) + +import atexit +atexit.register(kill_child) + class MyWindow(Gtk.Window): label = Gtk.Label() label.set_alignment(1, 0) ostype = os.environ.get('XDG_CURRENT_DESKTOP') + global child_pid kinto_status = Popen("while :; do clear; systemctl is-active xkeysnail; sleep 2; done", stdout=PIPE, shell=True) + child_pid = kinto_status.pid winkb = Gtk.RadioMenuItem(label='Windows') mackb = Gtk.RadioMenuItem(label='Apple',group=winkb) @@ -26,10 +39,12 @@ class MyWindow(Gtk.Window): ibmkb.signal_id = 0 winmackb.signal_id = 0 + menuitem_enable = Gtk.CheckMenuItem(label="Enabled") + menuitem_enable.signal_id = 0 menuitem_auto = Gtk.CheckMenuItem(label="Autostart") menuitem_auto.signal_id = 0 - menuitem_enable = Gtk.CheckMenuItem(label="Enable") - menuitem_enable.signal_id = 0 + menuitem_systray = Gtk.CheckMenuItem(label="Tray Enabled") + menuitem_systray.signal_id = 0 def __init__(self): @@ -173,8 +188,17 @@ class MyWindow(Gtk.Window): menubar.append(menuitem_file) submenu_file = Gtk.Menu() menuitem_file.set_submenu(submenu_file) - submenu_file.append(self.menuitem_auto) submenu_file.append(self.menuitem_enable) + submenu_file.append(self.menuitem_auto) + kintotray = int(self.queryConfig('ps -aux | grep [k]intotray >/dev/null 2>&1 && echo "1" || echo "0"')) + if kintotray and os.path.exists(os.environ['HOME']+'/.config/autostart/kintotray.desktop'): + self.menuitem_systray.set_active(True) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,False) + else: + self.menuitem_systray.set_active(False) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,True) + menuitem_file.connect('activate',self.refreshFile) + submenu_file.append(self.menuitem_systray) # self.menuitem_enable.connect('activate', self.setEnable) menuitem_restart = Gtk.MenuItem(label="Restart") menuitem_restart.connect('activate',self.runRestart) @@ -264,6 +288,22 @@ class MyWindow(Gtk.Window): # radiomenuitem2 = Gtk.RadioMenuItem(label="Apple", group=radiomenuitem1) # menu.append(radiomenuitem2) + def refreshFile(self,button): + kintotray = int(self.queryConfig('ps -aux | grep [k]intotray >/dev/null 2>&1 && echo "1" || echo "0"')) + if os.path.exists(os.environ['HOME']+'/.config/autostart/kintotray.desktop') and kintotray and self.menuitem_systray.get_active() == False: + self.menuitem_systray.disconnect(self.menuitem_systray.signal_id) + self.menuitem_systray.set_active(True) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,False) + elif os.path.exists(os.environ['HOME']+'/.config/autostart/kintotray.desktop') and not kintotray and self.menuitem_systray.get_active() == True: + self.menuitem_systray.disconnect(self.menuitem_systray.signal_id) + self.menuitem_systray.set_active(False) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,True) + elif not os.path.exists(os.environ['HOME']+'/.config/autostart/kintotray.desktop') and self.menuitem_systray.get_active() == True: + self.menuitem_systray.disconnect(self.menuitem_systray.signal_id) + self.menuitem_systray.set_active(False) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,True) + # return + def refresh(self,button): self.refreshKB() @@ -386,6 +426,31 @@ class MyWindow(Gtk.Window): return + def checkTray(self,button,tray_bool): + kintotray = int(self.queryConfig('ps -aux | grep [k]intotray >/dev/null 2>&1 && echo "1" || echo "0"')) + # path.exists('.config/autostart/kintotray.py') + if tray_bool: + Popen(['cp',os.environ['HOME']+'/.config/kinto/kintotray.desktop',os.environ['HOME']+'/.config/autostart/kintotray.desktop']) + if not kintotray: + Popen([os.environ['HOME']+'/.config/kinto/kintotray.py']) + self.menuitem_systray.disconnect(self.menuitem_systray.signal_id) + self.menuitem_systray.set_active(True) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,False) + else: + Popen(['rm',os.environ['HOME']+'/.config/autostart/kintotray.desktop']) + Popen(['pkill','-f','kintotray.py']) + killspawn = "for pid in `ps -ef | grep 'active xkeysnail' | awk '{print $2}'` ; do kill -2 $pid ; done" + self.queryConfig(killspawn) + time.sleep(1) + global child_pid + self.kinto_status = Popen("while :; do clear; systemctl is-active xkeysnail; sleep 2; done", stdout=PIPE, shell=True) + child_pid = self.kinto_status.pid + # Popen(['pkill','-f','kintotray.py']) + self.menuitem_systray.disconnect(self.menuitem_systray.signal_id) + self.menuitem_systray.set_active(False) + self.menuitem_systray.signal_id = self.menuitem_systray.connect('activate',self.checkTray,True) + return + def setKB(self,button,kbtype): try: if kbtype == "win": @@ -643,11 +708,11 @@ class MyWindow(Gtk.Window): pkillxkey = Popen(['sudo', 'pkill','-f','bin/xkeysnail']) pkillxkey.wait() Popen(['sudo', 'systemctl','start','xkeysnail']) - command = "send \003 journalctl -f --unit=xkeysnail.service -b\n" - cmdbytes = str.encode(command) - self.InputToTerm(cmdbytes) + self.command = "send \003 journalctl -f --unit=xkeysnail.service -b\n" + self.cmdbytes = str.encode(self.command) + self.InputToTerm(self.cmdbytes) except: - Popen(['notify-send','Kinto: Error restarting Kinto!','-i','budgie-desktop-symbolic']) + Popen(['notify-send','Kinto: Errror restarting Kinto!','-i','budgie-desktop-symbolic']) def setEnable(self,button,enableKinto): try: @@ -665,11 +730,13 @@ class MyWindow(Gtk.Window): self.menuitem_enable.disconnect(self.menuitem_enable.signal_id) self.menuitem_enable.set_active(True) self.menuitem_enable.signal_id = self.menuitem_enable.connect('activate',self.setEnable,False) - command = "send \003 journalctl -f --unit=xkeysnail.service -b\n" - cmdbytes = str.encode(command) + self.command = "send \003 journalctl -f --unit=xkeysnail.service -b\n" + self.cmdbytes = str.encode(self.command) self.InputToTerm(cmdbytes) else: Popen(['sudo', 'systemctl','stop','xkeysnail']) + self.command = "send \003 journalctl -f --unit=xkeysnail.service -b\n" + self.cmdbytes = str.encode(self.command) self.menuitem_enable.disconnect(self.menuitem_enable.signal_id) self.menuitem_enable.set_active(False) self.menuitem_enable.signal_id = self.menuitem_enable.connect('activate',self.setEnable,True) diff --git a/xkeysnail-config/trayapps/appindicator/kintotray.desktop b/xkeysnail-config/trayapps/appindicator/kintotray.desktop new file mode 100644 index 0000000..8ca4080 --- /dev/null +++ b/xkeysnail-config/trayapps/appindicator/kintotray.desktop @@ -0,0 +1,8 @@ +[Desktop Entry] +Name=Kinto Tray +GenericName=Kintotray +Comment=Make Linux Type Like it's a Mac +Exec=/bin/bash -c "{homedir}/.config/kinto/kintotray.py" +Terminal=false +Type=Application +X-GNOME-Autostart-enabled=true \ No newline at end of file diff --git a/xkeysnail-config/trayapps/appindicator/kintotray.py b/xkeysnail-config/trayapps/appindicator/kintotray.py index a1e4603..58696d2 100755 --- a/xkeysnail-config/trayapps/appindicator/kintotray.py +++ b/xkeysnail-config/trayapps/appindicator/kintotray.py @@ -65,6 +65,7 @@ class Indicator(): # Keyboard type set below button_syskb = Gtk.MenuItem('System Shortcuts') button_region = Gtk.MenuItem('Change Language') + systray = Gtk.CheckMenuItem('Tray Enabled') helpm = Gtk.MenuItem('Help') help_submenu = Gtk.Menu() helpm.set_submenu(help_submenu) @@ -109,7 +110,6 @@ class Indicator(): else: self.checkbox_autostart.set_active(False) self.chkautostart_id = self.checkbox_autostart.connect('activate',self.setAutostart,True) - self.menu.append(self.checkbox_autostart) # Kinto Enable @@ -161,6 +161,13 @@ class Indicator(): # Set Language self.button_region.connect('activate',self.setRegion) self.edit_submenu.append(self.button_region) + self.edit_submenu.append(self.checkbox_autostart) + if os.path.exists(os.environ['HOME']+'/.config/autostart/kintotray.desktop'): + self.systray.set_active(True) + self.systray.signal_id = self.systray.connect('activate',self.checkTray,False) + else: + self.systray.signal_id = self.systray.connect('activate',self.checkTray,True) + self.edit_submenu.append(self.systray) self.menu.append(self.edit) self.debug.connect('activate',self.runDebug) @@ -207,6 +214,21 @@ class Indicator(): # md.destroy() # return True + def checkTray(self,button,tray_bool): + # path.exists('.config/autostart/kintotray.py') + if tray_bool: + Popen(['cp',os.environ['HOME']+'/.config/kinto/kintotray.desktop',os.environ['HOME']+'/.config/autostart/kintotray.desktop']) + self.systray.disconnect(self.systray.signal_id) + self.systray.set_active(True) + self.systray.signal_id = self.systray.connect('activate',self.checkTray,False) + else: + Popen(['rm',os.environ['HOME']+'/.config/autostart/kintotray.desktop']) + Gtk.main_quit() + self.systray.disconnect(self.systray.signal_id) + self.systray.set_active(False) + self.systray.signal_id = self.systray.connect('activate',self.checkTray,True) + return + def refresh(self,button): self.refreshKB() @@ -340,7 +362,7 @@ class Indicator(): image = Gtk.Image() image.set_from_pixbuf(pixbuf) - with open('version', 'r') as file: + with open(os.environ['HOME']+'/.config/kinto/version', 'r') as file: verdata = file.read().replace('\n', '') version = Gtk.Label('Kinto v' + verdata) diff --git a/xkeysnail-config/xkeysnail.desktop b/xkeysnail-config/xkeysnail.desktop index 6b57886..5822f3e 100644 --- a/xkeysnail-config/xkeysnail.desktop +++ b/xkeysnail-config/xkeysnail.desktop @@ -3,6 +3,8 @@ Name=Kinto_xkey GenericName=Kinto_xkey Comment=Make Linux Type Like it's a Mac Exec=/bin/bash -c "grep -q 'autostart = true' {homedir}/.config/kinto/kinto.py && {xhost} +SI:localuser:root && sudo {systemctl} restart xkeysnail" +# Budgie logoff fix to avoid key input loss +# ;{homedir}/.config/kinto/logoff.sh # ;sudo /usr/local/bin/logoff.sh $USER $DISPLAY Terminal=false Type=Application diff --git a/xkeysnail_service.sh b/xkeysnail_service.sh index 25357c0..f002ce1 100755 --- a/xkeysnail_service.sh +++ b/xkeysnail_service.sh @@ -306,6 +306,7 @@ if [[ $1 == "1" || $1 == "2" || $1 == "3" || $1 == "4" || $1 == "winmac" || $1 = yes | cp -rf ./xkeysnail-config/killdups.sh ~/.config/kinto/killdups.sh yes | cp -rf ./xkeysnail-config/appleKB.sh ~/.config/kinto/appleKB.sh yes | cp -rf ./xkeysnail-config/trayapps/appindicator/kintotray.py ~/.config/kinto/kintotray.py + yes | cp -rf ./xkeysnail-config/trayapps/appindicator/kintotray.desktop ~/.config/kinto/kintotray.desktop yes | cp -rf ./xkeysnail-config/trayapps/BudgieApplet/icons/kinto-color-16.svg ~/.config/kinto/kinto-color.svg yes | cp -rf ./xkeysnail-config/trayapps/BudgieApplet/icons/kinto-invert-16.svg ~/.config/kinto/kinto-invert.svg yes | cp -rf ./xkeysnail-config/trayapps/BudgieApplet/icons/kinto-solid-16.svg ~/.config/kinto/kinto-solid.svg @@ -320,6 +321,7 @@ if [[ $1 == "1" || $1 == "2" || $1 == "3" || $1 == "4" || $1 == "winmac" || $1 = fi sed -i "s/{username}/`whoami`/g" ./xkeysnail-config/xkeysnail.service.new sed -i "s#{homedir}#`echo "$HOME"`#g" ./xkeysnail-config/xkeysnail.service.new + sed -i "s#{homedir}#`echo "$HOME"`#g" ~/.config/kinto/kintotray.desktop sed -i "s#{xhost}#`\\which xhost`#g" ./xkeysnail-config/xkeysnail.service.new sed -i "s/{username}/`whoami`/g" ./xkeysnail-config/limitedadmins.new sed -i "s#{systemctl}#`\\which systemctl`#g" ./xkeysnail-config/limitedadmins.new @@ -328,6 +330,7 @@ if [[ $1 == "1" || $1 == "2" || $1 == "3" || $1 == "4" || $1 == "winmac" || $1 = sudo chown root:root ./xkeysnail-config/limitedadmins.new sudo mv ./xkeysnail-config/limitedadmins.new /etc/sudoers.d/limitedadmins sed -i "s#{systemctl}#`\\which systemctl`#g" ~/.config/autostart/xkeysnail.desktop + yes | cp -rf ~/.config/kinto/kintotray.desktop ~/.config/autostart/kintotray.desktop if $installtray ; then sed -i "s#-c \"grep#-c \"python3 {homedir}/.config/kinto/kintotray.py;grep#g" ~/.config/autostart/xkeysnail.desktop sed -i "s#xkeysnail\"#xkeysnail;{homedir}/.config/kinto/logoff.sh\"#g" ~/.config/autostart/xkeysnail.desktop