Add 'optional' property to connector configuration
[src/drm-lease-manager.git] / drm-lease-manager / lease-manager.c
index b339037..3834631 100644 (file)
@@ -343,7 +343,8 @@ static struct lease *lease_create(struct lm *lm,
                goto err;
        }
 
-       int nconnectors = config->cnames > 0 ? config->cnames : config->ncids;
+       int nconnectors =
+           config->nconnectors > 0 ? config->nconnectors : config->ncids;
        int nobjects = lm->drm_plane_resource->count_planes +
                       nconnectors * DRM_OBJECTS_PER_CONNECTOR;
 
@@ -355,11 +356,27 @@ static struct lease *lease_create(struct lm *lm,
 
        for (int i = 0; i < nconnectors; i++) {
                uint32_t cid;
+               struct connector_config *con_config = NULL;
 
-               if (config->cnames > 0) {
-                       char *connector_name = config->connector_names[i];
+               if (config->nconnectors > 0)
+                       con_config = &config->connectors[i];
 
-                       if (!drm_find_connector(lm, connector_name, &cid)) {
+               if (con_config) {
+                       char *connector_name = con_config->name;
+                       bool optional = con_config->optional;
+
+                       bool found =
+                           drm_find_connector(lm, connector_name, &cid);
+
+                       bool missing_mandatory = !found && !optional;
+                       bool missing_optional = !found && optional;
+
+                       if (missing_mandatory) {
+                               ERROR_LOG("Lease: %s, "
+                                         "mandatory connector %s not found\n",
+                                         config->lease_name, connector_name);
+                               goto err;
+                       } else if (missing_optional) {
                                WARN_LOG("Lease: %s, "
                                         "unknown DRM connector: %s\n",
                                         config->lease_name, connector_name);
@@ -515,6 +532,29 @@ err:
        return NULL;
 }
 
+static struct lm *drm_find_drm_device(const char *device)
+{
+       drmDevicePtr devices[64];
+       int ndevs;
+       struct lm *lm = NULL;
+
+       if (device)
+               return drm_device_get_resources(device);
+
+       ndevs = drmGetDevices2(0, devices, 64);
+
+       for (int i = 0; i < ndevs; i++) {
+               lm = drm_device_get_resources(
+                   devices[i]->nodes[DRM_NODE_PRIMARY]);
+               if (lm)
+                       break;
+       }
+
+       drmFreeDevices(devices, ndevs);
+
+       return lm;
+}
+
 static int lm_create_leases(struct lm *lm, int num_leases,
                            const struct lease_config *configs)
 {
@@ -544,10 +584,12 @@ struct lm *lm_create_with_config(const char *device, int num_leases,
                                 struct lease_config *configs)
 {
        struct lease_config *default_configs = NULL;
-       struct lm *lm = drm_device_get_resources(device);
+       struct lm *lm = drm_find_drm_device(device);
 
-       if (!lm)
+       if (!lm) {
+               ERROR_LOG("No available DRM device found\n");
                return NULL;
+       }
 
        if (configs == NULL || num_leases == 0) {
                num_leases = create_default_lease_configs(lm, &default_configs);