Skip to content

Commit

Permalink
Initial mouse support with X11 integration via XClip
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Sep 28, 2016
1 parent e17df6b commit 080f993
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 66 deletions.
2 changes: 1 addition & 1 deletion CRT.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef enum {
#define KEY_CS_NPAGE KEY_F(57)
#define KEY_C_PPAGE KEY_F(58)
#define KEY_C_NPAGE KEY_F(59)
#define KEY_ALT(x) KEY_F(60) + (x - 'A')
#define KEY_ALT(x) KEY_F((x=='C'?60:(x=='J'?61:(x=='K'?62:63))))
#define KEY_CTRL(x) (x - 'A' + 1)
Expand Down
12 changes: 10 additions & 2 deletions Display.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ typedef struct mevent {
#endif
#define KEY_WHEELUP KEY_F(20)
#define KEY_WHEELDOWN KEY_F(21)
#ifdef HAVE_LIBNCURSESW
#define Display_writeChstrAtn mvadd_wchnstr
#elif HAVE_CURSES
Expand Down Expand Up @@ -535,8 +538,13 @@ bool Display_init(char* term) {
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
mousemask(BUTTON1_PRESSED, NULL);
ESCDELAY = 50;
#if NCURSES_MOUSE_VERSION > 1
mousemask(REPORT_MOUSE_POSITION | BUTTON1_PRESSED | BUTTON1_RELEASED | BUTTON2_RELEASED | BUTTON4_PRESSED | BUTTON5_PRESSED, NULL);
#else
mousemask(BUTTON1_PRESSED | BUTTON1_RELEASED | BUTTON2_RELEASED, NULL);
#endif
mouseinterval(0);
set_escdelay(25);
if (has_colors()) {
start_color();
use_default_colors();
Expand Down
60 changes: 45 additions & 15 deletions FileReader.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ struct FileReader_ {
FILE* fd;
char* buffer;
int size;
int available;
int used;
char* start;
bool eof;
bool finalEnter;
bool command;
};
}*/
Expand All @@ -27,7 +28,9 @@ FileReader* FileReader_new(char* filename, bool command) {
FileReader* this = malloc(sizeof(FileReader));
if (command) {
this->fd = popen(filename, "r");
this->command = true;
} else {
this->command = false;
struct stat st = {0};
stat(filename, &st);
if (S_ISDIR(st.st_mode)) {
Expand All @@ -43,62 +46,89 @@ FileReader* FileReader_new(char* filename, bool command) {
this->size = 600;
this->buffer = malloc(this->size);
this->start = this->buffer;
this->available = fread(this->buffer, 1, this->size, this->fd);
this->used = fread(this->buffer, 1, this->size, this->fd);
this->eof = feof(this->fd);
this->finalEnter = false;
return this;
}

void FileReader_delete(FileReader* this) {
fclose(this->fd);
if (this->command) {
pclose(this->fd);
} else {
fclose(this->fd);
}
free(this->buffer);
free(this);
}

bool FileReader_eof(FileReader* this) {
return this->eof && !this->finalEnter && this->available == 0;
return this->eof && !this->finalEnter && this->used == 0;
}

char* FileReader_readAllAndDelete(FileReader* this) {
if (this->start > this->buffer) {
memmove(this->buffer, this->start, this->used);
}
while (!this->eof) {
int free = this->size - this->used;
if (free == 0) {
this->size *= 2;
this->buffer = realloc(this->buffer, this->size);
}
this->used += fread(this->buffer + this->used, 1, this->size - this->used, this->fd);
this->eof = feof(this->fd);
}
if (this->command) {
pclose(this->fd);
} else {
fclose(this->fd);
}
char* buf = this->buffer;
free(this);
return buf;
}

char* FileReader_readLine(FileReader* this) {
int chunkSize = 0;
char* newline = NULL;
while (true) {
assert(this->start + this->available <= this->buffer + this->size);
newline = memchr(this->start, '\n', this->available);
assert(this->start + this->used <= this->buffer + this->size);
newline = memchr(this->start, '\n', this->used);
if (newline) {
chunkSize = newline - this->start;
break;
}
assert(this->available <= this->size);
assert(this->used <= this->size);
if (!this->eof) {
if (this->available) {
if (this->available < this->size) {
if (this->used) {
if (this->used < this->size) {
assert(this->start > this->buffer);
memmove(this->buffer, this->start, this->available);
memmove(this->buffer, this->start, this->used);
} else {
assert(this->start == this->buffer);
this->size *= 2;
this->buffer = realloc(this->buffer, this->size);
}
}
this->start = this->buffer;
this->available += fread(this->buffer + this->available, 1, this->size - this->available, this->fd);
this->used += fread(this->buffer + this->used, 1, this->size - this->used, this->fd);
this->eof = feof(this->fd);
} else {
chunkSize = this->available;
chunkSize = this->used;
break;
}
}
if (newline || chunkSize) {
assert(chunkSize <= this->available);
assert(chunkSize <= this->used);
char* result = malloc(chunkSize + 1);
memcpy(result, this->start, chunkSize);
result[chunkSize] = '\0';
if (newline)
chunkSize++;
this->start += chunkSize;
this->available -= chunkSize;
if (newline && this->eof && this->available == 0)
this->used -= chunkSize;
if (newline && this->eof && this->used == 0)
this->finalEnter = true;
return result;
}
Expand Down
9 changes: 1 addition & 8 deletions Prototypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ int Field_quickRun(Field* this, bool* quitMask);
FileReader* FileReader_new(char* filename, bool command);
void FileReader_delete(FileReader* this);
bool FileReader_eof(FileReader* this);
char* FileReader_readAllAndDelete(FileReader* this);
char* FileReader_readLine(FileReader* this);
FILE* Files_open(const char* mode, const char* picture, const char* value);
char* Files_findFile(const char* picture, const char* value, int* dirEndsAt);
Expand Down Expand Up @@ -336,14 +337,6 @@ void* Pool_allocate(Pool* this);
void* Pool_allocateClear(Pool* this);
void Pool_free(Pool* this, void* item);
void Pool_delete(Pool* this);
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, bool owner);
void ScreenManager_delete(ScreenManager* this);
int ScreenManager_size(ScreenManager* this);
void ScreenManager_add(ScreenManager* this, Panel* item, int size);
Panel* ScreenManager_remove(ScreenManager* this, int index);
void ScreenManager_setFunctionBar(ScreenManager* this, FunctionBar* fuBar);
void ScreenManager_resize(ScreenManager* this, int x1, int y1, int x2, int y2);
void ScreenManager_run(ScreenManager* this, Panel** lastFocus, int* lastKey);
Stack* Stack_new(ObjectClass* type, bool owner);
void Stack_delete(Stack* this);
void Stack_push(Stack* this, void* data, int size);
Expand Down
27 changes: 6 additions & 21 deletions Structures.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ typedef struct GraphNode_ GraphNode;
typedef struct PatternMatcher_ PatternMatcher;
typedef struct Pool_ Pool;
typedef struct RichString_ RichString;
typedef struct ScreenManager_ ScreenManager;
typedef struct ScriptState_ ScriptState;
typedef struct StackItem_ StackItem;
typedef struct Stack_ Stack;
Expand Down Expand Up @@ -321,7 +320,7 @@ typedef enum {
#define KEY_CS_NPAGE KEY_F(57)
#define KEY_C_PPAGE KEY_F(58)
#define KEY_C_NPAGE KEY_F(59)
#define KEY_ALT(x) KEY_F(60) + (x - 'A')
#define KEY_ALT(x) KEY_F((x=='C'?60:(x=='J'?61:(x=='K'?62:63))))

#define KEY_CTRL(x) (x - 'A' + 1)

Expand Down Expand Up @@ -531,6 +530,9 @@ typedef struct mevent {

#endif

#define KEY_WHEELUP KEY_F(20)
#define KEY_WHEELDOWN KEY_F(21)

#ifdef HAVE_LIBNCURSESW
#define Display_writeChstrAtn mvadd_wchnstr
#elif HAVE_CURSES
Expand Down Expand Up @@ -571,10 +573,11 @@ struct FileReader_ {
FILE* fd;
char* buffer;
int size;
int available;
int used;
char* start;
bool eof;
bool finalEnter;
bool command;
};


Expand Down Expand Up @@ -826,24 +829,6 @@ struct Pool_ {
};


typedef enum Orientation_ {
VERTICAL,
HORIZONTAL
} Orientation;

struct ScreenManager_ {
int x1;
int y1;
int x2;
int y2;
Orientation orientation;
Vector* items;
int itemCount;
FunctionBar* fuBar;
bool owner;
};


struct StackItem_ {
void* data;
int size;
Expand Down
Loading

0 comments on commit 080f993

Please sign in to comment.