File: main.cpp

package info (click to toggle)
msi-keyboard 1.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 124 kB
  • sloc: cpp: 244; xml: 17; makefile: 9; sh: 4
file content (207 lines) | stat: -rw-r--r-- 5,765 bytes parent folder | download | duplicates (3)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
  main.cpp

  (C) Copyright 2015, Brad Parker
  All rights reserved.

  License: 3-clause BSD. See COPYING
*/

#include <QCoreApplication>
#include <QCommandLineParser>
#include <iostream>
#include "keyboard.h"

enum CommandLineParseResult
{
  CommandLineOk,
  CommandLineError,
  CommandLineVersionRequested,
  CommandLineHelpRequested
};

struct ColorOption {
  ColorOption() :
  region(REGION_LEFT)
  ,color(COLOR_RED)
  ,intensity(INTENSITY_HIGH)
  {}

  Region region;
  Color color;
  Intensity intensity;
};

struct KeyboardOptions {
  KeyboardOptions() :
  modeOption(MODE_NORMAL)
  ,colorOptions()
  ,modeSet(false)
  ,colorSet(false)
  {}

  Mode modeOption;
  QList<ColorOption*> colorOptions;
  bool modeSet;
  bool colorSet;

  void setMode(QString mode) {
    if(mode == "normal") { modeOption = MODE_NORMAL; }
    if(mode == "gaming") { modeOption = MODE_GAMING; }
    if(mode == "breathe") { modeOption = MODE_BREATHE; }
    if(mode == "demo") { modeOption = MODE_DEMO; }
    if(mode == "wave") { modeOption = MODE_WAVE; }

    modeSet = true;
  }

  void setColor(QString colorString) {
    QStringList fields = colorString.split(',');

    if(fields.count() != 3) {
      std::cerr << "invalid color selection" << std::endl;
      qApp->quit();
      return;
    }

    QString region = fields.at(0);
    QString color = fields.at(1);
    QString intensity = fields.at(2);

    ColorOption *colorOption = new ColorOption;

    if(region == "left") colorOption->region = REGION_LEFT;
    if(region == "middle") colorOption->region = REGION_MIDDLE;
    if(region == "right") colorOption->region = REGION_RIGHT;

    if(color == "off") colorOption->color = COLOR_OFF;
    if(color == "red") colorOption->color = COLOR_RED;
    if(color == "orange") colorOption->color = COLOR_ORANGE;
    if(color == "yellow") colorOption->color = COLOR_YELLOW;
    if(color == "green") colorOption->color = COLOR_GREEN;
    if(color == "sky") colorOption->color = COLOR_SKY;
    if(color == "blue") colorOption->color = COLOR_BLUE;
    if(color == "purple") colorOption->color = COLOR_PURPLE;
    if(color == "white") colorOption->color = COLOR_WHITE;

    if(intensity == "high") colorOption->intensity = INTENSITY_HIGH;
    if(intensity == "medium") colorOption->intensity = INTENSITY_MEDIUM;
    if(intensity == "low") colorOption->intensity = INTENSITY_LOW;
    if(intensity == "light") colorOption->intensity = INTENSITY_LIGHT;

    colorSet = true;

    colorOptions.append(colorOption);
  }
};

CommandLineParseResult parseCommandLine(QCommandLineParser &parser, KeyboardOptions *keyboardOptions, QString *errorMessage) {
  QCommandLineOption helpOption = parser.addHelpOption();
  QCommandLineOption versionOption = parser.addVersionOption();

  QCommandLineOption mode(QStringList() << "m" << "mode", "set color <mode>: normal, gaming, breathe, demo, wave", "mode");
  QCommandLineOption color(QStringList() << "c" << "color", "set a <color> using the format: region,color,intensity", "color");

  parser.addOption(mode);
  parser.addOption(color);

  if(!parser.parse(QCoreApplication::arguments())) {
    *errorMessage = parser.errorText();
    return CommandLineError;
  }

  if(parser.isSet(versionOption))
    return CommandLineVersionRequested;

  if(parser.isSet(helpOption))
    return CommandLineHelpRequested;

  if(parser.isSet(mode)) {
    keyboardOptions->setMode(parser.value(mode));
  }

  if(parser.isSet(color)) {
    foreach(const QString &colorValue, parser.values(color)) {
      keyboardOptions->setColor(colorValue);
    }
  }

  return CommandLineOk;
}

int main(int argc, char *argv[]) {
  QCoreApplication app(argc, argv);
  app.setApplicationName("msi-keyboard");
  app.setApplicationVersion("1.0");

  QCommandLineParser parser;
  parser.setApplicationDescription("Keyboard color changer for MSI steelseries keyboards");

  QString errorMessage;
  KeyboardOptions keyboardOptions;

  switch(parseCommandLine(parser, &keyboardOptions, &errorMessage)) {
    case CommandLineOk:
      break;
    case CommandLineError:
      fputs(qPrintable(errorMessage), stderr);
      fputs("\n\n", stderr);
      fputs(qPrintable(parser.helpText()), stderr);
      return 1;
    case CommandLineVersionRequested:
      printf("%s %s\n", qPrintable(QCoreApplication::applicationName()),
             qPrintable(QCoreApplication::applicationVersion()));
      return 0;
    case CommandLineHelpRequested:
    {
      std::cout << qPrintable(parser.helpText()) << std::endl;

      QStringList regions = QStringList() << "left" << "middle" << "right";
      QStringList colors = QStringList() << "off" << "red" << "orange" << "yellow" << "green" << "sky" << "blue" << "purple" << "white";
      QStringList intensities = QStringList() << "high" << "medium" << "low" << "light";

      QString colorHelp = QString(R"(Available regions:

%1

Available colors:

%2

Available intensities:

%3

Example:

%4
)").arg(regions.join('\n')).arg(colors.join('\n')).arg(intensities.join('\n')).arg(QString(argv[0]) + " -m normal -c left,red,high -c middle,purple,high -c right,sky,high");

      std::cout << qPrintable(colorHelp) << std::endl;

      return 0;
    }
  }

  if(!keyboardOptions.modeSet || !keyboardOptions.colorSet) {
    std::cerr << "Please set a mode as well as at least one color region to change." << std::endl;
    return 1;
  }else{
    Keyboard k;

    if(keyboardOptions.colorSet) {
      for(int i = 0; i < keyboardOptions.colorOptions.count(); ++i) {
        ColorOption *colorOption = keyboardOptions.colorOptions.at(i);

        k.setColor(colorOption->region, colorOption->color, colorOption->intensity);
      }
    }

    if(keyboardOptions.modeSet) {
      k.setMode(keyboardOptions.modeOption);
    }

  }

  return 0;
}