Serenity Operating System
at portability 97 lines 3.3 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <AK/FileSystemPath.h> 28#include <AK/String.h> 29#include <AK/StringBuilder.h> 30#include <AK/Vector.h> 31#include <LibCore/ArgsParser.h> 32#include <LibCore/DirIterator.h> 33#include <LibGUI/Application.h> 34#include <LibGUI/Desktop.h> 35#include <dirent.h> 36#include <errno.h> 37#include <fcntl.h> 38#include <stdio.h> 39#include <stdlib.h> 40#include <string.h> 41#include <unistd.h> 42 43static int handle_show_all() 44{ 45 Core::DirIterator di("/res/wallpapers", Core::DirIterator::SkipDots); 46 if (di.has_error()) { 47 fprintf(stderr, "CDirIterator: %s\n", di.error_string()); 48 return 1; 49 } 50 51 while (di.has_next()) { 52 String name = di.next_path(); 53 printf("%s\n", name.characters()); 54 } 55 return 0; 56} 57 58static int handle_show_current() 59{ 60 printf("%s\n", GUI::Desktop::the().wallpaper().characters()); 61 return 0; 62} 63 64static int handle_set_pape(const String& name) 65{ 66 StringBuilder builder; 67 builder.append("/res/wallpapers/"); 68 builder.append(name); 69 String path = builder.to_string(); 70 if (!GUI::Desktop::the().set_wallpaper(path)) { 71 fprintf(stderr, "pape: Failed to set wallpaper %s\n", path.characters()); 72 return 1; 73 } 74 return 0; 75}; 76 77int main(int argc, char** argv) 78{ 79 bool show_all = false; 80 bool show_current = false; 81 const char* name = nullptr; 82 83 Core::ArgsParser args_parser; 84 args_parser.add_option(show_all, "Show all wallpapers", "show-all", 'a'); 85 args_parser.add_option(show_current, "Show current wallpaper", "show-current", 'c'); 86 args_parser.add_positional_argument(name, "Wallpaper to set", "name", Core::ArgsParser::Required::No); 87 args_parser.parse(argc, argv); 88 89 GUI::Application app(argc, argv); 90 91 if (show_all) 92 return handle_show_all(); 93 else if (show_current) 94 return handle_show_current(); 95 96 return handle_set_pape(name); 97}