2 vkeyb - camera motion detection virtual keyboard
3 Copyright (C) 2012 Eleni Maria Stea <elene.mst@gmail.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <X11/keysym.h>
26 static KeySym charmap[] = {
52 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
53 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
56 static unsigned int load_texture(const char *fname);
61 if(!(tex = load_texture("data/glyphs.png"))) {
70 glDeleteTextures(1, &tex);
73 void VKeyb::show() const
75 float uoffs = floor(offset) / num_glyphs;
76 float umax = (float)visible_glyphs / num_glyphs;
78 glEnable(GL_TEXTURE_2D);
79 glBindTexture(GL_TEXTURE_2D, tex);
83 glTexCoord2f(uoffs, 1);
85 glTexCoord2f(uoffs + umax, 1);
87 glTexCoord2f(uoffs + umax, 0);
89 glTexCoord2f(uoffs, 0);
93 glDisable(GL_TEXTURE_2D);
95 float rect_width = 2.0 / visible_glyphs;
98 glBegin(GL_LINE_LOOP);
101 glVertex2f(rect_width, -1);
102 glVertex2f(rect_width, 1);
107 void VKeyb::move(float offs)
109 float tmp = offset + offs;
112 offset = fmod(num_glyphs + tmp, num_glyphs);
114 offset = fmod(tmp, num_glyphs);
119 static unsigned int load_texture(const char *fname)
125 if(!(pixels = img_load_pixels(fname, &xsz, &ysz, IMG_FMT_RGBA32))) {
126 fprintf(stderr, "failed to load image: %s\n", fname);
130 glGenTextures(1, &tex);
131 glBindTexture(GL_TEXTURE_2D, tex);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
135 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
136 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, xsz, ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
138 if(glGetError() != GL_NO_ERROR) {
139 img_free_pixels(pixels);
143 img_free_pixels(pixels);
147 int VKeyb::active_glyph() const
149 return (int)(offset + visible_glyphs / 2) % num_glyphs;
152 KeySym VKeyb::active_key() const
154 return charmap[active_glyph()];