source: mainline/uspace/app/layout/layout.c@ 19f60a38

Last change on this file since 19f60a38 was 19f60a38, checked in by Matthieu Riolo <matthieu.riolo@…>, 5 years ago

Adding new application layout

This commit adds an application called layout which
can list, set and get the active keyboard layout

  • Property mode set to 100644
File size: 4.7 KB
Line 
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/** @addtogroup layout
30 * @{
31 */
32/**
33 * @file
34 */
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <str.h>
39#include <str_error.h>
40#include <ipc/services.h>
41#include <ipc/input.h>
42#include <abi/ipc/interfaces.h>
43#include <loc.h>
44
45static const char *cmdname = "layout";
46
47/* Dispays help for layout in various levels */
48static void print_help()
49{
50 printf("Changes, lists or displays the current keyboard layout.\n");
51
52 printf(
53 "Usage: %s\n"
54 "\t%s list\tlists all layouts\n"
55 "\t%s get\t displays currently set layout\n"
56 "\t%s set <layout>\tchanges to the new layout\n",
57 cmdname, cmdname, cmdname, cmdname);
58}
59
60/* lists all available kb layouts */
61static errno_t list_layout(void)
62{
63 /* TODO: replace this with the content of the folder containing the keymaps */
64
65 printf("ar\n");
66 printf("cz\n");
67 printf("fr_azerty\n");
68 printf("us_dvorak\n");
69 printf("us_qwerty\n");
70
71 return EOK;
72}
73
74/* displays active keyboard layout */
75static errno_t get_layout(void)
76{
77 service_id_t svcid;
78 ipc_call_t call;
79 errno_t rc = loc_service_get_id(SERVICE_NAME_HID_INPUT, &svcid, 0);
80 if (rc != EOK) {
81 printf("%s: Failing to find service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
82 return rc;
83 }
84
85 async_sess_t *sess = loc_service_connect(svcid, INTERFACE_ANY, 0);
86 if (sess == NULL) {
87 printf("%s: Failing to connect to service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
88 return rc;
89 }
90
91 void *layout_name = NULL;
92 async_exch_t *exch = async_exchange_begin(sess);
93 aid_t mid = async_send_0(exch, INPUT_GET_LAYOUT, &call);
94 async_wait_for(mid, &rc);
95
96 if (rc != EOK) {
97 goto error;
98 }
99
100 size_t length = ipc_get_arg1(&call);
101
102 layout_name = malloc(length * sizeof(char *));
103 if (layout_name == NULL) {
104 printf("%s: Failing to allocate memory for keyboard layout\n", cmdname);
105 rc = ENOMEM;
106 goto error;
107 }
108
109 rc = async_data_read_start(exch, layout_name, length);
110
111 if (rc == EOK) {
112 printf("%s\n", (char *)layout_name);
113 } else {
114 printf("%s: Failing to get activated keyboard layout\n (%s)\n", cmdname, str_error(rc));
115 goto error;
116 }
117
118error:
119 free(layout_name);
120 async_exchange_end(exch);
121 async_hangup(sess);
122
123 return rc;
124
125}
126
127/* changes the keyboard layout */
128static errno_t set_layout(char *layout)
129{
130 service_id_t svcid;
131 ipc_call_t call;
132 errno_t rc = loc_service_get_id(SERVICE_NAME_HID_INPUT, &svcid, 0);
133 if (rc != EOK) {
134 printf("%s: Failing to find service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
135 return rc;
136 }
137
138 async_sess_t *sess = loc_service_connect(svcid, INTERFACE_ANY, 0);
139 if (sess == NULL) {
140 printf("%s: Failing to connect to service `%s`\n", cmdname, SERVICE_NAME_HID_INPUT);
141 return rc;
142 }
143
144 async_exch_t *exch = async_exchange_begin(sess);
145
146 aid_t mid = async_send_0(exch, INPUT_CHANGE_LAYOUT, &call);
147 rc = async_data_write_start(exch, layout, str_size(layout));
148
149 if (rc == EOK) {
150 async_wait_for(mid, &rc);
151 }
152
153 async_exchange_end(exch);
154 async_hangup(sess);
155
156 if (rc != EOK) {
157 printf("%s: Cannot activate keyboard layout `%s`\n (%s)\n", cmdname, layout, str_error(rc));
158 }
159
160 return rc;
161}
162
163int main(int argc, char *argv[])
164{
165 if (argc == 2) {
166 if (str_cmp(argv[1], "list") == 0) {
167 return list_layout();
168 } else if (str_cmp(argv[1], "get") == 0) {
169 return get_layout();
170 }
171 } else if (argc == 3) {
172 if (str_cmp(argv[1], "set") == 0 && argc == 3) {
173 return set_layout(argv[2]);
174 }
175 }
176
177 print_help();
178 return 1;
179}
Note: See TracBrowser for help on using the repository browser.