<commit_before>#include "e.h"

/* local subsystem functions */
static void           _e_msgbus_request_name_cb(void *data, const Eldbus_Message *msg,
                                                Eldbus_Pending *pending);

static Eldbus_Message *_e_msgbus_core_restart_cb(const Eldbus_Service_Interface *iface,
                                                const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_core_shutdown_cb(const Eldbus_Service_Interface *iface,
                                                 const Eldbus_Message *msg);

static Eldbus_Message *_e_msgbus_module_load_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_unload_cb(const Eldbus_Service_Interface *iface,
                                                 const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_enable_cb(const Eldbus_Service_Interface *iface,
                                                 const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_disable_cb(const Eldbus_Service_Interface *iface,
                                                  const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_list_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);

static Eldbus_Message *_e_msgbus_profile_set_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_get_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_list_cb(const Eldbus_Service_Interface *iface,
                                                const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_add_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_delete_cb(const Eldbus_Service_Interface *iface,
                                                  const Eldbus_Message *msg);

#define E_MSGBUS_WIN_ACTION_CB_PROTO(NAME)                                                   \
  static Eldbus_Message * _e_msgbus_window_##NAME##_cb(const Eldbus_Service_Interface * iface, \
                                                      const Eldbus_Message * msg)

E_MSGBUS_WIN_ACTION_CB_PROTO(list);
E_MSGBUS_WIN_ACTION_CB_PROTO(close);
E_MSGBUS_WIN_ACTION_CB_PROTO(kill);
E_MSGBUS_WIN_ACTION_CB_PROTO(focus);
E_MSGBUS_WIN_ACTION_CB_PROTO(iconify);
E_MSGBUS_WIN_ACTION_CB_PROTO(uniconify);
E_MSGBUS_WIN_ACTION_CB_PROTO(maximize);
E_MSGBUS_WIN_ACTION_CB_PROTO(unmaximize);

/* local subsystem globals */
static E_Msgbus_Data *_e_msgbus_data = NULL;

static const Eldbus_Method core_methods[] = {
   { "Restart", NULL, NULL, _e_msgbus_core_restart_cb },
   { "Shutdown", NULL, NULL, _e_msgbus_core_shutdown_cb },
   { }
};

static const Eldbus_Method module_methods[] = {
   { "Load", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_load_cb },
   { "Unload", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_unload_cb },
   { "Enable", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_enable_cb },
   { "Disable", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_disable_cb },
   { "List", NULL, ELDBUS_ARGS({"a(si)", "modules"}),
     _e_msgbus_module_list_cb },
   { }
};

static const Eldbus_Method profile_methods[] = {
   { "Set", ELDBUS_ARGS({"s", "profile"}), NULL, _e_msgbus_profile_set_cb },
   { "Get", NULL, ELDBUS_ARGS({"s", "profile"}), _e_msgbus_profile_get_cb },
   { "List", NULL, ELDBUS_ARGS({"as", "array_profiles"}),
     _e_msgbus_profile_list_cb },
   { "Add", ELDBUS_ARGS({"s", "profile"}), NULL, _e_msgbus_profile_add_cb },
   { "Delete", ELDBUS_ARGS({"s", "profile"}), NULL, _e_msgbus_profile_delete_cb },
   { }
};

static const Eldbus_Method window_methods[] = {
   { "List", NULL, ELDBUS_ARGS({"a(si)", "array_of_window"}),
     _e_msgbus_window_list_cb },
   { "Close", ELDBUS_ARGS({"i", "window_id"}), NULL, _e_msgbus_window_close_cb },
   { "Kill", ELDBUS_ARGS({"i", "window_id"}), NULL, _e_msgbus_window_kill_cb },
   { "Focus", ELDBUS_ARGS({"i", "window_id"}), NULL, _e_msgbus_window_focus_cb },
   { "Iconify", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_iconify_cb },
   { "Uniconify", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_uniconify_cb },
   { "Maximize", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_maximize_cb },
   { "Unmaximize", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_unmaximize_cb },
   { }
};

#define PATH "/org/enlightenment/wm/RemoteObject"

static const Eldbus_Service_Interface_Desc core_desc = {
   "org.enlightenment.wm.Core", core_methods
};

static const Eldbus_Service_Interface_Desc module_desc = {
   "org.enlightenment.wm.Module", module_methods
};

static const Eldbus_Service_Interface_Desc profile_desc = {
   "org.enlightenment.wm.Profile", profile_methods
};

static const Eldbus_Service_Interface_Desc window_desc = {
   "org.enlightenment.wm.Window", window_methods
};

/* externally accessible functions */
EINTERN int
e_msgbus_init(void)
{
   _e_msgbus_data = E_NEW(E_Msgbus_Data, 1);

   eldbus_init();

   _e_msgbus_data->conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   if (!_e_msgbus_data->conn)
     {
        WRN("Cannot get ELDBUS_CONNECTION_TYPE_SESSION");
        return 0;
     }

   _e_msgbus_data->iface = eldbus_service_interface_register(_e_msgbus_data->conn,
                                                            PATH, &core_desc);
   eldbus_service_interface_register(_e_msgbus_data->conn, PATH, &module_desc);
   eldbus_service_interface_register(_e_msgbus_data->conn, PATH, &profile_desc);
   eldbus_service_interface_register(_e_msgbus_data->conn, PATH, &window_desc);
   eldbus_name_request(_e_msgbus_data->conn, "org.enlightenment.wm.service",
                      0, _e_msgbus_request_name_cb, NULL);
   return 1;
}

EINTERN int
e_msgbus_shutdown(void)
{
   if (_e_msgbus_data->iface)
     eldbus_service_object_unregister(_e_msgbus_data->iface);
   if (_e_msgbus_data->conn)
     {
        eldbus_name_release(_e_msgbus_data->conn,
                           "org.enlightenment.wm.service", NULL, NULL);
        eldbus_connection_unref(_e_msgbus_data->conn);
     }
   eldbus_shutdown();

   E_FREE(_e_msgbus_data);
   _e_msgbus_data = NULL;
   return 1;
}

EAPI Eldbus_Service_Interface *
e_msgbus_interface_attach(const Eldbus_Service_Interface_Desc *desc)
{
   if (!_e_msgbus_data->iface)
     return NULL;
   return eldbus_service_interface_register(_e_msgbus_data->conn, PATH, desc);
}

static void
_e_msgbus_request_name_cb(void *data __UNUSED__, const Eldbus_Message *msg,
                          Eldbus_Pending *pending __UNUSED__)
{
   unsigned int flag;

   if (eldbus_message_error_get(msg, NULL, NULL))
     {
        ERR("Could not request bus name");
        return;
     }

   if (!eldbus_message_arguments_get(msg, "u", &flag))
     {
        ERR("Could not get arguments on on_name_request");
        return;
     }

   if (!(flag & ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER))
     ERR("Name already in use\n");
}

/* Core Handlers */
static Eldbus_Message *
_e_msgbus_core_restart_cb(const Eldbus_Service_Interface *iface __UNUSED__,
                          const Eldbus_Message *msg)
{
   e_sys_action_do(E_SYS_RESTART, NULL);
   return eldbus_message_method_return_new(msg);
}

static Eldbus_Message *
_e_msgbus_core_shutdown_cb(const Eldbus_Service_Interface *iface __UNUSED__,
                           const Eldbus_Message *msg)
{
   e_sys_action_do(E_SYS_EXIT, NULL);
   return eldbus_message_method_return_new(msg);
}

/* Modules Handlers */
static Eldbus_Message *
_e_msgbus_module_load_cb(const Eldbus_Service_Interface *iface __UNUSED__,
                         const Eldbus_Message *msg)
{
   char *module;
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);

   if (!eldbus_message_arguments_get(msg, "s", &module))
     return reply;<commit_msg>msgbus: No error if name already registered<commit_after>#include "e.h"

/* local subsystem functions */
static void           _e_msgbus_request_name_cb(void *data, const Eldbus_Message *msg,
                                                Eldbus_Pending *pending);

static Eldbus_Message *_e_msgbus_core_restart_cb(const Eldbus_Service_Interface *iface,
                                                const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_core_shutdown_cb(const Eldbus_Service_Interface *iface,
                                                 const Eldbus_Message *msg);

static Eldbus_Message *_e_msgbus_module_load_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_unload_cb(const Eldbus_Service_Interface *iface,
                                                 const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_enable_cb(const Eldbus_Service_Interface *iface,
                                                 const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_disable_cb(const Eldbus_Service_Interface *iface,
                                                  const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_module_list_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);

static Eldbus_Message *_e_msgbus_profile_set_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_get_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_list_cb(const Eldbus_Service_Interface *iface,
                                                const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_add_cb(const Eldbus_Service_Interface *iface,
                                               const Eldbus_Message *msg);
static Eldbus_Message *_e_msgbus_profile_delete_cb(const Eldbus_Service_Interface *iface,
                                                  const Eldbus_Message *msg);

#define E_MSGBUS_WIN_ACTION_CB_PROTO(NAME)                                                   \
  static Eldbus_Message * _e_msgbus_window_##NAME##_cb(const Eldbus_Service_Interface * iface, \
                                                      const Eldbus_Message * msg)

E_MSGBUS_WIN_ACTION_CB_PROTO(list);
E_MSGBUS_WIN_ACTION_CB_PROTO(close);
E_MSGBUS_WIN_ACTION_CB_PROTO(kill);
E_MSGBUS_WIN_ACTION_CB_PROTO(focus);
E_MSGBUS_WIN_ACTION_CB_PROTO(iconify);
E_MSGBUS_WIN_ACTION_CB_PROTO(uniconify);
E_MSGBUS_WIN_ACTION_CB_PROTO(maximize);
E_MSGBUS_WIN_ACTION_CB_PROTO(unmaximize);

/* local subsystem globals */
static E_Msgbus_Data *_e_msgbus_data = NULL;

static const Eldbus_Method core_methods[] = {
   { "Restart", NULL, NULL, _e_msgbus_core_restart_cb },
   { "Shutdown", NULL, NULL, _e_msgbus_core_shutdown_cb },
   { }
};

static const Eldbus_Method module_methods[] = {
   { "Load", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_load_cb },
   { "Unload", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_unload_cb },
   { "Enable", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_enable_cb },
   { "Disable", ELDBUS_ARGS({"s", "module"}), NULL, _e_msgbus_module_disable_cb },
   { "List", NULL, ELDBUS_ARGS({"a(si)", "modules"}),
     _e_msgbus_module_list_cb },
   { }
};

static const Eldbus_Method profile_methods[] = {
   { "Set", ELDBUS_ARGS({"s", "profile"}), NULL, _e_msgbus_profile_set_cb },
   { "Get", NULL, ELDBUS_ARGS({"s", "profile"}), _e_msgbus_profile_get_cb },
   { "List", NULL, ELDBUS_ARGS({"as", "array_profiles"}),
     _e_msgbus_profile_list_cb },
   { "Add", ELDBUS_ARGS({"s", "profile"}), NULL, _e_msgbus_profile_add_cb },
   { "Delete", ELDBUS_ARGS({"s", "profile"}), NULL, _e_msgbus_profile_delete_cb },
   { }
};

static const Eldbus_Method window_methods[] = {
   { "List", NULL, ELDBUS_ARGS({"a(si)", "array_of_window"}),
     _e_msgbus_window_list_cb },
   { "Close", ELDBUS_ARGS({"i", "window_id"}), NULL, _e_msgbus_window_close_cb },
   { "Kill", ELDBUS_ARGS({"i", "window_id"}), NULL, _e_msgbus_window_kill_cb },
   { "Focus", ELDBUS_ARGS({"i", "window_id"}), NULL, _e_msgbus_window_focus_cb },
   { "Iconify", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_iconify_cb },
   { "Uniconify", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_uniconify_cb },
   { "Maximize", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_maximize_cb },
   { "Unmaximize", ELDBUS_ARGS({"i", "window_id"}), NULL,
     _e_msgbus_window_unmaximize_cb },
   { }
};

#define PATH "/org/enlightenment/wm/RemoteObject"

static const Eldbus_Service_Interface_Desc core_desc = {
   "org.enlightenment.wm.Core", core_methods
};

static const Eldbus_Service_Interface_Desc module_desc = {
   "org.enlightenment.wm.Module", module_methods
};

static const Eldbus_Service_Interface_Desc profile_desc = {
   "org.enlightenment.wm.Profile", profile_methods
};

static const Eldbus_Service_Interface_Desc window_desc = {
   "org.enlightenment.wm.Window", window_methods
};

/* externally accessible functions */
EINTERN int
e_msgbus_init(void)
{
   _e_msgbus_data = E_NEW(E_Msgbus_Data, 1);

   eldbus_init();

   _e_msgbus_data->conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   if (!_e_msgbus_data->conn)
     {
        WRN("Cannot get ELDBUS_CONNECTION_TYPE_SESSION");
        return 0;
     }

   _e_msgbus_data->iface = eldbus_service_interface_register(_e_msgbus_data->conn,
                                                            PATH, &core_desc);
   eldbus_service_interface_register(_e_msgbus_data->conn, PATH, &module_desc);
   eldbus_service_interface_register(_e_msgbus_data->conn, PATH, &profile_desc);
   eldbus_service_interface_register(_e_msgbus_data->conn, PATH, &window_desc);
   eldbus_name_request(_e_msgbus_data->conn, "org.enlightenment.wm.service",
                      0, _e_msgbus_request_name_cb, NULL);
   return 1;
}

EINTERN int
e_msgbus_shutdown(void)
{
   if (_e_msgbus_data->iface)
     eldbus_service_object_unregister(_e_msgbus_data->iface);
   if (_e_msgbus_data->conn)
     {
        eldbus_name_release(_e_msgbus_data->conn,
                           "org.enlightenment.wm.service", NULL, NULL);
        eldbus_connection_unref(_e_msgbus_data->conn);
     }
   eldbus_shutdown();

   E_FREE(_e_msgbus_data);
   _e_msgbus_data = NULL;
   return 1;
}

EAPI Eldbus_Service_Interface *
e_msgbus_interface_attach(const Eldbus_Service_Interface_Desc *desc)
{
   if (!_e_msgbus_data->iface)
     return NULL;
   return eldbus_service_interface_register(_e_msgbus_data->conn, PATH, desc);
}

static void
_e_msgbus_request_name_cb(void *data __UNUSED__, const Eldbus_Message *msg,
                          Eldbus_Pending *pending __UNUSED__)
{
   unsigned int flag;

   if (eldbus_message_error_get(msg, NULL, NULL))
     {
        ERR("Could not request bus name");
        return;
     }

   if (!eldbus_message_arguments_get(msg, "u", &flag))
     {
        ERR("Could not get arguments on on_name_request");
        return;
     }

   if (!(flag & ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER))
     WRN("Name already in use\n");
}

/* Core Handlers */
static Eldbus_Message *
_e_msgbus_core_restart_cb(const Eldbus_Service_Interface *iface __UNUSED__,
                          const Eldbus_Message *msg)
{
   e_sys_action_do(E_SYS_RESTART, NULL);
   return eldbus_message_method_return_new(msg);
}

static Eldbus_Message *
_e_msgbus_core_shutdown_cb(const Eldbus_Service_Interface *iface __UNUSED__,
                           const Eldbus_Message *msg)
{
   e_sys_action_do(E_SYS_EXIT, NULL);
   return eldbus_message_method_return_new(msg);
}

/* Modules Handlers */
static Eldbus_Message *
_e_msgbus_module_load_cb(const Eldbus_Service_Interface *iface __UNUSED__,
                         const Eldbus_Message *msg)
{
   char *module;
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);

   if (!eldbus_message_arguments_get(msg, "s", &module))
     return reply;
{'fraction_non_alphanumeric': 0.09989056420233464, 'fraction_numerical': 0.0006079766536964981, 'mean_word_length': 3.2669260700389104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 64, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4767853', 'n_tokens_mistral': 5492, 'n_tokens_neox': 5408, 'n_words': 883}