File: selection.c

package info (click to toggle)
sawfish 1:1.5.3-2.4
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 18,160 kB
  • ctags: 1,428
  • sloc: lisp: 23,542; ansic: 15,824; sh: 10,268; makefile: 615; perl: 19
file content (140 lines) | stat: -rw-r--r-- 4,034 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* selection.c -- selection handling (only retrieving)
   $Id$

   Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>

   Originally adapted from the Jade sources by Mark Probst.

   This file is part of sawfish.

   sawfish is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   sawfish is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with sawfish; see the file COPYING.   If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "sawfish.h"
#include <string.h>
#include <X11/Xatom.h>

/* selection functions */

static Atom sawfish_selection;

static inline Atom
symbol_to_atom (repv sym)
{
    return XInternAtom (dpy, rep_STR (rep_SYM (sym)->name), False);
}

DEFUN ("x-selection-active-p", Fx_selection_active_p,
       Sx_selection_active_p, (repv sel), rep_Subr1) /*
::doc:sawfish.wm.util.selection#x-selection-active-p::
x-selection-active-p SELECTION

Returns t if the X11 selection defined by the symbol SELECTION is
available for reading.
::end:: */
{
    Atom selection;
    rep_DECLARE1 (sel, rep_SYMBOLP);
    selection = symbol_to_atom (sel);
    return (XGetSelectionOwner (dpy, selection) != None) ? Qt : Qnil;
}

static Bool
selnotify_pred (Display *dpy, XEvent *ev, XPointer arg)
{
    return ev->type == SelectionNotify;
}

DEFUN ("x-get-selection", Fx_get_selection,
       Sx_get_selection, (repv sel), rep_Subr1) /*
::doc:sawfish.wm.util.selection#x-get-selection::
x-get-selection SELECTION

Returns the string corresponding to the current value of the X11
selection defined by the symbol SELECTION.

If the selection currently has no value, nil is returned.
::end:: */
{
    repv res = Qnil;
    Atom selection;
    Window owner;
    rep_DECLARE1 (sel, rep_SYMBOLP);
    selection = symbol_to_atom (sel);
    owner = XGetSelectionOwner (dpy, selection);
    if (owner != None)
    {
	XEvent ev;
	Window sel_window = no_focus_window;
	XConvertSelection (dpy, selection, XA_STRING,
			   sawfish_selection, sel_window, last_event_time);
	XIfEvent (dpy, &ev, selnotify_pred, (XPointer) 0);
	if (ev.xselection.property != None)
	{
	    /* First find the size of the property. */
	    Atom actual_type;
	    int actual_format;
	    unsigned long nitems, bytes_after;
	    unsigned char *prop;          
	    int r;
	    int offset;
	    r = XGetWindowProperty (dpy, sel_window, sawfish_selection,
				    0, 0, False, AnyPropertyType,
				    &actual_type, &actual_format,
				    &nitems, &bytes_after, &prop);
	    if (r != Success)
		return Qnil;
	    XFree (prop);
	    if (actual_type == None || actual_format != 8)
		return Qnil;
	    res = rep_make_string (bytes_after + 1);
	    if (!res)
		return rep_mem_error ();
	    offset = 0;
	    while (bytes_after > 0)
	    {
		r = XGetWindowProperty (dpy, sel_window, sawfish_selection,
					offset/4, (bytes_after / 4) + 1,
					False, AnyPropertyType,
					&actual_type, &actual_format,
					&nitems, &bytes_after, &prop);
		if (r != Success)
		    return Qnil;
		memcpy (rep_STR(res) + offset, prop, nitems);
		XFree (prop);
		offset += nitems;
	    }
	    XDeleteProperty (dpy, sel_window, sawfish_selection);
	    rep_STR(res)[offset] = 0;
	}
    }
    return res;
}

/* dl hooks */

repv
rep_dl_init (void)
{
    repv tem = rep_push_structure ("sawfish.wm.util.selection");
    /* ::alias:selection sawfish.wm.util.selection:: */
    rep_alias_structure ("selection");
    rep_ADD_SUBR (Sx_selection_active_p);
    rep_ADD_SUBR (Sx_get_selection);

    if (dpy != 0)
	sawfish_selection = XInternAtom (dpy, "SAWFISH_SELECTION", False);

    return rep_pop_structure (tem);
}