summaryrefslogtreecommitdiff
path: root/src/lib/ecore_drm/ecore_drm_launcher.c
blob: 6ba92ecd2ecb8e159a3d5b8a7b7705e11c33c6cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "ecore_drm_private.h"

static Eina_Bool logind = EINA_FALSE;

static Eina_Bool 
_ecore_drm_launcher_cb_vt_switch(void *data, int type EINA_UNUSED, void *event)
{
   Ecore_Drm_Device *dev;
   Ecore_Event_Key *ev;
   int keycode;
   int vt;

   dev = data;
   ev = event;
   keycode = ev->keycode - 8;

   if ((ev->modifiers & ECORE_EVENT_MODIFIER_CTRL) &&
       (ev->modifiers & ECORE_EVENT_MODIFIER_ALT) &&
       (keycode >= KEY_F1) && (keycode <= KEY_F8))
     {
        vt = (keycode - KEY_F1 + 1);

        if (!_ecore_drm_tty_switch(dev, vt))
          ERR("Failed to activate vt: %m");
     }

   return ECORE_CALLBACK_PASS_ON;
}

int
_ecore_drm_launcher_device_flags_set(int fd, int flags)
{
   int fl;

   fl = fcntl(fd, F_GETFL);
   if (fl < 0) return -1;

   if (flags & O_NONBLOCK)
     fl |= O_NONBLOCK;

   if (fcntl(fd, F_SETFL, fl) < 0)
     return -1;

   fl = fcntl(fd, F_GETFD);
   if (fl < 0) return -1;

   if (!(flags & O_CLOEXEC))
     fl &= ~FD_CLOEXEC;

   if (fcntl(fd, F_SETFD, fl) < 0)
     return -1;

   return fd;
}

EAPI Eina_Bool 
ecore_drm_launcher_connect(Ecore_Drm_Device *dev)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
   /* try to connect to logind */
   if (!(logind = _ecore_drm_logind_connect(dev)))
     {
        DBG("Launcher: Logind not supported");
        if (geteuid() == 0)
          {
             DBG("Launcher: Trying to continue with root privileges");
             if (!ecore_drm_tty_open(dev, NULL))
               {
                  ERR("Launcher: Could not setup tty");
                  return EINA_FALSE;
               }
          }
        else
          {
             ERR("Launcher: Root privileges needed");
             return EINA_FALSE;
          }
     }

   dev->tty.switch_hdlr = 
     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, 
                             _ecore_drm_launcher_cb_vt_switch, dev);

   return EINA_TRUE;
}

EAPI void 
ecore_drm_launcher_disconnect(Ecore_Drm_Device *dev)
{
   EINA_SAFETY_ON_NULL_RETURN(dev);
   if (dev->tty.switch_hdlr) ecore_event_handler_del(dev->tty.switch_hdlr);
   dev->tty.switch_hdlr = NULL;

   if (!logind)
     {
        if (!ecore_drm_tty_close(dev))
          ERR("Launcher: Could not close tty");
     }
   else
     {
        _ecore_drm_logind_disconnect(dev);
        logind = EINA_FALSE;
     }
}

Eina_Bool
_ecore_drm_launcher_device_open(const char *device, Ecore_Drm_Open_Cb callback, void *data, int flags)
{
   int fd = -1;
   struct stat s;

   if (logind)
     {
        if (!_ecore_drm_logind_device_open(device, callback, data))
          return EINA_FALSE;
     }
   else
     {
        fd = open(device, flags | O_CLOEXEC);
        if (fd < 0) return EINA_FALSE;
        if (fstat(fd, &s) == -1)
          {
             close(fd);
             fd = -1;
             return EINA_FALSE;
          }

        callback(data, fd, EINA_FALSE);
     }

   return EINA_TRUE;
}

int
_ecore_drm_launcher_device_open_no_pending(const char *device, int flags)
{
   int fd = -1;
   struct stat s;

   if (logind)
     {
        fd = _ecore_drm_logind_device_open_no_pending(device);
        if (fd < 0) return -1;
        if (_ecore_drm_launcher_device_flags_set(fd, flags | O_CLOEXEC) < 0)
          {
             close(fd);
             _ecore_drm_logind_device_close(device);
             return -1;
          }
     }
   else
     {
        fd = open(device, flags | O_CLOEXEC);
        if (fd < 0) return fd;
        if (fstat(fd, &s) == -1)
          {
             close(fd);
             return -1;
          }
     }

   return fd;
}

void
_ecore_drm_launcher_device_close(const char *device, int fd)
{
   if ((logind) && (device)) _ecore_drm_logind_device_close(device);

   if (fd < 0) return;
   close(fd);
}