macosx: Display icons depending on renderer capabilities
[vlc.git] / modules / gui / macosx / VLCRendererMenuController.m
1 /*****************************************************************************
2  * VLCRendererMenuController.m: Controller class for the renderer menu
3  *****************************************************************************
4  * Copyright (C) 2016-2018 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Marvin Scholz <epirat07 at gmail dot com>
8  *          Felix Paul K├╝hne <fkuehne -at- videolan -dot- org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #import "VLCRendererMenuController.h"
26
27 #import "VLCRendererItem.h"
28 #import "VLCMain.h"
29
30 #include <vlc_renderer_discovery.h>
31
32 @interface VLCRendererMenuController ()
33 {
34     NSMutableDictionary         *_rendererItems;
35     NSMutableArray              *_rendererDiscoveries;
36     BOOL                         _isDiscoveryEnabled;
37     NSMenuItem                  *_selectedItem;
38
39     intf_thread_t               *p_intf;
40     vlc_renderer_discovery_t    *p_rd;
41 }
42 @end
43
44 @implementation VLCRendererMenuController
45
46 - (instancetype)init
47 {
48     self = [super init];
49     if (self) {
50         _rendererItems = [NSMutableDictionary dictionary];
51         _rendererDiscoveries = [NSMutableArray array];
52         _isDiscoveryEnabled = NO;
53         p_intf = getIntf();
54
55         [self loadRendererDiscoveries];
56     }
57     return self;
58 }
59
60 - (void)awakeFromNib
61 {
62     _selectedItem = _rendererNoneItem;
63 }
64
65 - (void)dealloc
66 {
67     [self stopRendererDiscoveries];
68 }
69
70 - (void)loadRendererDiscoveries
71 {
72     playlist_t *playlist = pl_Get(p_intf);
73
74     // Service Discovery subnodes
75     char **ppsz_longnames;
76     char **ppsz_names;
77
78     if (vlc_rd_get_names(playlist, &ppsz_names, &ppsz_longnames) != VLC_SUCCESS) {
79         return;
80     }
81     char **ppsz_name = ppsz_names;
82     char **ppsz_longname = ppsz_longnames;
83
84     for( ; *ppsz_name; ppsz_name++, ppsz_longname++) {
85         VLCRendererDiscovery *dc = [[VLCRendererDiscovery alloc] initWithName:*ppsz_name andLongname:*ppsz_longname];
86         [dc setDelegate:self];
87         [_rendererDiscoveries addObject:dc];
88         free(*ppsz_name);
89         free(*ppsz_longname);
90     }
91     free(ppsz_names);
92     free(ppsz_longnames);
93 }
94
95 #pragma mark - Renderer item management
96
97 - (void)addRendererItem:(VLCRendererItem *)item
98 {
99     // Check if the item is already selected
100     if (_selectedItem.representedObject != nil)
101     {
102         VLCRendererItem *selected_rd_item = _selectedItem.representedObject;
103         if ([selected_rd_item.identifier isEqualToString:item.identifier])
104         {
105             [_selectedItem setRepresentedObject:item];
106             return;
107         }
108     }
109
110     // Create a menu item
111     NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:item.name
112                                                       action:@selector(selectRenderer:)
113                                                keyEquivalent:@""];
114     if (item.capabilityFlags & VLC_RENDERER_CAN_VIDEO)
115         [menuItem setImage:[NSImage imageNamed:@"sidebar-movie"]];
116     else
117         [menuItem setImage:[NSImage imageNamed:@"sidebar-music"]];
118     [menuItem setTarget:self];
119     [menuItem setRepresentedObject:item];
120     [_rendererMenu insertItem:menuItem atIndex:[_rendererMenu indexOfItem:_rendererNoneItem] + 1];
121 }
122
123 - (void)removeRendererItem:(VLCRendererItem *)item
124 {
125     NSInteger index = [_rendererMenu indexOfItemWithRepresentedObject:item];
126     if (index != NSNotFound) {
127         NSMenuItem *menuItem = [_rendererMenu itemAtIndex:index];
128         // Don't remove selected item
129         if (menuItem != _selectedItem)
130             [_rendererMenu removeItemAtIndex:index];
131     }
132 }
133
134 - (void)startRendererDiscoveries
135 {
136     _isDiscoveryEnabled = YES;
137     for (VLCRendererDiscovery *dc in _rendererDiscoveries) {
138         [dc startDiscovery];
139     }
140 }
141
142 - (void)stopRendererDiscoveries
143 {
144     _isDiscoveryEnabled = NO;
145     for (VLCRendererDiscovery *dc in _rendererDiscoveries) {
146         [dc stopDiscovery];
147     }
148 }
149
150 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
151     if (menuItem == _rendererNoneItem ||
152         [[menuItem representedObject] isKindOfClass:[VLCRendererItem class]]) {
153         return _isDiscoveryEnabled;
154     }
155     return [menuItem isEnabled];
156 }
157
158 - (void)selectRenderer:(NSMenuItem *)sender
159 {
160     [_rendererNoneItem setState:NSOffState];
161
162     [_selectedItem setState:NSOffState];
163     [sender setState:NSOnState];
164     _selectedItem = sender;
165
166     VLCRendererItem* item = [sender representedObject];
167     playlist_t *playlist = pl_Get(p_intf);
168
169     if (!playlist)
170         return;
171
172     if (item) {
173         [item setRendererForPlaylist:playlist];
174     } else {
175         [self unsetRendererForPlaylist:playlist];
176     }
177 }
178
179 - (void)unsetRendererForPlaylist:(playlist_t*)playlist
180 {
181     playlist_SetRenderer(playlist, NULL);
182 }
183
184 #pragma mark VLCRendererDiscovery delegate methods
185 - (void)addedRendererItem:(VLCRendererItem *)item from:(VLCRendererDiscovery *)sender
186 {
187     [self addRendererItem:item];
188 }
189
190 - (void)removedRendererItem:(VLCRendererItem *)item from:(VLCRendererDiscovery *)sender
191 {
192     [self removeRendererItem:item];
193 }
194
195 #pragma mark Menu actions
196 - (IBAction)toggleRendererDiscovery:(id)sender {
197     if (_isDiscoveryEnabled) {
198         [self stopRendererDiscoveries];
199     } else {
200         [self startRendererDiscoveries];
201     }
202 }
203
204 @end