2015-04-19 07:39:58 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2016-01-19 17:36:34 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-04-19 07:39:58 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-01-19 17:36:34 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2015-11-07 22:22:38 +00:00
|
|
|
*
|
2016-01-19 17:36:34 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2015-04-19 07:39:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
2016-10-04 20:07:19 +00:00
|
|
|
#include <string.h>
|
2015-04-19 07:39:58 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/ioctl.h>
|
2017-01-09 15:21:28 +00:00
|
|
|
#include <poll.h>
|
2016-10-04 20:07:19 +00:00
|
|
|
#include <sys/stat.h>
|
2015-04-19 07:39:58 +00:00
|
|
|
#include <unistd.h>
|
2018-06-02 10:54:05 +00:00
|
|
|
#include <limits.h>
|
2018-06-05 07:15:54 +00:00
|
|
|
#include <math.h>
|
2019-07-29 17:32:29 +00:00
|
|
|
#include <time.h>
|
2022-05-18 14:35:53 +00:00
|
|
|
#include <drm_fourcc.h>
|
2015-04-19 07:39:58 +00:00
|
|
|
|
2019-09-21 20:17:32 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#if HAVE_CONSIO_H
|
|
|
|
#include <sys/consio.h>
|
|
|
|
#else
|
|
|
|
#include <sys/vt.h>
|
|
|
|
#endif
|
|
|
|
|
2015-04-19 07:39:58 +00:00
|
|
|
#include "drm_common.h"
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "osdep/io.h"
|
2019-07-29 17:32:29 +00:00
|
|
|
#include "osdep/timer.h"
|
2018-06-02 10:53:58 +00:00
|
|
|
#include "misc/ctype.h"
|
2019-07-29 17:32:29 +00:00
|
|
|
#include "video/out/vo.h"
|
2015-04-19 07:39:58 +00:00
|
|
|
|
|
|
|
#define EVT_RELEASE 1
|
|
|
|
#define EVT_ACQUIRE 2
|
|
|
|
#define EVT_INTERRUPT 255
|
|
|
|
#define HANDLER_ACQUIRE 0
|
|
|
|
#define HANDLER_RELEASE 1
|
2015-10-30 17:55:48 +00:00
|
|
|
#define RELEASE_SIGNAL SIGUSR1
|
|
|
|
#define ACQUIRE_SIGNAL SIGUSR2
|
2016-10-04 20:07:19 +00:00
|
|
|
#define MAX_CONNECTOR_NAME_LEN 20
|
2015-04-19 07:39:58 +00:00
|
|
|
|
|
|
|
static int vt_switcher_pipe[2];
|
|
|
|
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
static int drm_connector_opt_help(
|
|
|
|
struct mp_log *log, const struct m_option *opt, struct bstr name);
|
|
|
|
|
|
|
|
static int drm_mode_opt_help(
|
|
|
|
struct mp_log *log, const struct m_option *opt, struct bstr name);
|
2018-06-02 10:54:01 +00:00
|
|
|
|
|
|
|
static int drm_validate_mode_opt(
|
|
|
|
struct mp_log *log, const struct m_option *opt, struct bstr name,
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
const char **value);
|
2018-06-02 10:54:01 +00:00
|
|
|
|
|
|
|
static void kms_show_available_modes(
|
|
|
|
struct mp_log *log, const drmModeConnector *connector);
|
|
|
|
|
2021-01-18 21:28:28 +00:00
|
|
|
static void kms_show_available_connectors(struct mp_log *log, int card_no,
|
|
|
|
const char *card_path);
|
2018-06-05 07:15:54 +00:00
|
|
|
static double mode_get_Hz(const drmModeModeInfo *mode);
|
2018-06-02 10:54:01 +00:00
|
|
|
|
2017-10-23 06:59:58 +00:00
|
|
|
#define OPT_BASE_STRUCT struct drm_opts
|
|
|
|
const struct m_sub_options drm_conf = {
|
|
|
|
.opts = (const struct m_option[]) {
|
2021-01-22 21:07:38 +00:00
|
|
|
{"drm-device", OPT_STRING(drm_device_path), .flags = M_OPT_FILE},
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
{"drm-connector", OPT_STRING(drm_connector_spec),
|
|
|
|
.help = drm_connector_opt_help},
|
|
|
|
{"drm-mode", OPT_STRING_VALIDATE(drm_mode_spec, drm_validate_mode_opt),
|
|
|
|
.help = drm_mode_opt_help},
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"drm-atomic", OPT_CHOICE(drm_atomic, {"no", 0}, {"auto", 1})},
|
|
|
|
{"drm-draw-plane", OPT_CHOICE(drm_draw_plane,
|
|
|
|
{"primary", DRM_OPTS_PRIMARY_PLANE},
|
|
|
|
{"overlay", DRM_OPTS_OVERLAY_PLANE}),
|
|
|
|
M_RANGE(0, INT_MAX)},
|
|
|
|
{"drm-drmprime-video-plane", OPT_CHOICE(drm_drmprime_video_plane,
|
|
|
|
{"primary", DRM_OPTS_PRIMARY_PLANE},
|
|
|
|
{"overlay", DRM_OPTS_OVERLAY_PLANE}),
|
|
|
|
M_RANGE(0, INT_MAX)},
|
|
|
|
{"drm-format", OPT_CHOICE(drm_format,
|
|
|
|
{"xrgb8888", DRM_OPTS_FORMAT_XRGB8888},
|
2021-11-06 03:05:42 +00:00
|
|
|
{"xrgb2101010", DRM_OPTS_FORMAT_XRGB2101010},
|
|
|
|
{"xbgr8888", DRM_OPTS_FORMAT_XBGR8888},
|
|
|
|
{"xbgr2101010", DRM_OPTS_FORMAT_XBGR2101010})},
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"drm-draw-surface-size", OPT_SIZE_BOX(drm_draw_surface_size)},
|
2018-12-01 11:01:17 +00:00
|
|
|
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"drm-osd-plane-id", OPT_REPLACED("drm-draw-plane")},
|
|
|
|
{"drm-video-plane-id", OPT_REPLACED("drm-drmprime-video-plane")},
|
|
|
|
{"drm-osd-size", OPT_REPLACED("drm-draw-surface-size")},
|
2022-04-04 03:08:33 +00:00
|
|
|
{"drm-vrr-enabled", OPT_CHOICE(drm_vrr_enabled,
|
|
|
|
{"no", 0}, {"yes", 1}, {"auto", -1})},
|
2017-10-23 06:59:58 +00:00
|
|
|
{0},
|
|
|
|
},
|
2018-04-29 15:46:23 +00:00
|
|
|
.defaults = &(const struct drm_opts) {
|
2018-06-05 07:15:54 +00:00
|
|
|
.drm_mode_spec = "preferred",
|
2018-06-28 13:23:19 +00:00
|
|
|
.drm_atomic = 1,
|
2018-12-01 11:01:17 +00:00
|
|
|
.drm_draw_plane = DRM_OPTS_PRIMARY_PLANE,
|
|
|
|
.drm_drmprime_video_plane = DRM_OPTS_OVERLAY_PLANE,
|
2022-04-04 03:08:33 +00:00
|
|
|
.drm_vrr_enabled = 0,
|
2018-04-29 15:46:23 +00:00
|
|
|
},
|
2017-10-23 06:59:58 +00:00
|
|
|
.size = sizeof(struct drm_opts),
|
|
|
|
};
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
static const char *connector_names[] = {
|
|
|
|
"Unknown", // DRM_MODE_CONNECTOR_Unknown
|
|
|
|
"VGA", // DRM_MODE_CONNECTOR_VGA
|
|
|
|
"DVI-I", // DRM_MODE_CONNECTOR_DVII
|
|
|
|
"DVI-D", // DRM_MODE_CONNECTOR_DVID
|
|
|
|
"DVI-A", // DRM_MODE_CONNECTOR_DVIA
|
|
|
|
"Composite", // DRM_MODE_CONNECTOR_Composite
|
|
|
|
"SVIDEO", // DRM_MODE_CONNECTOR_SVIDEO
|
|
|
|
"LVDS", // DRM_MODE_CONNECTOR_LVDS
|
|
|
|
"Component", // DRM_MODE_CONNECTOR_Component
|
|
|
|
"DIN", // DRM_MODE_CONNECTOR_9PinDIN
|
|
|
|
"DP", // DRM_MODE_CONNECTOR_DisplayPort
|
|
|
|
"HDMI-A", // DRM_MODE_CONNECTOR_HDMIA
|
|
|
|
"HDMI-B", // DRM_MODE_CONNECTOR_HDMIB
|
|
|
|
"TV", // DRM_MODE_CONNECTOR_TV
|
|
|
|
"eDP", // DRM_MODE_CONNECTOR_eDP
|
|
|
|
"Virtual", // DRM_MODE_CONNECTOR_VIRTUAL
|
|
|
|
"DSI", // DRM_MODE_CONNECTOR_DSI
|
|
|
|
"DPI", // DRM_MODE_CONNECTOR_DPI
|
2021-03-14 18:41:48 +00:00
|
|
|
"Writeback", // DRM_MODE_CONNECTOR_WRITEBACK
|
|
|
|
"SPI", // DRM_MODE_CONNECTOR_SPI
|
|
|
|
"USB", // DRM_MODE_CONNECTOR_USB
|
2016-10-04 20:07:19 +00:00
|
|
|
};
|
|
|
|
|
2018-06-05 07:15:54 +00:00
|
|
|
struct drm_mode_spec {
|
|
|
|
enum {
|
|
|
|
DRM_MODE_SPEC_BY_IDX, // Specified by idx
|
|
|
|
DRM_MODE_SPEC_BY_NUMBERS, // Specified by width, height and opt. refresh
|
|
|
|
DRM_MODE_SPEC_PREFERRED, // Select the preferred mode of the display
|
|
|
|
DRM_MODE_SPEC_HIGHEST, // Select the mode with the highest resolution
|
|
|
|
} type;
|
|
|
|
unsigned int idx;
|
|
|
|
unsigned int width;
|
|
|
|
unsigned int height;
|
|
|
|
double refresh;
|
|
|
|
};
|
|
|
|
|
2015-11-07 12:41:40 +00:00
|
|
|
// KMS ------------------------------------------------------------------------
|
|
|
|
|
2018-06-02 10:53:58 +00:00
|
|
|
static void get_connector_name(const drmModeConnector *connector,
|
|
|
|
char ret[MAX_CONNECTOR_NAME_LEN])
|
2015-11-07 12:41:40 +00:00
|
|
|
{
|
2021-03-14 18:36:10 +00:00
|
|
|
const char *type_name;
|
|
|
|
|
|
|
|
if (connector->connector_type < MP_ARRAY_SIZE(connector_names)) {
|
|
|
|
type_name = connector_names[connector->connector_type];
|
|
|
|
} else {
|
|
|
|
type_name = "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(ret, MAX_CONNECTOR_NAME_LEN, "%s-%d", type_name,
|
2016-10-04 20:07:19 +00:00
|
|
|
connector->connector_type_id);
|
|
|
|
}
|
2015-11-07 12:41:40 +00:00
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
// Gets the first connector whose name matches the input parameter.
|
|
|
|
// The returned connector may be disconnected.
|
|
|
|
// Result must be freed with drmModeFreeConnector.
|
|
|
|
static drmModeConnector *get_connector_by_name(const struct kms *kms,
|
|
|
|
const drmModeRes *res,
|
|
|
|
const char *connector_name)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < res->count_connectors; i++) {
|
|
|
|
drmModeConnector *connector
|
|
|
|
= drmModeGetConnector(kms->fd, res->connectors[i]);
|
|
|
|
if (!connector)
|
|
|
|
continue;
|
|
|
|
char other_connector_name[MAX_CONNECTOR_NAME_LEN];
|
|
|
|
get_connector_name(connector, other_connector_name);
|
|
|
|
if (!strcmp(connector_name, other_connector_name))
|
|
|
|
return connector;
|
|
|
|
drmModeFreeConnector(connector);
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
2016-10-04 20:07:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-07 12:41:40 +00:00
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
// Gets the first connected connector.
|
|
|
|
// Result must be freed with drmModeFreeConnector.
|
|
|
|
static drmModeConnector *get_first_connected_connector(const struct kms *kms,
|
|
|
|
const drmModeRes *res)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < res->count_connectors; i++) {
|
|
|
|
drmModeConnector *connector
|
|
|
|
= drmModeGetConnector(kms->fd, res->connectors[i]);
|
|
|
|
if (!connector)
|
|
|
|
continue;
|
|
|
|
if (connector->connection == DRM_MODE_CONNECTED
|
|
|
|
&& connector->count_modes > 0) {
|
|
|
|
return connector;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
2016-10-04 20:07:19 +00:00
|
|
|
drmModeFreeConnector(connector);
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
2016-10-04 20:07:19 +00:00
|
|
|
return NULL;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
static bool setup_connector(struct kms *kms, const drmModeRes *res,
|
|
|
|
const char *connector_name)
|
2015-11-07 12:41:40 +00:00
|
|
|
{
|
2016-10-04 20:07:19 +00:00
|
|
|
drmModeConnector *connector;
|
|
|
|
|
|
|
|
if (connector_name
|
|
|
|
&& strcmp(connector_name, "")
|
|
|
|
&& strcmp(connector_name, "auto")) {
|
|
|
|
connector = get_connector_by_name(kms, res, connector_name);
|
|
|
|
if (!connector) {
|
|
|
|
MP_ERR(kms, "No connector with name %s found\n", connector_name);
|
2021-01-18 21:28:28 +00:00
|
|
|
kms_show_available_connectors(kms->log, kms->card_no,
|
|
|
|
kms->primary_node_path);
|
2016-10-04 20:07:19 +00:00
|
|
|
return false;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
2016-10-04 20:07:19 +00:00
|
|
|
} else {
|
|
|
|
connector = get_first_connected_connector(kms, res);
|
|
|
|
if (!connector) {
|
2015-11-07 12:41:40 +00:00
|
|
|
MP_ERR(kms, "No connected connectors found\n");
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
if (connector->connection != DRM_MODE_CONNECTED) {
|
|
|
|
drmModeFreeConnector(connector);
|
|
|
|
MP_ERR(kms, "Chosen connector is disconnected\n");
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
if (connector->count_modes == 0) {
|
|
|
|
drmModeFreeConnector(connector);
|
|
|
|
MP_ERR(kms, "Chosen connector has no valid modes\n");
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2016-10-04 20:07:19 +00:00
|
|
|
}
|
2015-11-07 12:41:40 +00:00
|
|
|
|
|
|
|
kms->connector = connector;
|
2015-11-07 20:11:04 +00:00
|
|
|
return true;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2015-11-07 20:11:04 +00:00
|
|
|
static bool setup_crtc(struct kms *kms, const drmModeRes *res)
|
2015-11-07 12:41:40 +00:00
|
|
|
{
|
2018-04-21 13:16:47 +00:00
|
|
|
// First try to find currently connected encoder and its current CRTC
|
|
|
|
for (unsigned int i = 0; i < res->count_encoders; i++) {
|
|
|
|
drmModeEncoder *encoder = drmModeGetEncoder(kms->fd, res->encoders[i]);
|
|
|
|
if (!encoder) {
|
|
|
|
MP_WARN(kms, "Cannot retrieve encoder %u:%u: %s\n",
|
|
|
|
i, res->encoders[i], mp_strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder->encoder_id == kms->connector->encoder_id && encoder->crtc_id != 0) {
|
|
|
|
MP_VERBOSE(kms, "Connector %u currently connected to encoder %u\n",
|
|
|
|
kms->connector->connector_id, kms->connector->encoder_id);
|
|
|
|
kms->encoder = encoder;
|
|
|
|
kms->crtc_id = encoder->crtc_id;
|
|
|
|
goto success;
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeEncoder(encoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise pick first legal encoder and CRTC combo for the connector
|
2015-11-07 12:41:40 +00:00
|
|
|
for (unsigned int i = 0; i < kms->connector->count_encoders; ++i) {
|
|
|
|
drmModeEncoder *encoder
|
|
|
|
= drmModeGetEncoder(kms->fd, kms->connector->encoders[i]);
|
|
|
|
if (!encoder) {
|
|
|
|
MP_WARN(kms, "Cannot retrieve encoder %u:%u: %s\n",
|
|
|
|
i, kms->connector->encoders[i], mp_strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate all global CRTCs
|
|
|
|
for (unsigned int j = 0; j < res->count_crtcs; ++j) {
|
|
|
|
// check whether this CRTC works with the encoder
|
|
|
|
if (!(encoder->possible_crtcs & (1 << j)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
kms->encoder = encoder;
|
2016-01-18 21:33:52 +00:00
|
|
|
kms->crtc_id = res->crtcs[j];
|
2018-04-21 13:16:47 +00:00
|
|
|
goto success;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drmModeFreeEncoder(encoder);
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
MP_ERR(kms, "Connector %u has no suitable CRTC\n",
|
2015-11-07 12:41:40 +00:00
|
|
|
kms->connector->connector_id);
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2018-04-21 13:16:47 +00:00
|
|
|
|
|
|
|
success:
|
|
|
|
MP_VERBOSE(kms, "Selected Encoder %u with CRTC %u\n",
|
|
|
|
kms->encoder->encoder_id, kms->crtc_id);
|
|
|
|
return true;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 07:15:54 +00:00
|
|
|
static bool all_digits(const char *str)
|
|
|
|
{
|
|
|
|
if (str == NULL || str[0] == '\0') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const char *c = str; *c != '\0'; ++c) {
|
|
|
|
if (!mp_isdigit(*c))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool parse_mode_spec(const char *spec, struct drm_mode_spec *parse_result)
|
2015-11-07 12:41:40 +00:00
|
|
|
{
|
2018-06-05 07:15:54 +00:00
|
|
|
if (spec == NULL || spec[0] == '\0' || strcmp(spec, "preferred") == 0) {
|
|
|
|
if (parse_result) {
|
|
|
|
*parse_result =
|
|
|
|
(struct drm_mode_spec) { .type = DRM_MODE_SPEC_PREFERRED };
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(spec, "highest") == 0) {
|
|
|
|
if (parse_result) {
|
|
|
|
*parse_result =
|
|
|
|
(struct drm_mode_spec) { .type = DRM_MODE_SPEC_HIGHEST };
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-02 10:53:58 +00:00
|
|
|
|
2018-06-05 07:15:54 +00:00
|
|
|
// If the string is made up of only digits, it means that it is an index number
|
|
|
|
if (all_digits(spec)) {
|
|
|
|
if (parse_result) {
|
|
|
|
*parse_result = (struct drm_mode_spec) {
|
|
|
|
.type = DRM_MODE_SPEC_BY_IDX,
|
|
|
|
.idx = strtoul(spec, NULL, 10),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mp_isdigit(spec[0]))
|
|
|
|
return false;
|
|
|
|
char *height_part, *refresh_part;
|
|
|
|
const unsigned int width = strtoul(spec, &height_part, 10);
|
|
|
|
if (spec == height_part || height_part[0] == '\0' || height_part[0] != 'x')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
height_part += 1;
|
|
|
|
if (!mp_isdigit(height_part[0]))
|
|
|
|
return false;
|
|
|
|
const unsigned int height = strtoul(height_part, &refresh_part, 10);
|
|
|
|
if (height_part == refresh_part)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char *rest = NULL;
|
|
|
|
double refresh;
|
|
|
|
switch (refresh_part[0]) {
|
|
|
|
case '\0':
|
|
|
|
refresh = nan("");
|
|
|
|
break;
|
|
|
|
case '@':
|
|
|
|
refresh_part += 1;
|
|
|
|
if (!(mp_isdigit(refresh_part[0]) || refresh_part[0] == '.'))
|
|
|
|
return false;
|
|
|
|
refresh = strtod(refresh_part, &rest);
|
|
|
|
if (refresh_part == rest || rest[0] != '\0' || refresh < 0.0)
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parse_result) {
|
|
|
|
*parse_result = (struct drm_mode_spec) {
|
|
|
|
.type = DRM_MODE_SPEC_BY_NUMBERS,
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
.refresh = refresh,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_mode_by_idx(struct kms *kms, unsigned int mode_idx)
|
|
|
|
{
|
|
|
|
if (mode_idx >= kms->connector->count_modes) {
|
|
|
|
MP_ERR(kms, "Bad mode index (max = %d).\n",
|
2016-10-04 20:07:19 +00:00
|
|
|
kms->connector->count_modes - 1);
|
2018-06-05 07:15:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-04 20:07:19 +00:00
|
|
|
|
2018-06-05 07:15:54 +00:00
|
|
|
kms->mode.mode = kms->connector->modes[mode_idx];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mode_match(const drmModeModeInfo *mode,
|
|
|
|
unsigned int width,
|
|
|
|
unsigned int height,
|
|
|
|
double refresh)
|
|
|
|
{
|
|
|
|
if (isnan(refresh)) {
|
|
|
|
return
|
|
|
|
(mode->hdisplay == width) &&
|
|
|
|
(mode->vdisplay == height);
|
|
|
|
} else {
|
|
|
|
const double mode_refresh = mode_get_Hz(mode);
|
|
|
|
return
|
|
|
|
(mode->hdisplay == width) &&
|
|
|
|
(mode->vdisplay == height) &&
|
|
|
|
((int)round(refresh*100) == (int)round(mode_refresh*100));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_mode_by_numbers(struct kms *kms,
|
|
|
|
unsigned int width,
|
|
|
|
unsigned int height,
|
|
|
|
double refresh,
|
|
|
|
const char *mode_spec)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < kms->connector->count_modes; ++i) {
|
|
|
|
drmModeModeInfo *current_mode = &kms->connector->modes[i];
|
|
|
|
if (mode_match(current_mode, width, height, refresh)) {
|
|
|
|
kms->mode.mode = *current_mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_ERR(kms, "Could not find mode matching %s\n", mode_spec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_mode_preferred(struct kms *kms)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < kms->connector->count_modes; ++i) {
|
|
|
|
drmModeModeInfo *current_mode = &kms->connector->modes[i];
|
|
|
|
if (current_mode->type & DRM_MODE_TYPE_PREFERRED) {
|
|
|
|
kms->mode.mode = *current_mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to first mode
|
|
|
|
MP_WARN(kms, "Could not find any preferred mode. Picking the first mode.\n");
|
|
|
|
kms->mode.mode = kms->connector->modes[0];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_mode_highest(struct kms *kms)
|
|
|
|
{
|
|
|
|
unsigned int area = 0;
|
|
|
|
drmModeModeInfo *highest_resolution_mode = &kms->connector->modes[0];
|
|
|
|
for (unsigned int i = 0; i < kms->connector->count_modes; ++i) {
|
|
|
|
drmModeModeInfo *current_mode = &kms->connector->modes[i];
|
|
|
|
|
|
|
|
const unsigned int current_area =
|
|
|
|
current_mode->hdisplay * current_mode->vdisplay;
|
|
|
|
if (current_area > area) {
|
|
|
|
highest_resolution_mode = current_mode;
|
|
|
|
area = current_area;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
kms->mode.mode = *highest_resolution_mode;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool setup_mode(struct kms *kms, const char *mode_spec)
|
|
|
|
{
|
|
|
|
if (kms->connector->count_modes <= 0) {
|
|
|
|
MP_ERR(kms, "No available modes\n");
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2018-06-05 07:15:54 +00:00
|
|
|
struct drm_mode_spec parsed;
|
|
|
|
if (!parse_mode_spec(mode_spec, &parsed)) {
|
|
|
|
MP_ERR(kms, "Parse error\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parsed.type) {
|
|
|
|
case DRM_MODE_SPEC_BY_IDX:
|
|
|
|
if (!setup_mode_by_idx(kms, parsed.idx))
|
|
|
|
goto err;
|
|
|
|
break;
|
|
|
|
case DRM_MODE_SPEC_BY_NUMBERS:
|
|
|
|
if (!setup_mode_by_numbers(kms, parsed.width, parsed.height, parsed.refresh,
|
|
|
|
mode_spec))
|
|
|
|
goto err;
|
|
|
|
break;
|
|
|
|
case DRM_MODE_SPEC_PREFERRED:
|
|
|
|
if (!setup_mode_preferred(kms))
|
|
|
|
goto err;
|
|
|
|
break;
|
|
|
|
case DRM_MODE_SPEC_HIGHEST:
|
|
|
|
if (!setup_mode_highest(kms))
|
|
|
|
goto err;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MP_ERR(kms, "setup_mode: Internal error\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2019-12-06 15:24:56 +00:00
|
|
|
drmModeModeInfo *mode = &kms->mode.mode;
|
|
|
|
MP_VERBOSE(kms, "Selected mode: %s (%dx%d@%.2fHz)\n",
|
|
|
|
mode->name, mode->hdisplay, mode->vdisplay, mode_get_Hz(mode));
|
|
|
|
|
2015-11-07 20:11:04 +00:00
|
|
|
return true;
|
2018-06-05 07:15:54 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
MP_INFO(kms, "Available modes:\n");
|
|
|
|
kms_show_available_modes(kms->log, kms->connector);
|
|
|
|
return false;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 21:28:28 +00:00
|
|
|
static int open_card_path(const char *path)
|
2016-10-04 20:07:19 +00:00
|
|
|
{
|
2021-01-18 21:28:28 +00:00
|
|
|
return open(path, O_RDWR | O_CLOEXEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *get_primary_device_path(struct mp_log *log, int *card_no)
|
|
|
|
{
|
|
|
|
drmDevice *devices[DRM_MAX_MINOR] = { 0 };
|
|
|
|
int card_count = drmGetDevices2(0, devices, MP_ARRAY_SIZE(devices));
|
|
|
|
char *device_path = NULL;
|
|
|
|
bool card_no_given = (*card_no >= 0);
|
|
|
|
|
|
|
|
if (card_count < 0) {
|
|
|
|
mp_err(log, "Listing DRM devices with drmGetDevices failed! (%s)\n",
|
|
|
|
mp_strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (card_no_given && *card_no > (card_count - 1)) {
|
|
|
|
mp_err(log, "Card number %d given too high! %d devices located.\n",
|
|
|
|
*card_no, card_count);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = card_no_given ? *card_no : 0; i < card_count; i++) {
|
|
|
|
drmDevice *dev = devices[i];
|
|
|
|
|
|
|
|
if (!(dev->available_nodes & (1 << DRM_NODE_PRIMARY))) {
|
|
|
|
if (card_no_given) {
|
|
|
|
mp_err(log,
|
|
|
|
"DRM card number %d given, yet it does not have "
|
|
|
|
"a primary node!\n", i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *primary_node_path = dev->nodes[DRM_NODE_PRIMARY];
|
|
|
|
|
|
|
|
mp_verbose(log, "Picked DRM card %d, primary node %s%s.\n",
|
|
|
|
i, primary_node_path,
|
|
|
|
card_no_given ? "" : " as the default");
|
|
|
|
|
|
|
|
device_path = talloc_strdup(log, primary_node_path);
|
|
|
|
*card_no = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!device_path)
|
|
|
|
mp_err(log, "No primary DRM device could be picked!\n");
|
|
|
|
|
|
|
|
err:
|
|
|
|
drmFreeDevices(devices, card_count);
|
|
|
|
|
|
|
|
return device_path;
|
2016-10-04 20:07:19 +00:00
|
|
|
}
|
2015-11-07 12:41:40 +00:00
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
static void parse_connector_spec(struct mp_log *log,
|
|
|
|
const char *connector_spec,
|
|
|
|
int *card_no, char **connector_name)
|
2015-11-07 12:41:40 +00:00
|
|
|
{
|
2016-10-04 20:07:19 +00:00
|
|
|
if (!connector_spec) {
|
2021-01-18 21:28:28 +00:00
|
|
|
*card_no = -1;
|
2016-10-04 20:07:19 +00:00
|
|
|
*connector_name = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char *dot_ptr = strchr(connector_spec, '.');
|
|
|
|
if (dot_ptr) {
|
2021-01-22 21:07:38 +00:00
|
|
|
mp_warn(log, "Warning: Selecting a connector by index with drm-connector "
|
|
|
|
"is deprecated. Use the drm-device option instead.\n");
|
2016-10-04 20:07:19 +00:00
|
|
|
*card_no = atoi(connector_spec);
|
|
|
|
*connector_name = talloc_strdup(log, dot_ptr + 1);
|
|
|
|
} else {
|
2021-01-18 21:28:28 +00:00
|
|
|
*card_no = -1;
|
2016-10-04 20:07:19 +00:00
|
|
|
*connector_name = talloc_strdup(log, connector_spec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:07:38 +00:00
|
|
|
struct kms *kms_create(struct mp_log *log,
|
|
|
|
const char *drm_device_path,
|
|
|
|
const char *connector_spec,
|
2018-06-02 10:53:58 +00:00
|
|
|
const char* mode_spec,
|
|
|
|
int draw_plane, int drmprime_video_plane,
|
2018-06-28 13:23:19 +00:00
|
|
|
bool use_atomic)
|
2016-10-04 20:07:19 +00:00
|
|
|
{
|
|
|
|
int card_no = -1;
|
|
|
|
char *connector_name = NULL;
|
2021-01-22 21:07:38 +00:00
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
parse_connector_spec(log, connector_spec, &card_no, &connector_name);
|
2021-01-22 21:07:38 +00:00
|
|
|
if (drm_device_path && card_no != -1)
|
|
|
|
mp_warn(log, "Both DRM device and card number (as part of "
|
|
|
|
"drm-connector) are set! Will prefer given device path "
|
|
|
|
"'%s'!\n",
|
|
|
|
drm_device_path);
|
|
|
|
|
|
|
|
char *primary_node_path = drm_device_path ?
|
|
|
|
talloc_strdup(log, drm_device_path) :
|
|
|
|
get_primary_device_path(log, &card_no);
|
2021-01-18 21:28:28 +00:00
|
|
|
|
|
|
|
if (!primary_node_path) {
|
|
|
|
mp_err(log,
|
|
|
|
"Failed to find a usable DRM primary node!\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-10-04 20:07:19 +00:00
|
|
|
|
|
|
|
struct kms *kms = talloc(NULL, struct kms);
|
|
|
|
*kms = (struct kms) {
|
|
|
|
.log = mp_log_new(kms, log, "kms"),
|
2021-01-18 21:28:28 +00:00
|
|
|
.primary_node_path = primary_node_path,
|
|
|
|
.fd = open_card_path(primary_node_path),
|
2015-11-07 12:41:40 +00:00
|
|
|
.connector = NULL,
|
|
|
|
.encoder = NULL,
|
2018-06-02 10:53:54 +00:00
|
|
|
.mode = {{0}},
|
2015-11-07 12:41:40 +00:00
|
|
|
.crtc_id = -1,
|
2016-10-04 20:07:19 +00:00
|
|
|
.card_no = card_no,
|
2015-11-07 12:41:40 +00:00
|
|
|
};
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
drmModeRes *res = NULL;
|
|
|
|
|
2015-11-07 12:41:40 +00:00
|
|
|
if (kms->fd < 0) {
|
2016-10-04 20:07:19 +00:00
|
|
|
mp_err(log, "Cannot open card \"%d\": %s.\n",
|
|
|
|
card_no, mp_strerror(errno));
|
|
|
|
goto err;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 15:24:56 +00:00
|
|
|
drmVersionPtr ver = drmGetVersion(kms->fd);
|
|
|
|
if (ver) {
|
|
|
|
mp_verbose(log, "Driver: %s %d.%d.%d (%s)\n", ver->name,
|
|
|
|
ver->version_major, ver->version_minor, ver->version_patchlevel,
|
|
|
|
ver->date);
|
|
|
|
drmFreeVersion(ver);
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
res = drmModeGetResources(kms->fd);
|
2015-11-07 12:41:40 +00:00
|
|
|
if (!res) {
|
2016-10-04 20:07:19 +00:00
|
|
|
mp_err(log, "Cannot retrieve DRM resources: %s\n", mp_strerror(errno));
|
|
|
|
goto err;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
if (!setup_connector(kms, res, connector_name))
|
2016-10-03 22:20:07 +00:00
|
|
|
goto err;
|
2015-11-07 20:11:04 +00:00
|
|
|
if (!setup_crtc(kms, res))
|
2016-10-03 22:20:07 +00:00
|
|
|
goto err;
|
2018-06-02 10:53:58 +00:00
|
|
|
if (!setup_mode(kms, mode_spec))
|
2016-10-03 22:20:07 +00:00
|
|
|
goto err;
|
2015-11-07 12:41:40 +00:00
|
|
|
|
2017-10-23 15:51:49 +00:00
|
|
|
// Universal planes allows accessing all the planes (including primary)
|
|
|
|
if (drmSetClientCap(kms->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) {
|
|
|
|
mp_err(log, "Failed to set Universal planes capability\n");
|
|
|
|
}
|
|
|
|
|
2018-06-28 13:23:19 +00:00
|
|
|
if (!use_atomic) {
|
|
|
|
mp_verbose(log, "Using Legacy Modesetting\n");
|
|
|
|
} else if (drmSetClientCap(kms->fd, DRM_CLIENT_CAP_ATOMIC, 1)) {
|
|
|
|
mp_verbose(log, "No DRM Atomic support found. Falling back to legacy modesetting\n");
|
2017-10-23 15:51:49 +00:00
|
|
|
} else {
|
|
|
|
mp_verbose(log, "DRM Atomic support found\n");
|
2018-03-17 07:21:56 +00:00
|
|
|
kms->atomic_context = drm_atomic_create_context(kms->log, kms->fd, kms->crtc_id,
|
2018-12-01 11:01:17 +00:00
|
|
|
kms->connector->connector_id,
|
|
|
|
draw_plane, drmprime_video_plane);
|
2017-10-23 15:51:49 +00:00
|
|
|
if (!kms->atomic_context) {
|
|
|
|
mp_err(log, "Failed to create DRM atomic context\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:20:07 +00:00
|
|
|
drmModeFreeResources(res);
|
2016-10-04 20:07:19 +00:00
|
|
|
return kms;
|
2016-10-03 22:20:07 +00:00
|
|
|
|
|
|
|
err:
|
2016-10-04 20:07:19 +00:00
|
|
|
if (res)
|
|
|
|
drmModeFreeResources(res);
|
|
|
|
if (connector_name)
|
|
|
|
talloc_free(connector_name);
|
2021-01-18 21:28:28 +00:00
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
kms_destroy(kms);
|
|
|
|
return NULL;
|
2015-11-07 12:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void kms_destroy(struct kms *kms)
|
|
|
|
{
|
|
|
|
if (!kms)
|
|
|
|
return;
|
2018-06-02 10:53:54 +00:00
|
|
|
drm_mode_destroy_blob(kms->fd, &kms->mode);
|
2015-11-07 12:41:40 +00:00
|
|
|
if (kms->connector) {
|
|
|
|
drmModeFreeConnector(kms->connector);
|
|
|
|
kms->connector = NULL;
|
|
|
|
}
|
|
|
|
if (kms->encoder) {
|
|
|
|
drmModeFreeEncoder(kms->encoder);
|
|
|
|
kms->encoder = NULL;
|
|
|
|
}
|
2017-10-23 15:51:49 +00:00
|
|
|
if (kms->atomic_context) {
|
|
|
|
drm_atomic_destroy_context(kms->atomic_context);
|
|
|
|
}
|
|
|
|
|
2015-11-07 12:41:40 +00:00
|
|
|
close(kms->fd);
|
|
|
|
talloc_free(kms);
|
|
|
|
}
|
|
|
|
|
2017-05-26 16:23:14 +00:00
|
|
|
static double mode_get_Hz(const drmModeModeInfo *mode)
|
|
|
|
{
|
2019-12-06 16:08:07 +00:00
|
|
|
double rate = mode->clock * 1000.0 / mode->htotal / mode->vtotal;
|
|
|
|
if (mode->flags & DRM_MODE_FLAG_INTERLACE)
|
|
|
|
rate *= 2.0;
|
|
|
|
return rate;
|
2017-05-26 16:23:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 10:54:01 +00:00
|
|
|
static void kms_show_available_modes(
|
2016-10-04 20:07:19 +00:00
|
|
|
struct mp_log *log, const drmModeConnector *connector)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < connector->count_modes; i++) {
|
2018-06-02 10:53:58 +00:00
|
|
|
mp_info(log, " Mode %d: %s (%dx%d@%.2fHz)\n", i,
|
2016-10-04 20:07:19 +00:00
|
|
|
connector->modes[i].name,
|
|
|
|
connector->modes[i].hdisplay,
|
2017-05-26 16:23:14 +00:00
|
|
|
connector->modes[i].vdisplay,
|
|
|
|
mode_get_Hz(&connector->modes[i]));
|
2016-10-04 20:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:53:58 +00:00
|
|
|
static void kms_show_foreach_connector(struct mp_log *log, int card_no,
|
2021-01-18 21:28:28 +00:00
|
|
|
const char *card_path,
|
2018-06-02 10:53:58 +00:00
|
|
|
void (*show_fn)(struct mp_log*, int,
|
|
|
|
const drmModeConnector*))
|
2016-10-04 20:07:19 +00:00
|
|
|
{
|
2021-01-18 21:28:28 +00:00
|
|
|
int fd = open_card_path(card_path);
|
2016-10-04 20:07:19 +00:00
|
|
|
if (fd < 0) {
|
2021-01-18 21:28:28 +00:00
|
|
|
mp_err(log, "Failed to open card %d (%s)\n", card_no, card_path);
|
2016-10-04 20:07:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
drmModeRes *res = drmModeGetResources(fd);
|
|
|
|
if (!res) {
|
|
|
|
mp_err(log, "Cannot retrieve DRM resources: %s\n", mp_strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < res->count_connectors; i++) {
|
|
|
|
drmModeConnector *connector
|
|
|
|
= drmModeGetConnector(fd, res->connectors[i]);
|
|
|
|
if (!connector)
|
|
|
|
continue;
|
2018-06-02 10:53:58 +00:00
|
|
|
show_fn(log, card_no, connector);
|
2016-10-04 20:07:19 +00:00
|
|
|
drmModeFreeConnector(connector);
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
|
|
|
if (res)
|
|
|
|
drmModeFreeResources(res);
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:53:58 +00:00
|
|
|
static void kms_show_connector_name_and_state_callback(
|
|
|
|
struct mp_log *log, int card_no, const drmModeConnector *connector)
|
|
|
|
{
|
|
|
|
char other_connector_name[MAX_CONNECTOR_NAME_LEN];
|
|
|
|
get_connector_name(connector, other_connector_name);
|
|
|
|
const char *connection_str =
|
|
|
|
(connector->connection == DRM_MODE_CONNECTED) ? "connected" : "disconnected";
|
|
|
|
mp_info(log, " %s (%s)\n", other_connector_name, connection_str);
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:28:28 +00:00
|
|
|
static void kms_show_available_connectors(struct mp_log *log, int card_no,
|
|
|
|
const char *card_path)
|
2018-06-02 10:53:58 +00:00
|
|
|
{
|
2021-01-18 21:28:28 +00:00
|
|
|
mp_info(log, "Available connectors for card %d (%s):\n", card_no,
|
|
|
|
card_path);
|
2018-06-02 10:53:58 +00:00
|
|
|
kms_show_foreach_connector(
|
2021-01-18 21:28:28 +00:00
|
|
|
log, card_no, card_path, kms_show_connector_name_and_state_callback);
|
2018-06-02 10:53:58 +00:00
|
|
|
mp_info(log, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kms_show_connector_modes_callback(struct mp_log *log, int card_no,
|
|
|
|
const drmModeConnector *connector)
|
|
|
|
{
|
|
|
|
if (connector->connection != DRM_MODE_CONNECTED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char other_connector_name[MAX_CONNECTOR_NAME_LEN];
|
|
|
|
get_connector_name(connector, other_connector_name);
|
|
|
|
mp_info(log, "Available modes for drm-connector=%d.%s\n",
|
|
|
|
card_no, other_connector_name);
|
|
|
|
kms_show_available_modes(log, connector);
|
|
|
|
mp_info(log, "\n");
|
|
|
|
}
|
|
|
|
|
2021-01-18 21:28:28 +00:00
|
|
|
static void kms_show_available_connectors_and_modes(struct mp_log *log,
|
|
|
|
int card_no,
|
|
|
|
const char *card_path)
|
2018-06-02 10:53:58 +00:00
|
|
|
{
|
2021-01-18 21:28:28 +00:00
|
|
|
kms_show_foreach_connector(log, card_no, card_path,
|
|
|
|
kms_show_connector_modes_callback);
|
2018-06-02 10:53:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void kms_show_foreach_card(
|
2021-01-18 21:28:28 +00:00
|
|
|
struct mp_log *log, void (*show_fn)(struct mp_log*,int,const char *))
|
2016-10-04 20:07:19 +00:00
|
|
|
{
|
2021-01-18 21:28:28 +00:00
|
|
|
drmDevice *devices[DRM_MAX_MINOR] = { 0 };
|
|
|
|
int card_count = drmGetDevices2(0, devices, MP_ARRAY_SIZE(devices));
|
|
|
|
if (card_count < 0) {
|
|
|
|
mp_err(log, "Listing DRM devices with drmGetDevices failed! (%s)\n",
|
|
|
|
mp_strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < card_count; i++) {
|
|
|
|
drmDevice *dev = devices[i];
|
|
|
|
|
|
|
|
if (!(dev->available_nodes & (1 << DRM_NODE_PRIMARY)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const char *primary_node_path = dev->nodes[DRM_NODE_PRIMARY];
|
|
|
|
|
|
|
|
int fd = open_card_path(primary_node_path);
|
|
|
|
if (fd < 0) {
|
|
|
|
mp_err(log, "Failed to open primary DRM node path %s!\n",
|
|
|
|
primary_node_path);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-04 20:07:19 +00:00
|
|
|
close(fd);
|
2021-01-18 21:28:28 +00:00
|
|
|
show_fn(log, i, primary_node_path);
|
2016-10-04 20:07:19 +00:00
|
|
|
}
|
2021-01-18 21:28:28 +00:00
|
|
|
|
|
|
|
drmFreeDevices(devices, card_count);
|
2016-10-04 20:07:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 10:54:01 +00:00
|
|
|
static void kms_show_available_cards_and_connectors(struct mp_log *log)
|
2018-06-02 10:53:58 +00:00
|
|
|
{
|
|
|
|
kms_show_foreach_card(log, kms_show_available_connectors);
|
|
|
|
}
|
|
|
|
|
2018-06-02 10:54:01 +00:00
|
|
|
static void kms_show_available_cards_connectors_and_modes(struct mp_log *log)
|
2018-06-02 10:53:58 +00:00
|
|
|
{
|
|
|
|
kms_show_foreach_card(log, kms_show_available_connectors_and_modes);
|
|
|
|
}
|
|
|
|
|
2016-10-03 21:56:35 +00:00
|
|
|
double kms_get_display_fps(const struct kms *kms)
|
|
|
|
{
|
2018-06-02 10:53:54 +00:00
|
|
|
return mode_get_Hz(&kms->mode.mode);
|
2016-10-03 21:56:35 +00:00
|
|
|
}
|
|
|
|
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
static int drm_connector_opt_help(struct mp_log *log, const struct m_option *opt,
|
|
|
|
struct bstr name)
|
2016-10-04 20:07:19 +00:00
|
|
|
{
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
kms_show_available_cards_and_connectors(log);
|
|
|
|
return M_OPT_EXIT;
|
2016-10-04 20:07:19 +00:00
|
|
|
}
|
|
|
|
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
static int drm_mode_opt_help(struct mp_log *log, const struct m_option *opt,
|
|
|
|
struct bstr name)
|
2018-06-02 10:53:58 +00:00
|
|
|
{
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
kms_show_available_cards_connectors_and_modes(log);
|
|
|
|
return M_OPT_EXIT;
|
|
|
|
}
|
2018-06-05 07:15:54 +00:00
|
|
|
|
options: Make validation and help possible for all option types
Today, validation is only possible for string type options. But there's
no particular reason why it needs to be restricted in this way, and
there are potential uses, to allow other options to be validated
without forcing the option to have to reimplement parsing from
scratch.
The first part, simply making the validation function an explicit
field instead of overloading priv is simple enough. But if we only do
that, then the validation function still needs to deal with the raw
pre-parsed string. Instead, we want to allow the value to be parsed
before it is validated. That in turn leads to us having validator
functions that should be type aware. Unfortunately, that means we need
to keep the explicit macro like OPT_STRING_VALIDATE() as a way to
enforce the correct typing of the function. Otherwise, we'd have to
have the validator take a void * and hope the implementation can cast
it correctly.
For help, we don't have this problem, as help doesn't look at the
value.
Then, we turn validators that are really help generators into explicit
help functions and where a validator is help + validation, we split
them into two parts.
I have, however, left functions that need to query information for both
help and validation as single functions to avoid code duplication.
In this change, I have not added an other OPT_FOO_VALIDATE() macros as
they are not needed, but I will add some in a separate change to
illustrate the pattern.
2021-02-21 00:41:44 +00:00
|
|
|
static int drm_validate_mode_opt(struct mp_log *log, const struct m_option *opt,
|
|
|
|
struct bstr name, const char **value)
|
|
|
|
{
|
|
|
|
const char *param = *value;
|
|
|
|
if (!parse_mode_spec(param, NULL)) {
|
2018-06-05 07:15:54 +00:00
|
|
|
mp_fatal(log, "Invalid value for option drm-mode. Must be a positive number, a string of the format WxH[@R] or 'help'\n");
|
|
|
|
return M_OPT_INVALID;
|
2018-06-02 10:53:58 +00:00
|
|
|
}
|
2018-06-05 07:15:54 +00:00
|
|
|
|
2018-06-02 10:53:58 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-11-07 12:41:40 +00:00
|
|
|
|
|
|
|
// VT switcher ----------------------------------------------------------------
|
|
|
|
|
2015-04-19 07:39:58 +00:00
|
|
|
static void vt_switcher_sighandler(int sig)
|
|
|
|
{
|
2015-10-30 17:55:48 +00:00
|
|
|
unsigned char event = sig == RELEASE_SIGNAL ? EVT_RELEASE : EVT_ACQUIRE;
|
2016-06-07 11:39:43 +00:00
|
|
|
(void)write(vt_switcher_pipe[1], &event, sizeof(event));
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 17:55:48 +00:00
|
|
|
static bool has_signal_installed(int signo)
|
|
|
|
{
|
|
|
|
struct sigaction act = { 0 };
|
|
|
|
sigaction(signo, 0, &act);
|
|
|
|
return act.sa_handler != 0;
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:10:11 +00:00
|
|
|
static int install_signal(int signo, void (*handler)(int))
|
|
|
|
{
|
|
|
|
struct sigaction act = { 0 };
|
|
|
|
act.sa_handler = handler;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = SA_RESTART;
|
|
|
|
return sigaction(signo, &act, NULL);
|
|
|
|
}
|
|
|
|
|
2015-11-07 20:11:04 +00:00
|
|
|
bool vt_switcher_init(struct vt_switcher *s, struct mp_log *log)
|
2015-04-19 07:39:58 +00:00
|
|
|
{
|
|
|
|
s->log = log;
|
|
|
|
s->tty_fd = -1;
|
|
|
|
vt_switcher_pipe[0] = -1;
|
|
|
|
vt_switcher_pipe[1] = -1;
|
|
|
|
|
|
|
|
if (mp_make_cloexec_pipe(vt_switcher_pipe)) {
|
2015-06-28 13:14:49 +00:00
|
|
|
MP_ERR(s, "Creating pipe failed: %s\n", mp_strerror(errno));
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s->tty_fd = open("/dev/tty", O_RDWR | O_CLOEXEC);
|
|
|
|
if (s->tty_fd < 0) {
|
2015-06-28 13:14:49 +00:00
|
|
|
MP_ERR(s, "Can't open TTY for VT control: %s\n", mp_strerror(errno));
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 17:55:48 +00:00
|
|
|
if (has_signal_installed(RELEASE_SIGNAL)) {
|
|
|
|
MP_ERR(s, "Can't handle VT release - signal already used\n");
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-10-30 17:55:48 +00:00
|
|
|
}
|
|
|
|
if (has_signal_installed(ACQUIRE_SIGNAL)) {
|
|
|
|
MP_ERR(s, "Can't handle VT acquire - signal already used\n");
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-10-30 17:55:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 18:10:11 +00:00
|
|
|
if (install_signal(RELEASE_SIGNAL, vt_switcher_sighandler)) {
|
|
|
|
MP_ERR(s, "Failed to install release signal: %s\n", mp_strerror(errno));
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-10-30 18:10:11 +00:00
|
|
|
}
|
|
|
|
if (install_signal(ACQUIRE_SIGNAL, vt_switcher_sighandler)) {
|
|
|
|
MP_ERR(s, "Failed to install acquire signal: %s\n", mp_strerror(errno));
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-10-30 18:10:11 +00:00
|
|
|
}
|
2015-04-19 07:39:58 +00:00
|
|
|
|
2019-09-24 19:46:52 +00:00
|
|
|
struct vt_mode vt_mode = { 0 };
|
2015-04-19 07:39:58 +00:00
|
|
|
if (ioctl(s->tty_fd, VT_GETMODE, &vt_mode) < 0) {
|
2015-06-28 13:14:49 +00:00
|
|
|
MP_ERR(s, "VT_GETMODE failed: %s\n", mp_strerror(errno));
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vt_mode.mode = VT_PROCESS;
|
2015-10-30 17:55:48 +00:00
|
|
|
vt_mode.relsig = RELEASE_SIGNAL;
|
|
|
|
vt_mode.acqsig = ACQUIRE_SIGNAL;
|
2019-09-21 20:54:24 +00:00
|
|
|
// frsig is a signal for forced release. Not implemented on Linux,
|
|
|
|
// Solaris, BSDs but must be set to a valid signal on some of those.
|
|
|
|
vt_mode.frsig = SIGIO; // unused
|
2015-04-19 07:39:58 +00:00
|
|
|
if (ioctl(s->tty_fd, VT_SETMODE, &vt_mode) < 0) {
|
2015-06-28 13:14:49 +00:00
|
|
|
MP_ERR(s, "VT_SETMODE failed: %s\n", mp_strerror(errno));
|
2015-11-07 20:11:04 +00:00
|
|
|
return false;
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
2018-02-24 23:57:47 +00:00
|
|
|
// Block the VT switching signals from interrupting the VO thread (they will
|
|
|
|
// still be picked up by other threads, which will fill vt_switcher_pipe for us)
|
|
|
|
sigset_t set;
|
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, RELEASE_SIGNAL);
|
|
|
|
sigaddset(&set, ACQUIRE_SIGNAL);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &set, NULL);
|
|
|
|
|
2015-11-07 20:11:04 +00:00
|
|
|
return true;
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vt_switcher_acquire(struct vt_switcher *s,
|
|
|
|
void (*handler)(void*), void *user_data)
|
|
|
|
{
|
|
|
|
s->handlers[HANDLER_ACQUIRE] = handler;
|
|
|
|
s->handler_data[HANDLER_ACQUIRE] = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vt_switcher_release(struct vt_switcher *s,
|
|
|
|
void (*handler)(void*), void *user_data)
|
|
|
|
{
|
|
|
|
s->handlers[HANDLER_RELEASE] = handler;
|
|
|
|
s->handler_data[HANDLER_RELEASE] = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vt_switcher_interrupt_poll(struct vt_switcher *s)
|
|
|
|
{
|
|
|
|
unsigned char event = EVT_INTERRUPT;
|
2016-06-07 11:39:43 +00:00
|
|
|
(void)write(vt_switcher_pipe[1], &event, sizeof(event));
|
2015-04-19 07:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void vt_switcher_destroy(struct vt_switcher *s)
|
|
|
|
{
|
2018-02-24 23:57:47 +00:00
|
|
|
struct vt_mode vt_mode = {0};
|
|
|
|
vt_mode.mode = VT_AUTO;
|
|
|
|
if (ioctl(s->tty_fd, VT_SETMODE, &vt_mode) < 0) {
|
|
|
|
MP_ERR(s, "VT_SETMODE failed: %s\n", mp_strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:11:04 +00:00
|
|
|
install_signal(RELEASE_SIGNAL, SIG_DFL);
|
|
|
|
install_signal(ACQUIRE_SIGNAL, SIG_DFL);
|
2015-04-19 07:39:58 +00:00
|
|
|
close(s->tty_fd);
|
|
|
|
close(vt_switcher_pipe[0]);
|
|
|
|
close(vt_switcher_pipe[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vt_switcher_poll(struct vt_switcher *s, int timeout_ms)
|
|
|
|
{
|
|
|
|
struct pollfd fds[1] = {
|
|
|
|
{ .events = POLLIN, .fd = vt_switcher_pipe[0] },
|
|
|
|
};
|
|
|
|
poll(fds, 1, timeout_ms);
|
2015-04-21 09:50:44 +00:00
|
|
|
if (!fds[0].revents)
|
2015-04-19 07:39:58 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned char event;
|
2015-04-21 09:50:44 +00:00
|
|
|
if (read(fds[0].fd, &event, sizeof(event)) != sizeof(event))
|
2015-04-19 07:39:58 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
switch (event) {
|
|
|
|
case EVT_RELEASE:
|
|
|
|
s->handlers[HANDLER_RELEASE](s->handler_data[HANDLER_RELEASE]);
|
|
|
|
|
|
|
|
if (ioctl(s->tty_fd, VT_RELDISP, 1) < 0) {
|
|
|
|
MP_ERR(s, "Failed to release virtual terminal\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_ACQUIRE:
|
|
|
|
s->handlers[HANDLER_ACQUIRE](s->handler_data[HANDLER_ACQUIRE]);
|
|
|
|
|
|
|
|
if (ioctl(s->tty_fd, VT_RELDISP, VT_ACKACQ) < 0) {
|
|
|
|
MP_ERR(s, "Failed to acquire virtual terminal\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_INTERRUPT:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 17:32:29 +00:00
|
|
|
|
|
|
|
void drm_pflip_cb(int fd, unsigned int msc, unsigned int sec,
|
|
|
|
unsigned int usec, void *data)
|
|
|
|
{
|
|
|
|
struct drm_pflip_cb_closure *closure = data;
|
|
|
|
|
|
|
|
struct drm_vsync_tuple *vsync = closure->vsync;
|
|
|
|
// frame_vsync->ust is the timestamp of the pageflip that happened just before this flip was queued
|
|
|
|
// frame_vsync->msc is the sequence number of the pageflip that happened just before this flip was queued
|
|
|
|
// frame_vsync->sbc is the sequence number for the frame that was just flipped to screen
|
|
|
|
struct drm_vsync_tuple *frame_vsync = closure->frame_vsync;
|
|
|
|
struct vo_vsync_info *vsync_info = closure->vsync_info;
|
|
|
|
|
|
|
|
const bool ready =
|
|
|
|
(vsync->msc != 0) &&
|
|
|
|
(frame_vsync->ust != 0) && (frame_vsync->msc != 0);
|
|
|
|
|
|
|
|
const uint64_t ust = (sec * 1000000LL) + usec;
|
|
|
|
|
|
|
|
const unsigned int msc_since_last_flip = msc - vsync->msc;
|
2019-12-07 17:23:42 +00:00
|
|
|
if (ready && msc == vsync->msc) {
|
|
|
|
// Seems like some drivers only increment msc every other page flip when
|
|
|
|
// running in interlaced mode (I'm looking at you nouveau). Obviously we
|
|
|
|
// can't work with this, so shame the driver and bail.
|
|
|
|
mp_err(closure->log,
|
|
|
|
"Got the same msc value twice: (msc: %u, vsync->msc: %u). This shouldn't happen. Possibly broken driver/interlaced mode?\n",
|
|
|
|
msc, vsync->msc);
|
|
|
|
goto fail;
|
|
|
|
}
|
2019-07-29 17:32:29 +00:00
|
|
|
|
|
|
|
vsync->ust = ust;
|
|
|
|
vsync->msc = msc;
|
|
|
|
|
|
|
|
if (ready) {
|
|
|
|
// Convert to mp_time
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &ts))
|
|
|
|
goto fail;
|
|
|
|
const uint64_t now_monotonic = ts.tv_sec * 1000000LL + ts.tv_nsec / 1000;
|
|
|
|
const uint64_t ust_mp_time = mp_time_us() - (now_monotonic - vsync->ust);
|
|
|
|
|
|
|
|
const uint64_t ust_since_enqueue = vsync->ust - frame_vsync->ust;
|
|
|
|
const unsigned int msc_since_enqueue = vsync->msc - frame_vsync->msc;
|
|
|
|
const unsigned int sbc_since_enqueue = vsync->sbc - frame_vsync->sbc;
|
|
|
|
|
|
|
|
vsync_info->vsync_duration = ust_since_enqueue / msc_since_enqueue;
|
|
|
|
vsync_info->skipped_vsyncs = msc_since_last_flip - 1; // Valid iff swap_buffers is called every vsync
|
|
|
|
vsync_info->last_queue_display_time = ust_mp_time + (sbc_since_enqueue * vsync_info->vsync_duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
*closure->waiting_for_flip = false;
|
|
|
|
talloc_free(closure);
|
|
|
|
}
|