Support grab as an option

This patch supports --grab/--nograb as an option in both
webplot.sh and webplot.py

BUG=chromium:475169
TEST=Emerge and deploy webplot to a chromebook.

Test case 1:
------------
Run webplot without grabbing the touch device as
$ webplot --nograb

Or cd to the webplot directory, and type
$ python webplot.py --nograb

Type "localhost" on a chrome tab. Make a 2f-click.
Observe that there pops up a 2f right-click menu.

Test case 2:
------------
Now run webplot grabbing the touch device as default
$ webplot [--grab]            # --grab is optional

Or cd to the webplot directory, and type
$ python webplot.py [--grab]  # --grab is optional

Type "localhost" on a chrome tab. Make a 2f-click.
Observe that there is NO pop-up right-click menu
since the touch device has been grabbed by webplot.

Change-Id: I415864eb2825778ea937c452c0e5da7823e07871
Reviewed-on: https://chromium-review.googlesource.com/264897
Reviewed-by: Charlie Mooney <charliemooney@chromium.org>
Commit-Queue: Shyh-In Hwang <josephsih@chromium.org>
Tested-by: Shyh-In Hwang <josephsih@chromium.org>
diff --git a/webplot/webplot.py b/webplot/webplot.py
index af62e9b..936794f 100755
--- a/webplot/webplot.py
+++ b/webplot/webplot.py
@@ -136,11 +136,11 @@
   and ending of the event stream.
   """
 
-  def __init__(self, dut_type, addr, is_touchscreen):
+  def __init__(self, dut_type, addr, is_touchscreen, grab):
     if dut_type == 'chromeos':
       if addr is None:
         addr = '127.0.0.1'
-      self.device = ChromeOSTouchDevice(addr, is_touchscreen)
+      self.device = ChromeOSTouchDevice(addr, is_touchscreen, grab=grab)
     else:
       self.device = AndroidTouchDevice(addr, True)
 
@@ -430,15 +430,31 @@
   parser = argparse.ArgumentParser(description='Webplot Server')
   parser.add_argument('-d', '--dut_addr', default=None,
                       help='the address of the dut')
-  parser.add_argument('-s', '--server_addr', default='localhost',
-                      help='the address the webplot http server listens to')
-  parser.add_argument('-p', '--server_port', default=80, type=int,
-                      help='the port the web server to listen to (default: 80)')
+
+  # Make an exclusive group to make the webplot.py command option
+  # consistent with the webplot.sh script command option.
+  # What is desired:
+  #   When no command option specified in webplot.sh/webplot.py: grab is True
+  #   When '--grab'   option specified in webplot.sh/webplot.py: grab is True
+  #   When '--nograb' option specified in webplot.sh/webplot.py: grab is False
+  grab_group = parser.add_mutually_exclusive_group()
+  grab_group.add_argument('--grab', help='grab the device exclusively',
+                          action='store_true')
+  grab_group.add_argument('--nograb', help='do not grab the device',
+                          action='store_true')
+
   parser.add_argument('--is_touchscreen', help='the DUT is touchscreen',
                       action='store_true')
-  parser.add_argument('-t', '--dut_type', default='chromeos',
+  parser.add_argument('-p', '--server_port', default=80, type=int,
+                      help='the port the web server to listen to (default: 80)')
+  parser.add_argument('-s', '--server_addr', default='localhost',
+                      help='the address the webplot http server listens to')
+  parser.add_argument('-t', '--dut_type', default='chromeos', type=str.lower,
                       help='dut type: chromeos, android')
   args = parser.parse_args()
+
+  args.grab = not args.nograb
+
   return args
 
 
@@ -454,6 +470,10 @@
   cherrypy.log('dut address: %s' % args.dut_addr)
   cherrypy.log('web server address: %s' % args.server_addr)
   cherrypy.log('web server port: %s' % args.server_port)
+  cherrypy.log('grab the touch device: %s' % args.grab)
+  if args.dut_type == 'android' and args.grab:
+    cherrypy.log('Warning: the grab option is not supported on Android devices'
+                 ' yet.')
   cherrypy.log('touch events are saved in %s' % SAVED_FILE)
   print '-' * 70 + '\n\n'
 
@@ -474,7 +494,8 @@
   print '*' * 70 + '\n\n'
 
   # Instantiate a touch device.
-  device = TouchDeviceWrapper(args.dut_type, args.dut_addr, args.is_touchscreen)
+  device = TouchDeviceWrapper(args.dut_type, args.dut_addr, args.is_touchscreen,
+                              args.grab)
 
   # Instantiate a webplot server daemon and start it.
   webplot = Webplot(args.server_addr, args.server_port, device)