Skip to content

Commit c9929a4

Browse files
committed
add device_open()
1 parent 2d62ce1 commit c9929a4

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

drmtools.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "gfx.h"
1616
#include "drmtools.h"
17+
#include "logind.h"
1718

1819
/* ------------------------------------------------------------------ */
1920

@@ -88,7 +89,7 @@ int drm_init_dev(const char *dev, const char *output, const char *mode)
8889
int i, rc;
8990

9091
/* open device */
91-
drm_fd = open(dev, O_RDWR | O_CLOEXEC);
92+
drm_fd = device_open(dev);
9293
if (drm_fd < 0) {
9394
fprintf(stderr, "drm: open %s: %s\n", dev, strerror(errno));
9495
return -1;
@@ -247,7 +248,7 @@ static void drm_suspend_display(void)
247248

248249
static void drm_resume_display(void)
249250
{
250-
drm_fd = open(drm_dev, O_RDWR | O_CLOEXEC);
251+
drm_fd = device_open(drm_dev);
251252
drm_init_fb(&fb1, drm_fmt, false);
252253
if (fb2.mem)
253254
drm_init_fb(&fb2, drm_fmt, false);
@@ -333,7 +334,7 @@ void drm_info(const char *device)
333334
} else {
334335
snprintf(dev, sizeof(dev), DRM_DEV_NAME, DRM_DIR_NAME, 0);
335336
}
336-
drm_fd = open(dev, O_RDWR | O_CLOEXEC);
337+
drm_fd = device_open(dev);
337338
if (drm_fd < 0) {
338339
fprintf(stderr, "drm: open %s: %s\n", dev, strerror(errno));
339340
return;

fbtools.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "vt.h"
2626
#include "fbtools.h"
27+
#include "logind.h"
2728

2829
/* -------------------------------------------------------------------- */
2930
/* internal variables */
@@ -225,7 +226,7 @@ gfxstate* fb_init(const char *device, char *mode)
225226
fprintf(stderr, "trying fbdev: %s ...\n", device);
226227

227228
/* get current settings (which we have to restore) */
228-
if (-1 == (fb = open(device,O_RDWR | O_CLOEXEC))) {
229+
if (-1 == (fb = device_open(device))) {
229230
fprintf(stderr,"open %s: %s\n",device,strerror(errno));
230231
exit(1);
231232
}

logind.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ int logind_open(const char *path, int flags, void *user_data)
340340
int inactive;
341341
int handle, fd, r;
342342

343+
if (!logind_dbus)
344+
return -1;
345+
343346
r = stat(path, &st);
344347
if (r < 0) {
345348
fprintf(stderr, "stat %s failed: %s\n", path, strerror(errno));
@@ -396,6 +399,9 @@ void logind_close(int fd, void *user_data)
396399
unsigned int maj, min;
397400
int r;
398401

402+
if (!logind_dbus)
403+
return;
404+
399405
r = fstat(fd, &st);
400406
if (r < 0) {
401407
fprintf(stderr, "fstat failed: %s\n", strerror(errno));
@@ -482,3 +488,21 @@ const struct libinput_interface libinput_if_logind = {
482488
.open_restricted = logind_open,
483489
.close_restricted = logind_close,
484490
};
491+
492+
int device_open(const char *device)
493+
{
494+
int saved_errno, fd;
495+
496+
fd = open(device, O_RDWR | O_CLOEXEC);
497+
if (fd < 0) {
498+
saved_errno = errno;
499+
fd = logind_open(device, 0, NULL);
500+
if (fd < 0) {
501+
errno = saved_errno;
502+
return -1;
503+
} else {
504+
fprintf(stderr, "%s: got handle from logind\n", device);
505+
}
506+
}
507+
return fd;
508+
}

logind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ int logind_take_control(void);
1616
int logind_release_control(void);
1717
int logind_open(const char *path, int flags, void *user_data);
1818
void logind_close(int fd, void *user_data);
19+
20+
int device_open(const char *device);

0 commit comments

Comments
 (0)