Uses the default display

When the user sets a display ID as -1, evs_app uses the default
display that is the first entry of the list in config.json file.

$ adb shell /system/bin/evs_app --test --display -1

Fix: 156813260
Test: Run above example
Change-Id: Iea389fad320345fc4741a3ed134d1eaf83c1c4dc
diff --git a/evs/apps/default/ConfigManager.h b/evs/apps/default/ConfigManager.h
index 9c6d1a2..7c2186d 100644
--- a/evs/apps/default/ConfigManager.h
+++ b/evs/apps/default/ConfigManager.h
@@ -16,8 +16,9 @@
 #ifndef CONFIG_MANAGER_H
 #define CONFIG_MANAGER_H
 
-#include <vector>
+#include <cerrno>
 #include <string>
+#include <vector>
 
 #include <system/graphics-base.h>
 
@@ -78,16 +79,27 @@
 
     const std::vector<CameraInfo>& getCameras() const   { return mCameras; };
 
-    bool  setActiveDisplayId(int displayId) {
-        if (displayId < 0 or displayId > mDisplays.size()) {
-            printf("Display %d is invalid.  Current active display is display %d.",
-                   displayId, mActiveDisplayId);
-            return false;
+    int  setActiveDisplayId(int displayId) {
+        if (displayId == -1) {
+            // -1 is reserved for the default display, which is the first
+            // display in config.json's display list
+            printf("Uses a display with id %d", mDisplays[0].port);
+            mActiveDisplayId = mDisplays[0].port;
+            return mActiveDisplayId;
+        } else if (displayId < 0) {
+            printf("Display %d is invalid.", displayId);
+            return -ENOENT;
+        } else {
+            for (auto display : mDisplays) {
+                if (display.port == displayId) {
+                    mActiveDisplayId = displayId;
+                    return mActiveDisplayId;
+                }
+            }
+
+            printf("Display %d does not exist.", displayId);
+            return -ENOENT;
         }
-
-        mActiveDisplayId = displayId;
-
-        return true;
     }
     const std::vector<DisplayInfo>& getDisplays() const { return mDisplays; };
     const DisplayInfo& getActiveDisplay() const { return mDisplays[mActiveDisplayId]; };
diff --git a/evs/apps/default/config.json.readme b/evs/apps/default/config.json.readme
index 7d1ec9d..561adcc 100644
--- a/evs/apps/default/config.json.readme
+++ b/evs/apps/default/config.json.readme
@@ -19,7 +19,7 @@
     "rearExtent" : 40           // The extent of the car body behind the read axel
   },
   "displays" : [                // This configures the dimensions of the surround view display
-    {
+    {                           // The first display will be used as the default display
       "displayPort" : 1         // Display port number, the target display is connected to.
       "frontRange" : 100,       // How far to render the view in front of the front bumper
       "rearRange" : 100         // How far the view extends behind the rear bumper
diff --git a/evs/apps/default/evs_app.cpp b/evs/apps/default/evs_app.cpp
index 9f2f2c8..b30bcc7 100644
--- a/evs/apps/default/evs_app.cpp
+++ b/evs/apps/default/evs_app.cpp
@@ -94,7 +94,7 @@
     bool useVehicleHal = true;
     bool printHelp = false;
     const char* evsServiceName = "default";
-    int displayId = 1;
+    int displayId = -1;
     bool useExternalMemory = false;
     android_pixel_format_t extMemoryFormat = HAL_PIXEL_FORMAT_RGBA_8888;
     for (int i=1; i< argc; i++) {
@@ -133,7 +133,8 @@
         printf("  --test\n\tDo not talk to Vehicle Hal, but simulate 'reverse' instead\n");
         printf("  --hw\n\tBypass EvsManager by connecting directly to EvsEnumeratorHw\n");
         printf("  --mock\n\tConnect directly to EvsEnumeratorHw-Mock\n");
-        printf("  --display\n\tSpecify the display to use\n");
+        printf("  --display\n\tSpecify the display to use.  If this is not set, the first"
+                              "display in config.json's list will be used.\n");
         printf("  --extmem  <format>\n\t"
                "Application allocates buffers to capture camera frames.  "
                "Available format strings are (case insensitive):\n");
@@ -153,7 +154,7 @@
     ConfigManager config;
     if (!config.initialize("/system/etc/automotive/evs/config.json")) {
         LOG(ERROR) << "Missing or improper configuration for the EVS application.  Exiting.";
-        return 1;
+        return EXIT_FAILURE;
     }
 
     // Set thread pool size to one to avoid concurrent events from the HAL.
@@ -171,19 +172,25 @@
     if (pEvs.get() == nullptr) {
         LOG(ERROR) << "getService(" << evsServiceName
                    << ") returned NULL.  Exiting.";
-        return 1;
+        return EXIT_FAILURE;
     }
 
     // Request exclusive access to the EVS display
     LOG(INFO) << "Acquiring EVS Display";
 
     // We'll use an available display device.
+    displayId = config.setActiveDisplayId(displayId);
+    if (displayId < 0) {
+        PLOG(ERROR) << "EVS Display is unknown.  Exiting.";
+        return EXIT_FAILURE;
+    }
+
     android::sp<IEvsDisplay> pDisplay = pEvs->openDisplay_1_1(displayId);
     if (pDisplay.get() == nullptr) {
         LOG(ERROR) << "EVS Display unavailable.  Exiting.";
-        return 1;
+        return EXIT_FAILURE;
     }
-    config.setActiveDisplayId(displayId);
+
     config.useExternalMemory(useExternalMemory);
     config.setExternalMemoryFormat(extMemoryFormat);
 
@@ -194,13 +201,13 @@
         pVnet = IVehicle::getService();
         if (pVnet.get() == nullptr) {
             LOG(ERROR) << "Vehicle HAL getService returned NULL.  Exiting.";
-            return 1;
+            return EXIT_FAILURE;
         } else {
             // Register for vehicle state change callbacks we care about
             // Changes in these values are what will trigger a reconfiguration of the EVS pipeline
             if (!subscribeToVHal(pVnet, pEvsListener, VehicleProperty::GEAR_SELECTION)) {
                 LOG(ERROR) << "Without gear notification, we can't support EVS.  Exiting.";
-                return 1;
+                return EXIT_FAILURE;
             }
             if (!subscribeToVHal(pVnet, pEvsListener, VehicleProperty::TURN_SIGNAL_STATE)) {
                 LOG(WARNING) << "Didn't get turn signal notifications, so we'll ignore those.";
@@ -215,7 +222,7 @@
     EvsStateControl *pStateController = new EvsStateControl(pVnet, pEvs, pDisplay, config);
     if (!pStateController->startUpdateLoop()) {
         LOG(ERROR) << "Initial configuration failed.  Exiting.";
-        return 1;
+        return EXIT_FAILURE;
     }
 
     // Run forever, reacting to events as necessary
@@ -226,5 +233,5 @@
     // One known example is if another process preempts our registration for our service name.
     LOG(ERROR) << "EVS Listener stopped.  Exiting.";
 
-    return 0;
+    return EXIT_SUCCESS;
 }