Discussion:
[xmonad] Not running my startupHook on restart
Adam Sjøgren
2015-12-31 16:35:48 UTC
Permalink
I have a startupHook that spawns a number of windows on various
workspaces.

When I restart XMonad, I don't want those windows opened again.

Is there an easy way to have startupHook only run when logging in, and
not when restarting XMonad?

The way I have tried to solve this, unsuccessfully, is to make my
startupHook look like this:

myStartup = when (noRun /= "1") $ do
spawnOn "1" "emacs"
spawnOn "6" "x-terminal-emulator"
-- and so on...
where
noRun = unsafePerformIO $ env "NO_STARTUP_HOOK" "0"

env :: String -> String -> IO String
env variable fallthrough = getEnv variable `Control.Exception.catch` econst fallthrough

(where getEnv is imported from System.Environment).

And then I have changed the restart keybinding like this:

, ("M-S-r", spawn "xmonad --recompile; export NO_STARTUP_HOOK=1; xmonad --restart")

Unfortunately it doesn't work - I guess something "more" is going on
when "xmonad --restart" runs, because if I add a key:

, ("M-S-y", spawn "export HEP=yay; notify-send --expire-time 2000 'Testing' \"ENV: [$HEP]\"")

then pressing M-S-y gives me an alert with the text "ENV: [yay]" in it.
But if I do:

, ("M-S-y", spawn "notify-send --expire-time 2000 'Testing' \"ENV: [$NO_STARTUP_HOOK]\"")

the notification says "ENV: []" regardless of whether I have restarted
XMonad or not.

I have a feeling that I am taking the long walk to solve this - any
ideas of the easy way to the goal here?


Thanks!

Adam
--
"Här kommer rädslan, nu igen Adam Sjøgren
När alla fjärilar i magen nålas upp ***@koldfront.dk
till ett mönster på min säng"
Brandon Allbery
2015-12-31 16:42:34 UTC
Permalink
Post by Adam Sjøgren
I have a startupHook that spawns a number of windows on various
workspaces.
When I restart XMonad, I don't want those windows opened again.
Use XMonad.Util.SpawnOnce.spawnOnce. If you want to use SpawnOn as well
then you'd need to write your own spawner combining the two.

The more general way to do it is:

as <- io getArgs
when (null as) $ do
{- your startup code here -}

which takes advantage of the fact that restarts pass --resume as a
parameter, followed by the serialized xmonad state.
--
brandon s allbery kf8nh sine nomine associates
***@gmail.com ***@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
Adam Sjøgren
2015-12-31 17:06:24 UTC
Permalink
Post by Brandon Allbery
as <- io getArgs
when (null as) $ do
{- your startup code here -}
which takes advantage of the fact that restarts pass --resume as a
parameter, followed by the serialized xmonad state.
That worked great - thanks!


Best regards,

Adam
--
"On the quiet side. Somewhat peculiar. A good Adam Sjøgren
companion, in a weird sort of way." ***@koldfront.dk
Jakob Schöttl
2016-01-07 20:30:36 UTC
Permalink
Post by Adam Sjøgren
Post by Brandon Allbery
as <- io getArgs
when (null as) $ do
{- your startup code here -}
That worked great - thanks!
For me it does not work, I don't know why.

myStartupHook = do
args <- io getArgs
spawn $ "xmessage " ++ show (length args)
...

When I restart xmonad, xmessage pops up and shows "0". But `ps aux |
grep xmonad` shows me the --resume option and the serialized state in
the command line.

Any ideas?
Brandon Allbery
2016-01-07 20:44:32 UTC
Permalink
Post by Jakob Schöttl
For me it does not work, I don't know why.
myStartupHook = do
args <- io getArgs
spawn $ "xmessage " ++ show (length args)
...
Actually... I updated to 0.12 and at the same time switched from ghc 7.6.3
to 7.10.3, and found the same thing. I had to move the "args <- io getArgs"
directly into "main" before invocation of the "xmonad" function.

