aboutsummaryrefslogtreecommitdiff
path: root/util/fbcompose/plugins/opengl/preview/PreviewPlugin.cc
blob: 97faf3409d16ca868ab989d49a5d0eab3689490c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/** PreviewPlugin.cc file for the fluxbox compositor. */

// Copyright (c) 2011 Gediminas Liktaras (gliktaras at gmail dot com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#include "PreviewPlugin.hh"

#include "BaseScreen.hh"
#include "Exceptions.hh"
#include "OpenGLScreen.hh"
#include "OpenGLWindow.hh"
#include "Utility.hh"

#include <algorithm>
#include <iostream>

using namespace FbCompositor;


//--- SHADER SOURCES -----------------------------------------------------------

/** Plugin's fragment shader source. */
const GLchar FRAGMENT_SHADER[] = "\
    void preview() { }                                                       \n\
";

/** Plugin's vertex shader source. */
const GLchar VERTEX_SHADER[] = "\
    void preview() { }                                                       \n\
";


//--- CONSTANTS ----------------------------------------------------------------

/** Maximum height of the preview window. */
const int MAX_PREVIEW_HEIGHT = 150;

/** Maximum width of the preview window. */
const int MAX_PREVIEW_WIDTH = 150;

/** Transparency of the preview window. */
const unsigned int PREVIEW_ALPHA = 200;

/** Time in microseconds until the preview window is shown. */
const int SLEEP_TIME = 500000;


//--- CONSTRUCTORS AND DESTRUCTORS ---------------------------------------------

// Constructor.
PreviewPlugin::PreviewPlugin(const BaseScreen &screen, const std::vector<FbTk::FbString> &args) :
    OpenGLPlugin(screen, args) {

    m_tick_tracker.setTickSize(SLEEP_TIME);
}

// Destructor.
PreviewPlugin::~PreviewPlugin() { }


//--- ACCESSORS ----------------------------------------------------------------

// Returns the additional source code for the fragment shader.
const char *PreviewPlugin::fragmentShader() const {
    return FRAGMENT_SHADER;
}

// Returns the additional source code for the vertex shader.
const char *PreviewPlugin::vertexShader() const {
    return VERTEX_SHADER;
}


//--- WINDOW EVENT CALLBACKS ---------------------------------------------------

// Called, whenever a new window is created.
void PreviewPlugin::windowCreated(const BaseCompWindow &window) {
    const OpenGLWindow &gl_window = dynamic_cast<const OpenGLWindow&>(window);

    OpenGLRenderingJob job;
    job.prim_pos_buffer = new OpenGLBuffer(openGLScreen(), GL_ARRAY_BUFFER);
    job.main_tex_coord_buffer = openGLScreen().defaultTexCoordBuffer();
    job.shape_tex_coord_buffer = openGLScreen().defaultTexCoordBuffer();
    job.alpha = PREVIEW_ALPHA / 255.0f;
    job.shader_init = new NullInitializer();
    job.shader_deinit = new NullDeinitializer();

    PreviewWindowData win_data = { gl_window, job };
    m_preview_data.insert(std::make_pair(gl_window.window(), win_data));
}

/** Called, whenever a window is destroyed. */
void PreviewPlugin::windowDestroyed(const BaseCompWindow &window) {
    std::map<Window, PreviewWindowData>::iterator it = m_preview_data.find(window.window());
    if (it != m_preview_data.end()) {
        m_preview_data.erase(it);
    }
}


//--- RENDERING ACTIONS --------------------------------------------------------

/** Extra rendering actions and jobs. */
const std::vector<OpenGLRenderingJob> &PreviewPlugin::extraRenderingActions() {
    m_extra_jobs.clear();

    std::map<Window, PreviewWindowData>::iterator it = m_preview_data.find(screen().currentIconbarItem());
    if (it != m_preview_data.end()) {
        PreviewWindowData &cur_preview = it->second;

        if (!m_tick_tracker.isRunning()) {
            m_tick_tracker.start();
        }

        if (cur_preview.window.partitionCount() > 0) {
            updatePreviewWindow(cur_preview);
            if (m_tick_tracker.totalElapsedTicks() > 0) {
                m_extra_jobs.push_back(cur_preview.job);
            }
        }
    } else {
        m_tick_tracker.stop();
    }

    return m_extra_jobs;
}


//--- INTERNAL FUNCTIONS -------------------------------------------------------

// Update the preview window.
// TODO: Place the preview window on the edge of the toolbar.
// TODO: Left/Right toolbar orientations.
// TODO: Use all window texture partitions.
void PreviewPlugin::updatePreviewWindow(PreviewWindowData &win_preview) {
    XRectangle thumb_dim;

    // Find thumbnail's width and height.
    int full_thumb_width = std::min<int>(win_preview.window.realWidth(), openGLScreen().maxTextureSize());
    int full_thumb_height = std::min<int>(win_preview.window.realHeight(), openGLScreen().maxTextureSize());

    double scale_factor = 1.0;
    scale_factor = std::max(scale_factor, full_thumb_width / double(MAX_PREVIEW_WIDTH));
    scale_factor = std::max(scale_factor, full_thumb_height / double(MAX_PREVIEW_HEIGHT));

    thumb_dim.width = static_cast<int>(full_thumb_width / scale_factor);
    thumb_dim.height = static_cast<int>(full_thumb_height / scale_factor);

    // Find thumbnail's X and Y coordinates.
    int mouse_pos_x, mouse_pos_y;
    mousePointerLocation(screen(), mouse_pos_x, mouse_pos_y);

    if (screen().heads().size() > 0) {
        XRectangle cur_head = screen().heads()[0];

        // First find which head the mouse pointer is on,
        for (size_t i = 1; i < screen().heads().size(); i++) {
            XRectangle head = screen().heads()[i];
            if ((mouse_pos_x >= head.x) && (mouse_pos_y >= head.y)
                    && (mouse_pos_x < (head.x + head.width))
                    && (mouse_pos_y < (head.y + head.height))) {
                cur_head = head;
                break;
            }
        }

        // then position the thumbnail there.
        thumb_dim.x = mouse_pos_x - thumb_dim.width / 2;

        int mid_head = cur_head.y + (cur_head.height / 2);
        if (mouse_pos_y < mid_head) {
            thumb_dim.y = mouse_pos_y + 10;
        } else {
            thumb_dim.y = mouse_pos_y - thumb_dim.height - 10;
        }
    } else {    // But WHAT IF we have no heads?
        thumb_dim.x = mouse_pos_x - (thumb_dim.width / 2);
        thumb_dim.y = mouse_pos_y + 10;
    }

    // Update job struct variables.
    win_preview.job.prim_pos_buffer->bufferPosRectangle(screen().rootWindow().width(), screen().rootWindow().height(), thumb_dim);

    win_preview.job.main_texture = win_preview.window.contentTexturePartition(0);
    win_preview.job.shape_texture = win_preview.window.shapeTexturePartition(0);
}


//--- PLUGIN MANAGER FUNCTIONS -------------------------------------------------

// Creates a plugin object.
extern "C" BasePlugin *createPlugin(const BaseScreen &screen, const std::vector<FbTk::FbString> &args) {
    return new PreviewPlugin(screen, args);
}

// Returns the type of the plugin.
extern "C" FbCompositor::PluginType pluginType() {
    return Plugin_OpenGL;
}