listen: Move into own file. "Document" API.

Move these helpers into their own file/namespace. There might be
times we want to do additional communication. Also it keeps
edi_main.c less cluttered.
master
Alastair Poole 2 years ago
parent 9d077bc19c
commit 63f03c5ef5
  1. 149
      src/bin/edi_listen.c
  2. 63
      src/bin/edi_listen.h
  3. 124
      src/bin/edi_main.c
  4. 2
      src/bin/meson.build

@ -0,0 +1,149 @@
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <Eina.h>
#include <Ecore.h>
#include "edi_listen.h"
#include "mainview/edi_mainview.h"
#include "edi_private.h"
#define LISTEN_SOCKET_NAME "edi_listener"
typedef struct _Edi_Listen_Server {
Ecore_Event_Handler *handler;
Ecore_Con_Server *srv;
} Edi_Listen_Server;
static void *_edi_listener = NULL;
static Eina_Bool
_edi_listen_server_client_connect_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
Ecore_Con_Event_Client_Data *ev;
const char *path;
ev = event;
path = ev->data;
edi_mainview_open_path(path);
ecore_con_client_del(ev->client);
return ECORE_CALLBACK_RENEW;
}
void
edi_listen_shutdown(void)
{
Edi_Listen_Server *server = _edi_listener;
if (!server) return;
ecore_event_handler_del(server->handler);
ecore_con_server_del(server->srv);
free(server);
}
Eina_Bool
edi_listen_init(void)
{
Edi_Listen_Server *server = calloc(1, sizeof(Edi_Listen_Server));
if (!server) return EINA_FALSE;
server->srv = ecore_con_server_add(ECORE_CON_LOCAL_USER, LISTEN_SOCKET_NAME, 0, NULL);
if (!server->srv) return EINA_FALSE;
server->handler = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, _edi_listen_server_client_connect_cb, NULL);
_edi_listener = server;
return EINA_TRUE;
}
typedef struct _Edi_Listen_Client {
Ecore_Con_Server *srv;
char *path;
Eina_Bool success;
} Edi_Listen_Client;
static Eina_Bool
_edi_listen_client_closed_cb(void *data, int type EINA_UNUSED, void *event EINA_UNUSED)
{
Ecore_Con_Event_Server_Del *ev;
Edi_Listen_Client *client = data;
ev = event;
if (client->srv != ev->server) return ECORE_CALLBACK_RENEW;
client->success = EINA_TRUE;
return ECORE_CALLBACK_DONE;
}
static Eina_Bool
_edi_listen_client_check_timer_cb(void *data EINA_UNUSED)
{
Edi_Listen_Client *client;
static double total = 0.0;
client = data;
total += 0.1;
if (total < 3.0)
return ECORE_CALLBACK_RENEW;
if (client->success)
ecore_main_loop_quit();
else
evas_object_show(edi_main_win_get());
free(client->path);
free(client);
return ECORE_CALLBACK_DONE;
}
static Eina_Bool
_edi_listen_client_connect_cb(void *data, int type EINA_UNUSED, void *event EINA_UNUSED)
{
Ecore_Con_Event_Server_Add *ev;
Ecore_Con_Server *srv;
Edi_Listen_Client *client;
ev = event;
srv = ev->server;
client = data;
if (client->srv != srv) return ECORE_CALLBACK_RENEW;
ecore_con_server_send(srv, client->path, 1 + strlen(client->path));
ecore_con_server_flush(srv);
return ECORE_CALLBACK_DONE;
}
Eina_Bool
edi_listen_client_add(char *path)
{
Edi_Listen_Client *client;
Ecore_Con_Server *srv = ecore_con_server_connect(ECORE_CON_LOCAL_USER, LISTEN_SOCKET_NAME, 0, NULL);
if (!srv)
{
free(path);
return EINA_FALSE;
}
client = calloc(1, sizeof(Edi_Listen_Client));
if (!client) return EINA_FALSE;
client->path = path;
client->srv = srv;
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, _edi_listen_client_connect_cb, client);
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, _edi_listen_client_closed_cb, client);
ecore_timer_add(0.1, _edi_listen_client_check_timer_cb, client);
return EINA_TRUE;
}

@ -0,0 +1,63 @@
#ifndef EDI_LISTEN_H
#define EDI_LISTEN_H
#include <Eina.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @file
* @brief Listening and sending to existing and new Edi sessions.
*/
/**
* @brief Listen communication functions.
* @defgroup Listen
*
* @{
*
* Communication between Edi sessions.
*/
/**
* Initialise and begin a listening session.
*
* @return Boolean success of initialisation.
*
* @ingroup Listen
*/
Eina_Bool
edi_listen_init();
/**
* Shutdown the listening object and any connections, freeing the object.
*
* @ingroup Listen
*/
void
edi_listen_shutdown(void);
/**
* Attempt to send a file name to an existing Edi session to open.
*
* @param path The path of the file to open.
*
* @return Boolean success of the operation.
*
* @ingroup Listen
*/
Eina_Bool
edi_listen_client_add(char *path);
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

