Mercurial > sdl-ios-xcode
annotate src/video/x11/SDL_x11clipboard.c @ 5284:96a22141cf86 tip
Changed output directory of Universal libSDL.a for iOS to respect build configurations. Template generator was updated to reflect these changes as well.
author | Eric Wing <ewing . public |-at-| gmail . com> |
---|---|
date | Sat, 12 Feb 2011 21:52:30 -0800 |
parents | b530ef003506 |
children |
rev | line source |
---|---|
4508 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
5267 | 3 Copyright (C) 1997-2011 Sam Lantinga |
4508 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Lesser General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2.1 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Lesser General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Lesser General Public | |
16 License along with this library; if not, write to the Free Software | |
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 #include "SDL_config.h" | |
23 | |
24 #include <limits.h> /* For INT_MAX */ | |
25 | |
26 #include "SDL_events.h" | |
27 #include "SDL_x11video.h" | |
28 | |
29 | |
4509
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
30 /* If you don't support UTF-8, you might use XA_STRING here */ |
4516
f18bc9935507
Use a better switch for the clipboard property format
Sam Lantinga <slouken@libsdl.org>
parents:
4509
diff
changeset
|
31 #ifdef X_HAVE_UTF8_STRING |
4509
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
32 #define TEXT_FORMAT XInternAtom(display, "UTF8_STRING", False) |
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
33 #else |
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
34 #define TEXT_FORMAT XA_STRING |
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
35 #endif |
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
36 |
4508 | 37 /* Get any application owned window handle for clipboard association */ |
38 static Window | |
39 GetWindow(_THIS) | |
40 { | |
41 SDL_Window *window; | |
42 | |
5251
58265e606e4e
Window coordinates are in the global space and windows are not tied to a particular display.
Sam Lantinga <slouken@libsdl.org>
parents:
4516
diff
changeset
|
43 window = _this->windows; |
58265e606e4e
Window coordinates are in the global space and windows are not tied to a particular display.
Sam Lantinga <slouken@libsdl.org>
parents:
4516
diff
changeset
|
44 if (window) { |
58265e606e4e
Window coordinates are in the global space and windows are not tied to a particular display.
Sam Lantinga <slouken@libsdl.org>
parents:
4516
diff
changeset
|
45 return ((SDL_WindowData *) window->driverdata)->xwindow; |
4508 | 46 } |
47 return None; | |
48 } | |
49 | |
50 int | |
51 X11_SetClipboardText(_THIS, const char *text) | |
52 { | |
53 Display *display = ((SDL_VideoData *) _this->driverdata)->display; | |
54 Atom format; | |
55 Window window; | |
56 | |
57 /* Get the SDL window that will own the selection */ | |
58 window = GetWindow(_this); | |
59 if (window == None) { | |
60 SDL_SetError("Couldn't find a window to own the selection"); | |
61 return -1; | |
62 } | |
63 | |
4509
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
64 /* Save the selection on the root window */ |
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
65 format = TEXT_FORMAT; |
4508 | 66 XChangeProperty(display, DefaultRootWindow(display), |
67 XA_CUT_BUFFER0, format, 8, PropModeReplace, | |
68 (const unsigned char *)text, SDL_strlen(text)); | |
69 | |
70 if (XGetSelectionOwner(display, XA_PRIMARY) != window) { | |
71 XSetSelectionOwner(display, XA_PRIMARY, window, CurrentTime); | |
72 } | |
73 return 0; | |
74 } | |
75 | |
76 char * | |
77 X11_GetClipboardText(_THIS) | |
78 { | |
79 SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata; | |
80 Display *display = videodata->display; | |
81 Atom format; | |
82 Window window; | |
83 Window owner; | |
84 Atom selection; | |
85 Atom seln_type; | |
86 int seln_format; | |
87 unsigned long nbytes; | |
88 unsigned long overflow; | |
89 unsigned char *src; | |
90 char *text; | |
91 | |
92 text = NULL; | |
93 | |
4509
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
94 /* Get the window that holds the selection */ |
4508 | 95 window = GetWindow(_this); |
4509
8e91c3947210
Made it possible to switch the text format in one place
Sam Lantinga <slouken@libsdl.org>
parents:
4508
diff
changeset
|
96 format = TEXT_FORMAT; |
4508 | 97 owner = XGetSelectionOwner(display, XA_PRIMARY); |
98 if ((owner == None) || (owner == window)) { | |
99 owner = DefaultRootWindow(display); | |
100 selection = XA_CUT_BUFFER0; | |
101 } else { | |
102 /* Request that the selection owner copy the data to our window */ | |
103 owner = window; | |
104 selection = XInternAtom(display, "SDL_SELECTION", False); | |
105 XConvertSelection(display, XA_PRIMARY, format, selection, owner, | |
106 CurrentTime); | |
107 | |
108 /* FIXME: Should we have a timeout here? */ | |
109 videodata->selection_waiting = SDL_TRUE; | |
110 while (videodata->selection_waiting) { | |
111 SDL_PumpEvents(); | |
112 } | |
113 } | |
114 | |
115 if (XGetWindowProperty(display, owner, selection, 0, INT_MAX/4, False, | |
116 format, &seln_type, &seln_format, &nbytes, &overflow, &src) | |
117 == Success) { | |
118 if (seln_type == format) { | |
119 text = (char *)SDL_malloc(nbytes+1); | |
120 if (text) { | |
121 SDL_memcpy(text, src, nbytes); | |
122 text[nbytes] = '\0'; | |
123 } | |
124 } | |
125 XFree(src); | |
126 } | |
127 | |
128 if (!text) { | |
129 text = SDL_strdup(""); | |
130 } | |
131 return text; | |
132 } | |
133 | |
134 SDL_bool | |
135 X11_HasClipboardText(_THIS) | |
136 { | |
137 /* Not an easy way to tell with X11, as far as I know... */ | |
138 char *text; | |
139 SDL_bool retval; | |
140 | |
141 text = X11_GetClipboardText(_this); | |
142 if (*text) { | |
143 retval = SDL_TRUE; | |
144 } else { | |
145 retval = SDL_FALSE; | |
146 } | |
147 SDL_free(text); | |
148 | |
149 return retval; | |
150 } | |
151 | |
152 /* vi: set ts=4 sw=4 expandtab: */ |