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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8119363 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
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(cap_call_handle_t req_handle, ipc_call_t *request)
50{
51 char *data;
52 errno_t 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(req_handle, 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_handle, 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_handle, EOK);
87 break;
88 default:
89 async_answer_0(req_handle, EINVAL);
90 }
91}
92
93static void clip_get_data(cap_call_handle_t req_handle, ipc_call_t *request)
94{
95 fibril_mutex_lock(&clip_mtx);
96
97 cap_call_handle_t chandle;
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(&chandle, &size)) {
104 async_answer_0(chandle, EINVAL);
105 async_answer_0(req_handle, EINVAL);
106 break;
107 }
108
109 if (clip_tag != CLIPBOARD_TAG_DATA) {
110 /* So far we only understand binary data */
111 async_answer_0(chandle, EOVERFLOW);
112 async_answer_0(req_handle, EOVERFLOW);
113 break;
114 }
115
116 if (clip_size != size) {
117 /* The client expects different size of data */
118 async_answer_0(chandle, EOVERFLOW);
119 async_answer_0(req_handle, EOVERFLOW);
120 break;
121 }
122
123 errno_t retval = async_data_read_finalize(chandle, clip_data, size);
124 if (retval != EOK) {
125 async_answer_0(req_handle, retval);
126 break;
127 }
128
129 async_answer_0(req_handle, 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_handle, EINVAL);
137 break;
138 }
139
140 fibril_mutex_unlock(&clip_mtx);
141}
142
143static void clip_content(cap_call_handle_t req_handle, 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(req_handle, EOK, (sysarg_t) size, (sysarg_t) tag);
152}
153
154static void clip_connection(cap_call_handle_t icall_handle, ipc_call_t *icall,
155 void *arg)
156{
157 /* Accept connection */
158 async_answer_0(icall_handle, EOK);
159
160 while (true) {
161 ipc_call_t call;
162 cap_call_handle_t chandle = async_get_call(&call);
163
164 if (!IPC_GET_IMETHOD(call))
165 break;
166
167 switch (IPC_GET_IMETHOD(call)) {
168 case CLIPBOARD_PUT_DATA:
169 clip_put_data(chandle, &call);
170 break;
171 case CLIPBOARD_GET_DATA:
172 clip_get_data(chandle, &call);
173 break;
174 case CLIPBOARD_CONTENT:
175 clip_content(chandle, &call);
176 break;
177 default:
178 async_answer_0(chandle, ENOENT);
179 }
180 }
181}
182
183int main(int argc, char *argv[])
184{
185 errno_t rc;
186
187 printf("%s: HelenOS clipboard service\n", NAME);
188 async_set_fallback_port_handler(clip_connection, NULL);
189
190 rc = loc_server_register(NAME);
191 if (rc != EOK) {
192 printf("%s: Failed registering server: %s\n", NAME, str_error(rc));
193 return rc;
194 }
195
196 rc = loc_service_register(SERVICE_NAME_CLIPBOARD, &svc_id);
197 if (rc != EOK) {
198 printf("%s: Failed registering service : %s\n", NAME, str_error(rc));
199 return rc;
200 }
201
202 printf("%s: Accepting connections\n", NAME);
203 task_retval(0);
204 async_manager();
205
206 /* Never reached */
207 return 0;
208}
Note: See TracBrowser for help on using the repository browser.