window.h 3.6 KB
Newer Older
S
Simon Fels 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2016 Simon Fels <morphis@gravedo.de>
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

18 19
#ifndef ANBOX_PLATFORM_SDL_WINDOW_H_
#define ANBOX_PLATFORM_SDL_WINDOW_H_
S
Simon Fels 已提交
20

21
#include "anbox/wm/window.h"
22
#include "anbox/platform/sdl/sdl_wrapper.h"
23

S
Simon Fels 已提交
24
#include <EGL/egl.h>
25
#include<sys/time.h>
S
Simon Fels 已提交
26

27
#include <atomic>
S
Simon Fels 已提交
28
#include <memory>
29
#include <vector>
30
#include <map>
31
#include <mutex>
S
Simon Fels 已提交
32

S
Simon Fels 已提交
33 34
class Renderer;

35
namespace anbox {
36 37
namespace platform {
namespace sdl {
38 39 40 41
class Window : public std::enable_shared_from_this<Window>, public wm::Window {
 public:
  typedef std::int32_t Id;
  static Id Invalid;
42 43 44
  static const long long USEC_PER_SEC = 1000000;
  static const long long APP_START_MAX_TIME = 15 * USEC_PER_SEC;
  static const long long timespan_db_click = 500000;
45
  static const long long RESIZE_TIMESPAN = USEC_PER_SEC / 2;
46

47 48 49 50 51 52
  struct mini_size {
    int minimum_width;
    int minimum_height;
  };

  static const std::map<std::string, mini_size> custom_window_map;
53
  static const std::map<std::string, std::uint32_t> property_map;
54

55 56 57 58 59
  class Observer {
   public:
    virtual ~Observer();
    virtual void window_deleted(const Id &id) = 0;
    virtual void window_wants_focus(const Id &id) = 0;
S
Simon Fels 已提交
60 61 62 63
    virtual void window_moved(const Id &id, const std::int32_t &x,
                              const std::int32_t &y) = 0;
    virtual void window_resized(const Id &id, const std::int32_t &x,
                                const std::int32_t &y) = 0;
64
    virtual void input_key_event(const SDL_Scancode &scan_code, std::int32_t down_or_up) = 0;
65
  };
66

S
Simon Fels 已提交
67 68
  Window(const std::shared_ptr<Renderer> &renderer,
         const Id &id, const wm::Task::Id &task,
69
         const std::shared_ptr<Observer> &observer,
70
         const graphics::Rect &frame,
71 72
         const std::string &title,
         bool resizable);
73
  ~Window();
S
Simon Fels 已提交
74

75
  bool title_event_filter(int x, int y) override;
76
  void process_event(const SDL_Event &event);
77
  bool check_min_state() {return SDL_GetWindowFlags(window_) & SDL_WINDOW_MINIMIZED;}
78 79
 
  void update_state(const wm::WindowState::List &states) override;
80

81
  bool check_db_clicked(int x, int y);
82 83
  bool checkResizeable() override;
  void setResizing(bool resizing) override;
84 85

  void restore_window();
T
twwang 已提交
86
  bool get_fullscreen() { return fullscreen_; }
87 88
  Id id() const;
  std::uint32_t window_id() const;
89
  Uint32 GetWindowFlags(){return SDL_GetWindowFlags(window_);}
90
  inline std::uint32_t get_property() {
91 92
      return visible_property;
  }
W
wangpeiqiang 已提交
93
  void destroy_window() override;
S
Simon Fels 已提交
94

95
 private:
96 97 98 99
  static SDL_HitTestResult on_window_hit(SDL_Window *window, const SDL_Point *pt, void *data);
  void close();
  void switch_window_state();

100 101
  std::atomic<bool> initialized{false};
  long long last_update_time{ 0 };
102
  long long last_resize_time_{ 0 };
103 104 105 106
  Id id_;
  std::shared_ptr<Observer> observer_;
  EGLNativeDisplayType native_display_;
  SDL_Window *window_;
107 108 109 110 111
  long long lastClickTime{ 0 };
  int last_point_x{ 0 };
  int last_point_y{ 0 };
  int last_wnd_x{ 0 };
  int last_wnd_y{ 0 };
112
  std::uint32_t visible_property;
T
twwang 已提交
113
  bool fullscreen_ = false;
S
Simon Fels 已提交
114
};
115 116 117
} // namespace sdl
} // namespace platform
} // namespace anbox
S
Simon Fels 已提交
118 119

#endif