winapi_fixup_statements.pm 9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Copyright 1999, 2000, 2001 Patrik Stridvall
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18
#

19 20 21 22 23 24 25 26 27
package winapi_fixup_statements;

use strict;

use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;

@ISA = qw(Exporter);
@EXPORT = qw();
28
@EXPORT_OK = qw(fixup_statements);
29

30
use config qw($wine_dir);
31 32 33 34
use options qw($options);
use output qw($output);

use c_parser;
35
use winapi_module_user qw(
36 37 38
    get_message_result_kind
    get_message_wparam_kind
    get_message_lparam_kind
39 40 41 42
);

########################################################################
# fixup_function_call
43

44
sub fixup_function_call($$) {
45 46 47 48 49 50
    my $name = shift;
    my @arguments = @{(shift)};;

    return "$name(" . join(", ", @arguments) . ")";
}

51 52 53
########################################################################
# _parse_makelong

54
sub _parse_makelong($) {
55
    local $_ = shift;
56 57 58

    my $low;
    my $high;
59 60 61 62 63 64 65 66 67 68 69

    my $name;
    my @arguments;
    my @argument_lines;
    my @argument_columns;

    my $parser = new c_parser;

    my $line = 1;
    my $column = 0;
    if($parser->parse_c_function_call(\$_, \$line, \$column, \$name, \@arguments, \@argument_lines, \@argument_columns) &&
70
       $name =~ /^MAKE(?:LONG|LPARAM|LRESULT|WPARAM)$/)
71
    {
72 73 74
	$low = $arguments[0];
	$high = $arguments[1];
    } elsif(/^(?:\(\w+\)\s*)?0L?$/) {
75 76 77
	$low = "0";
	$high = "0";
    } else {
78 79
	$low = "($_) & 0xffff";
	$high = "($_) << 16";
80 81
    }

82 83 84
    $low =~ s/^\s*(.*?)\s*$/$1/;
    $high =~ s/^\s*(.*?)\s*$/$1/;

85 86 87
    return ($low, $high);
}

88 89 90
########################################################################
# fixup_function_call_2_windowsx

