source: mainline/uspace/lib/c/generic/clipboard.c@ 21cb3ac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 21cb3ac 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
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>
[007e6efa]41#include <ipc/ns.h>
[fb623e2]42#include <ipc/services.h>
43#include <ipc/clipboard.h>
44#include <async.h>
[19f857a]45#include <str.h>
[0902edfe]46#include <errno.h>
[fb623e2]47#include <malloc.h>
[0b772f5]48
[fb623e2]49static int clip_phone = -1;
[0902edfe]50
[fb623e2]51/** Connect to clipboard server
52 *
53 */
54static void clip_connect(void)
55{
56 while (clip_phone < 0)
[007e6efa]57 clip_phone = service_connect_blocking(SERVICE_CLIPBOARD, 0, 0);
[fb623e2]58}
[0902edfe]59
60/** Copy string to clipboard.
61 *
62 * Sets the clipboard contents to @a str. Passing an empty string or NULL
[fb623e2]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.
[0902edfe]68 *
69 */
70int clipboard_put_str(const char *str)
71{
[fb623e2]72 size_t size = str_size(str);
73
74 if (size == 0) {
75 async_serialize_start();
76 clip_connect();
77
[96b02eb9]78 sysarg_t rc = async_req_1_0(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_NONE);
[fb623e2]79
80 async_serialize_end();
81
82 return (int) rc;
83 } else {
84 async_serialize_start();
85 clip_connect();
86
[472c09d]87 aid_t req = async_send_1(clip_phone, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_DATA, NULL);
[96b02eb9]88 sysarg_t rc = async_data_write_start(clip_phone, (void *) str, size);
[fb623e2]89 if (rc != EOK) {
[96b02eb9]90 sysarg_t rc_orig;
[fb623e2]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;
[0b772f5]97 }
[fb623e2]98
99 async_wait_for(req, &rc);
100 async_serialize_end();
101
102 return (int) rc;
[0b772f5]103 }
[0902edfe]104}
105
[0b772f5]106/** Get a copy of clipboard contents.
107 *
108 * Returns a new string that can be deallocated with free().
109 *
[fb623e2]110 * @param str Here pointer to the newly allocated string is stored.
111 *
112 * @return Zero on success or negative error code.
113 *
[0b772f5]114 */
[0902edfe]115int clipboard_get_str(char **str)
116{
[fb623e2]117 /* Loop until clipboard read succesful */
118 while (true) {
119 async_serialize_start();
120 clip_connect();
121
[96b02eb9]122 sysarg_t size;
123 sysarg_t tag;
124 sysarg_t rc = async_req_0_2(clip_phone, CLIPBOARD_CONTENT, &size, &tag);
[fb623e2]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;
[472c09d]142 case CLIPBOARD_TAG_DATA:
[fb623e2]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);
[36e9cd1]151 if ((int) rc == EOVERFLOW) {
[fb623e2]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) {
[96b02eb9]161 sysarg_t rc_orig;
[fb623e2]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;
[0b772f5]181 }
182 }
[0902edfe]183}
184
185/** @}
186 */
Note: See TracBrowser for help on using the repository browser.