Page 1 of 1
System Event Notification
Posted: Wed Jul 06, 2016 4:43 am
by tgunr52
Not sure which topic this should go under. I am looking to port an application I wrote in Xcode Cocoa to Livecode but don't seem to be able to find a key requirement, namely receiving notification from the system on certain events. In my case, I need a notification of anytime a disk volume is mounted. Obviously, it would be great to have a cross platform capabilioty but I am beginning to fear I would have to write an extenstion for Mac, and Windows.
Is such a plugin/extension possible?
Re: System Event Notification
Posted: Wed Jul 06, 2016 7:18 pm
by jacque
Hm. I can't think of any built-in notification, but you could poll for it without much trouble.
Code: Select all
global gVolumeList
on preOpenStack -- store the current volumes
put the volumes into gVolumeList
checkVolumes -- start polling
end preOpenStack
command checkVolumes
put the volumes into tCurVolumes
if tCurVolumes <> gVolumeList then
-- check for differences, do somethng with it
put tCurVolumes into gVolumneList -- update the global
end if
send "checkVolumes" to me in 1 second -- adjust timing as needed
end checkVolumes
A one second polling delay shouldn't affect the responsiveness of your app at all.
Re: System Event Notification
Posted: Thu Jul 07, 2016 4:18 am
by mwieder
I would go even farther with Jacque's suggestion:
Code: Select all
command checkVolumes
put the volumes into tCurVolumes
if tCurVolumes <> gVolumeList then
-- check for differences, do somethng with it
send "volumesChanged tCurVolumes" to me in 10 milliseconds
end if
send "checkVolumes" to me in 1 second -- adjust timing as needed
end checkVolumes
-- here's your notification handler
on volumesChanged pNewVolumes
put pNewVolumes into gVolumeList -- update the global
-- now do whatever needs to be done when a volume is inserted
end volumesChanged
Re: System Event Notification
Posted: Thu Jul 07, 2016 6:10 am
by jacque
Of course, both our versions will also trigger when a volume is ejected.
Re: System Event Notification
Posted: Thu Jul 07, 2016 8:56 pm
by mwieder
...and on rethinking this, it might also be good to check for previous messages before adding to the queue:
Code: Select all
if "checkVolumes" is not in the pending messages then
send "checkVolumes" to me in 1 second -- adjust timing as needed
end if
Re: System Event Notification
Posted: Fri Jul 08, 2016 10:07 pm
by rkriesel
If you're interested in using an LC library to help implement and manage such notifications, I have one you could try.
For your example, your code could look like this:
Code: Select all
on preOpenStack
if the owner of the target is me then
start using stack "libPolling.livecodescript"
startPoll "checkVolumes", "volumesChanged", 2, "seconds" -- note: "command startPoll pProbe, pAlert, pTime, pUnit"
end if
pass preOpenStack
end preOpenStack
command checkVolumes
return the volumes
end checkVolumes
command volumesChanged pNew, pOld
-- do something about the diff
end volumesChanged
Note that you wouldn't need to code the scheduling and other operational control.
Here's what the library does:
Dispatch each probe in its time.
When a probe returns a different value, dispatch its alert with the new and old values.
For polls individually, handle events: start*, change*, stop*, pause*, resume*, delete*, get*, poll
For polls collectively, handle events: start*, stop*, pause*, resume*, get*
On closing or releasing the library, stop all probes.
On opening the script-only stack, facilitate closing it.
-- Dick