[4db37d1] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Matthieu Riolo
|
---|
| 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
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <stdio.h>
|
---|
| 30 | #include <stdlib.h>
|
---|
| 31 | #include <str.h>
|
---|
| 32 | #include <dirent.h>
|
---|
| 33 | #include <ipc/services.h>
|
---|
| 34 | #include <ipc/input.h>
|
---|
| 35 | #include <abi/ipc/interfaces.h>
|
---|
| 36 | #include <loc.h>
|
---|
| 37 |
|
---|
| 38 | #include "config.h"
|
---|
| 39 | #include "util.h"
|
---|
| 40 | #include "errors.h"
|
---|
| 41 | #include "entry.h"
|
---|
| 42 | #include "layout.h"
|
---|
| 43 | #include "cmds.h"
|
---|
| 44 |
|
---|
| 45 | static const char *cmdname = "layout";
|
---|
| 46 | static const char *path_layouts = "/lib/layouts/";
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | /* Dispays help for layout in various levels */
|
---|
| 50 | void help_cmd_layout(unsigned int level)
|
---|
| 51 | {
|
---|
| 52 | printf("Changes, list or display the current keyboard layout.\n");
|
---|
| 53 |
|
---|
| 54 | if (level != HELP_SHORT) {
|
---|
| 55 | printf(
|
---|
| 56 | "Usage: %s\n"
|
---|
| 57 | "\t%s list\tlists all layouts\n"
|
---|
| 58 | "\t%s get\t displays currently set layout\n"
|
---|
| 59 | "\t%s set <layout>\tchanges to the new layout\n",
|
---|
| 60 | cmdname, cmdname, cmdname, cmdname);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | /* lists all available kb layouts */
|
---|
| 67 | static int cmd_layout_list(void)
|
---|
| 68 | {
|
---|
| 69 | DIR *dir = opendir(path_layouts);
|
---|
| 70 | if (!dir) {
|
---|
| 71 | cli_error(CL_ENOENT, "%s: Error reading directory %s\n", cmdname, path_layouts);
|
---|
| 72 | return CMD_FAILURE;
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | struct dirent *dp;
|
---|
| 76 | int ERROR = CMD_SUCCESS;
|
---|
| 77 |
|
---|
| 78 | while ((dp = readdir(dir))) {
|
---|
| 79 | char *suffixpos = str_chr(dp->d_name, '.');
|
---|
| 80 | if (suffixpos == NULL || suffixpos == dp->d_name) {
|
---|
| 81 | continue;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | if (str_cmp(suffixpos, ".so") != 0) {
|
---|
| 85 | continue;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | char *name = str_ndup(dp->d_name, suffixpos - dp->d_name);
|
---|
| 89 | if (name == NULL) {
|
---|
| 90 | cli_error(CL_ENOMEM, "%s: Error while duplicating file name\n", cmdname);
|
---|
| 91 | ERROR = CMD_FAILURE;
|
---|
| 92 | goto error;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | printf("%s\n", name);
|
---|
| 96 | free(name);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | error:
|
---|
| 100 | closedir(dir);
|
---|
| 101 | return ERROR;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static int cmd_layout_get(void)
|
---|
| 105 | {
|
---|
| 106 | return CMD_FAILURE;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | static int cmd_layout_set(char *layout)
|
---|
| 111 | {
|
---|
| 112 | #ifdef CONFIG_RTLD
|
---|
| 113 | service_id_t svcid;
|
---|
| 114 | ipc_call_t call;
|
---|
| 115 | errno_t rc = loc_service_get_id(SERVICE_NAME_HID_INPUT, &svcid, 0);
|
---|
| 116 | if (rc != EOK) {
|
---|
| 117 | cli_error(CL_ENOENT, "%s: Failing to find service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
|
---|
| 118 | return CMD_FAILURE;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | async_sess_t *sess = loc_service_connect(svcid, INTERFACE_ANY, 0);
|
---|
| 122 | if (sess == NULL) {
|
---|
| 123 | cli_error(CL_ENOENT, "%s: Failing to connect to service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
|
---|
| 124 | return CMD_FAILURE;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 128 |
|
---|
| 129 | aid_t mid = async_send_0(exch, INPUT_CHANGE_LAYOUT, &call);
|
---|
| 130 | rc = async_data_write_start(exch, layout, str_size(layout));
|
---|
| 131 |
|
---|
| 132 | if (rc == EOK)
|
---|
| 133 | async_wait_for(mid, &rc);
|
---|
| 134 |
|
---|
| 135 | async_exchange_end(exch);
|
---|
| 136 | async_hangup(sess);
|
---|
| 137 |
|
---|
| 138 | return rc == EOK ? CMD_SUCCESS : CMD_FAILURE;
|
---|
| 139 | #else
|
---|
| 140 | cli_error(CL_ENOTSUP, "%s: No support for RTLD\n", cmdname);
|
---|
| 141 | return CMD_FAILURE;
|
---|
| 142 | #endif
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /* Main entry point for layout, accepts an array of arguments */
|
---|
| 146 | int cmd_layout(char **argv)
|
---|
| 147 | {
|
---|
| 148 | unsigned int argc = cli_count_args(argv);
|
---|
| 149 |
|
---|
| 150 | if (argc == 2 || argc == 3) {
|
---|
| 151 | if (str_cmp(argv[1], "list") == 0) {
|
---|
| 152 | return cmd_layout_list();
|
---|
| 153 | } else if (str_cmp(argv[1], "get") == 0) {
|
---|
| 154 | return cmd_layout_get();
|
---|
| 155 | } else if (str_cmp(argv[1], "set") == 0 && argc == 3) {
|
---|
| 156 | return cmd_layout_set(argv[2]);
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | help_cmd_layout(HELP_LONG);
|
---|
| 161 | return CMD_FAILURE;
|
---|
| 162 | }
|
---|
| 163 |
|
---|