showmessage.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2019 Clementine Computing LLC.
  3. *
  4. * This file is part of PopuFare.
  5. *
  6. * PopuFare is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * PopuFare is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with PopuFare. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <time.h>
  24. #include <string.h>
  25. #include "fbutil.h"
  26. void usage(char *progname)
  27. {
  28. fprintf(stderr, "Usage:\n");
  29. fprintf(stderr, "\t%s [-x nnn] [-y nnn] [-f 0|1|2] [-o] [[-m] \"message to display\"]\n", progname);
  30. fprintf(stderr, "\n");
  31. fprintf(stderr, "\t-x nnn\t Specifies the X coordinate to start drawing the message at.\n\t \t(Defaults to centered if none is specified).\n");
  32. fprintf(stderr, "\t-y nnn\t Specifies the Y coordinate to start drawing the message at.\n\t \t(Defaults to centered if none is specified).\n");
  33. fprintf(stderr, "\t-f n \t Specifies the font to draw the message with.\n\t \t(Defaults to 2 [0 = tiny, 1 = medium, 2 = large]).\n");
  34. fprintf(stderr, "\t-o \t Specifies that we want to draw the message over the current screen without clearing.\n");
  35. fprintf(stderr, "\t-m msg\t Specifies that the next parameter is the message, even if it starts with a '-'.\n");
  36. fprintf(stderr, "\t-b \t Specifies that we want the text to pop up in a translucent box all fancy like.\n");
  37. fprintf(stderr, "Any arguments after the message are ignored. If no message is specified, a blank message is displayed.\n\n");
  38. exit(1);
  39. }
  40. int x = 0, y = 0, width = 0, height = 0;
  41. int font = 2;
  42. char *str = NULL;
  43. int flag_bits = 0;
  44. enum
  45. {
  46. FLAG_X = 0,
  47. FLAG_Y,
  48. FLAG_F,
  49. FLAG_O,
  50. FLAG_MSG,
  51. FLAG_BOX,
  52. NUM_FLAGS
  53. };
  54. #define FLAG_BIT(x) (1 << (x))
  55. #define ADD_FLAG(x) (flag_bits |= FLAG_BIT(x))
  56. #define CLEAR_FLAG(x) (flag_bits &= ~FLAG_BIT(x))
  57. #define TEST_FLAG(x) (flag_bits & FLAG_BIT(x) )
  58. char flag_chars[NUM_FLAGS] = {'x', 'y', 'f', 'o' , 'm', 'b'}; //What character following a - constitutes this flag
  59. char flag_param_types[NUM_FLAGS] = {'i', 'i', 'i', '\0', 's', '\0'}; //What parameter type does this flag take:
  60. //i = (int) s = (char *)
  61. void *flag_vars[NUM_FLAGS] = {&x, &y, &font, NULL, &str, NULL}; //Okay, now for an array of pointers as spec'd above. NULL means no param.
  62. void parse_command_line(int argc, char **argv)
  63. {
  64. int i, j;
  65. for(i = 1; i < argc; i++)
  66. {
  67. if(argv[i][0] != '-')
  68. {
  69. if(TEST_FLAG(FLAG_MSG))
  70. {
  71. break;
  72. }
  73. str = argv[i];
  74. i++;
  75. break;
  76. }
  77. else
  78. {
  79. for(j = 0; j < NUM_FLAGS; j++) //For all the flags we have...
  80. {
  81. if(argv[i][1] == flag_chars[j]) //If the current argument (i) matches the current flag (j)
  82. {
  83. if(TEST_FLAG(j)) //See if we already've got one...
  84. {
  85. fprintf(stderr, "-%c specified more than once!\n", flag_chars[j]);
  86. usage(argv[0]);
  87. exit(1);
  88. }
  89. ADD_FLAG(j); //Mark us as having this flag (j)
  90. //Make sure we are expecting a parameter
  91. if(flag_vars[j] != NULL)
  92. {
  93. if( (i + 1) >= argc )
  94. {
  95. fprintf(stderr, "Flag -%c expects a parameter, but none was specified!\n", flag_chars[j]);
  96. usage(argv[0]);
  97. exit(1);
  98. }
  99. else
  100. {
  101. i++;
  102. }
  103. switch(flag_param_types[j])
  104. {
  105. case 'i': //for an integer, convert it
  106. if((argv[i][0] < '0') || (argv[i][0] > '9'))
  107. {
  108. fprintf(stderr, "Flag -%c expects a numeric argument but got \"%s\" instead!\n", flag_chars[j], argv[i]);
  109. usage(argv[0]);
  110. exit(1);
  111. break;
  112. }
  113. *((int *)flag_vars[j]) = (int)strtol(argv[i], NULL, 10);
  114. break;
  115. case 's': //for a string, assign the string pointer of our argv[i] to *param such that
  116. *((char **)flag_vars[j]) = argv[i]; //**flag_vars[j] points to the same string as argv[i]
  117. break;
  118. default:
  119. fprintf(stderr, "Flag -%c expects a parameter of unknown type!\n", flag_chars[j]);
  120. usage(argv[0]);
  121. exit(1);
  122. break;
  123. } //switch param type
  124. } //if flag takes param
  125. break;
  126. } //if flag match
  127. } //loop through out flag list (for j)
  128. if(j == NUM_FLAGS)
  129. {
  130. fprintf(stderr, "Unknown flag: \"%s\"!\n", argv[i]);
  131. usage(argv[0]);
  132. exit(1);
  133. }
  134. } //else (we are not processing a non-flag)
  135. } //loop though arguments (for i)
  136. if(str == NULL)
  137. {
  138. fprintf(stderr, "No message specified, using blank message \"\"\n");
  139. str = "";
  140. }
  141. if(i < argc)
  142. {
  143. fprintf(stderr, "Warning: Ignored the last %d parameters:", argc - i);
  144. for(j = i; j < argc; j++)
  145. {
  146. fprintf(stderr, " \"%s\"", argv[j]);
  147. }
  148. fprintf(stderr, "\n");
  149. }
  150. }
  151. int main(int argc, char **argv)
  152. {
  153. parse_command_line(argc, argv);
  154. if(open_framebuffer_nondestructive())
  155. {
  156. fprintf(stderr,"Cannot open framebuffer!\n");
  157. }
  158. set_font(font);
  159. font_cell_size(&width, &height);
  160. width *= strlen(str);
  161. if(!TEST_FLAG(FLAG_X))
  162. {
  163. x = (XRES - width) / 2;
  164. }
  165. if(!TEST_FLAG(FLAG_Y))
  166. {
  167. y = (YRES - height) / 2;
  168. }
  169. if(!TEST_FLAG(FLAG_O))
  170. {
  171. set_color(255,255,255);
  172. cls();
  173. }
  174. if(TEST_FLAG(FLAG_BOX))
  175. {
  176. //Shadow
  177. set_rawcolor(FBCOLOR_TRANSLUCENT | FBCOLOR_DKGRAY);
  178. set_pos(x - 8, y - 8);
  179. box_fill(x + width + 24, y + height + 24);
  180. //Dialog Body
  181. set_rawcolor(FBCOLOR_LTCYAN);
  182. set_pos(x - 16, y - 16);
  183. box_fill(x + width + 16, y + height + 16);
  184. //Dialog Outline
  185. set_rawcolor(FBCOLOR_BLACK);
  186. set_pos(x - 16, y - 16);
  187. box(x + width + 16, y + height + 16);
  188. }
  189. set_color(0,0,0);
  190. set_pos(x,y);
  191. print_string(str, 0);
  192. present_framebuffer();
  193. close_framebuffer();
  194. return 0;
  195. }