updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / xkmap / xkmap
blob2919c9758b0c525d4ca1ec4cb6328930010833ea
1 #!/usr/bin/env python
3 # xkmap -- Simple GUI for 'setxkbmap'
5 # Using pygtk / glade / libglade to build GUI.
7 # Copyright (C) 2006-2007 Michael Towers
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 2 of the License, or
12 # (at your option) any later version.
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.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #-------------------------------------------------------------------
24 # Version 1.2 // 27th March 2007
26 # Set this for your system (probably one of these is OK)
27 base_lst = "/usr/share/X11/xkb/rules/base.lst"
28 #base_lst = "/etc/X11/xkb/rules/base.lst"
29 #base_lst = "/usr/lib/X11/xkb/rules/base.lst"
31 #-------------------------------------------------------------------
33 import os
34 import os.path
35 import sys
37 import pygtk
38 pygtk.require("2.0")
39 import gtk
40 import gtk.glade
43 class gui:
45 def __init__ (self, i_xkbset):
46 self.i_xkbset = i_xkbset
48 # Read the glade file
49 self.wTree = gtk.glade.XML("/usr/share/xkmap.glade")
51 # Create a dictionay of connections, then autoconnect
52 dic = { "on_window1_destroy" : gtk.main_quit,
53 "on_btCancel_clicked" : gtk.main_quit,
54 "on_btApply_clicked" : self.change,
55 "on_btOK_clicked" : self.changeq,
56 "on_cbModel_changed" : self.model,
57 "on_cbLayout_changed" : self.layout,
58 "on_cbVariant_changed": self.variant
60 self.wTree.signal_autoconnect(dic)
62 # Set up comboboxes
63 # Model
64 cellm = gtk.CellRendererText()
65 self.modelModel = gtk.ListStore(str)
66 self.cbModel = self.wTree.get_widget("cbModel")
67 self.cbModel.set_model(self.modelModel)
68 self.cbModel.pack_start(cellm, True)
69 self.cbModel.set_attributes(cellm, text=0)
71 i = 0
72 for item in self.i_xkbset.models: # item list is a list of strings
73 self.modelModel.append([item])
74 if item.split(None , 1)[0] == self.i_xkbset.model:
75 self.iModel = i
76 i += 1
77 self.cbModel.set_active(self.iModel)
79 # Layout
80 celll = gtk.CellRendererText()
81 self.modelLayout = gtk.ListStore(str)
82 self.cbLayout = self.wTree.get_widget("cbLayout")
83 self.cbLayout.set_model(self.modelLayout)
84 self.cbLayout.pack_start(celll, True)
85 self.cbLayout.set_attributes(celll, text=0)
87 i = 0
88 for item in self.i_xkbset.layouts: # item list is a list of strings
89 self.modelLayout.append([item])
90 if item.split(None , 1)[0] == self.i_xkbset.layout:
91 self.iLayout = i
92 i += 1
93 self.cbLayout.set_active(self.iLayout)
95 # Variant -- this depends on the chosen Layout!
96 cellv = gtk.CellRendererText()
97 self.modelVariant = gtk.ListStore(str)
98 self.cbVariant = self.wTree.get_widget("cbVariant")
99 self.cbVariant.set_model(self.modelVariant)
100 self.cbVariant.pack_start(cellv, True)
101 self.cbVariant.set_attributes(cellv, text=0)
103 self.showVariants ()
105 def showVariants (self):
106 self.modelVariant.clear ()
107 layout = self.i_xkbset.layouts[self.iLayout].split ()[0]
108 self.variants = self.i_xkbset.allVariants[layout]
109 self.iVariant = 0
110 i = 0
111 for item in self.variants: # item list is a list of strings
112 self.modelVariant.append([item])
113 if item.split(None , 1)[0] == self.i_xkbset.variant:
114 self.iVariant = i
115 i += 1
116 self.cbVariant.set_active(self.iVariant)
119 def change (self, widget):
120 self.i_xkbset.new ()
122 def changeq (self, widget):
123 self.change (widget)
124 gtk.main_quit ()
126 def model (self, widget):
127 i = self.cbModel.get_active()
128 if i != self.iModel:
129 self.iModel = i
130 self.i_xkbset.model = self.i_xkbset.models[i].split(None , 1)[0]
132 def layout (self, widget):
133 i = self.cbLayout.get_active()
134 if i != self.iLayout:
135 self.iLayout = i
136 self.i_xkbset.layout = self.i_xkbset.layouts[i].split(None , 1)[0]
137 self.showVariants ()
138 self.variant(None)
140 def variant (self, widget):
141 i = self.cbVariant.get_active()
142 if (i != self.iVariant) or (widget == None):
143 self.iVariant = i
144 self.i_xkbset.variant = self.variants[i].split(None , 1)[0]
147 def mainloop (self):
148 gtk.main ()
152 class xkbset:
153 def __init__ (self):
154 self.configfile = os.path.expanduser("~/.xkmap")
155 # default values
156 self.model = "pc101"
157 self.layout = "us"
158 self.variant = "Standard"
159 if os.path.isfile (self.configfile):
160 f = open (self.configfile)
161 self.model, self.layout, self.variant = f.readline ().strip ().split ("|")
162 f.close ()
164 # Read 'base.lst'
165 blf = open(base_lst)
166 while blf.readline().strip() != "! model": pass
168 self.models = []
169 while True:
170 line = blf.readline().strip()
171 if not line: continue
172 if line == "! layout": break
173 self.models.append (line)
175 self.layouts = []
176 while True:
177 line = blf.readline().strip()
178 if not line: continue
179 if line == "! variant": break
180 self.layouts.append (line)
182 self.allVariants = {}
183 while True:
184 line = blf.readline().strip()
185 if not line: continue
186 if line == "! option": break
187 parts = line.split (None, 2)
188 line = parts[0] + " - " + parts[2]
189 layout = parts[1].rstrip (":")
190 if not self.allVariants.has_key (layout):
191 self.allVariants[layout] = [ "Standard" ]
192 self.allVariants[layout].append (line)
194 blf.close()
197 def new (self):
198 m = self.model
199 l = self.layout
200 v = self.variant
201 if v == "Standard":
202 v = '""'
204 print "setxkbmap -rules xorg -model %s -layout %s -variant %s" % (m, l, v)
205 os.system ("setxkbmap -rules xorg -model %s -layout %s -variant %s" % (m, l, v))
207 f = open (self.configfile, "w")
208 f.write ("%s|%s|%s\n" % (self.model, self.layout, self.variant))
209 f.close ()
211 if __name__ == "__main__":
212 i_xkbset = xkbset ()
213 i_gui = gui (i_xkbset)
214 i_gui.mainloop ()