summaryrefslogtreecommitdiff
path: root/src/plugins/splash/script/script-lib-plymouth.c
blob: 6cb1a25d100085e6131e0ef0e19619b8b12fe568 (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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* script-lib-plymouth.c - script library for interacting with plymouth
 *
 * Copyright (C) 2009 Charlie Brej <cbrej@cs.man.ac.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written by: Charlie Brej <cbrej@cs.man.ac.uk>
 */

#include "config.h"

#include "ply-boot-splash-plugin.h"
#include "ply-utils.h"
#include "script.h"
#include "script-parse.h"
#include "script-execute.h"
#include "script-object.h"
#include "script-lib-plymouth.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "script-lib-plymouth.script.h"

static script_return_t plymouth_set_function (script_state_t *state,
                                              void           *user_data)
{
  script_obj_t **script_func = user_data;
  script_obj_t *obj = script_obj_hash_get_element (state->local, "function");

  script_obj_deref (&obj);
  script_obj_unref (*script_func);
  *script_func = obj;
  return script_return_obj_null ();
}

static script_return_t plymouth_get_mode (script_state_t *state,
                                          void           *user_data)
{
  script_lib_plymouth_data_t *data = user_data;
  script_obj_t *obj;
  switch (data->mode)
    {
      case PLY_BOOT_SPLASH_MODE_BOOT_UP:
        obj = script_obj_new_string ("boot");
        break;
      case PLY_BOOT_SPLASH_MODE_SHUTDOWN:
        obj = script_obj_new_string ("shutdown");
        break;
      case PLY_BOOT_SPLASH_MODE_UPDATES:
        obj = script_obj_new_string ("updates");
        break;
      case PLY_BOOT_SPLASH_MODE_INVALID:
      default:
        obj = script_obj_new_string ("unknown");
        break;
    }
  return script_return_obj (obj);
}

script_lib_plymouth_data_t *script_lib_plymouth_setup (script_state_t         *state,
                                                       ply_boot_splash_mode_t  mode)
{
  script_lib_plymouth_data_t *data = malloc (sizeof (script_lib_plymouth_data_t));

  data->script_refresh_func = script_obj_new_null ();
  data->script_boot_progress_func = script_obj_new_null ();
  data->script_root_mounted_func = script_obj_new_null ();
  data->script_keyboard_input_func = script_obj_new_null ();
  data->script_register_operation_func = script_obj_new_null ();
  data->script_unregister_operation_func = script_obj_new_null ();
  data->script_update_status_func = script_obj_new_null ();
  data->script_display_normal_func = script_obj_new_null ();
  data->script_display_password_func = script_obj_new_null ();
  data->script_display_question_func = script_obj_new_null ();
  data->script_display_message_func = script_obj_new_null ();
  data->script_hide_message_func = script_obj_new_null ();
  data->script_quit_func = script_obj_new_null ();
  data->mode = mode;
  
  script_obj_t *plymouth_hash = script_obj_hash_get_element (state->global, "Plymouth");
  script_add_native_function (plymouth_hash,
                              "SetRefreshFunction",
                              plymouth_set_function,
                              &data->script_refresh_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetBootProgressFunction",
                              plymouth_set_function,
                              &data->script_boot_progress_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetRootMountedFunction",
                              plymouth_set_function,
                              &data->script_root_mounted_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetKeyboardInputFunction",
                              plymouth_set_function,
                              &data->script_keyboard_input_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetRegisterOperationFunction",
                              plymouth_set_function,
                              &data->script_register_operation_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetUnregisterOperationFunction",
                              plymouth_set_function,
                              &data->script_unregister_operation_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetUpdateStatusFunction",
                              plymouth_set_function,
                              &data->script_update_status_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetDisplayNormalFunction",
                              plymouth_set_function,
                              &data->script_display_normal_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetDisplayPasswordFunction",
                              plymouth_set_function,
                              &data->script_display_password_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetDisplayQuestionFunction",
                              plymouth_set_function,
                              &data->script_display_question_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetDisplayMessageFunction",
                              plymouth_set_function,
                              &data->script_display_message_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetHideMessageFunction",
                              plymouth_set_function,
                              &data->script_hide_message_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "SetQuitFunction",
                              plymouth_set_function,
                              &data->script_quit_func,
                              "function",
                              NULL);
  script_add_native_function (plymouth_hash,
                              "GetMode",
                              plymouth_get_mode,
                              data,
                              NULL);
  script_obj_unref (plymouth_hash);

  data->script_main_op = script_parse_string (script_lib_plymouth_string, "script-lib-plymouth.script");
  script_return_t ret = script_execute (state, data->script_main_op);
  script_obj_unref (ret.object);                /* Throw anything sent back away */

  return data;
}

void script_lib_plymouth_destroy (script_lib_plymouth_data_t *data)
{
  script_parse_op_free (data->script_main_op);
  script_obj_unref (data->script_refresh_func);
  script_obj_unref (data->script_boot_progress_func);
  script_obj_unref (data->script_root_mounted_func);
  script_obj_unref (data->script_keyboard_input_func);
  script_obj_unref (data->script_register_operation_func);
  script_obj_unref (data->script_unregister_operation_func);
  script_obj_unref (data->script_update_status_func);
  script_obj_unref (data->script_display_normal_func);
  script_obj_unref (data->script_display_password_func);
  script_obj_unref (data->script_display_question_func);
  script_obj_unref (data->script_display_message_func);
  script_obj_unref (data->script_hide_message_func);
  script_obj_unref (data->script_quit_func);
  free (data);
}

void script_lib_plymouth_on_refresh (script_state_t             *state,
                                     script_lib_plymouth_data_t *data)
{
    script_return_t ret = script_execute_object (state,
                                                 data->script_refresh_func,
                                                 NULL,
                                                 NULL);
    script_obj_unref (ret.object);
}

void script_lib_plymouth_on_boot_progress (script_state_t             *state,
                                           script_lib_plymouth_data_t *data,
                                           double                     duration,
                                           double                     progress)
{
  script_obj_t *duration_obj = script_obj_new_number (duration);
  script_obj_t *progress_obj = script_obj_new_number (progress);
  script_return_t ret = script_execute_object (state,
                                               data->script_boot_progress_func,
                                               NULL,
                                               duration_obj,
                                               progress_obj,
                                               NULL);
  script_obj_unref (ret.object);
  script_obj_unref (duration_obj);
  script_obj_unref (progress_obj);
}

void script_lib_plymouth_on_root_mounted (script_state_t             *state,
                                          script_lib_plymouth_data_t *data)
{
  script_return_t ret = script_execute_object (state,
                                               data->script_root_mounted_func,
                                               NULL,
                                               NULL);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_keyboard_input (script_state_t             *state,
                                            script_lib_plymouth_data_t *data,
                                            const char                 *keyboard_input)
{
  script_obj_t *keyboard_input_obj = script_obj_new_string (keyboard_input);
  script_return_t ret = script_execute_object (state,
                                                 data->script_keyboard_input_func,
                                                 NULL,
                                                 keyboard_input_obj,
                                                 NULL);
  script_obj_unref (keyboard_input_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_register_operation (script_state_t             *state,
                                                script_lib_plymouth_data_t *data,
                                                const char                 *operation_id,
                                                const char                 *name)
{
  script_obj_t *new_name_obj = script_obj_new_string (name);
  script_obj_t *new_operation_id_obj = script_obj_new_string (operation_id);
  script_return_t ret = script_execute_object (state,
                                               data->script_register_operation_func,
                                               NULL,
                                               new_operation_id_obj,
                                               new_name_obj,
                                               NULL);
  script_obj_unref (new_name_obj);
  script_obj_unref (new_operation_id_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_unregister_operation (script_state_t             *state,
                                                  script_lib_plymouth_data_t *data,
                                                  const char                 *operation_id)
{
  script_obj_t *new_operation_id_obj = script_obj_new_string (operation_id);
  script_return_t ret = script_execute_object (state,
                                               data->script_unregister_operation_func,
                                               NULL,
                                               new_operation_id_obj,
                                               NULL);
  script_obj_unref (new_operation_id_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_update_status (script_state_t             *state,
                                           script_lib_plymouth_data_t *data,
                                           const char                 *new_status,
                                           const char                 *operation_id)
{
  script_obj_t *new_status_obj = script_obj_new_string (new_status);
  script_obj_t *new_operation_id_obj = script_obj_new_string (operation_id);
  script_return_t ret = script_execute_object (state,
                                               data->script_update_status_func,
                                               NULL,
                                               new_status_obj,
                                               new_operation_id_obj,
                                               NULL);
  script_obj_unref (new_status_obj);
  script_obj_unref (new_operation_id_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_display_normal (script_state_t             *state,
                                            script_lib_plymouth_data_t *data)
{
  script_return_t ret = script_execute_object (state,
                                               data->script_display_normal_func,
                                               NULL,
                                               NULL);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_display_password (script_state_t             *state,
                                              script_lib_plymouth_data_t *data,
                                              const char                 *prompt,
                                              int                         bullets)
{
  script_obj_t *prompt_obj = script_obj_new_string (prompt);
  script_obj_t *bullets_obj = script_obj_new_number (bullets);
  script_return_t ret = script_execute_object (state,
                                               data->script_display_password_func,
                                               NULL,
                                               prompt_obj,
                                               bullets_obj,
                                               NULL);
  script_obj_unref (prompt_obj);
  script_obj_unref (bullets_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_display_question (script_state_t             *state,
                                              script_lib_plymouth_data_t *data,
                                              const char                 *prompt,
                                              const char                 *entry_text)
{
  script_obj_t *prompt_obj = script_obj_new_string (prompt);
  script_obj_t *entry_text_obj = script_obj_new_string (entry_text);
  script_return_t ret = script_execute_object (state,
                                               data->script_display_question_func,
                                               NULL,
                                               prompt_obj,
                                               entry_text_obj,
                                               NULL);
  script_obj_unref (prompt_obj);
  script_obj_unref (entry_text_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_display_message (script_state_t             *state,
                                             script_lib_plymouth_data_t *data,
                                             const char                 *message)
{
  script_obj_t *new_message_obj = script_obj_new_string (message);
  script_return_t ret = script_execute_object (state,
                                               data->script_display_message_func,
                                               NULL,
                                               new_message_obj,
                                               NULL);
  script_obj_unref (new_message_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_hide_message (script_state_t             *state,
                                          script_lib_plymouth_data_t *data,
                                          const char                 *message)
{
  script_obj_t *new_message_obj = script_obj_new_string (message);
  script_return_t ret = script_execute_object (state,
                                               data->script_hide_message_func,
                                               NULL,
                                               new_message_obj,
                                               NULL);
  script_obj_unref (new_message_obj);
  script_obj_unref (ret.object);
}

void script_lib_plymouth_on_quit (script_state_t             *state,
                                  script_lib_plymouth_data_t *data)
{
  script_return_t ret = script_execute_object (state,
                                               data->script_quit_func,
                                               NULL,
                                               NULL);
  script_obj_unref (ret.object);
}