Recent

Author Topic: AllocateHwnd alternative on Linux?  (Read 1184 times)

lazycat2

  • New member
  • *
  • Posts: 8
AllocateHwnd alternative on Linux?
« on: July 02, 2022, 05:51:24 pm »
I have a Delphi 11 project that extensively uses AllocateHwnd to communicate between the main thread and background threads (using SendMessage/SendMEssageTimeout/PostMessage). This is very convenient, but now I want to also make this work on Linux with Freepascal.

I was able to make it compile more or less, but AllocateHwnd and SendMessage, as it turned out, don't seem to work on Linux. If I use GTK2 it throws an error ("Widget is nil") and on GTK3 there's no error but it doesn't work. AllocateHwnd always returns zero on Linux. Did I miss something? Or maybe there's a more appropriate alternative (note TThread Synchronize/Queue I'd like to avoid)? The project does not require a GUI at all.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Re: AllocateHwnd alternative on Linux?
« Reply #1 on: July 02, 2022, 06:06:24 pm »
  AllocateHwnd always returns zero on Linux. Did I miss something? Or maybe there's a more appropriate alternative (note TThread Synchronize/Queue I'd like to avoid)? The project does not require a GUI at all.

Some of those windows specific calls exist for LCL internal purposes, but are by no means full emulations, nor is there the intention to do this.

So in short: use TThread Synchronize/Queue. There is no GUI relation, it is a sysutils construction. 



jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: AllocateHwnd alternative on Linux?
« Reply #2 on: July 02, 2022, 10:14:36 pm »
I have a Delphi 11 project that extensively uses AllocateHwnd to communicate between the main thread and background threads (using SendMessage/SendMEssageTimeout/PostMessage). This is very convenient, but now I want to also make this work on Linux with Freepascal.

I was able to make it compile more or less, but AllocateHwnd and SendMessage, as it turned out, don't seem to work on Linux. If I use GTK2 it throws an error ("Widget is nil") and on GTK3 there's no error but it doesn't work. AllocateHwnd always returns zero on Linux. Did I miss something? Or maybe there's a more appropriate alternative (note TThread Synchronize/Queue I'd like to avoid)? The project does not require a GUI at all.

How about inserting the use of the TSimpleIPCClient and TSimpleIPCServer between all these apps that need to talk ?
The only true wisdom is knowing you know nothing

PascalDragon

  • Hero Member
  • *****
  • Posts: 5480
  • Compiler Developer
Re: AllocateHwnd alternative on Linux?
« Reply #3 on: July 03, 2022, 02:29:37 pm »
I have a Delphi 11 project that extensively uses AllocateHwnd to communicate between the main thread and background threads (using SendMessage/SendMEssageTimeout/PostMessage). This is very convenient, but now I want to also make this work on Linux with Freepascal.

The functionality of AllocateHwnd is very Windows specific. And FPC does not yet even provide this for non-Win32, because it requires some assembly level code to work correctly. So better look for other solutions to deal with this that are independant of the used widgetset.

How about inserting the use of the TSimpleIPCClient and TSimpleIPCServer between all these apps that need to talk ?

lazycat2 mentioned main and background threads, not processes. While TSimpleIPC* can be used for that, it's simply overkill and simpler mechanisms can be used there.

lazycat2

  • New member
  • *
  • Posts: 8
Re: AllocateHwnd alternative on Linux?
« Reply #4 on: July 04, 2022, 03:57:12 pm »
Some of those windows specific calls exist for LCL internal purposes, but are by no means full emulations, nor is there the intention to do this.
My first impression (based on successful compilation on Linux) was that those features were somehow rewritten to become cross-platform, that would have made porting a lot easier in my case :) Sad that it's not the case.
By the way, shouldn't it throw an error or something (runtime or compile-time) to make it obvious that it won't work? On GTK3 it silently fails.

How about inserting the use of the TSimpleIPCClient and TSimpleIPCServer between all these apps that need to talk ?
I'm interested in in-process communication - between background and main thread mostly, I'm not sure those classes are the right fit for the task.

lazycat2

  • New member
  • *
  • Posts: 8
Re: AllocateHwnd alternative on Linux?
« Reply #5 on: July 04, 2022, 03:58:13 pm »
simpler mechanisms can be used there.
Apart from using TThread methods, is there anything else I should look at? Before I go all-in rewriting the code to use Queue/Synchronize.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5480
  • Compiler Developer
Re: AllocateHwnd alternative on Linux?
« Reply #6 on: July 05, 2022, 02:20:39 pm »
By the way, shouldn't it throw an error or something (runtime or compile-time) to make it obvious that it won't work? On GTK3 it silently fails.

Would probably be worth a bug report as this might otherwise lead to hard to debug problems.

maxerist

  • New Member
  • *
  • Posts: 26
Re: AllocateHwnd alternative on Linux?
« Reply #7 on: October 18, 2022, 05:35:52 pm »
Recently I made an attempt to implement an AllocateHWnd replacement object (almost drop-in) using pipes, but currently it supports only asynchronous messaging (PostMessage). Tested on Windows and Linux by posting messaging from multiple threads and from the main thread itself. It has no os-specific dependencies so probably should work on other systems too.

This (attached) unit contains only about 100 lines of code (utilizing lazarus TInputPipeStream, TOutputPipeStream classes and AddPipeEventHandler) so it is self-explanatory and may be used as a template (changing the record fields for example). In my own case I needed PeekMessage functionality for checking message existence so another class keeps its own thread-safe accessed queue and pipes are used just as a signals. As for SendMessage, I suspect it will be trickier



 

TinyPortal © 2005-2018