[fb623e2] | 1 | /*
|
---|
[ca48672] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[fb623e2] | 3 | * Copyright (c) 2009 Martin Decky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | #include <async.h>
|
---|
[8d6bcc8c] | 31 | #include <errno.h>
|
---|
[c1694b6b] | 32 | #include <str_error.h>
|
---|
[8d6bcc8c] | 33 | #include <fibril_synch.h>
|
---|
[fb623e2] | 34 | #include <ipc/services.h>
|
---|
| 35 | #include <ipc/clipboard.h>
|
---|
[8d6bcc8c] | 36 | #include <loc.h>
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdbool.h>
|
---|
[38d150e] | 39 | #include <stdlib.h>
|
---|
[8d6bcc8c] | 40 | #include <task.h>
|
---|
[fb623e2] | 41 |
|
---|
[d3a8e47] | 42 | #define NAME "clipboard"
|
---|
[fb623e2] | 43 |
|
---|
| 44 | static char *clip_data = NULL;
|
---|
| 45 | static size_t clip_size = 0;
|
---|
| 46 | static clipboard_tag_t clip_tag = CLIPBOARD_TAG_NONE;
|
---|
| 47 | static FIBRIL_MUTEX_INITIALIZE(clip_mtx);
|
---|
[8d6bcc8c] | 48 | static service_id_t svc_id;
|
---|
[fb623e2] | 49 |
|
---|
[984a9ba] | 50 | static void clip_put_data(ipc_call_t *req)
|
---|
[fb623e2] | 51 | {
|
---|
[531695f] | 52 | char *data;
|
---|
[b7fd2a0] | 53 | errno_t rc;
|
---|
[fb623e2] | 54 | size_t size;
|
---|
[a35b458] | 55 |
|
---|
[fafb8e5] | 56 | switch (ipc_get_arg1(req)) {
|
---|
[fb623e2] | 57 | case CLIPBOARD_TAG_NONE:
|
---|
| 58 | fibril_mutex_lock(&clip_mtx);
|
---|
[a35b458] | 59 |
|
---|
[fb623e2] | 60 | if (clip_data)
|
---|
| 61 | free(clip_data);
|
---|
[a35b458] | 62 |
|
---|
[fb623e2] | 63 | clip_data = NULL;
|
---|
| 64 | clip_size = 0;
|
---|
| 65 | clip_tag = CLIPBOARD_TAG_NONE;
|
---|
[a35b458] | 66 |
|
---|
[fb623e2] | 67 | fibril_mutex_unlock(&clip_mtx);
|
---|
[984a9ba] | 68 | async_answer_0(req, EOK);
|
---|
[fb623e2] | 69 | break;
|
---|
[472c09d] | 70 | case CLIPBOARD_TAG_DATA:
|
---|
[4cac2d69] | 71 | rc = async_data_write_accept((void **) &data, false, 0, 0, 0, &size);
|
---|
[531695f] | 72 | if (rc != EOK) {
|
---|
[984a9ba] | 73 | async_answer_0(req, rc);
|
---|
[fb623e2] | 74 | break;
|
---|
| 75 | }
|
---|
[a35b458] | 76 |
|
---|
[fb623e2] | 77 | fibril_mutex_lock(&clip_mtx);
|
---|
[a35b458] | 78 |
|
---|
[fb623e2] | 79 | if (clip_data)
|
---|
| 80 | free(clip_data);
|
---|
[a35b458] | 81 |
|
---|
[fb623e2] | 82 | clip_data = data;
|
---|
| 83 | clip_size = size;
|
---|
[472c09d] | 84 | clip_tag = CLIPBOARD_TAG_DATA;
|
---|
[a35b458] | 85 |
|
---|
[fb623e2] | 86 | fibril_mutex_unlock(&clip_mtx);
|
---|
[984a9ba] | 87 | async_answer_0(req, EOK);
|
---|
[fb623e2] | 88 | break;
|
---|
| 89 | default:
|
---|
[984a9ba] | 90 | async_answer_0(req, EINVAL);
|
---|
[fb623e2] | 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[984a9ba] | 94 | static void clip_get_data(ipc_call_t *req)
|
---|
[fb623e2] | 95 | {
|
---|
| 96 | fibril_mutex_lock(&clip_mtx);
|
---|
[a35b458] | 97 |
|
---|
[984a9ba] | 98 | ipc_call_t call;
|
---|
[fb623e2] | 99 | size_t size;
|
---|
[a35b458] | 100 |
|
---|
[fb623e2] | 101 | /* Check for clipboard data tag compatibility */
|
---|
[fafb8e5] | 102 | switch (ipc_get_arg1(req)) {
|
---|
[472c09d] | 103 | case CLIPBOARD_TAG_DATA:
|
---|
[984a9ba] | 104 | if (!async_data_read_receive(&call, &size)) {
|
---|
| 105 | async_answer_0(&call, EINVAL);
|
---|
| 106 | async_answer_0(req, EINVAL);
|
---|
[fb623e2] | 107 | break;
|
---|
| 108 | }
|
---|
[a35b458] | 109 |
|
---|
[472c09d] | 110 | if (clip_tag != CLIPBOARD_TAG_DATA) {
|
---|
| 111 | /* So far we only understand binary data */
|
---|
[984a9ba] | 112 | async_answer_0(&call, EOVERFLOW);
|
---|
| 113 | async_answer_0(req, EOVERFLOW);
|
---|
[fb623e2] | 114 | break;
|
---|
| 115 | }
|
---|
[a35b458] | 116 |
|
---|
[fb623e2] | 117 | if (clip_size != size) {
|
---|
| 118 | /* The client expects different size of data */
|
---|
[984a9ba] | 119 | async_answer_0(&call, EOVERFLOW);
|
---|
| 120 | async_answer_0(req, EOVERFLOW);
|
---|
[fb623e2] | 121 | break;
|
---|
| 122 | }
|
---|
[a35b458] | 123 |
|
---|
[984a9ba] | 124 | errno_t retval = async_data_read_finalize(&call, clip_data, size);
|
---|
[fb623e2] | 125 | if (retval != EOK) {
|
---|
[984a9ba] | 126 | async_answer_0(req, retval);
|
---|
[fb623e2] | 127 | break;
|
---|
| 128 | }
|
---|
[a35b458] | 129 |
|
---|
[984a9ba] | 130 | async_answer_0(req, EOK);
|
---|
[dc12262] | 131 | break;
|
---|
[fb623e2] | 132 | default:
|
---|
| 133 | /*
|
---|
| 134 | * Sorry, we don't know how to get unknown or NONE
|
---|
| 135 | * data from the clipbard
|
---|
| 136 | */
|
---|
[984a9ba] | 137 | async_answer_0(req, EINVAL);
|
---|
[fb623e2] | 138 | break;
|
---|
| 139 | }
|
---|
[a35b458] | 140 |
|
---|
[fb623e2] | 141 | fibril_mutex_unlock(&clip_mtx);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[984a9ba] | 144 | static void clip_content(ipc_call_t *req)
|
---|
[fb623e2] | 145 | {
|
---|
| 146 | fibril_mutex_lock(&clip_mtx);
|
---|
[a35b458] | 147 |
|
---|
[fb623e2] | 148 | size_t size = clip_size;
|
---|
| 149 | clipboard_tag_t tag = clip_tag;
|
---|
[a35b458] | 150 |
|
---|
[fb623e2] | 151 | fibril_mutex_unlock(&clip_mtx);
|
---|
[984a9ba] | 152 | async_answer_2(req, EOK, (sysarg_t) size, (sysarg_t) tag);
|
---|
[fb623e2] | 153 | }
|
---|
| 154 |
|
---|
[984a9ba] | 155 | static void clip_connection(ipc_call_t *icall, void *arg)
|
---|
[fb623e2] | 156 | {
|
---|
| 157 | /* Accept connection */
|
---|
[beb83c1] | 158 | async_accept_0(icall);
|
---|
[a35b458] | 159 |
|
---|
[79ae36dd] | 160 | while (true) {
|
---|
[fb623e2] | 161 | ipc_call_t call;
|
---|
[984a9ba] | 162 | async_get_call(&call);
|
---|
[a35b458] | 163 |
|
---|
[fafb8e5] | 164 | if (!ipc_get_imethod(&call)) {
|
---|
[889cdb1] | 165 | async_answer_0(&call, EOK);
|
---|
[79ae36dd] | 166 | break;
|
---|
[889cdb1] | 167 | }
|
---|
[a35b458] | 168 |
|
---|
[fafb8e5] | 169 | switch (ipc_get_imethod(&call)) {
|
---|
[fb623e2] | 170 | case CLIPBOARD_PUT_DATA:
|
---|
[984a9ba] | 171 | clip_put_data(&call);
|
---|
[fb623e2] | 172 | break;
|
---|
| 173 | case CLIPBOARD_GET_DATA:
|
---|
[984a9ba] | 174 | clip_get_data(&call);
|
---|
[fb623e2] | 175 | break;
|
---|
| 176 | case CLIPBOARD_CONTENT:
|
---|
[984a9ba] | 177 | clip_content(&call);
|
---|
[fb623e2] | 178 | break;
|
---|
| 179 | default:
|
---|
[984a9ba] | 180 | async_answer_0(&call, ENOENT);
|
---|
[fb623e2] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | int main(int argc, char *argv[])
|
---|
| 186 | {
|
---|
[b7fd2a0] | 187 | errno_t rc;
|
---|
[4c6fd56] | 188 | loc_srv_t *srv;
|
---|
[a35b458] | 189 |
|
---|
[8d6bcc8c] | 190 | printf("%s: HelenOS clipboard service\n", NAME);
|
---|
[b688fd8] | 191 | async_set_fallback_port_handler(clip_connection, NULL);
|
---|
[a35b458] | 192 |
|
---|
[4c6fd56] | 193 | rc = loc_server_register(NAME, &srv);
|
---|
[8d6bcc8c] | 194 | if (rc != EOK) {
|
---|
[c1694b6b] | 195 | printf("%s: Failed registering server: %s\n", NAME, str_error(rc));
|
---|
[a47f522] | 196 | return rc;
|
---|
[8d6bcc8c] | 197 | }
|
---|
[a35b458] | 198 |
|
---|
[ca48672] | 199 | rc = loc_service_register(srv, SERVICE_NAME_CLIPBOARD,
|
---|
| 200 | fallback_port_id, &svc_id);
|
---|
[8d6bcc8c] | 201 | if (rc != EOK) {
|
---|
[4c6fd56] | 202 | loc_server_unregister(srv);
|
---|
| 203 |
|
---|
[c1694b6b] | 204 | printf("%s: Failed registering service : %s\n", NAME, str_error(rc));
|
---|
[8d6bcc8c] | 205 | return rc;
|
---|
| 206 | }
|
---|
[a35b458] | 207 |
|
---|
[79ae36dd] | 208 | printf("%s: Accepting connections\n", NAME);
|
---|
| 209 | task_retval(0);
|
---|
[fb623e2] | 210 | async_manager();
|
---|
[a35b458] | 211 |
|
---|
[fb623e2] | 212 | /* Never reached */
|
---|
| 213 | return 0;
|
---|
| 214 | }
|
---|