background.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * background - Draw a desktop wallpaper.
  7. *
  8. * TODO: This is a very minimal wallpaper renderer.
  9. * ToaruOS-mainline, before it went all Python,
  10. * included a more complete wallpaper application,
  11. * which supported icons and config files.
  12. */
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <signal.h>
  16. #include <sys/utsname.h>
  17. #include <sys/wait.h>
  18. #include <sys/fswait.h>
  19. #include <toaru/yutani.h>
  20. #include <toaru/graphics.h>
  21. #include <toaru/menu.h>
  22. static yutani_t * yctx;
  23. static yutani_window_t * wallpaper_window;
  24. static gfx_context_t * wallpaper_ctx;
  25. static sprite_t * wallpaper;
  26. static struct MenuList * _rc_menu = NULL;
  27. static void draw_background(int width, int height) {
  28. float x = (float)wallpaper_window->width / (float)wallpaper->width;
  29. float y = (float)wallpaper_window->height / (float)wallpaper->height;
  30. int nh = (int)(x * (float)wallpaper->height);
  31. int nw = (int)(y * (float)wallpaper->width);
  32. if (nw == wallpaper->width && nh == wallpaper->height) {
  33. // special case
  34. draw_sprite(wallpaper_ctx, wallpaper, 0, 0);
  35. } else if (nw >= width) {
  36. draw_sprite_scaled(wallpaper_ctx, wallpaper, ((int)wallpaper_window->width - nw) / 2, 0, nw+2, wallpaper_window->height);
  37. } else {
  38. draw_sprite_scaled(wallpaper_ctx, wallpaper, 0, ((int)wallpaper_window->height - nh) / 2, wallpaper_window->width+2, nh);
  39. }
  40. }
  41. static void show_right_click_menu(int x, int y) {
  42. if (_rc_menu->window) return; /* Already shown */
  43. menu_show(_rc_menu, yctx);
  44. if (x + _rc_menu->window->width > yctx->display_width) {
  45. yutani_window_move(yctx, _rc_menu->window, x - _rc_menu->window->width, y);
  46. } else {
  47. yutani_window_move(yctx, _rc_menu->window, x, y);
  48. }
  49. }
  50. static void resize_finish_wallpaper(int width, int height) {
  51. yutani_window_resize_accept(yctx, wallpaper_window, width, height);
  52. reinit_graphics_yutani(wallpaper_ctx, wallpaper_window);
  53. draw_background(width, height);
  54. yutani_window_resize_done(yctx, wallpaper_window);
  55. yutani_flip(yctx, wallpaper_window);
  56. }
  57. static void launch_application(char * app) {
  58. if (!fork()) {
  59. printf("Starting %s\n", app);
  60. char * args[] = {"/bin/sh", "-c", app, NULL};
  61. execvp(args[0], args);
  62. exit(1);
  63. }
  64. }
  65. static void launch_application_menu(struct MenuEntry * self) {
  66. struct MenuEntry_Normal * _self = (void *)self;
  67. launch_application((char *)_self->action);
  68. }
  69. static void check_click(struct yutani_msg_window_mouse_event * evt) {
  70. if (evt->wid == wallpaper_window->wid) {
  71. if (evt->buttons & YUTANI_MOUSE_BUTTON_RIGHT) {
  72. show_right_click_menu(evt->new_x, evt->new_y);
  73. }
  74. }
  75. }
  76. static void sig_usr2(int sig) {
  77. yutani_set_stack(yctx, wallpaper_window, YUTANI_ZORDER_BOTTOM);
  78. yutani_flip(yctx, wallpaper_window);
  79. }
  80. int main (int argc, char ** argv) {
  81. if (argc < 2 || strcmp(argv[1],"--really")) {
  82. fprintf(stderr,
  83. "%s: Desktop environment wallpaper\n"
  84. "\n"
  85. " Renders the desktop wallpaper. You probably don't want\n"
  86. " to be running this directly - it is started by the\n"
  87. " session manager along with the panel.\n", argv[0]);
  88. return 1;
  89. }
  90. signal(SIGUSR2, sig_usr2);
  91. wallpaper = malloc(sizeof(sprite_t));
  92. load_sprite(wallpaper, "/usr/share/wallpaper.bmp");
  93. wallpaper->alpha = 0;
  94. yctx = yutani_init();
  95. if (!yctx) {
  96. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  97. return 1;
  98. }
  99. _rc_menu = menu_create();
  100. menu_insert(_rc_menu, menu_create_normal("utilities-terminal", "terminal", "Open Terminal", launch_application_menu));
  101. /* wallpaper */
  102. wallpaper_window = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
  103. yutani_window_move(yctx, wallpaper_window, 0, 0);
  104. yutani_set_stack(yctx, wallpaper_window, YUTANI_ZORDER_BOTTOM);
  105. wallpaper_ctx = init_graphics_yutani(wallpaper_window);
  106. draw_background(yctx->display_width, yctx->display_height);
  107. yutani_flip(yctx, wallpaper_window);
  108. int should_exit = 0;
  109. while (!should_exit) {
  110. int fds[1] = {fileno(yctx->sock)};
  111. int index = fswait2(1,fds,200);
  112. if (index == 0) {
  113. yutani_msg_t * m = yutani_poll(yctx);
  114. while (m) {
  115. menu_process_event(yctx, m);
  116. switch (m->type) {
  117. case YUTANI_MSG_WELCOME:
  118. yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
  119. break;
  120. case YUTANI_MSG_RESIZE_OFFER:
  121. {
  122. struct yutani_msg_window_resize * wr = (void*)m->data;
  123. if (wr->wid == wallpaper_window->wid) {
  124. resize_finish_wallpaper(wr->width, wr->height);
  125. }
  126. }
  127. break;
  128. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  129. check_click((struct yutani_msg_window_mouse_event *)m->data);
  130. break;
  131. case YUTANI_MSG_SESSION_END:
  132. should_exit = 1;
  133. break;
  134. default:
  135. break;
  136. }
  137. free(m);
  138. m = yutani_poll_async(yctx);
  139. }
  140. } else {
  141. /* Perform timer events here. Animations? */
  142. waitpid(-1, NULL, WNOHANG);
  143. }
  144. }
  145. yutani_close(yctx, wallpaper_window);
  146. return 0;
  147. }