Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split into application and library #3

Merged
merged 21 commits into from
Aug 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
platsch: add platsch_create_ctx
Add platsch_create_ctx() to bundle the initialization.

This prepares the code base for the upcoming application/library split.

Signed-off-by: Marco Felsch <[email protected]>
  • Loading branch information
Marco Felsch committed Aug 5, 2024
commit 3837c57e896505bfc31c91e387724dadddce4466
112 changes: 66 additions & 46 deletions platsch.c
Original file line number Diff line number Diff line change
@@ -564,6 +564,68 @@ static int drmprepare(struct platsch_ctx *ctx)
return 0;
}

static struct platsch_ctx *platsch_create_ctx(const char *dir, const char *base)
{
struct platsch_ctx *ctx;
int drmfd;
int ret;
int i;

ctx = calloc(1, sizeof(*ctx));
if (!ctx) {
error("Cannot allocate memory for platsch_ctx\n");
return NULL;
}
ctx->dir = strdup(dir);
ctx->base = strdup(base);

for (i = 0; i < 64; i++) {
struct drm_mode_card_res res = {0};
char *drmdev;

/*
* XXX: Maybe use drmOpen instead?
* (Where should name/busid come from?)
* XXX: Loop through drm devices to find one with connectors.
*/
ret = asprintf(&drmdev, DRM_DEV_NAME, DRM_DIR_NAME, i);
if (ret < 0) {
error("Huh, failed to allocate device name buffer\n");
goto err_out;
}

drmfd = open(drmdev, O_RDWR | O_CLOEXEC, 0);
free(drmdev);
if (drmfd < 0) {
error("Failed to open drm device: %m\n");
goto err_out;
}

ret = drmIoctl(drmfd, DRM_IOCTL_MODE_GETRESOURCES, &res);
if (ret < 0) {
close(drmfd);
continue;
} else {
/* Device found */
ctx->drmfd = drmfd;
break;
}
}

ret = drmprepare(ctx);
if (ret)
goto err_out;

return ctx;

err_out:
free(ctx->dir);
free(ctx->base);
free(ctx);

return NULL;
}

static struct option longopts[] =
{
{ "help", no_argument, 0, 'h' },
@@ -583,14 +645,13 @@ static void usage(const char *prog)
int main(int argc, char *argv[])
{
char **initsargv;
int drmfd;
struct platsch_ctx *ctx;
struct modeset_dev *iter;
bool pid1 = getpid() == 1;
const char *dir = "/usr/share/platsch";
const char *base = "splash";
const char *env;
int ret = 0, c, i;
int ret = 0, c;

env = getenv("platsch_directory");
if (env)
@@ -626,49 +687,9 @@ int main(int argc, char *argv[])
}
}

ctx = calloc(1, sizeof(*ctx));
if (!ctx) {
error("Cannot allocate memory for platsch_ctx\n");
exit(1);
}
ctx->dir = strdup(dir);
ctx->base = strdup(base);

for (i = 0; i < 64; i++) {
struct drm_mode_card_res res = {0};
char *drmdev;

/*
* XXX: Maybe use drmOpen instead?
* (Where should name/busid come from?)
* XXX: Loop through drm devices to find one with connectors.
*/
ret = asprintf(&drmdev, DRM_DEV_NAME, DRM_DIR_NAME, i);
if (ret < 0) {
error("Huh, failed to allocate device name buffer\n");
goto execinit;
}

drmfd = open(drmdev, O_RDWR | O_CLOEXEC, 0);
free(drmdev);
if (drmfd < 0) {
error("Failed to open drm device: %m\n");
goto execinit;
}

ret = drmIoctl(drmfd, DRM_IOCTL_MODE_GETRESOURCES, &res);
if (ret < 0) {
close(drmfd);
continue;
} else {
/* Device found */
ctx->drmfd = drmfd;
break;
}
}

ret = drmprepare(ctx);
assert(!ret);
ctx = platsch_create_ctx(dir, base);
if (!ctx)
return EXIT_FAILURE;

for (iter = ctx->modeset_list; iter; iter = iter->next) {

@@ -701,7 +722,6 @@ int main(int argc, char *argv[])
free(ctx->base);
free(ctx);

execinit:
if (pid1) {
ret = fork();
if (ret < 0) {