2
/* This is a program to show how mouse events can be handled by programs
3
running under an xterm window.
13
extern FILE *safe_popen(); /* From misc.c */
21
while ( (c=event_getc(&X_event)) != EOF ) {
22
if ( X_event.happening ) {
23
printf("\r\n************\r\n");
24
printf("Mouse Event: 0x%x\r\n", X_event.button_state);
25
printf("\tX coordinate: %d\r\n", X_event.x);
26
printf("\tY coordinate: %d\r\n", X_event.y);
27
/* One example of getting the mouse state */
28
printf("\tX Event: ");
29
switch(X_event.button_state&BUTTON_MASK) {
30
case RELEASE: printf("Button was released!\r\n");
32
default: printf("Button %d was pressed!\r\n",
33
(X_event.button_state&BUTTON_MASK)+1);
36
/* Another example of getting the mouse state */
37
printf("\tMouse %s buttons <%c|%c|%c>\r\n",
38
(BUTTON_ISSET(X_event, RELEASE) ? "released" : "pressed"),
39
(BUTTON_ISSET(X_event, BUTTON_1) ? 'X' : ' '),
40
(BUTTON_ISSET(X_event, BUTTON_2) ? 'X' : ' '),
41
(BUTTON_ISSET(X_event, BUTTON_3) ? 'X' : ' '));
42
#ifdef REPORT_SELECTION
43
if ( (X_event.button_state&SELECTED) == SELECTED )
44
printf("Selection: Start x=%d, y=%d; End x=%d, y=%d\r\n",
45
X_event.selection.begin_col,
46
X_event.selection.begin_row,
47
X_event.selection.end_col,
48
X_event.selection.end_row);
49
#endif /* REPORT_SELECTION */
50
printf("************\r\n");
55
} else if ( c < ' ' ) {
58
} else if ( c == 'Q' )
66
int main(int argc, char *argv[])
68
if ( event_init(stdin, stdout, NULL) < 0 ) {
69
printf("Not running under an xterm.\n");
72
system("stty -echo raw");
76
system("stty -raw echo");
83
/* I/O streams default to stdin and stdout. */
84
static FILE *xt_input, *xt_output;
85
static int have_xterm=0;
86
static int set_title=0;
87
static char *old_title=NULL;
88
int terminal_input=0; /* Is there pending terminal input? */
90
static char *get_xtitle()
92
char buffer[512], *title;
95
strcpy(buffer, "xterm");
96
if ( (title=(char *)getenv("WINDOWID")) ) {
97
if ( (strlen("xprop -id ")+strlen(title)+1) > 512 )
99
sprintf(buffer, "xprop -id %s", title);
100
if ( (pipe=safe_popen(buffer, "r")) ) {
101
while ( fgets(buffer, 511, pipe) );
102
/* Last line should be 'WM_NAME(STRING) = "title"' */
103
if (strlen(buffer) > strlen("WM_NAME(STRING) = \"")) {
104
buffer[strlen(buffer)-2]='\0';
105
title=(buffer+strlen("WM_NAME(STRING) = \""));
106
strcpy(buffer, title);
108
strcpy(buffer, "xterm");
111
strcpy(buffer, "xterm");
113
if ( (title=(char *)malloc(strlen(buffer)+1)) == NULL )
115
strcpy(title, buffer);
118
static void set_xtitle(titlebar)
121
fprintf(xt_output, "\033]0;%s\07", titlebar);
125
int event_init(input, output, titlebar)
126
FILE *input, *output;
131
/* A program can assume that input is unbuffered after this routine */
136
/* Check for xterm terminal type */
137
if ( (termtype=(char *)getenv("TERM")) && strcmp(termtype, "xterm") == 0 ) {
138
#ifdef REPORT_SELECTION
139
fprintf(xt_output, "\033[?1001h");
141
fprintf(xt_output, "\033[?1000h");
142
#endif /* REPORT_SELECTION */
145
old_title=get_xtitle();
147
set_xtitle(titlebar);
156
int event_getc(X_event)
157
struct event *X_event;
159
#ifdef REPORT_SELECTION
160
static int last_row, last_col;
163
static char prefix[8], *next;
164
#ifdef SPLITVT_SOURCE
165
extern struct physical physical;
169
X_event->happening=0;
172
if ( terminal_input == 0 ) { /* Brand new input */
173
strcpy(prefix, "\033["); /* Sequence prefix */
175
while ( (c=getc(xt_input)) != EOF ) {
179
terminal_input=(next-prefix);
187
} else { /* We already have input to return */
192
/* If we got here, we got prefix + c */
194
case 'M': /* ^[[M%d%d%d (assume getc() is ok) */
195
X_event->button_state =
196
(getc(xt_input)-' ');
197
X_event->y=(getc(xt_input)-' ');
199
X_event->x=(getc(xt_input)-' ');
201
#ifdef REPORT_SELECTION
202
if (BUTTON_ISSET((*X_event), BUTTON_1))
203
{ /* Tell xterm to start selection */
204
#ifdef SPLITVT_SOURCE
205
if ( X_event->x < (physical.subwins[LOWER])->row_offset )
206
thiswin=physical.subwins[UPPER];
207
else if ( X_event->x > (physical.subwins[LOWER])->row_offset )
208
thiswin=physical.subwins[LOWER];
209
else { /* On separator bar -- don't start cut-paste there */
210
fprintf(xt_output, "\033[0;0;0;0;0T");
212
X_event->happening=1;
215
fprintf(xt_output, "\033[1;%d;%d;%d;%dT",
216
/* startcol, startrow, */
217
X_event->y, X_event->x,
219
1 + thiswin->row_offset,
221
thiswin->rows + thiswin->row_offset+1);
224
"\033[1;%d;%d;%d;%dT",
225
/* startcol, startrow, */
226
X_event->y, X_event->x,
227
/* firstrow, lastrow */
229
#endif /* SPLITVT_SOURCE */
232
#endif /* REPORT_SELECTION */
233
X_event->happening=1;
235
#ifdef REPORT_SELECTION
236
case 't': /* ^[[t%d%d */
237
X_event->button_state =
239
X_event->selection.begin_row=last_row;
240
X_event->selection.begin_col=last_col;
241
X_event->selection.end_col =
242
(getc(xt_input)-' ');
243
X_event->selection.end_row =
244
(getc(xt_input)-' ');
245
X_event->happening=1;
247
case 'T': /* ^[[T%d%d%d%d%d%d */
248
X_event->y=(getc(xt_input)-' ');
249
X_event->x=(getc(xt_input)-' ');
250
X_event->button_state =
252
X_event->selection.begin_col =
253
(getc(xt_input)-' ');
254
X_event->selection.begin_row =
255
(getc(xt_input)-' ');
256
X_event->selection.end_col =
257
(getc(xt_input)-' ');
258
X_event->selection.end_row =
259
(getc(xt_input)-' ');
260
X_event->happening=1;
262
#endif /* REPORT_SELECTION */
263
case EOF: /* We got EOF, return EOF */
266
default: /* It's not an event sequence */
268
terminal_input=(next-prefix);
274
return(getc(xt_input));
281
#ifdef REPORT_SELECTION
282
fprintf(xt_output, "\033[?1001l");
284
fprintf(xt_output, "\033[?1000l");
285
#endif /* REPORT_SELECTION */
287
if ( set_title && old_title ) {
288
set_xtitle(old_title);
289
(void) free(old_title);