| 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 <ns.h>
|
|---|
| 42 | #include <ipc/services.h>
|
|---|
| 43 | #include <ipc/clipboard.h>
|
|---|
| 44 | #include <fibril_synch.h>
|
|---|
| 45 | #include <async.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <errno.h>
|
|---|
| 48 | #include <malloc.h>
|
|---|
| 49 |
|
|---|
| 50 | static FIBRIL_MUTEX_INITIALIZE(clip_mutex);
|
|---|
| 51 | static async_sess_t *clip_sess = NULL;
|
|---|
| 52 |
|
|---|
| 53 | /** Start an async exchange on the clipboard session.
|
|---|
| 54 | *
|
|---|
| 55 | * @return New exchange.
|
|---|
| 56 | *
|
|---|
| 57 | */
|
|---|
| 58 | static 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(SERVICE_CLIPBOARD,
|
|---|
| 64 | INTERFACE_CLIPBOARD, 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.
|
|---|
| 74 | *
|
|---|
| 75 | */
|
|---|
| 76 | static void clip_exchange_end(async_exch_t *exch)
|
|---|
| 77 | {
|
|---|
| 78 | async_exchange_end(exch);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /** Copy string to clipboard.
|
|---|
| 82 | *
|
|---|
| 83 | * Sets the clipboard contents to @a str. Passing an empty string or NULL
|
|---|
| 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.
|
|---|
| 89 | *
|
|---|
| 90 | */
|
|---|
| 91 | int clipboard_put_str(const char *str)
|
|---|
| 92 | {
|
|---|
| 93 | size_t size = str_size(str);
|
|---|
| 94 |
|
|---|
| 95 | if (size == 0) {
|
|---|
| 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);
|
|---|
| 100 |
|
|---|
| 101 | return (int) rc;
|
|---|
| 102 | } else {
|
|---|
| 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);
|
|---|
| 108 |
|
|---|
| 109 | if (rc != EOK) {
|
|---|
| 110 | sysarg_t rc_orig;
|
|---|
| 111 | async_wait_for(req, &rc_orig);
|
|---|
| 112 | if (rc_orig == EOK)
|
|---|
| 113 | return (int) rc;
|
|---|
| 114 | else
|
|---|
| 115 | return (int) rc_orig;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | async_wait_for(req, &rc);
|
|---|
| 119 |
|
|---|
| 120 | return (int) rc;
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | /** Get a copy of clipboard contents.
|
|---|
| 125 | *
|
|---|
| 126 | * Returns a new string that can be deallocated with free().
|
|---|
| 127 | *
|
|---|
| 128 | * @param str Here pointer to the newly allocated string is stored.
|
|---|
| 129 | *
|
|---|
| 130 | * @return Zero on success or negative error code.
|
|---|
| 131 | *
|
|---|
| 132 | */
|
|---|
| 133 | int clipboard_get_str(char **str)
|
|---|
| 134 | {
|
|---|
| 135 | /* Loop until clipboard read succesful */
|
|---|
| 136 | while (true) {
|
|---|
| 137 | async_exch_t *exch = clip_exchange_begin();
|
|---|
| 138 |
|
|---|
| 139 | sysarg_t size;
|
|---|
| 140 | sysarg_t tag;
|
|---|
| 141 | sysarg_t rc = async_req_0_2(exch, CLIPBOARD_CONTENT, &size, &tag);
|
|---|
| 142 |
|
|---|
| 143 | clip_exchange_end(exch);
|
|---|
| 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;
|
|---|
| 159 | case CLIPBOARD_TAG_DATA:
|
|---|
| 160 | sbuf = malloc(size + 1);
|
|---|
| 161 | if (sbuf == NULL)
|
|---|
| 162 | return ENOMEM;
|
|---|
| 163 |
|
|---|
| 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);
|
|---|
| 168 |
|
|---|
| 169 | if ((int) rc == EOVERFLOW) {
|
|---|
| 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) {
|
|---|
| 178 | sysarg_t rc_orig;
|
|---|
| 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;
|
|---|
| 196 | }
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /** @}
|
|---|
| 201 | */
|
|---|