Recent

Author Topic: How to pass a message/signal to a running program?  (Read 3095 times)

edvard

  • Full Member
  • ***
  • Posts: 172
How to pass a message/signal to a running program?
« on: December 21, 2014, 04:41:43 pm »
OK, I have a program in which I would like to control hiding/showing of the main form with a global hotkey.  Global hotkeys on Linux are problematic, if not downright impossible.  So, I'm thinking of a workaround in which I include with my program a Help or README file which describes how to set a hotkey in the major Desktop Environments (Gnome, KDE, Xfce, Lxde/Openbox).  My problem is, what is the best way to have the hotkey signal the running program that something needs to happen? 

There are two ways I can see:
1- Have the hotkey launch a smaller program whose only purpose is to send a signal to the currently-running program and quit.  This would involve some sort of socket sending/listening I assume? (reading up on sockets unit now).
2- Have the hotkey launch a second instance of the program, but with a parameter which will tell it to run a procedure to check for an already-running instance, and if found, send the appropriate signal to it.  Again, I assume this would mean additional code for sending and listening to sockets.

Which method is better?  Is there another/better alternative?  Am I correct that sending/listening over sockets is the way to go?  Which sockets would be best for this purpose?  Would I put the code in the project (the .lpr file), or in a separate unit? (in the interest of code cleanliness, I'd like to keep it out of the main form unit.) 
Links to sample code is appreciated, thank you.  I'm not asking anybody to write my homework for me...
« Last Edit: December 21, 2014, 04:45:56 pm by edvard »
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to pass a message/signal to a running program?
« Reply #1 on: December 21, 2014, 05:46:40 pm »
You might want to know about SimpleIPC. You can find the server and client components on System tab. Find ipcclient.pp/ipcserver.pp examples in Lazarus\fpc\2.x.x\source\packages\fcl-base\examples folder.

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: How to pass a message/signal to a running program?
« Reply #2 on: December 21, 2014, 07:13:16 pm »
Hello,

You might use also => https://github.com/fredvs/runonce_postit

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

edvard

  • Full Member
  • ***
  • Posts: 172
Re: How to pass a message/signal to a running program?
« Reply #3 on: December 22, 2014, 04:17:34 am »
Thanks for the replies, looks like either one will suffice.  Both are simple and clean interfaces, I like that.  Doing some testing with SimpleIPC now, but RunOncePostit looks easy.
All children left unattended will be given a mocha and a puppy.

Arch (though I may go back to Debian)| FreePascal 3.2.2 + Lazarus 2.2.4, GTK2+ and Qt.  Mostly Qt...

 

TinyPortal © 2005-2018