-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathikbdop.sh
216 lines (187 loc) · 4.52 KB
/
ikbdop.sh
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
#
# USE: This simple shell program can be used to disable the internal keyboard of a laptop in case you want to use external keyboard. This can be done by
# $ ./ikbdop.sh detach
#
# To attach the internal keyboard again, use the command
# $ ./ikbdop.sh attach
#
# USAGE: When the internal laptop keyboard goes defective, it usually results in continuous generation of some keyboard character, thereby making it impossible to do any commandline stuff. This shell program is an easy fix.
#
#
# Author: anitaggu@gmail.com
#
#!/bin/bash
#-----------------------------------------------------
# usage()
#
# ARGS: All commandline arguments passed to shell.
#
# Checks if option is either status, detach or attach
# Exits program if improper usage
#
# RETURNS: Nothing
#-----------------------------------------------------
usage()
{
USAGE=`echo "$0 [status | detach | attach ]"`
if [ $# -ne 1 ]
then
echo $USAGE
exit 1
fi
}
readonly mode_attached=2
readonly mode_detached=3
readonly attach_pattern='AT Translated Set 2 keyboard[[:space:]]*id=[[:digit:]]*[[:space:]]*\[slave keyboard \([[:digit:]]*\)\]'
readonly detach_pattern='AT Translated Set 2 keyboard[[:space:]]*id=[[:digit:]]*[[:space:]]*\[floating slave\]'
readonly masterkbd_pattern='\[slave[[:space:]]*keyboard[[:space:]]*\([[:digit:]]*\)\]'
#-----------------------------------------------------------
# find_status()
#
# ARGS: None
#
# Uses the output of xinput list command to detect whether
# keyboard attached or detached
#
# RETURNS:
# 2 if keyboard attached
# 3 if keyboard dettached
#-----------------------------------------------------------
find_status() {
xinput list | awk -v attach_pattern='$attach_pattern' \
-v detach_pattern='$detachpattern' \
-v mode_attached='$mode_attached' \
-v mode_detached='$mode_detached' \
'
/'"$attach_pattern"'/ {
#print "Internal Keyboard attached"
mode='"$mode_attached"' #kbd attached
}
/'"$detach_pattern"'/ {
#print "Internal Keyboard detached"
mode='"$mode_detached"' #kbd dettached
}
END {
exit mode
}'
return $?
}
#-------------------------------------------------------------
# find_attached_kbd_id()
#
# ARGS: None
# PRE-CONDITION: The internal keyboard must be attached.
# RETURNS: device id of the attached internal keyboard
#-------------------------------------------------------------
find_attached_kbd_id()
{
xinput list | awk -v attach_pattern='$attach_pattern' '
/'"$attach_pattern"'/ {
id=$7
gsub("id=","", id)
#print id
}
END {
exit id
}'
return $?
}
#
# find_detached_kbd_id()
#
# ARGS: None
# PRE-CONDITION: The internal keyboard must be detached.
#
# Function returns the device id
#
find_detached_kbd_id()
{
xinput list | awk -v detach_pattern='$attach_pattern' '
/'"$detach_pattern"'/ {
id=$7
gsub("id=","", id)
#print id
}
END {
exit id
}'
return $?
}
find_master_kbd_id()
{
xinput list | awk -v masterkbd_pattern='$masterkbd_pattern' '
BEGIN {
masterid = -1 # impossible master id
}
/'"$masterkbd_pattern"'/ {
id=$0 #save entire string to id first
gsub(".*\\[slave[[:space:]]*keyboard[[:space:]]*\\(","", id) #remove preceding junk
gsub("\\)\\]","", id) #remove trailing )]
if (masterid == -1) {
masterid = id # initialize masterid
#printf("Initialized masterid to %d\n", masterid)
} else {
#check if new master keyboard IDs are different.
if (masterid != id) {
printf("ERROR: Stored master keyboard ID %d is different from new ID %d\n", masterid, id);
exit -1 # More than one master keyboards found cannot safely reattach
}
}
}
END {
exit masterid
}'
#return $?
}
#----------------------------------------
# MAIN PROGRAM STARTS FROM HERE
#----------------------------------------
usage $*
case $1 in
status)
find_status
stat=$?
if [ $stat -eq $mode_detached ]
then
echo "$0: Internal keyboard detached"
else
echo "$0: Internal keyboard attached"
fi
exit 0
;;
detach)
find_status
stat=$?
if [ $stat -eq $mode_detached ]
then
echo "$0: Internal keyboard already detached. No action taken."
exit 0
fi
find_attached_kbd_id
kbd_id=$?
detach_cmd=`echo "xinput float $kbd_id"`
echo "Executing command: $detach_cmd"
$detach_cmd
exit $?
;;
attach)
find_status
stat=$?
if [ $stat -eq $mode_attached ]
then
echo "$0: Internal keyboard already attached. No action taken."
exit 0
fi
find_detached_kbd_id
kbd_id=$?
find_master_kbd_id
masterid=$?
attach_cmd=`echo "xinput reattach $kbd_id $masterid"`
echo "Executing command: $attach_cmd"
$attach_cmd
exit $?
;;
*)
usage $*
;;
esac