x11_loop.cpp
上传用户:riyaled888
上传日期:2009-03-27
资源大小:7338k
文件大小:12k
源码类别:

多媒体

开发平台:

MultiPlatform

  1. /*****************************************************************************
  2.  * x11_loop.cpp
  3.  *****************************************************************************
  4.  * Copyright (C) 2003 VideoLAN
  5.  * $Id: x11_loop.cpp 7279 2004-04-05 18:26:34Z asmax $
  6.  *
  7.  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  8.  *          Olivier Teuli鑢e <ipkiss@via.ecp.fr>
  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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  23.  *****************************************************************************/
  24. #ifdef X11_SKINS
  25. #include <X11/keysym.h>
  26. #include "x11_loop.hpp"
  27. #include "x11_display.hpp"
  28. #include "x11_dragdrop.hpp"
  29. #include "x11_factory.hpp"
  30. #include "x11_timer.hpp"
  31. #include "../src/generic_window.hpp"
  32. #include "../src/theme.hpp"
  33. #include "../src/window_manager.hpp"
  34. #include "../events/evt_focus.hpp"
  35. #include "../events/evt_key.hpp"
  36. #include "../events/evt_mouse.hpp"
  37. #include "../events/evt_motion.hpp"
  38. #include "../events/evt_leave.hpp"
  39. #include "../events/evt_refresh.hpp"
  40. #include "../events/evt_scroll.hpp"
  41. #include "../commands/async_queue.hpp"
  42. #include "../utils/var_bool.hpp"
  43. #include "vlc_keys.h"
  44. // Maximum interval between clicks for a double-click (in microsec)
  45. int X11Loop::m_dblClickDelay = 400000;
  46. X11Loop::X11Loop( intf_thread_t *pIntf, X11Display &rDisplay ):
  47.     OSLoop( pIntf ), m_rDisplay( rDisplay ), m_exit( false ),
  48.     m_lastClickTime( 0 ), m_lastClickPosX( 0 ), m_lastClickPosY( 0 )
  49. {
  50.     // Initialize the key map
  51.     keysymToVlcKey[XK_F1] = KEY_F1;
  52.     keysymToVlcKey[XK_F2] = KEY_F2;
  53.     keysymToVlcKey[XK_F3] = KEY_F3;
  54.     keysymToVlcKey[XK_F4] = KEY_F4;
  55.     keysymToVlcKey[XK_F5] = KEY_F5;
  56.     keysymToVlcKey[XK_F6] = KEY_F6;
  57.     keysymToVlcKey[XK_F7] = KEY_F7;
  58.     keysymToVlcKey[XK_F8] = KEY_F8;
  59.     keysymToVlcKey[XK_F9] = KEY_F9;
  60.     keysymToVlcKey[XK_F10] = KEY_F10;
  61.     keysymToVlcKey[XK_F11] = KEY_F11;
  62.     keysymToVlcKey[XK_F12] = KEY_F12;
  63.     keysymToVlcKey[XK_Return] = KEY_ENTER;
  64.     keysymToVlcKey[XK_space] = KEY_SPACE;
  65.     keysymToVlcKey[XK_Escape] = KEY_ESC;
  66.     keysymToVlcKey[XK_Left] = KEY_LEFT;
  67.     keysymToVlcKey[XK_Right] = KEY_RIGHT;
  68.     keysymToVlcKey[XK_Up] = KEY_UP;
  69.     keysymToVlcKey[XK_Down] = KEY_DOWN;
  70.     keysymToVlcKey[XK_Home] = KEY_HOME;
  71.     keysymToVlcKey[XK_End] = KEY_END;
  72.     keysymToVlcKey[XK_Prior] = KEY_PAGEUP;
  73.     keysymToVlcKey[XK_Next] = KEY_PAGEDOWN;
  74. }
  75. X11Loop::~X11Loop()
  76. {
  77. }
  78. OSLoop *X11Loop::instance( intf_thread_t *pIntf, X11Display &rDisplay )
  79. {
  80.     if( pIntf->p_sys->p_osLoop == NULL )
  81.     {
  82.         OSLoop *pOsLoop = new X11Loop( pIntf, rDisplay );
  83.         pIntf->p_sys->p_osLoop = pOsLoop;
  84.     }
  85.     return pIntf->p_sys->p_osLoop;
  86. }
  87. void X11Loop::destroy( intf_thread_t *pIntf )
  88. {
  89.     if( pIntf->p_sys->p_osLoop )
  90.     {
  91.         delete pIntf->p_sys->p_osLoop;
  92.         pIntf->p_sys->p_osLoop = NULL;
  93.     }
  94. }
  95. void X11Loop::run()
  96. {
  97.     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
  98.     X11TimerLoop *pTimerLoop = ((X11Factory*)pOsFactory)->getTimerLoop();
  99.     // Main event loop
  100.     while( ! m_exit )
  101.     {
  102.         int nPending;
  103.         // Number of pending events in the queue
  104.         nPending = XPending( XDISPLAY );
  105.         while( ! m_exit && nPending > 0 )
  106.         {
  107.             // Handle the next X11 event
  108.             handleX11Event();
  109.             // Number of pending events in the queue
  110.             nPending = XPending( XDISPLAY );
  111.         }
  112.         // Wait for the next timer and execute it
  113.         // The sleep is interrupted if an X11 event is received
  114.         if( !m_exit )
  115.         {
  116.             pTimerLoop->waitNextTimer();
  117.         }
  118.     }
  119. }
  120. void X11Loop::exit()
  121. {
  122.     m_exit = true;
  123. }
  124. void X11Loop::handleX11Event()
  125. {
  126.     XEvent event;
  127.     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
  128.     // Look for the next event in the queue
  129.     XNextEvent( XDISPLAY, &event );
  130.     if( event.xany.window == m_rDisplay.getMainWindow() )
  131.     {
  132.         if( event.type == MapNotify )
  133.         {
  134.             // When the "parent" window is mapped, show all the visible
  135.             // windows, as it is not automatic, unfortunately
  136.             Theme *pTheme = getIntf()->p_sys->p_theme;
  137.             if( pTheme )
  138.             {
  139.                 pTheme->getWindowManager().synchVisibility();
  140.             }
  141.         }
  142.         return;
  143.     }
  144.     // Find the window to which the event is sent
  145.     GenericWindow *pWin =
  146.         ((X11Factory*)pOsFactory)->m_windowMap[event.xany.window];
  147.     if( !pWin )
  148.     {
  149.         msg_Dbg( getIntf(), "No associated generic window !!" );
  150.         return;
  151.     }
  152.     // Send the right event object to the window
  153.     switch( event.type )
  154.     {
  155.         case Expose:
  156.         {
  157.             EvtRefresh evt( getIntf(), event.xexpose.x,
  158.                             event.xexpose.y, event.xexpose.width,
  159.                             event.xexpose.height );
  160.             pWin->processEvent( evt );
  161.             break;
  162.         }
  163.         case FocusIn:
  164.         {
  165.             EvtFocus evt( getIntf(), true );
  166.             pWin->processEvent( evt );
  167.             break;
  168.         }
  169.         case FocusOut:
  170.         {
  171.             EvtFocus evt( getIntf(), false );
  172.             pWin->processEvent( evt );
  173.             break;
  174.         }
  175.         case MotionNotify:
  176.         {
  177.             // Don't trust the position in the event, it is
  178.             // out of date. Get the actual current position instead
  179.             int x, y;
  180.             pOsFactory->getMousePos( x, y );
  181.             EvtMotion evt( getIntf(), x, y );
  182.             pWin->processEvent( evt );
  183.             break;
  184.         }
  185.         case LeaveNotify:
  186.         {
  187.             EvtLeave evt( getIntf() );
  188.             pWin->processEvent( evt );
  189.             break;
  190.         }
  191.         case ButtonPress:
  192.         case ButtonRelease:
  193.         {
  194.             EvtMouse::ActionType_t action = EvtMouse::kDown;
  195.             switch( event.type )
  196.             {
  197.                 case ButtonPress:
  198.                     action = EvtMouse::kDown;
  199.                     break;
  200.                 case ButtonRelease:
  201.                     action = EvtMouse::kUp;
  202.                     break;
  203.             }
  204.             // Get the modifiers
  205.             int mod = EvtInput::kModNone;
  206.             if( event.xbutton.state & Mod1Mask )
  207.             {
  208.                 mod |= EvtInput::kModAlt;
  209.             }
  210.             if( event.xbutton.state & ControlMask )
  211.             {
  212.                 mod |= EvtInput::kModCtrl;
  213.             }
  214.             if( event.xbutton.state & ShiftMask )
  215.             {
  216.                 mod |= EvtInput::kModShift;
  217.             }
  218.             // Check for double clicks
  219.             if( event.type == ButtonPress &&
  220.                 event.xbutton.button == 1 )
  221.             {
  222.                 mtime_t time = mdate();
  223.                 int x, y;
  224.                 pOsFactory->getMousePos( x, y );
  225.                 if( time - m_lastClickTime < m_dblClickDelay &&
  226.                     x == m_lastClickPosX && y == m_lastClickPosY )
  227.                 {
  228.                     m_lastClickTime = 0;
  229.                     action = EvtMouse::kDblClick;
  230.                 }
  231.                 else
  232.                 {
  233.                     m_lastClickTime = time;
  234.                     m_lastClickPosX = x;
  235.                     m_lastClickPosY = y;
  236.                 }
  237.             }
  238.             switch( event.xbutton.button )
  239.             {
  240.                 case 1:
  241.                 {
  242.                     EvtMouse evt( getIntf(), event.xbutton.x,
  243.                                   event.xbutton.y, EvtMouse::kLeft,
  244.                                   action, mod );
  245.                     pWin->processEvent( evt );
  246.                     break;
  247.                 }
  248.                 case 2:
  249.                 {
  250.                     EvtMouse evt( getIntf(), event.xbutton.x,
  251.                                   event.xbutton.y, EvtMouse::kMiddle,
  252.                                   action, mod );
  253.                     pWin->processEvent( evt );
  254.                     break;
  255.                 }
  256.                 case 3:
  257.                 {
  258.                     EvtMouse evt( getIntf(), event.xbutton.x,
  259.                                   event.xbutton.y, EvtMouse::kRight,
  260.                                   action, mod );
  261.                     pWin->processEvent( evt );
  262.                     break;
  263.                 }
  264.                 case 4:
  265.                 {
  266.                     // Scroll up
  267.                     EvtScroll evt( getIntf(), event.xbutton.x,
  268.                                    event.xbutton.y, EvtScroll::kUp,
  269.                                    mod );
  270.                     pWin->processEvent( evt );
  271.                     break;
  272.                 }
  273.                 case 5:
  274.                 {
  275.                     // Scroll down
  276.                     EvtScroll evt( getIntf(), event.xbutton.x,
  277.                                    event.xbutton.y, EvtScroll::kDown,
  278.                                    mod );
  279.                     pWin->processEvent( evt );
  280.                     break;
  281.                 }
  282.             }
  283.             break;
  284.         }
  285.         case KeyPress:
  286.         case KeyRelease:
  287.         {
  288.             EvtKey::ActionType_t action = EvtKey::kDown;
  289.             int mod = EvtInput::kModNone;
  290.             // Get the modifiers
  291.             if( event.xkey.state & Mod1Mask )
  292.             {
  293.                 mod |= EvtInput::kModAlt;
  294.             }
  295.             if( event.xkey.state & ControlMask )
  296.             {
  297.                 mod |= EvtInput::kModCtrl;
  298.             }
  299.             if( event.xkey.state & ShiftMask )
  300.             {
  301.                 mod |= EvtInput::kModShift;
  302.             }
  303.             // Take the first keysym = lower case character
  304.             KeySym keysym = XLookupKeysym( &event.xkey, 0 );
  305.             // Get VLC key code from the keysym
  306.             int key = keysymToVlcKey[keysym];
  307.             if( !key )
  308.             {
  309.                 // Normal key
  310.                 key = keysym;
  311.             }
  312.             switch( event.type )
  313.             {
  314.                 case KeyPress:
  315.                     action = EvtKey::kDown;
  316.                     break;
  317.                 case KeyRelease:
  318.                     action = EvtKey::kUp;
  319.                     break;
  320.             }
  321.             EvtKey evt( getIntf(), key, action, mod );
  322.             pWin->processEvent( evt );
  323.             break;
  324.         }
  325.         case ClientMessage:
  326.         {
  327.             // Get the message type
  328.             string type = XGetAtomName( XDISPLAY, event.xclient.message_type );
  329.             // Find the DnD object for this window
  330.             X11DragDrop *pDnd =
  331.                 ((X11Factory*)pOsFactory)->m_dndMap[event.xany.window];
  332.             if( !pDnd )
  333.             {
  334.                 msg_Err( getIntf(), "No associated D&D object !!" );
  335.                 return;
  336.             }
  337.             if( type == "XdndEnter" )
  338.             {
  339.                 pDnd->dndEnter( event.xclient.data.l );
  340.             }
  341.             else if( type == "XdndPosition" )
  342.             {
  343.                 pDnd->dndPosition( event.xclient.data.l );
  344.             }
  345.             else if( type == "XdndLeave" )
  346.             {
  347.                 pDnd->dndLeave( event.xclient.data.l );
  348.             }
  349.             else if( type == "XdndDrop" )
  350.             {
  351.                 pDnd->dndDrop( event.xclient.data.l );
  352.             }
  353.             break;
  354.         }
  355.     }
  356. }
  357. #endif