[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 | *
|
---|
| 35 | * The clipboard data is stored in a file and it is shared by the entire
|
---|
| 36 | * system.
|
---|
| 37 | *
|
---|
| 38 | * @note Synchronization is missing. File locks would be ideal for the job.
|
---|
[0902edfe] | 39 | */
|
---|
| 40 |
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <string.h>
|
---|
[0b772f5] | 43 | #include <sys/types.h>
|
---|
| 44 | #include <sys/stat.h>
|
---|
| 45 | #include <fcntl.h>
|
---|
| 46 | #include <macros.h>
|
---|
[0902edfe] | 47 | #include <errno.h>
|
---|
[0b772f5] | 48 | #include <clipboard.h>
|
---|
| 49 |
|
---|
| 50 | /** File used to store clipboard data */
|
---|
| 51 | #define CLIPBOARD_FILE "/data/clip"
|
---|
[0902edfe] | 52 |
|
---|
[0b772f5] | 53 | #define CHUNK_SIZE 4096
|
---|
[0902edfe] | 54 |
|
---|
| 55 | /** Copy string to clipboard.
|
---|
| 56 | *
|
---|
| 57 | * Sets the clipboard contents to @a str. Passing an empty string or NULL
|
---|
| 58 | * makes the clipboard empty.
|
---|
| 59 | *
|
---|
| 60 | * @param str String to put to clipboard or NULL.
|
---|
| 61 | * @return Zero on success or negative error code.
|
---|
| 62 | */
|
---|
| 63 | int clipboard_put_str(const char *str)
|
---|
| 64 | {
|
---|
[0b772f5] | 65 | int fd;
|
---|
| 66 | const char *sp;
|
---|
| 67 | ssize_t to_write, written, left;
|
---|
| 68 |
|
---|
| 69 | fd = open(CLIPBOARD_FILE, O_CREAT | O_TRUNC | O_WRONLY, 0666);
|
---|
| 70 | if (fd < 0)
|
---|
| 71 | return EIO;
|
---|
| 72 |
|
---|
| 73 | left = str_size(str);
|
---|
| 74 | sp = str;
|
---|
| 75 |
|
---|
| 76 | while (left > 0) {
|
---|
| 77 | to_write = min(left, CHUNK_SIZE);
|
---|
| 78 | written = write(fd, sp, to_write);
|
---|
| 79 |
|
---|
| 80 | if (written < 0) {
|
---|
| 81 | close(fd);
|
---|
| 82 | unlink(CLIPBOARD_FILE);
|
---|
| 83 | return EIO;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | sp += written;
|
---|
| 87 | left -= written;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (close(fd) != EOK) {
|
---|
| 91 | unlink(CLIPBOARD_FILE);
|
---|
| 92 | return EIO;
|
---|
| 93 | }
|
---|
[0902edfe] | 94 |
|
---|
| 95 | return EOK;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[0b772f5] | 98 | /** Get a copy of clipboard contents.
|
---|
| 99 | *
|
---|
| 100 | * Returns a new string that can be deallocated with free().
|
---|
| 101 | *
|
---|
| 102 | * @param str Here pointer to the newly allocated string is stored.
|
---|
| 103 | * @return Zero on success or negative error code.
|
---|
| 104 | */
|
---|
[0902edfe] | 105 | int clipboard_get_str(char **str)
|
---|
| 106 | {
|
---|
[0b772f5] | 107 | int fd;
|
---|
| 108 | char *sbuf, *sp;
|
---|
| 109 | ssize_t to_read, n_read, left;
|
---|
| 110 | struct stat cbs;
|
---|
| 111 |
|
---|
| 112 | if (stat(CLIPBOARD_FILE, &cbs) != EOK)
|
---|
| 113 | return EIO;
|
---|
| 114 |
|
---|
| 115 | sbuf = malloc(cbs.size + 1);
|
---|
| 116 | if (sbuf == NULL)
|
---|
| 117 | return ENOMEM;
|
---|
| 118 |
|
---|
| 119 | fd = open(CLIPBOARD_FILE, O_RDONLY);
|
---|
| 120 | if (fd < 0) {
|
---|
| 121 | free(sbuf);
|
---|
| 122 | return EIO;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | sp = sbuf;
|
---|
| 126 | left = cbs.size;
|
---|
| 127 |
|
---|
| 128 | while (left > 0) {
|
---|
| 129 | to_read = min(left, CHUNK_SIZE);
|
---|
| 130 | n_read = read(fd, sp, to_read);
|
---|
| 131 |
|
---|
| 132 | if (n_read < 0) {
|
---|
| 133 | close(fd);
|
---|
| 134 | free(sbuf);
|
---|
| 135 | return EIO;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | sp += n_read;
|
---|
| 139 | left -= n_read;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | close(fd);
|
---|
| 143 |
|
---|
| 144 | *sp = '\0';
|
---|
| 145 | *str = sbuf;
|
---|
| 146 |
|
---|
[0902edfe] | 147 | return EOK;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /** @}
|
---|
| 151 | */
|
---|