x11_loop.cpp
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:12k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * x11_loop.cpp
  3.  *****************************************************************************
  4.  * Copyright (C) 2003 the VideoLAN team
  5.  * $Id: f9cd7779d62c89a3227bdc2527943ed9eaab9656 $
  6.  *
  7.  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  8.  *          Olivier Teulière <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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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.     keysymToVlcKey[XK_Delete] = KEY_DELETE;
  75.     keysymToVlcKey[XK_Insert] = KEY_INSERT;
  76. }
  77. X11Loop::~X11Loop()
  78. {
  79. }
  80. OSLoop *X11Loop::instance( intf_thread_t *pIntf, X11Display &rDisplay )
  81. {
  82.     if( pIntf->p_sys->p_osLoop == NULL )
  83.     {
  84.         OSLoop *pOsLoop = new X11Loop( pIntf, rDisplay );
  85.         pIntf->p_sys->p_osLoop = pOsLoop;
  86.     }
  87.     return pIntf->p_sys->p_osLoop;
  88. }
  89. void X11Loop::destroy( intf_thread_t *pIntf )
  90. {
  91.     if( pIntf->p_sys->p_osLoop )
  92.     {
  93.         delete pIntf->p_sys->p_osLoop;
  94.         pIntf->p_sys->p_osLoop = NULL;
  95.     }
  96. }
  97. void X11Loop::run()
  98. {
  99.     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
  100.     X11TimerLoop *pTimerLoop = ((X11Factory*)pOsFactory)->getTimerLoop();
  101.     // Main event loop
  102.     while( ! m_exit )
  103.     {
  104.         int nPending;
  105.         // Number of pending events in the queue
  106.         nPending = XPending( XDISPLAY );
  107.         while( ! m_exit && nPending > 0 )
  108.         {
  109.             // Handle the next X11 event
  110.             handleX11Event();
  111.             // Number of pending events in the queue
  112.             nPending = XPending( XDISPLAY );
  113.         }
  114.         // Wait for the next timer and execute it
  115.         // The sleep is interrupted if an X11 event is received
  116.         if( !m_exit )
  117.         {
  118.             pTimerLoop->waitNextTimer();
  119.         }
  120.     }
  121. }
  122. void X11Loop::exit()
  123. {
  124.     m_exit = true;
  125. }
  126. void X11Loop::handleX11Event()
  127. {
  128.     XEvent event;
  129.     OSFactory *pOsFactory = OSFactory::instance( getIntf() );
  130.     // Look for the next event in the queue
  131.     XNextEvent( XDISPLAY, &event );
  132.     if( event.xany.window == m_rDisplay.getMainWindow() )
  133.     {
  134.         if( event.type == MapNotify )
  135.         {
  136.             // When the "parent" window is mapped, show all the visible
  137.             // windows, as it is not automatic, unfortunately
  138.             Theme *pTheme = getIntf()->p_sys->p_theme;
  139.             if( pTheme )
  140.             {
  141.                 pTheme->getWindowManager().synchVisibility();
  142.             }
  143.         }
  144.         return;
  145.     }
  146.     // Find the window to which the event is sent
  147.     GenericWindow *pWin =
  148.         ((X11Factory*)pOsFactory)->m_windowMap[event.xany.window];
  149.     if( !pWin )
  150.     {
  151.         return;
  152.     }
  153.     // Send the right event object to the window
  154.     switch( event.type )
  155.     {
  156.         case Expose:
  157.         {
  158.             EvtRefresh evt( getIntf(), event.xexpose.x,
  159.                             event.xexpose.y, event.xexpose.width,
  160.                             event.xexpose.height );
  161.             pWin->processEvent( evt );
  162.             break;
  163.         }
  164.         case FocusIn:
  165.         {
  166.             EvtFocus evt( getIntf(), true );
  167.             pWin->processEvent( evt );
  168.             break;
  169.         }
  170.         case FocusOut:
  171.         {
  172.             EvtFocus evt( getIntf(), false );
  173.             pWin->processEvent( evt );
  174.             break;
  175.         }
  176.         case MotionNotify:
  177.         {
  178.             // Don't trust the position in the event, it is
  179.             // out of date. Get the actual current position instead
  180.             int x, y;
  181.             pOsFactory->getMousePos( x, y );
  182.             EvtMotion evt( getIntf(), x, y );
  183.             pWin->processEvent( evt );
  184.             break;
  185.         }
  186.         case LeaveNotify:
  187.         {
  188.             EvtLeave evt( getIntf() );
  189.             pWin->processEvent( evt );
  190.             break;
  191.         }
  192.         case ButtonPress:
  193.         case ButtonRelease:
  194.         {
  195.             EvtMouse::ActionType_t action = EvtMouse::kDown;
  196.             switch( event.type )
  197.             {
  198.                 case ButtonPress:
  199.                     action = EvtMouse::kDown;
  200.                     break;
  201.                 case ButtonRelease:
  202.                     action = EvtMouse::kUp;
  203.                     break;
  204.             }
  205.             // Get the modifiers
  206.             int mod = EvtInput::kModNone;
  207.             if( event.xbutton.state & Mod1Mask )
  208.             {
  209.                 mod |= EvtInput::kModAlt;
  210.             }
  211.             if( event.xbutton.state & ControlMask )
  212.             {
  213.                 mod |= EvtInput::kModCtrl;
  214.             }
  215.             if( event.xbutton.state & ShiftMask )
  216.             {
  217.                 mod |= EvtInput::kModShift;
  218.             }
  219.             // Check for double clicks
  220.             if( event.type == ButtonPress &&
  221.                 event.xbutton.button == 1 )
  222.             {
  223.                 mtime_t time = mdate();
  224.                 int x, y;
  225.                 pOsFactory->getMousePos( x, y );
  226.                 if( time - m_lastClickTime < m_dblClickDelay &&
  227.                     x == m_lastClickPosX && y == m_lastClickPosY )
  228.                 {
  229.                     m_lastClickTime = 0;
  230.                     action = EvtMouse::kDblClick;
  231.                 }
  232.                 else
  233.                 {
  234.                     m_lastClickTime = time;
  235.                     m_lastClickPosX = x;
  236.                     m_lastClickPosY = y;
  237.                 }
  238.             }
  239.             switch( event.xbutton.button )
  240.             {
  241.                 case 1:
  242.                 {
  243.                     EvtMouse evt( getIntf(), event.xbutton.x,
  244.                                   event.xbutton.y, EvtMouse::kLeft,
  245.                                   action, mod );
  246.                     pWin->processEvent( evt );
  247.                     break;
  248.                 }
  249.                 case 2:
  250.                 {
  251.                     EvtMouse evt( getIntf(), event.xbutton.x,
  252.                                   event.xbutton.y, EvtMouse::kMiddle,
  253.                                   action, mod );
  254.                     pWin->processEvent( evt );
  255.                     break;
  256.                 }
  257.                 case 3:
  258.                 {
  259.                     EvtMouse evt( getIntf(), event.xbutton.x,
  260.                                   event.xbutton.y, EvtMouse::kRight,
  261.                                   action, mod );
  262.                     pWin->processEvent( evt );
  263.                     break;
  264.                 }
  265.                 case 4:
  266.                 {
  267.                     // Scroll up
  268.                     EvtScroll evt( getIntf(), event.xbutton.x,
  269.                                    event.xbutton.y, EvtScroll::kUp,
  270.                                    mod );
  271.                     pWin->processEvent( evt );
  272.                     break;
  273.                 }
  274.                 case 5:
  275.                 {
  276.                     // Scroll down
  277.                     EvtScroll evt( getIntf(), event.xbutton.x,
  278.                                    event.xbutton.y, EvtScroll::kDown,
  279.                                    mod );
  280.                     pWin->processEvent( evt );
  281.                     break;
  282.                 }
  283.             }
  284.             break;
  285.         }
  286.         case KeyPress:
  287.         case KeyRelease:
  288.         {
  289.             EvtKey::ActionType_t action = EvtKey::kDown;
  290.             int mod = EvtInput::kModNone;
  291.             // Get the modifiers
  292.             if( event.xkey.state & Mod1Mask )
  293.             {
  294.                 mod |= EvtInput::kModAlt;
  295.             }
  296.             if( event.xkey.state & ControlMask )
  297.             {
  298.                 mod |= EvtInput::kModCtrl;
  299.             }
  300.             if( event.xkey.state & ShiftMask )
  301.             {
  302.                 mod |= EvtInput::kModShift;
  303.             }
  304.             // Take the first keysym = lower case character
  305.             KeySym keysym = XLookupKeysym( &event.xkey, 0 );
  306.             // Get VLC key code from the keysym
  307.             int key = keysymToVlcKey[keysym];
  308.             if( !key )
  309.             {
  310.                 // Normal key
  311.                 key = keysym;
  312.             }
  313.             switch( event.type )
  314.             {
  315.                 case KeyPress:
  316.                     action = EvtKey::kDown;
  317.                     break;
  318.                 case KeyRelease:
  319.                     action = EvtKey::kUp;
  320.                     break;
  321.             }
  322.             EvtKey evt( getIntf(), key, action, mod );
  323.             pWin->processEvent( evt );
  324.             break;
  325.         }
  326.         case ClientMessage:
  327.         {
  328.             // Get the message type
  329.             string type = XGetAtomName( XDISPLAY, event.xclient.message_type );
  330.             // Find the DnD object for this window
  331.             X11DragDrop *pDnd =
  332.                 ((X11Factory*)pOsFactory)->m_dndMap[event.xany.window];
  333.             if( !pDnd )
  334.             {
  335.                 msg_Err( getIntf(), "no associated D&D object" );
  336.                 return;
  337.             }
  338.             if( type == "XdndEnter" )
  339.             {
  340.                 pDnd->dndEnter( event.xclient.data.l );
  341.             }
  342.             else if( type == "XdndPosition" )
  343.             {
  344.                 pDnd->dndPosition( event.xclient.data.l );
  345.             }
  346.             else if( type == "XdndLeave" )
  347.             {
  348.                 pDnd->dndLeave( event.xclient.data.l );
  349.             }
  350.             else if( type == "XdndDrop" )
  351.             {
  352.                 pDnd->dndDrop( event.xclient.data.l );
  353.             }
  354.             break;
  355.         }
  356.     }
  357. }
  358. #endif