source: mainline/uspace/srv/clipboard/clipboard.c@ 516d361

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

Add str_error() in numerous places.

  • Property mode set to 100644
File size: 5.1 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
49static void clip_put_data(ipc_callid_t rid, ipc_call_t *request)
50{
[531695f]51 char *data;
52 int rc;
[fb623e2]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);
[ffa2c8ef]67 async_answer_0(rid, 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) {
[ffa2c8ef]72 async_answer_0(rid, rc);
[fb623e2]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;
[472c09d]83 clip_tag = CLIPBOARD_TAG_DATA;
[fb623e2]84
85 fibril_mutex_unlock(&clip_mtx);
[ffa2c8ef]86 async_answer_0(rid, EOK);
[fb623e2]87 break;
88 default:
[ffa2c8ef]89 async_answer_0(rid, EINVAL);
[fb623e2]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)) {
[472c09d]102 case CLIPBOARD_TAG_DATA:
[fb623e2]103 if (!async_data_read_receive(&callid, &size)) {
[ffa2c8ef]104 async_answer_0(callid, EINVAL);
105 async_answer_0(rid, EINVAL);
[fb623e2]106 break;
107 }
108
[472c09d]109 if (clip_tag != CLIPBOARD_TAG_DATA) {
110 /* So far we only understand binary data */
[ffa2c8ef]111 async_answer_0(callid, EOVERFLOW);
112 async_answer_0(rid, EOVERFLOW);
[fb623e2]113 break;
114 }
115
116 if (clip_size != size) {
117 /* The client expects different size of data */
[ffa2c8ef]118 async_answer_0(callid, EOVERFLOW);
119 async_answer_0(rid, EOVERFLOW);
[fb623e2]120 break;
121 }
122
[96b02eb9]123 sysarg_t retval = async_data_read_finalize(callid, clip_data, size);
[fb623e2]124 if (retval != EOK) {
[ffa2c8ef]125 async_answer_0(rid, retval);
[fb623e2]126 break;
127 }
128
[ffa2c8ef]129 async_answer_0(rid, 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 */
[ffa2c8ef]136 async_answer_0(rid, EINVAL);
[fb623e2]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);
[ffa2c8ef]151 async_answer_2(rid, EOK, (sysarg_t) size, (sysarg_t) tag);
[fb623e2]152}
153
[9934f7d]154static void clip_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
[fb623e2]155{
156 /* Accept connection */
[ffa2c8ef]157 async_answer_0(iid, EOK);
[fb623e2]158
[79ae36dd]159 while (true) {
[fb623e2]160 ipc_call_t call;
161 ipc_callid_t callid = async_get_call(&call);
162
[79ae36dd]163 if (!IPC_GET_IMETHOD(call))
164 break;
165
[228e490]166 switch (IPC_GET_IMETHOD(call)) {
[fb623e2]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:
[ffa2c8ef]177 async_answer_0(callid, ENOENT);
[fb623e2]178 }
179 }
180}
181
182int main(int argc, char *argv[])
183{
[8d6bcc8c]184 int rc;
[fb623e2]185
[8d6bcc8c]186 printf("%s: HelenOS clipboard service\n", NAME);
[b688fd8]187 async_set_fallback_port_handler(clip_connection, NULL);
[8d6bcc8c]188
189 rc = loc_server_register(NAME);
190 if (rc != EOK) {
[c1694b6b]191 printf("%s: Failed registering server: %s\n", NAME, str_error(rc));
[a47f522]192 return rc;
[8d6bcc8c]193 }
194
195 rc = loc_service_register(SERVICE_NAME_CLIPBOARD, &svc_id);
196 if (rc != EOK) {
[c1694b6b]197 printf("%s: Failed registering service : %s\n", NAME, str_error(rc));
[8d6bcc8c]198 return rc;
199 }
[fb623e2]200
[79ae36dd]201 printf("%s: Accepting connections\n", NAME);
202 task_retval(0);
[fb623e2]203 async_manager();
204
205 /* Never reached */
206 return 0;
207}
Note: See TracBrowser for help on using the repository browser.