#!/usr/bin/perl -w # # Disable/Enable the caps lock key # 051206 liverpole # ############## ### Strict ### ############## use strict; use warnings; #################### ### User-defined ### #################### my $xmodmap = '/usr/bin/X11/xmodmap'; my $sysmodmap = '/etc/X11/Xmodmap'; my $usrmodmap = $ENV{'HOME'} . "/.Xmodmap"; ################# ### Libraries ### ################# use FileHandle; use File::Basename; use Getopt::Long; ############### ### Globals ### ############### my $iam = basename $0; my $b_global = 0; my $syntax = " syntax: $iam [switches] <'off' | 'on'> Disables or reenables the CAPS LOCK key by changing the appropriate 'Xmodmap' file. Use 'off' to disable, and 'on' to reenable. Switches: -g ... apply the change globally (must be run as 'root') "; #################### ### Command-line ### #################### GetOptions("g" => \$b_global) or die $syntax; (my $state = shift) or die $syntax; if ($state ne 'off' && $state ne 'on') { die "$iam: invalid state '$state' (should be 'off' or 'on')\n"; } ################### ### Subroutines ### ################### sub perform($) { my ($cmd) = @_; print "% $cmd\n"; system($cmd) } #################### ### Main program ### #################### # If making global changes, make sure user is 'root' if ($b_global) { (0 == $<) or die "$iam: you must run this as 'root'\n"; } # Read the appropriate file my $infile = ($b_global || (!-e $usrmodmap))? $sysmodmap: $usrmodmap; my $fh = new FileHandle; open($fh, "<", $infile) or die "$iam: cannot read '$infile' ($!)\n"; chomp(my @lines = <$fh>); close $fh; printf "Read '$infile' -- %d line%s\n", 0 + @lines, (1 == @lines)? "": "s"; # Modify the file @lines = grep { !/^\s*(remove|add)\s+Lock\s+=\s+Caps_Lock\s*(!|$)/i } @lines; ($state eq 'off') and push @lines, "remove Lock = Caps_Lock"; ($state eq 'on') and push @lines, "add Lock = Caps_Lock"; # Write the file my $outfile = $b_global? $sysmodmap: $usrmodmap; open($fh, ">", $outfile) or die "$iam: cannot write '$outfile' ($!)\n"; map { print $fh "$_\n" } @lines; printf "Wrote '$outfile' -- %d line%s\n", 0 + @lines, (1 == @lines)? "": "s"; close $fh; # Make the changes take effect if (-x $xmodmap) { (-f $sysmodmap) and perform "$xmodmap $sysmodmap"; (-f $usrmodmap) and perform "$xmodmap $usrmodmap"; }