91
sub fixup_user_message_2_windowsx($$) {
92 93 94
    my $name = shift;
    (my $hwnd, my $msg, my $wparam, my $lparam) = @{(shift)};

95 96 97
    if($msg !~ /^WM_/) {
	return undef;
    } elsif($msg =~ /^(?:WM_BEGINDRAG|WM_ENTERMENULOOP|WM_EXITMENULOOP|WM_HELP|
98
		       WM_ISACTIVEICON|WM_LBTRACKPOINT|WM_NEXTMENU)$/x)
99 100
    {
	return undef;
101 102
    } elsif($msg =~ /^WM_(?:GET|SET)TEXT$/) {
	return undef;
103 104 105 106 107 108 109 110 111 112
    }

    my $suffix;
    $name =~ /([AW])?$/;
    if(defined($1)) {
	$suffix = $1;
    } else {
	$suffix = "";
    }

113 114
    $wparam =~ s/^\(WPARAM\)\s*//;
    $lparam =~ s/^\(LPARAM\)\s*//;
115 116

    my @arguments;
117
    if($msg =~ /^WM_COMMAND$/) {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	(my $id, my $code_notify) = _parse_makelong($wparam);
	my $hwndctl = $lparam;
	@arguments = ($id, $hwndctl, $code_notify);
    } elsif($msg =~ /^WM_(?:COPY|CUT|PASTE)$/) {
	@arguments = ();
    } elsif($msg =~ /^WM_(?:CHARTO|VKEYTO)ITEM$/) {
	(my $key, my $caret) = _parse_makelong($wparam);
	my $hwndctl = $lparam;
	@arguments = ($key, $hwndctl, $caret);
    } elsif($msg =~ /^WM_(?:COMPARE|DELETE|DRAW|MEASURE)ITEM$/) {
	@arguments = ($lparam);
    } elsif($msg =~ s/^WM_GETTEXT$/$&$suffix/) {
	@arguments = ($wparam, $lparam);
    } elsif($msg =~ /^WM_INITMENU$/) {
	my $hmenu = $wparam;
	@arguments =  ($hmenu);
    } elsif($msg =~ /^WM_INITMENUPOPUP$/) {
	my $hmenu = $wparam;
	(my $item, my $system_menu) = _parse_makelong($lparam);
	@arguments =  ($hmenu, $item, $system_menu);
    } elsif($msg =~ /^WM_MENUCHAR$/) {
	(my $ch, my $flags) = _parse_makelong($wparam);
	my $hmenu = $lparam;
	@arguments = ($ch, $flags, $hmenu);
    } elsif($msg =~ /^WM_MENUSELECT$/) {
	(my $item, my $flags) = _parse_makelong($wparam);
	my $hmenu = $lparam;
	my $hmenu_popup = "NULL"; # FIXME: Is this really correct?
	@arguments = ($hmenu, $item, $hmenu_popup, $flags);
    } elsif($msg =~ s/^WM_(NC)?LBUTTONDBLCLK$/WM_$1LBUTTONDOWN/) {
	my $double_click = "TRUE";
	my $key_flags = $wparam;
	(my $x, my $y) = _parse_makelong($lparam);
	@arguments = ($double_click, $x, $y, $key_flags);
    } elsif($msg =~ /^WM_(NC)?LBUTTONDOWN$/) {
	my $double_click = "FALSE";
	my $key_flags = $wparam;
	(my $x, my $y) = _parse_makelong($lparam);
	@arguments = ($double_click, $x, $y, $key_flags);
    } elsif($msg =~ /^WM_LBUTTONUP$/) {
	my $key_flags = $wparam;
	(my $x, my $y) = _parse_makelong($lparam);
	@arguments = ($x, $y, $key_flags);
    } elsif($msg =~ /^WM_SETCURSOR$/) {
	my $hwnd_cursor = $wparam;
	(my $code_hit_test, my $msg2) = _parse_makelong($lparam);
	@arguments = ($hwnd_cursor, $code_hit_test, $msg2);
    } elsif($msg =~ s/^WM_SETTEXT$/$&$suffix/) {
	my $text = $lparam;
	@arguments = ($text);
    } elsif($msg =~ /^WM_(?:SYS)?KEYDOWN$/) {
	my $vk = $wparam;
	(my $repeat, my $flags) = _parse_makelong($lparam);
	@arguments = ($vk, $repeat, $flags);
    } else {
	@arguments = ($wparam, $lparam);
    }
    unshift @arguments, $hwnd;

    return "FORWARD_" . $msg . "(" . join(", ", @arguments) . ", $name)";
}

180
########################################################################
181
# _get_messages
182

183
sub _get_messages($) {
184 185
    local $_ = shift;

186 187 188 189 190 191 192
    if(/^(?:BM|CB|EM|LB|STM|WM)_\w+(.*?)$/) {
	if(!$1) {
	    return ($_);
	} else {
	    return ();
	}
    } elsif(/^(.*?)\s*\?\s*((?:BM|CB|EM|LB|STM|WM)_\w+)\s*:\s*((?:BM|CB|EM|LB|STM|WM)_\w+)$/) {
193 194 195
	return ($2, $3);
    } elsif(/^\w+$/) {
	return ();
196 197
    } elsif(/^RegisterWindowMessage[AW]\s*\(.*?\)$/) {
	return ();
198
    } else {
199 200
	$output->write("warning: _get_messages: '$_'\n");
	return ();
201 202 203 204 205 206
    }
}

########################################################################
# _fixup_user_message

