VLCExtensionsManager.m 9.93 KB
Newer Older
1
/*****************************************************************************
2
 * VLCExtensionsManager.m: Extensions manager for Cocoa
3 4 5 6
 ****************************************************************************
 * Copyright (C) 2009-2012 VideoLAN and authors
 * $Id$
 *
7
 * Authors: Brendon Justin <brendonjustin@gmail.com>,
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *          Jean-Philippe André < jpeg # videolan.org >
 *
 * 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.
 *****************************************************************************/

25
#import "VLCExtensionsManager.h"
26

27
#import "VLCExtensionsDialogProvider.h"
28 29 30 31

#import <vlc_modules.h>
#import "assert.h"

32 33 34
#define MENU_MAP(a,e) ((uint32_t)((((uint16_t)a) << 16) | ((uint16_t)e)))
#define MENU_GET_ACTION(a) ((uint16_t)(((uint32_t)a) >> 16))
#define MENU_GET_EXTENSION(a) ((uint16_t)(((uint32_t)a) & 0xFFFF))
35

36
@interface VLCExtensionsManager()
37 38
{
    extensions_manager_t *p_extensions_manager;
39
    VLCExtensionsDialogProvider *_extensionDialogProvider;
40 41 42 43 44 45 46

    NSMutableDictionary *p_extDict;

    BOOL b_failed; ///< Flag set to true if we could not load the module
}
@end

47
@implementation VLCExtensionsManager
48

49
- (id)init
50
{
51 52 53
    self = [super init];

    if (self) {
54
        p_extensions_manager = NULL;
55
        _extensionDialogProvider = [[VLCExtensionsDialogProvider alloc] init];
56 57 58

        p_extDict = [[NSMutableDictionary alloc] init];

59
        _isUnloading = false;
60 61 62 63 64 65 66 67
        b_failed = false;
    }

    return self;
}

- (void)buildMenu:(NSMenu *)extMenu
{
68
    assert(extMenu != nil);
69 70 71 72 73 74 75

    /* check if stuff was loaded, which can fail */
    if (![self isLoaded]) {
        BOOL b_ret = [self loadExtensions];
        if (b_ret == NO)
            return;
    }
76
    intf_thread_t *p_intf = getIntf();
77

78
    vlc_mutex_lock(&p_extensions_manager->lock);
79 80 81

    extension_t *p_ext = NULL;
    int i_ext = 0;
82

83 84
    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
        bool b_Active = extension_IsActivated(p_extensions_manager, p_ext);
85

86
        NSString *titleString = toNSStr(p_ext->psz_title);
87

88
        if (b_Active && extension_HasMenu(p_extensions_manager, p_ext)) {
89 90 91 92 93 94 95 96 97 98 99
            NSMenu *submenu = [[NSMenu alloc] initWithTitle:titleString];
            NSMenuItem *submenuItem = [extMenu addItemWithTitle:titleString
                                                         action:nil
                                                  keyEquivalent:@""];

            [extMenu setSubmenu:submenu forItem:submenuItem];

            char **ppsz_titles = NULL;
            uint16_t *pi_ids = NULL;
            size_t i_num = 0;

100
            if (extension_GetMenu(p_extensions_manager, p_ext, &ppsz_titles, &pi_ids) == VLC_SUCCESS) {
101
                for (int i = 0; ppsz_titles[i] != NULL; ++i) {
102
                    ++i_num;
103
                    titleString = toNSStr(ppsz_titles[i]);
104 105 106 107 108 109
                    NSMenuItem *menuItem = [submenu addItemWithTitle:titleString
                                                              action:@selector(triggerMenu:)
                                                       keyEquivalent:@""];
                    [menuItem setTarget:self];
                    menuItem.tag = MENU_MAP(pi_ids[i], i_ext);

110
                    free(ppsz_titles[i]);
111
                }
112
                if (!i_num) {
113 114 115 116 117
                    NSMenuItem *menuItem = [submenu addItemWithTitle:@"Empty"
                                                              action:@selector(triggerMenu:)
                                                       keyEquivalent:@""];
                    [menuItem setEnabled:NO];
                }
118 119 120 121 122
                free(ppsz_titles);
                free(pi_ids);
            } else {
                msg_Warn(p_intf, "Could not get menu for extension '%s'",
                          p_ext->psz_title);
123 124 125 126 127 128 129 130
                NSMenuItem *menuItem = [submenu addItemWithTitle:@"Empty"
                                                          action:@selector(triggerMenu:)
                                                   keyEquivalent:@""];
                [menuItem setEnabled:NO];
            }

            [submenu addItem:[NSMenuItem separatorItem]];

131 132
            NSMenuItem *deactivateItem = [submenu addItemWithTitle:@"Deactivate"
                                                            action:@selector(triggerMenu:)
133 134 135 136 137 138 139 140 141 142 143
                                                     keyEquivalent:@""];
            [deactivateItem setTarget:self];
            deactivateItem.tag = MENU_MAP(0, i_ext);
        }
        else
        {
            NSMenuItem *menuItem = [extMenu addItemWithTitle:titleString
                                                     action:@selector(triggerMenu:)
                                              keyEquivalent:@""];
            [menuItem setTarget:self];

144 145
            if (!extension_TriggerOnly(p_extensions_manager, p_ext)) {
                if (b_Active)
146 147 148 149 150 151 152 153
                    [menuItem setState:NSOnState];
            }
            menuItem.tag = MENU_MAP(0, i_ext);
        }
        i_ext++;
    }
    FOREACH_END()

154
    vlc_mutex_unlock(&p_extensions_manager->lock);
155 156 157 158
}

