The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

t/ctl: Add --get-brightness and --set-brightness options

Which calls device_get_brightness and device_set_brightness on the desired
client.

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2426>

averyv 06c75645 b521caa1

+121 -1
+121 -1
src/xrt/targets/ctl/main.c
··· 13 13 #include "client/ipc_client_connection.h" 14 14 15 15 #include "ipc_client_generated.h" 16 + #include "xrt/xrt_results.h" 16 17 18 + #include <getopt.h> 17 19 #include <ctype.h> 18 20 19 21 ··· 27 29 MODE_SET_FOCUSED, 28 30 MODE_TOGGLE_IO, 29 31 MODE_RECENTER, 32 + MODE_GET_BRIGHTNESS, 33 + MODE_SET_BRIGHTNESS, 30 34 } op_mode_t; 31 35 32 36 ··· 143 147 144 148 return 0; 145 149 } 150 + 151 + int 152 + get_first_device_id(struct ipc_connection *ipc_c) 153 + { 154 + for (size_t i = 0; i < ipc_c->ism->isdev_count; i++) { 155 + struct ipc_shared_device *device = &ipc_c->ism->isdevs[i]; 156 + if (device && device->device_type == XRT_DEVICE_TYPE_HMD) { 157 + // Print to stderr to enable scripting 158 + PE("Picked device %zu: %s\n", i, device->str); 159 + return i; 160 + } 161 + } 162 + 163 + return -1; 164 + } 165 + 166 + int 167 + get_brightness(struct ipc_connection *ipc_c, int device_id) 168 + { 169 + device_id = device_id >= 0 ? device_id : get_first_device_id(ipc_c); 170 + if (device_id < 0) { 171 + PE("Couldn't find a HMD device!\n"); 172 + return 1; 173 + } 174 + 175 + float out_brightness; 176 + xrt_result_t r = ipc_call_device_get_brightness(ipc_c, device_id, &out_brightness); 177 + if (r != XRT_SUCCESS) { 178 + PE("Failed to get brightness for device %d\n", device_id); 179 + return 1; 180 + } 181 + 182 + P("%d\n", (int)(out_brightness * 100)); 183 + 184 + return 0; 185 + } 186 + 187 + int 188 + set_brightness(struct ipc_connection *ipc_c, int device_id, const char *value) 189 + { 190 + const int length = strlen(value); 191 + if (length == 0) { 192 + return 1; 193 + } 194 + 195 + bool relative = (value[0] == '-' || value[0] == '+'); 196 + 197 + char *end = NULL; 198 + float target_brightness = strtof(value, &end); 199 + 200 + if ((length > (end - value)) && *end == '%') { 201 + target_brightness /= 100.f; 202 + } 203 + 204 + device_id = device_id >= 0 ? device_id : get_first_device_id(ipc_c); 205 + if (device_id < 0) { 206 + PE("Couldn't find a HMD device!\n"); 207 + return 1; 208 + } 209 + 210 + xrt_result_t r = ipc_call_device_set_brightness(ipc_c, device_id, target_brightness, relative); 211 + if (r != XRT_SUCCESS) { 212 + PE("Failed to set brightness for device %d\n", device_id); 213 + return 1; 214 + } 215 + 216 + float out_brightness; 217 + r = ipc_call_device_get_brightness(ipc_c, device_id, &out_brightness); 218 + if (r != XRT_SUCCESS) { 219 + PE("Failed to get brightness for device %d\n", device_id); 220 + return 1; 221 + } 222 + 223 + if (relative || out_brightness != target_brightness) { 224 + P("Set brightness to %d%%\n", (int)(out_brightness * 100)); 225 + } 226 + 227 + return 0; 228 + } 229 + 230 + enum LongOptions 231 + { 232 + OPTION_DEVICE = 1, 233 + OPTION_GET_BRIGHTNESS, 234 + OPTION_SET_BRIGHTNESS, 235 + }; 236 + 146 237 int 147 238 main(int argc, char *argv[]) 148 239 { ··· 151 242 // parse arguments 152 243 int c; 153 244 int s_val = 0; 245 + int device_val = -1; 246 + char *brightness; 247 + 248 + static struct option long_options[] = { 249 + {"device", required_argument, NULL, OPTION_DEVICE}, 250 + {"get-brightness", no_argument, NULL, OPTION_GET_BRIGHTNESS}, 251 + {"set-brightness", required_argument, NULL, OPTION_SET_BRIGHTNESS}, 252 + {NULL, 0, NULL, 0}, 253 + }; 154 254 255 + int option_index = 0; 155 256 opterr = 0; 156 - while ((c = getopt(argc, argv, "p:f:i:c")) != -1) { 257 + while ((c = getopt_long(argc, argv, "p:f:i:c", long_options, &option_index)) != -1) { 157 258 switch (c) { 158 259 case 'p': 159 260 s_val = atoi(optarg); ··· 168 269 op_mode = MODE_TOGGLE_IO; 169 270 break; 170 271 case 'c': op_mode = MODE_RECENTER; break; 272 + case OPTION_DEVICE: { 273 + device_val = atoi(optarg); 274 + break; 275 + } 276 + case OPTION_GET_BRIGHTNESS: { 277 + op_mode = MODE_GET_BRIGHTNESS; 278 + break; 279 + } 280 + case OPTION_SET_BRIGHTNESS: { 281 + brightness = optarg; 282 + op_mode = MODE_SET_BRIGHTNESS; 283 + break; 284 + } 171 285 case '?': 172 286 if (optopt == 's') { 173 287 PE("Option -s requires an id to set.\n"); ··· 177 291 PE(" -f <id>: Set focused client\n"); 178 292 PE(" -p <id>: Set primary client\n"); 179 293 PE(" -i <id>: Toggle whether client receives input\n"); 294 + PE(" --device <id>: Set device for subsequent command, otherwise defaults to the " 295 + "primary device\n"); 296 + PE(" --get-brightness: Get current display brightness in percent\n"); 297 + PE(" --set-brightness <[+-]brightness[%%]>: Set display brightness\n"); 180 298 } else { 181 299 PE("Option `\\x%x' unknown.\n", optopt); 182 300 } ··· 204 322 case MODE_SET_FOCUSED: exit(set_focused(&ipc_c, s_val)); break; 205 323 case MODE_TOGGLE_IO: exit(toggle_io(&ipc_c, s_val)); break; 206 324 case MODE_RECENTER: exit(recenter_local_spaces(&ipc_c)); break; 325 + case MODE_GET_BRIGHTNESS: exit(get_brightness(&ipc_c, device_val)); break; 326 + case MODE_SET_BRIGHTNESS: exit(set_brightness(&ipc_c, device_val, brightness)); break; 207 327 default: P("Unrecognised operation mode.\n"); exit(1); 208 328 } 209 329