@ -12,6 +12,7 @@
#include "edi_filepanel.h"
#include "edi_file.h"
#include "edi_process.h"
#include "edi_listen.h"
#include "edi_logpanel.h"
#include "edi_consolepanel.h"
#include "edi_searchpanel.h"
@ -52,8 +53,6 @@ static Evas_Object *_edi_menu_init, *_edi_menu_commit, *_edi_menu_push, *_edi_me
static Evas_Object *_edi_main_win, *_edi_main_box;
static Eina_Bool _edi_toolbar_is_horizontal, _edi_toolbar_text_visible;
static void *_edi_listener = NULL;
int _edi_log_dom = -1;
static void
@ -1752,127 +1751,6 @@ edi_main_win_get(void)
return _edi_main_win;
}
typedef struct _Edi_Listen_Server {
Ecore_Event_Handler *handler;
Ecore_Con_Server *srv;
} Edi_Listen_Server;
static Eina_Bool
_edi_listen_server_client_connect_cb(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
{
Ecore_Con_Event_Client_Data *ev;
const char *path;
ev = event;
path = ev->data;
edi_mainview_open_path(path);
ecore_con_client_del(ev->client);
return ECORE_CALLBACK_RENEW;
}
void
edi_listen_shutdown(void)
{
Edi_Listen_Server *server = _edi_listener;
if (!server) return;
ecore_event_handler_del(server->handler);
ecore_con_server_del(server->srv);
free(server);
}
Eina_Bool
edi_listen_init(void)
{
Edi_Listen_Server *server = calloc(1, sizeof(Edi_Listen_Server));
if (!server) return EINA_FALSE;
server->srv = ecore_con_server_add(ECORE_CON_LOCAL_USER, "listener", 0, NULL);
if (!server->srv) return EINA_FALSE;
server->handler = ecore_event_handler_add(ECORE_CON_EVENT_CLIENT_DATA, _edi_listen_server_client_connect_cb, NULL);
_edi_listener = server;
return EINA_TRUE;
}
typedef struct Edi_Listen_Client {
char *path;
Eina_Bool success;
} Edi_Listen_Client;
static Eina_Bool
_edi_listen_client_closed_cb(void *data, int type EINA_UNUSED, void *ev EINA_UNUSED)
{
Edi_Listen_Client *client = data;
client->success = EINA_TRUE;
return ECORE_CALLBACK_RENEW;
}
static Eina_Bool
_edi_listen_client_check_timer_cb(void *data EINA_UNUSED)
{
Edi_Listen_Client *client;
static double total = 0.0;
client = data;
total += 0.1;
if (total < 3.0)
return ECORE_CALLBACK_RENEW;
if (client->success)
ecore_main_loop_quit();
else
evas_object_show(edi_main_win_get());
return ECORE_CALLBACK_DONE;
}
static Eina_Bool
_edi_listen_client_connect_cb(void *data, int type EINA_UNUSED, void *event EINA_UNUSED)
{
Ecore_Con_Event_Server_Add *ev;
Ecore_Con_Server *srv;
Edi_Listen_Client *client;
ev = event;
srv = ev->server;
client = data;
ecore_con_server_send(srv, client->path, 1 + strlen(client->path));
ecore_con_server_flush(srv);
return ECORE_CALLBACK_DONE;
}
Eina_Bool
edi_listen_client_add(char *path)
{
Edi_Listen_Client *client;
Ecore_Con_Server *srv = ecore_con_server_connect(ECORE_CON_LOCAL_USER, "listener", 0, NULL);
if (!srv)
{
free(path);
return EINA_FALSE;
}
client = calloc(1, sizeof(Edi_Listen_Client));
if (!client) return EINA_FALSE;
client->path = path;
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_ADD, _edi_listen_client_connect_cb, client);
ecore_event_handler_add(ECORE_CON_EVENT_SERVER_DEL, _edi_listen_client_closed_cb, client);
ecore_timer_add(0.1, _edi_listen_client_check_timer_cb, client);
return EINA_TRUE;
}
Eina_Bool
edi_open(const char *inputpath)
{

@ -17,6 +17,8 @@ src = files([
'edi_file.h',
'edi_filepanel.c',
'edi_filepanel.h',
'edi_listen.c',
'edi_listen.h',
'edi_logpanel.c',
'edi_logpanel.h',
'edi_main.c',

Loading…
Cancel
Save