Serenity Operating System
1/*
2 * Copyright (c) 2021, Nico Weber <thakis@chromium.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibCore/ArgsParser.h>
8#include <LibGUI/Application.h>
9#include <LibGUI/ConnectionToWindowServer.h>
10#include <LibMain/Main.h>
11
12ErrorOr<int> serenity_main(Main::Arguments arguments)
13{
14 int screen = 0;
15 int width = -1;
16 int height = -1;
17 int scale = -1;
18
19 Core::ArgsParser args_parser;
20 args_parser.set_general_help("Change the screen resolution.");
21 args_parser.add_option(screen, "Screen", "screen", 's', "screen");
22 args_parser.add_positional_argument(width, "Width", "width");
23 args_parser.add_positional_argument(height, "Height", "height");
24 args_parser.add_positional_argument(scale, "Scale Factor", "scale", Core::ArgsParser::Required::No);
25 args_parser.parse(arguments);
26
27 // A Core::EventLoop is all we need, but ConnectionToWindowServer needs a full Application object.
28 char* dummy_argv[] = { arguments.argv[0] };
29 auto app = TRY(GUI::Application::try_create(1, dummy_argv));
30 auto screen_layout = GUI::ConnectionToWindowServer::the().get_screen_layout();
31 if (screen < 0 || (size_t)screen >= screen_layout.screens.size()) {
32 warnln("invalid screen index: {}", screen);
33 return 1;
34 }
35 auto& main_screen = screen_layout.screens[screen];
36 main_screen.resolution = { width, height };
37 if (scale != -1)
38 main_screen.scale_factor = scale;
39 auto set_result = GUI::ConnectionToWindowServer::the().set_screen_layout(screen_layout, true);
40 if (!set_result.success()) {
41 warnln("failed to set resolution: {}", set_result.error_msg());
42 return 1;
43 }
44
45 return 0;
46}