source: mainline/uspace/lib/c/generic/clipboard.c@ bcd7775

Last change on this file since bcd7775 was 7fa8589, checked in by Matthieu Riolo <matthieu.riolo@…>, 5 years ago

Removing unneeded casts from errno_t to errno_t

  • Property mode set to 100644
File size: 4.9 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;
[b7fd2a0]61 errno_t rc;
[a35b458]62
[79ae36dd]63 fibril_mutex_lock(&clip_mutex);
[a35b458]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;
[a35b458]70
[8d6bcc8c]71 clip_sess = loc_service_connect(sid, INTERFACE_CLIPBOARD,
72 IPC_FLAG_BLOCKING);
73 }
[a35b458]74
[79ae36dd]75 fibril_mutex_unlock(&clip_mutex);
[a35b458]76
[79ae36dd]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 */
[b7fd2a0]100errno_t clipboard_put_str(const char *str)
[0902edfe]101{
[fb623e2]102 size_t size = str_size(str);
[a35b458]103
[fb623e2]104 if (size == 0) {
[79ae36dd]105 async_exch_t *exch = clip_exchange_begin();
[b7fd2a0]106 errno_t rc = async_req_1_0(exch, CLIPBOARD_PUT_DATA,
[79ae36dd]107 CLIPBOARD_TAG_NONE);
108 clip_exchange_end(exch);
[a35b458]109
[7fa8589]110 return rc;
[fb623e2]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);
[b7fd2a0]115 errno_t rc = async_data_write_start(exch, (void *) str, size);
[79ae36dd]116 clip_exchange_end(exch);
[a35b458]117
[fb623e2]118 if (rc != EOK) {
[b7fd2a0]119 errno_t rc_orig;
[fb623e2]120 async_wait_for(req, &rc_orig);
121 if (rc_orig == EOK)
[7fa8589]122 return rc;
[fb623e2]123 else
[7fa8589]124 return rc_orig;
[0b772f5]125 }
[a35b458]126
[fb623e2]127 async_wait_for(req, &rc);
[a35b458]128
[7fa8589]129 return 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 */
[b7fd2a0]142errno_t clipboard_get_str(char **str)
[0902edfe]143{
[fb623e2]144 /* Loop until clipboard read succesful */
145 while (true) {
[79ae36dd]146 async_exch_t *exch = clip_exchange_begin();
[a35b458]147
[96b02eb9]148 sysarg_t size;
149 sysarg_t tag;
[b7fd2a0]150 errno_t rc = async_req_0_2(exch, CLIPBOARD_CONTENT, &size, &tag);
[a35b458]151
[79ae36dd]152 clip_exchange_end(exch);
[a35b458]153
[fb623e2]154 if (rc != EOK)
[7fa8589]155 return rc;
[a35b458]156
[fb623e2]157 char *sbuf;
[a35b458]158
[fb623e2]159 switch (tag) {
160 case CLIPBOARD_TAG_NONE:
161 sbuf = malloc(1);
162 if (sbuf == NULL)
163 return ENOMEM;
[a35b458]164
[fb623e2]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;
[a35b458]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);
[a35b458]177
[7fa8589]178 if (rc == EOVERFLOW) {
[fb623e2]179 /*
180 * The data in the clipboard has changed since
181 * the last call of CLIPBOARD_CONTENT
182 */
183 break;
184 }
[a35b458]185
[fb623e2]186 if (rc != EOK) {
[b7fd2a0]187 errno_t rc_orig;
[fb623e2]188 async_wait_for(req, &rc_orig);
189 if (rc_orig == EOK)
[7fa8589]190 return rc;
[fb623e2]191 else
[7fa8589]192 return rc_orig;
[fb623e2]193 }
[a35b458]194
[fb623e2]195 async_wait_for(req, &rc);
[a35b458]196
[fb623e2]197 if (rc == EOK) {
198 sbuf[size] = 0;
199 *str = sbuf;
200 }
[a35b458]201
[fb623e2]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.