aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc
diff options
context:
space:
mode:
authorGediminas Liktaras <gliktaras@gmail.com>2011-12-08 13:34:09 (GMT)
committerPaul Tagliamonte <paultag@fluxbox.org>2011-12-10 16:13:19 (GMT)
commitcd339169d1961eb508ea89cee2609ec6d0fc0c15 (patch)
tree01acd158a03fb17a72e067ff0b36701da75e49dc /util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc
parent85ac5c4b2c6a526992f483a6e91867dc2f82a19e (diff)
downloadfluxbox_paul-cd339169d1961eb508ea89cee2609ec6d0fc0c15.zip
fluxbox_paul-cd339169d1961eb508ea89cee2609ec6d0fc0c15.tar.bz2
fbcompose - A compositing addon for fluxbox window manager.
fbcompose(1) is an optional compositing addon for fluxbox window manager. It augments fluxbox with a number of graphical features. Most notably, fbcompose allows fluxbox to properly display applications that require compositing (docky, for example), adds support for true window transparency (as opposed to fluxbox's pseudo transparency) and provides a plugin framework to extend the compositor's functionality. As this is still a beta version of the compositor, the bugs are likely.
Diffstat (limited to 'util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc')
-rw-r--r--util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc217
1 files changed, 217 insertions, 0 deletions
diff --git a/util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc b/util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc
new file mode 100644
index 0000000..97faf34
--- /dev/null
+++ b/util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc
@@ -0,0 +1,217 @@
1/** PreviewPlugin.cc file for the fluxbox compositor. */
2
3// Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com)
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21// THE SOFTWARE.
22
23
24#include "PreviewPlugin.hh"
25
26#include "BaseScreen.hh"
27#include "Exceptions.hh"
28#include "OpenGLScreen.hh"
29#include "OpenGLWindow.hh"
30#include "Utility.hh"
31
32#include <algorithm>
33#include <iostream>
34
35using namespace FbCompositor;
36
37
38//--- SHADER SOURCES -----------------------------------------------------------
39
40/** Plugin's fragment shader source. */
41const GLchar FRAGMENT_SHADER[] = "\
42 void preview() { } \n\
43";
44
45/** Plugin's vertex shader source. */
46const GLchar VERTEX_SHADER[] = "\
47 void preview() { } \n\
48";
49
50
51//--- CONSTANTS ----------------------------------------------------------------
52
53/** Maximum height of the preview window. */
54const int MAX_PREVIEW_HEIGHT = 150;
55
56/** Maximum width of the preview window. */
57const int MAX_PREVIEW_WIDTH = 150;
58
59/** Transparency of the preview window. */
60const unsigned int PREVIEW_ALPHA = 200;
61
62/** Time in microseconds until the preview window is shown. */
63const int SLEEP_TIME = 500000;
64
65
66//--- CONSTRUCTORS AND DESTRUCTORS ---------------------------------------------
67
68// Constructor.
69PreviewPlugin::PreviewPlugin(const BaseScreen &screen, const std::vector<FbTk::FbString> &args) :
70 OpenGLPlugin(screen, args) {
71
72 m_tick_tracker.setTickSize(SLEEP_TIME);
73}
74
75// Destructor.
76PreviewPlugin::~PreviewPlugin() { }
77
78
79//--- ACCESSORS ----------------------------------------------------------------
80
81// Returns the additional source code for the fragment shader.
82const char *PreviewPlugin::fragmentShader() const {
83 return FRAGMENT_SHADER;
84}
85
86// Returns the additional source code for the vertex shader.
87const char *PreviewPlugin::vertexShader() const {
88 return VERTEX_SHADER;
89}
90
91
92//--- WINDOW EVENT CALLBACKS ---------------------------------------------------
93
94// Called, whenever a new window is created.
95void PreviewPlugin::windowCreated(const BaseCompWindow &window) {
96 const OpenGLWindow &gl_window = dynamic_cast<const OpenGLWindow&>(window);
97
98 OpenGLRenderingJob job;
99 job.prim_pos_buffer = new OpenGLBuffer(openGLScreen(), GL_ARRAY_BUFFER);
100 job.main_tex_coord_buffer = openGLScreen().defaultTexCoordBuffer();
101 job.shape_tex_coord_buffer = openGLScreen().defaultTexCoordBuffer();
102 job.alpha = PREVIEW_ALPHA / 255.0f;
103 job.shader_init = new NullInitializer();
104 job.shader_deinit = new NullDeinitializer();
105
106 PreviewWindowData win_data = { gl_window, job };
107 m_preview_data.insert(std::make_pair(gl_window.window(), win_data));
108}
109
110/** Called, whenever a window is destroyed. */
111void PreviewPlugin::windowDestroyed(const BaseCompWindow &window) {
112 std::map<Window, PreviewWindowData>::iterator it = m_preview_data.find(window.window());
113 if (it != m_preview_data.end()) {
114 m_preview_data.erase(it);
115 }
116}
117
118
119//--- RENDERING ACTIONS --------------------------------------------------------
120
121/** Extra rendering actions and jobs. */
122const std::vector<OpenGLRenderingJob> &PreviewPlugin::extraRenderingActions() {
123 m_extra_jobs.clear();
124
125 std::map<Window, PreviewWindowData>::iterator it = m_preview_data.find(screen().currentIconbarItem());
126 if (it != m_preview_data.end()) {
127 PreviewWindowData &cur_preview = it->second;
128
129 if (!m_tick_tracker.isRunning()) {
130 m_tick_tracker.start();
131 }
132
133 if (cur_preview.window.partitionCount() > 0) {
134 updatePreviewWindow(cur_preview);
135 if (m_tick_tracker.totalElapsedTicks() > 0) {
136 m_extra_jobs.push_back(cur_preview.job);
137 }
138 }
139 } else {
140 m_tick_tracker.stop();
141 }
142
143 return m_extra_jobs;
144}
145
146
147//--- INTERNAL FUNCTIONS -------------------------------------------------------
148
149// Update the preview window.
150// TODO: Place the preview window on the edge of the toolbar.
151// TODO: Left/Right toolbar orientations.
152// TODO: Use all window texture partitions.
153void PreviewPlugin::updatePreviewWindow(PreviewWindowData &win_preview) {
154 XRectangle thumb_dim;
155
156 // Find thumbnail's width and height.
157 int full_thumb_width = std::min<int>(win_preview.window.realWidth(), openGLScreen().maxTextureSize());
158 int full_thumb_height = std::min<int>(win_preview.window.realHeight(), openGLScreen().maxTextureSize());
159
160 double scale_factor = 1.0;
161 scale_factor = std::max(scale_factor, full_thumb_width / double(MAX_PREVIEW_WIDTH));
162 scale_factor = std::max(scale_factor, full_thumb_height / double(MAX_PREVIEW_HEIGHT));
163
164 thumb_dim.width = static_cast<int>(full_thumb_width / scale_factor);
165 thumb_dim.height = static_cast<int>(full_thumb_height / scale_factor);
166
167 // Find thumbnail's X and Y coordinates.
168 int mouse_pos_x, mouse_pos_y;
169 mousePointerLocation(screen(), mouse_pos_x, mouse_pos_y);
170
171 if (screen().heads().size() > 0) {
172 XRectangle cur_head = screen().heads()[0];
173
174 // First find which head the mouse pointer is on,
175 for (size_t i = 1; i < screen().heads().size(); i++) {
176 XRectangle head = screen().heads()[i];
177 if ((mouse_pos_x >= head.x) && (mouse_pos_y >= head.y)
178 && (mouse_pos_x < (head.x + head.width))
179 && (mouse_pos_y < (head.y + head.height))) {
180 cur_head = head;
181 break;
182 }
183 }
184
185 // then position the thumbnail there.
186 thumb_dim.x = mouse_pos_x - thumb_dim.width / 2;
187
188 int mid_head = cur_head.y + (cur_head.height / 2);
189 if (mouse_pos_y < mid_head) {
190 thumb_dim.y = mouse_pos_y + 10;
191 } else {
192 thumb_dim.y = mouse_pos_y - thumb_dim.height - 10;
193 }
194 } else { // But WHAT IF we have no heads?
195 thumb_dim.x = mouse_pos_x - (thumb_dim.width / 2);
196 thumb_dim.y = mouse_pos_y + 10;
197 }
198
199 // Update job struct variables.
200 win_preview.job.prim_pos_buffer->bufferPosRectangle(screen().rootWindow().width(), screen().rootWindow().height(), thumb_dim);
201
202 win_preview.job.main_texture = win_preview.window.contentTexturePartition(0);
203 win_preview.job.shape_texture = win_preview.window.shapeTexturePartition(0);
204}
205
206
207//--- PLUGIN MANAGER FUNCTIONS -------------------------------------------------
208
209// Creates a plugin object.
210extern "C" BasePlugin *createPlugin(const BaseScreen &screen, const std::vector<FbTk::FbString> &args) {
211 return new PreviewPlugin(screen, args);
212}
213
214// Returns the type of the plugin.
215extern "C" FbCompositor::PluginType pluginType() {
216 return Plugin_OpenGL;
217}