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 <stdio.h>
0008 
0009 #include <stdlib.h>
0010 
0011 #include <fstream>
0012 #include <iostream>
0013 #include <sstream>
0014 #include <string>
0015 #include <vector>
0016 
0017 using namespace std;
0018 
0019 int main(int argc, char *argv[])
0020 {
0021     bool cpyonly, secmon, success;
0022 
0023     cpyonly = secmon = success = false;
0024 
0025     if (!((argc - 1) % 3)) {
0026         int Screen = 0;
0027         int ScreenCount = (argc - 1) / 3;
0028 
0029         vector<string> searchPaths;
0030 
0031         // Xorg.conf file (the default config file now)
0032         searchPaths.push_back("/etc/X11/xorg.conf-4");
0033         searchPaths.push_back("/etc/X11/xorg.conf");
0034         searchPaths.push_back("/etc/xorg.conf");
0035         searchPaths.push_back("/usr/X11R6/etc/X11/xorg.conf-4");
0036         searchPaths.push_back("/usr/X11R6/etc/X11/xorg.conf");
0037         searchPaths.push_back("/usr/X11R6/lib/X11/xorg.conf-4");
0038         searchPaths.push_back("/usr/X11R6/lib/X11/xorg.conf");
0039 
0040         // Deprecated XF86Config file (Xfree86 times)
0041         searchPaths.push_back("/etc/X11/XF86Config-4");
0042         searchPaths.push_back("/etc/X11/XF86Config");
0043         searchPaths.push_back("/etc/XF86Config");
0044         searchPaths.push_back("/usr/X11R6/etc/X11/XF86Config-4");
0045         searchPaths.push_back("/usr/X11R6/etc/X11/XF86Config");
0046         searchPaths.push_back("/usr/X11R6/lib/X11/XF86Config-4");
0047         searchPaths.push_back("/usr/X11R6/lib/X11/XF86Config");
0048 
0049         std::vector<string>::iterator it = searchPaths.begin();
0050         for (; it != searchPaths.end(); ++it) {
0051             // Try to open file
0052             std::ifstream in((*it).c_str());
0053 
0054             if (in.is_open()) {
0055                 std::ofstream out(((*it) + ".tmp").c_str());
0056                 if (out.is_open()) {
0057                     std::string s, buf;
0058                     std::vector<string> words;
0059 
0060                     while (getline(in, s, '\n')) {
0061                         if (!cpyonly) {
0062                             words.clear();
0063                             std::istringstream ss(s.c_str());
0064                             while (ss >> buf) {
0065                                 words.push_back(buf);
0066                             }
0067 
0068                             if (!words.empty()) {
0069                                 if (words[0] == "Section" && words.size() > 1) {
0070                                     if (words[1] == "\"Monitor\"") {
0071                                         secmon = true;
0072                                         out << s << endl;
0073                                         continue;
0074                                     }
0075                                 }
0076                                 if (secmon) {
0077                                     if (words[0] == "Gamma") {
0078                                         out << "  Gamma   " << argv[3 * Screen + 1] << "  " << argv[3 * Screen + 2] << "  " << argv[3 * Screen + 3];
0079                                         out << "  # created by KGamma" << endl;
0080                                         cpyonly = success = (++Screen == ScreenCount);
0081                                         secmon = false;
0082                                         continue;
0083                                     }
0084                                     if (words[0] == "EndSection") {
0085                                         out << "  Gamma   " << argv[3 * Screen + 1] << "  " << argv[3 * Screen + 2] << "  " << argv[3 * Screen + 3];
0086                                         out << "  # created by KGamma" << endl;
0087                                         out << s << endl;
0088                                         cpyonly = success = (++Screen == ScreenCount);
0089                                         secmon = false;
0090                                         continue;
0091                                     }
0092                                 }
0093                             }
0094                         }
0095                         out << s << endl;
0096                     } // endwhile
0097 
0098                     in.close();
0099                     out.close();
0100 
0101                     if (success) {
0102                         rename((*it).c_str(), (*it + ".kgammaorig").c_str());
0103                         rename((*it + ".tmp").c_str(), (*it).c_str());
0104                     } else {
0105                         remove((*it + ".tmp").c_str());
0106                     }
0107                     break;
0108                 }
0109             }
0110         }
0111     }
0112     if (!success) {
0113         cerr << "Usage: xf86gammacfg RGAMMA GGAMMA "
0114                 "BGAMMA [RGAMMA GGAMMA BGAMMA [...]]"
0115              << endl;
0116     }
0117 
0118     return !success;
0119 }