toaruos/apps/about.c

224 lines
5.4 KiB
C
Raw Normal View History

2018-08-14 11:13:38 +03:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
2019-01-02 13:12:47 +03:00
* Copyright (C) 2018-2019 K. Lange
2018-08-14 11:13:38 +03:00
*
* about - Show an "About <Application>" dialog.
*
* By default, shows "About ToaruOS", suitable for use as an application
* menu entry. Optionally, takes arguments specifying another application
* to describe, suitable for the "Help > About" menu bar entry.
*/
2018-04-21 14:55:23 +03:00
#include <toaru/yutani.h>
#include <toaru/graphics.h>
#include <toaru/decorations.h>
#include <toaru/sdf.h>
#include <toaru/menu.h>
2018-04-21 14:55:23 +03:00
#include <sys/utsname.h>
static yutani_t * yctx;
static yutani_window_t * window = NULL;
static gfx_context_t * ctx = NULL;
static sprite_t logo;
static int32_t width = 350;
static int32_t height = 250;
static char * version_str;
2018-06-04 06:35:46 +03:00
static char * icon_path;
static char * title_str;
static char * version_str;
static char * copyright_str[20] = {NULL};
2018-04-21 14:55:23 +03:00
static int center_x(int x) {
return (width - x) / 2;
}
static void draw_string(int y, const char * string, int font, uint32_t color) {
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
draw_sdf_string(ctx, bounds.left_width + center_x(draw_sdf_string_width(string, 16, font)), bounds.top_height + 10 + logo.height + 10 + y, string, 16, color, font);
2018-04-21 14:55:23 +03:00
}
static void redraw(void) {
struct decor_bounds bounds;
decor_get_bounds(window, &bounds);
2018-04-21 14:55:23 +03:00
draw_fill(ctx, rgb(204,204,204));
draw_sprite(ctx, &logo, bounds.left_width + center_x(logo.width), bounds.top_height + 10);
2018-04-21 14:55:23 +03:00
2018-06-04 06:35:46 +03:00
draw_string(0, version_str, SDF_FONT_BOLD, rgb(0,0,0));
int offset = 20;
for (char ** copy_str = copyright_str; *copy_str; ++copy_str) {
if (**copy_str == '-') {
offset += 10;
} else if (**copy_str == '%') {
draw_string(offset, *copy_str+1, SDF_FONT_THIN, rgb(0,0,255));
offset += 20;
} else {
draw_string(offset, *copy_str, SDF_FONT_THIN, rgb(0,0,0));
offset += 20;
}
}
2018-04-21 14:55:23 +03:00
window->decorator_flags |= DECOR_FLAG_NO_MAXIMIZE;
2018-06-04 06:35:46 +03:00
render_decorations(window, ctx, title_str);
2018-04-21 14:55:23 +03:00
flip(ctx);
yutani_flip(yctx, window);
}
2018-06-04 06:35:46 +03:00
static void init_default(void) {
2018-10-06 15:28:43 +03:00
title_str = "About ToaruOS";
2018-06-04 06:35:46 +03:00
icon_path = "/usr/share/logo_login.bmp";
2018-04-21 14:55:23 +03:00
2018-06-04 06:35:46 +03:00
{
version_str = malloc(100);
struct utsname u;
uname(&u);
char * tmp = strstr(u.release, "-");
if (tmp) {
*tmp = '\0';
}
2018-10-06 15:28:43 +03:00
sprintf(version_str, "ToaruOS %s", u.release);
2018-04-21 14:55:23 +03:00
}
2019-01-02 13:12:47 +03:00
copyright_str[0] = "(C) 2011-2019 K. Lange, et al.";
2018-06-04 06:35:46 +03:00
copyright_str[1] = "-";
copyright_str[2] = "ToaruOS is free software released under the";
copyright_str[3] = "NCSA/University of Illinois license.";
copyright_str[4] = "-";
copyright_str[5] = "%https://toaruos.org";
copyright_str[6] = "%https://github.com/klange/toaruos";
2018-06-04 06:35:46 +03:00
}
int main(int argc, char * argv[]) {
int req_center_x, req_center_y;
2018-04-21 14:55:23 +03:00
yctx = yutani_init();
if (!yctx) {
fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
return 1;
}
2018-04-21 14:55:23 +03:00
init_decorations();
struct decor_bounds bounds;
decor_get_bounds(NULL, &bounds);
window = yutani_window_create(yctx, width + bounds.width, height + bounds.height);
req_center_x = yctx->display_width / 2;
req_center_y = yctx->display_height / 2;
2018-04-21 14:55:23 +03:00
2018-06-04 06:35:46 +03:00
if (argc < 2) {
init_default();
} else if (argc < 5) {
fprintf(stderr, "Invalid arguments.\n");
return 1;
} else {
title_str = argv[1];
icon_path = argv[2];
version_str = argv[3];
int i = 0;
char * me = argv[4], * end;
do {
copyright_str[i] = me;
i++;
end = strchr(me,'\n');
if (end) {
*end = '\0';
me = end+1;
}
} while (end);
if (argc > 6) {
req_center_x = atoi(argv[5]);
req_center_y = atoi(argv[6]);
}
2018-06-04 06:35:46 +03:00
}
yutani_window_move(yctx, window, req_center_x - window->width / 2, req_center_y - window->height / 2);
2018-06-04 06:35:46 +03:00
yutani_window_advertise_icon(yctx, window, title_str, "star");
2018-04-21 14:55:23 +03:00
ctx = init_graphics_yutani_double_buffer(window);
2018-06-04 06:35:46 +03:00
load_sprite(&logo, icon_path);
2018-04-21 14:55:23 +03:00
logo.alpha = ALPHA_EMBEDDED;
redraw();
int playing = 1;
while (playing) {
yutani_msg_t * m = yutani_poll(yctx);
while (m) {
if (menu_process_event(yctx, m)) {
redraw();
}
2018-04-21 14:55:23 +03:00
switch (m->type) {
case YUTANI_MSG_KEY_EVENT:
{
struct yutani_msg_key_event * ke = (void*)m->data;
if (ke->event.action == KEY_ACTION_DOWN && ke->event.keycode == 'q') {
playing = 0;
}
}
break;
case YUTANI_MSG_WINDOW_FOCUS_CHANGE:
{
struct yutani_msg_window_focus_change * wf = (void*)m->data;
yutani_window_t * win = hashmap_get(yctx->windows, (void*)wf->wid);
if (win) {
win->focused = wf->focused;
redraw();
}
}
break;
#if 0
case YUTANI_MSG_RESIZE_OFFER:
{
struct yutani_msg_window_resize * wr = (void*)m->data;
resize_finish(wr->width, wr->height);
}
break;
#endif
case YUTANI_MSG_WINDOW_MOUSE_EVENT:
{
struct yutani_msg_window_mouse_event * me = (void*)m->data;
2018-04-21 14:55:23 +03:00
int result = decor_handle_event(yctx, m);
switch (result) {
case DECOR_CLOSE:
playing = 0;
break;
case DECOR_RIGHT:
/* right click in decoration, show appropriate menu */
decor_show_default_menu(window, window->x + me->new_x, window->y + me->new_y);
break;
2018-04-21 14:55:23 +03:00
default:
/* Other actions */
break;
}
}
break;
case YUTANI_MSG_WINDOW_CLOSE:
2018-04-21 14:55:23 +03:00
case YUTANI_MSG_SESSION_END:
playing = 0;
break;
default:
break;
}
free(m);
m = yutani_poll_async(yctx);
2018-04-21 14:55:23 +03:00
}
}
yutani_close(yctx, window);
return 0;
}