ecore-drm: remove duplicated code from logind and tty.
authorSeunghun Lee <shiin.lee@samsung.com>
Mon, 5 Jan 2015 14:08:27 +0000 (09:08 -0500)
committerChris Michael <cp.michael@samsung.com>
Mon, 5 Jan 2015 14:08:27 +0000 (09:08 -0500)
Summary: integrates the code used in common.

Reviewers: devilhorns

Reviewed By: devilhorns

Subscribers: cedric

Differential Revision: https://phab.enlightenment.org/D1796

src/lib/ecore_drm/ecore_drm_launcher.c
src/lib/ecore_drm/ecore_drm_logind.c
src/lib/ecore_drm/ecore_drm_private.h
src/lib/ecore_drm/ecore_drm_tty.c

index dbe7303..e630932 100644 (file)
@@ -3,6 +3,111 @@
 
 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;
+}
+
+
+static Eina_Bool
+_ecore_drm_launcher_cb_vt_signal(void *data, int type EINA_UNUSED, void *event)
+{
+   Ecore_Drm_Device *dev;
+   Ecore_Event_Signal_User *ev;
+   siginfo_t sigdata;
+
+   dev = data;
+   ev = event;
+
+   sigdata = ev->data;
+   if (sigdata.si_code != SI_KERNEL) return ECORE_CALLBACK_RENEW;
+
+   if (ev->number == 1)
+     {
+        if (!logind)
+          {
+             Ecore_Drm_Input *input;
+             Ecore_Drm_Output *output;
+             Ecore_Drm_Sprite *sprite;
+             Eina_List *l;
+
+             /* disable inputs (suspends) */
+             EINA_LIST_FOREACH(dev->inputs, l, input)
+                ecore_drm_inputs_disable(input);
+
+             /* disable hardware cursor */
+             EINA_LIST_FOREACH(dev->outputs, l, output)
+                ecore_drm_output_cursor_size_set(output, 0, 0, 0);
+
+             /* disable sprites */
+             EINA_LIST_FOREACH(dev->sprites, l, sprite)
+                ecore_drm_sprites_fb_set(sprite, 0, 0);
+
+             /* drop drm master */
+             ecore_drm_device_master_drop(dev);
+
+             _ecore_drm_event_activate_send(EINA_FALSE);
+          }
+
+        /* issue ioctl to release vt */
+        if (!ecore_drm_tty_release(dev))
+          ERR("Could not release VT: %m");
+     }
+   else if (ev->number == 2)
+     {
+        if (!logind)
+          {
+             Ecore_Drm_Output *output;
+             Ecore_Drm_Input *input;
+             Eina_List *l;
+
+             /* set drm master */
+             if (!ecore_drm_device_master_set(dev))
+               ERR("Could not set drm master: %m");
+
+             /* set output mode */
+             EINA_LIST_FOREACH(dev->outputs, l, output)
+                ecore_drm_output_enable(output);
+
+             /* enable inputs */
+             EINA_LIST_FOREACH(dev->inputs, l, input)
+                ecore_drm_inputs_enable(input);
+
+             if (ecore_drm_tty_acquire(dev))
+               _ecore_drm_event_activate_send(EINA_TRUE);
+             else
+               ERR("Could not acquire VT: %m");
+          }
+        else
+          {
+             if (!ecore_drm_tty_acquire(dev))
+               ERR("Could not acquire VT: %m");
+          }
+     }
+
+   return ECORE_CALLBACK_RENEW;
+}
+
 EAPI Eina_Bool
 ecore_drm_launcher_connect(Ecore_Drm_Device *dev)
 {
@@ -10,20 +115,30 @@ ecore_drm_launcher_connect(Ecore_Drm_Device *dev)
      {
         DBG("Launcher: Not Support logind\n");
         if (geteuid() == 0)
-          {
-             DBG("Launcher: Try to keep going with root privilege\n");
-             if (!ecore_drm_tty_open(dev, NULL))
-               {
-                  ERR("Launcher: failed to open tty with root privilege\n");
-                  return EINA_FALSE;
-               }
-          }
+          DBG("Launcher: Try to keep going with root privilege\n");
         else
           {
              ERR("Launcher: Need Root Privilege or logind\n");
              return EINA_FALSE;
           }
      }
+
+   if (!ecore_drm_tty_open(dev, NULL))
+     {
+        ERR("Launcher: failed to open tty\n");
+        return EINA_FALSE;
+     }
+
+   /* setup handler for signals */
+   dev->tty.event_hdlr =
+      ecore_event_handler_add(ECORE_EVENT_SIGNAL_USER,
+                              _ecore_drm_launcher_cb_vt_signal, dev);
+
+   /* setup handler for key event of vt switch */
+   dev->tty.switch_hdlr =
+      ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
+                              _ecore_drm_launcher_cb_vt_switch, dev);
+
    DBG("Launcher: Success Connect\n");
 
    return EINA_TRUE;
