static Atom selarg_to_seltype(Display*dpy, char arg) { switch (arg) { case 'p': return XA_PRIMARY; case 's': return XA_SECONDARY; case 'b': return XA_STRING; case 'c': return XA_CLIPBOARD(dpy); default:return XA_PRIMARY; } }
/* process selection command line option */ static void doOptSel (void) { /* set selection to work with */ if ( XrmGetResource( opt_db, "xclip.selection", "Xclip.Selection", &rec_typ, &rec_val ) ) { switch (tolower(rec_val.addr[0])) { case 'p': sseln = XA_PRIMARY; break; case 's': sseln = XA_SECONDARY; break; case 'c': sseln = XA_CLIPBOARD(dpy); break; } if (fverb == OVERBOSE) { fprintf(stderr, "Using selection: "); if (sseln == XA_PRIMARY) fprintf(stderr, "XA_PRIMARY"); if (sseln == XA_SECONDARY) fprintf(stderr, "XA_SECONDARY"); if (sseln == XA_CLIPBOARD(dpy)) fprintf(stderr, "XA_CLIPBOARD"); fprintf(stderr, "\n"); } } }
static void MemoCB(Widget w, XtPointer client_data, Atom *selection, Atom *type, XtPointer value, unsigned long *len, int *format) { if (value==NULL || *len==0) return; /* nothing had been selected to copy */ selected_fen_position = value; selected_fen_position[*len]='\0'; /* normally this string is terminated, but be safe */ XtOwnSelection(menuBarWidget, XA_CLIPBOARD(xDisplay), CurrentTime, SendPositionSelection, NULL/* lose_ownership_proc */ , NULL/* transfer_done_proc */); }
int xdaemon(int *fds, pid_t *rpid) { int fd; pid_t pid; Window win; Display *dpy; ssize_t size, rcv_size; char rpassword[MAX_PASSWORD_LEN]; int x11_fd, efd; struct epoll_event ee, events[MAX_EVENTS]; x11_started = 1; pipe(fds); pid = fork(); if (pid < 0) { close(fds[0]); close(fds[1]); return 0; } if (pid) { *rpid = pid; close(fds[0]); return fds[1]; } fd = fds[0]; close(fds[1]); if (!setup_signals()) return 0; dpy = XOpenDisplay(NULL); if (!dpy) { syslog(LOG_WARNING, "Can not open display"); return 0; } win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, 1, 1, 0, 0, 0); if (!win) { XCloseDisplay(dpy); syslog(LOG_ERR, "Can not create window"); return 0; } // XSelectInput(dpy, win, PropertyChangeMask); XSetSelectionOwner(dpy, XA_CLIPBOARD(dpy), win, CurrentTime); syslog(LOG_INFO, "Started xdaemon on fd%d", fd); x11_fd = ConnectionNumber(dpy); efd = epoll_create1(0); if (efd < 0) { syslog(LOG_ERR, "Can not create epoll"); return 0; } ee.data.fd = x11_fd; ee.events = EPOLLIN; if (epoll_ctl(efd, EPOLL_CTL_ADD, x11_fd, &ee) < 0) { syslog(LOG_ERR, "Can not add epoll x11: %s", strerror(errno)); return 0; } ee.data.fd = fd; ee.events = EPOLLIN | EPOLLET; if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &ee) < 0) { syslog(LOG_ERR, "Can not add epoll pipe: %s", strerror(errno)); return 0; } memset(rpassword, 0, MAX_PASSWORD_LEN); while (1) { int n, i; n = epoll_wait (efd, events, MAX_EVENTS, -1); for (i = 0; i < n; i++) { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { syslog(LOG_WARNING, "Invalid epoll data"); continue; } if (events[i].data.fd == fd) { size = read(fd, &rcv_size, sizeof(int)); if (size != sizeof(int)) { syslog(LOG_WARNING, "Communication error size %d", size); continue; } if (rcv_size > MAX_PASSWORD_LEN) { syslog(LOG_WARNING, "Corrupter data received"); continue; } size = read(fd, rpassword, rcv_size); if (size != rcv_size) { syslog(LOG_WARNING, "Communication error rcv_size %d", rcv_size); continue; } rpassword[rcv_size] = '\0'; XSetSelectionOwner(dpy, XA_CLIPBOARD(dpy), win, CurrentTime); process_x11_event(dpy, rpassword); } else if (events[i].data.fd == x11_fd) { if (!rpassword[0]) continue; process_x11_event(dpy, rpassword); } } } XCloseDisplay(dpy); return 1; }
int main(int argc, char *argv[]) { Arg args[4]; Cardinal n; XtAppContext xtcontext; Widget parent; XtSetLanguageProc(NULL, NULL, NULL); top = XtAppInitialize( &xtcontext, "XClipboard", table, XtNumber(table), &argc, argv, fallback_resources, NULL, 0); XtGetApplicationResources(top, (XtPointer)&userOptions, resources, XtNumber(resources), NULL, 0); XtAppAddActions (xtcontext, xclipboard_actions, XtNumber (xclipboard_actions)); /* CLIPBOARD_MANAGER is a non-standard mechanism */ ManagerAtom = XInternAtom(XtDisplay(top), "CLIPBOARD_MANAGER", False); ClipboardAtom = XA_CLIPBOARD(XtDisplay(top)); if (XGetSelectionOwner(XtDisplay(top), ManagerAtom)) XtError("another clipboard is already running\n"); parent = XtCreateManagedWidget("form", formWidgetClass, top, NULL, ZERO); (void) XtCreateManagedWidget("quit", Command, parent, NULL, ZERO); (void) XtCreateManagedWidget("delete", Command, parent, NULL, ZERO); (void) XtCreateManagedWidget("new", Command, parent, NULL, ZERO); (void) XtCreateManagedWidget("save", Command, parent, NULL, ZERO); nextButton = XtCreateManagedWidget("next", Command, parent, NULL, ZERO); prevButton = XtCreateManagedWidget("prev", Command, parent, NULL, ZERO); indexLabel = XtCreateManagedWidget("index", Label, parent, NULL, ZERO); n=0; XtSetArg(args[n], XtNtype, XawAsciiString); n++; XtSetArg(args[n], XtNeditType, XawtextEdit); n++; if (userOptions.wrap) { XtSetArg(args[n], XtNwrap, XawtextWrapWord); n++; XtSetArg(args[n], XtNscrollHorizontal, False); n++; } text = XtCreateManagedWidget( "text", Text, parent, args, n); currentClip = NewClip (text, (ClipPtr) 0); set_button_state (); fileDialogShell = XtCreatePopupShell("fileDialogShell", transientShellWidgetClass, top, NULL, ZERO); fileDialog = XtCreateManagedWidget ("fileDialog", dialogWidgetClass, fileDialogShell, NULL, ZERO); XawDialogAddButton(fileDialog, "accept", NULL, NULL); XawDialogAddButton(fileDialog, "cancel", NULL, NULL); failDialogShell = XtCreatePopupShell("failDialogShell", transientShellWidgetClass, top, NULL, ZERO); failDialog = XtCreateManagedWidget ("failDialog", dialogWidgetClass, failDialogShell, NULL, ZERO); XawDialogAddButton (failDialog, "continue", NULL, NULL); XtRealizeWidget(top); XtRealizeWidget(fileDialogShell); XtRealizeWidget(failDialogShell); XtOwnSelection(top, ManagerAtom, CurrentTime, RefuseSelection, LoseManager, NULL); if (XGetSelectionOwner (XtDisplay(top), ClipboardAtom)) { LoseSelection (top, &ClipboardAtom); } else { XtOwnSelection(top, ClipboardAtom, CurrentTime, ConvertSelection, LoseSelection, NULL); } wm_delete_window = XInternAtom(XtDisplay(top), "WM_DELETE_WINDOW", False); wm_protocols = XInternAtom(XtDisplay(top), "WM_PROTOCOLS", False); (void) XSetWMProtocols(XtDisplay(top), XtWindow(top), &wm_delete_window,1); (void) XSetWMProtocols(XtDisplay(top), XtWindow(fileDialogShell), &wm_delete_window,1); (void) XSetWMProtocols(XtDisplay(top), XtWindow(failDialogShell), &wm_delete_window,1); XtAppMainLoop(xtcontext); exit(0); }