xref: /haiku/src/apps/screenshot/Screenshot.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright Karsten Heimrich, host.haiku@gmx.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Karsten Heimrich
7  *		Fredrik Modéen
8  */
9 
10 
11 #include "Screenshot.h"
12 
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
18 
19 #include <TranslatorFormats.h>
20 
21 
22 #include "ScreenshotWindow.h"
23 
24 
25 Screenshot::Screenshot()
26 	:
27 	BApplication("application/x-vnd.Haiku-Screenshot"),
28 	fArgvReceived(false),
29 	fRefsReceived(false),
30 	fImageFileType(B_PNG_FORMAT),
31 	fTranslator(8)
32 {
33 }
34 
35 
36 Screenshot::~Screenshot()
37 {
38 }
39 
40 
41 void
42 Screenshot::ReadyToRun()
43 {
44 	if (!fArgvReceived && !fRefsReceived)
45 		new ScreenshotWindow();
46 
47 	fArgvReceived = false;
48 	fRefsReceived = false;
49 }
50 
51 
52 void
53 Screenshot::RefsReceived(BMessage* message)
54 {
55 	int32 delay = 0;
56 	message->FindInt32("delay", &delay);
57 
58 	bool includeBorder = false;
59 	message->FindBool("border", &includeBorder);
60 
61 	bool includeMouse = false;
62 	message->FindBool("border", &includeMouse);
63 
64 	bool grabActiveWindow = false;
65 	message->FindBool("window", &grabActiveWindow);
66 
67 	bool saveScreenshotSilent = false;
68 	message->FindBool("silent", &saveScreenshotSilent);
69 
70 	bool showConfigureWindow = false;
71 	message->FindBool("configure", &showConfigureWindow);
72 
73 	new ScreenshotWindow(delay * 1000000, includeBorder, includeMouse,
74 		grabActiveWindow, showConfigureWindow, saveScreenshotSilent);
75 
76 	fRefsReceived = true;
77 }
78 
79 
80 void
81 Screenshot::ArgvReceived(int32 argc, char** argv)
82 {
83 	bigtime_t delay = 0;
84 
85 	bool includeBorder = false;
86 	bool includeMouse = false;
87 	bool grabActiveWindow = false;
88 	bool showConfigureWindow = false;
89 	bool saveScreenshotSilent = false;
90 
91 	for (int32 i = 0; i < argc; i++) {
92 		if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
93 			_ShowHelp();
94 		else if (strcmp(argv[i], "-b") == 0
95 			|| strcmp(argv[i], "--border") == 0)
96 			includeBorder = true;
97 		else if (strcmp(argv[i], "-m") == 0
98 			|| strcmp(argv[i], "--mouse-pointer") == 0)
99 			includeMouse = true;
100 		else if (strcmp(argv[i], "-w") == 0
101 			|| strcmp(argv[i], "--window") == 0)
102 			grabActiveWindow = true;
103 		else if (strcmp(argv[i], "-s") == 0
104 			|| strcmp(argv[i], "--silent") == 0)
105 			saveScreenshotSilent = true;
106 		else if (strcmp(argv[i], "-o") == 0
107 			|| strcmp(argv[i], "--options") == 0)
108 			showConfigureWindow = true;
109 		else if (strcmp(argv[i], "-f") == 0
110 			|| strncmp(argv[i], "--format", 6) == 0
111 			|| strncmp(argv[i], "--format=", 7) == 0) {
112 			_SetImageTypeSilence(argv[i + 1]);
113 		} else if (strcmp(argv[i], "-d") == 0
114 			|| strncmp(argv[i], "--delay", 7) == 0
115 			|| strncmp(argv[i], "--delay=", 8) == 0) {
116 			int32 seconds = -1;
117 			if (argc > i + 1)
118 				seconds = atoi(argv[i + 1]);
119 			if (seconds >= 0) {
120 				delay = seconds * 1000000;
121 				i++;
122 			} else {
123 				printf("Screenshot: option requires an argument -- %s\n"
124 					, argv[i]);
125 				exit(0);
126 			}
127 		}
128 	}
129 
130 	fArgvReceived = true;
131 
132 	new ScreenshotWindow(delay, includeBorder, includeMouse, grabActiveWindow,
133 		showConfigureWindow, saveScreenshotSilent, fImageFileType,
134 		fTranslator);
135 }
136 
137 
138 void
139 Screenshot::_ShowHelp() const
140 {
141 	printf("Screenshot [OPTION]... Creates a bitmap of the current screen\n\n");
142 	printf("OPTION\n");
143 	printf("  -o, --options         Show options window first\n");
144 	printf("  -m, --mouse-pointer   Include the mouse pointer\n");
145 	printf("  -b, --border          Include the window border\n");
146 	printf("  -w, --window          Capture the active window instead of the entire screen\n");
147 	printf("  -d, --delay=seconds   Take screenshot after specified delay [in seconds]\n");
148 	printf("  -s, --silent          Saves the screenshot without showing the app window\n");
149 	printf("                        overrides --options\n");
150 	printf("  -f, --format=image	Write the image format you like to save as\n");
151 	printf("                        [bmp], [gif], [jpg], [png], [ppm], [targa], [tiff]\n");
152 	printf("\n");
153 	printf("Note: OPTION -b, --border takes only effect when used with -w, --window\n");
154 	exit(0);
155 }
156 
157 
158 void
159 Screenshot::_SetImageTypeSilence(const char* name)
160 {
161 	if (strcmp(name, "bmp") == 0) {
162 		fImageFileType = B_BMP_FORMAT;
163 		fTranslator = 1;
164 	} else if (strcmp(name, "gif") == 0) {
165 		fImageFileType = B_GIF_FORMAT;
166 		fTranslator = 3;
167 	} else if (strcmp(name, "jpg") == 0) {
168 		fImageFileType = B_JPEG_FORMAT;
169 		fTranslator = 6;
170 	} else if (strcmp(name, "ppm") == 0) {
171 		fImageFileType = B_PPM_FORMAT;
172 		fTranslator = 9;
173 	} else if (strcmp(name, "targa") == 0) {
174 		fImageFileType = B_TGA_FORMAT;
175 		fTranslator = 14;
176 	} else if (strcmp(name, "tif") == 0) {
177 		fImageFileType = B_TIFF_FORMAT;
178 		fTranslator = 15;
179 	} else { //png
180 		fImageFileType = B_PNG_FORMAT;
181 		fTranslator = 8;
182 	}
183 }
184