libvisual  0.5.0
lv_event.cpp
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include "config.h"
25 #include "lv_event.h"
26 #include "lv_common.h"
27 #include <vector>
28 #include <queue>
29 
30 namespace LV {
31 
32  namespace {
33 
34  unsigned int get_event_priority (VisEvent const& event)
35  {
36  return event.type == VISUAL_EVENT_RESIZE ? 100 : 0;
37  }
38 
39  struct EventPriorityLesser
40  {
41  bool operator() (VisEvent const& event1, VisEvent const& event2) const
42  {
43  return get_event_priority (event1) < get_event_priority (event2);
44  }
45  };
46 
47  } // anonymous namespace
48 
49  class EventQueue::Impl
50  {
51  public:
52 
53  typedef std::priority_queue<Event, std::vector<Event>, EventPriorityLesser> Queue;
54 
55  Queue events;
56 
57  // FIXME: We need custom input handlers for actors
58  int mousex;
59  int mousey;
60  VisMouseState mousestate;
61 
62  Impl ()
63  : mousex (0)
64  , mousey (0)
65  , mousestate (VISUAL_MOUSE_UP)
66  {}
67  };
68 
69  EventQueue::EventQueue ()
70  : m_impl (new Impl)
71  {
72  // empty
73  }
74 
75  EventQueue::~EventQueue ()
76  {
77  // empty
78  }
79 
80  bool EventQueue::poll (VisEvent& event)
81  {
82  if (!m_impl->events.empty ()) {
83  event = m_impl->events.top ();
84  m_impl->events.pop ();
85  return true;
86  } else {
87  return false;
88  }
89  }
90 
91  void EventQueue::add (VisEvent const& event)
92  {
93  m_impl->events.push (event);
94  }
95 
96 } // LV namespace