source: mainline/uspace/lib/c/generic/clipboard.c@ 7907cf9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7907cf9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

  • Property mode set to 100644
File size: 4.5 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 <clipboard.h>
41#include <ipc/services.h>
42#include <ipc/clipboard.h>
43#include <async.h>
[19f857a]44#include <str.h>
[0902edfe]45#include <errno.h>
[fb623e2]46#include <malloc.h>
[0b772f5]47
[fb623e2]48static int clip_phone = -1;
[0902edfe]49
[fb623e2]50/** Connect to clipboard server
51 *
52 */
53static void clip_connect(void)
54{
55 while (clip_phone < 0)
56 clip_phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_CLIPBOARD, 0, 0);
57}
[0902edfe]58
59/** Copy string to clipboard.
60 *
61 * Sets the clipboard contents to @a str. Passing an empty string or NULL
[fb623e2]62 * makes the clipboard empty.
63 *
64 * @param str String to put to clipboard or NULL.
65 *
66 * @return Zero on success or negative error code.
[0902edfe]67 *
68 */
69int clipboard_put_str(const char *str)
70{
[fb623e2]71 size_t size = str_size(str);
72
73 if (size == 0) {
74 async_serialize_start();
75 clip_connect();
76
[96b02eb9]77 sysarg_t rc = async_req_1_0(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_NONE);
[fb623e2]78
79 async_serialize_end();
80
81 return (int) rc;
82 } else {
83 async_serialize_start();
84 clip_connect();
85
[472c09d]86 aid_t req = async_send_1(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_DATA, NULL);
[96b02eb9]87 sysarg_t rc = async_data_write_start(clip_phone, (void *) str, size);
[fb623e2]88 if (rc != EOK) {
[96b02eb9]89 sysarg_t rc_orig;
[fb623e2]90 async_wait_for(req, &rc_orig);
91 async_serialize_end();
92 if (rc_orig == EOK)
93 return (int) rc;
94 else
95 return (int) rc_orig;
[0b772f5]96 }
[fb623e2]97
98 async_wait_for(req, &rc);
99 async_serialize_end();
100
101 return (int) rc;
[0b772f5]102 }
[0902edfe]103}
104
[0b772f5]105/** Get a copy of clipboard contents.
106 *
107 * Returns a new string that can be deallocated with free().
108 *
[fb623e2]109 * @param str Here pointer to the newly allocated string is stored.
110 *
111 * @return Zero on success or negative error code.
112 *
[0b772f5]113 */
[0902edfe]114int clipboard_get_str(char **str)
115{
[fb623e2]116 /* Loop until clipboard read succesful */
117 while (true) {
118 async_serialize_start();
119 clip_connect();
120
[96b02eb9]121 sysarg_t size;
122 sysarg_t tag;
123 sysarg_t rc = async_req_0_2(clip_phone, CLIPBOARD_CONTENT, &size, &tag);
[fb623e2]124
125 async_serialize_end();
126
127 if (rc != EOK)
128 return (int) rc;
129
130 char *sbuf;
131
132 switch (tag) {
133 case CLIPBOARD_TAG_NONE:
134 sbuf = malloc(1);
135 if (sbuf == NULL)
136 return ENOMEM;
137
138 sbuf[0] = 0;
139 *str = sbuf;
140 return EOK;
[472c09d]141 case CLIPBOARD_TAG_DATA:
[fb623e2]142 sbuf = malloc(size + 1);
143 if (sbuf == NULL)
144 return ENOMEM;
145
146 async_serialize_start();
147
148 aid_t req = async_send_1(clip_phone, CLIPBOARD_GET_DATA, tag, NULL);
149 rc = async_data_read_start(clip_phone, (void *) sbuf, size);
[36e9cd1]150 if ((int) rc == EOVERFLOW) {
[fb623e2]151 /*
152 * The data in the clipboard has changed since
153 * the last call of CLIPBOARD_CONTENT
154 */
155 async_serialize_end();
156 break;
157 }
158
159 if (rc != EOK) {
[96b02eb9]160 sysarg_t rc_orig;
[fb623e2]161 async_wait_for(req, &rc_orig);
162 async_serialize_end();
163 if (rc_orig == EOK)
164 return (int) rc;
165 else
166 return (int) rc_orig;
167 }
168
169 async_wait_for(req, &rc);
170 async_serialize_end();
171
172 if (rc == EOK) {
173 sbuf[size] = 0;
174 *str = sbuf;
175 }
176
177 return rc;
178 default:
179 return EINVAL;
[0b772f5]180 }
181 }
[0902edfe]182}
183
184/** @}
185 */
Note: See TracBrowser for help on using the repository browser.