Home | Blog | Software | Reviews and Features | Forum | Help | Donate | About us
topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • October 20, 2017, 10:17 PM
  • Proudly celebrating 10 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: Desperate app request - restore window positions after wake from sleep scrambles  (Read 3470 times)

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Ok here is latest version, which is working -- tweaked/fixed a few more things in skrommel code (duplicate entries in ini files, a compare of monitorstatus that was not doing a string compare, etc.); very messy and lots of logging, but working.

Skrommel hopefully you have a good file compare tool? (winmerge is free but i love Beyond Compare; if you dont have it let's get you a copy!)

[OLD FILE REMOVED SEE SUBSEQUENT POST]
« Last Edit: July 26, 2017, 06:24 PM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
It actually *DOES* seem to work perfectly (other than the ugliness of all the maximized windows jumping around)!!!

This is quite remarkable to me as I 've been suffering through this problem for many months now and had basically given up on it.

I hadn't thought to watch for monitor on/off events.. This part of your approach is wonderful because I can essentially detect only these rare events of monitor on off and the script ONLY needs to take action on those events.  No background processing or periodic saving of window positions is actually needed as far as i can tell (at least for my situation).  So it's essentially not impacting pc performance at all.

Now it's just a matter of tweaking it to see if I can get it to operate in a little less jarring visual display.



Thank you so much skrommel!!!!  :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-* :-*

After I finish mangling and testing the code I will post a new version.



mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
ps I think the most likely solution to avoiding the unmaximizing-moving-remaximizing fix that is needed for the scrambled windows, is to try to DETECT when a window has been IMPROPERLY MOVED by the OS.. Im thinking now, based on watching how the code works, that this might be possible.  Perhaps by saving in the ini file the MAXIMIZED (not normal) window location and checking that against the current one to see if it has moved physical maximized locations even if it hasn't changed the restored-state location..  But there may be an easier way.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
And here is the new code that does just what I suggested -- saves both maximized and normal window positions for each monitor, and then for windows that have mysteriously stayed maximized but moved to a different monitor, those are first restored before they are remaximized.  Other windows are left alone, which makes for much visual ugliness on screen.  (it might be possible to improve this further by making window invisible before unaximizing and remaximizing but that as this is only running on monitor wakeup i think it's no big deal).

I am really excited to try this for real when i wake up tomorrow.

[ps. I have been testing it using nircmd to put monitors to sleep -- my earlier reports that this did not reproduce the problem were wrong; turns out i just needed to give the monitors at least 30 seconds or so to go into power saving mode before waking them up).

I will report tomorrow on real-world test of the tool.  :Thmbsup:

[OLD FILE REMOVED SEE SUBSEQUENT POST]
« Last Edit: July 26, 2017, 06:46 PM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
ps2. Skrommel - I think my new code that checks for maximized window position and does a restore-maximize cycle is needed even if you don't have my OS problem, if you wanted to properly restore a window that was saved maximized on one monitor and is newly maximized on a different monitor.  But it may be possible to avoid the restore-maximize cycle by just directly setting the window position while it's maximized to bring it back to the old monitor?

Another nice feature would be if the app checked to make sure that the restored positions were actually "visible" -- otherwise the app could end up moving things off screen if the old monitors did not all get reconnected, etc.  Not a concern for me just a future idea..

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
And here's a version without any window flickering, by directly restoring the maximized (real) window locations as WELL as the normal restored window locations when it finds a maximized window.
No flickering restore-maximize process needed anymore.

[OLD FILE REMOVED SEE SUBSEQUENT POST]
« Last Edit: July 26, 2017, 07:32 PM by mouser »

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 927
    • View Profile
    • Donate to Member
Some serious logging there, mouser! :) I'm sitting at a laptop on my holiday, so I haven't been able to test multiple monitors properly, but I'm surprised that wrong max positions was the solution to your problem. And I can't test it, because I can't get your script to do anything on my computer...

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Your manual save and load functions should work well in my version.

Because this version NEVER saves window positions except when you trigger via tray menu or when monitors turn off (testing via nircmd), it should be easy to use the tray menu to save your layout via tray menu, then move around some windows, then load configuration from tray menu.

No need to mess with the automatic restore when monitors power on and off, just experiment with the ability to manually save and load layouts.

Hopefully that will work for you with my version?


mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Quote
Some serious logging there, mouser!

Hey I learned to code in the 1970s -- log statements are my main method of debugging :)

