Merge branch 'canoe' into vlock-2
[vlock.git] / modules / example_module.c
blobc2ad87be841fbcb290ec088874543369fdf8b0d9
1 /* example_module.c -- example module for vlock,
2 * the VT locking program for linux
4 * This program is copyright (C) 2007 Frank Benkstein, and is free
5 * software. It comes without any warranty, to the extent permitted by
6 * applicable law. You can redistribute it and/or modify it under the
7 * terms of the Do What The Fuck You Want To Public License, Version 2,
8 * as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING
9 * for more details.
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <errno.h>
18 /* Do not use any vlock specific headers here unless you intend to
19 * submit your module for inclusion. */
21 /* Include this header file to make sure the types of the dependencies
22 * and hooks are correct. */
23 #include "vlock_plugin.h"
25 /* Declare dependencies. Please see PLUGINS for their meaning. Empty
26 * dependencies can be left out. */
27 const char *preceeds[] = { "new", "all", NULL };
28 /* const char *succeeds[]; */
29 /* const char *requires[]; */
30 /* const char *needs[]; */
31 const char *depends[] = { "all", NULL };
32 /* const char *conflicts[]; */
34 /* Every hook has a void** argument ctx_ptr. When they are called
35 * ctx_ptr points to the same location and *ctx_ptr is initially set to
36 * NULL. Hook functions should pass state by defining a context struct
37 * and saving the pointer to it in *ctx_ptr instead of using global
38 * variables.
41 struct example_context {
42 int a;
43 int b;
46 /* Do something that should happen at vlock's start here. An error in
47 * this hook aborts vlock. */
48 bool vlock_start(void **ctx_ptr)
50 struct example_context *ctx = malloc(sizeof *ctx);
52 if (ctx == NULL)
53 return false;
55 ctx->a = 23;
56 ctx->b = 42;
58 /* Save the context for use by the other hooks. */
59 *ctx_ptr = ctx;
61 return true;
64 /* Hooks that are not implemented should not be defined. */
66 /* Start a screensaver type action before the password prompt after a
67 * timeout. This hook must not block! */
68 /* bool vlock_save(void **); */
70 /* Abort a screensaver type action before the password prompt after a
71 * timeout. This hook must not block! */
72 /* bool vlock_save_abort(void **); */
74 /* Do something at the end of vlock. Error returns are ignored here. */
75 bool vlock_end(void **ctx_ptr)
77 struct example_context *ctx = *ctx_ptr;
78 bool result = true;
80 if (ctx != NULL) {
81 result = (ctx->a == 23 && ctx->b == 42);
83 free(ctx);
85 if (!result)
86 fprintf(stderr, "vlock-example_module: Whoops!\n");
89 return result;