Panda3D
 All Classes Functions Variables Enumerations
subprocessWindowBuffer.h
1 // Filename: subprocessWindowBuffer.h
2 // Created by: drose (11Jul09)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef SUBPROCESSWINDOWBUFFER_H
16 #define SUBPROCESSWINDOWBUFFER_H
17 
18 #include <stdio.h> // perror
19 #include <assert.h>
20 #include <string>
21 using namespace std;
22 
23 ////////////////////////////////////////////////////////////////////
24 // Class : SubprocessWindowBuffer
25 // Description : This is a special class that is designed to faciliate
26 // SubprocessWindow. It's intended to be allocated
27 // within a shared memory buffer, and it contains space
28 // for a framebuffer image to be stored for transferring
29 // between processes, as well as appropriate
30 // synchronization primitives.
31 //
32 // It's designed to be compiled outside of Panda, so
33 // that code that doesn't link with Panda (in
34 // particular, the Panda3D plugin core API) may still
35 // link with this and use it.
36 //
37 // At the moment, and maybe indefinitely, it is only
38 // compiled on OSX, and only when we are building
39 // support for the plugin; because it is only needed
40 // then.
41 ////////////////////////////////////////////////////////////////////
43 private:
44  void *operator new(size_t, void *addr);
45  SubprocessWindowBuffer(int x_size, int y_size);
46  SubprocessWindowBuffer(const SubprocessWindowBuffer &copy);
47  ~SubprocessWindowBuffer();
48 
49 public:
50  static SubprocessWindowBuffer *new_buffer(int &fd, size_t &mmap_size,
51  string &filename,
52  int x_size, int y_size);
53  static void destroy_buffer(int fd, size_t mmap_size,
54  const string &filename,
55  SubprocessWindowBuffer *buffer);
56 
57  static SubprocessWindowBuffer *open_buffer(int &fd, size_t &mmap_size,
58  const string &filename);
59  static void close_buffer(int fd, size_t mmap_size,
60  const string &filename,
61  SubprocessWindowBuffer *buffer);
62 
63  bool verify_magic_number() const;
64 
65  inline int get_x_size() const;
66  inline int get_y_size() const;
67  inline size_t get_row_size() const;
68  inline size_t get_framebuffer_size() const;
69 
70  inline bool ready_for_read() const;
71  inline bool ready_for_write() const;
72 
73  inline const void *open_read_framebuffer();
74  inline void close_read_framebuffer();
75  inline void *open_write_framebuffer();
76  inline void close_write_framebuffer();
77 
78  enum EventSource {
79  ES_none,
80  ES_mouse,
81  ES_keyboard
82  };
83 
84  enum EventType {
85  ET_none,
86  ET_button_down,
87  ET_button_up,
88  ET_button_again, // if supported
89  };
90 
91  enum EventFlags {
92  EF_has_mouse = 0x0001,
93  EF_mouse_position = 0x0002,
94  EF_shift_held = 0x0004,
95  EF_control_held = 0x0008,
96  EF_alt_held = 0x0010,
97  EF_meta_held = 0x0020,
98  EF_caps_lock = 0x0040,
99  };
100 
101  class Event {
102  public:
103  EventSource _source;
104  int _code; // mouse button, or os-specific keycode.
105  EventType _type;
106  int _x, _y; // position of mouse at the time of the event, if EF_mouse_position is set
107  unsigned int _flags;
108  };
109 
110  inline bool add_event(const Event &event);
111  inline bool has_event() const;
112  inline bool get_event(Event &event);
113 
114 private:
115  // The first thing we store in the buffer is a magic number, so we
116  // don't accidentally memory-map the wrong file and attempt to treat
117  // it as a window buffer.
118  enum { magic_number_length = 8 };
119  static const char _magic_number[magic_number_length];
120  char _this_magic[magic_number_length];
121 
122  // Then we have the required size of the entire structure, including
123  // its data blocks.
124  size_t _mmap_size;
125 
126  // Then some other important parameters.
127  int _x_size, _y_size;
128  size_t _row_size;
129  size_t _framebuffer_size;
130 
131  // A circular queue of events.
132  enum { max_events = 64 };
133  int _event_in; // next slot to write an event to
134  int _event_out; // next slot to read an event from
135  Event _events[max_events];
136  // The queue is empty when _event_in == _event_out.
137  // It is full when _event_in == _event_out - 1, circularly.
138 
139  // These sequence numbers are incremented as frames are written and
140  // read.
141  int _last_written;
142  int _last_read;
143 
144  // The framebuffer data begins immediately at the end of this class.
145 };
146 
147 #include "subprocessWindowBuffer.I"
148 
149 #endif
This is a special class that is designed to faciliate SubprocessWindow.
A named event, possibly with parameters.
Definition: event.h:36