summaryrefslogtreecommitdiffstats
path: root/kgamma/kcmkgamma/xvidextwrap.cpp
blob: f7bf532bdfe2e27a88d5ae5633170651bda37465 (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
/***************************************************************************
                          xvidextwrap.cpp  -  description
                             -------------------
    begin                : Sat Jan 5 2002
    copyright            : (C) 2002 by Michael v.Ostheim
    email                : MvOstheim@web.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
#include <tqnamespace.h>
#include <tqevent.h>

#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/xf86vmode.h>

#include <stdlib.h>
#include <iostream>
#include <fstream>

#include <vector>
#include <string>

#include <config.h>

#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream.h>
#define istringstream istrstream
#endif

#include <kdebug.h>

#include "xf86configpath.h"
#include "xvidextwrap.h"

using namespace std;


XVidExtWrap::XVidExtWrap(bool* OK, const char* displayname) {
  if ((dpy = XOpenDisplay(displayname))) {
    screen = DefaultScreen(dpy);
    setGammaLimits(0.1, 10.0);
    *OK = true;
  }
  else {
    kdDebug() << "KGamma: unable to open display " << displayname << endl;
    *OK = false;
  }
}

XVidExtWrap::~XVidExtWrap() {
  if (dpy) XCloseDisplay(dpy);
}

int XVidExtWrap::_DefaultScreen() {
  return DefaultScreen(dpy);
}

/** We have to parse XF86Config to get the number of screens, because */
/** ScreenCount() from X11/Xlib.h does not count correctly with xinerama */
int XVidExtWrap::_ScreenCount() {
  int count = 0;
  bool section = false;
  XF86ConfigPath Path;

  std::ifstream in( Path.get() );

  if ( in.is_open() ) {
    std::string s, buf;
    std::vector<string> words;
      
    while (getline(in, s,'\n')) {
      words.clear();
      istringstream ss(s.c_str());
      while (ss >> buf) words.push_back(buf);  // Split "s" into words

      if ( !words.empty() ) {
        if ( words[0] == "Section" && words.size() > 1 ) {
          if ( words[1] == "\"ServerLayout\"" ) section = true;
        }
        else if ( words[0] == "EndSection" )
           section = false;
        if ( section && words[0] == "Screen" ) ++count;
      }
    } //end while
    in.close();

  }

  if ( !count ) count = 1;  //If failed,fill count with a valid value;
  
  return( count );
}

const char* XVidExtWrap::DisplayName() {
  return DisplayString(dpy);
}

void XVidExtWrap::setGamma(int channel, float gam, bool* OK) {
  XF86VidModeGamma gamma;

  if ( gam >= mingamma && gam <= maxgamma ) {
    if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
      kdDebug() << "KGamma: Unable to query gamma correction" << endl;
      if ( OK ) *OK = false;
    }
    else {
      switch (channel) {
        case Value:
          gamma.red = gam;
          gamma.green = gam;
          gamma.blue = gam; break;
        case Red:
          gamma.red = gam; break;
        case Green:
          gamma.green = gam; break;
        case Blue:
          gamma.blue = gam;
      };
      if (!XF86VidModeSetGamma(dpy, screen, &gamma)) {
        kdDebug() << "KGamma: Unable to set gamma correction" << endl;
        if ( OK ) *OK = false;
      }
      else {
        XFlush(dpy);
        if ( OK ) *OK = true;
      }
    }
  }
}

float XVidExtWrap::getGamma(int channel, bool* OK) {
  XF86VidModeGamma gamma;
  float gam = 0;

  if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
    kdDebug() << "KGamma: Unable to query gamma correction" << endl;
    if ( OK ) *OK = false;
  }
  else {
    switch (channel) {
      case Value:
        gam = gamma.red; break;
      case Red:
        gam = gamma.red; break;
      case Green:
        gam = gamma.green; break;
      case Blue:
        gam = gamma.blue;
    };
    if ( OK ) *OK = true;
  }
  return(gam);
}

void XVidExtWrap::setGammaLimits( float min, float max ) {
  mingamma = (min < 0.1) ? 0.1 : min;
  maxgamma = (max > 10.0) ? 10.0 : max;
}

#ifdef istringstream
#undef istringstream
#endif