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

Last change on this file was ca48672, checked in by Jiri Svoboda <jiri@…>, 5 weeks ago

loc_service_register() needs to take a port ID argument.

  • Property mode set to 100644
File size: 5.1 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2009 Martin Decky
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <async.h>
31#include <errno.h>
32#include <str_error.h>
33#include <fibril_synch.h>
34#include <ipc/services.h>
35#include <ipc/clipboard.h>
36#include <loc.h>
37#include <stdio.h>
38#include <stdbool.h>
39#include <stdlib.h>
40#include <task.h>
41
42#define NAME "clipboard"
43
44static char *clip_data = NULL;
45static size_t clip_size = 0;
46static clipboard_tag_t clip_tag = CLIPBOARD_TAG_NONE;
47static FIBRIL_MUTEX_INITIALIZE(clip_mtx);
48static service_id_t svc_id;
49
50static void clip_put_data(ipc_call_t *req)
51{
52 char *data;
53 errno_t rc;
54 size_t size;
55
56 switch (ipc_get_arg1(req)) {
57 case CLIPBOARD_TAG_NONE:
58 fibril_mutex_lock(&clip_mtx);
59
60 if (clip_data)
61 free(clip_data);
62
63 clip_data = NULL;
64 clip_size = 0;
65 clip_tag = CLIPBOARD_TAG_NONE;
66
67 fibril_mutex_unlock(&clip_mtx);
68 async_answer_0(req, EOK);
69 break;
70 case CLIPBOARD_TAG_DATA:
71 rc = async_data_write_accept((void **) &data, false, 0, 0, 0, &size);
72 if (rc != EOK) {
73 async_answer_0(req, rc);
74 break;
75 }
76
77 fibril_mutex_lock(&clip_mtx);
78
79 if (clip_data)
80 free(clip_data);
81
82 clip_data = data;
83 clip_size = size;
84 clip_tag = CLIPBOARD_TAG_DATA;
85
86 fibril_mutex_unlock(&clip_mtx);
87 async_answer_0(req, EOK);
88 break;
89 default:
90 async_answer_0(req, EINVAL);
91 }
92}
93
94static void clip_get_data(ipc_call_t *req)
95{
96 fibril_mutex_lock(&clip_mtx);
97
98 ipc_call_t call;
99 size_t size;
100
101 /* Check for clipboard data tag compatibility */
102 switch (ipc_get_arg1(req)) {
103 case CLIPBOARD_TAG_DATA:
104 if (!async_data_read_receive(&call, &size)) {
105 async_answer_0(&call, EINVAL);
106 async_answer_0(req, EINVAL);
107 break;
108 }
109
110 if (clip_tag != CLIPBOARD_TAG_DATA) {
111 /* So far we only understand binary data */
112 async_answer_0(&call, EOVERFLOW);
113 async_answer_0(req, EOVERFLOW);
114 break;
115 }
116
117 if (clip_size != size) {
118 /* The client expects different size of data */
119 async_answer_0(&call, EOVERFLOW);
120 async_answer_0(req, EOVERFLOW);
121 break;
122 }
123
124 errno_t retval = async_data_read_finalize(&call, clip_data, size);
125 if (retval != EOK) {
126 async_answer_0(req, retval);
127 break;
128 }
129
130 async_answer_0(req, EOK);
131 break;
132 default:
133 /*
134 * Sorry, we don't know how to get unknown or NONE
135 * data from the clipbard
136 */
137 async_answer_0(req, EINVAL);
138 break;
139 }
140
141 fibril_mutex_unlock(&clip_mtx);
142}
143
144static void clip_content(ipc_call_t *req)
145{
146 fibril_mutex_lock(&clip_mtx);
147
148 size_t size = clip_size;
149 clipboard_tag_t tag = clip_tag;
150
151 fibril_mutex_unlock(&clip_mtx);
152 async_answer_2(req, EOK, (sysarg_t) size, (sysarg_t) tag);
153}
154
155static void clip_connection(ipc_call_t *icall, void *arg)
156{
157 /* Accept connection */
158 async_accept_0(icall);
159
160 while (true) {
161 ipc_call_t call;
162 async_get_call(&call);
163
164 if (!ipc_get_imethod(&call)) {
165 async_answer_0(&call, EOK);
166 break;
167 }
168
169 switch (ipc_get_imethod(&call)) {
170 case CLIPBOARD_PUT_DATA:
171 clip_put_data(&call);
172 break;
173 case CLIPBOARD_GET_DATA:
174 clip_get_data(&call);
175 break;
176 case CLIPBOARD_CONTENT:
177 clip_content(&call);
178 break;
179 default:
180 async_answer_0(&call, ENOENT);
181 }
182 }
183}
184
185int main(int argc, char *argv[])
186{
187 errno_t rc;
188 loc_srv_t *srv;
189
190 printf("%s: HelenOS clipboard service\n", NAME);
191 async_set_fallback_port_handler(clip_connection, NULL);
192
193 rc = loc_server_register(NAME, &srv);
194 if (rc != EOK) {
195 printf("%s: Failed registering server: %s\n", NAME, str_error(rc));
196 return rc;
197 }
198
199 rc = loc_service_register(srv, SERVICE_NAME_CLIPBOARD,
200 fallback_port_id, &svc_id);
201 if (rc != EOK) {
202 loc_server_unregister(srv);
203
204 printf("%s: Failed registering service : %s\n", NAME, str_error(rc));
205 return rc;
206 }
207
208 printf("%s: Accepting connections\n", NAME);
209 task_retval(0);
210 async_manager();
211
212 /* Never reached */
213 return 0;
214}
Note: See TracBrowser for help on using the repository browser.