#!/usr/bin/python
# Copyright 2010 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Executes applied command periodically while Space Navigator isn't touched.
It can be used to add screensaver-ish application to Liquid Galaxy.
"""

import fcntl
import os
import sys
import time
import statsd

# Config
tour_check_per = 30
tour_wait_for_trigger = 4
# Input Devices
space_nav = "/dev/input/spacenavigator"
quanta_ts = "/dev/input/quanta_touch"
# Stats
statsd_connection = statsd.Connection(
  host = 'lg-head',
  port = 8125,
  sample_rate = 1
)
statsd_client = statsd.Client('usage', statsd_connection)

active_counter = statsd_client.get_client( 'active', statsd.Counter )
idle_counter = statsd_client.get_client( 'idle', statsd.Counter )

def Touched(dev1, dev2):
  # open file handles first
  devlist = []
  for dev in [dev1, dev2]:
    try:
      a = open(dev)
    except:
      print "Failed to open {0}".format( dev )
    else:
      devlist.append(a)

  for dev in devlist:
    fd = dev.fileno()
    fcntl.fcntl(fd, fcntl.F_SETFL, os.O_NONBLOCK)

  # then sleep and let input accumulate
  time.sleep(tour_check_per)

  # then read input data, if any
  for dev in devlist:
    try:
      dev.read(6000)
      return True
    except IOError:
      pass

  # default to return False if no USB devs Touched
  return False

def main():
  if len(sys.argv) != 2:
    print "Usage:", sys.argv[0], "<command>"
    print "<command> will be called every", tour_check_per, "seconds",
    print "after", (tour_check_per * tour_wait_for_trigger), "seconds"
    print "if USB devs are not touched."
    sys.exit(1)
  
  cmd = sys.argv[1]
  cnt = 0
  while True:
    if Touched(space_nav, quanta_ts):
      cnt = 0
      print "Touched."
      active_counter += tour_check_per
    else:
      cnt += 1
      if cnt >= tour_wait_for_trigger:
        print cmd
        os.system(cmd)
        idle_counter += tour_check_per
      else:
        print "Wait...", cnt
        active_counter += tour_check_per

if __name__ == '__main__':
  main()

@@ -960,296 +960,39 @@
 ats%0A
-statsd_connection = statsd.Connection(%0A  host = 'lg-head',%0A  port = 8125,%0A  sample_rate = 1%0A)%0Astatsd_client = statsd.Client('usage', statsd_connection)%0A%0Aactive_counter = statsd_client.get_client( 'active', statsd.Counter )%0Aidle_counter = statsd_client.get_client( 'idle', statsd.Counter )
+IDLE_STATE = 0%0AACTIVE_STATE = 1
 %0A%0Ade
@@ -1586,16 +1586,526 @@
 main():%0A
+# Stats%0A  statsd_connection = statsd.Connection(%0A    host = 'lg-head',%0A    port = 8125,%0A    sample_rate = 1%0A  )%0A  statsd_client = statsd.Client('usage', statsd_connection)%0A  %0A  #active_counter = statsd_client.get_client( name = 'active', class_ = statsd.Counter )%0A  #idle_counter = statsd_client.get_client( name = 'idle', class_ = statsd.Counter )%0A  # Init timers%0A  state = IDLE_STATE%0A  idle_timer = statsd_client.get_client( name = 'idle', class_ = statsd.Timer )%0A  idle_timer.start()%0A  active_timer = None%0A%0A
   if len
@@ -2497,40 +2497,325 @@
     
-active_counter += tour_check_per
+if state == IDLE_STATE:%0A        if idle_timer != None:%0A          idle_timer.stop()%0A        active_timer = statsd_client.get_client( name = 'active', class_ = statsd.Timer )%0A        active_timer.start()%0A        state = ACTIVE_STATE%0A      elif state == ACTIVE_STATE:%0A        active_timer.intermediate(subname = 'total')
 %0A   
@@ -2924,26 +2924,25 @@
         
+#
 idle_
-count
+tim
 er += to
@@ -3003,28 +3003,25 @@
         
-active_count
+#idle_tim
 er += to
@@ -3032,16 +3032,334 @@
 heck_per
+%0A      if state == ACTIVE_STATE:%0A        if active_timer != None:%0A          active_timer.stop()%0A        idle_timer = statsd_client.get_client( name = 'idle', class_ = statsd.Timer )%0A        idle_timer.start()%0A        state = IDLE_STATE%0A      elif state == IDLE_STATE:%0A        idle_timer.intermediate(subname = 'total')
 %0A%0Aif __n

Update screensaver with statsd Timer