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

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

Mechanically lowercase IPC_SET_*/IPC_GET_*

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