瀏覽代碼

Added basic textured themes.

Dalton Nell 9 年之前
父節點
當前提交
2200ce763a
共有 6 個檔案被更改,包括 257 行新增5 行删除
  1. 10
    0
      CMakeLists.txt
  2. 115
    5
      src/glselectrectangle.cpp
  3. 10
    0
      src/glselectrectangle.hpp
  4. 2
    0
      src/main.cpp
  5. 68
    0
      src/resource.cpp
  6. 52
    0
      src/resource.hpp

+ 10
- 0
CMakeLists.txt 查看文件

@@ -79,6 +79,7 @@ set( source
79 79
      src/cmdline.c
80 80
      src/selectrectangle.cpp
81 81
      src/glselectrectangle.cpp
82
+     src/resource.cpp
82 83
      src/xselectrectangle.cpp
83 84
      src/x.cpp
84 85
      src/main.cpp )
@@ -91,6 +92,7 @@ find_package( OpenGL    REQUIRED )
91 92
 find_package( GLX       REQUIRED )
92 93
 find_package( XRender   REQUIRED )
93 94
 find_package( XRandr    REQUIRED )
95
+find_package( DevIL     REQUIRED )
94 96
 # This library is needed only for Ubuntu it seems, some platforms don't even
95 97
 # ship with it. I couldn't find a way to do a test compile to check if librt
96 98
 # was needed, so instead I just didn't mark it as REQUIRED.
@@ -105,12 +107,14 @@ if ( RT_INCLUDE_DIR )
105 107
                          ${XEXT_INCLUDE_DIR}
106 108
                          ${XRANDR_INCLUDE_DIR}
107 109
                          ${OPENGL_INCLUDE_DIR}
110
+                         ${IL_INCLUDE_DIR}
108 111
                          ${GLX_INCLUDE_DIR}
109 112
                          ${XRENDER_INCLUDE_DIRS}
110 113
                          ${RT_INCLUDE_DIR} )
111 114
 else()
112 115
     include_directories( ${X11_INCLUDE_DIR}
113 116
                          ${OPENGL_INCLUDE_DIR}
117
+                         ${IL_INCLUDE_DIR}
114 118
                          ${GLX_INCLUDE_DIR}
115 119
                          ${XEXT_INCLUDE_DIR} )
116 120
 endif()
