libpayload: Add PDCurses and ncurses' libform/libmenu
[coreboot.git] / payloads / libpayload / curses / PDCurses-3.4 / win32 / pdcclip.c
1 /* Public Domain Curses */
2
3 #include "pdcwin.h"
4
5 RCSID("$Id: pdcclip.c,v 1.30 2008/07/14 04:24:52 wmcbrine Exp $")
6
7 /*man-start**************************************************************
8
9   Name:                                                         clipboard
10
11   Synopsis:
12         int PDC_getclipboard(char **contents, long *length);
13         int PDC_setclipboard(const char *contents, long length);
14         int PDC_freeclipboard(char *contents);
15         int PDC_clearclipboard(void);
16
17   Description:
18         PDC_getclipboard() gets the textual contents of the system's 
19         clipboard. This function returns the contents of the clipboard 
20         in the contents argument. It is the responsibilitiy of the 
21         caller to free the memory returned, via PDC_freeclipboard().
22         The length of the clipboard contents is returned in the length 
23         argument.
24
25         PDC_setclipboard copies the supplied text into the system's 
26         clipboard, emptying the clipboard prior to the copy.
27
28         PDC_clearclipboard() clears the internal clipboard.
29
30   Return Values:
31         indicator of success/failure of call.
32         PDC_CLIP_SUCCESS        the call was successful
33         PDC_CLIP_MEMORY_ERROR   unable to allocate sufficient memory for 
34                                 the clipboard contents
35         PDC_CLIP_EMPTY          the clipboard contains no text
36         PDC_CLIP_ACCESS_ERROR   no clipboard support
37
38   Portability                                X/Open    BSD    SYS V
39         PDC_getclipboard                        -       -       -
40         PDC_setclipboard                        -       -       -
41         PDC_freeclipboard                       -       -       -
42         PDC_clearclipboard                      -       -       -
43
44 **man-end****************************************************************/
45
46 #ifdef PDC_WIDE
47 # define PDC_TEXT CF_UNICODETEXT
48 #else
49 # define PDC_TEXT CF_OEMTEXT
50 #endif
51
52 int PDC_getclipboard(char **contents, long *length)
53 {
54     HANDLE handle;
55     long len;
56
57     PDC_LOG(("PDC_getclipboard() - called\n"));
58
59     if (!OpenClipboard(NULL))
60         return PDC_CLIP_ACCESS_ERROR;
61
62     if ((handle = GetClipboardData(PDC_TEXT)) == NULL)
63     {
64         CloseClipboard();
65         return PDC_CLIP_EMPTY;
66     }
67
68 #ifdef PDC_WIDE
69     len = wcslen((wchar_t *)handle) * 3;
70 #else
71     len = strlen((char *)handle);
72 #endif
73     *contents = (char *)GlobalAlloc(GMEM_FIXED, len + 1);
74
75     if (!*contents)
76     {
77         CloseClipboard();
78         return PDC_CLIP_MEMORY_ERROR;
79     }
80
81 #ifdef PDC_WIDE
82     len = PDC_wcstombs((char *)*contents, (wchar_t *)handle, len);
83 #else
84     strcpy((char *)*contents, (char *)handle);
85 #endif
86     *length = len;
87     CloseClipboard();
88
89     return PDC_CLIP_SUCCESS;
90 }
91
92 int PDC_setclipboard(const char *contents, long length)
93 {
94     HGLOBAL ptr1;
95     LPTSTR ptr2;
96
97     PDC_LOG(("PDC_setclipboard() - called\n"));
98
99     if (!OpenClipboard(NULL))
100         return PDC_CLIP_ACCESS_ERROR;
101
102     ptr1 = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, 
103         (length + 1) * sizeof(TCHAR));
104
105     if (!ptr1)
106         return PDC_CLIP_MEMORY_ERROR;
107
108     ptr2 = GlobalLock(ptr1);
109
110 #ifdef PDC_WIDE
111     PDC_mbstowcs((wchar_t *)ptr2, contents, length);
112 #else
113     memcpy((char *)ptr2, contents, length + 1);
114 #endif
115     GlobalUnlock(ptr1);
116     EmptyClipboard();
117
118     if (!SetClipboardData(PDC_TEXT, ptr1))
119     {
120         GlobalFree(ptr1);
121         return PDC_CLIP_ACCESS_ERROR;
122     }
123
124     CloseClipboard();
125     GlobalFree(ptr1);
126
127     return PDC_CLIP_SUCCESS;
128 }
129
130 int PDC_freeclipboard(char *contents)
131 {
132     PDC_LOG(("PDC_freeclipboard() - called\n"));
133
134     GlobalFree(contents);
135     return PDC_CLIP_SUCCESS;
136 }
137
138 int PDC_clearclipboard(void)
139 {
140     PDC_LOG(("PDC_clearclipboard() - called\n"));
141
142     EmptyClipboard();
143
144     return PDC_CLIP_SUCCESS;
145 }