Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
Created March 11, 2009 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtemGr/77759 to your computer and use it in GitHub Desktop.
Save ArtemGr/77759 to your computer and use it in GitHub Desktop.
Automatically comment out the touchpad in the xorg.conf if there is an external mouse attached.
#!/usr/local/bin/runhaskell
-- Compile with: ghc --make -O2 -optl-s -odir /tmp -hidir /tmp mouse.hs
import Data.List
import Debug.Trace
import Control.Monad
import System.Directory; import System.Environment; import System.IO
{- If there is a USB mouse present in the system, then comment out the touchpad
mouse in "xorg.conf", otherwise uncomment it.
Note: moused should be disabled (in rc.conf and devd.conf) to free the
/dev/ums0 for Xorg. AutoEnableDevices and AllowEmptyInput should be "false". -}
main =
(liftM head $ getArgs) >>= \xorg ->
(liftM lines . readFile) xorg >>= \conf -> length conf `seq`
doesFileExist "/dev/ums0" >>= \haveUsbMouse ->
let newConf = (usbmouse haveUsbMouse) . (touchpad (not haveUsbMouse)) $ conf in
when (newConf /= conf) (writeFile xorg (unlines newConf))
touchpad :: Bool -> [String] -> [String]
touchpad = commentSection "# Touchpad mouse." "EndSection"
usbmouse :: Bool -> [String] -> [String]
usbmouse = commentSection "# USB mouse." "EndSection"
commentSection :: String -> String -> Bool -> [String] -> [String]
commentSection fromEx tillInc onOff conf =
let (header, tmp) = break (== fromEx) conf in
let (sec, footer) = break (isSuffixOf tillInc) tmp in
header ++ [head sec] ++ (commentLines onOff $ (tail sec) ++ [head footer]) ++ (tail footer)
commentLines :: Bool -> [String] -> [String]
commentLines on conf = flip map conf (\line ->
let commented = "#" `isPrefixOf` line in
if (on && commented) then tail line
else if (not on && not commented) then '#' : line
else line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment