System Event Notification

Are you developing tools to extend the LiveCode environment? This is the place to talk about the nuts and bolts of extending our nuts and bolts. If you want to use a LiveCode or third party Environment extension, visit the Using Evironment Extensions forum.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
tgunr52
Posts: 1
Joined: Sat Aug 02, 2014 11:44 pm

System Event Notification

Post by tgunr52 » Wed Jul 06, 2016 4:43 am

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?

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4564
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: System Event Notification

Post by jacque » Wed Jul 06, 2016 7:18 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2690
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: System Event Notification

Post by mwieder » Thu Jul 07, 2016 4:18 am

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4564
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: System Event Notification

Post by jacque » Thu Jul 07, 2016 6:10 am

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

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2690
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: System Event Notification

Post by mwieder » Thu Jul 07, 2016 8:56 pm

...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

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 64
Joined: Thu Apr 13, 2006 6:25 pm

Re: System Event Notification

Post by rkriesel » Fri Jul 08, 2016 10:07 pm

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

Post Reply

Return to “Extending LiveCode”