Page 1 of 1

[REQUEST] atStop

Posted: Thu Sep 04, 2008 7:24 am
by luonline
Hi!
Is it possible to add an atStop function (similar to atExit), which would be called when the script stops by pressing the stop key?
Thanks in advance.

Re: [REQUEST] atStop

Posted: Thu Sep 04, 2008 3:57 pm
by Administrator
Sure. I can add this. But the question is, should an exit (CTRL+L) generate the stop event, as well?

Re: [REQUEST] atStop

Posted: Fri Sep 05, 2008 6:32 am
by luonline
Good question. i think so, because stoping a script before it exits makes sense to me. Furthermore, i think that atStop would be used for cleaning or logging purposes and it doesn't affect later events.
Just my opinion.

Re: [REQUEST] atStop

Posted: Fri Sep 05, 2008 2:08 pm
by Administrator
I've added this as well as atResume(). The updated executable is attached (just the .exe, so don't delete anything yet).

Re: [REQUEST] atStop

Posted: Tue Sep 09, 2008 3:33 pm
by luonline
Hi! I tried:

Code: Select all

function stop_callback()
  printf("Stoping...\n");
end

atStop(stop_callback);
But I got an error:
"...of\Desktop\mutools\micromacro\scripts\mu-engnovo.lua:399: attempt to call global 'atStop' (a nil value)".
Could you give an example? Thank you.

Re: [REQUEST] atStop

Posted: Tue Sep 09, 2008 6:32 pm
by Administrator
The function is atPause() instead of atStop().

Re: [REQUEST] atStop

Posted: Wed Sep 10, 2008 7:37 am
by luonline
Didn't work. I tried:

Code: Select all

startKey = key.VK_INSERT;
stopKey = key.VK_DELETE;

function pause_callback()
  printf("Pausing...\n");
end

atPause( pause_callback );

function main()
  while(true) do
    printf("Running...\n");
    yrest(100);  
  end
end

startMacro(main);
But, I still got: "...of\Desktop\mutools\micromacro\scripts\atPause.lua:7: attempt to call global 'atPause' (a nil value)"

Where is my mistake?

Re: [REQUEST] atStop

Posted: Thu Sep 11, 2008 12:58 am
by Administrator
My mistake. I forgot about the changes to lib.lua. Here's a full update that includes the atPause() and atResume() functions.

Re: [REQUEST] atStop

Posted: Thu Sep 11, 2008 7:25 am
by luonline
Both files you've uploaded are the same. Not working yet.

Re: [REQUEST] atStop

Posted: Thu Sep 11, 2008 4:17 pm
by Administrator
I attached the wrong file again. This one should work.

Re: [REQUEST] atStop

Posted: Mon Sep 15, 2008 2:36 pm
by luonline
Function yrest causes error inside the callback function... For example:

Code: Select all

startKey = key.VK_INSERT;
stopKey = key.VK_DELETE;

function pause_callback()
  yrest(100);
  printf("Pausing...\n");
end

atPause( pause_callback );

function main()
  while(true) do
    printf("Running...\n");
    yrest(100); 
  end
end

startMacro(main);
I got error: "...of\Desktop\micromacro\scripts\atPause.lua:5: attempt to yield across metamethod/C-call boundary".
Isn't it possible? Thank you.

Re: [REQUEST] atStop

Posted: Mon Sep 15, 2008 9:11 pm
by Administrator
Nice find. Well, I figured out where the problem was. The good news is that it's "fixed." The bad news is that you still won't be able to yield.

See, the callback function is being called from the main thread (is not a coroutine). Because of this, you cannot yield it. So, I added a check into safeYield() to see if it is in the main thread, and do nothing if it is. The result is that you will still be resting, but not yielding (so other threads will not actually be getting CPU time here...which is actually a good thing for these types of events).

Here's the safeYield() patch:

Code: Select all

-- Runs a coroutine in a protected state, if available
function safeYield()
  -- make sure we're not trying to yield in the main thread
  -- do nothing.
  local co = coroutine.running();
  if( co == nil ) then
    return;
  end

  if( cocoAvailable ) then
    local status, err = pcall(coroutine.yield);

    if( status ~= true ) then
      __log_traceback();

      setTextColor(cli.yellow);
      error(err, 3);
    end
  else
    coroutine.yield();
  end
end