summaryrefslogtreecommitdiff
path: root/src/bin/e_remote_main.c
blob: 240b314198e4eea53fabe6d4878e3ca6fa1d9b89 (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "e.h"

/* local subsystem functions */
static int  _e_cb_signal_exit(void *data, int ev_type, void *ev);
static int  _e_ipc_init(void);
static void _e_ipc_shutdown(void);

static int _e_ipc_cb_server_add(void *data, int type, void *event);
static int _e_ipc_cb_server_del(void *data, int type, void *event);
static int _e_ipc_cb_server_data(void *data, int type, void *event);

/* local subsystem globals */
static Ecore_Ipc_Server *_e_ipc_server  = NULL;
static const char *display_name = NULL;
static int reply_count = 0;
static int reply_expect = 0;

/* externally accessible functions */
int
main(int argc, char **argv)
{
   int i;
   
   /* handle some command-line parameters */
   display_name = (const char *)getenv("DISPLAY");
   for (i = 1; i < argc; i++)
     {
	if ((!strcmp(argv[i], "-display")) && (i < (argc - 1)))
	  {
	     i++;
	     display_name = argv[i];
	  }
     }
  
   /* basic ecore init */
   if (!ecore_init())
     {
	printf("ERROR: Enlightenment_remote cannot Initialize Ecore!\n"
	       "Perhaps you are out of memory?\n");
	exit(-1);
     }
   ecore_app_args_set((int)argc, (const char **)argv);
   /* setup a handler for when e is asked to exit via a system signal */
   if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT, _e_cb_signal_exit, NULL))
     {
	printf("ERROR: Enlightenment_remote cannot set up an exit signal handler.\n"
	       "Perhaps you are out of memory?\n");
	exit(-1);
     }
   /* init ipc */
   if (!ecore_ipc_init())
     {
	printf("ERROR: Enlightenment_remote cannot initialize the ipc system.\n"
	       "Perhaps you are out of memory?\n");
	exit(-1);
     }
   
   /* setup e ipc service */
   if (!_e_ipc_init())
     {
	printf("ERROR: Enlightenment_remote cannot set up the IPC socket.\n"
	       "Maybe try the '-display :0' option?\n");
	exit(-1);
     }

   /* start our main loop */
   ecore_main_loop_begin();
   
   _e_ipc_shutdown();
   ecore_ipc_shutdown();
   ecore_shutdown();
   
   /* just return 0 to keep the compiler quiet */
   return 0;
}

/* local subsystem functions */
static int
_e_cb_signal_exit(void *data, int ev_type, void *ev)
{
   /* called on ctrl-c, kill (pid) (also SIGINT, SIGTERM and SIGQIT) */
   ecore_main_loop_quit();
   return 1;
}

static int
_e_ipc_init(void)
{
   char buf[1024];
   char *disp;
   
   disp = (char *)display_name;
   if (!disp) disp = ":0";
   snprintf(buf, sizeof(buf), "enlightenment-(%s)", disp);
   _e_ipc_server = ecore_ipc_server_connect(ECORE_IPC_LOCAL_USER, buf, 0, NULL);
   /* FIXME: we shoudl also try the generic ":0" if the display is ":0.0" */
   /* similar... */
   if (!_e_ipc_server) return 0;
   
   ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_ADD, _e_ipc_cb_server_add, NULL);
   ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DEL, _e_ipc_cb_server_del, NULL);
   ecore_event_handler_add(ECORE_IPC_EVENT_SERVER_DATA, _e_ipc_cb_server_data, NULL);
   
   return 1;
}

static void
_e_ipc_shutdown(void)
{
   if (_e_ipc_server)
     {
	ecore_ipc_server_del(_e_ipc_server);
	_e_ipc_server = NULL;
     }
}

static int
_e_ipc_cb_server_add(void *data, int type, void *event)
{
   Ecore_Ipc_Event_Server_Add *e;
   int argc;
   char **argv;
   int i;
   
   e = event;
   ecore_app_args_get(&argc, &argv);
   for (i = 1; i < argc; i++)
     {
	char *v;
	     
	if ((!strcmp(argv[i], "-load-module")) && (i < (argc - 1)))
	  {
	     i++;
	     v = argv[i];
	     ecore_ipc_server_send(_e_ipc_server,
				   E_IPC_DOMAIN_REQUEST,
				   E_IPC_OP_MODULE_LOAD,
				   0/*ref*/, 0/*ref_to*/, 0/*response*/,
				   v, strlen(v));
	  }
	else if ((!strcmp(argv[i], "-unload-module")) && (i < (argc - 1)))
	  {
	     i++;
	     v = argv[i];
	     ecore_ipc_server_send(_e_ipc_server,
				   E_IPC_DOMAIN_REQUEST,
				   E_IPC_OP_MODULE_UNLOAD,
				   0/*ref*/, 0/*ref_to*/, 0/*response*/,
				   v, strlen(v));
	  }
	else if ((!strcmp(argv[i], "-enable-module")) && (i < (argc - 1)))
	  {
	     i++;
	     v = argv[i];
	     ecore_ipc_server_send(_e_ipc_server,
				   E_IPC_DOMAIN_REQUEST,
				   E_IPC_OP_MODULE_ENABLE,
				   0/*ref*/, 0/*ref_to*/, 0/*response*/,
				   v, strlen(v));
	  }
	else if ((!strcmp(argv[i], "-disable-module")) && (i < (argc - 1)))
	  {
	     i++;
	     v = argv[i];
	     ecore_ipc_server_send(_e_ipc_server,
				   E_IPC_DOMAIN_REQUEST,
				   E_IPC_OP_MODULE_DISABLE,
				   0/*ref*/, 0/*ref_to*/, 0/*response*/,
				   v, strlen(v));
	  }
	else if ((!strcmp(argv[i], "-list-modules")))
	  {
	     reply_expect++;
	     ecore_ipc_server_send(_e_ipc_server,
				   E_IPC_DOMAIN_REQUEST,
				   E_IPC_OP_MODULE_LIST,
				   0/*ref*/, 0/*ref_to*/, 0/*response*/,
				   NULL, 0);
	  }
     }
   if (reply_count >= reply_expect) ecore_main_loop_quit();
   return 1;
}

static int
_e_ipc_cb_server_del(void *data, int type, void *event)
{
   Ecore_Ipc_Event_Server_Del *e;
   
   e = event;
   return 1;
}

static int
_e_ipc_cb_server_data(void *data, int type, void *event)
{
   Ecore_Ipc_Event_Server_Data *e;
   
   e = event;
   printf("REPLY: BEGIN\n");
   switch (e->minor)
     {  
      case E_IPC_OP_MODULE_LIST_REPLY:
	if (e->data)
	  {
	     char *p;
	     
	     p = e->data;
	     while (p < (e->data + e->size))
	       {
		  char *name;
		  char  enabled;
		  
		  name = p;
		  p += strlen(name);
		  if (p < (e->data + e->size))
		    {
		       p++;
		       if (p < (e->data + e->size))
			 {
			    enabled = *p;
			    p++;
			    printf("REPLY: MODULE NAME=\"%s\" ENABLED=%i\n",
				   name, (int)enabled);
			 }
		    }
	       }
	  }
	else
	  printf("REPLY: MODULE NONE\n");
	break;
      default:
	break;
     }
   printf("REPLY: END\n");
   reply_count++;
   if (reply_count >= reply_expect) ecore_main_loop_quit();
   return 1;
}