| 1 | /*
|
|---|
| 2 | * Copyright (c) 2009 Martin Decky
|
|---|
| 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 <async.h>
|
|---|
| 30 | #include <errno.h>
|
|---|
| 31 | #include <str_error.h>
|
|---|
| 32 | #include <fibril_synch.h>
|
|---|
| 33 | #include <ipc/services.h>
|
|---|
| 34 | #include <ipc/clipboard.h>
|
|---|
| 35 | #include <loc.h>
|
|---|
| 36 | #include <stdio.h>
|
|---|
| 37 | #include <stdbool.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <task.h>
|
|---|
| 40 |
|
|---|
| 41 | #define NAME "clipboard"
|
|---|
| 42 |
|
|---|
| 43 | static char *clip_data = NULL;
|
|---|
| 44 | static size_t clip_size = 0;
|
|---|
| 45 | static clipboard_tag_t clip_tag = CLIPBOARD_TAG_NONE;
|
|---|
| 46 | static FIBRIL_MUTEX_INITIALIZE(clip_mtx);
|
|---|
| 47 | static service_id_t svc_id;
|
|---|
| 48 |
|
|---|
| 49 | static void clip_put_data(ipc_call_t *req)
|
|---|
| 50 | {
|
|---|
| 51 | char *data;
|
|---|
| 52 | errno_t rc;
|
|---|
| 53 | size_t size;
|
|---|
| 54 |
|
|---|
| 55 | switch (IPC_GET_ARG1(*req)) {
|
|---|
| 56 | case CLIPBOARD_TAG_NONE:
|
|---|
| 57 | fibril_mutex_lock(&clip_mtx);
|
|---|
| 58 |
|
|---|
| 59 | if (clip_data)
|
|---|
| 60 | free(clip_data);
|
|---|
| 61 |
|
|---|
| 62 | clip_data = NULL;
|
|---|
| 63 | clip_size = 0;
|
|---|
| 64 | clip_tag = CLIPBOARD_TAG_NONE;
|
|---|
| 65 |
|
|---|
| 66 | fibril_mutex_unlock(&clip_mtx);
|
|---|
| 67 | async_answer_0(req, EOK);
|
|---|
| 68 | break;
|
|---|
| 69 | case CLIPBOARD_TAG_DATA:
|
|---|
| 70 | rc = async_data_write_accept((void **) &data, false, 0, 0, 0, &size);
|
|---|
| 71 | if (rc != EOK) {
|
|---|
| 72 | async_answer_0(req, rc);
|
|---|
| 73 | break;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | fibril_mutex_lock(&clip_mtx);
|
|---|
| 77 |
|
|---|
| 78 | if (clip_data)
|
|---|
| 79 | free(clip_data);
|
|---|
| 80 |
|
|---|
| 81 | clip_data = data;
|
|---|
| 82 | clip_size = size;
|
|---|
| 83 | clip_tag = CLIPBOARD_TAG_DATA;
|
|---|
| 84 |
|
|---|
| 85 | fibril_mutex_unlock(&clip_mtx);
|
|---|
| 86 | async_answer_0(req, EOK);
|
|---|
| 87 | break;
|
|---|
| 88 | default:
|
|---|
| 89 | async_answer_0(req, EINVAL);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | static void clip_get_data(ipc_call_t *req)
|
|---|
| 94 | {
|
|---|
| 95 | fibril_mutex_lock(&clip_mtx);
|
|---|
| 96 |
|
|---|
| 97 | ipc_call_t call;
|
|---|
| 98 | size_t size;
|
|---|
| 99 |
|
|---|
| 100 | /* Check for clipboard data tag compatibility */
|
|---|
| 101 | switch (IPC_GET_ARG1(*req)) {
|
|---|
| 102 | case CLIPBOARD_TAG_DATA:
|
|---|
| 103 | if (!async_data_read_receive(&call, &size)) {
|
|---|
| 104 | async_answer_0(&call, EINVAL);
|
|---|
| 105 | async_answer_0(req, EINVAL);
|
|---|
| 106 | break;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if (clip_tag != CLIPBOARD_TAG_DATA) {
|
|---|
| 110 | /* So far we only understand binary data */
|
|---|
| 111 | async_answer_0(&call, EOVERFLOW);
|
|---|
| 112 | async_answer_0(req, EOVERFLOW);
|
|---|
| 113 | break;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if (clip_size != size) {
|
|---|
| 117 | /* The client expects different size of data */
|
|---|
| 118 | async_answer_0(&call, EOVERFLOW);
|
|---|
| 119 | async_answer_0(req, EOVERFLOW);
|
|---|
| 120 | break;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | errno_t retval = async_data_read_finalize(&call, clip_data, size);
|
|---|
| 124 | if (retval != EOK) {
|
|---|
| 125 | async_answer_0(req, retval);
|
|---|
| 126 | break;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | async_answer_0(req, EOK);
|
|---|
| 130 | break;
|
|---|
| 131 | default:
|
|---|
| 132 | /*
|
|---|
| 133 | * Sorry, we don't know how to get unknown or NONE
|
|---|
| 134 | * data from the clipbard
|
|---|
| 135 | */
|
|---|
| 136 | async_answer_0(req, EINVAL);
|
|---|
| 137 | break;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | fibril_mutex_unlock(&clip_mtx);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | static void clip_content(ipc_call_t *req)
|
|---|
| 144 | {
|
|---|
| 145 | fibril_mutex_lock(&clip_mtx);
|
|---|
| 146 |
|
|---|
| 147 | size_t size = clip_size;
|
|---|
| 148 | clipboard_tag_t tag = clip_tag;
|
|---|
| 149 |
|
|---|
| 150 | fibril_mutex_unlock(&clip_mtx);
|
|---|
| 151 | async_answer_2(req, EOK, (sysarg_t) size, (sysarg_t) tag);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | static void clip_connection(ipc_call_t *icall, void *arg)
|
|---|
| 155 | {
|
|---|
| 156 | /* Accept connection */
|
|---|
| 157 | async_answer_0(icall, EOK);
|
|---|
| 158 |
|
|---|
| 159 | while (true) {
|
|---|
| 160 | ipc_call_t call;
|
|---|
| 161 | async_get_call(&call);
|
|---|
| 162 |
|
|---|
| 163 | if (!IPC_GET_IMETHOD(call))
|
|---|
| 164 | break;
|
|---|
| 165 |
|
|---|
| 166 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 167 | case CLIPBOARD_PUT_DATA:
|
|---|
| 168 | clip_put_data(&call);
|
|---|
| 169 | break;
|
|---|
| 170 | case CLIPBOARD_GET_DATA:
|
|---|
| 171 | clip_get_data(&call);
|
|---|
| 172 | break;
|
|---|
| 173 | case CLIPBOARD_CONTENT:
|
|---|
| 174 | clip_content(&call);
|
|---|
| 175 | break;
|
|---|
| 176 | default:
|
|---|
| 177 | async_answer_0(&call, ENOENT);
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | int main(int argc, char *argv[])
|
|---|
| 183 | {
|
|---|
| 184 | errno_t rc;
|
|---|
| 185 |
|
|---|
| 186 | printf("%s: HelenOS clipboard service\n", NAME);
|
|---|
| 187 | async_set_fallback_port_handler(clip_connection, NULL);
|
|---|
| 188 |
|
|---|
| 189 | rc = loc_server_register(NAME);
|
|---|
| 190 | if (rc != EOK) {
|
|---|
| 191 | printf("%s: Failed registering server: %s\n", NAME, str_error(rc));
|
|---|
| 192 | return rc;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | rc = loc_service_register(SERVICE_NAME_CLIPBOARD, &svc_id);
|
|---|
| 196 | if (rc != EOK) {
|
|---|
| 197 | printf("%s: Failed registering service : %s\n", NAME, str_error(rc));
|
|---|
| 198 | return rc;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | printf("%s: Accepting connections\n", NAME);
|
|---|
| 202 | task_retval(0);
|
|---|
| 203 | async_manager();
|
|---|
| 204 |
|
|---|
| 205 | /* Never reached */
|
|---|
| 206 | return 0;
|
|---|
| 207 | }
|
|---|