@@ -37,8 +152,15 @@ ecore_drm_launcher_disconnect(Ecore_Drm_Device *dev)
         logind = EINA_FALSE;
         _ecore_drm_logind_disconnect(dev);
      }
-   else
-     ecore_drm_tty_close(dev);
+
+   if (!ecore_drm_tty_close(dev))
+     ERR("Launcher: failed to close tty\n");
+
+   if (dev->tty.event_hdlr) ecore_event_handler_del(dev->tty.event_hdlr);
+   dev->tty.event_hdlr = NULL;
+
+   if (dev->tty.switch_hdlr) ecore_event_handler_del(dev->tty.switch_hdlr);
+   dev->tty.switch_hdlr = NULL;
 }
 
 static int
index 2e11102..9a8d7f1 100644 (file)
@@ -14,58 +14,6 @@ static Ecore_Event_Handler *active_hdl;
 static char *sid;
 
 static Eina_Bool
-_ecore_drm_logind_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 (ioctl(dev->tty.fd, VT_ACTIVATE, vt) < 0)
-          ERR("Failed to activate vt: %m");
-     }
-
-   return ECORE_CALLBACK_PASS_ON;
-}
-
-static Eina_Bool
-_ecore_drm_logind_cb_vt_signal(void *data, int type EINA_UNUSED, void *event)
-{
-   Ecore_Drm_Device *dev;
-   Ecore_Event_Signal_User *ev;
-   siginfo_t sigdata;
-
-   dev = data;
-   ev = event;
-
-   sigdata = ev->data;
-   if (sigdata.si_code != SI_KERNEL) return ECORE_CALLBACK_RENEW;
-
-   if (ev->number == 1)
-     {
-        if (!ecore_drm_tty_release(dev))
-          ERR("Could not release VT: %m");
-     }
-   else if (ev->number == 2)
-     {
-        if (!ecore_drm_tty_acquire(dev))
-          ERR("Could not acquire VT: %m");
-     }
-
-   return ECORE_CALLBACK_PASS_ON;
-}
-
-static Eina_Bool
 _ecore_drm_logind_cb_activate(void *data, int type EINA_UNUSED, void *event)
 {
    Ecore_Drm_Event_Activate *e;
@@ -100,163 +48,6 @@ _ecore_drm_logind_cb_activate(void *data, int type EINA_UNUSED, void *event)
    return ECORE_CALLBACK_PASS_ON;
 }
 
