2021-11-23 20:04:23 +01:00
|
|
|
/**
|
|
|
|
|
* Copyright 2021 Johannes Marbach
|
2024-01-12 09:51:43 +01:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2021-11-23 20:04:23 +01:00
|
|
|
*/
|
|
|
|
|
|
2024-01-12 10:00:05 +01:00
|
|
|
|
2021-11-23 20:04:23 +01:00
|
|
|
#include "command_line.h"
|
|
|
|
|
|
2021-11-23 20:11:25 +01:00
|
|
|
#include "buffyboard.h"
|
|
|
|
|
|
2024-03-30 08:06:29 +01:00
|
|
|
#include "../shared/log.h"
|
|
|
|
|
|
2025-06-18 18:13:25 +03:00
|
|
|
#include <fcntl.h>
|
2021-11-23 20:04:23 +01:00
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2025-06-18 18:13:25 +03:00
|
|
|
#include <unistd.h>
|
2021-11-23 20:04:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Static prototypes
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialise a command line options struct with default values and exit on failure.
|
|
|
|
|
*
|
|
|
|
|
* @param opts pointer to the options struct
|
|
|
|
|
*/
|
|
|
|
|
static void init_opts(bb_cli_opts *opts);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Output usage instructions.
|
|
|
|
|
*/
|
|
|
|
|
static void print_usage();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Static functions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void init_opts(bb_cli_opts *opts) {
|
2025-09-23 01:48:35 -07:00
|
|
|
bbx_cli_init_common_opts(&opts->common);
|
2024-03-30 12:46:09 +00:00
|
|
|
opts->num_config_files = 0;
|
|
|
|
|
opts->config_files = NULL;
|
2024-02-19 13:20:20 +00:00
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_0;
|
2025-06-18 18:13:25 +03:00
|
|
|
|
|
|
|
|
int fd_rotate = open("/sys/class/graphics/fbcon/rotate", O_RDONLY);
|
|
|
|
|
if (fd_rotate < 0) {
|
|
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Can not open /sys/class/graphics/fbcon/rotate");
|
2025-07-06 21:08:44 +03:00
|
|
|
return;
|
2025-06-18 18:13:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buffer[3];
|
|
|
|
|
int size = read(fd_rotate, buffer, sizeof(buffer));
|
|
|
|
|
if (size != 2 || buffer[1] != '\n') {
|
|
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Unexpected value of /sys/class/graphics/fbcon/rotate");
|
|
|
|
|
goto end;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (buffer[0]) {
|
|
|
|
|
case '0':
|
|
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_0;
|
|
|
|
|
break;
|
|
|
|
|
case '1':
|
|
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_270;
|
|
|
|
|
break;
|
|
|
|
|
case '2':
|
|
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_180;
|
|
|
|
|
break;
|
|
|
|
|
case '3':
|
|
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_90;
|
|
|
|
|
break;
|
2025-07-06 21:08:44 +03:00
|
|
|
default:
|
|
|
|
|
bbx_log(BBX_LOG_LEVEL_WARNING, "Unexpected value of /sys/class/graphics/fbcon/rotate");
|
|
|
|
|
break;
|
2025-06-18 18:13:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
end:
|
|
|
|
|
close(fd_rotate);
|
2021-11-23 20:04:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void print_usage() {
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
/*-------------------------------- 78 CHARS --------------------------------*/
|
|
|
|
|
"Usage: buffyboard [OPTION]\n"
|
|
|
|
|
"\n"
|
|
|
|
|
"Mandatory arguments to long options are mandatory for short options too.\n"
|
2024-03-30 12:46:09 +00:00
|
|
|
" -C, --config-override Path to a config override file. Can be supplied\n"
|
|
|
|
|
" multiple times. Config files are merged in the\n"
|
|
|
|
|
" following order:\n"
|
2024-09-24 19:09:53 +00:00
|
|
|
" * /usr/share/buffyboard/buffyboard.conf\n"
|
|
|
|
|
" * /usr/share/buffyboard/buffyboard.conf.d/* (alphabetically)\n"
|
2024-03-30 12:46:09 +00:00
|
|
|
" * /etc/buffyboard.conf\n"
|
|
|
|
|
" * /etc/buffyboard.conf.d/* (alphabetically)\n"
|
|
|
|
|
" * Override files (in supplied order)\n"
|
|
|
|
|
" -g, --geometry=NxM[@X,Y] Force a display size of N horizontal times M\n"
|
|
|
|
|
" vertical pixels, offset horizontally by X\n"
|
|
|
|
|
" pixels and vertically by Y pixels\n"
|
|
|
|
|
" -d --dpi=N Override the display's DPI value\n"
|
|
|
|
|
" -r, --rotate=[0-3] Rotate the UI to the given orientation. The\n"
|
|
|
|
|
" values match the ones provided by the kernel in\n"
|
|
|
|
|
" /sys/class/graphics/fbcon/rotate.\n"
|
|
|
|
|
" * 0 - normal orientation (0 degree)\n"
|
|
|
|
|
" * 1 - clockwise orientation (90 degrees)\n"
|
|
|
|
|
" * 2 - upside down orientation (180 degrees)\n"
|
|
|
|
|
" * 3 - counterclockwise orientation (270 degrees)\n"
|
|
|
|
|
" -h, --help Print this message and exit\n"
|
2024-04-01 16:21:44 +02:00
|
|
|
" -v, --verbose Enable more detailed logging output on STDERR\n"
|
2024-03-30 12:46:09 +00:00
|
|
|
" -V, --version Print the buffyboard version and exit\n");
|
2021-11-23 20:04:23 +01:00
|
|
|
/*-------------------------------- 78 CHARS --------------------------------*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Public functions
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
void bb_cli_parse_opts(int argc, char *argv[], bb_cli_opts *opts) {
|
|
|
|
|
init_opts(opts);
|
|
|
|
|
|
|
|
|
|
struct option long_opts[] = {
|
2024-03-30 08:06:29 +01:00
|
|
|
{ "config-override", required_argument, NULL, 'C' },
|
2024-03-30 12:46:09 +00:00
|
|
|
{ "geometry", required_argument, NULL, 'g' },
|
|
|
|
|
{ "dpi", required_argument, NULL, 'd' },
|
|
|
|
|
{ "rotate", required_argument, NULL, 'r' },
|
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2024-04-01 16:21:44 +02:00
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
2024-03-30 12:46:09 +00:00
|
|
|
{ "version", no_argument, NULL, 'V' },
|
2021-11-23 20:04:23 +01:00
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int opt, index = 0;
|
|
|
|
|
|
2024-04-01 16:21:44 +02:00
|
|
|
while ((opt = getopt_long(argc, argv, "C:g:d:r:hvV", long_opts, &index)) != -1) {
|
2021-11-23 20:04:23 +01:00
|
|
|
switch (opt) {
|
2024-03-30 08:06:29 +01:00
|
|
|
case 'C':
|
|
|
|
|
opts->config_files = realloc(opts->config_files, (opts->num_config_files + 1) * sizeof(char *));
|
|
|
|
|
if (!opts->config_files) {
|
2024-03-30 20:52:01 +01:00
|
|
|
bbx_log(BBX_LOG_LEVEL_ERROR, "Could not allocate memory for config file paths");
|
2024-03-30 08:06:29 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
opts->config_files[opts->num_config_files] = optarg;
|
|
|
|
|
opts->num_config_files++;
|
|
|
|
|
break;
|
2024-03-30 12:46:09 +00:00
|
|
|
case 'g':
|
2025-09-23 01:48:35 -07:00
|
|
|
if (bbx_cli_parse_geometry(optarg, &opts->common) != 0) {
|
|
|
|
|
exit(EXIT_FAILURE);
|
2024-03-30 12:46:09 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'd':
|
2025-09-23 01:48:35 -07:00
|
|
|
if (bbx_cli_parse_dpi(optarg, &opts->common) != 0) {
|
2024-03-30 12:46:09 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2021-11-23 20:04:23 +01:00
|
|
|
case 'r': {
|
2025-06-18 18:13:25 +03:00
|
|
|
unsigned int orientation;
|
|
|
|
|
if (sscanf(optarg, "%u", &orientation) != 1 || orientation > 3) {
|
|
|
|
|
bbx_log(BBX_LOG_LEVEL_ERROR, "Invalid orientation argument \"%s\"\n", optarg);
|
2021-11-23 20:04:23 +01:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
switch (orientation) {
|
|
|
|
|
case 0:
|
2024-02-19 13:20:20 +00:00
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_0;
|
2021-11-23 20:04:23 +01:00
|
|
|
break;
|
|
|
|
|
case 1:
|
2024-02-19 13:20:20 +00:00
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_270;
|
2021-11-23 20:04:23 +01:00
|
|
|
break;
|
|
|
|
|
case 2:
|
2024-02-19 13:20:20 +00:00
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_180;
|
2021-11-23 20:04:23 +01:00
|
|
|
break;
|
|
|
|
|
case 3:
|
2024-02-19 13:20:20 +00:00
|
|
|
opts->rotation = LV_DISPLAY_ROTATION_90;
|
2021-11-23 20:04:23 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case 'h':
|
|
|
|
|
print_usage();
|
|
|
|
|
exit(EXIT_SUCCESS);
|
2024-04-01 16:21:44 +02:00
|
|
|
case 'v':
|
2025-09-23 01:48:35 -07:00
|
|
|
opts->common.verbose = true;
|
2024-04-01 16:21:44 +02:00
|
|
|
break;
|
2021-11-23 20:04:23 +01:00
|
|
|
case 'V':
|
2025-09-23 01:48:35 -07:00
|
|
|
bbx_cli_print_version_and_exit("unl0kr");
|
|
|
|
|
break;
|
2021-11-23 20:04:23 +01:00
|
|
|
default:
|
|
|
|
|
print_usage();
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|