summaryrefslogtreecommitdiff
path: root/beryl-plugins/src/addhelper.c
diff options
context:
space:
mode:
authorkristian <kristian@d7aaf104-2d23-0410-ae22-9d23157bf5a3>2007-02-10 16:55:54 +0000
committerkristian <kristian@d7aaf104-2d23-0410-ae22-9d23157bf5a3>2007-02-10 16:55:54 +0000
commit745c442753651877779de93012e76574af5dba7c (patch)
tree2ce1e821a9ec5205420f7b9602b1d8b63f28e719 /beryl-plugins/src/addhelper.c
parent4b0a7073328cbbdd0bb7f600733b9340584fddb6 (diff)
downloadmarex-dev-745c442753651877779de93012e76574af5dba7c.tar.gz
marex-dev-745c442753651877779de93012e76574af5dba7c.tar.bz2
plugins: addhelper... Dim all but the active window when active.
... better name anyome? O_o git-svn-id: file:///beryl/trunk@3914 d7aaf104-2d23-0410-ae22-9d23157bf5a3
Diffstat (limited to 'beryl-plugins/src/addhelper.c')
-rw-r--r--beryl-plugins/src/addhelper.c429
1 files changed, 429 insertions, 0 deletions
diff --git a/beryl-plugins/src/addhelper.c b/beryl-plugins/src/addhelper.c
new file mode 100644
index 0000000..1aa0d11
--- /dev/null
+++ b/beryl-plugins/src/addhelper.c
@@ -0,0 +1,429 @@
+/**
+ * Beryl ADD Helper. Makes it easier to concentrate.
+ *
+ * Copyright (c) 2007 Kristian LyngstĂžl <kristian@beryl-project.org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * This plugin provides a toggle-feature that dims all but the active
+ * window. This makes it easier for people with lousy concentration
+ * to focus. Like me.
+ *
+ *
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <math.h>
+#include <X11/Xatom.h>
+#include <X11/extensions/Xrender.h>
+#include <beryl.h>
+
+#define GET_ADD_DISPLAY(d) \
+ ((AddHelperDisplay *) (d)->privates[displayPrivateIndex].ptr)
+#define ADD_DISPLAY(d) \
+ AddHelperDisplay *ad = GET_ADD_DISPLAY (d)
+#define GET_ADD_SCREEN(s, ad) \
+ ((AddHelperScreen *) (s)->privates[(ad)->screenPrivateIndex].ptr)
+#define ADD_SCREEN(s) \
+ AddHelperScreen *as = GET_ADD_SCREEN (s, GET_ADD_DISPLAY (s->display))
+
+typedef enum _AhDisplayOptios {
+ DOPT_TOGGLE = 0,
+ DOPT_OP,
+ DOPT_BRI,
+ DOPT_SAT,
+ DOPT_WINDOW_TYPE,
+ DOPT_NUM
+} AhDisplayOptions;
+
+#define ADD_TOGGLE_KEY "p"
+#define ADD_TOGGLE_MOD CompSuperMask
+
+typedef enum _AhScreenOptions {
+ SOPT_NUM
+} AhScreenOptions;
+
+#define NUM_OPTIONS(s) (sizeof ((s)->opt) / sizeof (CompOption))
+#define N_WIN_TYPE (sizeof (winType) / sizeof (winType[0]))
+
+static int displayPrivateIndex = 0;
+
+typedef enum _AhFocusBehavior {
+ FOCUS_RESET,
+ FOCUS_CHANGE,
+ FOCUS_ADD
+} AhFocusBehavior;
+
+typedef struct _AddHelperDisplay
+{
+ int screenPrivateIndex;
+ int active_screen;
+ GLushort opacity;
+ GLushort brightness;
+ GLushort saturation;
+ int wmMask;
+ Bool toggle;
+ AhFocusBehavior behavior;
+
+ HandleEventProc handleEvent;
+ CompOption opt[DOPT_NUM];
+} AddHelperDisplay;
+
+static char *winType[] = {
+ N_("Toolbar"),
+ N_("Utility"),
+ N_("Dialog"),
+ N_("ModalDialog"),
+ N_("Fullscreen"),
+ N_("Normal")
+};
+
+/* Core addhelper functions. These do the real work. ---------------------*/
+
+/* Reset a window */
+static void reset_window(CompWindow * w)
+{
+ resetWindowOpacity(w, PL_TEMP_HELLO);
+ resetWindowBrightness(w, PL_TEMP_HELLO);
+ resetWindowSaturation(w, PL_TEMP_HELLO);
+}
+/* Dim an (inactive) window.
+ */
+static void dim_window(CompWindow * w)
+{
+ ADD_DISPLAY(w->screen->display);
+ setWindowOpacity(w, MIN(ad->opacity, w->paint.opacity), PL_TEMP_HELLO);
+ setWindowBrightness(w, MIN(ad->brightness, w->paint.brightness), PL_TEMP_HELLO);
+ setWindowSaturation(w, MIN(ad->saturation, w->paint.saturation), PL_TEMP_HELLO);
+}
+
+/* Walk through all windows of the screen and adjust them if they
+ * are not the active window. If reset is true, this will reset
+ * the windows, including the active. Otherwise, it will dim
+ * and reset the active.
+ */
+static void walk_windows(CompScreen * s, Bool reset)
+{
+ CompWindow *w;
+ Window active = s->display->activeWindow;
+ ADD_DISPLAY(s->display);
+
+ for (w = s->windows; w; w = w->next)
+ {
+ if (!(w->type & ad->wmMask))
+ continue;
+ if (w->invisible || w->destroyed || w->hidden || w->minimized)
+ continue;
+ if (!reset)
+ {
+ if (w->id == active)
+ reset_window(w);
+ else
+ dim_window(w);
+ }
+ else
+ reset_window(w);
+
+ }
+}
+
+/* Takes the inital event.
+ * This checks for focus change and acts on it.
+ */
+static void addhelperHandleEvent(CompDisplay * d, XEvent * event)
+{
+ CompScreen *s;
+
+ ADD_DISPLAY(d);
+
+ UNWRAP(ad, d, handleEvent);
+ (*d->handleEvent) (d, event);
+ WRAP(ad, d, handleEvent, addhelperHandleEvent);
+
+ if (!ad->toggle)
+ return;
+ if (event->type == ConfigureNotify || event->type == PropertyNotify)
+ for (s = d->screens; s; s = s->next)
+ walk_windows(s, False);
+}
+
+
+/* Configuration, initialization, boring stuff. ----------------------- */
+static void addhelperFiniScreen(CompPlugin * p, CompScreen * s)
+{
+ ADD_DISPLAY(s->display);
+
+ removeScreenAction(s, &ad->opt[DOPT_TOGGLE].value.
+ action);
+}
+
+
+
+
+static Bool addhelperInitScreen(CompPlugin * p, CompScreen * s)
+{
+ ADD_DISPLAY(s->display);
+ addScreenAction(s, &ad->opt[DOPT_TOGGLE].value.action);
+ return TRUE;
+}
+
+
+/* Takes the action and toggles us.
+ */
+static Bool addhelper_toggle(CompDisplay * d, CompAction * ac,
+ CompActionState state, CompOption * option,
+ int nOption)
+{
+ ADD_DISPLAY(d);
+ CompScreen * s;
+ ad->toggle = !ad->toggle;
+ for (s = d->screens; s; s = s->next)
+ walk_windows(s, !ad->toggle);
+ return TRUE;
+}
+
+static void addhelperDisplayInitOptions(AddHelperDisplay * ad)
+{
+ CompOption *o;
+ int i;
+
+ o = &ad->opt[DOPT_TOGGLE];
+ o->name = "toggle";
+ o->group = N_("Bindings");
+ o->subGroup = N_("");
+ o->displayHints = "";
+ o->shortDesc = N_("Toggle AddHelper");
+ o->longDesc =
+ N_
+ ("Use this to enable/disable AddHelper on the fly.");
+ o->type = CompOptionTypeAction;
+ o->value.action.initiate = addhelper_toggle;
+ o->value.action.terminate = 0;
+ o->value.action.bell = FALSE;
+ o->value.action.edgeMask = 0;
+ o->value.action.type = CompBindingTypeKey;
+ o->value.action.state = CompActionStateInitKey;
+ o->value.action.key.modifiers = ADD_TOGGLE_MOD;
+ o->value.action.key.keysym = XStringToKeysym(ADD_TOGGLE_KEY);
+ o->advanced = False;
+ ad->toggle = False;
+
+ o = &ad->opt[DOPT_BRI];
+ o->name = "bri";
+ o->group = N_("Misc. options");
+ o->subGroup = N_("");
+ o->displayHints = "";
+ o->shortDesc = N_("Brightness");
+ o->longDesc =
+ N_
+ ("Brightness of faded windows");
+ o->type = CompOptionTypeInt;
+ o->value.i = 30;
+ o->rest.i.min = 1;
+ o->rest.i.max = 100;
+ o->advanced = False;
+ ad->brightness = BRIGHT * 0.30f;
+
+ o = &ad->opt[DOPT_SAT];
+ o->name = "sat";
+ o->group = N_("Misc. options");
+ o->subGroup = N_("");
+ o->displayHints = "";
+ o->shortDesc = N_("Saturation");
+ o->longDesc =
+ N_
+ ("Saturation of faded windows");
+ o->type = CompOptionTypeInt;
+ o->value.i = 50;
+ o->rest.i.min = 0;
+ o->rest.i.max = 100;
+ o->advanced = False;
+ ad->saturation = 0.0f;
+
+ o = &ad->opt[DOPT_OP];
+ o->name = "op";
+ o->group = N_("Misc. options");
+ o->subGroup = N_("");
+ o->displayHints = "";
+ o->shortDesc = N_("Opacity");
+ o->longDesc =
+ N_
+ ("Opacity of faded windows");
+ o->type = CompOptionTypeInt;
+ o->value.i = 100;
+ o->rest.i.min = 1;
+ o->rest.i.max = 100;
+ o->advanced = False;
+ ad->opacity = OPAQUE;
+
+ o = &ad->opt[DOPT_WINDOW_TYPE];
+ o->name = "window_types";
+ o->group = N_("Misc. options");
+ o->subGroup = N_("");
+ o->displayHints = "";
+ o->shortDesc = N_("Window Types");
+ o->longDesc = N_("Window types that should be Opacified.");
+ o->type = CompOptionTypeList;
+ o->value.list.type = CompOptionTypeString;
+ o->value.list.nValue = N_WIN_TYPE;
+ o->value.list.value = malloc(sizeof(CompOptionValue) * N_WIN_TYPE);
+ for (i = 0; i < N_WIN_TYPE; i++)
+ o->value.list.value[i].s = strdup(winType[i]);
+ o->rest.s.string = (char **)windowTypeString;
+ o->rest.s.nString = nWindowTypeString;
+ o->advanced = True;
+ ad->wmMask = compWindowTypeMaskFromStringList(&o->value);
+
+}
+
+static Bool addhelperSetDisplayOptions(CompDisplay * display, char *name,
+ CompOptionValue * value)
+{
+ CompOption *o;
+ int index;
+
+ ADD_DISPLAY(display);
+
+ o = compFindOption(ad->opt, NUM_OPTIONS(ad), name, &index);
+ if (!o)
+ return FALSE;
+
+ switch (index)
+ {
+ case DOPT_TOGGLE:
+ if (setDisplayAction(display, o, value))
+ return TRUE;
+ break;
+ case DOPT_BRI:
+ if (compSetIntOption(o, value))
+ {
+ ad->brightness = (o->value.i * 0xffff) / 100;
+ return TRUE;
+ }
+ break;
+ case DOPT_SAT:
+ if (compSetIntOption(o, value))
+ {
+ ad->saturation = (o->value.i * 0xffff) / 100;
+ return TRUE;
+ }
+ break;
+ case DOPT_OP:
+ if (compSetIntOption(o, value))
+ {
+ ad->opacity = (o->value.i * 0xffff) / 100;
+ return TRUE;
+ }
+ break;
+ case DOPT_WINDOW_TYPE:
+ if (compSetOptionList(o, value))
+ {
+ ad->wmMask = compWindowTypeMaskFromStringList(&o->value);
+ return TRUE;
+ }
+ break;
+ default:
+ break;
+ }
+ return FALSE;
+}
+
+static CompOption *addhelperGetDisplayOptions(CompDisplay * display, int *count)
+{
+ if (display)
+ {
+ ADD_DISPLAY(display);
+ *count = NUM_OPTIONS(ad);
+ return ad->opt;
+ }
+ else
+ {
+ AddHelperDisplay *ad = malloc(sizeof(AddHelperDisplay));
+
+ addhelperDisplayInitOptions(ad);
+ *count = NUM_OPTIONS(ad);
+ return ad->opt;
+ }
+}
+
+static Bool addhelperInitDisplay(CompPlugin * p, CompDisplay * d)
+{
+ AddHelperDisplay *ad = (AddHelperDisplay *) malloc(sizeof(AddHelperDisplay));
+
+ ad->screenPrivateIndex = allocateScreenPrivateIndex(d);
+ if (ad->screenPrivateIndex < 0)
+ {
+ free(ad);
+ return FALSE;
+ }
+ addhelperDisplayInitOptions(ad);
+ d->privates[displayPrivateIndex].ptr = ad;
+ ad->active_screen = d->screens->screenNum;
+ WRAP(ad, d, handleEvent, addhelperHandleEvent);
+ return TRUE;
+}
+
+static void addhelperFiniDisplay(CompPlugin * p, CompDisplay * d)
+{
+ ADD_DISPLAY(d);
+ UNWRAP(ad, d, handleEvent);
+ freeScreenPrivateIndex(d, ad->screenPrivateIndex);
+ free(ad);
+}
+
+static Bool addhelperInit(CompPlugin * p)
+{
+ displayPrivateIndex = allocateDisplayPrivateIndex();
+ if (displayPrivateIndex < 0)
+ return FALSE;
+ return TRUE;
+}
+
+static void addhelperFini(CompPlugin * p)
+{
+ if (displayPrivateIndex >= 0)
+ freeDisplayPrivateIndex(displayPrivateIndex);
+}
+
+CompPluginVTable addhelperVTable = {
+ "addhelper",
+ N_("AddHelper"),
+ N_("Make it slightly easier to concentrate by dimming all but the active window."),
+ addhelperInit,
+ addhelperFini,
+ addhelperInitDisplay,
+ addhelperFiniDisplay,
+ addhelperInitScreen,
+ addhelperFiniScreen,
+ 0,
+ 0,
+ addhelperGetDisplayOptions,
+ addhelperSetDisplayOptions,
+ 0, //addhelperGetScreenOptions,
+ 0, //addhelperSetScreenOptions,
+ 0,
+ 0,
+ 0,
+ 0,
+ BERYL_ABI_INFO,
+ "beryl-plugins",
+ "accessibility",
+ 0,
+ 0,
+ False,
+};
+
+CompPluginVTable *getCompPluginInfo(void)
+{
+ return &addhelperVTable;
+}