1 //  Construo - A wire-frame construction game
2 //  Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_X11_DISPLAY_HPP
18 #define HEADER_X11_DISPLAY_HPP
19 
20 #include <X11/Xlib.h>
21 #include <X11/Xutil.h>
22 
23 #ifdef HAVE_LIBXXF86VM
24 #  include <X11/extensions/xf86vmode.h>
25 #endif
26 
27 #include "cursors/cursors.hpp"
28 #include "math.hpp"
29 #include "root_graphic_context.hpp"
30 #include "input_context.hpp"
31 
32 #define X11_FULLSCREEN_MODE true
33 #define X11_WINDOW_MODE     false
34 
35 struct FlipRect
36 {
37   int x1;
38   int y1;
39   int x2;
40   int y2;
41 };
42 
43 /** X11Display driver */
44 class X11Display : public RootGraphicContext,
45                    public InputContext
46 {
47 private:
48   Cursor cursor_scroll;
49   Pixmap cursor_scroll_pix;
50   Pixmap cursor_scroll_mask;
51 
52   Cursor cursor_zoom;
53   Pixmap cursor_zoom_pix;
54   Pixmap cursor_zoom_mask;
55 
56   Cursor cursor_insert;
57   Pixmap cursor_insert_pix;
58   Pixmap cursor_insert_mask;
59 
60   Cursor cursor_select;
61   Pixmap cursor_select_pix;
62   Pixmap cursor_select_mask;
63 
64   Cursor cursor_collider;
65   Pixmap cursor_collider_pix;
66   Pixmap cursor_collider_mask;
67 
68   bool doublebuffer;
69 #ifdef HAVE_LIBXXF86VM
70   XF86VidModeModeLine orig_modeline;
71 #endif
72   int orig_viewport_x;
73   int orig_viewport_y;
74   int orig_dotclock;
75 
76   int       width;
77   int       height;
78   Display*  display;
79   Window    window;
80   Colormap  colormap;
81   Drawable  drawable;
82   GC        gc;
83 
84   bool shift_pressed;
85   int  mouse_x;
86   int  mouse_y;
87 
88   /** Color Depth of the Display */
89   int depth;
90 
91   /** true if display is in fullscreen mode, false for window mode */
92   bool fullscreen;
93 
94   std::vector<FlipRect> flip_rects;
95   std::vector<FlipRect> last_flip_rects;
96 
97   /** Save the current visual mode for later restoration after leaving
98       fullscreen */
99   void save_mode();
100 public:
101   X11Display (int w, int h, bool fullscreen_);
102   virtual ~X11Display ();
103 
104   // Graphic Context stuff
105   void draw_lines (std::vector<Line>& lines, Color color, int wide = 0);
106   void draw_line(float x1, float y1, float x2, float y2, Color color, int wide = 0);
107   void draw_rect(float x1, float y1, float x2, float y2, Color color);
108   void draw_fill_rect(float x1, float y1, float x2, float y2, Color color);
109   void draw_circle(float x, float y, float r, Color color);
110   void draw_circles(std::vector<Circle>& circles, Color color);
111 
112   void draw_fill_circle(float x, float y, float r, Color color);
113   void draw_string(float x, float y, const std::string& str, Color color);
114   void draw_string_centered(float x, float y, const std::string& str, Color color);
115 
get_width()116   int get_width () { return width; }
get_height()117   int get_height () { return height; }
118 
119   void toggle_fullscreen();
120 
121   void clear ();
122 
123   /** Flip the double buffered display */
124   void flip ();
125 
126   void enter_fullscreen();
127   void leave_fullscreen();
128 
129   /** perform the real flip, only flip marked reagions */
130   void real_flip ();
131 
132   void flip (int x1, int y1, int x2, int y2);
133 
134   // Input Context stuff
135   int get_mouse_x ();
136   int get_mouse_y ();
137 
138   bool get_key (int key);
139 
140   /** Waits for events to come in, blocks until new events are available */
141   void wait_for_events_blocking ();
142 
143   void wait_for_events ();
144 
145   void run();
146 
147   void set_clip_rect (int x1_, int y1_, int x2_, int y2_);
148 
149   unsigned int get_color_value(const Color& color);
150   XColor get_xcolor(const Color& color);
151 
152   void set_cursor_real(CursorType cursor);
153 private:
154   bool read_event ();
155   void send_button_press (int i);
156   void send_button_release (int i);
157   void send_load_or_save(int n);
158 
159   X11Display (const X11Display&);
160   X11Display& operator= (const X11Display&);
161 };
162 
163 #endif
164 
165 /* EOF */
166