-
Notifications
You must be signed in to change notification settings - Fork 177
/
invert.cpp
149 lines (118 loc) · 3.44 KB
/
invert.cpp
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
#include <wayfire/per-output-plugin.hpp>
#include <wayfire/output.hpp>
#include <wayfire/opengl.hpp>
#include <wayfire/render-manager.hpp>
static const char *vertex_shader =
R"(
#version 100
attribute mediump vec2 position;
attribute highp vec2 uvPosition;
varying highp vec2 uvpos;
void main() {
gl_Position = vec4(position.xy, 0.0, 1.0);
uvpos = uvPosition;
}
)";
static const char *fragment_shader =
R"(
#version 100
varying highp vec2 uvpos;
uniform sampler2D smp;
uniform bool preserve_hue;
void main()
{
mediump vec4 tex = texture2D(smp, uvpos);
if (preserve_hue)
{
mediump float hue = tex.a - min(tex.r, min(tex.g, tex.b)) - max(tex.r, max(tex.g, tex.b));
gl_FragColor = hue + tex;
} else
{
gl_FragColor = vec4(1.0 - tex.r, 1.0 - tex.g, 1.0 - tex.b, 1.0);
}
}
)";
class wayfire_invert_screen : public wf::per_output_plugin_instance_t
{
wf::post_hook_t hook;
wf::activator_callback toggle_cb;
wf::option_wrapper_t<bool> preserve_hue{"invert/preserve_hue"};
bool active = false;
OpenGL::program_t program;
wf::plugin_activation_data_t grab_interface = {
.name = "invert",
.capabilities = 0,
};
public:
void init() override
{
wf::option_wrapper_t<wf::activatorbinding_t> toggle_key{"invert/toggle"};
hook = [=] (const wf::framebuffer_t& source,
const wf::framebuffer_t& destination)
{
render(source, destination);
};
toggle_cb = [=] (auto)
{
if (!output->can_activate_plugin(&grab_interface))
{
return false;
}
if (active)
{
output->render->rem_post(&hook);
} else
{
output->render->add_post(&hook);
}
active = !active;
return true;
};
OpenGL::render_begin();
program.set_simple(
OpenGL::compile_program(vertex_shader, fragment_shader));
OpenGL::render_end();
output->add_activator(toggle_key, &toggle_cb);
}
void render(const wf::framebuffer_t& source,
const wf::framebuffer_t& destination)
{
static const float vertexData[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
1.0f, 1.0f,
-1.0f, 1.0f
};
static const float coordData[] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
OpenGL::render_begin(destination);
program.use(wf::TEXTURE_TYPE_RGBA);
GL_CALL(glBindTexture(GL_TEXTURE_2D, source.tex));
GL_CALL(glActiveTexture(GL_TEXTURE0));
program.attrib_pointer("position", 2, 0, vertexData);
program.attrib_pointer("uvPosition", 2, 0, coordData);
program.uniform1i("preserve_hue", preserve_hue);
GL_CALL(glDisable(GL_BLEND));
GL_CALL(glDrawArrays(GL_TRIANGLE_FAN, 0, 4));
GL_CALL(glEnable(GL_BLEND));
GL_CALL(glBindTexture(GL_TEXTURE_2D, 0));
program.deactivate();
OpenGL::render_end();
}
void fini() override
{
if (active)
{
output->render->rem_post(&hook);
}
OpenGL::render_begin();
program.free_resources();
OpenGL::render_end();
output->rem_binding(&toggle_cb);
}
};
DECLARE_WAYFIRE_PLUGIN(wf::per_output_plugin_t<wayfire_invert_screen>);