
[Add Stoppable layout for power saving
Anton Vorontsov <anton@enomsg.org>**20140901072141
 Ignore-this: a52805d9f3095cd7af48507847ed2fe3
 
 This module implements a special kind of layout modifier, which when
 applied to a layout, causes xmonad to stop all non-visible processes. In a
 way, this is a sledge-hammer for applications that drain power. For
 example, given a web browser on a stoppable workspace, once the workspace
 is hidden the web browser will be stopped.
 
 Note that the stopped application won't be able to communicate with X11
 clipboard. For this, the module actually stops applications after a
 certain delay, giving a chance for a user to complete copy-paste sequence.
 By default, the delay equals to 15 seconds, it is configurable via
 'Stoppable' constructor.
 
 The stoppable modifier prepends a mark (by default equals to "Stoppable")
 to the layout description (alternatively, you can choose your own mark and
 use it with 'Stoppable' constructor). The stoppable layout (identified by
 a mark) spans to multiple workspaces, letting you to create groups of
 stoppable workspaces that only stop processes when none of the workspaces
 are visible, and conversely, unfreezing all processes even if one of the
 stoppable workspaces are visible.
 
 To stop the process we use signals, which works for most cases. For
 processes that tinker with signal handling (debuggers), another
 (Linux-centric) approach may be used. See
 https://www.kernel.org/doc/Documentation/cgroups/freezer-subsystem.txt
] {
addfile ./XMonad/Layout/Stoppable.hs
hunk ./XMonad/Layout/Stoppable.hs 1
+{-# LANGUAGE MultiParamTypeClasses, TypeSynonymInstances #-}
+{-# LANGUAGE PatternGuards #-}
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  XMonad.Layout.Stoppable
+-- Copyright   :  (c) Anton Vorontsov <anton@enomsg.org> 2014
+-- License     :  BSD-style (as xmonad)
+--
+-- Maintainer  :  Anton Vorontsov <anton@enomsg.org>
+-- Stability   :  unstable
+-- Portability :  unportable
+--
+-- This module implements a special kind of layout modifier, which when
+-- applied to a layout, causes xmonad to stop all non-visible processes.
+-- In a way, this is a sledge-hammer for applications that drain power.
+-- For example, given a web browser on a stoppable workspace, once the
+-- workspace is hidden the web browser will be stopped.
+--
+-- Note that the stopped application won't be able to communicate with X11
+-- clipboard. For this, the module actually stops applications after a
+-- certain delay, giving a chance for a user to complete copy-paste
+-- sequence. By default, the delay equals to 15 seconds, it is
+-- configurable via 'Stoppable' constructor.
+--
+-- The stoppable modifier prepends a mark (by default equals to
+-- \"Stoppable\") to the layout description (alternatively, you can choose
+-- your own mark and use it with 'Stoppable' constructor). The stoppable
+-- layout (identified by a mark) spans to multiple workspaces, letting you
+-- to create groups of stoppable workspaces that only stop processes when
+-- none of the workspaces are visible, and conversely, unfreezing all
+-- processes even if one of the stoppable workspaces are visible.
+--
+-- To stop the process we use signals, which works for most cases. For
+-- processes that tinker with signal handling (debuggers), another
+-- (Linux-centric) approach may be used. See
+-- <https://www.kernel.org/doc/Documentation/cgroups/freezer-subsystem.txt>
+--
+-----------------------------------------------------------------------------
+
+module XMonad.Layout.Stoppable
+    ( -- $usage
+      Stoppable(..)
+    , stoppable
+    ) where
+
+import XMonad
+import XMonad.Actions.WithAll
+import XMonad.Util.WindowProperties
+import XMonad.Util.Timer
+import XMonad.StackSet hiding (filter)
+import XMonad.Layout.LayoutModifier
+import System.Posix.Env
+import System.Posix.Signals
+import Data.Maybe
+import Control.Monad
+
+-- $usage
+-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
+--
+-- > import XMonad
+-- > import XMonad.Layout.Stoppable
+-- >
+-- > main = xmonad def
+-- >    { layoutHook = layoutHook def ||| stoppable (layoutHook def) }
+--
+-- Note that the module has to distinguish between local and remote
+-- proccesses, which means that it needs to know the hostname, so it looks
+-- for environment variables (e.g. HOST).
+--
+-- Beware that as of now this module does not support dynamically changing
+-- hostnames.
+--
+-- For more detailed instructions on editing the layoutHook see:
+--
+-- "XMonad.Doc.Extending#Editing_the_layout_hook"
+
+signalWindow :: Signal -> Window -> X ()
+signalWindow s w = do
+    pid <- getProp32s "_NET_WM_PID" w
+    io $ (signalProcess s . fromIntegral) `mapM_` fromMaybe [] pid
+
+signalLocalWindow :: Signal -> Window -> X ()
+signalLocalWindow s w  = do
+    host <- io $ pickOneMaybe `liftM` (getEnv `mapM` vars)
+    hasProperty (Machine host) w >>= flip when (signalWindow s w)
+  where
+    pickOneMaybe = last . (mzero:) . take 1 . catMaybes
+    vars = ["XAUTHLOCALHOSTNAME","HOST","HOSTNAME"]
+
+withAllOn :: (a -> X ()) -> Workspace i l a -> X ()
+withAllOn f wspc = f `mapM_` integrate' (stack wspc)
+
+withAllFiltered :: (Workspace i l a -> Bool)
+                -> [Workspace i l a]
+                -> (a -> X ()) -> X ()
+withAllFiltered p wspcs f = withAllOn f `mapM_` filter p wspcs
+
+sigStoppableWorkspacesHook :: String -> X ()
+sigStoppableWorkspacesHook k = do
+    ws <- gets windowset
+    withAllFiltered isStoppable (hidden ws) (signalLocalWindow sigSTOP)
+  where
+    isStoppable ws = k `elem` words (description $ layout ws)
+
+-- | Data type for ModifiedLayout. The constructor lets you to specify a
+-- custom mark/description modifier and a delay. You can also use
+-- 'stoppable' helper function.
+data Stoppable a = Stoppable
+    { mark :: String
+    , delay :: Rational
+    , timer :: Maybe TimerId
+    } deriving (Show,Read)
+
+instance LayoutModifier Stoppable Window where
+    modifierDescription = mark
+
+    hook _   = withAll $ signalLocalWindow sigCONT
+
+    handleMess (Stoppable m _ (Just tid)) msg
+        | Just ev <- fromMessage msg = handleTimer tid ev run
+          where run = sigStoppableWorkspacesHook m >> return Nothing
+    handleMess (Stoppable m d _) msg
+        | Just Hide <- fromMessage msg =
+            (Just . Stoppable m d . Just) `liftM` startTimer d
+        | otherwise = return Nothing
+
+-- | Convert a layout to a stoppable layout using the default mark
+-- (\"Stoppable\") and a delay of 15 seconds.
+stoppable :: l a -> ModifiedLayout Stoppable l a
+stoppable = ModifiedLayout (Stoppable "Stoppable" 15 Nothing)
hunk ./xmonad-contrib.cabal 262
                         XMonad.Layout.Spiral
                         XMonad.Layout.Square
                         XMonad.Layout.StackTile
+                        XMonad.Layout.Stoppable
                         XMonad.Layout.SubLayouts
                         XMonad.Layout.TabBarDecoration
                         XMonad.Layout.Tabbed
}
[Implement proper handling of dynamically changing hostname
Anton Vorontsov <anton@enomsg.org>**20140901072158
 Ignore-this: 2aeac6d2161e666d40cda6a09f78b208
 
 The module implements a proper way of finding out whether the window is
 remote or local.
 
 Just checking for a hostname and WM_CLIENT_MACHINE being equal is often
 not enough because the hostname is a changing subject (without any
 established notification mechanisms), and thus WM_CLIENT_MACHINE and the
 hostname can diverge even for a local window.
 
 This module solves the problem. As soon as there is a new window created,
 we check the hostname and WM_CLIENT_MACHINE, and then we cache the result
 into the XMONAD_REMOTE property.
 
 Notice that XMonad itself does not know anything about hostnames, nor does
 it have any dependency on Network.* modules. For this module it is not a
 problem: you can provide a mean to get the hostname through your config
 file (see usage). Or, if you don't like the hassle of handling dynamic
 hostnames (suppose your hostname never changes), it is also fine: this
 module will fallback to using environment variables.
] {
hunk ./XMonad/Layout/Stoppable.hs 49
 import XMonad
 import XMonad.Actions.WithAll
 import XMonad.Util.WindowProperties
+import XMonad.Util.RemoteWindows
 import XMonad.Util.Timer
 import XMonad.StackSet hiding (filter)
 import XMonad.Layout.LayoutModifier
hunk ./XMonad/Layout/Stoppable.hs 53
-import System.Posix.Env
 import System.Posix.Signals
 import Data.Maybe
 import Control.Monad
hunk ./XMonad/Layout/Stoppable.hs 70
 -- proccesses, which means that it needs to know the hostname, so it looks
 -- for environment variables (e.g. HOST).
 --
--- Beware that as of now this module does not support dynamically changing
--- hostnames.
+-- Environment variables will work for most cases, but won't work if the
+-- hostname changes. To cover dynamic hostnames case, in addition to
+-- layoutHook you have to provide manageHook from
+-- "XMonad.Util.RemoteWindows" module.
 --
 -- For more detailed instructions on editing the layoutHook see:
 --
hunk ./XMonad/Layout/Stoppable.hs 85
     io $ (signalProcess s . fromIntegral) `mapM_` fromMaybe [] pid
 
 signalLocalWindow :: Signal -> Window -> X ()
-signalLocalWindow s w  = do
-    host <- io $ pickOneMaybe `liftM` (getEnv `mapM` vars)
-    hasProperty (Machine host) w >>= flip when (signalWindow s w)
-  where
-    pickOneMaybe = last . (mzero:) . take 1 . catMaybes
-    vars = ["XAUTHLOCALHOSTNAME","HOST","HOSTNAME"]
+signalLocalWindow s w  = isLocalWindow w >>= flip when (signalWindow s w)
 
 withAllOn :: (a -> X ()) -> Workspace i l a -> X ()
 withAllOn f wspc = f `mapM_` integrate' (stack wspc)
addfile ./XMonad/Util/RemoteWindows.hs
hunk ./XMonad/Util/RemoteWindows.hs 1
+-----------------------------------------------------------------------------
+-- |
+-- Module      :  XMonad.Util.RemoteWindows
+-- Copyright   :  (c) Anton Vorontsov <anton@enomsg.org> 2014
+-- License     :  BSD-style (as xmonad)
+--
+-- Maintainer  :  Anton Vorontsov <anton@enomsg.org>
+-- Stability   :  unstable
+-- Portability :  unportable
+--
+-- This module implements a proper way of finding out whether the window
+-- is remote or local.
+--
+-- Just checking for a hostname and WM_CLIENT_MACHINE being equal is often
+-- not enough because the hostname is a changing subject (without any
+-- established notification mechanisms), and thus WM_CLIENT_MACHINE and
+-- the hostname can diverge even for a local window.
+--
+-- This module solves the problem. As soon as there is a new window
+-- created, we check the hostname and WM_CLIENT_MACHINE, and then we cache
+-- the result into the XMONAD_REMOTE property.
+--
+-- Notice that XMonad itself does not know anything about hostnames, nor
+-- does it have any dependency on Network.* modules. For this module it is
+-- not a problem: you can provide a mean to get the hostname through your
+-- config file (see usage). Or, if you don't like the hassle of handling
+-- dynamic hostnames (suppose your hostname never changes), it is also
+-- fine: this module will fallback to using environment variables.
+--
+-----------------------------------------------------------------------------
+
+module XMonad.Util.RemoteWindows
+    ( -- $usage
+      isLocalWindow
+    , manageRemote
+    , manageRemoteG
+    ) where
+
+import XMonad
+import XMonad.Util.WindowProperties
+import Data.Monoid
+import Data.Maybe
+import Control.Monad
+import System.Posix.Env
+
+-- $usage
+-- You can use this module with the following in your @~\/.xmonad\/xmonad.hs@:
+--
+-- > import XMonad
+-- > import XMonad.Util.RemoteWindows
+-- > import Network.BSD
+-- >
+-- > main = xmonad def
+-- >    { manageHook = manageRemote =<< io getHostName }
+
+guessHostName :: IO String
+guessHostName = pickOneMaybe `liftM` (getEnv `mapM` vars)
+  where
+    pickOneMaybe = last . (mzero:) . take 1 . catMaybes
+    vars = ["XAUTHLOCALHOSTNAME","HOST","HOSTNAME"]
+
+setRemoteProp :: Window -> String -> X ()
+setRemoteProp w host = do
+    d <- asks display
+    p <- getAtom "XMONAD_REMOTE"
+    t <- getAtom "CARDINAL"
+    v <- hasProperty (Machine host) w
+    io $ changeProperty32 d w p t propModeReplace
+                          [fromIntegral . fromEnum $ not v]
+
+-- | Given a window, tell if it is a local or a remote process. Normally,
+-- it checks XMONAD_REMOTE property. If it does not exist (i.e. the
+-- manageRemote hook was not deployed in user's config), it falls back to
+-- checking environment variables and assuming that hostname never
+-- changes.
+isLocalWindow :: Window -> X Bool
+isLocalWindow w = getProp32s "XMONAD_REMOTE" w >>= \p -> case p of
+    Just [y] -> return $ y == 0
+    _ -> io guessHostName >>= \host -> hasProperty (Machine host) w
+
+-- | Use this hook to let XMonad properly track remote/local windows. For
+-- example, @manageHook = manageRemote =<< io getHostName@.
+manageRemote :: String -> ManageHook
+manageRemote host = ask >>= \w -> liftX (setRemoteProp w host) >> return mempty
+
+-- | Use this hook if you want to manage XMONAD_REMOTE properties, but
+-- don't want to use an external getHostName in your config. That way you
+-- are retreating to environment variables.
+manageRemoteG :: ManageHook
+manageRemoteG = manageRemote =<< io guessHostName
hunk ./xmonad-contrib.cabal 307
                         XMonad.Util.NamedWindows
                         XMonad.Util.Paste
                         XMonad.Util.PositionStore
+                        XMonad.Util.RemoteWindows
                         XMonad.Util.Replace
                         XMonad.Util.Run
                         XMonad.Util.Scratchpad
}

