ckb-next  beta-v0.2.8 at branch testing
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 void ColorMap::alloc(int newCount){
28  if(newCount > _mapCount){
29  // ^ map never shrinks, only expands
30  deAlloc();
31  _mapCount = newCount;
32  _keyNames = new const char*[_mapCount];
33  _colors = new QRgb[_mapCount];
34  }
35  _count = newCount;
36  clear();
37 }
38 
40  _count = _mapCount = 0;
41  if(_keyNames) delete[] _keyNames;
42  if(_colors) delete[] _colors;
43 }
44 
46  memset(_colors, 0, _count * sizeof(QRgb));
47 }
48 
49 static int qs_strcmp(const void* lhs, const void* rhs){
50  return strcmp(*(const char**)lhs, *(const char**)rhs);
51 }
52 
53 void ColorMap::init(const KeyMap& map){
54  QList<Key> newKeys = map.positions();
55  // There's no point including keys that don't have LEDs, so remove them now
56  QMutableListIterator<Key> i(newKeys);
57  while(i.hasNext()){
58  Key key = i.next();
59  if(!key.hasLed)
60  i.remove();
61  }
62  // Now that we know how many keys we'll have, check memory
63  alloc(newKeys.count());
64  // Copy key names
65  int keyPos = 0;
66  foreach(const Key& key, newKeys)
67  _keyNames[keyPos++] = key.name; // as above, it's safe to copy these since the strings are constants
68  // Sort names for quick access
69  qsort(_keyNames, _count, sizeof(const char*), qs_strcmp);
70 }
71 
72 QRgb* ColorMap::colorForName(const char* name){
73  if(!_keyNames)
74  return 0;
75  const char** namePtr = (const char**)bsearch(&name, _keyNames, _count, sizeof(const char*), qs_strcmp);
76  if(!namePtr)
77  return 0;
78  ptrdiff_t position = namePtr - _keyNames;
79  return _colors + position;
80 }
81 
82 const QRgb* ColorMap::colorForName(const char* name) const {
83  if(!_keyNames)
84  return 0;
85  const char** namePtr = (const char**)bsearch(&name, _keyNames, _count, sizeof(const char*), qs_strcmp);
86  if(!namePtr)
87  return 0;
88  ptrdiff_t position = namePtr - _keyNames;
89  return _colors + position;
90 }
~ColorMap()
Definition: colormap.cpp:8
void init(const KeyMap &map)
Definition: colormap.cpp:53
int _count
Definition: colormap.h:45
static int qs_strcmp(const void *lhs, const void *rhs)
Definition: colormap.cpp:49
bool hasLed
Definition: keymap.h:19
QRgb * colorForName(const char *name)
Definition: colormap.cpp:72
void deAlloc()
Definition: colormap.cpp:39
ColorMap()
Definition: colormap.cpp:3
const char * name
Definition: keymap.h:14
Definition: keymap.h:8
Definition: keymap.h:49
const ColorMap & operator=(const ColorMap &rhs)
Definition: colormap.cpp:18
const char ** _keyNames
Definition: colormap.h:43
int _mapCount
Definition: colormap.h:45
QRgb * _colors
Definition: colormap.h:44
QList< Key > positions() const
Definition: keymap.h:132
void clear()
Definition: colormap.cpp:45
void alloc(int count)
Definition: colormap.cpp:27