1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-13 12:31:52 +02:00

Add some shortcuts for mac

Fix osx shortcuts
This commit is contained in:
Leo Franchi 2011-02-13 23:57:25 -05:00
parent 0ca1212691
commit 1647293ada
11 changed files with 192 additions and 77 deletions

View File

@ -4,7 +4,7 @@
#define APP TomahawkApp::instance()
#include "headlesscheck.h"
#include "tomahawkapp_mac.h" // for PlatforInterface
#include "mac/tomahawkapp_mac.h" // for PlatforInterface
#include <QRegExp>
#include <QFile>
@ -29,6 +29,7 @@ class XMPPBot;
namespace Tomahawk
{
class ShortcutHandler;
namespace InfoSystem
{
class InfoSystem;
@ -99,6 +100,7 @@ private:
SipHandler* m_sipHandler;
Servent* m_servent;
XMPPBot* m_xmppBot;
Tomahawk::ShortcutHandler* m_shortcutHandler;
#ifndef NO_LIBLASTFM
Scrobbler* m_scrobbler;

View File

@ -39,6 +39,7 @@ SET( tomahawkSources ${tomahawkSources}
musicscanner.cpp
scriptresolver.cpp
scrobbler.cpp
shortcuthandler.cpp
tomahawkapp.cpp
main.cpp
@ -59,8 +60,8 @@ SET( tomahawkSourcesGui ${tomahawkSourcesGui}
IF( APPLE )
SET( tomahawkHeaders ${tomahawkHeaders} tomahawkapp_mac.h )
SET( tomahawkSources ${tomahawkSources} tomahawkapp_mac.mm )
SET( tomahawkHeaders ${tomahawkHeaders} mac/tomahawkapp_mac.h mac/macshortcuthandler.h )
SET( tomahawkSources ${tomahawkSources} mac/tomahawkapp_mac.mm mac/macshortcuthandler.cpp )
ENDIF( APPLE )
SET( tomahawkHeaders ${tomahawkHeaders}
@ -78,6 +79,7 @@ SET( tomahawkHeaders ${tomahawkHeaders}
musicscanner.h
scriptresolver.h
scrobbler.h
shortcuthandler.h
)
SET( tomahawkHeadersGui ${tomahawkHeadersGui}
@ -115,6 +117,7 @@ INCLUDE_DIRECTORIES(
topbar
utils
libtomahawk
mac
../alsa-playback
../rtaudio

View File

@ -62,6 +62,15 @@ AudioEngine::~AudioEngine()
delete m_audio;
}
void
AudioEngine::playPause()
{
if( m_audio->isPlaying() )
pause();
else
play();
}
void
AudioEngine::play()
@ -141,6 +150,11 @@ AudioEngine::setVolume( int percentage )
emit volumeChanged( percentage );
}
void
AudioEngine::mute()
{
setVolume( 0 );
}
void
AudioEngine::onTrackAboutToClose()

View File

@ -40,6 +40,7 @@ public:
PlaylistInterface* playlist() const { return m_playlist; }
public slots:
void playPause();
void play();
void pause();
void stop();
@ -51,6 +52,7 @@ public slots:
void lowerVolume() { setVolume( volume() - AUDIO_VOLUME_STEP ); }
void raiseVolume() { setVolume( volume() + AUDIO_VOLUME_STEP ); }
void onVolumeChanged( float volume ) { emit volumeChanged( volume * 100 ); }
void mute();
void playItem( PlaylistInterface* playlist, const Tomahawk::result_ptr& result );
void setPlaylist( PlaylistInterface* playlist ) { m_playlist = playlist; }

View File

@ -0,0 +1,31 @@
#include "macshortcuthandler.h"
#include <QDebug>
#include <IOKit/hidsystem/ev_keymap.h>
using namespace Tomahawk;
MacShortcutHandler::MacShortcutHandler(QObject *parent) :
Tomahawk::ShortcutHandler(parent)
{
}
void
MacShortcutHandler::macMediaKeyPressed( int key )
{
switch (key) {
case NX_KEYTYPE_PLAY:
qDebug() << "emitting PlayPause pressed";
emit playPause();
break;
case NX_KEYTYPE_FAST:
qDebug() << "emitting next pressed";
emit next();
break;
case NX_KEYTYPE_REWIND:
qDebug() << "emitting prev pressed";
emit previous();
break;
}
}

View File

@ -0,0 +1,22 @@
#ifndef MACSHORTCUTHANDLER_H
#define MACSHORTCUTHANDLER_H
#include "shortcuthandler.h"
#include <QObject>
namespace Tomahawk {
class MacShortcutHandler : public ShortcutHandler
{
Q_OBJECT
public:
explicit MacShortcutHandler(QObject *parent = 0);
void macMediaKeyPressed( int key );
};
}
#endif // MACSHORTCUTHANDLER_H

View File

@ -8,6 +8,8 @@ class QString;
namespace Tomahawk {
class MacShortcutHandler;
/// Interface between cocoa and tomahawk
class PlatformInterface {
public:
@ -19,7 +21,7 @@ class PlatformInterface {
};
void macMain();
//void setShortcutHandler(GlobalShortcuts* handler);
void setShortcutHandler(Tomahawk::MacShortcutHandler* engine);
// used for opening files with tomahawk
void setApplicationHandler(PlatformInterface* handler);

View File

@ -1,5 +1,6 @@
#include "tomahawkapp_mac.h"
#include "tomahawkapp_macdelegate.h"
#include "macshortcuthandler.h"
#include <QDebug>
#import <AppKit/NSApplication.h>
@ -19,17 +20,17 @@
// See: http://www.rogueamoeba.com/utm/2007/09/29/apple-keyboard-media-key-event-handling/
@interface MacApplication :NSApplication {
// MacGlobalShortcutBackend* shortcut_handler_;
Tomahawk::MacShortcutHandler* shortcut_handler_;
Tomahawk::PlatformInterface* application_handler_;
}
//- (MacGlobalShortcutBackend*) shortcut_handler;
//- (void) SetShortcutHandler: (MacGlobalShortcutBackend*)handler;
- (Tomahawk::MacShortcutHandler*) shortcutHandler;
- (void) setShortcutHandler: (Tomahawk::MacShortcutHandler*)handler;
- (Tomahawk::PlatformInterface*) application_handler;
- (void) SetApplicationHandler: (Tomahawk::PlatformInterface*)handler;
- (void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
//- (void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat;
- (void) setApplicationHandler: (Tomahawk::PlatformInterface*)handler;
- (void) getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent;
- (void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat;
@end
@ -78,40 +79,26 @@
- (id) init {
if ((self = [super init])) {
// [self SetShortcutHandler:nil];
[self SetApplicationHandler:nil];
NSAppleEventManager *em = [NSAppleEventManager sharedAppleEventManager];
[em
setEventHandler:self
andSelector:@selector(getUrl:withReplyEvent:)
forEventClass:kInternetEventClass
andEventID:kAEGetURL];
[em
setEventHandler:self
andSelector:@selector(getUrl:withReplyEvent:)
forEventClass:'WWW!'
andEventID:'OURL'];
NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
OSStatus httpResult = LSSetDefaultHandlerForURLScheme((CFStringRef)@"tomahawk", (CFStringRef)bundleID);
//TODO: Check httpResult and httpsResult for errors
[self setShortcutHandler:nil];
[self setApplicationHandler:nil];
}
return self;
}
/*
- (MacGlobalShortcutBackend*) shortcut_handler {
return shortcut_handler_;
- (Tomahawk::MacShortcutHandler*) shortcutHandler {
return shortcut_handler_;
}
- (void) SetShortcutHandler: (MacGlobalShortcutBackend*)handler {
- (void) setShortcutHandler: (Tomahawk::MacShortcutHandler*)handler {
qDebug() << "Setting shortcut handler of MacAPp";
shortcut_handler_ = handler;
}
*/
- (Tomahawk::PlatformInterface*) application_handler {
return application_handler_;
}
- (void) SetApplicationHandler: (Tomahawk::PlatformInterface*)handler {
- (void) setApplicationHandler: (Tomahawk::PlatformInterface*)handler {
AppDelegate* delegate = [[AppDelegate alloc] initWithHandler:handler];
[self setDelegate:delegate];
}
@ -123,30 +110,19 @@
int keystate = (((keyflags & 0xFF00) >> 8)) == 0xA;
int keyrepeat = (keyflags & 0x1);
//[self mediaKeyEvent: keycode state: keystate repeat: keyrepeat];
[self mediaKeyEvent: keycode state: keystate repeat: keyrepeat];
}
[super sendEvent: event];
}
/*
-(void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat {
if (!shortcut_handler_) {
return;
}
if (state == 0) {
shortcut_handler_->MacMediaKeyPressed(key);
shortcut_handler_->macMediaKeyPressed(key);
}
} */
- (void)getUrl:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)replyEvent
{
// Get the URL
NSString *urlStr = [[event paramDescriptorForKeyword:keyDirectObject]
stringValue];
qDebug() << "Wants to open:" << [urlStr UTF8String];
//TODO: Your custom URL handling code here
}
@end
@ -161,13 +137,13 @@ void Tomahawk::macMain() {
#endif
}
/*
void setShortcutHandler(MacGlobalShortcutBackend* handler) {
[NSApp SetShortcutHandler: handler];
void Tomahawk::setShortcutHandler(Tomahawk::MacShortcutHandler* handler) {
[NSApp setShortcutHandler: handler];
}
*/
void Tomahawk::setApplicationHandler(Tomahawk::PlatformInterface* handler) {
[NSApp SetApplicationHandler: handler];
[NSApp setApplicationHandler: handler];
}
void CheckForUpdates() {

13
src/shortcuthandler.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "shortcuthandler.h"
using namespace Tomahawk;
ShortcutHandler::ShortcutHandler( QObject *parent )
: QObject( parent )
{
}
ShortcutHandler::~ShortcutHandler()
{
}

34
src/shortcuthandler.h Normal file
View File

@ -0,0 +1,34 @@
#ifndef SHORTCUTHANDLER_H
#define SHORTCUTHANDLER_H
#include <QObject>
namespace Tomahawk {
/**
Base class for various shortcut plugins on different platforms
*/
class ShortcutHandler : public QObject
{
Q_OBJECT
public:
virtual ~ShortcutHandler();
signals:
// add more as needed
void playPause();
void pause();
void stop();
void previous();
void next();
void volumeUp();
void volumeDown();
void mute();
protected:
explicit ShortcutHandler( QObject *parent = 0 );
};
}
#endif // SHORTCUTHANDLER_H

View File

@ -24,8 +24,11 @@
#include "web/api_v1.h"
#include "scriptresolver.h"
#include "sourcelist.h"
#include "shortcuthandler.h"
#include "tomahawksettings.h"
#include "audio/audioengine.h"
#include "utils/xspfloader.h"
#ifndef TOMAHAWK_HEADLESS
#include "tomahawkwindow.h"
@ -34,7 +37,7 @@
#endif
#ifdef Q_WS_MAC
#include "tomahawkapp_mac.h"
#include "mac/macshortcuthandler.h"
#endif
#include <iostream>
@ -42,8 +45,6 @@
#define LOGFILE TomahawkUtils::appDataDir().filePath( "tomahawk.log" ).toLocal8Bit()
#define LOGFILE_SIZE 1024 * 512
#include "tomahawksettings.h"
#include <utils/xspfloader.h>
using namespace std;
ofstream logfile;
@ -114,6 +115,7 @@ TomahawkApp::TomahawkApp( int& argc, char *argv[] )
, m_audioEngine( 0 )
, m_sipHandler( 0 )
, m_servent( 0 )
, m_shortcutHandler( 0 )
, m_mainwindow( 0 )
, m_infoSystem( 0 )
{
@ -160,35 +162,49 @@ TomahawkApp::TomahawkApp( int& argc, char *argv[] )
qDebug() << "Init Echonest Factory.";
GeneratorFactory::registerFactory( "echonest", new EchonestFactory );
#ifndef NO_LIBLASTFM
qDebug() << "Init Scrobbler.";
m_scrobbler = new Scrobbler( this );
qDebug() << "Setting NAM.";
TomahawkUtils::setNam( new lastfm::NetworkAccessManager( this ) );
// Register shortcut handler for this platform
#ifdef Q_WS_MAC
m_shortcutHandler = new MacShortcutHandler( this );
Tomahawk::setShortcutHandler( static_cast<MacShortcutHandler*>( m_shortcutHandler) );
connect( m_audioEngine, SIGNAL( started( const Tomahawk::result_ptr& ) ),
m_scrobbler, SLOT( trackStarted( const Tomahawk::result_ptr& ) ), Qt::QueuedConnection );
connect( m_audioEngine, SIGNAL( paused() ),
m_scrobbler, SLOT( trackPaused() ), Qt::QueuedConnection );
connect( m_audioEngine, SIGNAL( resumed() ),
m_scrobbler, SLOT( trackResumed() ), Qt::QueuedConnection );
connect( m_audioEngine, SIGNAL( stopped() ),
m_scrobbler, SLOT( trackStopped() ), Qt::QueuedConnection );
#else
qDebug() << "Setting NAM.";
TomahawkUtils::setNam( new QNetworkAccessManager );
Tomahawk::setApplicationHandler( this );
#endif
#ifdef Q_WS_MAC
Tomahawk::setApplicationHandler( this );
// Connect up shortcuts
connect( m_shortcutHandler, SIGNAL( playPause() ), m_audioEngine, SLOT( playPause() ) );
connect( m_shortcutHandler, SIGNAL( pause() ), m_audioEngine, SLOT( pause() ) );
connect( m_shortcutHandler, SIGNAL( stop() ), m_audioEngine, SLOT( stop() ) );
connect( m_shortcutHandler, SIGNAL( previous() ), m_audioEngine, SLOT( previous() ) );
connect( m_shortcutHandler, SIGNAL( next() ), m_audioEngine, SLOT( next() ) );
connect( m_shortcutHandler, SIGNAL( volumeUp() ), m_audioEngine, SLOT( raiseVolume() ) );
connect( m_shortcutHandler, SIGNAL( volumeDown() ), m_audioEngine, SLOT( lowerVolume() ) );
connect( m_shortcutHandler, SIGNAL( mute() ), m_audioEngine, SLOT( mute() ) );
#ifndef NO_LIBLASTFM
qDebug() << "Init Scrobbler.";
m_scrobbler = new Scrobbler( this );
qDebug() << "Setting NAM.";
TomahawkUtils::setNam( new lastfm::NetworkAccessManager( this ) );
connect( m_audioEngine, SIGNAL( started( const Tomahawk::result_ptr& ) ),
m_scrobbler, SLOT( trackStarted( const Tomahawk::result_ptr& ) ), Qt::QueuedConnection );
connect( m_audioEngine, SIGNAL( paused() ),
m_scrobbler, SLOT( trackPaused() ), Qt::QueuedConnection );
connect( m_audioEngine, SIGNAL( resumed() ),
m_scrobbler, SLOT( trackResumed() ), Qt::QueuedConnection );
connect( m_audioEngine, SIGNAL( stopped() ),
m_scrobbler, SLOT( trackStopped() ), Qt::QueuedConnection );
#else
qDebug() << "Setting NAM.";
TomahawkUtils::setNam( new QNetworkAccessManager );
#endif
// Set up proxy
if( TomahawkSettings::instance()->proxyType() != QNetworkProxy::NoProxy &&
!TomahawkSettings::instance()->proxyHost().isEmpty() )
!TomahawkSettings::instance()->proxyHost().isEmpty() )
{
qDebug() << "Setting proxy to saved values";
TomahawkUtils::setProxy( new QNetworkProxy( static_cast<QNetworkProxy::ProxyType>(TomahawkSettings::instance()->proxyType()), TomahawkSettings::instance()->proxyHost(), TomahawkSettings::instance()->proxyPort(), TomahawkSettings::instance()->proxyUsername(), TomahawkSettings::instance()->proxyPassword() ) );