File indexing completed on 2024-05-19 16:31:48

0001 /*
0002  *  SPDX-License-Identifier: GPL-2.0-or-later
0003  *
0004  *  SPDX-FileCopyrightText: 2002 Michael v.Ostheim <MvOstheim@web.de>
0005  */
0006 
0007 #include <QDebug>
0008 
0009 #include <fstream>
0010 #include <iostream>
0011 #include <stdlib.h>
0012 
0013 #include <sstream>
0014 #include <string>
0015 #include <vector>
0016 
0017 #include "xf86configpath.h"
0018 
0019 #include <X11/Xlib.h>
0020 #include <X11/Xos.h>
0021 #include <X11/extensions/xf86vmode.h>
0022 
0023 // #include <fixx11h.h>
0024 #include "xvidextwrap.h"
0025 
0026 using namespace std;
0027 
0028 XVidExtWrap::XVidExtWrap(bool *OK, const char *displayname)
0029 {
0030     if ((dpy = XOpenDisplay(displayname))) {
0031         screen = DefaultScreen(dpy);
0032         setGammaLimits(0.1, 10.0);
0033         *OK = true;
0034     } else {
0035         qDebug() << "KGamma: unable to open display " << displayname;
0036         *OK = false;
0037     }
0038 }
0039 
0040 XVidExtWrap::~XVidExtWrap()
0041 {
0042     if (dpy) {
0043         XCloseDisplay(dpy);
0044     }
0045 }
0046 
0047 int XVidExtWrap::_DefaultScreen()
0048 {
0049     return DefaultScreen(dpy);
0050 }
0051 
0052 /** We have to parse XF86Config to get the number of screens, because */
0053 /** ScreenCount() from X11/Xlib.h does not count correctly with xinerama */
0054 int XVidExtWrap::_ScreenCount()
0055 {
0056     int count = 0;
0057     bool section = false;
0058     XF86ConfigPath Path;
0059 
0060     std::ifstream in(Path.get());
0061 
0062     if (in.is_open()) {
0063         std::string s, buf;
0064         std::vector<string> words;
0065 
0066         while (getline(in, s, '\n')) {
0067             words.clear();
0068             istringstream ss(s.c_str());
0069             while (ss >> buf) {
0070                 words.push_back(buf); // Split "s" into words
0071             }
0072 
0073             if (!words.empty()) {
0074                 if (words[0] == "Section" && words.size() > 1) {
0075                     if (words[1] == "\"ServerLayout\"") {
0076                         section = true;
0077                     }
0078                 } else if (words[0] == "EndSection") {
0079                     section = false;
0080                 }
0081                 if (section && words[0] == "Screen") {
0082                     ++count;
0083                 }
0084             }
0085         } // end while
0086         in.close();
0087     }
0088 
0089     if (!count) {
0090         count = 1; // If failed,fill count with a valid value;
0091     }
0092 
0093     return (count);
0094 }
0095 
0096 const char *XVidExtWrap::DisplayName()
0097 {
0098     return DisplayString(dpy);
0099 }
0100 
0101 void XVidExtWrap::setGamma(int channel, float gam, bool *OK)
0102 {
0103     XF86VidModeGamma gamma;
0104 
0105     if (gam >= mingamma && gam <= maxgamma) {
0106         if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
0107             qDebug() << "KGamma: Unable to query gamma correction";
0108             if (OK) {
0109                 *OK = false;
0110             }
0111         } else {
0112             switch (channel) {
0113             case Value:
0114                 gamma.red = gam;
0115                 gamma.green = gam;
0116                 gamma.blue = gam;
0117                 break;
0118             case Red:
0119                 gamma.red = gam;
0120                 break;
0121             case Green:
0122                 gamma.green = gam;
0123                 break;
0124             case Blue:
0125                 gamma.blue = gam;
0126             };
0127             if (!XF86VidModeSetGamma(dpy, screen, &gamma)) {
0128                 qDebug() << "KGamma: Unable to set gamma correction";
0129                 if (OK) {
0130                     *OK = false;
0131                 }
0132             } else {
0133                 XFlush(dpy);
0134                 if (OK) {
0135                     *OK = true;
0136                 }
0137             }
0138         }
0139     }
0140 }
0141 
0142 float XVidExtWrap::getGamma(int channel, bool *OK)
0143 {
0144     XF86VidModeGamma gamma;
0145     float gam = 0;
0146 
0147     if (!XF86VidModeGetGamma(dpy, screen, &gamma)) {
0148         qDebug() << "KGamma: Unable to query gamma correction";
0149         if (OK) {
0150             *OK = false;
0151         }
0152     } else {
0153         switch (channel) {
0154         case Value:
0155             gam = gamma.red;
0156             break;
0157         case Red:
0158             gam = gamma.red;
0159             break;
0160         case Green:
0161             gam = gamma.green;
0162             break;
0163         case Blue:
0164             gam = gamma.blue;
0165         };
0166         if (OK) {
0167             *OK = true;
0168         }
0169     }
0170     return (gam);
0171 }
0172 
0173 void XVidExtWrap::setGammaLimits(float min, float max)
0174 {
0175     mingamma = (min < 0.1) ? 0.1 : min;
0176     maxgamma = (max > 10.0) ? 10.0 : max;
0177 }