-
Notifications
You must be signed in to change notification settings - Fork 0
/
ceapi.c
executable file
·1802 lines (1309 loc) · 49.3 KB
/
ceapi.c
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*static char *sccsid = "%Z% %M% %I% - %G% %U% ";*/
/***************************************************************
*
* ARPUS/Ce text editor and terminal emulator modeled after the
* Apollo(r) Domain systems.
* Copyright 1988 - 2002 Enabling Technologies Group
* Copyright 2003 - 2005 Robert Styma Consulting
*
* This program 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
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Original Authors: Robert Styma and Kevin Plyler
* Email: [email protected]
*
***************************************************************/
/***************************************************************
*
* module ceapi.c
*
* This module provides the application program interface to Ce.
* It is compiled into libCeapi.a and allows client programs to
* interface with Ce.
*
* Routines:
* ceApiInit - Initialization routine to find the ce window.
* ceGetDisplay - Return the initialized display pointer
* ceXdmc - Send a dm command to a window
* ceGetLines - Get one or more lines
* cePutLines - Insert or replace one or more lines
* ceDelLines - Delete one or more lines
* ceGetMsg - Get the contents of the message window.
* ceGetStats - Get the current ce stats (line count, current position, etc)
* ceReadPasteBuff - Get the current value of a Ce paste buffer
*
* Internal:
* selection_request - Process an anticipated SelectionRequest
* find_select_request - Look for the selection request
* get_ce_env_var - Get the value of an environment variable from ce
* grab_baton - Get control of the selection for the a paste buffer
* wait_for_baton_loss - Wait for the baton to be grabbed by ce
* find_selection_clear - Look for an anticipated SelectionClear
* wait_for_stats - Wait for the client message from Ce with the stats.
* find_client_message - Look for the client message from Ce
*
***************************************************************/
#include <stdio.h> /* /usr/include/stdio.h */
#include <string.h> /* /bsd4.3/usr/include/string.h */
#include <errno.h> /* /usr/include/errno.h /usr/include/sys/errno.h */
#include <ctype.h> /* /usr/include/ctype.h */
#include <malloc.h> /* /usr/include/malloc.h */
#include <sys/types.h> /* /usr/include/sys/types.h */
#ifdef WIN32
#include <winsock.h>
#ifndef MAXPATHLEN
#define MAXPATHLEN 256
#include <time.h> /* /usr/include/sys/time.h */
#endif
#else
#include <netinet/in.h> /* /bsd4.3/usr/include/netinet/in.h */
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
#include <sys/time.h> /* /usr/include/sys/time.h */
#endif
#ifndef FD_ZERO
#include <sys/select.h> /* /usr/include/sys/select.h */
#endif
#include <X11/X.h> /* /usr/include/X11/X.h */
#include <X11/Xlib.h> /* /usr/include/X11/Xlib.h */
/* main needed to get cc.h constants to be declared */
#define _MAIN_ 1
#include "apistats.h"
#include "cc.h"
/* on Solaris 2.3 the memdata macro total_lines conflicts with CeStats.total lines */
#undef total_lines
#include "ceapi.h"
#include "debug.h"
#include "dmsyms.h"
#include "drawable.h"
#include "pastebuf.h"
#include "pad.h" /* needed for macro HT */
#include "xerror.h"
#include "xerrorpos.h"
#define CE_MALLOC(size) malloc(size)
#define dm_error(m,j) fprintf(stderr, "%s\n", m)
void sleep(int secs);
void usleep(int usecs);
#ifndef WIN32
void srand(int seed);
int rand(void);
#endif
/***************************************************************
*
* Static data initialized by ceApiInit
*
* display - The display opened to communicate with X.
* ce_window - This is the window id of the window we will be
* communicating with.
* paste_name - The window id in hex character string format. This
* name is used as the private paste buffer between this
* set of routines and the ce window.
* paste_atom - The atom id which corresponds to the paste_name.
*
***************************************************************/
/*static DRAWABLE_DESCR lcl_window;*/
static Window lcl_window_id;
static Window ce_window = None;
static char paste_name[20];
static Atom paste_atom = 0;
static Display *display;
#define DEFAULT_TIMEOUT 10
#define DEBUG_ENV_NAME "CEDEBUG"
/***************************************************************
*
* Prototypes for local routines.
*
***************************************************************/
static char *get_ce_env_var(char *var);
static int grab_baton(char *name);
static int wait_for_baton_loss(Atom atom,
int timeout_secs);
static int selection_request(Atom paste_atom,
int timeout_secs);
static Bool find_select_request(Display *display,
XEvent *event,
char *arg);
static Bool find_selection_clear(Display *display,
XEvent *event,
char *arg);
static int wait_for_stats(Atom atom,
XEvent *event,
int timeout_secs);
static Bool find_client_message(Display *display,
XEvent *event,
char *arg);
/***************************************************************
*
* System routines which have no built in defines
*
***************************************************************/
char *getenv(const char *name);
int fork(void);
int getpid(void);
int setpgrp(int pid, int pgrp); /* bsd version */
int isatty(int fd);
#ifndef WIN32
int gethostname(char *target, int size);
#endif
static char *copyright = "Copyright (c) 2001, A. G. Communication Systems Inc and Emerging Technologies Group, Inc. All rights reserved.";
char *getcwd(char *buf, int size);
#ifdef DebuG
/***************************************************************
*
* macro to translate event to its name
* The unsigned in the macro takes care of invalid negative values.
*
***************************************************************/
#define EVENT_NAME(event) (((unsigned)event < LASTEvent) ? event_names[event] : "Out of Range value")
char *event_names[] = {
"0",
"1",
"KeyPress",
"KeyRelease",
"ButtonPress",
"ButtonRelease",
"MotionNotify",
"EnterNotify",
"LeaveNotify",
"FocusIn",
"FocusOut",
"KeymapNotify",
"Expose",
"GraphicsExpose",
"NoExpose",
"VisibilityNotify",
"CreateNotify",
"DestroyNotify",
"UnmapNotify",
"MapNotify",
"MapRequest",
"ReparentNotify",
"ConfigureNotify",
"ConfigureRequest",
"GravityNotify",
"ResizeRequest",
"CirculateNotify",
"CirculateRequest",
"PropertyNotify",
"SelectionClear",
"SelectionRequest",
"SelectionNotify",
"ColormapNotify",
"ClientMessage",
"MappingNotify"};
#endif
/************************************************************************
NAME: ceApiInit - Initialization routine to find the ce window.
PURPOSE: This routine performs initialization and finds the ce window we need to
communicate with.
PARAMETERS:
1. cePid - int (INPUT)
This is the pid of the ce process to look for. It is used
when a process starts a ce and then goes on to use the
api to communicate with it or when the api is used from
a process not started by the ce cpo process.
Note that if the CE_WINDOW environment is set, specifying
this parameter to a value other than zero will cause the
CE_WINDOW value to be ignored.
VALUES:
0 - Used when the application is run from cpo
pid - the pid of the ce process when the api is not called from a cpo.
GLOBAL DATA:
lcl_window - DRAWABLE_DESCR (OUTPUT)
This structure contains the display pointer and
the window id of the local window we create but
do not map.
The display is opened by this routine.
It is initially NULL, but is set to -1 on
failure.
ce_window - int (OUTPUT)
The window id we will be communicating with
paste_name - int (OUTPUT)
The window id in hex character string format. This
name is used as the private paste buffer between this
set of routines and the ce window.
paste_atom - int (OUTPUT)
This is the atom used for the paste name.
FUNCTIONS :
1. Check the environment variables for the CE_WINDOW and the CEHOST
names. If CE_WINDOW is not set, we are not being called via the
dm cpo, cps, or !(bang) commands.
2. Open the display. If DISPLAY is available, use it. Otherwise
if the CEHOST variable is available, use it as
the display name. Otherwise, fail.
3. Create a window to use with the x requests. We will never map
this window.
4. Call ccxdmc with a null command to find and validate the window id.
If it fails, issue an error message and quit.
5. Create the paste buffer name and atom.
RETURNED VALUE:
rc - return code
0 - Successfull initialization
-1 - Error detected, message already output. must abort.
*************************************************************************/
int ceApiInit(int cePid)
{
int rc;
char *cehost;
char *window_string;
char *display_name;
char display_buff[80];
cehost = getenv("CEHOST");
window_string = getenv("CE_WINDOW");
display_name = getenv("DISPLAY");
/*lcl_window.display = &display;*/
/***************************************************************
*
* Get debugging flag from the environment. If it is high
* enough turn on X syncronization with the server.
*
***************************************************************/
DEBUG0(Debug(DEBUG_ENV_NAME);)
DEBUG18(
if (!cehost)
fprintf(stderr, "CEHOST was not set in the environment\n");
else
fprintf(stderr, "CEHOST=\"%s\"\n", cehost);
if (!window_string)
fprintf(stderr, "CE_WINDOW was not set in the environment\n");
else
fprintf(stderr, "CE_WINDOW=\"%s\"\n", window_string);
if (!display_name)
fprintf(stderr, "DISPLAY was not set in the environment\n");
else
fprintf(stderr, "DISPLAY=\"%s\"\n", display_name);
)
if (window_string)
sscanf(window_string, "%lX", &ce_window);
else
ce_window = None;
if (!display_name)
if (cehost)
{
snprintf(display_buff, sizeof(display_buff), "%s", cehost);
display_name = display_buff;
}
else
{
errno = ENXIO;
display = (Display *)-1;
DEBUG18(fprintf(stderr, "No display name available\n");)
return(-1);
}
display = XOpenDisplay(display_name);
if (!display)
return(-1);
lcl_window_id = XCreateSimpleWindow(
display,
RootWindow(display, DefaultScreen(display)),
0, 0, 100, 100, 1, /* arbitrary position and size since we never map it */
BlackPixel(display, DefaultScreen(display)),
WhitePixel(display, DefaultScreen(display)));
if (lcl_window_id == None)
{
DEBUG18(fprintf(stderr, "Could not create window (%s)\n", strerror(errno));)
return(-1);
}
/* needed to make ceGetLines work */
XSelectInput(display,
lcl_window_id,
PropertyChangeMask);
rc = cc_xdmc(display,
cePid,
";",
&ce_window,
False); /* quiet mode */
if (rc)
return(rc);
snprintf(paste_name, sizeof(paste_name), "%X", ce_window);
paste_atom = XInternAtom(display, paste_name, False);
return(0);
} /* end of ceApiInit */
/************************************************************************
NAME: ceGetDisplay - Return the initialized display pointer
PURPOSE: This routine is used if the Api user wants to do X windows stuff.
PARAMETERS:
None
GLOBAL DATA:
display - pointer to Display (Xlib type) (INPUT)
The display value is returned.
FUNCTIONS :
1. Return the display value.
RETURNED VALUE:
display - Display (Xlib type)
NULL - The display is not open
!NULL - The display pointer
*************************************************************************/
Display *ceGetDisplay(void)
{
if (display == (Display *)-1)
return(NULL);
else
return(display);
} /* end of ceGetDisplay */
/************************************************************************
NAME: ceXdmc - Send a dm command to a window
PURPOSE: This routine sends the passed string to ce window.
PARAMETERS:
1. display - pointer to Display (INPUT)
This is the display handle needed for X calls.
GLOBAL DATA:
display - pointer to Display (Xlib type) (INPUT)
The display is opened by ceApiInit.
It is initially NULL, but is set to -1 on
failure. It is needed for all the X calls.
ce_window - int (INPUT)
The window id we will be communicating with.
It is initialized by ceApiInit.
paste_name - int (INPUT)
The window id in hex character string format. This
name is used as the private paste buffer between this
set of routines and the ce window.
paste_atom - int (INPUT)
This is the atom used for the paste name.
FUNCTIONS :
1. Make sure the initialization routine was called successfully.
2. Pass the command to cc_xdmc.
RETURNED VALUE:
rc - return code
0 - Command sent
-1 - Error detected, message already output.
*************************************************************************/
int ceXdmc(char *cmdlist,
int synchronous)
{
int rc;
char *cmds_line;
int cmds_line_len;
/***************************************************************
*
* Make sure the init routine was called and worked.
*
***************************************************************/
if (display == NULL)
{
fprintf(stderr, "ceXdmc: ceApiInit has not been called\n");
return(-1);
}
else
if (display == (Display *)-1)
{
fprintf(stderr, "ceXdmc: ceApiInit initialization failed\n");
return(-1);
}
if (synchronous)
{
cmds_line_len = strlen(cmdlist) + 25;
cmds_line = malloc(cmds_line_len);
if (!cmds_line)
{
fprintf(stderr, "ceXdmc: Out of memory, synchronous option canceled\n");
synchronous = False;
cmds_line = cmdlist;
}
else
{
snprintf(cmds_line, cmds_line_len, "%s;xc %s", cmdlist, paste_name);
rc = grab_baton(paste_name);
if (rc)
{
fprintf(stderr, "ceXdmc: Synchronous option canceled\n");
synchronous = False;
free(cmds_line);
cmds_line = cmdlist;
}
}
}
else
cmds_line = cmdlist;
rc = cc_xdmc(display,
0,
cmds_line,
&ce_window,
False); /* quiet mode */
if ((rc == 0) && synchronous)
{
DEBUG18(fprintf(stderr, "ceXdmc: Waiting for loss of baton %s\n", paste_name);)
rc = wait_for_baton_loss(paste_atom, DEFAULT_TIMEOUT);
}
if (cmds_line != cmdlist)
free(cmds_line);
return(rc);
} /* end of ceXdmc */
/************************************************************************
NAME: ceGetLines - Get one or more lines
PURPOSE: This routine gets a set of lines from the calling program
It has to be done at an odd time in the intialization sequence
(especially in pad mode), so we have a special call for it.
PARAMETERS:
1. lineno - int (INPUT)
This is the first line number to be retrieved. One
based line numbers are used.
2. count - int (INPUT)
This is the number of lines to be extracted.
The special value -1 means to end of data.
GLOBAL DATA:
Same as ceXdmc
FUNCTIONS :
1. Verify that the init function has been called.
2. Grab the special paste buffer selection using grab_batton.
We are using the selection as both the baton and the
vehicle for carrying data. If the grab fails, return failure.,
3. Build and transmit the following command string:
<lineno>,<lineno+count>xc <winbuffname>
4. Wait for ce to grab the baton to signal it is done. If ce
does not grab the baton, the get fails.
5. Extract the paste buff data to a malloc'ed area using
ce_ReadPasteBuf
6. Take ownership of the paste special paste buffer name
using grab baton to free the storage in ce.
RETURNED VALUE:
buff - pointer to char
NULL - Could not retrieve lines
otherwise - Pointer to buffer containing the lines.
*************************************************************************/
char *ceGetLines(int lineno,
int count)
{
int rc;
char cmdline[80];
int timeout;
char *area;
/***************************************************************
*
* Make sure the init routine was called and worked.
*
***************************************************************/
if (display == NULL)
{
fprintf(stderr, "ceGetLines: ceApiInit has not been called\n");
return(NULL);
}
else
if (display == (Display *)-1)
{
fprintf(stderr, "ceGetLines: ceApiInit initialization failed\n");
return(NULL);
}
rc = grab_baton(paste_name);
if (rc)
return(NULL);
if (count < 0)
snprintf(cmdline, sizeof(cmdline), "tmw;%d,$xc %s", lineno, paste_name);
else
if (count == 0)
snprintf(cmdline, sizeof(cmdline), "tmw;%dxc %s", lineno, paste_name);
else
snprintf(cmdline, sizeof(cmdline), "tmw;%d,%dxc %s", lineno, lineno+count, paste_name);
rc = cc_xdmc(display,
0,
cmdline,
&ce_window,
False); /* quiet mode */
if (rc)
return(NULL);
timeout = count / 100;
if (timeout == 0)
timeout = DEFAULT_TIMEOUT; /* arbitrary default */
DEBUG18(fprintf(stderr, "ceGetLines: Waiting for loss of baton %s\n", paste_name);)
rc = wait_for_baton_loss(paste_atom, timeout);
if (rc)
return(NULL);
area = ceReadPasteBuff(paste_name);
grab_baton(paste_name);
return(area);
} /* end of ceGetLines */
/************************************************************************
NAME: cePutLines - Send lines back to ce
PURPOSE: This routine sends a block of lines back to ce to either
be inserted or replace an existing line.
PARAMETERS:
1. lineno - int (INPUT)
This is the first line number to be replaced or One
the line number to be inserted before.
2. count - int (INPUT)
This is the number of lines to be replaced.
This line is only interesting in replace mode.
It is the number of lines to delete.
The special value -1 means to end of data.
3. insert - int (INPUT)
True - Insert the data
False - Delete the requested number of lines
before sending the data for insert.
4. data - pointer to char (INPUT)
This parameter points to the data to send to
ce. It is a null terminated string with
embedded newlines.
GLOBAL DATA:
Same as ceXdmc
FUNCTIONS :
1. Verify that the init function has been called.
2. Open the special paste buffer for output and
initialize it's size.
3. Use ce_fputs to copy the data to the paste buffer.
4. If replace was specified, start creating the command string
with dr;<lineno>,<lineno+count>xd -l junk;
Otherwise start the command string with:
<lineno>
5. Attach the command xp <windbuffname>
7. Using select and XCheckEvent, wait for the request to transmit
the paste data. If we time out, return failure.
8. Use the pastebuf routine to send the data.
RETURNED VALUE:
rc - int
0 - data sent
-1 - Data could not be sent, request for to get data
from ce never arrived.
*************************************************************************/
int cePutLines(int lineno,
int count,
int insert,
char *data)
{
DMC_xc xc_dmc;
int rc;
char cmdline[80];
FILE *paste_stream;
/***************************************************************
*
* Make sure the init routine was called and worked.
*
***************************************************************/
if (display == NULL)
{
fprintf(stderr, "cePutLines: ceApiInit has not been called\n");
return(-1);
}
else
if (display == (Display *)-1)
{
fprintf(stderr, "cePutLines: ceApiInit initialization failed\n");
return(-1);
}
xc_dmc.next = NULL;
xc_dmc.cmd = DM_xc;
xc_dmc.temp = False;
xc_dmc.dash_r = True;
xc_dmc.dash_f = False;
xc_dmc.dash_a = False;
xc_dmc.dash_l = False;
xc_dmc.path = paste_name;
paste_stream = open_paste_buffer((DMC *)&xc_dmc, display, lcl_window_id, CurrentTime, '\\');
if (!paste_stream)
return(-1);
rc = setup_x_paste_buffer(paste_stream, NULL, 1, 1, strlen(data)+1);
if (rc)
return(rc);
ce_fputs(data, paste_stream);
ce_fclose(paste_stream);
if (insert)
snprintf(cmdline, sizeof(cmdline), "tmw;%d;xp %s", lineno, paste_name);
else
if (count < 0)
snprintf(cmdline, sizeof(cmdline), "tmw;%d,$xd -l junk;xp %s", lineno, paste_name);
else
if (count == 0)
snprintf(cmdline, sizeof(cmdline), "tmw;%dxd -l junk;xp %s", lineno, paste_name);
else
snprintf(cmdline, sizeof(cmdline), "tmw;%d,%dxd -l junk;xp %s", lineno, lineno+count, paste_name);
rc = cc_xdmc(display,
0,
cmdline,
&ce_window,
False); /* quiet mode */
if (rc)
return(rc);
rc = selection_request(paste_atom, DEFAULT_TIMEOUT);
return(rc);
} /* end of cePutLines */
/************************************************************************
NAME: selection_request - Process an anticipated SelectionRequest
PURPOSE: This routine watches for the SelectionRequest which will be sent
as a result of the xp command sent to the ce window. When it
arrives, it calls the pastebuf routine to process the request.
PARAMETERS:
1. atom - Atom (Xlib type)(INPUT)
This is the atom for the paste buffer we are One
waiting to get the selection request on.
2. timeout_secs - int (INPUT)
This is the maximum number of seconds to sleep
before giving up.
GLOBAL DATA:
Same as ceXdmc
FUNCTIONS :
1. Perform selects on the Xserver socket to watch for
information. If the select times out, we have failed.
2. Use XCHeckIfEvent to search for the expected SelectionRequest.
3. If found, process it with the routines in pastebuf.c.
RETURNED VALUE:
rc - int
0 - data sent
-1 - Data could not be sent, request for to get data
from ce never arrived.
*************************************************************************/
static int selection_request(Atom paste_atom,
int timeout_secs)
{
struct timeval time_out;
fd_set readfds;
int nfound;
int done = False;
int Xserver_socket = ConnectionNumber(display);
XEvent event_union;
while(!done)
{
FD_ZERO(&readfds);
FD_SET(Xserver_socket, &readfds);
time_out.tv_sec = timeout_secs;
time_out.tv_usec = 0;
nfound = select(Xserver_socket+1, HT &readfds, NULL, NULL, &time_out);
if (nfound)
{
done = XCheckIfEvent(display, &event_union, find_select_request, (char *)&paste_atom);
}
else
{
DEBUG18(fprintf(stderr, "selection_request: Timed out after %d seconds\n", timeout_secs);)
done = True;
}
} /* end of while not done */
/***************************************************************
*
* To get here, done was set to true. Either we timed out or
* we found the matching event. The nfound value will tell
* us this.
*
***************************************************************/
if (nfound) /* good case */
{
send_pastebuf_data(&event_union); /* in pastebuf.c */
return(0);
}
else
return(-1);
} /* end of selection_request */
/************************************************************************
NAME: find_select_request - Look for the selection request for the
special paste buffer.
PURPOSE: This routine is the test routine used in a call to XCheckIfEvent
It watches for the SelectionRequest for the xp (paste) command
sent by cePutLines.
PARAMETERS:
1. display - pointer to Display (INPUT)
This is the pointer to the current display.
2. event - pointer to XEvent (INPUT)
This is the pointer to event to be looked at.
3. arg - pointer to char (really pointer to Atom) (INPUT)
The atom we are looking for.
FUNCTIONS :
1. Unwrap the user parm.
2. If this event is a SelectionNotify on the main window for the
correct atom, return True (we found it). Otherwise return False.
*************************************************************************/
/* ARGSUSED */
static Bool find_select_request(Display *display,
XEvent *event,
char *arg)
{
Atom search_atom;
search_atom = *((Atom *)arg);
DEBUG15(fprintf(stderr, "find_select_request: got %s event\n", EVENT_NAME(event->type));)
if ((event->type == SelectionRequest) &&
(event->xselectionrequest.requestor == ce_window) &&
(event->xselectionrequest.selection == search_atom))
return(True);
else
return(False);
} /* end of find_select_request */
/************************************************************************
NAME: ceDelLines - Send a delete request to ce
PURPOSE: This routine sends a delete request for a block of lines.
PARAMETERS:
1. lineno - int (INPUT)
This is the first line number to be deleted.
2. count - int (INPUT)
This is the number of lines to be deleted.
It is the number of lines to delete.
The special value -1 means to end of data.
A count of 0 or 1 means to delete just the
line specified.
GLOBAL DATA:
Same as ceXdmc
FUNCTIONS :
1. Verify that the init function has been called.
2. Grab the special paste buffer.
3. Build and send the command:
<lineno>,<lineno+count>xd <paste_name>
4. Wait for ce to grab the paste_name to show it got the command.
RETURNED VALUE:
rc - int