diff --git a/project.conf b/project.conf index 3febcdf..069d149 100644 --- a/project.conf +++ b/project.conf @@ -2,3 +2,5 @@ package=Flashlight version=0.0.0 config=h dist=Makefile,config.h + +subdirs=src diff --git a/src/flashlight.c b/src/flashlight.c new file mode 100644 index 0000000..868023f --- /dev/null +++ b/src/flashlight.c @@ -0,0 +1,115 @@ +/* $Id$ */ +/* Copyright (c) 2016 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Flashlight */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#include +#include +#include "flashlight.h" + + +/* Flashlight */ +/* private */ +/* types */ +struct _Flashlight +{ + /* widgets */ + GtkWidget * box; + /* image */ + GtkWidget * image; + /* controls */ + GtkWidget * co_main; +}; + + +/* public */ +/* functions */ +/* flashlight_new */ +Flashlight * flashlight_new(GtkOrientation orientation) +{ + Flashlight * flashlight; + GtkWidget * widget; + + if((flashlight = object_new(sizeof(*flashlight))) == NULL) + return NULL; + flashlight->box = gtk_box_new(orientation, 0); + gtk_box_set_homogeneous(GTK_BOX(flashlight->box), TRUE); + /* image */ + flashlight->image = gtk_image_new(); + gtk_box_pack_start(GTK_BOX(flashlight->box), flashlight->image, TRUE, + TRUE, 0); + /* controls */ + widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 4); + flashlight->co_main = gtk_toggle_button_new_with_mnemonic("_Switch"); + gtk_box_pack_start(GTK_BOX(widget), flashlight->co_main, FALSE, TRUE, + 0); + gtk_box_pack_start(GTK_BOX(flashlight->box), widget, FALSE, TRUE, 0); + return flashlight; +} + + +/* flashlight_delete */ +void flashlight_delete(Flashlight * flashlight) +{ + gtk_widget_destroy(flashlight->box); + object_delete(flashlight); +} + + +/* accessors */ +/* flashlight_get_widget */ +GtkWidget * flashlight_get_widget(Flashlight * flashlight) +{ + return flashlight->box; +} + + +/* flashlight_set_orientation */ +void flashlight_set_orientation(Flashlight * flashlight, + GtkOrientation orientation) +{ +#if GTK_CHECK_VERSION(2, 16, 0) + gtk_orientable_set_orientation(GTK_ORIENTABLE(flashlight->box), + orientation); +#else +# warning Switching orientation is not supported (needs Gtk+ >= 2.16) +#endif +} + + +/* useful */ +/* flashlight_toggle */ +void flashlight_toggle(Flashlight * flashlight) +{ + gboolean active; + + active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON( + flashlight->co_main)); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(flashlight->co_main), + !active); +} diff --git a/src/flashlight.h b/src/flashlight.h new file mode 100644 index 0000000..210841b --- /dev/null +++ b/src/flashlight.h @@ -0,0 +1,53 @@ +/* $Id$ */ +/* Copyright (c) 2016 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Flashlight */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#ifndef FLASHLIGHT_FLASHLIGHT_H +# define FLASHLIGHT_FLASHLIGHT_H + + +/* Flashlight */ +/* types */ +typedef struct _Flashlight Flashlight; + + +/* functions */ +Flashlight * flashlight_new(GtkOrientation orientation); +void flashlight_delete(Flashlight * window); + +/* accessors */ +GtkWidget * flashlight_get_widget(Flashlight * flashlight); + +void flashlight_set_orientation(Flashlight * flashlight, + GtkOrientation orientation); + +/* useful */ +void flashlight_toggle(Flashlight * flashlight); + +#endif /* !FLASHLIGHT_FLASHLIGHT_H */ diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..0324a18 --- /dev/null +++ b/src/main.c @@ -0,0 +1,91 @@ +/* $Id$ */ +/* Copyright (c) 2016 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Flashlight */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#include +#include +#include +#include "window.h" +#include "../config.h" + +#ifndef PROGNAME +# define PROGNAME "flashlight" +#endif + + +/* Flashlight */ +/* private */ +/* prototypes */ +static int _flashlight(void); + +static int _usage(void); + + +/* functions */ +/* flashlight */ +static int _flashlight(void) +{ + FlashlightWindow * window; + + if((window = flashlightwindow_new()) == NULL) + /* FIXME report errors */ + return -1; + gtk_main(); + flashlightwindow_delete(window); + return 0; +} + + +/* usage */ +static int _usage(void) +{ + fputs("Usage: " PROGNAME "\n", stderr); + return 1; +} + + +/* public */ +/* functions */ +/* main */ +int main(int argc, char * argv[]) +{ + int o; + + gtk_init(&argc, &argv); + while((o = getopt(argc, argv, "")) != -1) + switch(o) + { + case '?': + default: + return _usage(); + } + if(optind != argc) + return _usage(); + return (_flashlight() == 0) ? 0 : 2; +} diff --git a/src/project.conf b/src/project.conf new file mode 100644 index 0000000..4dbd6dc --- /dev/null +++ b/src/project.conf @@ -0,0 +1,19 @@ +targets=flashlight +cflags=-W -Wall -g -O2 -pedantic -fPIE -D_FORTIFY_SOURCE=2 -fstack-protector-all +ldflags=-Wl,-pie -Wl,-z,relro -Wl,-z,now +cflags_force=`pkg-config --cflags libDesktop` +ldflags_force=`pkg-config --libs libDesktop` +dist=Makefile,flashlight.h,window.h + +[flashlight] +type=binary +sources=flashlight.c,window.c,main.c + +[flashlight.c] +depends=flashlight.h + +[main.c] +depends=window.h,../config.h + +[window.c] +depends=flashlight.h,window.h,../config.h diff --git a/src/window.c b/src/window.c new file mode 100644 index 0000000..f6406f5 --- /dev/null +++ b/src/window.c @@ -0,0 +1,120 @@ +/* $Id$ */ +/* Copyright (c) 2016 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Flashlight */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#include +#include +#include "flashlight.h" +#include "window.h" +#include "../config.h" + + +/* FlashlightWindow */ +/* private */ +/* types */ +struct _FlashlightWindow +{ + GtkWidget * window; + Flashlight * flashlight; + guint source; +}; + + +/* prototypes */ +/* callbacks */ +static gboolean _flashlightwindow_on_closex(void); +static gboolean _flashlightwindow_on_idle(gpointer data); + + +/* public */ +/* functions */ +/* flashlightwindow_new */ +FlashlightWindow * flashlightwindow_new(void) +{ + const int width = 300; + const int height = 400; + FlashlightWindow * window; + GtkWidget * widget; + GtkOrientation orientation = (height >= width) + ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL; + + if((window = object_new(sizeof(*window))) == NULL) + return NULL; + window->window = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size(GTK_WINDOW(window->window), width, height); + gtk_window_set_title(GTK_WINDOW(window->window), PACKAGE); + g_signal_connect(window->window, "delete-event", G_CALLBACK( + _flashlightwindow_on_closex), NULL); + window->flashlight = flashlight_new(orientation); + window->source = 0; + /* check for errors */ + if(window->flashlight == NULL) + { + flashlightwindow_delete(window); + return NULL; + } + widget = flashlight_get_widget(window->flashlight); + gtk_container_add(GTK_CONTAINER(window->window), widget); + gtk_widget_show_all(window->window); + window->source = g_idle_add(_flashlightwindow_on_idle, window); + return window; +} + + +/* flashlightwindow_delete */ +void flashlightwindow_delete(FlashlightWindow * window) +{ + if(window->source != 0) + g_source_remove(window->source); + if(window->flashlight != NULL) + flashlight_delete(window->flashlight); + gtk_widget_destroy(window->window); + object_delete(window); +} + + +/* prototypes */ +/* callbacks */ +/* flashlightwindow_on_closex */ +static gboolean _flashlightwindow_on_closex(void) +{ + gtk_main_quit(); + return TRUE; +} + + +/* flashlightwindow_on_idle */ +static gboolean _flashlightwindow_on_idle(gpointer data) +{ + FlashlightWindow * window = data; + + flashlight_toggle(window->flashlight); + window->source = 0; + return FALSE; +} diff --git a/src/window.h b/src/window.h new file mode 100644 index 0000000..11c91af --- /dev/null +++ b/src/window.h @@ -0,0 +1,44 @@ +/* $Id$ */ +/* Copyright (c) 2016 Pierre Pronchery */ +/* This file is part of DeforaOS Desktop Flashlight */ +/* All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + + + +#ifndef FLASHLIGHT_WINDOW_H +# define FLASHLIGHT_WINDOW_H + + +/* FlashlightWindow */ +/* types */ +typedef struct _FlashlightWindow FlashlightWindow; + + +/* functions */ +FlashlightWindow * flashlightwindow_new(void); +void flashlightwindow_delete(FlashlightWindow * window); + +#endif /* !FLASHLIGHT_WINDOW_H */