mirror of
https://github.com/swaywm/sway.git
synced 2026-04-26 06:46:26 -04:00
sway commands are implemented using functions that return an error code and (if the function was not successful) an error string. The two are bundled together by the type `struct cmd_results`. This patch alters the command handler prototype so that the cmd_results objects are returned by value (on the stack), instead of by a pointer to a heap allocated instance of a cmd_results. The latter method had the flaw that, if the heap allocation of the cmd_results object failed, the exact return code would be lost. Furthermore, for some command handlers (such as those in sway/commands/output), returning NULL signified success, so an allocation failure could lead to an ignored error. This change prevents both classes of errors.
35 lines
985 B
C
35 lines
985 B
C
#define _POSIX_C_SOURCE 200809L
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include "sway/config.h"
|
|
#include "sway/commands.h"
|
|
#include "sway/input/input-manager.h"
|
|
#include "log.h"
|
|
#include "stringop.h"
|
|
#include "util.h"
|
|
|
|
struct cmd_results input_cmd_calibration_matrix(int argc, char **argv) {
|
|
struct cmd_results error;
|
|
if (checkarg(&error, argc, "calibration_matrix", EXPECTED_EQUAL_TO, 6)) {
|
|
return error;
|
|
}
|
|
struct input_config *ic = config->handler_context.input_config;
|
|
if (!ic) {
|
|
return cmd_results_new(CMD_FAILURE, "No input device defined.");
|
|
}
|
|
|
|
float parsed[6];
|
|
for (int i = 0; i < argc; ++i) {
|
|
char *item = argv[i];
|
|
float x = parse_float(item);
|
|
if (isnan(x)) {
|
|
return cmd_results_new(CMD_FAILURE, "calibration_matrix: unable to parse float: %s", item);
|
|
}
|
|
parsed[i] = x;
|
|
}
|
|
|
|
ic->calibration_matrix.configured = true;
|
|
memcpy(ic->calibration_matrix.matrix, parsed, sizeof(ic->calibration_matrix.matrix));
|
|
|
|
return cmd_results_new(CMD_SUCCESS, NULL);
|
|
}
|