Re: [fpc-pascal] SysSetCtrlBreakHandler

2016-11-30 Thread Dmitry Boyarintsev
On Tue, Nov 29, 2016 at 5:13 PM, Tomas Hajny  wrote:

>
> 1) Instead of using the special handler registered via
> SetConsoleCtrlHandler (invoked in a special thread), isn't it possible to
> process this event in the exception handler (running in the same thread
> probably)?
>

Not that I'm (or MSDN) aware of.
Instead there is a flag ENABLE_PROCESSED_INPUT that's on by default for a
console.
It would suppress the handler for Ctrl+C and would pass it as a regular key
combination (bringing the desired #3 keychar).

(as I noted in my comment in the bug tracker, clearing
ENABLE_PROCESSED_INPUT flag was used originally. See TurnMouseOff
procedure. However it was removed at some point, causing the regression?)

Windows is raising an exception only if a process is being debugged, so a
debugger would be aware of it. That's the only time, whenever the exception
is used. MSDN however doesn't specify at what thread the exception is
risen.

So the newest patch removes all threading-related concerns.

thanks,
Dmitry
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] SysSetCtrlBreakHandler

2016-11-29 Thread Tomas Hajny
On Tue, November 29, 2016 20:47, Dmitry Boyarintsev wrote:


Hello Dmitry,

> A question regarding your comment (
> http://bugs.freepascal.org/view.php?id=31023#c96393)
> Specifically ad 2)
>
> Is it fine to safe-guard access to SpecialKey / ScanCode through the whole
> crt unit? My only concern is that there're many places that has to be
> adjusted making the patch rather big, (I'm looking into using
> TRTLCriticalSection)

I don't know what's the best solution, but unit Crt isn't thread-safe (not
really supposed to be). If the handler runs in a different thread on MS
Windows, the risk of breaking the event processing is IMHO rather high
(well, probably partly depending on how badly the event loop is written -
it's extremely high if there are no delays between event availability
checks). Some alternative ideas coming to my mind:

1) Instead of using the special handler registered via
SetConsoleCtrlHandler (invoked in a special thread), isn't it possible to
process this event in the exception handler (running in the same thread
probably)?

2) Instead of directly updating the ScanCode and SpecialCode variables
(accessed directly during each and every ReadKey/KeyPressed call), maybe
you could add a new variable specifically for this purpose which would
signalize readiness of this special key event. ReadKey and KeyPressed
could then check this variable if and only if they are not in the middle
of combined key processing yet (before trying to read / poll additional
events from the keyboard driver) and update SpecialKey and ScanCode
themselves (inside the main thread already - then the update from the
other thread becomes an atomic operation and thus probably safe).

Maybe there are other options, I don't know.

Tomas


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] SysSetCtrlBreakHandler

2016-11-28 Thread Tomas Hajny
On Mon, November 28, 2016 01:45, Dmitry Boyarintsev wrote:
>> On the subject of cross-platform console applications...
>
> While working recently on converting libwebsockets header to FPC, I ran
> into a need of adding Ctrl-Break handlers (used in sample apps).
> I was actually surprised of the presence of SysSetCtrlBreakHandler
> function in RTL.
> But i was even more surprised it doesn't do anything useful (for console
> application).
>
> So here's the first patch for Windows
> http://bugs.freepascal.org/view.php?id=31023
>
> If this one is good (or made to the state, when it's good), I'm looking
> into providing the same patches for linux and darwin.

Thanks, I'll certainly have a look at it and apply your patch if it works
as expected. I created the SysSetCtrlBreakHandler infrastructure when
trying to ensure compatibility of unit Crt and FreeVision with the
original TP/BP behaviour under OS/2 and preferred to use a generic and
common approach rather than use different hacks for different units and
different platforms.

Tomas


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


[fpc-pascal] SysSetCtrlBreakHandler

2016-11-27 Thread Dmitry Boyarintsev
On Sun, Nov 27, 2016 at 12:07 PM, Michael Van Canneyt <
mich...@freepascal.org> wrote:
>
> If you mean in a cross-platform way, I think we would welcome patches :)
>
> On the subject of cross-platform console applications...

While working recently on converting libwebsockets header to FPC, I ran
into a need of adding Ctrl-Break handlers (used in sample apps).
I was actually surprised of the presence of SysSetCtrlBreakHandler function
in RTL.
But i was even more surprised it doesn't do anything useful (for console
application).

So here's the first patch for Windows
http://bugs.freepascal.org/view.php?id=31023

If this one is good (or made to the state, when it's good), I'm looking
into providing the same patches for linux and darwin.

thanks,
Dmitry
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal