summaryrefslogtreecommitdiff
path: root/src/lib/elput/elput_manager.c
blob: 13db80973f0d235ca4fe99ec1f35f9ee428db7f7 (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
#include "elput_private.h"

static Elput_Interface *_ifaces[] =
{
#ifdef HAVE_SYSTEMD
   &_logind_interface,
#endif
   NULL,
};

static Eina_Bool
_cb_key_down(void *data, int type EINA_UNUSED, void *event)
{
   Elput_Manager *em;
   Ecore_Event_Key *ev;
   int code = 0, vt = 0;

   em = data;
   ev = event;
   code = (ev->keycode - 8);

   if ((ev->modifiers & ECORE_EVENT_MODIFIER_CTRL) &&
       (ev->modifiers & ECORE_EVENT_MODIFIER_ALT) &&
       (code >= KEY_F1) && (code <= KEY_F8))
     {
        vt = (code - KEY_F1 + 1);
        if (em->interface->vt_set)
          {
             if (!em->interface->vt_set(em, vt))
               ERR("Failed to switch to virtual terminal %d", vt);
          }
     }

   return ECORE_CALLBACK_RENEW;
}

EAPI Elput_Manager *
elput_manager_connect(const char *seat, unsigned int tty, Eina_Bool sync)
{
   Elput_Interface **it;

   for (it = _ifaces; *it != NULL; it++)
     {
        Elput_Interface *iface;
        Elput_Manager *em;

        iface = *it;
        if (iface->connect(&em, seat, tty, sync))
          return em;
     }

   return NULL;
}

EAPI void
elput_manager_disconnect(Elput_Manager *manager)
{
   EINA_SAFETY_ON_NULL_RETURN(manager);
   EINA_SAFETY_ON_NULL_RETURN(manager->interface);

   if (manager->interface->disconnect)
     manager->interface->disconnect(manager);
}

EAPI int
elput_manager_open(Elput_Manager *manager, const char *path, int flags)
{
   int ret = -1;

   EINA_SAFETY_ON_NULL_RETURN_VAL(manager, -1);
   EINA_SAFETY_ON_NULL_RETURN_VAL(manager->interface, -1);
   EINA_SAFETY_ON_NULL_RETURN_VAL(path, -1);

   if (flags < 0) flags = O_RDWR;

   if (manager->interface->open)
     {
        ret = manager->interface->open(manager, path, flags);
        if (ret)
          {
             manager->vt_hdlr =
               ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
                                       _cb_key_down, manager);
          }
     }

   return ret;
}

EAPI void
elput_manager_close(Elput_Manager *manager, int fd)
{
   EINA_SAFETY_ON_NULL_RETURN(manager);
   EINA_SAFETY_ON_NULL_RETURN(manager->interface);

   if (manager->vt_hdlr) ecore_event_handler_del(manager->vt_hdlr);
   manager->vt_hdlr = NULL;

   if (manager->interface->close)
     manager->interface->close(manager, fd);
}

EAPI Eina_Bool
elput_manager_vt_set(Elput_Manager *manager, int vt)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(manager, EINA_FALSE);
   EINA_SAFETY_ON_NULL_RETURN_VAL(manager->interface, EINA_FALSE);
   EINA_SAFETY_ON_TRUE_RETURN_VAL((vt < 0), EINA_FALSE);

   if (manager->interface->vt_set)
     return manager->interface->vt_set(manager, vt);

   return EINA_FALSE;
}