source: mainline/uspace/srv/clipboard/clipboard.c@ ad0d1f5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ad0d1f5 was 77f0a1d, checked in by Jakub Jermar <jakub@…>, 7 years ago

Get rid of rid in favor of req_handle

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