feynman/src/feynman.c

158 lines
4.4 KiB
C
Raw Normal View History

2022-08-06 22:15:00 +01:00
/*
* Feynman -- Wayland compositor for GNU Emacs
*
* Copyright (c) 2022 Sameer Rahmani <lxsameer@gnu.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, version 2.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-21 12:56:04 +01:00
#include "compositor.h"
2022-08-06 22:15:00 +01:00
#include "export.h"
2022-08-21 12:56:04 +01:00
#include "queue.h"
#include "utils.h"
2022-08-07 11:06:50 +01:00
2022-08-21 12:56:04 +01:00
#include <assert.h>
2022-08-06 22:15:00 +01:00
#include <emacs-module.h>
/* Declare mandatory GPL symbol. */
FEYNMAN_EXPORT int plugin_is_GPL_compatible = 0;
// static queue_t *feynman_events_q;
2022-08-21 12:56:04 +01:00
// This value will hold a global reference to a `feynman_server` struct
emacs_value server_state;
static void
free_value (void *ptr)
{
free (ptr);
};
void *
start_event_loop (void *server_ptr)
{
assert (server_ptr);
struct feynman_server *server = (struct feynman_server *)server_ptr;
start_feynman (server);
2022-08-21 12:56:04 +01:00
return NULL;
}
2022-08-07 11:06:50 +01:00
static emacs_value
2022-08-21 12:56:04 +01:00
feynman_start (emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *data)
2022-08-07 11:06:50 +01:00
{
2022-08-06 22:15:00 +01:00
(void)nargs;
(void)args;
(void)data;
emacs_value nil = env->intern (env, "nil");
2022-08-21 12:56:04 +01:00
struct feynman_server *server = malloc (sizeof (struct feynman_server));
int err = init_feynman_server (env, server);
if (err != 0)
{
em_error (env, "Start process failed.Received none zero error code: %d",
err);
return nil;
}
2022-08-21 12:56:04 +01:00
// feynman_events_q = (queue_t *)init_queue(100);
pthread_create (&server->server_thread_id, NULL, start_event_loop,
(void *)server);
2022-08-21 12:56:04 +01:00
server_state = env->make_global_ref (
env, env->make_user_ptr (env, free_value, (void *)server));
return server_state;
}
static emacs_value
/* cppcheck-suppress constParameter */
2022-08-21 12:56:04 +01:00
feynman_stop (emacs_env *env, ptrdiff_t nargs, emacs_value args[], void *data)
{
(void)nargs;
(void)data;
emacs_value nil = env->intern (env, "nil");
emacs_value first_arg = args[0];
if (env->eq (env, first_arg, nil))
{
em_message (env, "server parameter is nil");
return nil;
}
struct feynman_server *server
= (struct feynman_server *)env->get_user_ptr (env, first_arg);
stop_feynman (env, server);
2022-08-21 12:56:04 +01:00
// deinit_queue(feynman_events_q);
emacs_value t = env->intern (env, "t");
return t;
2022-08-06 22:15:00 +01:00
}
/* Bind NAME to FUN. */
2022-08-07 11:06:50 +01:00
static void
bind_function (emacs_env *env, const char *name, emacs_value Sfun)
{
2022-08-06 22:15:00 +01:00
/* Set the function cell of the symbol named NAME to SFUN using
the 'fset' function. */
/* Convert the strings to symbols by interning them */
2022-08-07 11:06:50 +01:00
emacs_value Qfset = env->intern (env, "fset");
emacs_value Qsym = env->intern (env, name);
2022-08-06 22:15:00 +01:00
/* Prepare the arguments array */
2022-08-07 11:06:50 +01:00
emacs_value args[] = { Qsym, Sfun };
2022-08-06 22:15:00 +01:00
/* Make the call (2 == nb of arguments) */
2022-08-07 11:06:50 +01:00
env->funcall (env, Qfset, 2, args);
2022-08-06 22:15:00 +01:00
}
/* Provide FEATURE to Emacs. */
2022-08-07 11:06:50 +01:00
static void
provide (emacs_env *env, const char *feature)
{
2022-08-06 22:15:00 +01:00
/* call 'provide' with FEATURE converted to a symbol */
2022-08-07 11:06:50 +01:00
emacs_value Qfeat = env->intern (env, feature);
emacs_value Qprovide = env->intern (env, "provide");
emacs_value args[] = { Qfeat };
2022-08-06 22:15:00 +01:00
2022-08-07 11:06:50 +01:00
env->funcall (env, Qprovide, 1, args);
2022-08-06 22:15:00 +01:00
}
2022-08-07 11:06:50 +01:00
FEYNMAN_EXPORT int
emacs_module_init (struct emacs_runtime *ert)
{
emacs_env *env = ert->get_environment (ert);
2022-08-06 22:15:00 +01:00
/* create a lambda (returns an emacs_value) */
2022-08-21 12:56:04 +01:00
emacs_value startfn = env->make_function (
2022-08-06 22:15:00 +01:00
env, 0, /* min. number of arguments */
0, /* max. number of arguments */
2022-08-21 12:56:04 +01:00
feynman_start, /* actual function pointer */
2022-08-06 22:15:00 +01:00
"Initialize the compositor", /* docstring */
NULL /* user pointer of your choice (data param in feynman_init) */
);
2022-08-21 12:56:04 +01:00
bind_function (env, "feynman/start", startfn);
emacs_value stopfn = env->make_function (
env, 1, /* min. number of arguments */
1, /* max. number of arguments */
feynman_stop, /* actual function pointer */
"Stop the compositor", /* docstring */
NULL /* user pointer of your choice (data param in feynman_init) */
);
bind_function (env, "feynman/stop", stopfn);
2022-08-06 22:15:00 +01:00
2022-08-07 11:06:50 +01:00
provide (env, "feynman");
2022-08-06 22:15:00 +01:00
return 0;
}