#!/usr/bin/env python # -*- coding: utf-8 -*- # # xkmap -- Simple GUI for 'setxkbmap' # # Using pygtk / glade / libglade to build GUI. # # Copyright (C) 2006-2007 Michael Towers # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # #------------------------------------------------------------------- # Version 1.2 // 27th March 2007 # Set this for your system (probably one of these is OK) base_lst = "/usr/share/X11/xkb/rules/base.lst" #base_lst = "/etc/X11/xkb/rules/base.lst" #base_lst = "/usr/lib/X11/xkb/rules/base.lst" #------------------------------------------------------------------- import os import os.path import sys import pygtk pygtk.require("2.0") import gtk import gtk.glade class gui: def __init__ (self, i_xkbset): self.i_xkbset = i_xkbset # Read the glade file self.wTree = gtk.glade.XML("/usr/share/xkmap/xkmap.glade") # Create a dictionay of connections, then autoconnect dic = { "on_window1_destroy" : gtk.main_quit, "on_btCancel_clicked" : gtk.main_quit, "on_btApply_clicked" : self.change, "on_btOK_clicked" : self.changeq, "on_cbModel_changed" : self.model, "on_cbLayout_changed" : self.layout, "on_cbVariant_changed": self.variant } self.wTree.signal_autoconnect(dic) # Set up comboboxes # Model cellm = gtk.CellRendererText() self.modelModel = gtk.ListStore(str) self.cbModel = self.wTree.get_widget("cbModel") self.cbModel.set_model(self.modelModel) self.cbModel.pack_start(cellm, True) self.cbModel.set_attributes(cellm, text=0) i = 0 for item in self.i_xkbset.models: # item list is a list of strings self.modelModel.append([item]) if item.split(None , 1)[0] == self.i_xkbset.model: self.iModel = i i += 1 self.cbModel.set_active(self.iModel) # Layout celll = gtk.CellRendererText() self.modelLayout = gtk.ListStore(str) self.cbLayout = self.wTree.get_widget("cbLayout") self.cbLayout.set_model(self.modelLayout) self.cbLayout.pack_start(celll, True) self.cbLayout.set_attributes(celll, text=0) i = 0 for item in self.i_xkbset.layouts: # item list is a list of strings self.modelLayout.append([item]) if item.split(None , 1)[0] == self.i_xkbset.layout: self.iLayout = i i += 1 self.cbLayout.set_active(self.iLayout) # Variant -- this depends on the chosen Layout! cellv = gtk.CellRendererText() self.modelVariant = gtk.ListStore(str) self.cbVariant = self.wTree.get_widget("cbVariant") self.cbVariant.set_model(self.modelVariant) self.cbVariant.pack_start(cellv, True) self.cbVariant.set_attributes(cellv, text=0) self.showVariants () def showVariants (self): self.modelVariant.clear () layout = self.i_xkbset.layouts[self.iLayout].split ()[0] self.variants = self.i_xkbset.allVariants[layout] self.iVariant = 0 i = 0 for item in self.variants: # item list is a list of strings self.modelVariant.append([item]) if item.split(None , 1)[0] == self.i_xkbset.variant: self.iVariant = i i += 1 self.cbVariant.set_active(self.iVariant) def change (self, widget): self.i_xkbset.new () def changeq (self, widget): self.change (widget) gtk.main_quit () def model (self, widget): i = self.cbModel.get_active() if i != self.iModel: self.iModel = i self.i_xkbset.model = self.i_xkbset.models[i].split(None , 1)[0] def layout (self, widget): i = self.cbLayout.get_active() if i != self.iLayout: self.iLayout = i self.i_xkbset.layout = self.i_xkbset.layouts[i].split(None , 1)[0] self.showVariants () self.variant(None) def variant (self, widget): i = self.cbVariant.get_active() if (i != self.iVariant) or (widget == None): self.iVariant = i self.i_xkbset.variant = self.variants[i].split(None , 1)[0] def mainloop (self): gtk.main () class xkbset: def __init__ (self): self.configfile = os.path.expanduser("~/.xkmap") # default values self.model = "pc104" self.layout = "us" self.variant = "Standard" if os.path.isfile (self.configfile): f = open (self.configfile) self.model, self.layout, self.variant = f.readline ().strip ().split ("|") f.close () # Read 'base.lst' blf = open(base_lst) while blf.readline().strip() != "! model": pass self.models = [] while True: line = blf.readline().strip() if not line: continue if line == "! layout": break self.models.append (line) self.layouts = [] while True: line = blf.readline().strip() if not line: continue if line == "! variant": break self.layouts.append (line) self.allVariants = {} while True: line = blf.readline().strip() if not line: continue if line == "! option": break parts = line.split (None, 2) line = parts[0] + " - " + parts[2] layout = parts[1].rstrip (":") if not self.allVariants.has_key (layout): self.allVariants[layout] = [ "Standard" ] self.allVariants[layout].append (line) blf.close() def new (self): m = self.model l = self.layout v = self.variant if v == "Standard": v = '""' print "setxkbmap -model %s -layout %s -variant %s" % (m, l, v) os.system ("setxkbmap -model %s -layout %s -variant %s" % (m, l, v)) # f = open (self.configfile, "w") # f.write ("%s|%s|%s\n" % (self.model, self.layout, self.variant)) # f.close () if __name__ == "__main__": i_xkbset = xkbset () i_gui = gui (i_xkbset) i_gui.mainloop ()