I haven't yet looked at the code but this seems surprising. I also suspect
it's not related to a change in xmonad, since the only core changes (and
only a core change could have any effect here) were documentation.
--
brandon s allbery kf8nh sine nomine associates
***@gmail.com ***@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
Jakob Schöttl
2016-01-08 02:31:39 UTC
Permalink
Post by Jakob Schöttl
For me it does not work, I don't know why.
myStartupHook = do
args <- io getArgs
spawn $ "xmessage " ++ show (length args)
...
Actually... I updated to 0.12 and at the same time switched from ghc
7.6.3 to 7.10.3, and found the same thing. I had to move the "args <-
io getArgs" directly into "main" before invocation of the "xmonad"
function.
I haven't yet looked at the code but this seems surprising. I also
suspect it's not related to a change in xmonad, since the only core
changes (and only a core change could have any effect here) were
documentation.
That works! Thank you. Yeah it sounds like a GHC bug for me. I also have
xmonad 0.12 and GHC 7.10.3.
Adam Sjøgren
2016-01-08 07:26:53 UTC
Permalink
Post by Jakob Schöttl
Post by Brandon Allbery
Actually... I updated to 0.12 and at the same time switched from ghc
7.6.3 to 7.10.3, and found the same thing. I had to move the "args <-
io getArgs" directly into "main" before invocation of the "xmonad"
function.
I haven't yet looked at the code but this seems surprising. I also
suspect it's not related to a change in xmonad, since the only core
changes (and only a core change could have any effect here) were
documentation.
That works! Thank you. Yeah it sounds like a GHC bug for me. I also
have xmonad 0.12 and GHC 7.10.3.
I run xmonad 0.11.1 and ghc 7.10.3 (both on Debian sid), and it works
for me (to have "as <- io getArgs" in my startupHook).


Just a data point,

Adam
--
"Boy, I love summer vacation. Adam Sjøgren
I can feel my brain beginning to atrophy already." ***@koldfront.dk
Brandon Allbery
2016-01-13 19:45:56 UTC
Permalink
Post by Adam Sjøgren
Post by Jakob Schöttl
Post by Brandon Allbery
Actually... I updated to 0.12 and at the same time switched from ghc
7.6.3 to 7.10.3, and found the same thing. I had to move the "args <-
io getArgs" directly into "main" before invocation of the "xmonad"
function.
That works! Thank you. Yeah it sounds like a GHC bug for me. I also
have xmonad 0.12 and GHC 7.10.3.
I run xmonad 0.11.1 and ghc 7.10.3 (both on Debian sid), and it works
for me (to have "as <- io getArgs" in my startupHook).
Turns out it is a code change.
https://github.com/xmonad/xmonad/commit/307b82a53d519f5c86c009eb1a54044a616e4a5c

This isn't even mentioned in breaking changes at
https://wiki.haskell.org/Xmonad/Notable_changes_since_0.11
There's also documentation in various places that would need to be updated.
--
brandon s allbery kf8nh sine nomine associates
***@gmail.com ***@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net
Peter Jones
2015-12-31 20:08:08 UTC
Permalink
Post by Adam Sjøgren
When I restart XMonad, I don't want those windows opened again.
Also take a look at the new XMonad.Actions.DynamicProjects module:

https://hackage.haskell.org/package/xmonad-contrib-0.12/docs/XMonad-Actions-DynamicProjects.html

Or XMonad.Actions.TopicSpace:

https://hackage.haskell.org/package/xmonad-contrib-0.12/docs/XMonad-Actions-TopicSpace.html

Both of which can spawn applications on a specific workspace when it has
no windows.
--
Peter Jones, Founder, Devalot.com
Defending the honor of good code
Daniel Wagner
2016-01-04 02:46:56 UTC
Permalink
Post by Adam Sjøgren
, ("M-S-r", spawn "xmonad --recompile; export NO_STARTUP_HOOK=1; xmonad --restart")
Unfortunately it doesn't work - I guess something "more" is going on
To explain the "something more", `xmonad --restart` doesn't do what you
think it does. Namely: it just sets a property on the root window and dies;
the new xmonad process does *not* start managing windows. Instead, the
existing xmonad process notices the property change on the root window and
restarts *itself*, passing along its current internal state in command-line
parameters. This is why the environment change is not absorbed: only the
process that sets a root window property and exits sees the new
environment; the existing process' environment, which is the one inherited
by the new window manager, is unchanged. You should be able to fix this by
setting the environment in-process with System.Environment.setEnv and then
directly calling XMonad.Operations.restart (why go through a complicated
rigamarole to send yourself a message when you can just pretend you already
got it? ;-). Something like this:

, ("M-S-r", setEnv "NO_STARTUP_HOOK" "1" >> restart "xmonad" True)

~d
Loading...