Skip to content

Commit 07a3a70

Browse files
committed
Add clipboard support
1 parent 6bec7b6 commit 07a3a70

File tree

7 files changed

+74
-16
lines changed

7 files changed

+74
-16
lines changed

call.c

+29
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,27 @@ static void commandMenuItem(pipe_buffer *target) {
413413
parameters_alloc_to_call(target, &menu, sizeof menu, setMenuItem);
414414
}
415415

416+
// clipboard
417+
418+
static struct { int16_t id; } clipboardtypeid;
419+
420+
_Static_assert(sizeof clipboardtypeid == 2, "wrong copy align");
421+
422+
static void clipboardGet() { request_clipboard(clipboardtypeid.id); }
423+
424+
static void commandClipboardGet(pipe_buffer *target) {
425+
parameters_to_call(target, &clipboardtypeid, sizeof clipboardtypeid, clipboardGet);
426+
}
427+
428+
static void clipboardPut(void *data) {
429+
set_clipboard(clipboardtypeid.id, data);
430+
free(data);
431+
}
432+
433+
static void commandClipboardPut(pipe_buffer *target) {
434+
parameters_alloc_to_call(target, &clipboardtypeid, sizeof clipboardtypeid, clipboardPut);
435+
}
436+
416437
// dispatch
417438

418439
void callcommand(char command, pipe_buffer *target) {
@@ -509,6 +530,14 @@ void callcommand(char command, pipe_buffer *target) {
509530
commandMenuItem(target);
510531
break;
511532

533+
// clipboard
534+
case '1':
535+
commandClipboardGet(target);
536+
break;
537+
case '2':
538+
commandClipboardPut(target);
539+
break;
540+
512541
default:
513542
printf("unknown command, char = %d\n", command);
514543
exit(EXIT_FAILURE);

draw.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ static id_list *image_list = NULL;
110110
image_elem *get_image(int id) { return (image_elem *)id_list_get_data(image_list, id); }
111111

112112
void image_add(int id, int width, int height, unsigned char *data) {
113-
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_RGB24, width);
113+
int cairo_format = CAIRO_FORMAT_ARGB32;
114+
int stride = cairo_format_stride_for_width(cairo_format, width);
114115
for (int i = 0; i < height; i++) {
115116
unsigned char *p = data + i * stride;
116117
for (int j = 0; j < width; j++) {
@@ -124,8 +125,7 @@ void image_add(int id, int width, int height, unsigned char *data) {
124125
e->data = data;
125126
e->width = width;
126127
e->height = height;
127-
e->image =
128-
cairo_image_surface_create_for_data(e->data, CAIRO_FORMAT_ARGB32, width, height, stride);
128+
e->image = cairo_image_surface_create_for_data(e->data, cairo_format, width, height, stride);
129129
if (image_list == NULL)
130130
image_list = id_list_new();
131131
id_list_append(image_list, id, e);

event.c

+31
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,34 @@ void s_menu_action(char *name) {
167167
pipe_event_write_string(name);
168168
pipe_event_flush();
169169
}
170+
171+
// Clipboard events
172+
173+
typedef struct {
174+
int16_t format;
175+
} clipboard_event;
176+
177+
void s_text_received(GtkClipboard *clipboard, const gchar *text, gpointer data) {
178+
if (text == NULL) {
179+
return;
180+
}
181+
char command_type = 'c';
182+
pipe_event_write(&command_type, sizeof command_type);
183+
clipboard_event e;
184+
e.format = 1;
185+
pipe_event_write(&e, sizeof e);
186+
pipe_event_write_string(text);
187+
pipe_event_flush();
188+
}
189+
190+
void request_clipboard(int clipboardtypeid) {
191+
GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
192+
gtk_clipboard_request_text(clipboard, s_text_received, NULL); // TODO text only
193+
}
194+
195+
// Clipboard funcs
196+
197+
void set_clipboard(int clipboardtypeid, void *data) {
198+
GtkClipboard *clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
199+
gtk_clipboard_set_text(clipboard, data, -1); // TODO text only
200+
}

main.c

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
#include <stdio.h>
44
#include <stdlib.h>
55

6-
#ifdef GDK_WINDOWING_WAYLAND
7-
#include <gdk/gdkwayland.h>
8-
#endif
9-
106
GtkApplication *app = NULL;
117
GtkWidget *top = NULL;
128

pipe.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,29 @@ void pipe_done() {
7878
}
7979
}
8080

81-
void pipe_output_write(void *data, int length) {
81+
void pipe_output_write(const void *data, const int length) {
8282
if (fwrite(data, length, 1, handle_output) == 0) {
8383
perror("pipe write (o)");
8484
exit(EXIT_FAILURE);
8585
}
8686
}
8787

88-
void pipe_output_write_string(char *data) {
88+
void pipe_output_write_string(const char *data) {
8989
int32_t length = strlen(data);
9090
pipe_output_write(&length, sizeof length);
9191
pipe_output_write(data, length);
9292
}
9393

9494
void pipe_output_flush() { fflush(handle_output); }
9595

96-
void pipe_event_write(void *data, int length) {
96+
void pipe_event_write(const void *data, const int length) {
9797
if (fwrite(data, length, 1, handle_event) == 0) {
9898
perror("pipe write (e)");
9999
exit(EXIT_FAILURE);
100100
}
101101
}
102102

103-
void pipe_event_write_string(char *data) {
103+
void pipe_event_write_string(const char *data) {
104104
int32_t length = strlen(data);
105105
pipe_event_write(&length, sizeof length);
106106
pipe_event_write(data, length);

terminal.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ extern GMenu *barmenu;
1919
void pipe_init(char *pipe_suffix, GIOFunc func);
2020
void pipe_done();
2121
void pipe_unwatch();
22-
void pipe_output_write(void *data, int length);
23-
void pipe_output_write_string(char *data);
22+
void pipe_output_write(const void *data, const int length);
23+
void pipe_output_write_string(const char *data);
2424
void pipe_output_flush();
25-
void pipe_event_write(void *data, int length);
26-
void pipe_event_write_string(char *data);
25+
void pipe_event_write(const void *data, const int length);
26+
void pipe_event_write_string(const char *data);
2727
void pipe_event_flush();
2828

2929
// io
@@ -45,6 +45,8 @@ gboolean s_button(GtkWidget *widget, GdkEventButton *event, gpointer data);
4545
gboolean s_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data);
4646
gboolean s_scroll(GtkWidget *widget, GdkEventScroll *event, gpointer data);
4747
void s_menu_action(char *action);
48+
void request_clipboard(int clipboardtypeid);
49+
void set_clipboard(int clipboardtypeid, void *data);
4850

4951
// call
5052

version.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
char *it_version = "0.3.8";
1+
char *it_version = "0.4.0";

0 commit comments

Comments
 (0)