207
sub _fixup_user_message($$) {
208 209 210 211 212 213 214 215
    my $name = shift;
    (my $hwnd, my $msg, my $wparam, my $lparam) = @{(shift)};

    my $modified = 0;

    my $wkind;
    my $lkind;
    foreach my $msg (_get_messages($msg)) {
216
	my $new_wkind = get_message_wparam_kind($msg);
217 218 219 220 221 222
	if(defined($wkind) && $new_wkind ne $wkind) {
	    $output->write("messsages used together do not have the same type\n");
	} else {
	    $wkind = $new_wkind;
	}

223
	my $new_lkind = get_message_lparam_kind($msg);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	if(defined($lkind) && $new_lkind ne $lkind) {
	    $output->write("messsages used together do not have the same type\n");
	} else {
	    $lkind = $new_lkind;
	}
    }

    my @entries = (
	[ \$wparam, $wkind, "W", "w" ],
	[ \$lparam, $lkind, "L", "l" ]
    );
    foreach my $entry (@entries) {
	(my $refparam, my $kind, my $upper, my $lower) = @$entry;

	if(!defined($kind)) {
	    if($msg =~ /^WM_/) {
240 241 242
		$output->write("messsage $msg not properly defined\n");
		$modified = 0;
		last;
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	    }
	} elsif($kind eq "ptr") {
	    if($$refparam =~ /^(\(${upper}PARAM\))?\s*($lower[pP]aram)$/) {
		if(defined($1)) {
		    $$refparam = $2;
		    $modified = 1;
		}
	    } elsif($$refparam =~ /^(\(${upper}PARAM\))?\s*0$/) {
	        $$refparam = "(${upper}PARAM) NULL";
		$modified = 1;
	    } elsif($$refparam !~ /^\(${upper}PARAM\)\s*/) {
                $$refparam = "(${upper}PARAM) $$refparam";
		$modified = 1;
	    }
	} elsif($kind eq "long") {
	    if($$refparam =~ s/^\(${upper}PARAM\)\s*//) {
		$modified = 1;
	    }
	}
    }

    if($modified) {
	my @arguments = ($hwnd, $msg, $wparam, $lparam);
	return "$name(" . join(", ", @arguments) . ")";
    } else {
	return undef;
    }
}

########################################################################
# fixup_statements

275
sub fixup_statements($$) {
276 277 278
    my $function = shift;
    my $editor = shift;

279
    my $file = $function->file;
280
    my $linkage = $function->linkage;
281
    my $name = $function->name;
282
    my $statements_line = $function->statements_line;
283
    my $statements_column = $function->statements_column;
284
    my $statements = $function->statements;
285

286
    if(!defined($statements)) {
287 288 289
	return;
    }

290
    my $parser = new c_parser($file);
291

292 293 294 295 296 297 298
    my $found_function_call = sub {
	my $begin_line = shift;
	my $begin_column = shift;
	my $end_line = shift;
	my $end_column = shift;
	my $name = shift;
	my $arguments = shift;
299

300 301 302
	foreach my $argument (@$arguments) {
	    $argument =~ s/^\s*(.*?)\s*$/$1/;
	}
303

304 305 306 307 308 309 310 311
	my $fixup_function_call;
	if($name =~ /^(?:DefWindowProc|SendMessage)[AW]$/)
	{
	    if($options->statements_windowsx) {
		$fixup_function_call = \&fixup_user_message_2_windowsx;
	    } else {
		$fixup_function_call = \&_fixup_user_message;
	    }
312
	}
313 314 315

	if(defined($fixup_function_call)) {
	    my $replace = &$fixup_function_call($name, $arguments);
316

317 318 319
	    if(defined($replace)) {
		$editor->replace($begin_line, $begin_column, $end_line, $end_column, $replace);
	    }
320
	} elsif($options->debug) {
321 322 323 324 325 326
	    $output->write("$begin_line.$begin_column-$end_line.$end_column: " .
			   "$name(" . join(", ", @$arguments) . ")\n");
	}

	return 0;
    };
327

328
    $parser->set_found_function_call_callback($found_function_call);
329

330
    my $line = $statements_line;
331
    my $column = 0;
332 333
    if(!$parser->parse_c_statements(\$statements, \$line, \$column)) {
	$output->write("error: can't parse statements\n");
334 335 336 337
    }
}

1;