- (BOOL)loadExtensions
{
159
    intf_thread_t *p_intf = getIntf();
160
    if (!p_extensions_manager) {
161
        p_extensions_manager = (extensions_manager_t*)vlc_object_create(p_intf, sizeof(extensions_manager_t));
162
        if (!p_extensions_manager) {
163 164 165 166
            b_failed = true;
            return false;
        }

167
        p_extensions_manager->p_module = module_need(p_extensions_manager, "extension", NULL, false);
168

169 170 171
        if (!p_extensions_manager->p_module) {
            msg_Err(p_intf, "Unable to load extensions module");
            vlc_object_release(p_extensions_manager);
172 173 174 175 176
            p_extensions_manager = NULL;
            b_failed = true;
            return false;
        }

177
        _isUnloading = false;
178 179 180 181 182 183 184
    }
    b_failed = false;
    return true;
}

- (void)unloadExtensions
{
185
    if (!p_extensions_manager)
186
        return;
187
    _isUnloading = true;
188

189 190
    module_unneed(p_extensions_manager, p_extensions_manager->p_module);
    vlc_object_release(p_extensions_manager);
191 192 193 194 195 196 197 198 199 200 201
    p_extensions_manager = NULL;
}

- (void)reloadExtensions
{
    [self unloadExtensions];
    [self loadExtensions];
}

- (void)triggerMenu:(id)sender
{
202
    intf_thread_t *p_intf = getIntf();
203 204
    uint32_t identifier = [(NSMenuItem *)sender tag];

205 206
    uint16_t i_ext = MENU_GET_EXTENSION(identifier);
    uint16_t i_action = MENU_GET_ACTION(identifier);
207

208
    vlc_mutex_lock(&p_extensions_manager->lock);
209

210 211 212
    if ((int) i_ext > p_extensions_manager->extensions.i_size) {
        msg_Dbg(p_intf, "can't trigger extension with wrong id %d",
                 (int) i_ext);
213 214 215
        return;
    }

216 217
    extension_t *p_ext = ARRAY_VAL(p_extensions_manager->extensions, i_ext);
    assert(p_ext != NULL);
218

219
    vlc_mutex_unlock(&p_extensions_manager->lock);
220

221 222 223
    if (i_action == 0) {
        msg_Dbg(p_intf, "activating or triggering extension '%s', id %d",
                 p_ext->psz_title, i_ext);
224

225 226 227 228 229
        if (extension_TriggerOnly(p_extensions_manager, p_ext)) {
            extension_Trigger(p_extensions_manager, p_ext);
        } else {
            if (!extension_IsActivated(p_extensions_manager, p_ext))
                extension_Activate(p_extensions_manager, p_ext);
230
            else
231
                extension_Deactivate(p_extensions_manager, p_ext);
232 233 234 235
        }
    }
    else
    {
236 237
        msg_Dbg(p_intf, "triggering extension '%s', on menu with id = 0x%x",
                 p_ext->psz_title, i_action);
238

239
        extension_TriggerMenu(p_extensions_manager, p_ext, i_action);
240 241 242 243 244 245
    }
}

- (void)inputChanged:(input_thread_t *)p_input
{
    //This is unlikely, but can happen if no extension modules can be loaded.
246
    if (p_extensions_manager == NULL)
David Fuhrmann's avatar
David Fuhrmann committed
247
        return;
248
    vlc_mutex_lock(&p_extensions_manager->lock);
249 250

    extension_t *p_ext;
251 252 253
    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
        if (extension_IsActivated(p_extensions_manager, p_ext))
            extension_SetInput(p_extensions_manager, p_ext, p_input);
254 255 256
    }
    FOREACH_END()

257
    vlc_mutex_unlock(&p_extensions_manager->lock);
258 259 260 261 262
}

- (void)playingChanged:(int)state
{
    //This is unlikely, but can happen if no extension modules can be loaded.
263
    if (p_extensions_manager == NULL)
David Fuhrmann's avatar
David Fuhrmann committed
264
        return;
265
    vlc_mutex_lock(&p_extensions_manager->lock);
266 267

    extension_t *p_ext;
268 269 270
    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
        if (extension_IsActivated(p_extensions_manager, p_ext))
            extension_PlayingChanged(p_extensions_manager, p_ext, state);
271 272 273
    }
    FOREACH_END()

274
    vlc_mutex_unlock(&p_extensions_manager->lock);
275 276 277 278 279
}

- (void)metaChanged:(input_item_t *)p_input
{
    //This is unlikely, but can happen if no extension modules can be loaded.
280
    if (p_extensions_manager == NULL)
David Fuhrmann's avatar
David Fuhrmann committed
281
        return;
282
    vlc_mutex_lock(&p_extensions_manager->lock);
283
    extension_t *p_ext;
284 285 286
    FOREACH_ARRAY(p_ext, p_extensions_manager->extensions) {
        if (extension_IsActivated(p_extensions_manager, p_ext))
            extension_MetaChanged(p_extensions_manager, p_ext);
287 288
    }
    FOREACH_END()
289
    vlc_mutex_unlock(&p_extensions_manager->lock);
290 291 292 293
}

- (void)dealloc
{
294
    intf_thread_t *p_intf = getIntf();
295
    msg_Dbg(p_intf, "Deinitializing extensions manager");
296

297
    _extensionDialogProvider = nil;
298 299
    if (p_extensions_manager)
        vlc_object_release(p_extensions_manager);
300 301 302 303 304 305 306 307 308
}

- (BOOL)isLoaded
{
    return p_extensions_manager != NULL;
}

- (BOOL)cannotLoad
{
309
    return self.isUnloading || b_failed;
310 311
}

312
@end