background.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/utsname.h>
  4. #include <toaru/yutani.h>
  5. #include <toaru/graphics.h>
  6. #include <toaru/drawstring.h>
  7. #define PANEL_HEIGHT 24
  8. static yutani_t * yctx;
  9. static yutani_window_t * wallpaper_window;
  10. static gfx_context_t * wallpaper_ctx;
  11. static yutani_window_t * panel_window;
  12. static gfx_context_t * panel_ctx;
  13. static sprite_t * wallpaper;
  14. static void draw_background(int width, int height) {
  15. float x = (float)wallpaper_window->width / (float)wallpaper->width;
  16. float y = (float)wallpaper_window->height / (float)wallpaper->height;
  17. int nh = (int)(x * (float)wallpaper->height);
  18. int nw = (int)(y * (float)wallpaper->width);
  19. if (nw == wallpaper->width && nh == wallpaper->height) {
  20. // special case
  21. draw_sprite(wallpaper_ctx, wallpaper, 0, 0);
  22. } else if (nw >= width) {
  23. draw_sprite_scaled(wallpaper_ctx, wallpaper, ((int)wallpaper_window->width - nw) / 2, 0, nw, wallpaper_window->height);
  24. } else {
  25. draw_sprite_scaled(wallpaper_ctx, wallpaper, 0, ((int)wallpaper_window->height - nh) / 2, wallpaper_window->width, nh);
  26. }
  27. }
  28. static void draw_panel(int width) {
  29. char label[100];
  30. struct utsname u;
  31. uname(&u);
  32. sprintf(label, "ToaruOS-NIH %s", u.release);
  33. draw_fill(panel_ctx, rgba(0,0,0,170));
  34. draw_string(panel_ctx, 1, 2, rgb(255,255,255), label);
  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. static void resize_finish_panel(int width, int height) {
  44. yutani_window_resize_accept(yctx, panel_window, width, height);
  45. reinit_graphics_yutani(panel_ctx, panel_window);
  46. draw_panel(width);
  47. yutani_window_resize_done(yctx, panel_window);
  48. yutani_flip(yctx, panel_window);
  49. }
  50. static void launch_application(char * app) {
  51. if (!fork()) {
  52. printf("Starting %s\n", app);
  53. char * args[] = {"/bin/sh", "-c", app, NULL};
  54. execvp(args[0], args);
  55. exit(1);
  56. }
  57. }
  58. static void handle_key_event(struct yutani_msg_key_event * ke) {
  59. if ((ke->event.modifiers & KEY_MOD_LEFT_CTRL) &&
  60. (ke->event.modifiers & KEY_MOD_LEFT_ALT) &&
  61. (ke->event.keycode == 't') &&
  62. (ke->event.action == KEY_ACTION_DOWN)) {
  63. launch_application("terminal");
  64. }
  65. }
  66. int main (int argc, char ** argv) {
  67. wallpaper = malloc(sizeof(sprite_t));
  68. load_sprite(wallpaper, "/usr/share/wallpaper.bmp");
  69. wallpaper->alpha = 0;
  70. yctx = yutani_init();
  71. /* wallpaper */
  72. wallpaper_window = yutani_window_create(yctx, yctx->display_width, yctx->display_height);
  73. yutani_window_move(yctx, wallpaper_window, 0, 0);
  74. yutani_set_stack(yctx, wallpaper_window, YUTANI_ZORDER_BOTTOM);
  75. wallpaper_ctx = init_graphics_yutani(wallpaper_window);
  76. draw_background(yctx->display_width, yctx->display_height);
  77. yutani_flip(yctx, wallpaper_window);
  78. /* panel */
  79. panel_window = yutani_window_create(yctx, yctx->display_width, PANEL_HEIGHT);
  80. yutani_window_move(yctx, panel_window, 0, 0);
  81. yutani_set_stack(yctx, panel_window, YUTANI_ZORDER_TOP);
  82. panel_ctx = init_graphics_yutani(panel_window);
  83. draw_panel(yctx->display_width);
  84. yutani_flip(yctx, panel_window);
  85. int should_exit = 0;
  86. yutani_key_bind(yctx, 't', KEY_MOD_LEFT_CTRL | KEY_MOD_LEFT_ALT, YUTANI_BIND_STEAL);
  87. int _terminal_pid = fork();
  88. if (!_terminal_pid) {
  89. char * args[] = {"/bin/terminal", NULL};
  90. execvp(args[0], args);
  91. }
  92. while (!should_exit) {
  93. yutani_msg_t * m = yutani_poll(yctx);
  94. if (m) {
  95. switch (m->type) {
  96. case YUTANI_MSG_WELCOME:
  97. fprintf(stderr, "Request to resize desktop received, resizing to %d x %d\n", yctx->display_width, yctx->display_height);
  98. yutani_window_resize_offer(yctx, wallpaper_window, yctx->display_width, yctx->display_height);
  99. yutani_window_resize_offer(yctx, panel_window, yctx->display_width, PANEL_HEIGHT);
  100. break;
  101. case YUTANI_MSG_KEY_EVENT:
  102. handle_key_event((struct yutani_msg_key_event *)m->data);
  103. break;
  104. case YUTANI_MSG_RESIZE_OFFER:
  105. {
  106. struct yutani_msg_window_resize * wr = (void*)m->data;
  107. if (wr->wid == wallpaper_window->wid) {
  108. resize_finish_wallpaper(wr->width, wr->height);
  109. } else if (wr->wid == panel_window->wid) {
  110. resize_finish_panel(wr->width, wr->height);
  111. }
  112. }
  113. break;
  114. case YUTANI_MSG_SESSION_END:
  115. should_exit = 1;
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. free(m);
  122. }
  123. yutani_close(yctx, wallpaper_window);
  124. return 0;
  125. }