Add a basic right-click menu to the wallpaper
This commit is contained in:
parent
964621ea08
commit
57d451301b
@ -13,14 +13,18 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/fswait.h>
|
||||
|
||||
#include <toaru/yutani.h>
|
||||
#include <toaru/graphics.h>
|
||||
#include <toaru/menu.h>
|
||||
|
||||
static yutani_t * yctx;
|
||||
static yutani_window_t * wallpaper_window;
|
||||
static gfx_context_t * wallpaper_ctx;
|
||||
static sprite_t * wallpaper;
|
||||
static struct MenuList * _rc_menu = NULL;
|
||||
|
||||
static void draw_background(int width, int height) {
|
||||
|
||||
@ -40,6 +44,15 @@ static void draw_background(int width, int height) {
|
||||
}
|
||||
}
|
||||
|
||||
static void show_right_click_menu(int x, int y) {
|
||||
menu_show(_rc_menu, yctx);
|
||||
if (x + _rc_menu->window->width > yctx->display_width) {
|
||||
yutani_window_move(yctx, _rc_menu->window, x - _rc_menu->window->width, y);
|
||||
} else {
|
||||
yutani_window_move(yctx, _rc_menu->window, x, y);
|
||||
}
|
||||
}
|
||||
|
||||
static void resize_finish_wallpaper(int width, int height) {
|
||||
yutani_window_resize_accept(yctx, wallpaper_window, width, height);
|
||||
reinit_graphics_yutani(wallpaper_ctx, wallpaper_window);
|
||||
@ -48,6 +61,28 @@ static void resize_finish_wallpaper(int width, int height) {
|
||||
yutani_flip(yctx, wallpaper_window);
|
||||
}
|
||||
|
||||
static void launch_application(char * app) {
|
||||
if (!fork()) {
|
||||
printf("Starting %s\n", app);
|
||||
char * args[] = {"/bin/sh", "-c", app, NULL};
|
||||
execvp(args[0], args);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void launch_application_menu(struct MenuEntry * self) {
|
||||
struct MenuEntry_Normal * _self = (void *)self;
|
||||
launch_application((char *)_self->action);
|
||||
}
|
||||
|
||||
static void check_click(struct yutani_msg_window_mouse_event * evt) {
|
||||
if (evt->wid == wallpaper_window->wid) {
|
||||
if (evt->buttons & YUTANI_MOUSE_BUTTON_RIGHT) {
|
||||
show_right_click_menu(evt->new_x, evt->new_y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main (int argc, char ** argv) {
|
||||
|
||||
if (argc < 2 || strcmp(argv[1],"--really")) {
|
||||
@ -70,6 +105,9 @@ int main (int argc, char ** argv) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
_rc_menu = menu_create();
|
||||
menu_insert(_rc_menu, menu_create_normal("utilities-terminal", "terminal", "Open Terminal", launch_application_menu));
|
||||
|
||||
/* wallpaper */
|
||||
wallpaper_window = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
|
||||
yutani_window_move(yctx, wallpaper_window, 0, 0);
|
||||
@ -82,28 +120,40 @@ int main (int argc, char ** argv) {
|
||||
int should_exit = 0;
|
||||
|
||||
while (!should_exit) {
|
||||
yutani_msg_t * m = yutani_poll(yctx);
|
||||
if (m) {
|
||||
switch (m->type) {
|
||||
case YUTANI_MSG_WELCOME:
|
||||
yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
|
||||
break;
|
||||
case YUTANI_MSG_RESIZE_OFFER:
|
||||
{
|
||||
struct yutani_msg_window_resize * wr = (void*)m->data;
|
||||
if (wr->wid == wallpaper_window->wid) {
|
||||
resize_finish_wallpaper(wr->width, wr->height);
|
||||
int fds[1] = {fileno(yctx->sock)};
|
||||
int index = fswait2(1,fds,200);
|
||||
if (index == 0) {
|
||||
yutani_msg_t * m = yutani_poll(yctx);
|
||||
while (m) {
|
||||
menu_process_event(yctx, m);
|
||||
switch (m->type) {
|
||||
case YUTANI_MSG_WELCOME:
|
||||
yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
|
||||
break;
|
||||
case YUTANI_MSG_RESIZE_OFFER:
|
||||
{
|
||||
struct yutani_msg_window_resize * wr = (void*)m->data;
|
||||
if (wr->wid == wallpaper_window->wid) {
|
||||
resize_finish_wallpaper(wr->width, wr->height);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case YUTANI_MSG_SESSION_END:
|
||||
should_exit = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
break;
|
||||
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
|
||||
check_click((struct yutani_msg_window_mouse_event *)m->data);
|
||||
break;
|
||||
case YUTANI_MSG_SESSION_END:
|
||||
should_exit = 1;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
free(m);
|
||||
m = yutani_poll_async(yctx);
|
||||
}
|
||||
} else {
|
||||
/* Perform timer events here. Animations? */
|
||||
waitpid(-1, NULL, WNOHANG);
|
||||
}
|
||||
free(m);
|
||||
}
|
||||
|
||||
yutani_close(yctx, wallpaper_window);
|
||||
|
Loading…
x
Reference in New Issue
Block a user