]> git.decadent.org.uk Git - ion3-doc.git/blob - conf-bindings.tex
[svn-upgrade] Integrating new upstream version, ion3-doc (20080103)
[ion3-doc.git] / conf-bindings.tex
1 \section{Keys and rodents}
2 \label{sec:bindings}
3
4 In the stock configuration file setup, most key and mouse bindings are set
5 from the file \file{cfg\_ioncore.lua} while module-specific bindings
6 are set from the modules' main configuration files (\file{cfg\_modname.lua}).
7 This, however, does not have to be so as long as the module has been
8 loaded prior to defining any module-specific bindings.
9
10 Bindings are defined by calling the function 
11 \fnrefx{ioncore}{defbindings} with the ``context'' of the
12 bindings and the a table of new bindings to make. The context is simply
13 string indicating one of the classes of regions (or modes such as
14 \type{WMoveresMode}) introduced in section \ref{sec:objects}, and fully
15 listed in appendix \ref{app:fullhierarchy}, although not all define
16 a binding map. For example, the following skeleton would be used to 
17 define new bindings for all frames:
18
19 \begin{verbatim}
20 defbindings("WFrame", {
21     -- List of bindings to make goes here.
22 })
23 \end{verbatim}
24
25 There has been some confusion among users about the need to define the
26 ``context'' for each binding, so let me try to explain this design
27 decision here. The thing is that if there was a just a simple 'bind this 
28 key to this action' method without knowledge of the context, some 
29 limitations would have to be made on the available actions and writing 
30 custom handlers would be more complicated. In addition one may want to 
31 bind the same function to different key for different types of objects.
32 Indeed, the workspace and frame tab switching functions are the same both
33 classes being based on \type{WMPlex}, and in the stock configuration the 
34 switch to $n$:th workspaces is bound to \key{Mod1+n} while the switch to 
35 $n$:th tab is bound to the sequence \key{Mod1+k n}.
36
37 Currently known contexts include: 
38 \codestr{WScreen},
39 \codestr{WMPlex},
40 \codestr{WMPlex.toplevel},
41 \codestr{WFrame},
42 \codestr{WFrame.toplevel},
43 \codestr{WFrame.floating},
44 \codestr{WFrame.tiled},
45 \codestr{WFrame.transient},
46 \codestr{WMoveresMode},
47 \codestr{WGroup},
48 \codestr{WGroupCW},
49 \codestr{WGroupWS},
50 \codestr{WClientWin},
51 \codestr{WTiling}, and
52 \codestr{WStatusBar}.
53 Most of these should be self-explanatory, corresponding to objects
54 of class with the same name. The ones with \codestr{.toplevel} suffix
55 refer to screens and ``toplevel''  frames, i.e. frames that are
56 not used for transient windows. Likewise \codestr{.transient} refers
57 to frames in transient mode, and \codestr{.tiled} and \codestr{.floating}
58 to frames in, respectively, tiled and floating modes. 
59
60                                                         
61
62 The following subsections describe how to construct elements of the
63 binding table. Note that \fnrefx{ioncore}{defbindings} adds
64 the the newly defined bindings to the previous bindings of the context,
65 overriding duplicates. To unbind an event, set the handler parameter
66 to \code{nil} for each of the functions to be described in the following
67 subsections.
68
69 Also note that when multiple objects want to handle a binding, the 
70 innermost (when the root window is considered the outermost) active object
71 in the parent--child hierarchy (see Figure \ref{fig:parentship}) of objects 
72 gets to handle the action.
73
74
75 \subsection{Binding handlers and special variables}
76
77 Unlike in Ion2, in Ion3 binding handlers are not normally passed as
78 ``anonymous functions'', although this is still possible. The preferred
79 method now is to pass the code of the handler as a string. Two following
80 special variables are available in this code.
81
82 \begin{tabularx}{\linewidth}{lX}
83     \tabhead{Variable & Description}
84     \code{_} (underscore) &
85       Reference to the object on which the 
86       binding was triggered. The object is of the same class as the the
87       context of the \fnrefx{ioncore}{defbindings} call
88       defining the binding. \\
89     \code{_sub} &
90       Usually, the currently active \emph{managed object} of the 
91       object referred to by \code{_}, but sometimes (e.g. mouse actions
92       on tabs of frames) something else relevant to the action triggering
93       the binding. \\
94     \code{_chld} &
95       Object corresponding to the currently active child window of the
96        object referred to by \code{_}. This should seldom be needed.
97 \end{tabularx}
98
99 For example, supposing \code{_} (underscore) is a \type{WFrame}, the 
100 following handler should move the active window to the right, if 
101 possible:
102
103 \begin{verbatim}
104 "_:inc_index(_sub)"
105 \end{verbatim}
106
107 \subsection{Guards}
108
109 To suppress error messages, each binding handler may also be accompanied
110 by a ``guard'' expression that blocks the handler from being called when
111 the guard condition is not met. Currently the following guard expressions
112 are supported (for both \code{_sub} and \code{_chld}):
113
114 \begin{tabularx}{\linewidth}{lX}
115     \tabhead{Guard & Description}
116     \codestr{\_sub:non-nil} & The \code{_sub} parameter must be set. \\
117     \codestr{\_sub:SomeClass} & The \code{_sub} parameter must be member
118       of class \type{SomeClass}. \\
119 \end{tabularx}
120
121
122 \subsection{Defining the bindings}
123 \label{sec:binddef}
124
125 The descriptions of the individual bindings in the binding table argument
126 to \fnrefx{ioncore}{defbindings} should be constructed with the following
127 functions.
128
129 Key presses:
130 \begin{itemize}
131     \item \fnrefx{ioncore}{kpress}, and
132           \fnrefx{ioncore}{kpress_wait}\code{(keyspec, handler [, guard])}.
133     \item \fnrefx{ioncore}{submap}\code{(keyspec, \{ ... more key bindings ... \})}.
134     \item \fnrefx{ioncore}{submap_enter}, and
135           \fnrefx{ioncore}{submap_wait}\code{(handler [, guard])}.
136 \end{itemize}
137 Mouse actions:
138 \begin{itemize}
139     \item \fnrefx{ioncore}{mclick},
140           \fnrefx{ioncore}{mdblclick},
141           \fnrefx{ioncore}{mpress}, and
142           \fnrefx{ioncore}{mdrag}\code{(buttonspec, handler [, guard])}.
143 \end{itemize}
144
145 The actions that most of these functions correspond to should be clear
146 and as explained in the reference, \fnrefx{ioncore}{kpress_wait} is simply
147 \fnrefx{ioncore}{kpress} with a flag set instructing Ioncore wait for
148 all modifiers to be released before processing any further actions.
149 This is to stop one from accidentally calling e.g.
150 \fnref{WRegion.rqclose} multiple times in a row. The 
151 \fnrefx{ioncore}{submap} function is used to define submaps or
152 ``prefix maps''. The second argument to this function is table listing
153 the key press actions (\fnrefx{ioncore}{kpress}) in the submap. 
154 The \fnrefx{ioncore}{submap_enter} handler is called when the submap
155 is entered, in which this handler is defined. Likewise, the
156 \fnrefx{ioncore}{submap_wait} handler is  called when all modifiers
157 have been released while waiting for further key presses in the submap.
158
159 The parameters \var{keyspec} and \var{buttonspec} are explained below
160 in detail. The parameter \var{handler} is the handler for the binding,
161 and the optional parameter \var{guard} its guard. These should normally
162 be strings as explained above. 
163
164 \subsection{Examples}
165
166 For example, to just bind the key \key{Mod1+1} to switch to the first
167 workspace and \key{Mod1+Right} to the next workspace, you would make the
168 following call
169 \begin{verbatim}
170 defbindings("WScreen", {
171     kpress("Mod1+Right", "_:switch_next()"),
172     kpress("Mod1+1", "_:switch_nth(1)"),
173 })
174 \end{verbatim}
175
176 Note that \code{_:switch_nth(1)} is the same as calling
177 \fnref{WMPlex.switch_next}\code{(_, 1)} as \type{WScreen} inherits
178 \type{WMPlex} and this is where the function is actually defined.
179
180 Similarly to the above example, to bind the key sequence \key{Mod1+k n} 
181 switch to the next managed object within a frame, and \key{Mod1+k 1} to the
182 first, you would issue the following call:
183 \begin{verbatim}
184 defbindings("WFrame", {
185     submap("Mod1+K", {
186         kpress("Right", "_:switch_next()"),
187         kpress("1", "_:switch_nth(1)"),
188    }),
189 })
190 \end{verbatim}
191
192
193 \subsection{Key specifications}
194
195 As seen above, the functions that create key binding specifications require
196 a \var{keyspec} argument. This argument should be a string containing the
197 name of a key as listed in the X header file \file{keysymdef.h}%
198 \footnote{This file can usually be found in the directory
199 \file{/usr/X11R6/include/X11/}.} without the \code{XK_} prefix.
200 \index{keysymdef.h@\file{keysymdef.h}}
201 Most of the key names are quite intuitive while some are not. For example,
202 the \key{Enter} key on the main part of the keyboard has the less common
203 name \key{Return} while the one the numpad is called \key{KP\_Enter}.
204
205 The \var{keyspec} string may optionally have multiple ``modifier'' names
206 followed by a plus sign (\code{+}) as a prefix. X defines the following
207 modifiers:
208
209 \key{Shift}, \key{Control}, \key{Mod1} to \key{Mod5},
210 \key{AnyModifier} and \key{Lock}.
211 \index{Shift@\key{Shift}}
212 \index{Control@\key{Control}}
213 \index{ModN@\key{ModN}}
214 \index{AnyModifier@\key{AnyModifier}}
215 \index{Lock@\key{Lock}}
216
217 X allows binding all of these modifiers to almost any key and while this
218 list of modifiers does not explicitly list keys such as 
219 \key{Alt}\index{Alt@\key{Alt}} that are common on modern keyboards, such
220 keys are bound to one of the \key{ModN}. On systems running XFree86
221 \key{Alt} is usually \key{Mod1}. On Suns \key{Mod1} is the diamond key
222 and \key{Alt} something else. One of the ``flying window'' keys on so
223 called Windows-keyboards is probably mapped to \key{Mod3} if you have
224 such a key. Use the program \file{xmodmap}\index{xmodmap@\file{xmodmap}}
225 to find out what exactly is bound where. 
226
227 Ion defaults to \key{AnyModifier} in submaps. This can sometimes lead to
228 unwanted effects when the same key is used with and without explicitly
229 specified modifiers in nested regions. For this reason, Ion recognises
230 \key{NoModifier} as a special modifier that can be used to reset this
231 default.
232
233 Ion ignores the \key{Lock} modifier and any \key{ModN} ($N=1{\ldots} 5$)
234 bound to \key{NumLock}\index{NumLock@\key{NumLock}} or
235 \key{ScrollLock}\index{ScrollLock@\key{ScrollLock}}
236 by default because such\footnote{Completely useless keys that should be
237 gotten rid of in the author's opinion.} locking keys may otherwise
238 cause confusion.
239
240
241 \subsection{Button specifications}
242
243 Button specifications are similar to key definitions but now
244 instead of specifying modifiers and a key, you specify modifiers
245 and one of the button names \key{Button1} to
246 \key{Button5}\index{Button-n@\key{Button-n}}. Additionally the
247 specification may end with an optional area name following an @-sign.
248 Only frames currently support areas, and the supported values in this
249 case are
250 \codestr{border}, \codestr{tab}, \codestr{empty\_tab}, \codestr{client} 
251 and \code{nil} (for the whole frame).
252
253 For example, the following code binds dragging a tab with the first 
254 button pressed to initiate tab drag\&drop handling:
255
256 \begin{verbatim}
257 defbindings("WFrame", {
258     mdrag("Button1@tab", "_:p_tabdrag()"),
259 })
260 \end{verbatim}
261
262
263 \subsection{A further note on the default binding configuration}
264
265 The default binding configuration contains references to the variables
266 \code{META} and \code{ALTMETA} instead of directly using the default
267 values of \codestr{Mod1+} and \codestr{} (nothing). As explained in
268 section \ref{sec:walkthrough}, the definitions of these variables
269 appear in \file{cfg\_ion.lua}. This way you can easily change the the
270 modifiers used by all bindings in the default configuration without 
271 changing the whole binding configuration. Quite a few people prefer 
272 to use the Windows keys as modifiers because many applications already
273 use \key{Alt}. Nevertheless, \key{Mod1} is the default as a key bound 
274 to it is available virtually everywhere.
275