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