@@ -123,6 +127,8 @@ if ( RT_LIBRARY )
123 127
     target_link_libraries( "${BIN_TARGET}"
124 128
                            ${X11_LIBRARIES}
125 129
                            ${OPENGL_LIBRARIES}
130
+                           ${IL_LIBRARIES}
131
+                           ${ILUT_LIBRARIES}
126 132
                            ${GLX_LIBRARIES}
127 133
                            ${XRENDER_LIBRARIES}
128 134
                            "${XEXT_LIBRARY}"
@@ -131,6 +137,8 @@ if ( RT_LIBRARY )
131 137
 else()
132 138
     target_link_libraries( "${BIN_TARGET}"
133 139
                            ${OPENGL_LIBRARIES}
140
+                           ${IL_LIBRARIES}
141
+                           ${ILUT_LIBRARIES}
134 142
                            ${GLX_LIBRARIES}
135 143
                            ${X11_LIBRARIES}
136 144
                            "${XEXT_LIBRARY}" )
@@ -138,3 +146,5 @@ endif()
138 146
 
139 147
 install( TARGETS ${BIN_TARGET}
140 148
          DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
149
+
150
+add_definitions(-DINSTALL_PREFIX="${CMAKE_INSTALL_PREFIX}")

+ 115
- 5
src/glselectrectangle.cpp 查看文件

@@ -222,6 +222,49 @@ void slop::GLSelectRectangle::generateMagnifyingGlass() {
222 222
     glDisable(GL_TEXTURE_2D);
223 223
 }
224 224
 
225
+void slop::GLSelectRectangle::setTheme( bool on, std::string name ) {
226
+    if ( !on ) {
227
+        return;
228
+    }
229
+    std::string root = resource->getRealPath( name );
230
+    std::string tl = root + "/corner_tl.png";
231
+    std::string bl = root + "/corner_bl.png";
232
+    std::string tr = root + "/corner_tr.png";
233
+    std::string br = root + "/corner_br.png";
234
+    std::string straight = root + "/straight.png";
235
+    // One of the textures didn't exist, so we cancel the theme.
236
+    if (!resource->validatePath( tl ) ||
237
+        !resource->validatePath( bl ) ||
238
+        !resource->validatePath( tr ) ||
239
+        !resource->validatePath( br ) ||
240
+        !resource->validatePath( straight ) ) {
241
+        fprintf( stderr, "One of the textures was missing in the theme... disabling.\n" );
242
+        return;
243
+    }
244
+    // Otherwise we load each one :)
245
+    ilInit();
246
+    ilutInit();
247
+    ilutRenderer(ILUT_OPENGL);
248
+    ILuint corner;
249
+    ilGenImages( 1, &corner );
250
+    ilBindImage( corner );
251
+    ilLoadImage( tl.c_str() );
252
+    m_cornerids[0] = ilutGLBindMipmaps();
253
+    ilLoadImage( tr.c_str() );
254
+    m_cornerids[1] = ilutGLBindMipmaps();
255
+    ilLoadImage( bl.c_str() );
256
+    m_cornerids[2] = ilutGLBindMipmaps();
257
+    ilLoadImage( br.c_str() );
258
+    m_cornerids[3] = ilutGLBindMipmaps();
259
+    ilLoadImage( straight.c_str() );
260
+    m_straightwidth = ilGetInteger( IL_IMAGE_WIDTH );
261
+    m_straightheight = ilGetInteger( IL_IMAGE_HEIGHT );
262
+    m_straightid = ilutGLBindMipmaps();
263
+    // And clean up after.
264
+    ilDeleteImages( 1, &corner );
265
+    m_themed = on;
266
+}
267
+
225 268
 slop::GLSelectRectangle::GLSelectRectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
226 269
     m_x = std::min( sx, ex );
227 270
     m_y = std::min( sy, ey );
@@ -242,6 +285,7 @@ slop::GLSelectRectangle::GLSelectRectangle( int sx, int sy, int ex, int ey, int
242 285
     m_glassSize = 4;
243 286
     m_glassBorder = 1;
244 287
     m_monitors = xengine->getCRTCS();
288
+    m_themed = false;
245 289
 
246 290
     // If we don't have a border, we don't exist, so just die.
247 291
     if ( m_border == 0 ) {
@@ -352,6 +396,8 @@ slop::GLSelectRectangle::GLSelectRectangle( int sx, int sy, int ex, int ey, int
352 396
     if ( !glXMakeContextCurrent( xengine->m_display, m_glxWindow, m_glxWindow, m_renderContext ) ) {
353 397
         fprintf( stderr, "Failed to attach GL context to window!\n" );
354 398
     }
399
+    glEnable( GL_BLEND );
400
+    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
355 401
     glClearColor( 0, 0, 0, 0 );
356 402
     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
357 403
     glXSwapBuffers( xengine->m_display, m_glxWindow );
@@ -382,11 +428,75 @@ void slop::GLSelectRectangle::update( double dt ) {
382 428
     glMatrixMode( GL_MODELVIEW );
383 429
     glLoadIdentity();
384 430
 
385
-    glColor4f( m_r, m_g, m_b, m_a );
386
-    glRecti( m_x-m_border, m_y, m_x+m_width+m_border, m_y-m_border );
387
-    glRecti( m_x-m_border, m_y+m_height, m_x+m_width+m_border, m_y+m_height+m_border );
388
-    glRecti( m_x-m_border, m_y, m_x, m_y+m_height );
389
-    glRecti( m_x+m_width, m_y, m_x+m_width+m_border, m_y+m_height );
431
+    if ( !m_themed ) {
432
+        glColor4f( m_r, m_g, m_b, m_a );
433
+        glRecti( m_x-m_border, m_y, m_x+m_width+m_border, m_y-m_border );
434
+        glRecti( m_x-m_border, m_y+m_height, m_x+m_width+m_border, m_y+m_height+m_border );
435
+        glRecti( m_x-m_border, m_y, m_x, m_y+m_height );
436
+        glRecti( m_x+m_width, m_y, m_x+m_width+m_border, m_y+m_height );
437
+    } else {
438
+        glColor4f( m_r, m_g, m_b, m_a );
439
+        glEnable( GL_TEXTURE_2D );
440
+        glBindTexture( GL_TEXTURE_2D, m_straightid );
441
+        float something = (float)(m_border)/(float)m_straightheight;
442
+        float txoffset = (((float)m_width+m_border)/(float)m_straightwidth)/something;
443
+        float tyoffset = (((float)m_height+m_border)/(float)m_straightwidth)/something;
444
+        //float ratio = ((float)m_straightwidth/(float)m_straightheight);
445
+        glBegin( GL_QUADS );
446
+        // straight top
447
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x-m_border/2, m_y-m_border );
448
+        glTexCoord2f(txoffset, 1.0); glVertex2f( m_x+m_width+m_border/2, m_y-m_border );
449
+        glTexCoord2f(txoffset, 0.0); glVertex2f( m_x+m_width+m_border/2, m_y );
450
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x-m_border/2, m_y );
451
+        // straight bot
452
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x-m_border/2, m_y+m_height );
453
+        glTexCoord2f(txoffset, 1.0); glVertex2f( m_x+m_width+m_border/2, m_y+m_height );
454
+        glTexCoord2f(txoffset, 0.0); glVertex2f( m_x+m_width+m_border/2, m_y+m_height+m_border );
455
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x-m_border/2, m_y+m_height+m_border );
456
+        // straight left
457
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x-m_border, m_y-m_border/2 );
458
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x, m_y-m_border/2 );
459
+        glTexCoord2f(tyoffset, 1.0); glVertex2f( m_x, m_y+m_height+m_border/2 );
460
+        glTexCoord2f(tyoffset, 0.0); glVertex2f( m_x-m_border, m_y+m_height+m_border/2 );
461
+        // straight right
462
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x+m_width, m_y-m_border/2 );
463
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x+m_width+m_border, m_y-m_border/2 );
464
+        glTexCoord2f(tyoffset, 1.0); glVertex2f( m_x+m_width+m_border, m_y+m_height+m_border/2 );
465
+        glTexCoord2f(tyoffset, 0.0); glVertex2f( m_x+m_width, m_y+m_height+m_border/2 );
466
+        glEnd();
467
+        // top left corner
468
+        glBindTexture( GL_TEXTURE_2D, m_cornerids[0] );
469
+        glBegin( GL_QUADS );
470
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x-m_border, m_y-m_border );
471
+        glTexCoord2f(1.0, 1.0); glVertex2f( m_x, m_y-m_border );
472
+        glTexCoord2f(1.0, 0.0); glVertex2f( m_x, m_y );
473
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x-m_border, m_y );
474
+        glEnd();
475
+        // top right
476
+        glBindTexture( GL_TEXTURE_2D, m_cornerids[1] );
477
+        glBegin( GL_QUADS );
478
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x+m_width, m_y-m_border );
479
+        glTexCoord2f(1.0, 1.0); glVertex2f( m_x+m_width+m_border, m_y-m_border );
480
+        glTexCoord2f(1.0, 0.0); glVertex2f( m_x+m_width+m_border, m_y );
481
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x+m_width, m_y );
482
+        glEnd();
483
+        // bottom left
484
+        glBindTexture( GL_TEXTURE_2D, m_cornerids[2] );
485
+        glBegin( GL_QUADS );
486
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x-m_border, m_y+m_height );
487
+        glTexCoord2f(1.0, 1.0); glVertex2f( m_x, m_y+m_height );
488
+        glTexCoord2f(1.0, 0.0); glVertex2f( m_x, m_y+m_height+m_border );
489
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x-m_border, m_y+m_height+m_border );
490
+        glEnd();
491
+        // bottom right
492
+        glBindTexture( GL_TEXTURE_2D, m_cornerids[2] );
493
+        glBegin( GL_QUADS );
494
+        glTexCoord2f(0.0, 1.0); glVertex2f( m_x+m_width, m_y+m_height );
495
+        glTexCoord2f(1.0, 1.0); glVertex2f( m_x+m_width+m_border, m_y+m_height );
496
+        glTexCoord2f(1.0, 0.0); glVertex2f( m_x+m_width+m_border, m_y+m_height+m_border );
497
+        glTexCoord2f(0.0, 0.0); glVertex2f( m_x+m_width, m_y+m_height+m_border );
498
+        glEnd();
499
+    }
390 500
 
