summaryrefslogtreecommitdiffstats
path: root/kgamma/xf86gammacfg/xf86gammacfg.cpp
blob: db98a8fb9794edab1bab8aed25d7c1139a24f936 (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
/***************************************************************************
                          main.cpp  -  description
                             -------------------
    begin                : Wed Aug  7 18:11:19 CEST 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 <stdio.h>

#include <stdlib.h>

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include <config.h>

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

using namespace std;

int main(int argc, char *argv[])
{

  bool cpyonly, secmon, success;

  cpyonly = secmon = success = false;

  if ( !((argc-1) % 3) ) {
    int Screen = 0;
    int ScreenCount = (argc-1) / 3;
    
    // First, search for XF86Config or xorg.conf
    vector <string> searchPaths;
    searchPaths.push_back("/etc/X11/XF86Config-4");
    searchPaths.push_back("/etc/X11/XF86Config");
    searchPaths.push_back("/etc/XF86Config");
    searchPaths.push_back("/usr/X11R6/etc/X11/XF86Config-4");
    searchPaths.push_back("/usr/X11R6/etc/X11/XF86Config");
    searchPaths.push_back("/usr/X11R6/lib/X11/XF86Config-4");
    searchPaths.push_back("/usr/X11R6/lib/X11/XF86Config");

    searchPaths.push_back("/etc/X11/xorg.conf-4");
    searchPaths.push_back("/etc/X11/xorg.conf");
    searchPaths.push_back("/etc/xorg.conf");
    searchPaths.push_back("/usr/X11R6/etc/X11/xorg.conf-4");
    searchPaths.push_back("/usr/X11R6/etc/X11/xorg.conf");
    searchPaths.push_back("/usr/X11R6/lib/X11/xorg.conf-4");
    searchPaths.push_back("/usr/X11R6/lib/X11/xorg.conf");

    std::vector<string>::iterator it = searchPaths.begin();
    for (; it != searchPaths.end(); ++it ) {
      //Try to open file
      std::ifstream in( (*it).c_str() );
            
      if ( in.is_open() ) {
        std::ofstream out( ( (*it) + ".tmp" ).c_str() );
        if ( out.is_open() ) {
          std::string s, buf;
          std::vector<string> words;
        
          while (getline(in, s,'\n')) {
            if (!cpyonly) {
              words.clear();
              std::istringstream ss(s.c_str());
              while (ss >> buf) words.push_back(buf);

              if ( !words.empty() ) {
                if ( words[0] == "Section" && words.size() > 1 ) {
                  if ( words[1] == "\"Monitor\"" ) {
                    secmon = true;
                    out << s << endl;
                    continue;
                  }
                }
                if ( secmon ) {
                  if ( words[0] == "Gamma" ) {
                    out << "  Gamma   " << argv[3*Screen+1] << "  " << \
                        argv[3*Screen+2]<< "  " << argv[3*Screen+3];
                    out << "  # created by KGamma" << endl;
                    cpyonly = success = ( ++Screen == ScreenCount );
                    secmon = false;
                    continue;
                  }
                  if ( words[0] == "EndSection" ) {
                    out << "  Gamma   " << argv[3*Screen+1] << "  " << \
                        argv[3*Screen+2]<< "  " << argv[3*Screen+3];
                    out << "  # created by KGamma" << endl;
                    out << s << endl;
                    cpyonly = success = ( ++Screen == ScreenCount );
                    secmon = false;
                    continue;
                  }
                }
              }
            }
            out << s << endl;
          } //endwhile
        
          in.close();
          out.close();

          if ( success ) {
            rename( (*it).c_str(),(*it + ".kgammaorig").c_str() );
            rename( (*it + ".tmp").c_str(),(*it).c_str() );
          }
          else remove( (*it + ".tmp").c_str() );
          break;
        }
      }
    }
  }
  if ( !success )
    cerr << "Usage: xf86gammacfg RGAMMA GGAMMA " \
            "BGAMMA [RGAMMA GGAMMA BGAMMA [...]]" << endl;

  return success;
}

#ifdef istringstream
#undef istringstream
#endif