[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 <ipc/ipc.h>
|
---|
| 32 | #include <async.h>
|
---|
| 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);
|
---|
| 64 | ipc_answer_0(rid, EOK);
|
---|
| 65 | break;
|
---|
[472c09d] | 66 | case CLIPBOARD_TAG_DATA:
|
---|
| 67 | rc = async_data_receive(&data, 0, 0, &size);
|
---|
[531695f] | 68 | if (rc != EOK) {
|
---|
| 69 | ipc_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);
|
---|
| 83 | ipc_answer_0(rid, EOK);
|
---|
| 84 | break;
|
---|
| 85 | default:
|
---|
| 86 | ipc_answer_0(rid, EINVAL);
|
---|
| 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)) {
|
---|
| 101 | ipc_answer_0(callid, EINVAL);
|
---|
| 102 | ipc_answer_0(rid, EINVAL);
|
---|
| 103 | break;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[472c09d] | 106 | if (clip_tag != CLIPBOARD_TAG_DATA) {
|
---|
| 107 | /* So far we only understand binary data */
|
---|
[fb623e2] | 108 | ipc_answer_0(callid, EOVERFLOW);
|
---|
| 109 | ipc_answer_0(rid, EOVERFLOW);
|
---|
| 110 | break;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if (clip_size != size) {
|
---|
| 114 | /* The client expects different size of data */
|
---|
| 115 | ipc_answer_0(callid, EOVERFLOW);
|
---|
| 116 | ipc_answer_0(rid, EOVERFLOW);
|
---|
| 117 | break;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | ipcarg_t retval = async_data_read_finalize(callid, clip_data, size);
|
---|
| 121 | if (retval != EOK) {
|
---|
| 122 | ipc_answer_0(rid, retval);
|
---|
| 123 | break;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | ipc_answer_0(rid, EOK);
|
---|
| 127 | default:
|
---|
| 128 | /*
|
---|
| 129 | * Sorry, we don't know how to get unknown or NONE
|
---|
| 130 | * data from the clipbard
|
---|
| 131 | */
|
---|
| 132 | ipc_answer_0(rid, EINVAL);
|
---|
| 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);
|
---|
| 147 | ipc_answer_2(rid, EOK, (ipcarg_t) size, (ipcarg_t) clip_tag);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static void clip_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
| 151 | {
|
---|
| 152 | /* Accept connection */
|
---|
| 153 | ipc_answer_0(iid, EOK);
|
---|
| 154 |
|
---|
| 155 | bool cont = true;
|
---|
| 156 | while (cont) {
|
---|
| 157 | ipc_call_t call;
|
---|
| 158 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 159 |
|
---|
| 160 | switch (IPC_GET_METHOD(call)) {
|
---|
| 161 | case IPC_M_PHONE_HUNGUP:
|
---|
| 162 | cont = false;
|
---|
| 163 | continue;
|
---|
| 164 | case CLIPBOARD_PUT_DATA:
|
---|
| 165 | clip_put_data(callid, &call);
|
---|
| 166 | break;
|
---|
| 167 | case CLIPBOARD_GET_DATA:
|
---|
| 168 | clip_get_data(callid, &call);
|
---|
| 169 | break;
|
---|
| 170 | case CLIPBOARD_CONTENT:
|
---|
| 171 | clip_content(callid, &call);
|
---|
| 172 | break;
|
---|
| 173 | default:
|
---|
| 174 | if (!(callid & IPC_CALLID_NOTIFICATION))
|
---|
| 175 | ipc_answer_0(callid, ENOENT);
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | int main(int argc, char *argv[])
|
---|
| 181 | {
|
---|
| 182 | printf(NAME ": HelenOS clipboard service\n");
|
---|
| 183 |
|
---|
| 184 | async_set_client_connection(clip_connection);
|
---|
| 185 |
|
---|
| 186 | ipcarg_t phonead;
|
---|
| 187 | if (ipc_connect_to_me(PHONE_NS, SERVICE_CLIPBOARD, 0, 0, &phonead) != 0)
|
---|
| 188 | return -1;
|
---|
| 189 |
|
---|
| 190 | printf(NAME ": Accepting connections\n");
|
---|
| 191 | async_manager();
|
---|
| 192 |
|
---|
| 193 | /* Never reached */
|
---|
| 194 | return 0;
|
---|
| 195 | }
|
---|