AHK is not a language I am comfortable with so I kept running into confusing issues where things were not behaving as I expected and I had to log info to see what was going on.

As an example, this line in your original code had me flummoxed for quite a while:

Code: Autohotkey [Select]
  1. If monitorStatus=Off

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
New version with substantial bug fix attached.

I was not performing my hex->dec process id check and dupe window avoidance at load time only save time.
This meant some windows would not be "seen" by the script.

The reason for this code is that there were dupe entries in the list of processes/windows which was causing the script to process windows twice.  It happened with some ids being hexadecimal and being dupes with their decimal versions.  I don't understand why but normalizing everything to decimal ids, and then making sure they aren't duplicate id that was just previously written out solves the problem.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
One nice refinement to the tool might be if the load process checked if the monitors were back in the same position they were in when settings were saved, and if not, waited another X MS and tried again, up to some max time.
That would eliminate the need to set a long delay waiting for the monitors to come back online and be available, and would let you start trying to restore the layout right away, and it would also avoid trying to restore a layout if the windows would be put offscreen.  Just checking total workspace x,y,w,h for a match with saved settings would be fine -- no need to iterate all the monitors..

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
I have code added now to check virtual screen (desktop) configuration and skip restoring unless it matches, but in initial tests it's not quite clear that this is going to work.. the thing i don't know is if the OS reports a different screen size when this weird OS wakeup moving windows behavior happens.. Stay tuned.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
I have code added now to check virtual screen (desktop) configuration and skip restoring unless it matches, but in initial tests it's not quite clear that this is going to work.. the thing i don't know is if the OS reports a different screen size when this weird OS wakeup moving windows behavior happens.. Stay tuned.

« Last Edit: July 26, 2017, 11:57 PM by mouser »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
I'm uploading a new version which has some options at top of file regarding delays and methods.
I think my new code to quickly cycle until the screen resolution is back to where it was, may not be all that useful because I suspect that the virtual screen size does *not* change when the weird window moving event happens.

So I wrote some new code that basically run the window position restore(load) function TWICE:
First as soon as it gets the signal that a monitor has turned on.
Second, after some delay after a monitor turns on.

The idea being that most of the time the first method works, which makes it so that you don't even notice the fix.
But sometimes this fast restore does not work and windows moves things around after it triggers.  So the second call is a kind of backup restore that will kick in if first one was too quick to take effect.


AndyD

  • Participant
  • Joined in 2017
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
Hi mouser,

I have signed up purely to say a massive thank you to you and everyone else who has contributed to this project. I too faced so many issues with windows and screen positions and i have been trying to come up with a solution and stumbled across this.

I have downloaded the latest version you have compiled which works great, no problems at all so far for me.

So again, many thanks and keep up the good work, i will be following closely and report back with any issues should that help.

Kind regards

Andrew

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Welcome, Andy.  :up:

Skrommel is the magical creature who deserves the real thanks here.  I am happy to be able to take his work and improve it.
I myself can't wait until I see how this performs tomorrow morning.

And it's nice to see that there is someone else who will benefit from this tool.

By the way -- how did you discover this thread so quickly?



I can already say that it works well enough in my testing that even if the current version isn't perfect, it soon will be -- which is a nice feeling.

Once the program is all cleaned and polished up, I hope we can spread the word to others who have been suffering with this problem.

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Skrommel -- If you are looking for features to add, another feature that would be nice to add to this tool is a menu item to put the monitors to sleep -- nircmd has a function for this but i think it's a naturally feature to add to this tool's tray menu if it's easy to add, and it would definitely help others test the tool.

AndyD

  • Participant
  • Joined in 2017
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
A big thanks to Skrommel as well then!

I searched for "keep the state of multiple screens when power is switched off" and this website was near the bottom of the first page.

I am using this project to fix issues inside my office which works great now, just a bit of background info should you want to read it on what i have been doing.

We have many multiple monitors in my head office which are connected to single computers that display information, how profitable we are etc using Power BI and Excel Dashboards.

So first issue was no one was turning the screens off, which meant we walk around the offices to make sure they went off at night, i fixed this issue with smart plugs to switch off at 7pm and on at 7am monday to friday.

Next issue was when power was restored at 7am on multiple monitors the windows was all on the primary screen.

So my solution was use nircmd to sleep and wake at the times above, this didnt work still. so i done some digging for software that will keep the state of the screens.

Which is when i came across this page and did a test and it worked, but my issue was still happening.

