diff --git a/x11/pdcclip.c b/x11/pdcclip.c index e415011a..355a36e0 100644 --- a/x11/pdcclip.c +++ b/x11/pdcclip.c @@ -47,6 +47,110 @@ clipboard **man-end****************************************************************/ +static char *tmpsel = NULL; +static unsigned long tmpsel_length = 0; + +static char *xc_selection = NULL; +static long xc_selection_len = 0; + +#ifndef X_HAVE_UTF8_STRING +static Atom XA_UTF8_STRING(Display *dpy) +{ + static AtomPtr p = NULL; + + if (!p) + p = XmuMakeAtom("UTF8_STRING"); + + return XmuInternAtom(dpy, p); +} +#endif + +static Boolean _convert_proc(Widget w, Atom *selection, Atom *target, + Atom *type_return, XtPointer *value_return, + unsigned long *length_return, int *format_return) +{ + PDC_LOG(("_convert_proc() - called\n")); + + if (*target == XA_TARGETS(XtDisplay(topLevel))) + { + XSelectionRequestEvent *req = XtGetSelectionRequest(w, + *selection, (XtRequestId)NULL); + + Atom *targetP; + XPointer std_targets; + unsigned long std_length; + + XmuConvertStandardSelection(topLevel, req->time, selection, + target, type_return, &std_targets, + &std_length, format_return); + + *length_return = std_length + 2; + *value_return = XtMalloc(sizeof(Atom) * (*length_return)); + + targetP = *(Atom**)value_return; + *targetP++ = XA_STRING; + *targetP++ = XA_UTF8_STRING(XtDisplay(topLevel)); + + memmove((void *)targetP, (const void *)std_targets, + sizeof(Atom) * std_length); + + XtFree((char *)std_targets); + *type_return = XA_ATOM; + *format_return = 8; + + return True; + } + else if (*target == XA_UTF8_STRING(XtDisplay(topLevel)) || + *target == XA_STRING) + { + char *data = XtMalloc(tmpsel_length + 1); + char *tmp = tmpsel; + int ret_length = 0; + + while (*tmp) + data[ret_length++] = *tmp++; + + data[ret_length] = '\0'; + + *value_return = data; + *length_return = ret_length; + *format_return = 8; + *type_return = *target; + + return True; + } + else + return XmuConvertStandardSelection(topLevel, CurrentTime, + selection, target, type_return, (XPointer*)value_return, + length_return, format_return); +} + +static void _lose_ownership(Widget w, Atom *type) +{ + PDC_LOG(("_lose_ownership() - called\n")); + + if (tmpsel) + free(tmpsel); + + tmpsel = NULL; + tmpsel_length = 0; +} + +static void _get_selection(Widget w, XtPointer data, Atom *selection, + Atom *type, XtPointer value, + unsigned long *length, int *format) +{ + PDC_LOG(("_get_selection() - called\n")); + + if (value) + { + xc_selection = value; + xc_selection_len = (long)(*length); + } + else + xc_selection_len = 0; +} + int PDC_getclipboard(char **contents, long *length) { XEvent event; @@ -56,7 +160,14 @@ int PDC_getclipboard(char **contents, long *length) xc_selection = NULL; xc_selection_len = -1; - XC_get_selection(); + XtGetSelectionValue(topLevel, XA_PRIMARY, +#ifdef PDC_WIDE + XA_UTF8_STRING(XtDisplay(topLevel)), +#else + XA_STRING, +#endif + _get_selection, (XtPointer)NULL, 0); + while (-1 == xc_selection_len) { XtAppNextEvent(app_context, &event); @@ -83,9 +194,37 @@ int PDC_getclipboard(char **contents, long *length) int PDC_setclipboard(const char *contents, long length) { + long pos; + int status; + PDC_LOG(("PDC_setclipboard() - called\n")); - return XC_set_selection(contents, length); + if (length > (long)tmpsel_length) + { + if (!tmpsel_length) + tmpsel = malloc(length + 1); + else + tmpsel = realloc(tmpsel, length + 1); + } + + for (pos = 0; pos < length; pos++) + tmpsel[pos] = contents[pos]; + + tmpsel_length = length; + tmpsel[length] = 0; + + if (XtOwnSelection(topLevel, XA_PRIMARY, CurrentTime, + _convert_proc, _lose_ownership, NULL) == False) + { + status = PDC_CLIP_ACCESS_ERROR; + free(tmpsel); + tmpsel = NULL; + tmpsel_length = 0; + } + else + status = PDC_CLIP_SUCCESS; + + return status; } int PDC_freeclipboard(char *contents) diff --git a/x11/pdcx11.h b/x11/pdcx11.h index 60406039..9a1093f6 100644 --- a/x11/pdcx11.h +++ b/x11/pdcx11.h @@ -107,8 +107,6 @@ void XC_refresh_scrollbar(void); void XC_set_blink(bool); XColor XC_get_color(short); void XC_set_color(short, XColor); -void XC_get_selection(void); -int XC_set_selection(const char *, long); extern XtAppContext app_context; extern Widget topLevel; @@ -116,10 +114,6 @@ extern Widget topLevel; extern int XCursesLINES; extern int XCursesCOLS; -typedef void (*signal_handler)(); - -signal_handler XCursesSetSignal(int, signal_handler); - #ifdef PDCDEBUG void XC_say(const char *msg); # define XC_LOG(x) XC_say x @@ -134,5 +128,3 @@ void XC_say(const char *msg); #endif extern bool xc_resize_now; -extern char *xc_selection; -extern long xc_selection_len; diff --git a/x11/x11.c b/x11/x11.c index 7aa440a8..6e0726e2 100644 --- a/x11/x11.c +++ b/x11/x11.c @@ -208,8 +208,6 @@ static Atom wm_atom[2]; static String class_name = "XCurses"; static Widget drawing, scrollBox, scrollVert, scrollHoriz; static int received_map_notify = 0; -static char *tmpsel = NULL; -static unsigned long tmpsel_length = 0; static Pixmap icon_pixmap; static Pixmap icon_pixmap_mask; static bool visible_cursor = FALSE; @@ -222,8 +220,6 @@ int XCursesLINES = 24; int XCursesCOLS = 80; bool xc_resize_now = FALSE; -char *xc_selection = NULL; -long xc_selection_len = 0; /* Macros just for app_resources */ @@ -385,18 +381,6 @@ void XC_say(const char *msg) } #endif -#ifndef X_HAVE_UTF8_STRING -static Atom XA_UTF8_STRING(Display *dpy) -{ - static AtomPtr p = NULL; - - if (!p) - p = XmuMakeAtom("UTF8_STRING"); - - return XmuInternAtom(dpy, p); -} -#endif - /* Convert character positions x and y to pixel positions, stored in xpos and ypos */ @@ -991,77 +975,6 @@ unsigned long XCursesKeyPress(XEvent *event) return -1; } -static Boolean _convert_proc(Widget w, Atom *selection, Atom *target, - Atom *type_return, XtPointer *value_return, - unsigned long *length_return, int *format_return) -{ - XC_LOG(("_convert_proc() - called\n")); - - if (*target == XA_TARGETS(XtDisplay(topLevel))) - { - XSelectionRequestEvent *req = XtGetSelectionRequest(w, - *selection, (XtRequestId)NULL); - - Atom *targetP; - XPointer std_targets; - unsigned long std_length; - - XmuConvertStandardSelection(topLevel, req->time, selection, - target, type_return, &std_targets, - &std_length, format_return); - - *length_return = std_length + 2; - *value_return = XtMalloc(sizeof(Atom) * (*length_return)); - - targetP = *(Atom**)value_return; - *targetP++ = XA_STRING; - *targetP++ = XA_UTF8_STRING(XtDisplay(topLevel)); - - memmove((void *)targetP, (const void *)std_targets, - sizeof(Atom) * std_length); - - XtFree((char *)std_targets); - *type_return = XA_ATOM; - *format_return = 8; - - return True; - } - else if (*target == XA_UTF8_STRING(XtDisplay(topLevel)) || - *target == XA_STRING) - { - char *data = XtMalloc(tmpsel_length + 1); - char *tmp = tmpsel; - int ret_length = 0; - - while (*tmp) - data[ret_length++] = *tmp++; - - data[ret_length] = '\0'; - - *value_return = data; - *length_return = ret_length; - *format_return = 8; - *type_return = *target; - - return True; - } - else - return XmuConvertStandardSelection(topLevel, CurrentTime, - selection, target, type_return, (XPointer*)value_return, - length_return, format_return); -} - -static void _lose_ownership(Widget w, Atom *type) -{ - XC_LOG(("_lose_ownership() - called\n")); - - if (tmpsel) - free(tmpsel); - - tmpsel = NULL; - tmpsel_length = 0; -} - static void _display_cursor(int old_row, int old_x, int new_row, int new_x) { int xpos, ypos, i; @@ -1496,73 +1409,6 @@ void XC_set_color(short index, XColor tmp) colors[index] = tmp.pixel; } -/* For PDC_getclipboard() */ - -static void _get_selection(Widget w, XtPointer data, Atom *selection, - Atom *type, XtPointer value, - unsigned long *length, int *format) -{ - XC_LOG(("_get_selection() - called\n")); - - if (value) - { - xc_selection = value; - xc_selection_len = (long)(*length); - } - else - xc_selection_len = 0; -} - -void XC_get_selection(void) -{ - XC_LOG(("XC_get_selection() - called\n")); - - XtGetSelectionValue(topLevel, XA_PRIMARY, -#ifdef PDC_WIDE - XA_UTF8_STRING(XtDisplay(topLevel)), -#else - XA_STRING, -#endif - _get_selection, (XtPointer)NULL, 0); -} - -/* For PDC_setclipboard() */ - -int XC_set_selection(const char *contents, long length) -{ - long pos; - int status; - - XC_LOG(("XC_set_selection() - called\n")); - - if (length > (long)tmpsel_length) - { - if (!tmpsel_length) - tmpsel = malloc(length + 1); - else - tmpsel = realloc(tmpsel, length + 1); - } - - for (pos = 0; pos < length; pos++) - tmpsel[pos] = contents[pos]; - - tmpsel_length = length; - tmpsel[length] = 0; - - if (XtOwnSelection(topLevel, XA_PRIMARY, CurrentTime, - _convert_proc, _lose_ownership, NULL) == False) - { - status = PDC_CLIP_ACCESS_ERROR; - free(tmpsel); - tmpsel = NULL; - tmpsel_length = 0; - } - else - status = PDC_CLIP_SUCCESS; - - return status; -} - void XC_set_blink(bool blinkon) { if (blinkon)