Notcurses 3.0.16
a blingful library for TUIs and character graphics
Loading...
Searching...
No Matches
gpm.c
Go to the documentation of this file.
1#include "internal.h"
2#ifdef USE_GPM
3#undef buttons // defined by both term.h and gpm.h, ugh
4#include <lib/gpm.h>
5#include <gpm.h>
6
7static Gpm_Connect gpmconn; // gpm server handle
8
9static void*
10gpmwatcher(void* vti){
11 static char cmdbuf[20]; // max is '\x1b[<int;int;intM'
12 cmdbuf[0] = '\x1b';
13 cmdbuf[1] = '[';
14 cmdbuf[2] = '<';
15 tinfo* ti = vti;
16 Gpm_Event gev;
17 const int space = sizeof(cmdbuf) - 3;
18 while(true){
19 if(!Gpm_GetEvent(&gev)){
20 logerror("error reading from gpm daemon");
21 continue;
22 }
23 loginfo("got gpm event y=%hd x=%hd mod=%u butt=%u", gev.y, gev.x,
24 (unsigned)gev.modifiers, (unsigned)gev.buttons);
25 if(gev.y < 0 || gev.x < 0){
26 logwarn("negative input %hd %hd", gev.x, gev.y);
27 continue;
28 }
29 // gpm is 0-indexed, but we assume mice reports to be 1-indexed, as they
30 // are in the XTerm protocols. no need to account for margins here.
31 if(snprintf(cmdbuf + 3, space, "%hd;%hd;%hdM", 0, gev.x + 1, gev.y + 1) >= space){
32 logwarn("input overflowed %hd %hd", gev.x, gev.y);
33 continue;
34 }
35 ncinput_shovel(ti->ictx, cmdbuf, strlen(cmdbuf));
36 }
37 return NULL;
38}
39
40int gpm_connect(tinfo* ti){
41 gpm_zerobased = 1;
42 // get all of _MOVE, _DRAG, _DOWN, and _UP
43 gpmconn.eventMask = GPM_DRAG | GPM_DOWN | GPM_UP;
44 gpmconn.defaultMask = 0;
45 gpmconn.minMod = 0;
46 gpmconn.maxMod = 0; // allow shift+drag to be used for direct copy+paste
47 if(Gpm_Open(&gpmconn, 0) == -1){
48 logerror("couldn't connect to gpm");
49 return -1;
50 }
51 if(pthread_create(&ti->gpmthread, NULL, gpmwatcher, ti)){
52 logerror("couldn't spawn gpm thread");
53 Gpm_Close();
54 memset(&gpmconn, 0, sizeof(gpmconn));
55 return -1;
56 }
57 loginfo("connected to gpm on %d", gpm_fd);
58 return gpm_fd;
59}
60
61int gpm_read(tinfo* ti, ncinput* ni){
62 (void)ti;
63 (void)ni;
64 return -1;
65}
66
67int gpm_close(tinfo* ti){
68 void* thrres;
69 cancel_and_join("gpm", ti->gpmthread, &thrres);
70 Gpm_Close();
71 memset(&gpmconn, 0, sizeof(gpmconn));
72 return 0;
73}
74
75const char* gpm_version(void){
76 return Gpm_GetLibVersion(NULL);
77}
78#else
80 (void)ti;
81 return -1;
82}
83
84int gpm_read(tinfo* ti, ncinput* ni){
85 (void)ti;
86 (void)ni;
87 return -1;
88}
89
91 (void)ti;
92 return -1;
93}
94
95const char* gpm_version(void){
96 return "n/a";
97}
98#endif
const char * gpm_version(void)
Definition gpm.c:95
int gpm_read(tinfo *ti, ncinput *ni)
Definition gpm.c:84
int gpm_connect(tinfo *ti)
Definition gpm.c:79
int gpm_close(tinfo *ti)
Definition gpm.c:90
int ncinput_shovel(inputctx *ictx, const void *buf, int len)
Definition in.c:2476
#define logerror(fmt,...)
Definition logging.h:32
#define loginfo(fmt,...)
Definition logging.h:42
#define logwarn(fmt,...)
Definition logging.h:37
struct inputctx * ictx
Definition termdesc.h:181
pthread_t gpmthread
Definition termdesc.h:194
return NULL
Definition termdesc.h:229