Skip to content

Commit 1ba5e5c

Browse files
committed
Changes to CMake, add keyboard lock
1 parent 1488dc4 commit 1ba5e5c

File tree

5 files changed

+46
-45
lines changed

5 files changed

+46
-45
lines changed

CMakeLists.txt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ FIND_LIBRARY(LIBUSB_LIBRARIES NAMES usb-1.0
1717
PATH_SUFFIXES lib)
1818
message("CMake found libusb: " ${LIBUSB_INCLUDE_DIR} ", usb-1.0")
1919
find_package(X11 REQUIRED)
20-
find_package(PythonInterp REQUIRED)
21-
find_package(PythonLibs REQUIRED)
22-
find_package(PythonExtensions)
2320

2421
include_directories(${LIBUSB_INCLUDE_DIR} ${X11_INCLUDE_DIRS} libmk)
2522
link_libraries(usb-1.0 ${X11_LIBRARIES})
@@ -53,15 +50,15 @@ target_link_libraries(ctrl mk mkc)
5350
add_executable(ambilight examples/ambilight/ambilight.c)
5451
target_link_libraries(ambilight mk pthread)
5552

56-
project(mk_notifications)
57-
add_library(mk_notifications MODULE
58-
examples/notifications/mk_notifications.c)
59-
python_extension_module(mk_notifications)
60-
set_target_properties(mk_notifications PROPERTIES
61-
OUTPUT_NAME "mk_notifications")
62-
6353
# masterkeys Python module
6454
if (SKBUILD) # python setup.py
55+
find_package(PythonInterp REQUIRED)
56+
find_package(PythonLibs REQUIRED)
57+
find_package(PythonExtensions)
58+
59+
find_package(PythonInterp REQUIRED)
60+
find_package(PythonLibs REQUIRED)
61+
find_package(PythonExtensions)
6562
project(masterkeys VERSION 0.2.0 DESCRIPTION "Wrapper around libmk")
6663
add_library(masterkeys MODULE
6764
masterkeys/masterkeys.c

examples/notifications/CMakeLists.txt

Lines changed: 0 additions & 22 deletions
This file was deleted.

examples/notifications/capture.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typedef struct CaptureArgs {
1313
unsigned char* target_color;
1414
pthread_mutex_t* target_lock;
1515
pthread_mutex_t* exit_lock;
16+
pthread_mutex_t* keyboard_lock;
1617
bool* exit_flag;
1718
Display* display;
1819
Window root;
@@ -32,9 +33,9 @@ typedef struct Screenshot {
3233

3334

3435
CaptureArgs* init_capture(int divider, int sat_bias, int lower, int upper,
35-
bool brightness_norm,
36-
unsigned char* target_color, pthread_mutex_t* target_lock,
37-
bool* exit_flag, pthread_mutex_t* exit_lock) {
36+
bool brightness_norm, unsigned char* target_color,
37+
pthread_mutex_t* target_lock, bool* exit_flag,
38+
pthread_mutex_t* exit_lock, pthread_mutex_t* kb_lock) {
3839
/** Initialize a CaptureArgs struct that can be passed as thread argument */
3940
CaptureArgs* args = (CaptureArgs*) malloc(sizeof(CaptureArgs));
4041

@@ -47,6 +48,7 @@ CaptureArgs* init_capture(int divider, int sat_bias, int lower, int upper,
4748
args->target_lock = target_lock;
4849
args->exit_flag = exit_flag;
4950
args->exit_lock = exit_lock;
51+
args->keyboard_lock = kb_lock;
5052

5153
args->display = XOpenDisplay(NULL);
5254
args->root = DefaultRootWindow(args->display);
@@ -83,10 +85,12 @@ void capturer(struct CaptureArgs* args) {
8385
free(screenshot->data);
8486
free(screenshot);
8587

88+
pthread_mutex_lock(args->keyboard_lock);
8689
pthread_mutex_lock(args->target_lock);
8790
for (int i=0; i<3; i++)
8891
args->target_color[i] = target[i];
8992
pthread_mutex_unlock(args->target_lock);
93+
pthread_mutex_unlock(args->keyboard_lock);
9094
}
9195
}
9296

examples/notifications/mk_notifications.c

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ unsigned char target_color[3] = {0};
2323
/// Mutexes
2424
pthread_mutex_t exit_lock = PTHREAD_MUTEX_INITIALIZER;
2525
pthread_mutex_t target_lock = PTHREAD_MUTEX_INITIALIZER;
26+
pthread_mutex_t keyboard_lock = PTHREAD_MUTEX_INITIALIZER;
2627

2728
/// Threads
2829
pthread_t keyboard_thread;
@@ -92,7 +93,7 @@ static PyObject* init(PyObject* self, PyObject* args) {
9293
}
9394

9495
args = init_capture(divider, sat_bias, lower, upper, brightness_norm, &target_color,
95-
&target_lock, &exit_requested, &exit_lock);
96+
&target_lock, &exit_requested, &exit_lock, &keyboard_lock);
9697

9798
if (args == NULL) {
9899
libmk_exit();
@@ -193,3 +194,33 @@ static PyObject* py_calculate_dominant_color(PyObject* self, PyObject* args) {
193194
PyTuple_SetItem(tuple, i, result[i]);
194195
return tuple;
195196
}
197+
198+
199+
void _flash_keyboard(unsigned char* color) {
200+
unsigned char r = color[0], g = color[1], b = color[2];
201+
for (int i=0; i<256; i++) {
202+
pthread_mutex_unlock(&target_lock);
203+
for (int j=0; j<3; j++)
204+
target_color[j] = (unsigned char) ((double) color[j] * (double) j / 255.0);
205+
pthread_mutex_unlock(&target_lock);
206+
}
207+
for (int i=255; i>-1; i--)
208+
pthread_mutex_unlock(&target_lock);
209+
for (int j=0; j<3; j++)
210+
target_color[j] = (unsigned char) ((double) color[j] * (double) j / 255.0);
211+
pthread_mutex_unlock(&target_lock);
212+
213+
}
214+
215+
}
216+
217+
218+
static PyObject* flash_keyboard(PyObject* self, PyObject* args) {
219+
/** Asynchronously flash the keyboard from Python in a sepcific color */
220+
pthread_t thread;
221+
unsigned char r, g, b;
222+
if (!PyArg_ParseTuple(args, "bbb", &r, &g, &b))
223+
return NULL;
224+
unsigned char color[3] = {r, g, b};
225+
pthread_create(&thread, NULL, _flash_keyboard, color);
226+
}

utils/CMakeLists.txt

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)