-static Eina_Bool
-_ecore_drm_logind_tty_setup(Ecore_Drm_Device *dev)
-{
-   struct stat st;
-   int kmode;
-   struct vt_mode vtmode = { 0 };
-
-   if (fstat(dev->tty.fd, &st) == -1)
-     {
-        ERR("Failed to get stats for tty: %m");
-        return EINA_FALSE;
-     }
-
-   if (ioctl(dev->tty.fd, KDGETMODE, &kmode))
-     {
-        ERR("Could not get tty mode: %m");
-        return EINA_FALSE;
-     }
-
-   if (ioctl(dev->tty.fd, VT_ACTIVATE, minor(st.st_rdev)) < 0)
-     {
-        ERR("Failed to activate vt: %m");
-        return EINA_FALSE;
-     }
-
-   if (ioctl(dev->tty.fd, VT_WAITACTIVE, minor(st.st_rdev)) < 0)
-     {
-        ERR("Failed to wait active: %m");
-        return EINA_FALSE;
-     }
-
-   /* NB: Don't set this. This Turns OFF keyboard on the VT */
-   /* if (ioctl(dev->tty.fd, KDSKBMUTE, 1) &&  */
-   /*     ioctl(dev->tty.fd, KDSKBMODE, K_OFF)) */
-   /*   { */
-   /*      ERR("Could not set K_OFF keyboard mode: %m"); */
-   /*      return EINA_FALSE; */
-   /*   } */
-
-   if (kmode != KD_GRAPHICS)
-     {
-        if (ioctl(dev->tty.fd, KDSETMODE, KD_GRAPHICS))
-          {
-             ERR("Could not set graphics mode: %m");
-             goto err_kmode;
-          }
-     }
-
-   vtmode.mode = VT_PROCESS;
-   vtmode.waitv = 0;
-   vtmode.relsig = SIGUSR1;
-   vtmode.acqsig = SIGUSR2;
-   if (ioctl(dev->tty.fd, VT_SETMODE, &vtmode) < 0)
-     {
-        ERR("Could not set Terminal Mode: %m");
-        goto err_setmode;
-     }
-
-   return EINA_TRUE;
-err_setmode:
-   ioctl(dev->tty.fd, KDSETMODE, KD_TEXT);
-err_kmode:
-   return EINA_FALSE;
-}
-
-static Eina_Bool
-_ecore_drm_logind_vt_open(Ecore_Drm_Device *dev, const char *name)
-{
-   char tty[32] = "<stdin>";
-
-   /* check for valid device */
-   if ((!dev) || (!dev->drm.name)) return EINA_FALSE;
-
-   /* assign default tty fd of -1 */
-   dev->tty.fd = -1;
-
-   if (!name)
-     {
-        char *env;
-
-        if ((env = getenv("ECORE_DRM_TTY")))
-          snprintf(tty, sizeof(tty), "%s", env);
-        else
-          dev->tty.fd = dup(STDIN_FILENO);
-     }
-   else
-     snprintf(tty, sizeof(tty), "%s", name);
-
-   if (dev->tty.fd < 0)
-     {
-        DBG("Trying to Open Tty: %s", tty);
-
-        dev->tty.fd = open(tty, O_RDWR | O_NOCTTY);
-        if (dev->tty.fd < 0)
-          {
-             DBG("Failed to Open Tty: %m");
-             return EINA_FALSE;
-          }
-     }
-
-   /* save tty name */
-   dev->tty.name = eina_stringshare_add(tty);
-
-   /* FIXME */
-   if (!_ecore_drm_logind_tty_setup(dev))
-     {
-        close(dev->tty.fd);
-        dev->tty.fd = -1;
-        if (dev->tty.name)
-          {
-             eina_stringshare_del(dev->tty.name);
-             dev->tty.name = NULL;
-          }
-        return EINA_FALSE;
-     }
-
-   /* setup handler for signals */
-   dev->tty.event_hdlr =
-     ecore_event_handler_add(ECORE_EVENT_SIGNAL_USER,
-                             _ecore_drm_logind_cb_vt_signal, dev);
-
-   /* setup handler for key event of vt switch */
-   dev->tty.switch_hdlr =
-     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
-                             _ecore_drm_logind_cb_vt_switch, dev);
-
-   active_hdl =
-     ecore_event_handler_add(ECORE_DRM_EVENT_ACTIVATE,
-                             _ecore_drm_logind_cb_activate, dev);
-
-   /* set current tty into env */
-   setenv("ECORE_DRM_TTY", tty, 1);
-
-   return EINA_TRUE;
-}
-
-static void
-_ecore_drm_logind_vt_close(Ecore_Drm_Device *dev)
-{
-   struct vt_mode mode = { 0 };
-
-   ioctl(dev->tty.fd, KDSETMODE, KD_TEXT);
-   mode.mode = VT_AUTO;
-   ioctl(dev->tty.fd, VT_SETMODE, &mode);
-
-   if (dev->tty.event_hdlr) ecore_event_handler_del(dev->tty.event_hdlr);
-   dev->tty.event_hdlr = NULL;
-
-   if (dev->tty.switch_hdlr) ecore_event_handler_del(dev->tty.switch_hdlr);
-   dev->tty.switch_hdlr = NULL;
-
-   if (dev->tty.name) eina_stringshare_del(dev->tty.name);
-   dev->tty.name = NULL;
-
-   unsetenv("ECORE_DRM_TTY");
-}
-
 Eina_Bool
 _ecore_drm_logind_connect(Ecore_Drm_Device *dev)
 {
@@ -267,24 +58,21 @@ _ecore_drm_logind_connect(Ecore_Drm_Device *dev)
 
    /* try to init dbus */
    if (!_ecore_drm_dbus_init(sid))
-     goto dbus_err;
+     {
+        free(sid);
+        return EINA_FALSE;
+     }
 
-   if (!_ecore_drm_logind_vt_open(dev, NULL))
-     goto vt_err;
+   active_hdl =
+      ecore_event_handler_add(ECORE_DRM_EVENT_ACTIVATE,
+                              _ecore_drm_logind_cb_activate, dev);
 
    return EINA_TRUE;
-
-vt_err:
-   _ecore_drm_dbus_shutdown();
-dbus_err:
-   free(sid);
-   return EINA_FALSE;
 }
 
 void
