PageRenderTime 124ms CodeModel.GetById 80ms app.highlight 17ms RepoModel.GetById 23ms app.codeStats 0ms

/src/main.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 213 lines | 144 code | 37 blank | 32 comment | 15 complexity | 5608c7ef7ebb2db367acab0f77a7426a MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19
 20#include "TomahawkApp.h"
 21
 22#include "thirdparty/kdsingleapplicationguard/kdsingleapplicationguard.h"
 23#include "UbuntuUnityHack.h"
 24#include "TomahawkSettings.h"
 25#include "utils/TomahawkUtils.h"
 26#include "config.h"
 27#include "utils/Logger.h"
 28
 29#ifdef Q_WS_MAC
 30    #include "TomahawkApp_Mac.h"
 31    #include </System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/Headers/AppleEvents.h>
 32    static pascal OSErr appleEventHandler( const AppleEvent*, AppleEvent*, long );
 33#endif
 34
 35#ifndef ENABLE_HEADLESS
 36    #include "TomahawkSettingsGui.h"
 37    #include "breakpad/BreakPad.h"
 38#endif
 39
 40
 41#ifdef Q_OS_WIN
 42// code from patch attached to QTBUG-19064 by Honglei Zhang
 43LRESULT QT_WIN_CALLBACK qt_LowLevelKeyboardHookProc( int nCode, WPARAM wParam, LPARAM lParam );
 44HHOOK hKeyboardHook;
 45HINSTANCE hGuiLibInstance;
 46
 47LRESULT QT_WIN_CALLBACK qt_LowLevelKeyboardHookProc( int nCode, WPARAM wParam, LPARAM lParam )
 48{
 49    LPKBDLLHOOKSTRUCT kbHookStruct = reinterpret_cast<LPKBDLLHOOKSTRUCT>(lParam);
 50
 51    switch(kbHookStruct->vkCode)
 52    {
 53        case VK_VOLUME_MUTE:
 54        case VK_VOLUME_DOWN:
 55        case VK_VOLUME_UP:
 56        case VK_MEDIA_NEXT_TRACK:
 57        case VK_MEDIA_PREV_TRACK:
 58        case VK_MEDIA_STOP:
 59        case VK_MEDIA_PLAY_PAUSE:
 60        case VK_LAUNCH_MEDIA_SELECT:
 61            // send message
 62            {
 63                HWND hWnd = NULL;
 64                foreach ( QWidget *widget, QApplication::topLevelWidgets() )
 65                {
 66                    // relay message to each top level widgets(window)
 67                    // if the window has focus, we don't send a duplicate message
 68                    if ( QApplication::activeWindow() == widget )
 69                        continue;
 70
 71                    hWnd = widget->winId();
 72
 73                    // generate message and post it to the message queue
 74                    LPKBDLLHOOKSTRUCT pKeyboardHookStruct = reinterpret_cast<LPKBDLLHOOKSTRUCT>(lParam);
 75                    WPARAM _wParam = pKeyboardHookStruct->vkCode;
 76                    LPARAM _lParam = MAKELPARAM( pKeyboardHookStruct->scanCode, pKeyboardHookStruct->flags );
 77                    PostMessage( hWnd, wParam, _wParam, _lParam );
 78                }
 79            }
 80            break;
 81
 82        default:
 83            break;
 84    }
 85
 86    return CallNextHookEx( 0, nCode, wParam, lParam );
 87}
 88
 89#include <io.h>
 90#define argc __argc
 91#define argv __argv
 92// code taken from AbiWord, (c) AbiSource Inc.
 93
 94int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow )
 95{
 96    hKeyboardHook = NULL;
 97    hGuiLibInstance = hInstance;
 98
 99    // setup keyboard hook to receive multimedia key events when application is at background
100    hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL,(HOOKPROC) qt_LowLevelKeyboardHookProc, hGuiLibInstance, 0 );
101
102    if ( fileno( stdout ) != -1 && _get_osfhandle( fileno( stdout ) ) != -1 )
103    {
104        /* stdout is fine, presumably redirected to a file or pipe */
105    }
106    else
107    {
108        typedef BOOL (WINAPI * AttachConsole_t) (DWORD);
109        AttachConsole_t p_AttachConsole = (AttachConsole_t) GetProcAddress( GetModuleHandleW( L"kernel32.dll" ), "AttachConsole" );
110
111        if ( p_AttachConsole != NULL && p_AttachConsole( ATTACH_PARENT_PROCESS ) )
112        {
113            _wfreopen ( L"CONOUT$", L"w", stdout );
114            dup2( fileno( stdout ), 1 );
115            _wfreopen ( L"CONOUT$", L"w", stderr );
116            dup2( fileno( stderr ), 2 );
117        }
118    }
119#else // Q_OS_WIN
120
121int
122main( int argc, char *argv[] )
123{
124#ifdef Q_WS_MAC
125    // Do Mac specific startup to get media keys working.
126    // This must go before QApplication initialisation.
127    Tomahawk::macMain();
128
129    // Fixes focus issue with NSSearchField, see QTBUG-11401
130    // code taken from clementine:main.cpp:336
131    QCoreApplication::setAttribute( Qt::AA_NativeWindows, true );
132
133    // used for url handler
134    AEEventHandlerUPP h = AEEventHandlerUPP( appleEventHandler );
135    AEInstallEventHandler( 'GURL', 'GURL', h, 0, false );
136    #endif // Q_WS_MAC
137#endif //Q_OS_WIN
138
139    TomahawkApp a( argc, argv );
140
141    // MUST register StateHash ****before*** initing TomahawkSettingsGui as constructor of settings does upgrade before Gui subclass registers type
142    TomahawkSettings::registerCustomSettingsHandlers();
143    TomahawkSettingsGui::registerCustomSettingsHandlers();
144
145#ifdef ENABLE_HEADLESS
146    new TomahawkSettings( &a );
147#else
148    new TomahawkSettingsGui( &a );
149#endif
150
151#ifndef ENABLE_HEADLESSs
152#ifdef WITH_BREAKPAD
153    new BreakPad( QDir::tempPath(), TomahawkSettings::instance()->crashReporterEnabled() && !TomahawkUtils::headless() );
154#endif
155#endif
156
157    KDSingleApplicationGuard guard( KDSingleApplicationGuard::AutoKillOtherInstances );
158    QObject::connect( &guard, SIGNAL( instanceStarted( KDSingleApplicationGuard::Instance ) ), &a, SLOT( instanceStarted( KDSingleApplicationGuard::Instance ) ) );
159
160    if ( guard.isPrimaryInstance() )
161        a.init();
162
163    if ( argc > 1 )
164    {
165        QString arg = a.arguments()[ 1 ];
166        a.loadUrl( arg );
167    }
168
169    int returnCode = 0;
170    if ( guard.isPrimaryInstance() )
171        returnCode = a.exec();
172
173#ifdef Q_OS_WIN
174    // clean up keyboard hook
175    if ( hKeyboardHook )
176    {
177        UnhookWindowsHookEx( hKeyboardHook );
178        hKeyboardHook = NULL;
179    }
180#endif
181
182    return returnCode;
183}
184
185
186#ifdef Q_WS_MAC
187static pascal OSErr
188appleEventHandler( const AppleEvent* e, AppleEvent*, long )
189{
190    OSType id = typeWildCard;
191    AEGetAttributePtr( e, keyEventIDAttr, typeType, 0, &id, sizeof( id ), 0 );
192
193    switch ( id )
194    {
195        case 'GURL':
196        {
197            DescType type;
198            Size size;
199
200            char buf[1024];
201            AEGetParamPtr( e, keyDirectObject, typeChar, &type, &buf, 1023, &size );
202            buf[size] = '\0';
203
204            QString url = QString::fromUtf8( buf );
205            static_cast<TomahawkApp*>(qApp)->loadUrl( url );
206            return noErr;
207        }
208
209        default:
210            return unimpErr;
211    }
212}
213#endif