aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/e_start_quicklaunch_impl.c
blob: 12663b008ad6f19bf0d949bcddf90d83c7f96e57 (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
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
#include <Ecore_Wl2.h>

extern int e_pid;
static Ecore_Event_Handler *ql_exe_data;
static Ecore_Event_Handler *ql_exe_del;

static Eina_Inlist *instances;

typedef struct Instance
{
   EINA_INLIST;
   char **argv;
   char *cwd;
   int argc;
   int last;
   Ecore_Exe_Flags flags;
   Ecore_Exe *exe;
} Instance;

typedef struct Exe
{
   int pid;
   struct wl_resource *res;
} Exe;

__attribute__((visibility("hidden"))) void post_fork();

static const struct quicklaunch_exe_interface ql_exe_instance_interface =
{
};

static void
set_cwd(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, const char *cwd)
{
   Instance *inst = wl_resource_get_user_data(resource);
   inst->cwd = eina_strdup(cwd);
}

static void
set_flags(struct wl_client *client EINA_UNUSED, struct wl_resource *resource, uint32_t flags)
{
   Instance *inst = wl_resource_get_user_data(resource);
   inst->flags = flags;
}

static void
ql_instance_run(struct wl_client *client, struct wl_resource *resource, uint32_t id, const char *env, uint32_t envc, struct wl_array *envoffsets)
{
   struct wl_resource *res;
   Instance *inst = wl_resource_get_user_data(resource);
   Exe *exe;

   res = wl_resource_create(client, &quicklaunch_exe_interface, 1, id);
   if (!res)
     {
        wl_client_post_no_memory(client);
        return;
     }
   exe = calloc(1, sizeof(Exe));
   wl_resource_set_implementation(res, &ql_exe_instance_interface, exe, ql_exe_instance_destroy);
   if (!efl_quicklaunch_prepare(inst->argc, inst->argv, inst->cwd))
     if (!elm_quicklaunch_prepare(inst->argc, inst->argv, inst->cwd))
       {
          wl_resource_destroy(res);
          return;
       }

   exe->pid = elm_quicklaunch_fork(e_argc, e_args, e_cwd, post_fork, NULL);
   if (exe->pid == -1)
     {
        wl_resource_destroy(res);
        return;
     }
   elm_quicklaunch_cleanup();
}

static void
ql_instance_destroy(struct wl_client *client, struct wl_resource *resource)
{
   wl_resource_destroy(resource);
}

static const struct quicklaunch_instance_interface ql_instance_interface =
{
   ql_instance_set_flags,
   ql_instance_run,
   ql_instance_destroy,
};

static void
ql_instance_destroy(struct wl_resource *res)
{
   Instance *inst = wl_resource_get_user_data(res);
   char **argv;
   
   for (argv = inst->argv; *argv; argv++)
     free(*argv);
   free(inst->argv);
   ecore_exe_free(inst->exe);
   instances = eina_inlist_remove(instances, EINA_INLIST_GET(inst));
   free(inst);
}

static void
ql_instance_create(struct wl_client *client, struct wl_resource *resource, uint32_t id, const char *cmd)
{
   Instance *inst;
   Eina_Strbuf *buf;
   struct wl_resource *res;

   res = wl_resource_create(client, &quicklaunch_instance_interface, 1, id);
   if (!res)
     {
        wl_client_post_no_memory(client);
        return;
     }
   inst = calloc(1, sizeof(Instance));
   inst->argc = -1;
   instances = eina_inlist_append(instances, EINA_INLIST_GET(inst));
   buf = eina_strbuf_new();
   eina_strbuf_append_printf(buf, "e_ql_helper %s", cmd)
   inst->exe = ecore_exe_pipe_run(eina_strbuf_string_get(buf), ECORE_EXE_USE_SH | ECORE_EXE_NOT_LEADER | ECORE_EXE_PIPE_READ_LINE_BUFFERED, inst);
   wl_resource_set_implementation(res, &ql_instance_interface, inst, ql_instance_destroy);
   eina_strbuf_free(buf);
}

static const struct quicklaunch_interface ql_interface =
{
   ql_instance_create
};

static void
ql_impl_bind(struct wl_client *client, void *data EINA_UNUSED, uint32_t version, uint32_t id)
{
   struct wl_resource *res;
   pid_t pid;

   wl_client_get_credentials(client, &pid, NULL, NULL);
   if (pid != e_pid)
     {
        wl_client_post_no_memory(client);
        return;
     }
   res = wl_resource_create(client, &quicklaunch_interface, version, id);
   if (!res)
     {
        wl_client_post_no_memory(client);
        return;
     }

   wl_resource_set_implementation(res, &ql_interface, NULL, NULL);
}

static Eina_Bool
ql_data(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Exe_Event_Data *ev)
{
   Instance *inst;

   EINA_INLIST_FOREACH(instances, inst)
     {
        int i;

        if (inst->exe != ev->exe) continue;
        for (i = 0; ev->lines[i].line; i++)
          {
            if ((!i) && (inst->argc == -1))
              {
                 inst->argc = strtol(ev->lines[i].line, NULL, 10);
                 inst->argv = calloc(argc + 1, sizeof(char*));
              }
            else
              inst->argv[inst->last++] = strdup(ev->lines[i].line);
          }
        break;
     }
   return ECORE_CALLBACK_RENEW;
}

static Eina_Bool
ql_del(void *d EINA_UNUSED, int t EINA_UNUSED, Ecore_Exe_Event_Del *ev)
{
   EINA_INLIST_FOREACH(instances, inst)
     {
        if (inst->exe != ev->exe) continue;
        ecore_exe_free(inst->exe);
        inst->exe = NULL;
        break;
     }
   return ECORE_CALLBACK_RENEW;
}

__attribute__((visibility("hidden"))) void
e_start_ql_postfork(void)
{
   ecore_event_handler_del(ql_exe_data);
   ecore_event_handler_del(ql_exe_del);
}

__attribute__((visibility("hidden"))) void
e_start_ql_impl_init(Ecore_Wl2_Display *disp)
{
   struct wl_display *d = ecore_wl2_display_get(disp);
   
   wl_global_create(d, &quicklaunch_interface, 1, NULL, ql_impl_bind);
   ql_exe_data = ecore_event_handler_add(ECORE_EXE_EVENT_DATA, (Ecore_Event_Handler_Cb)ql_data, NULL);
   ql_exe_del = ecore_event_handler_add(ECORE_EXE_EVENT_DEL, (Ecore_Event_Handler_Cb)ql_del, NULL);
}