From b237bfb6c2159d555c1778ef6f049a16c2cbd03d Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Tue, 29 Aug 2017 00:15:30 -0400 Subject: simplify memory ownership --- ggamma.c | 143 ++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 69 insertions(+), 74 deletions(-) (limited to 'ggamma.c') diff --git a/ggamma.c b/ggamma.c index 3cbc5d7..3373be2 100644 --- a/ggamma.c +++ b/ggamma.c @@ -11,24 +11,24 @@ #define UNUSED __attribute__((unused)) -struct curve_data { - int gamma_size; - void (*cb)(uint16_t *ivec); +struct gamma_channel { + int size; + uint16_t *data; + void (*flush)(void); }; -void curve_handle_update(GtkWidget *raw_curve, UNUSED GdkEvent *event, struct curve_data *data) { - gfloat fvec[data->gamma_size]; - uint16_t ivec[data->gamma_size]; +void curve_edited(GtkWidget *raw_curve, UNUSED GdkEvent *event, struct gamma_channel *gamma) { + gfloat vec[gamma->size]; - gtk_curve_get_vector(GTK_CURVE(raw_curve), data->gamma_size, fvec); - for (int i = 0; i < data->gamma_size; i++) - ivec[i] = (uint16_t)(fvec[i] * 65535.0 / data->gamma_size); + gtk_curve_get_vector(GTK_CURVE(raw_curve), gamma->size, vec); + for (int i = 0; i < gamma->size; i++) + gamma->data[i] = (uint16_t)(vec[i] * 65535.0 / gamma->size); - if (data->cb) - data->cb(ivec); + if (gamma->flush) + gamma->flush(); } -GtkWidget *curve_new(int gamma_size, uint16_t *ivec, void (*cb)(uint16_t *ivec)) { +GtkWidget *curve_new(struct gamma_channel *gamma) { GtkWidget *curve = gtk_gamma_curve_new(); GtkWidget *raw_curve = GTK_GAMMA_CURVE(curve)->curve; @@ -39,21 +39,17 @@ GtkWidget *curve_new(int gamma_size, uint16_t *ivec, void (*cb)(uint16_t *ivec)) * loosing anything. */ gtk_curve_set_range(GTK_CURVE(raw_curve), - /* x */0, gamma_size-1, - /* y */0, gamma_size-1); + /* x */0, gamma->size-1, + /* y */0, gamma->size-1); - gfloat fvec[gamma_size]; - for (int i = 0; i < gamma_size; i++) - fvec[i] = ivec[i] * gamma_size / 65535.0; + gfloat vec[gamma->size]; + for (int i = 0; i < gamma->size; i++) + vec[i] = gamma->data[i] * gamma->size / 65535.0; gtk_curve_set_vector(GTK_CURVE(raw_curve), - gamma_size, fvec); + gamma->size, vec); - // TODO: this memory gets leaked - struct curve_data *data = malloc(sizeof(struct curve_data)); - data->gamma_size = gamma_size; - data->cb = cb; // TODO: find a better signal - g_signal_connect(raw_curve, "event-after", G_CALLBACK(curve_handle_update), data); + g_signal_connect(raw_curve, "event-after", G_CALLBACK(curve_edited), gamma); return curve; } @@ -85,63 +81,62 @@ int main(int argc, char *argv[]) { } RRCrtc crtc = res->crtcs[0]; // TODO: real CTRC selection int gamma_size = XRRGetCrtcGammaSize(display, crtc); - XRRCrtcGamma *gamma = XRRAllocGamma(gamma_size); + XRRCrtcGamma *gamma = XRRGetCrtcGamma(display, crtc); - { GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); - gtk_window_set_resizable(GTK_WINDOW(window), FALSE); - g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); + /* start window */ + GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_resizable(GTK_WINDOW(window), FALSE); + g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); - // TODO: add a xrandr(1) emulation pane + // TODO: add a xrandr(1) emulation pane - { GtkWidget *layout = gtk_hbox_new(TRUE, 0); - gtk_container_add(GTK_CONTAINER(window), layout); - gtk_widget_show(layout); + /* start layout */ + GtkWidget *layout = gtk_hbox_new(TRUE, 0); + gtk_container_add(GTK_CONTAINER(window), layout); + gtk_widget_show(layout); - XRRCrtcGamma *orig_gamma = XRRGetCrtcGamma(display, crtc); - - /* red */ - { - void set_red(uint16_t *vec) { - for (int i = 0; i < gamma_size; i++) - gamma->red[i] = vec[i]; - XRRSetCrtcGamma(display, crtc, gamma); - } - GtkWidget *curve = curve_new(gamma_size, orig_gamma->red, set_red); - gtk_container_add(GTK_CONTAINER(layout), curve); - gtk_widget_show(curve); - } - - /* green */ - { - void set_green(uint16_t *vec) { - for (int i = 0; i < gamma_size; i++) - gamma->green[i] = vec[i]; - XRRSetCrtcGamma(display, crtc, gamma); - } - GtkWidget *curve = curve_new(gamma_size, orig_gamma->green, set_green); - gtk_container_add(GTK_CONTAINER(layout), curve); - gtk_widget_show(curve); - } - - /* blue */ - { - void set_blue(uint16_t *vec) { - for (int i = 0; i < gamma_size; i++) - gamma->blue[i] = vec[i]; - XRRSetCrtcGamma(display, crtc, gamma); - } - GtkWidget *curve = curve_new(gamma_size, orig_gamma->blue, set_blue); - gtk_container_add(GTK_CONTAINER(layout), curve); - gtk_widget_show(curve); - } - - XRRFreeGamma(orig_gamma); - - } - - gtk_widget_show(window); + void flush(void) { + XRRSetCrtcGamma(display, crtc, gamma); } + /* start red */ + struct gamma_channel r = { + .size = gamma_size, + .data = gamma->red, + .flush = flush, + }; + GtkWidget *curve = curve_new(&r); + gtk_container_add(GTK_CONTAINER(layout), curve); + gtk_widget_show(curve); + /* end red */ + + /* start green */ + struct gamma_channel g = { + .size = gamma_size, + .data = gamma->green, + .flush = flush, + }; + curve = curve_new(&g); + gtk_container_add(GTK_CONTAINER(layout), curve); + gtk_widget_show(curve); + /* end green */ + + /* start blue */ + struct gamma_channel b = { + .size = gamma_size, + .data = gamma->blue, + .flush = flush, + }; + curve = curve_new(&b); + gtk_container_add(GTK_CONTAINER(layout), curve); + gtk_widget_show(curve); + /* end blue */ + + /* end layout */ + + gtk_widget_show(window); + /* end window */ + gtk_main(); XFree(gamma); XCloseDisplay(display); -- cgit v1.2.3