Skip to content

Instantly share code, notes, and snippets.

@xlab
Created July 26, 2012 13:49
Show Gist options
  • Save xlab/3182119 to your computer and use it in GitHub Desktop.
Save xlab/3182119 to your computer and use it in GitHub Desktop.
Matchbox keyboard applet, PyGTK
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
# Matchbox-keyboard Applet
# (C) 2009 Maxim Kouprianov <http://kc.vc>
import pygtk
pygtk.require('2.0')
import gtk
import os
import commands
import string
class MKA:
def __init__(self):
self.last="default"
self.statusIcon = gtk.StatusIcon()
self.statusIcon.set_from_file("/usr/share/pixmaps/keyboard.svg")
self.statusIcon.set_visible(True)
self.statusIcon.connect('activate', self.on_activate)
self.menu = gtk.Menu()
self.lmenu = gtk.Menu()
self.layouts = gtk.MenuItem("Раскладки")
self.layouts.set_submenu(self.lmenu)
a = commands.getoutput("ls /usr/share/matchbox-keyboard/ | grep -o -e 'keyboard-[a-zA-Z-]*.xml' | sed -e 's/.xml//g' -e 's/keyboard-//g'")
layouts = a.split('\n')
self.button = gtk.RadioMenuItem(None, "По-умолчанию")
self.button.set_active(True)
self.button.connect("toggled", self.app_layout, "default")
self.lmenu.append(self.button)
for layout in layouts:
self.button = gtk.RadioMenuItem(self.button, layout)
self.button.connect("toggled", self.app_layout, layout)
self.lmenu.append(self.button)
self.button.set_flags(gtk.CAN_DEFAULT)
self.button.grab_default()
self.menu.append(self.layouts)
self.menuItem = gtk.CheckMenuItem("Запущена")
if not commands.getoutput("fuser /usr/bin/matchbox-keyboard"):
os.system("matchbox-keyboard -d &")
self.menuItem.set_active(True)
self.menuItem.connect('activate', self.run_cb, self.statusIcon)
self.menu.append(self.menuItem)
self.menuItem = gtk.CheckMenuItem("Автовсплыв")
try:
os.stat("/usr/lib/gtk-2.0/2.10.0/immodules/libmb-im-invoker.so")
self.menuItem.set_active(True)
except:
self.menuItem.set_active(False)
self.menuItem.connect('toggled', self.up_toggle_cb, self.statusIcon)
self.menu.append(self.menuItem)
self.menuItem = gtk.MenuItem("Выход")
self.menuItem.connect('activate', self.quit_cb, self.statusIcon)
self.menu.append(self.menuItem)
self.statusIcon.connect('popup-menu', self.popup_menu_cb, self.menu)
self.statusIcon.set_visible(1)
gtk.main()
def run_cb(self, widget, event, data = None):
if widget.get_active():
os.system("matchbox-keyboard -d &")
else:
os.system("killall matchbox-keyboard")
def up_toggle_cb(self, widget, event, data = None):
if widget.get_active():
os.system("mv /usr/lib/gtk-2.0/2.10.0/immodules/libmb-im-invoker.so2 /usr/lib/gtk-2.0/2.10.0/immodules/libmb-im-invoker.so")
else:
os.system("mv /usr/lib/gtk-2.0/2.10.0/immodules/libmb-im-invoker.so /usr/lib/gtk-2.0/2.10.0/immodules/libmb-im-invoker.so2")
def app_layout(self, widget, data=None):
if data=="default" or data==None:
os.system("killall matchbox-keyboard")
os.system("matchbox-keyboard -d &")
elif data!=self.last:
os.system("killall matchbox-keyboard")
os.system("matchbox-keyboard -d %s &" % data)
self.last=data
def quit_cb(self, widget, data = None):
os.system("killall matchbox-keyboard")
gtk.main_quit()
def on_activate(self, si):
os.system("mkt")
def popup_menu_cb(self, widget, button, time, data = None):
if button == 3:
if data:
data.show_all()
data.popup(None, None, gtk.status_icon_position_menu,
2, time, self.statusIcon)
if __name__ == "__main__":
mka = MKA()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment