background.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <sys/utsname.h>
  16. #include <toaru/yutani.h>
  17. #include <toaru/graphics.h>
  18. static yutani_t * yctx;
  19. static yutani_window_t * wallpaper_window;
  20. static gfx_context_t * wallpaper_ctx;
  21. static sprite_t * wallpaper;
  22. static void draw_background(int width, int height) {
  23. float x = (float)wallpaper_window->width / (float)wallpaper->width;
  24. float y = (float)wallpaper_window->height / (float)wallpaper->height;
  25. int nh = (int)(x * (float)wallpaper->height);
  26. int nw = (int)(y * (float)wallpaper->width);
  27. if (nw == wallpaper->width && nh == wallpaper->height) {
  28. // special case
  29. draw_sprite(wallpaper_ctx, wallpaper, 0, 0);
  30. } else if (nw >= width) {
  31. draw_sprite_scaled(wallpaper_ctx, wallpaper, ((int)wallpaper_window->width - nw) / 2, 0, nw+2, wallpaper_window->height);
  32. } else {
  33. draw_sprite_scaled(wallpaper_ctx, wallpaper, 0, ((int)wallpaper_window->height - nh) / 2, wallpaper_window->width+2, nh);
  34. }
  35. }
  36. static void resize_finish_wallpaper(int width, int height) {
  37. yutani_window_resize_accept(yctx, wallpaper_window, width, height);
  38. reinit_graphics_yutani(wallpaper_ctx, wallpaper_window);
  39. draw_background(width, height);
  40. yutani_window_resize_done(yctx, wallpaper_window);
  41. yutani_flip(yctx, wallpaper_window);
  42. }
  43. int main (int argc, char ** argv) {
  44. if (argc < 2 || strcmp(argv[1],"--really")) {
  45. fprintf(stderr,
  46. "%s: Desktop environment wallpaper\n"
  47. "\n"
  48. " Renders the desktop wallpaper. You probably don't want\n"
  49. " to be running this directly - it is started by the\n"
  50. " session manager along with the panel.\n", argv[0]);
  51. return 1;
  52. }
  53. wallpaper = malloc(sizeof(sprite_t));
  54. load_sprite(wallpaper, "/usr/share/wallpaper.bmp");
  55. wallpaper->alpha = 0;
  56. yctx = yutani_init();
  57. if (!yctx) {
  58. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  59. return 1;
  60. }
  61. /* wallpaper */
  62. wallpaper_window = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
  63. yutani_window_move(yctx, wallpaper_window, 0, 0);
  64. yutani_set_stack(yctx, wallpaper_window, YUTANI_ZORDER_BOTTOM);
  65. wallpaper_ctx = init_graphics_yutani(wallpaper_window);
  66. draw_background(yctx->display_width, yctx->display_height);
  67. yutani_flip(yctx, wallpaper_window);
  68. int should_exit = 0;
  69. while (!should_exit) {
  70. yutani_msg_t * m = yutani_poll(yctx);
  71. if (m) {
  72. switch (m->type) {
  73. case YUTANI_MSG_WELCOME:
  74. yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
  75. break;
  76. case YUTANI_MSG_RESIZE_OFFER:
  77. {
  78. struct yutani_msg_window_resize * wr = (void*)m->data;
  79. if (wr->wid == wallpaper_window->wid) {
  80. resize_finish_wallpaper(wr->width, wr->height);
  81. }
  82. }
  83. break;
  84. case YUTANI_MSG_SESSION_END:
  85. should_exit = 1;
  86. break;
  87. default:
  88. break;
  89. }
  90. }
  91. free(m);
  92. }
  93. yutani_close(yctx, wallpaper_window);
  94. return 0;
  95. }