background.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. signal(SIGUSR2, sig_usr2);
  80. }
  81. int main (int argc, char ** argv) {
  82. if (argc < 2 || strcmp(argv[1],"--really")) {
  83. fprintf(stderr,
  84. "%s: Desktop environment wallpaper\n"
  85. "\n"
  86. " Renders the desktop wallpaper. You probably don't want\n"
  87. " to be running this directly - it is started by the\n"
  88. " session manager along with the panel.\n", argv[0]);
  89. return 1;
  90. }
  91. signal(SIGUSR2, sig_usr2);
  92. wallpaper = malloc(sizeof(sprite_t));
  93. load_sprite(wallpaper, "/usr/share/wallpaper.bmp");
  94. wallpaper->alpha = 0;
  95. yctx = yutani_init();
  96. if (!yctx) {
  97. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  98. return 1;
  99. }
  100. _rc_menu = menu_create();
  101. menu_insert(_rc_menu, menu_create_normal("utilities-terminal", "terminal", "Open Terminal", launch_application_menu));
  102. /* wallpaper */
  103. wallpaper_window = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
  104. yutani_window_move(yctx, wallpaper_window, 0, 0);
  105. yutani_set_stack(yctx, wallpaper_window, YUTANI_ZORDER_BOTTOM);
  106. wallpaper_ctx = init_graphics_yutani(wallpaper_window);
  107. draw_background(yctx->display_width, yctx->display_height);
  108. yutani_flip(yctx, wallpaper_window);
  109. int should_exit = 0;
  110. while (!should_exit) {
  111. int fds[1] = {fileno(yctx->sock)};
  112. int index = fswait2(1,fds,200);
  113. if (index == 0) {
  114. yutani_msg_t * m = yutani_poll(yctx);
  115. while (m) {
  116. menu_process_event(yctx, m);
  117. switch (m->type) {
  118. case YUTANI_MSG_WELCOME:
  119. yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
  120. break;
  121. case YUTANI_MSG_RESIZE_OFFER:
  122. {
  123. struct yutani_msg_window_resize * wr = (void*)m->data;
  124. if (wr->wid == wallpaper_window->wid) {
  125. resize_finish_wallpaper(wr->width, wr->height);
  126. }
  127. }
  128. break;
  129. case YUTANI_MSG_WINDOW_MOUSE_EVENT:
  130. check_click((struct yutani_msg_window_mouse_event *)m->data);
  131. break;
  132. case YUTANI_MSG_SESSION_END:
  133. should_exit = 1;
  134. break;
  135. default:
  136. break;
  137. }
  138. free(m);
  139. m = yutani_poll_async(yctx);
  140. }
  141. } else {
  142. /* Perform timer events here. Animations? */
  143. waitpid(-1, NULL, WNOHANG);
  144. }
  145. }
  146. yutani_close(yctx, wallpaper_window);
  147. return 0;
  148. }