summaryrefslogtreecommitdiff
path: root/.config/wmii-hg/config.sh
blob: 02ec65b30e7a1ce2a496c4ec5eb67fb6fb502da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/hint/bash

MODKEY=Mod4 # "super"
UP=p
DOWN=n
LEFT=b
RIGHT=f

HIST="$XDG_CACHE_HOME/wmii/history"
mkdir -p -- "${HIST%/*}"

# Colors tuples: "<text> <background> <border>"
. theme-abyss

log() {
	echo "wmiirc[$$]: $*"
}

Event() {
	local event=$1; shift;
	case "$event" in
		## Mouse event meanings:
		##   MouseDown = mouse down
		##   Click     = mouse up
		##   DND       = hover, while dragging something (Drag 'n' Drop)
		##               The associated mouse button is always "1"
		##
		## Mouse-button IDs:
		##   1 = left
		##   2 = middle
		##   3 = right
		##   4 = scroll up
		##   5 = scroll down

		## Custom (non-WMII-generated) events
		WmiircStart) ## No args
			log " ==> Starting wmiirc[$$]"
			is_mounted $WMII_DIR && Event WmiircUnmount
			Event WmiircMount

			# Configure wmii
			< "`conffile rules`"    expand_variables >> $WMII_DIR/rules
			< "`conffile ctl-init`" expand_variables >> $WMII_DIR/ctl
			scansection Key | cut -sf2               >> $WMII_DIR/keys
			#hsetroot -solid "$WMII_BACKGROUND"

			# Clear the LBar and RBar
			find $WMII_DIR/{l,r}bar -type f -delete
			# Populate the LBar by emulating [Create|Focus]Tag events
			local tag seltag="$(sed 1q $WMII_DIR/tag/sel/ctl)"
			while read -r tag; do
				Event CreateTag "$tag"
				if [ "$tag" = "$seltag" ]; then
					Event FocusTag "$tag"
				fi
			done < <(lstags)
			;;
		WmiircQuit) ## No args
			trap - EXIT
			log " ==> Stopping wmiirc[$$]"
			exit;;
		WmiircMount) ## No args
			log "  -> Creating mountpoint WMII_DIR=$WMII_DIR..."
			mkdir -p "$WMII_DIR"
			log "  -> Mounting WMII_DIR=$WMII_DIR..."
			mount.9p "$WMII_ADDRESS" "$WMII_DIR"
			;;
		WmiircUnmount) ## No args
			log "  -> Unmounting WMII_DIR=$WMII_DIR..."
			umount.9p "$WMII_DIR"
			log "  -> Removing mountpoint WMII_DIR=$WMII_DIR..."
			rmdir -p "$WMII_DIR" 2>/dev/null;;
		## WMII-meta events
		Quit) ## No args
			trap - EXIT
			systemd-notify STOPPING=1 || true
			Event WmiircUnmount
			Event WmiircQuit;;
		Warning) ## $@=string
			notify-send "wmii warning: $*";;
		Key) ## $1=keystroke
			Key "$@";;

		## WMII Window management events
		FocusFloating) ## No args
			;;
		AreaFocus) ## $1=area
			;;
		DestroyArea) ## $1=area
			;;
		ColumnFocus) ## $1=column
			;;
		CreateColumn) ## $1=column
			;;

		## Client events
		CreateClient) ## $1=client
			;;
		DestroyClient) ## $1=client
			;;
		Urgent) ## $1=client $2=[Manager|Client]
			;;
		NotUrgent) ## $1=client $2=[Manager|Client]
			;;
		ClientMouseDown) ## $1=client $2=button
			local client=$1
			local button=$2
			case $button in
				1) ;;
				2) ;;
				3)
					{
						case $(wmii9menu Delete Fullscreen ToggleFloat SendLeft SendRight) in
							Delete)
								echo kill >> $WMII_DIR/client/$client/ctl;;
							Fullscreen)
								echo Fullscreen on >> $WMII_DIR/client/$client/ctl;;
							ToggleFloat)
								echo send $client toggle >> $WMII_DIR/tag/sel/ctl;;
							SendLeft)
								echo send sel left >> $WMII_DIR/tag/sel/ctl;;
							SendRight)
								echo send sel right >> $WMII_DIR/tag/sel/ctl;;
						esac
					}& ;;
				4) ;;
				5) ;;
			esac;;
		ClientClick) ## $1=client $2=button
			;;
		Unresponsive) ## $1=client
			local client=$1
			local client_name=$(< $WMII_DIR/client/$client/label)
			local msg resp
			msg="The client \`${client_name}' is not responding."
			msg+=" What would you like to do?"
			{
				resp=$(wihack -transient $client \
					xmessage -nearmouse -buttons Kill,Wait -print "$msg")
				if [ "$resp" = Kill ]; then
					echo slay >> $WMII_DIR/client/$client/ctl
				fi
			}& ;;
		Fullscreen) ## $1=client $2=[on|off]
			# TODO: hide any clients with the 'panel' class
			;;

		## Tag events
		CreateTag) ## $1=tag
			echo label "$*" >> $WMII_DIR/lbar/$1;;
		DestroyTag) ## $1=tag
			rm $WMII_DIR/lbar/$1;;
		FocusTag) ## $1=tag
			echo colors "$WMII_FOCUSCOLORS"  >> $WMII_DIR/lbar/$1;;
		UnfocusTag) ## $1=tag
			echo colors "$WMII_NORMCOLORS"   >> $WMII_DIR/lbar/$1;;
		UrgentTag) ## $1=[Manager|Client] $2=tag
			echo colors "$WMII_URGENTCOLORS" >> $WMII_DIR/lbar/$2;;
		NotUrgentTag) ## $1=[Manager|Client] $2=tag
			echo colors "$WMII_NORMCOLORS"   >> $WMII_DIR/lbar/$2;;

		## LeftBar events (usually tag buttons)
		LeftBarMouseDown) ## $1=button $2=bar_item
			;;
		LeftBarClick) ## $1=button $2=bar_item
			shift # ignore the button
			echo view "$@" >> $WMII_DIR/ctl;;
		LeftMouseDND) ## $1=button $2=bar_item
			Event LeftBarClick "$@";;

		## RightBar events
		RightBarMouseDown) ## $1=button $2=bar_item
			local button=$1
			local item=$2
			local name=${item#[0-9][0-9]_}
			name=${name%.*}
			Action "rbar_$name" "$button" & ;;
		RightBarClick) ## $1=button $2=bar_item
			;;
		RightMouseDND) ## $1=button $2=bar_item
			Event RightBarClick "$@";;
	esac
}

