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