source: mainline/uspace/lib/c/generic/clipboard.c@ 3d95c9d

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

Fix comments to stop referring to error codes as negative.

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[0902edfe]1/*
2 * Copyright (c) 2009 Jiri Svoboda
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/** @addtogroup libc
30 * @{
31 */
32/** @file
[0b772f5]33 * @brief System clipboard API.
34 *
[fb623e2]35 * The clipboard data is managed by the clipboard service and it is shared by
36 * the entire system.
[0b772f5]37 *
[0902edfe]38 */
39
[fb623e2]40#include <async.h>
[8d6bcc8c]41#include <clipboard.h>
[0902edfe]42#include <errno.h>
[8d6bcc8c]43#include <fibril_synch.h>
44#include <ipc/clipboard.h>
45#include <ipc/services.h>
46#include <loc.h>
[38d150e]47#include <stdlib.h>
[8d6bcc8c]48#include <str.h>
[0b772f5]49
[79ae36dd]50static FIBRIL_MUTEX_INITIALIZE(clip_mutex);
51static async_sess_t *clip_sess = NULL;
[0902edfe]52
[79ae36dd]53/** Start an async exchange on the clipboard session.
54 *
55 * @return New exchange.
56 *
57 */
58static async_exch_t *clip_exchange_begin(void)
59{
[8d6bcc8c]60 service_id_t sid;
61 int rc;
62
[79ae36dd]63 fibril_mutex_lock(&clip_mutex);
64
[8d6bcc8c]65 while (clip_sess == NULL) {
66 rc = loc_service_get_id(SERVICE_NAME_CLIPBOARD, &sid,
67 IPC_FLAG_BLOCKING);
68 if (rc != EOK)
69 continue;
70
71 clip_sess = loc_service_connect(sid, INTERFACE_CLIPBOARD,
72 IPC_FLAG_BLOCKING);
73 }
[79ae36dd]74
75 fibril_mutex_unlock(&clip_mutex);
76
77 return async_exchange_begin(clip_sess);
78}
79
80/** Finish an async exchange on the clipboard session.
81 *
82 * @param exch Exchange to be finished.
[fb623e2]83 *
84 */
[79ae36dd]85static void clip_exchange_end(async_exch_t *exch)
[fb623e2]86{
[79ae36dd]87 async_exchange_end(exch);
[fb623e2]88}
[0902edfe]89
90/** Copy string to clipboard.
91 *
92 * Sets the clipboard contents to @a str. Passing an empty string or NULL
[fb623e2]93 * makes the clipboard empty.
94 *
95 * @param str String to put to clipboard or NULL.
96 *
[cde999a]97 * @return Zero on success or an error code.
[0902edfe]98 *
99 */
100int clipboard_put_str(const char *str)
101{
[fb623e2]102 size_t size = str_size(str);
103
104 if (size == 0) {
[79ae36dd]105 async_exch_t *exch = clip_exchange_begin();
[25a179e]106 int rc = async_req_1_0(exch, CLIPBOARD_PUT_DATA,
[79ae36dd]107 CLIPBOARD_TAG_NONE);
108 clip_exchange_end(exch);
[fb623e2]109
110 return (int) rc;
111 } else {
[79ae36dd]112 async_exch_t *exch = clip_exchange_begin();
113 aid_t req = async_send_1(exch, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_DATA,
114 NULL);
[25a179e]115 int rc = async_data_write_start(exch, (void *) str, size);
[79ae36dd]116 clip_exchange_end(exch);
[fb623e2]117
118 if (rc != EOK) {
[25a179e]119 int rc_orig;
[fb623e2]120 async_wait_for(req, &rc_orig);
121 if (rc_orig == EOK)
122 return (int) rc;
123 else
124 return (int) rc_orig;
[0b772f5]125 }
[fb623e2]126
127 async_wait_for(req, &rc);
128
129 return (int) rc;
[0b772f5]130 }
[0902edfe]131}
132
[0b772f5]133/** Get a copy of clipboard contents.
134 *
135 * Returns a new string that can be deallocated with free().
136 *
[fb623e2]137 * @param str Here pointer to the newly allocated string is stored.
138 *
[cde999a]139 * @return Zero on success or an error code.
[fb623e2]140 *
[0b772f5]141 */
[0902edfe]142int clipboard_get_str(char **str)
143{
[fb623e2]144 /* Loop until clipboard read succesful */
145 while (true) {
[79ae36dd]146 async_exch_t *exch = clip_exchange_begin();
[fb623e2]147
[96b02eb9]148 sysarg_t size;
149 sysarg_t tag;
[25a179e]150 int rc = async_req_0_2(exch, CLIPBOARD_CONTENT, &size, &tag);
[fb623e2]151
[79ae36dd]152 clip_exchange_end(exch);
[fb623e2]153
154 if (rc != EOK)
155 return (int) rc;
156
157 char *sbuf;
158
159 switch (tag) {
160 case CLIPBOARD_TAG_NONE:
161 sbuf = malloc(1);
162 if (sbuf == NULL)
163 return ENOMEM;
164
165 sbuf[0] = 0;
166 *str = sbuf;
167 return EOK;
[472c09d]168 case CLIPBOARD_TAG_DATA:
[fb623e2]169 sbuf = malloc(size + 1);
170 if (sbuf == NULL)
171 return ENOMEM;
172
[79ae36dd]173 exch = clip_exchange_begin();
174 aid_t req = async_send_1(exch, CLIPBOARD_GET_DATA, tag, NULL);
175 rc = async_data_read_start(exch, (void *) sbuf, size);
176 clip_exchange_end(exch);
[fb623e2]177
[36e9cd1]178 if ((int) rc == EOVERFLOW) {
[fb623e2]179 /*
180 * The data in the clipboard has changed since
181 * the last call of CLIPBOARD_CONTENT
182 */
183 break;
184 }
185
186 if (rc != EOK) {
[25a179e]187 int rc_orig;
[fb623e2]188 async_wait_for(req, &rc_orig);
189 if (rc_orig == EOK)
190 return (int) rc;
191 else
192 return (int) rc_orig;
193 }
194
195 async_wait_for(req, &rc);
196
197 if (rc == EOK) {
198 sbuf[size] = 0;
199 *str = sbuf;
200 }
201
202 return rc;
203 default:
204 return EINVAL;
[0b772f5]205 }
206 }
[0902edfe]207}
208
209/** @}
210 */
Note: See TracBrowser for help on using the repository browser.