Skip to content

Commit b940f0f

Browse files
committed
Added Get and GetTimeout for main KbdBacklight object path to fetch all keyboards backlight pct/timeout.
1 parent 991f08e commit b940f0f

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

TODO.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
## 5.X
2-
- [ ] Keep it up to date with possible ddcutil/libmodule api changes
2+
3+
### Sensor
34
- [x] ALS sensors (and yoctolight too) have a logarithmic curve for lux values (#71)
45
- [x] fixed memleak in camera sensor
56

7+
### KbdBacklight
8+
- [x] Add Get and GetTimeout method on main object to fetch all kbd backlight and timeouts alltoghether
9+
610
## 6.0
711

812
### Backlight
913
- [ ] Create new object paths for each detected display
1014
- [ ] Drop "Set/Get" methods, and add a Set method on each object path
11-
- [ ] SetAll will call Set on each object path (and will be renamed to Set)
12-
- [ ] Drop GetAll method: you can only call Get on each object
15+
- [ ] SetAll will call Set on each object path (and will be renamed to Set); same for GetAll
1316
- [ ] Follow Keyboard API
1417

1518
## Ideas

src/modules/keyboard.c

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ static int kbd_new(struct udev_device *dev, void *userdata);
2020
static inline int set_value(kbd_t *k, const char *sysattr, int value);
2121
static map_ret_code set_brightness(void *userdata, const char *key, void *data);
2222
static map_ret_code set_timeout(void *userdata, const char *key, void *data);
23+
static map_ret_code append_backlight(void *userdata, const char *key, void *data);
24+
static map_ret_code append_timeout(void *userdata, const char *key, void *data);
2325
static int method_setkeyboard(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
2426
static int method_getkeyboard(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
2527
static int method_settimeout(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
2628
static int method_gettimeout(sd_bus_message *m, void *userdata, sd_bus_error *ret_error);
29+
static int fetch_timeout(kbd_t *k);
2730

2831
MODULE("KEYBOARD");
2932

@@ -32,7 +35,9 @@ static const char main_interface[] = "org.clightd.clightd.KbdBacklight";
3235
static const sd_bus_vtable main_vtable[] = {
3336
SD_BUS_VTABLE_START(0),
3437
SD_BUS_METHOD("Set", "d", "b", method_setkeyboard, SD_BUS_VTABLE_UNPRIVILEGED),
38+
SD_BUS_METHOD("Get", NULL, "a(sd)", method_getkeyboard, SD_BUS_VTABLE_UNPRIVILEGED),
3539
SD_BUS_METHOD("SetTimeout", "i", "b", method_settimeout, SD_BUS_VTABLE_UNPRIVILEGED),
40+
SD_BUS_METHOD("GetTimeout", NULL, "a(si)", method_gettimeout, SD_BUS_VTABLE_UNPRIVILEGED),
3641
SD_BUS_SIGNAL("Changed", "sd", 0),
3742
SD_BUS_VTABLE_END
3843
};
@@ -214,13 +219,39 @@ static int method_setkeyboard(sd_bus_message *m, void *userdata, sd_bus_error *r
214219

215220
static int method_getkeyboard(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
216221
kbd_t *k = (kbd_t *)userdata;
222+
if (k) {
223+
int curr = atoi(udev_device_get_sysattr_value(k->dev, "brightness"));
224+
const double pct = (double)curr / k->max;
225+
return sd_bus_reply_method_return(m, "d", pct);
226+
}
227+
sd_bus_message *reply = NULL;
228+
sd_bus_message_new_method_return(m, &reply);
229+
sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "(sd)");
230+
int r = map_iterate(kbds, append_backlight, reply);
231+
sd_bus_message_close_container(reply);
232+
if (r == 0) {
233+
r = sd_bus_send(NULL, reply, NULL);
234+
}
235+
sd_bus_message_unref(reply);
236+
return r;
237+
}
238+
239+
static map_ret_code append_backlight(void *userdata, const char *key, void *data) {
240+
sd_bus_message *reply = (sd_bus_message *)userdata;
241+
242+
kbd_t *k = (kbd_t *)data;
217243
int curr = atoi(udev_device_get_sysattr_value(k->dev, "brightness"));
218244
const double pct = (double)curr / k->max;
219-
return sd_bus_reply_method_return(m, "d", pct);
245+
246+
sd_bus_message_open_container(reply, SD_BUS_TYPE_STRUCT, "sd");
247+
sd_bus_message_append(reply, "sd", key, pct);
248+
sd_bus_message_close_container(reply);
249+
250+
return MAP_OK;
220251
}
221252

222253
static int method_settimeout(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
223-
// ASSERT_AUTH();
254+
ASSERT_AUTH();
224255

225256
int timeout;
226257
int r = sd_bus_message_read(m, "i", &timeout);
@@ -242,6 +273,26 @@ static int method_settimeout(sd_bus_message *m, void *userdata, sd_bus_error *re
242273

243274
static int method_gettimeout(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
244275
kbd_t *k = (kbd_t *)userdata;
276+
if (k) {
277+
int tm = fetch_timeout(k);
278+
if (tm >= 0) {
279+
return sd_bus_reply_method_return(m, "i", tm);
280+
}
281+
return sd_bus_error_set_errno(ret_error, -tm);
282+
}
283+
sd_bus_message *reply = NULL;
284+
sd_bus_message_new_method_return(m, &reply);
285+
sd_bus_message_open_container(reply, SD_BUS_TYPE_ARRAY, "(si)");
286+
int r = map_iterate(kbds, append_timeout, reply);
287+
sd_bus_message_close_container(reply);
288+
if (r == 0) {
289+
r = sd_bus_send(NULL, reply, NULL);
290+
}
291+
sd_bus_message_unref(reply);
292+
return r;
293+
}
294+
295+
static int fetch_timeout(kbd_t *k) {
245296
const char *timeout = udev_device_get_sysattr_value(k->dev, "stop_timeout");
246297
if (timeout) {
247298
int tm;
@@ -256,9 +307,22 @@ static int method_gettimeout(sd_bus_message *m, void *userdata, sd_bus_error *re
256307
break;
257308
}
258309
}
259-
return sd_bus_reply_method_return(m, "i", tm);
310+
return tm;
260311
}
261-
return sd_bus_error_set_errno(ret_error, EINVAL);
312+
return -EINVAL;
313+
}
314+
return -ENOENT;
315+
}
316+
317+
static map_ret_code append_timeout(void *userdata, const char *key, void *data) {
318+
sd_bus_message *reply = (sd_bus_message *)userdata;
319+
320+
kbd_t *k = (kbd_t *)data;
321+
int tm = fetch_timeout(k);
322+
if (tm >= 0) {
323+
sd_bus_message_open_container(reply, SD_BUS_TYPE_STRUCT, "si");
324+
sd_bus_message_append(reply, "si", key, tm);
325+
sd_bus_message_close_container(reply);
262326
}
263-
return sd_bus_error_set_errno(ret_error, ENOENT);
327+
return MAP_OK;
264328
}

0 commit comments

Comments
 (0)