ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
colormap.cpp
Go to the documentation of this file.
1 #include "colormap.h"
2 
4  _keyNames(0), _colors(0), _count(0), _mapCount(0)
5 {
6 }
7 
9  deAlloc();
10 }
11 
13  _keyNames(0), _colors(0), _count(0), _mapCount(0)
14 {
15  *this = rhs;
16 }
17 
19  alloc(rhs._count);
20  // Copy key names and colors
21  // (Note: it's safe to copy the const char*'s because key names are constants and will never move or be deleted)
22  memcpy(_keyNames, rhs._keyNames, sizeof(const char*) * _count);
23  memcpy(_colors, rhs._colors, sizeof(QRgb) * _count);
24  return rhs;
25 }
26 
27 bool ColorMap::operator==(const ColorMap& rhs) const{
28  if(_count != rhs._count)
29  return false;
30 
31  // Compare just the pointers because key names are sorted and the strings are constants.
32  if(memcmp(_keyNames, rhs._keyNames, sizeof(const char*) * _count) != 0)
33  return false;
34 
35  for(int x = 0;x < _count;x++){
36  if(_colors[x] != rhs._colors[x])
37  return false;
38  }
39 
40  return true;
41 }
42 
43 void ColorMap::alloc(int newCount){
44  if(newCount > _mapCount){
45  // ^ map never shrinks, only expands
46  deAlloc();
47  _mapCount = newCount;
48  _keyNames = new const char*[_mapCount];
49  _colors = new QRgb[_mapCount];
50  }
51  _count = newCount;
52  clear();
53 }
54 
56  _count = _mapCount = 0;
57  if(_keyNames) delete[] _keyNames;
58  if(_colors) delete[] _colors;
59 }
60 
62  memset(_colors, 0, _count * sizeof(QRgb));
63 }
64 
65 static int qs_strcmp(const void* lhs, const void* rhs){
66  return strcmp(*(const char**)lhs, *(const char**)rhs);
67 }
68 
69 void ColorMap::init(const KeyMap& map){
70  QList<Key> newKeys = map.positions();
71  // There's no point including keys that don't have LEDs, so remove them now
72  QMutableListIterator<Key> i(newKeys);
73  while(i.hasNext()){
74  Key key = i.next();
75  if(!key.hasLed)
76  i.remove();
77  }
78  // Now that we know how many keys we'll have, check memory
79  alloc(newKeys.count());
80  // Copy key names
81  int keyPos = 0;
82  foreach(const Key& key, newKeys)
83  _keyNames[keyPos++] = key.name; // as above, it's safe to copy these since the strings are constants
84  // Sort names for quick access
85  qsort(_keyNames, _count, sizeof(const char*), qs_strcmp);
86 }
87 
88 QRgb* ColorMap::colorForName(const char* name){
89  if(!_keyNames)
90  return 0;
91  const char** namePtr = (const char**)bsearch(&name, _keyNames, _count, sizeof(const char*), qs_strcmp);
92  if(!namePtr)
93  return 0;
94  ptrdiff_t position = namePtr - _keyNames;
95  return _colors + position;
96 }
97 
98 const QRgb* ColorMap::colorForName(const char* name) const {
99  if(!_keyNames)
100  return 0;
101  const char** namePtr = (const char**)bsearch(&name, _keyNames, _count, sizeof(const char*), qs_strcmp);
102  if(!namePtr)
103  return 0;
104  ptrdiff_t position = namePtr - _keyNames;
105  return _colors + position;
106 }
~ColorMap()
Definition: colormap.cpp:8
void init(const KeyMap &map)
Definition: colormap.cpp:69
int _count
Definition: colormap.h:46
static int qs_strcmp(const void *lhs, const void *rhs)
Definition: colormap.cpp:65
float x
Definition: main.c:66
bool hasLed
Definition: keymap.h:19
Definition: keymap.h:49
QRgb * colorForName(const char *name)
Definition: colormap.cpp:88
void deAlloc()
Definition: colormap.cpp:55
ColorMap()
Definition: colormap.cpp:3
const char * name
Definition: keymap.h:14
bool operator==(const ColorMap &rhs) const
Definition: colormap.cpp:27
Definition: keymap.h:8
Definition: keymap.h:49
const ColorMap & operator=(const ColorMap &rhs)
Definition: colormap.cpp:18
const char ** _keyNames
Definition: colormap.h:44
int _mapCount
Definition: colormap.h:46
QRgb * _colors
Definition: colormap.h:45
QList< Key > positions() const
Definition: keymap.h:148
void clear()
Definition: colormap.cpp:61
void alloc(int count)
Definition: colormap.cpp:43