391 501
     if ( m_glassEnabled ) {
392 502
         generateMagnifyingGlass();

+ 10
- 0
src/glselectrectangle.hpp 查看文件

@@ -23,12 +23,16 @@
23 23
 
24 24
 #include "x.hpp"
25 25
 #include "selectrectangle.hpp"
26
+#include "resource.hpp"
26 27
 
27 28
 #include <unistd.h>
28 29
 
29 30
 #include <GL/gl.h>
30 31
 #include <GL/glx.h>
31 32
 #include <GL/glxext.h>
33
+#define ILUT_USE_OPENGL
34
+#include <IL/il.h>
35
+#include <IL/ilut.h>
32 36
 #include <X11/Xlib.h>
33 37
 #include <X11/Xatom.h>
34 38
 #include <X11/extensions/Xrender.h>
@@ -54,6 +58,7 @@ public:
54 58
     void                pushOut( int* x, int* y, int w, int h, int rx, int ry, int rw, int rh );
55 59
     void                findOptimalGlassPosition();
56 60
     void                constrainWithinMonitor( int* x, int* y, int* w, int* h );
61
+    void                setTheme( bool on, std::string name );
57 62
     float               m_r;
58 63
     float               m_g;
59 64
     float               m_b;
@@ -64,7 +69,12 @@ public:
64 69
     GLXContext          m_renderContext;
65 70
     GLXWindow           m_glxWindow;
66 71
     Colormap            m_cmap;
72
+    bool                m_themed;
67 73
     unsigned int        m_texid;
74
+    unsigned int        m_cornerids[4];
75
+    unsigned int        m_straightid;
76
+    unsigned int        m_straightwidth;
77
+    unsigned int        m_straightheight;
68 78
     int                 m_offsetx;
69 79
     int                 m_offsety;
70 80
     int                 m_offsetw;

+ 2
- 0
src/main.cpp 查看文件

@@ -369,6 +369,7 @@ int app( int argc, char** argv ) {
369 369
                                                                      r, g, b, a );
370 370
                             // Haha why is this so hard to cast?
371 371
                             ((slop::GLSelectRectangle*)(selection))->setMagnifySettings( magenabled, magstrength, magpixels );
372
+                            ((slop::GLSelectRectangle*)(selection))->setTheme( true, "gothic" );
372 373
                         } else {
373 374
                             selection = new slop::XSelectRectangle( t.m_x, t.m_y,
374 375
                                                                     t.m_x + t.m_width,
@@ -426,6 +427,7 @@ int app( int argc, char** argv ) {
426 427
                                                                  r, g, b, a );
427 428
                         // Haha why is this so hard to cast?
428 429
                         ((slop::GLSelectRectangle*)(selection))->setMagnifySettings( magenabled, magstrength, magpixels );
430
+                        ((slop::GLSelectRectangle*)(selection))->setTheme( true, "gothic" );
429 431
                     } else {
430 432
                         selection = new slop::XSelectRectangle( sx, sy,
431 433
                                                                 ex, ey,

+ 68
- 0
src/resource.cpp 查看文件

@@ -0,0 +1,68 @@
1
+/* resource.cpp: Handles getting resources like textures and stuff.
2
+ *
3
+ * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
4
+ *
5
+ * This file is part of Slop.
6
+ *
7
+ * Slop is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * Slop is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with Slop.  If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+#include "resource.hpp"
22
+
23
+slop::Resource* resource = new slop::Resource();
24
+
25
+slop::Resource::Resource() {
26
+    // Find the configuration directory.
27
+    // usually ~/.config/slop and /usr/share/slop
28
+    char* config = getenv( "XDG_CONFIG_HOME" );
29
+    if ( config == NULL ) {
30
+        char* home = getpwuid(getuid())->pw_dir;
31
+        m_usrconfig += home;
32
+        m_usrconfig += "/.config/slop/";
33
+        return;
34
+    }
35
+    m_usrconfig += config;
36
+    m_usrconfig += "/slop/";
37
+    m_sysconfig = INSTALL_PREFIX;
38
+    m_sysconfig += "/share/slop";
39
+}
40
+
41
+std::string slop::Resource::getRealPath( std::string localpath ) {
42
+    if ( !validatePath( m_usrconfig + localpath ) ) {
43
+        if ( !validatePath( m_sysconfig + localpath ) ) {
44
+            fprintf( stderr, "Failed to find directory %s in %s or %s...\n", localpath.c_str(), m_usrconfig.c_str(), m_sysconfig.c_str() );
45
+        }
46
+        return m_sysconfig + localpath;
47
+    }
48
+    return m_usrconfig + localpath;
49
+}
50
+
51
+bool slop::Resource::validatePath( std::string path ) {
52
+
53
+    struct stat st;
54
+
55
+    const char* dirname = path.c_str();
56
+    if ( stat( dirname, &st ) != 0 ) {
57
+        return false;
58
+    }
59
+    // No read permissions.
60
+    //if ( !(st.st_mode & S_IROTH) &&
61
+         //(st.st_uid != getuid() || !(st.st_mode & S_IRUSR)) &&
62
+         //(st.st_gid != getgid() || !(st.st_mode & S_IRGRP)) ) {
63
+        //return false;
64
+    //}
65
+    // Lookin' good!
66
+    return true;
67
+}
68
+

+ 52
- 0
src/resource.hpp 查看文件

@@ -0,0 +1,52 @@
1
+/* resource.hpp: Handles getting resources like textures and stuff.
2
+ *
3
+ * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
4
+ *
5
+ * This file is part of Slop.
6
+ *
7
+ * Slop is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * Slop is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with Slop.  If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+#ifndef IS_RESOURCE_H_
22
+#define IS_RESOURCE_H_
23
+
24
+#include <stdlib.h>
25
+#include <cstdio>
26
+#include <string>
27
+#include <unistd.h>
28
+#include <sys/types.h>
29
+#include <sys/param.h>
30
+#include <sys/types.h>
31
+#include <sys/stat.h>
32
+#include <pwd.h>
33
+
34
+namespace slop {
35
+
36
+class Resource {
37
+public:
38
+
39
+                    Resource();
40
+                    ~Resource();
41
+    std::string     getRealPath( std::string localpath );
42
+    bool            validatePath( std::string path );
43
+private:
44
+    std::string     m_usrconfig;
45
+    std::string     m_sysconfig;
46
+};
47
+
48
+}
49
+
50
+extern slop::Resource* resource;
51
+
52
+#endif // IS_RESOURCE_H_