summaryrefslogtreecommitdiff
path: root/src/ScriptingPlugin.cc
blob: 214af263aa820be785ad72df0433fc185a7367bd (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
/*
 * vdr-scripting - A plugin for the Linux Video Disk Recorder
 * Copyright (c) 2009 Tobias Grimm <vdr@e-tobi.net>
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <ruby.h>

#include "ScriptingPlugin.h"
#include <vdr/tools.h>
#include <iostream>
#include "swigrubyrun.h"
#include "RubyObjectTracker.h"

using namespace std;

extern "C" void Init_swig();
extern RubyObjectTracker rubyObjectTracker;

VALUE require_pluginmanager_wrap(VALUE arg)
{
    return rb_require("vdr/pluginmanager");
}

VALUE new_pluginmanager_wrap(VALUE arg)
{
    VALUE module = rb_const_get(rb_cObject, rb_intern("Vdr"));
    VALUE klass = rb_const_get(module, rb_intern("PluginManager"));
    return rb_class_new_instance(0 ,0, klass);
}

ScriptingPlugin::ScriptingPlugin()
{
    // Initialize any member variables here.
    // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
    // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
}

ScriptingPlugin::~ScriptingPlugin()
{
      ruby_finalize();
}

const char *ScriptingPlugin::CommandLineHelp()
{
    // Return a string that describes all known command line options.
    return NULL;
}

bool ScriptingPlugin::ProcessArgs(int argc, char *argv[])
{
    // Implement command line argument processing here if applicable.
    return true;
}

bool ScriptingPlugin::Initialize()
{
    // Initialize any background activities the plugin shall perform.

    const char* configDir = cPlugin::ConfigDirectory(Name());

    RUBY_INIT_STACK;
    ruby_init();
    ruby_init_loadpath();
    ruby_incpush(cString::sprintf("%s/lib", configDir));
    ruby_script("vdr-plugin-ruby");
    Init_swig();

    rb_eval_string("STDERR.puts $LOAD_PATH");

    int error;
    VALUE result = rb_protect(require_pluginmanager_wrap, 0, &error);
    if (IsRubyError(error))
    {
        return true;
    }

    //rb_require("vdr/pluginmanager");

    VALUE rbPluginManager = rb_protect(new_pluginmanager_wrap, 0, &error);
    if (IsRubyError(error))
    {
        return true;
    }

    if (rbPluginManager == Qnil)
    {
        cerr << "rbPluginManger is nil" << endl;
    }

    Data_Get_Struct(rbPluginManager, PluginManager, _pluginManager);

    if (!_pluginManager)
    {
        cerr << "no plugin manager" << endl;
    }

    return true;
}

bool ScriptingPlugin::Start()
{
    // Start any background activities the plugin shall perform.
    return true;
}

void ScriptingPlugin::Stop()
{
    // Stop any background activities the plugin is performing.
}

void ScriptingPlugin::Housekeeping()
{
    VALUE gc_wrap(VALUE arg);
    // Perform any cleanup or other regular tasks.
}

void ScriptingPlugin::MainThreadHook()
{
    // Perform actions in the context of the main program thread.
    // WARNING: Use with great care - see PLUGINS.html!
}

cString ScriptingPlugin::Active()
{
    // Return a message string if shutdown should be postponed
    return NULL;
}

time_t ScriptingPlugin::WakeupTime()
{
    // Return custom wakeup time for shutdown script
    return 0;
}

cOsdObject *ScriptingPlugin::MainMenuAction()
{
    if (!_pluginManager)
    {
        cerr << "NO PLUGINMANAGER" << endl;
        return NULL;
    }

    cOsdObject* osdObject = _pluginManager->PluginMenu();

    if (!osdObject)
    {
        cerr << "NO MENU" << endl;
        return NULL;
    }

    rubyObjectTracker.KeepRubyObjectAlive(osdObject);
    return osdObject;
}

cMenuSetupPage *ScriptingPlugin::SetupMenu()
{
    // Return a setup menu in case the plugin supports one.
    return NULL;
}

bool ScriptingPlugin::SetupParse(const char *Name, const char *Value)
{
    // Parse your own setup parameters and store their values.
    return false;
}

bool ScriptingPlugin::Service(const char *Id, void *Data)
{
    // Handle custom service requests from other plugins
    return false;
}

const char **ScriptingPlugin::SVDRPHelpPages()
{
    // Return help text for SVDRP commands this plugin implements
    return NULL;
}

cString ScriptingPlugin::SVDRPCommand(const char *Command, const char *Option, int &ReplyCode)
{
    // Process SVDRP commands this plugin implements
    return NULL;
}


VALUE gc_wrap(VALUE arg)
{
    rb_gc();
    return Qnil;
}

void ScriptingPlugin::ForceGarbageCollect()
{
    int status;
    rb_protect(gc_wrap, 0, &status);
//    IsRubyError(status);
}


bool ScriptingPlugin::IsRubyError(int error)
{
    if (error)
    {
        VALUE lasterr = rb_gv_get("$!");

        VALUE klass = rb_class_path(CLASS_OF(lasterr));
        cerr << "class = " << RSTRING(klass)->ptr << endl;

        VALUE message = rb_obj_as_string(lasterr);
        cerr << "message = " << RSTRING(message)->ptr << endl;

        return true;
    }
    else
    {
        return false;
    }
}

VDRPLUGINCREATOR(ScriptingPlugin); // Don't touch this!