[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>
|
---|
[a8171b86] | 32 | #include <str_error.h>
|
---|
[4db37d1] | 33 | #include <dirent.h>
|
---|
| 34 | #include <ipc/services.h>
|
---|
| 35 | #include <ipc/input.h>
|
---|
| 36 | #include <abi/ipc/interfaces.h>
|
---|
| 37 | #include <loc.h>
|
---|
| 38 |
|
---|
| 39 | #include "config.h"
|
---|
| 40 | #include "util.h"
|
---|
| 41 | #include "errors.h"
|
---|
| 42 | #include "entry.h"
|
---|
| 43 | #include "layout.h"
|
---|
| 44 | #include "cmds.h"
|
---|
| 45 |
|
---|
| 46 | static const char *cmdname = "layout";
|
---|
| 47 | static const char *path_layouts = "/lib/layouts/";
|
---|
| 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(
|
---|
[a8171b86] | 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);
|
---|
[4db37d1] | 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 |
|
---|
[a8171b86] | 104 | /* displays active keyboard layout */
|
---|
[4db37d1] | 105 | static int cmd_layout_get(void)
|
---|
| 106 | {
|
---|
[a8171b86] | 107 | service_id_t svcid;
|
---|
| 108 | ipc_call_t call;
|
---|
| 109 | errno_t rc = loc_service_get_id(SERVICE_NAME_HID_INPUT, &svcid, 0);
|
---|
| 110 | if (rc != EOK) {
|
---|
| 111 | cli_error(CL_ENOENT, "%s: Failing to find service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
|
---|
| 112 | return CMD_FAILURE;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | async_sess_t *sess = loc_service_connect(svcid, INTERFACE_ANY, 0);
|
---|
| 116 | if (sess == NULL) {
|
---|
| 117 | cli_error(CL_ENOENT, "%s: Failing to connect to service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
|
---|
| 118 | return CMD_FAILURE;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | void *layout_name = NULL;
|
---|
| 122 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 123 | aid_t mid = async_send_0(exch, INPUT_GET_LAYOUT, &call);
|
---|
| 124 | async_wait_for(mid, &rc);
|
---|
| 125 |
|
---|
| 126 | if (rc != EOK) {
|
---|
| 127 | goto error;
|
---|
| 128 | }
|
---|
[4db37d1] | 129 |
|
---|
[a8171b86] | 130 | size_t length = ipc_get_arg1(&call);
|
---|
[4db37d1] | 131 |
|
---|
[a8171b86] | 132 | layout_name = malloc(length * sizeof(char *));
|
---|
| 133 | if (layout_name == NULL) {
|
---|
| 134 | cli_error(CL_ENOMEM, "%s: Failing to allocate memory for keyboard layout\n", cmdname);
|
---|
| 135 | rc = ENOMEM;
|
---|
| 136 | goto error;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | rc = async_data_read_start(exch, layout_name, length);
|
---|
| 140 |
|
---|
| 141 | if (rc == EOK) {
|
---|
| 142 | printf("%s\n", (char *)layout_name);
|
---|
| 143 | } else {
|
---|
| 144 | printf("%s: Failing to get activated keyboard layout\n (%s)\n", cmdname, str_error(rc));
|
---|
| 145 | goto error;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | error:
|
---|
| 149 | free(layout_name);
|
---|
| 150 | async_exchange_end(exch);
|
---|
| 151 | async_hangup(sess);
|
---|
| 152 |
|
---|
| 153 | return rc == EOK ? CMD_SUCCESS : CMD_FAILURE;
|
---|
| 154 |
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /* changes the keyboard layout */
|
---|
[4db37d1] | 158 | static int cmd_layout_set(char *layout)
|
---|
| 159 | {
|
---|
| 160 | #ifdef CONFIG_RTLD
|
---|
| 161 | service_id_t svcid;
|
---|
| 162 | ipc_call_t call;
|
---|
| 163 | errno_t rc = loc_service_get_id(SERVICE_NAME_HID_INPUT, &svcid, 0);
|
---|
| 164 | if (rc != EOK) {
|
---|
| 165 | cli_error(CL_ENOENT, "%s: Failing to find service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
|
---|
| 166 | return CMD_FAILURE;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | async_sess_t *sess = loc_service_connect(svcid, INTERFACE_ANY, 0);
|
---|
| 170 | if (sess == NULL) {
|
---|
| 171 | cli_error(CL_ENOENT, "%s: Failing to connect to service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
|
---|
| 172 | return CMD_FAILURE;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
| 176 |
|
---|
| 177 | aid_t mid = async_send_0(exch, INPUT_CHANGE_LAYOUT, &call);
|
---|
| 178 | rc = async_data_write_start(exch, layout, str_size(layout));
|
---|
[a8171b86] | 179 |
|
---|
| 180 | if (rc == EOK) {
|
---|
[4db37d1] | 181 | async_wait_for(mid, &rc);
|
---|
[a8171b86] | 182 | }
|
---|
| 183 |
|
---|
[4db37d1] | 184 | async_exchange_end(exch);
|
---|
| 185 | async_hangup(sess);
|
---|
| 186 |
|
---|
[a8171b86] | 187 | if (rc != EOK) {
|
---|
| 188 | printf("%s: Cannot activate keyboard layout `%s`\n (%s)\n", cmdname, layout, str_error(rc));
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[4db37d1] | 191 | return rc == EOK ? CMD_SUCCESS : CMD_FAILURE;
|
---|
| 192 | #else
|
---|
| 193 | cli_error(CL_ENOTSUP, "%s: No support for RTLD\n", cmdname);
|
---|
| 194 | return CMD_FAILURE;
|
---|
| 195 | #endif
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | /* Main entry point for layout, accepts an array of arguments */
|
---|
| 199 | int cmd_layout(char **argv)
|
---|
| 200 | {
|
---|
| 201 | unsigned int argc = cli_count_args(argv);
|
---|
| 202 |
|
---|
| 203 | if (argc == 2 || argc == 3) {
|
---|
| 204 | if (str_cmp(argv[1], "list") == 0) {
|
---|
| 205 | return cmd_layout_list();
|
---|
| 206 | } else if (str_cmp(argv[1], "get") == 0) {
|
---|
| 207 | return cmd_layout_get();
|
---|
| 208 | } else if (str_cmp(argv[1], "set") == 0 && argc == 3) {
|
---|
| 209 | return cmd_layout_set(argv[2]);
|
---|
[a8171b86] | 210 | }
|
---|
[4db37d1] | 211 | }
|
---|
| 212 |
|
---|
| 213 | help_cmd_layout(HELP_LONG);
|
---|
| 214 | return CMD_FAILURE;
|
---|
| 215 | }
|
---|