The way i have resolved it is, at 6:58pm, a scheduled task run with nircmd to put the computer into sleep (without this power is cut and WinPosSaver wont know the state of the windows). At 7am power is restored to the monitors and then at 7:02am, i send another task to wake the screen, WinPosSaver then re-arranges all the windows back for me.

I have some testing to do over the next week but if this works i will be rolling this out to all multiple monitors.

All this because people are lazy to switch a tv screen off!

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Quote
The way i have resolved it is, at 6:58pm, a scheduled task run with nircmd to put the computer into sleep (without this power is cut and WinPosSaver wont know the state of the windows)

Just a note that WinPosSaver doesn't need for the COMPUTER to go to sleep/hibernate (though that may work too), it just needs the MONITOR to go to sleep, which is a separate thing.

Note also that skrommel's code (and my tweaks to it), could very easily be modified to work as commandline function for saving windows and restoring them on command, regardless of monitor status.  So that would be an easy functionality to add if it would be useful.

AndyD

  • Participant
  • Joined in 2017
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
Apologies, i did not mean computer to sleep, put the monitor to sleep (in my case i am putting TV screens to sleep).

If the code could be tweaked so that when the script is run (if possible via a Scheduled Task), it could save the state of the windows that would save me messing around with extra batch files to get the screen to sleep so WinPosSaver will save the state.

Then all i would need to do is pull the screen power, and when the screens power restores the script auto runs and re-arranges the windows, or i run the script via Scheduled Task a few minutes after the power is restored to the screens to re-arrange windows.

Is this possible?

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Yep, easy to do.

Since I'm not an ahk person, I will wait for skrommel or someone else to add code to the ahk that can detect a commandline arguement of -load or -save and if found just do that and exit.
That way we can use the same ahk/exe tool both for commandline saving and loading, and also for it's current resident use.

AndyD

  • Participant
  • Joined in 2017
  • *
  • default avatar
  • Posts: 4
    • View Profile
    • Donate to Member
well you sir have made my day even better  :up:

Just to let you know, the current setup i have does work nicely, just have to get everything in place in the correct order.

18:59 monitor sleep command to off
19:00 power off monitor
07:00 power on monitor
07:01 monitor sleep command to on

I will leave as it is for the moment and see if everything went to plan in the morning

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
First real life report: program worked perfectly for me this morning to restore window positions.  Felt like magic.  :Thmbsup:



ps. For those who might be wondering why I've made such a big deal about this problem of moving windows on wake, let me explain:  I have a tiny netbook computer on the bedside table next to my bed.  It stays connected to my desktop in my office with a remote view of the desktop..  As soon as I wake up in the morning, I lean over from bed and tap the keyboard to turn on the netbook monitor and then look to see what new mail and forum activity there is.  The remote view of my desktop only focuses on a small area at a time because the netbook resolution is so small, and it's difficult to move the view around the 4 high-res screens of my desktop -- especially since one of my desktop monitors is in portrait orientation.  So when the window positions scramble, it means I spend the first several seconds of my waking mornings barely conscious and sliding my finger on the mousepad in what seems like an endless futile search for where my mail and browser windows have gone.. And sometimes they are moved under other windows, etc..  Not exactly how I want to spend the first moments of the morning.  So having this problem fixed is so wonderful.
« Last Edit: July 27, 2017, 11:07 AM by mouser »

skrommel

  • Fastest code in the west
  • Developer
  • Joined in 2005
  • ***
  • Posts: 927
    • View Profile
    • Donate to Member
Great to hear it's working, mouser! :) Yes, AutoHotkey has a few quirks, but you have picked it up fast!

Ta inn hele skjermen 28.07.2017 03.55.06.bmp.jpgDesperate app request - restore window positions after wake from sleep scrambles

I've started adding a few options, check out the settings screenshot. I hope to have a working version soon.
« Last Edit: July 27, 2017, 08:52 PM by skrommel »

mouser

  • First Author
  • Administrator
  • Joined in 2005
  • *****
  • Posts: 37,490
    • View Profile
    • Donate to Member
Nice -- looks like this could become a mutlti-faceted tool..

More importantly though: Second night of real-world deployment of WinPosSaver(M)  worked flawlessly.  :up: :up: :up: :up:

ps.
In your menu I see the entry labeled "Sleep"... I assume that puts the COMPUTER to sleep.  It would be nice to have another menu item for just putting the MONITORS to sleep, which is the function that people with my affliction will need for testing.