source: mainline/uspace/srv/clipboard/clipboard.c@ 36f0738

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 36f0738 was 25a179e, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

IPC return values are always errno constants. Adjust types to reflect that.

In principle, IPC server is not allowed to return non-errno values via
the "main" return value, because kernel interprets it (e.g. EHANGUP).
It's still possible to return arbitrary additional return values alongside EOK,
which are not interpreted in normal communication.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
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);
47static service_id_t svc_id;
48
49static void clip_put_data(ipc_callid_t rid, ipc_call_t *request)
50{
51 char *data;
52 int rc;
53 size_t size;
54
55 switch (IPC_GET_ARG1(*request)) {
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(rid, 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(rid, 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(rid, EOK);
87 break;
88 default:
89 async_answer_0(rid, EINVAL);
90 }
91}
92
93static void clip_get_data(ipc_callid_t rid, ipc_call_t *request)
94{
95 fibril_mutex_lock(&clip_mtx);
96
97 ipc_callid_t callid;
98 size_t size;
99
100 /* Check for clipboard data tag compatibility */
101 switch (IPC_GET_ARG1(*request)) {
102 case CLIPBOARD_TAG_DATA:
103 if (!async_data_read_receive(&callid, &size)) {
104 async_answer_0(callid, EINVAL);
105 async_answer_0(rid, EINVAL);
106 break;
107 }
108
109 if (clip_tag != CLIPBOARD_TAG_DATA) {
110 /* So far we only understand binary data */
111 async_answer_0(callid, EOVERFLOW);
112 async_answer_0(rid, EOVERFLOW);
113 break;
114 }
115
116 if (clip_size != size) {
117 /* The client expects different size of data */
118 async_answer_0(callid, EOVERFLOW);
119 async_answer_0(rid, EOVERFLOW);
120 break;
121 }
122
123 int retval = async_data_read_finalize(callid, clip_data, size);
124 if (retval != EOK) {
125 async_answer_0(rid, retval);
126 break;
127 }
128
129 async_answer_0(rid, 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(rid, EINVAL);
137 break;
138 }
139
140 fibril_mutex_unlock(&clip_mtx);
141}
142
143static void clip_content(ipc_callid_t rid, ipc_call_t *request)
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(rid, EOK, (sysarg_t) size, (sysarg_t) tag);
152}
153
154static void clip_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
155{
156 /* Accept connection */
157 async_answer_0(iid, EOK);
158
159 while (true) {
160 ipc_call_t call;
161 ipc_callid_t callid = 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(callid, &call);
169 break;
170 case CLIPBOARD_GET_DATA:
171 clip_get_data(callid, &call);
172 break;
173 case CLIPBOARD_CONTENT:
174 clip_content(callid, &call);
175 break;
176 default:
177 async_answer_0(callid, ENOENT);
178 }
179 }
180}
181
182int main(int argc, char *argv[])
183{
184 int 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}
Note: See TracBrowser for help on using the repository browser.