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