[dba5ed]: src / engine.c Maximize Restore History

Download this file

engine.c    578 lines (508 with data), 16.8 kB

  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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* Copyright (C) 2003-2006 the xine-project
*
* 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 of the
* License, 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* $Id: engine.c,v 1.36 2006/04/04 22:39:24 dsalt Exp $
*
* init xine engine, set up script engine
*/
#include "globals.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <stdarg.h>
#include <errno.h>
#include "engine.h"
#include "ui.h"
#include "utils.h"
se_t *gse; /* global script engine */
xine_t *xine; /* global xine instance */
pthread_mutex_t engine_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
gboolean initialised = FALSE;
int __lock_depth = -1;
static GtkWidget *se_startup_window, *se_startup_textarea;
static GtkTextBuffer *se_startup_buf;
static GtkTextTag *se_startup_tag;
static gint se_startup_insert;
static char *se_startup_cmds = NULL;
static gboolean modified = FALSE;
GAsyncQueue *js_queue;
static gboolean queue_available = FALSE;
#if GXINE_GTK_OLDER_THAN(2,8,0)
WEAK (void, gtk_about_dialog_set_wrap_license);
#endif
static JSBool
show_about (JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
{
static const gchar *const authors[] = {
"Guenter Bartsch <guenter@users.sourceforge.net>",
"Darren Salt <dsalt@users.sourceforge.net>",
NULL
};
static const gchar *const documenters[] = {
"Darren Salt <dsalt@users.sourceforge.net>",
"Craig Sanders <cas@taz.net.au>",
"Philipp Matthias Hahn <pmhahn@titan.lahn.de>",
NULL
};
static GtkWidget *about = NULL;
se_log_fncall_checkinit ("show_about");
if (!about)
{
GtkWidget *label;
GtkBox *vbox;
/* translators' names go here */
const gchar *translator = _("TRANSLATOR");
gchar *licence = GTK_HAVE (gtk_about_dialog_set_wrap_license, 2, 8, 0)
? _("This program is free software; you can redistribute it and/or "
"modify it under the terms of the GNU General Public Licence as "
"published by the Free Software Foundation; either version 2 of the "
"Licence, or (at your option) any later version.\n"
"\n"
"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 Licence for more details.\n"
"\n"
"You should have received a copy of the GNU General Public Licence "
"along with this program; if not, write to the Free Software "
"Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, "
"MA 02110-1301 USA")
/* Translators: rewrap as required */
: _("This program is free software; you can\n"
"redistribute it and/or modify it under the terms of\n"
"the GNU General Public Licence as published by\n"
"the Free Software Foundation; either version 2 of\n"
"the Licence, or (at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will\n"
"be useful, but WITHOUT ANY WARRANTY; without\n"
"even the implied warranty of MERCHANTABILITY\n"
"or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public Licence for more details.\n"
"\n"
"You should have received a copy of the GNU\n"
"General Public Licence along with this program;\n"
"if not, write to the Free Software Foundation, Inc.,\n"
"51 Franklin St, Fifth Floor, Boston,\n"
"MA 02110-1301 USA");
about = gtk_about_dialog_new ();
g_object_set (G_OBJECT (about),
"name", "gxine",
"version", VERSION,
"copyright", _("�� 2002-2006 the xine project team"),
"comments", _("A GTK media player front end for xine-lib"),
"license", licence,
"website", "http://xinehq.de/",
"authors", authors,
"documenters", documenters,
"translator-credits",
strcmp (translator, "TRANSLATOR") ? translator : NULL,
"logo-icon-name", GXINE_LOGO, NULL);
if (GTK_HAVE (gtk_about_dialog_set_wrap_license, 2, 8, 0))
gtk_about_dialog_set_wrap_license ((GtkAboutDialog *) about, TRUE);
label = gtk_label_new ("<small><i>" VENDOR_PKG_VERSION "</i></small>");
gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
vbox = GTK_BOX (GTK_DIALOG (about)->vbox);
gtk_box_pack_end (vbox, label, FALSE, FALSE, 2);
}
window_show (about, NULL);
*rval = JSVAL_VOID;
return JS_TRUE;
}
static void __attribute__ ((format (printf, 2, 3)))
print_cb (void *user_data, const char *str, ...)
{
if (verbosity)
{
va_list ap;
g_print (_("engine: < "));
va_start (ap, str);
vprintf (str, ap);
va_end (ap);
puts ("");
}
}
int engine_exec_obj (const char *cmd, se_o_t *obj, se_print_cb_t cb,
void *cb_data, se_error_cb_t ecb, const char *src)
{
struct {
JSInt32 i;
JSFloat64 d;
JSBool b;
} num;
char *str;
#ifdef LOG
/* this is probably harmless */
if (!queue_available)
g_warning ("JS execution during startup: %s: %s", src, cmd);
#endif
if (pthread_mutex_lock (&engine_lock))
{
g_critical (_("JS execution attempted while lock already held.\n"
"Try to reproduce this in a debugger (set a breakpoint on g_critical).\n"
"Get a backtrace of all gxine threads and report it!"));
engine_queue_push (cmd, NULL, cb, cb_data, ecb, src);
return 1;
}
if (verbosity)
g_print (_("engine: > executing ���%s���...\n"), cmd);
if (!cb) {
cb = print_cb;
cb_data = NULL;
}
se_eval_ext (gse, cmd, obj, cb, cb_data, ecb, src);
if ( (str = se_result_str (gse)) ) {
str = g_strdup_printf (_("result: %s"), str);
cb (cb_data, "%s", str);
free (str);
}
else if (se_result_double (gse, &num.d))
{
char str[32];
snprintf (str, sizeof (str), _("result: %lf"), num.d);
cb (cb_data, "%s", str);
}
else if (se_result_int (gse, &num.i))
{
char str[32];
snprintf (str, sizeof (str), _("result: %d"), num.i);
cb (cb_data, "%s", str);
}
else if (se_result_bool (gse, &num.b))
{
char str[32];
snprintf (str, sizeof (str), _("result: %s"),
num.b == JS_TRUE ? _("true") : _("false"));
cb (cb_data, "%s", str);
}
else if (JSVAL_IS_NULL (gse->rval))
{
cb (cb_data, _("null"));
}
else
{
/* return 0 iff the result type wasn't recognised */
pthread_mutex_unlock (&engine_lock);
return 0;
}
pthread_mutex_unlock (&engine_lock);
return 1;
}
int engine_exec (const char *cmd, se_print_cb_t cb, void *cb_data,
const char *src)
{
return engine_exec_obj (cmd, NULL, cb, cb_data, se_error_cb, src);
}
int v_engine_exec(const char *command, se_print_cb_t cb, void *cb_data,
const char *src, ...)
{
char *buf;
va_list va;
int ret;
va_start (va, src);
buf = g_strdup_vprintf (command, va);
va_end (va);
ret = engine_exec (buf, cb, cb_data, src);
free (buf);
return ret;
}
int v_engine_exec_obj (const char *command, se_o_t *obj, se_print_cb_t cb,
void *cb_data, se_error_cb_t ecb, const char *src, ...)
{
char *buf;
va_list va;
int ret;
va_start (va, src);
buf = g_strdup_vprintf (command, va);
va_end (va);
ret = engine_exec_obj (buf, obj, cb, cb_data, ecb, src);
free (buf);
return ret;
}
typedef struct {
char *cmd;
se_o_t *obj;
se_print_cb_t cb;
void *cb_data;
se_error_cb_t ecb;
/*const*/ char *src;
} exec_t;
void engine_queue_push (const char *command, se_o_t *obj, se_print_cb_t cb,
void *cb_data, se_error_cb_t ecb, const char *src)
{
exec_t *data = malloc (sizeof (exec_t));
*data = (exec_t){ g_strdup (command), obj, cb, cb_data, ecb, g_strdup (src) };
g_async_queue_push (js_queue, data);
GdkEventClient event;
event.type = GDK_CLIENT_EVENT;
event.window = app->window;
event.send_event = TRUE;
event.message_type = GDK_NONE;
event.data_format = 8;
strcpy (event.data.b, "Javascript");
gdk_event_put ((GdkEvent *) &event);
}
static gboolean js_queue_cb (GtkWidget *widget, GdkEventClient *event,
gpointer data)
{
exec_t *js;
while ((js = g_async_queue_try_pop (js_queue)))
{
/* spin; play_exec gets this & the GDK lock in reverse order */
while (pthread_mutex_trylock (&widgets_update_lock))
sched_yield ();
pthread_mutex_unlock (&widgets_update_lock); /* JS 'play()' needs it */
engine_exec_obj (js->cmd, js->obj, js->cb, js->cb_data, js->ecb, js->src);
free (js->cmd);
free (js->src);
free (js);
}
return TRUE;
}
static char *
load_user_startup_script (void)
{
char *file = g_strconcat (g_get_home_dir (), "/.gxine/startup", NULL);
char *text = read_entire_file (file, NULL);
if (!text)
{
text = strdup ("");
if (errno != ENOENT)
display_error (FROM_GXINE, _("Couldn't load startup script"), "%s: %s",
file, strerror (errno));
}
free (file);
g_strstrip (text); /* strips \n added by read_entire_file() */
return strcat (text, "\n"); /* ... so re-add it */
}
static void
reset_startup_buffer (const char *text)
{
GtkTextIter start, end;
gtk_text_buffer_get_iter_at_offset (se_startup_buf, &start,
se_startup_insert);
gtk_text_buffer_get_end_iter (se_startup_buf, &end);
gtk_text_buffer_delete (se_startup_buf, &start, &end);
gtk_text_buffer_get_iter_at_offset (se_startup_buf, &start,
se_startup_insert);
gtk_text_buffer_insert (se_startup_buf, &start, text, -1);
gtk_text_buffer_set_modified (se_startup_buf, FALSE);
}
static void
se_startup_tag_cb (GtkWidget *widget, GtkStyle *oldstyle, gpointer data)
{
if (se_startup_tag)
g_object_set (se_startup_tag, "foreground-gdk",
&widget->style->text[GTK_STATE_INSENSITIVE], NULL);
}
static void
se_startup_insert_text_cb (GtkTextBuffer *widget, const GtkTextIter *iter,
const gchar *text, gint length, gpointer data)
{
if (gtk_text_iter_get_offset (iter) < se_startup_insert)
g_signal_stop_emission_by_name (widget, "insert-text");
}
static void
se_startup_response_cb (GtkWidget *widget, int response, gpointer data)
{
GtkTextIter start, end;
switch (response)
{
case 1:
{
char *file = g_strconcat (g_get_home_dir (), "/.gxine/startup", NULL);
char *tmp = read_entire_file (file, NULL);
if (!tmp && errno != ENOENT)
display_error (FROM_GXINE, _("Couldn't load startup script"), "%s: %s",
file, strerror (errno));
else
modified = FALSE;
free (file);
reset_startup_buffer (tmp ? : "");
free (tmp);
}
gtk_widget_grab_focus (se_startup_textarea);
break;
case GTK_RESPONSE_REJECT:
reset_startup_buffer (se_startup_cmds);
gtk_widget_grab_focus (se_startup_textarea);
break;
case GTK_RESPONSE_OK:
free (se_startup_cmds);
gtk_text_buffer_get_iter_at_offset (se_startup_buf, &start,
se_startup_insert);
gtk_text_buffer_get_end_iter (se_startup_buf, &end);
se_startup_cmds = gtk_text_buffer_get_text (se_startup_buf, &start, &end,
FALSE);
g_strstrip (se_startup_cmds);
modified |= gtk_text_buffer_get_modified (se_startup_buf);
default:
gtk_widget_hide (se_startup_window);
}
}
static JSBool
show_startup (JSContext *cx, JSObject *obj, uintN argc,
jsval *argv, jsval *rval)
{
if (!GTK_WIDGET_VISIBLE (se_startup_window))
reset_startup_buffer (se_startup_cmds);
window_show (se_startup_window, NULL);
return JS_TRUE;
}
void engine_init (void)
{
char *cfgfilename;
GtkWidget *w;
/*
* init the global xine engine
*/
xine = xine_new ();
cfgfilename = g_strconcat (g_get_home_dir(), "/.gxine/config", NULL);
xine_config_load (xine, cfgfilename);
free (cfgfilename);
xine_engine_set_param (xine, XINE_ENGINE_PARAM_VERBOSITY, verbosity);
xine_init (xine);
ui_preferences_register (xine);
/* init the javascript engine */
gse = se_new ();
static const se_f_def_t defs[] = {
{ "about_show", show_about, 0, 0, SE_GROUP_DIALOGUE, NULL, NULL },
{ "startup_cmds_show", show_startup, 0, 0, SE_GROUP_DIALOGUE, NULL, NULL },
{ NULL }
};
se_defuns (gse, gse->g, defs);
/*
* create the startup commands editor window
*/
se_startup_window = gtk_dialog_new_with_buttons (_("Startup script"), NULL, 0,
GTK_STOCK_REVERT_TO_SAVED, 1,
GTK_STOCK_UNDO, GTK_RESPONSE_REJECT,
GTK_STOCK_CANCEL, GTK_RESPONSE_DELETE_EVENT,
GTK_STOCK_OK, GTK_RESPONSE_OK,
NULL);
gtk_window_set_default_size (GTK_WINDOW (se_startup_window), 400, 250);
gtk_dialog_set_default_response (GTK_DIALOG (se_startup_window),
GTK_RESPONSE_OK);
ui_add_undo_response (se_startup_window, NULL);
g_object_connect (se_startup_window,
"signal::delete-event", G_CALLBACK (gtk_widget_hide_on_delete), NULL,
"signal::response", G_CALLBACK (se_startup_response_cb), NULL,
NULL);
w = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (w),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
se_startup_buf = gtk_text_buffer_new (NULL);
se_startup_textarea = gtk_text_view_new_with_buffer (se_startup_buf);
gtk_widget_set_name (se_startup_textarea, "gxine_js_edit");
gtk_container_add (GTK_CONTAINER (w), se_startup_textarea);
gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (se_startup_window)->vbox),
w);
g_signal_connect (se_startup_textarea, "style-set",
G_CALLBACK (se_startup_tag_cb), NULL);
g_signal_connect (se_startup_buf, "insert-text",
G_CALLBACK (se_startup_insert_text_cb), NULL);
js_queue = g_async_queue_new ();
g_async_queue_ref (js_queue);
}
static gboolean
engine_startup_cb (gpointer data)
{
/* note: order of actions is significant */
queue_available = TRUE;
g_signal_connect (G_OBJECT (app), "client-event",
G_CALLBACK (js_queue_cb), NULL);
js_queue_cb (NULL, NULL, NULL);
return FALSE;
}
void
engine_startup_script (void)
{
gchar *file, *name = g_strconcat (confdir, "/startup", NULL);
GtkTextIter pos;
file = read_entire_file (name, NULL);
if (!file && errno != ENOENT)
display_error (FROM_GXINE, _("Couldn't load startup script"), "%s: %s",
name, strerror (errno));
else if (file)
{
se_eval_ext (gse, file, NULL, NULL, NULL, se_error_cb,
_("Startup script (system)"));
se_startup_tag = gtk_text_buffer_create_tag (se_startup_buf, "global",
"editable", FALSE, "foreground-gdk",
&se_startup_textarea->style->text[GTK_STATE_INSENSITIVE],
"scale", 0.9, NULL);
gtk_text_buffer_get_start_iter (se_startup_buf, &pos);
gtk_text_buffer_insert_with_tags (se_startup_buf, &pos, file, -1,
se_startup_tag, NULL);
}
free (name);
free (file);
gtk_text_buffer_get_end_iter (se_startup_buf, &pos);
se_startup_insert = gtk_text_buffer_get_char_count (se_startup_buf);
se_startup_cmds = load_user_startup_script ();
se_eval (gse, se_startup_cmds, NULL, NULL, NULL, _("Startup script"));
gtk_init_add ((GtkFunction) engine_startup_cb, NULL);
}
void
save_startup_script (void)
{
/* only save if the buffer is modified */
if (!modified)
return;
char *fname = g_strconcat (g_get_home_dir(), "/.gxine/startup", NULL);
FILE *f = open_write (fname, _("Failed to save startup script"));
if (f)
{
fputs (se_startup_cmds, f);
close_write (fname, f, _("Failed to save startup script"));
}
free (fname);
}
#ifdef LOCK_DEBUG
#include <assert.h>
static __thread int m_depth = 0;
#include <sys/syscall.h>
_syscall0 (pid_t, gettid);
int gxine_mutex_lock (pthread_mutex_t *mutex, const char *fn, int line)
{
int l = m_depth++;
g_printerr ("%*s lock %5d: %s %d\n", l + 3, "pth", gettid (), fn, line);
return (pthread_mutex_lock) (mutex);
}
int gxine_mutex_unlock (pthread_mutex_t *mutex, const char *fn, int line)
{
int l = --m_depth;
assert (l >= 0);
g_printerr ("%*s unlock %5d: %s %d\n", l + 3, "pth", gettid (), fn, line);
return (pthread_mutex_unlock) (mutex);
}
void gxine_threads_enter (const char *fn, int line)
{
int l = m_depth++;
g_printerr ("%*s lock %5d: %s %d\n", l + 3, "gdk", gettid (), fn, line);
(gdk_threads_enter) ();
}
void gxine_threads_leave (const char *fn, int line)
{
int l = --m_depth;
assert (l >= 0);
g_printerr ("%*s unlock %5d: %s %d\n", l + 3, "gdk", gettid (), fn, line);
(gdk_threads_leave) ();
}
#endif