source: mainline/uspace/lib/libc/generic/clipboard.c@ 1e4cada

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1e4cada was fb623e2, checked in by Martin Decky <martin@…>, 16 years ago

move from file-backed clipboard to service-backed clipboard
there is a possibility to do some type checking/conversion of the data stored in the clipboard (not used so far)

  • Property mode set to 100644
File size: 4.5 KB
Line 
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
33 * @brief System clipboard API.
34 *
35 * The clipboard data is managed by the clipboard service and it is shared by
36 * the entire system.
37 *
38 */
39
40#include <clipboard.h>
41#include <ipc/services.h>
42#include <ipc/clipboard.h>
43#include <async.h>
44#include <string.h>
45#include <errno.h>
46#include <malloc.h>
47
48static int clip_phone = -1;
49
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}
58
59/** Copy string to clipboard.
60 *
61 * Sets the clipboard contents to @a str. Passing an empty string or NULL
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.
67 *
68 */
69int clipboard_put_str(const char *str)
70{
71 size_t size = str_size(str);
72
73 if (size == 0) {
74 async_serialize_start();
75 clip_connect();
76
77 ipcarg_t rc = async_req_1_0(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_NONE);
78
79 async_serialize_end();
80
81 return (int) rc;
82 } else {
83 async_serialize_start();
84 clip_connect();
85
86 aid_t req = async_send_1(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_BLOB, NULL);
87 ipcarg_t rc = async_data_write_start(clip_phone, (void *) str, size);
88 if (rc != EOK) {
89 ipcarg_t rc_orig;
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;
96 }
97
98 async_wait_for(req, &rc);
99 async_serialize_end();
100
101 return (int) rc;
102 }
103}
104
105/** Get a copy of clipboard contents.
106 *
107 * Returns a new string that can be deallocated with free().
108 *
109 * @param str Here pointer to the newly allocated string is stored.
110 *
111 * @return Zero on success or negative error code.
112 *
113 */
114int clipboard_get_str(char **str)
115{
116 /* Loop until clipboard read succesful */
117 while (true) {
118 async_serialize_start();
119 clip_connect();
120
121 ipcarg_t size;
122 ipcarg_t tag;
123 ipcarg_t rc = async_req_0_2(clip_phone, CLIPBOARD_CONTENT, &size, &tag);
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;
141 case CLIPBOARD_TAG_BLOB:
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);
150 if (rc == EOVERFLOW) {
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) {
160 ipcarg_t rc_orig;
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;
180 }
181 }
182}
183
184/** @}
185 */
Note: See TracBrowser for help on using the repository browser.