-_ecore_drm_logind_disconnect(Ecore_Drm_Device *dev)
+_ecore_drm_logind_disconnect(Ecore_Drm_Device *dev EINA_UNUSED)
 {
-   _ecore_drm_logind_vt_close(dev);
    _ecore_drm_dbus_shutdown();
 
    if (active_hdl)
index 151303f..8b907b5 100644 (file)
@@ -235,6 +235,8 @@ Eina_Bool _ecore_drm_launcher_device_open(const char *device, Ecore_Drm_Open_Cb
 int _ecore_drm_launcher_device_open_no_pending(const char *device, int flags);
 void _ecore_drm_launcher_device_close(const char *device, int fd);
 
+Eina_Bool _ecore_drm_tty_switch(Ecore_Drm_Device *dev, int activate_vt);
+
 void _ecore_drm_inputs_update_output(Ecore_Drm_Device *dev, int w, int h);
 
 Ecore_Drm_Evdev *_ecore_drm_evdev_device_create(Ecore_Drm_Seat *seat, struct libinput_device *device);
index 11a074e..2bf7027 100644 (file)
 # define KDSKBMUTE 0x4B51
 #endif
 
-static Eina_Bool
-_ecore_drm_tty_cb_vt_switch(void *data, int type EINA_UNUSED, void *event)
+Eina_Bool
+_ecore_drm_tty_switch(Ecore_Drm_Device *dev, int activate_vt)
 {
-   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 (ioctl(dev->tty.fd, VT_ACTIVATE, vt) < 0)
-          ERR("Failed to activate vt: %m");
-     }
-
-   return ECORE_CALLBACK_PASS_ON;
-}
-
-static Eina_Bool 
-_ecore_drm_tty_cb_signal(void *data, int type EINA_UNUSED, void *event)
-{
-   Ecore_Drm_Device *dev;
-   Ecore_Event_Signal_User *ev;
-   siginfo_t sigdata;
-
-   dev = data;
-   ev = event;
-
-   sigdata = ev->data;
-   if (sigdata.si_code != SI_KERNEL) return ECORE_CALLBACK_RENEW;
-
-   if (ev->number == 1)
-     {
-        Ecore_Drm_Input *input;
-        Ecore_Drm_Output *output;
-        Ecore_Drm_Sprite *sprite;
-        Eina_List *l;
-
-        /* disable inputs (suspends) */
-        EINA_LIST_FOREACH(dev->inputs, l, input)
-          ecore_drm_inputs_disable(input);
-
-        /* disable hardware cursor */
-        EINA_LIST_FOREACH(dev->outputs, l, output)
-          ecore_drm_output_cursor_size_set(output, 0, 0, 0);
-
-        /* disable sprites */
-        EINA_LIST_FOREACH(dev->sprites, l, sprite)
-          ecore_drm_sprites_fb_set(sprite, 0, 0);
-
-        /* drop drm master */
-        if (ecore_drm_device_master_drop(dev))
-          {
-             /* issue ioctl to release vt */
-             if (!ecore_drm_tty_release(dev))
-               ERR("Could not release VT: %m");
-          }
-        else
-          ERR("Could not drop drm master: %m");
-
-        _ecore_drm_event_activate_send(EINA_FALSE);
-     }
-   else if (ev->number == 2)
-     {
-        /* issue ioctl to acquire vt */
-        if (ecore_drm_tty_acquire(dev))
-          {
-             Ecore_Drm_Output *output;
-             Ecore_Drm_Input *input;
-             Eina_List *l;
-
-             /* set drm master */
-             if (!ecore_drm_device_master_set(dev))
-               ERR("Could not set drm master: %m");
-
-             /* set output mode */
-             EINA_LIST_FOREACH(dev->outputs, l, output)
-               ecore_drm_output_enable(output);
-
-             /* enable inputs */
-             EINA_LIST_FOREACH(dev->inputs, l, input)
-               ecore_drm_inputs_enable(input);
-
-             _ecore_drm_event_activate_send(EINA_TRUE);
-          }
-        else
-          ERR("Could not acquire VT: %m");
-     }
-
-   return ECORE_CALLBACK_RENEW;
+   if (!ioctl(dev->tty.fd, VT_ACTIVATE, activate_vt) < 0)
+     return EINA_FALSE;
+   return EINA_TRUE;
 }
 
 static Eina_Bool 
@@ -246,16 +155,6 @@ ecore_drm_tty_open(Ecore_Drm_Device *dev, const char *name)
         return EINA_FALSE;
      }
 
-   /* setup handler for signals */
-   dev->tty.event_hdlr = 
-     ecore_event_handler_add(ECORE_EVENT_SIGNAL_USER, 
-                             _ecore_drm_tty_cb_signal, dev);
-
-   /* setup handler for key event of vt switch */
-   dev->tty.switch_hdlr =
-     ecore_event_handler_add(ECORE_EVENT_KEY_DOWN,
-                             _ecore_drm_tty_cb_vt_switch, dev);
-
    /* set current tty into env */
    setenv("ECORE_DRM_TTY", tty, 1);
 
@@ -301,14 +200,6 @@ ecore_drm_tty_close(Ecore_Drm_Device *dev)
 
    dev->tty.fd = -1;
 
-   /* destroy the event handler */
-   if (dev->tty.event_hdlr) ecore_event_handler_del(dev->tty.event_hdlr);
-   dev->tty.event_hdlr = NULL;
-
-   /* destroy the event handler */
-   if (dev->tty.switch_hdlr) ecore_event_handler_del(dev->tty.switch_hdlr);
-   dev->tty.switch_hdlr = NULL;
-
    /* clear the tty name */
    if (dev->tty.name) eina_stringshare_del(dev->tty.name);
    dev->tty.name = NULL;