source: mainline/uspace/lib/c/generic/clipboard.c@ 0c322fa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0c322fa was 79ae36dd, checked in by Martin Decky <martin@…>, 14 years ago

new async framework with integrated exchange tracking

  • strict isolation between low-level IPC and high-level async framework with integrated exchange tracking
    • each IPC connection is represented by an async_sess_t structure
    • each IPC exchange is represented by an async_exch_t structure
    • exchange management is either based on atomic messages (EXCHANGE_ATOMIC), locking (EXCHANGE_SERIALIZE) or connection cloning (EXCHANGE_CLONE)
  • async_obsolete: temporary compatibility layer to keep old async clients working (several pieces of code are currently broken, but only non-essential functionality)
  • IPC_M_PHONE_HANGUP is now method no. 0 (for elegant boolean evaluation)
  • IPC_M_DEBUG_ALL has been renamed to IPC_M_DEBUG
  • IPC_M_PING has been removed (VFS protocol now has VFS_IN_PING)
  • console routines in libc have been rewritten for better abstraction
  • additional use for libc-private header files (FILE structure opaque to the client)
  • various cstyle changes (typos, indentation, missing externs in header files, improved comments, etc.)
  • Property mode set to 100644
File size: 4.8 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>
[79ae36dd]41#include <ns.h>
[fb623e2]42#include <ipc/services.h>
43#include <ipc/clipboard.h>
[79ae36dd]44#include <fibril_synch.h>
[fb623e2]45#include <async.h>
[19f857a]46#include <str.h>
[0902edfe]47#include <errno.h>
[fb623e2]48#include <malloc.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{
60 fibril_mutex_lock(&clip_mutex);
61
62 while (clip_sess == NULL)
63 clip_sess = service_connect_blocking(EXCHANGE_SERIALIZE,
64 SERVICE_CLIPBOARD, 0, 0);
65
66 fibril_mutex_unlock(&clip_mutex);
67
68 return async_exchange_begin(clip_sess);
69}
70
71/** Finish an async exchange on the clipboard session.
72 *
73 * @param exch Exchange to be finished.
[fb623e2]74 *
75 */
[79ae36dd]76static void clip_exchange_end(async_exch_t *exch)
[fb623e2]77{
[79ae36dd]78 async_exchange_end(exch);
[fb623e2]79}
[0902edfe]80
81/** Copy string to clipboard.
82 *
83 * Sets the clipboard contents to @a str. Passing an empty string or NULL
[fb623e2]84 * makes the clipboard empty.
85 *
86 * @param str String to put to clipboard or NULL.
87 *
88 * @return Zero on success or negative error code.
[0902edfe]89 *
90 */
91int clipboard_put_str(const char *str)
92{
[fb623e2]93 size_t size = str_size(str);
94
95 if (size == 0) {
[79ae36dd]96 async_exch_t *exch = clip_exchange_begin();
97 sysarg_t rc = async_req_1_0(exch, CLIPBOARD_PUT_DATA,
98 CLIPBOARD_TAG_NONE);
99 clip_exchange_end(exch);
[fb623e2]100
101 return (int) rc;
102 } else {
[79ae36dd]103 async_exch_t *exch = clip_exchange_begin();
104 aid_t req = async_send_1(exch, CLIPBOARD_PUT_DATA, CLIPBOARD_TAG_DATA,
105 NULL);
106 sysarg_t rc = async_data_write_start(exch, (void *) str, size);
107 clip_exchange_end(exch);
[fb623e2]108
109 if (rc != EOK) {
[96b02eb9]110 sysarg_t rc_orig;
[fb623e2]111 async_wait_for(req, &rc_orig);
112 if (rc_orig == EOK)
113 return (int) rc;
114 else
115 return (int) rc_orig;
[0b772f5]116 }
[fb623e2]117
118 async_wait_for(req, &rc);
119
120 return (int) rc;
[0b772f5]121 }
[0902edfe]122}
123
[0b772f5]124/** Get a copy of clipboard contents.
125 *
126 * Returns a new string that can be deallocated with free().
127 *
[fb623e2]128 * @param str Here pointer to the newly allocated string is stored.
129 *
130 * @return Zero on success or negative error code.
131 *
[0b772f5]132 */
[0902edfe]133int clipboard_get_str(char **str)
134{
[fb623e2]135 /* Loop until clipboard read succesful */
136 while (true) {
[79ae36dd]137 async_exch_t *exch = clip_exchange_begin();
[fb623e2]138
[96b02eb9]139 sysarg_t size;
140 sysarg_t tag;
[79ae36dd]141 sysarg_t rc = async_req_0_2(exch, CLIPBOARD_CONTENT, &size, &tag);
[fb623e2]142
[79ae36dd]143 clip_exchange_end(exch);
[fb623e2]144
145 if (rc != EOK)
146 return (int) rc;
147
148 char *sbuf;
149
150 switch (tag) {
151 case CLIPBOARD_TAG_NONE:
152 sbuf = malloc(1);
153 if (sbuf == NULL)
154 return ENOMEM;
155
156 sbuf[0] = 0;
157 *str = sbuf;
158 return EOK;
[472c09d]159 case CLIPBOARD_TAG_DATA:
[fb623e2]160 sbuf = malloc(size + 1);
161 if (sbuf == NULL)
162 return ENOMEM;
163
[79ae36dd]164 exch = clip_exchange_begin();
165 aid_t req = async_send_1(exch, CLIPBOARD_GET_DATA, tag, NULL);
166 rc = async_data_read_start(exch, (void *) sbuf, size);
167 clip_exchange_end(exch);
[fb623e2]168
[36e9cd1]169 if ((int) rc == EOVERFLOW) {
[fb623e2]170 /*
171 * The data in the clipboard has changed since
172 * the last call of CLIPBOARD_CONTENT
173 */
174 break;
175 }
176
177 if (rc != EOK) {
[96b02eb9]178 sysarg_t rc_orig;
[fb623e2]179 async_wait_for(req, &rc_orig);
180 if (rc_orig == EOK)
181 return (int) rc;
182 else
183 return (int) rc_orig;
184 }
185
186 async_wait_for(req, &rc);
187
188 if (rc == EOK) {
189 sbuf[size] = 0;
190 *str = sbuf;
191 }
192
193 return rc;
194 default:
195 return EINVAL;
[0b772f5]196 }
197 }
[0902edfe]198}
199
200/** @}
201 */
Note: See TracBrowser for help on using the repository browser.