Key() {
	local key=$1
	case "$key" in
		## Moving around

		$MODKEY-$LEFT) ## Select the client to the left
			echo select left   >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-$RIGHT) ## Select the client to the right
			echo select right  >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-$UP) ## Select the client above
			echo select up     >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-$DOWN) ## Select the client below
			echo select down   >> $WMII_DIR/tag/sel/ctl;;

		$MODKEY-space) ## Toggle between floating and managed layers
			echo select toggle >> $WMII_DIR/tag/sel/ctl;;

		## Moving clients around

		$MODKEY-Shift-$LEFT) ## Move selected client to the left
			echo send sel left   >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-Shift-$RIGHT) ## Move selected client to the right
			echo send sel right  >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-Shift-$UP) ## Move selected client up
			echo send sel up     >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-Shift-$DOWN) ## Move selected client down
			echo send sel down   >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-Shift-space) ## Toggle selected client between floating and managed layers
			echo send sel toggle >> $WMII_DIR/tag/sel/ctl;;

		## Moving through stacks
		$MODKEY-Control-$UP) ## Select the stack above
			echo select up   stack >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-Control-$DOWN) ## Select the stack below
			echo select down stack >> $WMII_DIR/tag/sel/ctl;;

		## Client actions
		$MODKEY-shift-1) ## Toggle selected client's fullsceen state
			echo Fullscreen toggle >> $WMII_DIR/client/sel/ctl;;
		$MODKEY-shift-0) ## Close client
			echo kill >> $WMII_DIR/client/sel/ctl;;

		## Changing column modes
		$MODKEY-d) ## Set column to default mode
			echo colmode sel default-max >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-s) ## Set column to stack mode
			echo colmode sel stack-max   >> $WMII_DIR/tag/sel/ctl;;
		$MODKEY-m) ## Set column to max mode
			echo colmode sel stack+max   >> $WMII_DIR/tag/sel/ctl;;

		## Running programs
		$MODKEY-a) ## Open wmii actions menu
			local action
			action="$(path_ls "$WMII_CONFPATH" | wimenu -p 'S-a' -h "${HIST}.actions" -n 5000)" || return
			Action "$action" & ;;
		$MODKEY-x) ## Open program menu
			local command
			command="$(path_ls "$_PATH" | wimenu -p 'S-x' -h "${HIST}.progs" -n 5000)" || return
			runcmd "$command" & ;;
		$MODKEY-Return) ## Launch a terminal
			runcmd x-terminal-emulator & ;;

		## Tag actions
		$MODKEY-t) ## Change to another tag
			local tag
			tag=$(lstags | wimenu -p 'S-t' -h "${HIST}.tags" -n 50) || return
			echo view "$tag" >> $WMII_DIR/ctl;;
		$MODKEY-Shift-t) ## Retag the selected client
			local sel tag
			sel=$(sed 1q $WMII_DIR/client/sel/ctl)
			tag=$(lstags | wimenu -p 'S-T' -h "${HIST}.tags" -n 50) || return
			echo "$tag" >> $WMII_DIR/client/$sel/tags;;

		## Hardware keys
		XF86RotateWindows) ## Toggle between 'normal' an 'right'
			local old new
			old="$(xrotate|cut -d $'\t' -f3|sort -u)"
			case "$old" in
				normal) new=right;;
				*)      new=normal;;
			esac
			xrotate "$new"
			;;
		$MODKEY-XF86RotateWindows) ## Rotate in 90° steps
			local old new
			old="$(xrotate|cut -d $'\t' -f3|sort -u)"
			case "$old" in
				normal)   new=right;;
				right)    new=inverted;;
				inverted) new=left;;
				left)     new=normal;;
			esac
			xrotate "$new"
			;;
	esac
}

runcmd() (
	PATH=$_PATH
	unset NOTIFY_SOCKET # systemd
	unset WMII_CONFPATH # wmii
	unset WMII_ADDRESS # wmii
	unset _PATH WMII_NAMESPACE WMII_DIR # wmiirc
	exec 8>&- # xinit/systemd handshake
	exec systemd-run --user --scope -- sh -c "$*"
)

Action() {
	local prog=$(path_which "$WMII_CONFPATH" "$1"); shift
	if [ -n "$prog" ]; then
		exec "$prog" "$@"
	else
		exec false
	fi
}