static void meta_monitor_manager_kms_read_current (MetaMonitorManager *manager) { MetaMonitorManagerKms *manager_kms = META_MONITOR_MANAGER_KMS (manager); drmModeRes *resources; drmModeEncoder **encoders; GHashTable *modes; GHashTableIter iter; drmModeModeInfo *mode; unsigned int i, j, k; unsigned int n_actual_outputs; int width, height; MetaOutput *old_outputs; unsigned int n_old_outputs; resources = drmModeGetResources(manager_kms->fd); modes = g_hash_table_new (drm_mode_hash, drm_mode_equal); manager->max_screen_width = resources->max_width; manager->max_screen_height = resources->max_height; manager->power_save_mode = META_POWER_SAVE_ON; old_outputs = manager->outputs; n_old_outputs = manager->n_outputs; /* Note: we must not free the public structures (output, crtc, monitor mode and monitor info) here, they must be kept alive until the API users are done with them after we emit monitors-changed, and thus are freed by the platform-independent layer. */ free_resources (manager_kms); manager_kms->n_connectors = resources->count_connectors; manager_kms->connectors = g_new (drmModeConnector *, manager_kms->n_connectors); for (i = 0; i < manager_kms->n_connectors; i++) { drmModeConnector *connector; connector = drmModeGetConnector (manager_kms->fd, resources->connectors[i]); manager_kms->connectors[i] = connector; if (connector && connector->connection == DRM_MODE_CONNECTED) { /* Collect all modes for this connector */ for (j = 0; j < (unsigned)connector->count_modes; j++) g_hash_table_add (modes, &connector->modes[j]); } } encoders = g_new (drmModeEncoder *, resources->count_encoders); for (i = 0; i < (unsigned)resources->count_encoders; i++) encoders[i] = drmModeGetEncoder (manager_kms->fd, resources->encoders[i]); manager->n_modes = g_hash_table_size (modes); manager->modes = g_new0 (MetaMonitorMode, manager->n_modes); g_hash_table_iter_init (&iter, modes); i = 0; while (g_hash_table_iter_next (&iter, NULL, (gpointer)&mode)) { MetaMonitorMode *meta_mode; meta_mode = &manager->modes[i]; meta_mode->mode_id = i; meta_mode->name = g_strndup (mode->name, DRM_DISPLAY_MODE_LEN); meta_mode->width = mode->hdisplay; meta_mode->height = mode->vdisplay; /* Calculate refresh rate in milliHz first for extra precision. */ meta_mode->refresh_rate = (mode->clock * 1000000LL) / mode->htotal; meta_mode->refresh_rate += (mode->vtotal / 2); meta_mode->refresh_rate /= mode->vtotal; if (mode->flags & DRM_MODE_FLAG_INTERLACE) meta_mode->refresh_rate *= 2; if (mode->flags & DRM_MODE_FLAG_DBLSCAN) meta_mode->refresh_rate /= 2; if (mode->vscan > 1) meta_mode->refresh_rate /= mode->vscan; meta_mode->refresh_rate /= 1000.0; meta_mode->driver_private = g_slice_dup (drmModeModeInfo, mode); meta_mode->driver_notify = (GDestroyNotify)meta_monitor_mode_destroy_notify; i++; } g_hash_table_destroy (modes); manager->n_crtcs = resources->count_crtcs; manager->crtcs = g_new0 (MetaCRTC, manager->n_crtcs); width = 0; height = 0; for (i = 0; i < (unsigned)resources->count_crtcs; i++) { drmModeCrtc *crtc; MetaCRTC *meta_crtc; crtc = drmModeGetCrtc (manager_kms->fd, resources->crtcs[i]); meta_crtc = &manager->crtcs[i]; meta_crtc->crtc_id = crtc->crtc_id; meta_crtc->rect.x = crtc->x; meta_crtc->rect.y = crtc->y; meta_crtc->rect.width = crtc->width; meta_crtc->rect.height = crtc->height; meta_crtc->is_dirty = FALSE; meta_crtc->transform = META_MONITOR_TRANSFORM_NORMAL; /* FIXME: implement! */ meta_crtc->all_transforms = 1 << META_MONITOR_TRANSFORM_NORMAL; if (crtc->mode_valid) { for (j = 0; j < manager->n_modes; j++) { if (drm_mode_equal (&crtc->mode, manager->modes[j].driver_private)) { meta_crtc->current_mode = &manager->modes[j]; break; } } width = MAX (width, meta_crtc->rect.x + meta_crtc->rect.width); height = MAX (height, meta_crtc->rect.y + meta_crtc->rect.height); } meta_crtc->driver_private = g_new0 (MetaCRTCKms, 1); meta_crtc->driver_notify = (GDestroyNotify) meta_crtc_destroy_notify; find_crtc_properties (manager_kms, meta_crtc); init_crtc_rotations (manager, meta_crtc, i); drmModeFreeCrtc (crtc); } manager->screen_width = width; manager->screen_height = height; manager->outputs = g_new0 (MetaOutput, manager_kms->n_connectors); n_actual_outputs = 0; for (i = 0; i < manager_kms->n_connectors; i++) { MetaOutput *meta_output, *old_output; MetaOutputKms *output_kms; drmModeConnector *connector; GArray *crtcs; unsigned int crtc_mask; GBytes *edid; connector = manager_kms->connectors[i]; meta_output = &manager->outputs[n_actual_outputs]; if (connector && connector->connection == DRM_MODE_CONNECTED) { meta_output->driver_private = output_kms = g_slice_new0 (MetaOutputKms); meta_output->driver_notify = (GDestroyNotify)meta_output_destroy_notify; meta_output->winsys_id = connector->connector_id; meta_output->name = make_output_name (connector); meta_output->width_mm = connector->mmWidth; meta_output->height_mm = connector->mmHeight; switch (connector->subpixel) { case DRM_MODE_SUBPIXEL_NONE: meta_output->subpixel_order = COGL_SUBPIXEL_ORDER_NONE; break; case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB: meta_output->subpixel_order = COGL_SUBPIXEL_ORDER_HORIZONTAL_RGB; break; case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR: meta_output->subpixel_order = COGL_SUBPIXEL_ORDER_HORIZONTAL_BGR; break; case DRM_MODE_SUBPIXEL_VERTICAL_RGB: meta_output->subpixel_order = COGL_SUBPIXEL_ORDER_VERTICAL_RGB; break; case DRM_MODE_SUBPIXEL_VERTICAL_BGR: meta_output->subpixel_order = COGL_SUBPIXEL_ORDER_VERTICAL_BGR; break; case DRM_MODE_SUBPIXEL_UNKNOWN: default: meta_output->subpixel_order = COGL_SUBPIXEL_ORDER_UNKNOWN; break; } meta_output->preferred_mode = NULL; meta_output->n_modes = connector->count_modes; meta_output->modes = g_new0 (MetaMonitorMode *, meta_output->n_modes); for (j = 0; j < meta_output->n_modes; j++) { meta_output->modes[j] = find_meta_mode (manager, &connector->modes[j]); if (connector->modes[j].type & DRM_MODE_TYPE_PREFERRED) meta_output->preferred_mode = meta_output->modes[j]; } if (!meta_output->preferred_mode) meta_output->preferred_mode = meta_output->modes[0]; output_kms->connector = connector; output_kms->n_encoders = connector->count_encoders; output_kms->encoders = g_new0 (drmModeEncoderPtr, output_kms->n_encoders); crtc_mask = ~(unsigned int)0; for (j = 0; j < output_kms->n_encoders; j++) { output_kms->encoders[j] = drmModeGetEncoder (manager_kms->fd, connector->encoders[j]); if (!output_kms->encoders[j]) continue; /* We only list CRTCs as supported if they are supported by all encoders for this connectors. This is what xf86-video-modesetting does (see drmmode_output_init()) */ crtc_mask &= output_kms->encoders[j]->possible_crtcs; if (output_kms->encoders[j]->encoder_id == connector->encoder_id) output_kms->current_encoder = output_kms->encoders[j]; } crtcs = g_array_new (FALSE, FALSE, sizeof (MetaCRTC*)); for (j = 0; j < manager->n_crtcs; j++) { if (crtc_mask & (1 << j)) { MetaCRTC *crtc = &manager->crtcs[j]; g_array_append_val (crtcs, crtc); } } meta_output->n_possible_crtcs = crtcs->len; meta_output->possible_crtcs = (void*)g_array_free (crtcs, FALSE); if (output_kms->current_encoder && output_kms->current_encoder->crtc_id != 0) { for (j = 0; j < manager->n_crtcs; j++) { if (manager->crtcs[j].crtc_id == output_kms->current_encoder->crtc_id) { meta_output->crtc = &manager->crtcs[j]; break; } } } else meta_output->crtc = NULL; old_output = find_output_by_id (old_outputs, n_old_outputs, meta_output->winsys_id); if (old_output) { meta_output->is_primary = old_output->is_primary; meta_output->is_presentation = old_output->is_presentation; } else { meta_output->is_primary = FALSE; meta_output->is_presentation = FALSE; } find_connector_properties (manager_kms, output_kms); meta_output->suggested_x = output_kms->suggested_x; meta_output->suggested_y = output_kms->suggested_y; meta_output->hotplug_mode_update = output_kms->hotplug_mode_update; edid = read_output_edid (manager_kms, meta_output); meta_output_parse_edid (meta_output, edid); g_bytes_unref (edid); /* MetaConnectorType matches DRM's connector types */ meta_output->connector_type = (MetaConnectorType) connector->connector_type; meta_output->scale = get_output_scale (manager, meta_output); output_get_tile_info (manager_kms, meta_output); /* FIXME: backlight is a very driver specific thing unfortunately, every DDX does its own thing, and the dumb KMS API does not include it. For example, xf86-video-intel has a list of paths to probe in /sys/class/backlight (one for each major HW maker, and then some). We can't do the same because we're not root. It might be best to leave backlight out of the story and rely on the setuid helper in gnome-settings-daemon. */ meta_output->backlight_min = 0; meta_output->backlight_max = 0; meta_output->backlight = -1; n_actual_outputs++; } }
MetaOutput * meta_create_xrandr_output (MetaGpuXrandr *gpu_xrandr, XRROutputInfo *xrandr_output, RROutput output_id, RROutput primary_output) { MetaOutputXrandr *output_xrandr; MetaOutput *output; GBytes *edid; unsigned int i; output = g_object_new (META_TYPE_OUTPUT, NULL); output->gpu = META_GPU (gpu_xrandr); output->winsys_id = output_id; output->name = g_strdup (xrandr_output->name); output_xrandr = g_slice_new0 (MetaOutputXrandr); output->driver_private = output_xrandr; output->driver_notify = (GDestroyNotify)meta_output_xrandr_destroy_notify; edid = meta_output_xrandr_read_edid (output); meta_output_parse_edid (output, edid); g_bytes_unref (edid); output->subpixel_order = COGL_SUBPIXEL_ORDER_UNKNOWN; output->hotplug_mode_update = output_get_hotplug_mode_update (output); output->suggested_x = output_get_suggested_x (output); output->suggested_y = output_get_suggested_y (output); output->connector_type = output_get_connector_type (output); output->panel_orientation_transform = output_get_panel_orientation_transform (output); if (meta_monitor_transform_is_rotated ( output->panel_orientation_transform)) { output->width_mm = xrandr_output->mm_height; output->height_mm = xrandr_output->mm_width; } else { output->width_mm = xrandr_output->mm_width; output->height_mm = xrandr_output->mm_height; } output_get_tile_info (output); output_get_modes (output, xrandr_output); output_get_crtcs (output, xrandr_output); output->n_possible_clones = xrandr_output->nclone; output->possible_clones = g_new0 (MetaOutput *, output->n_possible_clones); /* * We can build the list of clones now, because we don't have the list of * outputs yet, so temporarily set the pointers to the bare XIDs, and then * we'll fix them in a second pass. */ for (i = 0; i < (unsigned int) xrandr_output->nclone; i++) { output->possible_clones[i] = GINT_TO_POINTER (xrandr_output->clones[i]); } output->is_primary = ((XID) output->winsys_id == primary_output); output->is_presentation = output_get_presentation_xrandr (output); output->supports_underscanning = output_get_supports_underscanning_xrandr (output, &output_xrandr->underscan_value); output->is_underscanning = output_get_underscanning_xrandr (output); output_get_backlight_limits_xrandr (output); output_get_underscanning_borders_xrandr (output); /* Override the 'supports underscanning' property for non HDTV sets. * Note that we need to do this after checking if underscanning is on, so * that we now the exact values for width and height to be checked. */ if (output->supports_underscanning && !output_is_hdtv (output)) output->supports_underscanning = FALSE; if (!(output->backlight_min == 0 && output->backlight_max == 0)) output->backlight = output_get_backlight_xrandr (output); else output->backlight = -1; if (output->n_modes == 0 || output->n_possible_crtcs == 0) { g_object_unref (output); return NULL; } else { return output; } }