System Event Notification
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
System Event Notification
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?
Is such a plugin/extension possible?
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: System Event Notification
Hm. I can't think of any built-in notification, but you could poll for it without much trouble.
A one second polling delay shouldn't affect the responsiveness of your app at all.
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
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: System Event Notification
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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
-
- VIP Livecode Opensource Backer
- Posts: 7258
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: System Event Notification
Of course, both our versions will also trigger when a volume is ejected.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 3581
- Joined: Mon Jan 22, 2007 7:36 am
- Location: Berkeley, CA, US
- Contact:
Re: System Event Notification
...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
PowerDebug http://powerdebug.ahsoftware.net
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
PowerTools http://www.ahsoftware.net/PowerTools/PowerTools.irev
Re: System Event Notification
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:
Note that you wouldn't need to code the scheduling and other operational control.
Here's what the library does:
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
Here's what the library does:
-- DickDispatch 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.