source: mainline/uspace/lib/c/generic/clipboard.c@ 764d71e

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